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.
24 * @short_description: Container for features loaded from a shared object module
25 * @see_also: #GstPluginFeature, #GstElementFactory
27 * GStreamer is extensible, so #GstElement instances can be loaded at runtime.
28 * A plugin system can provide one or more of the basic <application>GStreamer</application>
29 * #GstPluginFeature subclasses.
31 * A plugin should export a symbol <symbol>plugin_desc</symbol> that is a struct of type #GstPluginDesc.
32 * the plugin loader will check the version of the core library the plugin was linked against
33 * and will create a new #GstPlugin. It will then call the #GstPluginInitFunc function
34 * that was provided in the plugin_desc.
36 * Once you have a handle to a #GstPlugin (e.g. from the #GstRegistryPool), you can
37 * add any object that subclasses #GstPluginFeature.
39 * Use gst_plugin_find_feature() and gst_plugin_get_feature_list() to find features in a plugin.
41 * Usually plugins are always automaticlly loaded so you don't need to call gst_plugin_load() explicitly
42 * to bring it into memory. There are options to statically link plugins to an app or even
43 * use GStreamer without a plugin repository in which case gst_plugin_load() can be needed
44 * to bring the plugin into memory.
50 #include <sys/types.h>
61 #include "gst_private.h"
63 #include "gstplugin.h"
64 #include "gstversion.h"
66 #include "gstfilter.h"
67 #include "gstregistry.h"
68 #include "gstmacros.h"
71 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
73 static GModule *main_module = NULL;
74 static GList *_gst_plugin_static = NULL;
76 /* static variables for segfault handling of plugin loading */
77 static char *_gst_plugin_fault_handler_filename = NULL;
78 extern gboolean _gst_disable_segtrap; /* see gst.c */
81 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
84 /* list of valid licenses.
85 * One of these must be specified or the plugin won't be loaded
86 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
89 * GPL: http://www.gnu.org/copyleft/gpl.html
90 * LGPL: http://www.gnu.org/copyleft/lesser.html
91 * QPL: http://www.trolltech.com/licenses/qpl.html
93 static gchar *valid_licenses[] = {
94 "LGPL", /* GNU Lesser General Public License */
95 "GPL", /* GNU General Public License */
96 "QPL", /* Trolltech Qt Public License */
97 "GPL/QPL", /* Combi-license of GPL + QPL */
98 GST_LICENSE_UNKNOWN, /* some other license */
102 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
103 GModule * module, GstPluginDesc * desc);
105 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src);
106 static void gst_plugin_desc_free (GstPluginDesc * desc);
109 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
111 static GstObjectClass *parent_class = NULL;
114 gst_plugin_init (GstPlugin * plugin)
120 gst_plugin_finalize (GObject * object)
122 GstPlugin *plugin = GST_PLUGIN (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_OBJECT_CLASS (parent_class)->finalize (object);
140 gst_plugin_class_init (GstPluginClass * klass)
142 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
144 G_OBJECT_CLASS (klass)->finalize = GST_DEBUG_FUNCPTR (gst_plugin_finalize);
148 gst_plugin_error_quark (void)
150 static GQuark quark = 0;
153 quark = g_quark_from_static_string ("gst_plugin_error");
157 /* this function can be called in the GCC constructor extension, before
158 * the _gst_plugin_initialize() was called. In that case, we store the
159 * plugin description in a list to initialize it when we open the main
161 * When the main module is known, we can register the plugin right away.
164 _gst_plugin_register_static (GstPluginDesc * desc)
166 if (main_module == NULL) {
168 GST_LOG ("queueing static plugin \"%s\" for loading later on",
170 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
175 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
176 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
177 if (gst_plugin_register_func (plugin, main_module, desc)) {
179 GST_INFO ("loaded static plugin \"%s\"", desc->name);
180 gst_default_registry_add_plugin (plugin);
186 _gst_plugin_initialize (void)
188 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
190 /* now register all static plugins */
191 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
195 /* this function could be extended to check if the plugin license matches the
196 * applications license (would require the app to register its license somehow).
197 * We'll wait for someone who's interested in it to code it :)
200 gst_plugin_check_license (const gchar * license)
202 gchar **check_license = valid_licenses;
204 g_assert (check_license);
206 while (*check_license) {
207 if (strcmp (license, *check_license) == 0)
215 gst_plugin_check_version (gint major, gint minor)
217 /* return NULL if the major and minor version numbers are not compatible */
219 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
226 gst_plugin_register_func (GstPlugin * plugin, GModule * module,
227 GstPluginDesc * desc)
229 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
231 GST_INFO ("plugin \"%s\" has incompatible version, not loading",
236 if (!desc->license || !desc->description || !desc->source ||
237 !desc->package || !desc->origin) {
239 GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
244 if (!gst_plugin_check_license (desc->license)) {
246 GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
247 plugin->filename, desc->license);
252 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
254 gst_plugin_desc_copy (&plugin->desc, desc);
256 if (!((desc->plugin_init) (plugin))) {
258 GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
259 plugin->module = NULL;
264 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
271 * _gst_plugin_fault_handler_restore:
272 * segfault handler restorer
275 _gst_plugin_fault_handler_restore (void)
277 struct sigaction action;
279 memset (&action, 0, sizeof (action));
280 action.sa_handler = SIG_DFL;
282 sigaction (SIGSEGV, &action, NULL);
286 * _gst_plugin_fault_handler_sighandler:
287 * segfault handler implementation
290 _gst_plugin_fault_handler_sighandler (int signum)
292 /* We need to restore the fault handler or we'll keep getting it */
293 _gst_plugin_fault_handler_restore ();
297 g_print ("\nERROR: ");
298 g_print ("Caught a segmentation fault while loading plugin file:\n");
299 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
300 g_print ("Please either:\n");
301 g_print ("- remove it and restart.\n");
302 g_print ("- run with --gst-disable-segtrap and debug.\n");
306 g_print ("Caught unhandled signal on plugin loading\n");
312 * _gst_plugin_fault_handler_setup:
313 * sets up the segfault handler
316 _gst_plugin_fault_handler_setup (void)
318 struct sigaction action;
320 /* if asked to leave segfaults alone, just return */
321 if (_gst_disable_segtrap)
324 if (_gst_plugin_fault_handler_is_setup)
327 memset (&action, 0, sizeof (action));
328 action.sa_handler = _gst_plugin_fault_handler_sighandler;
330 sigaction (SIGSEGV, &action, NULL);
334 _gst_plugin_fault_handler_restore (void)
339 _gst_plugin_fault_handler_setup (void)
344 static void _gst_plugin_fault_handler_setup ();
346 GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
349 * gst_plugin_load_file:
350 * @filename: the plugin filename to load
351 * @error: pointer to a NULL-valued GError
353 * Loads the given plugin and refs it. Caller needs to unref after use.
355 * Returns: a reference to the existing loaded GstPlugin, a reference to the
356 * newly-loaded GstPlugin, or NULL if an error occurred.
359 gst_plugin_load_file (const gchar * filename, GError ** error)
365 struct stat file_status;
366 GstRegistry *registry;
368 g_return_val_if_fail (filename != NULL, NULL);
370 registry = gst_registry_get_default ();
371 g_static_mutex_lock (&gst_plugin_loading_mutex);
373 plugin = gst_registry_lookup (registry, filename);
375 if (plugin->module) {
376 g_static_mutex_unlock (&gst_plugin_loading_mutex);
379 gst_object_unref (plugin);
384 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
387 if (g_module_supported () == FALSE) {
388 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
391 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
395 if (stat (filename, &file_status)) {
396 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
399 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s\n", filename,
404 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
405 if (module == NULL) {
406 GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
409 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed");
413 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
415 plugin->module = module;
416 plugin->filename = strdup (filename);
417 plugin->basename = g_path_get_basename (filename);
418 plugin->file_mtime = file_status.st_mtime;
419 plugin->file_size = file_status.st_size;
421 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
423 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
426 GST_PLUGIN_ERROR_MODULE,
427 "File \"%s\" is not a GStreamer plugin", filename);
430 plugin->orig_desc = (GstPluginDesc *) ptr;
432 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
435 /* this is where we load the actual .so, so let's trap SIGSEGV */
436 _gst_plugin_fault_handler_setup ();
437 _gst_plugin_fault_handler_filename = plugin->filename;
439 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
442 if (!gst_plugin_register_func (plugin, module, plugin->orig_desc)) {
443 /* remove signal handler */
444 _gst_plugin_fault_handler_restore ();
445 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
449 GST_PLUGIN_ERROR_MODULE,
450 "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
452 g_module_close (module);
456 /* remove signal handler */
457 _gst_plugin_fault_handler_restore ();
458 _gst_plugin_fault_handler_filename = NULL;
459 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
461 gst_object_ref (plugin);
462 gst_default_registry_add_plugin (plugin);
464 g_static_mutex_unlock (&gst_plugin_loading_mutex);
468 gst_object_unref (plugin);
469 g_static_mutex_unlock (&gst_plugin_loading_mutex);
474 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
476 dest->major_version = src->major_version;
477 dest->minor_version = src->minor_version;
478 dest->name = g_strdup (src->name);
479 dest->description = g_strdup (src->description);
480 dest->plugin_init = src->plugin_init;
481 dest->version = g_strdup (src->version);
482 dest->license = g_strdup (src->license);
483 dest->source = g_strdup (src->source);
484 dest->package = g_strdup (src->package);
485 dest->origin = g_strdup (src->origin);
490 gst_plugin_desc_free (GstPluginDesc * desc)
493 g_free (desc->description);
494 g_free (desc->version);
495 g_free (desc->license);
496 g_free (desc->source);
497 g_free (desc->package);
498 g_free (desc->origin);
500 memset (desc, 0, sizeof (GstPluginDesc));
504 * gst_plugin_get_name:
505 * @plugin: plugin to get the name of
507 * Get the short name of the plugin
509 * Returns: the name of the plugin
512 gst_plugin_get_name (GstPlugin * plugin)
514 g_return_val_if_fail (plugin != NULL, NULL);
516 return plugin->desc.name;
520 * gst_plugin_get_description:
521 * @plugin: plugin to get long name of
523 * Get the long descriptive name of the plugin
525 * Returns: the long name of the plugin
527 G_CONST_RETURN gchar *
528 gst_plugin_get_description (GstPlugin * plugin)
530 g_return_val_if_fail (plugin != NULL, NULL);
532 return plugin->desc.description;
536 * gst_plugin_get_filename:
537 * @plugin: plugin to get the filename of
539 * get the filename of the plugin
541 * Returns: the filename of the plugin
543 G_CONST_RETURN gchar *
544 gst_plugin_get_filename (GstPlugin * plugin)
546 g_return_val_if_fail (plugin != NULL, NULL);
548 return plugin->filename;
552 * gst_plugin_get_version:
553 * @plugin: plugin to get the version of
555 * get the version of the plugin
557 * Returns: the version of the plugin
559 G_CONST_RETURN gchar *
560 gst_plugin_get_version (GstPlugin * plugin)
562 g_return_val_if_fail (plugin != NULL, NULL);
564 return plugin->desc.version;
568 * gst_plugin_get_license:
569 * @plugin: plugin to get the license of
571 * get the license of the plugin
573 * Returns: the license of the plugin
575 G_CONST_RETURN gchar *
576 gst_plugin_get_license (GstPlugin * plugin)
578 g_return_val_if_fail (plugin != NULL, NULL);
580 return plugin->desc.license;
584 * gst_plugin_get_source:
585 * @plugin: plugin to get the source of
587 * get the source module the plugin belongs to.
589 * Returns: the source of the plugin
591 G_CONST_RETURN gchar *
592 gst_plugin_get_source (GstPlugin * plugin)
594 g_return_val_if_fail (plugin != NULL, NULL);
596 return plugin->desc.source;
600 * gst_plugin_get_package:
601 * @plugin: plugin to get the package of
603 * get the package the plugin belongs to.
605 * Returns: the package of the plugin
607 G_CONST_RETURN gchar *
608 gst_plugin_get_package (GstPlugin * plugin)
610 g_return_val_if_fail (plugin != NULL, NULL);
612 return plugin->desc.package;
616 * gst_plugin_get_origin:
617 * @plugin: plugin to get the origin of
619 * get the URL where the plugin comes from
621 * Returns: the origin of the plugin
623 G_CONST_RETURN gchar *
624 gst_plugin_get_origin (GstPlugin * plugin)
626 g_return_val_if_fail (plugin != NULL, NULL);
628 return plugin->desc.origin;
632 * gst_plugin_get_module:
633 * @plugin: plugin to query
635 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
638 * Returns: module belonging to the plugin or NULL if the plugin isn't
642 gst_plugin_get_module (GstPlugin * plugin)
644 g_return_val_if_fail (plugin != NULL, NULL);
646 return plugin->module;
650 * gst_plugin_is_loaded:
651 * @plugin: plugin to query
653 * queries if the plugin is loaded into memory
655 * Returns: TRUE is loaded, FALSE otherwise
658 gst_plugin_is_loaded (GstPlugin * plugin)
660 g_return_val_if_fail (plugin != NULL, FALSE);
662 return (plugin->module != NULL || plugin->filename == NULL);
667 * gst_plugin_feature_list:
668 * @plugin: plugin to query
669 * @filter: the filter to use
670 * @first: only return first match
671 * @user_data: user data passed to the filter function
673 * Runs a filter against all plugin features and returns a GList with
674 * the results. If the first flag is set, only the first match is
675 * returned (as a list with a single object).
677 * Returns: a GList of features, g_list_free after use.
680 gst_plugin_feature_filter (GstPlugin * plugin,
681 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
686 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
688 for (g = list; g; g = g->next) {
689 gst_object_ref (plugin);
697 GstPluginFeatureFilter filter;
705 _feature_filter (GstPlugin * plugin, gpointer user_data)
708 FeatureFilterData *data = (FeatureFilterData *) user_data;
710 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
713 data->result = g_list_concat (data->result, result);
720 * gst_plugin_list_feature_filter:
721 * @list: a #GList of plugins to query
722 * @filter: the filter function to use
723 * @first: only return first match
724 * @user_data: user data passed to the filter function
726 * Runs a filter against all plugin features of the plugins in the given
727 * list and returns a GList with the results.
728 * If the first flag is set, only the first match is
729 * returned (as a list with a single object).
731 * Returns: a GList of features, g_list_free after use.
734 gst_plugin_list_feature_filter (GList * list,
735 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
737 FeatureFilterData data;
740 data.filter = filter;
742 data.user_data = user_data;
745 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
746 g_list_free (result);
753 * gst_plugin_name_filter:
754 * @plugin: the plugin to check
755 * @name: the name of the plugin
757 * A standard filter that returns TRUE when the plugin is of the
760 * Returns: TRUE if the plugin is of the given name.
763 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
765 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
770 * gst_plugin_find_feature:
771 * @plugin: plugin to get the feature from
772 * @name: The name of the feature to find
773 * @type: The type of the feature to find
775 * Find a feature of the given name and type in the given plugin.
777 * Returns: a GstPluginFeature or NULL if the feature was not found.
780 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
783 GstPluginFeature *result = NULL;
784 GstTypeNameData data;
786 g_return_val_if_fail (name != NULL, NULL);
791 walk = gst_filter_run (plugin->features,
792 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
795 result = GST_PLUGIN_FEATURE (walk->data);
797 gst_object_ref (result);
798 gst_plugin_feature_list_free (walk);
807 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
809 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
815 * gst_plugin_find_feature_by_name:
816 * @plugin: plugin to get the feature from
817 * @name: The name of the feature to find
819 * Find a feature of the given name in the given plugin.
821 * Returns: a GstPluginFeature or NULL if the feature was not found.
824 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
827 GstPluginFeature *result = NULL;
829 g_return_val_if_fail (name != NULL, NULL);
831 walk = gst_filter_run (plugin->features,
832 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
835 result = GST_PLUGIN_FEATURE (walk->data);
837 gst_object_ref (result);
838 gst_plugin_feature_list_free (walk);
846 * gst_plugin_load_by_name:
847 * @name: name of plugin to load
849 * Load the named plugin. Refs the plugin.
851 * Returns: A reference to a loaded plugin, or NULL on error.
854 gst_plugin_load_by_name (const gchar * name)
856 GstPlugin *plugin, *newplugin;
857 GError *error = NULL;
859 GST_DEBUG ("looking up plugin %s in default registry", name);
860 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
862 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
863 newplugin = gst_plugin_load_file (plugin->filename, &error);
864 gst_object_unref (plugin);
867 GST_WARNING ("load_plugin error: %s\n", error->message);
868 g_error_free (error);
871 /* newplugin was reffed by load_file */
875 GST_DEBUG ("Could not find plugin %s in registry", name);
881 * @plugin: plugin to load
883 * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
884 * untouched. The normal use pattern of this function goes like this:
887 * GstPlugin *loaded_plugin;
888 * loaded_plugin = gst_plugin_load (plugin);
890 * // presumably, we're no longer interested in the potentially-unloaded plugin
891 * gst_object_unref (plugin);
892 * plugin = loaded_plugin;
895 * Returns: A reference to a loaded plugin, or NULL on error.
898 gst_plugin_load (GstPlugin * plugin)
900 GError *error = NULL;
901 GstPlugin *newplugin;
903 if (gst_plugin_is_loaded (plugin)) {
907 if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
914 GST_WARNING ("load_plugin error: %s\n", error->message);
915 g_error_free (error);
921 * gst_plugin_list_free:
922 * @list: list of #GstPlugin
924 * Unrefs each member of @list, then frees the list.
927 gst_plugin_list_free (GList * list)
931 for (g = list; g; g = g->next) {
932 gst_object_unref (GST_PLUGIN (g->data));