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