bufferlist: fix a comment
[platform/upstream/gstreamer.git] / gst / gstregistry.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2005 David A. Schleef <ds@schleef.org>
5  *
6  * gstregistry.c: handle registry
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /**
25  * SECTION:gstregistry
26  * @short_description: Abstract base class for management of #GstPlugin objects
27  * @see_also: #GstPlugin, #GstPluginFeature
28  *
29  * One registry holds the metadata of a set of plugins.
30  *
31  * <emphasis role="bold">Design:</emphasis>
32  *
33  * The #GstRegistry object is a list of plugins and some functions for dealing
34  * with them. #GstPlugins are matched 1-1 with a file on disk, and may or may
35  * not be loaded at a given time. There may be multiple #GstRegistry objects,
36  * but the "default registry" is the only object that has any meaning to the
37  * core.
38  *
39  * The registry.xml file is actually a cache of plugin information. This is
40  * unlike versions prior to 0.10, where the registry file was the primary source
41  * of plugin information, and was created by the gst-register command.
42  *
43  * The primary source, at all times, of plugin information is each plugin file
44  * itself. Thus, if an application wants information about a particular plugin,
45  * or wants to search for a feature that satisfies given criteria, the primary
46  * means of doing so is to load every plugin and look at the resulting
47  * information that is gathered in the default registry. Clearly, this is a time
48  * consuming process, so we cache information in the registry.xml file.
49  *
50  * On startup, plugins are searched for in the plugin search path. This path can
51  * be set directly using the %GST_PLUGIN_PATH environment variable. The registry
52  * file is loaded from ~/.gstreamer-$GST_MAJORMINOR/registry-$ARCH.xml or the
53  * file listed in the %GST_REGISTRY env var. The only reason to change the
54  * registry location is for testing.
55  *
56  * For each plugin that is found in the plugin search path, there could be 3
57  * possibilities for cached information:
58  * <itemizedlist>
59  *   <listitem>
60  *     <para>the cache may not contain information about a given file.</para>
61  *   </listitem>
62  *   <listitem>
63  *     <para>the cache may have stale information.</para>
64  *   </listitem>
65  *   <listitem>
66  *     <para>the cache may have current information.</para>
67  *   </listitem>
68  * </itemizedlist>
69  *
70  * In the first two cases, the plugin is loaded and the cache updated. In
71  * addition to these cases, the cache may have entries for plugins that are not
72  * relevant to the current process. These are marked as not available to the
73  * current process. If the cache is updated for whatever reason, it is marked
74  * dirty.
75  *
76  * A dirty cache is written out at the end of initialization. Each entry is
77  * checked to make sure the information is minimally valid. If not, the entry is
78  * simply dropped.
79  *
80  * <emphasis role="bold">Implementation notes:</emphasis>
81  *
82  * The "cache" and "default registry" are different concepts and can represent
83  * different sets of plugins. For various reasons, at init time, the cache is
84  * stored in the default registry, and plugins not relevant to the current
85  * process are marked with the %GST_PLUGIN_FLAG_CACHED bit. These plugins are
86  * removed at the end of intitialization.
87  */
88
89 #ifdef HAVE_CONFIG_H
90 #include "config.h"
91 #endif
92 #include "gst_private.h"
93 #include <glib.h>
94 #include <sys/types.h>
95 #include <sys/stat.h>
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
99 #include <errno.h>
100 #include <stdio.h>
101 #include <string.h>
102
103 /* For g_stat () */
104 #include <glib/gstdio.h>
105
106 #include "gstinfo.h"
107 #include "gstregistry.h"
108 #include "gstmarshal.h"
109 #include "gstfilter.h"
110
111 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
112
113 /* the one instance of the default registry and the mutex protecting the
114  * variable. */
115 static GStaticMutex _gst_registry_mutex = G_STATIC_MUTEX_INIT;
116 static GstRegistry *_gst_registry_default = NULL;
117
118 /* Element signals and args */
119 enum
120 {
121   PLUGIN_ADDED,
122   FEATURE_ADDED,
123   LAST_SIGNAL
124 };
125
126 static void gst_registry_finalize (GObject * object);
127
128 static guint gst_registry_signals[LAST_SIGNAL] = { 0 };
129
130 static GstPluginFeature *gst_registry_lookup_feature_locked (GstRegistry *
131     registry, const char *name);
132 static GstPlugin *gst_registry_lookup_locked (GstRegistry * registry,
133     const char *filename);
134
135 G_DEFINE_TYPE (GstRegistry, gst_registry, GST_TYPE_OBJECT);
136 static GstObjectClass *parent_class = NULL;
137
138 static void
139 gst_registry_class_init (GstRegistryClass * klass)
140 {
141   GObjectClass *gobject_class;
142
143   gobject_class = (GObjectClass *) klass;
144
145   parent_class = g_type_class_peek_parent (klass);
146
147   /**
148    * GstRegistry::plugin-added:
149    * @registry: the registry that emitted the signal
150    * @plugin: the plugin that has been added
151    *
152    * Signals that a plugin has been added to the registry (possibly
153    * replacing a previously-added one by the same name)
154    */
155   gst_registry_signals[PLUGIN_ADDED] =
156       g_signal_new ("plugin-added", G_TYPE_FROM_CLASS (klass),
157       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, plugin_added), NULL,
158       NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
159
160   /**
161    * GstRegistry::feature-added:
162    * @registry: the registry that emitted the signal
163    * @feature: the feature that has been added
164    *
165    * Signals that a feature has been added to the registry (possibly
166    * replacing a previously-added one by the same name)
167    */
168   gst_registry_signals[FEATURE_ADDED] =
169       g_signal_new ("feature-added", G_TYPE_FROM_CLASS (klass),
170       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, feature_added),
171       NULL, NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
172
173   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_registry_finalize);
174 }
175
176 static void
177 gst_registry_init (GstRegistry * registry)
178 {
179   registry->feature_hash = g_hash_table_new (g_str_hash, g_str_equal);
180 }
181
182 static void
183 gst_registry_finalize (GObject * object)
184 {
185   GstRegistry *registry = GST_REGISTRY (object);
186   GList *plugins, *p;
187   GList *features, *f;
188
189   plugins = registry->plugins;
190   registry->plugins = NULL;
191
192   GST_DEBUG_OBJECT (registry, "registry finalize");
193   p = plugins;
194   while (p) {
195     GstPlugin *plugin = p->data;
196
197     if (plugin) {
198       GST_LOG_OBJECT (registry, "removing plugin %s",
199           gst_plugin_get_name (plugin));
200       gst_object_unref (plugin);
201     }
202     p = g_list_next (p);
203   }
204   g_list_free (plugins);
205
206   features = registry->features;
207   registry->features = NULL;
208
209   f = features;
210   while (f) {
211     GstPluginFeature *feature = f->data;
212
213     if (feature) {
214       GST_LOG_OBJECT (registry, "removing feature %p (%s)",
215           feature, gst_plugin_feature_get_name (feature));
216       gst_object_unref (feature);
217     }
218     f = g_list_next (f);
219   }
220   g_list_free (features);
221
222   g_hash_table_destroy (registry->feature_hash);
223   registry->feature_hash = NULL;
224
225   G_OBJECT_CLASS (parent_class)->finalize (object);
226 }
227
228 /**
229  * gst_registry_get_default:
230  *
231  * Retrieves the default registry. The caller does not own a reference on the
232  * registry, as it is alive as long as GStreamer is initialized.
233  *
234  * Returns: The default #GstRegistry.
235  */
236 GstRegistry *
237 gst_registry_get_default (void)
238 {
239   GstRegistry *registry;
240
241   g_static_mutex_lock (&_gst_registry_mutex);
242   if (G_UNLIKELY (!_gst_registry_default)) {
243     _gst_registry_default = g_object_new (GST_TYPE_REGISTRY, NULL);
244     gst_object_ref_sink (GST_OBJECT_CAST (_gst_registry_default));
245   }
246   registry = _gst_registry_default;
247   g_static_mutex_unlock (&_gst_registry_mutex);
248
249   return registry;
250 }
251
252 /**
253  * gst_registry_add_path:
254  * @registry: the registry to add the path to
255  * @path: the path to add to the registry
256  *
257  * Add the given path to the registry. The syntax of the
258  * path is specific to the registry. If the path has already been
259  * added, do nothing.
260  */
261 void
262 gst_registry_add_path (GstRegistry * registry, const gchar * path)
263 {
264   g_return_if_fail (GST_IS_REGISTRY (registry));
265   g_return_if_fail (path != NULL);
266
267   if (strlen (path) == 0)
268     goto empty_path;
269
270   GST_OBJECT_LOCK (registry);
271   if (g_list_find_custom (registry->paths, path, (GCompareFunc) strcmp))
272     goto was_added;
273
274   GST_INFO ("Adding plugin path: \"%s\"", path);
275   registry->paths = g_list_append (registry->paths, g_strdup (path));
276   GST_OBJECT_UNLOCK (registry);
277
278   return;
279
280 empty_path:
281   {
282     GST_INFO ("Ignoring empty plugin path");
283     return;
284   }
285 was_added:
286   {
287     g_warning ("path %s already added to registry", path);
288     GST_OBJECT_UNLOCK (registry);
289     return;
290   }
291 }
292
293 /**
294  * gst_registry_get_path_list:
295  * @registry: the registry to get the pathlist of
296  *
297  * Get the list of paths for the given registry.
298  *
299  * Returns: A Glist of paths as strings. g_list_free after use.
300  *
301  * MT safe.
302  */
303 GList *
304 gst_registry_get_path_list (GstRegistry * registry)
305 {
306   GList *list;
307
308   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
309
310   GST_OBJECT_LOCK (registry);
311   /* We don't need to copy the strings, because they won't be deleted
312    * as long as the GstRegistry is around */
313   list = g_list_copy (registry->paths);
314   GST_OBJECT_UNLOCK (registry);
315
316   return list;
317 }
318
319
320 /**
321  * gst_registry_add_plugin:
322  * @registry: the registry to add the plugin to
323  * @plugin: the plugin to add
324  *
325  * Add the plugin to the registry. The plugin-added signal will be emitted.
326  * This function will sink @plugin.
327  *
328  * Returns: TRUE on success.
329  *
330  * MT safe.
331  */
332 gboolean
333 gst_registry_add_plugin (GstRegistry * registry, GstPlugin * plugin)
334 {
335   GstPlugin *existing_plugin;
336
337   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
338   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
339
340   GST_OBJECT_LOCK (registry);
341   existing_plugin = gst_registry_lookup_locked (registry, plugin->filename);
342   if (G_UNLIKELY (existing_plugin)) {
343     GST_DEBUG_OBJECT (registry,
344         "Replacing existing plugin %p with new plugin %p for filename \"%s\"",
345         existing_plugin, plugin, GST_STR_NULL (plugin->filename));
346     registry->plugins = g_list_remove (registry->plugins, existing_plugin);
347     gst_object_unref (existing_plugin);
348   }
349
350   GST_DEBUG_OBJECT (registry, "adding plugin %p for filename \"%s\"",
351       plugin, GST_STR_NULL (plugin->filename));
352
353   registry->plugins = g_list_prepend (registry->plugins, plugin);
354
355   gst_object_ref_sink (plugin);
356   GST_OBJECT_UNLOCK (registry);
357
358   GST_LOG_OBJECT (registry, "emitting plugin-added for filename \"%s\"",
359       GST_STR_NULL (plugin->filename));
360   g_signal_emit (registry, gst_registry_signals[PLUGIN_ADDED], 0, plugin);
361
362   return TRUE;
363 }
364
365 static void
366 gst_registry_remove_features_for_plugin_unlocked (GstRegistry * registry,
367     GstPlugin * plugin)
368 {
369   GList *f;
370   const gchar *name;
371
372   g_return_if_fail (GST_IS_REGISTRY (registry));
373   g_return_if_fail (GST_IS_PLUGIN (plugin));
374
375   name = gst_plugin_get_name (plugin);
376
377   /* Remove all features for this plugin */
378   f = registry->features;
379   while (f != NULL) {
380     GList *next = g_list_next (f);
381     GstPluginFeature *feature = f->data;
382
383     if (feature && !strcmp (feature->plugin_name, name)) {
384       GST_DEBUG_OBJECT (registry, "removing feature %p (%s) for plugin %s",
385           feature, gst_plugin_feature_get_name (feature), name);
386
387       registry->features = g_list_delete_link (registry->features, f);
388       g_hash_table_remove (registry->feature_hash, feature->name);
389       gst_object_unref (feature);
390     }
391     f = next;
392   }
393 }
394
395 /**
396  * gst_registry_remove_plugin:
397  * @registry: the registry to remove the plugin from
398  * @plugin: the plugin to remove
399  *
400  * Remove the plugin from the registry.
401  *
402  * MT safe.
403  */
404 void
405 gst_registry_remove_plugin (GstRegistry * registry, GstPlugin * plugin)
406 {
407   g_return_if_fail (GST_IS_REGISTRY (registry));
408   g_return_if_fail (GST_IS_PLUGIN (plugin));
409
410   GST_DEBUG_OBJECT (registry, "removing plugin %p (%s)",
411       plugin, gst_plugin_get_name (plugin));
412
413   GST_OBJECT_LOCK (registry);
414   registry->plugins = g_list_remove (registry->plugins, plugin);
415   gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
416   GST_OBJECT_UNLOCK (registry);
417   gst_object_unref (plugin);
418 }
419
420 /**
421  * gst_registry_add_feature:
422  * @registry: the registry to add the plugin to
423  * @feature: the feature to add
424  *
425  * Add the feature to the registry. The feature-added signal will be emitted.
426  * This function sinks @feature.
427  *
428  * Returns: TRUE on success.
429  *
430  * MT safe.
431  */
432 gboolean
433 gst_registry_add_feature (GstRegistry * registry, GstPluginFeature * feature)
434 {
435   GstPluginFeature *existing_feature;
436
437   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
438   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
439   g_return_val_if_fail (feature->name != NULL, FALSE);
440   g_return_val_if_fail (feature->plugin_name != NULL, FALSE);
441
442   GST_OBJECT_LOCK (registry);
443   existing_feature = gst_registry_lookup_feature_locked (registry,
444       feature->name);
445   if (G_UNLIKELY (existing_feature)) {
446     GST_DEBUG_OBJECT (registry, "replacing existing feature %p (%s)",
447         existing_feature, feature->name);
448     /* Remove the existing feature from the list now, before we insert the new
449      * one, but don't unref yet because the hash is still storing a reference to     * it. */
450     registry->features = g_list_remove (registry->features, existing_feature);
451   }
452
453   GST_DEBUG_OBJECT (registry, "adding feature %p (%s)", feature, feature->name);
454
455   registry->features = g_list_prepend (registry->features, feature);
456   g_hash_table_replace (registry->feature_hash, feature->name, feature);
457
458   if (G_UNLIKELY (existing_feature)) {
459     /* We unref now. No need to remove the feature name from the hash table, it      * got replaced by the new feature */
460     gst_object_unref (existing_feature);
461   }
462
463   gst_object_ref_sink (feature);
464   GST_OBJECT_UNLOCK (registry);
465
466   GST_LOG_OBJECT (registry, "emitting feature-added for %s", feature->name);
467   g_signal_emit (registry, gst_registry_signals[FEATURE_ADDED], 0, feature);
468
469   return TRUE;
470 }
471
472 /**
473  * gst_registry_remove_feature:
474  * @registry: the registry to remove the feature from
475  * @feature: the feature to remove
476  *
477  * Remove the feature from the registry.
478  *
479  * MT safe.
480  */
481 void
482 gst_registry_remove_feature (GstRegistry * registry, GstPluginFeature * feature)
483 {
484   g_return_if_fail (GST_IS_REGISTRY (registry));
485   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
486
487   GST_DEBUG_OBJECT (registry, "removing feature %p (%s)",
488       feature, gst_plugin_feature_get_name (feature));
489
490   GST_OBJECT_LOCK (registry);
491   registry->features = g_list_remove (registry->features, feature);
492   g_hash_table_remove (registry->feature_hash, feature->name);
493   GST_OBJECT_UNLOCK (registry);
494   gst_object_unref (feature);
495 }
496
497 /**
498  * gst_registry_plugin_filter:
499  * @registry: registry to query
500  * @filter: the filter to use
501  * @first: only return first match
502  * @user_data: user data passed to the filter function
503  *
504  * Runs a filter against all plugins in the registry and returns a #GList with
505  * the results. If the first flag is set, only the first match is
506  * returned (as a list with a single object).
507  * Every plugin is reffed; use gst_plugin_list_free() after use, which
508  * will unref again.
509  *
510  * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
511  *
512  * MT safe.
513  */
514 GList *
515 gst_registry_plugin_filter (GstRegistry * registry,
516     GstPluginFilter filter, gboolean first, gpointer user_data)
517 {
518   GList *list;
519   GList *g;
520
521   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
522
523   GST_OBJECT_LOCK (registry);
524   list = gst_filter_run (registry->plugins, (GstFilterFunc) filter, first,
525       user_data);
526   for (g = list; g; g = g->next) {
527     gst_object_ref (GST_PLUGIN_CAST (g->data));
528   }
529   GST_OBJECT_UNLOCK (registry);
530
531   return list;
532 }
533
534 /**
535  * gst_registry_feature_filter:
536  * @registry: registry to query
537  * @filter: the filter to use
538  * @first: only return first match
539  * @user_data: user data passed to the filter function
540  *
541  * Runs a filter against all features of the plugins in the registry
542  * and returns a GList with the results.
543  * If the first flag is set, only the first match is
544  * returned (as a list with a single object).
545  *
546  * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
547  * after usage.
548  *
549  * MT safe.
550  */
551 GList *
552 gst_registry_feature_filter (GstRegistry * registry,
553     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
554 {
555   GList *list;
556   GList *g;
557
558   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
559
560   GST_OBJECT_LOCK (registry);
561   list = gst_filter_run (registry->features, (GstFilterFunc) filter, first,
562       user_data);
563   for (g = list; g; g = g->next) {
564     gst_object_ref (GST_PLUGIN_FEATURE_CAST (g->data));
565   }
566   GST_OBJECT_UNLOCK (registry);
567
568   return list;
569 }
570
571 /**
572  * gst_registry_find_plugin:
573  * @registry: the registry to search
574  * @name: the plugin name to find
575  *
576  * Find the plugin with the given name in the registry.
577  * The plugin will be reffed; caller is responsible for unreffing.
578  *
579  * Returns: The plugin with the given name or NULL if the plugin was not found.
580  * gst_object_unref() after usage.
581  *
582  * MT safe.
583  */
584 GstPlugin *
585 gst_registry_find_plugin (GstRegistry * registry, const gchar * name)
586 {
587   GList *walk;
588   GstPlugin *result = NULL;
589
590   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
591   g_return_val_if_fail (name != NULL, NULL);
592
593   walk = gst_registry_plugin_filter (registry,
594       (GstPluginFilter) gst_plugin_name_filter, TRUE, (gpointer) name);
595   if (walk) {
596     result = GST_PLUGIN_CAST (walk->data);
597
598     gst_object_ref (result);
599     gst_plugin_list_free (walk);
600   }
601
602   return result;
603 }
604
605 /**
606  * gst_registry_find_feature:
607  * @registry: the registry to search
608  * @name: the pluginfeature name to find
609  * @type: the pluginfeature type to find
610  *
611  * Find the pluginfeature with the given name and type in the registry.
612  *
613  * Returns: The pluginfeature with the given name and type or NULL
614  * if the plugin was not found. gst_object_unref() after usage.
615  *
616  * MT safe.
617  */
618 GstPluginFeature *
619 gst_registry_find_feature (GstRegistry * registry, const gchar * name,
620     GType type)
621 {
622   GstPluginFeature *feature = NULL;
623   GList *walk;
624   GstTypeNameData data;
625
626   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
627   g_return_val_if_fail (name != NULL, NULL);
628   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
629
630   data.name = name;
631   data.type = type;
632
633   walk = gst_registry_feature_filter (registry,
634       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
635       TRUE, &data);
636
637   if (walk) {
638     feature = GST_PLUGIN_FEATURE_CAST (walk->data);
639
640     gst_object_ref (feature);
641     gst_plugin_feature_list_free (walk);
642   }
643
644   return feature;
645 }
646
647 /**
648  * gst_registry_get_feature_list:
649  * @registry: a #GstRegistry
650  * @type: a #GType.
651  *
652  * Retrieves a #GList of #GstPluginFeature of @type.
653  *
654  * Returns: a #GList of #GstPluginFeature of @type. Use
655  * gst_plugin_feature_list_free() after usage.
656  *
657  * MT safe.
658  */
659 GList *
660 gst_registry_get_feature_list (GstRegistry * registry, GType type)
661 {
662   GstTypeNameData data;
663
664   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
665   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
666
667   data.type = type;
668   data.name = NULL;
669
670   return gst_registry_feature_filter (registry,
671       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
672       FALSE, &data);
673 }
674
675 /**
676  * gst_registry_get_plugin_list:
677  * @registry: the registry to search
678  *
679  * Get a copy of all plugins registered in the given registry. The refcount
680  * of each element in the list in incremented.
681  *
682  * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
683  *
684  * MT safe.
685  */
686 GList *
687 gst_registry_get_plugin_list (GstRegistry * registry)
688 {
689   GList *list;
690   GList *g;
691
692   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
693
694   GST_OBJECT_LOCK (registry);
695   list = g_list_copy (registry->plugins);
696   for (g = list; g; g = g->next) {
697     gst_object_ref (GST_PLUGIN_CAST (g->data));
698   }
699   GST_OBJECT_UNLOCK (registry);
700
701   return list;
702 }
703
704 static GstPluginFeature *
705 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
706 {
707   if (G_UNLIKELY (name == NULL))
708     return NULL;
709
710   return g_hash_table_lookup (registry->feature_hash, name);
711 }
712
713 /**
714  * gst_registry_lookup_feature:
715  * @registry: a #GstRegistry
716  * @name: a #GstPluginFeature name
717  *
718  * Find a #GstPluginFeature with @name in @registry.
719  *
720  * Returns: a #GstPluginFeature with its refcount incremented, use
721  * gst_object_unref() after usage.
722  *
723  * MT safe.
724  */
725 GstPluginFeature *
726 gst_registry_lookup_feature (GstRegistry * registry, const char *name)
727 {
728   GstPluginFeature *feature;
729
730   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
731   g_return_val_if_fail (name != NULL, NULL);
732
733   GST_OBJECT_LOCK (registry);
734   feature = gst_registry_lookup_feature_locked (registry, name);
735   if (feature)
736     gst_object_ref (feature);
737   GST_OBJECT_UNLOCK (registry);
738
739   return feature;
740 }
741
742 static GstPlugin *
743 gst_registry_lookup_locked (GstRegistry * registry, const char *filename)
744 {
745   GList *g;
746   GstPlugin *plugin;
747   gchar *basename;
748
749   if (G_UNLIKELY (filename == NULL))
750     return NULL;
751
752   basename = g_path_get_basename (filename);
753   /* FIXME: use GTree speed up lookups */
754   for (g = registry->plugins; g; g = g_list_next (g)) {
755     plugin = GST_PLUGIN_CAST (g->data);
756     if (plugin->basename && strcmp (basename, plugin->basename) == 0) {
757       g_free (basename);
758       return plugin;
759     }
760   }
761
762   g_free (basename);
763   return NULL;
764 }
765
766 /**
767  * gst_registry_lookup:
768  * @registry: the registry to look up in
769  * @filename: the name of the file to look up
770  *
771  * Look up a plugin in the given registry with the given filename.
772  * If found, plugin is reffed.
773  *
774  * Returns: the #GstPlugin if found, or NULL if not. gst_object_unref()
775  * after usage.
776  */
777 GstPlugin *
778 gst_registry_lookup (GstRegistry * registry, const char *filename)
779 {
780   GstPlugin *plugin;
781
782   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
783   g_return_val_if_fail (filename != NULL, NULL);
784
785   GST_OBJECT_LOCK (registry);
786   plugin = gst_registry_lookup_locked (registry, filename);
787   if (plugin)
788     gst_object_ref (plugin);
789   GST_OBJECT_UNLOCK (registry);
790
791   return plugin;
792 }
793
794 static gboolean
795 gst_registry_scan_path_level (GstRegistry * registry, const gchar * path,
796     int level)
797 {
798   GDir *dir;
799   const gchar *dirent;
800   gchar *filename;
801   GstPlugin *plugin;
802   GstPlugin *newplugin;
803   gboolean changed = FALSE;
804
805   dir = g_dir_open (path, 0, NULL);
806   if (!dir)
807     return FALSE;
808
809   while ((dirent = g_dir_read_name (dir))) {
810     struct stat file_status;
811
812     filename = g_strjoin ("/", path, dirent, NULL);
813     if (g_stat (filename, &file_status) < 0) {
814       /* Plugin will be removed from cache after the scan completes if it
815        * is still marked 'cached' */
816       g_free (filename);
817       continue;
818     }
819
820     if (file_status.st_mode & S_IFDIR) {
821       /* skip the .debug directory, these contain elf files that are not
822        * useful or worse, can crash dlopen () */
823       if (g_str_equal (dirent, ".debug") || g_str_equal (dirent, ".git")) {
824         GST_LOG_OBJECT (registry, "ignoring .debug or .git directory");
825         g_free (filename);
826         continue;
827       }
828       /* FIXME 0.11: Don't recurse into directories, this behaviour
829        * is inconsistent with other PATH environment variables
830        */
831       if (level > 0) {
832         GST_LOG_OBJECT (registry, "recursing into directory %s", filename);
833         changed |= gst_registry_scan_path_level (registry, filename, level - 1);
834       } else {
835         GST_LOG_OBJECT (registry, "not recursing into directory %s, "
836             "recursion level too deep", filename);
837       }
838       g_free (filename);
839       continue;
840     }
841     if (!(file_status.st_mode & S_IFREG)) {
842       GST_LOG_OBJECT (registry, "%s is not a regular file, ignoring", filename);
843       g_free (filename);
844       continue;
845     }
846     if (!g_str_has_suffix (dirent, G_MODULE_SUFFIX)
847 #ifdef GST_EXTRA_MODULE_SUFFIX
848         && !g_str_has_suffix (dirent, GST_EXTRA_MODULE_SUFFIX)
849 #endif
850         ) {
851       GST_LOG_OBJECT (registry, "extension is not recognized as module file, "
852           "ignoring file %s", filename);
853       g_free (filename);
854       continue;
855     }
856
857     GST_LOG_OBJECT (registry, "file %s looks like a possible module", filename);
858
859     /* plug-ins are considered unique by basename; if the given name
860      * was already seen by the registry, we ignore it */
861     plugin = gst_registry_lookup (registry, filename);
862     if (plugin) {
863       gboolean env_vars_changed, deps_changed = FALSE;
864
865       if (plugin->registered) {
866         GST_DEBUG_OBJECT (registry,
867             "plugin already registered from path \"%s\"",
868             GST_STR_NULL (plugin->filename));
869         g_free (filename);
870         gst_object_unref (plugin);
871         continue;
872       }
873
874       env_vars_changed = _priv_plugin_deps_env_vars_changed (plugin);
875
876       if (plugin->file_mtime == file_status.st_mtime &&
877           plugin->file_size == file_status.st_size && !env_vars_changed &&
878           !(deps_changed = _priv_plugin_deps_files_changed (plugin))) {
879         GST_LOG_OBJECT (registry, "file %s cached", filename);
880         plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
881         GST_LOG_OBJECT (registry, "marking plugin %p as registered as %s",
882             plugin, filename);
883         plugin->registered = TRUE;
884         /* Update the file path on which we've seen this cached plugin
885          * to ensure the registry cache will reflect up to date information */
886         if (strcmp (plugin->filename, filename) != 0) {
887           g_free (plugin->filename);
888           plugin->filename = g_strdup (filename);
889           changed = TRUE;
890         }
891       } else {
892         GST_INFO_OBJECT (registry, "cached info for %s is stale", filename);
893         GST_DEBUG_OBJECT (registry, "mtime %ld != %ld or size %"
894             G_GINT64_FORMAT " != %" G_GINT64_FORMAT " or external dependency "
895             "env_vars changed: %d or external dependencies changed: %d",
896             plugin->file_mtime, file_status.st_mtime,
897             (gint64) plugin->file_size, (gint64) file_status.st_size,
898             env_vars_changed, deps_changed);
899         gst_registry_remove_plugin (gst_registry_get_default (), plugin);
900         /* We don't use a GError here because a failure to load some shared 
901          * objects as plugins is normal (particularly in the uninstalled case)
902          */
903         newplugin = gst_plugin_load_file (filename, NULL);
904         if (newplugin) {
905           GST_DEBUG_OBJECT (registry, "marking new plugin %p as registered",
906               newplugin);
907           newplugin->registered = TRUE;
908           gst_object_unref (newplugin);
909         }
910         changed = TRUE;
911       }
912       gst_object_unref (plugin);
913
914     } else {
915       GST_DEBUG_OBJECT (registry, "file %s not yet in registry", filename);
916       newplugin = gst_plugin_load_file (filename, NULL);
917       if (newplugin) {
918         newplugin->registered = TRUE;
919         gst_object_unref (newplugin);
920         changed = TRUE;
921       }
922     }
923
924     g_free (filename);
925   }
926
927   g_dir_close (dir);
928
929   return changed;
930 }
931
932 /**
933  * gst_registry_scan_path:
934  * @registry: the registry to add the path to
935  * @path: the path to add to the registry
936  *
937  * Add the given path to the registry. The syntax of the
938  * path is specific to the registry. If the path has already been
939  * added, do nothing.
940  *
941  * Returns: %TRUE if registry changed
942  */
943 gboolean
944 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
945 {
946   gboolean changed;
947
948   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
949   g_return_val_if_fail (path != NULL, FALSE);
950
951   GST_DEBUG_OBJECT (registry, "scanning path %s", path);
952   changed = gst_registry_scan_path_level (registry, path, 10);
953
954   GST_DEBUG_OBJECT (registry, "registry changed in path %s: %d", path, changed);
955
956   return changed;
957 }
958
959 /* Unref all plugins marked 'cached', to clear old plugins that no
960  * longer exist. Returns TRUE if any plugins were removed */
961 gboolean
962 _priv_gst_registry_remove_cache_plugins (GstRegistry * registry)
963 {
964   GList *g;
965   GList *g_next;
966   GstPlugin *plugin;
967   gboolean changed = FALSE;
968
969   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
970
971   GST_OBJECT_LOCK (registry);
972
973   GST_DEBUG_OBJECT (registry, "removing cached plugins");
974   g = registry->plugins;
975   while (g) {
976     g_next = g->next;
977     plugin = g->data;
978     if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
979       GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
980           GST_STR_NULL (plugin->filename));
981       registry->plugins = g_list_delete_link (registry->plugins, g);
982       gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
983       gst_object_unref (plugin);
984       changed = TRUE;
985     }
986     g = g_next;
987   }
988
989   GST_OBJECT_UNLOCK (registry);
990
991   return changed;
992 }
993
994
995 static gboolean
996 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
997     gpointer user_data)
998 {
999   return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
1000 }
1001
1002 /**
1003  * gst_registry_get_feature_list_by_plugin:
1004  * @registry: a #GstRegistry.
1005  * @name: a plugin name.
1006  *
1007  * Retrieves a #GList of features of the plugin with name @name.
1008  *
1009  * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
1010  * after usage.
1011  */
1012 GList *
1013 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
1014     const gchar * name)
1015 {
1016   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1017   g_return_val_if_fail (name != NULL, NULL);
1018
1019   return gst_registry_feature_filter (registry,
1020       _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1021 }
1022
1023 /* Unref and delete the default registry */
1024 void
1025 _priv_gst_registry_cleanup ()
1026 {
1027   GstRegistry *registry;
1028
1029   g_static_mutex_lock (&_gst_registry_mutex);
1030   if ((registry = _gst_registry_default) != NULL) {
1031     _gst_registry_default = NULL;
1032   }
1033   g_static_mutex_unlock (&_gst_registry_mutex);
1034
1035   /* unref outside of the lock because we can. */
1036   if (registry)
1037     gst_object_unref (registry);
1038 }
1039
1040 /**
1041  * gst_default_registry_check_feature_version:
1042  * @feature_name: the name of the feature (e.g. "oggdemux")
1043  * @min_major: the minimum major version number
1044  * @min_minor: the minimum minor version number
1045  * @min_micro: the minimum micro version number
1046  *
1047  * Checks whether a plugin feature by the given name exists in the
1048  * default registry and whether its version is at least the
1049  * version required.
1050  *
1051  * Returns: #TRUE if the feature could be found and the version is
1052  * the same as the required version or newer, and #FALSE otherwise.
1053  */
1054 gboolean
1055 gst_default_registry_check_feature_version (const gchar * feature_name,
1056     guint min_major, guint min_minor, guint min_micro)
1057 {
1058   GstPluginFeature *feature;
1059   GstRegistry *registry;
1060   gboolean ret = FALSE;
1061
1062   g_return_val_if_fail (feature_name != NULL, FALSE);
1063
1064   GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1065
1066   registry = gst_registry_get_default ();
1067   feature = gst_registry_lookup_feature (registry, feature_name);
1068   if (feature) {
1069     ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1070         min_micro);
1071     gst_object_unref (feature);
1072   } else {
1073     GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1074   }
1075
1076   return ret;
1077 }