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