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"
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 GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
78 GModule * module, GstPluginDesc * desc);
80 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src);
83 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
86 gst_plugin_init (GstPlugin * plugin)
92 gst_plugin_finalize (GstPlugin * plugin)
94 GstRegistry *registry = gst_registry_get_default ();
97 GST_DEBUG ("finalizing plugin %p", plugin);
98 for (g = registry->plugins; g; g = g->next) {
99 if (g->data == (gpointer) plugin) {
100 g_warning ("removing plugin that is still in registry");
106 gst_plugin_class_init (GstPluginClass * klass)
109 G_OBJECT_CLASS (klass)->finalize = (GObjectFinalizeFunc) gst_plugin_finalize;
113 gst_plugin_error_quark (void)
115 static GQuark quark = 0;
118 quark = g_quark_from_static_string ("gst_plugin_error");
122 /* this function can be called in the GCC constructor extension, before
123 * the _gst_plugin_initialize() was called. In that case, we store the
124 * plugin description in a list to initialize it when we open the main
126 * When the main module is known, we can register the plugin right away.
129 _gst_plugin_register_static (GstPluginDesc * desc)
131 if (main_module == NULL) {
133 GST_LOG ("queueing static plugin \"%s\" for loading later on",
135 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
140 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
141 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
142 if (gst_plugin_register_func (plugin, main_module, desc)) {
144 GST_INFO ("loaded static plugin \"%s\"", desc->name);
145 gst_default_registry_add_plugin (plugin);
151 _gst_plugin_initialize (void)
153 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
155 /* now register all static plugins */
156 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
160 /* this function could be extended to check if the plugin license matches the
161 * applications license (would require the app to register its license somehow).
162 * We'll wait for someone who's interested in it to code it :)
165 gst_plugin_check_license (const gchar * license)
167 gchar **check_license = valid_licenses;
169 g_assert (check_license);
171 while (*check_license) {
172 if (strcmp (license, *check_license) == 0)
180 gst_plugin_check_version (gint major, gint minor)
182 /* return NULL if the major and minor version numbers are not compatible */
184 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
191 gst_plugin_register_func (GstPlugin * plugin, GModule * module,
192 GstPluginDesc * desc)
194 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
196 GST_INFO ("plugin \"%s\" has incompatible version, not loading",
201 if (!desc->license || !desc->description || !desc->source ||
202 !desc->package || !desc->origin) {
204 GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
209 if (!gst_plugin_check_license (desc->license)) {
211 GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
212 plugin->filename, desc->license);
217 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
219 gst_plugin_desc_copy (&plugin->desc, desc);
221 if (!((desc->plugin_init) (plugin))) {
223 GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
224 plugin->module = NULL;
229 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
236 * _gst_plugin_fault_handler_restore:
237 * segfault handler restorer
240 _gst_plugin_fault_handler_restore (void)
242 struct sigaction action;
244 memset (&action, 0, sizeof (action));
245 action.sa_handler = SIG_DFL;
247 sigaction (SIGSEGV, &action, NULL);
251 * _gst_plugin_fault_handler_sighandler:
252 * segfault handler implementation
255 _gst_plugin_fault_handler_sighandler (int signum)
257 /* We need to restore the fault handler or we'll keep getting it */
258 _gst_plugin_fault_handler_restore ();
262 g_print ("\nERROR: ");
263 g_print ("Caught a segmentation fault while loading plugin file:\n");
264 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
265 g_print ("Please either:\n");
266 g_print ("- remove it and restart.\n");
267 g_print ("- run with --gst-disable-segtrap and debug.\n");
271 g_print ("Caught unhandled signal on plugin loading\n");
277 * _gst_plugin_fault_handler_setup:
278 * sets up the segfault handler
281 _gst_plugin_fault_handler_setup (void)
283 struct sigaction action;
285 /* if asked to leave segfaults alone, just return */
286 if (_gst_disable_segtrap)
289 if (_gst_plugin_fault_handler_is_setup)
292 memset (&action, 0, sizeof (action));
293 action.sa_handler = _gst_plugin_fault_handler_sighandler;
295 sigaction (SIGSEGV, &action, NULL);
299 _gst_plugin_fault_handler_restore (void)
304 _gst_plugin_fault_handler_setup (void)
309 static void _gst_plugin_fault_handler_setup ();
311 GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
314 * gst_plugin_load_file:
315 * @filename: the plugin filename to load
316 * @error: pointer to a NULL-valued GError
318 * Loads the given plugin.
320 * Returns: a new GstPlugin or NULL, if an error occurred.
323 gst_plugin_load_file (const gchar * filename, GError ** error)
329 struct stat file_status;
330 GstRegistry *registry;
332 g_return_val_if_fail (filename != NULL, NULL);
334 registry = gst_registry_get_default ();
335 g_static_mutex_lock (&gst_plugin_loading_mutex);
337 plugin = gst_registry_lookup (registry, filename);
339 if (plugin->module) {
340 g_static_mutex_unlock (&gst_plugin_loading_mutex);
343 gst_object_unref (plugin);
347 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
350 if (g_module_supported () == FALSE) {
351 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
354 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
358 if (stat (filename, &file_status)) {
359 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
362 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s\n", filename,
367 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
368 if (module == NULL) {
369 GST_CAT_ERROR (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
372 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed");
376 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
378 plugin->module = module;
379 plugin->filename = strdup (filename);
380 plugin->file_mtime = file_status.st_mtime;
381 plugin->file_size = file_status.st_size;
383 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
385 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
388 GST_PLUGIN_ERROR_MODULE,
389 "Could not find plugin entry point in \"%s\"", filename);
392 plugin->orig_desc = (GstPluginDesc *) ptr;
394 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
397 /* this is where we load the actual .so, so let's trap SIGSEGV */
398 _gst_plugin_fault_handler_setup ();
399 _gst_plugin_fault_handler_filename = plugin->filename;
401 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
404 if (!gst_plugin_register_func (plugin, module, plugin->orig_desc)) {
405 /* remove signal handler */
406 _gst_plugin_fault_handler_restore ();
407 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
411 GST_PLUGIN_ERROR_MODULE,
412 "gst_plugin_register_func failed for plugin \"%s\"", filename);
413 g_module_close (module);
417 /* remove signal handler */
418 _gst_plugin_fault_handler_restore ();
419 _gst_plugin_fault_handler_filename = NULL;
420 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
422 gst_object_ref (plugin);
423 gst_default_registry_add_plugin (plugin);
425 g_static_mutex_unlock (&gst_plugin_loading_mutex);
428 g_static_mutex_unlock (&gst_plugin_loading_mutex);
433 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
435 dest->major_version = src->major_version;
436 dest->minor_version = src->minor_version;
437 dest->name = g_strdup (src->name);
438 dest->description = g_strdup (src->description);
439 dest->plugin_init = src->plugin_init;
440 dest->version = g_strdup (src->version);
441 dest->license = g_strdup (src->license);
442 dest->source = g_strdup (src->source);
443 dest->package = g_strdup (src->package);
444 dest->origin = g_strdup (src->origin);
450 gst_plugin_desc_free (GstPluginDesc * desc)
453 g_free (desc->description);
454 g_free (desc->version);
455 g_free (desc->license);
456 g_free (desc->source);
457 g_free (desc->package);
458 g_free (desc->origin);
460 memset (desc, 0, sizeof (GstPluginDesc));
465 * gst_plugin_get_name:
466 * @plugin: plugin to get the name of
468 * Get the short name of the plugin
470 * Returns: the name of the plugin
473 gst_plugin_get_name (GstPlugin * plugin)
475 g_return_val_if_fail (plugin != NULL, NULL);
477 return plugin->desc.name;
481 * gst_plugin_get_description:
482 * @plugin: plugin to get long name of
484 * Get the long descriptive name of the plugin
486 * Returns: the long name of the plugin
488 G_CONST_RETURN gchar *
489 gst_plugin_get_description (GstPlugin * plugin)
491 g_return_val_if_fail (plugin != NULL, NULL);
493 return plugin->desc.description;
497 * gst_plugin_get_filename:
498 * @plugin: plugin to get the filename of
500 * get the filename of the plugin
502 * Returns: the filename of the plugin
504 G_CONST_RETURN gchar *
505 gst_plugin_get_filename (GstPlugin * plugin)
507 g_return_val_if_fail (plugin != NULL, NULL);
509 return plugin->filename;
513 * gst_plugin_get_version:
514 * @plugin: plugin to get the version of
516 * get the version of the plugin
518 * Returns: the version of the plugin
520 G_CONST_RETURN gchar *
521 gst_plugin_get_version (GstPlugin * plugin)
523 g_return_val_if_fail (plugin != NULL, NULL);
525 return plugin->desc.version;
529 * gst_plugin_get_license:
530 * @plugin: plugin to get the license of
532 * get the license of the plugin
534 * Returns: the license of the plugin
536 G_CONST_RETURN gchar *
537 gst_plugin_get_license (GstPlugin * plugin)
539 g_return_val_if_fail (plugin != NULL, NULL);
541 return plugin->desc.license;
545 * gst_plugin_get_source:
546 * @plugin: plugin to get the source of
548 * get the source module the plugin belongs to.
550 * Returns: the source of the plugin
552 G_CONST_RETURN gchar *
553 gst_plugin_get_source (GstPlugin * plugin)
555 g_return_val_if_fail (plugin != NULL, NULL);
557 return plugin->desc.source;
561 * gst_plugin_get_package:
562 * @plugin: plugin to get the package of
564 * get the package the plugin belongs to.
566 * Returns: the package of the plugin
568 G_CONST_RETURN gchar *
569 gst_plugin_get_package (GstPlugin * plugin)
571 g_return_val_if_fail (plugin != NULL, NULL);
573 return plugin->desc.package;
577 * gst_plugin_get_origin:
578 * @plugin: plugin to get the origin of
580 * get the URL where the plugin comes from
582 * Returns: the origin of the plugin
584 G_CONST_RETURN gchar *
585 gst_plugin_get_origin (GstPlugin * plugin)
587 g_return_val_if_fail (plugin != NULL, NULL);
589 return plugin->desc.origin;
593 * gst_plugin_get_module:
594 * @plugin: plugin to query
596 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
599 * Returns: module belonging to the plugin or NULL if the plugin isn't
603 gst_plugin_get_module (GstPlugin * plugin)
605 g_return_val_if_fail (plugin != NULL, NULL);
607 return plugin->module;
611 * gst_plugin_is_loaded:
612 * @plugin: plugin to query
614 * queries if the plugin is loaded into memory
616 * Returns: TRUE is loaded, FALSE otherwise
619 gst_plugin_is_loaded (GstPlugin * plugin)
621 g_return_val_if_fail (plugin != NULL, FALSE);
623 return (plugin->module != NULL || plugin->filename == NULL);
628 * gst_plugin_feature_list:
629 * @plugin: plugin to query
630 * @filter: the filter to use
631 * @first: only return first match
632 * @user_data: user data passed to the filter function
634 * Runs a filter against all plugin features and returns a GList with
635 * the results. If the first flag is set, only the first match is
636 * returned (as a list with a single object).
638 * Returns: a GList of features, g_list_free after use.
641 gst_plugin_feature_filter (GstPlugin * plugin,
642 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
647 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
649 for (g = list; g; g = g->next) {
650 gst_object_ref (plugin);
658 GstPluginFeatureFilter filter;
666 _feature_filter (GstPlugin * plugin, gpointer user_data)
669 FeatureFilterData *data = (FeatureFilterData *) user_data;
671 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
674 data->result = g_list_concat (data->result, result);
681 * gst_plugin_list_feature_filter:
682 * @list: a #GList of plugins to query
683 * @filter: the filter function to use
684 * @first: only return first match
685 * @user_data: user data passed to the filter function
687 * Runs a filter against all plugin features of the plugins in the given
688 * list and returns a GList with the results.
689 * 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_list_feature_filter (GList * list,
696 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
698 FeatureFilterData data;
701 data.filter = filter;
703 data.user_data = user_data;
706 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
707 g_list_free (result);
714 * gst_plugin_name_filter:
715 * @plugin: the plugin to check
716 * @name: the name of the plugin
718 * A standard filter that returns TRUE when the plugin is of the
721 * Returns: TRUE if the plugin is of the given name.
724 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
726 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
731 * gst_plugin_find_feature:
732 * @plugin: plugin to get the feature from
733 * @name: The name of the feature to find
734 * @type: The type of the feature to find
736 * Find a feature of the given name and type in the given plugin.
738 * Returns: a GstPluginFeature or NULL if the feature was not found.
741 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
744 GstPluginFeature *result = NULL;
745 GstTypeNameData data;
747 g_return_val_if_fail (name != NULL, NULL);
752 walk = gst_filter_run (plugin->features,
753 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
756 result = GST_PLUGIN_FEATURE (walk->data);
758 gst_object_ref (result);
759 gst_plugin_feature_list_free (walk);
768 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
770 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
776 * gst_plugin_find_feature_by_name:
777 * @plugin: plugin to get the feature from
778 * @name: The name of the feature to find
780 * Find a feature of the given name in the given plugin.
782 * Returns: a GstPluginFeature or NULL if the feature was not found.
785 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
788 GstPluginFeature *result = NULL;
790 g_return_val_if_fail (name != NULL, NULL);
792 walk = gst_filter_run (plugin->features,
793 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
796 result = GST_PLUGIN_FEATURE (walk->data);
798 gst_object_ref (result);
799 gst_plugin_feature_list_free (walk);
807 * gst_plugin_load_by_name:
808 * @name: name of plugin to load
810 * Load the named plugin.
812 * Returns: whether the plugin was loaded or not
815 gst_plugin_load_by_name (const gchar * name)
818 GError *error = NULL;
820 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
822 plugin = gst_plugin_load_file (plugin->filename, &error);
824 GST_WARNING ("load_plugin error: %s\n", error->message);
825 g_error_free (error);
831 GST_DEBUG ("Could not find plugin %s in registry", name);
836 gst_plugin_load (GstPlugin * plugin)
838 GError *error = NULL;
839 GstPlugin *newplugin;
841 if (gst_plugin_is_loaded (plugin)) {
845 newplugin = gst_plugin_load_file (plugin->filename, &error);
846 if (newplugin == NULL) {
847 GST_WARNING ("load_plugin error: %s\n", error->message);
848 g_error_free (error);
849 gst_object_unref (plugin);
853 gst_object_unref (plugin);
859 gst_plugin_list_free (GList * list)
863 for (g = list; g; g = g->next) {
864 gst_object_unref (GST_PLUGIN (g->data));