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