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