gstregistry: Don't leak feature name
[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 #endif
179
180 /* Element signals and args */
181 enum
182 {
183   PLUGIN_ADDED,
184   FEATURE_ADDED,
185   LAST_SIGNAL
186 };
187
188 static void gst_registry_finalize (GObject * object);
189
190 static guint gst_registry_signals[LAST_SIGNAL] = { 0 };
191
192 static GstPluginFeature *gst_registry_lookup_feature_locked (GstRegistry *
193     registry, const char *name);
194 static GstPlugin *gst_registry_lookup_bn_locked (GstRegistry * registry,
195     const char *basename);
196
197 #define gst_registry_parent_class parent_class
198 G_DEFINE_TYPE (GstRegistry, gst_registry, GST_TYPE_OBJECT);
199
200 static void
201 gst_registry_class_init (GstRegistryClass * klass)
202 {
203   GObjectClass *gobject_class;
204
205   gobject_class = (GObjectClass *) klass;
206
207   g_type_class_add_private (klass, sizeof (GstRegistryPrivate));
208
209   /**
210    * GstRegistry::plugin-added:
211    * @registry: the registry that emitted the signal
212    * @plugin: the plugin that has been added
213    *
214    * Signals that a plugin has been added to the registry (possibly
215    * replacing a previously-added one by the same name)
216    */
217   gst_registry_signals[PLUGIN_ADDED] =
218       g_signal_new ("plugin-added", G_TYPE_FROM_CLASS (klass),
219       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, plugin_added), NULL,
220       NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
221
222   /**
223    * GstRegistry::feature-added:
224    * @registry: the registry that emitted the signal
225    * @feature: the feature that has been added
226    *
227    * Signals that a feature has been added to the registry (possibly
228    * replacing a previously-added one by the same name)
229    */
230   gst_registry_signals[FEATURE_ADDED] =
231       g_signal_new ("feature-added", G_TYPE_FROM_CLASS (klass),
232       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, feature_added),
233       NULL, NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
234
235   gobject_class->finalize = gst_registry_finalize;
236 }
237
238 static void
239 gst_registry_init (GstRegistry * registry)
240 {
241   registry->feature_hash = g_hash_table_new (g_str_hash, g_str_equal);
242   registry->basename_hash = g_hash_table_new (g_str_hash, g_str_equal);
243   registry->priv =
244       G_TYPE_INSTANCE_GET_PRIVATE (registry, GST_TYPE_REGISTRY,
245       GstRegistryPrivate);
246 }
247
248 static void
249 gst_registry_finalize (GObject * object)
250 {
251   GstRegistry *registry = GST_REGISTRY (object);
252   GList *plugins, *p;
253   GList *features, *f;
254
255   plugins = registry->plugins;
256   registry->plugins = NULL;
257
258   GST_DEBUG_OBJECT (registry, "registry finalize");
259   p = plugins;
260   while (p) {
261     GstPlugin *plugin = p->data;
262
263     if (plugin) {
264       GST_LOG_OBJECT (registry, "removing plugin %s",
265           gst_plugin_get_name (plugin));
266       gst_object_unref (plugin);
267     }
268     p = g_list_next (p);
269   }
270   g_list_free (plugins);
271
272   features = registry->features;
273   registry->features = NULL;
274
275   f = features;
276   while (f) {
277     GstPluginFeature *feature = f->data;
278
279     if (feature) {
280 #ifndef GST_DISABLE_GST_DEBUG
281       gchar *name = gst_plugin_feature_get_name (feature);
282       GST_LOG_OBJECT (registry, "removing feature %p (%s)", feature, name);
283       g_free (name);
284 #endif
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, GST_OBJECT_NAME (feature));
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 (GST_OBJECT_NAME (feature) != 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       GST_OBJECT_NAME (feature));
549   if (G_UNLIKELY (existing_feature)) {
550     GST_DEBUG_OBJECT (registry, "replacing existing feature %p (%s)",
551         existing_feature, GST_OBJECT_NAME (feature));
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,
559       GST_OBJECT_NAME (feature));
560
561   registry->features = g_list_prepend (registry->features, feature);
562   g_hash_table_replace (registry->feature_hash, GST_OBJECT_NAME (feature),
563       feature);
564
565   if (G_UNLIKELY (existing_feature)) {
566     /* We unref now. No need to remove the feature name from the hash table, it
567      * got replaced by the new feature */
568     gst_object_unparent (GST_OBJECT_CAST (existing_feature));
569   }
570
571   gst_object_set_parent (GST_OBJECT_CAST (feature), GST_OBJECT_CAST (registry));
572
573   registry->priv->cookie++;
574   GST_OBJECT_UNLOCK (registry);
575
576   GST_LOG_OBJECT (registry, "emitting feature-added for %s",
577       GST_OBJECT_NAME (feature));
578   g_signal_emit (registry, gst_registry_signals[FEATURE_ADDED], 0, feature);
579
580   return TRUE;
581 }
582
583 /**
584  * gst_registry_remove_feature:
585  * @registry: the registry to remove the feature from
586  * @feature: (transfer none): the feature to remove
587  *
588  * Remove the feature from the registry.
589  *
590  * MT safe.
591  */
592 void
593 gst_registry_remove_feature (GstRegistry * registry, GstPluginFeature * feature)
594 {
595   g_return_if_fail (GST_IS_REGISTRY (registry));
596   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
597
598   GST_DEBUG_OBJECT (registry, "removing feature %p (%s)",
599       feature, gst_plugin_feature_get_name (feature));
600
601   GST_OBJECT_LOCK (registry);
602   registry->features = g_list_remove (registry->features, feature);
603   g_hash_table_remove (registry->feature_hash, GST_OBJECT_NAME (feature));
604   registry->priv->cookie++;
605   GST_OBJECT_UNLOCK (registry);
606
607   gst_object_unparent ((GstObject *) feature);
608 }
609
610 /**
611  * gst_registry_plugin_filter:
612  * @registry: registry to query
613  * @filter: (scope call): the filter to use
614  * @first: only return first match
615  * @user_data: (closure): user data passed to the filter function
616  *
617  * Runs a filter against all plugins in the registry and returns a #GList with
618  * the results. If the first flag is set, only the first match is
619  * returned (as a list with a single object).
620  * Every plugin is reffed; use gst_plugin_list_free() after use, which
621  * will unref again.
622  *
623  * Returns: (transfer full) (element-type Gst.Plugin): a #GList of #GstPlugin.
624  *     Use gst_plugin_list_free() after usage.
625  *
626  * MT safe.
627  */
628 GList *
629 gst_registry_plugin_filter (GstRegistry * registry,
630     GstPluginFilter filter, gboolean first, gpointer user_data)
631 {
632   GList *list;
633   GList *g;
634
635   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
636
637   GST_OBJECT_LOCK (registry);
638   list = gst_filter_run (registry->plugins, (GstFilterFunc) filter, first,
639       user_data);
640   for (g = list; g; g = g->next) {
641     gst_object_ref (GST_PLUGIN_CAST (g->data));
642   }
643   GST_OBJECT_UNLOCK (registry);
644
645   return list;
646 }
647
648 /* returns TRUE if the list was changed
649  *
650  * Must be called with the object lock taken */
651 static gboolean
652 gst_registry_get_feature_list_or_create (GstRegistry * registry,
653     GList ** previous, guint32 * cookie, GType type)
654 {
655   gboolean res = FALSE;
656   GstRegistryPrivate *priv = registry->priv;
657
658   if (G_UNLIKELY (!*previous || priv->cookie != *cookie)) {
659     GstTypeNameData data;
660
661     if (*previous)
662       gst_plugin_feature_list_free (*previous);
663
664     data.type = type;
665     data.name = NULL;
666     *previous =
667         gst_filter_run (registry->features,
668         (GstFilterFunc) gst_plugin_feature_type_name_filter, FALSE, &data);
669     g_list_foreach (*previous, (GFunc) gst_object_ref, NULL);
670     *cookie = priv->cookie;
671     res = TRUE;
672   }
673
674   return res;
675 }
676
677 static gint
678 type_find_factory_rank_cmp (const GstPluginFeature * fac1,
679     const GstPluginFeature * fac2)
680 {
681   if (G_LIKELY (fac1->rank != fac2->rank))
682     return fac2->rank - fac1->rank;
683
684   /* to make the order in which things happen more deterministic,
685    * sort by name when the ranks are the same. */
686   return strcmp (GST_OBJECT_NAME (fac1), GST_OBJECT_NAME (fac2));
687 }
688
689 static GList *
690 gst_registry_get_element_factory_list (GstRegistry * registry)
691 {
692   GList *list;
693
694   GST_OBJECT_LOCK (registry);
695
696   gst_registry_get_feature_list_or_create (registry,
697       &registry->priv->element_factory_list, &registry->priv->efl_cookie,
698       GST_TYPE_ELEMENT_FACTORY);
699
700   /* Return reffed copy */
701   list = gst_plugin_feature_list_copy (registry->priv->element_factory_list);
702
703   GST_OBJECT_UNLOCK (registry);
704
705   return list;
706 }
707
708 static GList *
709 gst_registry_get_typefind_factory_list (GstRegistry * registry)
710 {
711   GList *list;
712
713   GST_OBJECT_LOCK (registry);
714
715   if (G_UNLIKELY (gst_registry_get_feature_list_or_create (registry,
716               &registry->priv->typefind_factory_list,
717               &registry->priv->tfl_cookie, GST_TYPE_TYPE_FIND_FACTORY)))
718     registry->priv->typefind_factory_list =
719         g_list_sort (registry->priv->typefind_factory_list,
720         (GCompareFunc) type_find_factory_rank_cmp);
721
722   /* Return reffed copy */
723   list = gst_plugin_feature_list_copy (registry->priv->typefind_factory_list);
724
725   GST_OBJECT_UNLOCK (registry);
726
727   return list;
728 }
729
730 /**
731  * gst_registry_feature_filter:
732  * @registry: registry to query
733  * @filter: (scope call): the filter to use
734  * @first: only return first match
735  * @user_data: (closure): user data passed to the filter function
736  *
737  * Runs a filter against all features of the plugins in the registry
738  * and returns a GList with the results.
739  * If the first flag is set, only the first match is
740  * returned (as a list with a single object).
741  *
742  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
743  *     #GstPluginFeature. Use gst_plugin_feature_list_free() after usage.
744  *
745  * MT safe.
746  */
747 GList *
748 gst_registry_feature_filter (GstRegistry * registry,
749     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
750 {
751   GList *list;
752   GList *g;
753
754   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
755
756   GST_OBJECT_LOCK (registry);
757   list = gst_filter_run (registry->features, (GstFilterFunc) filter, first,
758       user_data);
759   for (g = list; g; g = g->next) {
760     gst_object_ref (GST_PLUGIN_FEATURE_CAST (g->data));
761   }
762   GST_OBJECT_UNLOCK (registry);
763
764   return list;
765 }
766
767 /**
768  * gst_registry_find_plugin:
769  * @registry: the registry to search
770  * @name: the plugin name to find
771  *
772  * Find the plugin with the given name in the registry.
773  * The plugin will be reffed; caller is responsible for unreffing.
774  *
775  * Returns: (transfer full): the plugin with the given name or NULL if the
776  *     plugin was not found. gst_object_unref() after usage.
777  *
778  * MT safe.
779  */
780 GstPlugin *
781 gst_registry_find_plugin (GstRegistry * registry, const gchar * name)
782 {
783   GList *walk;
784   GstPlugin *result = NULL;
785
786   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
787   g_return_val_if_fail (name != NULL, NULL);
788
789   walk = gst_registry_plugin_filter (registry,
790       (GstPluginFilter) gst_plugin_name_filter, TRUE, (gpointer) name);
791   if (walk) {
792     result = GST_PLUGIN_CAST (walk->data);
793
794     gst_object_ref (result);
795     gst_plugin_list_free (walk);
796   }
797
798   return result;
799 }
800
801 /**
802  * gst_registry_find_feature:
803  * @registry: the registry to search
804  * @name: the pluginfeature name to find
805  * @type: the pluginfeature type to find
806  *
807  * Find the pluginfeature with the given name and type in the registry.
808  *
809  * Returns: (transfer full): the pluginfeature with the given name and type
810  *     or NULL if the plugin was not found. gst_object_unref() after usage.
811  *
812  * MT safe.
813  */
814 GstPluginFeature *
815 gst_registry_find_feature (GstRegistry * registry, const gchar * name,
816     GType type)
817 {
818   GstPluginFeature *feature = NULL;
819
820   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
821   g_return_val_if_fail (name != NULL, NULL);
822   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
823
824   feature = gst_registry_lookup_feature (registry, name);
825   if (feature && !g_type_is_a (G_TYPE_FROM_INSTANCE (feature), type)) {
826     gst_object_unref (feature);
827     feature = NULL;
828   }
829
830   return feature;
831 }
832
833 /**
834  * gst_registry_get_feature_list:
835  * @registry: a #GstRegistry
836  * @type: a #GType.
837  *
838  * Retrieves a #GList of #GstPluginFeature of @type.
839  *
840  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
841  *     #GstPluginFeature of @type. Use gst_plugin_feature_list_free() after use
842  *
843  * MT safe.
844  */
845 GList *
846 gst_registry_get_feature_list (GstRegistry * registry, GType type)
847 {
848   GstTypeNameData data;
849
850   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
851   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
852
853   /* Speed up */
854   if (type == GST_TYPE_ELEMENT_FACTORY)
855     return gst_registry_get_element_factory_list (registry);
856   else if (type == GST_TYPE_TYPE_FIND_FACTORY)
857     return gst_registry_get_typefind_factory_list (registry);
858
859   data.type = type;
860   data.name = NULL;
861
862   return gst_registry_feature_filter (registry,
863       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
864       FALSE, &data);
865 }
866
867 /**
868  * gst_registry_get_plugin_list:
869  * @registry: the registry to search
870  *
871  * Get a copy of all plugins registered in the given registry. The refcount
872  * of each element in the list in incremented.
873  *
874  * Returns: (transfer full) (element-type Gst.Plugin): a #GList of #GstPlugin.
875  *     Use gst_plugin_list_free() after usage.
876  *
877  * MT safe.
878  */
879 GList *
880 gst_registry_get_plugin_list (GstRegistry * registry)
881 {
882   GList *list;
883   GList *g;
884
885   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
886
887   GST_OBJECT_LOCK (registry);
888   list = g_list_copy (registry->plugins);
889   for (g = list; g; g = g->next) {
890     gst_object_ref (GST_PLUGIN_CAST (g->data));
891   }
892   GST_OBJECT_UNLOCK (registry);
893
894   return list;
895 }
896
897 static GstPluginFeature *
898 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
899 {
900   return g_hash_table_lookup (registry->feature_hash, name);
901 }
902
903 /**
904  * gst_registry_lookup_feature:
905  * @registry: a #GstRegistry
906  * @name: a #GstPluginFeature name
907  *
908  * Find a #GstPluginFeature with @name in @registry.
909  *
910  * Returns: (transfer full): a #GstPluginFeature with its refcount incremented,
911  *     use gst_object_unref() after usage.
912  *
913  * MT safe.
914  */
915 GstPluginFeature *
916 gst_registry_lookup_feature (GstRegistry * registry, const char *name)
917 {
918   GstPluginFeature *feature;
919
920   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
921   g_return_val_if_fail (name != NULL, NULL);
922
923   GST_OBJECT_LOCK (registry);
924   feature = gst_registry_lookup_feature_locked (registry, name);
925   if (feature)
926     gst_object_ref (feature);
927   GST_OBJECT_UNLOCK (registry);
928
929   return feature;
930 }
931
932 static GstPlugin *
933 gst_registry_lookup_bn_locked (GstRegistry * registry, const char *basename)
934 {
935   return g_hash_table_lookup (registry->basename_hash, basename);
936 }
937
938 static GstPlugin *
939 gst_registry_lookup_bn (GstRegistry * registry, const char *basename)
940 {
941   GstPlugin *plugin;
942
943   GST_OBJECT_LOCK (registry);
944   plugin = gst_registry_lookup_bn_locked (registry, basename);
945   if (plugin)
946     gst_object_ref (plugin);
947   GST_OBJECT_UNLOCK (registry);
948
949   return plugin;
950 }
951
952 /**
953  * gst_registry_lookup:
954  * @registry: the registry to look up in
955  * @filename: the name of the file to look up
956  *
957  * Look up a plugin in the given registry with the given filename.
958  * If found, plugin is reffed.
959  *
960  * Returns: (transfer full): the #GstPlugin if found, or NULL if not.
961  *     gst_object_unref() after usage.
962  */
963 GstPlugin *
964 gst_registry_lookup (GstRegistry * registry, const char *filename)
965 {
966   GstPlugin *plugin;
967   gchar *basename;
968
969   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
970   g_return_val_if_fail (filename != NULL, NULL);
971
972   basename = g_path_get_basename (filename);
973   if (G_UNLIKELY (basename == NULL))
974     return NULL;
975
976   plugin = gst_registry_lookup_bn (registry, basename);
977
978   g_free (basename);
979
980   return plugin;
981 }
982
983 typedef enum
984 {
985   REGISTRY_SCAN_HELPER_NOT_STARTED = 0,
986   REGISTRY_SCAN_HELPER_DISABLED,
987   REGISTRY_SCAN_HELPER_RUNNING
988 } GstRegistryScanHelperState;
989
990 typedef struct
991 {
992   GstRegistry *registry;
993   GstRegistryScanHelperState helper_state;
994   GstPluginLoader *helper;
995   gboolean changed;
996 } GstRegistryScanContext;
997
998 static void
999 init_scan_context (GstRegistryScanContext * context, GstRegistry * registry)
1000 {
1001   gboolean do_fork;
1002
1003   context->registry = registry;
1004
1005   /* see if forking is enabled and set up the scan helper state accordingly */
1006   do_fork = _gst_enable_registry_fork;
1007   if (do_fork) {
1008     const gchar *fork_env;
1009
1010     /* forking enabled, see if it is disabled with an env var */
1011     if ((fork_env = g_getenv ("GST_REGISTRY_FORK"))) {
1012       /* fork enabled for any value different from "no" */
1013       do_fork = strcmp (fork_env, "no") != 0;
1014     }
1015   }
1016
1017   if (do_fork)
1018     context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
1019   else
1020     context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1021
1022   context->helper = NULL;
1023   context->changed = FALSE;
1024 }
1025
1026 static void
1027 clear_scan_context (GstRegistryScanContext * context)
1028 {
1029   if (context->helper) {
1030     context->changed |= _priv_gst_plugin_loader_funcs.destroy (context->helper);
1031     context->helper = NULL;
1032   }
1033 }
1034
1035 static gboolean
1036 gst_registry_scan_plugin_file (GstRegistryScanContext * context,
1037     const gchar * filename, off_t file_size, time_t file_mtime)
1038 {
1039   gboolean changed = FALSE;
1040   GstPlugin *newplugin = NULL;
1041
1042 #ifdef G_OS_WIN32
1043   /* Disable external plugin loader on Windows until it is ported properly. */
1044   context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1045 #endif
1046
1047
1048   /* Have a plugin to load - see if the scan-helper needs starting */
1049   if (context->helper_state == REGISTRY_SCAN_HELPER_NOT_STARTED) {
1050     GST_DEBUG ("Starting plugin scanner for file %s", filename);
1051     context->helper = _priv_gst_plugin_loader_funcs.create (context->registry);
1052     if (context->helper != NULL)
1053       context->helper_state = REGISTRY_SCAN_HELPER_RUNNING;
1054     else {
1055       GST_WARNING ("Failed starting plugin scanner. Scanning in-process");
1056       context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1057     }
1058   }
1059
1060   if (context->helper_state == REGISTRY_SCAN_HELPER_RUNNING) {
1061     GST_DEBUG ("Using scan-helper to load plugin %s", filename);
1062     if (!_priv_gst_plugin_loader_funcs.load (context->helper,
1063             filename, file_size, file_mtime)) {
1064       g_warning ("External plugin loader failed. This most likely means that "
1065           "the plugin loader helper binary was not found or could not be run. "
1066           "%s", (g_getenv ("GST_PLUGIN_PATH") != NULL) ?
1067           "If you are running an uninstalled GStreamer setup, you might need "
1068           "to update your gst-uninstalled script so that the "
1069           "GST_PLUGIN_SCANNER environment variable gets set." : "");
1070       context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1071     }
1072   }
1073
1074   /* Check if the helper is disabled (or just got disabled above) */
1075   if (context->helper_state == REGISTRY_SCAN_HELPER_DISABLED) {
1076     /* Load plugin the old fashioned way... */
1077
1078     /* We don't use a GError here because a failure to load some shared
1079      * objects as plugins is normal (particularly in the uninstalled case)
1080      */
1081     newplugin = gst_plugin_load_file (filename, NULL);
1082   }
1083
1084   if (newplugin) {
1085     GST_DEBUG_OBJECT (context->registry, "marking new plugin %p as registered",
1086         newplugin);
1087     newplugin->registered = TRUE;
1088     gst_object_unref (newplugin);
1089     changed = TRUE;
1090   }
1091
1092   return changed;
1093 }
1094
1095 static gboolean
1096 is_blacklisted_hidden_directory (const gchar * dirent)
1097 {
1098   if (G_LIKELY (dirent[0] != '.'))
1099     return FALSE;
1100
1101   /* skip the .debug directory, these contain elf files that are not
1102    * useful or worse, can crash dlopen () */
1103   if (strcmp (dirent, ".debug") == 0)
1104     return TRUE;
1105
1106   /* can also skip .git and .deps dirs, those won't contain useful files.
1107    * This speeds up scanning a bit in uninstalled setups. */
1108   if (strcmp (dirent, ".git") == 0 || strcmp (dirent, ".deps") == 0)
1109     return TRUE;
1110
1111   return FALSE;
1112 }
1113
1114 static gboolean
1115 gst_registry_scan_path_level (GstRegistryScanContext * context,
1116     const gchar * path, int level)
1117 {
1118   GDir *dir;
1119   const gchar *dirent;
1120   gchar *filename;
1121   GstPlugin *plugin;
1122   gboolean changed = FALSE;
1123
1124   dir = g_dir_open (path, 0, NULL);
1125   if (!dir)
1126     return FALSE;
1127
1128   while ((dirent = g_dir_read_name (dir))) {
1129     GStatBuf file_status;
1130
1131     filename = g_build_filename (path, dirent, NULL);
1132     if (g_stat (filename, &file_status) < 0) {
1133       /* Plugin will be removed from cache after the scan completes if it
1134        * is still marked 'cached' */
1135       g_free (filename);
1136       continue;
1137     }
1138
1139     if (file_status.st_mode & S_IFDIR) {
1140       if (G_UNLIKELY (is_blacklisted_hidden_directory (dirent))) {
1141         GST_TRACE_OBJECT (context->registry, "ignoring %s directory", dirent);
1142         g_free (filename);
1143         continue;
1144       }
1145       /* FIXME 0.11: Don't recurse into directories, this behaviour
1146        * is inconsistent with other PATH environment variables
1147        */
1148       if (level > 0) {
1149         GST_LOG_OBJECT (context->registry, "recursing into directory %s",
1150             filename);
1151         changed |= gst_registry_scan_path_level (context, filename, level - 1);
1152       } else {
1153         GST_LOG_OBJECT (context->registry, "not recursing into directory %s, "
1154             "recursion level too deep", filename);
1155       }
1156       g_free (filename);
1157       continue;
1158     }
1159     if (!(file_status.st_mode & S_IFREG)) {
1160       GST_TRACE_OBJECT (context->registry, "%s is not a regular file, ignoring",
1161           filename);
1162       g_free (filename);
1163       continue;
1164     }
1165     if (!g_str_has_suffix (dirent, G_MODULE_SUFFIX)
1166 #ifdef GST_EXTRA_MODULE_SUFFIX
1167         && !g_str_has_suffix (dirent, GST_EXTRA_MODULE_SUFFIX)
1168 #endif
1169         ) {
1170       GST_TRACE_OBJECT (context->registry,
1171           "extension is not recognized as module file, ignoring file %s",
1172           filename);
1173       g_free (filename);
1174       continue;
1175     }
1176
1177     GST_LOG_OBJECT (context->registry, "file %s looks like a possible module",
1178         filename);
1179
1180     /* try to avoid unnecessary plugin-move pain */
1181     if (g_str_has_prefix (dirent, "libgstvalve") ||
1182         g_str_has_prefix (dirent, "libgstselector")) {
1183       GST_WARNING_OBJECT (context->registry, "ignoring old plugin %s which "
1184           "has been merged into the corelements plugin", filename);
1185       /* Plugin will be removed from cache after the scan completes if it
1186        * is still marked 'cached' */
1187       g_free (filename);
1188       continue;
1189     }
1190
1191     /* plug-ins are considered unique by basename; if the given name
1192      * was already seen by the registry, we ignore it */
1193     plugin = gst_registry_lookup_bn (context->registry, dirent);
1194     if (plugin) {
1195       gboolean env_vars_changed, deps_changed = FALSE;
1196
1197       if (plugin->registered) {
1198         GST_DEBUG_OBJECT (context->registry,
1199             "plugin already registered from path \"%s\"",
1200             GST_STR_NULL (plugin->filename));
1201         g_free (filename);
1202         gst_object_unref (plugin);
1203         continue;
1204       }
1205
1206       env_vars_changed = _priv_plugin_deps_env_vars_changed (plugin);
1207
1208       /* If a file with a certain basename is seen on a different path,
1209        * update the plugin to ensure the registry cache will reflect up
1210        * to date information */
1211
1212       if (plugin->file_mtime == file_status.st_mtime &&
1213           plugin->file_size == file_status.st_size && !env_vars_changed &&
1214           !(deps_changed = _priv_plugin_deps_files_changed (plugin)) &&
1215           !strcmp (plugin->filename, filename)) {
1216         GST_LOG_OBJECT (context->registry, "file %s cached", filename);
1217         plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
1218         GST_LOG_OBJECT (context->registry,
1219             "marking plugin %p as registered as %s", plugin, filename);
1220         plugin->registered = TRUE;
1221       } else {
1222         GST_INFO_OBJECT (context->registry, "cached info for %s is stale",
1223             filename);
1224         GST_DEBUG_OBJECT (context->registry, "mtime %" G_GINT64_FORMAT " != %"
1225             G_GINT64_FORMAT " or size %" G_GINT64_FORMAT " != %"
1226             G_GINT64_FORMAT " or external dependency env_vars changed: %d or"
1227             " external dependencies changed: %d or old path %s != new path %s",
1228             (gint64) plugin->file_mtime, (gint64) file_status.st_mtime,
1229             (gint64) plugin->file_size, (gint64) file_status.st_size,
1230             env_vars_changed, deps_changed, plugin->filename, filename);
1231         gst_registry_remove_plugin (context->registry, plugin);
1232         changed |= gst_registry_scan_plugin_file (context, filename,
1233             file_status.st_size, file_status.st_mtime);
1234       }
1235       gst_object_unref (plugin);
1236
1237     } else {
1238       GST_DEBUG_OBJECT (context->registry, "file %s not yet in registry",
1239           filename);
1240       changed |= gst_registry_scan_plugin_file (context, filename,
1241           file_status.st_size, file_status.st_mtime);
1242     }
1243
1244     g_free (filename);
1245   }
1246
1247   g_dir_close (dir);
1248
1249   return changed;
1250 }
1251
1252 static gboolean
1253 gst_registry_scan_path_internal (GstRegistryScanContext * context,
1254     const gchar * path)
1255 {
1256   gboolean changed;
1257
1258   GST_DEBUG_OBJECT (context->registry, "scanning path %s", path);
1259   changed = gst_registry_scan_path_level (context, path, 10);
1260
1261   GST_DEBUG_OBJECT (context->registry, "registry changed in path %s: %d", path,
1262       changed);
1263   return changed;
1264 }
1265
1266 /**
1267  * gst_registry_scan_path:
1268  * @registry: the registry to add found plugins to
1269  * @path: the path to scan
1270  *
1271  * Scan the given path for plugins to add to the registry. The syntax of the
1272  * path is specific to the registry.
1273  *
1274  * Returns: %TRUE if registry changed
1275  */
1276 gboolean
1277 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
1278 {
1279   GstRegistryScanContext context;
1280   gboolean result;
1281
1282   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1283   g_return_val_if_fail (path != NULL, FALSE);
1284
1285   init_scan_context (&context, registry);
1286
1287   result = gst_registry_scan_path_internal (&context, path);
1288
1289   clear_scan_context (&context);
1290   result |= context.changed;
1291
1292   return result;
1293 }
1294
1295 static gboolean
1296 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
1297     gpointer user_data)
1298 {
1299   return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
1300 }
1301
1302 /**
1303  * gst_registry_get_feature_list_by_plugin:
1304  * @registry: a #GstRegistry.
1305  * @name: a plugin name.
1306  *
1307  * Retrieves a #GList of features of the plugin with name @name.
1308  *
1309  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
1310  *     #GstPluginFeature. Use gst_plugin_feature_list_free() after usage.
1311  */
1312 GList *
1313 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
1314     const gchar * name)
1315 {
1316   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1317   g_return_val_if_fail (name != NULL, NULL);
1318
1319   return gst_registry_feature_filter (registry,
1320       _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1321 }
1322
1323 /* Unref and delete the default registry */
1324 void
1325 _priv_gst_registry_cleanup (void)
1326 {
1327   GstRegistry *registry;
1328
1329   g_static_mutex_lock (&_gst_registry_mutex);
1330   if ((registry = _gst_registry_default) != NULL) {
1331     _gst_registry_default = NULL;
1332   }
1333   g_static_mutex_unlock (&_gst_registry_mutex);
1334
1335   /* unref outside of the lock because we can. */
1336   if (registry)
1337     gst_object_unref (registry);
1338 }
1339
1340 /**
1341  * gst_default_registry_check_feature_version:
1342  * @feature_name: the name of the feature (e.g. "oggdemux")
1343  * @min_major: the minimum major version number
1344  * @min_minor: the minimum minor version number
1345  * @min_micro: the minimum micro version number
1346  *
1347  * Checks whether a plugin feature by the given name exists in the
1348  * default registry and whether its version is at least the
1349  * version required.
1350  *
1351  * Returns: #TRUE if the feature could be found and the version is
1352  * the same as the required version or newer, and #FALSE otherwise.
1353  */
1354 gboolean
1355 gst_default_registry_check_feature_version (const gchar * feature_name,
1356     guint min_major, guint min_minor, guint min_micro)
1357 {
1358   GstPluginFeature *feature;
1359   GstRegistry *registry;
1360   gboolean ret = FALSE;
1361
1362   g_return_val_if_fail (feature_name != NULL, FALSE);
1363
1364   GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1365
1366   registry = gst_registry_get_default ();
1367   feature = gst_registry_lookup_feature (registry, feature_name);
1368   if (feature) {
1369     ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1370         min_micro);
1371     gst_object_unref (feature);
1372   } else {
1373     GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1374   }
1375
1376   return ret;
1377 }
1378
1379 static void
1380 load_plugin_func (gpointer data, gpointer user_data)
1381 {
1382   GstPlugin *plugin;
1383   const gchar *filename;
1384   GError *err = NULL;
1385
1386   filename = (const gchar *) data;
1387   GST_DEBUG ("Pre-loading plugin %s", filename);
1388
1389   plugin = gst_plugin_load_file (filename, &err);
1390
1391   if (plugin) {
1392     GST_INFO ("Loaded plugin: \"%s\"", filename);
1393
1394     gst_default_registry_add_plugin (plugin);
1395   } else {
1396     if (err) {
1397       /* Report error to user, and free error */
1398       GST_ERROR ("Failed to load plugin: %s", err->message);
1399       g_error_free (err);
1400     } else {
1401       GST_WARNING ("Failed to load plugin: \"%s\"", filename);
1402     }
1403   }
1404 }
1405
1406 #ifndef GST_DISABLE_REGISTRY
1407 /* Unref all plugins marked 'cached', to clear old plugins that no
1408  * longer exist. Returns TRUE if any plugins were removed */
1409 static gboolean
1410 gst_registry_remove_cache_plugins (GstRegistry * registry)
1411 {
1412   GList *g;
1413   GList *g_next;
1414   GstPlugin *plugin;
1415   gboolean changed = FALSE;
1416
1417   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1418
1419   GST_OBJECT_LOCK (registry);
1420
1421   GST_DEBUG_OBJECT (registry, "removing cached plugins");
1422   g = registry->plugins;
1423   while (g) {
1424     g_next = g->next;
1425     plugin = g->data;
1426     if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
1427       GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
1428           GST_STR_NULL (plugin->filename));
1429       registry->plugins = g_list_delete_link (registry->plugins, g);
1430       if (G_LIKELY (plugin->basename))
1431         g_hash_table_remove (registry->basename_hash, plugin->basename);
1432       gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
1433       gst_object_unref (plugin);
1434       changed = TRUE;
1435     }
1436     g = g_next;
1437   }
1438
1439   GST_OBJECT_UNLOCK (registry);
1440
1441   return changed;
1442 }
1443
1444 typedef enum
1445 {
1446   REGISTRY_SCAN_AND_UPDATE_FAILURE = 0,
1447   REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED,
1448   REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED
1449 } GstRegistryScanAndUpdateResult;
1450
1451 /*
1452  * scan_and_update_registry:
1453  * @default_registry: the #GstRegistry
1454  * @registry_file: registry filename
1455  * @write_changes: write registry if it has changed?
1456  *
1457  * Scans for registry changes and eventually updates the registry cache.
1458  *
1459  * Return: %REGISTRY_SCAN_AND_UPDATE_FAILURE if the registry could not scanned
1460  *         or updated, %REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED if the
1461  *         registry is clean and %REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED if
1462  *         it has been updated and the cache needs to be re-read.
1463  */
1464 static GstRegistryScanAndUpdateResult
1465 scan_and_update_registry (GstRegistry * default_registry,
1466     const gchar * registry_file, gboolean write_changes, GError ** error)
1467 {
1468   const gchar *plugin_path;
1469   gboolean changed = FALSE;
1470   GList *l;
1471   GstRegistryScanContext context;
1472
1473   GST_INFO ("Validating plugins from registry cache: %s", registry_file);
1474
1475   init_scan_context (&context, default_registry);
1476
1477   /* It sounds tempting to just compare the mtime of directories with the mtime
1478    * of the registry cache, but it does not work. It would not catch updated
1479    * plugins, which might bring more or less features.
1480    */
1481
1482   /* scan paths specified via --gst-plugin-path */
1483   GST_DEBUG ("scanning paths added via --gst-plugin-path");
1484   for (l = _priv_gst_plugin_paths; l != NULL; l = l->next) {
1485     GST_INFO ("Scanning plugin path: \"%s\"", (gchar *) l->data);
1486     changed |= gst_registry_scan_path_internal (&context, (gchar *) l->data);
1487   }
1488   /* keep plugin_paths around in case a re-scan is forced later on */
1489
1490   /* GST_PLUGIN_PATH specifies a list of directories to scan for
1491    * additional plugins.  These take precedence over the system plugins */
1492   plugin_path = g_getenv ("GST_PLUGIN_PATH");
1493   if (plugin_path) {
1494     char **list;
1495     int i;
1496
1497     GST_DEBUG ("GST_PLUGIN_PATH set to %s", plugin_path);
1498     list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1499     for (i = 0; list[i]; i++) {
1500       changed |= gst_registry_scan_path_internal (&context, list[i]);
1501     }
1502     g_strfreev (list);
1503   } else {
1504     GST_DEBUG ("GST_PLUGIN_PATH not set");
1505   }
1506
1507   /* GST_PLUGIN_SYSTEM_PATH specifies a list of plugins that are always
1508    * loaded by default.  If not set, this defaults to the system-installed
1509    * path, and the plugins installed in the user's home directory */
1510   plugin_path = g_getenv ("GST_PLUGIN_SYSTEM_PATH");
1511   if (plugin_path == NULL) {
1512     char *home_plugins;
1513
1514     GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH not set");
1515
1516     /* plugins in the user's home directory take precedence over
1517      * system-installed ones */
1518     home_plugins = g_build_filename (g_get_user_data_dir (),
1519         "gstreamer-" GST_MAJORMINOR, "plugins", NULL);
1520
1521     GST_DEBUG ("scanning home plugins %s", home_plugins);
1522     changed |= gst_registry_scan_path_internal (&context, home_plugins);
1523     g_free (home_plugins);
1524
1525     /* add the main (installed) library path */
1526     GST_DEBUG ("scanning main plugins %s", PLUGINDIR);
1527     changed |= gst_registry_scan_path_internal (&context, PLUGINDIR);
1528
1529 #ifdef G_OS_WIN32
1530     {
1531       char *base_dir;
1532       char *dir;
1533
1534       base_dir =
1535           g_win32_get_package_installation_directory_of_module
1536           (_priv_gst_dll_handle);
1537
1538       dir = g_build_filename (base_dir, "lib", "gstreamer-0.10", NULL);
1539       GST_DEBUG ("scanning DLL dir %s", dir);
1540
1541       changed |= gst_registry_scan_path_internal (&context, dir);
1542
1543       g_free (dir);
1544       g_free (base_dir);
1545     }
1546 #endif
1547   } else {
1548     gchar **list;
1549     gint i;
1550
1551     GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH set to %s", plugin_path);
1552     list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1553     for (i = 0; list[i]; i++) {
1554       changed |= gst_registry_scan_path_internal (&context, list[i]);
1555     }
1556     g_strfreev (list);
1557   }
1558
1559   clear_scan_context (&context);
1560   changed |= context.changed;
1561
1562   /* Remove cached plugins so stale info is cleared. */
1563   changed |= gst_registry_remove_cache_plugins (default_registry);
1564
1565   if (!changed) {
1566     GST_INFO ("Registry cache has not changed");
1567     return REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED;
1568   }
1569
1570   if (!write_changes) {
1571     GST_INFO ("Registry cache changed, but writing is disabled. Not writing.");
1572     return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1573   }
1574
1575   GST_INFO ("Registry cache changed. Writing new registry cache");
1576   if (!priv_gst_registry_binary_write_cache (default_registry, registry_file)) {
1577     g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
1578         _("Error writing registry cache to %s: %s"),
1579         registry_file, g_strerror (errno));
1580     return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1581   }
1582
1583   GST_INFO ("Registry cache written successfully");
1584   return REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED;
1585 }
1586
1587 static gboolean
1588 ensure_current_registry (GError ** error)
1589 {
1590   gchar *registry_file;
1591   GstRegistry *default_registry;
1592   gboolean ret = TRUE;
1593   gboolean do_update = TRUE;
1594   gboolean have_cache = TRUE;
1595
1596   default_registry = gst_registry_get_default ();
1597   registry_file = g_strdup (g_getenv ("GST_REGISTRY"));
1598   if (registry_file == NULL) {
1599     registry_file = g_build_filename (g_get_user_cache_dir (),
1600         "gstreamer-" GST_MAJORMINOR, "registry." HOST_CPU ".bin", NULL);
1601   }
1602
1603   if (!_gst_disable_registry_cache) {
1604     GST_INFO ("reading registry cache: %s", registry_file);
1605     have_cache = priv_gst_registry_binary_read_cache (default_registry,
1606         registry_file);
1607     /* Only ever read the registry cache once, then disable it for
1608      * subsequent updates during the program lifetime */
1609     _gst_disable_registry_cache = TRUE;
1610   }
1611
1612   if (have_cache) {
1613     do_update = !_priv_gst_disable_registry_update;
1614     if (do_update) {
1615       const gchar *update_env;
1616
1617       if ((update_env = g_getenv ("GST_REGISTRY_UPDATE"))) {
1618         /* do update for any value different from "no" */
1619         do_update = (strcmp (update_env, "no") != 0);
1620       }
1621     }
1622   }
1623
1624   if (do_update) {
1625     /* now check registry */
1626     GST_DEBUG ("Updating registry cache");
1627     scan_and_update_registry (default_registry, registry_file, TRUE, error);
1628   } else {
1629     GST_DEBUG ("Not updating registry cache (disabled)");
1630   }
1631
1632   g_free (registry_file);
1633   GST_INFO ("registry reading and updating done, result = %d", ret);
1634
1635   return ret;
1636 }
1637 #endif /* GST_DISABLE_REGISTRY */
1638
1639 /**
1640  * gst_registry_fork_is_enabled:
1641  *
1642  * By default GStreamer will perform scanning and rebuilding of the
1643  * registry file using a helper child process.
1644  *
1645  * Applications might want to disable this behaviour with the
1646  * gst_registry_fork_set_enabled() function, in which case new plugins
1647  * are scanned (and loaded) into the application process.
1648  *
1649  * Returns: %TRUE if GStreamer will use the child helper process when
1650  * rebuilding the registry.
1651  *
1652  * Since: 0.10.10
1653  */
1654 gboolean
1655 gst_registry_fork_is_enabled (void)
1656 {
1657   return _gst_enable_registry_fork;
1658 }
1659
1660 /**
1661  * gst_registry_fork_set_enabled:
1662  * @enabled: whether rebuilding the registry can use a temporary child helper process.
1663  *
1664  * Applications might want to disable/enable spawning of a child helper process
1665  * when rebuilding the registry. See gst_registry_fork_is_enabled() for more
1666  * information.
1667  *
1668  * Since: 0.10.10
1669  */
1670 void
1671 gst_registry_fork_set_enabled (gboolean enabled)
1672 {
1673   _gst_enable_registry_fork = enabled;
1674 }
1675
1676 /**
1677  * gst_update_registry:
1678  *
1679  * Forces GStreamer to re-scan its plugin paths and update the default
1680  * plugin registry.
1681  *
1682  * Applications will almost never need to call this function, it is only
1683  * useful if the application knows new plugins have been installed (or old
1684  * ones removed) since the start of the application (or, to be precise, the
1685  * first call to gst_init()) and the application wants to make use of any
1686  * newly-installed plugins without restarting the application.
1687  *
1688  * Applications should assume that the registry update is neither atomic nor
1689  * thread-safe and should therefore not have any dynamic pipelines running
1690  * (including the playbin and decodebin elements) and should also not create
1691  * any elements or access the GStreamer registry while the update is in
1692  * progress.
1693  *
1694  * Note that this function may block for a significant amount of time.
1695  *
1696  * Returns: %TRUE if the registry has been updated successfully (does not
1697  *          imply that there were changes), otherwise %FALSE.
1698  *
1699  * Since: 0.10.12
1700  */
1701 gboolean
1702 gst_update_registry (void)
1703 {
1704   gboolean res;
1705
1706 #ifndef GST_DISABLE_REGISTRY
1707   GError *err = NULL;
1708
1709   res = ensure_current_registry (&err);
1710   if (err) {
1711     GST_WARNING ("registry update failed: %s", err->message);
1712     g_error_free (err);
1713   } else {
1714     GST_LOG ("registry update succeeded");
1715   }
1716
1717 #else
1718   GST_WARNING ("registry update failed: %s", "registry disabled");
1719   res = TRUE;
1720 #endif /* GST_DISABLE_REGISTRY */
1721
1722   if (_priv_gst_preload_plugins) {
1723     GST_DEBUG ("Preloading indicated plugins...");
1724     g_slist_foreach (_priv_gst_preload_plugins, load_plugin_func, NULL);
1725   }
1726
1727   return res;
1728 }
1729
1730 /**
1731  * gst_registry_get_feature_list_cookie:
1732  * @registry: the registry
1733  *
1734  * Returns the registrys feature list cookie. This changes
1735  * every time a feature is added or removed from the registry.
1736  *
1737  * Returns: the feature list cookie.
1738  *
1739  * Since: 0.10.26
1740  */
1741 guint32
1742 gst_registry_get_feature_list_cookie (GstRegistry * registry)
1743 {
1744   g_return_val_if_fail (GST_IS_REGISTRY (registry), 0);
1745
1746   return registry->priv->cookie;
1747 }