]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - gst/gstregistry.c
gst/gstregistry.c: Don't recurse into .debug directories as some distros install...
[glsdk/gstreamer0-10.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  */
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  * All registries build the #GstRegistryPool.
31  *
32  * <emphasis role="bold">Design:</emphasis>
33  *
34  * The #GstRegistry object is a list of plugins and some functions for dealing
35  * with them. #GstPlugins are matched 1-1 with a file on disk, and may or may
36  * not be loaded at a given time. There may be multiple #GstRegistry objects,
37  * but the "default registry" is the only object that has any meaning to the
38  * core.
39  *
40  * The registry.xml file is actually a cache of plugin information. This is
41  * unlike versions prior to 0.10, where the registry file was the primary source
42  * of plugin information, and was created by the gst-register command.
43  *
44  * The primary source, at all times, of plugin information is each plugin file
45  * itself. Thus, if an application wants information about a particular plugin,
46  * or wants to search for a feature that satisfies given criteria, the primary
47  * means of doing so is to load every plugin and look at the resulting
48  * information that is gathered in the default registry. Clearly, this is a time
49  * consuming process, so we cache information in the registry.xml file.
50  *
51  * On startup, plugins are searched for in the plugin search path. This path can
52  * be set directly using the %GST_PLUGIN_PATH environment variable. The registry
53  * file is loaded from ~/.gstreamer-$GST_MAJORMINOR/registry-$ARCH.xml or the
54  * file listed in the %GST_REGISTRY env var. The only reason to change the
55  * registry location is for testing.
56  *
57  * For each plugin that is found in the plugin search path, there could be 3
58  * possibilities for cached information:
59  * <itemizedlist>
60  *   <listitem>
61  *     <para>the cache may not contain information about a given file.</para>
62  *   </listitem>
63  *   <listitem>
64  *     <para>the cache may have stale information.</para>
65  *   </listitem>
66  *   <listitem>
67  *     <para>the cache may have current information.</para>
68  *   </listitem>
69  * </itemizedlist>
70  *
71  * In the first two cases, the plugin is loaded and the cache updated. In
72  * addition to these cases, the cache may have entries for plugins that are not
73  * relevant to the current process. These are marked as not available to the
74  * current process. If the cache is updated for whatever reason, it is marked
75  * dirty.
76  *
77  * A dirty cache is written out at the end of initialization. Each entry is
78  * checked to make sure the information is minimally valid. If not, the entry is
79  * simply dropped.
80  *
81  * <emphasis role="bold">Implementation notes:</emphasis>
82  *
83  * The "cache" and "default registry" are different concepts and can represent
84  * different sets of plugins. For various reasons, at init time, the cache is
85  * stored in the default registry, and plugins not relevant to the current
86  * process are marked with the %GST_PLUGIN_FLAG_CACHED bit. These plugins are
87  * removed at the end of intitialization.
88  */
90 #ifdef HAVE_CONFIG_H
91 #include "config.h"
92 #endif
93 #include "gst_private.h"
94 #include <glib.h>
95 #include <sys/types.h>
96 #include <sys/stat.h>
97 #ifdef HAVE_UNISTD_H
98 #include <unistd.h>
99 #endif
100 #include <errno.h>
101 #include <stdio.h>
102 #include <string.h>
105 #include "gstinfo.h"
106 #include "gstregistry.h"
107 #include "gstmarshal.h"
108 #include "gstfilter.h"
110 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
112 /* the one instance of the default registry and the mutex protecting the
113  * variable. */
114 static GStaticMutex _gst_registry_mutex = G_STATIC_MUTEX_INIT;
115 static GstRegistry *_gst_registry_default = NULL;
117 /* Element signals and args */
118 enum
120   PLUGIN_ADDED,
121   FEATURE_ADDED,
122   LAST_SIGNAL
123 };
125 static void gst_registry_finalize (GObject * object);
127 static guint gst_registry_signals[LAST_SIGNAL] = { 0 };
129 static GstPluginFeature *gst_registry_lookup_feature_locked (GstRegistry *
130     registry, const char *name);
131 static GstPlugin *gst_registry_lookup_locked (GstRegistry * registry,
132     const char *filename);
134 G_DEFINE_TYPE (GstRegistry, gst_registry, GST_TYPE_OBJECT);
135 static GstObjectClass *parent_class = NULL;
137 static void
138 gst_registry_class_init (GstRegistryClass * klass)
140   GObjectClass *gobject_class;
142   gobject_class = (GObjectClass *) klass;
144   parent_class = g_type_class_peek_parent (klass);
146   /**
147    * GstRegistry::plugin-added:
148    * @registry: the registry that emitted the signal
149    * @plugin: the plugin that has been added
150    *
151    * Signals that a plugin has been added to the registry (possibly
152    * replacing a previously-added one by the same name)
153    */
154   gst_registry_signals[PLUGIN_ADDED] =
155       g_signal_new ("plugin-added", G_TYPE_FROM_CLASS (klass),
156       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, plugin_added), NULL,
157       NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
159   /**
160    * GstRegistry::feature-added:
161    * @registry: the registry that emitted the signal
162    * @feature: the feature that has been added
163    *
164    * Signals that a feature has been added to the registry (possibly
165    * replacing a previously-added one by the same name)
166    */
167   gst_registry_signals[FEATURE_ADDED] =
168       g_signal_new ("feature-added", G_TYPE_FROM_CLASS (klass),
169       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, feature_added),
170       NULL, NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
172   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_registry_finalize);
175 static void
176 gst_registry_init (GstRegistry * registry)
178   registry->feature_hash = g_hash_table_new (g_str_hash, g_str_equal);
181 static void
182 gst_registry_finalize (GObject * object)
184   GstRegistry *registry = GST_REGISTRY (object);
185   GList *plugins, *p;
186   GList *features, *f;
188   plugins = registry->plugins;
189   registry->plugins = NULL;
191   GST_DEBUG_OBJECT (registry, "registry finalize");
192   p = plugins;
193   while (p) {
194     GstPlugin *plugin = p->data;
196     if (plugin) {
197       GST_LOG_OBJECT (registry, "removing plugin %s",
198           gst_plugin_get_name (plugin));
199       gst_object_unref (plugin);
200     }
201     p = g_list_next (p);
202   }
203   g_list_free (plugins);
205   features = registry->features;
206   registry->features = NULL;
208   f = features;
209   while (f) {
210     GstPluginFeature *feature = f->data;
212     if (feature) {
213       GST_LOG_OBJECT (registry, "removing feature %p (%s)",
214           feature, gst_plugin_feature_get_name (feature));
215       gst_object_unref (feature);
216     }
217     f = g_list_next (f);
218   }
219   g_list_free (features);
221   g_hash_table_destroy (registry->feature_hash);
222   registry->feature_hash = NULL;
224   G_OBJECT_CLASS (parent_class)->finalize (object);
227 /**
228  * gst_registry_get_default:
229  *
230  * Retrieves the default registry. The caller does not own a reference on the
231  * registry, as it is alive as long as GStreamer is initialized.
232  *
233  * Returns: The default #GstRegistry.
234  */
235 GstRegistry *
236 gst_registry_get_default (void)
238   GstRegistry *registry;
240   g_static_mutex_lock (&_gst_registry_mutex);
241   if (G_UNLIKELY (!_gst_registry_default)) {
242     _gst_registry_default = g_object_new (GST_TYPE_REGISTRY, NULL);
243     gst_object_ref (GST_OBJECT_CAST (_gst_registry_default));
244     gst_object_sink (GST_OBJECT_CAST (_gst_registry_default));
245   }
246   registry = _gst_registry_default;
247   g_static_mutex_unlock (&_gst_registry_mutex);
249   return registry;
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)
264   g_return_if_fail (GST_IS_REGISTRY (registry));
265   g_return_if_fail (path != NULL);
267   if (strlen (path) == 0)
268     goto empty_path;
270   GST_OBJECT_LOCK (registry);
271   if (g_list_find_custom (registry->paths, path, (GCompareFunc) strcmp))
272     goto was_added;
274   GST_INFO ("Adding plugin path: \"%s\"", path);
275   registry->paths = g_list_append (registry->paths, g_strdup (path));
276   GST_OBJECT_UNLOCK (registry);
278   return;
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   }
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)
306   GList *list;
308   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
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);
316   return list;
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)
335   GstPlugin *existing_plugin;
337   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
338   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
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   }
350   GST_DEBUG_OBJECT (registry, "adding plugin %p for filename \"%s\"",
351       plugin, GST_STR_NULL (plugin->filename));
353   registry->plugins = g_list_prepend (registry->plugins, plugin);
355   gst_object_ref (plugin);
356   gst_object_sink (plugin);
357   GST_OBJECT_UNLOCK (registry);
359   GST_LOG_OBJECT (registry, "emitting plugin-added for filename \"%s\"",
360       GST_STR_NULL (plugin->filename));
361   g_signal_emit (G_OBJECT (registry), gst_registry_signals[PLUGIN_ADDED], 0,
362       plugin);
364   return TRUE;
367 static void
368 gst_registry_remove_features_for_plugin_unlocked (GstRegistry * registry,
369     GstPlugin * plugin)
371   GList *f;
373   g_return_if_fail (GST_IS_REGISTRY (registry));
374   g_return_if_fail (GST_IS_PLUGIN (plugin));
376   /* Remove all features for this plugin */
377   f = registry->features;
378   while (f != NULL) {
379     GList *next = g_list_next (f);
380     GstPluginFeature *feature = f->data;
382     if (feature && !strcmp (feature->plugin_name, gst_plugin_get_name (plugin))) {
383       GST_DEBUG_OBJECT (registry, "removing feature %p (%s) for plugin %s",
384           feature, gst_plugin_feature_get_name (feature),
385           gst_plugin_get_name (plugin));
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   }
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)
407   g_return_if_fail (GST_IS_REGISTRY (registry));
408   g_return_if_fail (GST_IS_PLUGIN (plugin));
410   GST_DEBUG_OBJECT (registry, "removing plugin %p (%s)",
411       plugin, gst_plugin_get_name (plugin));
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);
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)
435   GstPluginFeature *existing_feature;
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);
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   }
453   GST_DEBUG_OBJECT (registry, "adding feature %p (%s)", feature, feature->name);
455   registry->features = g_list_prepend (registry->features, feature);
456   g_hash_table_replace (registry->feature_hash, feature->name, feature);
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   }
463   gst_object_ref (feature);
464   gst_object_sink (feature);
465   GST_OBJECT_UNLOCK (registry);
467   GST_LOG_OBJECT (registry, "emitting feature-added for %s", feature->name);
468   g_signal_emit (G_OBJECT (registry), gst_registry_signals[FEATURE_ADDED], 0,
469       feature);
471   return TRUE;
474 /**
475  * gst_registry_remove_feature:
476  * @registry: the registry to remove the feature from
477  * @feature: the feature to remove
478  *
479  * Remove the feature from the registry.
480  *
481  * MT safe.
482  */
483 void
484 gst_registry_remove_feature (GstRegistry * registry, GstPluginFeature * feature)
486   g_return_if_fail (GST_IS_REGISTRY (registry));
487   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
489   GST_DEBUG_OBJECT (registry, "removing feature %p (%s)",
490       feature, gst_plugin_feature_get_name (feature));
492   GST_OBJECT_LOCK (registry);
493   registry->features = g_list_remove (registry->features, feature);
494   g_hash_table_remove (registry->feature_hash, feature->name);
495   GST_OBJECT_UNLOCK (registry);
496   gst_object_unref (feature);
499 /**
500  * gst_registry_plugin_filter:
501  * @registry: registry to query
502  * @filter: the filter to use
503  * @first: only return first match
504  * @user_data: user data passed to the filter function
505  *
506  * Runs a filter against all plugins in the registry and returns a #GList with
507  * the results. If the first flag is set, only the first match is
508  * returned (as a list with a single object).
509  * Every plugin is reffed; use gst_plugin_list_free() after use, which
510  * will unref again.
511  *
512  * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
513  *
514  * MT safe.
515  */
516 GList *
517 gst_registry_plugin_filter (GstRegistry * registry,
518     GstPluginFilter filter, gboolean first, gpointer user_data)
520   GList *list;
521   GList *g;
523   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
525   GST_OBJECT_LOCK (registry);
526   list = gst_filter_run (registry->plugins, (GstFilterFunc) filter, first,
527       user_data);
528   for (g = list; g; g = g->next) {
529     gst_object_ref (GST_PLUGIN_CAST (g->data));
530   }
531   GST_OBJECT_UNLOCK (registry);
533   return list;
536 /**
537  * gst_registry_feature_filter:
538  * @registry: registry to query
539  * @filter: the filter to use
540  * @first: only return first match
541  * @user_data: user data passed to the filter function
542  *
543  * Runs a filter against all features of the plugins in the registry
544  * and returns a GList with the results.
545  * If the first flag is set, only the first match is
546  * returned (as a list with a single object).
547  *
548  * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
549  * after usage.
550  *
551  * MT safe.
552  */
553 GList *
554 gst_registry_feature_filter (GstRegistry * registry,
555     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
557   GList *list;
558   GList *g;
560   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
562   GST_OBJECT_LOCK (registry);
563   list = gst_filter_run (registry->features, (GstFilterFunc) filter, first,
564       user_data);
565   for (g = list; g; g = g->next) {
566     gst_object_ref (GST_PLUGIN_FEATURE_CAST (g->data));
567   }
568   GST_OBJECT_UNLOCK (registry);
570   return list;
573 /**
574  * gst_registry_find_plugin:
575  * @registry: the registry to search
576  * @name: the plugin name to find
577  *
578  * Find the plugin with the given name in the registry.
579  * The plugin will be reffed; caller is responsible for unreffing.
580  *
581  * Returns: The plugin with the given name or NULL if the plugin was not found.
582  * gst_object_unref() after usage.
583  *
584  * MT safe.
585  */
586 GstPlugin *
587 gst_registry_find_plugin (GstRegistry * registry, const gchar * name)
589   GList *walk;
590   GstPlugin *result = NULL;
592   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
593   g_return_val_if_fail (name != NULL, NULL);
595   walk = gst_registry_plugin_filter (registry,
596       (GstPluginFilter) gst_plugin_name_filter, TRUE, (gpointer) name);
597   if (walk) {
598     result = GST_PLUGIN_CAST (walk->data);
600     gst_object_ref (result);
601     gst_plugin_list_free (walk);
602   }
604   return result;
607 /**
608  * gst_registry_find_feature:
609  * @registry: the registry to search
610  * @name: the pluginfeature name to find
611  * @type: the pluginfeature type to find
612  *
613  * Find the pluginfeature with the given name and type in the registry.
614  *
615  * Returns: The pluginfeature with the given name and type or NULL
616  * if the plugin was not found. gst_object_unref() after usage.
617  *
618  * MT safe.
619  */
620 GstPluginFeature *
621 gst_registry_find_feature (GstRegistry * registry, const gchar * name,
622     GType type)
624   GstPluginFeature *feature = NULL;
625   GList *walk;
626   GstTypeNameData data;
628   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
629   g_return_val_if_fail (name != NULL, NULL);
630   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
632   data.name = name;
633   data.type = type;
635   walk = gst_registry_feature_filter (registry,
636       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
637       TRUE, &data);
639   if (walk) {
640     feature = GST_PLUGIN_FEATURE_CAST (walk->data);
642     gst_object_ref (feature);
643     gst_plugin_feature_list_free (walk);
644   }
646   return feature;
649 /**
650  * gst_registry_get_feature_list:
651  * @registry: a #GstRegistry
652  * @type: a #GType.
653  *
654  * Retrieves a #GList of #GstPluginFeature of @type.
655  *
656  * Returns: a #GList of #GstPluginFeature of @type. Use
657  * gst_plugin_feature_list_free() after usage.
658  *
659  * MT safe.
660  */
661 GList *
662 gst_registry_get_feature_list (GstRegistry * registry, GType type)
664   GstTypeNameData data;
666   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
667   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
669   data.type = type;
670   data.name = NULL;
672   return gst_registry_feature_filter (registry,
673       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
674       FALSE, &data);
677 /**
678  * gst_registry_get_plugin_list:
679  * @registry: the registry to search
680  *
681  * Get a copy of all plugins registered in the given registry. The refcount
682  * of each element in the list in incremented.
683  *
684  * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
685  *
686  * MT safe.
687  */
688 GList *
689 gst_registry_get_plugin_list (GstRegistry * registry)
691   GList *list;
692   GList *g;
694   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
696   GST_OBJECT_LOCK (registry);
697   list = g_list_copy (registry->plugins);
698   for (g = list; g; g = g->next) {
699     gst_object_ref (GST_PLUGIN_CAST (g->data));
700   }
701   GST_OBJECT_UNLOCK (registry);
703   return list;
706 static GstPluginFeature *
707 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
709   if (G_UNLIKELY (name == NULL))
710     return NULL;
712   return g_hash_table_lookup (registry->feature_hash, name);
715 /**
716  * gst_registry_lookup_feature:
717  * @registry: a #GstRegistry
718  * @name: a #GstPluginFeature name
719  *
720  * Find a #GstPluginFeature with @name in @registry.
721  *
722  * Returns: a #GstPluginFeature with its refcount incremented, use
723  * gst_object_unref() after usage.
724  *
725  * MT safe.
726  */
727 GstPluginFeature *
728 gst_registry_lookup_feature (GstRegistry * registry, const char *name)
730   GstPluginFeature *feature;
732   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
733   g_return_val_if_fail (name != NULL, NULL);
735   GST_OBJECT_LOCK (registry);
736   feature = gst_registry_lookup_feature_locked (registry, name);
737   if (feature)
738     gst_object_ref (feature);
739   GST_OBJECT_UNLOCK (registry);
741   return feature;
744 static GstPlugin *
745 gst_registry_lookup_locked (GstRegistry * registry, const char *filename)
747   GList *g;
748   GstPlugin *plugin;
749   gchar *basename;
751   if (G_UNLIKELY (filename == NULL))
752     return NULL;
754   basename = g_path_get_basename (filename);
755   /* FIXME: use GTree speed up lookups */
756   for (g = registry->plugins; g; g = g_list_next (g)) {
757     plugin = GST_PLUGIN_CAST (g->data);
758     if (plugin->basename && strcmp (basename, plugin->basename) == 0) {
759       g_free (basename);
760       return plugin;
761     }
762   }
764   g_free (basename);
765   return NULL;
768 /**
769  * gst_registry_lookup:
770  * @registry: the registry to look up in
771  * @filename: the name of the file to look up
772  *
773  * Look up a plugin in the given registry with the given filename.
774  * If found, plugin is reffed.
775  *
776  * Returns: the #GstPlugin if found, or NULL if not. gst_object_unref()
777  * after usage.
778  */
779 GstPlugin *
780 gst_registry_lookup (GstRegistry * registry, const char *filename)
782   GstPlugin *plugin;
784   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
785   g_return_val_if_fail (filename != NULL, NULL);
787   GST_OBJECT_LOCK (registry);
788   plugin = gst_registry_lookup_locked (registry, filename);
789   if (plugin)
790     gst_object_ref (plugin);
791   GST_OBJECT_UNLOCK (registry);
793   return plugin;
796 static gboolean
797 gst_registry_scan_path_level (GstRegistry * registry, const gchar * path,
798     int level)
800   GDir *dir;
801   const gchar *dirent;
802   gchar *filename;
803   GstPlugin *plugin;
804   GstPlugin *newplugin;
805   gboolean changed = FALSE;
807   dir = g_dir_open (path, 0, NULL);
808   if (!dir)
809     return FALSE;
811   while ((dirent = g_dir_read_name (dir))) {
812     filename = g_strjoin ("/", path, dirent, NULL);
814     if (g_file_test (filename, G_FILE_TEST_IS_DIR)) {
815       /* skip the .debug directory, these contain elf files that are not
816        * useful or worse, can crash dlopen () */
817       if (g_str_equal (dirent, ".debug")) {
818         GST_LOG_OBJECT (registry, "found .debug directory, ignoring");
819         g_free (filename);
820         continue;
821       }
822       /* FIXME 0.11: Don't recurse into directories, this behaviour
823        * is inconsistent with other PATH environment variables
824        */
825       if (level > 0) {
826         GST_LOG_OBJECT (registry, "recursing into directory %s", filename);
827         changed |= gst_registry_scan_path_level (registry, filename, level - 1);
828       } else {
829         GST_LOG_OBJECT (registry, "not recursing into directory %s, "
830             "recursion level too deep", filename);
831       }
832       g_free (filename);
833       continue;
834     }
835     if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
836       GST_LOG_OBJECT (registry, "%s is not a regular file, ignoring", filename);
837       g_free (filename);
838       continue;
839     }
840     if (!g_str_has_suffix (dirent, G_MODULE_SUFFIX)
841 #ifdef GST_EXTRA_MODULE_SUFFIX
842         && !g_str_has_suffix (dirent, GST_EXTRA_MODULE_SUFFIX)
843 #endif
844         ) {
845       GST_LOG_OBJECT (registry, "extension is not recognized as module file, "
846           "ignoring file %s", filename);
847       g_free (filename);
848       continue;
849     }
851     GST_LOG_OBJECT (registry, "file %s looks like a possible module", filename);
853     /* plug-ins are considered unique by basename; if the given name
854      * was already seen by the registry, we ignore it */
855     plugin = gst_registry_lookup (registry, filename);
856     if (plugin) {
857       struct stat file_status;
859       if (stat (filename, &file_status)) {
860         /* Plugin will be removed from cache after the scan completes if it
861          * is still marked 'cached' */
862         g_free (filename);
863         gst_object_unref (plugin);
864         continue;
865       }
866       if (plugin->registered) {
867         GST_DEBUG_OBJECT (registry,
868             "plugin already registered from path \"%s\"",
869             GST_STR_NULL (plugin->filename));
870         g_free (filename);
871         gst_object_unref (plugin);
872         continue;
873       }
874       if (plugin->file_mtime == file_status.st_mtime &&
875           plugin->file_size == file_status.st_size) {
876         GST_LOG_OBJECT (registry, "file %s cached", filename);
877         plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
878         GST_LOG_OBJECT (registry, "marking plugin %p as registered as %s",
879             plugin, filename);
880         plugin->registered = TRUE;
881         /* Update the file path on which we've seen this cached plugin
882          * to ensure the registry cache will reflect up to date information */
883         if (strcmp (plugin->filename, filename) != 0) {
884           g_free (plugin->filename);
885           plugin->filename = g_strdup (filename);
886           changed = TRUE;
887         }
888       } else {
889         GST_INFO_OBJECT (registry, "cached info for %s is stale", filename);
890         GST_DEBUG_OBJECT (registry, "mtime %ld != %ld or size %"
891             G_GINT64_FORMAT " != %"
892             G_GINT64_FORMAT, plugin->file_mtime, file_status.st_mtime,
893             (gint64) plugin->file_size, (gint64) file_status.st_size);
894         gst_registry_remove_plugin (gst_registry_get_default (), plugin);
895         /* We don't use a GError here because a failure to load some shared 
896          * objects as plugins is normal (particularly in the uninstalled case)
897          */
898         newplugin = gst_plugin_load_file (filename, NULL);
899         if (newplugin) {
900           GST_DEBUG_OBJECT (registry, "marking new plugin %p as registered",
901               newplugin);
902           newplugin->registered = TRUE;
903           gst_object_unref (newplugin);
904         }
905         changed = TRUE;
906       }
907       gst_object_unref (plugin);
909     } else {
910       GST_DEBUG_OBJECT (registry, "file %s not yet in registry", filename);
911       newplugin = gst_plugin_load_file (filename, NULL);
912       if (newplugin) {
913         newplugin->registered = TRUE;
914         gst_object_unref (newplugin);
915         changed = TRUE;
916       }
917     }
919     g_free (filename);
920   }
922   g_dir_close (dir);
924   return changed;
927 /**
928  * gst_registry_scan_path:
929  * @registry: the registry to add the path to
930  * @path: the path to add to the registry
931  *
932  * Add the given path to the registry. The syntax of the
933  * path is specific to the registry. If the path has already been
934  * added, do nothing.
935  *
936  * Returns: %TRUE if registry changed
937  */
938 gboolean
939 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
941   gboolean changed;
943   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
944   g_return_val_if_fail (path != NULL, FALSE);
946   GST_DEBUG_OBJECT (registry, "scanning path %s", path);
947   changed = gst_registry_scan_path_level (registry, path, 10);
949   GST_DEBUG_OBJECT (registry, "registry changed in path %s: %d", path, changed);
951   return changed;
954 /* Unref all plugins marked 'cached', to clear old plugins that no
955  * longer exist. Returns TRUE if any plugins were removed */
956 gboolean
957 _priv_gst_registry_remove_cache_plugins (GstRegistry * registry)
959   GList *g;
960   GList *g_next;
961   GstPlugin *plugin;
962   gboolean changed = FALSE;
964   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
966   GST_OBJECT_LOCK (registry);
968   GST_DEBUG_OBJECT (registry, "removing cached plugins");
969   g = registry->plugins;
970   while (g) {
971     g_next = g->next;
972     plugin = g->data;
973     if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
974       GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
975           GST_STR_NULL (plugin->filename));
976       registry->plugins = g_list_delete_link (registry->plugins, g);
977       gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
978       gst_object_unref (plugin);
979       changed = TRUE;
980     }
981     g = g_next;
982   }
984   GST_OBJECT_UNLOCK (registry);
986   return changed;
990 static gboolean
991 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
992     gpointer user_data)
994   return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
997 /**
998  * gst_registry_get_feature_list_by_plugin:
999  * @registry: a #GstRegistry.
1000  * @name: a plugin name.
1001  *
1002  * Retrieves a #GList of features of the plugin with name @name.
1003  *
1004  * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
1005  * after usage.
1006  */
1007 GList *
1008 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
1009     const gchar * name)
1011   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1012   g_return_val_if_fail (name != NULL, NULL);
1014   return gst_registry_feature_filter (registry,
1015       _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1018 /* Unref and delete the default registry */
1019 void
1020 _priv_gst_registry_cleanup ()
1022   GstRegistry *registry;
1024   g_static_mutex_lock (&_gst_registry_mutex);
1025   if ((registry = _gst_registry_default) != NULL) {
1026     _gst_registry_default = NULL;
1027   }
1028   g_static_mutex_unlock (&_gst_registry_mutex);
1030   /* unref outside of the lock because we can. */
1031   if (registry)
1032     gst_object_unref (registry);
1035 /**
1036  * gst_default_registry_check_feature_version:
1037  * @feature_name: the name of the feature (e.g. "oggdemux")
1038  * @min_major: the minimum major version number
1039  * @min_minor: the minimum minor version number
1040  * @min_micro: the minimum micro version number
1041  *
1042  * Checks whether a plugin feature by the given name exists in the
1043  * default registry and whether its version is at least the
1044  * version required.
1045  *
1046  * Returns: #TRUE if the feature could be found and the version is
1047  * the same as the required version or newer, and #FALSE otherwise.
1048  */
1049 gboolean
1050 gst_default_registry_check_feature_version (const gchar * feature_name,
1051     guint min_major, guint min_minor, guint min_micro)
1053   GstPluginFeature *feature;
1054   GstRegistry *registry;
1055   gboolean ret = FALSE;
1057   g_return_val_if_fail (feature_name != NULL, FALSE);
1059   GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1061   registry = gst_registry_get_default ();
1062   feature = gst_registry_lookup_feature (registry, feature_name);
1063   if (feature) {
1064     ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1065         min_micro);
1066     gst_object_unref (feature);
1067   } else {
1068     GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1069   }
1071   return ret;