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 <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 GModule *main_module = NULL;
78 static GList *_gst_plugin_static = NULL;
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
97 static gchar *valid_licenses[] = {
98 "LGPL", /* GNU Lesser General Public License */
99 "GPL", /* GNU General Public License */
100 "QPL", /* Trolltech Qt Public License */
101 "GPL/QPL", /* Combi-license of GPL + QPL */
102 GST_LICENSE_UNKNOWN, /* some other license */
106 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
107 GModule * module, GstPluginDesc * desc);
109 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src);
110 static void gst_plugin_desc_free (GstPluginDesc * desc);
113 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
115 static GstObjectClass *parent_class = NULL;
118 gst_plugin_init (GstPlugin * plugin)
124 gst_plugin_finalize (GObject * object)
126 GstPlugin *plugin = GST_PLUGIN (object);
127 GstRegistry *registry = gst_registry_get_default ();
130 GST_DEBUG ("finalizing plugin %p", plugin);
131 for (g = registry->plugins; g; g = g->next) {
132 if (g->data == (gpointer) plugin) {
133 g_warning ("removing plugin that is still in registry");
136 g_free (plugin->filename);
137 g_free (plugin->basename);
138 gst_plugin_desc_free (&plugin->desc);
140 G_OBJECT_CLASS (parent_class)->finalize (object);
144 gst_plugin_class_init (GstPluginClass * klass)
146 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
148 G_OBJECT_CLASS (klass)->finalize = GST_DEBUG_FUNCPTR (gst_plugin_finalize);
152 gst_plugin_error_quark (void)
154 static GQuark quark = 0;
157 quark = g_quark_from_static_string ("gst_plugin_error");
161 /* this function can be called in the GCC constructor extension, before
162 * the _gst_plugin_initialize() was called. In that case, we store the
163 * plugin description in a list to initialize it when we open the main
165 * When the main module is known, we can register the plugin right away.
168 _gst_plugin_register_static (GstPluginDesc * desc)
170 if (main_module == NULL) {
172 GST_LOG ("queueing static plugin \"%s\" for loading later on",
174 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
179 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
180 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
181 if (gst_plugin_register_func (plugin, main_module, desc)) {
183 GST_INFO ("loaded static plugin \"%s\"", desc->name);
184 gst_default_registry_add_plugin (plugin);
190 _gst_plugin_initialize (void)
192 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
194 /* now register all static plugins */
195 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
199 /* this function could be extended to check if the plugin license matches the
200 * applications license (would require the app to register its license somehow).
201 * We'll wait for someone who's interested in it to code it :)
204 gst_plugin_check_license (const gchar * license)
206 gchar **check_license = valid_licenses;
208 g_assert (check_license);
210 while (*check_license) {
211 if (strcmp (license, *check_license) == 0)
219 gst_plugin_check_version (gint major, gint minor)
221 /* return NULL if the major and minor version numbers are not compatible */
223 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
230 gst_plugin_register_func (GstPlugin * plugin, GModule * module,
231 GstPluginDesc * desc)
233 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
235 GST_INFO ("plugin \"%s\" has incompatible version, not loading",
240 if (!desc->license || !desc->description || !desc->source ||
241 !desc->package || !desc->origin) {
243 GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
248 if (!gst_plugin_check_license (desc->license)) {
250 GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
251 plugin->filename, desc->license);
256 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
258 gst_plugin_desc_copy (&plugin->desc, desc);
260 if (!((desc->plugin_init) (plugin))) {
262 GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
263 plugin->module = NULL;
268 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
275 * _gst_plugin_fault_handler_restore:
276 * segfault handler restorer
279 _gst_plugin_fault_handler_restore (void)
281 struct sigaction action;
283 memset (&action, 0, sizeof (action));
284 action.sa_handler = SIG_DFL;
286 sigaction (SIGSEGV, &action, 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 memset (&action, 0, sizeof (action));
332 action.sa_handler = _gst_plugin_fault_handler_sighandler;
334 sigaction (SIGSEGV, &action, NULL);
338 _gst_plugin_fault_handler_restore (void)
343 _gst_plugin_fault_handler_setup (void)
348 static void _gst_plugin_fault_handler_setup ();
350 GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
353 * gst_plugin_load_file:
354 * @filename: the plugin filename to load
355 * @error: pointer to a NULL-valued GError
357 * Loads the given plugin and refs it. Caller needs to unref after use.
359 * Returns: a reference to the existing loaded GstPlugin, a reference to the
360 * newly-loaded GstPlugin, or NULL if an error occurred.
363 gst_plugin_load_file (const gchar * filename, GError ** error)
369 struct stat file_status;
370 GstRegistry *registry;
372 g_return_val_if_fail (filename != NULL, NULL);
374 registry = gst_registry_get_default ();
375 g_static_mutex_lock (&gst_plugin_loading_mutex);
377 plugin = gst_registry_lookup (registry, filename);
379 if (plugin->module) {
380 g_static_mutex_unlock (&gst_plugin_loading_mutex);
383 gst_object_unref (plugin);
388 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
391 if (g_module_supported () == FALSE) {
392 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
395 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
399 if (stat (filename, &file_status)) {
400 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
403 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s\n", filename,
408 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
409 if (module == NULL) {
410 GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
413 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed");
417 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
419 plugin->module = module;
420 plugin->filename = strdup (filename);
421 plugin->basename = g_path_get_basename (filename);
422 plugin->file_mtime = file_status.st_mtime;
423 plugin->file_size = file_status.st_size;
425 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
427 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
430 GST_PLUGIN_ERROR_MODULE,
431 "File \"%s\" is not a GStreamer plugin", filename);
434 plugin->orig_desc = (GstPluginDesc *) ptr;
436 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
439 /* this is where we load the actual .so, so let's trap SIGSEGV */
440 _gst_plugin_fault_handler_setup ();
441 _gst_plugin_fault_handler_filename = plugin->filename;
443 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
446 if (!gst_plugin_register_func (plugin, module, plugin->orig_desc)) {
447 /* remove signal handler */
448 _gst_plugin_fault_handler_restore ();
449 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
453 GST_PLUGIN_ERROR_MODULE,
454 "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
456 g_module_close (module);
460 /* remove signal handler */
461 _gst_plugin_fault_handler_restore ();
462 _gst_plugin_fault_handler_filename = NULL;
463 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
465 gst_object_ref (plugin);
466 gst_default_registry_add_plugin (plugin);
468 g_static_mutex_unlock (&gst_plugin_loading_mutex);
472 gst_object_unref (plugin);
473 g_static_mutex_unlock (&gst_plugin_loading_mutex);
478 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
480 dest->major_version = src->major_version;
481 dest->minor_version = src->minor_version;
482 dest->name = g_strdup (src->name);
483 dest->description = g_strdup (src->description);
484 dest->plugin_init = src->plugin_init;
485 dest->version = g_strdup (src->version);
486 dest->license = g_strdup (src->license);
487 dest->source = g_strdup (src->source);
488 dest->package = g_strdup (src->package);
489 dest->origin = g_strdup (src->origin);
494 gst_plugin_desc_free (GstPluginDesc * desc)
497 g_free (desc->description);
498 g_free (desc->version);
499 g_free (desc->license);
500 g_free (desc->source);
501 g_free (desc->package);
502 g_free (desc->origin);
504 memset (desc, 0, sizeof (GstPluginDesc));
508 * gst_plugin_get_name:
509 * @plugin: plugin to get the name of
511 * Get the short name of the plugin
513 * Returns: the name of the plugin
516 gst_plugin_get_name (GstPlugin * plugin)
518 g_return_val_if_fail (plugin != NULL, NULL);
520 return plugin->desc.name;
524 * gst_plugin_get_description:
525 * @plugin: plugin to get long name of
527 * Get the long descriptive name of the plugin
529 * Returns: the long name of the plugin
531 G_CONST_RETURN gchar *
532 gst_plugin_get_description (GstPlugin * plugin)
534 g_return_val_if_fail (plugin != NULL, NULL);
536 return plugin->desc.description;
540 * gst_plugin_get_filename:
541 * @plugin: plugin to get the filename of
543 * get the filename of the plugin
545 * Returns: the filename of the plugin
547 G_CONST_RETURN gchar *
548 gst_plugin_get_filename (GstPlugin * plugin)
550 g_return_val_if_fail (plugin != NULL, NULL);
552 return plugin->filename;
556 * gst_plugin_get_version:
557 * @plugin: plugin to get the version of
559 * get the version of the plugin
561 * Returns: the version of the plugin
563 G_CONST_RETURN gchar *
564 gst_plugin_get_version (GstPlugin * plugin)
566 g_return_val_if_fail (plugin != NULL, NULL);
568 return plugin->desc.version;
572 * gst_plugin_get_license:
573 * @plugin: plugin to get the license of
575 * get the license of the plugin
577 * Returns: the license of the plugin
579 G_CONST_RETURN gchar *
580 gst_plugin_get_license (GstPlugin * plugin)
582 g_return_val_if_fail (plugin != NULL, NULL);
584 return plugin->desc.license;
588 * gst_plugin_get_source:
589 * @plugin: plugin to get the source of
591 * get the source module the plugin belongs to.
593 * Returns: the source of the plugin
595 G_CONST_RETURN gchar *
596 gst_plugin_get_source (GstPlugin * plugin)
598 g_return_val_if_fail (plugin != NULL, NULL);
600 return plugin->desc.source;
604 * gst_plugin_get_package:
605 * @plugin: plugin to get the package of
607 * get the package the plugin belongs to.
609 * Returns: the package of the plugin
611 G_CONST_RETURN gchar *
612 gst_plugin_get_package (GstPlugin * plugin)
614 g_return_val_if_fail (plugin != NULL, NULL);
616 return plugin->desc.package;
620 * gst_plugin_get_origin:
621 * @plugin: plugin to get the origin of
623 * get the URL where the plugin comes from
625 * Returns: the origin of the plugin
627 G_CONST_RETURN gchar *
628 gst_plugin_get_origin (GstPlugin * plugin)
630 g_return_val_if_fail (plugin != NULL, NULL);
632 return plugin->desc.origin;
636 * gst_plugin_get_module:
637 * @plugin: plugin to query
639 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
642 * Returns: module belonging to the plugin or NULL if the plugin isn't
646 gst_plugin_get_module (GstPlugin * plugin)
648 g_return_val_if_fail (plugin != NULL, NULL);
650 return plugin->module;
654 * gst_plugin_is_loaded:
655 * @plugin: plugin to query
657 * queries if the plugin is loaded into memory
659 * Returns: TRUE is loaded, FALSE otherwise
662 gst_plugin_is_loaded (GstPlugin * plugin)
664 g_return_val_if_fail (plugin != NULL, FALSE);
666 return (plugin->module != NULL || plugin->filename == NULL);
671 * gst_plugin_feature_list:
672 * @plugin: plugin to query
673 * @filter: the filter to use
674 * @first: only return first match
675 * @user_data: user data passed to the filter function
677 * Runs a filter against all plugin features and returns a GList with
678 * the results. If the first flag is set, only the first match is
679 * returned (as a list with a single object).
681 * Returns: a GList of features, g_list_free after use.
684 gst_plugin_feature_filter (GstPlugin * plugin,
685 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
690 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
692 for (g = list; g; g = g->next) {
693 gst_object_ref (plugin);
701 GstPluginFeatureFilter filter;
709 _feature_filter (GstPlugin * plugin, gpointer user_data)
712 FeatureFilterData *data = (FeatureFilterData *) user_data;
714 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
717 data->result = g_list_concat (data->result, result);
724 * gst_plugin_list_feature_filter:
725 * @list: a #GList of plugins to query
726 * @filter: the filter function to use
727 * @first: only return first match
728 * @user_data: user data passed to the filter function
730 * Runs a filter against all plugin features of the plugins in the given
731 * list and returns a GList with the results.
732 * If the first flag is set, only the first match is
733 * returned (as a list with a single object).
735 * Returns: a GList of features, g_list_free after use.
738 gst_plugin_list_feature_filter (GList * list,
739 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
741 FeatureFilterData data;
744 data.filter = filter;
746 data.user_data = user_data;
749 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
750 g_list_free (result);
757 * gst_plugin_name_filter:
758 * @plugin: the plugin to check
759 * @name: the name of the plugin
761 * A standard filter that returns TRUE when the plugin is of the
764 * Returns: TRUE if the plugin is of the given name.
767 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
769 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
774 * gst_plugin_find_feature:
775 * @plugin: plugin to get the feature from
776 * @name: The name of the feature to find
777 * @type: The type of the feature to find
779 * Find a feature of the given name and type in the given plugin.
781 * Returns: a GstPluginFeature or NULL if the feature was not found.
784 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
787 GstPluginFeature *result = NULL;
788 GstTypeNameData data;
790 g_return_val_if_fail (name != NULL, NULL);
795 walk = gst_filter_run (plugin->features,
796 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
799 result = GST_PLUGIN_FEATURE (walk->data);
801 gst_object_ref (result);
802 gst_plugin_feature_list_free (walk);
811 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
813 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
819 * gst_plugin_find_feature_by_name:
820 * @plugin: plugin to get the feature from
821 * @name: The name of the feature to find
823 * Find a feature of the given name in the given plugin.
825 * Returns: a GstPluginFeature or NULL if the feature was not found.
828 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
831 GstPluginFeature *result = NULL;
833 g_return_val_if_fail (name != NULL, NULL);
835 walk = gst_filter_run (plugin->features,
836 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
839 result = GST_PLUGIN_FEATURE (walk->data);
841 gst_object_ref (result);
842 gst_plugin_feature_list_free (walk);
850 * gst_plugin_load_by_name:
851 * @name: name of plugin to load
853 * Load the named plugin. Refs the plugin.
855 * Returns: A reference to a loaded plugin, or NULL on error.
858 gst_plugin_load_by_name (const gchar * name)
860 GstPlugin *plugin, *newplugin;
861 GError *error = NULL;
863 GST_DEBUG ("looking up plugin %s in default registry", name);
864 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
866 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
867 newplugin = gst_plugin_load_file (plugin->filename, &error);
868 gst_object_unref (plugin);
871 GST_WARNING ("load_plugin error: %s\n", error->message);
872 g_error_free (error);
875 /* newplugin was reffed by load_file */
879 GST_DEBUG ("Could not find plugin %s in registry", name);
885 * @plugin: plugin to load
887 * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
888 * untouched. The normal use pattern of this function goes like this:
891 * GstPlugin *loaded_plugin;
892 * loaded_plugin = gst_plugin_load (plugin);
893 * // presumably, we're no longer interested in the potentially-unloaded plugin
894 * gst_object_unref (plugin);
895 * plugin = loaded_plugin;
898 * Returns: A reference to a loaded plugin, or NULL on error.
901 gst_plugin_load (GstPlugin * plugin)
903 GError *error = NULL;
904 GstPlugin *newplugin;
906 if (gst_plugin_is_loaded (plugin)) {
910 if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
917 GST_WARNING ("load_plugin error: %s\n", error->message);
918 g_error_free (error);
924 * gst_plugin_list_free:
925 * @list: list of #GstPlugin
927 * Unrefs each member of @list, then frees the list.
930 gst_plugin_list_free (GList * list)
934 for (g = list; g; g = g->next) {
935 gst_object_unref (GST_PLUGIN (g->data));