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