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"
42 #include "gstfilter.h"
43 #include "gstregistry.h"
44 #include "gstmacros.h"
47 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
49 static GModule *main_module = NULL;
50 static GList *_gst_plugin_static = NULL;
52 /* static variables for segfault handling of plugin loading */
53 static char *_gst_plugin_fault_handler_filename = NULL;
54 extern gboolean _gst_disable_segtrap; /* see gst.c */
57 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
60 /* list of valid licenses.
61 * One of these must be specified or the plugin won't be loaded
62 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
65 * GPL: http://www.gnu.org/copyleft/gpl.html
66 * LGPL: http://www.gnu.org/copyleft/lesser.html
67 * QPL: http://www.trolltech.com/licenses/qpl.html
69 static gchar *valid_licenses[] = {
70 "LGPL", /* GNU Lesser General Public License */
71 "GPL", /* GNU General Public License */
72 "QPL", /* Trolltech Qt Public License */
73 "GPL/QPL", /* Combi-license of GPL + QPL */
74 GST_LICENSE_UNKNOWN, /* some other license */
78 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
79 GModule * module, GstPluginDesc * desc);
81 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src);
82 static void gst_plugin_desc_free (GstPluginDesc * desc);
85 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
87 static GstObjectClass *parent_class = NULL;
90 gst_plugin_init (GstPlugin * plugin)
96 gst_plugin_finalize (GObject * object)
98 GstPlugin *plugin = GST_PLUGIN (object);
99 GstRegistry *registry = gst_registry_get_default ();
102 GST_DEBUG ("finalizing plugin %p", plugin);
103 for (g = registry->plugins; g; g = g->next) {
104 if (g->data == (gpointer) plugin) {
105 g_warning ("removing plugin that is still in registry");
108 g_free (plugin->filename);
109 gst_plugin_desc_free (&plugin->desc);
111 G_OBJECT_CLASS (parent_class)->finalize (object);
115 gst_plugin_class_init (GstPluginClass * klass)
117 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
119 G_OBJECT_CLASS (klass)->finalize = GST_DEBUG_FUNCPTR (gst_plugin_finalize);
123 gst_plugin_error_quark (void)
125 static GQuark quark = 0;
128 quark = g_quark_from_static_string ("gst_plugin_error");
132 /* this function can be called in the GCC constructor extension, before
133 * the _gst_plugin_initialize() was called. In that case, we store the
134 * plugin description in a list to initialize it when we open the main
136 * When the main module is known, we can register the plugin right away.
139 _gst_plugin_register_static (GstPluginDesc * desc)
141 if (main_module == NULL) {
143 GST_LOG ("queueing static plugin \"%s\" for loading later on",
145 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
150 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
151 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
152 if (gst_plugin_register_func (plugin, main_module, desc)) {
154 GST_INFO ("loaded static plugin \"%s\"", desc->name);
155 gst_default_registry_add_plugin (plugin);
161 _gst_plugin_initialize (void)
163 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
165 /* now register all static plugins */
166 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
170 /* this function could be extended to check if the plugin license matches the
171 * applications license (would require the app to register its license somehow).
172 * We'll wait for someone who's interested in it to code it :)
175 gst_plugin_check_license (const gchar * license)
177 gchar **check_license = valid_licenses;
179 g_assert (check_license);
181 while (*check_license) {
182 if (strcmp (license, *check_license) == 0)
190 gst_plugin_check_version (gint major, gint minor)
192 /* return NULL if the major and minor version numbers are not compatible */
194 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
201 gst_plugin_register_func (GstPlugin * plugin, GModule * module,
202 GstPluginDesc * desc)
204 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
206 GST_INFO ("plugin \"%s\" has incompatible version, not loading",
211 if (!desc->license || !desc->description || !desc->source ||
212 !desc->package || !desc->origin) {
214 GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
219 if (!gst_plugin_check_license (desc->license)) {
221 GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
222 plugin->filename, desc->license);
227 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
229 gst_plugin_desc_copy (&plugin->desc, desc);
231 if (!((desc->plugin_init) (plugin))) {
233 GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
234 plugin->module = NULL;
239 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
246 * _gst_plugin_fault_handler_restore:
247 * segfault handler restorer
250 _gst_plugin_fault_handler_restore (void)
252 struct sigaction action;
254 memset (&action, 0, sizeof (action));
255 action.sa_handler = SIG_DFL;
257 sigaction (SIGSEGV, &action, NULL);
261 * _gst_plugin_fault_handler_sighandler:
262 * segfault handler implementation
265 _gst_plugin_fault_handler_sighandler (int signum)
267 /* We need to restore the fault handler or we'll keep getting it */
268 _gst_plugin_fault_handler_restore ();
272 g_print ("\nERROR: ");
273 g_print ("Caught a segmentation fault while loading plugin file:\n");
274 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
275 g_print ("Please either:\n");
276 g_print ("- remove it and restart.\n");
277 g_print ("- run with --gst-disable-segtrap and debug.\n");
281 g_print ("Caught unhandled signal on plugin loading\n");
287 * _gst_plugin_fault_handler_setup:
288 * sets up the segfault handler
291 _gst_plugin_fault_handler_setup (void)
293 struct sigaction action;
295 /* if asked to leave segfaults alone, just return */
296 if (_gst_disable_segtrap)
299 if (_gst_plugin_fault_handler_is_setup)
302 memset (&action, 0, sizeof (action));
303 action.sa_handler = _gst_plugin_fault_handler_sighandler;
305 sigaction (SIGSEGV, &action, NULL);
309 _gst_plugin_fault_handler_restore (void)
314 _gst_plugin_fault_handler_setup (void)
319 static void _gst_plugin_fault_handler_setup ();
321 GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
324 * gst_plugin_load_file:
325 * @filename: the plugin filename to load
326 * @error: pointer to a NULL-valued GError
328 * Loads the given plugin and refs it. Caller needs to unref after use.
330 * Returns: a new GstPlugin or NULL, if an error occurred.
333 gst_plugin_load_file (const gchar * filename, GError ** error)
339 struct stat file_status;
340 GstRegistry *registry;
342 g_return_val_if_fail (filename != NULL, NULL);
344 registry = gst_registry_get_default ();
345 g_static_mutex_lock (&gst_plugin_loading_mutex);
347 plugin = gst_registry_lookup (registry, filename);
349 if (plugin->module) {
350 g_static_mutex_unlock (&gst_plugin_loading_mutex);
353 gst_object_unref (plugin);
357 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
360 if (g_module_supported () == FALSE) {
361 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
364 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
368 if (stat (filename, &file_status)) {
369 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
372 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s\n", filename,
377 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
378 if (module == NULL) {
379 GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
382 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed");
386 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
388 plugin->module = module;
389 plugin->filename = strdup (filename);
390 plugin->file_mtime = file_status.st_mtime;
391 plugin->file_size = file_status.st_size;
393 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
395 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
398 GST_PLUGIN_ERROR_MODULE,
399 "Could not find plugin entry point in \"%s\"", filename);
402 plugin->orig_desc = (GstPluginDesc *) ptr;
404 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
407 /* this is where we load the actual .so, so let's trap SIGSEGV */
408 _gst_plugin_fault_handler_setup ();
409 _gst_plugin_fault_handler_filename = plugin->filename;
411 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
414 if (!gst_plugin_register_func (plugin, module, plugin->orig_desc)) {
415 /* remove signal handler */
416 _gst_plugin_fault_handler_restore ();
417 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
421 GST_PLUGIN_ERROR_MODULE,
422 "gst_plugin_register_func failed for plugin \"%s\"", filename);
423 g_module_close (module);
427 /* remove signal handler */
428 _gst_plugin_fault_handler_restore ();
429 _gst_plugin_fault_handler_filename = NULL;
430 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
432 gst_object_ref (plugin);
433 gst_default_registry_add_plugin (plugin);
435 g_static_mutex_unlock (&gst_plugin_loading_mutex);
439 gst_object_unref (plugin);
440 g_static_mutex_unlock (&gst_plugin_loading_mutex);
445 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
447 dest->major_version = src->major_version;
448 dest->minor_version = src->minor_version;
449 dest->name = g_strdup (src->name);
450 dest->description = g_strdup (src->description);
451 dest->plugin_init = src->plugin_init;
452 dest->version = g_strdup (src->version);
453 dest->license = g_strdup (src->license);
454 dest->source = g_strdup (src->source);
455 dest->package = g_strdup (src->package);
456 dest->origin = g_strdup (src->origin);
461 gst_plugin_desc_free (GstPluginDesc * desc)
464 g_free (desc->description);
465 g_free (desc->version);
466 g_free (desc->license);
467 g_free (desc->source);
468 g_free (desc->package);
469 g_free (desc->origin);
471 memset (desc, 0, sizeof (GstPluginDesc));
475 * gst_plugin_get_name:
476 * @plugin: plugin to get the name of
478 * Get the short name of the plugin
480 * Returns: the name of the plugin
483 gst_plugin_get_name (GstPlugin * plugin)
485 g_return_val_if_fail (plugin != NULL, NULL);
487 return plugin->desc.name;
491 * gst_plugin_get_description:
492 * @plugin: plugin to get long name of
494 * Get the long descriptive name of the plugin
496 * Returns: the long name of the plugin
498 G_CONST_RETURN gchar *
499 gst_plugin_get_description (GstPlugin * plugin)
501 g_return_val_if_fail (plugin != NULL, NULL);
503 return plugin->desc.description;
507 * gst_plugin_get_filename:
508 * @plugin: plugin to get the filename of
510 * get the filename of the plugin
512 * Returns: the filename of the plugin
514 G_CONST_RETURN gchar *
515 gst_plugin_get_filename (GstPlugin * plugin)
517 g_return_val_if_fail (plugin != NULL, NULL);
519 return plugin->filename;
523 * gst_plugin_get_version:
524 * @plugin: plugin to get the version of
526 * get the version of the plugin
528 * Returns: the version of the plugin
530 G_CONST_RETURN gchar *
531 gst_plugin_get_version (GstPlugin * plugin)
533 g_return_val_if_fail (plugin != NULL, NULL);
535 return plugin->desc.version;
539 * gst_plugin_get_license:
540 * @plugin: plugin to get the license of
542 * get the license of the plugin
544 * Returns: the license of the plugin
546 G_CONST_RETURN gchar *
547 gst_plugin_get_license (GstPlugin * plugin)
549 g_return_val_if_fail (plugin != NULL, NULL);
551 return plugin->desc.license;
555 * gst_plugin_get_source:
556 * @plugin: plugin to get the source of
558 * get the source module the plugin belongs to.
560 * Returns: the source of the plugin
562 G_CONST_RETURN gchar *
563 gst_plugin_get_source (GstPlugin * plugin)
565 g_return_val_if_fail (plugin != NULL, NULL);
567 return plugin->desc.source;
571 * gst_plugin_get_package:
572 * @plugin: plugin to get the package of
574 * get the package the plugin belongs to.
576 * Returns: the package of the plugin
578 G_CONST_RETURN gchar *
579 gst_plugin_get_package (GstPlugin * plugin)
581 g_return_val_if_fail (plugin != NULL, NULL);
583 return plugin->desc.package;
587 * gst_plugin_get_origin:
588 * @plugin: plugin to get the origin of
590 * get the URL where the plugin comes from
592 * Returns: the origin of the plugin
594 G_CONST_RETURN gchar *
595 gst_plugin_get_origin (GstPlugin * plugin)
597 g_return_val_if_fail (plugin != NULL, NULL);
599 return plugin->desc.origin;
603 * gst_plugin_get_module:
604 * @plugin: plugin to query
606 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
609 * Returns: module belonging to the plugin or NULL if the plugin isn't
613 gst_plugin_get_module (GstPlugin * plugin)
615 g_return_val_if_fail (plugin != NULL, NULL);
617 return plugin->module;
621 * gst_plugin_is_loaded:
622 * @plugin: plugin to query
624 * queries if the plugin is loaded into memory
626 * Returns: TRUE is loaded, FALSE otherwise
629 gst_plugin_is_loaded (GstPlugin * plugin)
631 g_return_val_if_fail (plugin != NULL, FALSE);
633 return (plugin->module != NULL || plugin->filename == NULL);
638 * gst_plugin_feature_list:
639 * @plugin: plugin to query
640 * @filter: the filter to use
641 * @first: only return first match
642 * @user_data: user data passed to the filter function
644 * Runs a filter against all plugin features and returns a GList with
645 * the results. If the first flag is set, only the first match is
646 * returned (as a list with a single object).
648 * Returns: a GList of features, g_list_free after use.
651 gst_plugin_feature_filter (GstPlugin * plugin,
652 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
657 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
659 for (g = list; g; g = g->next) {
660 gst_object_ref (plugin);
668 GstPluginFeatureFilter filter;
676 _feature_filter (GstPlugin * plugin, gpointer user_data)
679 FeatureFilterData *data = (FeatureFilterData *) user_data;
681 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
684 data->result = g_list_concat (data->result, result);
691 * gst_plugin_list_feature_filter:
692 * @list: a #GList of plugins to query
693 * @filter: the filter function to use
694 * @first: only return first match
695 * @user_data: user data passed to the filter function
697 * Runs a filter against all plugin features of the plugins in the given
698 * list and returns a GList with the results.
699 * If the first flag is set, only the first match is
700 * returned (as a list with a single object).
702 * Returns: a GList of features, g_list_free after use.
705 gst_plugin_list_feature_filter (GList * list,
706 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
708 FeatureFilterData data;
711 data.filter = filter;
713 data.user_data = user_data;
716 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
717 g_list_free (result);
724 * gst_plugin_name_filter:
725 * @plugin: the plugin to check
726 * @name: the name of the plugin
728 * A standard filter that returns TRUE when the plugin is of the
731 * Returns: TRUE if the plugin is of the given name.
734 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
736 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
741 * gst_plugin_find_feature:
742 * @plugin: plugin to get the feature from
743 * @name: The name of the feature to find
744 * @type: The type of the feature to find
746 * Find a feature of the given name and type in the given plugin.
748 * Returns: a GstPluginFeature or NULL if the feature was not found.
751 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
754 GstPluginFeature *result = NULL;
755 GstTypeNameData data;
757 g_return_val_if_fail (name != NULL, NULL);
762 walk = gst_filter_run (plugin->features,
763 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
766 result = GST_PLUGIN_FEATURE (walk->data);
768 gst_object_ref (result);
769 gst_plugin_feature_list_free (walk);
778 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
780 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
786 * gst_plugin_find_feature_by_name:
787 * @plugin: plugin to get the feature from
788 * @name: The name of the feature to find
790 * Find a feature of the given name in the given plugin.
792 * Returns: a GstPluginFeature or NULL if the feature was not found.
795 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
798 GstPluginFeature *result = NULL;
800 g_return_val_if_fail (name != NULL, NULL);
802 walk = gst_filter_run (plugin->features,
803 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
806 result = GST_PLUGIN_FEATURE (walk->data);
808 gst_object_ref (result);
809 gst_plugin_feature_list_free (walk);
817 * gst_plugin_load_by_name:
818 * @name: name of plugin to load
820 * Load the named plugin. Refs the plugin.
822 * Returns: whether the plugin was loaded or not
825 gst_plugin_load_by_name (const gchar * name)
827 GstPlugin *plugin, *newplugin;
828 GError *error = NULL;
830 GST_DEBUG ("looking up plugin %s in default registry", name);
831 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
833 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
834 newplugin = gst_plugin_load_file (plugin->filename, &error);
835 gst_object_unref (plugin);
838 GST_WARNING ("load_plugin error: %s\n", error->message);
839 g_error_free (error);
842 /* newplugin was reffed by load_file */
846 GST_DEBUG ("Could not find plugin %s in registry", name);
851 gst_plugin_load (GstPlugin * plugin)
853 GError *error = NULL;
854 GstPlugin *newplugin;
856 if (gst_plugin_is_loaded (plugin)) {
860 newplugin = gst_plugin_load_file (plugin->filename, &error);
861 if (newplugin == NULL) {
862 GST_WARNING ("load_plugin error: %s\n", error->message);
863 g_error_free (error);
864 gst_object_unref (plugin);
868 gst_object_unref (plugin);
874 gst_plugin_list_free (GList * list)
878 for (g = list; g; g = g->next) {
879 gst_object_unref (GST_PLUGIN (g->data));