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 #GstRegistryPool), you
40 * can add any object that subclasses #GstPluginFeature.
42 * Use gst_plugin_find_feature() and gst_plugin_get_feature_list() to find
43 * features in a plugin.
45 * Usually plugins are always automaticlly loaded so you don't need to call
46 * gst_plugin_load() explicitly to bring it into memory. There are options to
47 * statically link plugins to an app or even use GStreamer without a plugin
48 * repository in which case gst_plugin_load() can be needed to bring the plugin
55 #include <glib/gstdio.h>
56 #include <sys/types.h>
66 #include "gst_private.h"
68 #include "gstplugin.h"
69 #include "gstversion.h"
71 #include "gstfilter.h"
72 #include "gstregistry.h"
73 #include "gstmacros.h"
76 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
78 static GList *_gst_plugin_static = NULL;
79 static gboolean _gst_plugin_inited;
81 /* static variables for segfault handling of plugin loading */
82 static char *_gst_plugin_fault_handler_filename = NULL;
83 extern gboolean _gst_disable_segtrap; /* see gst.c */
86 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
89 /* list of valid licenses.
90 * One of these must be specified or the plugin won't be loaded
91 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
94 * GPL: http://www.gnu.org/copyleft/gpl.html
95 * LGPL: http://www.gnu.org/copyleft/lesser.html
96 * QPL: http://www.trolltech.com/licenses/qpl.html
97 * MPL: http://www.opensource.org/licenses/mozilla1.1.php
98 * MIT/X11: http://www.opensource.org/licenses/mit-license.php
99 * 3-clause BSD: http://www.opensource.org/licenses/bsd-license.php
101 static const gchar *valid_licenses[] = {
102 "LGPL", /* GNU Lesser General Public License */
103 "GPL", /* GNU General Public License */
104 "QPL", /* Trolltech Qt Public License */
105 "GPL/QPL", /* Combi-license of GPL + QPL */
106 "MPL", /* MPL 1.1 license */
107 "BSD", /* 3-clause BSD license */
108 "MIT/X11", /* MIT/X11 license */
109 "Proprietary", /* Proprietary license */
110 GST_LICENSE_UNKNOWN, /* some other license */
114 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
115 GstPluginDesc * desc);
116 static void gst_plugin_desc_copy (GstPluginDesc * dest,
117 const GstPluginDesc * src);
118 static void gst_plugin_desc_free (GstPluginDesc * desc);
121 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
124 gst_plugin_init (GstPlugin * plugin)
126 /* do nothing, needed because of G_DEFINE_TYPE */
130 gst_plugin_finalize (GObject * object)
132 GstPlugin *plugin = GST_PLUGIN_CAST (object);
133 GstRegistry *registry = gst_registry_get_default ();
136 GST_DEBUG ("finalizing plugin %p", plugin);
137 for (g = registry->plugins; g; g = g->next) {
138 if (g->data == (gpointer) plugin) {
139 g_warning ("removing plugin that is still in registry");
142 g_free (plugin->filename);
143 g_free (plugin->basename);
144 gst_plugin_desc_free (&plugin->desc);
146 G_OBJECT_CLASS (gst_plugin_parent_class)->finalize (object);
150 gst_plugin_class_init (GstPluginClass * klass)
152 G_OBJECT_CLASS (klass)->finalize = GST_DEBUG_FUNCPTR (gst_plugin_finalize);
156 gst_plugin_error_quark (void)
158 static GQuark quark = 0;
161 quark = g_quark_from_static_string ("gst_plugin_error");
165 /* this function can be called in the GCC constructor extension, before
166 * the _gst_plugin_initialize() was called. In that case, we store the
167 * plugin description in a list to initialize it when we open the main
169 * When the main module is known, we can register the plugin right away.
172 _gst_plugin_register_static (GstPluginDesc * desc)
174 if (!_gst_plugin_inited) {
176 GST_LOG ("queueing static plugin \"%s\" for loading later on",
178 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
183 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
184 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
185 if (gst_plugin_register_func (plugin, desc)) {
187 GST_INFO ("loaded static plugin \"%s\"", desc->name);
188 gst_default_registry_add_plugin (plugin);
194 _gst_plugin_initialize (void)
196 _gst_plugin_inited = TRUE;
198 /* now register all static plugins */
199 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
203 /* this function could be extended to check if the plugin license matches the
204 * applications license (would require the app to register its license somehow).
205 * We'll wait for someone who's interested in it to code it :)
208 gst_plugin_check_license (const gchar * license)
210 const gchar **check_license = valid_licenses;
212 g_assert (check_license);
214 while (*check_license) {
215 if (strcmp (license, *check_license) == 0)
223 gst_plugin_check_version (gint major, gint minor)
225 /* return NULL if the major and minor version numbers are not compatible */
227 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
234 gst_plugin_register_func (GstPlugin * plugin, GstPluginDesc * desc)
236 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
238 GST_WARNING ("plugin \"%s\" has incompatible version, not loading",
243 if (!desc->license || !desc->description || !desc->source ||
244 !desc->package || !desc->origin) {
246 GST_WARNING ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
251 if (!gst_plugin_check_license (desc->license)) {
253 GST_WARNING ("plugin \"%s\" has invalid license \"%s\", not loading",
254 plugin->filename, desc->license);
259 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
261 gst_plugin_desc_copy (&plugin->desc, desc);
263 if (!((desc->plugin_init) (plugin))) {
265 GST_WARNING ("plugin \"%s\" failed to initialise", plugin->filename);
266 plugin->module = NULL;
271 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
276 #ifdef HAVE_SIGACTION
277 static struct sigaction oldaction;
280 * _gst_plugin_fault_handler_restore:
281 * segfault handler restorer
284 _gst_plugin_fault_handler_restore (void)
286 if (!_gst_plugin_fault_handler_is_setup)
289 _gst_plugin_fault_handler_is_setup = FALSE;
291 sigaction (SIGSEGV, &oldaction, NULL);
295 * _gst_plugin_fault_handler_sighandler:
296 * segfault handler implementation
299 _gst_plugin_fault_handler_sighandler (int signum)
301 /* We need to restore the fault handler or we'll keep getting it */
302 _gst_plugin_fault_handler_restore ();
306 g_print ("\nERROR: ");
307 g_print ("Caught a segmentation fault while loading plugin file:\n");
308 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
309 g_print ("Please either:\n");
310 g_print ("- remove it and restart.\n");
311 g_print ("- run with --gst-disable-segtrap and debug.\n");
315 g_print ("Caught unhandled signal on plugin loading\n");
321 * _gst_plugin_fault_handler_setup:
322 * sets up the segfault handler
325 _gst_plugin_fault_handler_setup (void)
327 struct sigaction action;
329 /* if asked to leave segfaults alone, just return */
330 if (_gst_disable_segtrap)
333 if (_gst_plugin_fault_handler_is_setup)
336 _gst_plugin_fault_handler_is_setup = TRUE;
338 memset (&action, 0, sizeof (action));
339 action.sa_handler = _gst_plugin_fault_handler_sighandler;
341 sigaction (SIGSEGV, &action, &oldaction);
345 _gst_plugin_fault_handler_restore (void)
350 _gst_plugin_fault_handler_setup (void)
355 static void _gst_plugin_fault_handler_setup ();
357 GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
360 * gst_plugin_load_file:
361 * @filename: the plugin filename to load
362 * @error: pointer to a NULL-valued GError
364 * Loads the given plugin and refs it. Caller needs to unref after use.
366 * Returns: a reference to the existing loaded GstPlugin, a reference to the
367 * newly-loaded GstPlugin, or NULL if an error occurred.
370 gst_plugin_load_file (const gchar * filename, GError ** error)
376 struct stat file_status;
377 GstRegistry *registry;
379 g_return_val_if_fail (filename != NULL, NULL);
381 registry = gst_registry_get_default ();
382 g_static_mutex_lock (&gst_plugin_loading_mutex);
384 plugin = gst_registry_lookup (registry, filename);
386 if (plugin->module) {
387 g_static_mutex_unlock (&gst_plugin_loading_mutex);
390 gst_object_unref (plugin);
395 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
398 if (g_module_supported () == FALSE) {
399 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
402 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
406 if (g_stat (filename, &file_status)) {
407 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
410 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
415 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
416 if (module == NULL) {
417 GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
420 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
422 /* If we failed to open the shared object, then it's probably because a
423 * plugin is linked against the wrong libraries. Print out an easy-to-see
424 * message in this case. */
425 g_warning ("Failed to load plugin '%s': %s", filename, g_module_error ());
429 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
431 plugin->module = module;
432 plugin->filename = g_strdup (filename);
433 plugin->basename = g_path_get_basename (filename);
434 plugin->file_mtime = file_status.st_mtime;
435 plugin->file_size = file_status.st_size;
437 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
439 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
442 GST_PLUGIN_ERROR_MODULE,
443 "File \"%s\" is not a GStreamer plugin", filename);
444 g_module_close (module);
447 plugin->orig_desc = (GstPluginDesc *) ptr;
449 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
452 /* this is where we load the actual .so, so let's trap SIGSEGV */
453 _gst_plugin_fault_handler_setup ();
454 _gst_plugin_fault_handler_filename = plugin->filename;
456 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
459 if (!gst_plugin_register_func (plugin, plugin->orig_desc)) {
460 /* remove signal handler */
461 _gst_plugin_fault_handler_restore ();
462 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
466 GST_PLUGIN_ERROR_MODULE,
467 "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
469 g_module_close (module);
473 /* remove signal handler */
474 _gst_plugin_fault_handler_restore ();
475 _gst_plugin_fault_handler_filename = NULL;
476 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
478 gst_object_ref (plugin);
479 gst_default_registry_add_plugin (plugin);
481 g_static_mutex_unlock (&gst_plugin_loading_mutex);
487 gst_object_unref (plugin);
488 g_static_mutex_unlock (&gst_plugin_loading_mutex);
494 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
496 dest->major_version = src->major_version;
497 dest->minor_version = src->minor_version;
498 dest->name = g_intern_string (src->name);
499 /* maybe intern the description too, just for convenience? */
500 dest->description = g_strdup (src->description);
501 dest->plugin_init = src->plugin_init;
502 dest->version = g_intern_string (src->version);
503 dest->license = g_intern_string (src->license);
504 dest->source = g_intern_string (src->source);
505 dest->package = g_intern_string (src->package);
506 dest->origin = g_intern_string (src->origin);
511 gst_plugin_desc_free (GstPluginDesc * desc)
513 g_free (desc->description);
514 memset (desc, 0, sizeof (GstPluginDesc));
518 * gst_plugin_get_name:
519 * @plugin: plugin to get the name of
521 * Get the short name of the plugin
523 * Returns: the name of the plugin
526 gst_plugin_get_name (GstPlugin * plugin)
528 g_return_val_if_fail (plugin != NULL, NULL);
530 return plugin->desc.name;
534 * gst_plugin_get_description:
535 * @plugin: plugin to get long name of
537 * Get the long descriptive name of the plugin
539 * Returns: the long name of the plugin
541 G_CONST_RETURN gchar *
542 gst_plugin_get_description (GstPlugin * plugin)
544 g_return_val_if_fail (plugin != NULL, NULL);
546 return plugin->desc.description;
550 * gst_plugin_get_filename:
551 * @plugin: plugin to get the filename of
553 * get the filename of the plugin
555 * Returns: the filename of the plugin
557 G_CONST_RETURN gchar *
558 gst_plugin_get_filename (GstPlugin * plugin)
560 g_return_val_if_fail (plugin != NULL, NULL);
562 return plugin->filename;
566 * gst_plugin_get_version:
567 * @plugin: plugin to get the version of
569 * get the version of the plugin
571 * Returns: the version of the plugin
573 G_CONST_RETURN gchar *
574 gst_plugin_get_version (GstPlugin * plugin)
576 g_return_val_if_fail (plugin != NULL, NULL);
578 return plugin->desc.version;
582 * gst_plugin_get_license:
583 * @plugin: plugin to get the license of
585 * get the license of the plugin
587 * Returns: the license of the plugin
589 G_CONST_RETURN gchar *
590 gst_plugin_get_license (GstPlugin * plugin)
592 g_return_val_if_fail (plugin != NULL, NULL);
594 return plugin->desc.license;
598 * gst_plugin_get_source:
599 * @plugin: plugin to get the source of
601 * get the source module the plugin belongs to.
603 * Returns: the source of the plugin
605 G_CONST_RETURN gchar *
606 gst_plugin_get_source (GstPlugin * plugin)
608 g_return_val_if_fail (plugin != NULL, NULL);
610 return plugin->desc.source;
614 * gst_plugin_get_package:
615 * @plugin: plugin to get the package of
617 * get the package the plugin belongs to.
619 * Returns: the package of the plugin
621 G_CONST_RETURN gchar *
622 gst_plugin_get_package (GstPlugin * plugin)
624 g_return_val_if_fail (plugin != NULL, NULL);
626 return plugin->desc.package;
630 * gst_plugin_get_origin:
631 * @plugin: plugin to get the origin of
633 * get the URL where the plugin comes from
635 * Returns: the origin of the plugin
637 G_CONST_RETURN gchar *
638 gst_plugin_get_origin (GstPlugin * plugin)
640 g_return_val_if_fail (plugin != NULL, NULL);
642 return plugin->desc.origin;
646 * gst_plugin_get_module:
647 * @plugin: plugin to query
649 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
652 * Returns: module belonging to the plugin or NULL if the plugin isn't
656 gst_plugin_get_module (GstPlugin * plugin)
658 g_return_val_if_fail (plugin != NULL, NULL);
660 return plugin->module;
664 * gst_plugin_is_loaded:
665 * @plugin: plugin to query
667 * queries if the plugin is loaded into memory
669 * Returns: TRUE is loaded, FALSE otherwise
672 gst_plugin_is_loaded (GstPlugin * plugin)
674 g_return_val_if_fail (plugin != NULL, FALSE);
676 return (plugin->module != NULL || plugin->filename == NULL);
681 * gst_plugin_feature_list:
682 * @plugin: plugin to query
683 * @filter: the filter to use
684 * @first: only return first match
685 * @user_data: user data passed to the filter function
687 * Runs a filter against all plugin features and returns a GList with
688 * the results. If the first flag is set, only the first match is
689 * returned (as a list with a single object).
691 * Returns: a GList of features, g_list_free after use.
694 gst_plugin_feature_filter (GstPlugin * plugin,
695 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
700 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
702 for (g = list; g; g = g->next) {
703 gst_object_ref (plugin);
711 GstPluginFeatureFilter filter;
719 _feature_filter (GstPlugin * plugin, gpointer user_data)
722 FeatureFilterData *data = (FeatureFilterData *) user_data;
724 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
727 data->result = g_list_concat (data->result, result);
734 * gst_plugin_list_feature_filter:
735 * @list: a #GList of plugins to query
736 * @filter: the filter function to use
737 * @first: only return first match
738 * @user_data: user data passed to the filter function
740 * Runs a filter against all plugin features of the plugins in the given
741 * list and returns a GList with the results.
742 * If the first flag is set, only the first match is
743 * returned (as a list with a single object).
745 * Returns: a GList of features, g_list_free after use.
748 gst_plugin_list_feature_filter (GList * list,
749 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
751 FeatureFilterData data;
754 data.filter = filter;
756 data.user_data = user_data;
759 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
760 g_list_free (result);
767 * gst_plugin_name_filter:
768 * @plugin: the plugin to check
769 * @name: the name of the plugin
771 * A standard filter that returns TRUE when the plugin is of the
774 * Returns: TRUE if the plugin is of the given name.
777 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
779 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
784 * gst_plugin_find_feature:
785 * @plugin: plugin to get the feature from
786 * @name: The name of the feature to find
787 * @type: The type of the feature to find
789 * Find a feature of the given name and type in the given plugin.
791 * Returns: a GstPluginFeature or NULL if the feature was not found.
794 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
797 GstPluginFeature *result = NULL;
798 GstTypeNameData data;
800 g_return_val_if_fail (name != NULL, NULL);
805 walk = gst_filter_run (plugin->features,
806 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
809 result = GST_PLUGIN_FEATURE (walk->data);
811 gst_object_ref (result);
812 gst_plugin_feature_list_free (walk);
821 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
823 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
829 * gst_plugin_find_feature_by_name:
830 * @plugin: plugin to get the feature from
831 * @name: The name of the feature to find
833 * Find a feature of the given name in the given plugin.
835 * Returns: a GstPluginFeature or NULL if the feature was not found.
838 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
841 GstPluginFeature *result = NULL;
843 g_return_val_if_fail (name != NULL, NULL);
845 walk = gst_filter_run (plugin->features,
846 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
849 result = GST_PLUGIN_FEATURE (walk->data);
851 gst_object_ref (result);
852 gst_plugin_feature_list_free (walk);
860 * gst_plugin_load_by_name:
861 * @name: name of plugin to load
863 * Load the named plugin. Refs the plugin.
865 * Returns: A reference to a loaded plugin, or NULL on error.
868 gst_plugin_load_by_name (const gchar * name)
870 GstPlugin *plugin, *newplugin;
871 GError *error = NULL;
873 GST_DEBUG ("looking up plugin %s in default registry", name);
874 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
876 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
877 newplugin = gst_plugin_load_file (plugin->filename, &error);
878 gst_object_unref (plugin);
881 GST_WARNING ("load_plugin error: %s", error->message);
882 g_error_free (error);
885 /* newplugin was reffed by load_file */
889 GST_DEBUG ("Could not find plugin %s in registry", name);
895 * @plugin: plugin to load
897 * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
898 * untouched. The normal use pattern of this function goes like this:
901 * GstPlugin *loaded_plugin;
902 * loaded_plugin = gst_plugin_load (plugin);
903 * // presumably, we're no longer interested in the potentially-unloaded plugin
904 * gst_object_unref (plugin);
905 * plugin = loaded_plugin;
908 * Returns: A reference to a loaded plugin, or NULL on error.
911 gst_plugin_load (GstPlugin * plugin)
913 GError *error = NULL;
914 GstPlugin *newplugin;
916 if (gst_plugin_is_loaded (plugin)) {
920 if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
927 GST_WARNING ("load_plugin error: %s", error->message);
928 g_error_free (error);
934 * gst_plugin_list_free:
935 * @list: list of #GstPlugin
937 * Unrefs each member of @list, then frees the list.
940 gst_plugin_list_free (GList * list)
944 for (g = list; g; g = g->next) {
945 gst_object_unref (GST_PLUGIN_CAST (g->data));