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 */
47 static gboolean *_gst_plugin_fault_handler_is_setup = FALSE;
49 /* list of valid licenses.
50 * One of these must be specified or the plugin won't be loaded
51 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
54 * GPL: http://www.gnu.org/copyleft/gpl.html
55 * LGPL: http://www.gnu.org/copyleft/lesser.html
56 * QPL: http://www.trolltech.com/licenses/qpl.html
58 static gchar *valid_licenses[] = {
59 "LGPL", /* GNU Lesser General Public License */
60 "GPL", /* GNU General Public License */
61 "QPL", /* Trolltech Qt Public License */
62 "GPL/QPL", /* Combi-license of GPL + QPL */
63 GST_LICENSE_UNKNOWN, /* some other license */
67 static void gst_plugin_desc_copy (GstPluginDesc * dest,
68 const GstPluginDesc * src);
70 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
71 GModule * module, GstPluginDesc * desc);
74 gst_plugin_copy (GstPlugin * plugin)
76 return g_memdup (plugin, sizeof (*plugin));
80 gst_plugin_get_type (void)
82 static GType plugin_type;
84 if (plugin_type == 0) {
85 plugin_type = g_boxed_type_register_static ("GstPlugin",
86 (GBoxedCopyFunc) gst_plugin_copy, g_free);
93 gst_plugin_error_quark (void)
95 static GQuark quark = 0;
98 quark = g_quark_from_static_string ("gst_plugin_error");
102 /* this function can be called in the GCC constructor extension, before
103 * the _gst_plugin_initialize() was called. In that case, we store the
104 * plugin description in a list to initialize it when we open the main
106 * When the main module is known, we can register the plugin right away.
109 _gst_plugin_register_static (GstPluginDesc * desc)
111 if (main_module == NULL) {
113 GST_LOG ("queueing static plugin \"%s\" for loading later on",
115 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
120 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
121 plugin = g_new0 (GstPlugin, 1);
122 if (gst_plugin_register_func (plugin, main_module, desc)) {
124 GST_INFO ("loaded static plugin \"%s\"", desc->name);
125 gst_registry_pool_add_plugin (plugin);
131 _gst_plugin_initialize (void)
133 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
135 /* now register all static plugins */
136 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
140 /* this function could be extended to check if the plugin license matches the
141 * applications license (would require the app to register its license somehow).
142 * We'll wait for someone who's interested in it to code it :)
145 gst_plugin_check_license (const gchar * license)
147 gchar **check_license = valid_licenses;
149 g_assert (check_license);
151 while (*check_license) {
152 if (strcmp (license, *check_license) == 0)
160 gst_plugin_check_version (gint major, gint minor)
162 /* return NULL if the major and minor version numbers are not compatible */
164 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
171 gst_plugin_register_func (GstPlugin * plugin, GModule * module,
172 GstPluginDesc * desc)
174 g_assert (plugin->module == NULL);
176 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
178 GST_INFO ("plugin \"%s\" has incompatible version, not loading",
183 if (!desc->license || !desc->description || !desc->package || !desc->origin) {
185 GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
190 if (!gst_plugin_check_license (desc->license)) {
192 GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
193 plugin->filename, desc->license);
197 gst_plugin_desc_copy (&plugin->desc, desc);
198 plugin->module = module;
200 if (!((desc->plugin_init) (plugin))) {
202 GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
203 plugin->module = NULL;
208 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
214 * _gst_plugin_fault_handler_restore:
215 * segfault handler restorer
218 _gst_plugin_fault_handler_restore (void)
220 struct sigaction action;
222 memset (&action, 0, sizeof (action));
223 action.sa_handler = SIG_DFL;
225 sigaction (SIGSEGV, &action, NULL);
229 * _gst_plugin_fault_handler_sighandler:
230 * segfault handler implementation
233 _gst_plugin_fault_handler_sighandler (int signum)
235 /* We need to restore the fault handler or we'll keep getting it */
236 _gst_plugin_fault_handler_restore ();
240 g_print ("\nERROR: ");
241 g_print ("Caught a segmentation fault while loading plugin file:\n");
242 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
243 g_print ("Please either:\n");
244 g_print ("- remove it and restart.\n");
245 g_print ("- run with --gst-disable-segtrap and debug.\n");
249 g_print ("Caught unhandled signal on plugin loading\n");
255 * _gst_plugin_fault_handler_setup:
256 * sets up the segfault handler
259 _gst_plugin_fault_handler_setup (void)
261 struct sigaction action;
263 /* if asked to leave segfaults alone, just return */
264 if (_gst_disable_segtrap)
267 if (_gst_plugin_fault_handler_is_setup)
270 memset (&action, 0, sizeof (action));
271 action.sa_handler = _gst_plugin_fault_handler_sighandler;
273 sigaction (SIGSEGV, &action, NULL);
276 static void _gst_plugin_fault_handler_setup ();
279 * gst_plugin_load_file:
280 * @plugin: The plugin to load
281 * @error: Pointer to a NULL-valued GError.
283 * Load the given plugin.
285 * Returns: a new GstPlugin or NULL, if an error occurred.
288 gst_plugin_load_file (const gchar * filename, GError ** error)
293 struct stat file_status;
294 gboolean free_plugin;
296 g_return_val_if_fail (filename != NULL, NULL);
298 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
301 if (g_module_supported () == FALSE) {
304 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
308 if (stat (filename, &file_status)) {
311 GST_PLUGIN_ERROR_MODULE, "Problem opening file %s\n", filename);
315 module = g_module_open (filename, G_MODULE_BIND_LAZY);
317 if (module != NULL) {
320 if (g_module_symbol (module, "gst_plugin_desc", &ptr)) {
321 desc = (GstPluginDesc *) ptr;
323 plugin = gst_registry_pool_find_plugin (desc->name);
326 plugin = g_new0 (GstPlugin, 1);
327 plugin->filename = g_strdup (filename);
328 GST_DEBUG ("created new GstPlugin %p for file \"%s\"", plugin,
332 if (gst_plugin_is_loaded (plugin)) {
333 if (strcmp (plugin->filename, filename) != 0) {
335 ("plugin %p from file \"%s\" with same name %s is already "
336 "loaded, aborting loading of \"%s\"", plugin, plugin->filename,
337 plugin->desc.name, filename);
338 g_set_error (error, GST_PLUGIN_ERROR,
339 GST_PLUGIN_ERROR_NAME_MISMATCH,
340 "already a plugin with name \"%s\" loaded", desc->name);
345 GST_LOG ("Plugin %p for file \"%s\" already loaded, returning it now",
350 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
353 if (g_module_symbol (module, "plugin_init", &ptr)) {
355 ("plugin %p from file \"%s\" exports a symbol named plugin_init\n",
356 plugin, plugin->filename);
357 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_NAME_MISMATCH,
358 "plugin \"%s\" exports a symbol named plugin_init", desc->name);
361 /* this is where we load the actual .so, so let's trap SIGSEGV */
362 _gst_plugin_fault_handler_setup ();
363 _gst_plugin_fault_handler_filename = plugin->filename;
365 if (gst_plugin_register_func (plugin, module, desc)) {
366 /* remove signal handler */
367 _gst_plugin_fault_handler_restore ();
368 _gst_plugin_fault_handler_filename = NULL;
369 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
372 /* remove signal handler */
373 _gst_plugin_fault_handler_restore ();
374 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"",
379 GST_PLUGIN_ERROR_MODULE,
380 "gst_plugin_register_func failed for plugin \"%s\"", filename);
386 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
389 GST_PLUGIN_ERROR_MODULE,
390 "Could not find plugin entry point in \"%s\"", filename);
394 GST_DEBUG ("Error loading plugin %s, reason: %s\n", filename,
396 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
397 "Error loading plugin %s, reason: %s\n", filename, g_module_error ());
403 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
405 dest->major_version = src->major_version;
406 dest->minor_version = src->minor_version;
408 dest->name = g_strdup (src->name);
409 g_free (dest->description);
410 dest->description = g_strdup (src->description);
411 dest->plugin_init = src->plugin_init;
412 dest->plugin_exit = src->plugin_exit;
413 g_free (dest->version);
414 dest->version = g_strdup (src->version);
415 g_free (dest->license);
416 dest->license = g_strdup (src->license);
417 g_free (dest->package);
418 dest->package = g_strdup (src->package);
419 g_free (dest->origin);
420 dest->origin = g_strdup (src->origin);
426 gst_plugin_desc_free (GstPluginDesc * desc)
429 g_free (desc->description);
430 g_free (desc->version);
431 g_free (desc->license);
432 g_free (desc->package);
433 g_free (desc->origin);
435 memset (desc, 0, sizeof (GstPluginDesc));
439 * gst_plugin_unload_plugin:
440 * @plugin: The plugin to unload
442 * Unload the given plugin.
444 * Returns: whether or not the plugin unloaded
447 gst_plugin_unload_plugin (GstPlugin * plugin)
449 g_return_val_if_fail (plugin != NULL, FALSE);
454 if (g_module_close (plugin->module)) {
455 plugin->module = NULL;
456 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" unloaded",
460 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "failed to unload plugin \"%s\"",
467 * gst_plugin_get_name:
468 * @plugin: plugin to get the name of
470 * Get the short name of the plugin
472 * Returns: the name of the plugin
475 gst_plugin_get_name (GstPlugin * plugin)
477 g_return_val_if_fail (plugin != NULL, NULL);
479 return plugin->desc.name;
483 * gst_plugin_get_description:
484 * @plugin: plugin to get long name of
486 * Get the long descriptive name of the plugin
488 * Returns: the long name of the plugin
490 G_CONST_RETURN gchar *
491 gst_plugin_get_description (GstPlugin * plugin)
493 g_return_val_if_fail (plugin != NULL, NULL);
495 return plugin->desc.description;
499 * gst_plugin_get_filename:
500 * @plugin: plugin to get the filename of
502 * get the filename of the plugin
504 * Returns: the filename of the plugin
506 G_CONST_RETURN gchar *
507 gst_plugin_get_filename (GstPlugin * plugin)
509 g_return_val_if_fail (plugin != NULL, NULL);
511 return plugin->filename;
515 * gst_plugin_get_license:
516 * @plugin: plugin to get the license of
518 * get the license of the plugin
520 * Returns: the license of the plugin
522 G_CONST_RETURN gchar *
523 gst_plugin_get_license (GstPlugin * plugin)
525 g_return_val_if_fail (plugin != NULL, NULL);
527 return plugin->desc.license;
531 * gst_plugin_get_package:
532 * @plugin: plugin to get the package of
534 * get the package the plugin belongs to.
536 * Returns: the package of the plugin
538 G_CONST_RETURN gchar *
539 gst_plugin_get_package (GstPlugin * plugin)
541 g_return_val_if_fail (plugin != NULL, NULL);
543 return plugin->desc.package;
547 * gst_plugin_get_origin:
548 * @plugin: plugin to get the origin of
550 * get the URL where the plugin comes from
552 * Returns: the origin of the plugin
554 G_CONST_RETURN gchar *
555 gst_plugin_get_origin (GstPlugin * plugin)
557 g_return_val_if_fail (plugin != NULL, NULL);
559 return plugin->desc.origin;
563 * gst_plugin_get_module:
564 * @plugin: plugin to query
566 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
569 * Returns: module belonging to the plugin or NULL if the plugin isn't
573 gst_plugin_get_module (GstPlugin * plugin)
575 g_return_val_if_fail (plugin != NULL, FALSE);
577 return plugin->module;
581 * gst_plugin_is_loaded:
582 * @plugin: plugin to query
584 * queries if the plugin is loaded into memory
586 * Returns: TRUE is loaded, FALSE otherwise
589 gst_plugin_is_loaded (GstPlugin * plugin)
591 g_return_val_if_fail (plugin != NULL, FALSE);
593 return (plugin->module != NULL);
597 * gst_plugin_feature_list:
598 * @plugin: plugin to query
599 * @filter: the filter to use
600 * @first: only return first match
601 * @user_data: user data passed to the filter function
603 * Runs a filter against all plugin features and returns a GList with
604 * the results. If the first flag is set, only the first match is
605 * returned (as a list with a single object).
607 * Returns: a GList of features, g_list_free after use.
610 gst_plugin_feature_filter (GstPlugin * plugin,
611 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
613 return gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
619 GstPluginFeatureFilter filter;
627 _feature_filter (GstPlugin * plugin, gpointer user_data)
630 FeatureFilterData *data = (FeatureFilterData *) user_data;
633 gst_plugin_feature_filter (plugin, data->filter, data->first,
636 data->result = g_list_concat (data->result, result);
643 * gst_plugin_list_feature_list:
644 * @list: a list of plugins to query
645 * @filter: the filter to use
646 * @first: only return first match
647 * @user_data: user data passed to the filter function
649 * Runs a filter against all plugin features of the plugins in the given
650 * list and returns a GList with the results.
651 * If the first flag is set, only the first match is
652 * returned (as a list with a single object).
654 * Returns: a GList of features, g_list_free after use.
657 gst_plugin_list_feature_filter (GList * list,
658 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
660 FeatureFilterData data;
663 data.filter = filter;
665 data.user_data = user_data;
668 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
669 g_list_free (result);
675 * gst_plugin_name_filter:
676 * @plugin: the plugin to check
677 * @name: the name of the plugin
679 * A standard filter that returns TRUE when the plugin is of the
682 * Returns: TRUE if the plugin is of the given name.
685 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
687 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
691 * gst_plugin_find_feature:
692 * @plugin: plugin to get the feature from
693 * @name: The name of the feature to find
694 * @type: The type of the feature to find
696 * Find a feature of the given name and type in the given plugin.
698 * Returns: a GstPluginFeature or NULL if the feature was not found.
701 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
704 GstPluginFeature *result = NULL;
705 GstTypeNameData data;
707 g_return_val_if_fail (name != NULL, NULL);
712 walk = gst_filter_run (plugin->features,
713 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
716 result = GST_PLUGIN_FEATURE (walk->data);
722 * gst_plugin_add_feature:
723 * @plugin: plugin to add feature to
724 * @feature: feature to add
726 * Add feature to the list of those provided by the plugin.
727 * There is a separate namespace for each plugin feature type.
728 * See #gst_plugin_get_feature_list
731 gst_plugin_add_feature (GstPlugin * plugin, GstPluginFeature * feature)
733 GstPluginFeature *oldfeature;
735 g_return_if_fail (plugin != NULL);
736 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
737 g_return_if_fail (feature != NULL);
739 oldfeature = gst_plugin_find_feature (plugin,
740 GST_PLUGIN_FEATURE_NAME (feature), G_OBJECT_TYPE (feature));
743 feature->manager = plugin;
744 plugin->features = g_list_prepend (plugin->features, feature);
745 plugin->numfeatures++;
750 * gst_plugin_get_feature_list:
751 * @plugin: the plugin to get the features from
753 * get a list of all the features that this plugin provides
755 * Returns: a GList of features, use g_list_free to free the list.
758 gst_plugin_get_feature_list (GstPlugin * plugin)
760 g_return_val_if_fail (plugin != NULL, NULL);
762 return g_list_copy (plugin->features);
767 * @name: name of plugin to load
769 * Load the named plugin.
771 * Returns: whether the plugin was loaded or not
774 gst_plugin_load (const gchar * name)
777 GError *error = NULL;
779 plugin = gst_registry_pool_find_plugin (name);
781 gst_plugin_load_file (plugin->filename, &error);
783 GST_WARNING ("load_plugin error: %s\n", error->message);
784 g_error_free (error);
790 GST_DEBUG ("Could not find %s in registry pool", name);
796 * @name: name of library to load
798 * Load the named library. Name should be given as
799 * "liblibrary.so".
801 * Returns: whether the library was loaded or not
804 gst_library_load (const gchar * name)
808 /* for now this is the same */
809 res = gst_plugin_load (name);