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