[shotwell] Port thumbnailer spawning code to Subprocess



commit cbe5c44717c59658e144732dca5bcabe1bc4665f
Author: Jens Georg <mail jensge org>
Date:   Mon Jan 13 16:36:07 2020 +0100

    Port thumbnailer spawning code to Subprocess

 src/VideoSupport.vala | 51 ++++++++++++++++++---------------------------------
 1 file changed, 18 insertions(+), 33 deletions(-)
---
diff --git a/src/VideoSupport.vala b/src/VideoSupport.vala
index 533dfe67..91f06330 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,47 +242,36 @@ 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);
-            buf = new Gdk.Pixbuf.from_stream(unix_input, null);
+            Bytes pixbuf_bytes;
+            thumbnailer_process.communicate(null, null, out pixbuf_bytes, null);
+            var loader = new Gdk.PixbufLoader.with_type("png");
+            loader.write_bytes(pixbuf_bytes);
+            loader.close();
+            buf = loader.get_pixbuf();
         } catch (Error e) {
             debug("Error creating pixbuf: %s", e.message);
             buf = null;
         }
         
-        // 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;
-        }
-        
-        GLib.Process.close_pid(thumbnailer_pid);
-        thumbnailer_pid = 0;
+        thumbnailer_process = null;
+        Source.remove (timeout);
+
         return buf;
     }
     


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]