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 *
31 * <emphasis role="bold">Design:</emphasis>
32 *
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
37 * core.
38 *
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.
42 *
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.
49 *
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.
55 *
56 * For each plugin that is found in the plugin search path, there could be 3
57 * possibilities for cached information:
58 * <itemizedlist>
59 * <listitem>
60 * <para>the cache may not contain information about a given file.</para>
61 * </listitem>
62 * <listitem>
63 * <para>the cache may have stale information.</para>
64 * </listitem>
65 * <listitem>
66 * <para>the cache may have current information.</para>
67 * </listitem>
68 * </itemizedlist>
69 *
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
74 * dirty.
75 *
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
78 * simply dropped.
79 *
80 * <emphasis role="bold">Implementation notes:</emphasis>
81 *
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.
87 */
89 #ifdef HAVE_CONFIG_H
90 #include "config.h"
91 #endif
92 #include "gstconfig.h"
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>
104 /* For g_stat () */
105 #include <glib/gstdio.h>
107 #include "gstinfo.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 /* needed for fast retrieval of element and typefind factory lists */
118 extern GType gst_type_find_factory_get_type (void);
119 #define GST_TYPE_TYPE_FIND_FACTORY (gst_type_find_factory_get_type())
120 extern GType gst_element_factory_get_type (void);
121 #define GST_TYPE_ELEMENT_FACTORY (gst_element_factory_get_type())
123 #ifdef G_OS_WIN32
124 #include <windows.h>
125 extern HMODULE _priv_gst_dll_handle;
126 #endif
128 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
130 struct _GstRegistryPrivate
131 {
132 /* updated whenever the feature list changes */
133 guint32 cookie;
134 /* speedup for searching features */
135 GList *element_factory_list;
136 guint32 efl_cookie;
137 GList *typefind_factory_list;
138 guint32 tfl_cookie;
139 };
141 /* the one instance of the default registry and the mutex protecting the
142 * variable. */
143 static GStaticMutex _gst_registry_mutex = G_STATIC_MUTEX_INIT;
144 static GstRegistry *_gst_registry_default = NULL;
146 /* defaults */
147 #define DEFAULT_FORK TRUE
149 /* control the behaviour of registry rebuild */
150 static gboolean _gst_enable_registry_fork = DEFAULT_FORK;
151 /* List of plugins that need preloading/reloading after scanning registry */
152 extern GSList *_priv_gst_preload_plugins;
154 #ifndef GST_DISABLE_REGISTRY
155 /*set to TRUE when registry needn't to be updated */
156 gboolean _priv_gst_disable_registry_update = FALSE;
157 extern GList *_priv_gst_plugin_paths;
159 /* Set to TRUE when the registry cache should be disabled */
160 gboolean _gst_disable_registry_cache = FALSE;
161 #endif
163 /* Element signals and args */
164 enum
165 {
166 PLUGIN_ADDED,
167 FEATURE_ADDED,
168 LAST_SIGNAL
169 };
171 static void gst_registry_finalize (GObject * object);
173 static guint gst_registry_signals[LAST_SIGNAL] = { 0 };
175 static GstPluginFeature *gst_registry_lookup_feature_locked (GstRegistry *
176 registry, const char *name);
177 static GstPlugin *gst_registry_lookup_bn_locked (GstRegistry * registry,
178 const char *basename);
180 G_DEFINE_TYPE (GstRegistry, gst_registry, GST_TYPE_OBJECT);
181 static GstObjectClass *parent_class = NULL;
183 static void
184 gst_registry_class_init (GstRegistryClass * klass)
185 {
186 GObjectClass *gobject_class;
188 gobject_class = (GObjectClass *) klass;
190 parent_class = g_type_class_peek_parent (klass);
191 g_type_class_add_private (klass, sizeof (GstRegistryPrivate));
193 /**
194 * GstRegistry::plugin-added:
195 * @registry: the registry that emitted the signal
196 * @plugin: the plugin that has been added
197 *
198 * Signals that a plugin has been added to the registry (possibly
199 * replacing a previously-added one by the same name)
200 */
201 gst_registry_signals[PLUGIN_ADDED] =
202 g_signal_new ("plugin-added", G_TYPE_FROM_CLASS (klass),
203 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, plugin_added), NULL,
204 NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
206 /**
207 * GstRegistry::feature-added:
208 * @registry: the registry that emitted the signal
209 * @feature: the feature that has been added
210 *
211 * Signals that a feature has been added to the registry (possibly
212 * replacing a previously-added one by the same name)
213 */
214 gst_registry_signals[FEATURE_ADDED] =
215 g_signal_new ("feature-added", G_TYPE_FROM_CLASS (klass),
216 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRegistryClass, feature_added),
217 NULL, NULL, gst_marshal_VOID__POINTER, G_TYPE_NONE, 1, G_TYPE_POINTER);
219 gobject_class->finalize = gst_registry_finalize;
220 }
222 static void
223 gst_registry_init (GstRegistry * registry)
224 {
225 registry->feature_hash = g_hash_table_new (g_str_hash, g_str_equal);
226 registry->basename_hash = g_hash_table_new (g_str_hash, g_str_equal);
227 registry->priv =
228 G_TYPE_INSTANCE_GET_PRIVATE (registry, GST_TYPE_REGISTRY,
229 GstRegistryPrivate);
230 }
232 static void
233 gst_registry_finalize (GObject * object)
234 {
235 GstRegistry *registry = GST_REGISTRY (object);
236 GList *plugins, *p;
237 GList *features, *f;
239 plugins = registry->plugins;
240 registry->plugins = NULL;
242 GST_DEBUG_OBJECT (registry, "registry finalize");
243 p = plugins;
244 while (p) {
245 GstPlugin *plugin = p->data;
247 if (plugin) {
248 GST_LOG_OBJECT (registry, "removing plugin %s",
249 gst_plugin_get_name (plugin));
250 gst_object_unref (plugin);
251 }
252 p = g_list_next (p);
253 }
254 g_list_free (plugins);
256 features = registry->features;
257 registry->features = NULL;
259 f = features;
260 while (f) {
261 GstPluginFeature *feature = f->data;
263 if (feature) {
264 GST_LOG_OBJECT (registry, "removing feature %p (%s)",
265 feature, gst_plugin_feature_get_name (feature));
266 gst_object_unref (feature);
267 }
268 f = g_list_next (f);
269 }
270 g_list_free (features);
272 g_hash_table_destroy (registry->feature_hash);
273 registry->feature_hash = NULL;
274 g_hash_table_destroy (registry->basename_hash);
275 registry->basename_hash = NULL;
277 if (registry->priv->element_factory_list) {
278 GST_DEBUG_OBJECT (registry, "Cleaning up cached element factory list");
279 gst_plugin_feature_list_free (registry->priv->element_factory_list);
280 }
282 if (registry->priv->typefind_factory_list) {
283 GST_DEBUG_OBJECT (registry, "Cleaning up cached typefind factory list");
284 gst_plugin_feature_list_free (registry->priv->typefind_factory_list);
285 }
287 G_OBJECT_CLASS (parent_class)->finalize (object);
288 }
290 /**
291 * gst_registry_get_default:
292 *
293 * Retrieves the default registry. The caller does not own a reference on the
294 * registry, as it is alive as long as GStreamer is initialized.
295 *
296 * Returns: The default #GstRegistry.
297 */
298 GstRegistry *
299 gst_registry_get_default (void)
300 {
301 GstRegistry *registry;
303 g_static_mutex_lock (&_gst_registry_mutex);
304 if (G_UNLIKELY (!_gst_registry_default)) {
305 _gst_registry_default = g_object_newv (GST_TYPE_REGISTRY, 0, NULL);
306 gst_object_ref_sink (GST_OBJECT_CAST (_gst_registry_default));
307 }
308 registry = _gst_registry_default;
309 g_static_mutex_unlock (&_gst_registry_mutex);
311 return registry;
312 }
314 /**
315 * gst_registry_add_path:
316 * @registry: the registry to add the path to
317 * @path: the path to add to the registry
318 *
319 * Add the given path to the registry. The syntax of the
320 * path is specific to the registry. If the path has already been
321 * added, do nothing.
322 */
323 void
324 gst_registry_add_path (GstRegistry * registry, const gchar * path)
325 {
326 g_return_if_fail (GST_IS_REGISTRY (registry));
327 g_return_if_fail (path != NULL);
329 if (strlen (path) == 0)
330 goto empty_path;
332 GST_OBJECT_LOCK (registry);
333 if (g_list_find_custom (registry->paths, path, (GCompareFunc) strcmp))
334 goto was_added;
336 GST_INFO ("Adding plugin path: \"%s\"", path);
337 registry->paths = g_list_append (registry->paths, g_strdup (path));
338 GST_OBJECT_UNLOCK (registry);
340 return;
342 empty_path:
343 {
344 GST_INFO ("Ignoring empty plugin path");
345 return;
346 }
347 was_added:
348 {
349 g_warning ("path %s already added to registry", path);
350 GST_OBJECT_UNLOCK (registry);
351 return;
352 }
353 }
355 /**
356 * gst_registry_get_path_list:
357 * @registry: the registry to get the pathlist of
358 *
359 * Get the list of paths for the given registry.
360 *
361 * Returns: A Glist of paths as strings. g_list_free after use.
362 *
363 * MT safe.
364 */
365 GList *
366 gst_registry_get_path_list (GstRegistry * registry)
367 {
368 GList *list;
370 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
372 GST_OBJECT_LOCK (registry);
373 /* We don't need to copy the strings, because they won't be deleted
374 * as long as the GstRegistry is around */
375 list = g_list_copy (registry->paths);
376 GST_OBJECT_UNLOCK (registry);
378 return list;
379 }
382 /**
383 * gst_registry_add_plugin:
384 * @registry: the registry to add the plugin to
385 * @plugin: the plugin to add
386 *
387 * Add the plugin to the registry. The plugin-added signal will be emitted.
388 * This function will sink @plugin.
389 *
390 * Returns: TRUE on success.
391 *
392 * MT safe.
393 */
394 gboolean
395 gst_registry_add_plugin (GstRegistry * registry, GstPlugin * plugin)
396 {
397 GstPlugin *existing_plugin;
399 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
400 g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
402 GST_OBJECT_LOCK (registry);
403 if (G_LIKELY (plugin->basename)) {
404 /* we have a basename, see if we find the plugin */
405 existing_plugin =
406 gst_registry_lookup_bn_locked (registry, plugin->basename);
407 if (existing_plugin) {
408 GST_DEBUG_OBJECT (registry,
409 "Replacing existing plugin %p with new plugin %p for filename \"%s\"",
410 existing_plugin, plugin, GST_STR_NULL (plugin->filename));
411 registry->plugins = g_list_remove (registry->plugins, existing_plugin);
412 if (G_LIKELY (existing_plugin->basename))
413 g_hash_table_remove (registry->basename_hash,
414 existing_plugin->basename);
415 gst_object_unref (existing_plugin);
416 }
417 }
419 GST_DEBUG_OBJECT (registry, "adding plugin %p for filename \"%s\"",
420 plugin, GST_STR_NULL (plugin->filename));
422 registry->plugins = g_list_prepend (registry->plugins, plugin);
423 if (G_LIKELY (plugin->basename))
424 g_hash_table_replace (registry->basename_hash, plugin->basename, plugin);
426 gst_object_ref_sink (plugin);
427 GST_OBJECT_UNLOCK (registry);
429 GST_LOG_OBJECT (registry, "emitting plugin-added for filename \"%s\"",
430 GST_STR_NULL (plugin->filename));
431 g_signal_emit (registry, gst_registry_signals[PLUGIN_ADDED], 0, plugin);
433 return TRUE;
434 }
436 static void
437 gst_registry_remove_features_for_plugin_unlocked (GstRegistry * registry,
438 GstPlugin * plugin)
439 {
440 GList *f;
441 const gchar *name;
443 g_return_if_fail (GST_IS_REGISTRY (registry));
444 g_return_if_fail (GST_IS_PLUGIN (plugin));
446 name = gst_plugin_get_name (plugin);
448 /* Remove all features for this plugin */
449 f = registry->features;
450 while (f != NULL) {
451 GList *next = g_list_next (f);
452 GstPluginFeature *feature = f->data;
454 if (G_UNLIKELY (feature && !strcmp (feature->plugin_name, name))) {
455 GST_DEBUG_OBJECT (registry, "removing feature %p (%s) for plugin %s",
456 feature, gst_plugin_feature_get_name (feature), name);
458 registry->features = g_list_delete_link (registry->features, f);
459 g_hash_table_remove (registry->feature_hash, feature->name);
460 gst_object_unref (feature);
461 }
462 f = next;
463 }
464 registry->priv->cookie++;
465 }
467 /**
468 * gst_registry_remove_plugin:
469 * @registry: the registry to remove the plugin from
470 * @plugin: the plugin to remove
471 *
472 * Remove the plugin from the registry.
473 *
474 * MT safe.
475 */
476 void
477 gst_registry_remove_plugin (GstRegistry * registry, GstPlugin * plugin)
478 {
479 g_return_if_fail (GST_IS_REGISTRY (registry));
480 g_return_if_fail (GST_IS_PLUGIN (plugin));
482 GST_DEBUG_OBJECT (registry, "removing plugin %p (%s)",
483 plugin, gst_plugin_get_name (plugin));
485 GST_OBJECT_LOCK (registry);
486 registry->plugins = g_list_remove (registry->plugins, plugin);
487 if (G_LIKELY (plugin->basename))
488 g_hash_table_remove (registry->basename_hash, plugin->basename);
489 gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
490 GST_OBJECT_UNLOCK (registry);
491 gst_object_unref (plugin);
492 }
494 /**
495 * gst_registry_add_feature:
496 * @registry: the registry to add the plugin to
497 * @feature: the feature to add
498 *
499 * Add the feature to the registry. The feature-added signal will be emitted.
500 * This function sinks @feature.
501 *
502 * Returns: TRUE on success.
503 *
504 * MT safe.
505 */
506 gboolean
507 gst_registry_add_feature (GstRegistry * registry, GstPluginFeature * feature)
508 {
509 GstPluginFeature *existing_feature;
511 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
512 g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
513 g_return_val_if_fail (feature->name != NULL, FALSE);
514 g_return_val_if_fail (feature->plugin_name != NULL, FALSE);
516 GST_OBJECT_LOCK (registry);
517 existing_feature = gst_registry_lookup_feature_locked (registry,
518 feature->name);
519 if (G_UNLIKELY (existing_feature)) {
520 GST_DEBUG_OBJECT (registry, "replacing existing feature %p (%s)",
521 existing_feature, feature->name);
522 /* Remove the existing feature from the list now, before we insert the new
523 * one, but don't unref yet because the hash is still storing a reference to
524 * it. */
525 registry->features = g_list_remove (registry->features, existing_feature);
526 }
528 GST_DEBUG_OBJECT (registry, "adding feature %p (%s)", feature, feature->name);
530 registry->features = g_list_prepend (registry->features, feature);
531 g_hash_table_replace (registry->feature_hash, feature->name, feature);
533 if (G_UNLIKELY (existing_feature)) {
534 /* We unref now. No need to remove the feature name from the hash table, it
535 * got replaced by the new feature */
536 gst_object_unref (existing_feature);
537 }
539 gst_object_ref_sink (feature);
541 registry->priv->cookie++;
542 GST_OBJECT_UNLOCK (registry);
544 GST_LOG_OBJECT (registry, "emitting feature-added for %s", feature->name);
545 g_signal_emit (registry, gst_registry_signals[FEATURE_ADDED], 0, feature);
547 return TRUE;
548 }
550 /**
551 * gst_registry_remove_feature:
552 * @registry: the registry to remove the feature from
553 * @feature: the feature to remove
554 *
555 * Remove the feature from the registry.
556 *
557 * MT safe.
558 */
559 void
560 gst_registry_remove_feature (GstRegistry * registry, GstPluginFeature * feature)
561 {
562 g_return_if_fail (GST_IS_REGISTRY (registry));
563 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
565 GST_DEBUG_OBJECT (registry, "removing feature %p (%s)",
566 feature, gst_plugin_feature_get_name (feature));
568 GST_OBJECT_LOCK (registry);
569 registry->features = g_list_remove (registry->features, feature);
570 g_hash_table_remove (registry->feature_hash, feature->name);
571 registry->priv->cookie++;
572 GST_OBJECT_UNLOCK (registry);
573 gst_object_unref (feature);
574 }
576 /**
577 * gst_registry_plugin_filter:
578 * @registry: registry to query
579 * @filter: the filter to use
580 * @first: only return first match
581 * @user_data: user data passed to the filter function
582 *
583 * Runs a filter against all plugins in the registry and returns a #GList with
584 * the results. If the first flag is set, only the first match is
585 * returned (as a list with a single object).
586 * Every plugin is reffed; use gst_plugin_list_free() after use, which
587 * will unref again.
588 *
589 * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
590 *
591 * MT safe.
592 */
593 GList *
594 gst_registry_plugin_filter (GstRegistry * registry,
595 GstPluginFilter filter, gboolean first, gpointer user_data)
596 {
597 GList *list;
598 GList *g;
600 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
602 GST_OBJECT_LOCK (registry);
603 list = gst_filter_run (registry->plugins, (GstFilterFunc) filter, first,
604 user_data);
605 for (g = list; g; g = g->next) {
606 gst_object_ref (GST_PLUGIN_CAST (g->data));
607 }
608 GST_OBJECT_UNLOCK (registry);
610 return list;
611 }
613 /* returns TRUE if the list was changed
614 *
615 * Must be called with the object lock taken */
616 static gboolean
617 gst_registry_get_feature_list_or_create (GstRegistry * registry,
618 GList ** previous, guint32 * cookie, GType type)
619 {
620 gboolean res = FALSE;
621 GstRegistryPrivate *priv = registry->priv;
623 if (G_UNLIKELY (!*previous || priv->cookie != *cookie)) {
624 GstTypeNameData data;
626 if (*previous)
627 gst_plugin_feature_list_free (*previous);
629 data.type = type;
630 data.name = NULL;
631 *previous =
632 gst_filter_run (registry->features,
633 (GstFilterFunc) gst_plugin_feature_type_name_filter, FALSE, &data);
634 g_list_foreach (*previous, (GFunc) gst_object_ref, NULL);
635 *cookie = priv->cookie;
636 res = TRUE;
637 }
639 return res;
640 }
642 static gint
643 type_find_factory_rank_cmp (const GstPluginFeature * fac1,
644 const GstPluginFeature * fac2)
645 {
646 if (G_LIKELY (fac1->rank != fac2->rank))
647 return fac2->rank - fac1->rank;
649 /* to make the order in which things happen more deterministic,
650 * sort by name when the ranks are the same. */
651 return strcmp (fac1->name, fac2->name);
652 }
654 static GList *
655 gst_registry_get_element_factory_list (GstRegistry * registry)
656 {
657 GList *list;
659 GST_OBJECT_LOCK (registry);
661 gst_registry_get_feature_list_or_create (registry,
662 ®istry->priv->element_factory_list, ®istry->priv->efl_cookie,
663 GST_TYPE_ELEMENT_FACTORY);
665 /* Return reffed copy */
666 list = gst_plugin_feature_list_copy (registry->priv->element_factory_list);
668 GST_OBJECT_UNLOCK (registry);
670 return list;
671 }
673 static GList *
674 gst_registry_get_typefind_factory_list (GstRegistry * registry)
675 {
676 GList *list;
678 GST_OBJECT_LOCK (registry);
680 if (G_UNLIKELY (gst_registry_get_feature_list_or_create (registry,
681 ®istry->priv->typefind_factory_list,
682 ®istry->priv->tfl_cookie, GST_TYPE_TYPE_FIND_FACTORY)))
683 registry->priv->typefind_factory_list =
684 g_list_sort (registry->priv->typefind_factory_list,
685 (GCompareFunc) type_find_factory_rank_cmp);
687 /* Return reffed copy */
688 list = gst_plugin_feature_list_copy (registry->priv->typefind_factory_list);
690 GST_OBJECT_UNLOCK (registry);
692 return list;
693 }
695 /**
696 * gst_registry_feature_filter:
697 * @registry: registry to query
698 * @filter: the filter to use
699 * @first: only return first match
700 * @user_data: user data passed to the filter function
701 *
702 * Runs a filter against all features of the plugins in the registry
703 * and returns a GList with the results.
704 * If the first flag is set, only the first match is
705 * returned (as a list with a single object).
706 *
707 * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
708 * after usage.
709 *
710 * MT safe.
711 */
712 GList *
713 gst_registry_feature_filter (GstRegistry * registry,
714 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
715 {
716 GList *list;
717 GList *g;
719 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
721 GST_OBJECT_LOCK (registry);
722 list = gst_filter_run (registry->features, (GstFilterFunc) filter, first,
723 user_data);
724 for (g = list; g; g = g->next) {
725 gst_object_ref (GST_PLUGIN_FEATURE_CAST (g->data));
726 }
727 GST_OBJECT_UNLOCK (registry);
729 return list;
730 }
732 /**
733 * gst_registry_find_plugin:
734 * @registry: the registry to search
735 * @name: the plugin name to find
736 *
737 * Find the plugin with the given name in the registry.
738 * The plugin will be reffed; caller is responsible for unreffing.
739 *
740 * Returns: The plugin with the given name or NULL if the plugin was not found.
741 * gst_object_unref() after usage.
742 *
743 * MT safe.
744 */
745 GstPlugin *
746 gst_registry_find_plugin (GstRegistry * registry, const gchar * name)
747 {
748 GList *walk;
749 GstPlugin *result = NULL;
751 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
752 g_return_val_if_fail (name != NULL, NULL);
754 walk = gst_registry_plugin_filter (registry,
755 (GstPluginFilter) gst_plugin_name_filter, TRUE, (gpointer) name);
756 if (walk) {
757 result = GST_PLUGIN_CAST (walk->data);
759 gst_object_ref (result);
760 gst_plugin_list_free (walk);
761 }
763 return result;
764 }
766 /**
767 * gst_registry_find_feature:
768 * @registry: the registry to search
769 * @name: the pluginfeature name to find
770 * @type: the pluginfeature type to find
771 *
772 * Find the pluginfeature with the given name and type in the registry.
773 *
774 * Returns: The pluginfeature with the given name and type or NULL
775 * if the plugin was not found. gst_object_unref() after usage.
776 *
777 * MT safe.
778 */
779 GstPluginFeature *
780 gst_registry_find_feature (GstRegistry * registry, const gchar * name,
781 GType type)
782 {
783 GstPluginFeature *feature = NULL;
785 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
786 g_return_val_if_fail (name != NULL, NULL);
787 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
789 feature = gst_registry_lookup_feature (registry, name);
790 if (feature && !g_type_is_a (G_TYPE_FROM_INSTANCE (feature), type)) {
791 gst_object_unref (feature);
792 feature = NULL;
793 }
795 return feature;
796 }
798 /**
799 * gst_registry_get_feature_list:
800 * @registry: a #GstRegistry
801 * @type: a #GType.
802 *
803 * Retrieves a #GList of #GstPluginFeature of @type.
804 *
805 * Returns: a #GList of #GstPluginFeature of @type. Use
806 * gst_plugin_feature_list_free() after usage.
807 *
808 * MT safe.
809 */
810 GList *
811 gst_registry_get_feature_list (GstRegistry * registry, GType type)
812 {
813 GstTypeNameData data;
815 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
816 g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
818 /* Speed up */
819 if (type == GST_TYPE_ELEMENT_FACTORY)
820 return gst_registry_get_element_factory_list (registry);
821 else if (type == GST_TYPE_TYPE_FIND_FACTORY)
822 return gst_registry_get_typefind_factory_list (registry);
824 data.type = type;
825 data.name = NULL;
827 return gst_registry_feature_filter (registry,
828 (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
829 FALSE, &data);
830 }
832 /**
833 * gst_registry_get_plugin_list:
834 * @registry: the registry to search
835 *
836 * Get a copy of all plugins registered in the given registry. The refcount
837 * of each element in the list in incremented.
838 *
839 * Returns: a #GList of #GstPlugin. Use gst_plugin_list_free() after usage.
840 *
841 * MT safe.
842 */
843 GList *
844 gst_registry_get_plugin_list (GstRegistry * registry)
845 {
846 GList *list;
847 GList *g;
849 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
851 GST_OBJECT_LOCK (registry);
852 list = g_list_copy (registry->plugins);
853 for (g = list; g; g = g->next) {
854 gst_object_ref (GST_PLUGIN_CAST (g->data));
855 }
856 GST_OBJECT_UNLOCK (registry);
858 return list;
859 }
861 static GstPluginFeature *
862 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
863 {
864 return g_hash_table_lookup (registry->feature_hash, name);
865 }
867 /**
868 * gst_registry_lookup_feature:
869 * @registry: a #GstRegistry
870 * @name: a #GstPluginFeature name
871 *
872 * Find a #GstPluginFeature with @name in @registry.
873 *
874 * Returns: a #GstPluginFeature with its refcount incremented, use
875 * gst_object_unref() after usage.
876 *
877 * MT safe.
878 */
879 GstPluginFeature *
880 gst_registry_lookup_feature (GstRegistry * registry, const char *name)
881 {
882 GstPluginFeature *feature;
884 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
885 g_return_val_if_fail (name != NULL, NULL);
887 GST_OBJECT_LOCK (registry);
888 feature = gst_registry_lookup_feature_locked (registry, name);
889 if (feature)
890 gst_object_ref (feature);
891 GST_OBJECT_UNLOCK (registry);
893 return feature;
894 }
896 static GstPlugin *
897 gst_registry_lookup_bn_locked (GstRegistry * registry, const char *basename)
898 {
899 return g_hash_table_lookup (registry->basename_hash, basename);
900 }
902 static GstPlugin *
903 gst_registry_lookup_bn (GstRegistry * registry, const char *basename)
904 {
905 GstPlugin *plugin;
907 GST_OBJECT_LOCK (registry);
908 plugin = gst_registry_lookup_bn_locked (registry, basename);
909 if (plugin)
910 gst_object_ref (plugin);
911 GST_OBJECT_UNLOCK (registry);
913 return plugin;
914 }
916 /**
917 * gst_registry_lookup:
918 * @registry: the registry to look up in
919 * @filename: the name of the file to look up
920 *
921 * Look up a plugin in the given registry with the given filename.
922 * If found, plugin is reffed.
923 *
924 * Returns: the #GstPlugin if found, or NULL if not. gst_object_unref()
925 * after usage.
926 */
927 GstPlugin *
928 gst_registry_lookup (GstRegistry * registry, const char *filename)
929 {
930 GstPlugin *plugin;
931 gchar *basename;
933 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
934 g_return_val_if_fail (filename != NULL, NULL);
936 basename = g_path_get_basename (filename);
937 if (G_UNLIKELY (basename == NULL))
938 return NULL;
940 plugin = gst_registry_lookup_bn (registry, basename);
942 g_free (basename);
944 return plugin;
945 }
947 typedef enum
948 {
949 REGISTRY_SCAN_HELPER_NOT_STARTED = 0,
950 REGISTRY_SCAN_HELPER_DISABLED,
951 REGISTRY_SCAN_HELPER_RUNNING
952 } GstRegistryScanHelperState;
954 typedef struct
955 {
956 GstRegistry *registry;
957 GstRegistryScanHelperState helper_state;
958 GstPluginLoader *helper;
959 gboolean changed;
960 } GstRegistryScanContext;
962 static void
963 init_scan_context (GstRegistryScanContext * context, GstRegistry * registry)
964 {
965 gboolean do_fork;
967 context->registry = registry;
969 /* see if forking is enabled and set up the scan helper state accordingly */
970 do_fork = _gst_enable_registry_fork;
971 if (do_fork) {
972 const gchar *fork_env;
974 /* forking enabled, see if it is disabled with an env var */
975 if ((fork_env = g_getenv ("GST_REGISTRY_FORK"))) {
976 /* fork enabled for any value different from "no" */
977 do_fork = strcmp (fork_env, "no") != 0;
978 }
979 }
981 if (do_fork)
982 context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
983 else
984 context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
986 context->helper = NULL;
987 context->changed = FALSE;
988 }
990 static void
991 clear_scan_context (GstRegistryScanContext * context)
992 {
993 if (context->helper) {
994 context->changed |= _priv_gst_plugin_loader_funcs.destroy (context->helper);
995 context->helper = NULL;
996 }
997 }
999 static gboolean
1000 gst_registry_scan_plugin_file (GstRegistryScanContext * context,
1001 const gchar * filename, off_t file_size, time_t file_mtime)
1002 {
1003 gboolean changed = FALSE;
1004 GstPlugin *newplugin = NULL;
1006 /* Have a plugin to load - see if the scan-helper needs starting */
1007 if (context->helper_state == REGISTRY_SCAN_HELPER_NOT_STARTED) {
1008 GST_DEBUG ("Starting plugin scanner for file %s", filename);
1009 context->helper = _priv_gst_plugin_loader_funcs.create (context->registry);
1010 if (context->helper != NULL)
1011 context->helper_state = REGISTRY_SCAN_HELPER_RUNNING;
1012 else {
1013 GST_WARNING ("Failed starting plugin scanner. Scanning in-process");
1014 context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1015 }
1016 }
1018 if (context->helper_state == REGISTRY_SCAN_HELPER_RUNNING) {
1019 GST_DEBUG ("Using scan-helper to load plugin %s", filename);
1020 if (!_priv_gst_plugin_loader_funcs.load (context->helper,
1021 filename, file_size, file_mtime)) {
1022 g_warning ("External plugin loader failed. This most likely means that "
1023 "the plugin loader helper binary was not found or could not be run. "
1024 "%s", (g_getenv ("GST_PLUGIN_PATH") != NULL) ?
1025 "If you are running an uninstalled GStreamer setup, you might need "
1026 "to update your gst-uninstalled script so that the "
1027 "GST_PLUGIN_SCANNER environment variable gets set." : "");
1028 context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1029 }
1030 }
1032 /* Check if the helper is disabled (or just got disabled above) */
1033 if (context->helper_state == REGISTRY_SCAN_HELPER_DISABLED) {
1034 /* Load plugin the old fashioned way... */
1036 /* We don't use a GError here because a failure to load some shared
1037 * objects as plugins is normal (particularly in the uninstalled case)
1038 */
1039 newplugin = gst_plugin_load_file (filename, NULL);
1040 }
1042 if (newplugin) {
1043 GST_DEBUG_OBJECT (context->registry, "marking new plugin %p as registered",
1044 newplugin);
1045 newplugin->registered = TRUE;
1046 gst_object_unref (newplugin);
1047 changed = TRUE;
1048 }
1050 return changed;
1051 }
1053 static gboolean
1054 gst_registry_scan_path_level (GstRegistryScanContext * context,
1055 const gchar * path, int level)
1056 {
1057 GDir *dir;
1058 const gchar *dirent;
1059 gchar *filename;
1060 GstPlugin *plugin;
1061 gboolean changed = FALSE;
1063 dir = g_dir_open (path, 0, NULL);
1064 if (!dir)
1065 return FALSE;
1067 while ((dirent = g_dir_read_name (dir))) {
1068 struct stat file_status;
1070 filename = g_build_filename (path, dirent, NULL);
1071 if (g_stat (filename, &file_status) < 0) {
1072 /* Plugin will be removed from cache after the scan completes if it
1073 * is still marked 'cached' */
1074 g_free (filename);
1075 continue;
1076 }
1078 if (file_status.st_mode & S_IFDIR) {
1079 /* skip the .debug directory, these contain elf files that are not
1080 * useful or worse, can crash dlopen (). do a quick check for the . first
1081 * and then call the compare functions. */
1082 if (G_UNLIKELY (dirent[0] == '.' && (g_str_equal (dirent, ".debug")
1083 || g_str_equal (dirent, ".git")))) {
1084 GST_LOG_OBJECT (context->registry, "ignoring .debug or .git directory");
1085 g_free (filename);
1086 continue;
1087 }
1088 /* FIXME 0.11: Don't recurse into directories, this behaviour
1089 * is inconsistent with other PATH environment variables
1090 */
1091 if (level > 0) {
1092 GST_LOG_OBJECT (context->registry, "recursing into directory %s",
1093 filename);
1094 changed |= gst_registry_scan_path_level (context, filename, level - 1);
1095 } else {
1096 GST_LOG_OBJECT (context->registry, "not recursing into directory %s, "
1097 "recursion level too deep", filename);
1098 }
1099 g_free (filename);
1100 continue;
1101 }
1102 if (!(file_status.st_mode & S_IFREG)) {
1103 GST_LOG_OBJECT (context->registry, "%s is not a regular file, ignoring",
1104 filename);
1105 g_free (filename);
1106 continue;
1107 }
1108 if (!g_str_has_suffix (dirent, G_MODULE_SUFFIX)
1109 #ifdef GST_EXTRA_MODULE_SUFFIX
1110 && !g_str_has_suffix (dirent, GST_EXTRA_MODULE_SUFFIX)
1111 #endif
1112 ) {
1113 GST_LOG_OBJECT (context->registry,
1114 "extension is not recognized as module file, ignoring file %s",
1115 filename);
1116 g_free (filename);
1117 continue;
1118 }
1120 GST_LOG_OBJECT (context->registry, "file %s looks like a possible module",
1121 filename);
1123 /* plug-ins are considered unique by basename; if the given name
1124 * was already seen by the registry, we ignore it */
1125 plugin = gst_registry_lookup_bn (context->registry, dirent);
1126 if (plugin) {
1127 gboolean env_vars_changed, deps_changed = FALSE;
1129 if (plugin->registered) {
1130 GST_DEBUG_OBJECT (context->registry,
1131 "plugin already registered from path \"%s\"",
1132 GST_STR_NULL (plugin->filename));
1133 g_free (filename);
1134 gst_object_unref (plugin);
1135 continue;
1136 }
1138 env_vars_changed = _priv_plugin_deps_env_vars_changed (plugin);
1140 /* If a file with a certain basename is seen on a different path,
1141 * update the plugin to ensure the registry cache will reflect up
1142 * to date information */
1144 if (plugin->file_mtime == file_status.st_mtime &&
1145 plugin->file_size == file_status.st_size && !env_vars_changed &&
1146 !(deps_changed = _priv_plugin_deps_files_changed (plugin)) &&
1147 !strcmp (plugin->filename, filename)) {
1148 GST_LOG_OBJECT (context->registry, "file %s cached", filename);
1149 plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
1150 GST_LOG_OBJECT (context->registry,
1151 "marking plugin %p as registered as %s", plugin, filename);
1152 plugin->registered = TRUE;
1153 } else {
1154 GST_INFO_OBJECT (context->registry, "cached info for %s is stale",
1155 filename);
1156 GST_DEBUG_OBJECT (context->registry, "mtime %ld != %ld or size %"
1157 G_GINT64_FORMAT " != %" G_GINT64_FORMAT " or external dependency "
1158 "env_vars changed: %d or external dependencies changed: %d"
1159 " or old path %s != new path %s",
1160 plugin->file_mtime, file_status.st_mtime,
1161 (gint64) plugin->file_size, (gint64) file_status.st_size,
1162 env_vars_changed, deps_changed, plugin->filename, filename);
1163 gst_registry_remove_plugin (context->registry, plugin);
1164 changed |= gst_registry_scan_plugin_file (context, filename,
1165 file_status.st_size, file_status.st_mtime);
1166 }
1167 gst_object_unref (plugin);
1169 } else {
1170 GST_DEBUG_OBJECT (context->registry, "file %s not yet in registry",
1171 filename);
1172 changed |= gst_registry_scan_plugin_file (context, filename,
1173 file_status.st_size, file_status.st_mtime);
1174 }
1176 g_free (filename);
1177 }
1179 g_dir_close (dir);
1181 return changed;
1182 }
1184 static gboolean
1185 gst_registry_scan_path_internal (GstRegistryScanContext * context,
1186 const gchar * path)
1187 {
1188 gboolean changed;
1190 GST_DEBUG_OBJECT (context->registry, "scanning path %s", path);
1191 changed = gst_registry_scan_path_level (context, path, 10);
1193 GST_DEBUG_OBJECT (context->registry, "registry changed in path %s: %d", path,
1194 changed);
1195 return changed;
1196 }
1198 /**
1199 * gst_registry_scan_path:
1200 * @registry: the registry to add found plugins to
1201 * @path: the path to scan
1202 *
1203 * Scan the given path for plugins to add to the registry. The syntax of the
1204 * path is specific to the registry.
1205 *
1206 * Returns: %TRUE if registry changed
1207 */
1208 gboolean
1209 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
1210 {
1211 GstRegistryScanContext context;
1212 gboolean result;
1214 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1215 g_return_val_if_fail (path != NULL, FALSE);
1217 init_scan_context (&context, registry);
1219 result = gst_registry_scan_path_internal (&context, path);
1221 clear_scan_context (&context);
1222 result |= context.changed;
1224 return result;
1225 }
1227 static gboolean
1228 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
1229 gpointer user_data)
1230 {
1231 return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
1232 }
1234 /**
1235 * gst_registry_get_feature_list_by_plugin:
1236 * @registry: a #GstRegistry.
1237 * @name: a plugin name.
1238 *
1239 * Retrieves a #GList of features of the plugin with name @name.
1240 *
1241 * Returns: a #GList of #GstPluginFeature. Use gst_plugin_feature_list_free()
1242 * after usage.
1243 */
1244 GList *
1245 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
1246 const gchar * name)
1247 {
1248 g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1249 g_return_val_if_fail (name != NULL, NULL);
1251 return gst_registry_feature_filter (registry,
1252 _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1253 }
1255 /* Unref and delete the default registry */
1256 void
1257 _priv_gst_registry_cleanup ()
1258 {
1259 GstRegistry *registry;
1261 g_static_mutex_lock (&_gst_registry_mutex);
1262 if ((registry = _gst_registry_default) != NULL) {
1263 _gst_registry_default = NULL;
1264 }
1265 g_static_mutex_unlock (&_gst_registry_mutex);
1267 /* unref outside of the lock because we can. */
1268 if (registry)
1269 gst_object_unref (registry);
1270 }
1272 /**
1273 * gst_default_registry_check_feature_version:
1274 * @feature_name: the name of the feature (e.g. "oggdemux")
1275 * @min_major: the minimum major version number
1276 * @min_minor: the minimum minor version number
1277 * @min_micro: the minimum micro version number
1278 *
1279 * Checks whether a plugin feature by the given name exists in the
1280 * default registry and whether its version is at least the
1281 * version required.
1282 *
1283 * Returns: #TRUE if the feature could be found and the version is
1284 * the same as the required version or newer, and #FALSE otherwise.
1285 */
1286 gboolean
1287 gst_default_registry_check_feature_version (const gchar * feature_name,
1288 guint min_major, guint min_minor, guint min_micro)
1289 {
1290 GstPluginFeature *feature;
1291 GstRegistry *registry;
1292 gboolean ret = FALSE;
1294 g_return_val_if_fail (feature_name != NULL, FALSE);
1296 GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1298 registry = gst_registry_get_default ();
1299 feature = gst_registry_lookup_feature (registry, feature_name);
1300 if (feature) {
1301 ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1302 min_micro);
1303 gst_object_unref (feature);
1304 } else {
1305 GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1306 }
1308 return ret;
1309 }
1311 static void
1312 load_plugin_func (gpointer data, gpointer user_data)
1313 {
1314 GstPlugin *plugin;
1315 const gchar *filename;
1316 GError *err = NULL;
1318 filename = (const gchar *) data;
1319 GST_DEBUG ("Pre-loading plugin %s", filename);
1321 plugin = gst_plugin_load_file (filename, &err);
1323 if (plugin) {
1324 GST_INFO ("Loaded plugin: \"%s\"", filename);
1326 gst_default_registry_add_plugin (plugin);
1327 } else {
1328 if (err) {
1329 /* Report error to user, and free error */
1330 GST_ERROR ("Failed to load plugin: %s", err->message);
1331 g_error_free (err);
1332 } else {
1333 GST_WARNING ("Failed to load plugin: \"%s\"", filename);
1334 }
1335 }
1336 }
1338 #ifndef GST_DISABLE_REGISTRY
1339 /* Unref all plugins marked 'cached', to clear old plugins that no
1340 * longer exist. Returns TRUE if any plugins were removed */
1341 static gboolean
1342 gst_registry_remove_cache_plugins (GstRegistry * registry)
1343 {
1344 GList *g;
1345 GList *g_next;
1346 GstPlugin *plugin;
1347 gboolean changed = FALSE;
1349 g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1351 GST_OBJECT_LOCK (registry);
1353 GST_DEBUG_OBJECT (registry, "removing cached plugins");
1354 g = registry->plugins;
1355 while (g) {
1356 g_next = g->next;
1357 plugin = g->data;
1358 if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
1359 GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
1360 GST_STR_NULL (plugin->filename));
1361 registry->plugins = g_list_delete_link (registry->plugins, g);
1362 if (G_LIKELY (plugin->basename))
1363 g_hash_table_remove (registry->basename_hash, plugin->basename);
1364 gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
1365 gst_object_unref (plugin);
1366 changed = TRUE;
1367 }
1368 g = g_next;
1369 }
1371 GST_OBJECT_UNLOCK (registry);
1373 return changed;
1374 }
1376 typedef enum
1377 {
1378 REGISTRY_SCAN_AND_UPDATE_FAILURE = 0,
1379 REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED,
1380 REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED
1381 } GstRegistryScanAndUpdateResult;
1383 /*
1384 * scan_and_update_registry:
1385 * @default_registry: the #GstRegistry
1386 * @registry_file: registry filename
1387 * @write_changes: write registry if it has changed?
1388 *
1389 * Scans for registry changes and eventually updates the registry cache.
1390 *
1391 * Return: %REGISTRY_SCAN_AND_UPDATE_FAILURE if the registry could not scanned
1392 * or updated, %REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED if the
1393 * registry is clean and %REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED if
1394 * it has been updated and the cache needs to be re-read.
1395 */
1396 static GstRegistryScanAndUpdateResult
1397 scan_and_update_registry (GstRegistry * default_registry,
1398 const gchar * registry_file, gboolean write_changes, GError ** error)
1399 {
1400 const gchar *plugin_path;
1401 gboolean changed = FALSE;
1402 GList *l;
1403 GstRegistryScanContext context;
1405 GST_INFO ("Validating plugins from registry cache: %s", registry_file);
1407 init_scan_context (&context, default_registry);
1409 /* It sounds tempting to just compare the mtime of directories with the mtime
1410 * of the registry cache, but it does not work. It would not catch updated
1411 * plugins, which might bring more or less features.
1412 */
1414 /* scan paths specified via --gst-plugin-path */
1415 GST_DEBUG ("scanning paths added via --gst-plugin-path");
1416 for (l = _priv_gst_plugin_paths; l != NULL; l = l->next) {
1417 GST_INFO ("Scanning plugin path: \"%s\"", (gchar *) l->data);
1418 changed |= gst_registry_scan_path_internal (&context, (gchar *) l->data);
1419 }
1420 /* keep plugin_paths around in case a re-scan is forced later on */
1422 /* GST_PLUGIN_PATH specifies a list of directories to scan for
1423 * additional plugins. These take precedence over the system plugins */
1424 plugin_path = g_getenv ("GST_PLUGIN_PATH");
1425 if (plugin_path) {
1426 char **list;
1427 int i;
1429 GST_DEBUG ("GST_PLUGIN_PATH set to %s", plugin_path);
1430 list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1431 for (i = 0; list[i]; i++) {
1432 changed |= gst_registry_scan_path_internal (&context, list[i]);
1433 }
1434 g_strfreev (list);
1435 } else {
1436 GST_DEBUG ("GST_PLUGIN_PATH not set");
1437 }
1439 /* GST_PLUGIN_SYSTEM_PATH specifies a list of plugins that are always
1440 * loaded by default. If not set, this defaults to the system-installed
1441 * path, and the plugins installed in the user's home directory */
1442 plugin_path = g_getenv ("GST_PLUGIN_SYSTEM_PATH");
1443 if (plugin_path == NULL) {
1444 char *home_plugins;
1446 GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH not set");
1448 /* plugins in the user's home directory take precedence over
1449 * system-installed ones */
1450 home_plugins = g_build_filename (g_get_home_dir (),
1451 ".gstreamer-" GST_MAJORMINOR, "plugins", NULL);
1452 GST_DEBUG ("scanning home plugins %s", home_plugins);
1453 changed |= gst_registry_scan_path_internal (&context, home_plugins);
1454 g_free (home_plugins);
1456 /* add the main (installed) library path */
1457 GST_DEBUG ("scanning main plugins %s", PLUGINDIR);
1458 changed |= gst_registry_scan_path_internal (&context, PLUGINDIR);
1460 #ifdef G_OS_WIN32
1461 {
1462 char *base_dir;
1463 char *dir;
1465 base_dir =
1466 g_win32_get_package_installation_directory_of_module
1467 (_priv_gst_dll_handle);
1469 dir = g_build_filename (base_dir, "lib", "gstreamer-0.10", NULL);
1470 GST_DEBUG ("scanning DLL dir %s", dir);
1472 changed |= gst_registry_scan_path_internal (&context, dir);
1474 g_free (dir);
1475 g_free (base_dir);
1476 }
1477 #endif
1478 } else {
1479 gchar **list;
1480 gint i;
1482 GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH set to %s", plugin_path);
1483 list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1484 for (i = 0; list[i]; i++) {
1485 changed |= gst_registry_scan_path_internal (&context, list[i]);
1486 }
1487 g_strfreev (list);
1488 }
1490 clear_scan_context (&context);
1491 changed |= context.changed;
1493 /* Remove cached plugins so stale info is cleared. */
1494 changed |= gst_registry_remove_cache_plugins (default_registry);
1496 if (!changed) {
1497 GST_INFO ("Registry cache has not changed");
1498 return REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED;
1499 }
1501 if (!write_changes) {
1502 GST_INFO ("Registry cache changed, but writing is disabled. Not writing.");
1503 return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1504 }
1506 GST_INFO ("Registry cache changed. Writing new registry cache");
1507 if (!gst_registry_binary_write_cache (default_registry, registry_file)) {
1508 g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
1509 _("Error writing registry cache to %s: %s"),
1510 registry_file, g_strerror (errno));
1511 return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1512 }
1514 GST_INFO ("Registry cache written successfully");
1515 return REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED;
1516 }
1518 static gboolean
1519 ensure_current_registry (GError ** error)
1520 {
1521 gchar *registry_file;
1522 GstRegistry *default_registry;
1523 gboolean ret = TRUE;
1524 gboolean do_update = TRUE;
1525 gboolean have_cache = TRUE;
1527 default_registry = gst_registry_get_default ();
1528 registry_file = g_strdup (g_getenv ("GST_REGISTRY"));
1529 if (registry_file == NULL) {
1530 registry_file = g_build_filename (g_get_home_dir (),
1531 ".gstreamer-" GST_MAJORMINOR, "registry." HOST_CPU ".bin", NULL);
1532 }
1534 if (!_gst_disable_registry_cache) {
1535 GST_INFO ("reading registry cache: %s", registry_file);
1536 have_cache = gst_registry_binary_read_cache (default_registry,
1537 registry_file);
1538 /* Only ever read the registry cache once, then disable it for
1539 * subsequent updates during the program lifetime */
1540 _gst_disable_registry_cache = TRUE;
1541 }
1543 if (have_cache) {
1544 do_update = !_priv_gst_disable_registry_update;
1545 if (do_update) {
1546 const gchar *update_env;
1548 if ((update_env = g_getenv ("GST_REGISTRY_UPDATE"))) {
1549 /* do update for any value different from "no" */
1550 do_update = (strcmp (update_env, "no") != 0);
1551 }
1552 }
1553 }
1555 if (do_update) {
1556 /* now check registry */
1557 GST_DEBUG ("Updating registry cache");
1558 scan_and_update_registry (default_registry, registry_file, TRUE, error);
1559 } else {
1560 GST_DEBUG ("Not updating registry cache (disabled)");
1561 }
1563 g_free (registry_file);
1564 GST_INFO ("registry reading and updating done, result = %d", ret);
1566 return ret;
1567 }
1568 #endif /* GST_DISABLE_REGISTRY */
1570 /**
1571 * gst_registry_fork_is_enabled:
1572 *
1573 * By default GStreamer will perform scanning and rebuilding of the
1574 * registry file using a helper child process.
1575 *
1576 * Applications might want to disable this behaviour with the
1577 * gst_registry_fork_set_enabled() function, in which case new plugins
1578 * are scanned (and loaded) into the application process.
1579 *
1580 * Returns: %TRUE if GStreamer will use the child helper process when
1581 * rebuilding the registry.
1582 *
1583 * Since: 0.10.10
1584 */
1585 gboolean
1586 gst_registry_fork_is_enabled (void)
1587 {
1588 return _gst_enable_registry_fork;
1589 }
1591 /**
1592 * gst_registry_fork_set_enabled:
1593 * @enabled: whether rebuilding the registry can use a temporary child helper process.
1594 *
1595 * Applications might want to disable/enable spawning of a child helper process
1596 * when rebuilding the registry. See gst_registry_fork_is_enabled() for more
1597 * information.
1598 *
1599 * Since: 0.10.10
1600 */
1601 void
1602 gst_registry_fork_set_enabled (gboolean enabled)
1603 {
1604 _gst_enable_registry_fork = enabled;
1605 }
1607 /**
1608 * gst_update_registry:
1609 *
1610 * Forces GStreamer to re-scan its plugin paths and update the default
1611 * plugin registry.
1612 *
1613 * Applications will almost never need to call this function, it is only
1614 * useful if the application knows new plugins have been installed (or old
1615 * ones removed) since the start of the application (or, to be precise, the
1616 * first call to gst_init()) and the application wants to make use of any
1617 * newly-installed plugins without restarting the application.
1618 *
1619 * Applications should assume that the registry update is neither atomic nor
1620 * thread-safe and should therefore not have any dynamic pipelines running
1621 * (including the playbin and decodebin elements) and should also not create
1622 * any elements or access the GStreamer registry while the update is in
1623 * progress.
1624 *
1625 * Note that this function may block for a significant amount of time.
1626 *
1627 * Returns: %TRUE if the registry has been updated successfully (does not
1628 * imply that there were changes), otherwise %FALSE.
1629 *
1630 * Since: 0.10.12
1631 */
1632 gboolean
1633 gst_update_registry (void)
1634 {
1635 gboolean res;
1637 #ifndef GST_DISABLE_REGISTRY
1638 GError *err = NULL;
1640 res = ensure_current_registry (&err);
1641 if (err) {
1642 GST_WARNING ("registry update failed: %s", err->message);
1643 g_error_free (err);
1644 } else {
1645 GST_LOG ("registry update succeeded");
1646 }
1648 #else
1649 GST_WARNING ("registry update failed: %s", "registry disabled");
1650 res = TRUE;
1651 #endif /* GST_DISABLE_REGISTRY */
1653 if (_priv_gst_preload_plugins) {
1654 GST_DEBUG ("Preloading indicated plugins...");
1655 g_slist_foreach (_priv_gst_preload_plugins, load_plugin_func, NULL);
1656 }
1658 return res;
1659 }
1661 /**
1662 * gst_registry_get_feature_list_cookie:
1663 * @registry: the registry
1664 *
1665 * Returns the registrys feature list cookie. This changes
1666 * every time a feature is added or removed from the registry.
1667 *
1668 * Returns: the feature list cookie.
1669 *
1670 * Since: 0.10.26
1671 */
1672 guint32
1673 gst_registry_get_feature_list_cookie (GstRegistry * registry)
1674 {
1675 g_return_val_if_fail (GST_IS_REGISTRY (registry), 0);
1677 return registry->priv->cookie;
1678 }