2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstplugin.c: Plugin subsystem for loading elements, types, and libs
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.
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.
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.
25 * @short_description: Container for features loaded from a shared object module
26 * @see_also: #GstPluginFeature, #GstElementFactory
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.
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>.
39 * Once you have a handle to a #GstPlugin (e.g. from the #GstRegistry), you
40 * can add any object that subclasses #GstPluginFeature.
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
52 #include <glib/gstdio.h>
53 #include <sys/types.h>
63 #include "gst_private.h"
64 #include "glib-compat-private.h"
68 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
70 static guint _num_static_plugins; /* 0 */
71 static GstPluginDesc *_static_plugins; /* NULL */
72 static gboolean _gst_plugin_inited;
74 /* static variables for segfault handling of plugin loading */
75 static char *_gst_plugin_fault_handler_filename = NULL;
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
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
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 */
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);
108 static void gst_plugin_ext_dep_free (GstPluginDep * dep);
110 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
113 gst_plugin_init (GstPlugin * plugin)
116 G_TYPE_INSTANCE_GET_PRIVATE (plugin, GST_TYPE_PLUGIN, GstPluginPrivate);
120 gst_plugin_finalize (GObject * object)
122 GstPlugin *plugin = GST_PLUGIN_CAST (object);
123 GstRegistry *registry = gst_registry_get_default ();
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");
132 g_free (plugin->filename);
133 g_free (plugin->basename);
134 gst_plugin_desc_free (&plugin->desc);
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;
140 G_OBJECT_CLASS (gst_plugin_parent_class)->finalize (object);
144 gst_plugin_class_init (GstPluginClass * klass)
146 G_OBJECT_CLASS (klass)->finalize = GST_DEBUG_FUNCPTR (gst_plugin_finalize);
148 g_type_class_add_private (klass, sizeof (GstPluginPrivate));
152 gst_plugin_error_quark (void)
154 static GQuark quark = 0;
157 quark = g_quark_from_static_string ("gst_plugin_error");
161 #ifndef GST_REMOVE_DEPRECATED
162 /* this function can be called in the GCC constructor extension, before
163 * the _gst_plugin_initialize() was called. In that case, we store the
164 * plugin description in a list to initialize it when we open the main
166 * When the main module is known, we can register the plugin right away.
169 _gst_plugin_register_static (GstPluginDesc * desc)
171 g_return_if_fail (desc != NULL);
173 if (!_gst_plugin_inited) {
174 /* We can't use any GLib functions here, since g_thread_init hasn't been
175 * called yet, and we can't call it here either, or programs that don't
176 * guard their g_thread_init calls in main() will just abort */
177 ++_num_static_plugins;
179 realloc (_static_plugins, _num_static_plugins * sizeof (GstPluginDesc));
180 /* assume strings in the GstPluginDesc are static const or live forever */
181 _static_plugins[_num_static_plugins - 1] = *desc;
183 gst_plugin_register_static (desc->major_version, desc->minor_version,
184 desc->name, desc->description, desc->plugin_init, desc->version,
185 desc->license, desc->source, desc->package, desc->origin);
191 * gst_plugin_register_static:
192 * @major_version: the major version number of the GStreamer core that the
193 * plugin was compiled for, you can just use GST_VERSION_MAJOR here
194 * @minor_version: the minor version number of the GStreamer core that the
195 * plugin was compiled for, you can just use GST_VERSION_MINOR here
196 * @name: a unique name of the plugin (ideally prefixed with an application- or
197 * library-specific namespace prefix in order to avoid name conflicts in
198 * case a similar plugin with the same name ever gets added to GStreamer)
199 * @description: description of the plugin
200 * @init_func: pointer to the init function of this plugin.
201 * @version: version string of the plugin
202 * @license: effective license of plugin. Must be one of the approved licenses
203 * (see #GstPluginDesc above) or the plugin will not be registered.
204 * @source: source module plugin belongs to
205 * @package: shipped package plugin belongs to
206 * @origin: URL to provider of plugin
208 * Registers a static plugin, ie. a plugin which is private to an application
209 * or library and contained within the application or library (as opposed to
210 * being shipped as a separate module file).
212 * You must make sure that GStreamer has been initialised (with gst_init() or
213 * via gst_init_get_option_group()) before calling this function.
215 * Returns: TRUE if the plugin was registered correctly, otherwise FALSE.
220 gst_plugin_register_static (gint major_version, gint minor_version,
221 const gchar * name, gchar * description, GstPluginInitFunc init_func,
222 const gchar * version, const gchar * license, const gchar * source,
223 const gchar * package, const gchar * origin)
225 GstPluginDesc desc = { major_version, minor_version, name, description,
226 init_func, version, license, source, package, origin,
229 gboolean res = FALSE;
231 g_return_val_if_fail (name != NULL, FALSE);
232 g_return_val_if_fail (description != NULL, FALSE);
233 g_return_val_if_fail (init_func != NULL, FALSE);
234 g_return_val_if_fail (version != NULL, FALSE);
235 g_return_val_if_fail (license != NULL, FALSE);
236 g_return_val_if_fail (source != NULL, FALSE);
237 g_return_val_if_fail (package != NULL, FALSE);
238 g_return_val_if_fail (origin != NULL, FALSE);
240 /* make sure gst_init() has been called */
241 g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);
243 GST_LOG ("attempting to load static plugin \"%s\" now...", name);
244 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
245 if (gst_plugin_register_func (plugin, &desc, NULL) != NULL) {
246 GST_INFO ("registered static plugin \"%s\"", name);
247 res = gst_default_registry_add_plugin (plugin);
248 GST_INFO ("added static plugin \"%s\", result: %d", name, res);
254 * gst_plugin_register_static_full:
255 * @major_version: the major version number of the GStreamer core that the
256 * plugin was compiled for, you can just use GST_VERSION_MAJOR here
257 * @minor_version: the minor version number of the GStreamer core that the
258 * plugin was compiled for, you can just use GST_VERSION_MINOR here
259 * @name: a unique name of the plugin (ideally prefixed with an application- or
260 * library-specific namespace prefix in order to avoid name conflicts in
261 * case a similar plugin with the same name ever gets added to GStreamer)
262 * @description: description of the plugin
263 * @init_full_func: pointer to the init function with user data of this plugin.
264 * @version: version string of the plugin
265 * @license: effective license of plugin. Must be one of the approved licenses
266 * (see #GstPluginDesc above) or the plugin will not be registered.
267 * @source: source module plugin belongs to
268 * @package: shipped package plugin belongs to
269 * @origin: URL to provider of plugin
270 * @user_data: gpointer to user data
272 * Registers a static plugin, ie. a plugin which is private to an application
273 * or library and contained within the application or library (as opposed to
274 * being shipped as a separate module file) with a #GstPluginInitFullFunc
275 * which allows user data to be passed to the callback function (useful
278 * You must make sure that GStreamer has been initialised (with gst_init() or
279 * via gst_init_get_option_group()) before calling this function.
281 * Returns: TRUE if the plugin was registered correctly, otherwise FALSE.
287 gst_plugin_register_static_full (gint major_version, gint minor_version,
288 const gchar * name, gchar * description,
289 GstPluginInitFullFunc init_full_func, const gchar * version,
290 const gchar * license, const gchar * source, const gchar * package,
291 const gchar * origin, gpointer user_data)
293 GstPluginDesc desc = { major_version, minor_version, name, description,
294 (GstPluginInitFunc) init_full_func, version, license, source, package,
298 gboolean res = FALSE;
300 g_return_val_if_fail (name != NULL, FALSE);
301 g_return_val_if_fail (description != NULL, FALSE);
302 g_return_val_if_fail (init_full_func != NULL, FALSE);
303 g_return_val_if_fail (version != NULL, FALSE);
304 g_return_val_if_fail (license != NULL, FALSE);
305 g_return_val_if_fail (source != NULL, FALSE);
306 g_return_val_if_fail (package != NULL, FALSE);
307 g_return_val_if_fail (origin != NULL, FALSE);
309 /* make sure gst_init() has been called */
310 g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);
312 GST_LOG ("attempting to load static plugin \"%s\" now...", name);
313 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
314 if (gst_plugin_register_func (plugin, &desc, user_data) != NULL) {
315 GST_INFO ("registered static plugin \"%s\"", name);
316 res = gst_default_registry_add_plugin (plugin);
317 GST_INFO ("added static plugin \"%s\", result: %d", name, res);
323 _gst_plugin_initialize (void)
327 _gst_plugin_inited = TRUE;
329 /* now register all static plugins */
330 GST_INFO ("registering %u static plugins", _num_static_plugins);
331 for (i = 0; i < _num_static_plugins; ++i) {
332 gst_plugin_register_static (_static_plugins[i].major_version,
333 _static_plugins[i].minor_version, _static_plugins[i].name,
334 _static_plugins[i].description, _static_plugins[i].plugin_init,
335 _static_plugins[i].version, _static_plugins[i].license,
336 _static_plugins[i].source, _static_plugins[i].package,
337 _static_plugins[i].origin);
340 if (_static_plugins) {
341 free (_static_plugins);
342 _static_plugins = NULL;
343 _num_static_plugins = 0;
347 /* this function could be extended to check if the plugin license matches the
348 * applications license (would require the app to register its license somehow).
349 * We'll wait for someone who's interested in it to code it :)
352 gst_plugin_check_license (const gchar * license)
354 const gchar **check_license = valid_licenses;
356 g_assert (check_license);
358 while (*check_license) {
359 if (strcmp (license, *check_license) == 0)
367 gst_plugin_check_version (gint major, gint minor)
369 /* return NULL if the major and minor version numbers are not compatible */
371 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
378 gst_plugin_register_func (GstPlugin * plugin, const GstPluginDesc * desc,
381 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
383 GST_WARNING ("plugin \"%s\" has incompatible version, not loading",
388 if (!desc->license || !desc->description || !desc->source ||
389 !desc->package || !desc->origin) {
391 GST_WARNING ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
396 if (!gst_plugin_check_license (desc->license)) {
398 GST_WARNING ("plugin \"%s\" has invalid license \"%s\", not loading",
399 plugin->filename, desc->license);
404 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
406 gst_plugin_desc_copy (&plugin->desc, desc);
409 if (!(((GstPluginInitFullFunc) (desc->plugin_init)) (plugin, user_data))) {
411 GST_WARNING ("plugin \"%s\" failed to initialise", plugin->filename);
412 plugin->module = NULL;
416 if (!((desc->plugin_init) (plugin))) {
418 GST_WARNING ("plugin \"%s\" failed to initialise", plugin->filename);
419 plugin->module = NULL;
425 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
430 #ifdef HAVE_SIGACTION
431 static struct sigaction oldaction;
432 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
435 * _gst_plugin_fault_handler_restore:
436 * segfault handler restorer
439 _gst_plugin_fault_handler_restore (void)
441 if (!_gst_plugin_fault_handler_is_setup)
444 _gst_plugin_fault_handler_is_setup = FALSE;
446 sigaction (SIGSEGV, &oldaction, NULL);
450 * _gst_plugin_fault_handler_sighandler:
451 * segfault handler implementation
454 _gst_plugin_fault_handler_sighandler (int signum)
456 /* We need to restore the fault handler or we'll keep getting it */
457 _gst_plugin_fault_handler_restore ();
461 g_print ("\nERROR: ");
462 g_print ("Caught a segmentation fault while loading plugin file:\n");
463 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
464 g_print ("Please either:\n");
465 g_print ("- remove it and restart.\n");
466 g_print ("- run with --gst-disable-segtrap and debug.\n");
470 g_print ("Caught unhandled signal on plugin loading\n");
476 * _gst_plugin_fault_handler_setup:
477 * sets up the segfault handler
480 _gst_plugin_fault_handler_setup (void)
482 struct sigaction action;
484 /* if asked to leave segfaults alone, just return */
485 if (!gst_segtrap_is_enabled ())
488 if (_gst_plugin_fault_handler_is_setup)
491 _gst_plugin_fault_handler_is_setup = TRUE;
493 memset (&action, 0, sizeof (action));
494 action.sa_handler = _gst_plugin_fault_handler_sighandler;
496 sigaction (SIGSEGV, &action, &oldaction);
498 #else /* !HAVE_SIGACTION */
500 _gst_plugin_fault_handler_restore (void)
505 _gst_plugin_fault_handler_setup (void)
508 #endif /* HAVE_SIGACTION */
510 static void _gst_plugin_fault_handler_setup ();
512 static GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
514 #define CHECK_PLUGIN_DESC_FIELD(desc,field,fn) \
515 if (G_UNLIKELY ((desc)->field == NULL)) { \
516 GST_ERROR ("GstPluginDesc for '%s' has no %s", fn, G_STRINGIFY (field)); \
520 * gst_plugin_load_file:
521 * @filename: the plugin filename to load
522 * @error: pointer to a NULL-valued GError
524 * Loads the given plugin and refs it. Caller needs to unref after use.
526 * Returns: a reference to the existing loaded GstPlugin, a reference to the
527 * newly-loaded GstPlugin, or NULL if an error occurred.
530 gst_plugin_load_file (const gchar * filename, GError ** error)
536 struct stat file_status;
537 GstRegistry *registry;
538 gboolean new_plugin = TRUE;
540 g_return_val_if_fail (filename != NULL, NULL);
542 registry = gst_registry_get_default ();
543 g_static_mutex_lock (&gst_plugin_loading_mutex);
545 plugin = gst_registry_lookup (registry, filename);
547 if (plugin->module) {
549 g_static_mutex_unlock (&gst_plugin_loading_mutex);
552 /* load plugin and update fields */
557 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
560 if (g_module_supported () == FALSE) {
561 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
564 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
568 if (g_stat (filename, &file_status)) {
569 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
572 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
577 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
578 if (module == NULL) {
579 GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
582 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
584 /* If we failed to open the shared object, then it's probably because a
585 * plugin is linked against the wrong libraries. Print out an easy-to-see
586 * message in this case. */
587 g_warning ("Failed to load plugin '%s': %s", filename, g_module_error ());
592 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
593 plugin->file_mtime = file_status.st_mtime;
594 plugin->file_size = file_status.st_size;
596 plugin->module = module;
597 plugin->filename = g_strdup (filename);
598 plugin->basename = g_path_get_basename (filename);
600 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
602 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
605 GST_PLUGIN_ERROR_MODULE,
606 "File \"%s\" is not a GStreamer plugin", filename);
607 g_module_close (module);
610 plugin->orig_desc = (GstPluginDesc *) ptr;
613 /* check plugin description: complain about bad values but accept them, to
614 * maintain backwards compatibility (FIXME: 0.11) */
615 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, name, filename);
616 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, description, filename);
617 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, version, filename);
618 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, license, filename);
619 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, source, filename);
620 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, package, filename);
621 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, origin, filename);
624 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
627 /* this is where we load the actual .so, so let's trap SIGSEGV */
628 _gst_plugin_fault_handler_setup ();
629 _gst_plugin_fault_handler_filename = plugin->filename;
631 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
634 if (!gst_plugin_register_func (plugin, plugin->orig_desc, NULL)) {
635 /* remove signal handler */
636 _gst_plugin_fault_handler_restore ();
637 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
641 GST_PLUGIN_ERROR_MODULE,
642 "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
644 g_module_close (module);
648 /* remove signal handler */
649 _gst_plugin_fault_handler_restore ();
650 _gst_plugin_fault_handler_filename = NULL;
651 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
654 gst_object_ref (plugin);
655 gst_default_registry_add_plugin (plugin);
658 g_static_mutex_unlock (&gst_plugin_loading_mutex);
664 gst_object_unref (plugin);
665 g_static_mutex_unlock (&gst_plugin_loading_mutex);
671 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
673 dest->major_version = src->major_version;
674 dest->minor_version = src->minor_version;
675 dest->name = g_intern_string (src->name);
676 /* maybe intern the description too, just for convenience? */
677 dest->description = g_strdup (src->description);
678 dest->plugin_init = src->plugin_init;
679 dest->version = g_intern_string (src->version);
680 dest->license = g_intern_string (src->license);
681 dest->source = g_intern_string (src->source);
682 dest->package = g_intern_string (src->package);
683 dest->origin = g_intern_string (src->origin);
688 gst_plugin_desc_free (GstPluginDesc * desc)
690 g_free (desc->description);
691 memset (desc, 0, sizeof (GstPluginDesc));
695 * gst_plugin_get_name:
696 * @plugin: plugin to get the name of
698 * Get the short name of the plugin
700 * Returns: the name of the plugin
703 gst_plugin_get_name (GstPlugin * plugin)
705 g_return_val_if_fail (plugin != NULL, NULL);
707 return plugin->desc.name;
711 * gst_plugin_get_description:
712 * @plugin: plugin to get long name of
714 * Get the long descriptive name of the plugin
716 * Returns: the long name of the plugin
718 G_CONST_RETURN gchar *
719 gst_plugin_get_description (GstPlugin * plugin)
721 g_return_val_if_fail (plugin != NULL, NULL);
723 return plugin->desc.description;
727 * gst_plugin_get_filename:
728 * @plugin: plugin to get the filename of
730 * get the filename of the plugin
732 * Returns: the filename of the plugin
734 G_CONST_RETURN gchar *
735 gst_plugin_get_filename (GstPlugin * plugin)
737 g_return_val_if_fail (plugin != NULL, NULL);
739 return plugin->filename;
743 * gst_plugin_get_version:
744 * @plugin: plugin to get the version of
746 * get the version of the plugin
748 * Returns: the version of the plugin
750 G_CONST_RETURN gchar *
751 gst_plugin_get_version (GstPlugin * plugin)
753 g_return_val_if_fail (plugin != NULL, NULL);
755 return plugin->desc.version;
759 * gst_plugin_get_license:
760 * @plugin: plugin to get the license of
762 * get the license of the plugin
764 * Returns: the license of the plugin
766 G_CONST_RETURN gchar *
767 gst_plugin_get_license (GstPlugin * plugin)
769 g_return_val_if_fail (plugin != NULL, NULL);
771 return plugin->desc.license;
775 * gst_plugin_get_source:
776 * @plugin: plugin to get the source of
778 * get the source module the plugin belongs to.
780 * Returns: the source of the plugin
782 G_CONST_RETURN gchar *
783 gst_plugin_get_source (GstPlugin * plugin)
785 g_return_val_if_fail (plugin != NULL, NULL);
787 return plugin->desc.source;
791 * gst_plugin_get_package:
792 * @plugin: plugin to get the package of
794 * get the package the plugin belongs to.
796 * Returns: the package of the plugin
798 G_CONST_RETURN gchar *
799 gst_plugin_get_package (GstPlugin * plugin)
801 g_return_val_if_fail (plugin != NULL, NULL);
803 return plugin->desc.package;
807 * gst_plugin_get_origin:
808 * @plugin: plugin to get the origin of
810 * get the URL where the plugin comes from
812 * Returns: the origin of the plugin
814 G_CONST_RETURN gchar *
815 gst_plugin_get_origin (GstPlugin * plugin)
817 g_return_val_if_fail (plugin != NULL, NULL);
819 return plugin->desc.origin;
823 * gst_plugin_get_module:
824 * @plugin: plugin to query
826 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
829 * Returns: module belonging to the plugin or NULL if the plugin isn't
833 gst_plugin_get_module (GstPlugin * plugin)
835 g_return_val_if_fail (plugin != NULL, NULL);
837 return plugin->module;
841 * gst_plugin_is_loaded:
842 * @plugin: plugin to query
844 * queries if the plugin is loaded into memory
846 * Returns: TRUE is loaded, FALSE otherwise
849 gst_plugin_is_loaded (GstPlugin * plugin)
851 g_return_val_if_fail (plugin != NULL, FALSE);
853 return (plugin->module != NULL || plugin->filename == NULL);
858 * gst_plugin_feature_list:
859 * @plugin: plugin to query
860 * @filter: the filter to use
861 * @first: only return first match
862 * @user_data: user data passed to the filter function
864 * Runs a filter against all plugin features and returns a GList with
865 * the results. If the first flag is set, only the first match is
866 * returned (as a list with a single object).
868 * Returns: a GList of features, g_list_free after use.
871 gst_plugin_feature_filter (GstPlugin * plugin,
872 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
877 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
879 for (g = list; g; g = g->next) {
880 gst_object_ref (plugin);
888 GstPluginFeatureFilter filter;
896 _feature_filter (GstPlugin * plugin, gpointer user_data)
899 FeatureFilterData *data = (FeatureFilterData *) user_data;
901 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
904 data->result = g_list_concat (data->result, result);
911 * gst_plugin_list_feature_filter:
912 * @list: a #GList of plugins to query
913 * @filter: the filter function to use
914 * @first: only return first match
915 * @user_data: user data passed to the filter function
917 * Runs a filter against all plugin features of the plugins in the given
918 * list and returns a GList with the results.
919 * If the first flag is set, only the first match is
920 * returned (as a list with a single object).
922 * Returns: a GList of features, g_list_free after use.
925 gst_plugin_list_feature_filter (GList * list,
926 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
928 FeatureFilterData data;
931 data.filter = filter;
933 data.user_data = user_data;
936 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
937 g_list_free (result);
944 * gst_plugin_name_filter:
945 * @plugin: the plugin to check
946 * @name: the name of the plugin
948 * A standard filter that returns TRUE when the plugin is of the
951 * Returns: TRUE if the plugin is of the given name.
954 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
956 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
961 * gst_plugin_find_feature:
962 * @plugin: plugin to get the feature from
963 * @name: The name of the feature to find
964 * @type: The type of the feature to find
966 * Find a feature of the given name and type in the given plugin.
968 * Returns: a GstPluginFeature or NULL if the feature was not found.
971 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
974 GstPluginFeature *result = NULL;
975 GstTypeNameData data;
977 g_return_val_if_fail (name != NULL, NULL);
982 walk = gst_filter_run (plugin->features,
983 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
986 result = GST_PLUGIN_FEATURE (walk->data);
988 gst_object_ref (result);
989 gst_plugin_feature_list_free (walk);
998 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
1000 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
1006 * gst_plugin_find_feature_by_name:
1007 * @plugin: plugin to get the feature from
1008 * @name: The name of the feature to find
1010 * Find a feature of the given name in the given plugin.
1012 * Returns: a GstPluginFeature or NULL if the feature was not found.
1015 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
1018 GstPluginFeature *result = NULL;
1020 g_return_val_if_fail (name != NULL, NULL);
1022 walk = gst_filter_run (plugin->features,
1023 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
1026 result = GST_PLUGIN_FEATURE (walk->data);
1028 gst_object_ref (result);
1029 gst_plugin_feature_list_free (walk);
1037 * gst_plugin_load_by_name:
1038 * @name: name of plugin to load
1040 * Load the named plugin. Refs the plugin.
1042 * Returns: A reference to a loaded plugin, or NULL on error.
1045 gst_plugin_load_by_name (const gchar * name)
1047 GstPlugin *plugin, *newplugin;
1048 GError *error = NULL;
1050 GST_DEBUG ("looking up plugin %s in default registry", name);
1051 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
1053 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
1054 newplugin = gst_plugin_load_file (plugin->filename, &error);
1055 gst_object_unref (plugin);
1058 GST_WARNING ("load_plugin error: %s", error->message);
1059 g_error_free (error);
1062 /* newplugin was reffed by load_file */
1066 GST_DEBUG ("Could not find plugin %s in registry", name);
1072 * @plugin: plugin to load
1074 * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
1075 * untouched. The normal use pattern of this function goes like this:
1078 * GstPlugin *loaded_plugin;
1079 * loaded_plugin = gst_plugin_load (plugin);
1080 * // presumably, we're no longer interested in the potentially-unloaded plugin
1081 * gst_object_unref (plugin);
1082 * plugin = loaded_plugin;
1085 * Returns: A reference to a loaded plugin, or NULL on error.
1088 gst_plugin_load (GstPlugin * plugin)
1090 GError *error = NULL;
1091 GstPlugin *newplugin;
1093 if (gst_plugin_is_loaded (plugin)) {
1097 if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
1104 GST_WARNING ("load_plugin error: %s", error->message);
1105 g_error_free (error);
1111 * gst_plugin_list_free:
1112 * @list: list of #GstPlugin
1114 * Unrefs each member of @list, then frees the list.
1117 gst_plugin_list_free (GList * list)
1121 for (g = list; g; g = g->next) {
1122 gst_object_unref (GST_PLUGIN_CAST (g->data));
1127 /* ===== plugin dependencies ===== */
1130 * ENV + xyz where ENV can contain multiple values separated by SEPARATOR
1131 * xyz may be "" (if ENV contains path to file rather than dir)
1132 * ENV + *xyz same as above, but xyz acts as suffix filter
1133 * ENV + xyz* same as above, but xyz acts as prefix filter (is this needed?)
1134 * ENV + *xyz* same as above, but xyz acts as strstr filter (is this needed?)
1136 * same as above, with additional paths hard-coded at compile-time:
1137 * - only check paths + ... if ENV is not set or yields not paths
1138 * - always check paths + ... in addition to ENV
1140 * When user specifies set of environment variables, he/she may also use e.g.
1141 * "HOME/.mystuff/plugins", and we'll expand the content of $HOME with the
1145 /* we store in registry:
1148 * - environment variables (array of strings)
1149 * - last hash of env variable contents (uint) (so we can avoid doing stats
1150 * if one of the env vars has changed; premature optimisation galore)
1151 * - hard-coded paths (array of strings)
1152 * - xyz filename/suffix/prefix strings (array of strings)
1154 * - last hash of file/dir stats (int)
1156 * (= struct GstPluginDep)
1160 gst_plugin_ext_dep_get_env_vars_hash (GstPlugin * plugin, GstPluginDep * dep)
1165 /* there's no deeper logic to what we do here; all we want to know (when
1166 * checking if the plugin needs to be rescanned) is whether the content of
1167 * one of the environment variables in the list is different from when it
1168 * was last scanned */
1170 for (e = dep->env_vars; e != NULL && *e != NULL; ++e) {
1174 /* order matters: "val",NULL needs to yield a different hash than
1175 * NULL,"val", so do a shift here whether the var is set or not */
1178 /* want environment variable at beginning of string */
1179 if (!g_ascii_isalnum (**e)) {
1180 GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1181 "variable string: %s", *e);
1185 /* user is allowed to specify e.g. "HOME/.pitivi/plugins" */
1186 g_strlcpy (env_var, *e, sizeof (env_var));
1187 g_strdelimit (env_var, "/\\", '\0');
1189 if ((val = g_getenv (env_var)))
1190 hash += g_str_hash (val);
1197 _priv_plugin_deps_env_vars_changed (GstPlugin * plugin)
1201 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1202 GstPluginDep *dep = l->data;
1204 if (dep->env_hash != gst_plugin_ext_dep_get_env_vars_hash (plugin, dep))
1212 gst_plugin_ext_dep_extract_env_vars_paths (GstPlugin * plugin,
1216 GList *paths = NULL;
1218 for (evars = dep->env_vars; evars != NULL && *evars != NULL; ++evars) {
1222 /* want environment variable at beginning of string */
1223 if (!g_ascii_isalnum (**evars)) {
1224 GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1225 "variable string: %s", *evars);
1229 /* user is allowed to specify e.g. "HOME/.pitivi/plugins", which we want to
1230 * split into the env_var name component and the path component */
1231 components = g_strsplit_set (*evars, "/\\", 2);
1232 g_assert (components != NULL);
1234 e = g_getenv (components[0]);
1235 GST_LOG_OBJECT (plugin, "expanding %s = '%s' (path suffix: %s)",
1236 components[0], GST_STR_NULL (e), GST_STR_NULL (components[1]));
1238 if (components[1] != NULL) {
1239 g_strdelimit (components[1], "/\\", G_DIR_SEPARATOR);
1242 if (e != NULL && *e != '\0') {
1246 arr = g_strsplit (e, G_SEARCHPATH_SEPARATOR_S, -1);
1248 for (i = 0; arr != NULL && arr[i] != NULL; ++i) {
1251 if (!g_path_is_absolute (arr[i])) {
1252 GST_INFO_OBJECT (plugin, "ignoring environment variable content '%s'"
1253 ": either not an absolute path or not a path at all", arr[i]);
1257 if (components[1] != NULL) {
1258 full_path = g_build_filename (arr[i], components[1], NULL);
1260 full_path = g_strdup (arr[i]);
1263 if (!g_list_find_custom (paths, full_path, (GCompareFunc) strcmp)) {
1264 GST_LOG_OBJECT (plugin, "path: '%s'", full_path);
1265 paths = g_list_prepend (paths, full_path);
1268 GST_LOG_OBJECT (plugin, "path: '%s' (duplicate,ignoring)", full_path);
1276 g_strfreev (components);
1279 GST_LOG_OBJECT (plugin, "Extracted %d paths from environment",
1280 g_list_length (paths));
1286 gst_plugin_ext_dep_get_hash_from_stat_entry (struct stat *s)
1288 if (!(s->st_mode & (S_IFDIR | S_IFREG)))
1291 /* completely random formula */
1292 return ((s->st_size << 3) + (s->st_mtime << 5)) ^ s->st_ctime;
1296 gst_plugin_ext_dep_direntry_matches (GstPlugin * plugin, const gchar * entry,
1297 const gchar ** filenames, GstPluginDependencyFlags flags)
1299 /* no filenames specified, match all entries for now (could probably
1300 * optimise by just taking the dir stat hash or so) */
1301 if (filenames == NULL || *filenames == NULL || **filenames == '\0')
1304 while (*filenames != NULL) {
1306 if (((flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)) &&
1307 g_str_has_suffix (entry, *filenames)) {
1309 /* else it's an exact match that's needed */
1310 } else if (strcmp (entry, *filenames) == 0) {
1313 GST_LOG ("%s does not match %s, flags=0x%04x", entry, *filenames, flags);
1320 gst_plugin_ext_dep_scan_dir_and_match_names (GstPlugin * plugin,
1321 const gchar * path, const gchar ** filenames,
1322 GstPluginDependencyFlags flags, int depth)
1325 gboolean recurse_dirs;
1330 recurse_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1332 dir = g_dir_open (path, 0, &err);
1334 GST_DEBUG_OBJECT (plugin, "g_dir_open(%s) failed: %s", path, err->message);
1339 /* FIXME: we're assuming here that we always get the directory entries in
1340 * the same order, and not in a random order */
1341 while ((entry = g_dir_read_name (dir))) {
1342 gboolean have_match;
1348 gst_plugin_ext_dep_direntry_matches (plugin, entry, filenames, flags);
1350 /* avoid the stat if possible */
1351 if (!have_match && !recurse_dirs)
1354 full_path = g_build_filename (path, entry, NULL);
1355 if (g_stat (full_path, &s) < 0) {
1356 fhash = (guint) - 1;
1357 GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1358 g_strerror (errno));
1359 } else if (have_match) {
1360 fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1361 GST_LOG_OBJECT (plugin, "stat: %s (result: %u)", full_path, fhash);
1362 } else if ((s.st_mode & (S_IFDIR))) {
1363 fhash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, full_path,
1364 filenames, flags, depth + 1);
1366 /* it's not a name match, we want to recurse, but it's not a directory */
1371 hash = (hash + fhash) << 1;
1380 gst_plugin_ext_dep_scan_path_with_filenames (GstPlugin * plugin,
1381 const gchar * path, const gchar ** filenames,
1382 GstPluginDependencyFlags flags)
1384 const gchar *empty_filenames[] = { "", NULL };
1385 gboolean recurse_into_dirs, partial_names;
1388 /* to avoid special-casing below (FIXME?) */
1389 if (filenames == NULL || *filenames == NULL)
1390 filenames = empty_filenames;
1392 recurse_into_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1393 partial_names = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX);
1395 /* if we can construct the exact paths to check with the data we have, just
1396 * stat them one by one; this is more efficient than opening the directory
1397 * and going through each entry to see if it matches one of our filenames. */
1398 if (!recurse_into_dirs && !partial_names) {
1399 for (i = 0; filenames[i] != NULL; ++i) {
1404 full_path = g_build_filename (path, filenames[i], NULL);
1405 if (g_stat (full_path, &s) < 0) {
1406 fhash = (guint) - 1;
1407 GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1408 g_strerror (errno));
1410 fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1411 GST_LOG_OBJECT (plugin, "stat: %s (result: %08x)", full_path, fhash);
1413 hash = (hash + fhash) << 1;
1417 hash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, path,
1418 filenames, flags, 0);
1425 gst_plugin_ext_dep_get_stat_hash (GstPlugin * plugin, GstPluginDep * dep)
1427 gboolean paths_are_default_only;
1429 guint scan_hash = 0;
1431 GST_LOG_OBJECT (plugin, "start");
1433 paths_are_default_only =
1434 dep->flags & GST_PLUGIN_DEPENDENCY_FLAG_PATHS_ARE_DEFAULT_ONLY;
1436 scan_paths = gst_plugin_ext_dep_extract_env_vars_paths (plugin, dep);
1438 if (scan_paths == NULL || !paths_are_default_only) {
1441 for (paths = dep->paths; paths != NULL && *paths != NULL; ++paths) {
1442 const gchar *path = *paths;
1444 if (!g_list_find_custom (scan_paths, path, (GCompareFunc) strcmp)) {
1445 GST_LOG_OBJECT (plugin, "path: '%s'", path);
1446 scan_paths = g_list_prepend (scan_paths, g_strdup (path));
1448 GST_LOG_OBJECT (plugin, "path: '%s' (duplicate, ignoring)", path);
1453 /* not that the order really matters, but it makes debugging easier */
1454 scan_paths = g_list_reverse (scan_paths);
1456 while (scan_paths != NULL) {
1457 const gchar *path = scan_paths->data;
1459 scan_hash += gst_plugin_ext_dep_scan_path_with_filenames (plugin, path,
1460 (const gchar **) dep->names, dep->flags);
1461 scan_hash = scan_hash << 1;
1463 g_free (scan_paths->data);
1464 scan_paths = g_list_delete_link (scan_paths, scan_paths);
1467 GST_LOG_OBJECT (plugin, "done, scan_hash: %08x", scan_hash);
1472 _priv_plugin_deps_files_changed (GstPlugin * plugin)
1476 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1477 GstPluginDep *dep = l->data;
1479 if (dep->stat_hash != gst_plugin_ext_dep_get_stat_hash (plugin, dep))
1487 gst_plugin_ext_dep_free (GstPluginDep * dep)
1489 g_strfreev (dep->env_vars);
1490 g_strfreev (dep->paths);
1491 g_strfreev (dep->names);
1496 gst_plugin_ext_dep_strv_equal (gchar ** arr1, gchar ** arr2)
1500 if (arr1 == NULL || arr2 == NULL)
1502 for (; *arr1 != NULL && *arr2 != NULL; ++arr1, ++arr2) {
1503 if (strcmp (*arr1, *arr2) != 0)
1506 return (*arr1 == *arr2);
1510 gst_plugin_ext_dep_equals (GstPluginDep * dep, const gchar ** env_vars,
1511 const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1513 if (dep->flags != flags)
1516 return gst_plugin_ext_dep_strv_equal (dep->env_vars, (gchar **) env_vars) &&
1517 gst_plugin_ext_dep_strv_equal (dep->paths, (gchar **) paths) &&
1518 gst_plugin_ext_dep_strv_equal (dep->names, (gchar **) names);
1522 * gst_plugin_add_dependency:
1523 * @plugin: a #GstPlugin
1524 * @env_vars: NULL-terminated array of environent variables affecting the
1525 * feature set of the plugin (e.g. an environment variable containing
1526 * paths where to look for additional modules/plugins of a library),
1527 * or NULL. Environment variable names may be followed by a path component
1528 * which will be added to the content of the environment variable, e.g.
1529 * "HOME/.mystuff/plugins".
1530 * @paths: NULL-terminated array of directories/paths where dependent files
1532 * @names: NULL-terminated array of file names (or file name suffixes,
1533 * depending on @flags) to be used in combination with the paths from
1534 * @paths and/or the paths extracted from the environment variables in
1535 * @env_vars, or NULL.
1536 * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1538 * Make GStreamer aware of external dependencies which affect the feature
1539 * set of this plugin (ie. the elements or typefinders associated with it).
1541 * GStreamer will re-inspect plugins with external dependencies whenever any
1542 * of the external dependencies change. This is useful for plugins which wrap
1543 * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1544 * library and makes visualisations available as GStreamer elements, or a
1545 * codec loader which exposes elements and/or caps dependent on what external
1546 * codec libraries are currently installed.
1551 gst_plugin_add_dependency (GstPlugin * plugin, const gchar ** env_vars,
1552 const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1557 g_return_if_fail (GST_IS_PLUGIN (plugin));
1558 g_return_if_fail (env_vars != NULL || paths != NULL);
1560 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1561 if (gst_plugin_ext_dep_equals (l->data, env_vars, paths, names, flags)) {
1562 GST_LOG_OBJECT (plugin, "dependency already registered");
1567 dep = g_new0 (GstPluginDep, 1);
1569 dep->env_vars = g_strdupv ((gchar **) env_vars);
1570 dep->paths = g_strdupv ((gchar **) paths);
1571 dep->names = g_strdupv ((gchar **) names);
1574 dep->env_hash = gst_plugin_ext_dep_get_env_vars_hash (plugin, dep);
1575 dep->stat_hash = gst_plugin_ext_dep_get_stat_hash (plugin, dep);
1577 plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
1579 GST_DEBUG_OBJECT (plugin, "added dependency:");
1580 for (; env_vars != NULL && *env_vars != NULL; ++env_vars)
1581 GST_DEBUG_OBJECT (plugin, " evar: %s", *env_vars);
1582 for (; paths != NULL && *paths != NULL; ++paths)
1583 GST_DEBUG_OBJECT (plugin, " path: %s", *paths);
1584 for (; names != NULL && *names != NULL; ++names)
1585 GST_DEBUG_OBJECT (plugin, " name: %s", *names);
1589 * gst_plugin_add_dependency_simple:
1590 * @plugin: the #GstPlugin
1591 * @env_vars: one or more environent variables (separated by ':', ';' or ','),
1592 * or NULL. Environment variable names may be followed by a path component
1593 * which will be added to the content of the environment variable, e.g.
1594 * "HOME/.mystuff/plugins:MYSTUFF_PLUGINS_PATH"
1595 * @paths: one ore more directory paths (separated by ':' or ';' or ','),
1596 * or NULL. Example: "/usr/lib/mystuff/plugins"
1597 * @names: one or more file names or file name suffixes (separated by commas),
1599 * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1601 * Make GStreamer aware of external dependencies which affect the feature
1602 * set of this plugin (ie. the elements or typefinders associated with it).
1604 * GStreamer will re-inspect plugins with external dependencies whenever any
1605 * of the external dependencies change. This is useful for plugins which wrap
1606 * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1607 * library and makes visualisations available as GStreamer elements, or a
1608 * codec loader which exposes elements and/or caps dependent on what external
1609 * codec libraries are currently installed.
1611 * Convenience wrapper function for gst_plugin_add_dependency() which
1612 * takes simple strings as arguments instead of string arrays, with multiple
1613 * arguments separated by predefined delimiters (see above).
1618 gst_plugin_add_dependency_simple (GstPlugin * plugin,
1619 const gchar * env_vars, const gchar * paths, const gchar * names,
1620 GstPluginDependencyFlags flags)
1622 gchar **a_evars = NULL;
1623 gchar **a_paths = NULL;
1624 gchar **a_names = NULL;
1627 a_evars = g_strsplit_set (env_vars, ":;,", -1);
1629 a_paths = g_strsplit_set (paths, ":;,", -1);
1631 a_names = g_strsplit_set (names, ",", -1);
1633 gst_plugin_add_dependency (plugin, (const gchar **) a_evars,
1634 (const gchar **) a_paths, (const gchar **) a_names, flags);
1637 g_strfreev (a_evars);
1639 g_strfreev (a_paths);
1641 g_strfreev (a_names);