remove deprecated symbols and methods
[platform/upstream/gstreamer.git] / gst / gstplugin.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstplugin.c: Plugin subsystem for loading elements, types, and libs
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /**
24  * SECTION:gstplugin
25  * @short_description: Container for features loaded from a shared object module
26  * @see_also: #GstPluginFeature, #GstElementFactory
27  *
28  * GStreamer is extensible, so #GstElement instances can be loaded at runtime.
29  * A plugin system can provide one or more of the basic
30  * <application>GStreamer</application> #GstPluginFeature subclasses.
31  *
32  * A plugin should export a symbol <symbol>gst_plugin_desc</symbol> that is a
33  * struct of type #GstPluginDesc.
34  * the plugin loader will check the version of the core library the plugin was
35  * linked against and will create a new #GstPlugin. It will then call the
36  * #GstPluginInitFunc function that was provided in the
37  * <symbol>gst_plugin_desc</symbol>.
38  *
39  * Once you have a handle to a #GstPlugin (e.g. from the #GstRegistry), you
40  * can add any object that subclasses #GstPluginFeature.
41  *
42  * Usually plugins are always automaticlly loaded so you don't need to call
43  * gst_plugin_load() explicitly to bring it into memory. There are options to
44  * statically link plugins to an app or even use GStreamer without a plugin
45  * repository in which case gst_plugin_load() can be needed to bring the plugin
46  * into memory.
47  */
48
49 #ifdef HAVE_CONFIG_H
50 #include "config.h"
51 #endif
52
53 #include "gst_private.h"
54
55 #include <glib/gstdio.h>
56 #include <sys/types.h>
57 #ifdef HAVE_DIRENT_H
58 #include <dirent.h>
59 #endif
60 #ifdef HAVE_UNISTD_H
61 #include <unistd.h>
62 #endif
63 #include <signal.h>
64 #include <errno.h>
65 #include <string.h>
66
67 #include "glib-compat-private.h"
68
69 #include <gst/gst.h>
70
71 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
72
73 static guint _num_static_plugins;       /* 0    */
74 static GstPluginDesc *_static_plugins;  /* NULL */
75 static gboolean _gst_plugin_inited;
76 static gchar **_plugin_loading_whitelist;       /* NULL */
77
78 /* static variables for segfault handling of plugin loading */
79 static char *_gst_plugin_fault_handler_filename = NULL;
80
81 /* list of valid licenses.
82  * One of these must be specified or the plugin won't be loaded
83  * Contact gstreamer-devel@lists.sourceforge.net if your license should be
84  * added.
85  *
86  * GPL: http://www.gnu.org/copyleft/gpl.html
87  * LGPL: http://www.gnu.org/copyleft/lesser.html
88  * QPL: http://www.trolltech.com/licenses/qpl.html
89  * MPL: http://www.opensource.org/licenses/mozilla1.1.php
90  * MIT/X11: http://www.opensource.org/licenses/mit-license.php
91  * 3-clause BSD: http://www.opensource.org/licenses/bsd-license.php
92  */
93 static const gchar *valid_licenses[] = {
94   "LGPL",                       /* GNU Lesser General Public License */
95   "GPL",                        /* GNU General Public License */
96   "QPL",                        /* Trolltech Qt Public License */
97   "GPL/QPL",                    /* Combi-license of GPL + QPL */
98   "MPL",                        /* MPL 1.1 license */
99   "BSD",                        /* 3-clause BSD license */
100   "MIT/X11",                    /* MIT/X11 license */
101   "Proprietary",                /* Proprietary license */
102   GST_LICENSE_UNKNOWN,          /* some other license */
103   NULL
104 };
105
106 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
107     const GstPluginDesc * desc, gpointer user_data);
108 static void gst_plugin_desc_copy (GstPluginDesc * dest,
109     const GstPluginDesc * src);
110
111 static void gst_plugin_ext_dep_free (GstPluginDep * dep);
112
113 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
114
115 static void
116 gst_plugin_init (GstPlugin * plugin)
117 {
118   plugin->priv =
119       G_TYPE_INSTANCE_GET_PRIVATE (plugin, GST_TYPE_PLUGIN, GstPluginPrivate);
120 }
121
122 static void
123 gst_plugin_finalize (GObject * object)
124 {
125   GstPlugin *plugin = GST_PLUGIN_CAST (object);
126   GstRegistry *registry = gst_registry_get_default ();
127   GList *g;
128
129   GST_DEBUG ("finalizing plugin %" GST_PTR_FORMAT, plugin);
130   for (g = registry->plugins; g; g = g->next) {
131     if (g->data == (gpointer) plugin) {
132       g_warning ("removing plugin that is still in registry");
133     }
134   }
135   g_free (plugin->filename);
136   g_free (plugin->basename);
137
138   g_list_foreach (plugin->priv->deps, (GFunc) gst_plugin_ext_dep_free, NULL);
139   g_list_free (plugin->priv->deps);
140   plugin->priv->deps = NULL;
141
142   if (plugin->priv->cache_data) {
143     gst_structure_free (plugin->priv->cache_data);
144   }
145
146   G_OBJECT_CLASS (gst_plugin_parent_class)->finalize (object);
147 }
148
149 static void
150 gst_plugin_class_init (GstPluginClass * klass)
151 {
152   G_OBJECT_CLASS (klass)->finalize = gst_plugin_finalize;
153
154   g_type_class_add_private (klass, sizeof (GstPluginPrivate));
155 }
156
157 GQuark
158 gst_plugin_error_quark (void)
159 {
160   static GQuark quark = 0;
161
162   if (!quark)
163     quark = g_quark_from_static_string ("gst_plugin_error");
164   return quark;
165 }
166
167 /**
168  * gst_plugin_register_static:
169  * @major_version: the major version number of the GStreamer core that the
170  *     plugin was compiled for, you can just use GST_VERSION_MAJOR here
171  * @minor_version: the minor version number of the GStreamer core that the
172  *     plugin was compiled for, you can just use GST_VERSION_MINOR here
173  * @name: a unique name of the plugin (ideally prefixed with an application- or
174  *     library-specific namespace prefix in order to avoid name conflicts in
175  *     case a similar plugin with the same name ever gets added to GStreamer)
176  * @description: description of the plugin
177  * @init_func: pointer to the init function of this plugin.
178  * @version: version string of the plugin
179  * @license: effective license of plugin. Must be one of the approved licenses
180  *     (see #GstPluginDesc above) or the plugin will not be registered.
181  * @source: source module plugin belongs to
182  * @package: shipped package plugin belongs to
183  * @origin: URL to provider of plugin
184  *
185  * Registers a static plugin, ie. a plugin which is private to an application
186  * or library and contained within the application or library (as opposed to
187  * being shipped as a separate module file).
188  *
189  * You must make sure that GStreamer has been initialised (with gst_init() or
190  * via gst_init_get_option_group()) before calling this function.
191  *
192  * Returns: TRUE if the plugin was registered correctly, otherwise FALSE.
193  *
194  * Since: 0.10.16
195  */
196 gboolean
197 gst_plugin_register_static (gint major_version, gint minor_version,
198     const gchar * name, const gchar * description, GstPluginInitFunc init_func,
199     const gchar * version, const gchar * license, const gchar * source,
200     const gchar * package, const gchar * origin)
201 {
202   GstPluginDesc desc = { major_version, minor_version, name, description,
203     init_func, version, license, source, package, origin, NULL,
204   };
205   GstPlugin *plugin;
206   gboolean res = FALSE;
207
208   g_return_val_if_fail (name != NULL, FALSE);
209   g_return_val_if_fail (description != NULL, FALSE);
210   g_return_val_if_fail (init_func != NULL, FALSE);
211   g_return_val_if_fail (version != NULL, FALSE);
212   g_return_val_if_fail (license != NULL, FALSE);
213   g_return_val_if_fail (source != NULL, FALSE);
214   g_return_val_if_fail (package != NULL, FALSE);
215   g_return_val_if_fail (origin != NULL, FALSE);
216
217   /* make sure gst_init() has been called */
218   g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);
219
220   GST_LOG ("attempting to load static plugin \"%s\" now...", name);
221   plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
222   if (gst_plugin_register_func (plugin, &desc, NULL) != NULL) {
223     GST_INFO ("registered static plugin \"%s\"", name);
224     res = gst_default_registry_add_plugin (plugin);
225     GST_INFO ("added static plugin \"%s\", result: %d", name, res);
226   }
227   return res;
228 }
229
230 /**
231  * gst_plugin_register_static_full:
232  * @major_version: the major version number of the GStreamer core that the
233  *     plugin was compiled for, you can just use GST_VERSION_MAJOR here
234  * @minor_version: the minor version number of the GStreamer core that the
235  *     plugin was compiled for, you can just use GST_VERSION_MINOR here
236  * @name: a unique name of the plugin (ideally prefixed with an application- or
237  *     library-specific namespace prefix in order to avoid name conflicts in
238  *     case a similar plugin with the same name ever gets added to GStreamer)
239  * @description: description of the plugin
240  * @init_full_func: pointer to the init function with user data of this plugin.
241  * @version: version string of the plugin
242  * @license: effective license of plugin. Must be one of the approved licenses
243  *     (see #GstPluginDesc above) or the plugin will not be registered.
244  * @source: source module plugin belongs to
245  * @package: shipped package plugin belongs to
246  * @origin: URL to provider of plugin
247  * @user_data: gpointer to user data
248  *
249  * Registers a static plugin, ie. a plugin which is private to an application
250  * or library and contained within the application or library (as opposed to
251  * being shipped as a separate module file) with a #GstPluginInitFullFunc
252  * which allows user data to be passed to the callback function (useful
253  * for bindings).
254  *
255  * You must make sure that GStreamer has been initialised (with gst_init() or
256  * via gst_init_get_option_group()) before calling this function.
257  *
258  * Returns: TRUE if the plugin was registered correctly, otherwise FALSE.
259  *
260  * Since: 0.10.24
261  *
262  */
263 gboolean
264 gst_plugin_register_static_full (gint major_version, gint minor_version,
265     const gchar * name, const gchar * description,
266     GstPluginInitFullFunc init_full_func, const gchar * version,
267     const gchar * license, const gchar * source, const gchar * package,
268     const gchar * origin, gpointer user_data)
269 {
270   GstPluginDesc desc = { major_version, minor_version, name, description,
271     (GstPluginInitFunc) init_full_func, version, license, source, package,
272     origin, NULL,
273   };
274   GstPlugin *plugin;
275   gboolean res = FALSE;
276
277   g_return_val_if_fail (name != NULL, FALSE);
278   g_return_val_if_fail (description != NULL, FALSE);
279   g_return_val_if_fail (init_full_func != NULL, FALSE);
280   g_return_val_if_fail (version != NULL, FALSE);
281   g_return_val_if_fail (license != NULL, FALSE);
282   g_return_val_if_fail (source != NULL, FALSE);
283   g_return_val_if_fail (package != NULL, FALSE);
284   g_return_val_if_fail (origin != NULL, FALSE);
285
286   /* make sure gst_init() has been called */
287   g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);
288
289   GST_LOG ("attempting to load static plugin \"%s\" now...", name);
290   plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
291   if (gst_plugin_register_func (plugin, &desc, user_data) != NULL) {
292     GST_INFO ("registered static plugin \"%s\"", name);
293     res = gst_default_registry_add_plugin (plugin);
294     GST_INFO ("added static plugin \"%s\", result: %d", name, res);
295   }
296   return res;
297 }
298
299 void
300 _gst_plugin_initialize (void)
301 {
302   const gchar *whitelist;
303   guint i;
304
305   _gst_plugin_inited = TRUE;
306
307   whitelist = g_getenv ("GST_PLUGIN_LOADING_WHITELIST");
308   if (whitelist != NULL && *whitelist != '\0') {
309     _plugin_loading_whitelist = g_strsplit (whitelist,
310         G_SEARCHPATH_SEPARATOR_S, -1);
311     for (i = 0; _plugin_loading_whitelist[i] != NULL; ++i) {
312       GST_INFO ("plugins whitelist entry: %s", _plugin_loading_whitelist[i]);
313     }
314   }
315
316   /* now register all static plugins */
317   GST_INFO ("registering %u static plugins", _num_static_plugins);
318   for (i = 0; i < _num_static_plugins; ++i) {
319     gst_plugin_register_static (_static_plugins[i].major_version,
320         _static_plugins[i].minor_version, _static_plugins[i].name,
321         _static_plugins[i].description, _static_plugins[i].plugin_init,
322         _static_plugins[i].version, _static_plugins[i].license,
323         _static_plugins[i].source, _static_plugins[i].package,
324         _static_plugins[i].origin);
325   }
326
327   if (_static_plugins) {
328     free (_static_plugins);
329     _static_plugins = NULL;
330     _num_static_plugins = 0;
331   }
332 }
333
334 /* Whitelist entry format:
335  *
336  *   plugin1,plugin2@pathprefix or
337  *   plugin1,plugin2@* or just
338  *   plugin1,plugin2 or
339  *   source-package@pathprefix or
340  *   source-package@* or just
341  *   source-package
342  *
343  * ie. the bit before the path will be checked against both the plugin
344  * name and the plugin's source package name, to keep the format simple.
345  */
346 static gboolean
347 gst_plugin_desc_matches_whitelist_entry (GstPluginDesc * desc,
348     const gchar * filename, const gchar * pattern)
349 {
350   const gchar *sep;
351   gboolean ret = FALSE;
352   gchar *name;
353
354   GST_LOG ("Whitelist pattern '%s', plugin: %s of %s@%s", pattern, desc->name,
355       desc->source, GST_STR_NULL (filename));
356
357   /* do we have a path prefix? */
358   sep = strchr (pattern, '@');
359   if (sep != NULL && strcmp (sep, "@*") != 0 && strcmp (sep, "@") != 0) {
360     /* paths are not canonicalised or treated with realpath() here. This
361      * should be good enough for our use case, since we just use the paths
362      * autotools uses, and those will be constructed from the same prefix. */
363     if (filename != NULL && !g_str_has_prefix (filename, sep + 1))
364       return FALSE;
365
366     GST_LOG ("%s matches path prefix %s", GST_STR_NULL (filename), sep + 1);
367   }
368
369   if (sep != NULL) {
370     name = g_strndup (pattern, (gsize) (sep - pattern));
371   } else {
372     name = g_strdup (pattern);
373   }
374
375   g_strstrip (name);
376   if (!g_ascii_isalnum (*name)) {
377     GST_WARNING ("Invalid whitelist pattern: %s", pattern);
378     goto done;
379   }
380
381   /* now check plugin names / source package name */
382   if (strchr (name, ',') == NULL) {
383     /* only a single name: either a plugin name or the source package name */
384     ret = (strcmp (desc->source, name) == 0 || strcmp (desc->name, name) == 0);
385   } else {
386     gchar **n, **names;
387
388     /* multiple names: assume these are plugin names */
389     names = g_strsplit (name, ",", -1);
390     for (n = names; n != NULL && *n != NULL; ++n) {
391       g_strstrip (*n);
392       if (strcmp (desc->name, *n) == 0) {
393         ret = TRUE;
394         break;
395       }
396     }
397     g_strfreev (names);
398   }
399
400   GST_LOG ("plugin / source package name match: %d", ret);
401
402 done:
403
404   g_free (name);
405   return ret;
406 }
407
408 gboolean
409 priv_gst_plugin_desc_is_whitelisted (GstPluginDesc * desc,
410     const gchar * filename)
411 {
412   gchar **entry;
413
414   if (_plugin_loading_whitelist == NULL)
415     return TRUE;
416
417   for (entry = _plugin_loading_whitelist; *entry != NULL; ++entry) {
418     if (gst_plugin_desc_matches_whitelist_entry (desc, filename, *entry)) {
419       GST_LOG ("Plugin %s is in whitelist", filename);
420       return TRUE;
421     }
422   }
423
424   GST_LOG ("Plugin %s (package %s, file %s) not in whitelist", desc->name,
425       desc->source, filename);
426   return FALSE;
427 }
428
429 gboolean
430 priv_gst_plugin_loading_have_whitelist (void)
431 {
432   return (_plugin_loading_whitelist != NULL);
433 }
434
435 guint32
436 priv_gst_plugin_loading_get_whitelist_hash (void)
437 {
438   guint32 hash = 0;
439
440   if (_plugin_loading_whitelist != NULL) {
441     gchar **w;
442
443     for (w = _plugin_loading_whitelist; *w != NULL; ++w)
444       hash = (hash << 1) ^ g_str_hash (*w);
445   }
446
447   return hash;
448 }
449
450 /* this function could be extended to check if the plugin license matches the
451  * applications license (would require the app to register its license somehow).
452  * We'll wait for someone who's interested in it to code it :)
453  */
454 static gboolean
455 gst_plugin_check_license (const gchar * license)
456 {
457   const gchar **check_license = valid_licenses;
458
459   g_assert (check_license);
460
461   while (*check_license) {
462     if (strcmp (license, *check_license) == 0)
463       return TRUE;
464     check_license++;
465   }
466   return FALSE;
467 }
468
469 static gboolean
470 gst_plugin_check_version (gint major, gint minor)
471 {
472   /* return NULL if the major and minor version numbers are not compatible */
473   /* with ours. */
474   if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
475     return FALSE;
476
477   return TRUE;
478 }
479
480 static GstPlugin *
481 gst_plugin_register_func (GstPlugin * plugin, const GstPluginDesc * desc,
482     gpointer user_data)
483 {
484   if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
485     if (GST_CAT_DEFAULT)
486       GST_WARNING ("plugin \"%s\" has incompatible version, not loading",
487           plugin->filename);
488     return NULL;
489   }
490
491   if (!desc->license || !desc->description || !desc->source ||
492       !desc->package || !desc->origin) {
493     if (GST_CAT_DEFAULT)
494       GST_WARNING ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
495           plugin->filename);
496     return NULL;
497   }
498
499   if (!gst_plugin_check_license (desc->license)) {
500     if (GST_CAT_DEFAULT)
501       GST_WARNING ("plugin \"%s\" has invalid license \"%s\", not loading",
502           plugin->filename, desc->license);
503     return NULL;
504   }
505
506   if (GST_CAT_DEFAULT)
507     GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
508
509   gst_plugin_desc_copy (&plugin->desc, desc);
510
511   /* make resident so we're really sure it never gets unloaded again.
512    * Theoretically this is not needed, but practically it doesn't hurt.
513    * And we're rather safe than sorry. */
514   if (plugin->module)
515     g_module_make_resident (plugin->module);
516
517   if (user_data) {
518     if (!(((GstPluginInitFullFunc) (desc->plugin_init)) (plugin, user_data))) {
519       if (GST_CAT_DEFAULT)
520         GST_WARNING ("plugin \"%s\" failed to initialise", plugin->filename);
521       return NULL;
522     }
523   } else {
524     if (!((desc->plugin_init) (plugin))) {
525       if (GST_CAT_DEFAULT)
526         GST_WARNING ("plugin \"%s\" failed to initialise", plugin->filename);
527       return NULL;
528     }
529   }
530
531   if (GST_CAT_DEFAULT)
532     GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
533
534   return plugin;
535 }
536
537 #ifdef HAVE_SIGACTION
538 static struct sigaction oldaction;
539 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
540
541 /*
542  * _gst_plugin_fault_handler_restore:
543  * segfault handler restorer
544  */
545 static void
546 _gst_plugin_fault_handler_restore (void)
547 {
548   if (!_gst_plugin_fault_handler_is_setup)
549     return;
550
551   _gst_plugin_fault_handler_is_setup = FALSE;
552
553   sigaction (SIGSEGV, &oldaction, NULL);
554 }
555
556 /*
557  * _gst_plugin_fault_handler_sighandler:
558  * segfault handler implementation
559  */
560 static void
561 _gst_plugin_fault_handler_sighandler (int signum)
562 {
563   /* We need to restore the fault handler or we'll keep getting it */
564   _gst_plugin_fault_handler_restore ();
565
566   switch (signum) {
567     case SIGSEGV:
568       g_print ("\nERROR: ");
569       g_print ("Caught a segmentation fault while loading plugin file:\n");
570       g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
571       g_print ("Please either:\n");
572       g_print ("- remove it and restart.\n");
573       g_print ("- run with --gst-disable-segtrap and debug.\n");
574       exit (-1);
575       break;
576     default:
577       g_print ("Caught unhandled signal on plugin loading\n");
578       break;
579   }
580 }
581
582 /*
583  * _gst_plugin_fault_handler_setup:
584  * sets up the segfault handler
585  */
586 static void
587 _gst_plugin_fault_handler_setup (void)
588 {
589   struct sigaction action;
590
591   /* if asked to leave segfaults alone, just return */
592   if (!gst_segtrap_is_enabled ())
593     return;
594
595   if (_gst_plugin_fault_handler_is_setup)
596     return;
597
598   _gst_plugin_fault_handler_is_setup = TRUE;
599
600   memset (&action, 0, sizeof (action));
601   action.sa_handler = _gst_plugin_fault_handler_sighandler;
602
603   sigaction (SIGSEGV, &action, &oldaction);
604 }
605 #else /* !HAVE_SIGACTION */
606 static void
607 _gst_plugin_fault_handler_restore (void)
608 {
609 }
610
611 static void
612 _gst_plugin_fault_handler_setup (void)
613 {
614 }
615 #endif /* HAVE_SIGACTION */
616
617 /* g_time_val_from_iso8601() doesn't do quite what we want */
618 static gboolean
619 check_release_datetime (const gchar * date_time)
620 {
621   guint64 val;
622
623   /* we require YYYY-MM-DD or YYYY-MM-DDTHH:MMZ format */
624   if (!g_ascii_isdigit (*date_time))
625     return FALSE;
626
627   val = g_ascii_strtoull (date_time, (gchar **) & date_time, 10);
628   if (val < 2000 || val > 2100 || *date_time != '-')
629     return FALSE;
630
631   val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
632   if (val == 0 || val > 12 || *date_time != '-')
633     return FALSE;
634
635   val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
636   if (val == 0 || val > 32)
637     return FALSE;
638
639   /* end of string or date/time separator + HH:MMZ */
640   if (*date_time == 'T' || *date_time == ' ') {
641     val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
642     if (val > 24 || *date_time != ':')
643       return FALSE;
644
645     val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
646     if (val > 59 || *date_time != 'Z')
647       return FALSE;
648
649     ++date_time;
650   }
651
652   return (*date_time == '\0');
653 }
654
655 static GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
656
657 #define CHECK_PLUGIN_DESC_FIELD(desc,field,fn)                               \
658   if (G_UNLIKELY ((desc)->field == NULL)) {                                  \
659     GST_ERROR ("GstPluginDesc for '%s' has no %s", fn, G_STRINGIFY (field)); \
660   }
661
662 /**
663  * gst_plugin_load_file:
664  * @filename: the plugin filename to load
665  * @error: pointer to a NULL-valued GError
666  *
667  * Loads the given plugin and refs it.  Caller needs to unref after use.
668  *
669  * Returns: a reference to the existing loaded GstPlugin, a reference to the
670  * newly-loaded GstPlugin, or NULL if an error occurred.
671  */
672 GstPlugin *
673 gst_plugin_load_file (const gchar * filename, GError ** error)
674 {
675   GstPluginDesc *desc;
676   GstPlugin *plugin;
677   GModule *module;
678   gboolean ret;
679   gpointer ptr;
680   GStatBuf file_status;
681   GstRegistry *registry;
682   gboolean new_plugin = TRUE;
683   GModuleFlags flags;
684
685   g_return_val_if_fail (filename != NULL, NULL);
686
687   registry = gst_registry_get_default ();
688   g_static_mutex_lock (&gst_plugin_loading_mutex);
689
690   plugin = gst_registry_lookup (registry, filename);
691   if (plugin) {
692     if (plugin->module) {
693       /* already loaded */
694       g_static_mutex_unlock (&gst_plugin_loading_mutex);
695       return plugin;
696     } else {
697       /* load plugin and update fields */
698       new_plugin = FALSE;
699     }
700   }
701
702   GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
703       filename);
704
705   if (g_module_supported () == FALSE) {
706     GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
707     g_set_error (error,
708         GST_PLUGIN_ERROR,
709         GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
710     goto return_error;
711   }
712
713   if (g_stat (filename, &file_status)) {
714     GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
715     g_set_error (error,
716         GST_PLUGIN_ERROR,
717         GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
718         g_strerror (errno));
719     goto return_error;
720   }
721
722   flags = G_MODULE_BIND_LOCAL;
723   /* libgstpython.so is the gst-python plugin loader. It needs to be loaded with
724    * G_MODULE_BIND_LAZY.
725    *
726    * Ideally there should be a generic way for plugins to specify that they
727    * need to be loaded with _LAZY.
728    * */
729   if (strstr (filename, "libgstpython"))
730     flags |= G_MODULE_BIND_LAZY;
731
732   module = g_module_open (filename, flags);
733   if (module == NULL) {
734     GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
735         g_module_error ());
736     g_set_error (error,
737         GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
738         g_module_error ());
739     /* If we failed to open the shared object, then it's probably because a
740      * plugin is linked against the wrong libraries. Print out an easy-to-see
741      * message in this case. */
742     g_warning ("Failed to load plugin '%s': %s", filename, g_module_error ());
743     goto return_error;
744   }
745
746   ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
747   if (!ret) {
748     GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
749     g_set_error (error,
750         GST_PLUGIN_ERROR,
751         GST_PLUGIN_ERROR_MODULE,
752         "File \"%s\" is not a GStreamer plugin", filename);
753     g_module_close (module);
754     goto return_error;
755   }
756
757   desc = (GstPluginDesc *) ptr;
758
759   if (priv_gst_plugin_loading_have_whitelist () &&
760       !priv_gst_plugin_desc_is_whitelisted (desc, filename)) {
761     GST_INFO ("Whitelist specified and plugin not in whitelist, not loading: "
762         "name=%s, package=%s, file=%s", desc->name, desc->source, filename);
763     g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
764         "Not loading plugin file \"%s\", not in whitelist", filename);
765     g_module_close (module);
766     goto return_error;
767   }
768
769   if (new_plugin) {
770     plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
771     plugin->file_mtime = file_status.st_mtime;
772     plugin->file_size = file_status.st_size;
773     plugin->filename = g_strdup (filename);
774     plugin->basename = g_path_get_basename (filename);
775   }
776
777   plugin->module = module;
778   plugin->orig_desc = desc;
779
780   if (new_plugin) {
781     /* check plugin description: complain about bad values but accept them, to
782      * maintain backwards compatibility (FIXME: 0.11) */
783     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, name, filename);
784     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, description, filename);
785     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, version, filename);
786     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, license, filename);
787     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, source, filename);
788     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, package, filename);
789     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, origin, filename);
790
791     if (plugin->orig_desc->release_datetime != NULL &&
792         !check_release_datetime (plugin->orig_desc->release_datetime)) {
793       GST_ERROR ("GstPluginDesc for '%s' has invalid datetime '%s'",
794           filename, plugin->orig_desc->release_datetime);
795       plugin->orig_desc->release_datetime = NULL;
796     }
797   }
798
799   GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
800       plugin, filename);
801
802   /* this is where we load the actual .so, so let's trap SIGSEGV */
803   _gst_plugin_fault_handler_setup ();
804   _gst_plugin_fault_handler_filename = plugin->filename;
805
806   GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
807       plugin, filename);
808
809   if (!gst_plugin_register_func (plugin, plugin->orig_desc, NULL)) {
810     /* remove signal handler */
811     _gst_plugin_fault_handler_restore ();
812     GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
813     /* plugin == NULL */
814     g_set_error (error,
815         GST_PLUGIN_ERROR,
816         GST_PLUGIN_ERROR_MODULE,
817         "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
818         filename);
819     goto return_error;
820   }
821
822   /* remove signal handler */
823   _gst_plugin_fault_handler_restore ();
824   _gst_plugin_fault_handler_filename = NULL;
825   GST_INFO ("plugin \"%s\" loaded", plugin->filename);
826
827   if (new_plugin) {
828     gst_object_ref (plugin);
829     gst_default_registry_add_plugin (plugin);
830   }
831
832   g_static_mutex_unlock (&gst_plugin_loading_mutex);
833   return plugin;
834
835 return_error:
836   {
837     if (plugin)
838       gst_object_unref (plugin);
839     g_static_mutex_unlock (&gst_plugin_loading_mutex);
840     return NULL;
841   }
842 }
843
844 static void
845 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
846 {
847   dest->major_version = src->major_version;
848   dest->minor_version = src->minor_version;
849   dest->name = g_intern_string (src->name);
850   dest->description = g_intern_string (src->description);
851   dest->plugin_init = src->plugin_init;
852   dest->version = g_intern_string (src->version);
853   dest->license = g_intern_string (src->license);
854   dest->source = g_intern_string (src->source);
855   dest->package = g_intern_string (src->package);
856   dest->origin = g_intern_string (src->origin);
857   dest->release_datetime = g_intern_string (src->release_datetime);
858 }
859
860 /**
861  * gst_plugin_get_name:
862  * @plugin: plugin to get the name of
863  *
864  * Get the short name of the plugin
865  *
866  * Returns: the name of the plugin
867  */
868 const gchar *
869 gst_plugin_get_name (GstPlugin * plugin)
870 {
871   g_return_val_if_fail (plugin != NULL, NULL);
872
873   return plugin->desc.name;
874 }
875
876 /**
877  * gst_plugin_get_description:
878  * @plugin: plugin to get long name of
879  *
880  * Get the long descriptive name of the plugin
881  *
882  * Returns: the long name of the plugin
883  */
884 G_CONST_RETURN gchar *
885 gst_plugin_get_description (GstPlugin * plugin)
886 {
887   g_return_val_if_fail (plugin != NULL, NULL);
888
889   return plugin->desc.description;
890 }
891
892 /**
893  * gst_plugin_get_filename:
894  * @plugin: plugin to get the filename of
895  *
896  * get the filename of the plugin
897  *
898  * Returns: the filename of the plugin
899  */
900 G_CONST_RETURN gchar *
901 gst_plugin_get_filename (GstPlugin * plugin)
902 {
903   g_return_val_if_fail (plugin != NULL, NULL);
904
905   return plugin->filename;
906 }
907
908 /**
909  * gst_plugin_get_version:
910  * @plugin: plugin to get the version of
911  *
912  * get the version of the plugin
913  *
914  * Returns: the version of the plugin
915  */
916 G_CONST_RETURN gchar *
917 gst_plugin_get_version (GstPlugin * plugin)
918 {
919   g_return_val_if_fail (plugin != NULL, NULL);
920
921   return plugin->desc.version;
922 }
923
924 /**
925  * gst_plugin_get_license:
926  * @plugin: plugin to get the license of
927  *
928  * get the license of the plugin
929  *
930  * Returns: the license of the plugin
931  */
932 G_CONST_RETURN gchar *
933 gst_plugin_get_license (GstPlugin * plugin)
934 {
935   g_return_val_if_fail (plugin != NULL, NULL);
936
937   return plugin->desc.license;
938 }
939
940 /**
941  * gst_plugin_get_source:
942  * @plugin: plugin to get the source of
943  *
944  * get the source module the plugin belongs to.
945  *
946  * Returns: the source of the plugin
947  */
948 G_CONST_RETURN gchar *
949 gst_plugin_get_source (GstPlugin * plugin)
950 {
951   g_return_val_if_fail (plugin != NULL, NULL);
952
953   return plugin->desc.source;
954 }
955
956 /**
957  * gst_plugin_get_package:
958  * @plugin: plugin to get the package of
959  *
960  * get the package the plugin belongs to.
961  *
962  * Returns: the package of the plugin
963  */
964 G_CONST_RETURN gchar *
965 gst_plugin_get_package (GstPlugin * plugin)
966 {
967   g_return_val_if_fail (plugin != NULL, NULL);
968
969   return plugin->desc.package;
970 }
971
972 /**
973  * gst_plugin_get_origin:
974  * @plugin: plugin to get the origin of
975  *
976  * get the URL where the plugin comes from
977  *
978  * Returns: the origin of the plugin
979  */
980 G_CONST_RETURN gchar *
981 gst_plugin_get_origin (GstPlugin * plugin)
982 {
983   g_return_val_if_fail (plugin != NULL, NULL);
984
985   return plugin->desc.origin;
986 }
987
988 /**
989  * gst_plugin_get_module:
990  * @plugin: plugin to query
991  *
992  * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
993  * returned.
994  *
995  * Returns: module belonging to the plugin or NULL if the plugin isn't
996  *          loaded yet.
997  */
998 GModule *
999 gst_plugin_get_module (GstPlugin * plugin)
1000 {
1001   g_return_val_if_fail (plugin != NULL, NULL);
1002
1003   return plugin->module;
1004 }
1005
1006 /**
1007  * gst_plugin_is_loaded:
1008  * @plugin: plugin to query
1009  *
1010  * queries if the plugin is loaded into memory
1011  *
1012  * Returns: TRUE is loaded, FALSE otherwise
1013  */
1014 gboolean
1015 gst_plugin_is_loaded (GstPlugin * plugin)
1016 {
1017   g_return_val_if_fail (plugin != NULL, FALSE);
1018
1019   return (plugin->module != NULL || plugin->filename == NULL);
1020 }
1021
1022 /**
1023  * gst_plugin_get_cache_data:
1024  * @plugin: a plugin
1025  *
1026  * Gets the plugin specific data cache. If it is %NULL there is no cached data
1027  * stored. This is the case when the registry is getting rebuilt.
1028  *
1029  * Returns: The cached data as a #GstStructure or %NULL.
1030  *
1031  * Since: 0.10.24
1032  */
1033 G_CONST_RETURN GstStructure *
1034 gst_plugin_get_cache_data (GstPlugin * plugin)
1035 {
1036   g_return_val_if_fail (GST_IS_PLUGIN (plugin), NULL);
1037
1038   return plugin->priv->cache_data;
1039 }
1040
1041 /**
1042  * gst_plugin_set_cache_data:
1043  * @plugin: a plugin
1044  * @cache_data: a structure containing the data to cache
1045  *
1046  * Adds plugin specific data to cache. Passes the ownership of the structure to
1047  * the @plugin.
1048  *
1049  * The cache is flushed every time the registry is rebuilt.
1050  *
1051  * Since: 0.10.24
1052  */
1053 void
1054 gst_plugin_set_cache_data (GstPlugin * plugin, GstStructure * cache_data)
1055 {
1056   g_return_if_fail (GST_IS_PLUGIN (plugin));
1057   g_return_if_fail (GST_IS_STRUCTURE (cache_data));
1058
1059   if (plugin->priv->cache_data) {
1060     gst_structure_free (plugin->priv->cache_data);
1061   }
1062   plugin->priv->cache_data = cache_data;
1063 }
1064
1065 #if 0
1066 /**
1067  * gst_plugin_feature_list:
1068  * @plugin: plugin to query
1069  * @filter: the filter to use
1070  * @first: only return first match
1071  * @user_data: user data passed to the filter function
1072  *
1073  * Runs a filter against all plugin features and returns a GList with
1074  * the results. If the first flag is set, only the first match is
1075  * returned (as a list with a single object).
1076  *
1077  * Returns: a GList of features, g_list_free after use.
1078  */
1079 GList *
1080 gst_plugin_feature_filter (GstPlugin * plugin,
1081     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
1082 {
1083   GList *list;
1084   GList *g;
1085
1086   list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
1087       user_data);
1088   for (g = list; g; g = g->next) {
1089     gst_object_ref (plugin);
1090   }
1091
1092   return list;
1093 }
1094
1095 typedef struct
1096 {
1097   GstPluginFeatureFilter filter;
1098   gboolean first;
1099   gpointer user_data;
1100   GList *result;
1101 }
1102 FeatureFilterData;
1103
1104 static gboolean
1105 _feature_filter (GstPlugin * plugin, gpointer user_data)
1106 {
1107   GList *result;
1108   FeatureFilterData *data = (FeatureFilterData *) user_data;
1109
1110   result = gst_plugin_feature_filter (plugin, data->filter, data->first,
1111       data->user_data);
1112   if (result) {
1113     data->result = g_list_concat (data->result, result);
1114     return TRUE;
1115   }
1116   return FALSE;
1117 }
1118
1119 /**
1120  * gst_plugin_list_feature_filter:
1121  * @list: a #GList of plugins to query
1122  * @filter: the filter function to use
1123  * @first: only return first match
1124  * @user_data: user data passed to the filter function
1125  *
1126  * Runs a filter against all plugin features of the plugins in the given
1127  * list and returns a GList with the results.
1128  * If the first flag is set, only the first match is
1129  * returned (as a list with a single object).
1130  *
1131  * Returns: a GList of features, g_list_free after use.
1132  */
1133 GList *
1134 gst_plugin_list_feature_filter (GList * list,
1135     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
1136 {
1137   FeatureFilterData data;
1138   GList *result;
1139
1140   data.filter = filter;
1141   data.first = first;
1142   data.user_data = user_data;
1143   data.result = NULL;
1144
1145   result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
1146   g_list_free (result);
1147
1148   return data.result;
1149 }
1150 #endif
1151
1152 /**
1153  * gst_plugin_name_filter:
1154  * @plugin: the plugin to check
1155  * @name: the name of the plugin
1156  *
1157  * A standard filter that returns TRUE when the plugin is of the
1158  * given name.
1159  *
1160  * Returns: TRUE if the plugin is of the given name.
1161  */
1162 gboolean
1163 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
1164 {
1165   return (plugin->desc.name && !strcmp (plugin->desc.name, name));
1166 }
1167
1168 #if 0
1169 /**
1170  * gst_plugin_find_feature:
1171  * @plugin: plugin to get the feature from
1172  * @name: The name of the feature to find
1173  * @type: The type of the feature to find
1174  *
1175  * Find a feature of the given name and type in the given plugin.
1176  *
1177  * Returns: a GstPluginFeature or NULL if the feature was not found.
1178  */
1179 GstPluginFeature *
1180 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
1181 {
1182   GList *walk;
1183   GstPluginFeature *result = NULL;
1184   GstTypeNameData data;
1185
1186   g_return_val_if_fail (name != NULL, NULL);
1187
1188   data.type = type;
1189   data.name = name;
1190
1191   walk = gst_filter_run (plugin->features,
1192       (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
1193
1194   if (walk) {
1195     result = GST_PLUGIN_FEATURE (walk->data);
1196
1197     gst_object_ref (result);
1198     gst_plugin_feature_list_free (walk);
1199   }
1200
1201   return result;
1202 }
1203 #endif
1204
1205 #if 0
1206 static gboolean
1207 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
1208 {
1209   return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
1210 }
1211 #endif
1212
1213 #if 0
1214 /**
1215  * gst_plugin_find_feature_by_name:
1216  * @plugin: plugin to get the feature from
1217  * @name: The name of the feature to find
1218  *
1219  * Find a feature of the given name in the given plugin.
1220  *
1221  * Returns: a GstPluginFeature or NULL if the feature was not found.
1222  */
1223 GstPluginFeature *
1224 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
1225 {
1226   GList *walk;
1227   GstPluginFeature *result = NULL;
1228
1229   g_return_val_if_fail (name != NULL, NULL);
1230
1231   walk = gst_filter_run (plugin->features,
1232       (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
1233
1234   if (walk) {
1235     result = GST_PLUGIN_FEATURE (walk->data);
1236
1237     gst_object_ref (result);
1238     gst_plugin_feature_list_free (walk);
1239   }
1240
1241   return result;
1242 }
1243 #endif
1244
1245 /**
1246  * gst_plugin_load_by_name:
1247  * @name: name of plugin to load
1248  *
1249  * Load the named plugin. Refs the plugin.
1250  *
1251  * Returns: A reference to a loaded plugin, or NULL on error.
1252  */
1253 GstPlugin *
1254 gst_plugin_load_by_name (const gchar * name)
1255 {
1256   GstPlugin *plugin, *newplugin;
1257   GError *error = NULL;
1258
1259   GST_DEBUG ("looking up plugin %s in default registry", name);
1260   plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
1261   if (plugin) {
1262     GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
1263     newplugin = gst_plugin_load_file (plugin->filename, &error);
1264     gst_object_unref (plugin);
1265
1266     if (!newplugin) {
1267       GST_WARNING ("load_plugin error: %s", error->message);
1268       g_error_free (error);
1269       return NULL;
1270     }
1271     /* newplugin was reffed by load_file */
1272     return newplugin;
1273   }
1274
1275   GST_DEBUG ("Could not find plugin %s in registry", name);
1276   return NULL;
1277 }
1278
1279 /**
1280  * gst_plugin_load:
1281  * @plugin: plugin to load
1282  *
1283  * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
1284  * untouched. The normal use pattern of this function goes like this:
1285  *
1286  * <programlisting>
1287  * GstPlugin *loaded_plugin;
1288  * loaded_plugin = gst_plugin_load (plugin);
1289  * // presumably, we're no longer interested in the potentially-unloaded plugin
1290  * gst_object_unref (plugin);
1291  * plugin = loaded_plugin;
1292  * </programlisting>
1293  *
1294  * Returns: A reference to a loaded plugin, or NULL on error.
1295  */
1296 GstPlugin *
1297 gst_plugin_load (GstPlugin * plugin)
1298 {
1299   GError *error = NULL;
1300   GstPlugin *newplugin;
1301
1302   if (gst_plugin_is_loaded (plugin)) {
1303     return plugin;
1304   }
1305
1306   if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
1307     goto load_error;
1308
1309   return newplugin;
1310
1311 load_error:
1312   {
1313     GST_WARNING ("load_plugin error: %s", error->message);
1314     g_error_free (error);
1315     return NULL;
1316   }
1317 }
1318
1319 /**
1320  * gst_plugin_list_free:
1321  * @list: list of #GstPlugin
1322  *
1323  * Unrefs each member of @list, then frees the list.
1324  */
1325 void
1326 gst_plugin_list_free (GList * list)
1327 {
1328   GList *g;
1329
1330   for (g = list; g; g = g->next) {
1331     gst_object_unref (GST_PLUGIN_CAST (g->data));
1332   }
1333   g_list_free (list);
1334 }
1335
1336 /* ===== plugin dependencies ===== */
1337
1338 /* Scenarios:
1339  * ENV + xyz     where ENV can contain multiple values separated by SEPARATOR
1340  *               xyz may be "" (if ENV contains path to file rather than dir)
1341  * ENV + *xyz   same as above, but xyz acts as suffix filter
1342  * ENV + xyz*   same as above, but xyz acts as prefix filter (is this needed?)
1343  * ENV + *xyz*  same as above, but xyz acts as strstr filter (is this needed?)
1344  * 
1345  * same as above, with additional paths hard-coded at compile-time:
1346  *   - only check paths + ... if ENV is not set or yields not paths
1347  *   - always check paths + ... in addition to ENV
1348  *
1349  * When user specifies set of environment variables, he/she may also use e.g.
1350  * "HOME/.mystuff/plugins", and we'll expand the content of $HOME with the
1351  * remainder 
1352  */
1353
1354 /* we store in registry:
1355  *  sets of:
1356  *   { 
1357  *     - environment variables (array of strings)
1358  *     - last hash of env variable contents (uint) (so we can avoid doing stats
1359  *       if one of the env vars has changed; premature optimisation galore)
1360  *     - hard-coded paths (array of strings)
1361  *     - xyz filename/suffix/prefix strings (array of strings)
1362  *     - flags (int)
1363  *     - last hash of file/dir stats (int)
1364  *   }
1365  *   (= struct GstPluginDep)
1366  */
1367
1368 static guint
1369 gst_plugin_ext_dep_get_env_vars_hash (GstPlugin * plugin, GstPluginDep * dep)
1370 {
1371   gchar **e;
1372   guint hash;
1373
1374   /* there's no deeper logic to what we do here; all we want to know (when
1375    * checking if the plugin needs to be rescanned) is whether the content of
1376    * one of the environment variables in the list is different from when it
1377    * was last scanned */
1378   hash = 0;
1379   for (e = dep->env_vars; e != NULL && *e != NULL; ++e) {
1380     const gchar *val;
1381     gchar env_var[256];
1382
1383     /* order matters: "val",NULL needs to yield a different hash than
1384      * NULL,"val", so do a shift here whether the var is set or not */
1385     hash = hash << 5;
1386
1387     /* want environment variable at beginning of string */
1388     if (!g_ascii_isalnum (**e)) {
1389       GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1390           "variable string: %s", *e);
1391       continue;
1392     }
1393
1394     /* user is allowed to specify e.g. "HOME/.pitivi/plugins" */
1395     g_strlcpy (env_var, *e, sizeof (env_var));
1396     g_strdelimit (env_var, "/\\", '\0');
1397
1398     if ((val = g_getenv (env_var)))
1399       hash += g_str_hash (val);
1400   }
1401
1402   return hash;
1403 }
1404
1405 gboolean
1406 _priv_plugin_deps_env_vars_changed (GstPlugin * plugin)
1407 {
1408   GList *l;
1409
1410   for (l = plugin->priv->deps; l != NULL; l = l->next) {
1411     GstPluginDep *dep = l->data;
1412
1413     if (dep->env_hash != gst_plugin_ext_dep_get_env_vars_hash (plugin, dep))
1414       return TRUE;
1415   }
1416
1417   return FALSE;
1418 }
1419
1420 static GList *
1421 gst_plugin_ext_dep_extract_env_vars_paths (GstPlugin * plugin,
1422     GstPluginDep * dep)
1423 {
1424   gchar **evars;
1425   GList *paths = NULL;
1426
1427   for (evars = dep->env_vars; evars != NULL && *evars != NULL; ++evars) {
1428     const gchar *e;
1429     gchar **components;
1430
1431     /* want environment variable at beginning of string */
1432     if (!g_ascii_isalnum (**evars)) {
1433       GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1434           "variable string: %s", *evars);
1435       continue;
1436     }
1437
1438     /* user is allowed to specify e.g. "HOME/.pitivi/plugins", which we want to
1439      * split into the env_var name component and the path component */
1440     components = g_strsplit_set (*evars, "/\\", 2);
1441     g_assert (components != NULL);
1442
1443     e = g_getenv (components[0]);
1444     GST_LOG_OBJECT (plugin, "expanding %s = '%s' (path suffix: %s)",
1445         components[0], GST_STR_NULL (e), GST_STR_NULL (components[1]));
1446
1447     if (components[1] != NULL) {
1448       g_strdelimit (components[1], "/\\", G_DIR_SEPARATOR);
1449     }
1450
1451     if (e != NULL && *e != '\0') {
1452       gchar **arr;
1453       guint i;
1454
1455       arr = g_strsplit (e, G_SEARCHPATH_SEPARATOR_S, -1);
1456
1457       for (i = 0; arr != NULL && arr[i] != NULL; ++i) {
1458         gchar *full_path;
1459
1460         if (!g_path_is_absolute (arr[i])) {
1461           GST_INFO_OBJECT (plugin, "ignoring environment variable content '%s'"
1462               ": either not an absolute path or not a path at all", arr[i]);
1463           continue;
1464         }
1465
1466         if (components[1] != NULL) {
1467           full_path = g_build_filename (arr[i], components[1], NULL);
1468         } else {
1469           full_path = g_strdup (arr[i]);
1470         }
1471
1472         if (!g_list_find_custom (paths, full_path, (GCompareFunc) strcmp)) {
1473           GST_LOG_OBJECT (plugin, "path: '%s'", full_path);
1474           paths = g_list_prepend (paths, full_path);
1475           full_path = NULL;
1476         } else {
1477           GST_LOG_OBJECT (plugin, "path: '%s' (duplicate,ignoring)", full_path);
1478           g_free (full_path);
1479         }
1480       }
1481
1482       g_strfreev (arr);
1483     }
1484
1485     g_strfreev (components);
1486   }
1487
1488   GST_LOG_OBJECT (plugin, "Extracted %d paths from environment",
1489       g_list_length (paths));
1490
1491   return paths;
1492 }
1493
1494 static guint
1495 gst_plugin_ext_dep_get_hash_from_stat_entry (GStatBuf * s)
1496 {
1497   if (!(s->st_mode & (S_IFDIR | S_IFREG)))
1498     return (guint) - 1;
1499
1500   /* completely random formula */
1501   return ((s->st_size << 3) + (s->st_mtime << 5)) ^ s->st_ctime;
1502 }
1503
1504 static gboolean
1505 gst_plugin_ext_dep_direntry_matches (GstPlugin * plugin, const gchar * entry,
1506     const gchar ** filenames, GstPluginDependencyFlags flags)
1507 {
1508   /* no filenames specified, match all entries for now (could probably
1509    * optimise by just taking the dir stat hash or so) */
1510   if (filenames == NULL || *filenames == NULL || **filenames == '\0')
1511     return TRUE;
1512
1513   while (*filenames != NULL) {
1514     /* suffix match? */
1515     if (((flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)) &&
1516         g_str_has_suffix (entry, *filenames)) {
1517       return TRUE;
1518       /* else it's an exact match that's needed */
1519     } else if (strcmp (entry, *filenames) == 0) {
1520       return TRUE;
1521     }
1522     GST_LOG ("%s does not match %s, flags=0x%04x", entry, *filenames, flags);
1523     ++filenames;
1524   }
1525   return FALSE;
1526 }
1527
1528 static guint
1529 gst_plugin_ext_dep_scan_dir_and_match_names (GstPlugin * plugin,
1530     const gchar * path, const gchar ** filenames,
1531     GstPluginDependencyFlags flags, int depth)
1532 {
1533   const gchar *entry;
1534   gboolean recurse_dirs;
1535   GError *err = NULL;
1536   GDir *dir;
1537   guint hash = 0;
1538
1539   recurse_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1540
1541   dir = g_dir_open (path, 0, &err);
1542   if (dir == NULL) {
1543     GST_DEBUG_OBJECT (plugin, "g_dir_open(%s) failed: %s", path, err->message);
1544     g_error_free (err);
1545     return (guint) - 1;
1546   }
1547
1548   /* FIXME: we're assuming here that we always get the directory entries in
1549    * the same order, and not in a random order */
1550   while ((entry = g_dir_read_name (dir))) {
1551     gboolean have_match;
1552     GStatBuf s;
1553     gchar *full_path;
1554     guint fhash;
1555
1556     have_match =
1557         gst_plugin_ext_dep_direntry_matches (plugin, entry, filenames, flags);
1558
1559     /* avoid the stat if possible */
1560     if (!have_match && !recurse_dirs)
1561       continue;
1562
1563     full_path = g_build_filename (path, entry, NULL);
1564     if (g_stat (full_path, &s) < 0) {
1565       fhash = (guint) - 1;
1566       GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1567           g_strerror (errno));
1568     } else if (have_match) {
1569       fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1570       GST_LOG_OBJECT (plugin, "stat: %s (result: %u)", full_path, fhash);
1571     } else if ((s.st_mode & (S_IFDIR))) {
1572       fhash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, full_path,
1573           filenames, flags, depth + 1);
1574     } else {
1575       /* it's not a name match, we want to recurse, but it's not a directory */
1576       g_free (full_path);
1577       continue;
1578     }
1579
1580     hash = (hash + fhash) << 1;
1581     g_free (full_path);
1582   }
1583
1584   g_dir_close (dir);
1585   return hash;
1586 }
1587
1588 static guint
1589 gst_plugin_ext_dep_scan_path_with_filenames (GstPlugin * plugin,
1590     const gchar * path, const gchar ** filenames,
1591     GstPluginDependencyFlags flags)
1592 {
1593   const gchar *empty_filenames[] = { "", NULL };
1594   gboolean recurse_into_dirs, partial_names;
1595   guint i, hash = 0;
1596
1597   /* to avoid special-casing below (FIXME?) */
1598   if (filenames == NULL || *filenames == NULL)
1599     filenames = empty_filenames;
1600
1601   recurse_into_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1602   partial_names = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX);
1603
1604   /* if we can construct the exact paths to check with the data we have, just
1605    * stat them one by one; this is more efficient than opening the directory
1606    * and going through each entry to see if it matches one of our filenames. */
1607   if (!recurse_into_dirs && !partial_names) {
1608     for (i = 0; filenames[i] != NULL; ++i) {
1609       GStatBuf s;
1610       gchar *full_path;
1611       guint fhash;
1612
1613       full_path = g_build_filename (path, filenames[i], NULL);
1614       if (g_stat (full_path, &s) < 0) {
1615         fhash = (guint) - 1;
1616         GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1617             g_strerror (errno));
1618       } else {
1619         fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1620         GST_LOG_OBJECT (plugin, "stat: %s (result: %08x)", full_path, fhash);
1621       }
1622       hash = (hash + fhash) << 1;
1623       g_free (full_path);
1624     }
1625   } else {
1626     hash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, path,
1627         filenames, flags, 0);
1628   }
1629
1630   return hash;
1631 }
1632
1633 static guint
1634 gst_plugin_ext_dep_get_stat_hash (GstPlugin * plugin, GstPluginDep * dep)
1635 {
1636   gboolean paths_are_default_only;
1637   GList *scan_paths;
1638   guint scan_hash = 0;
1639
1640   GST_LOG_OBJECT (plugin, "start");
1641
1642   paths_are_default_only =
1643       dep->flags & GST_PLUGIN_DEPENDENCY_FLAG_PATHS_ARE_DEFAULT_ONLY;
1644
1645   scan_paths = gst_plugin_ext_dep_extract_env_vars_paths (plugin, dep);
1646
1647   if (scan_paths == NULL || !paths_are_default_only) {
1648     gchar **paths;
1649
1650     for (paths = dep->paths; paths != NULL && *paths != NULL; ++paths) {
1651       const gchar *path = *paths;
1652
1653       if (!g_list_find_custom (scan_paths, path, (GCompareFunc) strcmp)) {
1654         GST_LOG_OBJECT (plugin, "path: '%s'", path);
1655         scan_paths = g_list_prepend (scan_paths, g_strdup (path));
1656       } else {
1657         GST_LOG_OBJECT (plugin, "path: '%s' (duplicate, ignoring)", path);
1658       }
1659     }
1660   }
1661
1662   /* not that the order really matters, but it makes debugging easier */
1663   scan_paths = g_list_reverse (scan_paths);
1664
1665   while (scan_paths != NULL) {
1666     const gchar *path = scan_paths->data;
1667
1668     scan_hash += gst_plugin_ext_dep_scan_path_with_filenames (plugin, path,
1669         (const gchar **) dep->names, dep->flags);
1670     scan_hash = scan_hash << 1;
1671
1672     g_free (scan_paths->data);
1673     scan_paths = g_list_delete_link (scan_paths, scan_paths);
1674   }
1675
1676   GST_LOG_OBJECT (plugin, "done, scan_hash: %08x", scan_hash);
1677   return scan_hash;
1678 }
1679
1680 gboolean
1681 _priv_plugin_deps_files_changed (GstPlugin * plugin)
1682 {
1683   GList *l;
1684
1685   for (l = plugin->priv->deps; l != NULL; l = l->next) {
1686     GstPluginDep *dep = l->data;
1687
1688     if (dep->stat_hash != gst_plugin_ext_dep_get_stat_hash (plugin, dep))
1689       return TRUE;
1690   }
1691
1692   return FALSE;
1693 }
1694
1695 static void
1696 gst_plugin_ext_dep_free (GstPluginDep * dep)
1697 {
1698   g_strfreev (dep->env_vars);
1699   g_strfreev (dep->paths);
1700   g_strfreev (dep->names);
1701   g_slice_free (GstPluginDep, dep);
1702 }
1703
1704 static gboolean
1705 gst_plugin_ext_dep_strv_equal (gchar ** arr1, gchar ** arr2)
1706 {
1707   if (arr1 == arr2)
1708     return TRUE;
1709   if (arr1 == NULL || arr2 == NULL)
1710     return FALSE;
1711   for (; *arr1 != NULL && *arr2 != NULL; ++arr1, ++arr2) {
1712     if (strcmp (*arr1, *arr2) != 0)
1713       return FALSE;
1714   }
1715   return (*arr1 == *arr2);
1716 }
1717
1718 static gboolean
1719 gst_plugin_ext_dep_equals (GstPluginDep * dep, const gchar ** env_vars,
1720     const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1721 {
1722   if (dep->flags != flags)
1723     return FALSE;
1724
1725   return gst_plugin_ext_dep_strv_equal (dep->env_vars, (gchar **) env_vars) &&
1726       gst_plugin_ext_dep_strv_equal (dep->paths, (gchar **) paths) &&
1727       gst_plugin_ext_dep_strv_equal (dep->names, (gchar **) names);
1728 }
1729
1730 /**
1731  * gst_plugin_add_dependency:
1732  * @plugin: a #GstPlugin
1733  * @env_vars: NULL-terminated array of environent variables affecting the
1734  *     feature set of the plugin (e.g. an environment variable containing
1735  *     paths where to look for additional modules/plugins of a library),
1736  *     or NULL. Environment variable names may be followed by a path component
1737  *      which will be added to the content of the environment variable, e.g.
1738  *      "HOME/.mystuff/plugins".
1739  * @paths: NULL-terminated array of directories/paths where dependent files
1740  *     may be.
1741  * @names: NULL-terminated array of file names (or file name suffixes,
1742  *     depending on @flags) to be used in combination with the paths from
1743  *     @paths and/or the paths extracted from the environment variables in
1744  *     @env_vars, or NULL.
1745  * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1746  *
1747  * Make GStreamer aware of external dependencies which affect the feature
1748  * set of this plugin (ie. the elements or typefinders associated with it).
1749  *
1750  * GStreamer will re-inspect plugins with external dependencies whenever any
1751  * of the external dependencies change. This is useful for plugins which wrap
1752  * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1753  * library and makes visualisations available as GStreamer elements, or a
1754  * codec loader which exposes elements and/or caps dependent on what external
1755  * codec libraries are currently installed.
1756  *
1757  * Since: 0.10.22
1758  */
1759 void
1760 gst_plugin_add_dependency (GstPlugin * plugin, const gchar ** env_vars,
1761     const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1762 {
1763   GstPluginDep *dep;
1764   GList *l;
1765
1766   g_return_if_fail (GST_IS_PLUGIN (plugin));
1767
1768   if ((env_vars == NULL || env_vars[0] == NULL) &&
1769       (paths == NULL || paths[0] == NULL)) {
1770     GST_DEBUG_OBJECT (plugin,
1771         "plugin registered empty dependency set. Ignoring");
1772     return;
1773   }
1774
1775   for (l = plugin->priv->deps; l != NULL; l = l->next) {
1776     if (gst_plugin_ext_dep_equals (l->data, env_vars, paths, names, flags)) {
1777       GST_LOG_OBJECT (plugin, "dependency already registered");
1778       return;
1779     }
1780   }
1781
1782   dep = g_slice_new (GstPluginDep);
1783
1784   dep->env_vars = g_strdupv ((gchar **) env_vars);
1785   dep->paths = g_strdupv ((gchar **) paths);
1786   dep->names = g_strdupv ((gchar **) names);
1787   dep->flags = flags;
1788
1789   dep->env_hash = gst_plugin_ext_dep_get_env_vars_hash (plugin, dep);
1790   dep->stat_hash = gst_plugin_ext_dep_get_stat_hash (plugin, dep);
1791
1792   plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
1793
1794   GST_DEBUG_OBJECT (plugin, "added dependency:");
1795   for (; env_vars != NULL && *env_vars != NULL; ++env_vars)
1796     GST_DEBUG_OBJECT (plugin, " evar: %s", *env_vars);
1797   for (; paths != NULL && *paths != NULL; ++paths)
1798     GST_DEBUG_OBJECT (plugin, " path: %s", *paths);
1799   for (; names != NULL && *names != NULL; ++names)
1800     GST_DEBUG_OBJECT (plugin, " name: %s", *names);
1801 }
1802
1803 /**
1804  * gst_plugin_add_dependency_simple:
1805  * @plugin: the #GstPlugin
1806  * @env_vars: one or more environent variables (separated by ':', ';' or ','),
1807  *      or NULL. Environment variable names may be followed by a path component
1808  *      which will be added to the content of the environment variable, e.g.
1809  *      "HOME/.mystuff/plugins:MYSTUFF_PLUGINS_PATH"
1810  * @paths: one ore more directory paths (separated by ':' or ';' or ','),
1811  *      or NULL. Example: "/usr/lib/mystuff/plugins"
1812  * @names: one or more file names or file name suffixes (separated by commas),
1813  *   or NULL
1814  * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1815  *
1816  * Make GStreamer aware of external dependencies which affect the feature
1817  * set of this plugin (ie. the elements or typefinders associated with it).
1818  *
1819  * GStreamer will re-inspect plugins with external dependencies whenever any
1820  * of the external dependencies change. This is useful for plugins which wrap
1821  * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1822  * library and makes visualisations available as GStreamer elements, or a
1823  * codec loader which exposes elements and/or caps dependent on what external
1824  * codec libraries are currently installed.
1825  *
1826  * Convenience wrapper function for gst_plugin_add_dependency() which
1827  * takes simple strings as arguments instead of string arrays, with multiple
1828  * arguments separated by predefined delimiters (see above).
1829  *
1830  * Since: 0.10.22
1831  */
1832 void
1833 gst_plugin_add_dependency_simple (GstPlugin * plugin,
1834     const gchar * env_vars, const gchar * paths, const gchar * names,
1835     GstPluginDependencyFlags flags)
1836 {
1837   gchar **a_evars = NULL;
1838   gchar **a_paths = NULL;
1839   gchar **a_names = NULL;
1840
1841   if (env_vars)
1842     a_evars = g_strsplit_set (env_vars, ":;,", -1);
1843   if (paths)
1844     a_paths = g_strsplit_set (paths, ":;,", -1);
1845   if (names)
1846     a_names = g_strsplit_set (names, ",", -1);
1847
1848   gst_plugin_add_dependency (plugin, (const gchar **) a_evars,
1849       (const gchar **) a_paths, (const gchar **) a_names, flags);
1850
1851   if (a_evars)
1852     g_strfreev (a_evars);
1853   if (a_paths)
1854     g_strfreev (a_paths);
1855   if (a_names)
1856     g_strfreev (a_names);
1857 }