[pitivi] Fix the factory cache in singledecodebin.



commit 6c019612aa4e9a3ba7c3c74da5d535baa100ad81
Author: Alessandro Decina <alessandro d gmail com>
Date:   Fri Sep 4 18:29:29 2009 +0200

    Fix the factory cache in singledecodebin.

 pitivi/elements/singledecodebin.py |   39 +++++++++++++----------------------
 pitivi/utils.py                    |   27 ++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 24 deletions(-)
---
diff --git a/pitivi/elements/singledecodebin.py b/pitivi/elements/singledecodebin.py
index a6db1aa..addb42a 100644
--- a/pitivi/elements/singledecodebin.py
+++ b/pitivi/elements/singledecodebin.py
@@ -26,6 +26,7 @@ Single-stream queue-less decodebin
 import gobject
 import gst
 from pitivi.stream import get_pad_id, pad_compatible_stream
+from pitivi.utils import CachedFactoryList
 
 def is_raw(caps):
     """ returns True if the caps are RAW """
@@ -36,6 +37,19 @@ def is_raw(caps):
             return True
     return False
 
+def factoryFilter(factory):
+    if factory.get_rank() < 64 :
+        return False
+
+    klass = factory.get_klass()
+    for cat in ("Demuxer", "Decoder", "Parse"):
+        if cat in klass:
+            return True
+
+    return False
+
+_factoryCache = CachedFactoryList(factoryFilter)
+
 class SingleDecodeBin(gst.Bin):
     """
     A variant of decodebin.
@@ -57,8 +71,6 @@ class SingleDecodeBin(gst.Bin):
                          gst.caps_new_any())
         )
 
-    _factories = []
-
     def __init__(self, caps=None, uri=None, stream=None, *args, **kwargs):
         gst.Bin.__init__(self, *args, **kwargs)
         if not caps:
@@ -91,9 +103,6 @@ class SingleDecodeBin(gst.Bin):
 
         self._validelements = [] #added elements
 
-        if self._factories == []:
-            self._factories = self._getSortedFactoryList()
-
         self.debug("stream:%r" % self.stream)
 
         self.pending_newsegment = False
@@ -107,24 +116,6 @@ class SingleDecodeBin(gst.Bin):
         element.connect("pad-added", self._dynamicPadAddedCb)
         element.connect("no-more-pads", self._dynamicNoMorePadsCb)
 
-    def _getSortedFactoryList(self):
-        """
-        Returns the list of demuxers, decoders and parsers available, sorted
-        by rank
-        """
-        self.debug("getting factory list")
-        def _myfilter(fact):
-            if fact.get_rank() < 64 :
-                return False
-            klass = fact.get_klass()
-            if not ("Demuxer" in klass or "Decoder" in klass or "Parse" in klass):
-                return False
-            return True
-        reg = gst.registry_get_default()
-        res = [x for x in reg.get_feature_list(gst.ElementFactory) if _myfilter(x)]
-        res.sort(lambda a, b: int(b.get_rank() - a.get_rank()))
-        return res
-
     def _findCompatibleFactory(self, caps):
         """
         Returns a list of factories (sorted by rank) which can take caps as
@@ -132,7 +123,7 @@ class SingleDecodeBin(gst.Bin):
         """
         self.debug("caps:%s" % caps.to_string())
         res = []
-        for factory in self._factories:
+        for factory in _factoryCache.get():
             for template in factory.get_static_pad_templates():
                 if template.direction == gst.PAD_SINK:
                     intersect = caps.intersect(template.static_caps.get())
diff --git a/pitivi/utils.py b/pitivi/utils.py
index 02db982..c3744f6 100644
--- a/pitivi/utils.py
+++ b/pitivi/utils.py
@@ -4,6 +4,7 @@
 #       utils.py
 #
 # Copyright (c) 2005, Edward Hervey <bilboed bilboed com>
+# Copyright (c) 2009, Alessandro Decina <alessandro d gmail com>
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -430,3 +431,29 @@ def getNextObject(obj, objects, priority=-1, skip=None):
         next_obj_index += 1
 
     return None
+
+class CachedFactoryList(object):
+    def __init__(self, factoryFilter=None):
+        self._factoryFilter = factoryFilter
+        self._factories = None
+        self._registry = gst.registry_get_default()
+        self._registry.connect("feature-added", self._registryFeatureAddedCb)
+
+    def get(self):
+        if self._factories is None:
+            self._buildFactories()
+
+        return self._factories
+
+    def _buildFactories(self):
+        # build the cache
+        factories = self._registry.get_feature_list(gst.ElementFactory)
+        if self._factoryFilter is not None:
+            factories = filter(self._factoryFilter, factories)
+
+        factories.sort(key=lambda factory: factory.get_rank())
+        self._factories = factories
+
+    def _registryFeatureAddedCb(self, registry, feature):
+        # invalidate the cache
+        self._factories = None



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