docs: fix some docs
[platform/upstream/gstreamer.git] / gst / gstregistry.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *                    2005 David A. Schleef <ds@schleef.org>
5  *
6  * gstregistry.c: handle registry
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
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. Each #GstPlugin is 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 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 file. The format
49  * and location of the cache file is internal to gstreamer.
50  *
51  * On startup, plugins are searched for in the plugin search path. The following
52  * locations are checked in this order:
53  * <itemizedlist>
54  *   <listitem>
55  *     <para>location from --gst-plugin-path commandline option.</para>
56  *   </listitem>
57  *   <listitem>
58  *     <para>the GST_PLUGIN_PATH environment variable.</para>
59  *   </listitem>
60  *   <listitem>
61  *     <para>the GST_PLUGIN_SYSTEM_PATH environment variable.</para>
62  *   </listitem>
63  *   <listitem>
64  *     <para>default locations (if GST_PLUGIN_SYSTEM_PATH is not set). Those
65  *       default locations are:
66  *       <filename>~/.gstreamer-$GST_MAJORMINOR/plugins/</filename>
67  *       and <filename>$prefix/libs/gstreamer-$GST_MAJORMINOR/</filename>.
68  *     </para>
69  *   </listitem>
70  * </itemizedlist>
71  * The registry cache file is loaded from
72  * <filename>~/.gstreamer-$GST_MAJORMINOR/registry-$ARCH.bin</filename> or the
73  * file listed in the GST_REGISTRY env var. One reason to change the registry
74  * location is for testing.
75  *
76  * For each plugin that is found in the plugin search path, there could be 3
77  * possibilities for cached information:
78  * <itemizedlist>
79  *   <listitem>
80  *     <para>the cache may not contain information about a given file.</para>
81  *   </listitem>
82  *   <listitem>
83  *     <para>the cache may have stale information.</para>
84  *   </listitem>
85  *   <listitem>
86  *     <para>the cache may have current information.</para>
87  *   </listitem>
88  * </itemizedlist>
89  *
90  * In the first two cases, the plugin is loaded and the cache updated. In
91  * addition to these cases, the cache may have entries for plugins that are not
92  * relevant to the current process. These are marked as not available to the
93  * current process. If the cache is updated for whatever reason, it is marked
94  * dirty.
95  *
96  * A dirty cache is written out at the end of initialization. Each entry is
97  * checked to make sure the information is minimally valid. If not, the entry is
98  * simply dropped.
99  *
100  * <emphasis role="bold">Implementation notes:</emphasis>
101  *
102  * The "cache" and "default registry" are different concepts and can represent
103  * different sets of plugins. For various reasons, at init time, the cache is
104  * stored in the default registry, and plugins not relevant to the current
105  * process are marked with the %GST_PLUGIN_FLAG_CACHED bit. These plugins are
106  * removed at the end of initialization.
107  */
108
109 #ifdef HAVE_CONFIG_H
110 #include "config.h"
111 #endif
112 #include "gstconfig.h"
113 #include "gst_private.h"
114 #include <glib.h>
115 #include <sys/types.h>
116 #include <sys/stat.h>
117 #ifdef HAVE_UNISTD_H
118 #include <unistd.h>
119 #endif
120 #include <errno.h>
121 #include <stdio.h>
122 #include <string.h>
123
124 /* For g_stat () */
125 #include <glib/gstdio.h>
126
127 #include "gstinfo.h"
128 #include "gsterror.h"
129 #include "gstregistry.h"
130 #include "gstmarshal.h"
131
132 #include "gstpluginloader.h"
133
134 #include "gst-i18n-lib.h"
135
136 #include "gst.h"
137 #include "glib-compat-private.h"
138
139 #ifdef G_OS_WIN32
140 #include <windows.h>
141 extern HMODULE _priv_gst_dll_handle;
142 #endif
143
144 #define GST_CAT_DEFAULT GST_CAT_REGISTRY
145
146 struct _GstRegistryPrivate
147 {
148   GList *plugins;
149   GList *features;
150
151   GList *paths;
152
153   int cache_file;
154
155   /* hash to speedup _lookup_feature_locked() */
156   GHashTable *feature_hash;
157   /* hash to speedup _lookup */
158   GHashTable *basename_hash;
159
160   /* updated whenever the feature list changes */
161   guint32 cookie;
162   /* speedup for searching features */
163   GList *element_factory_list;
164   guint32 efl_cookie;
165   GList *typefind_factory_list;
166   guint32 tfl_cookie;
167 };
168
169 /* the one instance of the default registry and the mutex protecting the
170  * variable. */
171 static GStaticMutex _gst_registry_mutex = G_STATIC_MUTEX_INIT;
172 static GstRegistry *_gst_registry_default = NULL;
173
174 /* defaults */
175 #define DEFAULT_FORK TRUE
176
177 /* control the behaviour of registry rebuild */
178 static gboolean _gst_enable_registry_fork = DEFAULT_FORK;
179 /* List of plugins that need preloading/reloading after scanning registry */
180 extern GSList *_priv_gst_preload_plugins;
181
182 #ifndef GST_DISABLE_REGISTRY
183 /*set to TRUE when registry needn't to be updated */
184 gboolean _priv_gst_disable_registry_update = FALSE;
185 extern GList *_priv_gst_plugin_paths;
186
187 /* Set to TRUE when the registry cache should be disabled */
188 gboolean _gst_disable_registry_cache = FALSE;
189
190 static gboolean __registry_reuse_plugin_scanner = TRUE;
191 #endif
192
193 /* Element signals and args */
194 enum
195 {
196   PLUGIN_ADDED,
197   FEATURE_ADDED,
198   LAST_SIGNAL
199 };
200
201 static void gst_registry_finalize (GObject * object);
202
203 static guint gst_registry_signals[LAST_SIGNAL] = { 0 };
204
205 static GstPluginFeature *gst_registry_lookup_feature_locked (GstRegistry *
206     registry, const char *name);
207 static GstPlugin *gst_registry_lookup_bn_locked (GstRegistry * registry,
208     const char *basename);
209
210 #define gst_registry_parent_class parent_class
211 G_DEFINE_TYPE (GstRegistry, gst_registry, GST_TYPE_OBJECT);
212
213 static void
214 gst_registry_class_init (GstRegistryClass * klass)
215 {
216   GObjectClass *gobject_class;
217
218   gobject_class = (GObjectClass *) klass;
219
220   g_type_class_add_private (klass, sizeof (GstRegistryPrivate));
221
222   /**
223    * GstRegistry::plugin-added:
224    * @registry: the registry that emitted the signal
225    * @plugin: the plugin that has been added
226    *
227    * Signals that a plugin has been added to the registry (possibly
228    * replacing a previously-added one by the same name)
229    */
230   gst_registry_signals[PLUGIN_ADDED] =
231       g_signal_new ("plugin-added", G_TYPE_FROM_CLASS (klass),
232       G_SIGNAL_RUN_LAST, 0, NULL, NULL, gst_marshal_VOID__OBJECT,
233       G_TYPE_NONE, 1, GST_TYPE_PLUGIN);
234
235   /**
236    * GstRegistry::feature-added:
237    * @registry: the registry that emitted the signal
238    * @feature: the feature that has been added
239    *
240    * Signals that a feature has been added to the registry (possibly
241    * replacing a previously-added one by the same name)
242    */
243   gst_registry_signals[FEATURE_ADDED] =
244       g_signal_new ("feature-added", G_TYPE_FROM_CLASS (klass),
245       G_SIGNAL_RUN_LAST, 0, NULL, NULL, gst_marshal_VOID__OBJECT,
246       G_TYPE_NONE, 1, GST_TYPE_PLUGIN_FEATURE);
247
248   gobject_class->finalize = gst_registry_finalize;
249 }
250
251 static void
252 gst_registry_init (GstRegistry * registry)
253 {
254   registry->priv =
255       G_TYPE_INSTANCE_GET_PRIVATE (registry, GST_TYPE_REGISTRY,
256       GstRegistryPrivate);
257   registry->priv->feature_hash = g_hash_table_new (g_str_hash, g_str_equal);
258   registry->priv->basename_hash = g_hash_table_new (g_str_hash, g_str_equal);
259 }
260
261 static void
262 gst_registry_finalize (GObject * object)
263 {
264   GstRegistry *registry = GST_REGISTRY (object);
265   GList *plugins, *p;
266   GList *features, *f;
267
268   plugins = registry->priv->plugins;
269   registry->priv->plugins = NULL;
270
271   GST_DEBUG_OBJECT (registry, "registry finalize");
272   p = plugins;
273   while (p) {
274     GstPlugin *plugin = p->data;
275
276     if (plugin) {
277       GST_LOG_OBJECT (registry, "removing plugin %s",
278           gst_plugin_get_name (plugin));
279       gst_object_unref (plugin);
280     }
281     p = g_list_next (p);
282   }
283   g_list_free (plugins);
284
285   features = registry->priv->features;
286   registry->priv->features = NULL;
287
288   f = features;
289   while (f) {
290     GstPluginFeature *feature = f->data;
291
292     if (feature) {
293       GST_LOG_OBJECT (registry, "removing feature %p (%s)", feature,
294           GST_OBJECT_NAME (feature));
295       gst_object_unparent (GST_OBJECT_CAST (feature));
296     }
297     f = g_list_next (f);
298   }
299   g_list_free (features);
300
301   g_hash_table_destroy (registry->priv->feature_hash);
302   registry->priv->feature_hash = NULL;
303   g_hash_table_destroy (registry->priv->basename_hash);
304   registry->priv->basename_hash = NULL;
305
306   if (registry->priv->element_factory_list) {
307     GST_DEBUG_OBJECT (registry, "Cleaning up cached element factory list");
308     gst_plugin_feature_list_free (registry->priv->element_factory_list);
309   }
310
311   if (registry->priv->typefind_factory_list) {
312     GST_DEBUG_OBJECT (registry, "Cleaning up cached typefind factory list");
313     gst_plugin_feature_list_free (registry->priv->typefind_factory_list);
314   }
315
316   G_OBJECT_CLASS (parent_class)->finalize (object);
317 }
318
319 /**
320  * gst_registry_get:
321  *
322  * Retrieves the singleton plugin registry. The caller does not own a
323  * reference on the registry, as it is alive as long as GStreamer is
324  * initialized.
325  *
326  * Returns: (transfer none): the #GstRegistry.
327  */
328 GstRegistry *
329 gst_registry_get (void)
330 {
331   GstRegistry *registry;
332
333   g_static_mutex_lock (&_gst_registry_mutex);
334   if (G_UNLIKELY (!_gst_registry_default)) {
335     _gst_registry_default = g_object_newv (GST_TYPE_REGISTRY, 0, NULL);
336     gst_object_ref_sink (GST_OBJECT_CAST (_gst_registry_default));
337   }
338   registry = _gst_registry_default;
339   g_static_mutex_unlock (&_gst_registry_mutex);
340
341   return registry;
342 }
343
344 /**
345  * gst_registry_add_path:
346  * @registry: the registry to add the path to
347  * @path: the path to add to the registry
348  *
349  * Add the given path to the registry. The syntax of the
350  * path is specific to the registry. If the path has already been
351  * added, do nothing.
352  */
353 void
354 gst_registry_add_path (GstRegistry * registry, const gchar * path)
355 {
356   g_return_if_fail (GST_IS_REGISTRY (registry));
357   g_return_if_fail (path != NULL);
358
359   if (strlen (path) == 0)
360     goto empty_path;
361
362   GST_OBJECT_LOCK (registry);
363   if (g_list_find_custom (registry->priv->paths, path, (GCompareFunc) strcmp))
364     goto was_added;
365
366   GST_INFO ("Adding plugin path: \"%s\"", path);
367   registry->priv->paths =
368       g_list_append (registry->priv->paths, g_strdup (path));
369   GST_OBJECT_UNLOCK (registry);
370
371   return;
372
373 empty_path:
374   {
375     GST_INFO ("Ignoring empty plugin path");
376     return;
377   }
378 was_added:
379   {
380     g_warning ("path %s already added to registry", path);
381     GST_OBJECT_UNLOCK (registry);
382     return;
383   }
384 }
385
386 /**
387  * gst_registry_get_path_list:
388  * @registry: the registry to get the pathlist of
389  *
390  * Get the list of paths for the given registry.
391  *
392  * Returns: (transfer container) (element-type char*): A #GList of paths as
393  *     strings. g_list_free after use.
394  *
395  * MT safe.
396  */
397 GList *
398 gst_registry_get_path_list (GstRegistry * registry)
399 {
400   GList *list;
401
402   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
403
404   GST_OBJECT_LOCK (registry);
405   /* We don't need to copy the strings, because they won't be deleted
406    * as long as the GstRegistry is around */
407   list = g_list_copy (registry->priv->paths);
408   GST_OBJECT_UNLOCK (registry);
409
410   return list;
411 }
412
413
414 /**
415  * gst_registry_add_plugin:
416  * @registry: the registry to add the plugin to
417  * @plugin: (transfer full): the plugin to add
418  *
419  * Add the plugin to the registry. The plugin-added signal will be emitted.
420  * This function will sink @plugin.
421  *
422  * Returns: TRUE on success.
423  *
424  * MT safe.
425  */
426 gboolean
427 gst_registry_add_plugin (GstRegistry * registry, GstPlugin * plugin)
428 {
429   GstPlugin *existing_plugin;
430
431   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
432   g_return_val_if_fail (GST_IS_PLUGIN (plugin), FALSE);
433
434   GST_OBJECT_LOCK (registry);
435   if (G_LIKELY (plugin->basename)) {
436     /* we have a basename, see if we find the plugin */
437     existing_plugin =
438         gst_registry_lookup_bn_locked (registry, plugin->basename);
439     if (existing_plugin) {
440       GST_DEBUG_OBJECT (registry,
441           "Replacing existing plugin \"%s\" %p with new plugin %p for filename \"%s\"",
442           GST_STR_NULL (existing_plugin->filename), existing_plugin, plugin,
443           GST_STR_NULL (plugin->filename));
444       /* If the new plugin is blacklisted and the existing one isn't cached, do not
445        * accept if it's from a different location than the existing one */
446       if ((plugin->flags & GST_PLUGIN_FLAG_BLACKLISTED) &&
447           strcmp (plugin->filename, existing_plugin->filename)) {
448         GST_WARNING_OBJECT (registry,
449             "Not replacing plugin because new one (%s) is blacklisted but for a different location than existing one (%s)",
450             plugin->filename, existing_plugin->filename);
451         gst_object_unref (plugin);
452         GST_OBJECT_UNLOCK (registry);
453         return FALSE;
454       }
455       registry->priv->plugins =
456           g_list_remove (registry->priv->plugins, existing_plugin);
457       if (G_LIKELY (existing_plugin->basename))
458         g_hash_table_remove (registry->priv->basename_hash,
459             existing_plugin->basename);
460       gst_object_unref (existing_plugin);
461     }
462   }
463
464   GST_DEBUG_OBJECT (registry, "adding plugin %p for filename \"%s\"",
465       plugin, GST_STR_NULL (plugin->filename));
466
467   registry->priv->plugins = g_list_prepend (registry->priv->plugins, plugin);
468   if (G_LIKELY (plugin->basename))
469     g_hash_table_replace (registry->priv->basename_hash, plugin->basename,
470         plugin);
471
472   gst_object_ref_sink (plugin);
473   GST_OBJECT_UNLOCK (registry);
474
475   GST_LOG_OBJECT (registry, "emitting plugin-added for filename \"%s\"",
476       GST_STR_NULL (plugin->filename));
477   g_signal_emit (registry, gst_registry_signals[PLUGIN_ADDED], 0, plugin);
478
479   return TRUE;
480 }
481
482 static void
483 gst_registry_remove_features_for_plugin_unlocked (GstRegistry * registry,
484     GstPlugin * plugin)
485 {
486   GList *f;
487
488   g_return_if_fail (GST_IS_REGISTRY (registry));
489   g_return_if_fail (GST_IS_PLUGIN (plugin));
490
491   /* Remove all features for this plugin */
492   f = registry->priv->features;
493   while (f != NULL) {
494     GList *next = g_list_next (f);
495     GstPluginFeature *feature = f->data;
496
497     if (G_UNLIKELY (feature && feature->plugin == plugin)) {
498       GST_DEBUG_OBJECT (registry, "removing feature %p (%s) for plugin %p (%s)",
499           feature, gst_plugin_feature_get_name (feature), plugin,
500           plugin->desc.name);
501
502       registry->priv->features =
503           g_list_delete_link (registry->priv->features, f);
504       g_hash_table_remove (registry->priv->feature_hash,
505           GST_OBJECT_NAME (feature));
506       gst_object_unparent (GST_OBJECT_CAST (feature));
507     }
508     f = next;
509   }
510   registry->priv->cookie++;
511 }
512
513 /**
514  * gst_registry_remove_plugin:
515  * @registry: the registry to remove the plugin from
516  * @plugin: (transfer none): the plugin to remove
517  *
518  * Remove the plugin from the registry.
519  *
520  * MT safe.
521  */
522 void
523 gst_registry_remove_plugin (GstRegistry * registry, GstPlugin * plugin)
524 {
525   g_return_if_fail (GST_IS_REGISTRY (registry));
526   g_return_if_fail (GST_IS_PLUGIN (plugin));
527
528   GST_DEBUG_OBJECT (registry, "removing plugin %p (%s)",
529       plugin, gst_plugin_get_name (plugin));
530
531   GST_OBJECT_LOCK (registry);
532   registry->priv->plugins = g_list_remove (registry->priv->plugins, plugin);
533   if (G_LIKELY (plugin->basename))
534     g_hash_table_remove (registry->priv->basename_hash, plugin->basename);
535   gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
536   GST_OBJECT_UNLOCK (registry);
537   gst_object_unref (plugin);
538 }
539
540 /**
541  * gst_registry_add_feature:
542  * @registry: the registry to add the plugin to
543  * @feature: (transfer full): the feature to add
544  *
545  * Add the feature to the registry. The feature-added signal will be emitted.
546  * This function sinks @feature.
547  *
548  * Returns: TRUE on success.
549  *
550  * MT safe.
551  */
552 gboolean
553 gst_registry_add_feature (GstRegistry * registry, GstPluginFeature * feature)
554 {
555   GstPluginFeature *existing_feature;
556
557   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
558   g_return_val_if_fail (GST_IS_PLUGIN_FEATURE (feature), FALSE);
559   g_return_val_if_fail (GST_OBJECT_NAME (feature) != NULL, FALSE);
560   g_return_val_if_fail (feature->plugin_name != NULL, FALSE);
561
562   GST_OBJECT_LOCK (registry);
563   existing_feature = gst_registry_lookup_feature_locked (registry,
564       GST_OBJECT_NAME (feature));
565   if (G_UNLIKELY (existing_feature)) {
566     GST_DEBUG_OBJECT (registry, "replacing existing feature %p (%s)",
567         existing_feature, GST_OBJECT_NAME (feature));
568     /* Remove the existing feature from the list now, before we insert the new
569      * one, but don't unref yet because the hash is still storing a reference to
570      * it. */
571     registry->priv->features =
572         g_list_remove (registry->priv->features, existing_feature);
573   }
574
575   GST_DEBUG_OBJECT (registry, "adding feature %p (%s)", feature,
576       GST_OBJECT_NAME (feature));
577
578   registry->priv->features = g_list_prepend (registry->priv->features, feature);
579   g_hash_table_replace (registry->priv->feature_hash, GST_OBJECT_NAME (feature),
580       feature);
581
582   if (G_UNLIKELY (existing_feature)) {
583     /* We unref now. No need to remove the feature name from the hash table, it
584      * got replaced by the new feature */
585     gst_object_unparent (GST_OBJECT_CAST (existing_feature));
586   }
587
588   gst_object_set_parent (GST_OBJECT_CAST (feature), GST_OBJECT_CAST (registry));
589
590   registry->priv->cookie++;
591   GST_OBJECT_UNLOCK (registry);
592
593   GST_LOG_OBJECT (registry, "emitting feature-added for %s",
594       GST_OBJECT_NAME (feature));
595   g_signal_emit (registry, gst_registry_signals[FEATURE_ADDED], 0, feature);
596
597   return TRUE;
598 }
599
600 /**
601  * gst_registry_remove_feature:
602  * @registry: the registry to remove the feature from
603  * @feature: (transfer none): the feature to remove
604  *
605  * Remove the feature from the registry.
606  *
607  * MT safe.
608  */
609 void
610 gst_registry_remove_feature (GstRegistry * registry, GstPluginFeature * feature)
611 {
612   g_return_if_fail (GST_IS_REGISTRY (registry));
613   g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
614
615   GST_DEBUG_OBJECT (registry, "removing feature %p (%s)",
616       feature, gst_plugin_feature_get_name (feature));
617
618   GST_OBJECT_LOCK (registry);
619   registry->priv->features = g_list_remove (registry->priv->features, feature);
620   g_hash_table_remove (registry->priv->feature_hash, GST_OBJECT_NAME (feature));
621   registry->priv->cookie++;
622   GST_OBJECT_UNLOCK (registry);
623
624   gst_object_unparent ((GstObject *) feature);
625 }
626
627 /**
628  * gst_registry_plugin_filter:
629  * @registry: registry to query
630  * @filter: (scope call): the filter to use
631  * @first: only return first match
632  * @user_data: (closure): user data passed to the filter function
633  *
634  * Runs a filter against all plugins in the registry and returns a #GList with
635  * the results. If the first flag is set, only the first match is
636  * returned (as a list with a single object).
637  * Every plugin is reffed; use gst_plugin_list_free() after use, which
638  * will unref again.
639  *
640  * Returns: (transfer full) (element-type Gst.Plugin): a #GList of #GstPlugin.
641  *     Use gst_plugin_list_free() after usage.
642  *
643  * MT safe.
644  */
645 GList *
646 gst_registry_plugin_filter (GstRegistry * registry,
647     GstPluginFilter filter, gboolean first, gpointer user_data)
648 {
649   GList *list = NULL;
650
651   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
652
653   GST_OBJECT_LOCK (registry);
654   {
655     const GList *walk;
656
657     for (walk = registry->priv->plugins; walk != NULL; walk = walk->next) {
658       GstPlugin *plugin = walk->data;
659
660       if (filter == NULL || filter (plugin, user_data)) {
661         list = g_list_prepend (list, gst_object_ref (plugin));
662
663         if (first)
664           break;
665       }
666     }
667   }
668   GST_OBJECT_UNLOCK (registry);
669
670   return list;
671 }
672
673 typedef struct
674 {
675   const gchar *name;
676   GType type;
677 } GstTypeNameData;
678
679 static gboolean
680 gst_plugin_feature_type_name_filter (GstPluginFeature * feature,
681     GstTypeNameData * data)
682 {
683   g_assert (GST_IS_PLUGIN_FEATURE (feature));
684
685   return ((data->type == 0 || data->type == G_OBJECT_TYPE (feature)) &&
686       (data->name == NULL || !strcmp (data->name, GST_OBJECT_NAME (feature))));
687 }
688
689 /* returns TRUE if the list was changed
690  *
691  * Must be called with the object lock taken */
692 static gboolean
693 gst_registry_get_feature_list_or_create (GstRegistry * registry,
694     GList ** previous, guint32 * cookie, GType type)
695 {
696   gboolean res = FALSE;
697   GstRegistryPrivate *priv = registry->priv;
698
699   if (G_UNLIKELY (!*previous || priv->cookie != *cookie)) {
700     GstTypeNameData data;
701     const GList *walk;
702
703     if (*previous) {
704       gst_plugin_feature_list_free (*previous);
705       *previous = NULL;
706     }
707
708     data.type = type;
709     data.name = NULL;
710
711     for (walk = registry->priv->features; walk != NULL; walk = walk->next) {
712       GstPluginFeature *feature = walk->data;
713
714       if (gst_plugin_feature_type_name_filter (feature, &data)) {
715         *previous = g_list_prepend (*previous, gst_object_ref (feature));
716       }
717     }
718
719     *cookie = priv->cookie;
720     res = TRUE;
721   }
722
723   return res;
724 }
725
726 static gint
727 type_find_factory_rank_cmp (const GstPluginFeature * fac1,
728     const GstPluginFeature * fac2)
729 {
730   if (G_LIKELY (fac1->rank != fac2->rank))
731     return fac2->rank - fac1->rank;
732
733   /* to make the order in which things happen more deterministic,
734    * sort by name when the ranks are the same. */
735   return strcmp (GST_OBJECT_NAME (fac1), GST_OBJECT_NAME (fac2));
736 }
737
738 static GList *
739 gst_registry_get_element_factory_list (GstRegistry * registry)
740 {
741   GList *list;
742
743   GST_OBJECT_LOCK (registry);
744
745   gst_registry_get_feature_list_or_create (registry,
746       &registry->priv->element_factory_list, &registry->priv->efl_cookie,
747       GST_TYPE_ELEMENT_FACTORY);
748
749   /* Return reffed copy */
750   list = gst_plugin_feature_list_copy (registry->priv->element_factory_list);
751
752   GST_OBJECT_UNLOCK (registry);
753
754   return list;
755 }
756
757 static GList *
758 gst_registry_get_typefind_factory_list (GstRegistry * registry)
759 {
760   GList *list;
761
762   GST_OBJECT_LOCK (registry);
763
764   if (G_UNLIKELY (gst_registry_get_feature_list_or_create (registry,
765               &registry->priv->typefind_factory_list,
766               &registry->priv->tfl_cookie, GST_TYPE_TYPE_FIND_FACTORY)))
767     registry->priv->typefind_factory_list =
768         g_list_sort (registry->priv->typefind_factory_list,
769         (GCompareFunc) type_find_factory_rank_cmp);
770
771   /* Return reffed copy */
772   list = gst_plugin_feature_list_copy (registry->priv->typefind_factory_list);
773
774   GST_OBJECT_UNLOCK (registry);
775
776   return list;
777 }
778
779 /**
780  * gst_registry_feature_filter:
781  * @registry: registry to query
782  * @filter: (scope call): the filter to use
783  * @first: only return first match
784  * @user_data: (closure): user data passed to the filter function
785  *
786  * Runs a filter against all features of the plugins in the registry
787  * and returns a GList with the results.
788  * If the first flag is set, only the first match is
789  * returned (as a list with a single object).
790  *
791  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
792  *     #GstPluginFeature. Use gst_plugin_feature_list_free() after usage.
793  *
794  * MT safe.
795  */
796 GList *
797 gst_registry_feature_filter (GstRegistry * registry,
798     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
799 {
800   GList *list = NULL;
801
802   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
803
804   GST_OBJECT_LOCK (registry);
805   {
806     const GList *walk;
807
808     for (walk = registry->priv->features; walk != NULL; walk = walk->next) {
809       GstPluginFeature *feature = walk->data;
810
811       if (filter == NULL || filter (feature, user_data)) {
812         list = g_list_prepend (list, gst_object_ref (feature));
813
814         if (first)
815           break;
816       }
817     }
818   }
819   GST_OBJECT_UNLOCK (registry);
820
821   return list;
822 }
823
824 /**
825  * gst_registry_find_plugin:
826  * @registry: the registry to search
827  * @name: the plugin name to find
828  *
829  * Find the plugin with the given name in the registry.
830  * The plugin will be reffed; caller is responsible for unreffing.
831  *
832  * Returns: (transfer full): the plugin with the given name or NULL if the
833  *     plugin was not found. gst_object_unref() after usage.
834  *
835  * MT safe.
836  */
837 GstPlugin *
838 gst_registry_find_plugin (GstRegistry * registry, const gchar * name)
839 {
840   GList *walk;
841   GstPlugin *result = NULL;
842
843   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
844   g_return_val_if_fail (name != NULL, NULL);
845
846   walk = gst_registry_plugin_filter (registry,
847       (GstPluginFilter) gst_plugin_name_filter, TRUE, (gpointer) name);
848   if (walk) {
849     result = GST_PLUGIN_CAST (walk->data);
850
851     gst_object_ref (result);
852     gst_plugin_list_free (walk);
853   }
854
855   return result;
856 }
857
858 /**
859  * gst_registry_find_feature:
860  * @registry: the registry to search
861  * @name: the pluginfeature name to find
862  * @type: the pluginfeature type to find
863  *
864  * Find the pluginfeature with the given name and type in the registry.
865  *
866  * Returns: (transfer full): the pluginfeature with the given name and type
867  *     or NULL if the plugin was not found. gst_object_unref() after usage.
868  *
869  * MT safe.
870  */
871 GstPluginFeature *
872 gst_registry_find_feature (GstRegistry * registry, const gchar * name,
873     GType type)
874 {
875   GstPluginFeature *feature = NULL;
876
877   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
878   g_return_val_if_fail (name != NULL, NULL);
879   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
880
881   feature = gst_registry_lookup_feature (registry, name);
882   if (feature && !g_type_is_a (G_TYPE_FROM_INSTANCE (feature), type)) {
883     gst_object_unref (feature);
884     feature = NULL;
885   }
886
887   return feature;
888 }
889
890 /**
891  * gst_registry_get_feature_list:
892  * @registry: a #GstRegistry
893  * @type: a #GType.
894  *
895  * Retrieves a #GList of #GstPluginFeature of @type.
896  *
897  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
898  *     #GstPluginFeature of @type. Use gst_plugin_feature_list_free() after use
899  *
900  * MT safe.
901  */
902 GList *
903 gst_registry_get_feature_list (GstRegistry * registry, GType type)
904 {
905   GstTypeNameData data;
906
907   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
908   g_return_val_if_fail (g_type_is_a (type, GST_TYPE_PLUGIN_FEATURE), NULL);
909
910   /* Speed up */
911   if (type == GST_TYPE_ELEMENT_FACTORY)
912     return gst_registry_get_element_factory_list (registry);
913   else if (type == GST_TYPE_TYPE_FIND_FACTORY)
914     return gst_registry_get_typefind_factory_list (registry);
915
916   data.type = type;
917   data.name = NULL;
918
919   return gst_registry_feature_filter (registry,
920       (GstPluginFeatureFilter) gst_plugin_feature_type_name_filter,
921       FALSE, &data);
922 }
923
924 /**
925  * gst_registry_get_plugin_list:
926  * @registry: the registry to search
927  *
928  * Get a copy of all plugins registered in the given registry. The refcount
929  * of each element in the list in incremented.
930  *
931  * Returns: (transfer full) (element-type Gst.Plugin): a #GList of #GstPlugin.
932  *     Use gst_plugin_list_free() after usage.
933  *
934  * MT safe.
935  */
936 GList *
937 gst_registry_get_plugin_list (GstRegistry * registry)
938 {
939   GList *list;
940   GList *g;
941
942   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
943
944   GST_OBJECT_LOCK (registry);
945   list = g_list_copy (registry->priv->plugins);
946   for (g = list; g; g = g->next) {
947     gst_object_ref (GST_PLUGIN_CAST (g->data));
948   }
949   GST_OBJECT_UNLOCK (registry);
950
951   return list;
952 }
953
954 static GstPluginFeature *
955 gst_registry_lookup_feature_locked (GstRegistry * registry, const char *name)
956 {
957   return g_hash_table_lookup (registry->priv->feature_hash, name);
958 }
959
960 /**
961  * gst_registry_lookup_feature:
962  * @registry: a #GstRegistry
963  * @name: a #GstPluginFeature name
964  *
965  * Find a #GstPluginFeature with @name in @registry.
966  *
967  * Returns: (transfer full): a #GstPluginFeature with its refcount incremented,
968  *     use gst_object_unref() after usage.
969  *
970  * MT safe.
971  */
972 GstPluginFeature *
973 gst_registry_lookup_feature (GstRegistry * registry, const char *name)
974 {
975   GstPluginFeature *feature;
976
977   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
978   g_return_val_if_fail (name != NULL, NULL);
979
980   GST_OBJECT_LOCK (registry);
981   feature = gst_registry_lookup_feature_locked (registry, name);
982   if (feature)
983     gst_object_ref (feature);
984   GST_OBJECT_UNLOCK (registry);
985
986   return feature;
987 }
988
989 static GstPlugin *
990 gst_registry_lookup_bn_locked (GstRegistry * registry, const char *basename)
991 {
992   return g_hash_table_lookup (registry->priv->basename_hash, basename);
993 }
994
995 static GstPlugin *
996 gst_registry_lookup_bn (GstRegistry * registry, const char *basename)
997 {
998   GstPlugin *plugin;
999
1000   GST_OBJECT_LOCK (registry);
1001   plugin = gst_registry_lookup_bn_locked (registry, basename);
1002   if (plugin)
1003     gst_object_ref (plugin);
1004   GST_OBJECT_UNLOCK (registry);
1005
1006   return plugin;
1007 }
1008
1009 /**
1010  * gst_registry_lookup:
1011  * @registry: the registry to look up in
1012  * @filename: the name of the file to look up
1013  *
1014  * Look up a plugin in the given registry with the given filename.
1015  * If found, plugin is reffed.
1016  *
1017  * Returns: (transfer full): the #GstPlugin if found, or NULL if not.
1018  *     gst_object_unref() after usage.
1019  */
1020 GstPlugin *
1021 gst_registry_lookup (GstRegistry * registry, const char *filename)
1022 {
1023   GstPlugin *plugin;
1024   gchar *basename;
1025
1026   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1027   g_return_val_if_fail (filename != NULL, NULL);
1028
1029   basename = g_path_get_basename (filename);
1030   if (G_UNLIKELY (basename == NULL))
1031     return NULL;
1032
1033   plugin = gst_registry_lookup_bn (registry, basename);
1034
1035   g_free (basename);
1036
1037   return plugin;
1038 }
1039
1040 typedef enum
1041 {
1042   REGISTRY_SCAN_HELPER_NOT_STARTED = 0,
1043   REGISTRY_SCAN_HELPER_DISABLED,
1044   REGISTRY_SCAN_HELPER_RUNNING
1045 } GstRegistryScanHelperState;
1046
1047 typedef struct
1048 {
1049   GstRegistry *registry;
1050   GstRegistryScanHelperState helper_state;
1051   GstPluginLoader *helper;
1052   gboolean changed;
1053 } GstRegistryScanContext;
1054
1055 static void
1056 init_scan_context (GstRegistryScanContext * context, GstRegistry * registry)
1057 {
1058   gboolean do_fork;
1059
1060   context->registry = registry;
1061
1062   /* see if forking is enabled and set up the scan helper state accordingly */
1063   do_fork = _gst_enable_registry_fork;
1064   if (do_fork) {
1065     const gchar *fork_env;
1066
1067     /* forking enabled, see if it is disabled with an env var */
1068     if ((fork_env = g_getenv ("GST_REGISTRY_FORK"))) {
1069       /* fork enabled for any value different from "no" */
1070       do_fork = strcmp (fork_env, "no") != 0;
1071     }
1072   }
1073
1074   if (do_fork)
1075     context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
1076   else
1077     context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1078
1079   context->helper = NULL;
1080   context->changed = FALSE;
1081 }
1082
1083 static void
1084 clear_scan_context (GstRegistryScanContext * context)
1085 {
1086   if (context->helper) {
1087     context->changed |= _priv_gst_plugin_loader_funcs.destroy (context->helper);
1088     context->helper = NULL;
1089   }
1090 }
1091
1092 static gboolean
1093 gst_registry_scan_plugin_file (GstRegistryScanContext * context,
1094     const gchar * filename, off_t file_size, time_t file_mtime)
1095 {
1096   gboolean changed = FALSE;
1097   GstPlugin *newplugin = NULL;
1098
1099 #ifdef G_OS_WIN32
1100   /* Disable external plugin loader on Windows until it is ported properly. */
1101   context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1102 #endif
1103
1104
1105   /* Have a plugin to load - see if the scan-helper needs starting */
1106   if (context->helper_state == REGISTRY_SCAN_HELPER_NOT_STARTED) {
1107     GST_DEBUG ("Starting plugin scanner for file %s", filename);
1108     context->helper = _priv_gst_plugin_loader_funcs.create (context->registry);
1109     if (context->helper != NULL)
1110       context->helper_state = REGISTRY_SCAN_HELPER_RUNNING;
1111     else {
1112       GST_WARNING ("Failed starting plugin scanner. Scanning in-process");
1113       context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1114     }
1115   }
1116
1117   if (context->helper_state == REGISTRY_SCAN_HELPER_RUNNING) {
1118     GST_DEBUG ("Using scan-helper to load plugin %s", filename);
1119     if (!_priv_gst_plugin_loader_funcs.load (context->helper,
1120             filename, file_size, file_mtime)) {
1121       g_warning ("External plugin loader failed. This most likely means that "
1122           "the plugin loader helper binary was not found or could not be run. "
1123           "%s", (g_getenv ("GST_PLUGIN_PATH") != NULL) ?
1124           "If you are running an uninstalled GStreamer setup, you might need "
1125           "to update your gst-uninstalled script so that the "
1126           "GST_PLUGIN_SCANNER environment variable gets set." : "");
1127       context->helper_state = REGISTRY_SCAN_HELPER_DISABLED;
1128     }
1129   }
1130
1131   /* Check if the helper is disabled (or just got disabled above) */
1132   if (context->helper_state == REGISTRY_SCAN_HELPER_DISABLED) {
1133     /* Load plugin the old fashioned way... */
1134
1135     /* We don't use a GError here because a failure to load some shared
1136      * objects as plugins is normal (particularly in the uninstalled case)
1137      */
1138     newplugin = gst_plugin_load_file (filename, NULL);
1139   }
1140
1141   if (newplugin) {
1142     GST_DEBUG_OBJECT (context->registry, "marking new plugin %p as registered",
1143         newplugin);
1144     newplugin->registered = TRUE;
1145     gst_object_unref (newplugin);
1146     changed = TRUE;
1147   }
1148
1149   if (!__registry_reuse_plugin_scanner) {
1150     clear_scan_context (context);
1151     context->helper_state = REGISTRY_SCAN_HELPER_NOT_STARTED;
1152   }
1153
1154   return changed;
1155 }
1156
1157 static gboolean
1158 is_blacklisted_hidden_directory (const gchar * dirent)
1159 {
1160   if (G_LIKELY (dirent[0] != '.'))
1161     return FALSE;
1162
1163   /* skip the .debug directory, these contain elf files that are not
1164    * useful or worse, can crash dlopen () */
1165   if (strcmp (dirent, ".debug") == 0)
1166     return TRUE;
1167
1168   /* can also skip .git and .deps dirs, those won't contain useful files.
1169    * This speeds up scanning a bit in uninstalled setups. */
1170   if (strcmp (dirent, ".git") == 0 || strcmp (dirent, ".deps") == 0)
1171     return TRUE;
1172
1173   return FALSE;
1174 }
1175
1176 static gboolean
1177 gst_registry_scan_path_level (GstRegistryScanContext * context,
1178     const gchar * path, int level)
1179 {
1180   GDir *dir;
1181   const gchar *dirent;
1182   gchar *filename;
1183   GstPlugin *plugin;
1184   gboolean changed = FALSE;
1185
1186   dir = g_dir_open (path, 0, NULL);
1187   if (!dir)
1188     return FALSE;
1189
1190   while ((dirent = g_dir_read_name (dir))) {
1191     GStatBuf file_status;
1192
1193     filename = g_build_filename (path, dirent, NULL);
1194     if (g_stat (filename, &file_status) < 0) {
1195       /* Plugin will be removed from cache after the scan completes if it
1196        * is still marked 'cached' */
1197       g_free (filename);
1198       continue;
1199     }
1200
1201     if (file_status.st_mode & S_IFDIR) {
1202       if (G_UNLIKELY (is_blacklisted_hidden_directory (dirent))) {
1203         GST_TRACE_OBJECT (context->registry, "ignoring %s directory", dirent);
1204         g_free (filename);
1205         continue;
1206       }
1207       /* FIXME 0.11: Don't recurse into directories, this behaviour
1208        * is inconsistent with other PATH environment variables
1209        */
1210       if (level > 0) {
1211         GST_LOG_OBJECT (context->registry, "recursing into directory %s",
1212             filename);
1213         changed |= gst_registry_scan_path_level (context, filename, level - 1);
1214       } else {
1215         GST_LOG_OBJECT (context->registry, "not recursing into directory %s, "
1216             "recursion level too deep", filename);
1217       }
1218       g_free (filename);
1219       continue;
1220     }
1221     if (!(file_status.st_mode & S_IFREG)) {
1222       GST_TRACE_OBJECT (context->registry, "%s is not a regular file, ignoring",
1223           filename);
1224       g_free (filename);
1225       continue;
1226     }
1227     if (!g_str_has_suffix (dirent, G_MODULE_SUFFIX)
1228 #ifdef GST_EXTRA_MODULE_SUFFIX
1229         && !g_str_has_suffix (dirent, GST_EXTRA_MODULE_SUFFIX)
1230 #endif
1231         ) {
1232       GST_TRACE_OBJECT (context->registry,
1233           "extension is not recognized as module file, ignoring file %s",
1234           filename);
1235       g_free (filename);
1236       continue;
1237     }
1238
1239     GST_LOG_OBJECT (context->registry, "file %s looks like a possible module",
1240         filename);
1241
1242     /* try to avoid unnecessary plugin-move pain */
1243     if (g_str_has_prefix (dirent, "libgstvalve") ||
1244         g_str_has_prefix (dirent, "libgstselector")) {
1245       GST_WARNING_OBJECT (context->registry, "ignoring old plugin %s which "
1246           "has been merged into the corelements plugin", filename);
1247       /* Plugin will be removed from cache after the scan completes if it
1248        * is still marked 'cached' */
1249       g_free (filename);
1250       continue;
1251     }
1252
1253     /* plug-ins are considered unique by basename; if the given name
1254      * was already seen by the registry, we ignore it */
1255     plugin = gst_registry_lookup_bn (context->registry, dirent);
1256     if (plugin) {
1257       gboolean env_vars_changed, deps_changed = FALSE;
1258
1259       if (plugin->registered) {
1260         GST_DEBUG_OBJECT (context->registry,
1261             "plugin already registered from path \"%s\"",
1262             GST_STR_NULL (plugin->filename));
1263         g_free (filename);
1264         gst_object_unref (plugin);
1265         continue;
1266       }
1267
1268       env_vars_changed = _priv_plugin_deps_env_vars_changed (plugin);
1269
1270       /* If a file with a certain basename is seen on a different path,
1271        * update the plugin to ensure the registry cache will reflect up
1272        * to date information */
1273
1274       if (plugin->file_mtime == file_status.st_mtime &&
1275           plugin->file_size == file_status.st_size && !env_vars_changed &&
1276           !(deps_changed = _priv_plugin_deps_files_changed (plugin)) &&
1277           !strcmp (plugin->filename, filename)) {
1278         GST_LOG_OBJECT (context->registry, "file %s cached", filename);
1279         plugin->flags &= ~GST_PLUGIN_FLAG_CACHED;
1280         GST_LOG_OBJECT (context->registry,
1281             "marking plugin %p as registered as %s", plugin, filename);
1282         plugin->registered = TRUE;
1283       } else {
1284         GST_INFO_OBJECT (context->registry, "cached info for %s is stale",
1285             filename);
1286         GST_DEBUG_OBJECT (context->registry, "mtime %" G_GINT64_FORMAT " != %"
1287             G_GINT64_FORMAT " or size %" G_GINT64_FORMAT " != %"
1288             G_GINT64_FORMAT " or external dependency env_vars changed: %d or"
1289             " external dependencies changed: %d or old path %s != new path %s",
1290             (gint64) plugin->file_mtime, (gint64) file_status.st_mtime,
1291             (gint64) plugin->file_size, (gint64) file_status.st_size,
1292             env_vars_changed, deps_changed, plugin->filename, filename);
1293         gst_registry_remove_plugin (context->registry, plugin);
1294         changed |= gst_registry_scan_plugin_file (context, filename,
1295             file_status.st_size, file_status.st_mtime);
1296       }
1297       gst_object_unref (plugin);
1298
1299     } else {
1300       GST_DEBUG_OBJECT (context->registry, "file %s not yet in registry",
1301           filename);
1302       changed |= gst_registry_scan_plugin_file (context, filename,
1303           file_status.st_size, file_status.st_mtime);
1304     }
1305
1306     g_free (filename);
1307   }
1308
1309   g_dir_close (dir);
1310
1311   return changed;
1312 }
1313
1314 static gboolean
1315 gst_registry_scan_path_internal (GstRegistryScanContext * context,
1316     const gchar * path)
1317 {
1318   gboolean changed;
1319
1320   GST_DEBUG_OBJECT (context->registry, "scanning path %s", path);
1321   changed = gst_registry_scan_path_level (context, path, 10);
1322
1323   GST_DEBUG_OBJECT (context->registry, "registry changed in path %s: %d", path,
1324       changed);
1325   return changed;
1326 }
1327
1328 /**
1329  * gst_registry_scan_path:
1330  * @registry: the registry to add found plugins to
1331  * @path: the path to scan
1332  *
1333  * Scan the given path for plugins to add to the registry. The syntax of the
1334  * path is specific to the registry.
1335  *
1336  * Returns: %TRUE if registry changed
1337  */
1338 gboolean
1339 gst_registry_scan_path (GstRegistry * registry, const gchar * path)
1340 {
1341   GstRegistryScanContext context;
1342   gboolean result;
1343
1344   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1345   g_return_val_if_fail (path != NULL, FALSE);
1346
1347   init_scan_context (&context, registry);
1348
1349   result = gst_registry_scan_path_internal (&context, path);
1350
1351   clear_scan_context (&context);
1352   result |= context.changed;
1353
1354   return result;
1355 }
1356
1357 static gboolean
1358 _gst_plugin_feature_filter_plugin_name (GstPluginFeature * feature,
1359     gpointer user_data)
1360 {
1361   return (strcmp (feature->plugin_name, (gchar *) user_data) == 0);
1362 }
1363
1364 /**
1365  * gst_registry_get_feature_list_by_plugin:
1366  * @registry: a #GstRegistry.
1367  * @name: a plugin name.
1368  *
1369  * Retrieves a #GList of features of the plugin with name @name.
1370  *
1371  * Returns: (transfer full) (element-type Gst.PluginFeature): a #GList of
1372  *     #GstPluginFeature. Use gst_plugin_feature_list_free() after usage.
1373  */
1374 GList *
1375 gst_registry_get_feature_list_by_plugin (GstRegistry * registry,
1376     const gchar * name)
1377 {
1378   g_return_val_if_fail (GST_IS_REGISTRY (registry), NULL);
1379   g_return_val_if_fail (name != NULL, NULL);
1380
1381   return gst_registry_feature_filter (registry,
1382       _gst_plugin_feature_filter_plugin_name, FALSE, (gpointer) name);
1383 }
1384
1385 /* Unref and delete the default registry */
1386 void
1387 _priv_gst_registry_cleanup (void)
1388 {
1389   GstRegistry *registry;
1390
1391   g_static_mutex_lock (&_gst_registry_mutex);
1392   if ((registry = _gst_registry_default) != NULL) {
1393     _gst_registry_default = NULL;
1394   }
1395   g_static_mutex_unlock (&_gst_registry_mutex);
1396
1397   /* unref outside of the lock because we can. */
1398   if (registry)
1399     gst_object_unref (registry);
1400 }
1401
1402 /**
1403  * gst_registry_check_feature_version:
1404  * @registry: a #GstRegistry
1405  * @feature_name: the name of the feature (e.g. "oggdemux")
1406  * @min_major: the minimum major version number
1407  * @min_minor: the minimum minor version number
1408  * @min_micro: the minimum micro version number
1409  *
1410  * Checks whether a plugin feature by the given name exists in
1411  * @registry and whether its version is at least the
1412  * version required.
1413  *
1414  * Returns: #TRUE if the feature could be found and the version is
1415  * the same as the required version or newer, and #FALSE otherwise.
1416  */
1417 gboolean
1418 gst_registry_check_feature_version (GstRegistry * registry,
1419     const gchar * feature_name, guint min_major, guint min_minor,
1420     guint min_micro)
1421 {
1422   GstPluginFeature *feature;
1423   gboolean ret = FALSE;
1424
1425   g_return_val_if_fail (feature_name != NULL, FALSE);
1426
1427   GST_DEBUG ("Looking up plugin feature '%s'", feature_name);
1428
1429   feature = gst_registry_lookup_feature (registry, feature_name);
1430   if (feature) {
1431     ret = gst_plugin_feature_check_version (feature, min_major, min_minor,
1432         min_micro);
1433     gst_object_unref (feature);
1434   } else {
1435     GST_DEBUG ("Could not find plugin feature '%s'", feature_name);
1436   }
1437
1438   return ret;
1439 }
1440
1441 static void
1442 load_plugin_func (gpointer data, gpointer user_data)
1443 {
1444   GstPlugin *plugin;
1445   const gchar *filename;
1446   GError *err = NULL;
1447
1448   filename = (const gchar *) data;
1449   GST_DEBUG ("Pre-loading plugin %s", filename);
1450
1451   plugin = gst_plugin_load_file (filename, &err);
1452
1453   if (plugin) {
1454     GST_INFO ("Loaded plugin: \"%s\"", filename);
1455
1456     gst_registry_add_plugin (gst_registry_get (), plugin);
1457   } else {
1458     if (err) {
1459       /* Report error to user, and free error */
1460       GST_ERROR ("Failed to load plugin: %s", err->message);
1461       g_error_free (err);
1462     } else {
1463       GST_WARNING ("Failed to load plugin: \"%s\"", filename);
1464     }
1465   }
1466 }
1467
1468 #ifndef GST_DISABLE_REGISTRY
1469 /* Unref all plugins marked 'cached', to clear old plugins that no
1470  * longer exist. Returns TRUE if any plugins were removed */
1471 static gboolean
1472 gst_registry_remove_cache_plugins (GstRegistry * registry)
1473 {
1474   GList *g;
1475   GList *g_next;
1476   GstPlugin *plugin;
1477   gboolean changed = FALSE;
1478
1479   g_return_val_if_fail (GST_IS_REGISTRY (registry), FALSE);
1480
1481   GST_OBJECT_LOCK (registry);
1482
1483   GST_DEBUG_OBJECT (registry, "removing cached plugins");
1484   g = registry->priv->plugins;
1485   while (g) {
1486     g_next = g->next;
1487     plugin = g->data;
1488     if (plugin->flags & GST_PLUGIN_FLAG_CACHED) {
1489       GST_DEBUG_OBJECT (registry, "removing cached plugin \"%s\"",
1490           GST_STR_NULL (plugin->filename));
1491       registry->priv->plugins = g_list_delete_link (registry->priv->plugins, g);
1492       if (G_LIKELY (plugin->basename))
1493         g_hash_table_remove (registry->priv->basename_hash, plugin->basename);
1494       gst_registry_remove_features_for_plugin_unlocked (registry, plugin);
1495       gst_object_unref (plugin);
1496       changed = TRUE;
1497     }
1498     g = g_next;
1499   }
1500
1501   GST_OBJECT_UNLOCK (registry);
1502
1503   return changed;
1504 }
1505
1506 typedef enum
1507 {
1508   REGISTRY_SCAN_AND_UPDATE_FAILURE = 0,
1509   REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED,
1510   REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED
1511 } GstRegistryScanAndUpdateResult;
1512
1513 /*
1514  * scan_and_update_registry:
1515  * @default_registry: the #GstRegistry
1516  * @registry_file: registry filename
1517  * @write_changes: write registry if it has changed?
1518  *
1519  * Scans for registry changes and eventually updates the registry cache.
1520  *
1521  * Return: %REGISTRY_SCAN_AND_UPDATE_FAILURE if the registry could not scanned
1522  *         or updated, %REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED if the
1523  *         registry is clean and %REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED if
1524  *         it has been updated and the cache needs to be re-read.
1525  */
1526 static GstRegistryScanAndUpdateResult
1527 scan_and_update_registry (GstRegistry * default_registry,
1528     const gchar * registry_file, gboolean write_changes, GError ** error)
1529 {
1530   const gchar *plugin_path;
1531   gboolean changed = FALSE;
1532   GList *l;
1533   GstRegistryScanContext context;
1534
1535   GST_INFO ("Validating plugins from registry cache: %s", registry_file);
1536
1537   init_scan_context (&context, default_registry);
1538
1539   /* It sounds tempting to just compare the mtime of directories with the mtime
1540    * of the registry cache, but it does not work. It would not catch updated
1541    * plugins, which might bring more or less features.
1542    */
1543
1544   /* scan paths specified via --gst-plugin-path */
1545   GST_DEBUG ("scanning paths added via --gst-plugin-path");
1546   for (l = _priv_gst_plugin_paths; l != NULL; l = l->next) {
1547     GST_INFO ("Scanning plugin path: \"%s\"", (gchar *) l->data);
1548     changed |= gst_registry_scan_path_internal (&context, (gchar *) l->data);
1549   }
1550   /* keep plugin_paths around in case a re-scan is forced later on */
1551
1552   /* GST_PLUGIN_PATH specifies a list of directories to scan for
1553    * additional plugins.  These take precedence over the system plugins */
1554   plugin_path = g_getenv ("GST_PLUGIN_PATH");
1555   if (plugin_path) {
1556     char **list;
1557     int i;
1558
1559     GST_DEBUG ("GST_PLUGIN_PATH set to %s", plugin_path);
1560     list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1561     for (i = 0; list[i]; i++) {
1562       changed |= gst_registry_scan_path_internal (&context, list[i]);
1563     }
1564     g_strfreev (list);
1565   } else {
1566     GST_DEBUG ("GST_PLUGIN_PATH not set");
1567   }
1568
1569   /* GST_PLUGIN_SYSTEM_PATH specifies a list of plugins that are always
1570    * loaded by default.  If not set, this defaults to the system-installed
1571    * path, and the plugins installed in the user's home directory */
1572   plugin_path = g_getenv ("GST_PLUGIN_SYSTEM_PATH");
1573   if (plugin_path == NULL) {
1574     char *home_plugins;
1575
1576     GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH not set");
1577
1578     /* plugins in the user's home directory take precedence over
1579      * system-installed ones */
1580     home_plugins = g_build_filename (g_get_user_data_dir (),
1581         "gstreamer-" GST_MAJORMINOR, "plugins", NULL);
1582
1583     GST_DEBUG ("scanning home plugins %s", home_plugins);
1584     changed |= gst_registry_scan_path_internal (&context, home_plugins);
1585     g_free (home_plugins);
1586
1587     /* add the main (installed) library path */
1588     GST_DEBUG ("scanning main plugins %s", PLUGINDIR);
1589     changed |= gst_registry_scan_path_internal (&context, PLUGINDIR);
1590
1591 #ifdef G_OS_WIN32
1592     {
1593       char *base_dir;
1594       char *dir;
1595
1596       base_dir =
1597           g_win32_get_package_installation_directory_of_module
1598           (_priv_gst_dll_handle);
1599
1600       dir = g_build_filename (base_dir, "lib", "gstreamer-0.10", NULL);
1601       GST_DEBUG ("scanning DLL dir %s", dir);
1602
1603       changed |= gst_registry_scan_path_internal (&context, dir);
1604
1605       g_free (dir);
1606       g_free (base_dir);
1607     }
1608 #endif
1609   } else {
1610     gchar **list;
1611     gint i;
1612
1613     GST_DEBUG ("GST_PLUGIN_SYSTEM_PATH set to %s", plugin_path);
1614     list = g_strsplit (plugin_path, G_SEARCHPATH_SEPARATOR_S, 0);
1615     for (i = 0; list[i]; i++) {
1616       changed |= gst_registry_scan_path_internal (&context, list[i]);
1617     }
1618     g_strfreev (list);
1619   }
1620
1621   clear_scan_context (&context);
1622   changed |= context.changed;
1623
1624   /* Remove cached plugins so stale info is cleared. */
1625   changed |= gst_registry_remove_cache_plugins (default_registry);
1626
1627   if (!changed) {
1628     GST_INFO ("Registry cache has not changed");
1629     return REGISTRY_SCAN_AND_UPDATE_SUCCESS_NOT_CHANGED;
1630   }
1631
1632   if (!write_changes) {
1633     GST_INFO ("Registry cache changed, but writing is disabled. Not writing.");
1634     return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1635   }
1636
1637   GST_INFO ("Registry cache changed. Writing new registry cache");
1638   if (!priv_gst_registry_binary_write_cache (default_registry,
1639           default_registry->priv->plugins, registry_file)) {
1640     g_set_error (error, GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
1641         _("Error writing registry cache to %s: %s"),
1642         registry_file, g_strerror (errno));
1643     return REGISTRY_SCAN_AND_UPDATE_FAILURE;
1644   }
1645
1646   GST_INFO ("Registry cache written successfully");
1647   return REGISTRY_SCAN_AND_UPDATE_SUCCESS_UPDATED;
1648 }
1649
1650 static gboolean
1651 ensure_current_registry (GError ** error)
1652 {
1653   gchar *registry_file;
1654   GstRegistry *default_registry;
1655   gboolean ret = TRUE;
1656   gboolean do_update = TRUE;
1657   gboolean have_cache = TRUE;
1658
1659   default_registry = gst_registry_get ();
1660   registry_file = g_strdup (g_getenv ("GST_REGISTRY"));
1661   if (registry_file == NULL) {
1662     registry_file = g_build_filename (g_get_user_cache_dir (),
1663         "gstreamer-" GST_MAJORMINOR, "registry." HOST_CPU ".bin", NULL);
1664   }
1665
1666   if (!_gst_disable_registry_cache) {
1667     GST_INFO ("reading registry cache: %s", registry_file);
1668     have_cache = priv_gst_registry_binary_read_cache (default_registry,
1669         registry_file);
1670     /* Only ever read the registry cache once, then disable it for
1671      * subsequent updates during the program lifetime */
1672     _gst_disable_registry_cache = TRUE;
1673   }
1674
1675   if (have_cache) {
1676     do_update = !_priv_gst_disable_registry_update;
1677     if (do_update) {
1678       const gchar *update_env;
1679
1680       if ((update_env = g_getenv ("GST_REGISTRY_UPDATE"))) {
1681         /* do update for any value different from "no" */
1682         do_update = (strcmp (update_env, "no") != 0);
1683       }
1684     }
1685   }
1686
1687   if (do_update) {
1688     const gchar *reuse_env;
1689
1690     if ((reuse_env = g_getenv ("GST_REGISTRY_REUSE_PLUGIN_SCANNER"))) {
1691       /* do reuse for any value different from "no" */
1692       __registry_reuse_plugin_scanner = (strcmp (reuse_env, "no") != 0);
1693     }
1694     /* now check registry */
1695     GST_DEBUG ("Updating registry cache");
1696     scan_and_update_registry (default_registry, registry_file, TRUE, error);
1697   } else {
1698     GST_DEBUG ("Not updating registry cache (disabled)");
1699   }
1700
1701   g_free (registry_file);
1702   GST_INFO ("registry reading and updating done, result = %d", ret);
1703
1704   return ret;
1705 }
1706 #endif /* GST_DISABLE_REGISTRY */
1707
1708 /**
1709  * gst_registry_fork_is_enabled:
1710  *
1711  * By default GStreamer will perform scanning and rebuilding of the
1712  * registry file using a helper child process.
1713  *
1714  * Applications might want to disable this behaviour with the
1715  * gst_registry_fork_set_enabled() function, in which case new plugins
1716  * are scanned (and loaded) into the application process.
1717  *
1718  * Returns: %TRUE if GStreamer will use the child helper process when
1719  * rebuilding the registry.
1720  *
1721  * Since: 0.10.10
1722  */
1723 gboolean
1724 gst_registry_fork_is_enabled (void)
1725 {
1726   return _gst_enable_registry_fork;
1727 }
1728
1729 /**
1730  * gst_registry_fork_set_enabled:
1731  * @enabled: whether rebuilding the registry can use a temporary child helper process.
1732  *
1733  * Applications might want to disable/enable spawning of a child helper process
1734  * when rebuilding the registry. See gst_registry_fork_is_enabled() for more
1735  * information.
1736  *
1737  * Since: 0.10.10
1738  */
1739 void
1740 gst_registry_fork_set_enabled (gboolean enabled)
1741 {
1742   _gst_enable_registry_fork = enabled;
1743 }
1744
1745 /**
1746  * gst_update_registry:
1747  *
1748  * Forces GStreamer to re-scan its plugin paths and update the default
1749  * plugin registry.
1750  *
1751  * Applications will almost never need to call this function, it is only
1752  * useful if the application knows new plugins have been installed (or old
1753  * ones removed) since the start of the application (or, to be precise, the
1754  * first call to gst_init()) and the application wants to make use of any
1755  * newly-installed plugins without restarting the application.
1756  *
1757  * Applications should assume that the registry update is neither atomic nor
1758  * thread-safe and should therefore not have any dynamic pipelines running
1759  * (including the playbin and decodebin elements) and should also not create
1760  * any elements or access the GStreamer registry while the update is in
1761  * progress.
1762  *
1763  * Note that this function may block for a significant amount of time.
1764  *
1765  * Returns: %TRUE if the registry has been updated successfully (does not
1766  *          imply that there were changes), otherwise %FALSE.
1767  *
1768  * Since: 0.10.12
1769  */
1770 gboolean
1771 gst_update_registry (void)
1772 {
1773   gboolean res;
1774
1775 #ifndef GST_DISABLE_REGISTRY
1776   GError *err = NULL;
1777
1778   res = ensure_current_registry (&err);
1779   if (err) {
1780     GST_WARNING ("registry update failed: %s", err->message);
1781     g_error_free (err);
1782   } else {
1783     GST_LOG ("registry update succeeded");
1784   }
1785
1786 #else
1787   GST_WARNING ("registry update failed: %s", "registry disabled");
1788   res = TRUE;
1789 #endif /* GST_DISABLE_REGISTRY */
1790
1791   if (_priv_gst_preload_plugins) {
1792     GST_DEBUG ("Preloading indicated plugins...");
1793     g_slist_foreach (_priv_gst_preload_plugins, load_plugin_func, NULL);
1794   }
1795
1796   return res;
1797 }
1798
1799 /**
1800  * gst_registry_get_feature_list_cookie:
1801  * @registry: the registry
1802  *
1803  * Returns the registrys feature list cookie. This changes
1804  * every time a feature is added or removed from the registry.
1805  *
1806  * Returns: the feature list cookie.
1807  *
1808  * Since: 0.10.26
1809  */
1810 guint32
1811 gst_registry_get_feature_list_cookie (GstRegistry * registry)
1812 {
1813   g_return_val_if_fail (GST_IS_REGISTRY (registry), 0);
1814
1815   return registry->priv->cookie;
1816 }