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"
67 #include "glib-compat-private.h"
71 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
73 static GList *_gst_plugin_static = NULL;
74 static gboolean _gst_plugin_inited;
76 /* static variables for segfault handling of plugin loading */
77 static char *_gst_plugin_fault_handler_filename = NULL;
80 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
83 /* list of valid licenses.
84 * One of these must be specified or the plugin won't be loaded
85 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
88 * GPL: http://www.gnu.org/copyleft/gpl.html
89 * LGPL: http://www.gnu.org/copyleft/lesser.html
90 * QPL: http://www.trolltech.com/licenses/qpl.html
91 * MPL: http://www.opensource.org/licenses/mozilla1.1.php
92 * MIT/X11: http://www.opensource.org/licenses/mit-license.php
93 * 3-clause BSD: http://www.opensource.org/licenses/bsd-license.php
95 static const gchar *valid_licenses[] = {
96 "LGPL", /* GNU Lesser General Public License */
97 "GPL", /* GNU General Public License */
98 "QPL", /* Trolltech Qt Public License */
99 "GPL/QPL", /* Combi-license of GPL + QPL */
100 "MPL", /* MPL 1.1 license */
101 "BSD", /* 3-clause BSD license */
102 "MIT/X11", /* MIT/X11 license */
103 "Proprietary", /* Proprietary license */
104 GST_LICENSE_UNKNOWN, /* some other license */
108 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
109 GstPluginDesc * desc);
110 static void gst_plugin_desc_copy (GstPluginDesc * dest,
111 const GstPluginDesc * src);
112 static void gst_plugin_desc_free (GstPluginDesc * desc);
115 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
118 gst_plugin_init (GstPlugin * plugin)
120 /* do nothing, needed because of G_DEFINE_TYPE */
124 gst_plugin_finalize (GObject * object)
126 GstPlugin *plugin = GST_PLUGIN_CAST (object);
127 GstRegistry *registry = gst_registry_get_default ();
130 GST_DEBUG ("finalizing plugin %p", plugin);
131 for (g = registry->plugins; g; g = g->next) {
132 if (g->data == (gpointer) plugin) {
133 g_warning ("removing plugin that is still in registry");
136 g_free (plugin->filename);
137 g_free (plugin->basename);
138 gst_plugin_desc_free (&plugin->desc);
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);
150 gst_plugin_error_quark (void)
152 static GQuark quark = 0;
155 quark = g_quark_from_static_string ("gst_plugin_error");
159 /* this function can be called in the GCC constructor extension, before
160 * the _gst_plugin_initialize() was called. In that case, we store the
161 * plugin description in a list to initialize it when we open the main
163 * When the main module is known, we can register the plugin right away.
166 _gst_plugin_register_static (GstPluginDesc * desc)
168 if (!_gst_plugin_inited) {
170 GST_LOG ("queueing static plugin \"%s\" for loading later on",
172 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
177 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
178 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
179 if (gst_plugin_register_func (plugin, desc)) {
181 GST_INFO ("loaded static plugin \"%s\"", desc->name);
182 gst_default_registry_add_plugin (plugin);
188 _gst_plugin_initialize (void)
190 _gst_plugin_inited = TRUE;
192 /* now register all static plugins */
193 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
197 /* this function could be extended to check if the plugin license matches the
198 * applications license (would require the app to register its license somehow).
199 * We'll wait for someone who's interested in it to code it :)
202 gst_plugin_check_license (const gchar * license)
204 const gchar **check_license = valid_licenses;
206 g_assert (check_license);
208 while (*check_license) {
209 if (strcmp (license, *check_license) == 0)
217 gst_plugin_check_version (gint major, gint minor)
219 /* return NULL if the major and minor version numbers are not compatible */
221 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
228 gst_plugin_register_func (GstPlugin * plugin, GstPluginDesc * desc)
230 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
232 GST_WARNING ("plugin \"%s\" has incompatible version, not loading",
237 if (!desc->license || !desc->description || !desc->source ||
238 !desc->package || !desc->origin) {
240 GST_WARNING ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
245 if (!gst_plugin_check_license (desc->license)) {
247 GST_WARNING ("plugin \"%s\" has invalid license \"%s\", not loading",
248 plugin->filename, desc->license);
253 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
255 gst_plugin_desc_copy (&plugin->desc, desc);
257 if (!((desc->plugin_init) (plugin))) {
259 GST_WARNING ("plugin \"%s\" failed to initialise", plugin->filename);
260 plugin->module = NULL;
265 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
270 #ifdef HAVE_SIGACTION
271 static struct sigaction oldaction;
274 * _gst_plugin_fault_handler_restore:
275 * segfault handler restorer
278 _gst_plugin_fault_handler_restore (void)
280 if (!_gst_plugin_fault_handler_is_setup)
283 _gst_plugin_fault_handler_is_setup = FALSE;
285 sigaction (SIGSEGV, &oldaction, NULL);
289 * _gst_plugin_fault_handler_sighandler:
290 * segfault handler implementation
293 _gst_plugin_fault_handler_sighandler (int signum)
295 /* We need to restore the fault handler or we'll keep getting it */
296 _gst_plugin_fault_handler_restore ();
300 g_print ("\nERROR: ");
301 g_print ("Caught a segmentation fault while loading plugin file:\n");
302 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
303 g_print ("Please either:\n");
304 g_print ("- remove it and restart.\n");
305 g_print ("- run with --gst-disable-segtrap and debug.\n");
309 g_print ("Caught unhandled signal on plugin loading\n");
315 * _gst_plugin_fault_handler_setup:
316 * sets up the segfault handler
319 _gst_plugin_fault_handler_setup (void)
321 struct sigaction action;
323 /* if asked to leave segfaults alone, just return */
324 if (!gst_segtrap_is_enabled ())
327 if (_gst_plugin_fault_handler_is_setup)
330 _gst_plugin_fault_handler_is_setup = TRUE;
332 memset (&action, 0, sizeof (action));
333 action.sa_handler = _gst_plugin_fault_handler_sighandler;
335 sigaction (SIGSEGV, &action, &oldaction);
339 _gst_plugin_fault_handler_restore (void)
344 _gst_plugin_fault_handler_setup (void)
349 static void _gst_plugin_fault_handler_setup ();
351 static GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
354 * gst_plugin_load_file:
355 * @filename: the plugin filename to load
356 * @error: pointer to a NULL-valued GError
358 * Loads the given plugin and refs it. Caller needs to unref after use.
360 * Returns: a reference to the existing loaded GstPlugin, a reference to the
361 * newly-loaded GstPlugin, or NULL if an error occurred.
364 gst_plugin_load_file (const gchar * filename, GError ** error)
370 struct stat file_status;
371 GstRegistry *registry;
373 g_return_val_if_fail (filename != NULL, NULL);
375 registry = gst_registry_get_default ();
376 g_static_mutex_lock (&gst_plugin_loading_mutex);
378 plugin = gst_registry_lookup (registry, filename);
380 if (plugin->module) {
381 g_static_mutex_unlock (&gst_plugin_loading_mutex);
384 gst_object_unref (plugin);
389 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
392 if (g_module_supported () == FALSE) {
393 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
396 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
400 if (g_stat (filename, &file_status)) {
401 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
404 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
409 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
410 if (module == NULL) {
411 GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
414 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
416 /* If we failed to open the shared object, then it's probably because a
417 * plugin is linked against the wrong libraries. Print out an easy-to-see
418 * message in this case. */
419 g_warning ("Failed to load plugin '%s': %s", filename, g_module_error ());
423 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
425 plugin->module = module;
426 plugin->filename = g_strdup (filename);
427 plugin->basename = g_path_get_basename (filename);
428 plugin->file_mtime = file_status.st_mtime;
429 plugin->file_size = file_status.st_size;
431 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
433 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
436 GST_PLUGIN_ERROR_MODULE,
437 "File \"%s\" is not a GStreamer plugin", filename);
438 g_module_close (module);
441 plugin->orig_desc = (GstPluginDesc *) ptr;
443 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
446 /* this is where we load the actual .so, so let's trap SIGSEGV */
447 _gst_plugin_fault_handler_setup ();
448 _gst_plugin_fault_handler_filename = plugin->filename;
450 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
453 if (!gst_plugin_register_func (plugin, plugin->orig_desc)) {
454 /* remove signal handler */
455 _gst_plugin_fault_handler_restore ();
456 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
460 GST_PLUGIN_ERROR_MODULE,
461 "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
463 g_module_close (module);
467 /* remove signal handler */
468 _gst_plugin_fault_handler_restore ();
469 _gst_plugin_fault_handler_filename = NULL;
470 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
472 gst_object_ref (plugin);
473 gst_default_registry_add_plugin (plugin);
475 g_static_mutex_unlock (&gst_plugin_loading_mutex);
481 gst_object_unref (plugin);
482 g_static_mutex_unlock (&gst_plugin_loading_mutex);
488 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
490 dest->major_version = src->major_version;
491 dest->minor_version = src->minor_version;
492 dest->name = g_intern_string (src->name);
493 /* maybe intern the description too, just for convenience? */
494 dest->description = g_strdup (src->description);
495 dest->plugin_init = src->plugin_init;
496 dest->version = g_intern_string (src->version);
497 dest->license = g_intern_string (src->license);
498 dest->source = g_intern_string (src->source);
499 dest->package = g_intern_string (src->package);
500 dest->origin = g_intern_string (src->origin);
505 gst_plugin_desc_free (GstPluginDesc * desc)
507 g_free (desc->description);
508 memset (desc, 0, sizeof (GstPluginDesc));
512 * gst_plugin_get_name:
513 * @plugin: plugin to get the name of
515 * Get the short name of the plugin
517 * Returns: the name of the plugin
520 gst_plugin_get_name (GstPlugin * plugin)
522 g_return_val_if_fail (plugin != NULL, NULL);
524 return plugin->desc.name;
528 * gst_plugin_get_description:
529 * @plugin: plugin to get long name of
531 * Get the long descriptive name of the plugin
533 * Returns: the long name of the plugin
535 G_CONST_RETURN gchar *
536 gst_plugin_get_description (GstPlugin * plugin)
538 g_return_val_if_fail (plugin != NULL, NULL);
540 return plugin->desc.description;
544 * gst_plugin_get_filename:
545 * @plugin: plugin to get the filename of
547 * get the filename of the plugin
549 * Returns: the filename of the plugin
551 G_CONST_RETURN gchar *
552 gst_plugin_get_filename (GstPlugin * plugin)
554 g_return_val_if_fail (plugin != NULL, NULL);
556 return plugin->filename;
560 * gst_plugin_get_version:
561 * @plugin: plugin to get the version of
563 * get the version of the plugin
565 * Returns: the version of the plugin
567 G_CONST_RETURN gchar *
568 gst_plugin_get_version (GstPlugin * plugin)
570 g_return_val_if_fail (plugin != NULL, NULL);
572 return plugin->desc.version;
576 * gst_plugin_get_license:
577 * @plugin: plugin to get the license of
579 * get the license of the plugin
581 * Returns: the license of the plugin
583 G_CONST_RETURN gchar *
584 gst_plugin_get_license (GstPlugin * plugin)
586 g_return_val_if_fail (plugin != NULL, NULL);
588 return plugin->desc.license;
592 * gst_plugin_get_source:
593 * @plugin: plugin to get the source of
595 * get the source module the plugin belongs to.
597 * Returns: the source of the plugin
599 G_CONST_RETURN gchar *
600 gst_plugin_get_source (GstPlugin * plugin)
602 g_return_val_if_fail (plugin != NULL, NULL);
604 return plugin->desc.source;
608 * gst_plugin_get_package:
609 * @plugin: plugin to get the package of
611 * get the package the plugin belongs to.
613 * Returns: the package of the plugin
615 G_CONST_RETURN gchar *
616 gst_plugin_get_package (GstPlugin * plugin)
618 g_return_val_if_fail (plugin != NULL, NULL);
620 return plugin->desc.package;
624 * gst_plugin_get_origin:
625 * @plugin: plugin to get the origin of
627 * get the URL where the plugin comes from
629 * Returns: the origin of the plugin
631 G_CONST_RETURN gchar *
632 gst_plugin_get_origin (GstPlugin * plugin)
634 g_return_val_if_fail (plugin != NULL, NULL);
636 return plugin->desc.origin;
640 * gst_plugin_get_module:
641 * @plugin: plugin to query
643 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
646 * Returns: module belonging to the plugin or NULL if the plugin isn't
650 gst_plugin_get_module (GstPlugin * plugin)
652 g_return_val_if_fail (plugin != NULL, NULL);
654 return plugin->module;
658 * gst_plugin_is_loaded:
659 * @plugin: plugin to query
661 * queries if the plugin is loaded into memory
663 * Returns: TRUE is loaded, FALSE otherwise
666 gst_plugin_is_loaded (GstPlugin * plugin)
668 g_return_val_if_fail (plugin != NULL, FALSE);
670 return (plugin->module != NULL || plugin->filename == NULL);
675 * gst_plugin_feature_list:
676 * @plugin: plugin to query
677 * @filter: the filter to use
678 * @first: only return first match
679 * @user_data: user data passed to the filter function
681 * Runs a filter against all plugin features and returns a GList with
682 * the results. If the first flag is set, only the first match is
683 * returned (as a list with a single object).
685 * Returns: a GList of features, g_list_free after use.
688 gst_plugin_feature_filter (GstPlugin * plugin,
689 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
694 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
696 for (g = list; g; g = g->next) {
697 gst_object_ref (plugin);
705 GstPluginFeatureFilter filter;
713 _feature_filter (GstPlugin * plugin, gpointer user_data)
716 FeatureFilterData *data = (FeatureFilterData *) user_data;
718 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
721 data->result = g_list_concat (data->result, result);
728 * gst_plugin_list_feature_filter:
729 * @list: a #GList of plugins to query
730 * @filter: the filter function to use
731 * @first: only return first match
732 * @user_data: user data passed to the filter function
734 * Runs a filter against all plugin features of the plugins in the given
735 * list and returns a GList with the results.
736 * If the first flag is set, only the first match is
737 * returned (as a list with a single object).
739 * Returns: a GList of features, g_list_free after use.
742 gst_plugin_list_feature_filter (GList * list,
743 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
745 FeatureFilterData data;
748 data.filter = filter;
750 data.user_data = user_data;
753 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
754 g_list_free (result);
761 * gst_plugin_name_filter:
762 * @plugin: the plugin to check
763 * @name: the name of the plugin
765 * A standard filter that returns TRUE when the plugin is of the
768 * Returns: TRUE if the plugin is of the given name.
771 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
773 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
778 * gst_plugin_find_feature:
779 * @plugin: plugin to get the feature from
780 * @name: The name of the feature to find
781 * @type: The type of the feature to find
783 * Find a feature of the given name and type in the given plugin.
785 * Returns: a GstPluginFeature or NULL if the feature was not found.
788 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
791 GstPluginFeature *result = NULL;
792 GstTypeNameData data;
794 g_return_val_if_fail (name != NULL, NULL);
799 walk = gst_filter_run (plugin->features,
800 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
803 result = GST_PLUGIN_FEATURE (walk->data);
805 gst_object_ref (result);
806 gst_plugin_feature_list_free (walk);
815 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
817 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
823 * gst_plugin_find_feature_by_name:
824 * @plugin: plugin to get the feature from
825 * @name: The name of the feature to find
827 * Find a feature of the given name in the given plugin.
829 * Returns: a GstPluginFeature or NULL if the feature was not found.
832 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
835 GstPluginFeature *result = NULL;
837 g_return_val_if_fail (name != NULL, NULL);
839 walk = gst_filter_run (plugin->features,
840 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
843 result = GST_PLUGIN_FEATURE (walk->data);
845 gst_object_ref (result);
846 gst_plugin_feature_list_free (walk);
854 * gst_plugin_load_by_name:
855 * @name: name of plugin to load
857 * Load the named plugin. Refs the plugin.
859 * Returns: A reference to a loaded plugin, or NULL on error.
862 gst_plugin_load_by_name (const gchar * name)
864 GstPlugin *plugin, *newplugin;
865 GError *error = NULL;
867 GST_DEBUG ("looking up plugin %s in default registry", name);
868 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
870 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
871 newplugin = gst_plugin_load_file (plugin->filename, &error);
872 gst_object_unref (plugin);
875 GST_WARNING ("load_plugin error: %s", error->message);
876 g_error_free (error);
879 /* newplugin was reffed by load_file */
883 GST_DEBUG ("Could not find plugin %s in registry", name);
889 * @plugin: plugin to load
891 * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
892 * untouched. The normal use pattern of this function goes like this:
895 * GstPlugin *loaded_plugin;
896 * loaded_plugin = gst_plugin_load (plugin);
897 * // presumably, we're no longer interested in the potentially-unloaded plugin
898 * gst_object_unref (plugin);
899 * plugin = loaded_plugin;
902 * Returns: A reference to a loaded plugin, or NULL on error.
905 gst_plugin_load (GstPlugin * plugin)
907 GError *error = NULL;
908 GstPlugin *newplugin;
910 if (gst_plugin_is_loaded (plugin)) {
914 if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
921 GST_WARNING ("load_plugin error: %s", error->message);
922 g_error_free (error);
928 * gst_plugin_list_free:
929 * @list: list of #GstPlugin
931 * Unrefs each member of @list, then frees the list.
934 gst_plugin_list_free (GList * list)
938 for (g = list; g; g = g->next) {
939 gst_object_unref (GST_PLUGIN_CAST (g->data));