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