]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - gst/gstregistry.c
gst/: Re-commit the registry changes, along with an extra fix:
[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_class_init (GstRegistryClass * klass);
126 static void gst_registry_init (GstRegistry * registry);
127 static void gst_registry_finalize (GObject * object);
129 static guint gst_registry_signals[LAST_SIGNAL] = { 0 };
131 static GstPluginFeature *gst_registry_lookup_feature_locked (GstRegistry *
132     registry, const char *name);
133 static GstPlugin *gst_registry_lookup_locked (GstRegistry * registry,
134     const char *filename);
136 G_DEFINE_TYPE (GstRegistry, gst_registry, GST_TYPE_OBJECT);
137 static GstObjectClass *parent_class = NULL;
139 static void
140 gst_registry_class_init (GstRegistryClass * klass)
142   GObjectClass *gobject_class;
144   gobject_class = (GObjectClass *) klass;
146   parent_class = g_type_class_peek_parent (klass);
148   gst_registry_signals[PLUGIN_ADDED] =
149       g_signal_new ("plugin-added", G_TYPE_FROM_CLASS (klass),
150       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, plugin_added), NULL,
151       NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
152   gst_registry_signals[FEATURE_ADDED] =
153       g_signal_new ("feature-added", G_TYPE_FROM_CLASS (klass),
154       G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, feature_added),
155       NULL, NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
157   gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_registry_finalize);
160 static void
161 gst_registry_init (GstRegistry * registry)
165 static void
166 gst_registry_finalize (GObject * object)
168   GstRegistry *registry = GST_REGISTRY (object);
169   GList *plugins, *p;
170   GList *features, *f;
172   plugins = registry->plugins;
173   registry->plugins = NULL;
175   GST_DEBUG_OBJECT (registry, "registry finalize");
176   p = plugins;
177   while (p) {
178     GstPlugin *plugin = p->data;
180     if (plugin) {
181       GST_DEBUG_OBJECT (registry, "removing plugin %s",
182           gst_plugin_get_name (plugin));
183       gst_object_unref (plugin);
184     }
185     p = g_list_next (p);
186   }
187   g_list_free (plugins);
189   features = registry->features;
190   registry->features = NULL;
192   f = features;
193   while (f) {
194     GstPluginFeature *feature = f->data;
196     if (feature) {
197       GST_DEBUG_OBJECT (registry, "removing feature %p (%s)",
198           feature, gst_plugin_feature_get_name (feature));
199       gst_object_unref (feature);
200     }
201     f = g_list_next (f);
202   }
203   g_list_free (features);
206   G_OBJECT_CLASS (parent_class)->finalize (object);
209 /**
210  * gst_registry_get_default:
211  *
212  * Retrieves the default registry. The caller does not own a reference on the
213  * registry, as it is alive as long as GStreamer is initialized.
214  *
215  * Returns: The default #GstRegistry.
216  */
217 GstRegistry *
218 gst_registry_get_default (void)
220   GstRegistry *registry;
222   g_static_mutex_lock (&_gst_registry_mutex);
223   if (G_UNLIKELY (!_gst_registry_default)) {
224     _gst_registry_default = g_object_new (GST_TYPE_REGISTRY, NULL);
225     gst_object_ref (GST_OBJECT_CAST (_gst_registry_default));
226     gst_object_sink (GST_OBJECT_CAST (_gst_registry_default));
227   }
228   registry = _gst_registry_default;
229   g_static_mutex_unlock (&_gst_registry_mutex);
231   return registry;
234 /**
235  * gst_registry_add_path:
236  * @registry: the registry to add the path to
237  * @path: the path to add to the registry
238  *
239  * Add the given path to the registry. The syntax of the
240  * path is specific to the registry. If the path has already been
241  * added, do nothing.
242  */
243 void
244 gst_registry_add_path (GstRegistry * registry, const gchar * path)
246   g_return_if_fail (GST_IS_REGISTRY (registry));
247   g_return_if_fail (path != NULL);
249   if (strlen (path) == 0)
250     goto empty_path;
252   GST_OBJECT_LOCK (registry);
253   if (g_list_find_custom (registry->paths, path, (GCompareFunc) strcmp))
254     goto was_added;
256   GST_INFO ("Adding plugin path: \"%s\"", path);
257   registry->paths = g_list_append (registry->paths, g_strdup (path));
258   GST_OBJECT_UNLOCK (registry);
260   return;
262 empty_path:
263   {
264     GST_INFO ("Ignoring empty plugin path");
265     return;
266   }
267 was_added:
268   {
269     g_warning ("path %s already added to registry", path);
270     GST_OBJECT_UNLOCK (registry);
271     return;
272   }
275 /**
276  * gst_registry_get_path_list:
277  * @registry: the registry to get the pathlist of
278  *
279  * Get the list of paths for the given registry.
280  *
281  * Returns: A Glist of paths as strings. g_list_free after use.
282  *
283  * MT safe.
284  */
285 GList *
286 gst_registry_get_path_list (GstRegistry * registry)
288   GList *list;
290   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
292   GST_OBJECT_LOCK (registry);
293   /* We don't need to copy the strings, because they won't be deleted
294    * as long as the GstRegistry is around */
295   list = g_list_copy (registry->paths);
296   GST_OBJECT_UNLOCK (registry);
298   return list;
302 /**
303  * gst_registry_add_plugin:
304  * @registry: the registry to add the plugin to
305  * @plugin: the plugin to add
306  *
307  * Add the plugin to the registry. The plugin-added signal will be emitted.
308  * This function will sink @plugin.
309  *
310  * Returns: TRUE on success.
311  *
312  * MT safe.
313  */
314 gboolean
315 gst_registry_add_plugin (GstRegistry * registry, GstPlugin * plugin)
317   GstPlugin *existing_plugin;
319   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
320   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
322   GST_OBJECT_LOCK (registry);
323   existing_plugin = gst_registry_lookup_locked (registry, plugin->filename);
324   if (G_UNLIKELY (existing_plugin)) {
325     GST_DEBUG_OBJECT (registry,
326         "Replacing existing plugin %p with new plugin %p for filename \"%s\"",
327         existing_plugin, plugin, GST_STR_NULL (plugin->filename));
328     registry->plugins = g_list_remove (registry->plugins, existing_plugin);
329     gst_object_unref (existing_plugin);
330   }
332   GST_DEBUG_OBJECT (registry, "adding plugin %p for filename \"%s\"",
333       plugin, GST_STR_NULL (plugin->filename));
335   registry->plugins = g_list_prepend (registry->plugins, plugin);
337   gst_object_ref (plugin);
338   gst_object_sink (plugin);
339   GST_OBJECT_UNLOCK (registry);
341   GST_DEBUG_OBJECT (registry, "emitting plugin-added for filename \"%s\"",
342       GST_STR_NULL (plugin->filename));
343   g_signal_emit (G_OBJECT (registry), gst_registry_signals[PLUGIN_ADDED], 0,
344       plugin);
346   return TRUE;
349 static void
350 gst_registry_remove_features_for_plugin_unlocked (GstRegistry * registry,
351     GstPlugin * plugin)
353   GList *f;
355   g_return_if_fail (GST_IS_REGISTRY (registry));
356   g_return_if_fail (GST_IS_PLUGIN (plugin));
358   /* Remove all features for this plugin */
359   f = registry->features;
360   while (f != NULL) {
361     GList *next = g_list_next (f);
362     GstPluginFeature *feature = f->data;
364     if (feature && !strcmp (feature->plugin_name, gst_plugin_get_name (plugin))) {
365       GST_DEBUG_OBJECT (registry, "removing feature %p (%s) for plugin %s",
366           feature, gst_plugin_feature_get_name (feature),
367           gst_plugin_get_name (plugin));
369       registry->features = g_list_delete_link (registry->features, f);
370       gst_object_unref (feature);
371     }
372     f = next;
373   }
376 /**
377  * gst_registry_remove_plugin:
378  * @registry: the registry to remove the plugin from
379  * @plugin: the plugin to remove
380  *
381  * Remove the plugin from the registry.
382  *
383  * MT safe.
384  */
385 void
386 gst_registry_remove_plugin (GstRegistry * registry, GstPlugin * plugin)
388   g_return_if_fail (GST_IS_REGISTRY (registry));
389   g_return_if_fail (GST_IS_PLUGIN (plugin));
391   GST_DEBUG_OBJECT (registry, "removing plugin %p (%s)",
392       plugin, gst_plugin_get_name (plugin));
394   GST_OBJECT_LOCK (registry);
395   registry->plugins = g_list_remove (registry->plugins, plugin);
396   gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
397   GST_OBJECT_UNLOCK (registry);
398   gst_object_unref (plugin);
401 /**
402  * gst_registry_add_feature:
403  * @registry: the registry to add the plugin to
404  * @feature: the feature to add
405  *
406  * Add the feature to the registry. The feature-added signal will be emitted.
407  * This function sinks @feature.
408  *
409  * Returns: TRUE on success.
410  *
411  * MT safe.
412  */
413 gboolean
414 gst_registry_add_feature (GstRegistry * registry, GstPluginFeature * feature)
416   GstPluginFeature *existing_feature;
418   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
419   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
420   g_return_val_if_fail (feature->name != NULL, FALSE);
421   g_return_val_if_fail (feature->plugin_name != NULL, FALSE);
423   GST_OBJECT_LOCK (registry);
424   existing_feature = gst_registry_lookup_feature_locked (registry,
425       feature->name);
426   if (G_UNLIKELY (existing_feature)) {
427     GST_DEBUG_OBJECT (registry, "Replacing existing feature %p (%s)",
428         existing_feature, feature->name);
429     registry->features = g_list_remove (registry->features, existing_feature);
430     gst_object_unref (existing_feature);
431   }
433   GST_DEBUG_OBJECT (registry, "adding feature %p (%s)", feature, feature->name);
435   registry->features = g_list_prepend (registry->features, feature);
437   gst_object_ref (feature);
438   gst_object_sink (feature);
439   GST_OBJECT_UNLOCK (registry);
441   GST_DEBUG_OBJECT (registry, "emitting feature-added for %s", feature->name);
442   g_signal_emit (G_OBJECT (registry), gst_registry_signals[FEATURE_ADDED], 0,
443       feature);
445   return TRUE;
448 /**
449  * gst_registry_remove_feature:
450  * @registry: the registry to remove the feature from
451  * @feature: the feature to remove
452  *
453  * Remove the feature from the registry.
454  *
455  * MT safe.
456  */
457 void
458 gst_registry_remove_feature (GstRegistry * registry, GstPluginFeature * feature)
460   g_return_if_fail (GST_IS_REGISTRY (registry));
461   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
463   GST_DEBUG_OBJECT (registry, "removing feature %p (%s)",
464       feature, gst_plugin_feature_get_name (feature));
466   GST_OBJECT_LOCK (registry);
467   registry->features = g_list_remove (registry->features, feature);
468   GST_OBJECT_UNLOCK (registry);
469   gst_object_unref (feature);
472 /**
473  * gst_registry_plugin_filter:
474  * @registry: registry to query
475  * @filter: the filter to use
476  * @first: only return first match
477  * @user_data: user data passed to the filter function
478  *
479  * Runs a filter against all plugins in the registry and returns a #GList with
480  * the results. If the first flag is set, only the first match is
481  * returned (as a list with a single object).
482  * Every plugin is reffed; use gst_plugin_list_free() after use, which
483  * will unref again.
484  *
485  * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
486  *
487  * MT safe.
488  */
489 GList *
490 gst_registry_plugin_filter (GstRegistry * registry,
491     GstPluginFilter filter, gboolean first, gpointer user_data)
493   GList *list;
494   GList *g;
496   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
498   GST_OBJECT_LOCK (registry);
499   list = gst_filter_run (registry->plugins, (GstFilterFunc) filter, first,
500       user_data);
501   for (g = list; g; g = g->next) {
502     gst_object_ref (GST_PLUGIN_CAST (g->data));
503   }
504   GST_OBJECT_UNLOCK (registry);
506   return list;
509 /**
510  * gst_registry_feature_filter:
511  * @registry: registry to query
512  * @filter: the filter to use
513  * @first: only return first match
514  * @user_data: user data passed to the filter function
515  *
516  * Runs a filter against all features of the plugins in the registry
517  * and returns a GList with the results.
518  * If the first flag is set, only the first match is
519  * returned (as a list with a single object).
520  *
521  * Returns: a GList of plugin features, gst_plugin_feature_list_free after use.
522  *
523  * MT safe.
524  */
525 GList *
526 gst_registry_feature_filter (GstRegistry * registry,
527     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
529   GList *list;
530   GList *g;
532   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
534   GST_OBJECT_LOCK (registry);
535   list = gst_filter_run (registry->features, (GstFilterFunc) filter, first,
536       user_data);
537   for (g = list; g; g = g->next) {
538     gst_object_ref (GST_PLUGIN_FEATURE_CAST (g->data));
539   }
540   GST_OBJECT_UNLOCK (registry);
542   return list;
545 /**
546  * gst_registry_find_plugin:
547  * @registry: the registry to search
548  * @name: the plugin name to find
549  *
550  * Find the plugin with the given name in the registry.
551  * The plugin will be reffed; caller is responsible for unreffing.
552  *
553  * Returns: The plugin with the given name or NULL if the plugin was not found.
554  * gst_object_unref() after usage.
555  *
556  * MT safe.
557  */
558 GstPlugin *
559 gst_registry_find_plugin (GstRegistry * registry, const gchar * name)
561   GList *walk;
562   GstPlugin *result = NULL;
564   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
565   g_return_val_if_fail (name != NULL, NULL);
567   walk = gst_registry_plugin_filter (registry,
568       (GstPluginFilter) gst_plugin_name_filter, TRUE, (gpointer) name);
569   if (walk) {
570     result = GST_PLUGIN_CAST (walk->data);
572     gst_object_ref (result);
573     gst_plugin_list_free (walk);
574   }
576   return result;
579 /**
580  * gst_registry_find_feature:
581  * @registry: the registry to search
582  * @name: the pluginfeature name to find
583  * @type: the pluginfeature type to find
584  *
585  * Find the pluginfeature with the given name and type in the registry.
586  *
587  * Returns: The pluginfeature with the given name and type or NULL
588  * if the plugin was not found. gst_object_unref() after usage.
589  *
590  * MT safe.
591  */
592 GstPluginFeature *
593 gst_registry_find_feature (GstRegistry * registry, const gchar * name,
594     GType type)
596   GstPluginFeature *feature = NULL;
597   GList *walk;
598   GstTypeNameData data;
600   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
601   g_return_val_if_fail (name != NULL, NULL);
602   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
604   data.name = name;
605   data.type = type;
607   walk = gst_registry_feature_filter (registry,
608       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
609       TRUE, &data);
611   if (walk) {
612     feature = GST_PLUGIN_FEATURE_CAST (walk->data);
614     gst_object_ref (feature);
615     gst_plugin_feature_list_free (walk);
616   }
618   return feature;
621 /**
622  * gst_registry_get_feature_list:
623  * @registry: a #GstRegistry
624  * @type: a #GType.
625  *
626  * Retrieves a #GList of #GstPluginFeature of @type.
627  *
628  * Returns: a #GList of #GstPluginFeature of @type. gst_plugin_feature_list_free
629  * after usage.
630  *
631  * MT safe.
632  */
633 GList *
634 gst_registry_get_feature_list (GstRegistry * registry, GType type)
636   GstTypeNameData data;
638   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
639   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
641   data.type = type;
642   data.name = NULL;
644   return gst_registry_feature_filter (registry,
645       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
646       FALSE, &data);
649 /**
650  * gst_registry_get_plugin_list:
651  * @registry: the registry to search
652  *
653  * Get a copy of all plugins registered in the given registry. The refcount
654  * of each element in the list in incremented.
655  *
656  * Returns: a #GList of #GstPlugin. gst_plugin_list_free after use.
657  *
658  * MT safe.
659  */
660 GList *
661 gst_registry_get_plugin_list (GstRegistry * registry)
663   GList *list;
664   GList *g;
666   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
668   GST_OBJECT_LOCK (registry);
669   list = g_list_copy (registry->plugins);
670   for (g = list; g; g = g->next) {
671     gst_object_ref (GST_PLUGIN_CAST (g->data));
672   }
673   GST_OBJECT_UNLOCK (registry);
675   return list;
678 static GstPluginFeature *
679 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
681   GList *g;
682   GstPluginFeature *feature;
684   if (G_UNLIKELY (name == NULL))
685     return NULL;
687   for (g = registry->features; g; g = g_list_next (g)) {
688     feature = GST_PLUGIN_FEATURE_CAST (g->data);
689     if (feature->name && strcmp (name, feature->name) == 0) {
690       return feature;
691     }
692   }
694   return NULL;
697 /**
698  * gst_registry_lookup_feature:
699  * @registry: a #GstRegistry
700  * @name: a #GstPluginFeature name
701  *
702  * Find a #GstPluginFeature with @name in @registry.
703  * 
704  * Returns: a #GstPluginFeature with its refcount incremented, use
705  * gst_object_unref() after usage.
706  *
707  * MT safe.
708  */
709 GstPluginFeature *
710 gst_registry_lookup_feature (GstRegistry * registry, const char *name)
712   GstPluginFeature *feature;
714   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
715   g_return_val_if_fail (name != NULL, NULL);
717   GST_OBJECT_LOCK (registry);
718   feature = gst_registry_lookup_feature_locked (registry, name);
719   if (feature)
720     gst_object_ref (feature);
721   GST_OBJECT_UNLOCK (registry);
723   return feature;
726 static GstPlugin *
727 gst_registry_lookup_locked (GstRegistry * registry, const char *filename)
729   GList *g;
730   GstPlugin *plugin;
731   gchar *basename;
733   if (G_UNLIKELY (filename == NULL))
734     return NULL;
736   basename = g_path_get_basename (filename);
737   for (g = registry->plugins; g; g = g_list_next (g)) {
738     plugin = GST_PLUGIN_CAST (g->data);
739     if (plugin->basename && strcmp (basename, plugin->basename) == 0) {
740       g_free (basename);
741       return plugin;
742     }
743   }
745   g_free (basename);
746   return NULL;
749 /**
750  * gst_registry_lookup:
751  * @registry: the registry to look up in
752  * @filename: the name of the file to look up
753  *
754  * Look up a plugin in the given registry with the given filename.
755  * If found, plugin is reffed.
756  *
757  * Returns: the #GstPlugin if found, or NULL if not. gst_object_unref()
758  * after usage.
759  */
760 GstPlugin *
761 gst_registry_lookup (GstRegistry * registry, const char *filename)
763   GstPlugin *plugin;
765   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
766   g_return_val_if_fail (filename != NULL, NULL);
768   GST_OBJECT_LOCK (registry);
769   plugin = gst_registry_lookup_locked (registry, filename);
770   if (plugin)
771     gst_object_ref (plugin);
772   GST_OBJECT_UNLOCK (registry);
774   return plugin;
777 static gboolean
778 gst_registry_scan_path_level (GstRegistry * registry, const gchar * path,
779     int level)
781   GDir *dir;
782   const gchar *dirent;
783   gchar *filename;
784   GstPlugin *plugin;
785   GstPlugin *newplugin;
786   gboolean changed = FALSE;
788   dir = g_dir_open (path, 0, NULL);
789   if (!dir)
790     return FALSE;
792   while ((dirent = g_dir_read_name (dir))) {
793     filename = g_strjoin ("/", path, dirent, NULL);
795     GST_LOG_OBJECT (registry, "examining file: %s", filename);
797     if (g_file_test (filename, G_FILE_TEST_IS_DIR)) {
798       if (level > 0) {
799         GST_LOG_OBJECT (registry, "found directory, recursing");
800         changed |= gst_registry_scan_path_level (registry, filename, level - 1);
801       } else {
802         GST_LOG_OBJECT (registry,
803             "found directory, but recursion level is too deep");
804       }
805       g_free (filename);
806       continue;
807     }
808     if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
809       GST_LOG_OBJECT (registry, "not a regular file, ignoring");
810       g_free (filename);
811       continue;
812     }
813     if (!g_str_has_suffix (filename, ".so") &&
814         !g_str_has_suffix (filename, ".dll") &&
815         !g_str_has_suffix (filename, ".dynlib")) {
816       GST_LOG_OBJECT (registry,
817           "extension is not recognized as module file, ignoring");
818       g_free (filename);
819       continue;
820     }
822     /* plug-ins are considered unique by basename; if the given name
823      * was already seen by the registry, we ignore it */
824     plugin = gst_registry_lookup (registry, filename);
825     if (plugin) {
826       struct stat file_status;
828       if (stat (filename, &file_status)) {
829         /* Plugin will be removed from cache after the scan completes if it
830          * is still marked 'cached' */
831         g_free (filename);
832         gst_object_unref (plugin);
833         continue;
834       }
835       if (plugin->registered) {
836         GST_DEBUG_OBJECT (registry,
837             "plugin already registered from path \"%s\"",
838             GST_STR_NULL (plugin->filename));
839         g_free (filename);
840         gst_object_unref (plugin);
841         continue;
842       }
843       if (plugin->file_mtime == file_status.st_mtime &&
844           plugin->file_size == file_status.st_size) {
845         GST_DEBUG_OBJECT (registry, "file %s cached", filename);
846         plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
847         GST_DEBUG_OBJECT (registry, "marking plugin %p as registered as %s",
848             plugin, filename);
849         plugin->registered = TRUE;
850         /* Update the file path on which we've seen this cached plugin 
851          * to ensure the registry cache will reflect up to date information */
852         if (strcmp (plugin->filename, filename) != 0) {
853           g_free (plugin->filename);
854           plugin->filename = g_strdup (filename);
855           changed = TRUE;
856         }
857       } else {
858         GST_INFO_OBJECT (registry, "cached info for %s is stale", filename);
859         GST_DEBUG_OBJECT (registry, "mtime %ld != %ld or size %"
860             G_GSIZE_FORMAT " != %"
861             G_GSIZE_FORMAT, plugin->file_mtime, file_status.st_mtime,
862             plugin->file_size, file_status.st_size);
863         gst_registry_remove_plugin (gst_registry_get_default (), plugin);
864         newplugin = gst_plugin_load_file (filename, NULL);
865         if (newplugin) {
866           GST_DEBUG_OBJECT (registry, "marking new plugin %p as registered",
867               newplugin);
868           newplugin->registered = TRUE;
869           gst_object_unref (newplugin);
870         }
871         changed = TRUE;
872       }
873       gst_object_unref (plugin);
875     } else {
876       GST_DEBUG_OBJECT (registry, "file %s not yet in registry", filename);
877       newplugin = gst_plugin_load_file (filename, NULL);
878       if (newplugin) {
879         newplugin->registered = TRUE;
880         gst_object_unref (newplugin);
881         changed = TRUE;
882       }
883     }
885     g_free (filename);
886   }
888   g_dir_close (dir);
890   return changed;
893 /**
894  * gst_registry_scan_path:
895  * @registry: the registry to add the path to
896  * @path: the path to add to the registry
897  *
898  * Add the given path to the registry. The syntax of the
899  * path is specific to the registry. If the path has already been
900  * added, do nothing.
901  *
902  * Returns: %TRUE if registry changed
903  */
904 gboolean
905 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
907   gboolean changed;
909   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
910   g_return_val_if_fail (path != NULL, FALSE);
912   GST_DEBUG_OBJECT (registry, "scanning path %s", path);
913   changed = gst_registry_scan_path_level (registry, path, 10);
915   GST_DEBUG_OBJECT (registry, "registry changed in path %s: %d", path, changed);
917   return changed;
920 /* Unref all plugins marked 'cached', to clear old plugins that no
921  * longer exist. Returns TRUE if any plugins were removed */
922 gboolean
923 _priv_gst_registry_remove_cache_plugins (GstRegistry * registry)
925   GList *g;
926   GList *g_next;
927   GstPlugin *plugin;
928   gboolean changed = FALSE;
930   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
932   GST_OBJECT_LOCK (registry);
934   GST_DEBUG_OBJECT (registry, "removing cached plugins");
935   g = registry->plugins;
936   while (g) {
937     g_next = g->next;
938     plugin = g->data;
939     if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
940       GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
941           GST_STR_NULL (plugin->filename));
942       registry->plugins = g_list_delete_link (registry->plugins, g);
943       gst_object_unref (plugin);
944       changed = TRUE;
945     }
946     g = g_next;
947   }
949   GST_OBJECT_UNLOCK (registry);
951   return changed;
955 static gboolean
956 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
957     gpointer user_data)
959   return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
962 /**
963  * gst_registry_get_feature_list_by_plugin:
964  * @registry: a #GstRegistry.
965  * @name: a plugin name.
966  *
967  * Retrieves a #GList of features of the plugin with name @name.
968  *
969  * Returns: a #GList of #GstPluginFeature. gst_plugin_feature_list_free() after usage.
970  */
971 GList *
972 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
973     const gchar * name)
975   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
976   g_return_val_if_fail (name != NULL, NULL);
978   return gst_registry_feature_filter (registry,
979       _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
982 /* Unref and delete the default registry */
983 void
984 _priv_gst_registry_cleanup ()
986   GstRegistry *registry;
988   g_static_mutex_lock (&_gst_registry_mutex);
989   if ((registry = _gst_registry_default) != NULL) {
990     _gst_registry_default = NULL;
991   }
992   g_static_mutex_unlock (&_gst_registry_mutex);
994   /* unref outside of the lock because we can. */
995   if (registry)
996     gst_object_unref (registry);
999 /**
1000  * gst_default_registry_check_feature_version:
1001  * @feature_name: the name of the feature (e.g. "oggdemux")
1002  * @min_major: the minimum major version number
1003  * @min_minor: the minimum minor version number
1004  * @min_micro: the minimum micro version number
1005  *
1006  * Checks whether a plugin feature by the given name exists in the
1007  * default registry and whether its version is at least the
1008  * version required.
1009  *
1010  * Returns: #TRUE if the feature could be found and the version is
1011  * the same as the required version or newer, and #FALSE otherwise.
1012  */
1013 gboolean
1014 gst_default_registry_check_feature_version (const gchar * feature_name,
1015     guint min_major, guint min_minor, guint min_micro)
1017   GstPluginFeature *feature;
1018   GstRegistry *registry;
1019   gboolean ret = FALSE;
1021   g_return_val_if_fail (feature_name != NULL, FALSE);
1023   GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1025   registry = gst_registry_get_default ();
1026   feature = gst_registry_lookup_feature (registry, feature_name);
1027   if (feature) {
1028     ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1029         min_micro);
1030     gst_object_unref (feature);
1031   } else {
1032     GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1033   }
1035   return ret;