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