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