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