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