[pitivi] viewer: Cache the trim preview pipelines



commit ec9ca33bae2279d4e8cf1e6b6dfc1c0d3e10dcc7
Author: Alexandru Băluț <alexandru balut gmail com>
Date:   Sun Jan 20 03:25:52 2019 +0100

    viewer: Cache the trim preview pipelines
    
    Setting a trim preview pipeline to PAUSED can take some time.
    The trim preview apears faster when the cache is hit.

 pitivi/viewer/viewer.py | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
---
diff --git a/pitivi/viewer/viewer.py b/pitivi/viewer/viewer.py
index c6d6a67e..82d2ba2a 100644
--- a/pitivi/viewer/viewer.py
+++ b/pitivi/viewer/viewer.py
@@ -16,6 +16,7 @@
 # License along with this program; if not, write to the
 # Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 # Boston, MA 02110-1301, USA.
+import collections
 from gettext import gettext as _
 
 from gi.repository import Gdk
@@ -84,6 +85,7 @@ class ViewerContainer(Gtk.Box, Loggable):
 
         self.project = None
         self.trim_pipeline = None
+        self.trim_pipelines_cache = collections.OrderedDict()
         self.docked = True
         self.target = None
         self._compactMode = False
@@ -519,9 +521,7 @@ class ViewerContainer(Gtk.Box, Loggable):
             self.trim_pipeline = None
 
         if not self.trim_pipeline:
-            self.debug("Creating temporary pipeline for clip %s", uri)
-            self.trim_pipeline = AssetPipeline(uri)
-            unused_video_sink, sink_widget = self.trim_pipeline.create_sink()
+            self.trim_pipeline, sink_widget = self.get_trim_preview_pipeline(uri)
             # Add the widget to a hidden container and make it appear later
             # when it's ready. If we show it before the initial seek completion,
             # there is a flicker when the first frame of the asset is shown for
@@ -540,6 +540,22 @@ class ViewerContainer(Gtk.Box, Loggable):
 
         self.trim_pipeline.simple_seek(position)
 
+    def get_trim_preview_pipeline(self, uri):
+        try:
+            trim_pipeline, sink_widget = self.trim_pipelines_cache[uri]
+            self.debug("Reusing temporary pipeline for clip %s", uri)
+        except KeyError:
+            self.debug("Creating temporary pipeline for clip %s", uri)
+            trim_pipeline = AssetPipeline(uri)
+            unused_video_sink, sink_widget = trim_pipeline.create_sink()
+        self.trim_pipelines_cache[uri] = trim_pipeline, sink_widget
+        if len(self.trim_pipelines_cache) > 4:
+            # Pop the first inserted item.
+            expired_uri, (expired_pipeline, unused_expired_widget) = 
self.trim_pipelines_cache.popitem(last=False)
+            self.debug("Releasing temporary pipeline for clip %s", expired_uri)
+            expired_pipeline.release()
+        return trim_pipeline, sink_widget
+
     def _state_change_cb(self, trim_pipeline, state, prev_state):
         if self.trim_pipeline is not trim_pipeline:
             self.warning("State change reported for previous trim preview pipeline")
@@ -559,7 +575,6 @@ class ViewerContainer(Gtk.Box, Loggable):
         if not self.trim_pipeline:
             return
         self.target.switch_widget(self.overlay_stack)
-        self.trim_pipeline.release()
         self.trim_pipeline = None
 
     def _pipelineStateChangedCb(self, pipeline, state, old_state):


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