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
132 #include "gstpluginloader.h"
133
134 #include "gst-i18n-lib.h"
135
136 #include "gst.h"
137 #include "glib-compat-private.h"
138
139 #ifdef G_OS_WIN32
140 #include <windows.h>
141 extern HMODULE _priv_gst_dll_handle;
142 #endif
143
144 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
145
146 struct _GstRegistryPrivate
147 {
148   /* updated whenever the feature list changes */
149   guint32 cookie;
150   /* speedup for searching features */
151   GList *element_factory_list;
152   guint32 efl_cookie;
153   GList *typefind_factory_list;
154   guint32 tfl_cookie;
155 };
156
157 /* the one instance of the default registry and the mutex protecting the
158  * variable. */
159 static GStaticMutex _gst_registry_mutex = G_STATIC_MUTEX_INIT;
160 static GstRegistry *_gst_registry_default = NULL;
161
162 /* defaults */
163 #define DEFAULT_FORK TRUE
164
165 /* control the behaviour of registry rebuild */
166 static gboolean _gst_enable_registry_fork = DEFAULT_FORK;
167 /* List of plugins that need preloading/reloading after scanning registry */
168 extern GSList *_priv_gst_preload_plugins;
169
170 #ifndef GST_DISABLE_REGISTRY
171 /*set to TRUE when registry needn't to be updated */
172 gboolean _priv_gst_disable_registry_update = FALSE;
173 extern GList *_priv_gst_plugin_paths;
174
175 /* Set to TRUE when the registry cache should be disabled */
176 gboolean _gst_disable_registry_cache = FALSE;
177
178 static gboolean __registry_reuse_plugin_scanner = TRUE;
179 #endif
180
181 /* Element signals and args */
182 enum
183 {
184   PLUGIN_ADDED,
185   FEATURE_ADDED,
186   LAST_SIGNAL
187 };
188
189 static void gst_registry_finalize (GObject * object);
190
191 static guint gst_registry_signals[LAST_SIGNAL] = { 0 };
192
193 static GstPluginFeature *gst_registry_lookup_feature_locked (GstRegistry *
194     registry, const char *name);
195 static GstPlugin *gst_registry_lookup_bn_locked (GstRegistry * registry,
196     const char *basename);
197
198 #define gst_registry_parent_class parent_class
199 G_DEFINE_TYPE (GstRegistry, gst_registry, GST_TYPE_OBJECT);
200
201 static void
202 gst_registry_class_init (GstRegistryClass * klass)
203 {
204   GObjectClass *gobject_class;
205
206   gobject_class = (GObjectClass *) klass;
207
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)", feature,
282           GST_OBJECT_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 = NULL;
631
632   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
633
634   GST_OBJECT_LOCK (registry);
635   {
636     const GList *walk;
637
638     for (walk = registry->plugins; walk != NULL; walk = walk->next) {
639       GstPlugin *plugin = walk->data;
640
641       if (filter == NULL || filter (plugin, user_data)) {
642         list = g_list_prepend (list, gst_object_ref (plugin));
643
644         if (first)
645           break;
646       }
647     }
648   }
649   GST_OBJECT_UNLOCK (registry);
650
651   return list;
652 }
653
654 typedef struct
655 {
656   const gchar *name;
657   GType type;
658 } GstTypeNameData;
659
660 static gboolean
661 gst_plugin_feature_type_name_filter (GstPluginFeature * feature,
662     GstTypeNameData * data)
663 {
664   g_assert (GST_IS_PLUGIN_FEATURE (feature));
665
666   return ((data->type == 0 || data->type == G_OBJECT_TYPE (feature)) &&
667       (data->name == NULL || !strcmp (data->name, GST_OBJECT_NAME (feature))));
668 }
669
670 /* returns TRUE if the list was changed
671  *
672  * Must be called with the object lock taken */
673 static gboolean
674 gst_registry_get_feature_list_or_create (GstRegistry * registry,
675     GList ** previous, guint32 * cookie, GType type)
676 {
677   gboolean res = FALSE;
678   GstRegistryPrivate *priv = registry->priv;
679
680   if (G_UNLIKELY (!*previous || priv->cookie != *cookie)) {
681     GstTypeNameData data;
682     const GList *walk;
683
684     if (*previous) {
685       gst_plugin_feature_list_free (*previous);
686       *previous = NULL;
687     }
688
689     data.type = type;
690     data.name = NULL;
691
692     for (walk = registry->features; walk != NULL; walk = walk->next) {
693       GstPluginFeature *feature = walk->data;
694
695       if (gst_plugin_feature_type_name_filter (feature, &data)) {
696         *previous = g_list_prepend (*previous, gst_object_ref (feature));
697       }
698     }
699
700     *cookie = priv->cookie;
701     res = TRUE;
702   }
703
704   return res;
705 }
706
707 static gint
708 type_find_factory_rank_cmp (const GstPluginFeature * fac1,
709     const GstPluginFeature * fac2)
710 {
711   if (G_LIKELY (fac1->rank != fac2->rank))
712     return fac2->rank - fac1->rank;
713
714   /* to make the order in which things happen more deterministic,
715    * sort by name when the ranks are the same. */
716   return strcmp (GST_OBJECT_NAME (fac1), GST_OBJECT_NAME (fac2));
717 }
718
719 static GList *
720 gst_registry_get_element_factory_list (GstRegistry * registry)
721 {
722   GList *list;
723
724   GST_OBJECT_LOCK (registry);
725
726   gst_registry_get_feature_list_or_create (registry,
727       &registry->priv->element_factory_list, &registry->priv->efl_cookie,
728       GST_TYPE_ELEMENT_FACTORY);
729
730   /* Return reffed copy */
731   list = gst_plugin_feature_list_copy (registry->priv->element_factory_list);
732
733   GST_OBJECT_UNLOCK (registry);
734
735   return list;
736 }
737
738 static GList *
739 gst_registry_get_typefind_factory_list (GstRegistry * registry)
740 {
741   GList *list;
742
743   GST_OBJECT_LOCK (registry);
744
745   if (G_UNLIKELY (gst_registry_get_feature_list_or_create (registry,
746               &registry->priv->typefind_factory_list,
747               &registry->priv->tfl_cookie, GST_TYPE_TYPE_FIND_FACTORY)))
748     registry->priv->typefind_factory_list =
749         g_list_sort (registry->priv->typefind_factory_list,
750         (GCompareFunc) type_find_factory_rank_cmp);
751
752   /* Return reffed copy */
753   list = gst_plugin_feature_list_copy (registry->priv->typefind_factory_list);
754
755   GST_OBJECT_UNLOCK (registry);
756
757   return list;
758 }
759
760 /**
761  * gst_registry_feature_filter:
762  * @registry: registry to query
763  * @filter: (scope call): the filter to use
764  * @first: only return first match
765  * @user_data: (closure): user data passed to the filter function
766  *
767  * Runs a filter against all features of the plugins in the registry
768  * and returns a GList with the results.
769  * If the first flag is set, only the first match is
770  * returned (as a list with a single object).
771  *
772  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
773  *     #GstPluginFeature. Use gst_plugin_feature_list_free() after usage.
774  *
775  * MT safe.
776  */
777 GList *
778 gst_registry_feature_filter (GstRegistry * registry,
779     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
780 {
781   GList *list = NULL;
782
783   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
784
785   GST_OBJECT_LOCK (registry);
786   {
787     const GList *walk;
788
789     for (walk = registry->features; walk != NULL; walk = walk->next) {
790       GstPluginFeature *feature = walk->data;
791
792       if (filter == NULL || filter (feature, user_data)) {
793         list = g_list_prepend (list, gst_object_ref (feature));
794
795         if (first)
796           break;
797       }
798     }
799   }
800   GST_OBJECT_UNLOCK (registry);
801
802   return list;
803 }
804
805 /**
806  * gst_registry_find_plugin:
807  * @registry: the registry to search
808  * @name: the plugin name to find
809  *
810  * Find the plugin with the given name in the registry.
811  * The plugin will be reffed; caller is responsible for unreffing.
812  *
813  * Returns: (transfer full): the plugin with the given name or NULL if the
814  *     plugin was not found. gst_object_unref() after usage.
815  *
816  * MT safe.
817  */
818 GstPlugin *
819 gst_registry_find_plugin (GstRegistry * registry, const gchar * name)
820 {
821   GList *walk;
822   GstPlugin *result = NULL;
823
824   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
825   g_return_val_if_fail (name != NULL, NULL);
826
827   walk = gst_registry_plugin_filter (registry,
828       (GstPluginFilter) gst_plugin_name_filter, TRUE, (gpointer) name);
829   if (walk) {
830     result = GST_PLUGIN_CAST (walk->data);
831
832     gst_object_ref (result);
833     gst_plugin_list_free (walk);
834   }
835
836   return result;
837 }
838
839 /**
840  * gst_registry_find_feature:
841  * @registry: the registry to search
842  * @name: the pluginfeature name to find
843  * @type: the pluginfeature type to find
844  *
845  * Find the pluginfeature with the given name and type in the registry.
846  *
847  * Returns: (transfer full): the pluginfeature with the given name and type
848  *     or NULL if the plugin was not found. gst_object_unref() after usage.
849  *
850  * MT safe.
851  */
852 GstPluginFeature *
853 gst_registry_find_feature (GstRegistry * registry, const gchar * name,
854     GType type)
855 {
856   GstPluginFeature *feature = NULL;
857
858   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
859   g_return_val_if_fail (name != NULL, NULL);
860   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
861
862   feature = gst_registry_lookup_feature (registry, name);
863   if (feature && !g_type_is_a (G_TYPE_FROM_INSTANCE (feature), type)) {
864     gst_object_unref (feature);
865     feature = NULL;
866   }
867
868   return feature;
869 }
870
871 /**
872  * gst_registry_get_feature_list:
873  * @registry: a #GstRegistry
874  * @type: a #GType.
875  *
876  * Retrieves a #GList of #GstPluginFeature of @type.
877  *
878  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
879  *     #GstPluginFeature of @type. Use gst_plugin_feature_list_free() after use
880  *
881  * MT safe.
882  */
883 GList *
884 gst_registry_get_feature_list (GstRegistry * registry, GType type)
885 {
886   GstTypeNameData data;
887
888   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
889   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
890
891   /* Speed up */
892   if (type == GST_TYPE_ELEMENT_FACTORY)
893     return gst_registry_get_element_factory_list (registry);
894   else if (type == GST_TYPE_TYPE_FIND_FACTORY)
895     return gst_registry_get_typefind_factory_list (registry);
896
897   data.type = type;
898   data.name = NULL;
899
900   return gst_registry_feature_filter (registry,
901       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
902       FALSE, &data);
903 }
904
905 /**
906  * gst_registry_get_plugin_list:
907  * @registry: the registry to search
908  *
909  * Get a copy of all plugins registered in the given registry. The refcount
910  * of each element in the list in incremented.
911  *
912  * Returns: (transfer full) (element-type Gst.Plugin): a #GList of #GstPlugin.
913  *     Use gst_plugin_list_free() after usage.
914  *
915  * MT safe.
916  */
917 GList *
918 gst_registry_get_plugin_list (GstRegistry * registry)
919 {
920   GList *list;
921   GList *g;
922
923   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
924
925   GST_OBJECT_LOCK (registry);
926   list = g_list_copy (registry->plugins);
927   for (g = list; g; g = g->next) {
928     gst_object_ref (GST_PLUGIN_CAST (g->data));
929   }
930   GST_OBJECT_UNLOCK (registry);
931
932   return list;
933 }
934
935 static GstPluginFeature *
936 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
937 {
938   return g_hash_table_lookup (registry->feature_hash, name);
939 }
940
941 /**
942  * gst_registry_lookup_feature:
943  * @registry: a #GstRegistry
944  * @name: a #GstPluginFeature name
945  *
946  * Find a #GstPluginFeature with @name in @registry.
947  *
948  * Returns: (transfer full): a #GstPluginFeature with its refcount incremented,
949  *     use gst_object_unref() after usage.
950  *
951  * MT safe.
952  */
953 GstPluginFeature *
954 gst_registry_lookup_feature (GstRegistry * registry, const char *name)
955 {
956   GstPluginFeature *feature;
957
958   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
959   g_return_val_if_fail (name != NULL, NULL);
960
961   GST_OBJECT_LOCK (registry);
962   feature = gst_registry_lookup_feature_locked (registry, name);
963   if (feature)
964     gst_object_ref (feature);
965   GST_OBJECT_UNLOCK (registry);
966
967   return feature;
968 }
969
970 static GstPlugin *
971 gst_registry_lookup_bn_locked (GstRegistry * registry, const char *basename)
972 {
973   return g_hash_table_lookup (registry->basename_hash, basename);
974 }
975
976 static GstPlugin *
977 gst_registry_lookup_bn (GstRegistry * registry, const char *basename)
978 {
979   GstPlugin *plugin;
980
981   GST_OBJECT_LOCK (registry);
982   plugin = gst_registry_lookup_bn_locked (registry, basename);
983   if (plugin)
984     gst_object_ref (plugin);
985   GST_OBJECT_UNLOCK (registry);
986
987   return plugin;
988 }
989
990 /**
991  * gst_registry_lookup:
992  * @registry: the registry to look up in
993  * @filename: the name of the file to look up
994  *
995  * Look up a plugin in the given registry with the given filename.
996  * If found, plugin is reffed.
997  *
998  * Returns: (transfer full): the #GstPlugin if found, or NULL if not.
999  *     gst_object_unref() after usage.
1000  */
1001 GstPlugin *
1002 gst_registry_lookup (GstRegistry * registry, const char *filename)
1003 {
1004   GstPlugin *plugin;
1005   gchar *basename;
1006
1007   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1008   g_return_val_if_fail (filename != NULL, NULL);
1009
1010   basename = g_path_get_basename (filename);
1011   if (G_UNLIKELY (basename == NULL))
1012     return NULL;
1013
1014   plugin = gst_registry_lookup_bn (registry, basename);
1015
1016   g_free (basename);
1017
1018   return plugin;
1019 }
1020
1021 typedef enum
1022 {
1023   REGISTRY_SCAN_HELPER_NOT_STARTED = 0,
1024   REGISTRY_SCAN_HELPER_DISABLED,
1025   REGISTRY_SCAN_HELPER_RUNNING
1026 } GstRegistryScanHelperState;
1027
1028 typedef struct
1029 {
1030   GstRegistry *registry;
1031   GstRegistryScanHelperState helper_state;
1032   GstPluginLoader *helper;
1033   gboolean changed;
1034 } GstRegistryScanContext;
1035
1036 static void
1037 init_scan_context (GstRegistryScanContext * context, GstRegistry * registry)
1038 {
1039   gboolean do_fork;
1040
1041   context->registry = registry;
1042
1043   /* see if forking is enabled and set up the scan helper state accordingly */
1044   do_fork = _gst_enable_registry_fork;
1045   if (do_fork) {
1046     const gchar *fork_env;
1047
1048     /* forking enabled, see if it is disabled with an env var */
1049     if ((fork_env = g_getenv ("GST_REGISTRY_FORK"))) {
1050       /* fork enabled for any value different from "no" */
1051       do_fork = strcmp (fork_env, "no") != 0;
1052     }
1053   }
1054
1055   if (do_fork)
1056     context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
1057   else
1058     context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1059
1060   context->helper = NULL;
1061   context->changed = FALSE;
1062 }
1063
1064 static void
1065 clear_scan_context (GstRegistryScanContext * context)
1066 {
1067   if (context->helper) {
1068     context->changed |= _priv_gst_plugin_loader_funcs.destroy (context->helper);
1069     context->helper = NULL;
1070   }
1071 }
1072
1073 static gboolean
1074 gst_registry_scan_plugin_file (GstRegistryScanContext * context,
1075     const gchar * filename, off_t file_size, time_t file_mtime)
1076 {
1077   gboolean changed = FALSE;
1078   GstPlugin *newplugin = NULL;
1079
1080 #ifdef G_OS_WIN32
1081   /* Disable external plugin loader on Windows until it is ported properly. */
1082   context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1083 #endif
1084
1085
1086   /* Have a plugin to load - see if the scan-helper needs starting */
1087   if (context->helper_state == REGISTRY_SCAN_HELPER_NOT_STARTED) {
1088     GST_DEBUG ("Starting plugin scanner for file %s", filename);
1089     context->helper = _priv_gst_plugin_loader_funcs.create (context->registry);
1090     if (context->helper != NULL)
1091       context->helper_state = REGISTRY_SCAN_HELPER_RUNNING;
1092     else {
1093       GST_WARNING ("Failed starting plugin scanner. Scanning in-process");
1094       context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1095     }
1096   }
1097
1098   if (context->helper_state == REGISTRY_SCAN_HELPER_RUNNING) {
1099     GST_DEBUG ("Using scan-helper to load plugin %s", filename);
1100     if (!_priv_gst_plugin_loader_funcs.load (context->helper,
1101             filename, file_size, file_mtime)) {
1102       g_warning ("External plugin loader failed. This most likely means that "
1103           "the plugin loader helper binary was not found or could not be run. "
1104           "%s", (g_getenv ("GST_PLUGIN_PATH") != NULL) ?
1105           "If you are running an uninstalled GStreamer setup, you might need "
1106           "to update your gst-uninstalled script so that the "
1107           "GST_PLUGIN_SCANNER environment variable gets set." : "");
1108       context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1109     }
1110   }
1111
1112   /* Check if the helper is disabled (or just got disabled above) */
1113   if (context->helper_state == REGISTRY_SCAN_HELPER_DISABLED) {
1114     /* Load plugin the old fashioned way... */
1115
1116     /* We don't use a GError here because a failure to load some shared
1117      * objects as plugins is normal (particularly in the uninstalled case)
1118      */
1119     newplugin = gst_plugin_load_file (filename, NULL);
1120   }
1121
1122   if (newplugin) {
1123     GST_DEBUG_OBJECT (context->registry, "marking new plugin %p as registered",
1124         newplugin);
1125     newplugin->registered = TRUE;
1126     gst_object_unref (newplugin);
1127     changed = TRUE;
1128   }
1129
1130   if (!__registry_reuse_plugin_scanner) {
1131     clear_scan_context (context);
1132     context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
1133   }
1134
1135   return changed;
1136 }
1137
1138 static gboolean
1139 is_blacklisted_hidden_directory (const gchar * dirent)
1140 {
1141   if (G_LIKELY (dirent[0] != '.'))
1142     return FALSE;
1143
1144   /* skip the .debug directory, these contain elf files that are not
1145    * useful or worse, can crash dlopen () */
1146   if (strcmp (dirent, ".debug") == 0)
1147     return TRUE;
1148
1149   /* can also skip .git and .deps dirs, those won't contain useful files.
1150    * This speeds up scanning a bit in uninstalled setups. */
1151   if (strcmp (dirent, ".git") == 0 || strcmp (dirent, ".deps") == 0)
1152     return TRUE;
1153
1154   return FALSE;
1155 }
1156
1157 static gboolean
1158 gst_registry_scan_path_level (GstRegistryScanContext * context,
1159     const gchar * path, int level)
1160 {
1161   GDir *dir;
1162   const gchar *dirent;
1163   gchar *filename;
1164   GstPlugin *plugin;
1165   gboolean changed = FALSE;
1166
1167   dir = g_dir_open (path, 0, NULL);
1168   if (!dir)
1169     return FALSE;
1170
1171   while ((dirent = g_dir_read_name (dir))) {
1172     GStatBuf file_status;
1173
1174     filename = g_build_filename (path, dirent, NULL);
1175     if (g_stat (filename, &file_status) < 0) {
1176       /* Plugin will be removed from cache after the scan completes if it
1177        * is still marked 'cached' */
1178       g_free (filename);
1179       continue;
1180     }
1181
1182     if (file_status.st_mode & S_IFDIR) {
1183       if (G_UNLIKELY (is_blacklisted_hidden_directory (dirent))) {
1184         GST_TRACE_OBJECT (context->registry, "ignoring %s directory", dirent);
1185         g_free (filename);
1186         continue;
1187       }
1188       /* FIXME 0.11: Don't recurse into directories, this behaviour
1189        * is inconsistent with other PATH environment variables
1190        */
1191       if (level > 0) {
1192         GST_LOG_OBJECT (context->registry, "recursing into directory %s",
1193             filename);
1194         changed |= gst_registry_scan_path_level (context, filename, level - 1);
1195       } else {
1196         GST_LOG_OBJECT (context->registry, "not recursing into directory %s, "
1197             "recursion level too deep", filename);
1198       }
1199       g_free (filename);
1200       continue;
1201     }
1202     if (!(file_status.st_mode & S_IFREG)) {
1203       GST_TRACE_OBJECT (context->registry, "%s is not a regular file, ignoring",
1204           filename);
1205       g_free (filename);
1206       continue;
1207     }
1208     if (!g_str_has_suffix (dirent, G_MODULE_SUFFIX)
1209 #ifdef GST_EXTRA_MODULE_SUFFIX
1210         && !g_str_has_suffix (dirent, GST_EXTRA_MODULE_SUFFIX)
1211 #endif
1212         ) {
1213       GST_TRACE_OBJECT (context->registry,
1214           "extension is not recognized as module file, ignoring file %s",
1215           filename);
1216       g_free (filename);
1217       continue;
1218     }
1219
1220     GST_LOG_OBJECT (context->registry, "file %s looks like a possible module",
1221         filename);
1222
1223     /* try to avoid unnecessary plugin-move pain */
1224     if (g_str_has_prefix (dirent, "libgstvalve") ||
1225         g_str_has_prefix (dirent, "libgstselector")) {
1226       GST_WARNING_OBJECT (context->registry, "ignoring old plugin %s which "
1227           "has been merged into the corelements plugin", filename);
1228       /* Plugin will be removed from cache after the scan completes if it
1229        * is still marked 'cached' */
1230       g_free (filename);
1231       continue;
1232     }
1233
1234     /* plug-ins are considered unique by basename; if the given name
1235      * was already seen by the registry, we ignore it */
1236     plugin = gst_registry_lookup_bn (context->registry, dirent);
1237     if (plugin) {
1238       gboolean env_vars_changed, deps_changed = FALSE;
1239
1240       if (plugin->registered) {
1241         GST_DEBUG_OBJECT (context->registry,
1242             "plugin already registered from path \"%s\"",
1243             GST_STR_NULL (plugin->filename));
1244         g_free (filename);
1245         gst_object_unref (plugin);
1246         continue;
1247       }
1248
1249       env_vars_changed = _priv_plugin_deps_env_vars_changed (plugin);
1250
1251       /* If a file with a certain basename is seen on a different path,
1252        * update the plugin to ensure the registry cache will reflect up
1253        * to date information */
1254
1255       if (plugin->file_mtime == file_status.st_mtime &&
1256           plugin->file_size == file_status.st_size && !env_vars_changed &&
1257           !(deps_changed = _priv_plugin_deps_files_changed (plugin)) &&
1258           !strcmp (plugin->filename, filename)) {
1259         GST_LOG_OBJECT (context->registry, "file %s cached", filename);
1260         plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
1261         GST_LOG_OBJECT (context->registry,
1262             "marking plugin %p as registered as %s", plugin, filename);
1263         plugin->registered = TRUE;
1264       } else {
1265         GST_INFO_OBJECT (context->registry, "cached info for %s is stale",
1266             filename);
1267         GST_DEBUG_OBJECT (context->registry, "mtime %" G_GINT64_FORMAT " != %"
1268             G_GINT64_FORMAT " or size %" G_GINT64_FORMAT " != %"
1269             G_GINT64_FORMAT " or external dependency env_vars changed: %d or"
1270             " external dependencies changed: %d or old path %s != new path %s",
1271             (gint64) plugin->file_mtime, (gint64) file_status.st_mtime,
1272             (gint64) plugin->file_size, (gint64) file_status.st_size,
1273             env_vars_changed, deps_changed, plugin->filename, filename);
1274         gst_registry_remove_plugin (context->registry, plugin);
1275         changed |= gst_registry_scan_plugin_file (context, filename,
1276             file_status.st_size, file_status.st_mtime);
1277       }
1278       gst_object_unref (plugin);
1279
1280     } else {
1281       GST_DEBUG_OBJECT (context->registry, "file %s not yet in registry",
1282           filename);
1283       changed |= gst_registry_scan_plugin_file (context, filename,
1284           file_status.st_size, file_status.st_mtime);
1285     }
1286
1287     g_free (filename);
1288   }
1289
1290   g_dir_close (dir);
1291
1292   return changed;
1293 }
1294
1295 static gboolean
1296 gst_registry_scan_path_internal (GstRegistryScanContext * context,
1297     const gchar * path)
1298 {
1299   gboolean changed;
1300
1301   GST_DEBUG_OBJECT (context->registry, "scanning path %s", path);
1302   changed = gst_registry_scan_path_level (context, path, 10);
1303
1304   GST_DEBUG_OBJECT (context->registry, "registry changed in path %s: %d", path,
1305       changed);
1306   return changed;
1307 }
1308
1309 /**
1310  * gst_registry_scan_path:
1311  * @registry: the registry to add found plugins to
1312  * @path: the path to scan
1313  *
1314  * Scan the given path for plugins to add to the registry. The syntax of the
1315  * path is specific to the registry.
1316  *
1317  * Returns: %TRUE if registry changed
1318  */
1319 gboolean
1320 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
1321 {
1322   GstRegistryScanContext context;
1323   gboolean result;
1324
1325   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1326   g_return_val_if_fail (path != NULL, FALSE);
1327
1328   init_scan_context (&context, registry);
1329
1330   result = gst_registry_scan_path_internal (&context, path);
1331
1332   clear_scan_context (&context);
1333   result |= context.changed;
1334
1335   return result;
1336 }
1337
1338 static gboolean
1339 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
1340     gpointer user_data)
1341 {
1342   return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
1343 }
1344
1345 /**
1346  * gst_registry_get_feature_list_by_plugin:
1347  * @registry: a #GstRegistry.
1348  * @name: a plugin name.
1349  *
1350  * Retrieves a #GList of features of the plugin with name @name.
1351  *
1352  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
1353  *     #GstPluginFeature. Use gst_plugin_feature_list_free() after usage.
1354  */
1355 GList *
1356 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
1357     const gchar * name)
1358 {
1359   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1360   g_return_val_if_fail (name != NULL, NULL);
1361
1362   return gst_registry_feature_filter (registry,
1363       _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1364 }
1365
1366 /* Unref and delete the default registry */
1367 void
1368 _priv_gst_registry_cleanup (void)
1369 {
1370   GstRegistry *registry;
1371
1372   g_static_mutex_lock (&_gst_registry_mutex);
1373   if ((registry = _gst_registry_default) != NULL) {
1374     _gst_registry_default = NULL;
1375   }
1376   g_static_mutex_unlock (&_gst_registry_mutex);
1377
1378   /* unref outside of the lock because we can. */
1379   if (registry)
1380     gst_object_unref (registry);
1381 }
1382
1383 /**
1384  * gst_default_registry_check_feature_version:
1385  * @feature_name: the name of the feature (e.g. "oggdemux")
1386  * @min_major: the minimum major version number
1387  * @min_minor: the minimum minor version number
1388  * @min_micro: the minimum micro version number
1389  *
1390  * Checks whether a plugin feature by the given name exists in the
1391  * default registry and whether its version is at least the
1392  * version required.
1393  *
1394  * Returns: #TRUE if the feature could be found and the version is
1395  * the same as the required version or newer, and #FALSE otherwise.
1396  */
1397 gboolean
1398 gst_default_registry_check_feature_version (const gchar * feature_name,
1399     guint min_major, guint min_minor, guint min_micro)
1400 {
1401   GstPluginFeature *feature;
1402   GstRegistry *registry;
1403   gboolean ret = FALSE;
1404
1405   g_return_val_if_fail (feature_name != NULL, FALSE);
1406
1407   GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1408
1409   registry = gst_registry_get_default ();
1410   feature = gst_registry_lookup_feature (registry, feature_name);
1411   if (feature) {
1412     ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1413         min_micro);
1414     gst_object_unref (feature);
1415   } else {
1416     GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1417   }
1418
1419   return ret;
1420 }
1421
1422 static void
1423 load_plugin_func (gpointer data, gpointer user_data)
1424 {
1425   GstPlugin *plugin;
1426   const gchar *filename;
1427   GError *err = NULL;
1428
1429   filename = (const gchar *) data;
1430   GST_DEBUG ("Pre-loading plugin %s", filename);
1431
1432   plugin = gst_plugin_load_file (filename, &err);
1433
1434   if (plugin) {
1435     GST_INFO ("Loaded plugin: \"%s\"", filename);
1436
1437     gst_default_registry_add_plugin (plugin);
1438   } else {
1439     if (err) {
1440       /* Report error to user, and free error */
1441       GST_ERROR ("Failed to load plugin: %s", err->message);
1442       g_error_free (err);
1443     } else {
1444       GST_WARNING ("Failed to load plugin: \"%s\"", filename);
1445     }
1446   }
1447 }
1448
1449 #ifndef GST_DISABLE_REGISTRY
1450 /* Unref all plugins marked 'cached', to clear old plugins that no
1451  * longer exist. Returns TRUE if any plugins were removed */
1452 static gboolean
1453 gst_registry_remove_cache_plugins (GstRegistry * registry)
1454 {
1455   GList *g;
1456   GList *g_next;
1457   GstPlugin *plugin;
1458   gboolean changed = FALSE;
1459
1460   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1461
1462   GST_OBJECT_LOCK (registry);
1463
1464   GST_DEBUG_OBJECT (registry, "removing cached plugins");
1465   g = registry->plugins;
1466   while (g) {
1467     g_next = g->next;
1468     plugin = g->data;
1469     if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
1470       GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
1471           GST_STR_NULL (plugin->filename));
1472       registry->plugins = g_list_delete_link (registry->plugins, g);
1473       if (G_LIKELY (plugin->basename))
1474         g_hash_table_remove (registry->basename_hash, plugin->basename);
1475       gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
1476       gst_object_unref (plugin);
1477       changed = TRUE;
1478     }
1479     g = g_next;
1480   }
1481
1482   GST_OBJECT_UNLOCK (registry);
1483
1484   return changed;
1485 }
1486
1487 typedef enum
1488 {
1489   REGISTRY_SCAN_AND_UPDATE_FAILURE = 0,
1490   REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED,
1491   REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED
1492 } GstRegistryScanAndUpdateResult;
1493
1494 /*
1495  * scan_and_update_registry:
1496  * @default_registry: the #GstRegistry
1497  * @registry_file: registry filename
1498  * @write_changes: write registry if it has changed?
1499  *
1500  * Scans for registry changes and eventually updates the registry cache.
1501  *
1502  * Return: %REGISTRY_SCAN_AND_UPDATE_FAILURE if the registry could not scanned
1503  *         or updated, %REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED if the
1504  *         registry is clean and %REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED if
1505  *         it has been updated and the cache needs to be re-read.
1506  */
1507 static GstRegistryScanAndUpdateResult
1508 scan_and_update_registry (GstRegistry * default_registry,
1509     const gchar * registry_file, gboolean write_changes, GError ** error)
1510 {
1511   const gchar *plugin_path;
1512   gboolean changed = FALSE;
1513   GList *l;
1514   GstRegistryScanContext context;
1515
1516   GST_INFO ("Validating plugins from registry cache: %s", registry_file);
1517
1518   init_scan_context (&context, default_registry);
1519
1520   /* It sounds tempting to just compare the mtime of directories with the mtime
1521    * of the registry cache, but it does not work. It would not catch updated
1522    * plugins, which might bring more or less features.
1523    */
1524
1525   /* scan paths specified via --gst-plugin-path */
1526   GST_DEBUG ("scanning paths added via --gst-plugin-path");
1527   for (l = _priv_gst_plugin_paths; l != NULL; l = l->next) {
1528     GST_INFO ("Scanning plugin path: \"%s\"", (gchar *) l->data);
1529     changed |= gst_registry_scan_path_internal (&context, (gchar *) l->data);
1530   }
1531   /* keep plugin_paths around in case a re-scan is forced later on */
1532
1533   /* GST_PLUGIN_PATH specifies a list of directories to scan for
1534    * additional plugins.  These take precedence over the system plugins */
1535   plugin_path = g_getenv ("GST_PLUGIN_PATH");
1536   if (plugin_path) {
1537     char **list;
1538     int i;
1539
1540     GST_DEBUG ("GST_PLUGIN_PATH set to %s", plugin_path);
1541     list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1542     for (i = 0; list[i]; i++) {
1543       changed |= gst_registry_scan_path_internal (&context, list[i]);
1544     }
1545     g_strfreev (list);
1546   } else {
1547     GST_DEBUG ("GST_PLUGIN_PATH not set");
1548   }
1549
1550   /* GST_PLUGIN_SYSTEM_PATH specifies a list of plugins that are always
1551    * loaded by default.  If not set, this defaults to the system-installed
1552    * path, and the plugins installed in the user's home directory */
1553   plugin_path = g_getenv ("GST_PLUGIN_SYSTEM_PATH");
1554   if (plugin_path == NULL) {
1555     char *home_plugins;
1556
1557     GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH not set");
1558
1559     /* plugins in the user's home directory take precedence over
1560      * system-installed ones */
1561     home_plugins = g_build_filename (g_get_user_data_dir (),
1562         "gstreamer-" GST_MAJORMINOR, "plugins", NULL);
1563
1564     GST_DEBUG ("scanning home plugins %s", home_plugins);
1565     changed |= gst_registry_scan_path_internal (&context, home_plugins);
1566     g_free (home_plugins);
1567
1568     /* add the main (installed) library path */
1569     GST_DEBUG ("scanning main plugins %s", PLUGINDIR);
1570     changed |= gst_registry_scan_path_internal (&context, PLUGINDIR);
1571
1572 #ifdef G_OS_WIN32
1573     {
1574       char *base_dir;
1575       char *dir;
1576
1577       base_dir =
1578           g_win32_get_package_installation_directory_of_module
1579           (_priv_gst_dll_handle);
1580
1581       dir = g_build_filename (base_dir, "lib", "gstreamer-0.10", NULL);
1582       GST_DEBUG ("scanning DLL dir %s", dir);
1583
1584       changed |= gst_registry_scan_path_internal (&context, dir);
1585
1586       g_free (dir);
1587       g_free (base_dir);
1588     }
1589 #endif
1590   } else {
1591     gchar **list;
1592     gint i;
1593
1594     GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH set to %s", plugin_path);
1595     list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1596     for (i = 0; list[i]; i++) {
1597       changed |= gst_registry_scan_path_internal (&context, list[i]);
1598     }
1599     g_strfreev (list);
1600   }
1601
1602   clear_scan_context (&context);
1603   changed |= context.changed;
1604
1605   /* Remove cached plugins so stale info is cleared. */
1606   changed |= gst_registry_remove_cache_plugins (default_registry);
1607
1608   if (!changed) {
1609     GST_INFO ("Registry cache has not changed");
1610     return REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED;
1611   }
1612
1613   if (!write_changes) {
1614     GST_INFO ("Registry cache changed, but writing is disabled. Not writing.");
1615     return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1616   }
1617
1618   GST_INFO ("Registry cache changed. Writing new registry cache");
1619   if (!priv_gst_registry_binary_write_cache (default_registry, registry_file)) {
1620     g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
1621         _("Error writing registry cache to %s: %s"),
1622         registry_file, g_strerror (errno));
1623     return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1624   }
1625
1626   GST_INFO ("Registry cache written successfully");
1627   return REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED;
1628 }
1629
1630 static gboolean
1631 ensure_current_registry (GError ** error)
1632 {
1633   gchar *registry_file;
1634   GstRegistry *default_registry;
1635   gboolean ret = TRUE;
1636   gboolean do_update = TRUE;
1637   gboolean have_cache = TRUE;
1638
1639   default_registry = gst_registry_get_default ();
1640   registry_file = g_strdup (g_getenv ("GST_REGISTRY"));
1641   if (registry_file == NULL) {
1642     registry_file = g_build_filename (g_get_user_cache_dir (),
1643         "gstreamer-" GST_MAJORMINOR, "registry." HOST_CPU ".bin", NULL);
1644   }
1645
1646   if (!_gst_disable_registry_cache) {
1647     GST_INFO ("reading registry cache: %s", registry_file);
1648     have_cache = priv_gst_registry_binary_read_cache (default_registry,
1649         registry_file);
1650     /* Only ever read the registry cache once, then disable it for
1651      * subsequent updates during the program lifetime */
1652     _gst_disable_registry_cache = TRUE;
1653   }
1654
1655   if (have_cache) {
1656     do_update = !_priv_gst_disable_registry_update;
1657     if (do_update) {
1658       const gchar *update_env;
1659
1660       if ((update_env = g_getenv ("GST_REGISTRY_UPDATE"))) {
1661         /* do update for any value different from "no" */
1662         do_update = (strcmp (update_env, "no") != 0);
1663       }
1664     }
1665   }
1666
1667   if (do_update) {
1668     const gchar *reuse_env;
1669
1670     if ((reuse_env = g_getenv ("GST_REGISTRY_REUSE_PLUGIN_SCANNER"))) {
1671       /* do reuse for any value different from "no" */
1672       __registry_reuse_plugin_scanner = (strcmp (reuse_env, "no") != 0);
1673     }
1674     /* now check registry */
1675     GST_DEBUG ("Updating registry cache");
1676     scan_and_update_registry (default_registry, registry_file, TRUE, error);
1677   } else {
1678     GST_DEBUG ("Not updating registry cache (disabled)");
1679   }
1680
1681   g_free (registry_file);
1682   GST_INFO ("registry reading and updating done, result = %d", ret);
1683
1684   return ret;
1685 }
1686 #endif /* GST_DISABLE_REGISTRY */
1687
1688 /**
1689  * gst_registry_fork_is_enabled:
1690  *
1691  * By default GStreamer will perform scanning and rebuilding of the
1692  * registry file using a helper child process.
1693  *
1694  * Applications might want to disable this behaviour with the
1695  * gst_registry_fork_set_enabled() function, in which case new plugins
1696  * are scanned (and loaded) into the application process.
1697  *
1698  * Returns: %TRUE if GStreamer will use the child helper process when
1699  * rebuilding the registry.
1700  *
1701  * Since: 0.10.10
1702  */
1703 gboolean
1704 gst_registry_fork_is_enabled (void)
1705 {
1706   return _gst_enable_registry_fork;
1707 }
1708
1709 /**
1710  * gst_registry_fork_set_enabled:
1711  * @enabled: whether rebuilding the registry can use a temporary child helper process.
1712  *
1713  * Applications might want to disable/enable spawning of a child helper process
1714  * when rebuilding the registry. See gst_registry_fork_is_enabled() for more
1715  * information.
1716  *
1717  * Since: 0.10.10
1718  */
1719 void
1720 gst_registry_fork_set_enabled (gboolean enabled)
1721 {
1722   _gst_enable_registry_fork = enabled;
1723 }
1724
1725 /**
1726  * gst_update_registry:
1727  *
1728  * Forces GStreamer to re-scan its plugin paths and update the default
1729  * plugin registry.
1730  *
1731  * Applications will almost never need to call this function, it is only
1732  * useful if the application knows new plugins have been installed (or old
1733  * ones removed) since the start of the application (or, to be precise, the
1734  * first call to gst_init()) and the application wants to make use of any
1735  * newly-installed plugins without restarting the application.
1736  *
1737  * Applications should assume that the registry update is neither atomic nor
1738  * thread-safe and should therefore not have any dynamic pipelines running
1739  * (including the playbin and decodebin elements) and should also not create
1740  * any elements or access the GStreamer registry while the update is in
1741  * progress.
1742  *
1743  * Note that this function may block for a significant amount of time.
1744  *
1745  * Returns: %TRUE if the registry has been updated successfully (does not
1746  *          imply that there were changes), otherwise %FALSE.
1747  *
1748  * Since: 0.10.12
1749  */
1750 gboolean
1751 gst_update_registry (void)
1752 {
1753   gboolean res;
1754
1755 #ifndef GST_DISABLE_REGISTRY
1756   GError *err = NULL;
1757
1758   res = ensure_current_registry (&err);
1759   if (err) {
1760     GST_WARNING ("registry update failed: %s", err->message);
1761     g_error_free (err);
1762   } else {
1763     GST_LOG ("registry update succeeded");
1764   }
1765
1766 #else
1767   GST_WARNING ("registry update failed: %s", "registry disabled");
1768   res = TRUE;
1769 #endif /* GST_DISABLE_REGISTRY */
1770
1771   if (_priv_gst_preload_plugins) {
1772     GST_DEBUG ("Preloading indicated plugins...");
1773     g_slist_foreach (_priv_gst_preload_plugins, load_plugin_func, NULL);
1774   }
1775
1776   return res;
1777 }
1778
1779 /**
1780  * gst_registry_get_feature_list_cookie:
1781  * @registry: the registry
1782  *
1783  * Returns the registrys feature list cookie. This changes
1784  * every time a feature is added or removed from the registry.
1785  *
1786  * Returns: the feature list cookie.
1787  *
1788  * Since: 0.10.26
1789  */
1790 guint32
1791 gst_registry_get_feature_list_cookie (GstRegistry * registry)
1792 {
1793   g_return_val_if_fail (GST_IS_REGISTRY (registry), 0);
1794
1795   return registry->priv->cookie;
1796 }