bfb099d3ad3e1ac3b58721671e88847e95a57e12
[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 (GST_OBJECT_CAST (_gst_registry_default));
245     gst_object_sink (GST_OBJECT_CAST (_gst_registry_default));
246   }
247   registry = _gst_registry_default;
248   g_static_mutex_unlock (&_gst_registry_mutex);
249
250   return registry;
251 }
252
253 /**
254  * gst_registry_add_path:
255  * @registry: the registry to add the path to
256  * @path: the path to add to the registry
257  *
258  * Add the given path to the registry. The syntax of the
259  * path is specific to the registry. If the path has already been
260  * added, do nothing.
261  */
262 void
263 gst_registry_add_path (GstRegistry * registry, const gchar * path)
264 {
265   g_return_if_fail (GST_IS_REGISTRY (registry));
266   g_return_if_fail (path != NULL);
267
268   if (strlen (path) == 0)
269     goto empty_path;
270
271   GST_OBJECT_LOCK (registry);
272   if (g_list_find_custom (registry->paths, path, (GCompareFunc) strcmp))
273     goto was_added;
274
275   GST_INFO ("Adding plugin path: \"%s\"", path);
276   registry->paths = g_list_append (registry->paths, g_strdup (path));
277   GST_OBJECT_UNLOCK (registry);
278
279   return;
280
281 empty_path:
282   {
283     GST_INFO ("Ignoring empty plugin path");
284     return;
285   }
286 was_added:
287   {
288     g_warning ("path %s already added to registry", path);
289     GST_OBJECT_UNLOCK (registry);
290     return;
291   }
292 }
293
294 /**
295  * gst_registry_get_path_list:
296  * @registry: the registry to get the pathlist of
297  *
298  * Get the list of paths for the given registry.
299  *
300  * Returns: A Glist of paths as strings. g_list_free after use.
301  *
302  * MT safe.
303  */
304 GList *
305 gst_registry_get_path_list (GstRegistry * registry)
306 {
307   GList *list;
308
309   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
310
311   GST_OBJECT_LOCK (registry);
312   /* We don't need to copy the strings, because they won't be deleted
313    * as long as the GstRegistry is around */
314   list = g_list_copy (registry->paths);
315   GST_OBJECT_UNLOCK (registry);
316
317   return list;
318 }
319
320
321 /**
322  * gst_registry_add_plugin:
323  * @registry: the registry to add the plugin to
324  * @plugin: the plugin to add
325  *
326  * Add the plugin to the registry. The plugin-added signal will be emitted.
327  * This function will sink @plugin.
328  *
329  * Returns: TRUE on success.
330  *
331  * MT safe.
332  */
333 gboolean
334 gst_registry_add_plugin (GstRegistry * registry, GstPlugin * plugin)
335 {
336   GstPlugin *existing_plugin;
337
338   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
339   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
340
341   GST_OBJECT_LOCK (registry);
342   existing_plugin = gst_registry_lookup_locked (registry, plugin->filename);
343   if (G_UNLIKELY (existing_plugin)) {
344     GST_DEBUG_OBJECT (registry,
345         "Replacing existing plugin %p with new plugin %p for filename \"%s\"",
346         existing_plugin, plugin, GST_STR_NULL (plugin->filename));
347     registry->plugins = g_list_remove (registry->plugins, existing_plugin);
348     gst_object_unref (existing_plugin);
349   }
350
351   GST_DEBUG_OBJECT (registry, "adding plugin %p for filename \"%s\"",
352       plugin, GST_STR_NULL (plugin->filename));
353
354   registry->plugins = g_list_prepend (registry->plugins, plugin);
355
356   gst_object_ref (plugin);
357   gst_object_sink (plugin);
358   GST_OBJECT_UNLOCK (registry);
359
360   GST_LOG_OBJECT (registry, "emitting plugin-added for filename \"%s\"",
361       GST_STR_NULL (plugin->filename));
362   g_signal_emit (G_OBJECT (registry), gst_registry_signals[PLUGIN_ADDED], 0,
363       plugin);
364
365   return TRUE;
366 }
367
368 static void
369 gst_registry_remove_features_for_plugin_unlocked (GstRegistry * registry,
370     GstPlugin * plugin)
371 {
372   GList *f;
373
374   g_return_if_fail (GST_IS_REGISTRY (registry));
375   g_return_if_fail (GST_IS_PLUGIN (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, gst_plugin_get_name (plugin))) {
384       GST_DEBUG_OBJECT (registry, "removing feature %p (%s) for plugin %s",
385           feature, gst_plugin_feature_get_name (feature),
386           gst_plugin_get_name (plugin));
387
388       registry->features = g_list_delete_link (registry->features, f);
389       g_hash_table_remove (registry->feature_hash, feature->name);
390       gst_object_unref (feature);
391     }
392     f = next;
393   }
394 }
395
396 /**
397  * gst_registry_remove_plugin:
398  * @registry: the registry to remove the plugin from
399  * @plugin: the plugin to remove
400  *
401  * Remove the plugin from the registry.
402  *
403  * MT safe.
404  */
405 void
406 gst_registry_remove_plugin (GstRegistry * registry, GstPlugin * plugin)
407 {
408   g_return_if_fail (GST_IS_REGISTRY (registry));
409   g_return_if_fail (GST_IS_PLUGIN (plugin));
410
411   GST_DEBUG_OBJECT (registry, "removing plugin %p (%s)",
412       plugin, gst_plugin_get_name (plugin));
413
414   GST_OBJECT_LOCK (registry);
415   registry->plugins = g_list_remove (registry->plugins, plugin);
416   gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
417   GST_OBJECT_UNLOCK (registry);
418   gst_object_unref (plugin);
419 }
420
421 /**
422  * gst_registry_add_feature:
423  * @registry: the registry to add the plugin to
424  * @feature: the feature to add
425  *
426  * Add the feature to the registry. The feature-added signal will be emitted.
427  * This function sinks @feature.
428  *
429  * Returns: TRUE on success.
430  *
431  * MT safe.
432  */
433 gboolean
434 gst_registry_add_feature (GstRegistry * registry, GstPluginFeature * feature)
435 {
436   GstPluginFeature *existing_feature;
437
438   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
439   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
440   g_return_val_if_fail (feature->name != NULL, FALSE);
441   g_return_val_if_fail (feature->plugin_name != NULL, FALSE);
442
443   GST_OBJECT_LOCK (registry);
444   existing_feature = gst_registry_lookup_feature_locked (registry,
445       feature->name);
446   if (G_UNLIKELY (existing_feature)) {
447     GST_DEBUG_OBJECT (registry, "replacing existing feature %p (%s)",
448         existing_feature, feature->name);
449     /* Remove the existing feature from the list now, before we insert the new
450      * one, but don't unref yet because the hash is still storing a reference to     * it. */
451     registry->features = g_list_remove (registry->features, existing_feature);
452   }
453
454   GST_DEBUG_OBJECT (registry, "adding feature %p (%s)", feature, feature->name);
455
456   registry->features = g_list_prepend (registry->features, feature);
457   g_hash_table_replace (registry->feature_hash, feature->name, feature);
458
459   if (G_UNLIKELY (existing_feature)) {
460     /* We unref now. No need to remove the feature name from the hash table, it      * got replaced by the new feature */
461     gst_object_unref (existing_feature);
462   }
463
464   gst_object_ref (feature);
465   gst_object_sink (feature);
466   GST_OBJECT_UNLOCK (registry);
467
468   GST_LOG_OBJECT (registry, "emitting feature-added for %s", feature->name);
469   g_signal_emit (G_OBJECT (registry), gst_registry_signals[FEATURE_ADDED], 0,
470       feature);
471
472   return TRUE;
473 }
474
475 /**
476  * gst_registry_remove_feature:
477  * @registry: the registry to remove the feature from
478  * @feature: the feature to remove
479  *
480  * Remove the feature from the registry.
481  *
482  * MT safe.
483  */
484 void
485 gst_registry_remove_feature (GstRegistry * registry, GstPluginFeature * feature)
486 {
487   g_return_if_fail (GST_IS_REGISTRY (registry));
488   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
489
490   GST_DEBUG_OBJECT (registry, "removing feature %p (%s)",
491       feature, gst_plugin_feature_get_name (feature));
492
493   GST_OBJECT_LOCK (registry);
494   registry->features = g_list_remove (registry->features, feature);
495   g_hash_table_remove (registry->feature_hash, feature->name);
496   GST_OBJECT_UNLOCK (registry);
497   gst_object_unref (feature);
498 }
499
500 /**
501  * gst_registry_plugin_filter:
502  * @registry: registry to query
503  * @filter: the filter to use
504  * @first: only return first match
505  * @user_data: user data passed to the filter function
506  *
507  * Runs a filter against all plugins in the registry and returns a #GList with
508  * the results. If the first flag is set, only the first match is
509  * returned (as a list with a single object).
510  * Every plugin is reffed; use gst_plugin_list_free() after use, which
511  * will unref again.
512  *
513  * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
514  *
515  * MT safe.
516  */
517 GList *
518 gst_registry_plugin_filter (GstRegistry * registry,
519     GstPluginFilter filter, gboolean first, gpointer user_data)
520 {
521   GList *list;
522   GList *g;
523
524   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
525
526   GST_OBJECT_LOCK (registry);
527   list = gst_filter_run (registry->plugins, (GstFilterFunc) filter, first,
528       user_data);
529   for (g = list; g; g = g->next) {
530     gst_object_ref (GST_PLUGIN_CAST (g->data));
531   }
532   GST_OBJECT_UNLOCK (registry);
533
534   return list;
535 }
536
537 /**
538  * gst_registry_feature_filter:
539  * @registry: registry to query
540  * @filter: the filter to use
541  * @first: only return first match
542  * @user_data: user data passed to the filter function
543  *
544  * Runs a filter against all features of the plugins in the registry
545  * and returns a GList with the results.
546  * If the first flag is set, only the first match is
547  * returned (as a list with a single object).
548  *
549  * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
550  * after usage.
551  *
552  * MT safe.
553  */
554 GList *
555 gst_registry_feature_filter (GstRegistry * registry,
556     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
557 {
558   GList *list;
559   GList *g;
560
561   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
562
563   GST_OBJECT_LOCK (registry);
564   list = gst_filter_run (registry->features, (GstFilterFunc) filter, first,
565       user_data);
566   for (g = list; g; g = g->next) {
567     gst_object_ref (GST_PLUGIN_FEATURE_CAST (g->data));
568   }
569   GST_OBJECT_UNLOCK (registry);
570
571   return list;
572 }
573
574 /**
575  * gst_registry_find_plugin:
576  * @registry: the registry to search
577  * @name: the plugin name to find
578  *
579  * Find the plugin with the given name in the registry.
580  * The plugin will be reffed; caller is responsible for unreffing.
581  *
582  * Returns: The plugin with the given name or NULL if the plugin was not found.
583  * gst_object_unref() after usage.
584  *
585  * MT safe.
586  */
587 GstPlugin *
588 gst_registry_find_plugin (GstRegistry * registry, const gchar * name)
589 {
590   GList *walk;
591   GstPlugin *result = NULL;
592
593   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
594   g_return_val_if_fail (name != NULL, NULL);
595
596   walk = gst_registry_plugin_filter (registry,
597       (GstPluginFilter) gst_plugin_name_filter, TRUE, (gpointer) name);
598   if (walk) {
599     result = GST_PLUGIN_CAST (walk->data);
600
601     gst_object_ref (result);
602     gst_plugin_list_free (walk);
603   }
604
605   return result;
606 }
607
608 /**
609  * gst_registry_find_feature:
610  * @registry: the registry to search
611  * @name: the pluginfeature name to find
612  * @type: the pluginfeature type to find
613  *
614  * Find the pluginfeature with the given name and type in the registry.
615  *
616  * Returns: The pluginfeature with the given name and type or NULL
617  * if the plugin was not found. gst_object_unref() after usage.
618  *
619  * MT safe.
620  */
621 GstPluginFeature *
622 gst_registry_find_feature (GstRegistry * registry, const gchar * name,
623     GType type)
624 {
625   GstPluginFeature *feature = NULL;
626   GList *walk;
627   GstTypeNameData data;
628
629   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
630   g_return_val_if_fail (name != NULL, NULL);
631   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
632
633   data.name = name;
634   data.type = type;
635
636   walk = gst_registry_feature_filter (registry,
637       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
638       TRUE, &data);
639
640   if (walk) {
641     feature = GST_PLUGIN_FEATURE_CAST (walk->data);
642
643     gst_object_ref (feature);
644     gst_plugin_feature_list_free (walk);
645   }
646
647   return feature;
648 }
649
650 /**
651  * gst_registry_get_feature_list:
652  * @registry: a #GstRegistry
653  * @type: a #GType.
654  *
655  * Retrieves a #GList of #GstPluginFeature of @type.
656  *
657  * Returns: a #GList of #GstPluginFeature of @type. Use
658  * gst_plugin_feature_list_free() after usage.
659  *
660  * MT safe.
661  */
662 GList *
663 gst_registry_get_feature_list (GstRegistry * registry, GType type)
664 {
665   GstTypeNameData data;
666
667   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
668   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
669
670   data.type = type;
671   data.name = NULL;
672
673   return gst_registry_feature_filter (registry,
674       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
675       FALSE, &data);
676 }
677
678 /**
679  * gst_registry_get_plugin_list:
680  * @registry: the registry to search
681  *
682  * Get a copy of all plugins registered in the given registry. The refcount
683  * of each element in the list in incremented.
684  *
685  * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
686  *
687  * MT safe.
688  */
689 GList *
690 gst_registry_get_plugin_list (GstRegistry * registry)
691 {
692   GList *list;
693   GList *g;
694
695   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
696
697   GST_OBJECT_LOCK (registry);
698   list = g_list_copy (registry->plugins);
699   for (g = list; g; g = g->next) {
700     gst_object_ref (GST_PLUGIN_CAST (g->data));
701   }
702   GST_OBJECT_UNLOCK (registry);
703
704   return list;
705 }
706
707 static GstPluginFeature *
708 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
709 {
710   if (G_UNLIKELY (name == NULL))
711     return NULL;
712
713   return g_hash_table_lookup (registry->feature_hash, name);
714 }
715
716 /**
717  * gst_registry_lookup_feature:
718  * @registry: a #GstRegistry
719  * @name: a #GstPluginFeature name
720  *
721  * Find a #GstPluginFeature with @name in @registry.
722  *
723  * Returns: a #GstPluginFeature with its refcount incremented, use
724  * gst_object_unref() after usage.
725  *
726  * MT safe.
727  */
728 GstPluginFeature *
729 gst_registry_lookup_feature (GstRegistry * registry, const char *name)
730 {
731   GstPluginFeature *feature;
732
733   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
734   g_return_val_if_fail (name != NULL, NULL);
735
736   GST_OBJECT_LOCK (registry);
737   feature = gst_registry_lookup_feature_locked (registry, name);
738   if (feature)
739     gst_object_ref (feature);
740   GST_OBJECT_UNLOCK (registry);
741
742   return feature;
743 }
744
745 static GstPlugin *
746 gst_registry_lookup_locked (GstRegistry * registry, const char *filename)
747 {
748   GList *g;
749   GstPlugin *plugin;
750   gchar *basename;
751
752   if (G_UNLIKELY (filename == NULL))
753     return NULL;
754
755   basename = g_path_get_basename (filename);
756   /* FIXME: use GTree speed up lookups */
757   for (g = registry->plugins; g; g = g_list_next (g)) {
758     plugin = GST_PLUGIN_CAST (g->data);
759     if (plugin->basename && strcmp (basename, plugin->basename) == 0) {
760       g_free (basename);
761       return plugin;
762     }
763   }
764
765   g_free (basename);
766   return NULL;
767 }
768
769 /**
770  * gst_registry_lookup:
771  * @registry: the registry to look up in
772  * @filename: the name of the file to look up
773  *
774  * Look up a plugin in the given registry with the given filename.
775  * If found, plugin is reffed.
776  *
777  * Returns: the #GstPlugin if found, or NULL if not. gst_object_unref()
778  * after usage.
779  */
780 GstPlugin *
781 gst_registry_lookup (GstRegistry * registry, const char *filename)
782 {
783   GstPlugin *plugin;
784
785   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
786   g_return_val_if_fail (filename != NULL, NULL);
787
788   GST_OBJECT_LOCK (registry);
789   plugin = gst_registry_lookup_locked (registry, filename);
790   if (plugin)
791     gst_object_ref (plugin);
792   GST_OBJECT_UNLOCK (registry);
793
794   return plugin;
795 }
796
797 static gboolean
798 gst_registry_scan_path_level (GstRegistry * registry, const gchar * path,
799     int level)
800 {
801   GDir *dir;
802   const gchar *dirent;
803   gchar *filename;
804   GstPlugin *plugin;
805   GstPlugin *newplugin;
806   gboolean changed = FALSE;
807
808   dir = g_dir_open (path, 0, NULL);
809   if (!dir)
810     return FALSE;
811
812   while ((dirent = g_dir_read_name (dir))) {
813     struct stat file_status;
814
815     filename = g_strjoin ("/", path, dirent, NULL);
816     if (g_stat (filename, &file_status) < 0) {
817       /* Plugin will be removed from cache after the scan completes if it
818        * is still marked 'cached' */
819       g_free (filename);
820       continue;
821     }
822
823     if (file_status.st_mode & S_IFDIR) {
824       /* skip the .debug directory, these contain elf files that are not
825        * useful or worse, can crash dlopen () */
826       if (g_str_equal (dirent, ".debug")) {
827         GST_LOG_OBJECT (registry, "found .debug directory, ignoring");
828         g_free (filename);
829         continue;
830       }
831       /* FIXME 0.11: Don't recurse into directories, this behaviour
832        * is inconsistent with other PATH environment variables
833        */
834       if (level > 0) {
835         GST_LOG_OBJECT (registry, "recursing into directory %s", filename);
836         changed |= gst_registry_scan_path_level (registry, filename, level - 1);
837       } else {
838         GST_LOG_OBJECT (registry, "not recursing into directory %s, "
839             "recursion level too deep", filename);
840       }
841       g_free (filename);
842       continue;
843     }
844     if (!(file_status.st_mode & S_IFREG)) {
845       GST_LOG_OBJECT (registry, "%s is not a regular file, ignoring", filename);
846       g_free (filename);
847       continue;
848     }
849     if (!g_str_has_suffix (dirent, G_MODULE_SUFFIX)
850 #ifdef GST_EXTRA_MODULE_SUFFIX
851         && !g_str_has_suffix (dirent, GST_EXTRA_MODULE_SUFFIX)
852 #endif
853         ) {
854       GST_LOG_OBJECT (registry, "extension is not recognized as module file, "
855           "ignoring file %s", filename);
856       g_free (filename);
857       continue;
858     }
859
860     GST_LOG_OBJECT (registry, "file %s looks like a possible module", filename);
861
862     /* plug-ins are considered unique by basename; if the given name
863      * was already seen by the registry, we ignore it */
864     plugin = gst_registry_lookup (registry, filename);
865     if (plugin) {
866       gboolean env_vars_changed, deps_changed = FALSE;
867
868       if (plugin->registered) {
869         GST_DEBUG_OBJECT (registry,
870             "plugin already registered from path \"%s\"",
871             GST_STR_NULL (plugin->filename));
872         g_free (filename);
873         gst_object_unref (plugin);
874         continue;
875       }
876
877       env_vars_changed = _priv_plugin_deps_env_vars_changed (plugin);
878
879       if (plugin->file_mtime == file_status.st_mtime &&
880           plugin->file_size == file_status.st_size && !env_vars_changed &&
881           !(deps_changed = _priv_plugin_deps_files_changed (plugin))) {
882         GST_LOG_OBJECT (registry, "file %s cached", filename);
883         plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
884         GST_LOG_OBJECT (registry, "marking plugin %p as registered as %s",
885             plugin, filename);
886         plugin->registered = TRUE;
887         /* Update the file path on which we've seen this cached plugin
888          * to ensure the registry cache will reflect up to date information */
889         if (strcmp (plugin->filename, filename) != 0) {
890           g_free (plugin->filename);
891           plugin->filename = g_strdup (filename);
892           changed = TRUE;
893         }
894       } else {
895         GST_INFO_OBJECT (registry, "cached info for %s is stale", filename);
896         GST_DEBUG_OBJECT (registry, "mtime %ld != %ld or size %"
897             G_GINT64_FORMAT " != %" G_GINT64_FORMAT " or external dependency "
898             "env_vars changed: %d or external dependencies changed: %d",
899             plugin->file_mtime, file_status.st_mtime,
900             (gint64) plugin->file_size, (gint64) file_status.st_size,
901             env_vars_changed, deps_changed);
902         gst_registry_remove_plugin (gst_registry_get_default (), plugin);
903         /* We don't use a GError here because a failure to load some shared 
904          * objects as plugins is normal (particularly in the uninstalled case)
905          */
906         newplugin = gst_plugin_load_file (filename, NULL);
907         if (newplugin) {
908           GST_DEBUG_OBJECT (registry, "marking new plugin %p as registered",
909               newplugin);
910           newplugin->registered = TRUE;
911           gst_object_unref (newplugin);
912         }
913         changed = TRUE;
914       }
915       gst_object_unref (plugin);
916
917     } else {
918       GST_DEBUG_OBJECT (registry, "file %s not yet in registry", filename);
919       newplugin = gst_plugin_load_file (filename, NULL);
920       if (newplugin) {
921         newplugin->registered = TRUE;
922         gst_object_unref (newplugin);
923         changed = TRUE;
924       }
925     }
926
927     g_free (filename);
928   }
929
930   g_dir_close (dir);
931
932   return changed;
933 }
934
935 /**
936  * gst_registry_scan_path:
937  * @registry: the registry to add the path to
938  * @path: the path to add to the registry
939  *
940  * Add the given path to the registry. The syntax of the
941  * path is specific to the registry. If the path has already been
942  * added, do nothing.
943  *
944  * Returns: %TRUE if registry changed
945  */
946 gboolean
947 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
948 {
949   gboolean changed;
950
951   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
952   g_return_val_if_fail (path != NULL, FALSE);
953
954   GST_DEBUG_OBJECT (registry, "scanning path %s", path);
955   changed = gst_registry_scan_path_level (registry, path, 10);
956
957   GST_DEBUG_OBJECT (registry, "registry changed in path %s: %d", path, changed);
958
959   return changed;
960 }
961
962 /* Unref all plugins marked 'cached', to clear old plugins that no
963  * longer exist. Returns TRUE if any plugins were removed */
964 gboolean
965 _priv_gst_registry_remove_cache_plugins (GstRegistry * registry)
966 {
967   GList *g;
968   GList *g_next;
969   GstPlugin *plugin;
970   gboolean changed = FALSE;
971
972   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
973
974   GST_OBJECT_LOCK (registry);
975
976   GST_DEBUG_OBJECT (registry, "removing cached plugins");
977   g = registry->plugins;
978   while (g) {
979     g_next = g->next;
980     plugin = g->data;
981     if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
982       GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
983           GST_STR_NULL (plugin->filename));
984       registry->plugins = g_list_delete_link (registry->plugins, g);
985       gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
986       gst_object_unref (plugin);
987       changed = TRUE;
988     }
989     g = g_next;
990   }
991
992   GST_OBJECT_UNLOCK (registry);
993
994   return changed;
995 }
996
997
998 static gboolean
999 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
1000     gpointer user_data)
1001 {
1002   return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
1003 }
1004
1005 /**
1006  * gst_registry_get_feature_list_by_plugin:
1007  * @registry: a #GstRegistry.
1008  * @name: a plugin name.
1009  *
1010  * Retrieves a #GList of features of the plugin with name @name.
1011  *
1012  * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
1013  * after usage.
1014  */
1015 GList *
1016 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
1017     const gchar * name)
1018 {
1019   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1020   g_return_val_if_fail (name != NULL, NULL);
1021
1022   return gst_registry_feature_filter (registry,
1023       _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1024 }
1025
1026 /* Unref and delete the default registry */
1027 void
1028 _priv_gst_registry_cleanup ()
1029 {
1030   GstRegistry *registry;
1031
1032   g_static_mutex_lock (&_gst_registry_mutex);
1033   if ((registry = _gst_registry_default) != NULL) {
1034     _gst_registry_default = NULL;
1035   }
1036   g_static_mutex_unlock (&_gst_registry_mutex);
1037
1038   /* unref outside of the lock because we can. */
1039   if (registry)
1040     gst_object_unref (registry);
1041 }
1042
1043 /**
1044  * gst_default_registry_check_feature_version:
1045  * @feature_name: the name of the feature (e.g. "oggdemux")
1046  * @min_major: the minimum major version number
1047  * @min_minor: the minimum minor version number
1048  * @min_micro: the minimum micro version number
1049  *
1050  * Checks whether a plugin feature by the given name exists in the
1051  * default registry and whether its version is at least the
1052  * version required.
1053  *
1054  * Returns: #TRUE if the feature could be found and the version is
1055  * the same as the required version or newer, and #FALSE otherwise.
1056  */
1057 gboolean
1058 gst_default_registry_check_feature_version (const gchar * feature_name,
1059     guint min_major, guint min_minor, guint min_micro)
1060 {
1061   GstPluginFeature *feature;
1062   GstRegistry *registry;
1063   gboolean ret = FALSE;
1064
1065   g_return_val_if_fail (feature_name != NULL, FALSE);
1066
1067   GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1068
1069   registry = gst_registry_get_default ();
1070   feature = gst_registry_lookup_feature (registry, feature_name);
1071   if (feature) {
1072     ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1073         min_micro);
1074     gst_object_unref (feature);
1075   } else {
1076     GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1077   }
1078
1079   return ret;
1080 }