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>
6 * gstregistry.c: handle registry
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.
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.
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.
26 * @short_description: Abstract base class for management of #GstPlugin objects
27 * @see_also: #GstPlugin, #GstPluginFeature
29 * One registry holds the metadata of a set of plugins.
31 * <emphasis role="bold">Design:</emphasis>
33 * The #GstRegistry object is a list of plugins and some functions for dealing
34 * with them. #GstPlugins are matched 1-1 with a file on disk, and may or may
35 * not be loaded at a given time. There may be multiple #GstRegistry objects,
36 * but the "default registry" is the only object that has any meaning to the
39 * The registry.xml file is actually a cache of plugin information. This is
40 * unlike versions prior to 0.10, where the registry file was the primary source
41 * of plugin information, and was created by the gst-register command.
43 * The primary source, at all times, of plugin information is each plugin file
44 * itself. Thus, if an application wants information about a particular plugin,
45 * or wants to search for a feature that satisfies given criteria, the primary
46 * means of doing so is to load every plugin and look at the resulting
47 * information that is gathered in the default registry. Clearly, this is a time
48 * consuming process, so we cache information in the registry.xml file.
50 * On startup, plugins are searched for in the plugin search path. This path can
51 * be set directly using the %GST_PLUGIN_PATH environment variable. The registry
52 * file is loaded from ~/.gstreamer-$GST_MAJORMINOR/registry-$ARCH.xml or the
53 * file listed in the %GST_REGISTRY env var. The only reason to change the
54 * registry location is for testing.
56 * For each plugin that is found in the plugin search path, there could be 3
57 * possibilities for cached information:
60 * <para>the cache may not contain information about a given file.</para>
63 * <para>the cache may have stale information.</para>
66 * <para>the cache may have current information.</para>
70 * In the first two cases, the plugin is loaded and the cache updated. In
71 * addition to these cases, the cache may have entries for plugins that are not
72 * relevant to the current process. These are marked as not available to the
73 * current process. If the cache is updated for whatever reason, it is marked
76 * A dirty cache is written out at the end of initialization. Each entry is
77 * checked to make sure the information is minimally valid. If not, the entry is
80 * <emphasis role="bold">Implementation notes:</emphasis>
82 * The "cache" and "default registry" are different concepts and can represent
83 * different sets of plugins. For various reasons, at init time, the cache is
84 * stored in the default registry, and plugins not relevant to the current
85 * process are marked with the %GST_PLUGIN_FLAG_CACHED bit. These plugins are
86 * removed at the end of intitialization.
92 #include "gstconfig.h"
93 #include "gst_private.h"
95 #include <sys/types.h>
105 #include <glib/gstdio.h>
108 #include "gsterror.h"
109 #include "gstregistry.h"
110 #include "gstmarshal.h"
111 #include "gstfilter.h"
113 #include "gstpluginloader.h"
115 #include "gst-i18n-lib.h"
117 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
119 /* the one instance of the default registry and the mutex protecting the
121 static GStaticMutex _gst_registry_mutex = G_STATIC_MUTEX_INIT;
122 static GstRegistry *_gst_registry_default = NULL;
125 #define DEFAULT_FORK TRUE
127 /* control the behaviour of registry rebuild */
128 static gboolean _gst_enable_registry_fork = DEFAULT_FORK;
129 /* List of plugins that need preloading/reloading after scanning registry */
130 extern GSList *_priv_gst_preload_plugins;
132 #ifndef GST_DISABLE_REGISTRY
133 /*set to TRUE when registry needn't to be updated */
134 gboolean _priv_gst_disable_registry_update = FALSE;
135 extern GList *_priv_gst_plugin_paths;
137 /* Set to TRUE when the registry cache should be disabled */
138 gboolean _gst_disable_registry_cache = FALSE;
141 /* Element signals and args */
149 static void gst_registry_finalize (GObject * object);
151 static guint gst_registry_signals[LAST_SIGNAL] = { 0 };
153 static GstPluginFeature *gst_registry_lookup_feature_locked (GstRegistry *
154 registry, const char *name);
155 static GstPlugin *gst_registry_lookup_bn_locked (GstRegistry * registry,
156 const char *basename);
158 G_DEFINE_TYPE (GstRegistry, gst_registry, GST_TYPE_OBJECT);
159 static GstObjectClass *parent_class = NULL;
162 gst_registry_class_init (GstRegistryClass * klass)
164 GObjectClass *gobject_class;
166 gobject_class = (GObjectClass *) klass;
168 parent_class = g_type_class_peek_parent (klass);
171 * GstRegistry::plugin-added:
172 * @registry: the registry that emitted the signal
173 * @plugin: the plugin that has been added
175 * Signals that a plugin has been added to the registry (possibly
176 * replacing a previously-added one by the same name)
178 gst_registry_signals[PLUGIN_ADDED] =
179 g_signal_new ("plugin-added", G_TYPE_FROM_CLASS (klass),
180 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, plugin_added), NULL,
181 NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
184 * GstRegistry::feature-added:
185 * @registry: the registry that emitted the signal
186 * @feature: the feature that has been added
188 * Signals that a feature has been added to the registry (possibly
189 * replacing a previously-added one by the same name)
191 gst_registry_signals[FEATURE_ADDED] =
192 g_signal_new ("feature-added", G_TYPE_FROM_CLASS (klass),
193 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, feature_added),
194 NULL, NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
196 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_registry_finalize);
200 gst_registry_init (GstRegistry * registry)
202 registry->feature_hash = g_hash_table_new (g_str_hash, g_str_equal);
206 gst_registry_finalize (GObject * object)
208 GstRegistry *registry = GST_REGISTRY (object);
212 plugins = registry->plugins;
213 registry->plugins = NULL;
215 GST_DEBUG_OBJECT (registry, "registry finalize");
218 GstPlugin *plugin = p->data;
221 GST_LOG_OBJECT (registry, "removing plugin %s",
222 gst_plugin_get_name (plugin));
223 gst_object_unref (plugin);
227 g_list_free (plugins);
229 features = registry->features;
230 registry->features = NULL;
234 GstPluginFeature *feature = f->data;
237 GST_LOG_OBJECT (registry, "removing feature %p (%s)",
238 feature, gst_plugin_feature_get_name (feature));
239 gst_object_unref (feature);
243 g_list_free (features);
245 g_hash_table_destroy (registry->feature_hash);
246 registry->feature_hash = NULL;
248 G_OBJECT_CLASS (parent_class)->finalize (object);
252 * gst_registry_get_default:
254 * Retrieves the default registry. The caller does not own a reference on the
255 * registry, as it is alive as long as GStreamer is initialized.
257 * Returns: The default #GstRegistry.
260 gst_registry_get_default (void)
262 GstRegistry *registry;
264 g_static_mutex_lock (&_gst_registry_mutex);
265 if (G_UNLIKELY (!_gst_registry_default)) {
266 _gst_registry_default = g_object_new (GST_TYPE_REGISTRY, NULL);
267 gst_object_ref_sink (GST_OBJECT_CAST (_gst_registry_default));
269 registry = _gst_registry_default;
270 g_static_mutex_unlock (&_gst_registry_mutex);
276 * gst_registry_add_path:
277 * @registry: the registry to add the path to
278 * @path: the path to add to the registry
280 * Add the given path to the registry. The syntax of the
281 * path is specific to the registry. If the path has already been
285 gst_registry_add_path (GstRegistry * registry, const gchar * path)
287 g_return_if_fail (GST_IS_REGISTRY (registry));
288 g_return_if_fail (path != NULL);
290 if (strlen (path) == 0)
293 GST_OBJECT_LOCK (registry);
294 if (g_list_find_custom (registry->paths, path, (GCompareFunc) strcmp))
297 GST_INFO ("Adding plugin path: \"%s\"", path);
298 registry->paths = g_list_append (registry->paths, g_strdup (path));
299 GST_OBJECT_UNLOCK (registry);
305 GST_INFO ("Ignoring empty plugin path");
310 g_warning ("path %s already added to registry", path);
311 GST_OBJECT_UNLOCK (registry);
317 * gst_registry_get_path_list:
318 * @registry: the registry to get the pathlist of
320 * Get the list of paths for the given registry.
322 * Returns: A Glist of paths as strings. g_list_free after use.
327 gst_registry_get_path_list (GstRegistry * registry)
331 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
333 GST_OBJECT_LOCK (registry);
334 /* We don't need to copy the strings, because they won't be deleted
335 * as long as the GstRegistry is around */
336 list = g_list_copy (registry->paths);
337 GST_OBJECT_UNLOCK (registry);
344 * gst_registry_add_plugin:
345 * @registry: the registry to add the plugin to
346 * @plugin: the plugin to add
348 * Add the plugin to the registry. The plugin-added signal will be emitted.
349 * This function will sink @plugin.
351 * Returns: TRUE on success.
356 gst_registry_add_plugin (GstRegistry * registry, GstPlugin * plugin)
358 GstPlugin *existing_plugin;
360 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
361 g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
363 GST_OBJECT_LOCK (registry);
364 existing_plugin = gst_registry_lookup_bn_locked (registry, plugin->basename);
365 if (G_UNLIKELY (existing_plugin)) {
366 GST_DEBUG_OBJECT (registry,
367 "Replacing existing plugin %p with new plugin %p for filename \"%s\"",
368 existing_plugin, plugin, GST_STR_NULL (plugin->filename));
369 registry->plugins = g_list_remove (registry->plugins, existing_plugin);
370 gst_object_unref (existing_plugin);
373 GST_DEBUG_OBJECT (registry, "adding plugin %p for filename \"%s\"",
374 plugin, GST_STR_NULL (plugin->filename));
376 registry->plugins = g_list_prepend (registry->plugins, plugin);
378 gst_object_ref_sink (plugin);
379 GST_OBJECT_UNLOCK (registry);
381 GST_LOG_OBJECT (registry, "emitting plugin-added for filename \"%s\"",
382 GST_STR_NULL (plugin->filename));
383 g_signal_emit (registry, gst_registry_signals[PLUGIN_ADDED], 0, plugin);
389 gst_registry_remove_features_for_plugin_unlocked (GstRegistry * registry,
395 g_return_if_fail (GST_IS_REGISTRY (registry));
396 g_return_if_fail (GST_IS_PLUGIN (plugin));
398 name = gst_plugin_get_name (plugin);
400 /* Remove all features for this plugin */
401 f = registry->features;
403 GList *next = g_list_next (f);
404 GstPluginFeature *feature = f->data;
406 if (G_UNLIKELY (feature && !strcmp (feature->plugin_name, name))) {
407 GST_DEBUG_OBJECT (registry, "removing feature %p (%s) for plugin %s",
408 feature, gst_plugin_feature_get_name (feature), name);
410 registry->features = g_list_delete_link (registry->features, f);
411 g_hash_table_remove (registry->feature_hash, feature->name);
412 gst_object_unref (feature);
419 * gst_registry_remove_plugin:
420 * @registry: the registry to remove the plugin from
421 * @plugin: the plugin to remove
423 * Remove the plugin from the registry.
428 gst_registry_remove_plugin (GstRegistry * registry, GstPlugin * plugin)
430 g_return_if_fail (GST_IS_REGISTRY (registry));
431 g_return_if_fail (GST_IS_PLUGIN (plugin));
433 GST_DEBUG_OBJECT (registry, "removing plugin %p (%s)",
434 plugin, gst_plugin_get_name (plugin));
436 GST_OBJECT_LOCK (registry);
437 registry->plugins = g_list_remove (registry->plugins, plugin);
438 gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
439 GST_OBJECT_UNLOCK (registry);
440 gst_object_unref (plugin);
444 * gst_registry_add_feature:
445 * @registry: the registry to add the plugin to
446 * @feature: the feature to add
448 * Add the feature to the registry. The feature-added signal will be emitted.
449 * This function sinks @feature.
451 * Returns: TRUE on success.
456 gst_registry_add_feature (GstRegistry * registry, GstPluginFeature * feature)
458 GstPluginFeature *existing_feature;
460 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
461 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
462 g_return_val_if_fail (feature->name != NULL, FALSE);
463 g_return_val_if_fail (feature->plugin_name != NULL, FALSE);
465 GST_OBJECT_LOCK (registry);
466 existing_feature = gst_registry_lookup_feature_locked (registry,
468 if (G_UNLIKELY (existing_feature)) {
469 GST_DEBUG_OBJECT (registry, "replacing existing feature %p (%s)",
470 existing_feature, feature->name);
471 /* Remove the existing feature from the list now, before we insert the new
472 * one, but don't unref yet because the hash is still storing a reference to
474 registry->features = g_list_remove (registry->features, existing_feature);
477 GST_DEBUG_OBJECT (registry, "adding feature %p (%s)", feature, feature->name);
479 registry->features = g_list_prepend (registry->features, feature);
480 g_hash_table_replace (registry->feature_hash, feature->name, feature);
482 if (G_UNLIKELY (existing_feature)) {
483 /* We unref now. No need to remove the feature name from the hash table, it
484 * got replaced by the new feature */
485 gst_object_unref (existing_feature);
488 gst_object_ref_sink (feature);
489 GST_OBJECT_UNLOCK (registry);
491 GST_LOG_OBJECT (registry, "emitting feature-added for %s", feature->name);
492 g_signal_emit (registry, gst_registry_signals[FEATURE_ADDED], 0, feature);
498 * gst_registry_remove_feature:
499 * @registry: the registry to remove the feature from
500 * @feature: the feature to remove
502 * Remove the feature from the registry.
507 gst_registry_remove_feature (GstRegistry * registry, GstPluginFeature * feature)
509 g_return_if_fail (GST_IS_REGISTRY (registry));
510 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
512 GST_DEBUG_OBJECT (registry, "removing feature %p (%s)",
513 feature, gst_plugin_feature_get_name (feature));
515 GST_OBJECT_LOCK (registry);
516 registry->features = g_list_remove (registry->features, feature);
517 g_hash_table_remove (registry->feature_hash, feature->name);
518 GST_OBJECT_UNLOCK (registry);
519 gst_object_unref (feature);
523 * gst_registry_plugin_filter:
524 * @registry: registry to query
525 * @filter: the filter to use
526 * @first: only return first match
527 * @user_data: user data passed to the filter function
529 * Runs a filter against all plugins in the registry and returns a #GList with
530 * the results. If the first flag is set, only the first match is
531 * returned (as a list with a single object).
532 * Every plugin is reffed; use gst_plugin_list_free() after use, which
535 * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
540 gst_registry_plugin_filter (GstRegistry * registry,
541 GstPluginFilter filter, gboolean first, gpointer user_data)
546 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
548 GST_OBJECT_LOCK (registry);
549 list = gst_filter_run (registry->plugins, (GstFilterFunc) filter, first,
551 for (g = list; g; g = g->next) {
552 gst_object_ref (GST_PLUGIN_CAST (g->data));
554 GST_OBJECT_UNLOCK (registry);
560 * gst_registry_feature_filter:
561 * @registry: registry to query
562 * @filter: the filter to use
563 * @first: only return first match
564 * @user_data: user data passed to the filter function
566 * Runs a filter against all features of the plugins in the registry
567 * and returns a GList with the results.
568 * If the first flag is set, only the first match is
569 * returned (as a list with a single object).
571 * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
577 gst_registry_feature_filter (GstRegistry * registry,
578 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
583 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
585 GST_OBJECT_LOCK (registry);
586 list = gst_filter_run (registry->features, (GstFilterFunc) filter, first,
588 for (g = list; g; g = g->next) {
589 gst_object_ref (GST_PLUGIN_FEATURE_CAST (g->data));
591 GST_OBJECT_UNLOCK (registry);
597 * gst_registry_find_plugin:
598 * @registry: the registry to search
599 * @name: the plugin name to find
601 * Find the plugin with the given name in the registry.
602 * The plugin will be reffed; caller is responsible for unreffing.
604 * Returns: The plugin with the given name or NULL if the plugin was not found.
605 * gst_object_unref() after usage.
610 gst_registry_find_plugin (GstRegistry * registry, const gchar * name)
613 GstPlugin *result = NULL;
615 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
616 g_return_val_if_fail (name != NULL, NULL);
618 walk = gst_registry_plugin_filter (registry,
619 (GstPluginFilter) gst_plugin_name_filter, TRUE, (gpointer) name);
621 result = GST_PLUGIN_CAST (walk->data);
623 gst_object_ref (result);
624 gst_plugin_list_free (walk);
631 * gst_registry_find_feature:
632 * @registry: the registry to search
633 * @name: the pluginfeature name to find
634 * @type: the pluginfeature type to find
636 * Find the pluginfeature with the given name and type in the registry.
638 * Returns: The pluginfeature with the given name and type or NULL
639 * if the plugin was not found. gst_object_unref() after usage.
644 gst_registry_find_feature (GstRegistry * registry, const gchar * name,
647 GstPluginFeature *feature = NULL;
649 GstTypeNameData data;
651 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
652 g_return_val_if_fail (name != NULL, NULL);
653 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
658 walk = gst_registry_feature_filter (registry,
659 (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
663 feature = GST_PLUGIN_FEATURE_CAST (walk->data);
665 gst_object_ref (feature);
666 gst_plugin_feature_list_free (walk);
673 * gst_registry_get_feature_list:
674 * @registry: a #GstRegistry
677 * Retrieves a #GList of #GstPluginFeature of @type.
679 * Returns: a #GList of #GstPluginFeature of @type. Use
680 * gst_plugin_feature_list_free() after usage.
685 gst_registry_get_feature_list (GstRegistry * registry, GType type)
687 GstTypeNameData data;
689 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
690 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
695 return gst_registry_feature_filter (registry,
696 (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
701 * gst_registry_get_plugin_list:
702 * @registry: the registry to search
704 * Get a copy of all plugins registered in the given registry. The refcount
705 * of each element in the list in incremented.
707 * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
712 gst_registry_get_plugin_list (GstRegistry * registry)
717 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
719 GST_OBJECT_LOCK (registry);
720 list = g_list_copy (registry->plugins);
721 for (g = list; g; g = g->next) {
722 gst_object_ref (GST_PLUGIN_CAST (g->data));
724 GST_OBJECT_UNLOCK (registry);
729 static GstPluginFeature *
730 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
732 if (G_UNLIKELY (name == NULL))
735 return g_hash_table_lookup (registry->feature_hash, name);
739 * gst_registry_lookup_feature:
740 * @registry: a #GstRegistry
741 * @name: a #GstPluginFeature name
743 * Find a #GstPluginFeature with @name in @registry.
745 * Returns: a #GstPluginFeature with its refcount incremented, use
746 * gst_object_unref() after usage.
751 gst_registry_lookup_feature (GstRegistry * registry, const char *name)
753 GstPluginFeature *feature;
755 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
756 g_return_val_if_fail (name != NULL, NULL);
758 GST_OBJECT_LOCK (registry);
759 feature = gst_registry_lookup_feature_locked (registry, name);
761 gst_object_ref (feature);
762 GST_OBJECT_UNLOCK (registry);
768 gst_registry_lookup_bn_locked (GstRegistry * registry, const char *basename)
773 /* FIXME: use GTree speed up lookups */
774 for (g = registry->plugins; g; g = g_list_next (g)) {
775 plugin = GST_PLUGIN_CAST (g->data);
776 if (G_UNLIKELY (plugin->basename
777 && strcmp (basename, plugin->basename) == 0)) {
785 gst_registry_lookup_bn (GstRegistry * registry, const char *basename)
789 GST_OBJECT_LOCK (registry);
790 plugin = gst_registry_lookup_bn_locked (registry, basename);
792 gst_object_ref (plugin);
793 GST_OBJECT_UNLOCK (registry);
799 * gst_registry_lookup:
800 * @registry: the registry to look up in
801 * @filename: the name of the file to look up
803 * Look up a plugin in the given registry with the given filename.
804 * If found, plugin is reffed.
806 * Returns: the #GstPlugin if found, or NULL if not. gst_object_unref()
810 gst_registry_lookup (GstRegistry * registry, const char *filename)
815 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
816 g_return_val_if_fail (filename != NULL, NULL);
818 basename = g_path_get_basename (filename);
819 if (G_UNLIKELY (basename == NULL))
822 plugin = gst_registry_lookup_bn (registry, basename);
831 REGISTRY_SCAN_HELPER_NOT_STARTED = 0,
832 REGISTRY_SCAN_HELPER_DISABLED,
833 REGISTRY_SCAN_HELPER_RUNNING
834 } GstRegistryScanHelperState;
838 GstRegistry *registry;
839 GstRegistryScanHelperState helper_state;
840 GstPluginLoader *helper;
842 } GstRegistryScanContext;
845 init_scan_context (GstRegistryScanContext * context, GstRegistry * registry)
849 context->registry = registry;
851 /* see if forking is enabled and set up the scan helper state accordingly */
852 do_fork = _gst_enable_registry_fork;
854 const gchar *fork_env;
856 /* forking enabled, see if it is disabled with an env var */
857 if ((fork_env = g_getenv ("GST_REGISTRY_FORK"))) {
858 /* fork enabled for any value different from "no" */
859 do_fork = strcmp (fork_env, "no") != 0;
864 context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
866 context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
868 context->helper = NULL;
869 context->changed = FALSE;
873 clear_scan_context (GstRegistryScanContext * context)
875 if (context->helper) {
876 context->changed |= _priv_gst_plugin_loader_funcs.destroy (context->helper);
877 context->helper = NULL;
882 gst_registry_scan_plugin_file (GstRegistryScanContext * context,
883 const gchar * filename, off_t file_size, time_t file_mtime)
885 gboolean changed = FALSE;
886 GstPlugin *newplugin = NULL;
888 /* Have a plugin to load - see if the scan-helper needs starting */
889 if (context->helper_state == REGISTRY_SCAN_HELPER_NOT_STARTED) {
890 GST_DEBUG ("Starting plugin scanner for file %s", filename);
891 context->helper = _priv_gst_plugin_loader_funcs.create (context->registry);
892 if (context->helper != NULL)
893 context->helper_state = REGISTRY_SCAN_HELPER_RUNNING;
895 GST_WARNING ("Failed starting plugin scanner. Scanning in-process");
896 context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
900 if (context->helper_state == REGISTRY_SCAN_HELPER_RUNNING) {
901 GST_DEBUG ("Using scan-helper to load plugin %s", filename);
902 if (!_priv_gst_plugin_loader_funcs.load (context->helper,
903 filename, file_size, file_mtime)) {
904 g_warning ("External plugin loader failed. This most likely means that "
905 "the plugin loader helper binary was not found or could not be run. "
906 "%s", (g_getenv ("GST_PLUGIN_PATH") != NULL) ?
907 "If you are running an uninstalled GStreamer setup, you might need "
908 "to update your gst-uninstalled script so that the "
909 "GST_PLUGIN_SCANNER environment variable gets set." : "");
910 context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
914 /* Check if the helper is disabled (or just got disabled above) */
915 if (context->helper_state == REGISTRY_SCAN_HELPER_DISABLED) {
916 /* Load plugin the old fashioned way... */
918 /* We don't use a GError here because a failure to load some shared
919 * objects as plugins is normal (particularly in the uninstalled case)
921 newplugin = gst_plugin_load_file (filename, NULL);
925 GST_DEBUG_OBJECT (context->registry, "marking new plugin %p as registered",
927 newplugin->registered = TRUE;
928 gst_object_unref (newplugin);
936 gst_registry_scan_path_level (GstRegistryScanContext * context,
937 const gchar * path, int level)
943 gboolean changed = FALSE;
945 dir = g_dir_open (path, 0, NULL);
949 while ((dirent = g_dir_read_name (dir))) {
950 struct stat file_status;
952 filename = g_build_filename (path, dirent, NULL);
953 if (g_stat (filename, &file_status) < 0) {
954 /* Plugin will be removed from cache after the scan completes if it
955 * is still marked 'cached' */
960 if (file_status.st_mode & S_IFDIR) {
961 /* skip the .debug directory, these contain elf files that are not
962 * useful or worse, can crash dlopen (). do a quick check for the . first
963 * and then call the compare functions. */
964 if (G_UNLIKELY (dirent[0] == '.' && (g_str_equal (dirent, ".debug")
965 || g_str_equal (dirent, ".git")))) {
966 GST_LOG_OBJECT (context->registry, "ignoring .debug or .git directory");
970 /* FIXME 0.11: Don't recurse into directories, this behaviour
971 * is inconsistent with other PATH environment variables
974 GST_LOG_OBJECT (context->registry, "recursing into directory %s",
976 changed |= gst_registry_scan_path_level (context, filename, level - 1);
978 GST_LOG_OBJECT (context->registry, "not recursing into directory %s, "
979 "recursion level too deep", filename);
984 if (!(file_status.st_mode & S_IFREG)) {
985 GST_LOG_OBJECT (context->registry, "%s is not a regular file, ignoring",
990 if (!g_str_has_suffix (dirent, G_MODULE_SUFFIX)
991 #ifdef GST_EXTRA_MODULE_SUFFIX
992 && !g_str_has_suffix (dirent, GST_EXTRA_MODULE_SUFFIX)
995 GST_LOG_OBJECT (context->registry,
996 "extension is not recognized as module file, ignoring file %s",
1002 GST_LOG_OBJECT (context->registry, "file %s looks like a possible module",
1005 /* plug-ins are considered unique by basename; if the given name
1006 * was already seen by the registry, we ignore it */
1007 plugin = gst_registry_lookup_bn (context->registry, dirent);
1009 gboolean env_vars_changed, deps_changed = FALSE;
1011 if (plugin->registered) {
1012 GST_DEBUG_OBJECT (context->registry,
1013 "plugin already registered from path \"%s\"",
1014 GST_STR_NULL (plugin->filename));
1016 gst_object_unref (plugin);
1020 env_vars_changed = _priv_plugin_deps_env_vars_changed (plugin);
1022 /* If a file with a certain basename is seen on a different path,
1023 * update the plugin to ensure the registry cache will reflect up
1024 * to date information */
1026 if (plugin->file_mtime == file_status.st_mtime &&
1027 plugin->file_size == file_status.st_size && !env_vars_changed &&
1028 !(deps_changed = _priv_plugin_deps_files_changed (plugin)) &&
1029 !strcmp (plugin->filename, filename)) {
1030 GST_LOG_OBJECT (context->registry, "file %s cached", filename);
1031 plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
1032 GST_LOG_OBJECT (context->registry,
1033 "marking plugin %p as registered as %s", plugin, filename);
1034 plugin->registered = TRUE;
1036 GST_INFO_OBJECT (context->registry, "cached info for %s is stale",
1038 GST_DEBUG_OBJECT (context->registry, "mtime %ld != %ld or size %"
1039 G_GINT64_FORMAT " != %" G_GINT64_FORMAT " or external dependency "
1040 "env_vars changed: %d or external dependencies changed: %d"
1041 " or old path %s != new path %s",
1042 plugin->file_mtime, file_status.st_mtime,
1043 (gint64) plugin->file_size, (gint64) file_status.st_size,
1044 env_vars_changed, deps_changed, plugin->filename, filename);
1045 gst_registry_remove_plugin (context->registry, plugin);
1046 changed |= gst_registry_scan_plugin_file (context, filename,
1047 file_status.st_size, file_status.st_mtime);
1049 gst_object_unref (plugin);
1052 GST_DEBUG_OBJECT (context->registry, "file %s not yet in registry",
1054 changed |= gst_registry_scan_plugin_file (context, filename,
1055 file_status.st_size, file_status.st_mtime);
1067 gst_registry_scan_path_internal (GstRegistryScanContext * context,
1072 GST_DEBUG_OBJECT (context->registry, "scanning path %s", path);
1073 changed = gst_registry_scan_path_level (context, path, 10);
1075 GST_DEBUG_OBJECT (context->registry, "registry changed in path %s: %d", path,
1081 * gst_registry_scan_path:
1082 * @registry: the registry to add found plugins to
1083 * @path: the path to scan
1085 * Scan the given path for plugins to add to the registry. The syntax of the
1086 * path is specific to the registry.
1088 * Returns: %TRUE if registry changed
1091 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
1093 GstRegistryScanContext context;
1096 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1097 g_return_val_if_fail (path != NULL, FALSE);
1099 init_scan_context (&context, registry);
1101 result = gst_registry_scan_path_internal (&context, path);
1103 clear_scan_context (&context);
1104 result |= context.changed;
1110 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
1113 return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
1117 * gst_registry_get_feature_list_by_plugin:
1118 * @registry: a #GstRegistry.
1119 * @name: a plugin name.
1121 * Retrieves a #GList of features of the plugin with name @name.
1123 * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
1127 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
1130 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1131 g_return_val_if_fail (name != NULL, NULL);
1133 return gst_registry_feature_filter (registry,
1134 _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1137 /* Unref and delete the default registry */
1139 _priv_gst_registry_cleanup ()
1141 GstRegistry *registry;
1143 g_static_mutex_lock (&_gst_registry_mutex);
1144 if ((registry = _gst_registry_default) != NULL) {
1145 _gst_registry_default = NULL;
1147 g_static_mutex_unlock (&_gst_registry_mutex);
1149 /* unref outside of the lock because we can. */
1151 gst_object_unref (registry);
1155 * gst_default_registry_check_feature_version:
1156 * @feature_name: the name of the feature (e.g. "oggdemux")
1157 * @min_major: the minimum major version number
1158 * @min_minor: the minimum minor version number
1159 * @min_micro: the minimum micro version number
1161 * Checks whether a plugin feature by the given name exists in the
1162 * default registry and whether its version is at least the
1165 * Returns: #TRUE if the feature could be found and the version is
1166 * the same as the required version or newer, and #FALSE otherwise.
1169 gst_default_registry_check_feature_version (const gchar * feature_name,
1170 guint min_major, guint min_minor, guint min_micro)
1172 GstPluginFeature *feature;
1173 GstRegistry *registry;
1174 gboolean ret = FALSE;
1176 g_return_val_if_fail (feature_name != NULL, FALSE);
1178 GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1180 registry = gst_registry_get_default ();
1181 feature = gst_registry_lookup_feature (registry, feature_name);
1183 ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1185 gst_object_unref (feature);
1187 GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1194 load_plugin_func (gpointer data, gpointer user_data)
1197 const gchar *filename;
1200 filename = (const gchar *) data;
1201 GST_DEBUG ("Pre-loading plugin %s", filename);
1203 plugin = gst_plugin_load_file (filename, &err);
1206 GST_INFO ("Loaded plugin: \"%s\"", filename);
1208 gst_default_registry_add_plugin (plugin);
1211 /* Report error to user, and free error */
1212 GST_ERROR ("Failed to load plugin: %s", err->message);
1215 GST_WARNING ("Failed to load plugin: \"%s\"", filename);
1220 #ifndef GST_DISABLE_REGISTRY
1221 /* Unref all plugins marked 'cached', to clear old plugins that no
1222 * longer exist. Returns TRUE if any plugins were removed */
1224 gst_registry_remove_cache_plugins (GstRegistry * registry)
1229 gboolean changed = FALSE;
1231 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1233 GST_OBJECT_LOCK (registry);
1235 GST_DEBUG_OBJECT (registry, "removing cached plugins");
1236 g = registry->plugins;
1240 if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
1241 GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
1242 GST_STR_NULL (plugin->filename));
1243 registry->plugins = g_list_delete_link (registry->plugins, g);
1244 gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
1245 gst_object_unref (plugin);
1251 GST_OBJECT_UNLOCK (registry);
1258 REGISTRY_SCAN_AND_UPDATE_FAILURE = 0,
1259 REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED,
1260 REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED
1261 } GstRegistryScanAndUpdateResult;
1264 * scan_and_update_registry:
1265 * @default_registry: the #GstRegistry
1266 * @registry_file: registry filename
1267 * @write_changes: write registry if it has changed?
1269 * Scans for registry changes and eventually updates the registry cache.
1271 * Return: %REGISTRY_SCAN_AND_UPDATE_FAILURE if the registry could not scanned
1272 * or updated, %REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED if the
1273 * registry is clean and %REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED if
1274 * it has been updated and the cache needs to be re-read.
1276 static GstRegistryScanAndUpdateResult
1277 scan_and_update_registry (GstRegistry * default_registry,
1278 const gchar * registry_file, gboolean write_changes, GError ** error)
1280 const gchar *plugin_path;
1281 gboolean changed = FALSE;
1283 GstRegistryScanContext context;
1285 GST_INFO ("Validating plugins from registry cache: %s", registry_file);
1287 init_scan_context (&context, default_registry);
1289 /* It sounds tempting to just compare the mtime of directories with the mtime
1290 * of the registry cache, but it does not work. It would not catch updated
1291 * plugins, which might bring more or less features.
1294 /* scan paths specified via --gst-plugin-path */
1295 GST_DEBUG ("scanning paths added via --gst-plugin-path");
1296 for (l = _priv_gst_plugin_paths; l != NULL; l = l->next) {
1297 GST_INFO ("Scanning plugin path: \"%s\"", (gchar *) l->data);
1298 changed |= gst_registry_scan_path_internal (&context, (gchar *) l->data);
1300 /* keep plugin_paths around in case a re-scan is forced later on */
1302 /* GST_PLUGIN_PATH specifies a list of directories to scan for
1303 * additional plugins. These take precedence over the system plugins */
1304 plugin_path = g_getenv ("GST_PLUGIN_PATH");
1309 GST_DEBUG ("GST_PLUGIN_PATH set to %s", plugin_path);
1310 list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1311 for (i = 0; list[i]; i++) {
1312 changed |= gst_registry_scan_path_internal (&context, list[i]);
1316 GST_DEBUG ("GST_PLUGIN_PATH not set");
1319 /* GST_PLUGIN_SYSTEM_PATH specifies a list of plugins that are always
1320 * loaded by default. If not set, this defaults to the system-installed
1321 * path, and the plugins installed in the user's home directory */
1322 plugin_path = g_getenv ("GST_PLUGIN_SYSTEM_PATH");
1323 if (plugin_path == NULL) {
1326 GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH not set");
1328 /* plugins in the user's home directory take precedence over
1329 * system-installed ones */
1330 home_plugins = g_build_filename (g_get_home_dir (),
1331 ".gstreamer-" GST_MAJORMINOR, "plugins", NULL);
1332 GST_DEBUG ("scanning home plugins %s", home_plugins);
1333 changed |= gst_registry_scan_path_internal (&context, home_plugins);
1334 g_free (home_plugins);
1336 /* add the main (installed) library path */
1337 GST_DEBUG ("scanning main plugins %s", PLUGINDIR);
1338 changed |= gst_registry_scan_path_internal (&context, PLUGINDIR);
1346 g_win32_get_package_installation_directory_of_module
1347 (_priv_gst_dll_handle);
1349 dir = g_build_filename (base_dir, "lib", "gstreamer-0.10", NULL);
1350 GST_DEBUG ("scanning DLL dir %s", dir);
1352 changed |= gst_registry_scan_path_internal (&context, dir);
1362 GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH set to %s", plugin_path);
1363 list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1364 for (i = 0; list[i]; i++) {
1365 changed |= gst_registry_scan_path_internal (&context, list[i]);
1370 clear_scan_context (&context);
1371 changed |= context.changed;
1373 /* Remove cached plugins so stale info is cleared. */
1374 changed |= gst_registry_remove_cache_plugins (default_registry);
1377 GST_INFO ("Registry cache has not changed");
1378 return REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED;
1381 if (!write_changes) {
1382 GST_INFO ("Registry cache changed, but writing is disabled. Not writing.");
1383 return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1386 GST_INFO ("Registry cache changed. Writing new registry cache");
1387 if (!gst_registry_binary_write_cache (default_registry, registry_file)) {
1388 g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
1389 _("Error writing registry cache to %s: %s"),
1390 registry_file, g_strerror (errno));
1391 return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1394 GST_INFO ("Registry cache written successfully");
1395 return REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED;
1399 ensure_current_registry (GError ** error)
1401 gchar *registry_file;
1402 GstRegistry *default_registry;
1403 gboolean ret = TRUE;
1404 gboolean do_update = TRUE;
1405 gboolean have_cache = TRUE;
1407 default_registry = gst_registry_get_default ();
1408 registry_file = g_strdup (g_getenv ("GST_REGISTRY"));
1409 if (registry_file == NULL) {
1410 registry_file = g_build_filename (g_get_home_dir (),
1411 ".gstreamer-" GST_MAJORMINOR, "registry." HOST_CPU ".bin", NULL);
1414 if (!_gst_disable_registry_cache) {
1415 GST_INFO ("reading registry cache: %s", registry_file);
1416 have_cache = gst_registry_binary_read_cache (default_registry,
1418 /* Only ever read the registry cache once, then disable it for
1419 * subsequent updates during the program lifetime */
1420 _gst_disable_registry_cache = TRUE;
1424 do_update = !_priv_gst_disable_registry_update;
1426 const gchar *update_env;
1428 if ((update_env = g_getenv ("GST_REGISTRY_UPDATE"))) {
1429 /* do update for any value different from "no" */
1430 do_update = (strcmp (update_env, "no") != 0);
1436 /* now check registry */
1437 GST_DEBUG ("Updating registry cache");
1438 scan_and_update_registry (default_registry, registry_file, TRUE, error);
1440 GST_DEBUG ("Not updating registry cache (disabled)");
1443 g_free (registry_file);
1444 GST_INFO ("registry reading and updating done, result = %d", ret);
1448 #endif /* GST_DISABLE_REGISTRY */
1451 * gst_registry_fork_is_enabled:
1453 * By default GStreamer will perform scanning and rebuilding of the
1454 * registry file using a helper child process.
1456 * Applications might want to disable this behaviour with the
1457 * gst_registry_fork_set_enabled() function, in which case new plugins
1458 * are scanned (and loaded) into the application process.
1460 * Returns: %TRUE if GStreamer will use the child helper process when
1461 * rebuilding the registry.
1466 gst_registry_fork_is_enabled (void)
1468 return _gst_enable_registry_fork;
1472 * gst_registry_fork_set_enabled:
1473 * @enabled: whether rebuilding the registry can use a temporary child helper process.
1475 * Applications might want to disable/enable spawning of a child helper process
1476 * when rebuilding the registry. See gst_registry_fork_is_enabled() for more
1482 gst_registry_fork_set_enabled (gboolean enabled)
1484 _gst_enable_registry_fork = enabled;
1488 * gst_update_registry:
1490 * Forces GStreamer to re-scan its plugin paths and update the default
1493 * Applications will almost never need to call this function, it is only
1494 * useful if the application knows new plugins have been installed (or old
1495 * ones removed) since the start of the application (or, to be precise, the
1496 * first call to gst_init()) and the application wants to make use of any
1497 * newly-installed plugins without restarting the application.
1499 * Applications should assume that the registry update is neither atomic nor
1500 * thread-safe and should therefore not have any dynamic pipelines running
1501 * (including the playbin and decodebin elements) and should also not create
1502 * any elements or access the GStreamer registry while the update is in
1505 * Note that this function may block for a significant amount of time.
1507 * Returns: %TRUE if the registry has been updated successfully (does not
1508 * imply that there were changes), otherwise %FALSE.
1513 gst_update_registry (void)
1517 #ifndef GST_DISABLE_REGISTRY
1520 res = ensure_current_registry (&err);
1522 GST_WARNING ("registry update failed: %s", err->message);
1525 GST_LOG ("registry update succeeded");
1529 GST_WARNING ("registry update failed: %s", "registry disabled");
1531 #endif /* GST_DISABLE_REGISTRY */
1533 if (_priv_gst_preload_plugins) {
1534 GST_DEBUG ("Preloading indicated plugins...");
1535 g_slist_foreach (_priv_gst_preload_plugins, load_plugin_func, NULL);