devicemonitor: Add GstDeviceMonitor and related
[platform/upstream/gstreamer.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., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
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.
36  *
37  * The primary source, at all times, of plugin information is each plugin file
38  * itself. Thus, if an application wants information about a particular plugin,
39  * or wants to search for a feature that satisfies given criteria, the primary
40  * means of doing so is to load every plugin and look at the resulting
41  * information that is gathered in the default registry. Clearly, this is a time
42  * consuming process, so we cache information in the registry file. The format
43  * and location of the cache file is internal to gstreamer.
44  *
45  * On startup, plugins are searched for in the plugin search path. The following
46  * locations are checked in this order:
47  * <itemizedlist>
48  *   <listitem>
49  *     <para>location from --gst-plugin-path commandline option.</para>
50  *   </listitem>
51  *   <listitem>
52  *     <para>the GST_PLUGIN_PATH environment variable.</para>
53  *   </listitem>
54  *   <listitem>
55  *     <para>the GST_PLUGIN_SYSTEM_PATH environment variable.</para>
56  *   </listitem>
57  *   <listitem>
58  *     <para>default locations (if GST_PLUGIN_SYSTEM_PATH is not set). Those
59  *       default locations are:
60  *       <filename>$XDG_DATA_HOME/gstreamer-$GST_API_VERSION/plugins/</filename>
61  *       and <filename>$prefix/libs/gstreamer-$GST_API_VERSION/</filename>.
62  *       <ulink url="http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html">
63  *       <filename>$XDG_DATA_HOME</filename></ulink> defaults to
64  *       <filename>$HOME/.local/share</filename>.
65  *     </para>
66  *   </listitem>
67  * </itemizedlist>
68  * The registry cache file is loaded from
69  * <filename>$XDG_CACHE_HOME/gstreamer-$GST_API_VERSION/registry-$ARCH.bin</filename>
70  * (where
71  * <ulink url="http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html">
72  * <filename>$XDG_CACHE_HOME</filename></ulink> defaults to
73  * <filename>$HOME/.cache</filename>) or the file listed in the GST_REGISTRY
74  * env var. One reason to change the registry 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 "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  *
108  * Last reviewed on 2012-03-29 (0.11.3)
109  */
110
111 #ifdef HAVE_CONFIG_H
112 #include "config.h"
113 #endif
114 #include "gstconfig.h"
115 #include "gst_private.h"
116 #include <glib.h>
117 #include <sys/types.h>
118 #include <sys/stat.h>
119 #ifdef HAVE_UNISTD_H
120 #include <unistd.h>
121 #endif
122 #include <errno.h>
123 #include <stdio.h>
124 #include <string.h>
125
126 /* For g_stat () */
127 #include <glib/gstdio.h>
128
129 #include "gstinfo.h"
130 #include "gsterror.h"
131 #include "gstregistry.h"
132 #include "gstdevicemonitorfactory.h"
133
134 #include "gstpluginloader.h"
135
136 #include "gst-i18n-lib.h"
137
138 #include "gst.h"
139 #include "glib-compat-private.h"
140
141 #ifdef G_OS_WIN32
142 #include <windows.h>
143 extern HMODULE _priv_gst_dll_handle;
144 #endif
145
146 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
147
148 struct _GstRegistryPrivate
149 {
150   GList *plugins;
151   GList *features;
152
153 #if 0
154   GList *paths;
155 #endif
156
157   int cache_file;
158
159   /* hash to speedup _lookup_feature_locked() */
160   GHashTable *feature_hash;
161   /* hash to speedup _lookup */
162   GHashTable *basename_hash;
163
164   /* updated whenever the feature list changes */
165   guint32 cookie;
166   /* speedup for searching features */
167   GList *element_factory_list;
168   guint32 efl_cookie;
169   GList *typefind_factory_list;
170   guint32 tfl_cookie;
171   GList *device_monitor_factory_list;
172   guint32 dmfl_cookie;
173 };
174
175 /* the one instance of the default registry and the mutex protecting the
176  * variable. */
177 static GMutex _gst_registry_mutex;
178 static GstRegistry *_gst_registry_default = NULL;
179
180 /* defaults */
181 #define DEFAULT_FORK TRUE
182
183 /* control the behaviour of registry rebuild */
184 static gboolean _gst_enable_registry_fork = DEFAULT_FORK;
185 /* List of plugins that need preloading/reloading after scanning registry */
186 extern GSList *_priv_gst_preload_plugins;
187
188 #ifndef GST_DISABLE_REGISTRY
189 /*set to TRUE when registry needn't to be updated */
190 gboolean _priv_gst_disable_registry_update = FALSE;
191 extern GList *_priv_gst_plugin_paths;
192
193 /* Set to TRUE when the registry cache should be disabled */
194 gboolean _gst_disable_registry_cache = FALSE;
195
196 static gboolean __registry_reuse_plugin_scanner = TRUE;
197 #endif
198
199 /* Element signals and args */
200 enum
201 {
202   PLUGIN_ADDED,
203   FEATURE_ADDED,
204   LAST_SIGNAL
205 };
206
207 static void gst_registry_finalize (GObject * object);
208
209 static guint gst_registry_signals[LAST_SIGNAL] = { 0 };
210
211 static GstPluginFeature *gst_registry_lookup_feature_locked (GstRegistry *
212     registry, const char *name);
213 static GstPlugin *gst_registry_lookup_bn_locked (GstRegistry * registry,
214     const char *basename);
215
216 #define gst_registry_parent_class parent_class
217 G_DEFINE_TYPE (GstRegistry, gst_registry, GST_TYPE_OBJECT);
218
219 static void
220 gst_registry_class_init (GstRegistryClass * klass)
221 {
222   GObjectClass *gobject_class;
223
224   gobject_class = (GObjectClass *) klass;
225
226   g_type_class_add_private (klass, sizeof (GstRegistryPrivate));
227
228   /**
229    * GstRegistry::plugin-added:
230    * @registry: the registry that emitted the signal
231    * @plugin: the plugin that has been added
232    *
233    * Signals that a plugin has been added to the registry (possibly
234    * replacing a previously-added one by the same name)
235    */
236   gst_registry_signals[PLUGIN_ADDED] =
237       g_signal_new ("plugin-added", G_TYPE_FROM_CLASS (klass),
238       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
239       G_TYPE_NONE, 1, GST_TYPE_PLUGIN);
240
241   /**
242    * GstRegistry::feature-added:
243    * @registry: the registry that emitted the signal
244    * @feature: the feature that has been added
245    *
246    * Signals that a feature has been added to the registry (possibly
247    * replacing a previously-added one by the same name)
248    */
249   gst_registry_signals[FEATURE_ADDED] =
250       g_signal_new ("feature-added", G_TYPE_FROM_CLASS (klass),
251       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
252       G_TYPE_NONE, 1, GST_TYPE_PLUGIN_FEATURE);
253
254   gobject_class->finalize = gst_registry_finalize;
255 }
256
257 static void
258 gst_registry_init (GstRegistry * registry)
259 {
260   registry->priv =
261       G_TYPE_INSTANCE_GET_PRIVATE (registry, GST_TYPE_REGISTRY,
262       GstRegistryPrivate);
263   registry->priv->feature_hash = g_hash_table_new (g_str_hash, g_str_equal);
264   registry->priv->basename_hash = g_hash_table_new (g_str_hash, g_str_equal);
265 }
266
267 static void
268 gst_registry_finalize (GObject * object)
269 {
270   GstRegistry *registry = GST_REGISTRY (object);
271   GList *plugins, *p;
272   GList *features, *f;
273
274   plugins = registry->priv->plugins;
275   registry->priv->plugins = NULL;
276
277   GST_DEBUG_OBJECT (registry, "registry finalize");
278   p = plugins;
279   while (p) {
280     GstPlugin *plugin = p->data;
281
282     if (plugin) {
283       GST_LOG_OBJECT (registry, "removing plugin %s",
284           gst_plugin_get_name (plugin));
285       gst_object_unref (plugin);
286     }
287     p = g_list_next (p);
288   }
289   g_list_free (plugins);
290
291   features = registry->priv->features;
292   registry->priv->features = NULL;
293
294   f = features;
295   while (f) {
296     GstPluginFeature *feature = f->data;
297
298     if (feature) {
299       GST_LOG_OBJECT (registry, "removing feature %p (%s)", feature,
300           GST_OBJECT_NAME (feature));
301       gst_object_unparent (GST_OBJECT_CAST (feature));
302     }
303     f = g_list_next (f);
304   }
305   g_list_free (features);
306
307   g_hash_table_destroy (registry->priv->feature_hash);
308   registry->priv->feature_hash = NULL;
309   g_hash_table_destroy (registry->priv->basename_hash);
310   registry->priv->basename_hash = NULL;
311
312   if (registry->priv->element_factory_list) {
313     GST_DEBUG_OBJECT (registry, "Cleaning up cached element factory list");
314     gst_plugin_feature_list_free (registry->priv->element_factory_list);
315   }
316
317   if (registry->priv->typefind_factory_list) {
318     GST_DEBUG_OBJECT (registry, "Cleaning up cached typefind factory list");
319     gst_plugin_feature_list_free (registry->priv->typefind_factory_list);
320   }
321
322   if (registry->priv->device_monitor_factory_list) {
323     GST_DEBUG_OBJECT (registry,
324         "Cleaning up cached device monitor factory list");
325     gst_plugin_feature_list_free (registry->priv->device_monitor_factory_list);
326   }
327
328   G_OBJECT_CLASS (parent_class)->finalize (object);
329 }
330
331 /**
332  * gst_registry_get:
333  *
334  * Retrieves the singleton plugin registry. The caller does not own a
335  * reference on the registry, as it is alive as long as GStreamer is
336  * initialized.
337  *
338  * Returns: (transfer none): the #GstRegistry.
339  */
340 GstRegistry *
341 gst_registry_get (void)
342 {
343   GstRegistry *registry;
344
345   g_mutex_lock (&_gst_registry_mutex);
346   if (G_UNLIKELY (!_gst_registry_default)) {
347     _gst_registry_default = g_object_newv (GST_TYPE_REGISTRY, 0, NULL);
348     gst_object_ref_sink (GST_OBJECT_CAST (_gst_registry_default));
349   }
350   registry = _gst_registry_default;
351   g_mutex_unlock (&_gst_registry_mutex);
352
353   return registry;
354 }
355
356 #if 0
357 /**
358  * gst_registry_add_path:
359  * @registry: the registry to add the path to
360  * @path: the path to add to the registry
361  *
362  * Add the given path to the registry. The syntax of the
363  * path is specific to the registry. If the path has already been
364  * added, do nothing.
365  */
366 void
367 gst_registry_add_path (GstRegistry * registry, const gchar * path)
368 {
369   g_return_if_fail (GST_IS_REGISTRY (registry));
370   g_return_if_fail (path != NULL);
371
372   if (strlen (path) == 0)
373     goto empty_path;
374
375   GST_OBJECT_LOCK (registry);
376   if (g_list_find_custom (registry->priv->paths, path, (GCompareFunc) strcmp))
377     goto was_added;
378
379   GST_INFO ("Adding plugin path: \"%s\"", path);
380   registry->priv->paths =
381       g_list_append (registry->priv->paths, g_strdup (path));
382   GST_OBJECT_UNLOCK (registry);
383
384   return;
385
386 empty_path:
387   {
388     GST_INFO ("Ignoring empty plugin path");
389     return;
390   }
391 was_added:
392   {
393     g_warning ("path %s already added to registry", path);
394     GST_OBJECT_UNLOCK (registry);
395     return;
396   }
397 }
398
399 /**
400  * gst_registry_get_path_list:
401  * @registry: the registry to get the pathlist of
402  *
403  * Get the list of paths for the given registry.
404  *
405  * Returns: (transfer container) (element-type char*): A #GList of paths as
406  *     strings. g_list_free after use.
407  *
408  * MT safe.
409  */
410 GList *
411 gst_registry_get_path_list (GstRegistry * registry)
412 {
413   GList *list;
414
415   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
416
417   GST_OBJECT_LOCK (registry);
418   /* We don't need to copy the strings, because they won't be deleted
419    * as long as the GstRegistry is around */
420   list = g_list_copy (registry->priv->paths);
421   GST_OBJECT_UNLOCK (registry);
422
423   return list;
424 }
425 #endif
426
427 /**
428  * gst_registry_add_plugin:
429  * @registry: the registry to add the plugin to
430  * @plugin: (transfer full): the plugin to add
431  *
432  * Add the plugin to the registry. The plugin-added signal will be emitted.
433  * This function will sink @plugin.
434  *
435  * Returns: TRUE on success.
436  *
437  * MT safe.
438  */
439 gboolean
440 gst_registry_add_plugin (GstRegistry * registry, GstPlugin * plugin)
441 {
442   GstPlugin *existing_plugin;
443
444   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
445   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
446
447   GST_OBJECT_LOCK (registry);
448   if (G_LIKELY (plugin->basename)) {
449     /* we have a basename, see if we find the plugin */
450     existing_plugin =
451         gst_registry_lookup_bn_locked (registry, plugin->basename);
452     if (existing_plugin) {
453       GST_DEBUG_OBJECT (registry,
454           "Replacing existing plugin \"%s\" %p with new plugin %p for filename \"%s\"",
455           GST_STR_NULL (existing_plugin->filename), existing_plugin, plugin,
456           GST_STR_NULL (plugin->filename));
457       /* If the new plugin is blacklisted and the existing one isn't cached, do not
458        * accept if it's from a different location than the existing one */
459       if (GST_OBJECT_FLAG_IS_SET (plugin, GST_PLUGIN_FLAG_BLACKLISTED) &&
460           strcmp (plugin->filename, existing_plugin->filename)) {
461         GST_WARNING_OBJECT (registry,
462             "Not replacing plugin because new one (%s) is blacklisted but for a different location than existing one (%s)",
463             plugin->filename, existing_plugin->filename);
464         gst_object_unref (plugin);
465         GST_OBJECT_UNLOCK (registry);
466         return FALSE;
467       }
468       registry->priv->plugins =
469           g_list_remove (registry->priv->plugins, existing_plugin);
470       if (G_LIKELY (existing_plugin->basename))
471         g_hash_table_remove (registry->priv->basename_hash,
472             existing_plugin->basename);
473       gst_object_unref (existing_plugin);
474     }
475   }
476
477   GST_DEBUG_OBJECT (registry, "adding plugin %p for filename \"%s\"",
478       plugin, GST_STR_NULL (plugin->filename));
479
480   registry->priv->plugins = g_list_prepend (registry->priv->plugins, plugin);
481   if (G_LIKELY (plugin->basename))
482     g_hash_table_replace (registry->priv->basename_hash, plugin->basename,
483         plugin);
484
485   gst_object_ref_sink (plugin);
486   GST_OBJECT_UNLOCK (registry);
487
488   GST_LOG_OBJECT (registry, "emitting plugin-added for filename \"%s\"",
489       GST_STR_NULL (plugin->filename));
490   g_signal_emit (registry, gst_registry_signals[PLUGIN_ADDED], 0, plugin);
491
492   return TRUE;
493 }
494
495 static void
496 gst_registry_remove_features_for_plugin_unlocked (GstRegistry * registry,
497     GstPlugin * plugin)
498 {
499   GList *f;
500
501   g_return_if_fail (GST_IS_REGISTRY (registry));
502   g_return_if_fail (GST_IS_PLUGIN (plugin));
503
504   /* Remove all features for this plugin */
505   f = registry->priv->features;
506   while (f != NULL) {
507     GList *next = g_list_next (f);
508     GstPluginFeature *feature = f->data;
509
510     if (G_UNLIKELY (feature && feature->plugin == plugin)) {
511       GST_DEBUG_OBJECT (registry, "removing feature %p (%s) for plugin %p (%s)",
512           feature, gst_plugin_feature_get_name (feature), plugin,
513           plugin->desc.name);
514
515       registry->priv->features =
516           g_list_delete_link (registry->priv->features, f);
517       g_hash_table_remove (registry->priv->feature_hash,
518           GST_OBJECT_NAME (feature));
519       gst_object_unparent (GST_OBJECT_CAST (feature));
520     }
521     f = next;
522   }
523   registry->priv->cookie++;
524 }
525
526 /**
527  * gst_registry_remove_plugin:
528  * @registry: the registry to remove the plugin from
529  * @plugin: (transfer none): the plugin to remove
530  *
531  * Remove the plugin from the registry.
532  *
533  * MT safe.
534  */
535 void
536 gst_registry_remove_plugin (GstRegistry * registry, GstPlugin * plugin)
537 {
538   g_return_if_fail (GST_IS_REGISTRY (registry));
539   g_return_if_fail (GST_IS_PLUGIN (plugin));
540
541   GST_DEBUG_OBJECT (registry, "removing plugin %p (%s)",
542       plugin, gst_plugin_get_name (plugin));
543
544   GST_OBJECT_LOCK (registry);
545   registry->priv->plugins = g_list_remove (registry->priv->plugins, plugin);
546   if (G_LIKELY (plugin->basename))
547     g_hash_table_remove (registry->priv->basename_hash, plugin->basename);
548   gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
549   GST_OBJECT_UNLOCK (registry);
550   gst_object_unref (plugin);
551 }
552
553 /**
554  * gst_registry_add_feature:
555  * @registry: the registry to add the plugin to
556  * @feature: (transfer full): the feature to add
557  *
558  * Add the feature to the registry. The feature-added signal will be emitted.
559  * This function sinks @feature.
560  *
561  * Returns: TRUE on success.
562  *
563  * MT safe.
564  */
565 gboolean
566 gst_registry_add_feature (GstRegistry * registry, GstPluginFeature * feature)
567 {
568   GstPluginFeature *existing_feature;
569
570   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
571   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
572   g_return_val_if_fail (GST_OBJECT_NAME (feature) != NULL, FALSE);
573   g_return_val_if_fail (feature->plugin_name != NULL, FALSE);
574
575   GST_OBJECT_LOCK (registry);
576   existing_feature = gst_registry_lookup_feature_locked (registry,
577       GST_OBJECT_NAME (feature));
578   if (G_UNLIKELY (existing_feature)) {
579     GST_DEBUG_OBJECT (registry, "replacing existing feature %p (%s)",
580         existing_feature, GST_OBJECT_NAME (feature));
581     /* Remove the existing feature from the list now, before we insert the new
582      * one, but don't unref yet because the hash is still storing a reference to
583      * it. */
584     registry->priv->features =
585         g_list_remove (registry->priv->features, existing_feature);
586   }
587
588   GST_DEBUG_OBJECT (registry, "adding feature %p (%s)", feature,
589       GST_OBJECT_NAME (feature));
590
591   registry->priv->features = g_list_prepend (registry->priv->features, feature);
592   g_hash_table_replace (registry->priv->feature_hash, GST_OBJECT_NAME (feature),
593       feature);
594
595   if (G_UNLIKELY (existing_feature)) {
596     /* We unref now. No need to remove the feature name from the hash table, it
597      * got replaced by the new feature */
598     gst_object_unparent (GST_OBJECT_CAST (existing_feature));
599   }
600
601   gst_object_set_parent (GST_OBJECT_CAST (feature), GST_OBJECT_CAST (registry));
602
603   registry->priv->cookie++;
604   GST_OBJECT_UNLOCK (registry);
605
606   GST_LOG_OBJECT (registry, "emitting feature-added for %s",
607       GST_OBJECT_NAME (feature));
608   g_signal_emit (registry, gst_registry_signals[FEATURE_ADDED], 0, feature);
609
610   return TRUE;
611 }
612
613 /**
614  * gst_registry_remove_feature:
615  * @registry: the registry to remove the feature from
616  * @feature: (transfer none): the feature to remove
617  *
618  * Remove the feature from the registry.
619  *
620  * MT safe.
621  */
622 void
623 gst_registry_remove_feature (GstRegistry * registry, GstPluginFeature * feature)
624 {
625   g_return_if_fail (GST_IS_REGISTRY (registry));
626   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
627
628   GST_DEBUG_OBJECT (registry, "removing feature %p (%s)",
629       feature, gst_plugin_feature_get_name (feature));
630
631   GST_OBJECT_LOCK (registry);
632   registry->priv->features = g_list_remove (registry->priv->features, feature);
633   g_hash_table_remove (registry->priv->feature_hash, GST_OBJECT_NAME (feature));
634   registry->priv->cookie++;
635   GST_OBJECT_UNLOCK (registry);
636
637   gst_object_unparent ((GstObject *) feature);
638 }
639
640 /**
641  * gst_registry_plugin_filter:
642  * @registry: registry to query
643  * @filter: (scope call): the filter to use
644  * @first: only return first match
645  * @user_data: (closure): user data passed to the filter function
646  *
647  * Runs a filter against all plugins in the registry and returns a #GList with
648  * the results. If the first flag is set, only the first match is
649  * returned (as a list with a single object).
650  * Every plugin is reffed; use gst_plugin_list_free() after use, which
651  * will unref again.
652  *
653  * Returns: (transfer full) (element-type Gst.Plugin): a #GList of #GstPlugin.
654  *     Use gst_plugin_list_free() after usage.
655  *
656  * MT safe.
657  */
658 GList *
659 gst_registry_plugin_filter (GstRegistry * registry,
660     GstPluginFilter filter, gboolean first, gpointer user_data)
661 {
662   GList *list = NULL;
663
664   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
665
666   GST_OBJECT_LOCK (registry);
667   {
668     const GList *walk;
669
670     for (walk = registry->priv->plugins; walk != NULL; walk = walk->next) {
671       GstPlugin *plugin = walk->data;
672
673       if (filter == NULL || filter (plugin, user_data)) {
674         list = g_list_prepend (list, gst_object_ref (plugin));
675
676         if (first)
677           break;
678       }
679     }
680   }
681   GST_OBJECT_UNLOCK (registry);
682
683   return list;
684 }
685
686 typedef struct
687 {
688   const gchar *name;
689   GType type;
690 } GstTypeNameData;
691
692 static gboolean
693 gst_plugin_feature_type_name_filter (GstPluginFeature * feature,
694     GstTypeNameData * data)
695 {
696   g_assert (GST_IS_PLUGIN_FEATURE (feature));
697
698   return ((data->type == 0 || data->type == G_OBJECT_TYPE (feature)) &&
699       (data->name == NULL || !strcmp (data->name, GST_OBJECT_NAME (feature))));
700 }
701
702 /* returns TRUE if the list was changed
703  *
704  * Must be called with the object lock taken */
705 static gboolean
706 gst_registry_get_feature_list_or_create (GstRegistry * registry,
707     GList ** previous, guint32 * cookie, GType type)
708 {
709   gboolean res = FALSE;
710   GstRegistryPrivate *priv = registry->priv;
711
712   if (G_UNLIKELY (!*previous || priv->cookie != *cookie)) {
713     GstTypeNameData data;
714     const GList *walk;
715
716     if (*previous) {
717       gst_plugin_feature_list_free (*previous);
718       *previous = NULL;
719     }
720
721     data.type = type;
722     data.name = NULL;
723
724     for (walk = registry->priv->features; walk != NULL; walk = walk->next) {
725       GstPluginFeature *feature = walk->data;
726
727       if (gst_plugin_feature_type_name_filter (feature, &data)) {
728         *previous = g_list_prepend (*previous, gst_object_ref (feature));
729       }
730     }
731
732     *cookie = priv->cookie;
733     res = TRUE;
734   }
735
736   return res;
737 }
738
739 static gint
740 type_find_factory_rank_cmp (const GstPluginFeature * fac1,
741     const GstPluginFeature * fac2)
742 {
743   if (G_LIKELY (fac1->rank != fac2->rank))
744     return fac2->rank - fac1->rank;
745
746   /* to make the order in which things happen more deterministic,
747    * sort by name when the ranks are the same. */
748   return strcmp (GST_OBJECT_NAME (fac1), GST_OBJECT_NAME (fac2));
749 }
750
751 static GList *
752 gst_registry_get_element_factory_list (GstRegistry * registry)
753 {
754   GList *list;
755
756   GST_OBJECT_LOCK (registry);
757
758   gst_registry_get_feature_list_or_create (registry,
759       &registry->priv->element_factory_list, &registry->priv->efl_cookie,
760       GST_TYPE_ELEMENT_FACTORY);
761
762   /* Return reffed copy */
763   list = gst_plugin_feature_list_copy (registry->priv->element_factory_list);
764
765   GST_OBJECT_UNLOCK (registry);
766
767   return list;
768 }
769
770 static GList *
771 gst_registry_get_typefind_factory_list (GstRegistry * registry)
772 {
773   GList *list;
774
775   GST_OBJECT_LOCK (registry);
776
777   if (G_UNLIKELY (gst_registry_get_feature_list_or_create (registry,
778               &registry->priv->typefind_factory_list,
779               &registry->priv->tfl_cookie, GST_TYPE_TYPE_FIND_FACTORY)))
780     registry->priv->typefind_factory_list =
781         g_list_sort (registry->priv->typefind_factory_list,
782         (GCompareFunc) type_find_factory_rank_cmp);
783
784   /* Return reffed copy */
785   list = gst_plugin_feature_list_copy (registry->priv->typefind_factory_list);
786
787   GST_OBJECT_UNLOCK (registry);
788
789   return list;
790 }
791
792
793 static GList *
794 gst_registry_get_device_monitor_factory_list (GstRegistry * registry)
795 {
796   GList *list;
797
798   GST_OBJECT_LOCK (registry);
799
800   gst_registry_get_feature_list_or_create (registry,
801       &registry->priv->device_monitor_factory_list,
802       &registry->priv->dmfl_cookie, GST_TYPE_DEVICE_MONITOR_FACTORY);
803
804   /* Return reffed copy */
805   list =
806       gst_plugin_feature_list_copy (registry->priv->
807       device_monitor_factory_list);
808
809   GST_OBJECT_UNLOCK (registry);
810
811   return list;
812 }
813
814 /**
815  * gst_registry_feature_filter:
816  * @registry: registry to query
817  * @filter: (scope call): the filter to use
818  * @first: only return first match
819  * @user_data: (closure): user data passed to the filter function
820  *
821  * Runs a filter against all features of the plugins in the registry
822  * and returns a GList with the results.
823  * If the first flag is set, only the first match is
824  * returned (as a list with a single object).
825  *
826  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
827  *     #GstPluginFeature. Use gst_plugin_feature_list_free() after usage.
828  *
829  * MT safe.
830  */
831 GList *
832 gst_registry_feature_filter (GstRegistry * registry,
833     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
834 {
835   GList *list = NULL;
836
837   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
838
839   GST_OBJECT_LOCK (registry);
840   {
841     const GList *walk;
842
843     for (walk = registry->priv->features; walk != NULL; walk = walk->next) {
844       GstPluginFeature *feature = walk->data;
845
846       if (filter == NULL || filter (feature, user_data)) {
847         list = g_list_prepend (list, gst_object_ref (feature));
848
849         if (first)
850           break;
851       }
852     }
853   }
854   GST_OBJECT_UNLOCK (registry);
855
856   return list;
857 }
858
859 static gboolean
860 gst_registry_plugin_name_filter (GstPlugin * plugin, const gchar * name)
861 {
862   return (plugin->desc.name && !strcmp (plugin->desc.name, name));
863 }
864
865 /**
866  * gst_registry_find_plugin:
867  * @registry: the registry to search
868  * @name: the plugin name to find
869  *
870  * Find the plugin with the given name in the registry.
871  * The plugin will be reffed; caller is responsible for unreffing.
872  *
873  * Returns: (transfer full): the plugin with the given name or NULL if the
874  *     plugin was not found. gst_object_unref() after usage.
875  *
876  * MT safe.
877  */
878 GstPlugin *
879 gst_registry_find_plugin (GstRegistry * registry, const gchar * name)
880 {
881   GList *walk;
882   GstPlugin *result = NULL;
883
884   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
885   g_return_val_if_fail (name != NULL, NULL);
886
887   walk = gst_registry_plugin_filter (registry,
888       (GstPluginFilter) gst_registry_plugin_name_filter, TRUE, (gpointer) name);
889   if (walk) {
890     result = GST_PLUGIN_CAST (walk->data);
891
892     gst_object_ref (result);
893     gst_plugin_list_free (walk);
894   }
895
896   return result;
897 }
898
899 /**
900  * gst_registry_find_feature:
901  * @registry: the registry to search
902  * @name: the pluginfeature name to find
903  * @type: the pluginfeature type to find
904  *
905  * Find the pluginfeature with the given name and type in the registry.
906  *
907  * Returns: (transfer full): the pluginfeature with the given name and type
908  *     or NULL if the plugin was not found. gst_object_unref() after usage.
909  *
910  * MT safe.
911  */
912 GstPluginFeature *
913 gst_registry_find_feature (GstRegistry * registry, const gchar * name,
914     GType type)
915 {
916   GstPluginFeature *feature = NULL;
917
918   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
919   g_return_val_if_fail (name != NULL, NULL);
920   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
921
922   feature = gst_registry_lookup_feature (registry, name);
923   if (feature && !g_type_is_a (G_TYPE_FROM_INSTANCE (feature), type)) {
924     gst_object_unref (feature);
925     feature = NULL;
926   }
927
928   return feature;
929 }
930
931 /**
932  * gst_registry_get_feature_list:
933  * @registry: a #GstRegistry
934  * @type: a #GType.
935  *
936  * Retrieves a #GList of #GstPluginFeature of @type.
937  *
938  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
939  *     #GstPluginFeature of @type. Use gst_plugin_feature_list_free() after use
940  *
941  * MT safe.
942  */
943 GList *
944 gst_registry_get_feature_list (GstRegistry * registry, GType type)
945 {
946   GstTypeNameData data;
947
948   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
949   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
950
951   /* Speed up */
952   if (type == GST_TYPE_ELEMENT_FACTORY)
953     return gst_registry_get_element_factory_list (registry);
954   else if (type == GST_TYPE_TYPE_FIND_FACTORY)
955     return gst_registry_get_typefind_factory_list (registry);
956   else if (type == GST_TYPE_DEVICE_MONITOR_FACTORY)
957     return gst_registry_get_device_monitor_factory_list (registry);
958
959   data.type = type;
960   data.name = NULL;
961
962   return gst_registry_feature_filter (registry,
963       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
964       FALSE, &data);
965 }
966
967 /**
968  * gst_registry_get_plugin_list:
969  * @registry: the registry to search
970  *
971  * Get a copy of all plugins registered in the given registry. The refcount
972  * of each element in the list in incremented.
973  *
974  * Returns: (transfer full) (element-type Gst.Plugin): a #GList of #GstPlugin.
975  *     Use gst_plugin_list_free() after usage.
976  *
977  * MT safe.
978  */
979 GList *
980 gst_registry_get_plugin_list (GstRegistry * registry)
981 {
982   GList *list;
983   GList *g;
984
985   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
986
987   GST_OBJECT_LOCK (registry);
988   list = g_list_copy (registry->priv->plugins);
989   for (g = list; g; g = g->next) {
990     gst_object_ref (GST_PLUGIN_CAST (g->data));
991   }
992   GST_OBJECT_UNLOCK (registry);
993
994   return list;
995 }
996
997 static GstPluginFeature *
998 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
999 {
1000   return g_hash_table_lookup (registry->priv->feature_hash, name);
1001 }
1002
1003 /**
1004  * gst_registry_lookup_feature:
1005  * @registry: a #GstRegistry
1006  * @name: a #GstPluginFeature name
1007  *
1008  * Find a #GstPluginFeature with @name in @registry.
1009  *
1010  * Returns: (transfer full): a #GstPluginFeature with its refcount incremented,
1011  *     use gst_object_unref() after usage.
1012  *
1013  * MT safe.
1014  */
1015 GstPluginFeature *
1016 gst_registry_lookup_feature (GstRegistry * registry, const char *name)
1017 {
1018   GstPluginFeature *feature;
1019
1020   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1021   g_return_val_if_fail (name != NULL, NULL);
1022
1023   GST_OBJECT_LOCK (registry);
1024   feature = gst_registry_lookup_feature_locked (registry, name);
1025   if (feature)
1026     gst_object_ref (feature);
1027   GST_OBJECT_UNLOCK (registry);
1028
1029   return feature;
1030 }
1031
1032 static GstPlugin *
1033 gst_registry_lookup_bn_locked (GstRegistry * registry, const char *basename)
1034 {
1035   return g_hash_table_lookup (registry->priv->basename_hash, basename);
1036 }
1037
1038 static GstPlugin *
1039 gst_registry_lookup_bn (GstRegistry * registry, const char *basename)
1040 {
1041   GstPlugin *plugin;
1042
1043   GST_OBJECT_LOCK (registry);
1044   plugin = gst_registry_lookup_bn_locked (registry, basename);
1045   if (plugin)
1046     gst_object_ref (plugin);
1047   GST_OBJECT_UNLOCK (registry);
1048
1049   return plugin;
1050 }
1051
1052 /**
1053  * gst_registry_lookup:
1054  * @registry: the registry to look up in
1055  * @filename: the name of the file to look up
1056  *
1057  * Look up a plugin in the given registry with the given filename.
1058  * If found, plugin is reffed.
1059  *
1060  * Returns: (transfer full): the #GstPlugin if found, or NULL if not.
1061  *     gst_object_unref() after usage.
1062  */
1063 GstPlugin *
1064 gst_registry_lookup (GstRegistry * registry, const char *filename)
1065 {
1066   GstPlugin *plugin;
1067   gchar *basename;
1068
1069   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1070   g_return_val_if_fail (filename != NULL, NULL);
1071
1072   basename = g_path_get_basename (filename);
1073   if (G_UNLIKELY (basename == NULL))
1074     return NULL;
1075
1076   plugin = gst_registry_lookup_bn (registry, basename);
1077
1078   g_free (basename);
1079
1080   return plugin;
1081 }
1082
1083 typedef enum
1084 {
1085   REGISTRY_SCAN_HELPER_NOT_STARTED = 0,
1086   REGISTRY_SCAN_HELPER_DISABLED,
1087   REGISTRY_SCAN_HELPER_RUNNING
1088 } GstRegistryScanHelperState;
1089
1090 typedef struct
1091 {
1092   GstRegistry *registry;
1093   GstRegistryScanHelperState helper_state;
1094   GstPluginLoader *helper;
1095   gboolean changed;
1096 } GstRegistryScanContext;
1097
1098 static void
1099 init_scan_context (GstRegistryScanContext * context, GstRegistry * registry)
1100 {
1101   gboolean do_fork;
1102
1103   context->registry = registry;
1104
1105   /* see if forking is enabled and set up the scan helper state accordingly */
1106   do_fork = _gst_enable_registry_fork;
1107   if (do_fork) {
1108     const gchar *fork_env;
1109
1110     /* forking enabled, see if it is disabled with an env var */
1111     if ((fork_env = g_getenv ("GST_REGISTRY_FORK"))) {
1112       /* fork enabled for any value different from "no" */
1113       do_fork = strcmp (fork_env, "no") != 0;
1114     }
1115   }
1116
1117   if (do_fork)
1118     context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
1119   else
1120     context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1121
1122   context->helper = NULL;
1123   context->changed = FALSE;
1124 }
1125
1126 static void
1127 clear_scan_context (GstRegistryScanContext * context)
1128 {
1129   if (context->helper) {
1130     context->changed |= _priv_gst_plugin_loader_funcs.destroy (context->helper);
1131     context->helper = NULL;
1132   }
1133 }
1134
1135 static gboolean
1136 gst_registry_scan_plugin_file (GstRegistryScanContext * context,
1137     const gchar * filename, off_t file_size, time_t file_mtime)
1138 {
1139   gboolean changed = FALSE;
1140   GstPlugin *newplugin = NULL;
1141
1142 #ifdef G_OS_WIN32
1143   /* Disable external plugin loader on Windows until it is ported properly. */
1144   context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1145 #endif
1146
1147
1148   /* Have a plugin to load - see if the scan-helper needs starting */
1149   if (context->helper_state == REGISTRY_SCAN_HELPER_NOT_STARTED) {
1150     GST_DEBUG ("Starting plugin scanner for file %s", filename);
1151     context->helper = _priv_gst_plugin_loader_funcs.create (context->registry);
1152     if (context->helper != NULL)
1153       context->helper_state = REGISTRY_SCAN_HELPER_RUNNING;
1154     else {
1155       GST_WARNING ("Failed starting plugin scanner. Scanning in-process");
1156       context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1157     }
1158   }
1159
1160   if (context->helper_state == REGISTRY_SCAN_HELPER_RUNNING) {
1161     GST_DEBUG ("Using scan-helper to load plugin %s", filename);
1162     if (!_priv_gst_plugin_loader_funcs.load (context->helper,
1163             filename, file_size, file_mtime)) {
1164       g_warning ("External plugin loader failed. This most likely means that "
1165           "the plugin loader helper binary was not found or could not be run. "
1166           "You might need to set the GST_PLUGIN_SCANNER environment variable "
1167           "if your setup is unusual. This should normally not be required "
1168           "though.");
1169       context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1170     }
1171   }
1172
1173   /* Check if the helper is disabled (or just got disabled above) */
1174   if (context->helper_state == REGISTRY_SCAN_HELPER_DISABLED) {
1175     /* Load plugin the old fashioned way... */
1176
1177     /* We don't use a GError here because a failure to load some shared
1178      * objects as plugins is normal (particularly in the uninstalled case)
1179      */
1180     newplugin = gst_plugin_load_file (filename, NULL);
1181   }
1182
1183   if (newplugin) {
1184     GST_DEBUG_OBJECT (context->registry, "marking new plugin %p as registered",
1185         newplugin);
1186     newplugin->registered = TRUE;
1187     gst_object_unref (newplugin);
1188     changed = TRUE;
1189   }
1190 #ifndef GST_DISABLE_REGISTRY
1191   if (!__registry_reuse_plugin_scanner) {
1192     clear_scan_context (context);
1193     context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
1194   }
1195 #endif
1196
1197   return changed;
1198 }
1199
1200 static gboolean
1201 is_blacklisted_hidden_directory (const gchar * dirent)
1202 {
1203   if (G_LIKELY (dirent[0] != '.'))
1204     return FALSE;
1205
1206   /* skip the .debug directory, these contain elf files that are not
1207    * useful or worse, can crash dlopen () */
1208   if (strcmp (dirent, ".debug") == 0)
1209     return TRUE;
1210
1211   /* can also skip .git and .deps dirs, those won't contain useful files.
1212    * This speeds up scanning a bit in uninstalled setups. */
1213   if (strcmp (dirent, ".git") == 0 || strcmp (dirent, ".deps") == 0)
1214     return TRUE;
1215
1216   return FALSE;
1217 }
1218
1219 static gboolean
1220 gst_registry_scan_path_level (GstRegistryScanContext * context,
1221     const gchar * path, int level)
1222 {
1223   GDir *dir;
1224   const gchar *dirent;
1225   gchar *filename;
1226   GstPlugin *plugin;
1227   gboolean changed = FALSE;
1228
1229   dir = g_dir_open (path, 0, NULL);
1230   if (!dir)
1231     return FALSE;
1232
1233   while ((dirent = g_dir_read_name (dir))) {
1234     GStatBuf file_status;
1235
1236     filename = g_build_filename (path, dirent, NULL);
1237     if (g_stat (filename, &file_status) < 0) {
1238       /* Plugin will be removed from cache after the scan completes if it
1239        * is still marked 'cached' */
1240       g_free (filename);
1241       continue;
1242     }
1243
1244     if (file_status.st_mode & S_IFDIR) {
1245       if (G_UNLIKELY (is_blacklisted_hidden_directory (dirent))) {
1246         GST_TRACE_OBJECT (context->registry, "ignoring %s directory", dirent);
1247         g_free (filename);
1248         continue;
1249       }
1250       /* FIXME 0.11: Don't recurse into directories, this behaviour
1251        * is inconsistent with other PATH environment variables
1252        */
1253       if (level > 0) {
1254         GST_LOG_OBJECT (context->registry, "recursing into directory %s",
1255             filename);
1256         changed |= gst_registry_scan_path_level (context, filename, level - 1);
1257       } else {
1258         GST_LOG_OBJECT (context->registry, "not recursing into directory %s, "
1259             "recursion level too deep", filename);
1260       }
1261       g_free (filename);
1262       continue;
1263     }
1264     if (!(file_status.st_mode & S_IFREG)) {
1265       GST_TRACE_OBJECT (context->registry, "%s is not a regular file, ignoring",
1266           filename);
1267       g_free (filename);
1268       continue;
1269     }
1270     if (!g_str_has_suffix (dirent, G_MODULE_SUFFIX)
1271 #ifdef GST_EXTRA_MODULE_SUFFIX
1272         && !g_str_has_suffix (dirent, GST_EXTRA_MODULE_SUFFIX)
1273 #endif
1274         ) {
1275       GST_TRACE_OBJECT (context->registry,
1276           "extension is not recognized as module file, ignoring file %s",
1277           filename);
1278       g_free (filename);
1279       continue;
1280     }
1281
1282     GST_LOG_OBJECT (context->registry, "file %s looks like a possible module",
1283         filename);
1284
1285     /* try to avoid unnecessary plugin-move pain */
1286     if (g_str_has_prefix (dirent, "libgstvalve") ||
1287         g_str_has_prefix (dirent, "libgstselector")) {
1288       GST_WARNING_OBJECT (context->registry, "ignoring old plugin %s which "
1289           "has been merged into the corelements plugin", filename);
1290       /* Plugin will be removed from cache after the scan completes if it
1291        * is still marked 'cached' */
1292       g_free (filename);
1293       continue;
1294     }
1295
1296     /* plug-ins are considered unique by basename; if the given name
1297      * was already seen by the registry, we ignore it */
1298     plugin = gst_registry_lookup_bn (context->registry, dirent);
1299     if (plugin) {
1300       gboolean env_vars_changed, deps_changed = FALSE;
1301
1302       if (plugin->registered) {
1303         GST_DEBUG_OBJECT (context->registry,
1304             "plugin already registered from path \"%s\"",
1305             GST_STR_NULL (plugin->filename));
1306         g_free (filename);
1307         gst_object_unref (plugin);
1308         continue;
1309       }
1310
1311       env_vars_changed = _priv_plugin_deps_env_vars_changed (plugin);
1312
1313       /* If a file with a certain basename is seen on a different path,
1314        * update the plugin to ensure the registry cache will reflect up
1315        * to date information */
1316
1317       if (plugin->file_mtime == file_status.st_mtime &&
1318           plugin->file_size == file_status.st_size && !env_vars_changed &&
1319           !(deps_changed = _priv_plugin_deps_files_changed (plugin)) &&
1320           !strcmp (plugin->filename, filename)) {
1321         GST_LOG_OBJECT (context->registry, "file %s cached", filename);
1322         GST_OBJECT_FLAG_UNSET (plugin, GST_PLUGIN_FLAG_CACHED);
1323         GST_LOG_OBJECT (context->registry,
1324             "marking plugin %p as registered as %s", plugin, filename);
1325         plugin->registered = TRUE;
1326       } else {
1327         GST_INFO_OBJECT (context->registry, "cached info for %s is stale",
1328             filename);
1329         GST_DEBUG_OBJECT (context->registry, "mtime %" G_GINT64_FORMAT " != %"
1330             G_GINT64_FORMAT " or size %" G_GINT64_FORMAT " != %"
1331             G_GINT64_FORMAT " or external dependency env_vars changed: %d or"
1332             " external dependencies changed: %d or old path %s != new path %s",
1333             (gint64) plugin->file_mtime, (gint64) file_status.st_mtime,
1334             (gint64) plugin->file_size, (gint64) file_status.st_size,
1335             env_vars_changed, deps_changed, plugin->filename, filename);
1336         gst_registry_remove_plugin (context->registry, plugin);
1337         changed |= gst_registry_scan_plugin_file (context, filename,
1338             file_status.st_size, file_status.st_mtime);
1339       }
1340       gst_object_unref (plugin);
1341
1342     } else {
1343       GST_DEBUG_OBJECT (context->registry, "file %s not yet in registry",
1344           filename);
1345       changed |= gst_registry_scan_plugin_file (context, filename,
1346           file_status.st_size, file_status.st_mtime);
1347     }
1348
1349     g_free (filename);
1350   }
1351
1352   g_dir_close (dir);
1353
1354   return changed;
1355 }
1356
1357 static gboolean
1358 gst_registry_scan_path_internal (GstRegistryScanContext * context,
1359     const gchar * path)
1360 {
1361   gboolean changed;
1362
1363   GST_DEBUG_OBJECT (context->registry, "scanning path %s", path);
1364   changed = gst_registry_scan_path_level (context, path, 10);
1365
1366   GST_DEBUG_OBJECT (context->registry, "registry changed in path %s: %d", path,
1367       changed);
1368   return changed;
1369 }
1370
1371 /**
1372  * gst_registry_scan_path:
1373  * @registry: the registry to add found plugins to
1374  * @path: the path to scan
1375  *
1376  * Scan the given path for plugins to add to the registry. The syntax of the
1377  * path is specific to the registry.
1378  *
1379  * Returns: %TRUE if registry changed
1380  */
1381 gboolean
1382 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
1383 {
1384   GstRegistryScanContext context;
1385   gboolean result;
1386
1387   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1388   g_return_val_if_fail (path != NULL, FALSE);
1389
1390   init_scan_context (&context, registry);
1391
1392   result = gst_registry_scan_path_internal (&context, path);
1393
1394   clear_scan_context (&context);
1395   result |= context.changed;
1396
1397   return result;
1398 }
1399
1400 static gboolean
1401 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
1402     gpointer user_data)
1403 {
1404   return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
1405 }
1406
1407 /**
1408  * gst_registry_get_feature_list_by_plugin:
1409  * @registry: a #GstRegistry.
1410  * @name: a plugin name.
1411  *
1412  * Retrieves a #GList of features of the plugin with name @name.
1413  *
1414  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
1415  *     #GstPluginFeature. Use gst_plugin_feature_list_free() after usage.
1416  */
1417 GList *
1418 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
1419     const gchar * name)
1420 {
1421   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1422   g_return_val_if_fail (name != NULL, NULL);
1423
1424   return gst_registry_feature_filter (registry,
1425       _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1426 }
1427
1428 /* Unref and delete the default registry */
1429 void
1430 _priv_gst_registry_cleanup (void)
1431 {
1432   GstRegistry *registry;
1433
1434   g_mutex_lock (&_gst_registry_mutex);
1435   if ((registry = _gst_registry_default) != NULL) {
1436     _gst_registry_default = NULL;
1437   }
1438   g_mutex_unlock (&_gst_registry_mutex);
1439
1440   /* unref outside of the lock because we can. */
1441   if (registry)
1442     gst_object_unref (registry);
1443 }
1444
1445 /**
1446  * gst_registry_check_feature_version:
1447  * @registry: a #GstRegistry
1448  * @feature_name: the name of the feature (e.g. "oggdemux")
1449  * @min_major: the minimum major version number
1450  * @min_minor: the minimum minor version number
1451  * @min_micro: the minimum micro version number
1452  *
1453  * Checks whether a plugin feature by the given name exists in
1454  * @registry and whether its version is at least the
1455  * version required.
1456  *
1457  * Returns: #TRUE if the feature could be found and the version is
1458  * the same as the required version or newer, and #FALSE otherwise.
1459  */
1460 gboolean
1461 gst_registry_check_feature_version (GstRegistry * registry,
1462     const gchar * feature_name, guint min_major, guint min_minor,
1463     guint min_micro)
1464 {
1465   GstPluginFeature *feature;
1466   gboolean ret = FALSE;
1467
1468   g_return_val_if_fail (feature_name != NULL, FALSE);
1469
1470   GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1471
1472   feature = gst_registry_lookup_feature (registry, feature_name);
1473   if (feature) {
1474     ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1475         min_micro);
1476     gst_object_unref (feature);
1477   } else {
1478     GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1479   }
1480
1481   return ret;
1482 }
1483
1484 static void
1485 load_plugin_func (gpointer data, gpointer user_data)
1486 {
1487   GstPlugin *plugin;
1488   const gchar *filename;
1489   GError *err = NULL;
1490
1491   filename = (const gchar *) data;
1492   GST_DEBUG ("Pre-loading plugin %s", filename);
1493
1494   plugin = gst_plugin_load_file (filename, &err);
1495
1496   if (plugin) {
1497     GST_INFO ("Loaded plugin: \"%s\"", filename);
1498
1499     gst_registry_add_plugin (gst_registry_get (), plugin);
1500   } else {
1501     if (err) {
1502       /* Report error to user, and free error */
1503       GST_ERROR ("Failed to load plugin: %s", err->message);
1504       g_error_free (err);
1505     } else {
1506       GST_WARNING ("Failed to load plugin: \"%s\"", filename);
1507     }
1508   }
1509 }
1510
1511 #ifndef GST_DISABLE_REGISTRY
1512 /* Unref all plugins marked 'cached', to clear old plugins that no
1513  * longer exist. Returns TRUE if any plugins were removed */
1514 static gboolean
1515 gst_registry_remove_cache_plugins (GstRegistry * registry)
1516 {
1517   GList *g;
1518   GList *g_next;
1519   GstPlugin *plugin;
1520   gboolean changed = FALSE;
1521
1522   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1523
1524   GST_OBJECT_LOCK (registry);
1525
1526   GST_DEBUG_OBJECT (registry, "removing cached plugins");
1527   g = registry->priv->plugins;
1528   while (g) {
1529     g_next = g->next;
1530     plugin = g->data;
1531     if (GST_OBJECT_FLAG_IS_SET (plugin, GST_PLUGIN_FLAG_CACHED)) {
1532       GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
1533           GST_STR_NULL (plugin->filename));
1534       registry->priv->plugins = g_list_delete_link (registry->priv->plugins, g);
1535       if (G_LIKELY (plugin->basename))
1536         g_hash_table_remove (registry->priv->basename_hash, plugin->basename);
1537       gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
1538       gst_object_unref (plugin);
1539       changed = TRUE;
1540     }
1541     g = g_next;
1542   }
1543
1544   GST_OBJECT_UNLOCK (registry);
1545
1546   return changed;
1547 }
1548
1549 typedef enum
1550 {
1551   REGISTRY_SCAN_AND_UPDATE_FAILURE = 0,
1552   REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED,
1553   REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED
1554 } GstRegistryScanAndUpdateResult;
1555
1556 /*
1557  * scan_and_update_registry:
1558  * @default_registry: the #GstRegistry
1559  * @registry_file: registry filename
1560  * @write_changes: write registry if it has changed?
1561  *
1562  * Scans for registry changes and eventually updates the registry cache.
1563  *
1564  * Return: %REGISTRY_SCAN_AND_UPDATE_FAILURE if the registry could not scanned
1565  *         or updated, %REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED if the
1566  *         registry is clean and %REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED if
1567  *         it has been updated and the cache needs to be re-read.
1568  */
1569 static GstRegistryScanAndUpdateResult
1570 scan_and_update_registry (GstRegistry * default_registry,
1571     const gchar * registry_file, gboolean write_changes, GError ** error)
1572 {
1573   const gchar *plugin_path;
1574   gboolean changed = FALSE;
1575   GList *l;
1576   GstRegistryScanContext context;
1577
1578   GST_INFO ("Validating plugins from registry cache: %s", registry_file);
1579
1580   init_scan_context (&context, default_registry);
1581
1582   /* It sounds tempting to just compare the mtime of directories with the mtime
1583    * of the registry cache, but it does not work. It would not catch updated
1584    * plugins, which might bring more or less features.
1585    */
1586
1587   /* scan paths specified via --gst-plugin-path */
1588   GST_DEBUG ("scanning paths added via --gst-plugin-path");
1589   for (l = _priv_gst_plugin_paths; l != NULL; l = l->next) {
1590     GST_INFO ("Scanning plugin path: \"%s\"", (gchar *) l->data);
1591     changed |= gst_registry_scan_path_internal (&context, (gchar *) l->data);
1592   }
1593   /* keep plugin_paths around in case a re-scan is forced later on */
1594
1595   /* GST_PLUGIN_PATH specifies a list of directories to scan for
1596    * additional plugins.  These take precedence over the system plugins */
1597   plugin_path = g_getenv ("GST_PLUGIN_PATH_1_0");
1598   if (plugin_path == NULL)
1599     plugin_path = g_getenv ("GST_PLUGIN_PATH");
1600   if (plugin_path) {
1601     char **list;
1602     int i;
1603
1604     GST_DEBUG ("GST_PLUGIN_PATH set to %s", plugin_path);
1605     list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1606     for (i = 0; list[i]; i++) {
1607       changed |= gst_registry_scan_path_internal (&context, list[i]);
1608     }
1609     g_strfreev (list);
1610   } else {
1611     GST_DEBUG ("GST_PLUGIN_PATH not set");
1612   }
1613
1614   /* GST_PLUGIN_SYSTEM_PATH specifies a list of plugins that are always
1615    * loaded by default.  If not set, this defaults to the system-installed
1616    * path, and the plugins installed in the user's home directory */
1617   plugin_path = g_getenv ("GST_PLUGIN_SYSTEM_PATH_1_0");
1618   if (plugin_path == NULL)
1619     plugin_path = g_getenv ("GST_PLUGIN_SYSTEM_PATH");
1620   if (plugin_path == NULL) {
1621     char *home_plugins;
1622
1623     GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH not set");
1624
1625     /* plugins in the user's home directory take precedence over
1626      * system-installed ones */
1627     home_plugins = g_build_filename (g_get_user_data_dir (),
1628         "gstreamer-" GST_API_VERSION, "plugins", NULL);
1629
1630     GST_DEBUG ("scanning home plugins %s", home_plugins);
1631     changed |= gst_registry_scan_path_internal (&context, home_plugins);
1632     g_free (home_plugins);
1633
1634     /* add the main (installed) library path */
1635
1636 #ifdef G_OS_WIN32
1637     {
1638       char *base_dir;
1639       char *dir;
1640
1641       base_dir =
1642           g_win32_get_package_installation_directory_of_module
1643           (_priv_gst_dll_handle);
1644
1645       dir =
1646           g_build_filename (base_dir,
1647 #ifdef _DEBUG
1648                             "debug"
1649 #endif
1650                             "lib",
1651                             "gstreamer-" GST_API_VERSION,
1652                             NULL);
1653       GST_DEBUG ("scanning DLL dir %s", dir);
1654
1655       changed |= gst_registry_scan_path_internal (&context, dir);
1656
1657       g_free (dir);
1658       g_free (base_dir);
1659     }
1660 #else
1661     GST_DEBUG ("scanning main plugins %s", PLUGINDIR);
1662     changed |= gst_registry_scan_path_internal (&context, PLUGINDIR);
1663 #endif
1664   } else {
1665     gchar **list;
1666     gint i;
1667
1668     GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH set to %s", plugin_path);
1669     list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1670     for (i = 0; list[i]; i++) {
1671       changed |= gst_registry_scan_path_internal (&context, list[i]);
1672     }
1673     g_strfreev (list);
1674   }
1675
1676   clear_scan_context (&context);
1677   changed |= context.changed;
1678
1679   /* Remove cached plugins so stale info is cleared. */
1680   changed |= gst_registry_remove_cache_plugins (default_registry);
1681
1682   if (!changed) {
1683     GST_INFO ("Registry cache has not changed");
1684     return REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED;
1685   }
1686
1687   if (!write_changes) {
1688     GST_INFO ("Registry cache changed, but writing is disabled. Not writing.");
1689     return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1690   }
1691
1692   GST_INFO ("Registry cache changed. Writing new registry cache");
1693   if (!priv_gst_registry_binary_write_cache (default_registry,
1694           default_registry->priv->plugins, registry_file)) {
1695     g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
1696         _("Error writing registry cache to %s: %s"),
1697         registry_file, g_strerror (errno));
1698     return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1699   }
1700
1701   GST_INFO ("Registry cache written successfully");
1702   return REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED;
1703 }
1704
1705 static gboolean
1706 ensure_current_registry (GError ** error)
1707 {
1708   gchar *registry_file;
1709   GstRegistry *default_registry;
1710   gboolean ret = TRUE;
1711   gboolean do_update = TRUE;
1712   gboolean have_cache = TRUE;
1713
1714   default_registry = gst_registry_get ();
1715
1716   registry_file = g_strdup (g_getenv ("GST_REGISTRY_1_0"));
1717   if (registry_file == NULL)
1718     registry_file = g_strdup (g_getenv ("GST_REGISTRY"));
1719   if (registry_file == NULL) {
1720     registry_file = g_build_filename (g_get_user_cache_dir (),
1721         "gstreamer-" GST_API_VERSION, "registry." TARGET_CPU ".bin", NULL);
1722   }
1723
1724   if (!_gst_disable_registry_cache) {
1725     GST_INFO ("reading registry cache: %s", registry_file);
1726     have_cache = priv_gst_registry_binary_read_cache (default_registry,
1727         registry_file);
1728     /* Only ever read the registry cache once, then disable it for
1729      * subsequent updates during the program lifetime */
1730     _gst_disable_registry_cache = TRUE;
1731   }
1732
1733   if (have_cache) {
1734     do_update = !_priv_gst_disable_registry_update;
1735     if (do_update) {
1736       const gchar *update_env;
1737
1738       if ((update_env = g_getenv ("GST_REGISTRY_UPDATE"))) {
1739         /* do update for any value different from "no" */
1740         do_update = (strcmp (update_env, "no") != 0);
1741       }
1742     }
1743   }
1744
1745   if (do_update) {
1746     const gchar *reuse_env;
1747
1748     if ((reuse_env = g_getenv ("GST_REGISTRY_REUSE_PLUGIN_SCANNER"))) {
1749       /* do reuse for any value different from "no" */
1750       __registry_reuse_plugin_scanner = (strcmp (reuse_env, "no") != 0);
1751     }
1752     /* now check registry */
1753     GST_DEBUG ("Updating registry cache");
1754     scan_and_update_registry (default_registry, registry_file, TRUE, error);
1755   } else {
1756     GST_DEBUG ("Not updating registry cache (disabled)");
1757   }
1758
1759   g_free (registry_file);
1760   GST_INFO ("registry reading and updating done, result = %d", ret);
1761
1762   return ret;
1763 }
1764 #endif /* GST_DISABLE_REGISTRY */
1765
1766 /**
1767  * gst_registry_fork_is_enabled:
1768  *
1769  * By default GStreamer will perform scanning and rebuilding of the
1770  * registry file using a helper child process.
1771  *
1772  * Applications might want to disable this behaviour with the
1773  * gst_registry_fork_set_enabled() function, in which case new plugins
1774  * are scanned (and loaded) into the application process.
1775  *
1776  * Returns: %TRUE if GStreamer will use the child helper process when
1777  * rebuilding the registry.
1778  */
1779 gboolean
1780 gst_registry_fork_is_enabled (void)
1781 {
1782   return _gst_enable_registry_fork;
1783 }
1784
1785 /**
1786  * gst_registry_fork_set_enabled:
1787  * @enabled: whether rebuilding the registry can use a temporary child helper process.
1788  *
1789  * Applications might want to disable/enable spawning of a child helper process
1790  * when rebuilding the registry. See gst_registry_fork_is_enabled() for more
1791  * information.
1792  */
1793 void
1794 gst_registry_fork_set_enabled (gboolean enabled)
1795 {
1796   _gst_enable_registry_fork = enabled;
1797 }
1798
1799 /**
1800  * gst_update_registry:
1801  *
1802  * Forces GStreamer to re-scan its plugin paths and update the default
1803  * plugin registry.
1804  *
1805  * Applications will almost never need to call this function, it is only
1806  * useful if the application knows new plugins have been installed (or old
1807  * ones removed) since the start of the application (or, to be precise, the
1808  * first call to gst_init()) and the application wants to make use of any
1809  * newly-installed plugins without restarting the application.
1810  *
1811  * Applications should assume that the registry update is neither atomic nor
1812  * thread-safe and should therefore not have any dynamic pipelines running
1813  * (including the playbin and decodebin elements) and should also not create
1814  * any elements or access the GStreamer registry while the update is in
1815  * progress.
1816  *
1817  * Note that this function may block for a significant amount of time.
1818  *
1819  * Returns: %TRUE if the registry has been updated successfully (does not
1820  *          imply that there were changes), otherwise %FALSE.
1821  */
1822 gboolean
1823 gst_update_registry (void)
1824 {
1825   gboolean res;
1826
1827 #ifndef GST_DISABLE_REGISTRY
1828   GError *err = NULL;
1829
1830   res = ensure_current_registry (&err);
1831   if (err) {
1832     GST_WARNING ("registry update failed: %s", err->message);
1833     g_error_free (err);
1834   } else {
1835     GST_LOG ("registry update succeeded");
1836   }
1837
1838 #else
1839   GST_WARNING ("registry update failed: %s", "registry disabled");
1840   res = TRUE;
1841 #endif /* GST_DISABLE_REGISTRY */
1842
1843   if (_priv_gst_preload_plugins) {
1844     GST_DEBUG ("Preloading indicated plugins...");
1845     g_slist_foreach (_priv_gst_preload_plugins, load_plugin_func, NULL);
1846   }
1847
1848   return res;
1849 }
1850
1851 /**
1852  * gst_registry_get_feature_list_cookie:
1853  * @registry: the registry
1854  *
1855  * Returns the registry's feature list cookie. This changes
1856  * every time a feature is added or removed from the registry.
1857  *
1858  * Returns: the feature list cookie.
1859  */
1860 guint32
1861 gst_registry_get_feature_list_cookie (GstRegistry * registry)
1862 {
1863   g_return_val_if_fail (GST_IS_REGISTRY (registry), 0);
1864
1865   return registry->priv->cookie;
1866 }