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