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