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