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>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 plugin_desc.
38 * Once you have a handle to a #GstPlugin (e.g. from the #GstRegistryPool), you
39 * can add any object that subclasses #GstPluginFeature.
41 * Use gst_plugin_find_feature() and gst_plugin_get_feature_list() to find
42 * features in a plugin.
44 * Usually plugins are always automaticlly loaded so you don't need to call
45 * gst_plugin_load() explicitly to bring it into memory. There are options to
46 * statically link plugins to an app or even use GStreamer without a plugin
47 * repository in which case gst_plugin_load() can be needed to bring the plugin
54 #include <sys/types.h>
65 #include "gst_private.h"
67 #include "gstplugin.h"
68 #include "gstversion.h"
70 #include "gstfilter.h"
71 #include "gstregistry.h"
72 #include "gstmacros.h"
75 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
77 static GModule *main_module = NULL;
78 static GList *_gst_plugin_static = NULL;
80 /* static variables for segfault handling of plugin loading */
81 static char *_gst_plugin_fault_handler_filename = NULL;
82 extern gboolean _gst_disable_segtrap; /* see gst.c */
85 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
88 /* list of valid licenses.
89 * One of these must be specified or the plugin won't be loaded
90 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
93 * GPL: http://www.gnu.org/copyleft/gpl.html
94 * LGPL: http://www.gnu.org/copyleft/lesser.html
95 * QPL: http://www.trolltech.com/licenses/qpl.html
96 * MPL: http://www.opensource.org/licenses/mozilla1.1.php
98 static const gchar *valid_licenses[] = {
99 "LGPL", /* GNU Lesser General Public License */
100 "GPL", /* GNU General Public License */
101 "QPL", /* Trolltech Qt Public License */
102 "GPL/QPL", /* Combi-license of GPL + QPL */
103 "MPL", /* MPL 1.1 license */
104 "Proprietary", /* Proprietary license */
105 GST_LICENSE_UNKNOWN, /* some other license */
109 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
110 GModule * module, GstPluginDesc * desc);
112 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src);
113 static void gst_plugin_desc_free (GstPluginDesc * desc);
116 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
119 gst_plugin_init (GstPlugin * plugin)
125 gst_plugin_finalize (GObject * object)
127 GstPlugin *plugin = GST_PLUGIN_CAST (object);
128 GstRegistry *registry = gst_registry_get_default ();
131 GST_DEBUG ("finalizing plugin %p", plugin);
132 for (g = registry->plugins; g; g = g->next) {
133 if (g->data == (gpointer) plugin) {
134 g_warning ("removing plugin that is still in registry");
137 g_free (plugin->filename);
138 g_free (plugin->basename);
139 gst_plugin_desc_free (&plugin->desc);
141 G_OBJECT_CLASS (gst_plugin_parent_class)->finalize (object);
145 gst_plugin_class_init (GstPluginClass * klass)
147 G_OBJECT_CLASS (klass)->finalize = GST_DEBUG_FUNCPTR (gst_plugin_finalize);
151 gst_plugin_error_quark (void)
153 static GQuark quark = 0;
156 quark = g_quark_from_static_string ("gst_plugin_error");
160 /* this function can be called in the GCC constructor extension, before
161 * the _gst_plugin_initialize() was called. In that case, we store the
162 * plugin description in a list to initialize it when we open the main
164 * When the main module is known, we can register the plugin right away.
167 _gst_plugin_register_static (GstPluginDesc * desc)
169 if (main_module == NULL) {
171 GST_LOG ("queueing static plugin \"%s\" for loading later on",
173 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
178 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
179 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
180 if (gst_plugin_register_func (plugin, main_module, desc)) {
182 GST_INFO ("loaded static plugin \"%s\"", desc->name);
183 gst_default_registry_add_plugin (plugin);
189 _gst_plugin_initialize (void)
191 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
193 /* now register all static plugins */
194 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
198 /* this function could be extended to check if the plugin license matches the
199 * applications license (would require the app to register its license somehow).
200 * We'll wait for someone who's interested in it to code it :)
203 gst_plugin_check_license (const gchar * license)
205 const gchar **check_license = valid_licenses;
207 g_assert (check_license);
209 while (*check_license) {
210 if (strcmp (license, *check_license) == 0)
218 gst_plugin_check_version (gint major, gint minor)
220 /* return NULL if the major and minor version numbers are not compatible */
222 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
229 gst_plugin_register_func (GstPlugin * plugin, GModule * module,
230 GstPluginDesc * desc)
232 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
234 GST_WARNING ("plugin \"%s\" has incompatible version, not loading",
239 if (!desc->license || !desc->description || !desc->source ||
240 !desc->package || !desc->origin) {
242 GST_WARNING ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
247 if (!gst_plugin_check_license (desc->license)) {
249 GST_WARNING ("plugin \"%s\" has invalid license \"%s\", not loading",
250 plugin->filename, desc->license);
255 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
257 gst_plugin_desc_copy (&plugin->desc, desc);
259 if (!((desc->plugin_init) (plugin))) {
261 GST_WARNING ("plugin \"%s\" failed to initialise", plugin->filename);
262 plugin->module = NULL;
267 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
273 static struct sigaction oldaction;
276 * _gst_plugin_fault_handler_restore:
277 * segfault handler restorer
280 _gst_plugin_fault_handler_restore (void)
282 if (!_gst_plugin_fault_handler_is_setup)
285 _gst_plugin_fault_handler_is_setup = FALSE;
287 sigaction (SIGSEGV, &oldaction, NULL);
291 * _gst_plugin_fault_handler_sighandler:
292 * segfault handler implementation
295 _gst_plugin_fault_handler_sighandler (int signum)
297 /* We need to restore the fault handler or we'll keep getting it */
298 _gst_plugin_fault_handler_restore ();
302 g_print ("\nERROR: ");
303 g_print ("Caught a segmentation fault while loading plugin file:\n");
304 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
305 g_print ("Please either:\n");
306 g_print ("- remove it and restart.\n");
307 g_print ("- run with --gst-disable-segtrap and debug.\n");
311 g_print ("Caught unhandled signal on plugin loading\n");
317 * _gst_plugin_fault_handler_setup:
318 * sets up the segfault handler
321 _gst_plugin_fault_handler_setup (void)
323 struct sigaction action;
325 /* if asked to leave segfaults alone, just return */
326 if (_gst_disable_segtrap)
329 if (_gst_plugin_fault_handler_is_setup)
332 _gst_plugin_fault_handler_is_setup = TRUE;
334 memset (&action, 0, sizeof (action));
335 action.sa_handler = _gst_plugin_fault_handler_sighandler;
337 sigaction (SIGSEGV, &action, &oldaction);
341 _gst_plugin_fault_handler_restore (void)
346 _gst_plugin_fault_handler_setup (void)
351 static void _gst_plugin_fault_handler_setup ();
353 GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
356 * gst_plugin_load_file:
357 * @filename: the plugin filename to load
358 * @error: pointer to a NULL-valued GError
360 * Loads the given plugin and refs it. Caller needs to unref after use.
362 * Returns: a reference to the existing loaded GstPlugin, a reference to the
363 * newly-loaded GstPlugin, or NULL if an error occurred.
366 gst_plugin_load_file (const gchar * filename, GError ** error)
372 struct stat file_status;
373 GstRegistry *registry;
375 g_return_val_if_fail (filename != NULL, NULL);
377 registry = gst_registry_get_default ();
378 g_static_mutex_lock (&gst_plugin_loading_mutex);
380 plugin = gst_registry_lookup (registry, filename);
382 if (plugin->module) {
383 g_static_mutex_unlock (&gst_plugin_loading_mutex);
386 gst_object_unref (plugin);
391 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
394 if (g_module_supported () == FALSE) {
395 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
398 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
402 if (stat (filename, &file_status)) {
403 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
406 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
411 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
412 if (module == NULL) {
413 GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
416 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
418 /* If we failed to open the shared object, then it's probably because a
419 * plugin is linked against the wrong libraries. Print out an easy-to-see
420 * message in this case. */
421 g_warning ("Failed to load plugin: %s", g_module_error ());
425 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
427 plugin->module = module;
428 plugin->filename = g_strdup (filename);
429 plugin->basename = g_path_get_basename (filename);
430 plugin->file_mtime = file_status.st_mtime;
431 plugin->file_size = file_status.st_size;
433 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
435 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
438 GST_PLUGIN_ERROR_MODULE,
439 "File \"%s\" is not a GStreamer plugin", filename);
440 g_module_close (module);
443 plugin->orig_desc = (GstPluginDesc *) ptr;
445 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
448 /* this is where we load the actual .so, so let's trap SIGSEGV */
449 _gst_plugin_fault_handler_setup ();
450 _gst_plugin_fault_handler_filename = plugin->filename;
452 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
455 if (!gst_plugin_register_func (plugin, module, plugin->orig_desc)) {
456 /* remove signal handler */
457 _gst_plugin_fault_handler_restore ();
458 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
462 GST_PLUGIN_ERROR_MODULE,
463 "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
465 g_module_close (module);
469 /* remove signal handler */
470 _gst_plugin_fault_handler_restore ();
471 _gst_plugin_fault_handler_filename = NULL;
472 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
474 gst_object_ref (plugin);
475 gst_default_registry_add_plugin (plugin);
477 g_static_mutex_unlock (&gst_plugin_loading_mutex);
483 gst_object_unref (plugin);
484 g_static_mutex_unlock (&gst_plugin_loading_mutex);
490 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
492 dest->major_version = src->major_version;
493 dest->minor_version = src->minor_version;
494 dest->name = g_strdup (src->name);
495 dest->description = g_strdup (src->description);
496 dest->plugin_init = src->plugin_init;
497 dest->version = g_strdup (src->version);
498 dest->license = g_strdup (src->license);
499 dest->source = g_strdup (src->source);
500 dest->package = g_strdup (src->package);
501 dest->origin = g_strdup (src->origin);
506 gst_plugin_desc_free (GstPluginDesc * desc)
509 g_free (desc->description);
510 g_free (desc->version);
511 g_free (desc->license);
512 g_free (desc->source);
513 g_free (desc->package);
514 g_free (desc->origin);
516 memset (desc, 0, sizeof (GstPluginDesc));
520 * gst_plugin_get_name:
521 * @plugin: plugin to get the name of
523 * Get the short name of the plugin
525 * Returns: the name of the plugin
528 gst_plugin_get_name (GstPlugin * plugin)
530 g_return_val_if_fail (plugin != NULL, NULL);
532 return plugin->desc.name;
536 * gst_plugin_get_description:
537 * @plugin: plugin to get long name of
539 * Get the long descriptive name of the plugin
541 * Returns: the long name of the plugin
543 G_CONST_RETURN gchar *
544 gst_plugin_get_description (GstPlugin * plugin)
546 g_return_val_if_fail (plugin != NULL, NULL);
548 return plugin->desc.description;
552 * gst_plugin_get_filename:
553 * @plugin: plugin to get the filename of
555 * get the filename of the plugin
557 * Returns: the filename of the plugin
559 G_CONST_RETURN gchar *
560 gst_plugin_get_filename (GstPlugin * plugin)
562 g_return_val_if_fail (plugin != NULL, NULL);
564 return plugin->filename;
568 * gst_plugin_get_version:
569 * @plugin: plugin to get the version of
571 * get the version of the plugin
573 * Returns: the version of the plugin
575 G_CONST_RETURN gchar *
576 gst_plugin_get_version (GstPlugin * plugin)
578 g_return_val_if_fail (plugin != NULL, NULL);
580 return plugin->desc.version;
584 * gst_plugin_get_license:
585 * @plugin: plugin to get the license of
587 * get the license of the plugin
589 * Returns: the license of the plugin
591 G_CONST_RETURN gchar *
592 gst_plugin_get_license (GstPlugin * plugin)
594 g_return_val_if_fail (plugin != NULL, NULL);
596 return plugin->desc.license;
600 * gst_plugin_get_source:
601 * @plugin: plugin to get the source of
603 * get the source module the plugin belongs to.
605 * Returns: the source of the plugin
607 G_CONST_RETURN gchar *
608 gst_plugin_get_source (GstPlugin * plugin)
610 g_return_val_if_fail (plugin != NULL, NULL);
612 return plugin->desc.source;
616 * gst_plugin_get_package:
617 * @plugin: plugin to get the package of
619 * get the package the plugin belongs to.
621 * Returns: the package of the plugin
623 G_CONST_RETURN gchar *
624 gst_plugin_get_package (GstPlugin * plugin)
626 g_return_val_if_fail (plugin != NULL, NULL);
628 return plugin->desc.package;
632 * gst_plugin_get_origin:
633 * @plugin: plugin to get the origin of
635 * get the URL where the plugin comes from
637 * Returns: the origin of the plugin
639 G_CONST_RETURN gchar *
640 gst_plugin_get_origin (GstPlugin * plugin)
642 g_return_val_if_fail (plugin != NULL, NULL);
644 return plugin->desc.origin;
648 * gst_plugin_get_module:
649 * @plugin: plugin to query
651 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
654 * Returns: module belonging to the plugin or NULL if the plugin isn't
658 gst_plugin_get_module (GstPlugin * plugin)
660 g_return_val_if_fail (plugin != NULL, NULL);
662 return plugin->module;
666 * gst_plugin_is_loaded:
667 * @plugin: plugin to query
669 * queries if the plugin is loaded into memory
671 * Returns: TRUE is loaded, FALSE otherwise
674 gst_plugin_is_loaded (GstPlugin * plugin)
676 g_return_val_if_fail (plugin != NULL, FALSE);
678 return (plugin->module != NULL || plugin->filename == NULL);
683 * gst_plugin_feature_list:
684 * @plugin: plugin to query
685 * @filter: the filter to use
686 * @first: only return first match
687 * @user_data: user data passed to the filter function
689 * Runs a filter against all plugin features and returns a GList with
690 * the results. If the first flag is set, only the first match is
691 * returned (as a list with a single object).
693 * Returns: a GList of features, g_list_free after use.
696 gst_plugin_feature_filter (GstPlugin * plugin,
697 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
702 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
704 for (g = list; g; g = g->next) {
705 gst_object_ref (plugin);
713 GstPluginFeatureFilter filter;
721 _feature_filter (GstPlugin * plugin, gpointer user_data)
724 FeatureFilterData *data = (FeatureFilterData *) user_data;
726 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
729 data->result = g_list_concat (data->result, result);
736 * gst_plugin_list_feature_filter:
737 * @list: a #GList of plugins to query
738 * @filter: the filter function to use
739 * @first: only return first match
740 * @user_data: user data passed to the filter function
742 * Runs a filter against all plugin features of the plugins in the given
743 * list and returns a GList with the results.
744 * If the first flag is set, only the first match is
745 * returned (as a list with a single object).
747 * Returns: a GList of features, g_list_free after use.
750 gst_plugin_list_feature_filter (GList * list,
751 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
753 FeatureFilterData data;
756 data.filter = filter;
758 data.user_data = user_data;
761 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
762 g_list_free (result);
769 * gst_plugin_name_filter:
770 * @plugin: the plugin to check
771 * @name: the name of the plugin
773 * A standard filter that returns TRUE when the plugin is of the
776 * Returns: TRUE if the plugin is of the given name.
779 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
781 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
786 * gst_plugin_find_feature:
787 * @plugin: plugin to get the feature from
788 * @name: The name of the feature to find
789 * @type: The type of the feature to find
791 * Find a feature of the given name and type in the given plugin.
793 * Returns: a GstPluginFeature or NULL if the feature was not found.
796 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
799 GstPluginFeature *result = NULL;
800 GstTypeNameData data;
802 g_return_val_if_fail (name != NULL, NULL);
807 walk = gst_filter_run (plugin->features,
808 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
811 result = GST_PLUGIN_FEATURE (walk->data);
813 gst_object_ref (result);
814 gst_plugin_feature_list_free (walk);
823 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
825 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
831 * gst_plugin_find_feature_by_name:
832 * @plugin: plugin to get the feature from
833 * @name: The name of the feature to find
835 * Find a feature of the given name in the given plugin.
837 * Returns: a GstPluginFeature or NULL if the feature was not found.
840 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
843 GstPluginFeature *result = NULL;
845 g_return_val_if_fail (name != NULL, NULL);
847 walk = gst_filter_run (plugin->features,
848 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
851 result = GST_PLUGIN_FEATURE (walk->data);
853 gst_object_ref (result);
854 gst_plugin_feature_list_free (walk);
862 * gst_plugin_load_by_name:
863 * @name: name of plugin to load
865 * Load the named plugin. Refs the plugin.
867 * Returns: A reference to a loaded plugin, or NULL on error.
870 gst_plugin_load_by_name (const gchar * name)
872 GstPlugin *plugin, *newplugin;
873 GError *error = NULL;
875 GST_DEBUG ("looking up plugin %s in default registry", name);
876 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
878 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
879 newplugin = gst_plugin_load_file (plugin->filename, &error);
880 gst_object_unref (plugin);
883 GST_WARNING ("load_plugin error: %s", error->message);
884 g_error_free (error);
887 /* newplugin was reffed by load_file */
891 GST_DEBUG ("Could not find plugin %s in registry", name);
897 * @plugin: plugin to load
899 * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
900 * untouched. The normal use pattern of this function goes like this:
903 * GstPlugin *loaded_plugin;
904 * loaded_plugin = gst_plugin_load (plugin);
905 * // presumably, we're no longer interested in the potentially-unloaded plugin
906 * gst_object_unref (plugin);
907 * plugin = loaded_plugin;
910 * Returns: A reference to a loaded plugin, or NULL on error.
913 gst_plugin_load (GstPlugin * plugin)
915 GError *error = NULL;
916 GstPlugin *newplugin;
918 if (gst_plugin_is_loaded (plugin)) {
922 if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
929 GST_WARNING ("load_plugin error: %s", error->message);
930 g_error_free (error);
936 * gst_plugin_list_free:
937 * @list: list of #GstPlugin
939 * Unrefs each member of @list, then frees the list.
942 gst_plugin_list_free (GList * list)
946 for (g = list; g; g = g->next) {
947 gst_object_unref (GST_PLUGIN_CAST (g->data));