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.
26 #include <sys/types.h>
36 #include "gst_private.h"
38 #include "gstplugin.h"
39 #include "gstversion.h"
40 #include "gstregistrypool.h"
42 #include "gstfilter.h"
45 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
47 static GModule *main_module = NULL;
48 static GList *_gst_plugin_static = NULL;
50 /* static variables for segfault handling of plugin loading */
51 static char *_gst_plugin_fault_handler_filename = NULL;
52 extern gboolean *_gst_disable_segtrap; /* see gst.c */
55 static gboolean *_gst_plugin_fault_handler_is_setup = FALSE;
58 /* list of valid licenses.
59 * One of these must be specified or the plugin won't be loaded
60 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
63 * GPL: http://www.gnu.org/copyleft/gpl.html
64 * LGPL: http://www.gnu.org/copyleft/lesser.html
65 * QPL: http://www.trolltech.com/licenses/qpl.html
67 static gchar *valid_licenses[] = {
68 "LGPL", /* GNU Lesser General Public License */
69 "GPL", /* GNU General Public License */
70 "QPL", /* Trolltech Qt Public License */
71 "GPL/QPL", /* Combi-license of GPL + QPL */
72 GST_LICENSE_UNKNOWN, /* some other license */
76 static void gst_plugin_desc_copy (GstPluginDesc * dest,
77 const GstPluginDesc * src);
79 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
80 GModule * module, GstPluginDesc * desc);
83 gst_plugin_copy (GstPlugin * plugin)
85 return g_memdup (plugin, sizeof (*plugin));
89 gst_plugin_get_type (void)
91 static GType plugin_type;
93 if (plugin_type == 0) {
94 plugin_type = g_boxed_type_register_static ("GstPlugin",
95 (GBoxedCopyFunc) gst_plugin_copy, g_free);
102 gst_plugin_error_quark (void)
104 static GQuark quark = 0;
107 quark = g_quark_from_static_string ("gst_plugin_error");
111 /* this function can be called in the GCC constructor extension, before
112 * the _gst_plugin_initialize() was called. In that case, we store the
113 * plugin description in a list to initialize it when we open the main
115 * When the main module is known, we can register the plugin right away.
118 _gst_plugin_register_static (GstPluginDesc * desc)
120 if (main_module == NULL) {
122 GST_LOG ("queueing static plugin \"%s\" for loading later on",
124 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
129 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
130 plugin = g_new0 (GstPlugin, 1);
131 if (gst_plugin_register_func (plugin, main_module, desc)) {
133 GST_INFO ("loaded static plugin \"%s\"", desc->name);
134 gst_registry_pool_add_plugin (plugin);
140 _gst_plugin_initialize (void)
142 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
144 /* now register all static plugins */
145 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
149 /* this function could be extended to check if the plugin license matches the
150 * applications license (would require the app to register its license somehow).
151 * We'll wait for someone who's interested in it to code it :)
154 gst_plugin_check_license (const gchar * license)
156 gchar **check_license = valid_licenses;
158 g_assert (check_license);
160 while (*check_license) {
161 if (strcmp (license, *check_license) == 0)
169 gst_plugin_check_version (gint major, gint minor)
171 /* return NULL if the major and minor version numbers are not compatible */
173 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
180 gst_plugin_register_func (GstPlugin * plugin, GModule * module,
181 GstPluginDesc * desc)
183 g_assert (plugin->module == NULL);
185 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
187 GST_INFO ("plugin \"%s\" has incompatible version, not loading",
192 if (!desc->license || !desc->description || !desc->package || !desc->origin) {
194 GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
199 if (!gst_plugin_check_license (desc->license)) {
201 GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
202 plugin->filename, desc->license);
206 gst_plugin_desc_copy (&plugin->desc, desc);
207 plugin->module = module;
209 if (!((desc->plugin_init) (plugin))) {
211 GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
212 plugin->module = NULL;
217 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
224 * _gst_plugin_fault_handler_restore:
225 * segfault handler restorer
228 _gst_plugin_fault_handler_restore (void)
230 struct sigaction action;
232 memset (&action, 0, sizeof (action));
233 action.sa_handler = SIG_DFL;
235 sigaction (SIGSEGV, &action, NULL);
239 * _gst_plugin_fault_handler_sighandler:
240 * segfault handler implementation
243 _gst_plugin_fault_handler_sighandler (int signum)
245 /* We need to restore the fault handler or we'll keep getting it */
246 _gst_plugin_fault_handler_restore ();
250 g_print ("\nERROR: ");
251 g_print ("Caught a segmentation fault while loading plugin file:\n");
252 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
253 g_print ("Please either:\n");
254 g_print ("- remove it and restart.\n");
255 g_print ("- run with --gst-disable-segtrap and debug.\n");
259 g_print ("Caught unhandled signal on plugin loading\n");
265 * _gst_plugin_fault_handler_setup:
266 * sets up the segfault handler
269 _gst_plugin_fault_handler_setup (void)
271 struct sigaction action;
273 /* if asked to leave segfaults alone, just return */
274 if (_gst_disable_segtrap)
277 if (_gst_plugin_fault_handler_is_setup)
280 memset (&action, 0, sizeof (action));
281 action.sa_handler = _gst_plugin_fault_handler_sighandler;
283 sigaction (SIGSEGV, &action, NULL);
287 _gst_plugin_fault_handler_restore (void)
292 _gst_plugin_fault_handler_setup (void)
297 static void _gst_plugin_fault_handler_setup ();
300 * gst_plugin_load_file:
301 * @filename: the plugin filename to load
302 * @error: pointer to a NULL-valued GError
304 * Loads the given plugin.
306 * Returns: a new GstPlugin or NULL, if an error occurred.
309 gst_plugin_load_file (const gchar * filename, GError ** error)
314 struct stat file_status;
315 gboolean free_plugin;
317 g_return_val_if_fail (filename != NULL, NULL);
319 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
322 if (g_module_supported () == FALSE) {
325 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
329 if (stat (filename, &file_status)) {
332 GST_PLUGIN_ERROR_MODULE, "Problem opening file %s\n", filename);
336 module = g_module_open (filename, G_MODULE_BIND_LAZY);
338 if (module != NULL) {
341 if (g_module_symbol (module, "gst_plugin_desc", &ptr)) {
342 desc = (GstPluginDesc *) ptr;
344 plugin = gst_registry_pool_find_plugin (desc->name);
347 plugin = g_new0 (GstPlugin, 1);
348 plugin->filename = g_strdup (filename);
349 GST_DEBUG ("created new GstPlugin %p for file \"%s\"", plugin,
353 if (gst_plugin_is_loaded (plugin)) {
354 if (plugin->filename && strcmp (plugin->filename, filename) != 0) {
356 ("plugin %p from file \"%s\" with same name %s is already "
357 "loaded, aborting loading of \"%s\"", plugin, plugin->filename,
358 plugin->desc.name, filename);
359 g_set_error (error, GST_PLUGIN_ERROR,
360 GST_PLUGIN_ERROR_NAME_MISMATCH,
361 "already a plugin with name \"%s\" loaded", desc->name);
366 GST_LOG ("Plugin %p for file \"%s\" already loaded, returning it now",
371 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
374 if (g_module_symbol (module, "plugin_init", &ptr)) {
376 ("plugin %p from file \"%s\" exports a symbol named plugin_init\n",
377 plugin, plugin->filename);
378 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_NAME_MISMATCH,
379 "plugin \"%s\" exports a symbol named plugin_init", desc->name);
382 /* this is where we load the actual .so, so let's trap SIGSEGV */
383 _gst_plugin_fault_handler_setup ();
384 _gst_plugin_fault_handler_filename = plugin->filename;
386 if (gst_plugin_register_func (plugin, module, desc)) {
387 /* remove signal handler */
388 _gst_plugin_fault_handler_restore ();
389 _gst_plugin_fault_handler_filename = NULL;
390 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
393 /* remove signal handler */
394 _gst_plugin_fault_handler_restore ();
395 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"",
400 GST_PLUGIN_ERROR_MODULE,
401 "gst_plugin_register_func failed for plugin \"%s\"", filename);
407 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
410 GST_PLUGIN_ERROR_MODULE,
411 "Could not find plugin entry point in \"%s\"", filename);
415 GST_DEBUG ("Error loading plugin %s, reason: %s\n", filename,
417 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
418 "Error loading plugin %s, reason: %s\n", filename, g_module_error ());
424 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
426 dest->major_version = src->major_version;
427 dest->minor_version = src->minor_version;
429 dest->name = g_strdup (src->name);
430 g_free (dest->description);
431 dest->description = g_strdup (src->description);
432 dest->plugin_init = src->plugin_init;
433 dest->plugin_exit = src->plugin_exit;
434 g_free (dest->version);
435 dest->version = g_strdup (src->version);
436 g_free (dest->license);
437 dest->license = g_strdup (src->license);
438 g_free (dest->package);
439 dest->package = g_strdup (src->package);
440 g_free (dest->origin);
441 dest->origin = g_strdup (src->origin);
447 gst_plugin_desc_free (GstPluginDesc * desc)
450 g_free (desc->description);
451 g_free (desc->version);
452 g_free (desc->license);
453 g_free (desc->package);
454 g_free (desc->origin);
456 memset (desc, 0, sizeof (GstPluginDesc));
460 * gst_plugin_unload_plugin:
461 * @plugin: The plugin to unload
463 * Unload the given plugin.
465 * Returns: whether or not the plugin unloaded
468 gst_plugin_unload_plugin (GstPlugin * plugin)
470 g_return_val_if_fail (plugin != NULL, FALSE);
475 if (g_module_close (plugin->module)) {
476 plugin->module = NULL;
477 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" unloaded",
481 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "failed to unload plugin \"%s\"",
488 * gst_plugin_get_name:
489 * @plugin: plugin to get the name of
491 * Get the short name of the plugin
493 * Returns: the name of the plugin
496 gst_plugin_get_name (GstPlugin * plugin)
498 g_return_val_if_fail (plugin != NULL, NULL);
500 return plugin->desc.name;
504 * gst_plugin_get_description:
505 * @plugin: plugin to get long name of
507 * Get the long descriptive name of the plugin
509 * Returns: the long name of the plugin
511 G_CONST_RETURN gchar *
512 gst_plugin_get_description (GstPlugin * plugin)
514 g_return_val_if_fail (plugin != NULL, NULL);
516 return plugin->desc.description;
520 * gst_plugin_get_filename:
521 * @plugin: plugin to get the filename of
523 * get the filename of the plugin
525 * Returns: the filename of the plugin
527 G_CONST_RETURN gchar *
528 gst_plugin_get_filename (GstPlugin * plugin)
530 g_return_val_if_fail (plugin != NULL, NULL);
532 return plugin->filename;
536 * gst_plugin_get_license:
537 * @plugin: plugin to get the license of
539 * get the license of the plugin
541 * Returns: the license of the plugin
543 G_CONST_RETURN gchar *
544 gst_plugin_get_license (GstPlugin * plugin)
546 g_return_val_if_fail (plugin != NULL, NULL);
548 return plugin->desc.license;
552 * gst_plugin_get_package:
553 * @plugin: plugin to get the package of
555 * get the package the plugin belongs to.
557 * Returns: the package of the plugin
559 G_CONST_RETURN gchar *
560 gst_plugin_get_package (GstPlugin * plugin)
562 g_return_val_if_fail (plugin != NULL, NULL);
564 return plugin->desc.package;
568 * gst_plugin_get_origin:
569 * @plugin: plugin to get the origin of
571 * get the URL where the plugin comes from
573 * Returns: the origin of the plugin
575 G_CONST_RETURN gchar *
576 gst_plugin_get_origin (GstPlugin * plugin)
578 g_return_val_if_fail (plugin != NULL, NULL);
580 return plugin->desc.origin;
584 * gst_plugin_get_module:
585 * @plugin: plugin to query
587 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
590 * Returns: module belonging to the plugin or NULL if the plugin isn't
594 gst_plugin_get_module (GstPlugin * plugin)
596 g_return_val_if_fail (plugin != NULL, FALSE);
598 return plugin->module;
602 * gst_plugin_is_loaded:
603 * @plugin: plugin to query
605 * queries if the plugin is loaded into memory
607 * Returns: TRUE is loaded, FALSE otherwise
610 gst_plugin_is_loaded (GstPlugin * plugin)
612 g_return_val_if_fail (plugin != NULL, FALSE);
614 return (plugin->module != NULL);
618 * gst_plugin_feature_list:
619 * @plugin: plugin to query
620 * @filter: the filter to use
621 * @first: only return first match
622 * @user_data: user data passed to the filter function
624 * Runs a filter against all plugin features and returns a GList with
625 * the results. If the first flag is set, only the first match is
626 * returned (as a list with a single object).
628 * Returns: a GList of features, g_list_free after use.
631 gst_plugin_feature_filter (GstPlugin * plugin,
632 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
634 return gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
640 GstPluginFeatureFilter filter;
648 _feature_filter (GstPlugin * plugin, gpointer user_data)
651 FeatureFilterData *data = (FeatureFilterData *) user_data;
654 gst_plugin_feature_filter (plugin, data->filter, data->first,
657 data->result = g_list_concat (data->result, result);
664 * gst_plugin_list_feature_list:
665 * @list: a list of plugins to query
666 * @filter: the filter to use
667 * @first: only return first match
668 * @user_data: user data passed to the filter function
670 * Runs a filter against all plugin features of the plugins in the given
671 * list and returns a GList with the results.
672 * If the first flag is set, only the first match is
673 * returned (as a list with a single object).
675 * Returns: a GList of features, g_list_free after use.
678 gst_plugin_list_feature_filter (GList * list,
679 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
681 FeatureFilterData data;
684 data.filter = filter;
686 data.user_data = user_data;
689 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
690 g_list_free (result);
696 * gst_plugin_name_filter:
697 * @plugin: the plugin to check
698 * @name: the name of the plugin
700 * A standard filter that returns TRUE when the plugin is of the
703 * Returns: TRUE if the plugin is of the given name.
706 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
708 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
712 * gst_plugin_find_feature:
713 * @plugin: plugin to get the feature from
714 * @name: The name of the feature to find
715 * @type: The type of the feature to find
717 * Find a feature of the given name and type in the given plugin.
719 * Returns: a GstPluginFeature or NULL if the feature was not found.
722 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
725 GstPluginFeature *result = NULL;
726 GstTypeNameData data;
728 g_return_val_if_fail (name != NULL, NULL);
733 walk = gst_filter_run (plugin->features,
734 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
737 result = GST_PLUGIN_FEATURE (walk->data);
743 * gst_plugin_add_feature:
744 * @plugin: plugin to add feature to
745 * @feature: feature to add
747 * Add feature to the list of those provided by the plugin.
748 * There is a separate namespace for each plugin feature type.
749 * See #gst_plugin_get_feature_list
752 gst_plugin_add_feature (GstPlugin * plugin, GstPluginFeature * feature)
754 GstPluginFeature *oldfeature;
756 g_return_if_fail (plugin != NULL);
757 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
758 g_return_if_fail (feature != NULL);
760 oldfeature = gst_plugin_find_feature (plugin,
761 GST_PLUGIN_FEATURE_NAME (feature), G_OBJECT_TYPE (feature));
764 feature->manager = plugin;
765 plugin->features = g_list_prepend (plugin->features, feature);
766 plugin->numfeatures++;
771 * gst_plugin_get_feature_list:
772 * @plugin: the plugin to get the features from
774 * get a list of all the features that this plugin provides
776 * Returns: a GList of features, use g_list_free to free the list.
779 gst_plugin_get_feature_list (GstPlugin * plugin)
781 g_return_val_if_fail (plugin != NULL, NULL);
783 return g_list_copy (plugin->features);
788 * @name: name of plugin to load
790 * Load the named plugin.
792 * Returns: whether the plugin was loaded or not
795 gst_plugin_load (const gchar * name)
798 GError *error = NULL;
800 plugin = gst_registry_pool_find_plugin (name);
802 gst_plugin_load_file (plugin->filename, &error);
804 GST_WARNING ("load_plugin error: %s\n", error->message);
805 g_error_free (error);
811 GST_DEBUG ("Could not find %s in registry pool", name);
817 * @name: name of library to load
819 * Load the named library. Name should be given as
820 * "liblibrary.so".
822 * Returns: whether the library was loaded or not
825 gst_library_load (const gchar * name)
829 /* for now this is the same */
830 res = gst_plugin_load (name);