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