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