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.
23 #include <sys/types.h>
29 #include "gst_private.h"
31 #include "gstplugin.h"
32 #include "gstversion.h"
33 #include "gstregistrypool.h"
36 #include "gstfilter.h"
39 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
41 static GModule *main_module = NULL;
42 static GList *_gst_plugin_static = NULL;
44 /* static variables for segfault handling of plugin loading */
45 static char *_gst_plugin_fault_handler_filename = NULL;
46 extern gboolean *_gst_disable_segtrap; /* see gst.c */
49 static gboolean *_gst_plugin_fault_handler_is_setup = FALSE;
52 /* list of valid licenses.
53 * One of these must be specified or the plugin won't be loaded
54 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
57 * GPL: http://www.gnu.org/copyleft/gpl.html
58 * LGPL: http://www.gnu.org/copyleft/lesser.html
59 * QPL: http://www.trolltech.com/licenses/qpl.html
61 static gchar *valid_licenses[] = {
62 "LGPL", /* GNU Lesser General Public License */
63 "GPL", /* GNU General Public License */
64 "QPL", /* Trolltech Qt Public License */
65 "GPL/QPL", /* Combi-license of GPL + QPL */
66 GST_LICENSE_UNKNOWN, /* some other license */
70 static void gst_plugin_desc_copy (GstPluginDesc * dest,
71 const GstPluginDesc * src);
73 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
74 GModule * module, GstPluginDesc * desc);
77 gst_plugin_copy (GstPlugin * plugin)
79 return g_memdup (plugin, sizeof (*plugin));
83 gst_plugin_get_type (void)
85 static GType plugin_type;
87 if (plugin_type == 0) {
88 plugin_type = g_boxed_type_register_static ("GstPlugin",
89 (GBoxedCopyFunc) gst_plugin_copy, g_free);
96 gst_plugin_error_quark (void)
98 static GQuark quark = 0;
101 quark = g_quark_from_static_string ("gst_plugin_error");
105 /* this function can be called in the GCC constructor extension, before
106 * the _gst_plugin_initialize() was called. In that case, we store the
107 * plugin description in a list to initialize it when we open the main
109 * When the main module is known, we can register the plugin right away.
112 _gst_plugin_register_static (GstPluginDesc * desc)
114 if (main_module == NULL) {
116 GST_LOG ("queueing static plugin \"%s\" for loading later on",
118 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
123 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
124 plugin = g_new0 (GstPlugin, 1);
125 if (gst_plugin_register_func (plugin, main_module, desc)) {
127 GST_INFO ("loaded static plugin \"%s\"", desc->name);
128 gst_registry_pool_add_plugin (plugin);
134 _gst_plugin_initialize (void)
136 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
138 /* now register all static plugins */
139 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
143 /* this function could be extended to check if the plugin license matches the
144 * applications license (would require the app to register its license somehow).
145 * We'll wait for someone who's interested in it to code it :)
148 gst_plugin_check_license (const gchar * license)
150 gchar **check_license = valid_licenses;
152 g_assert (check_license);
154 while (*check_license) {
155 if (strcmp (license, *check_license) == 0)
163 gst_plugin_check_version (gint major, gint minor)
165 /* return NULL if the major and minor version numbers are not compatible */
167 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
174 gst_plugin_register_func (GstPlugin * plugin, GModule * module,
175 GstPluginDesc * desc)
177 g_assert (plugin->module == NULL);
179 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
181 GST_INFO ("plugin \"%s\" has incompatible version, not loading",
186 if (!desc->license || !desc->description || !desc->package || !desc->origin) {
188 GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
193 if (!gst_plugin_check_license (desc->license)) {
195 GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
196 plugin->filename, desc->license);
200 gst_plugin_desc_copy (&plugin->desc, desc);
201 plugin->module = module;
203 if (!((desc->plugin_init) (plugin))) {
205 GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
206 plugin->module = NULL;
211 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
218 * _gst_plugin_fault_handler_restore:
219 * segfault handler restorer
222 _gst_plugin_fault_handler_restore (void)
224 struct sigaction action;
226 memset (&action, 0, sizeof (action));
227 action.sa_handler = SIG_DFL;
229 sigaction (SIGSEGV, &action, NULL);
233 * _gst_plugin_fault_handler_sighandler:
234 * segfault handler implementation
237 _gst_plugin_fault_handler_sighandler (int signum)
239 /* We need to restore the fault handler or we'll keep getting it */
240 _gst_plugin_fault_handler_restore ();
244 g_print ("\nERROR: ");
245 g_print ("Caught a segmentation fault while loading plugin file:\n");
246 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
247 g_print ("Please either:\n");
248 g_print ("- remove it and restart.\n");
249 g_print ("- run with --gst-disable-segtrap and debug.\n");
253 g_print ("Caught unhandled signal on plugin loading\n");
259 * _gst_plugin_fault_handler_setup:
260 * sets up the segfault handler
263 _gst_plugin_fault_handler_setup (void)
265 struct sigaction action;
267 /* if asked to leave segfaults alone, just return */
268 if (_gst_disable_segtrap)
271 if (_gst_plugin_fault_handler_is_setup)
274 memset (&action, 0, sizeof (action));
275 action.sa_handler = _gst_plugin_fault_handler_sighandler;
277 sigaction (SIGSEGV, &action, NULL);
281 _gst_plugin_fault_handler_restore (void)
286 _gst_plugin_fault_handler_setup (void)
291 static void _gst_plugin_fault_handler_setup ();
294 * gst_plugin_load_file:
295 * @filename: the plugin filename to load
296 * @error: pointer to a NULL-valued GError
298 * Loads the given plugin.
300 * Returns: a new GstPlugin or NULL, if an error occurred.
303 gst_plugin_load_file (const gchar * filename, GError ** error)
308 struct stat file_status;
309 gboolean free_plugin;
311 g_return_val_if_fail (filename != NULL, NULL);
313 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
316 if (g_module_supported () == FALSE) {
319 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
323 if (stat (filename, &file_status)) {
326 GST_PLUGIN_ERROR_MODULE, "Problem opening file %s\n", filename);
330 module = g_module_open (filename, G_MODULE_BIND_LAZY);
332 if (module != NULL) {
335 if (g_module_symbol (module, "gst_plugin_desc", &ptr)) {
336 desc = (GstPluginDesc *) ptr;
338 plugin = gst_registry_pool_find_plugin (desc->name);
341 plugin = g_new0 (GstPlugin, 1);
342 plugin->filename = g_strdup (filename);
343 GST_DEBUG ("created new GstPlugin %p for file \"%s\"", plugin,
347 if (gst_plugin_is_loaded (plugin)) {
348 if (plugin->filename && strcmp (plugin->filename, filename) != 0) {
350 ("plugin %p from file \"%s\" with same name %s is already "
351 "loaded, aborting loading of \"%s\"", plugin, plugin->filename,
352 plugin->desc.name, filename);
353 g_set_error (error, GST_PLUGIN_ERROR,
354 GST_PLUGIN_ERROR_NAME_MISMATCH,
355 "already a plugin with name \"%s\" loaded", desc->name);
360 GST_LOG ("Plugin %p for file \"%s\" already loaded, returning it now",
365 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
368 if (g_module_symbol (module, "plugin_init", &ptr)) {
370 ("plugin %p from file \"%s\" exports a symbol named plugin_init\n",
371 plugin, plugin->filename);
372 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_NAME_MISMATCH,
373 "plugin \"%s\" exports a symbol named plugin_init", desc->name);
376 /* this is where we load the actual .so, so let's trap SIGSEGV */
377 _gst_plugin_fault_handler_setup ();
378 _gst_plugin_fault_handler_filename = plugin->filename;
380 if (gst_plugin_register_func (plugin, module, desc)) {
381 /* remove signal handler */
382 _gst_plugin_fault_handler_restore ();
383 _gst_plugin_fault_handler_filename = NULL;
384 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
387 /* remove signal handler */
388 _gst_plugin_fault_handler_restore ();
389 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"",
394 GST_PLUGIN_ERROR_MODULE,
395 "gst_plugin_register_func failed for plugin \"%s\"", filename);
401 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
404 GST_PLUGIN_ERROR_MODULE,
405 "Could not find plugin entry point in \"%s\"", filename);
409 GST_DEBUG ("Error loading plugin %s, reason: %s\n", filename,
411 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
412 "Error loading plugin %s, reason: %s\n", filename, g_module_error ());
418 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
420 dest->major_version = src->major_version;
421 dest->minor_version = src->minor_version;
423 dest->name = g_strdup (src->name);
424 g_free (dest->description);
425 dest->description = g_strdup (src->description);
426 dest->plugin_init = src->plugin_init;
427 dest->plugin_exit = src->plugin_exit;
428 g_free (dest->version);
429 dest->version = g_strdup (src->version);
430 g_free (dest->license);
431 dest->license = g_strdup (src->license);
432 g_free (dest->package);
433 dest->package = g_strdup (src->package);
434 g_free (dest->origin);
435 dest->origin = g_strdup (src->origin);
441 gst_plugin_desc_free (GstPluginDesc * desc)
444 g_free (desc->description);
445 g_free (desc->version);
446 g_free (desc->license);
447 g_free (desc->package);
448 g_free (desc->origin);
450 memset (desc, 0, sizeof (GstPluginDesc));
454 * gst_plugin_unload_plugin:
455 * @plugin: The plugin to unload
457 * Unload the given plugin.
459 * Returns: whether or not the plugin unloaded
462 gst_plugin_unload_plugin (GstPlugin * plugin)
464 g_return_val_if_fail (plugin != NULL, FALSE);
469 if (g_module_close (plugin->module)) {
470 plugin->module = NULL;
471 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" unloaded",
475 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "failed to unload plugin \"%s\"",
482 * gst_plugin_get_name:
483 * @plugin: plugin to get the name of
485 * Get the short name of the plugin
487 * Returns: the name of the plugin
490 gst_plugin_get_name (GstPlugin * plugin)
492 g_return_val_if_fail (plugin != NULL, NULL);
494 return plugin->desc.name;
498 * gst_plugin_get_description:
499 * @plugin: plugin to get long name of
501 * Get the long descriptive name of the plugin
503 * Returns: the long name of the plugin
505 G_CONST_RETURN gchar *
506 gst_plugin_get_description (GstPlugin * plugin)
508 g_return_val_if_fail (plugin != NULL, NULL);
510 return plugin->desc.description;
514 * gst_plugin_get_filename:
515 * @plugin: plugin to get the filename of
517 * get the filename of the plugin
519 * Returns: the filename of the plugin
521 G_CONST_RETURN gchar *
522 gst_plugin_get_filename (GstPlugin * plugin)
524 g_return_val_if_fail (plugin != NULL, NULL);
526 return plugin->filename;
530 * gst_plugin_get_license:
531 * @plugin: plugin to get the license of
533 * get the license of the plugin
535 * Returns: the license of the plugin
537 G_CONST_RETURN gchar *
538 gst_plugin_get_license (GstPlugin * plugin)
540 g_return_val_if_fail (plugin != NULL, NULL);
542 return plugin->desc.license;
546 * gst_plugin_get_package:
547 * @plugin: plugin to get the package of
549 * get the package the plugin belongs to.
551 * Returns: the package of the plugin
553 G_CONST_RETURN gchar *
554 gst_plugin_get_package (GstPlugin * plugin)
556 g_return_val_if_fail (plugin != NULL, NULL);
558 return plugin->desc.package;
562 * gst_plugin_get_origin:
563 * @plugin: plugin to get the origin of
565 * get the URL where the plugin comes from
567 * Returns: the origin of the plugin
569 G_CONST_RETURN gchar *
570 gst_plugin_get_origin (GstPlugin * plugin)
572 g_return_val_if_fail (plugin != NULL, NULL);
574 return plugin->desc.origin;
578 * gst_plugin_get_module:
579 * @plugin: plugin to query
581 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
584 * Returns: module belonging to the plugin or NULL if the plugin isn't
588 gst_plugin_get_module (GstPlugin * plugin)
590 g_return_val_if_fail (plugin != NULL, FALSE);
592 return plugin->module;
596 * gst_plugin_is_loaded:
597 * @plugin: plugin to query
599 * queries if the plugin is loaded into memory
601 * Returns: TRUE is loaded, FALSE otherwise
604 gst_plugin_is_loaded (GstPlugin * plugin)
606 g_return_val_if_fail (plugin != NULL, FALSE);
608 return (plugin->module != NULL);
612 * gst_plugin_feature_list:
613 * @plugin: plugin to query
614 * @filter: the filter to use
615 * @first: only return first match
616 * @user_data: user data passed to the filter function
618 * Runs a filter against all plugin features and returns a GList with
619 * the results. If the first flag is set, only the first match is
620 * returned (as a list with a single object).
622 * Returns: a GList of features, g_list_free after use.
625 gst_plugin_feature_filter (GstPlugin * plugin,
626 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
628 return gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
634 GstPluginFeatureFilter filter;
642 _feature_filter (GstPlugin * plugin, gpointer user_data)
645 FeatureFilterData *data = (FeatureFilterData *) user_data;
648 gst_plugin_feature_filter (plugin, data->filter, data->first,
651 data->result = g_list_concat (data->result, result);
658 * gst_plugin_list_feature_list:
659 * @list: a list of plugins to query
660 * @filter: the filter to use
661 * @first: only return first match
662 * @user_data: user data passed to the filter function
664 * Runs a filter against all plugin features of the plugins in the given
665 * list and returns a GList with the results.
666 * If the first flag is set, only the first match is
667 * returned (as a list with a single object).
669 * Returns: a GList of features, g_list_free after use.
672 gst_plugin_list_feature_filter (GList * list,
673 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
675 FeatureFilterData data;
678 data.filter = filter;
680 data.user_data = user_data;
683 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
684 g_list_free (result);
690 * gst_plugin_name_filter:
691 * @plugin: the plugin to check
692 * @name: the name of the plugin
694 * A standard filter that returns TRUE when the plugin is of the
697 * Returns: TRUE if the plugin is of the given name.
700 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
702 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
706 * gst_plugin_find_feature:
707 * @plugin: plugin to get the feature from
708 * @name: The name of the feature to find
709 * @type: The type of the feature to find
711 * Find a feature of the given name and type in the given plugin.
713 * Returns: a GstPluginFeature or NULL if the feature was not found.
716 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
719 GstPluginFeature *result = NULL;
720 GstTypeNameData data;
722 g_return_val_if_fail (name != NULL, NULL);
727 walk = gst_filter_run (plugin->features,
728 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
731 result = GST_PLUGIN_FEATURE (walk->data);
737 * gst_plugin_add_feature:
738 * @plugin: plugin to add feature to
739 * @feature: feature to add
741 * Add feature to the list of those provided by the plugin.
742 * There is a separate namespace for each plugin feature type.
743 * See #gst_plugin_get_feature_list
746 gst_plugin_add_feature (GstPlugin * plugin, GstPluginFeature * feature)
748 GstPluginFeature *oldfeature;
750 g_return_if_fail (plugin != NULL);
751 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
752 g_return_if_fail (feature != NULL);
754 oldfeature = gst_plugin_find_feature (plugin,
755 GST_PLUGIN_FEATURE_NAME (feature), G_OBJECT_TYPE (feature));
758 feature->manager = plugin;
759 plugin->features = g_list_prepend (plugin->features, feature);
760 plugin->numfeatures++;
765 * gst_plugin_get_feature_list:
766 * @plugin: the plugin to get the features from
768 * get a list of all the features that this plugin provides
770 * Returns: a GList of features, use g_list_free to free the list.
773 gst_plugin_get_feature_list (GstPlugin * plugin)
775 g_return_val_if_fail (plugin != NULL, NULL);
777 return g_list_copy (plugin->features);
782 * @name: name of plugin to load
784 * Load the named plugin.
786 * Returns: whether the plugin was loaded or not
789 gst_plugin_load (const gchar * name)
792 GError *error = NULL;
794 plugin = gst_registry_pool_find_plugin (name);
796 gst_plugin_load_file (plugin->filename, &error);
798 GST_WARNING ("load_plugin error: %s\n", error->message);
799 g_error_free (error);
805 GST_DEBUG ("Could not find %s in registry pool", name);
811 * @name: name of library to load
813 * Load the named library. Name should be given as
814 * "liblibrary.so".
816 * Returns: whether the library was loaded or not
819 gst_library_load (const gchar * name)
823 /* for now this is the same */
824 res = gst_plugin_load (name);