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