Fix typos in documentation
[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 void _gst_plugin_fault_handler_setup ();
517
518 static GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
519
520 #define CHECK_PLUGIN_DESC_FIELD(desc,field,fn)                               \
521   if (G_UNLIKELY ((desc)->field == NULL)) {                                  \
522     GST_ERROR ("GstPluginDesc for '%s' has no %s", fn, G_STRINGIFY (field)); \
523   }
524
525 /**
526  * gst_plugin_load_file:
527  * @filename: the plugin filename to load
528  * @error: pointer to a NULL-valued GError
529  *
530  * Loads the given plugin and refs it.  Caller needs to unref after use.
531  *
532  * Returns: a reference to the existing loaded GstPlugin, a reference to the
533  * newly-loaded GstPlugin, or NULL if an error occurred.
534  */
535 GstPlugin *
536 gst_plugin_load_file (const gchar * filename, GError ** error)
537 {
538   GstPlugin *plugin;
539   GModule *module;
540   gboolean ret;
541   gpointer ptr;
542   struct stat file_status;
543   GstRegistry *registry;
544   gboolean new_plugin = TRUE;
545
546   g_return_val_if_fail (filename != NULL, NULL);
547
548   registry = gst_registry_get_default ();
549   g_static_mutex_lock (&gst_plugin_loading_mutex);
550
551   plugin = gst_registry_lookup (registry, filename);
552   if (plugin) {
553     if (plugin->module) {
554       /* already loaded */
555       g_static_mutex_unlock (&gst_plugin_loading_mutex);
556       return plugin;
557     } else {
558       /* load plugin and update fields */
559       new_plugin = FALSE;
560     }
561   }
562
563   GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
564       filename);
565
566   if (g_module_supported () == FALSE) {
567     GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
568     g_set_error (error,
569         GST_PLUGIN_ERROR,
570         GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
571     goto return_error;
572   }
573
574   if (g_stat (filename, &file_status)) {
575     GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
576     g_set_error (error,
577         GST_PLUGIN_ERROR,
578         GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
579         g_strerror (errno));
580     goto return_error;
581   }
582
583   module = g_module_open (filename, G_MODULE_BIND_LOCAL);
584   if (module == NULL) {
585     GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
586         g_module_error ());
587     g_set_error (error,
588         GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
589         g_module_error ());
590     /* If we failed to open the shared object, then it's probably because a
591      * plugin is linked against the wrong libraries. Print out an easy-to-see
592      * message in this case. */
593     g_warning ("Failed to load plugin '%s': %s", filename, g_module_error ());
594     goto return_error;
595   }
596
597   if (new_plugin) {
598     plugin = g_object_newv (GST_TYPE_PLUGIN, 0, NULL);
599     plugin->file_mtime = file_status.st_mtime;
600     plugin->file_size = file_status.st_size;
601     plugin->filename = g_strdup (filename);
602     plugin->basename = g_path_get_basename (filename);
603   }
604   plugin->module = module;
605
606   ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
607   if (!ret) {
608     GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
609     g_set_error (error,
610         GST_PLUGIN_ERROR,
611         GST_PLUGIN_ERROR_MODULE,
612         "File \"%s\" is not a GStreamer plugin", filename);
613     g_module_close (module);
614     goto return_error;
615   }
616   plugin->orig_desc = (GstPluginDesc *) ptr;
617
618   if (new_plugin) {
619     /* check plugin description: complain about bad values but accept them, to
620      * maintain backwards compatibility (FIXME: 0.11) */
621     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, name, filename);
622     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, description, filename);
623     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, version, filename);
624     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, license, filename);
625     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, source, filename);
626     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, package, filename);
627     CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, origin, filename);
628   } else {
629     /* this is overwritten by gst_plugin_register_func() */
630     g_free (plugin->desc.description);
631   }
632
633   GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
634       plugin, filename);
635
636   /* this is where we load the actual .so, so let's trap SIGSEGV */
637   _gst_plugin_fault_handler_setup ();
638   _gst_plugin_fault_handler_filename = plugin->filename;
639
640   GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
641       plugin, filename);
642
643   if (!gst_plugin_register_func (plugin, plugin->orig_desc, NULL)) {
644     /* remove signal handler */
645     _gst_plugin_fault_handler_restore ();
646     GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
647     /* plugin == NULL */
648     g_set_error (error,
649         GST_PLUGIN_ERROR,
650         GST_PLUGIN_ERROR_MODULE,
651         "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
652         filename);
653     g_module_close (module);
654     goto return_error;
655   }
656
657   /* remove signal handler */
658   _gst_plugin_fault_handler_restore ();
659   _gst_plugin_fault_handler_filename = NULL;
660   GST_INFO ("plugin \"%s\" loaded", plugin->filename);
661
662   if (new_plugin) {
663     gst_object_ref (plugin);
664     gst_default_registry_add_plugin (plugin);
665   }
666
667   g_static_mutex_unlock (&gst_plugin_loading_mutex);
668   return plugin;
669
670 return_error:
671   {
672     if (plugin)
673       gst_object_unref (plugin);
674     g_static_mutex_unlock (&gst_plugin_loading_mutex);
675     return NULL;
676   }
677 }
678
679 static void
680 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
681 {
682   dest->major_version = src->major_version;
683   dest->minor_version = src->minor_version;
684   dest->name = g_intern_string (src->name);
685   /* maybe intern the description too, just for convenience? */
686   dest->description = g_strdup (src->description);
687   dest->plugin_init = src->plugin_init;
688   dest->version = g_intern_string (src->version);
689   dest->license = g_intern_string (src->license);
690   dest->source = g_intern_string (src->source);
691   dest->package = g_intern_string (src->package);
692   dest->origin = g_intern_string (src->origin);
693 }
694
695 /* unused */
696 static void
697 gst_plugin_desc_free (GstPluginDesc * desc)
698 {
699   g_free (desc->description);
700   memset (desc, 0, sizeof (GstPluginDesc));
701 }
702
703 /**
704  * gst_plugin_get_name:
705  * @plugin: plugin to get the name of
706  *
707  * Get the short name of the plugin
708  *
709  * Returns: the name of the plugin
710  */
711 const gchar *
712 gst_plugin_get_name (GstPlugin * plugin)
713 {
714   g_return_val_if_fail (plugin != NULL, NULL);
715
716   return plugin->desc.name;
717 }
718
719 /**
720  * gst_plugin_get_description:
721  * @plugin: plugin to get long name of
722  *
723  * Get the long descriptive name of the plugin
724  *
725  * Returns: the long name of the plugin
726  */
727 G_CONST_RETURN gchar *
728 gst_plugin_get_description (GstPlugin * plugin)
729 {
730   g_return_val_if_fail (plugin != NULL, NULL);
731
732   return plugin->desc.description;
733 }
734
735 /**
736  * gst_plugin_get_filename:
737  * @plugin: plugin to get the filename of
738  *
739  * get the filename of the plugin
740  *
741  * Returns: the filename of the plugin
742  */
743 G_CONST_RETURN gchar *
744 gst_plugin_get_filename (GstPlugin * plugin)
745 {
746   g_return_val_if_fail (plugin != NULL, NULL);
747
748   return plugin->filename;
749 }
750
751 /**
752  * gst_plugin_get_version:
753  * @plugin: plugin to get the version of
754  *
755  * get the version of the plugin
756  *
757  * Returns: the version of the plugin
758  */
759 G_CONST_RETURN gchar *
760 gst_plugin_get_version (GstPlugin * plugin)
761 {
762   g_return_val_if_fail (plugin != NULL, NULL);
763
764   return plugin->desc.version;
765 }
766
767 /**
768  * gst_plugin_get_license:
769  * @plugin: plugin to get the license of
770  *
771  * get the license of the plugin
772  *
773  * Returns: the license of the plugin
774  */
775 G_CONST_RETURN gchar *
776 gst_plugin_get_license (GstPlugin * plugin)
777 {
778   g_return_val_if_fail (plugin != NULL, NULL);
779
780   return plugin->desc.license;
781 }
782
783 /**
784  * gst_plugin_get_source:
785  * @plugin: plugin to get the source of
786  *
787  * get the source module the plugin belongs to.
788  *
789  * Returns: the source of the plugin
790  */
791 G_CONST_RETURN gchar *
792 gst_plugin_get_source (GstPlugin * plugin)
793 {
794   g_return_val_if_fail (plugin != NULL, NULL);
795
796   return plugin->desc.source;
797 }
798
799 /**
800  * gst_plugin_get_package:
801  * @plugin: plugin to get the package of
802  *
803  * get the package the plugin belongs to.
804  *
805  * Returns: the package of the plugin
806  */
807 G_CONST_RETURN gchar *
808 gst_plugin_get_package (GstPlugin * plugin)
809 {
810   g_return_val_if_fail (plugin != NULL, NULL);
811
812   return plugin->desc.package;
813 }
814
815 /**
816  * gst_plugin_get_origin:
817  * @plugin: plugin to get the origin of
818  *
819  * get the URL where the plugin comes from
820  *
821  * Returns: the origin of the plugin
822  */
823 G_CONST_RETURN gchar *
824 gst_plugin_get_origin (GstPlugin * plugin)
825 {
826   g_return_val_if_fail (plugin != NULL, NULL);
827
828   return plugin->desc.origin;
829 }
830
831 /**
832  * gst_plugin_get_module:
833  * @plugin: plugin to query
834  *
835  * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
836  * returned.
837  *
838  * Returns: module belonging to the plugin or NULL if the plugin isn't
839  *          loaded yet.
840  */
841 GModule *
842 gst_plugin_get_module (GstPlugin * plugin)
843 {
844   g_return_val_if_fail (plugin != NULL, NULL);
845
846   return plugin->module;
847 }
848
849 /**
850  * gst_plugin_is_loaded:
851  * @plugin: plugin to query
852  *
853  * queries if the plugin is loaded into memory
854  *
855  * Returns: TRUE is loaded, FALSE otherwise
856  */
857 gboolean
858 gst_plugin_is_loaded (GstPlugin * plugin)
859 {
860   g_return_val_if_fail (plugin != NULL, FALSE);
861
862   return (plugin->module != NULL || plugin->filename == NULL);
863 }
864
865 /**
866  * gst_plugin_get_cache_data:
867  * @plugin: a plugin
868  *
869  * Gets the plugin specific data cache. If it is %NULL there is no cached data
870  * stored. This is the case when the registry is getting rebuilt.
871  *
872  * Returns: The cached data as a #GstStructure or %NULL.
873  *
874  * Since: 0.10.24
875  */
876 G_CONST_RETURN GstStructure *
877 gst_plugin_get_cache_data (GstPlugin * plugin)
878 {
879   g_return_val_if_fail (GST_IS_PLUGIN (plugin), NULL);
880
881   return plugin->priv->cache_data;
882 }
883
884 /**
885  * gst_plugin_set_cache_data:
886  * @plugin: a plugin
887  * @cache_data: a structure containing the data to cache
888  *
889  * Adds plugin specific data to cache. Passes the ownership of the structure to
890  * the @plugin.
891  *
892  * The cache is flushed every time the registry is rebuilt.
893  *
894  * Since: 0.10.24
895  */
896 void
897 gst_plugin_set_cache_data (GstPlugin * plugin, GstStructure * cache_data)
898 {
899   g_return_if_fail (GST_IS_PLUGIN (plugin));
900   g_return_if_fail (GST_IS_STRUCTURE (cache_data));
901
902   if (plugin->priv->cache_data) {
903     gst_structure_free (plugin->priv->cache_data);
904   }
905   plugin->priv->cache_data = cache_data;
906 }
907
908 #if 0
909 /**
910  * gst_plugin_feature_list:
911  * @plugin: plugin to query
912  * @filter: the filter to use
913  * @first: only return first match
914  * @user_data: user data passed to the filter function
915  *
916  * Runs a filter against all plugin features and returns a GList with
917  * the results. If the first flag is set, only the first match is
918  * returned (as a list with a single object).
919  *
920  * Returns: a GList of features, g_list_free after use.
921  */
922 GList *
923 gst_plugin_feature_filter (GstPlugin * plugin,
924     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
925 {
926   GList *list;
927   GList *g;
928
929   list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
930       user_data);
931   for (g = list; g; g = g->next) {
932     gst_object_ref (plugin);
933   }
934
935   return list;
936 }
937
938 typedef struct
939 {
940   GstPluginFeatureFilter filter;
941   gboolean first;
942   gpointer user_data;
943   GList *result;
944 }
945 FeatureFilterData;
946
947 static gboolean
948 _feature_filter (GstPlugin * plugin, gpointer user_data)
949 {
950   GList *result;
951   FeatureFilterData *data = (FeatureFilterData *) user_data;
952
953   result = gst_plugin_feature_filter (plugin, data->filter, data->first,
954       data->user_data);
955   if (result) {
956     data->result = g_list_concat (data->result, result);
957     return TRUE;
958   }
959   return FALSE;
960 }
961
962 /**
963  * gst_plugin_list_feature_filter:
964  * @list: a #GList of plugins to query
965  * @filter: the filter function to use
966  * @first: only return first match
967  * @user_data: user data passed to the filter function
968  *
969  * Runs a filter against all plugin features of the plugins in the given
970  * list and returns a GList with the results.
971  * If the first flag is set, only the first match is
972  * returned (as a list with a single object).
973  *
974  * Returns: a GList of features, g_list_free after use.
975  */
976 GList *
977 gst_plugin_list_feature_filter (GList * list,
978     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
979 {
980   FeatureFilterData data;
981   GList *result;
982
983   data.filter = filter;
984   data.first = first;
985   data.user_data = user_data;
986   data.result = NULL;
987
988   result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
989   g_list_free (result);
990
991   return data.result;
992 }
993 #endif
994
995 /**
996  * gst_plugin_name_filter:
997  * @plugin: the plugin to check
998  * @name: the name of the plugin
999  *
1000  * A standard filter that returns TRUE when the plugin is of the
1001  * given name.
1002  *
1003  * Returns: TRUE if the plugin is of the given name.
1004  */
1005 gboolean
1006 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
1007 {
1008   return (plugin->desc.name && !strcmp (plugin->desc.name, name));
1009 }
1010
1011 #if 0
1012 /**
1013  * gst_plugin_find_feature:
1014  * @plugin: plugin to get the feature from
1015  * @name: The name of the feature to find
1016  * @type: The type of the feature to find
1017  *
1018  * Find a feature of the given name and type in the given plugin.
1019  *
1020  * Returns: a GstPluginFeature or NULL if the feature was not found.
1021  */
1022 GstPluginFeature *
1023 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
1024 {
1025   GList *walk;
1026   GstPluginFeature *result = NULL;
1027   GstTypeNameData data;
1028
1029   g_return_val_if_fail (name != NULL, NULL);
1030
1031   data.type = type;
1032   data.name = name;
1033
1034   walk = gst_filter_run (plugin->features,
1035       (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
1036
1037   if (walk) {
1038     result = GST_PLUGIN_FEATURE (walk->data);
1039
1040     gst_object_ref (result);
1041     gst_plugin_feature_list_free (walk);
1042   }
1043
1044   return result;
1045 }
1046 #endif
1047
1048 #if 0
1049 static gboolean
1050 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
1051 {
1052   return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
1053 }
1054 #endif
1055
1056 #if 0
1057 /**
1058  * gst_plugin_find_feature_by_name:
1059  * @plugin: plugin to get the feature from
1060  * @name: The name of the feature to find
1061  *
1062  * Find a feature of the given name in the given plugin.
1063  *
1064  * Returns: a GstPluginFeature or NULL if the feature was not found.
1065  */
1066 GstPluginFeature *
1067 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
1068 {
1069   GList *walk;
1070   GstPluginFeature *result = NULL;
1071
1072   g_return_val_if_fail (name != NULL, NULL);
1073
1074   walk = gst_filter_run (plugin->features,
1075       (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
1076
1077   if (walk) {
1078     result = GST_PLUGIN_FEATURE (walk->data);
1079
1080     gst_object_ref (result);
1081     gst_plugin_feature_list_free (walk);
1082   }
1083
1084   return result;
1085 }
1086 #endif
1087
1088 /**
1089  * gst_plugin_load_by_name:
1090  * @name: name of plugin to load
1091  *
1092  * Load the named plugin. Refs the plugin.
1093  *
1094  * Returns: A reference to a loaded plugin, or NULL on error.
1095  */
1096 GstPlugin *
1097 gst_plugin_load_by_name (const gchar * name)
1098 {
1099   GstPlugin *plugin, *newplugin;
1100   GError *error = NULL;
1101
1102   GST_DEBUG ("looking up plugin %s in default registry", name);
1103   plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
1104   if (plugin) {
1105     GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
1106     newplugin = gst_plugin_load_file (plugin->filename, &error);
1107     gst_object_unref (plugin);
1108
1109     if (!newplugin) {
1110       GST_WARNING ("load_plugin error: %s", error->message);
1111       g_error_free (error);
1112       return NULL;
1113     }
1114     /* newplugin was reffed by load_file */
1115     return newplugin;
1116   }
1117
1118   GST_DEBUG ("Could not find plugin %s in registry", name);
1119   return NULL;
1120 }
1121
1122 /**
1123  * gst_plugin_load:
1124  * @plugin: plugin to load
1125  *
1126  * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
1127  * untouched. The normal use pattern of this function goes like this:
1128  *
1129  * <programlisting>
1130  * GstPlugin *loaded_plugin;
1131  * loaded_plugin = gst_plugin_load (plugin);
1132  * // presumably, we're no longer interested in the potentially-unloaded plugin
1133  * gst_object_unref (plugin);
1134  * plugin = loaded_plugin;
1135  * </programlisting>
1136  *
1137  * Returns: A reference to a loaded plugin, or NULL on error.
1138  */
1139 GstPlugin *
1140 gst_plugin_load (GstPlugin * plugin)
1141 {
1142   GError *error = NULL;
1143   GstPlugin *newplugin;
1144
1145   if (gst_plugin_is_loaded (plugin)) {
1146     return plugin;
1147   }
1148
1149   if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
1150     goto load_error;
1151
1152   return newplugin;
1153
1154 load_error:
1155   {
1156     GST_WARNING ("load_plugin error: %s", error->message);
1157     g_error_free (error);
1158     return NULL;
1159   }
1160 }
1161
1162 /**
1163  * gst_plugin_list_free:
1164  * @list: list of #GstPlugin
1165  *
1166  * Unrefs each member of @list, then frees the list.
1167  */
1168 void
1169 gst_plugin_list_free (GList * list)
1170 {
1171   GList *g;
1172
1173   for (g = list; g; g = g->next) {
1174     gst_object_unref (GST_PLUGIN_CAST (g->data));
1175   }
1176   g_list_free (list);
1177 }
1178
1179 /* ===== plugin dependencies ===== */
1180
1181 /* Scenarios:
1182  * ENV + xyz     where ENV can contain multiple values separated by SEPARATOR
1183  *               xyz may be "" (if ENV contains path to file rather than dir)
1184  * ENV + *xyz   same as above, but xyz acts as suffix filter
1185  * ENV + xyz*   same as above, but xyz acts as prefix filter (is this needed?)
1186  * ENV + *xyz*  same as above, but xyz acts as strstr filter (is this needed?)
1187  * 
1188  * same as above, with additional paths hard-coded at compile-time:
1189  *   - only check paths + ... if ENV is not set or yields not paths
1190  *   - always check paths + ... in addition to ENV
1191  *
1192  * When user specifies set of environment variables, he/she may also use e.g.
1193  * "HOME/.mystuff/plugins", and we'll expand the content of $HOME with the
1194  * remainder 
1195  */
1196
1197 /* we store in registry:
1198  *  sets of:
1199  *   { 
1200  *     - environment variables (array of strings)
1201  *     - last hash of env variable contents (uint) (so we can avoid doing stats
1202  *       if one of the env vars has changed; premature optimisation galore)
1203  *     - hard-coded paths (array of strings)
1204  *     - xyz filename/suffix/prefix strings (array of strings)
1205  *     - flags (int)
1206  *     - last hash of file/dir stats (int)
1207  *   }
1208  *   (= struct GstPluginDep)
1209  */
1210
1211 static guint
1212 gst_plugin_ext_dep_get_env_vars_hash (GstPlugin * plugin, GstPluginDep * dep)
1213 {
1214   gchar **e;
1215   guint hash;
1216
1217   /* there's no deeper logic to what we do here; all we want to know (when
1218    * checking if the plugin needs to be rescanned) is whether the content of
1219    * one of the environment variables in the list is different from when it
1220    * was last scanned */
1221   hash = 0;
1222   for (e = dep->env_vars; e != NULL && *e != NULL; ++e) {
1223     const gchar *val;
1224     gchar env_var[256];
1225
1226     /* order matters: "val",NULL needs to yield a different hash than
1227      * NULL,"val", so do a shift here whether the var is set or not */
1228     hash = hash << 5;
1229
1230     /* want environment variable at beginning of string */
1231     if (!g_ascii_isalnum (**e)) {
1232       GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1233           "variable string: %s", *e);
1234       continue;
1235     }
1236
1237     /* user is allowed to specify e.g. "HOME/.pitivi/plugins" */
1238     g_strlcpy (env_var, *e, sizeof (env_var));
1239     g_strdelimit (env_var, "/\\", '\0');
1240
1241     if ((val = g_getenv (env_var)))
1242       hash += g_str_hash (val);
1243   }
1244
1245   return hash;
1246 }
1247
1248 gboolean
1249 _priv_plugin_deps_env_vars_changed (GstPlugin * plugin)
1250 {
1251   GList *l;
1252
1253   for (l = plugin->priv->deps; l != NULL; l = l->next) {
1254     GstPluginDep *dep = l->data;
1255
1256     if (dep->env_hash != gst_plugin_ext_dep_get_env_vars_hash (plugin, dep))
1257       return TRUE;
1258   }
1259
1260   return FALSE;
1261 }
1262
1263 static GList *
1264 gst_plugin_ext_dep_extract_env_vars_paths (GstPlugin * plugin,
1265     GstPluginDep * dep)
1266 {
1267   gchar **evars;
1268   GList *paths = NULL;
1269
1270   for (evars = dep->env_vars; evars != NULL && *evars != NULL; ++evars) {
1271     const gchar *e;
1272     gchar **components;
1273
1274     /* want environment variable at beginning of string */
1275     if (!g_ascii_isalnum (**evars)) {
1276       GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1277           "variable string: %s", *evars);
1278       continue;
1279     }
1280
1281     /* user is allowed to specify e.g. "HOME/.pitivi/plugins", which we want to
1282      * split into the env_var name component and the path component */
1283     components = g_strsplit_set (*evars, "/\\", 2);
1284     g_assert (components != NULL);
1285
1286     e = g_getenv (components[0]);
1287     GST_LOG_OBJECT (plugin, "expanding %s = '%s' (path suffix: %s)",
1288         components[0], GST_STR_NULL (e), GST_STR_NULL (components[1]));
1289
1290     if (components[1] != NULL) {
1291       g_strdelimit (components[1], "/\\", G_DIR_SEPARATOR);
1292     }
1293
1294     if (e != NULL && *e != '\0') {
1295       gchar **arr;
1296       guint i;
1297
1298       arr = g_strsplit (e, G_SEARCHPATH_SEPARATOR_S, -1);
1299
1300       for (i = 0; arr != NULL && arr[i] != NULL; ++i) {
1301         gchar *full_path;
1302
1303         if (!g_path_is_absolute (arr[i])) {
1304           GST_INFO_OBJECT (plugin, "ignoring environment variable content '%s'"
1305               ": either not an absolute path or not a path at all", arr[i]);
1306           continue;
1307         }
1308
1309         if (components[1] != NULL) {
1310           full_path = g_build_filename (arr[i], components[1], NULL);
1311         } else {
1312           full_path = g_strdup (arr[i]);
1313         }
1314
1315         if (!g_list_find_custom (paths, full_path, (GCompareFunc) strcmp)) {
1316           GST_LOG_OBJECT (plugin, "path: '%s'", full_path);
1317           paths = g_list_prepend (paths, full_path);
1318           full_path = NULL;
1319         } else {
1320           GST_LOG_OBJECT (plugin, "path: '%s' (duplicate,ignoring)", full_path);
1321           g_free (full_path);
1322         }
1323       }
1324
1325       g_strfreev (arr);
1326     }
1327
1328     g_strfreev (components);
1329   }
1330
1331   GST_LOG_OBJECT (plugin, "Extracted %d paths from environment",
1332       g_list_length (paths));
1333
1334   return paths;
1335 }
1336
1337 static guint
1338 gst_plugin_ext_dep_get_hash_from_stat_entry (struct stat *s)
1339 {
1340   if (!(s->st_mode & (S_IFDIR | S_IFREG)))
1341     return (guint) - 1;
1342
1343   /* completely random formula */
1344   return ((s->st_size << 3) + (s->st_mtime << 5)) ^ s->st_ctime;
1345 }
1346
1347 static gboolean
1348 gst_plugin_ext_dep_direntry_matches (GstPlugin * plugin, const gchar * entry,
1349     const gchar ** filenames, GstPluginDependencyFlags flags)
1350 {
1351   /* no filenames specified, match all entries for now (could probably
1352    * optimise by just taking the dir stat hash or so) */
1353   if (filenames == NULL || *filenames == NULL || **filenames == '\0')
1354     return TRUE;
1355
1356   while (*filenames != NULL) {
1357     /* suffix match? */
1358     if (((flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)) &&
1359         g_str_has_suffix (entry, *filenames)) {
1360       return TRUE;
1361       /* else it's an exact match that's needed */
1362     } else if (strcmp (entry, *filenames) == 0) {
1363       return TRUE;
1364     }
1365     GST_LOG ("%s does not match %s, flags=0x%04x", entry, *filenames, flags);
1366     ++filenames;
1367   }
1368   return FALSE;
1369 }
1370
1371 static guint
1372 gst_plugin_ext_dep_scan_dir_and_match_names (GstPlugin * plugin,
1373     const gchar * path, const gchar ** filenames,
1374     GstPluginDependencyFlags flags, int depth)
1375 {
1376   const gchar *entry;
1377   gboolean recurse_dirs;
1378   GError *err = NULL;
1379   GDir *dir;
1380   guint hash = 0;
1381
1382   recurse_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1383
1384   dir = g_dir_open (path, 0, &err);
1385   if (dir == NULL) {
1386     GST_DEBUG_OBJECT (plugin, "g_dir_open(%s) failed: %s", path, err->message);
1387     g_error_free (err);
1388     return (guint) - 1;
1389   }
1390
1391   /* FIXME: we're assuming here that we always get the directory entries in
1392    * the same order, and not in a random order */
1393   while ((entry = g_dir_read_name (dir))) {
1394     gboolean have_match;
1395     struct stat s;
1396     gchar *full_path;
1397     guint fhash;
1398
1399     have_match =
1400         gst_plugin_ext_dep_direntry_matches (plugin, entry, filenames, flags);
1401
1402     /* avoid the stat if possible */
1403     if (!have_match && !recurse_dirs)
1404       continue;
1405
1406     full_path = g_build_filename (path, entry, NULL);
1407     if (g_stat (full_path, &s) < 0) {
1408       fhash = (guint) - 1;
1409       GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1410           g_strerror (errno));
1411     } else if (have_match) {
1412       fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1413       GST_LOG_OBJECT (plugin, "stat: %s (result: %u)", full_path, fhash);
1414     } else if ((s.st_mode & (S_IFDIR))) {
1415       fhash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, full_path,
1416           filenames, flags, depth + 1);
1417     } else {
1418       /* it's not a name match, we want to recurse, but it's not a directory */
1419       g_free (full_path);
1420       continue;
1421     }
1422
1423     hash = (hash + fhash) << 1;
1424     g_free (full_path);
1425   }
1426
1427   g_dir_close (dir);
1428   return hash;
1429 }
1430
1431 static guint
1432 gst_plugin_ext_dep_scan_path_with_filenames (GstPlugin * plugin,
1433     const gchar * path, const gchar ** filenames,
1434     GstPluginDependencyFlags flags)
1435 {
1436   const gchar *empty_filenames[] = { "", NULL };
1437   gboolean recurse_into_dirs, partial_names;
1438   guint i, hash = 0;
1439
1440   /* to avoid special-casing below (FIXME?) */
1441   if (filenames == NULL || *filenames == NULL)
1442     filenames = empty_filenames;
1443
1444   recurse_into_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1445   partial_names = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX);
1446
1447   /* if we can construct the exact paths to check with the data we have, just
1448    * stat them one by one; this is more efficient than opening the directory
1449    * and going through each entry to see if it matches one of our filenames. */
1450   if (!recurse_into_dirs && !partial_names) {
1451     for (i = 0; filenames[i] != NULL; ++i) {
1452       struct stat s;
1453       gchar *full_path;
1454       guint fhash;
1455
1456       full_path = g_build_filename (path, filenames[i], NULL);
1457       if (g_stat (full_path, &s) < 0) {
1458         fhash = (guint) - 1;
1459         GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1460             g_strerror (errno));
1461       } else {
1462         fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1463         GST_LOG_OBJECT (plugin, "stat: %s (result: %08x)", full_path, fhash);
1464       }
1465       hash = (hash + fhash) << 1;
1466       g_free (full_path);
1467     }
1468   } else {
1469     hash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, path,
1470         filenames, flags, 0);
1471   }
1472
1473   return hash;
1474 }
1475
1476 static guint
1477 gst_plugin_ext_dep_get_stat_hash (GstPlugin * plugin, GstPluginDep * dep)
1478 {
1479   gboolean paths_are_default_only;
1480   GList *scan_paths;
1481   guint scan_hash = 0;
1482
1483   GST_LOG_OBJECT (plugin, "start");
1484
1485   paths_are_default_only =
1486       dep->flags & GST_PLUGIN_DEPENDENCY_FLAG_PATHS_ARE_DEFAULT_ONLY;
1487
1488   scan_paths = gst_plugin_ext_dep_extract_env_vars_paths (plugin, dep);
1489
1490   if (scan_paths == NULL || !paths_are_default_only) {
1491     gchar **paths;
1492
1493     for (paths = dep->paths; paths != NULL && *paths != NULL; ++paths) {
1494       const gchar *path = *paths;
1495
1496       if (!g_list_find_custom (scan_paths, path, (GCompareFunc) strcmp)) {
1497         GST_LOG_OBJECT (plugin, "path: '%s'", path);
1498         scan_paths = g_list_prepend (scan_paths, g_strdup (path));
1499       } else {
1500         GST_LOG_OBJECT (plugin, "path: '%s' (duplicate, ignoring)", path);
1501       }
1502     }
1503   }
1504
1505   /* not that the order really matters, but it makes debugging easier */
1506   scan_paths = g_list_reverse (scan_paths);
1507
1508   while (scan_paths != NULL) {
1509     const gchar *path = scan_paths->data;
1510
1511     scan_hash += gst_plugin_ext_dep_scan_path_with_filenames (plugin, path,
1512         (const gchar **) dep->names, dep->flags);
1513     scan_hash = scan_hash << 1;
1514
1515     g_free (scan_paths->data);
1516     scan_paths = g_list_delete_link (scan_paths, scan_paths);
1517   }
1518
1519   GST_LOG_OBJECT (plugin, "done, scan_hash: %08x", scan_hash);
1520   return scan_hash;
1521 }
1522
1523 gboolean
1524 _priv_plugin_deps_files_changed (GstPlugin * plugin)
1525 {
1526   GList *l;
1527
1528   for (l = plugin->priv->deps; l != NULL; l = l->next) {
1529     GstPluginDep *dep = l->data;
1530
1531     if (dep->stat_hash != gst_plugin_ext_dep_get_stat_hash (plugin, dep))
1532       return TRUE;
1533   }
1534
1535   return FALSE;
1536 }
1537
1538 static void
1539 gst_plugin_ext_dep_free (GstPluginDep * dep)
1540 {
1541   g_strfreev (dep->env_vars);
1542   g_strfreev (dep->paths);
1543   g_strfreev (dep->names);
1544   g_free (dep);
1545 }
1546
1547 static gboolean
1548 gst_plugin_ext_dep_strv_equal (gchar ** arr1, gchar ** arr2)
1549 {
1550   if (arr1 == arr2)
1551     return TRUE;
1552   if (arr1 == NULL || arr2 == NULL)
1553     return FALSE;
1554   for (; *arr1 != NULL && *arr2 != NULL; ++arr1, ++arr2) {
1555     if (strcmp (*arr1, *arr2) != 0)
1556       return FALSE;
1557   }
1558   return (*arr1 == *arr2);
1559 }
1560
1561 static gboolean
1562 gst_plugin_ext_dep_equals (GstPluginDep * dep, const gchar ** env_vars,
1563     const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1564 {
1565   if (dep->flags != flags)
1566     return FALSE;
1567
1568   return gst_plugin_ext_dep_strv_equal (dep->env_vars, (gchar **) env_vars) &&
1569       gst_plugin_ext_dep_strv_equal (dep->paths, (gchar **) paths) &&
1570       gst_plugin_ext_dep_strv_equal (dep->names, (gchar **) names);
1571 }
1572
1573 /**
1574  * gst_plugin_add_dependency:
1575  * @plugin: a #GstPlugin
1576  * @env_vars: NULL-terminated array of environent variables affecting the
1577  *     feature set of the plugin (e.g. an environment variable containing
1578  *     paths where to look for additional modules/plugins of a library),
1579  *     or NULL. Environment variable names may be followed by a path component
1580  *      which will be added to the content of the environment variable, e.g.
1581  *      "HOME/.mystuff/plugins".
1582  * @paths: NULL-terminated array of directories/paths where dependent files
1583  *     may be.
1584  * @names: NULL-terminated array of file names (or file name suffixes,
1585  *     depending on @flags) to be used in combination with the paths from
1586  *     @paths and/or the paths extracted from the environment variables in
1587  *     @env_vars, or NULL.
1588  * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1589  *
1590  * Make GStreamer aware of external dependencies which affect the feature
1591  * set of this plugin (ie. the elements or typefinders associated with it).
1592  *
1593  * GStreamer will re-inspect plugins with external dependencies whenever any
1594  * of the external dependencies change. This is useful for plugins which wrap
1595  * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1596  * library and makes visualisations available as GStreamer elements, or a
1597  * codec loader which exposes elements and/or caps dependent on what external
1598  * codec libraries are currently installed.
1599  *
1600  * Since: 0.10.22
1601  */
1602 void
1603 gst_plugin_add_dependency (GstPlugin * plugin, const gchar ** env_vars,
1604     const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1605 {
1606   GstPluginDep *dep;
1607   GList *l;
1608
1609   g_return_if_fail (GST_IS_PLUGIN (plugin));
1610
1611   if ((env_vars == NULL || env_vars[0] == NULL) &&
1612       (paths == NULL || paths[0] == NULL)) {
1613     GST_DEBUG_OBJECT (plugin,
1614         "plugin registered empty dependency set. Ignoring");
1615     return;
1616   }
1617
1618   for (l = plugin->priv->deps; l != NULL; l = l->next) {
1619     if (gst_plugin_ext_dep_equals (l->data, env_vars, paths, names, flags)) {
1620       GST_LOG_OBJECT (plugin, "dependency already registered");
1621       return;
1622     }
1623   }
1624
1625   dep = g_new0 (GstPluginDep, 1);
1626
1627   dep->env_vars = g_strdupv ((gchar **) env_vars);
1628   dep->paths = g_strdupv ((gchar **) paths);
1629   dep->names = g_strdupv ((gchar **) names);
1630   dep->flags = flags;
1631
1632   dep->env_hash = gst_plugin_ext_dep_get_env_vars_hash (plugin, dep);
1633   dep->stat_hash = gst_plugin_ext_dep_get_stat_hash (plugin, dep);
1634
1635   plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
1636
1637   GST_DEBUG_OBJECT (plugin, "added dependency:");
1638   for (; env_vars != NULL && *env_vars != NULL; ++env_vars)
1639     GST_DEBUG_OBJECT (plugin, " evar: %s", *env_vars);
1640   for (; paths != NULL && *paths != NULL; ++paths)
1641     GST_DEBUG_OBJECT (plugin, " path: %s", *paths);
1642   for (; names != NULL && *names != NULL; ++names)
1643     GST_DEBUG_OBJECT (plugin, " name: %s", *names);
1644 }
1645
1646 /**
1647  * gst_plugin_add_dependency_simple:
1648  * @plugin: the #GstPlugin
1649  * @env_vars: one or more environent variables (separated by ':', ';' or ','),
1650  *      or NULL. Environment variable names may be followed by a path component
1651  *      which will be added to the content of the environment variable, e.g.
1652  *      "HOME/.mystuff/plugins:MYSTUFF_PLUGINS_PATH"
1653  * @paths: one ore more directory paths (separated by ':' or ';' or ','),
1654  *      or NULL. Example: "/usr/lib/mystuff/plugins"
1655  * @names: one or more file names or file name suffixes (separated by commas),
1656  *   or NULL
1657  * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1658  *
1659  * Make GStreamer aware of external dependencies which affect the feature
1660  * set of this plugin (ie. the elements or typefinders associated with it).
1661  *
1662  * GStreamer will re-inspect plugins with external dependencies whenever any
1663  * of the external dependencies change. This is useful for plugins which wrap
1664  * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1665  * library and makes visualisations available as GStreamer elements, or a
1666  * codec loader which exposes elements and/or caps dependent on what external
1667  * codec libraries are currently installed.
1668  *
1669  * Convenience wrapper function for gst_plugin_add_dependency() which
1670  * takes simple strings as arguments instead of string arrays, with multiple
1671  * arguments separated by predefined delimiters (see above).
1672  *
1673  * Since: 0.10.22
1674  */
1675 void
1676 gst_plugin_add_dependency_simple (GstPlugin * plugin,
1677     const gchar * env_vars, const gchar * paths, const gchar * names,
1678     GstPluginDependencyFlags flags)
1679 {
1680   gchar **a_evars = NULL;
1681   gchar **a_paths = NULL;
1682   gchar **a_names = NULL;
1683
1684   if (env_vars)
1685     a_evars = g_strsplit_set (env_vars, ":;,", -1);
1686   if (paths)
1687     a_paths = g_strsplit_set (paths, ":;,", -1);
1688   if (names)
1689     a_names = g_strsplit_set (names, ",", -1);
1690
1691   gst_plugin_add_dependency (plugin, (const gchar **) a_evars,
1692       (const gchar **) a_paths, (const gchar **) a_names, flags);
1693
1694   if (a_evars)
1695     g_strfreev (a_evars);
1696   if (a_paths)
1697     g_strfreev (a_paths);
1698   if (a_names)
1699     g_strfreev (a_names);
1700 }