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 "plugin %p from file \"%s\" with same name %s is already "
362 "loaded, aborting loading of \"%s\"", plugin, plugin->filename,
363 plugin->desc.name, filename);
368 GST_LOG ("Plugin %p for file \"%s\" already loaded, returning it now",
373 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
376 if (g_module_symbol (module, "plugin_init", &ptr)) {
378 ("plugin %p from file \"%s\" exports a symbol named plugin_init\n",
379 plugin, plugin->filename);
380 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_NAME_MISMATCH,
381 "plugin \"%s\" exports a symbol named plugin_init", desc->name);
384 /* this is where we load the actual .so, so let's trap SIGSEGV */
385 _gst_plugin_fault_handler_setup ();
386 _gst_plugin_fault_handler_filename = plugin->filename;
388 if (gst_plugin_register_func (plugin, module, desc)) {
389 /* remove signal handler */
390 _gst_plugin_fault_handler_restore ();
391 _gst_plugin_fault_handler_filename = NULL;
392 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
395 /* remove signal handler */
396 _gst_plugin_fault_handler_restore ();
397 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"",
402 GST_PLUGIN_ERROR_MODULE,
403 "gst_plugin_register_func failed for plugin \"%s\"", filename);
409 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
412 GST_PLUGIN_ERROR_MODULE,
413 "Could not find plugin entry point in \"%s\"", filename);
417 GST_DEBUG ("Error loading plugin %s, reason: %s\n", filename,
419 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
420 "Error loading plugin %s, reason: %s\n", filename, g_module_error ());
426 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
428 dest->major_version = src->major_version;
429 dest->minor_version = src->minor_version;
431 dest->name = g_strdup (src->name);
432 g_free (dest->description);
433 dest->description = g_strdup (src->description);
434 dest->plugin_init = src->plugin_init;
435 dest->plugin_exit = src->plugin_exit;
436 g_free (dest->version);
437 dest->version = g_strdup (src->version);
438 g_free (dest->license);
439 dest->license = g_strdup (src->license);
440 g_free (dest->package);
441 dest->package = g_strdup (src->package);
442 g_free (dest->origin);
443 dest->origin = g_strdup (src->origin);
449 gst_plugin_desc_free (GstPluginDesc * desc)
452 g_free (desc->description);
453 g_free (desc->version);
454 g_free (desc->license);
455 g_free (desc->package);
456 g_free (desc->origin);
458 memset (desc, 0, sizeof (GstPluginDesc));
462 * gst_plugin_unload_plugin:
463 * @plugin: The plugin to unload
465 * Unload the given plugin.
467 * Returns: whether or not the plugin unloaded
470 gst_plugin_unload_plugin (GstPlugin * plugin)
472 g_return_val_if_fail (plugin != NULL, FALSE);
477 if (g_module_close (plugin->module)) {
478 plugin->module = NULL;
479 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" unloaded",
483 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "failed to unload plugin \"%s\"",
490 * gst_plugin_get_name:
491 * @plugin: plugin to get the name of
493 * Get the short name of the plugin
495 * Returns: the name of the plugin
498 gst_plugin_get_name (GstPlugin * plugin)
500 g_return_val_if_fail (plugin != NULL, NULL);
502 return plugin->desc.name;
506 * gst_plugin_get_description:
507 * @plugin: plugin to get long name of
509 * Get the long descriptive name of the plugin
511 * Returns: the long name of the plugin
513 G_CONST_RETURN gchar *
514 gst_plugin_get_description (GstPlugin * plugin)
516 g_return_val_if_fail (plugin != NULL, NULL);
518 return plugin->desc.description;
522 * gst_plugin_get_filename:
523 * @plugin: plugin to get the filename of
525 * get the filename of the plugin
527 * Returns: the filename of the plugin
529 G_CONST_RETURN gchar *
530 gst_plugin_get_filename (GstPlugin * plugin)
532 g_return_val_if_fail (plugin != NULL, NULL);
534 return plugin->filename;
538 * gst_plugin_get_license:
539 * @plugin: plugin to get the license of
541 * get the license of the plugin
543 * Returns: the license of the plugin
545 G_CONST_RETURN gchar *
546 gst_plugin_get_license (GstPlugin * plugin)
548 g_return_val_if_fail (plugin != NULL, NULL);
550 return plugin->desc.license;
554 * gst_plugin_get_package:
555 * @plugin: plugin to get the package of
557 * get the package the plugin belongs to.
559 * Returns: the package of the plugin
561 G_CONST_RETURN gchar *
562 gst_plugin_get_package (GstPlugin * plugin)
564 g_return_val_if_fail (plugin != NULL, NULL);
566 return plugin->desc.package;
570 * gst_plugin_get_origin:
571 * @plugin: plugin to get the origin of
573 * get the URL where the plugin comes from
575 * Returns: the origin of the plugin
577 G_CONST_RETURN gchar *
578 gst_plugin_get_origin (GstPlugin * plugin)
580 g_return_val_if_fail (plugin != NULL, NULL);
582 return plugin->desc.origin;
586 * gst_plugin_get_module:
587 * @plugin: plugin to query
589 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
592 * Returns: module belonging to the plugin or NULL if the plugin isn't
596 gst_plugin_get_module (GstPlugin * plugin)
598 g_return_val_if_fail (plugin != NULL, FALSE);
600 return plugin->module;
604 * gst_plugin_is_loaded:
605 * @plugin: plugin to query
607 * queries if the plugin is loaded into memory
609 * Returns: TRUE is loaded, FALSE otherwise
612 gst_plugin_is_loaded (GstPlugin * plugin)
614 g_return_val_if_fail (plugin != NULL, FALSE);
616 return (plugin->module != NULL);
620 * gst_plugin_feature_list:
621 * @plugin: plugin to query
622 * @filter: the filter to use
623 * @first: only return first match
624 * @user_data: user data passed to the filter function
626 * Runs a filter against all plugin features and returns a GList with
627 * the results. If the first flag is set, only the first match is
628 * returned (as a list with a single object).
630 * Returns: a GList of features, g_list_free after use.
633 gst_plugin_feature_filter (GstPlugin * plugin,
634 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
636 return gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
642 GstPluginFeatureFilter filter;
650 _feature_filter (GstPlugin * plugin, gpointer user_data)
653 FeatureFilterData *data = (FeatureFilterData *) user_data;
656 gst_plugin_feature_filter (plugin, data->filter, data->first,
659 data->result = g_list_concat (data->result, result);
666 * gst_plugin_list_feature_list:
667 * @list: a list of plugins to query
668 * @filter: the filter to use
669 * @first: only return first match
670 * @user_data: user data passed to the filter function
672 * Runs a filter against all plugin features of the plugins in the given
673 * list and returns a GList with the results.
674 * 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_list_feature_filter (GList * list,
681 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
683 FeatureFilterData data;
686 data.filter = filter;
688 data.user_data = user_data;
691 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
692 g_list_free (result);
698 * gst_plugin_name_filter:
699 * @plugin: the plugin to check
700 * @name: the name of the plugin
702 * A standard filter that returns TRUE when the plugin is of the
705 * Returns: TRUE if the plugin is of the given name.
708 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
710 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
714 * gst_plugin_find_feature:
715 * @plugin: plugin to get the feature from
716 * @name: The name of the feature to find
717 * @type: The type of the feature to find
719 * Find a feature of the given name and type in the given plugin.
721 * Returns: a GstPluginFeature or NULL if the feature was not found.
724 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
727 GstPluginFeature *result = NULL;
728 GstTypeNameData data;
730 g_return_val_if_fail (name != NULL, NULL);
735 walk = gst_filter_run (plugin->features,
736 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
739 result = GST_PLUGIN_FEATURE (walk->data);
745 * gst_plugin_add_feature:
746 * @plugin: plugin to add feature to
747 * @feature: feature to add
749 * Add feature to the list of those provided by the plugin.
750 * There is a separate namespace for each plugin feature type.
751 * See #gst_plugin_get_feature_list
754 gst_plugin_add_feature (GstPlugin * plugin, GstPluginFeature * feature)
756 GstPluginFeature *oldfeature;
758 g_return_if_fail (plugin != NULL);
759 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
760 g_return_if_fail (feature != NULL);
762 oldfeature = gst_plugin_find_feature (plugin,
763 GST_PLUGIN_FEATURE_NAME (feature), G_OBJECT_TYPE (feature));
766 feature->manager = plugin;
767 plugin->features = g_list_prepend (plugin->features, feature);
768 plugin->numfeatures++;
773 * gst_plugin_get_feature_list:
774 * @plugin: the plugin to get the features from
776 * get a list of all the features that this plugin provides
778 * Returns: a GList of features, use g_list_free to free the list.
781 gst_plugin_get_feature_list (GstPlugin * plugin)
783 g_return_val_if_fail (plugin != NULL, NULL);
785 return g_list_copy (plugin->features);
790 * @name: name of plugin to load
792 * Load the named plugin.
794 * Returns: whether the plugin was loaded or not
797 gst_plugin_load (const gchar * name)
800 GError *error = NULL;
802 plugin = gst_registry_pool_find_plugin (name);
804 gst_plugin_load_file (plugin->filename, &error);
806 GST_WARNING ("load_plugin error: %s\n", error->message);
807 g_error_free (error);
813 GST_DEBUG ("Could not find %s in registry pool", name);
819 * @name: name of library to load
821 * Load the named library. Name should be given as
822 * "liblibrary.so".
824 * Returns: whether the library was loaded or not
827 gst_library_load (const gchar * name)
831 /* for now this is the same */
832 res = gst_plugin_load (name);