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