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
119 {
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)
139 {
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);
173 }
175 static void
176 gst_registry_init (GstRegistry * registry)
177 {
178 registry->feature_hash = g_hash_table_new (g_str_hash, g_str_equal);
179 }
181 static void
182 gst_registry_finalize (GObject * object)
183 {
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);
225 }
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)
237 {
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;
250 }
252 /**
253 * gst_registry_add_path:
254 * @registry: the registry to add the path to
255 * @path: the path to add to the registry
256 *
257 * Add the given path to the registry. The syntax of the
258 * path is specific to the registry. If the path has already been
259 * added, do nothing.
260 */
261 void
262 gst_registry_add_path (GstRegistry * registry, const gchar * path)
263 {
264 g_return_if_fail (GST_IS_REGISTRY (registry));
265 g_return_if_fail (path != NULL);
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 }
291 }
293 /**
294 * gst_registry_get_path_list:
295 * @registry: the registry to get the pathlist of
296 *
297 * Get the list of paths for the given registry.
298 *
299 * Returns: A Glist of paths as strings. g_list_free after use.
300 *
301 * MT safe.
302 */
303 GList *
304 gst_registry_get_path_list (GstRegistry * registry)
305 {
306 GList *list;
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;
317 }
320 /**
321 * gst_registry_add_plugin:
322 * @registry: the registry to add the plugin to
323 * @plugin: the plugin to add
324 *
325 * Add the plugin to the registry. The plugin-added signal will be emitted.
326 * This function will sink @plugin.
327 *
328 * Returns: TRUE on success.
329 *
330 * MT safe.
331 */
332 gboolean
333 gst_registry_add_plugin (GstRegistry * registry, GstPlugin * plugin)
334 {
335 GstPlugin *existing_plugin;
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;
365 }
367 static void
368 gst_registry_remove_features_for_plugin_unlocked (GstRegistry * registry,
369 GstPlugin * plugin)
370 {
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 }
393 }
395 /**
396 * gst_registry_remove_plugin:
397 * @registry: the registry to remove the plugin from
398 * @plugin: the plugin to remove
399 *
400 * Remove the plugin from the registry.
401 *
402 * MT safe.
403 */
404 void
405 gst_registry_remove_plugin (GstRegistry * registry, GstPlugin * plugin)
406 {
407 g_return_if_fail (GST_IS_REGISTRY (registry));
408 g_return_if_fail (GST_IS_PLUGIN (plugin));
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);
418 }
420 /**
421 * gst_registry_add_feature:
422 * @registry: the registry to add the plugin to
423 * @feature: the feature to add
424 *
425 * Add the feature to the registry. The feature-added signal will be emitted.
426 * This function sinks @feature.
427 *
428 * Returns: TRUE on success.
429 *
430 * MT safe.
431 */
432 gboolean
433 gst_registry_add_feature (GstRegistry * registry, GstPluginFeature * feature)
434 {
435 GstPluginFeature *existing_feature;
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;
472 }
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)
485 {
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);
497 }
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)
519 {
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;
534 }
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)
556 {
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;
571 }
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)
588 {
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;
605 }
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)
623 {
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;
647 }
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)
663 {
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);
675 }
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)
690 {
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;
704 }
706 static GstPluginFeature *
707 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
708 {
709 if (G_UNLIKELY (name == NULL))
710 return NULL;
712 return g_hash_table_lookup (registry->feature_hash, name);
713 }
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)
729 {
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;
742 }
744 static GstPlugin *
745 gst_registry_lookup_locked (GstRegistry * registry, const char *filename)
746 {
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;
766 }
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)
781 {
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;
794 }
796 static gboolean
797 gst_registry_scan_path_level (GstRegistry * registry, const gchar * path,
798 int level)
799 {
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 GST_LOG_OBJECT (registry, "examining file: %s", filename);
816 if (g_file_test (filename, G_FILE_TEST_IS_DIR)) {
817 if (level > 0) {
818 GST_LOG_OBJECT (registry, "found directory, recursing");
819 changed |= gst_registry_scan_path_level (registry, filename, level - 1);
820 } else {
821 GST_LOG_OBJECT (registry,
822 "found directory, but recursion level is too deep");
823 }
824 g_free (filename);
825 continue;
826 }
827 if (!g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
828 GST_LOG_OBJECT (registry, "not a regular file, ignoring");
829 g_free (filename);
830 continue;
831 }
832 if (!g_str_has_suffix (filename, G_MODULE_SUFFIX)
833 #ifdef GST_EXTRA_MODULE_SUFFIX
834 && !g_str_has_suffix (filename, GST_EXTRA_MODULE_SUFFIX)
835 #endif
836 ) {
837 GST_LOG_OBJECT (registry,
838 "extension is not recognized as module file, ignoring");
839 g_free (filename);
840 continue;
841 }
843 /* plug-ins are considered unique by basename; if the given name
844 * was already seen by the registry, we ignore it */
845 plugin = gst_registry_lookup (registry, filename);
846 if (plugin) {
847 struct stat file_status;
849 if (stat (filename, &file_status)) {
850 /* Plugin will be removed from cache after the scan completes if it
851 * is still marked 'cached' */
852 g_free (filename);
853 gst_object_unref (plugin);
854 continue;
855 }
856 if (plugin->registered) {
857 GST_DEBUG_OBJECT (registry,
858 "plugin already registered from path \"%s\"",
859 GST_STR_NULL (plugin->filename));
860 g_free (filename);
861 gst_object_unref (plugin);
862 continue;
863 }
864 if (plugin->file_mtime == file_status.st_mtime &&
865 plugin->file_size == file_status.st_size) {
866 GST_LOG_OBJECT (registry, "file %s cached", filename);
867 plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
868 GST_LOG_OBJECT (registry, "marking plugin %p as registered as %s",
869 plugin, filename);
870 plugin->registered = TRUE;
871 /* Update the file path on which we've seen this cached plugin
872 * to ensure the registry cache will reflect up to date information */
873 if (strcmp (plugin->filename, filename) != 0) {
874 g_free (plugin->filename);
875 plugin->filename = g_strdup (filename);
876 changed = TRUE;
877 }
878 } else {
879 GST_INFO_OBJECT (registry, "cached info for %s is stale", filename);
880 GST_DEBUG_OBJECT (registry, "mtime %ld != %ld or size %"
881 G_GINT64_FORMAT " != %"
882 G_GINT64_FORMAT, plugin->file_mtime, file_status.st_mtime,
883 (gint64) plugin->file_size, (gint64) file_status.st_size);
884 gst_registry_remove_plugin (gst_registry_get_default (), plugin);
885 /* We don't use a GError here because a failure to load some shared
886 * objects as plugins is normal (particularly in the uninstalled case)
887 */
888 newplugin = gst_plugin_load_file (filename, NULL);
889 if (newplugin) {
890 GST_DEBUG_OBJECT (registry, "marking new plugin %p as registered",
891 newplugin);
892 newplugin->registered = TRUE;
893 gst_object_unref (newplugin);
894 }
895 changed = TRUE;
896 }
897 gst_object_unref (plugin);
899 } else {
900 GST_DEBUG_OBJECT (registry, "file %s not yet in registry", filename);
901 newplugin = gst_plugin_load_file (filename, NULL);
902 if (newplugin) {
903 newplugin->registered = TRUE;
904 gst_object_unref (newplugin);
905 changed = TRUE;
906 }
907 }
909 g_free (filename);
910 }
912 g_dir_close (dir);
914 return changed;
915 }
917 /**
918 * gst_registry_scan_path:
919 * @registry: the registry to add the path to
920 * @path: the path to add to the registry
921 *
922 * Add the given path to the registry. The syntax of the
923 * path is specific to the registry. If the path has already been
924 * added, do nothing.
925 *
926 * Returns: %TRUE if registry changed
927 */
928 gboolean
929 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
930 {
931 gboolean changed;
933 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
934 g_return_val_if_fail (path != NULL, FALSE);
936 GST_DEBUG_OBJECT (registry, "scanning path %s", path);
937 changed = gst_registry_scan_path_level (registry, path, 10);
939 GST_DEBUG_OBJECT (registry, "registry changed in path %s: %d", path, changed);
941 return changed;
942 }
944 /* Unref all plugins marked 'cached', to clear old plugins that no
945 * longer exist. Returns TRUE if any plugins were removed */
946 gboolean
947 _priv_gst_registry_remove_cache_plugins (GstRegistry * registry)
948 {
949 GList *g;
950 GList *g_next;
951 GstPlugin *plugin;
952 gboolean changed = FALSE;
954 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
956 GST_OBJECT_LOCK (registry);
958 GST_DEBUG_OBJECT (registry, "removing cached plugins");
959 g = registry->plugins;
960 while (g) {
961 g_next = g->next;
962 plugin = g->data;
963 if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
964 GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
965 GST_STR_NULL (plugin->filename));
966 registry->plugins = g_list_delete_link (registry->plugins, g);
967 gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
968 gst_object_unref (plugin);
969 changed = TRUE;
970 }
971 g = g_next;
972 }
974 GST_OBJECT_UNLOCK (registry);
976 return changed;
977 }
980 static gboolean
981 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
982 gpointer user_data)
983 {
984 return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
985 }
987 /**
988 * gst_registry_get_feature_list_by_plugin:
989 * @registry: a #GstRegistry.
990 * @name: a plugin name.
991 *
992 * Retrieves a #GList of features of the plugin with name @name.
993 *
994 * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
995 * after usage.
996 */
997 GList *
998 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
999 const gchar * name)
1000 {
1001 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1002 g_return_val_if_fail (name != NULL, NULL);
1004 return gst_registry_feature_filter (registry,
1005 _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1006 }
1008 /* Unref and delete the default registry */
1009 void
1010 _priv_gst_registry_cleanup ()
1011 {
1012 GstRegistry *registry;
1014 g_static_mutex_lock (&_gst_registry_mutex);
1015 if ((registry = _gst_registry_default) != NULL) {
1016 _gst_registry_default = NULL;
1017 }
1018 g_static_mutex_unlock (&_gst_registry_mutex);
1020 /* unref outside of the lock because we can. */
1021 if (registry)
1022 gst_object_unref (registry);
1023 }
1025 /**
1026 * gst_default_registry_check_feature_version:
1027 * @feature_name: the name of the feature (e.g. "oggdemux")
1028 * @min_major: the minimum major version number
1029 * @min_minor: the minimum minor version number
1030 * @min_micro: the minimum micro version number
1031 *
1032 * Checks whether a plugin feature by the given name exists in the
1033 * default registry and whether its version is at least the
1034 * version required.
1035 *
1036 * Returns: #TRUE if the feature could be found and the version is
1037 * the same as the required version or newer, and #FALSE otherwise.
1038 */
1039 gboolean
1040 gst_default_registry_check_feature_version (const gchar * feature_name,
1041 guint min_major, guint min_minor, guint min_micro)
1042 {
1043 GstPluginFeature *feature;
1044 GstRegistry *registry;
1045 gboolean ret = FALSE;
1047 g_return_val_if_fail (feature_name != NULL, FALSE);
1049 GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1051 registry = gst_registry_get_default ();
1052 feature = gst_registry_lookup_feature (registry, feature_name);
1053 if (feature) {
1054 ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1055 min_micro);
1056 gst_object_unref (feature);
1057 } else {
1058 GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1059 }
1061 return ret;
1062 }