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.
25 * @short_description: Container for features loaded from a shared object module
26 * @see_also: #GstPluginFeature, #GstElementFactory
28 * GStreamer is extensible, so #GstElement instances can be loaded at runtime.
29 * A plugin system can provide one or more of the basic
30 * <application>GStreamer</application> #GstPluginFeature subclasses.
32 * A plugin should export a symbol <symbol>plugin_desc</symbol> that is a
33 * struct of type #GstPluginDesc.
34 * the plugin loader will check the version of the core library the plugin was
35 * linked against and will create a new #GstPlugin. It will then call the
36 * #GstPluginInitFunc function that was provided in the plugin_desc.
38 * Once you have a handle to a #GstPlugin (e.g. from the #GstRegistryPool), you
39 * can add any object that subclasses #GstPluginFeature.
41 * Use gst_plugin_find_feature() and gst_plugin_get_feature_list() to find
42 * features in a plugin.
44 * Usually plugins are always automaticlly loaded so you don't need to call
45 * gst_plugin_load() explicitly to bring it into memory. There are options to
46 * statically link plugins to an app or even use GStreamer without a plugin
47 * repository in which case gst_plugin_load() can be needed to bring the plugin
54 #include <glib/gstdio.h>
55 #include <sys/types.h>
65 #include "gst_private.h"
67 #include "gstplugin.h"
68 #include "gstversion.h"
70 #include "gstfilter.h"
71 #include "gstregistry.h"
72 #include "gstmacros.h"
75 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
77 static GList *_gst_plugin_static = NULL;
78 static gboolean _gst_plugin_inited;
80 /* static variables for segfault handling of plugin loading */
81 static char *_gst_plugin_fault_handler_filename = NULL;
82 extern gboolean _gst_disable_segtrap; /* see gst.c */
85 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
88 /* list of valid licenses.
89 * One of these must be specified or the plugin won't be loaded
90 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
93 * GPL: http://www.gnu.org/copyleft/gpl.html
94 * LGPL: http://www.gnu.org/copyleft/lesser.html
95 * QPL: http://www.trolltech.com/licenses/qpl.html
96 * MPL: http://www.opensource.org/licenses/mozilla1.1.php
98 static const gchar *valid_licenses[] = {
99 "LGPL", /* GNU Lesser General Public License */
100 "GPL", /* GNU General Public License */
101 "QPL", /* Trolltech Qt Public License */
102 "GPL/QPL", /* Combi-license of GPL + QPL */
103 "MPL", /* MPL 1.1 license */
104 "Proprietary", /* Proprietary license */
105 GST_LICENSE_UNKNOWN, /* some other license */
109 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
110 GstPluginDesc * desc);
112 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src);
113 static void gst_plugin_desc_free (GstPluginDesc * desc);
116 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
119 gst_plugin_init (GstPlugin * plugin)
125 gst_plugin_finalize (GObject * object)
127 GstPlugin *plugin = GST_PLUGIN_CAST (object);
128 GstRegistry *registry = gst_registry_get_default ();
131 GST_DEBUG ("finalizing plugin %p", plugin);
132 for (g = registry->plugins; g; g = g->next) {
133 if (g->data == (gpointer) plugin) {
134 g_warning ("removing plugin that is still in registry");
137 g_free (plugin->filename);
138 g_free (plugin->basename);
139 gst_plugin_desc_free (&plugin->desc);
141 G_OBJECT_CLASS (gst_plugin_parent_class)->finalize (object);
145 gst_plugin_class_init (GstPluginClass * klass)
147 G_OBJECT_CLASS (klass)->finalize = GST_DEBUG_FUNCPTR (gst_plugin_finalize);
151 gst_plugin_error_quark (void)
153 static GQuark quark = 0;
156 quark = g_quark_from_static_string ("gst_plugin_error");
160 /* this function can be called in the GCC constructor extension, before
161 * the _gst_plugin_initialize() was called. In that case, we store the
162 * plugin description in a list to initialize it when we open the main
164 * When the main module is known, we can register the plugin right away.
167 _gst_plugin_register_static (GstPluginDesc * desc)
169 if (!_gst_plugin_inited) {
171 GST_LOG ("queueing static plugin \"%s\" for loading later on",
173 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
178 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
179 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
180 if (gst_plugin_register_func (plugin, desc)) {
182 GST_INFO ("loaded static plugin \"%s\"", desc->name);
183 gst_default_registry_add_plugin (plugin);
189 _gst_plugin_initialize (void)
191 _gst_plugin_inited = TRUE;
193 /* now register all static plugins */
194 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
198 /* this function could be extended to check if the plugin license matches the
199 * applications license (would require the app to register its license somehow).
200 * We'll wait for someone who's interested in it to code it :)
203 gst_plugin_check_license (const gchar * license)
205 const gchar **check_license = valid_licenses;
207 g_assert (check_license);
209 while (*check_license) {
210 if (strcmp (license, *check_license) == 0)
218 gst_plugin_check_version (gint major, gint minor)
220 /* return NULL if the major and minor version numbers are not compatible */
222 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
229 gst_plugin_register_func (GstPlugin * plugin, GstPluginDesc * desc)
231 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
233 GST_WARNING ("plugin \"%s\" has incompatible version, not loading",
238 if (!desc->license || !desc->description || !desc->source ||
239 !desc->package || !desc->origin) {
241 GST_WARNING ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
246 if (!gst_plugin_check_license (desc->license)) {
248 GST_WARNING ("plugin \"%s\" has invalid license \"%s\", not loading",
249 plugin->filename, desc->license);
254 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
256 gst_plugin_desc_copy (&plugin->desc, desc);
258 if (!((desc->plugin_init) (plugin))) {
260 GST_WARNING ("plugin \"%s\" failed to initialise", plugin->filename);
261 plugin->module = NULL;
266 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
271 #ifdef HAVE_SIGACTION
272 static struct sigaction oldaction;
275 * _gst_plugin_fault_handler_restore:
276 * segfault handler restorer
279 _gst_plugin_fault_handler_restore (void)
281 if (!_gst_plugin_fault_handler_is_setup)
284 _gst_plugin_fault_handler_is_setup = FALSE;
286 sigaction (SIGSEGV, &oldaction, NULL);
290 * _gst_plugin_fault_handler_sighandler:
291 * segfault handler implementation
294 _gst_plugin_fault_handler_sighandler (int signum)
296 /* We need to restore the fault handler or we'll keep getting it */
297 _gst_plugin_fault_handler_restore ();
301 g_print ("\nERROR: ");
302 g_print ("Caught a segmentation fault while loading plugin file:\n");
303 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
304 g_print ("Please either:\n");
305 g_print ("- remove it and restart.\n");
306 g_print ("- run with --gst-disable-segtrap and debug.\n");
310 g_print ("Caught unhandled signal on plugin loading\n");
316 * _gst_plugin_fault_handler_setup:
317 * sets up the segfault handler
320 _gst_plugin_fault_handler_setup (void)
322 struct sigaction action;
324 /* if asked to leave segfaults alone, just return */
325 if (_gst_disable_segtrap)
328 if (_gst_plugin_fault_handler_is_setup)
331 _gst_plugin_fault_handler_is_setup = TRUE;
333 memset (&action, 0, sizeof (action));
334 action.sa_handler = _gst_plugin_fault_handler_sighandler;
336 sigaction (SIGSEGV, &action, &oldaction);
340 _gst_plugin_fault_handler_restore (void)
345 _gst_plugin_fault_handler_setup (void)
350 static void _gst_plugin_fault_handler_setup ();
352 GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
355 * gst_plugin_load_file:
356 * @filename: the plugin filename to load
357 * @error: pointer to a NULL-valued GError
359 * Loads the given plugin and refs it. Caller needs to unref after use.
361 * Returns: a reference to the existing loaded GstPlugin, a reference to the
362 * newly-loaded GstPlugin, or NULL if an error occurred.
365 gst_plugin_load_file (const gchar * filename, GError ** error)
371 struct stat file_status;
372 GstRegistry *registry;
374 g_return_val_if_fail (filename != NULL, NULL);
376 registry = gst_registry_get_default ();
377 g_static_mutex_lock (&gst_plugin_loading_mutex);
379 plugin = gst_registry_lookup (registry, filename);
381 if (plugin->module) {
382 g_static_mutex_unlock (&gst_plugin_loading_mutex);
385 gst_object_unref (plugin);
390 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
393 if (g_module_supported () == FALSE) {
394 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
397 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
401 if (g_stat (filename, &file_status)) {
402 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
405 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
410 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
411 if (module == NULL) {
412 GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
415 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
417 /* If we failed to open the shared object, then it's probably because a
418 * plugin is linked against the wrong libraries. Print out an easy-to-see
419 * message in this case. */
420 g_warning ("Failed to load plugin: %s", g_module_error ());
424 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
426 plugin->module = module;
427 plugin->filename = g_strdup (filename);
428 plugin->basename = g_path_get_basename (filename);
429 plugin->file_mtime = file_status.st_mtime;
430 plugin->file_size = file_status.st_size;
432 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
434 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
437 GST_PLUGIN_ERROR_MODULE,
438 "File \"%s\" is not a GStreamer plugin", filename);
439 g_module_close (module);
442 plugin->orig_desc = (GstPluginDesc *) ptr;
444 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
447 /* this is where we load the actual .so, so let's trap SIGSEGV */
448 _gst_plugin_fault_handler_setup ();
449 _gst_plugin_fault_handler_filename = plugin->filename;
451 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
454 if (!gst_plugin_register_func (plugin, plugin->orig_desc)) {
455 /* remove signal handler */
456 _gst_plugin_fault_handler_restore ();
457 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
461 GST_PLUGIN_ERROR_MODULE,
462 "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
464 g_module_close (module);
468 /* remove signal handler */
469 _gst_plugin_fault_handler_restore ();
470 _gst_plugin_fault_handler_filename = NULL;
471 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
473 gst_object_ref (plugin);
474 gst_default_registry_add_plugin (plugin);
476 g_static_mutex_unlock (&gst_plugin_loading_mutex);
482 gst_object_unref (plugin);
483 g_static_mutex_unlock (&gst_plugin_loading_mutex);
489 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
491 dest->major_version = src->major_version;
492 dest->minor_version = src->minor_version;
493 dest->name = g_strdup (src->name);
494 dest->description = g_strdup (src->description);
495 dest->plugin_init = src->plugin_init;
496 dest->version = g_strdup (src->version);
497 dest->license = g_strdup (src->license);
498 dest->source = g_strdup (src->source);
499 dest->package = g_strdup (src->package);
500 dest->origin = g_strdup (src->origin);
505 gst_plugin_desc_free (GstPluginDesc * desc)
508 g_free (desc->description);
509 g_free (desc->version);
510 g_free (desc->license);
511 g_free (desc->source);
512 g_free (desc->package);
513 g_free (desc->origin);
515 memset (desc, 0, sizeof (GstPluginDesc));
519 * gst_plugin_get_name:
520 * @plugin: plugin to get the name of
522 * Get the short name of the plugin
524 * Returns: the name of the plugin
527 gst_plugin_get_name (GstPlugin * plugin)
529 g_return_val_if_fail (plugin != NULL, NULL);
531 return plugin->desc.name;
535 * gst_plugin_get_description:
536 * @plugin: plugin to get long name of
538 * Get the long descriptive name of the plugin
540 * Returns: the long name of the plugin
542 G_CONST_RETURN gchar *
543 gst_plugin_get_description (GstPlugin * plugin)
545 g_return_val_if_fail (plugin != NULL, NULL);
547 return plugin->desc.description;
551 * gst_plugin_get_filename:
552 * @plugin: plugin to get the filename of
554 * get the filename of the plugin
556 * Returns: the filename of the plugin
558 G_CONST_RETURN gchar *
559 gst_plugin_get_filename (GstPlugin * plugin)
561 g_return_val_if_fail (plugin != NULL, NULL);
563 return plugin->filename;
567 * gst_plugin_get_version:
568 * @plugin: plugin to get the version of
570 * get the version of the plugin
572 * Returns: the version of the plugin
574 G_CONST_RETURN gchar *
575 gst_plugin_get_version (GstPlugin * plugin)
577 g_return_val_if_fail (plugin != NULL, NULL);
579 return plugin->desc.version;
583 * gst_plugin_get_license:
584 * @plugin: plugin to get the license of
586 * get the license of the plugin
588 * Returns: the license of the plugin
590 G_CONST_RETURN gchar *
591 gst_plugin_get_license (GstPlugin * plugin)
593 g_return_val_if_fail (plugin != NULL, NULL);
595 return plugin->desc.license;
599 * gst_plugin_get_source:
600 * @plugin: plugin to get the source of
602 * get the source module the plugin belongs to.
604 * Returns: the source of the plugin
606 G_CONST_RETURN gchar *
607 gst_plugin_get_source (GstPlugin * plugin)
609 g_return_val_if_fail (plugin != NULL, NULL);
611 return plugin->desc.source;
615 * gst_plugin_get_package:
616 * @plugin: plugin to get the package of
618 * get the package the plugin belongs to.
620 * Returns: the package of the plugin
622 G_CONST_RETURN gchar *
623 gst_plugin_get_package (GstPlugin * plugin)
625 g_return_val_if_fail (plugin != NULL, NULL);
627 return plugin->desc.package;
631 * gst_plugin_get_origin:
632 * @plugin: plugin to get the origin of
634 * get the URL where the plugin comes from
636 * Returns: the origin of the plugin
638 G_CONST_RETURN gchar *
639 gst_plugin_get_origin (GstPlugin * plugin)
641 g_return_val_if_fail (plugin != NULL, NULL);
643 return plugin->desc.origin;
647 * gst_plugin_get_module:
648 * @plugin: plugin to query
650 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
653 * Returns: module belonging to the plugin or NULL if the plugin isn't
657 gst_plugin_get_module (GstPlugin * plugin)
659 g_return_val_if_fail (plugin != NULL, NULL);
661 return plugin->module;
665 * gst_plugin_is_loaded:
666 * @plugin: plugin to query
668 * queries if the plugin is loaded into memory
670 * Returns: TRUE is loaded, FALSE otherwise
673 gst_plugin_is_loaded (GstPlugin * plugin)
675 g_return_val_if_fail (plugin != NULL, FALSE);
677 return (plugin->module != NULL || plugin->filename == NULL);
682 * gst_plugin_feature_list:
683 * @plugin: plugin to query
684 * @filter: the filter to use
685 * @first: only return first match
686 * @user_data: user data passed to the filter function
688 * Runs a filter against all plugin features and returns a GList with
689 * the results. If the first flag is set, only the first match is
690 * returned (as a list with a single object).
692 * Returns: a GList of features, g_list_free after use.
695 gst_plugin_feature_filter (GstPlugin * plugin,
696 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
701 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
703 for (g = list; g; g = g->next) {
704 gst_object_ref (plugin);
712 GstPluginFeatureFilter filter;
720 _feature_filter (GstPlugin * plugin, gpointer user_data)
723 FeatureFilterData *data = (FeatureFilterData *) user_data;
725 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
728 data->result = g_list_concat (data->result, result);
735 * gst_plugin_list_feature_filter:
736 * @list: a #GList of plugins to query
737 * @filter: the filter function to use
738 * @first: only return first match
739 * @user_data: user data passed to the filter function
741 * Runs a filter against all plugin features of the plugins in the given
742 * list and returns a GList with the results.
743 * If the first flag is set, only the first match is
744 * returned (as a list with a single object).
746 * Returns: a GList of features, g_list_free after use.
749 gst_plugin_list_feature_filter (GList * list,
750 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
752 FeatureFilterData data;
755 data.filter = filter;
757 data.user_data = user_data;
760 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
761 g_list_free (result);
768 * gst_plugin_name_filter:
769 * @plugin: the plugin to check
770 * @name: the name of the plugin
772 * A standard filter that returns TRUE when the plugin is of the
775 * Returns: TRUE if the plugin is of the given name.
778 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
780 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
785 * gst_plugin_find_feature:
786 * @plugin: plugin to get the feature from
787 * @name: The name of the feature to find
788 * @type: The type of the feature to find
790 * Find a feature of the given name and type in the given plugin.
792 * Returns: a GstPluginFeature or NULL if the feature was not found.
795 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
798 GstPluginFeature *result = NULL;
799 GstTypeNameData data;
801 g_return_val_if_fail (name != NULL, NULL);
806 walk = gst_filter_run (plugin->features,
807 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
810 result = GST_PLUGIN_FEATURE (walk->data);
812 gst_object_ref (result);
813 gst_plugin_feature_list_free (walk);
822 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
824 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
830 * gst_plugin_find_feature_by_name:
831 * @plugin: plugin to get the feature from
832 * @name: The name of the feature to find
834 * Find a feature of the given name in the given plugin.
836 * Returns: a GstPluginFeature or NULL if the feature was not found.
839 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
842 GstPluginFeature *result = NULL;
844 g_return_val_if_fail (name != NULL, NULL);
846 walk = gst_filter_run (plugin->features,
847 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
850 result = GST_PLUGIN_FEATURE (walk->data);
852 gst_object_ref (result);
853 gst_plugin_feature_list_free (walk);
861 * gst_plugin_load_by_name:
862 * @name: name of plugin to load
864 * Load the named plugin. Refs the plugin.
866 * Returns: A reference to a loaded plugin, or NULL on error.
869 gst_plugin_load_by_name (const gchar * name)
871 GstPlugin *plugin, *newplugin;
872 GError *error = NULL;
874 GST_DEBUG ("looking up plugin %s in default registry", name);
875 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
877 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
878 newplugin = gst_plugin_load_file (plugin->filename, &error);
879 gst_object_unref (plugin);
882 GST_WARNING ("load_plugin error: %s", error->message);
883 g_error_free (error);
886 /* newplugin was reffed by load_file */
890 GST_DEBUG ("Could not find plugin %s in registry", name);
896 * @plugin: plugin to load
898 * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
899 * untouched. The normal use pattern of this function goes like this:
902 * GstPlugin *loaded_plugin;
903 * loaded_plugin = gst_plugin_load (plugin);
904 * // presumably, we're no longer interested in the potentially-unloaded plugin
905 * gst_object_unref (plugin);
906 * plugin = loaded_plugin;
909 * Returns: A reference to a loaded plugin, or NULL on error.
912 gst_plugin_load (GstPlugin * plugin)
914 GError *error = NULL;
915 GstPlugin *newplugin;
917 if (gst_plugin_is_loaded (plugin)) {
921 if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
928 GST_WARNING ("load_plugin error: %s", error->message);
929 g_error_free (error);
935 * gst_plugin_list_free:
936 * @list: list of #GstPlugin
938 * Unrefs each member of @list, then frees the list.
941 gst_plugin_list_free (GList * list)
945 for (g = list; g; g = g->next) {
946 gst_object_unref (GST_PLUGIN_CAST (g->data));