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>
37 #include "gst_private.h"
39 #include "gstplugin.h"
40 #include "gstversion.h"
41 #include "gstregistrypool.h"
43 #include "gstfilter.h"
46 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
48 static GModule *main_module = NULL;
49 static GList *_gst_plugin_static = NULL;
51 /* static variables for segfault handling of plugin loading */
52 static char *_gst_plugin_fault_handler_filename = NULL;
53 extern gboolean _gst_disable_segtrap; /* see gst.c */
56 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
59 /* list of valid licenses.
60 * One of these must be specified or the plugin won't be loaded
61 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
64 * GPL: http://www.gnu.org/copyleft/gpl.html
65 * LGPL: http://www.gnu.org/copyleft/lesser.html
66 * QPL: http://www.trolltech.com/licenses/qpl.html
68 static gchar *valid_licenses[] = {
69 "LGPL", /* GNU Lesser General Public License */
70 "GPL", /* GNU General Public License */
71 "QPL", /* Trolltech Qt Public License */
72 "GPL/QPL", /* Combi-license of GPL + QPL */
73 GST_LICENSE_UNKNOWN, /* some other license */
77 static void gst_plugin_desc_copy (GstPluginDesc * dest,
78 const GstPluginDesc * src);
80 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
81 GModule * module, GstPluginDesc * desc);
84 gst_plugin_copy (GstPlugin * plugin)
86 return g_memdup (plugin, sizeof (*plugin));
90 gst_plugin_get_type (void)
92 static GType plugin_type;
94 if (plugin_type == 0) {
95 plugin_type = g_boxed_type_register_static ("GstPlugin",
96 (GBoxedCopyFunc) gst_plugin_copy, g_free);
103 gst_plugin_error_quark (void)
105 static GQuark quark = 0;
108 quark = g_quark_from_static_string ("gst_plugin_error");
112 /* this function can be called in the GCC constructor extension, before
113 * the _gst_plugin_initialize() was called. In that case, we store the
114 * plugin description in a list to initialize it when we open the main
116 * When the main module is known, we can register the plugin right away.
119 _gst_plugin_register_static (GstPluginDesc * desc)
121 if (main_module == NULL) {
123 GST_LOG ("queueing static plugin \"%s\" for loading later on",
125 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
130 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
131 plugin = g_new0 (GstPlugin, 1);
132 if (gst_plugin_register_func (plugin, main_module, desc)) {
134 GST_INFO ("loaded static plugin \"%s\"", desc->name);
135 gst_registry_pool_add_plugin (plugin);
141 _gst_plugin_initialize (void)
143 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
145 /* now register all static plugins */
146 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
150 /* this function could be extended to check if the plugin license matches the
151 * applications license (would require the app to register its license somehow).
152 * We'll wait for someone who's interested in it to code it :)
155 gst_plugin_check_license (const gchar * license)
157 gchar **check_license = valid_licenses;
159 g_assert (check_license);
161 while (*check_license) {
162 if (strcmp (license, *check_license) == 0)
170 gst_plugin_check_version (gint major, gint minor)
172 /* return NULL if the major and minor version numbers are not compatible */
174 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
181 gst_plugin_register_func (GstPlugin * plugin, GModule * module,
182 GstPluginDesc * desc)
184 g_assert (plugin->module == NULL);
186 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
188 GST_INFO ("plugin \"%s\" has incompatible version, not loading",
193 if (!desc->license || !desc->description || !desc->source ||
194 !desc->package || !desc->origin) {
196 GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
201 if (!gst_plugin_check_license (desc->license)) {
203 GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
204 plugin->filename, desc->license);
209 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
211 gst_plugin_desc_copy (&plugin->desc, desc);
212 plugin->module = module;
214 if (!((desc->plugin_init) (plugin))) {
216 GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
217 plugin->module = NULL;
222 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
229 * _gst_plugin_fault_handler_restore:
230 * segfault handler restorer
233 _gst_plugin_fault_handler_restore (void)
235 struct sigaction action;
237 memset (&action, 0, sizeof (action));
238 action.sa_handler = SIG_DFL;
240 sigaction (SIGSEGV, &action, NULL);
244 * _gst_plugin_fault_handler_sighandler:
245 * segfault handler implementation
248 _gst_plugin_fault_handler_sighandler (int signum)
250 /* We need to restore the fault handler or we'll keep getting it */
251 _gst_plugin_fault_handler_restore ();
255 g_print ("\nERROR: ");
256 g_print ("Caught a segmentation fault while loading plugin file:\n");
257 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
258 g_print ("Please either:\n");
259 g_print ("- remove it and restart.\n");
260 g_print ("- run with --gst-disable-segtrap and debug.\n");
264 g_print ("Caught unhandled signal on plugin loading\n");
270 * _gst_plugin_fault_handler_setup:
271 * sets up the segfault handler
274 _gst_plugin_fault_handler_setup (void)
276 struct sigaction action;
278 /* if asked to leave segfaults alone, just return */
279 if (_gst_disable_segtrap)
282 if (_gst_plugin_fault_handler_is_setup)
285 memset (&action, 0, sizeof (action));
286 action.sa_handler = _gst_plugin_fault_handler_sighandler;
288 sigaction (SIGSEGV, &action, NULL);
292 _gst_plugin_fault_handler_restore (void)
297 _gst_plugin_fault_handler_setup (void)
302 static void _gst_plugin_fault_handler_setup ();
305 * gst_plugin_check_module:
306 * @module: GModule handle to check for pluginness
307 * @error: pointer to a NULL-valued GError
308 * @pptr: pointer to a gpointer used to return the gst_plugin_desc symbol
311 * Checks if the given module is a GStreamer plugin
313 * Returns: TRUE if the given module is a GStreamer plugin
316 gst_plugin_check_module (GModule * module, const char *filename,
317 GError ** error, gpointer * pptr)
324 if (module == NULL) {
325 GST_DEBUG ("Error loading plugin %s, reason: %s", filename,
327 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
328 "Error loading plugin %s, reason: %s", filename, g_module_error ());
332 if (!g_module_symbol (module, "gst_plugin_desc", pptr)) {
333 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
336 GST_PLUGIN_ERROR_MODULE,
337 "Could not find plugin entry point in \"%s\"", filename);
345 * gst_plugin_check_file:
346 * @filename: the plugin filename to check for pluginness
347 * @error: pointer to a NULL-valued GError
349 * Checks if the given path represents a GStreamer plugin.
351 * Returns: TRUE if the given path is a GStreamer plugin.
354 gst_plugin_check_file (const gchar * filename, GError ** error)
357 struct stat file_status;
360 g_return_val_if_fail (filename != NULL, FALSE);
362 if (g_module_supported () == FALSE) {
365 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
369 if (stat (filename, &file_status)) {
372 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s\n", filename,
377 module = g_module_open (filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
378 check = gst_plugin_check_module (module, filename, error, NULL);
379 g_module_close (module);
381 GST_INFO ("file \"%s\" %s look like a gst plugin", filename,
382 check ? "does" : "doesn't");
388 * gst_plugin_load_file:
389 * @filename: the plugin filename to load
390 * @error: pointer to a NULL-valued GError
392 * Loads the given plugin.
394 * Returns: a new GstPlugin or NULL, if an error occurred.
397 gst_plugin_load_file (const gchar * filename, GError ** error)
402 gboolean free_plugin;
405 g_return_val_if_fail (filename != NULL, NULL);
407 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
410 module = g_module_open (filename, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
412 /* handle module == NULL case */
413 if (!gst_plugin_check_module (module, filename, error, &ptr))
416 desc = (GstPluginDesc *) ptr;
418 plugin = gst_registry_pool_find_plugin (desc->name);
421 plugin = g_new0 (GstPlugin, 1);
422 plugin->filename = g_strdup (filename);
423 GST_DEBUG ("created new GstPlugin %p for file \"%s\"", plugin, filename);
426 if (gst_plugin_is_loaded (plugin)) {
427 g_module_close (module);
428 if (plugin->filename && strcmp (plugin->filename, filename) != 0) {
430 ("plugin %p from file \"%s\" with same name %s is already "
431 "loaded, aborting loading of \"%s\"", plugin, plugin->filename,
432 plugin->desc.name, filename);
433 g_set_error (error, GST_PLUGIN_ERROR,
434 GST_PLUGIN_ERROR_NAME_MISMATCH,
435 "plugin %p from file \"%s\" with same name %s is already "
436 "loaded, aborting loading of \"%s\"", plugin, plugin->filename,
437 plugin->desc.name, filename);
442 GST_LOG ("Plugin %p for file \"%s\" already loaded, returning it now",
447 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
450 if (g_module_symbol (module, "plugin_init", &ptr)) {
452 ("plugin %p from file \"%s\" exports a symbol named plugin_init\n",
453 plugin, plugin->filename);
454 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_NAME_MISMATCH,
455 "plugin \"%s\" exports a symbol named plugin_init", desc->name);
458 /* this is where we load the actual .so, so let's trap SIGSEGV */
459 _gst_plugin_fault_handler_setup ();
460 _gst_plugin_fault_handler_filename = plugin->filename;
462 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
465 if (gst_plugin_register_func (plugin, module, desc)) {
466 /* remove signal handler */
467 _gst_plugin_fault_handler_restore ();
468 _gst_plugin_fault_handler_filename = NULL;
469 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
472 /* remove signal handler */
473 _gst_plugin_fault_handler_restore ();
474 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
478 GST_PLUGIN_ERROR_MODULE,
479 "gst_plugin_register_func failed for plugin \"%s\"", filename);
480 g_module_close (module);
488 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
490 dest->major_version = src->major_version;
491 dest->minor_version = src->minor_version;
493 dest->name = g_strdup (src->name);
494 g_free (dest->description);
495 dest->description = g_strdup (src->description);
496 dest->plugin_init = src->plugin_init;
497 dest->plugin_exit = src->plugin_exit;
498 g_free (dest->version);
499 dest->version = g_strdup (src->version);
500 g_free (dest->license);
501 dest->license = g_strdup (src->license);
502 g_free (dest->source);
503 dest->source = g_strdup (src->source);
504 g_free (dest->package);
505 dest->package = g_strdup (src->package);
506 g_free (dest->origin);
507 dest->origin = g_strdup (src->origin);
513 gst_plugin_desc_free (GstPluginDesc * desc)
516 g_free (desc->description);
517 g_free (desc->version);
518 g_free (desc->license);
519 g_free (desc->source);
520 g_free (desc->package);
521 g_free (desc->origin);
523 memset (desc, 0, sizeof (GstPluginDesc));
527 * gst_plugin_unload_plugin:
528 * @plugin: The plugin to unload
530 * Unload the given plugin.
532 * Returns: whether or not the plugin unloaded
535 gst_plugin_unload_plugin (GstPlugin * plugin)
537 g_return_val_if_fail (plugin != NULL, FALSE);
542 if (g_module_close (plugin->module)) {
543 plugin->module = NULL;
544 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" unloaded",
548 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "failed to unload plugin \"%s\"",
555 * gst_plugin_get_name:
556 * @plugin: plugin to get the name of
558 * Get the short name of the plugin
560 * Returns: the name of the plugin
563 gst_plugin_get_name (GstPlugin * plugin)
565 g_return_val_if_fail (plugin != NULL, NULL);
567 return plugin->desc.name;
571 * gst_plugin_get_description:
572 * @plugin: plugin to get long name of
574 * Get the long descriptive name of the plugin
576 * Returns: the long name of the plugin
578 G_CONST_RETURN gchar *
579 gst_plugin_get_description (GstPlugin * plugin)
581 g_return_val_if_fail (plugin != NULL, NULL);
583 return plugin->desc.description;
587 * gst_plugin_get_filename:
588 * @plugin: plugin to get the filename of
590 * get the filename of the plugin
592 * Returns: the filename of the plugin
594 G_CONST_RETURN gchar *
595 gst_plugin_get_filename (GstPlugin * plugin)
597 g_return_val_if_fail (plugin != NULL, NULL);
599 return plugin->filename;
603 * gst_plugin_get_version:
604 * @plugin: plugin to get the version of
606 * get the version of the plugin
608 * Returns: the version of the plugin
610 G_CONST_RETURN gchar *
611 gst_plugin_get_version (GstPlugin * plugin)
613 g_return_val_if_fail (plugin != NULL, NULL);
615 return plugin->desc.version;
619 * gst_plugin_get_license:
620 * @plugin: plugin to get the license of
622 * get the license of the plugin
624 * Returns: the license of the plugin
626 G_CONST_RETURN gchar *
627 gst_plugin_get_license (GstPlugin * plugin)
629 g_return_val_if_fail (plugin != NULL, NULL);
631 return plugin->desc.license;
635 * gst_plugin_get_source:
636 * @plugin: plugin to get the source of
638 * get the source module the plugin belongs to.
640 * Returns: the source of the plugin
642 G_CONST_RETURN gchar *
643 gst_plugin_get_source (GstPlugin * plugin)
645 g_return_val_if_fail (plugin != NULL, NULL);
647 return plugin->desc.source;
651 * gst_plugin_get_package:
652 * @plugin: plugin to get the package of
654 * get the package the plugin belongs to.
656 * Returns: the package of the plugin
658 G_CONST_RETURN gchar *
659 gst_plugin_get_package (GstPlugin * plugin)
661 g_return_val_if_fail (plugin != NULL, NULL);
663 return plugin->desc.package;
667 * gst_plugin_get_origin:
668 * @plugin: plugin to get the origin of
670 * get the URL where the plugin comes from
672 * Returns: the origin of the plugin
674 G_CONST_RETURN gchar *
675 gst_plugin_get_origin (GstPlugin * plugin)
677 g_return_val_if_fail (plugin != NULL, NULL);
679 return plugin->desc.origin;
683 * gst_plugin_get_module:
684 * @plugin: plugin to query
686 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
689 * Returns: module belonging to the plugin or NULL if the plugin isn't
693 gst_plugin_get_module (GstPlugin * plugin)
695 g_return_val_if_fail (plugin != NULL, NULL);
697 return plugin->module;
701 * gst_plugin_is_loaded:
702 * @plugin: plugin to query
704 * queries if the plugin is loaded into memory
706 * Returns: TRUE is loaded, FALSE otherwise
709 gst_plugin_is_loaded (GstPlugin * plugin)
711 g_return_val_if_fail (plugin != NULL, FALSE);
713 return (plugin->module != NULL);
717 * gst_plugin_feature_list:
718 * @plugin: plugin to query
719 * @filter: the filter to use
720 * @first: only return first match
721 * @user_data: user data passed to the filter function
723 * Runs a filter against all plugin features and returns a GList with
724 * the results. If the first flag is set, only the first match is
725 * returned (as a list with a single object).
727 * Returns: a GList of features, g_list_free after use.
730 gst_plugin_feature_filter (GstPlugin * plugin,
731 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
733 return gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
739 GstPluginFeatureFilter filter;
747 _feature_filter (GstPlugin * plugin, gpointer user_data)
750 FeatureFilterData *data = (FeatureFilterData *) user_data;
753 gst_plugin_feature_filter (plugin, data->filter, data->first,
756 data->result = g_list_concat (data->result, result);
763 * gst_plugin_list_feature_filter:
764 * @list: a #GList of plugins to query
765 * @filter: the filter function to use
766 * @first: only return first match
767 * @user_data: user data passed to the filter function
769 * Runs a filter against all plugin features of the plugins in the given
770 * list and returns a GList with the results.
771 * If the first flag is set, only the first match is
772 * returned (as a list with a single object).
774 * Returns: a GList of features, g_list_free after use.
777 gst_plugin_list_feature_filter (GList * list,
778 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
780 FeatureFilterData data;
783 data.filter = filter;
785 data.user_data = user_data;
788 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
789 g_list_free (result);
795 * gst_plugin_name_filter:
796 * @plugin: the plugin to check
797 * @name: the name of the plugin
799 * A standard filter that returns TRUE when the plugin is of the
802 * Returns: TRUE if the plugin is of the given name.
805 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
807 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
811 * gst_plugin_find_feature:
812 * @plugin: plugin to get the feature from
813 * @name: The name of the feature to find
814 * @type: The type of the feature to find
816 * Find a feature of the given name and type in the given plugin.
818 * Returns: a GstPluginFeature or NULL if the feature was not found.
821 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
824 GstPluginFeature *result = NULL;
825 GstTypeNameData data;
827 g_return_val_if_fail (name != NULL, NULL);
832 walk = gst_filter_run (plugin->features,
833 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
836 result = GST_PLUGIN_FEATURE (walk->data);
842 * gst_plugin_add_feature:
843 * @plugin: plugin to add feature to
844 * @feature: feature to add
846 * Add feature to the list of those provided by the plugin.
847 * There is a separate namespace for each plugin feature type.
848 * See #gst_plugin_get_feature_list
851 gst_plugin_add_feature (GstPlugin * plugin, GstPluginFeature * feature)
853 GstPluginFeature *oldfeature;
855 /* FIXME 0.9: get reference counting somewhat right in here,
856 * GstPluginFeatures should probably be GstObjects that are sinked when
857 * adding them to a plugin */
858 g_return_if_fail (plugin != NULL);
859 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
860 g_return_if_fail (feature != NULL);
862 oldfeature = gst_plugin_find_feature (plugin,
863 GST_PLUGIN_FEATURE_NAME (feature), G_OBJECT_TYPE (feature));
865 if (oldfeature == feature) {
866 GST_WARNING ("feature %s has already been added",
867 GST_PLUGIN_FEATURE_NAME (feature));
868 /* g_object_unref (feature); */
869 } else if (oldfeature) {
870 GST_WARNING ("feature %s already present in plugin",
871 GST_PLUGIN_FEATURE_NAME (feature));
872 /* g_object_unref (feature); */
874 feature->manager = plugin;
875 plugin->features = g_list_prepend (plugin->features, feature);
876 plugin->numfeatures++;
881 * gst_plugin_get_feature_list:
882 * @plugin: the plugin to get the features from
884 * get a list of all the features that this plugin provides
886 * Returns: a GList of features, use g_list_free to free the list.
889 gst_plugin_get_feature_list (GstPlugin * plugin)
891 g_return_val_if_fail (plugin != NULL, NULL);
893 return g_list_copy (plugin->features);
898 * @name: name of plugin to load
900 * Load the named plugin.
902 * Returns: whether the plugin was loaded or not
905 gst_plugin_load (const gchar * name)
908 GError *error = NULL;
910 plugin = gst_registry_pool_find_plugin (name);
912 plugin = gst_plugin_load_file (plugin->filename, &error);
914 GST_WARNING ("load_plugin error: %s\n", error->message);
915 g_error_free (error);
921 GST_DEBUG ("Could not find %s in registry pool", name);