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