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