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