]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - gst/gstregistry.c
gststructure: early out when we know a value cannot be a subset
[glsdk/gstreamer0-10.git] / gst / gstregistry.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2005 David A. Schleef <ds@schleef.org>
5  *
6  * gstregistry.c: handle registry
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
24 /**
25  * SECTION:gstregistry
26  * @short_description: Abstract base class for management of #GstPlugin objects
27  * @see_also: #GstPlugin, #GstPluginFeature
28  *
29  * One registry holds the metadata of a set of plugins.
30  *
31  * <emphasis role="bold">Design:</emphasis>
32  *
33  * The #GstRegistry object is a list of plugins and some functions for dealing
34  * with them. Each #GstPlugin is matched 1-1 with a file on disk, and may or may
35  * not be loaded at a given time. There may be multiple #GstRegistry objects,
36  * but the "default registry" is the only object that has any meaning to the
37  * core.
38  *
39  * The registry file is actually a cache of plugin information. This is
40  * unlike versions prior to 0.10, where the registry file was the primary source
41  * of plugin information, and was created by the gst-register command.
42  *
43  * The primary source, at all times, of plugin information is each plugin file
44  * itself. Thus, if an application wants information about a particular plugin,
45  * or wants to search for a feature that satisfies given criteria, the primary
46  * means of doing so is to load every plugin and look at the resulting
47  * information that is gathered in the default registry. Clearly, this is a time
48  * consuming process, so we cache information in the registry file. The format
49  * and location of the cache file is internal to gstreamer.
50  *
51  * On startup, plugins are searched for in the plugin search path. The following
52  * locations are checked in this order:
53  * <itemizedlist>
54  *   <listitem>
55  *     <para>location from --gst-plugin-path commandline option.</para>
56  *   </listitem>
57  *   <listitem>
58  *     <para>the GST_PLUGIN_PATH environment variable.</para>
59  *   </listitem>
60  *   <listitem>
61  *     <para>the GST_PLUGIN_SYSTEM_PATH environment variable.</para>
62  *   </listitem>
63  *   <listitem>
64  *     <para>default locations (if GST_PLUGIN_SYSTEM_PATH is not set). Those
65  *       default locations are:
66  *       <filename>~/.gstreamer-$GST_MAJORMINOR/plugins/</filename>
67  *       and <filename>$prefix/libs/gstreamer-$GST_MAJORMINOR/</filename>.
68  *     </para>
69  *   </listitem>
70  * </itemizedlist>
71  * The registry cache file is loaded from
72  * <filename>~/.gstreamer-$GST_MAJORMINOR/registry-$ARCH.bin</filename> or the
73  * file listed in the GST_REGISTRY env var. One reason to change the registry
74  * location is for testing.
75  *
76  * For each plugin that is found in the plugin search path, there could be 3
77  * possibilities for cached information:
78  * <itemizedlist>
79  *   <listitem>
80  *     <para>the cache may not contain information about a given file.</para>
81  *   </listitem>
82  *   <listitem>
83  *     <para>the cache may have stale information.</para>
84  *   </listitem>
85  *   <listitem>
86  *     <para>the cache may have current information.</para>
87  *   </listitem>
88  * </itemizedlist>
89  *
90  * In the first two cases, the plugin is loaded and the cache updated. In
91  * addition to these cases, the cache may have entries for plugins that are not
92  * relevant to the current process. These are marked as not available to the
93  * current process. If the cache is updated for whatever reason, it is marked
94  * dirty.
95  *
96  * A dirty cache is written out at the end of initialization. Each entry is
97  * checked to make sure the information is minimally valid. If not, the entry is
98  * simply dropped.
99  *
100  * <emphasis role="bold">Implementation notes:</emphasis>
101  *
102  * The "cache" and "default registry" are different concepts and can represent
103  * different sets of plugins. For various reasons, at init time, the cache is
104  * stored in the default registry, and plugins not relevant to the current
105  * process are marked with the %GST_PLUGIN_FLAG_CACHED bit. These plugins are
106  * removed at the end of initialization.
107  */
109 #ifdef HAVE_CONFIG_H
110 #include "config.h"
111 #endif
112 #include "gstconfig.h"
113 #include "gst_private.h"
114 #include <glib.h>
115 #include <sys/types.h>
116 #include <sys/stat.h>
117 #ifdef HAVE_UNISTD_H
118 #include <unistd.h>
119 #endif
120 #include <errno.h>
121 #include <stdio.h>
122 #include <string.h>
124 /* For g_stat () */
125 #include <glib/gstdio.h>
127 #include "gstinfo.h"
128 #include "gsterror.h"
129 #include "gstregistry.h"
130 #include "gstmarshal.h"
131 #include "gstfilter.h"
133 #include "gstpluginloader.h"
135 #include "gst-i18n-lib.h"
137 #include "gst.h"
138 #include "glib-compat-private.h"
140 #ifdef G_OS_WIN32
141 #include <windows.h>
142 extern HMODULE _priv_gst_dll_handle;
143 #endif
145 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
147 struct _GstRegistryPrivate
149   /* updated whenever the feature list changes */
150   guint32 cookie;
151   /* speedup for searching features */
152   GList *element_factory_list;
153   guint32 efl_cookie;
154   GList *typefind_factory_list;
155   guint32 tfl_cookie;
156 };
158 /* the one instance of the default registry and the mutex protecting the
159  * variable. */
160 static GStaticMutex _gst_registry_mutex = G_STATIC_MUTEX_INIT;
161 static GstRegistry *_gst_registry_default = NULL;
163 /* defaults */
164 #define DEFAULT_FORK TRUE
166 /* control the behaviour of registry rebuild */
167 static gboolean _gst_enable_registry_fork = DEFAULT_FORK;
168 /* List of plugins that need preloading/reloading after scanning registry */
169 extern GSList *_priv_gst_preload_plugins;
171 #ifndef GST_DISABLE_REGISTRY
172 /*set to TRUE when registry needn't to be updated */
173 gboolean _priv_gst_disable_registry_update = FALSE;
174 extern GList *_priv_gst_plugin_paths;
176 /* Set to TRUE when the registry cache should be disabled */
177 gboolean _gst_disable_registry_cache = FALSE;
179 static gboolean __registry_reuse_plugin_scanner = TRUE;
180 #endif
182 /* Element signals and args */
183 enum
185   PLUGIN_ADDED,
186   FEATURE_ADDED,
187   LAST_SIGNAL
188 };
190 static void gst_registry_finalize (GObject * object);
192 static guint gst_registry_signals[LAST_SIGNAL] = { 0 };
194 static GstPluginFeature *gst_registry_lookup_feature_locked (GstRegistry *
195     registry, const char *name);
196 static GstPlugin *gst_registry_lookup_bn_locked (GstRegistry * registry,
197     const char *basename);
199 G_DEFINE_TYPE (GstRegistry, gst_registry, GST_TYPE_OBJECT);
200 static GstObjectClass *parent_class = NULL;
202 static void
203 gst_registry_class_init (GstRegistryClass * klass)
205   GObjectClass *gobject_class;
207   gobject_class = (GObjectClass *) klass;
209   parent_class = g_type_class_peek_parent (klass);
210   g_type_class_add_private (klass, sizeof (GstRegistryPrivate));
212   /**
213    * GstRegistry::plugin-added:
214    * @registry: the registry that emitted the signal
215    * @plugin: the plugin that has been added
216    *
217    * Signals that a plugin has been added to the registry (possibly
218    * replacing a previously-added one by the same name)
219    */
220   gst_registry_signals[PLUGIN_ADDED] =
221       g_signal_new ("plugin-added", G_TYPE_FROM_CLASS (klass),
222       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, plugin_added), NULL,
223       NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
225   /**
226    * GstRegistry::feature-added:
227    * @registry: the registry that emitted the signal
228    * @feature: the feature that has been added
229    *
230    * Signals that a feature has been added to the registry (possibly
231    * replacing a previously-added one by the same name)
232    */
233   gst_registry_signals[FEATURE_ADDED] =
234       g_signal_new ("feature-added", G_TYPE_FROM_CLASS (klass),
235       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, feature_added),
236       NULL, NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
238   gobject_class->finalize = gst_registry_finalize;
241 static void
242 gst_registry_init (GstRegistry * registry)
244   registry->feature_hash = g_hash_table_new (g_str_hash, g_str_equal);
245   registry->basename_hash = g_hash_table_new (g_str_hash, g_str_equal);
246   registry->priv =
247       G_TYPE_INSTANCE_GET_PRIVATE (registry, GST_TYPE_REGISTRY,
248       GstRegistryPrivate);
251 static void
252 gst_registry_finalize (GObject * object)
254   GstRegistry *registry = GST_REGISTRY (object);
255   GList *plugins, *p;
256   GList *features, *f;
258   plugins = registry->plugins;
259   registry->plugins = NULL;
261   GST_DEBUG_OBJECT (registry, "registry finalize");
262   p = plugins;
263   while (p) {
264     GstPlugin *plugin = p->data;
266     if (plugin) {
267       GST_LOG_OBJECT (registry, "removing plugin %s",
268           gst_plugin_get_name (plugin));
269       gst_object_unref (plugin);
270     }
271     p = g_list_next (p);
272   }
273   g_list_free (plugins);
275   features = registry->features;
276   registry->features = NULL;
278   f = features;
279   while (f) {
280     GstPluginFeature *feature = f->data;
282     if (feature) {
283       GST_LOG_OBJECT (registry, "removing feature %p (%s)",
284           feature, gst_plugin_feature_get_name (feature));
285       gst_object_unparent (GST_OBJECT_CAST (feature));
286     }
287     f = g_list_next (f);
288   }
289   g_list_free (features);
291   g_hash_table_destroy (registry->feature_hash);
292   registry->feature_hash = NULL;
293   g_hash_table_destroy (registry->basename_hash);
294   registry->basename_hash = NULL;
296   if (registry->priv->element_factory_list) {
297     GST_DEBUG_OBJECT (registry, "Cleaning up cached element factory list");
298     gst_plugin_feature_list_free (registry->priv->element_factory_list);
299   }
301   if (registry->priv->typefind_factory_list) {
302     GST_DEBUG_OBJECT (registry, "Cleaning up cached typefind factory list");
303     gst_plugin_feature_list_free (registry->priv->typefind_factory_list);
304   }
306   G_OBJECT_CLASS (parent_class)->finalize (object);
309 /**
310  * gst_registry_get_default:
311  *
312  * Retrieves the default registry. The caller does not own a reference on the
313  * registry, as it is alive as long as GStreamer is initialized.
314  *
315  * Returns: (transfer none): The default #GstRegistry.
316  */
317 GstRegistry *
318 gst_registry_get_default (void)
320   GstRegistry *registry;
322   g_static_mutex_lock (&_gst_registry_mutex);
323   if (G_UNLIKELY (!_gst_registry_default)) {
324     _gst_registry_default = g_object_newv (GST_TYPE_REGISTRY, 0, NULL);
325     gst_object_ref_sink (GST_OBJECT_CAST (_gst_registry_default));
326   }
327   registry = _gst_registry_default;
328   g_static_mutex_unlock (&_gst_registry_mutex);
330   return registry;
333 /**
334  * gst_registry_add_path:
335  * @registry: the registry to add the path to
336  * @path: the path to add to the registry
337  *
338  * Add the given path to the registry. The syntax of the
339  * path is specific to the registry. If the path has already been
340  * added, do nothing.
341  */
342 void
343 gst_registry_add_path (GstRegistry * registry, const gchar * path)
345   g_return_if_fail (GST_IS_REGISTRY (registry));
346   g_return_if_fail (path != NULL);
348   if (strlen (path) == 0)
349     goto empty_path;
351   GST_OBJECT_LOCK (registry);
352   if (g_list_find_custom (registry->paths, path, (GCompareFunc) strcmp))
353     goto was_added;
355   GST_INFO ("Adding plugin path: \"%s\"", path);
356   registry->paths = g_list_append (registry->paths, g_strdup (path));
357   GST_OBJECT_UNLOCK (registry);
359   return;
361 empty_path:
362   {
363     GST_INFO ("Ignoring empty plugin path");
364     return;
365   }
366 was_added:
367   {
368     g_warning ("path %s already added to registry", path);
369     GST_OBJECT_UNLOCK (registry);
370     return;
371   }
374 /**
375  * gst_registry_get_path_list:
376  * @registry: the registry to get the pathlist of
377  *
378  * Get the list of paths for the given registry.
379  *
380  * Returns: (transfer container) (element-type char*): A #GList of paths as
381  *     strings. g_list_free after use.
382  *
383  * MT safe.
384  */
385 GList *
386 gst_registry_get_path_list (GstRegistry * registry)
388   GList *list;
390   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
392   GST_OBJECT_LOCK (registry);
393   /* We don't need to copy the strings, because they won't be deleted
394    * as long as the GstRegistry is around */
395   list = g_list_copy (registry->paths);
396   GST_OBJECT_UNLOCK (registry);
398   return list;
402 /**
403  * gst_registry_add_plugin:
404  * @registry: the registry to add the plugin to
405  * @plugin: (transfer full): the plugin to add
406  *
407  * Add the plugin to the registry. The plugin-added signal will be emitted.
408  * This function will sink @plugin.
409  *
410  * Returns: TRUE on success.
411  *
412  * MT safe.
413  */
414 gboolean
415 gst_registry_add_plugin (GstRegistry * registry, GstPlugin * plugin)
417   GstPlugin *existing_plugin;
419   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
420   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
422   GST_OBJECT_LOCK (registry);
423   if (G_LIKELY (plugin->basename)) {
424     /* we have a basename, see if we find the plugin */
425     existing_plugin =
426         gst_registry_lookup_bn_locked (registry, plugin->basename);
427     if (existing_plugin) {
428       GST_DEBUG_OBJECT (registry,
429           "Replacing existing plugin \"%s\" %p with new plugin %p for filename \"%s\"",
430           GST_STR_NULL (existing_plugin->filename), existing_plugin, plugin,
431           GST_STR_NULL (plugin->filename));
432       /* If the new plugin is blacklisted and the existing one isn't cached, do not
433        * accept if it's from a different location than the existing one */
434       if ((plugin->flags & GST_PLUGIN_FLAG_BLACKLISTED) &&
435           strcmp (plugin->filename, existing_plugin->filename)) {
436         GST_WARNING_OBJECT (registry,
437             "Not replacing plugin because new one (%s) is blacklisted but for a different location than existing one (%s)",
438             plugin->filename, existing_plugin->filename);
439         gst_object_unref (plugin);
440         GST_OBJECT_UNLOCK (registry);
441         return FALSE;
442       }
443       registry->plugins = g_list_remove (registry->plugins, existing_plugin);
444       if (G_LIKELY (existing_plugin->basename))
445         g_hash_table_remove (registry->basename_hash,
446             existing_plugin->basename);
447       gst_object_unref (existing_plugin);
448     }
449   }
451   GST_DEBUG_OBJECT (registry, "adding plugin %p for filename \"%s\"",
452       plugin, GST_STR_NULL (plugin->filename));
454   registry->plugins = g_list_prepend (registry->plugins, plugin);
455   if (G_LIKELY (plugin->basename))
456     g_hash_table_replace (registry->basename_hash, plugin->basename, plugin);
458   gst_object_ref_sink (plugin);
459   GST_OBJECT_UNLOCK (registry);
461   GST_LOG_OBJECT (registry, "emitting plugin-added for filename \"%s\"",
462       GST_STR_NULL (plugin->filename));
463   g_signal_emit (registry, gst_registry_signals[PLUGIN_ADDED], 0, plugin);
465   return TRUE;
468 static void
469 gst_registry_remove_features_for_plugin_unlocked (GstRegistry * registry,
470     GstPlugin * plugin)
472   GList *f;
474   g_return_if_fail (GST_IS_REGISTRY (registry));
475   g_return_if_fail (GST_IS_PLUGIN (plugin));
477   /* Remove all features for this plugin */
478   f = registry->features;
479   while (f != NULL) {
480     GList *next = g_list_next (f);
481     GstPluginFeature *feature = f->data;
483     if (G_UNLIKELY (feature && feature->plugin == plugin)) {
484       GST_DEBUG_OBJECT (registry, "removing feature %p (%s) for plugin %p (%s)",
485           feature, gst_plugin_feature_get_name (feature), plugin,
486           plugin->desc.name);
488       registry->features = g_list_delete_link (registry->features, f);
489       g_hash_table_remove (registry->feature_hash, feature->name);
490       gst_object_unparent (GST_OBJECT_CAST (feature));
491     }
492     f = next;
493   }
494   registry->priv->cookie++;
497 /**
498  * gst_registry_remove_plugin:
499  * @registry: the registry to remove the plugin from
500  * @plugin: (transfer none): the plugin to remove
501  *
502  * Remove the plugin from the registry.
503  *
504  * MT safe.
505  */
506 void
507 gst_registry_remove_plugin (GstRegistry * registry, GstPlugin * plugin)
509   g_return_if_fail (GST_IS_REGISTRY (registry));
510   g_return_if_fail (GST_IS_PLUGIN (plugin));
512   GST_DEBUG_OBJECT (registry, "removing plugin %p (%s)",
513       plugin, gst_plugin_get_name (plugin));
515   GST_OBJECT_LOCK (registry);
516   registry->plugins = g_list_remove (registry->plugins, plugin);
517   if (G_LIKELY (plugin->basename))
518     g_hash_table_remove (registry->basename_hash, plugin->basename);
519   gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
520   GST_OBJECT_UNLOCK (registry);
521   gst_object_unref (plugin);
524 /**
525  * gst_registry_add_feature:
526  * @registry: the registry to add the plugin to
527  * @feature: (transfer full): the feature to add
528  *
529  * Add the feature to the registry. The feature-added signal will be emitted.
530  * This function sinks @feature.
531  *
532  * Returns: TRUE on success.
533  *
534  * MT safe.
535  */
536 gboolean
537 gst_registry_add_feature (GstRegistry * registry, GstPluginFeature * feature)
539   GstPluginFeature *existing_feature;
541   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
542   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
543   g_return_val_if_fail (feature->name != NULL, FALSE);
544   g_return_val_if_fail (feature->plugin_name != NULL, FALSE);
546   GST_OBJECT_LOCK (registry);
547   existing_feature = gst_registry_lookup_feature_locked (registry,
548       feature->name);
549   if (G_UNLIKELY (existing_feature)) {
550     GST_DEBUG_OBJECT (registry, "replacing existing feature %p (%s)",
551         existing_feature, feature->name);
552     /* Remove the existing feature from the list now, before we insert the new
553      * one, but don't unref yet because the hash is still storing a reference to
554      * it. */
555     registry->features = g_list_remove (registry->features, existing_feature);
556   }
558   GST_DEBUG_OBJECT (registry, "adding feature %p (%s)", feature, feature->name);
560   registry->features = g_list_prepend (registry->features, feature);
561   g_hash_table_replace (registry->feature_hash, feature->name, feature);
563   if (G_UNLIKELY (existing_feature)) {
564     /* We unref now. No need to remove the feature name from the hash table, it
565      * got replaced by the new feature */
566     gst_object_unparent (GST_OBJECT_CAST (existing_feature));
567   }
569   gst_object_set_parent (GST_OBJECT_CAST (feature), GST_OBJECT_CAST (registry));
571   registry->priv->cookie++;
572   GST_OBJECT_UNLOCK (registry);
574   GST_LOG_OBJECT (registry, "emitting feature-added for %s", feature->name);
575   g_signal_emit (registry, gst_registry_signals[FEATURE_ADDED], 0, feature);
577   return TRUE;
580 /**
581  * gst_registry_remove_feature:
582  * @registry: the registry to remove the feature from
583  * @feature: (transfer none): the feature to remove
584  *
585  * Remove the feature from the registry.
586  *
587  * MT safe.
588  */
589 void
590 gst_registry_remove_feature (GstRegistry * registry, GstPluginFeature * feature)
592   g_return_if_fail (GST_IS_REGISTRY (registry));
593   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
595   GST_DEBUG_OBJECT (registry, "removing feature %p (%s)",
596       feature, gst_plugin_feature_get_name (feature));
598   GST_OBJECT_LOCK (registry);
599   registry->features = g_list_remove (registry->features, feature);
600   g_hash_table_remove (registry->feature_hash, feature->name);
601   registry->priv->cookie++;
602   GST_OBJECT_UNLOCK (registry);
604   gst_object_unparent ((GstObject *) feature);
607 /**
608  * gst_registry_plugin_filter:
609  * @registry: registry to query
610  * @filter: (scope call): the filter to use
611  * @first: only return first match
612  * @user_data: (closure): user data passed to the filter function
613  *
614  * Runs a filter against all plugins in the registry and returns a #GList with
615  * the results. If the first flag is set, only the first match is
616  * returned (as a list with a single object).
617  * Every plugin is reffed; use gst_plugin_list_free() after use, which
618  * will unref again.
619  *
620  * Returns: (transfer full) (element-type Gst.Plugin): a #GList of #GstPlugin.
621  *     Use gst_plugin_list_free() after usage.
622  *
623  * MT safe.
624  */
625 GList *
626 gst_registry_plugin_filter (GstRegistry * registry,
627     GstPluginFilter filter, gboolean first, gpointer user_data)
629   GList *list = NULL;
631   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
633   GST_OBJECT_LOCK (registry);
634   {
635     const GList *walk;
637     for (walk = registry->plugins; walk != NULL; walk = walk->next) {
638       GstPlugin *plugin = walk->data;
640       if (filter == NULL || filter (plugin, user_data)) {
641         list = g_list_prepend (list, gst_object_ref (plugin));
643         if (first)
644           break;
645       }
646     }
647   }
648   GST_OBJECT_UNLOCK (registry);
650   return list;
653 #ifdef GST_DISABLE_DEPRECATED
654 typedef struct
656   const gchar *name;
657   GType type;
658 } GstTypeNameData;
659 static gboolean
660 gst_plugin_feature_type_name_filter (GstPluginFeature * feature,
661     GstTypeNameData * data)
663   g_assert (GST_IS_PLUGIN_FEATURE (feature));
665   return ((data->type == 0 || data->type == G_OBJECT_TYPE (feature)) &&
666       (data->name == NULL
667           || !strcmp (data->name, GST_PLUGIN_FEATURE_NAME (feature))));
669 #endif
671 /* returns TRUE if the list was changed
672  *
673  * Must be called with the object lock taken */
674 static gboolean
675 gst_registry_get_feature_list_or_create (GstRegistry * registry,
676     GList ** previous, guint32 * cookie, GType type)
678   gboolean res = FALSE;
679   GstRegistryPrivate *priv = registry->priv;
681   if (G_UNLIKELY (!*previous || priv->cookie != *cookie)) {
682     GstTypeNameData data;
683     const GList *walk;
685     if (*previous) {
686       gst_plugin_feature_list_free (*previous);
687       *previous = NULL;
688     }
690     data.type = type;
691     data.name = NULL;
693     for (walk = registry->features; walk != NULL; walk = walk->next) {
694       GstPluginFeature *feature = walk->data;
696       if (gst_plugin_feature_type_name_filter (feature, &data)) {
697         *previous = g_list_prepend (*previous, gst_object_ref (feature));
698       }
699     }
701     *cookie = priv->cookie;
702     res = TRUE;
703   }
705   return res;
708 static gint
709 type_find_factory_rank_cmp (const GstPluginFeature * fac1,
710     const GstPluginFeature * fac2)
712   if (G_LIKELY (fac1->rank != fac2->rank))
713     return fac2->rank - fac1->rank;
715   /* to make the order in which things happen more deterministic,
716    * sort by name when the ranks are the same. */
717   return strcmp (fac1->name, fac2->name);
720 static GList *
721 gst_registry_get_element_factory_list (GstRegistry * registry)
723   GList *list;
725   GST_OBJECT_LOCK (registry);
727   gst_registry_get_feature_list_or_create (registry,
728       &registry->priv->element_factory_list, &registry->priv->efl_cookie,
729       GST_TYPE_ELEMENT_FACTORY);
731   /* Return reffed copy */
732   list = gst_plugin_feature_list_copy (registry->priv->element_factory_list);
734   GST_OBJECT_UNLOCK (registry);
736   return list;
739 static GList *
740 gst_registry_get_typefind_factory_list (GstRegistry * registry)
742   GList *list;
744   GST_OBJECT_LOCK (registry);
746   if (G_UNLIKELY (gst_registry_get_feature_list_or_create (registry,
747               &registry->priv->typefind_factory_list,
748               &registry->priv->tfl_cookie, GST_TYPE_TYPE_FIND_FACTORY)))
749     registry->priv->typefind_factory_list =
750         g_list_sort (registry->priv->typefind_factory_list,
751         (GCompareFunc) type_find_factory_rank_cmp);
753   /* Return reffed copy */
754   list = gst_plugin_feature_list_copy (registry->priv->typefind_factory_list);
756   GST_OBJECT_UNLOCK (registry);
758   return list;
761 /**
762  * gst_registry_feature_filter:
763  * @registry: registry to query
764  * @filter: (scope call): the filter to use
765  * @first: only return first match
766  * @user_data: (closure): user data passed to the filter function
767  *
768  * Runs a filter against all features of the plugins in the registry
769  * and returns a GList with the results.
770  * If the first flag is set, only the first match is
771  * returned (as a list with a single object).
772  *
773  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
774  *     #GstPluginFeature. Use gst_plugin_feature_list_free() after usage.
775  *
776  * MT safe.
777  */
778 GList *
779 gst_registry_feature_filter (GstRegistry * registry,
780     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
782   GList *list = NULL;
784   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
786   GST_OBJECT_LOCK (registry);
787   {
788     const GList *walk;
790     for (walk = registry->features; walk != NULL; walk = walk->next) {
791       GstPluginFeature *feature = walk->data;
793       if (filter == NULL || filter (feature, user_data)) {
794         list = g_list_prepend (list, gst_object_ref (feature));
796         if (first)
797           break;
798       }
799     }
800   }
801   GST_OBJECT_UNLOCK (registry);
803   return list;
806 /**
807  * gst_registry_find_plugin:
808  * @registry: the registry to search
809  * @name: the plugin name to find
810  *
811  * Find the plugin with the given name in the registry.
812  * The plugin will be reffed; caller is responsible for unreffing.
813  *
814  * Returns: (transfer full): the plugin with the given name or NULL if the
815  *     plugin was not found. gst_object_unref() after usage.
816  *
817  * MT safe.
818  */
819 GstPlugin *
820 gst_registry_find_plugin (GstRegistry * registry, const gchar * name)
822   GList *walk;
823   GstPlugin *result = NULL;
825   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
826   g_return_val_if_fail (name != NULL, NULL);
828   walk = gst_registry_plugin_filter (registry,
829       (GstPluginFilter) gst_plugin_name_filter, TRUE, (gpointer) name);
830   if (walk) {
831     result = GST_PLUGIN_CAST (walk->data);
833     gst_object_ref (result);
834     gst_plugin_list_free (walk);
835   }
837   return result;
840 /**
841  * gst_registry_find_feature:
842  * @registry: the registry to search
843  * @name: the pluginfeature name to find
844  * @type: the pluginfeature type to find
845  *
846  * Find the pluginfeature with the given name and type in the registry.
847  *
848  * Returns: (transfer full): the pluginfeature with the given name and type
849  *     or NULL if the plugin was not found. gst_object_unref() after usage.
850  *
851  * MT safe.
852  */
853 GstPluginFeature *
854 gst_registry_find_feature (GstRegistry * registry, const gchar * name,
855     GType type)
857   GstPluginFeature *feature = NULL;
859   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
860   g_return_val_if_fail (name != NULL, NULL);
861   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
863   feature = gst_registry_lookup_feature (registry, name);
864   if (feature && !g_type_is_a (G_TYPE_FROM_INSTANCE (feature), type)) {
865     gst_object_unref (feature);
866     feature = NULL;
867   }
869   return feature;
872 /**
873  * gst_registry_get_feature_list:
874  * @registry: a #GstRegistry
875  * @type: a #GType.
876  *
877  * Retrieves a #GList of #GstPluginFeature of @type.
878  *
879  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
880  *     #GstPluginFeature of @type. Use gst_plugin_feature_list_free() after use
881  *
882  * MT safe.
883  */
884 GList *
885 gst_registry_get_feature_list (GstRegistry * registry, GType type)
887   GstTypeNameData data;
889   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
890   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
892   /* Speed up */
893   if (type == GST_TYPE_ELEMENT_FACTORY)
894     return gst_registry_get_element_factory_list (registry);
895   else if (type == GST_TYPE_TYPE_FIND_FACTORY)
896     return gst_registry_get_typefind_factory_list (registry);
898   data.type = type;
899   data.name = NULL;
901   return gst_registry_feature_filter (registry,
902       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
903       FALSE, &data);
906 /**
907  * gst_registry_get_plugin_list:
908  * @registry: the registry to search
909  *
910  * Get a copy of all plugins registered in the given registry. The refcount
911  * of each element in the list in incremented.
912  *
913  * Returns: (transfer full) (element-type Gst.Plugin): a #GList of #GstPlugin.
914  *     Use gst_plugin_list_free() after usage.
915  *
916  * MT safe.
917  */
918 GList *
919 gst_registry_get_plugin_list (GstRegistry * registry)
921   GList *list;
922   GList *g;
924   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
926   GST_OBJECT_LOCK (registry);
927   list = g_list_copy (registry->plugins);
928   for (g = list; g; g = g->next) {
929     gst_object_ref (GST_PLUGIN_CAST (g->data));
930   }
931   GST_OBJECT_UNLOCK (registry);
933   return list;
936 static GstPluginFeature *
937 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
939   return g_hash_table_lookup (registry->feature_hash, name);
942 /**
943  * gst_registry_lookup_feature:
944  * @registry: a #GstRegistry
945  * @name: a #GstPluginFeature name
946  *
947  * Find a #GstPluginFeature with @name in @registry.
948  *
949  * Returns: (transfer full): a #GstPluginFeature with its refcount incremented,
950  *     use gst_object_unref() after usage.
951  *
952  * MT safe.
953  */
954 GstPluginFeature *
955 gst_registry_lookup_feature (GstRegistry * registry, const char *name)
957   GstPluginFeature *feature;
959   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
960   g_return_val_if_fail (name != NULL, NULL);
962   GST_OBJECT_LOCK (registry);
963   feature = gst_registry_lookup_feature_locked (registry, name);
964   if (feature)
965     gst_object_ref (feature);
966   GST_OBJECT_UNLOCK (registry);
968   return feature;
971 static GstPlugin *
972 gst_registry_lookup_bn_locked (GstRegistry * registry, const char *basename)
974   return g_hash_table_lookup (registry->basename_hash, basename);
977 static GstPlugin *
978 gst_registry_lookup_bn (GstRegistry * registry, const char *basename)
980   GstPlugin *plugin;
982   GST_OBJECT_LOCK (registry);
983   plugin = gst_registry_lookup_bn_locked (registry, basename);
984   if (plugin)
985     gst_object_ref (plugin);
986   GST_OBJECT_UNLOCK (registry);
988   return plugin;
991 /**
992  * gst_registry_lookup:
993  * @registry: the registry to look up in
994  * @filename: the name of the file to look up
995  *
996  * Look up a plugin in the given registry with the given filename.
997  * If found, plugin is reffed.
998  *
999  * Returns: (transfer full): the #GstPlugin if found, or NULL if not.
1000  *     gst_object_unref() after usage.
1001  */
1002 GstPlugin *
1003 gst_registry_lookup (GstRegistry * registry, const char *filename)
1005   GstPlugin *plugin;
1006   gchar *basename;
1008   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1009   g_return_val_if_fail (filename != NULL, NULL);
1011   basename = g_path_get_basename (filename);
1012   if (G_UNLIKELY (basename == NULL))
1013     return NULL;
1015   plugin = gst_registry_lookup_bn (registry, basename);
1017   g_free (basename);
1019   return plugin;
1022 typedef enum
1024   REGISTRY_SCAN_HELPER_NOT_STARTED = 0,
1025   REGISTRY_SCAN_HELPER_DISABLED,
1026   REGISTRY_SCAN_HELPER_RUNNING
1027 } GstRegistryScanHelperState;
1029 typedef struct
1031   GstRegistry *registry;
1032   GstRegistryScanHelperState helper_state;
1033   GstPluginLoader *helper;
1034   gboolean changed;
1035 } GstRegistryScanContext;
1037 static void
1038 init_scan_context (GstRegistryScanContext * context, GstRegistry * registry)
1040   gboolean do_fork;
1042   context->registry = registry;
1044   /* see if forking is enabled and set up the scan helper state accordingly */
1045   do_fork = _gst_enable_registry_fork;
1046   if (do_fork) {
1047     const gchar *fork_env;
1049     /* forking enabled, see if it is disabled with an env var */
1050     if ((fork_env = g_getenv ("GST_REGISTRY_FORK"))) {
1051       /* fork enabled for any value different from "no" */
1052       do_fork = strcmp (fork_env, "no") != 0;
1053     }
1054   }
1056   if (do_fork)
1057     context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
1058   else
1059     context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1061   context->helper = NULL;
1062   context->changed = FALSE;
1065 static void
1066 clear_scan_context (GstRegistryScanContext * context)
1068   if (context->helper) {
1069     context->changed |= _priv_gst_plugin_loader_funcs.destroy (context->helper);
1070     context->helper = NULL;
1071   }
1074 static gboolean
1075 gst_registry_scan_plugin_file (GstRegistryScanContext * context,
1076     const gchar * filename, off_t file_size, time_t file_mtime)
1078   gboolean changed = FALSE;
1079   GstPlugin *newplugin = NULL;
1081 #ifdef G_OS_WIN32
1082   /* Disable external plugin loader on Windows until it is ported properly. */
1083   context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1084 #endif
1087   /* Have a plugin to load - see if the scan-helper needs starting */
1088   if (context->helper_state == REGISTRY_SCAN_HELPER_NOT_STARTED) {
1089     GST_DEBUG ("Starting plugin scanner for file %s", filename);
1090     context->helper = _priv_gst_plugin_loader_funcs.create (context->registry);
1091     if (context->helper != NULL)
1092       context->helper_state = REGISTRY_SCAN_HELPER_RUNNING;
1093     else {
1094       GST_WARNING ("Failed starting plugin scanner. Scanning in-process");
1095       context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1096     }
1097   }
1099   if (context->helper_state == REGISTRY_SCAN_HELPER_RUNNING) {
1100     GST_DEBUG ("Using scan-helper to load plugin %s", filename);
1101     if (!_priv_gst_plugin_loader_funcs.load (context->helper,
1102             filename, file_size, file_mtime)) {
1103       g_warning ("External plugin loader failed. This most likely means that "
1104           "the plugin loader helper binary was not found or could not be run. "
1105           "%s", (g_getenv ("GST_PLUGIN_PATH") != NULL) ?
1106           "If you are running an uninstalled GStreamer setup, you might need "
1107           "to update your gst-uninstalled script so that the "
1108           "GST_PLUGIN_SCANNER environment variable gets set." : "");
1109       context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1110     }
1111   }
1113   /* Check if the helper is disabled (or just got disabled above) */
1114   if (context->helper_state == REGISTRY_SCAN_HELPER_DISABLED) {
1115     /* Load plugin the old fashioned way... */
1117     /* We don't use a GError here because a failure to load some shared
1118      * objects as plugins is normal (particularly in the uninstalled case)
1119      */
1120     newplugin = gst_plugin_load_file (filename, NULL);
1121   }
1123   if (newplugin) {
1124     GST_DEBUG_OBJECT (context->registry, "marking new plugin %p as registered",
1125         newplugin);
1126     newplugin->registered = TRUE;
1127     gst_object_unref (newplugin);
1128     changed = TRUE;
1129   }
1131   if (!__registry_reuse_plugin_scanner) {
1132     clear_scan_context (context);
1133     context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
1134   }
1136   return changed;
1139 static gboolean
1140 is_blacklisted_hidden_directory (const gchar * dirent)
1142   if (G_LIKELY (dirent[0] != '.'))
1143     return FALSE;
1145   /* skip the .debug directory, these contain elf files that are not
1146    * useful or worse, can crash dlopen () */
1147   if (strcmp (dirent, ".debug") == 0)
1148     return TRUE;
1150   /* can also skip .git and .deps dirs, those won't contain useful files.
1151    * This speeds up scanning a bit in uninstalled setups. */
1152   if (strcmp (dirent, ".git") == 0 || strcmp (dirent, ".deps") == 0)
1153     return TRUE;
1155   return FALSE;
1158 static gboolean
1159 gst_registry_scan_path_level (GstRegistryScanContext * context,
1160     const gchar * path, int level)
1162   GDir *dir;
1163   const gchar *dirent;
1164   gchar *filename;
1165   GstPlugin *plugin;
1166   gboolean changed = FALSE;
1168   dir = g_dir_open (path, 0, NULL);
1169   if (!dir)
1170     return FALSE;
1172   while ((dirent = g_dir_read_name (dir))) {
1173     GStatBuf file_status;
1175     filename = g_build_filename (path, dirent, NULL);
1176     if (g_stat (filename, &file_status) < 0) {
1177       /* Plugin will be removed from cache after the scan completes if it
1178        * is still marked 'cached' */
1179       g_free (filename);
1180       continue;
1181     }
1183     if (file_status.st_mode & S_IFDIR) {
1184       if (G_UNLIKELY (is_blacklisted_hidden_directory (dirent))) {
1185         GST_TRACE_OBJECT (context->registry, "ignoring %s directory", dirent);
1186         g_free (filename);
1187         continue;
1188       }
1189       /* FIXME 0.11: Don't recurse into directories, this behaviour
1190        * is inconsistent with other PATH environment variables
1191        */
1192       if (level > 0) {
1193         GST_LOG_OBJECT (context->registry, "recursing into directory %s",
1194             filename);
1195         changed |= gst_registry_scan_path_level (context, filename, level - 1);
1196       } else {
1197         GST_LOG_OBJECT (context->registry, "not recursing into directory %s, "
1198             "recursion level too deep", filename);
1199       }
1200       g_free (filename);
1201       continue;
1202     }
1203     if (!(file_status.st_mode & S_IFREG)) {
1204       GST_TRACE_OBJECT (context->registry, "%s is not a regular file, ignoring",
1205           filename);
1206       g_free (filename);
1207       continue;
1208     }
1209     if (!g_str_has_suffix (dirent, G_MODULE_SUFFIX)
1210 #ifdef GST_EXTRA_MODULE_SUFFIX
1211         && !g_str_has_suffix (dirent, GST_EXTRA_MODULE_SUFFIX)
1212 #endif
1213         ) {
1214       GST_TRACE_OBJECT (context->registry,
1215           "extension is not recognized as module file, ignoring file %s",
1216           filename);
1217       g_free (filename);
1218       continue;
1219     }
1221     GST_LOG_OBJECT (context->registry, "file %s looks like a possible module",
1222         filename);
1224     /* try to avoid unnecessary plugin-move pain */
1225     if (g_str_has_prefix (dirent, "libgstvalve") ||
1226         g_str_has_prefix (dirent, "libgstselector")) {
1227       GST_WARNING_OBJECT (context->registry, "ignoring old plugin %s which "
1228           "has been merged into the corelements plugin", filename);
1229       /* Plugin will be removed from cache after the scan completes if it
1230        * is still marked 'cached' */
1231       g_free (filename);
1232       continue;
1233     }
1235     /* plug-ins are considered unique by basename; if the given name
1236      * was already seen by the registry, we ignore it */
1237     plugin = gst_registry_lookup_bn (context->registry, dirent);
1238     if (plugin) {
1239       gboolean env_vars_changed, deps_changed = FALSE;
1241       if (plugin->registered) {
1242         GST_DEBUG_OBJECT (context->registry,
1243             "plugin already registered from path \"%s\"",
1244             GST_STR_NULL (plugin->filename));
1245         g_free (filename);
1246         gst_object_unref (plugin);
1247         continue;
1248       }
1250       env_vars_changed = _priv_plugin_deps_env_vars_changed (plugin);
1252       /* If a file with a certain basename is seen on a different path,
1253        * update the plugin to ensure the registry cache will reflect up
1254        * to date information */
1256       if (plugin->file_mtime == file_status.st_mtime &&
1257           plugin->file_size == file_status.st_size && !env_vars_changed &&
1258           !(deps_changed = _priv_plugin_deps_files_changed (plugin)) &&
1259           !strcmp (plugin->filename, filename)) {
1260         GST_LOG_OBJECT (context->registry, "file %s cached", filename);
1261         plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
1262         GST_LOG_OBJECT (context->registry,
1263             "marking plugin %p as registered as %s", plugin, filename);
1264         plugin->registered = TRUE;
1265       } else {
1266         GST_INFO_OBJECT (context->registry, "cached info for %s is stale",
1267             filename);
1268         GST_DEBUG_OBJECT (context->registry, "mtime %" G_GINT64_FORMAT " != %"
1269             G_GINT64_FORMAT " or size %" G_GINT64_FORMAT " != %"
1270             G_GINT64_FORMAT " or external dependency env_vars changed: %d or"
1271             " external dependencies changed: %d or old path %s != new path %s",
1272             (gint64) plugin->file_mtime, (gint64) file_status.st_mtime,
1273             (gint64) plugin->file_size, (gint64) file_status.st_size,
1274             env_vars_changed, deps_changed, plugin->filename, filename);
1275         gst_registry_remove_plugin (context->registry, plugin);
1276         changed |= gst_registry_scan_plugin_file (context, filename,
1277             file_status.st_size, file_status.st_mtime);
1278       }
1279       gst_object_unref (plugin);
1281     } else {
1282       GST_DEBUG_OBJECT (context->registry, "file %s not yet in registry",
1283           filename);
1284       changed |= gst_registry_scan_plugin_file (context, filename,
1285           file_status.st_size, file_status.st_mtime);
1286     }
1288     g_free (filename);
1289   }
1291   g_dir_close (dir);
1293   return changed;
1296 static gboolean
1297 gst_registry_scan_path_internal (GstRegistryScanContext * context,
1298     const gchar * path)
1300   gboolean changed;
1302   GST_DEBUG_OBJECT (context->registry, "scanning path %s", path);
1303   changed = gst_registry_scan_path_level (context, path, 10);
1305   GST_DEBUG_OBJECT (context->registry, "registry changed in path %s: %d", path,
1306       changed);
1307   return changed;
1310 /**
1311  * gst_registry_scan_path:
1312  * @registry: the registry to add found plugins to
1313  * @path: the path to scan
1314  *
1315  * Scan the given path for plugins to add to the registry. The syntax of the
1316  * path is specific to the registry.
1317  *
1318  * Returns: %TRUE if registry changed
1319  */
1320 gboolean
1321 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
1323   GstRegistryScanContext context;
1324   gboolean result;
1326   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1327   g_return_val_if_fail (path != NULL, FALSE);
1329   init_scan_context (&context, registry);
1331   result = gst_registry_scan_path_internal (&context, path);
1333   clear_scan_context (&context);
1334   result |= context.changed;
1336   return result;
1339 static gboolean
1340 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
1341     gpointer user_data)
1343   return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
1346 /**
1347  * gst_registry_get_feature_list_by_plugin:
1348  * @registry: a #GstRegistry.
1349  * @name: a plugin name.
1350  *
1351  * Retrieves a #GList of features of the plugin with name @name.
1352  *
1353  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
1354  *     #GstPluginFeature. Use gst_plugin_feature_list_free() after usage.
1355  */
1356 GList *
1357 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
1358     const gchar * name)
1360   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1361   g_return_val_if_fail (name != NULL, NULL);
1363   return gst_registry_feature_filter (registry,
1364       _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1367 /* Unref and delete the default registry */
1368 void
1369 _priv_gst_registry_cleanup (void)
1371   GstRegistry *registry;
1373   g_static_mutex_lock (&_gst_registry_mutex);
1374   if ((registry = _gst_registry_default) != NULL) {
1375     _gst_registry_default = NULL;
1376   }
1377   g_static_mutex_unlock (&_gst_registry_mutex);
1379   /* unref outside of the lock because we can. */
1380   if (registry)
1381     gst_object_unref (registry);
1384 /**
1385  * gst_default_registry_check_feature_version:
1386  * @feature_name: the name of the feature (e.g. "oggdemux")
1387  * @min_major: the minimum major version number
1388  * @min_minor: the minimum minor version number
1389  * @min_micro: the minimum micro version number
1390  *
1391  * Checks whether a plugin feature by the given name exists in the
1392  * default registry and whether its version is at least the
1393  * version required.
1394  *
1395  * Returns: #TRUE if the feature could be found and the version is
1396  * the same as the required version or newer, and #FALSE otherwise.
1397  */
1398 gboolean
1399 gst_default_registry_check_feature_version (const gchar * feature_name,
1400     guint min_major, guint min_minor, guint min_micro)
1402   GstPluginFeature *feature;
1403   GstRegistry *registry;
1404   gboolean ret = FALSE;
1406   g_return_val_if_fail (feature_name != NULL, FALSE);
1408   GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1410   registry = gst_registry_get_default ();
1411   feature = gst_registry_lookup_feature (registry, feature_name);
1412   if (feature) {
1413     ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1414         min_micro);
1415     gst_object_unref (feature);
1416   } else {
1417     GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1418   }
1420   return ret;
1423 static void
1424 load_plugin_func (gpointer data, gpointer user_data)
1426   GstPlugin *plugin;
1427   const gchar *filename;
1428   GError *err = NULL;
1430   filename = (const gchar *) data;
1431   GST_DEBUG ("Pre-loading plugin %s", filename);
1433   plugin = gst_plugin_load_file (filename, &err);
1435   if (plugin) {
1436     GST_INFO ("Loaded plugin: \"%s\"", filename);
1438     gst_default_registry_add_plugin (plugin);
1439   } else {
1440     if (err) {
1441       /* Report error to user, and free error */
1442       GST_ERROR ("Failed to load plugin: %s", err->message);
1443       g_error_free (err);
1444     } else {
1445       GST_WARNING ("Failed to load plugin: \"%s\"", filename);
1446     }
1447   }
1450 #ifndef GST_DISABLE_REGISTRY
1451 /* Unref all plugins marked 'cached', to clear old plugins that no
1452  * longer exist. Returns TRUE if any plugins were removed */
1453 static gboolean
1454 gst_registry_remove_cache_plugins (GstRegistry * registry)
1456   GList *g;
1457   GList *g_next;
1458   GstPlugin *plugin;
1459   gboolean changed = FALSE;
1461   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1463   GST_OBJECT_LOCK (registry);
1465   GST_DEBUG_OBJECT (registry, "removing cached plugins");
1466   g = registry->plugins;
1467   while (g) {
1468     g_next = g->next;
1469     plugin = g->data;
1470     if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
1471       GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
1472           GST_STR_NULL (plugin->filename));
1473       registry->plugins = g_list_delete_link (registry->plugins, g);
1474       if (G_LIKELY (plugin->basename))
1475         g_hash_table_remove (registry->basename_hash, plugin->basename);
1476       gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
1477       gst_object_unref (plugin);
1478       changed = TRUE;
1479     }
1480     g = g_next;
1481   }
1483   GST_OBJECT_UNLOCK (registry);
1485   return changed;
1488 typedef enum
1490   REGISTRY_SCAN_AND_UPDATE_FAILURE = 0,
1491   REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED,
1492   REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED
1493 } GstRegistryScanAndUpdateResult;
1495 /*
1496  * scan_and_update_registry:
1497  * @default_registry: the #GstRegistry
1498  * @registry_file: registry filename
1499  * @write_changes: write registry if it has changed?
1500  *
1501  * Scans for registry changes and eventually updates the registry cache.
1502  *
1503  * Return: %REGISTRY_SCAN_AND_UPDATE_FAILURE if the registry could not scanned
1504  *         or updated, %REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED if the
1505  *         registry is clean and %REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED if
1506  *         it has been updated and the cache needs to be re-read.
1507  */
1508 static GstRegistryScanAndUpdateResult
1509 scan_and_update_registry (GstRegistry * default_registry,
1510     const gchar * registry_file, gboolean write_changes, GError ** error)
1512   const gchar *plugin_path;
1513   gboolean changed = FALSE;
1514   GList *l;
1515   GstRegistryScanContext context;
1517   GST_INFO ("Validating plugins from registry cache: %s", registry_file);
1519   init_scan_context (&context, default_registry);
1521   /* It sounds tempting to just compare the mtime of directories with the mtime
1522    * of the registry cache, but it does not work. It would not catch updated
1523    * plugins, which might bring more or less features.
1524    */
1526   /* scan paths specified via --gst-plugin-path */
1527   GST_DEBUG ("scanning paths added via --gst-plugin-path");
1528   for (l = _priv_gst_plugin_paths; l != NULL; l = l->next) {
1529     GST_INFO ("Scanning plugin path: \"%s\"", (gchar *) l->data);
1530     changed |= gst_registry_scan_path_internal (&context, (gchar *) l->data);
1531   }
1532   /* keep plugin_paths around in case a re-scan is forced later on */
1534   /* GST_PLUGIN_PATH specifies a list of directories to scan for
1535    * additional plugins.  These take precedence over the system plugins */
1536   plugin_path = g_getenv ("GST_PLUGIN_PATH");
1537   if (plugin_path) {
1538     char **list;
1539     int i;
1541     GST_DEBUG ("GST_PLUGIN_PATH set to %s", plugin_path);
1542     list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1543     for (i = 0; list[i]; i++) {
1544       changed |= gst_registry_scan_path_internal (&context, list[i]);
1545     }
1546     g_strfreev (list);
1547   } else {
1548     GST_DEBUG ("GST_PLUGIN_PATH not set");
1549   }
1551   /* GST_PLUGIN_SYSTEM_PATH specifies a list of plugins that are always
1552    * loaded by default.  If not set, this defaults to the system-installed
1553    * path, and the plugins installed in the user's home directory */
1554   plugin_path = g_getenv ("GST_PLUGIN_SYSTEM_PATH");
1555   if (plugin_path == NULL) {
1556     char *home_plugins;
1558     GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH not set");
1560     /* plugins in the user's home directory take precedence over
1561      * system-installed ones */
1562     home_plugins = g_build_filename (g_get_home_dir (),
1563         ".gstreamer-" GST_MAJORMINOR, "plugins", NULL);
1564     GST_DEBUG ("scanning home plugins %s", home_plugins);
1565     changed |= gst_registry_scan_path_internal (&context, home_plugins);
1566     g_free (home_plugins);
1568     /* add the main (installed) library path */
1569     GST_DEBUG ("scanning main plugins %s", PLUGINDIR);
1570     changed |= gst_registry_scan_path_internal (&context, PLUGINDIR);
1572 #ifdef G_OS_WIN32
1573     {
1574       char *base_dir;
1575       char *dir;
1577       base_dir =
1578           g_win32_get_package_installation_directory_of_module
1579           (_priv_gst_dll_handle);
1581       dir = g_build_filename (base_dir, "lib", "gstreamer-0.10", NULL);
1582       GST_DEBUG ("scanning DLL dir %s", dir);
1584       changed |= gst_registry_scan_path_internal (&context, dir);
1586       g_free (dir);
1587       g_free (base_dir);
1588     }
1589 #endif
1590   } else {
1591     gchar **list;
1592     gint i;
1594     GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH set to %s", plugin_path);
1595     list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1596     for (i = 0; list[i]; i++) {
1597       changed |= gst_registry_scan_path_internal (&context, list[i]);
1598     }
1599     g_strfreev (list);
1600   }
1602   clear_scan_context (&context);
1603   changed |= context.changed;
1605   /* Remove cached plugins so stale info is cleared. */
1606   changed |= gst_registry_remove_cache_plugins (default_registry);
1608   if (!changed) {
1609     GST_INFO ("Registry cache has not changed");
1610     return REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED;
1611   }
1613   if (!write_changes) {
1614     GST_INFO ("Registry cache changed, but writing is disabled. Not writing.");
1615     return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1616   }
1618   GST_INFO ("Registry cache changed. Writing new registry cache");
1619   if (!gst_registry_binary_write_cache (default_registry, registry_file)) {
1620     g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
1621         _("Error writing registry cache to %s: %s"),
1622         registry_file, g_strerror (errno));
1623     return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1624   }
1626   GST_INFO ("Registry cache written successfully");
1627   return REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED;
1630 static gboolean
1631 ensure_current_registry (GError ** error)
1633   gchar *registry_file;
1634   GstRegistry *default_registry;
1635   gboolean ret = TRUE;
1636   gboolean do_update = TRUE;
1637   gboolean have_cache = TRUE;
1639   default_registry = gst_registry_get_default ();
1640   registry_file = g_strdup (g_getenv ("GST_REGISTRY"));
1641   if (registry_file == NULL) {
1642     registry_file = g_build_filename (g_get_home_dir (),
1643         ".gstreamer-" GST_MAJORMINOR, "registry." HOST_CPU ".bin", NULL);
1644   }
1646   if (!_gst_disable_registry_cache) {
1647     GST_INFO ("reading registry cache: %s", registry_file);
1648     have_cache = gst_registry_binary_read_cache (default_registry,
1649         registry_file);
1650     /* Only ever read the registry cache once, then disable it for
1651      * subsequent updates during the program lifetime */
1652     _gst_disable_registry_cache = TRUE;
1653   }
1655   if (have_cache) {
1656     do_update = !_priv_gst_disable_registry_update;
1657     if (do_update) {
1658       const gchar *update_env;
1660       if ((update_env = g_getenv ("GST_REGISTRY_UPDATE"))) {
1661         /* do update for any value different from "no" */
1662         do_update = (strcmp (update_env, "no") != 0);
1663       }
1664     }
1665   }
1667   if (do_update) {
1668     const gchar *reuse_env;
1670     if ((reuse_env = g_getenv ("GST_REGISTRY_REUSE_PLUGIN_SCANNER"))) {
1671       /* do reuse for any value different from "no" */
1672       __registry_reuse_plugin_scanner = (strcmp (reuse_env, "no") != 0);
1673     }
1674     /* now check registry */
1675     GST_DEBUG ("Updating registry cache");
1676     scan_and_update_registry (default_registry, registry_file, TRUE, error);
1677   } else {
1678     GST_DEBUG ("Not updating registry cache (disabled)");
1679   }
1681   g_free (registry_file);
1682   GST_INFO ("registry reading and updating done, result = %d", ret);
1684   return ret;
1686 #endif /* GST_DISABLE_REGISTRY */
1688 /**
1689  * gst_registry_fork_is_enabled:
1690  *
1691  * By default GStreamer will perform scanning and rebuilding of the
1692  * registry file using a helper child process.
1693  *
1694  * Applications might want to disable this behaviour with the
1695  * gst_registry_fork_set_enabled() function, in which case new plugins
1696  * are scanned (and loaded) into the application process.
1697  *
1698  * Returns: %TRUE if GStreamer will use the child helper process when
1699  * rebuilding the registry.
1700  *
1701  * Since: 0.10.10
1702  */
1703 gboolean
1704 gst_registry_fork_is_enabled (void)
1706   return _gst_enable_registry_fork;
1709 /**
1710  * gst_registry_fork_set_enabled:
1711  * @enabled: whether rebuilding the registry can use a temporary child helper process.
1712  *
1713  * Applications might want to disable/enable spawning of a child helper process
1714  * when rebuilding the registry. See gst_registry_fork_is_enabled() for more
1715  * information.
1716  *
1717  * Since: 0.10.10
1718  */
1719 void
1720 gst_registry_fork_set_enabled (gboolean enabled)
1722   _gst_enable_registry_fork = enabled;
1725 /**
1726  * gst_update_registry:
1727  *
1728  * Forces GStreamer to re-scan its plugin paths and update the default
1729  * plugin registry.
1730  *
1731  * Applications will almost never need to call this function, it is only
1732  * useful if the application knows new plugins have been installed (or old
1733  * ones removed) since the start of the application (or, to be precise, the
1734  * first call to gst_init()) and the application wants to make use of any
1735  * newly-installed plugins without restarting the application.
1736  *
1737  * Applications should assume that the registry update is neither atomic nor
1738  * thread-safe and should therefore not have any dynamic pipelines running
1739  * (including the playbin and decodebin elements) and should also not create
1740  * any elements or access the GStreamer registry while the update is in
1741  * progress.
1742  *
1743  * Note that this function may block for a significant amount of time.
1744  *
1745  * Returns: %TRUE if the registry has been updated successfully (does not
1746  *          imply that there were changes), otherwise %FALSE.
1747  *
1748  * Since: 0.10.12
1749  */
1750 gboolean
1751 gst_update_registry (void)
1753   gboolean res;
1755 #ifndef GST_DISABLE_REGISTRY
1756   GError *err = NULL;
1758   res = ensure_current_registry (&err);
1759   if (err) {
1760     GST_WARNING ("registry update failed: %s", err->message);
1761     g_error_free (err);
1762   } else {
1763     GST_LOG ("registry update succeeded");
1764   }
1766 #else
1767   GST_WARNING ("registry update failed: %s", "registry disabled");
1768   res = TRUE;
1769 #endif /* GST_DISABLE_REGISTRY */
1771   if (_priv_gst_preload_plugins) {
1772     GST_DEBUG ("Preloading indicated plugins...");
1773     g_slist_foreach (_priv_gst_preload_plugins, load_plugin_func, NULL);
1774   }
1776   return res;
1779 /**
1780  * gst_registry_get_feature_list_cookie:
1781  * @registry: the registry
1782  *
1783  * Returns the registrys feature list cookie. This changes
1784  * every time a feature is added or removed from the registry.
1785  *
1786  * Returns: the feature list cookie.
1787  *
1788  * Since: 0.10.26
1789  */
1790 guint32
1791 gst_registry_get_feature_list_cookie (GstRegistry * registry)
1793   g_return_val_if_fail (GST_IS_REGISTRY (registry), 0);
1795   return registry->priv->cookie;