>From 8a7c7df77122f60ce5f11a3bae6976b62fac5197 Mon Sep 17 00:00:00 2001
From: Jens Georg <jens georg desy de>
Date: Mon, 13 Jan 2020 16:22:31 +0100
Subject: [PATCH] Port thumbnailer spawning code to Subprocess

---
 src/VideoSupport.vala | 53 +++++++++++++++++++------------------------
 1 file changed, 23 insertions(+), 30 deletions(-)

diff --git a/src/VideoSupport.vala b/src/VideoSupport.vala
index 533dfe67..0f7dd3c4 100644
--- a/src/VideoSupport.vala
+++ b/src/VideoSupport.vala
@@ -45,7 +45,7 @@ public class VideoReader {
     private double clip_duration = UNKNOWN_CLIP_DURATION;
     private Gdk.Pixbuf preview_frame = null;
     private File file = null;
-    private GLib.Pid thumbnailer_pid = 0;
+    private Subprocess? thumbnailer_process = null;
     public DateTime? timestamp { get; private set; default = null; }
 
     public VideoReader(File file) {
@@ -230,14 +230,10 @@ public class VideoReader {
     // Used by thumbnailer() to kill the external process if need be.
     private bool on_thumbnailer_timer() {
         debug("Thumbnailer timer called");
-        if (thumbnailer_pid != 0) {
-            debug("Killing thumbnailer process: %d", thumbnailer_pid);
-#if VALA_0_40
-            Posix.kill(thumbnailer_pid, Posix.Signal.KILL);
-#else
-            Posix.kill(thumbnailer_pid, Posix.SIGKILL);
-#endif
+        if (thumbnailer_process != null) {
+            thumbnailer_process.force_exit();
         }
+
         return false; // Don't call again.
     }
     
@@ -246,27 +242,25 @@ public class VideoReader {
     private Gdk.Pixbuf? thumbnailer(string video_file) {
         // Use Shotwell's thumbnailer, redirect output to stdout.
         debug("Launching thumbnailer process: %s", AppDirs.get_thumbnailer_bin().get_path());
-        string[] argv = {AppDirs.get_thumbnailer_bin().get_path(), video_file};
-        int child_stdout;
         try {
-            GLib.Process.spawn_async_with_pipes(null, argv, null, GLib.SpawnFlags.SEARCH_PATH | 
-                GLib.SpawnFlags.DO_NOT_REAP_CHILD, null, out thumbnailer_pid, null, out child_stdout,
-                null);
-            debug("Spawned thumbnailer, child pid: %d", (int) thumbnailer_pid);
+            thumbnailer_process = new GLib.Subprocess(SubprocessFlags.STDOUT_PIPE,
+                                          AppDirs.get_thumbnailer_bin().get_path(),
+                                          video_file);
+            debug("Spawned thumbnailer, child id: %s", thumbnailer_process.get_identifier());
         } catch (Error e) {
             debug("Error spawning process: %s", e.message);
-            if (thumbnailer_pid != 0)
-                GLib.Process.close_pid(thumbnailer_pid);
             return null;
         }
         
         // Start timer.
-        Timeout.add(THUMBNAILER_TIMEOUT, on_thumbnailer_timer);
+        var timeout = Timeout.add(THUMBNAILER_TIMEOUT, on_thumbnailer_timer);
         
         // Read pixbuf from stream.
         Gdk.Pixbuf? buf = null;
         try {
-            GLib.UnixInputStream unix_input = new GLib.UnixInputStream(child_stdout, true);
+            Bytes pixbuf_bytes;
+            thumbnailer_process.communicate(null, null, out pixbuf_bytes, null);
+            var unix_input = new GLib.MemoryInputStream.from_bytes(pixbuf_bytes);
             buf = new Gdk.Pixbuf.from_stream(unix_input, null);
         } catch (Error e) {
             debug("Error creating pixbuf: %s", e.message);
@@ -274,19 +268,18 @@ public class VideoReader {
         }
         
         // Make sure process exited properly.
-        int child_status = 0;
-        int ret_waitpid = Posix.waitpid(thumbnailer_pid, out child_status, 0);
-        if (ret_waitpid < 0) {
-            debug("waitpid returned error code: %d", ret_waitpid);
-            buf = null;
-        } else if (0 != Process.exit_status(child_status)) {
-            debug("Thumbnailer exited with error code: %d",
-                    Process.exit_status(child_status));
-            buf = null;
+        try {
+            if (!thumbnailer_process.wait_check(null)) {
+                debug("wait_check failed");
+                buf = null;
+            }
+        } catch (Error error) {
+            debug("thumbnailer exited with error %s", error.message);
         }
-        
-        GLib.Process.close_pid(thumbnailer_pid);
-        thumbnailer_pid = 0;
+
+        thumbnailer_process = null;
+        Source.remove (timeout);
+
         return buf;
     }
     
-- 
2.17.1