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_locked (GstRegistry * registry,
156 const char *filename);
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_locked (registry, plugin->filename);
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_locked (GstRegistry * registry, const char *filename)
774 if (G_UNLIKELY (filename == NULL))
777 basename = g_path_get_basename (filename);
778 /* FIXME: use GTree speed up lookups */
779 for (g = registry->plugins; g; g = g_list_next (g)) {
780 plugin = GST_PLUGIN_CAST (g->data);
781 if (G_UNLIKELY (plugin->basename
782 && strcmp (basename, plugin->basename) == 0)) {
793 * gst_registry_lookup:
794 * @registry: the registry to look up in
795 * @filename: the name of the file to look up
797 * Look up a plugin in the given registry with the given filename.
798 * If found, plugin is reffed.
800 * Returns: the #GstPlugin if found, or NULL if not. gst_object_unref()
804 gst_registry_lookup (GstRegistry * registry, const char *filename)
808 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
809 g_return_val_if_fail (filename != NULL, NULL);
811 GST_OBJECT_LOCK (registry);
812 plugin = gst_registry_lookup_locked (registry, filename);
814 gst_object_ref (plugin);
815 GST_OBJECT_UNLOCK (registry);
822 REGISTRY_SCAN_HELPER_NOT_STARTED = 0,
823 REGISTRY_SCAN_HELPER_DISABLED,
824 REGISTRY_SCAN_HELPER_RUNNING
825 } GstRegistryScanHelperState;
829 GstRegistry *registry;
830 GstRegistryScanHelperState helper_state;
831 GstPluginLoader *helper;
833 } GstRegistryScanContext;
836 init_scan_context (GstRegistryScanContext * context, GstRegistry * registry)
840 context->registry = registry;
842 /* see if forking is enabled and set up the scan helper state accordingly */
843 do_fork = _gst_enable_registry_fork;
845 const gchar *fork_env;
847 /* forking enabled, see if it is disabled with an env var */
848 if ((fork_env = g_getenv ("GST_REGISTRY_FORK"))) {
849 /* fork enabled for any value different from "no" */
850 do_fork = strcmp (fork_env, "no") != 0;
855 context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
857 context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
859 context->helper = NULL;
860 context->changed = FALSE;
864 clear_scan_context (GstRegistryScanContext * context)
866 if (context->helper) {
867 context->changed |= _priv_gst_plugin_loader_funcs.destroy (context->helper);
868 context->helper = NULL;
873 gst_registry_scan_plugin_file (GstRegistryScanContext * context,
874 const gchar * filename, off_t file_size, time_t file_mtime)
876 gboolean changed = FALSE;
877 GstPlugin *newplugin = NULL;
879 /* Have a plugin to load - see if the scan-helper needs starting */
880 if (context->helper_state == REGISTRY_SCAN_HELPER_NOT_STARTED) {
881 GST_DEBUG ("Starting plugin scanner for file %s", filename);
882 context->helper = _priv_gst_plugin_loader_funcs.create (context->registry);
883 if (context->helper != NULL)
884 context->helper_state = REGISTRY_SCAN_HELPER_RUNNING;
886 context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
889 if (context->helper_state == REGISTRY_SCAN_HELPER_RUNNING) {
890 GST_DEBUG ("Using scan-helper to load plugin %s", filename);
891 if (!_priv_gst_plugin_loader_funcs.load (context->helper,
892 filename, file_size, file_mtime)) {
893 g_warning ("External plugin loader failed...");
894 context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
897 /* Load plugin the old fashioned way... */
899 /* We don't use a GError here because a failure to load some shared
900 * objects as plugins is normal (particularly in the uninstalled case)
902 newplugin = gst_plugin_load_file (filename, NULL);
906 GST_DEBUG_OBJECT (context->registry, "marking new plugin %p as registered",
908 newplugin->registered = TRUE;
909 gst_object_unref (newplugin);
917 gst_registry_scan_path_level (GstRegistryScanContext * context,
918 const gchar * path, int level)
924 gboolean changed = FALSE;
926 dir = g_dir_open (path, 0, NULL);
930 while ((dirent = g_dir_read_name (dir))) {
931 struct stat file_status;
933 filename = g_build_filename (path, dirent, NULL);
934 if (g_stat (filename, &file_status) < 0) {
935 /* Plugin will be removed from cache after the scan completes if it
936 * is still marked 'cached' */
941 if (file_status.st_mode & S_IFDIR) {
942 /* skip the .debug directory, these contain elf files that are not
943 * useful or worse, can crash dlopen () */
944 if (g_str_equal (dirent, ".debug") || g_str_equal (dirent, ".git")) {
945 GST_LOG_OBJECT (context->registry, "ignoring .debug or .git directory");
949 /* FIXME 0.11: Don't recurse into directories, this behaviour
950 * is inconsistent with other PATH environment variables
953 GST_LOG_OBJECT (context->registry, "recursing into directory %s",
955 changed |= gst_registry_scan_path_level (context, filename, level - 1);
957 GST_LOG_OBJECT (context->registry, "not recursing into directory %s, "
958 "recursion level too deep", filename);
963 if (!(file_status.st_mode & S_IFREG)) {
964 GST_LOG_OBJECT (context->registry, "%s is not a regular file, ignoring",
969 if (!g_str_has_suffix (dirent, G_MODULE_SUFFIX)
970 #ifdef GST_EXTRA_MODULE_SUFFIX
971 && !g_str_has_suffix (dirent, GST_EXTRA_MODULE_SUFFIX)
974 GST_LOG_OBJECT (context->registry,
975 "extension is not recognized as module file, ignoring file %s",
981 GST_LOG_OBJECT (context->registry, "file %s looks like a possible module",
984 /* plug-ins are considered unique by basename; if the given name
985 * was already seen by the registry, we ignore it */
986 plugin = gst_registry_lookup (context->registry, filename);
988 gboolean env_vars_changed, deps_changed = FALSE;
990 if (plugin->registered) {
991 GST_DEBUG_OBJECT (context->registry,
992 "plugin already registered from path \"%s\"",
993 GST_STR_NULL (plugin->filename));
995 gst_object_unref (plugin);
999 env_vars_changed = _priv_plugin_deps_env_vars_changed (plugin);
1001 /* If a file with a certain basename is seen on a different path,
1002 * update the plugin to ensure the registry cache will reflect up
1003 * to date information */
1005 if (plugin->file_mtime == file_status.st_mtime &&
1006 plugin->file_size == file_status.st_size && !env_vars_changed &&
1007 !(deps_changed = _priv_plugin_deps_files_changed (plugin)) &&
1008 !strcmp (plugin->filename, filename)) {
1009 GST_LOG_OBJECT (context->registry, "file %s cached", filename);
1010 plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
1011 GST_LOG_OBJECT (context->registry,
1012 "marking plugin %p as registered as %s", plugin, filename);
1013 plugin->registered = TRUE;
1015 GST_INFO_OBJECT (context->registry, "cached info for %s is stale",
1017 GST_DEBUG_OBJECT (context->registry, "mtime %ld != %ld or size %"
1018 G_GINT64_FORMAT " != %" G_GINT64_FORMAT " or external dependency "
1019 "env_vars changed: %d or external dependencies changed: %d"
1020 " or old path %s != new path %s",
1021 plugin->file_mtime, file_status.st_mtime,
1022 (gint64) plugin->file_size, (gint64) file_status.st_size,
1023 env_vars_changed, deps_changed, plugin->filename, filename);
1024 gst_registry_remove_plugin (context->registry, plugin);
1025 changed |= gst_registry_scan_plugin_file (context, filename,
1026 file_status.st_size, file_status.st_mtime);
1028 gst_object_unref (plugin);
1031 GST_DEBUG_OBJECT (context->registry, "file %s not yet in registry",
1033 changed |= gst_registry_scan_plugin_file (context, filename,
1034 file_status.st_size, file_status.st_mtime);
1046 gst_registry_scan_path_internal (GstRegistryScanContext * context,
1051 GST_DEBUG_OBJECT (context->registry, "scanning path %s", path);
1052 changed = gst_registry_scan_path_level (context, path, 10);
1054 GST_DEBUG_OBJECT (context->registry, "registry changed in path %s: %d", path,
1060 * gst_registry_scan_path:
1061 * @registry: the registry to add found plugins to
1062 * @path: the path to scan
1064 * Scan the given path for plugins to add to the registry. The syntax of the
1065 * path is specific to the registry.
1067 * Returns: %TRUE if registry changed
1070 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
1072 GstRegistryScanContext context;
1075 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1076 g_return_val_if_fail (path != NULL, FALSE);
1078 init_scan_context (&context, registry);
1080 result = gst_registry_scan_path_internal (&context, path);
1082 clear_scan_context (&context);
1083 result |= context.changed;
1089 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
1092 return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
1096 * gst_registry_get_feature_list_by_plugin:
1097 * @registry: a #GstRegistry.
1098 * @name: a plugin name.
1100 * Retrieves a #GList of features of the plugin with name @name.
1102 * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
1106 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
1109 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1110 g_return_val_if_fail (name != NULL, NULL);
1112 return gst_registry_feature_filter (registry,
1113 _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1116 /* Unref and delete the default registry */
1118 _priv_gst_registry_cleanup ()
1120 GstRegistry *registry;
1122 g_static_mutex_lock (&_gst_registry_mutex);
1123 if ((registry = _gst_registry_default) != NULL) {
1124 _gst_registry_default = NULL;
1126 g_static_mutex_unlock (&_gst_registry_mutex);
1128 /* unref outside of the lock because we can. */
1130 gst_object_unref (registry);
1134 * gst_default_registry_check_feature_version:
1135 * @feature_name: the name of the feature (e.g. "oggdemux")
1136 * @min_major: the minimum major version number
1137 * @min_minor: the minimum minor version number
1138 * @min_micro: the minimum micro version number
1140 * Checks whether a plugin feature by the given name exists in the
1141 * default registry and whether its version is at least the
1144 * Returns: #TRUE if the feature could be found and the version is
1145 * the same as the required version or newer, and #FALSE otherwise.
1148 gst_default_registry_check_feature_version (const gchar * feature_name,
1149 guint min_major, guint min_minor, guint min_micro)
1151 GstPluginFeature *feature;
1152 GstRegistry *registry;
1153 gboolean ret = FALSE;
1155 g_return_val_if_fail (feature_name != NULL, FALSE);
1157 GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1159 registry = gst_registry_get_default ();
1160 feature = gst_registry_lookup_feature (registry, feature_name);
1162 ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1164 gst_object_unref (feature);
1166 GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1173 load_plugin_func (gpointer data, gpointer user_data)
1176 const gchar *filename;
1179 filename = (const gchar *) data;
1180 GST_DEBUG ("Pre-loading plugin %s", filename);
1182 plugin = gst_plugin_load_file (filename, &err);
1185 GST_INFO ("Loaded plugin: \"%s\"", filename);
1187 gst_default_registry_add_plugin (plugin);
1190 /* Report error to user, and free error */
1191 GST_ERROR ("Failed to load plugin: %s", err->message);
1194 GST_WARNING ("Failed to load plugin: \"%s\"", filename);
1199 #ifndef GST_DISABLE_REGISTRY
1200 /* Unref all plugins marked 'cached', to clear old plugins that no
1201 * longer exist. Returns TRUE if any plugins were removed */
1203 gst_registry_remove_cache_plugins (GstRegistry * registry)
1208 gboolean changed = FALSE;
1210 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1212 GST_OBJECT_LOCK (registry);
1214 GST_DEBUG_OBJECT (registry, "removing cached plugins");
1215 g = registry->plugins;
1219 if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
1220 GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
1221 GST_STR_NULL (plugin->filename));
1222 registry->plugins = g_list_delete_link (registry->plugins, g);
1223 gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
1224 gst_object_unref (plugin);
1230 GST_OBJECT_UNLOCK (registry);
1237 REGISTRY_SCAN_AND_UPDATE_FAILURE = 0,
1238 REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED,
1239 REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED
1240 } GstRegistryScanAndUpdateResult;
1243 * scan_and_update_registry:
1244 * @default_registry: the #GstRegistry
1245 * @registry_file: registry filename
1246 * @write_changes: write registry if it has changed?
1248 * Scans for registry changes and eventually updates the registry cache.
1250 * Return: %REGISTRY_SCAN_AND_UPDATE_FAILURE if the registry could not scanned
1251 * or updated, %REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED if the
1252 * registry is clean and %REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED if
1253 * it has been updated and the cache needs to be re-read.
1255 static GstRegistryScanAndUpdateResult
1256 scan_and_update_registry (GstRegistry * default_registry,
1257 const gchar * registry_file, gboolean write_changes, GError ** error)
1259 const gchar *plugin_path;
1260 gboolean changed = FALSE;
1262 GstRegistryScanContext context;
1264 GST_INFO ("Validating plugins from registry cache: %s", registry_file);
1266 init_scan_context (&context, default_registry);
1268 /* It sounds tempting to just compare the mtime of directories with the mtime
1269 * of the registry cache, but it does not work. It would not catch updated
1270 * plugins, which might bring more or less features.
1273 /* scan paths specified via --gst-plugin-path */
1274 GST_DEBUG ("scanning paths added via --gst-plugin-path");
1275 for (l = _priv_gst_plugin_paths; l != NULL; l = l->next) {
1276 GST_INFO ("Scanning plugin path: \"%s\"", (gchar *) l->data);
1277 changed |= gst_registry_scan_path_internal (&context, (gchar *) l->data);
1279 /* keep plugin_paths around in case a re-scan is forced later on */
1281 /* GST_PLUGIN_PATH specifies a list of directories to scan for
1282 * additional plugins. These take precedence over the system plugins */
1283 plugin_path = g_getenv ("GST_PLUGIN_PATH");
1288 GST_DEBUG ("GST_PLUGIN_PATH set to %s", plugin_path);
1289 list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1290 for (i = 0; list[i]; i++) {
1291 changed |= gst_registry_scan_path_internal (&context, list[i]);
1295 GST_DEBUG ("GST_PLUGIN_PATH not set");
1298 /* GST_PLUGIN_SYSTEM_PATH specifies a list of plugins that are always
1299 * loaded by default. If not set, this defaults to the system-installed
1300 * path, and the plugins installed in the user's home directory */
1301 plugin_path = g_getenv ("GST_PLUGIN_SYSTEM_PATH");
1302 if (plugin_path == NULL) {
1305 GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH not set");
1307 /* plugins in the user's home directory take precedence over
1308 * system-installed ones */
1309 home_plugins = g_build_filename (g_get_home_dir (),
1310 ".gstreamer-" GST_MAJORMINOR, "plugins", NULL);
1311 GST_DEBUG ("scanning home plugins %s", home_plugins);
1312 changed |= gst_registry_scan_path_internal (&context, home_plugins);
1313 g_free (home_plugins);
1315 /* add the main (installed) library path */
1316 GST_DEBUG ("scanning main plugins %s", PLUGINDIR);
1317 changed |= gst_registry_scan_path_internal (&context, PLUGINDIR);
1325 g_win32_get_package_installation_directory_of_module
1326 (_priv_gst_dll_handle);
1328 dir = g_build_filename (base_dir, "lib", "gstreamer-0.10", NULL);
1329 GST_DEBUG ("scanning DLL dir %s", dir);
1331 changed |= gst_registry_scan_path_internal (&context, dir);
1341 GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH set to %s", plugin_path);
1342 list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1343 for (i = 0; list[i]; i++) {
1344 changed |= gst_registry_scan_path_internal (&context, list[i]);
1349 clear_scan_context (&context);
1350 changed |= context.changed;
1352 /* Remove cached plugins so stale info is cleared. */
1353 changed |= gst_registry_remove_cache_plugins (default_registry);
1356 GST_INFO ("Registry cache has not changed");
1357 return REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED;
1360 if (!write_changes) {
1361 GST_INFO ("Registry cache changed, but writing is disabled. Not writing.");
1362 return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1365 GST_INFO ("Registry cache changed. Writing new registry cache");
1366 if (!gst_registry_binary_write_cache (default_registry, registry_file)) {
1367 g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
1368 _("Error writing registry cache to %s: %s"),
1369 registry_file, g_strerror (errno));
1370 return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1373 GST_INFO ("Registry cache written successfully");
1374 return REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED;
1378 ensure_current_registry (GError ** error)
1380 gchar *registry_file;
1381 GstRegistry *default_registry;
1382 gboolean ret = TRUE;
1383 gboolean do_update = TRUE;
1384 gboolean have_cache = TRUE;
1386 default_registry = gst_registry_get_default ();
1387 registry_file = g_strdup (g_getenv ("GST_REGISTRY"));
1388 if (registry_file == NULL) {
1389 registry_file = g_build_filename (g_get_home_dir (),
1390 ".gstreamer-" GST_MAJORMINOR, "registry." HOST_CPU ".bin", NULL);
1393 if (!_gst_disable_registry_cache) {
1394 GST_INFO ("reading registry cache: %s", registry_file);
1395 have_cache = gst_registry_binary_read_cache (default_registry,
1397 /* Only ever read the registry cache once, then disable it for
1398 * subsequent updates during the program lifetime */
1399 _gst_disable_registry_cache = TRUE;
1403 do_update = !_priv_gst_disable_registry_update;
1405 const gchar *update_env;
1407 if ((update_env = g_getenv ("GST_REGISTRY_UPDATE"))) {
1408 /* do update for any value different from "no" */
1409 do_update = (strcmp (update_env, "no") != 0);
1415 /* now check registry */
1416 GST_DEBUG ("Updating registry cache");
1417 scan_and_update_registry (default_registry, registry_file, TRUE, error);
1419 GST_DEBUG ("Not updating registry cache (disabled)");
1422 g_free (registry_file);
1423 GST_INFO ("registry reading and updating done, result = %d", ret);
1427 #endif /* GST_DISABLE_REGISTRY */
1430 * gst_registry_fork_is_enabled:
1432 * By default GStreamer will perform scanning and rebuilding of the
1433 * registry file using a helper child process.
1435 * Applications might want to disable this behaviour with the
1436 * gst_registry_fork_set_enabled() function, in which case new plugins
1437 * are scanned (and loaded) into the application process.
1439 * Returns: %TRUE if GStreamer will use the child helper process when
1440 * rebuilding the registry.
1445 gst_registry_fork_is_enabled (void)
1447 return _gst_enable_registry_fork;
1451 * gst_registry_fork_set_enabled:
1452 * @enabled: whether rebuilding the registry can use a temporary child helper process.
1454 * Applications might want to disable/enable spawning of a child helper process
1455 * when rebuilding the registry. See gst_registry_fork_is_enabled() for more
1461 gst_registry_fork_set_enabled (gboolean enabled)
1463 _gst_enable_registry_fork = enabled;
1467 * gst_update_registry:
1469 * Forces GStreamer to re-scan its plugin paths and update the default
1472 * Applications will almost never need to call this function, it is only
1473 * useful if the application knows new plugins have been installed (or old
1474 * ones removed) since the start of the application (or, to be precise, the
1475 * first call to gst_init()) and the application wants to make use of any
1476 * newly-installed plugins without restarting the application.
1478 * Applications should assume that the registry update is neither atomic nor
1479 * thread-safe and should therefore not have any dynamic pipelines running
1480 * (including the playbin and decodebin elements) and should also not create
1481 * any elements or access the GStreamer registry while the update is in
1484 * Note that this function may block for a significant amount of time.
1486 * Returns: %TRUE if the registry has been updated successfully (does not
1487 * imply that there were changes), otherwise %FALSE.
1492 gst_update_registry (void)
1496 #ifndef GST_DISABLE_REGISTRY
1499 res = ensure_current_registry (&err);
1501 GST_WARNING ("registry update failed: %s", err->message);
1504 GST_LOG ("registry update succeeded");
1508 GST_WARNING ("registry update failed: %s", "registry disabled");
1510 #endif /* GST_DISABLE_REGISTRY */
1512 if (_priv_gst_preload_plugins) {
1513 GST_DEBUG ("Preloading indicated plugins...");
1514 g_slist_foreach (_priv_gst_preload_plugins, load_plugin_func, NULL);