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