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