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