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