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_class_init (GstPluginClass * klass)
99 gst_plugin_error_quark (void)
101 static GQuark quark = 0;
104 quark = g_quark_from_static_string ("gst_plugin_error");
108 /* this function can be called in the GCC constructor extension, before
109 * the _gst_plugin_initialize() was called. In that case, we store the
110 * plugin description in a list to initialize it when we open the main
112 * When the main module is known, we can register the plugin right away.
115 _gst_plugin_register_static (GstPluginDesc * desc)
117 if (main_module == NULL) {
119 GST_LOG ("queueing static plugin \"%s\" for loading later on",
121 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
126 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
127 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
128 if (gst_plugin_register_func (plugin, main_module, desc)) {
130 GST_INFO ("loaded static plugin \"%s\"", desc->name);
131 gst_default_registry_add_plugin (plugin);
137 _gst_plugin_initialize (void)
139 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
141 /* now register all static plugins */
142 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
146 /* this function could be extended to check if the plugin license matches the
147 * applications license (would require the app to register its license somehow).
148 * We'll wait for someone who's interested in it to code it :)
151 gst_plugin_check_license (const gchar * license)
153 gchar **check_license = valid_licenses;
155 g_assert (check_license);
157 while (*check_license) {
158 if (strcmp (license, *check_license) == 0)
166 gst_plugin_check_version (gint major, gint minor)
168 /* return NULL if the major and minor version numbers are not compatible */
170 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
177 gst_plugin_register_func (GstPlugin * plugin, GModule * module,
178 GstPluginDesc * desc)
180 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
182 GST_INFO ("plugin \"%s\" has incompatible version, not loading",
187 if (!desc->license || !desc->description || !desc->source ||
188 !desc->package || !desc->origin) {
190 GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
195 if (!gst_plugin_check_license (desc->license)) {
197 GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
198 plugin->filename, desc->license);
203 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
205 gst_plugin_desc_copy (&plugin->desc, desc);
207 if (!((desc->plugin_init) (plugin))) {
209 GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
210 plugin->module = NULL;
215 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
222 * _gst_plugin_fault_handler_restore:
223 * segfault handler restorer
226 _gst_plugin_fault_handler_restore (void)
228 struct sigaction action;
230 memset (&action, 0, sizeof (action));
231 action.sa_handler = SIG_DFL;
233 sigaction (SIGSEGV, &action, NULL);
237 * _gst_plugin_fault_handler_sighandler:
238 * segfault handler implementation
241 _gst_plugin_fault_handler_sighandler (int signum)
243 /* We need to restore the fault handler or we'll keep getting it */
244 _gst_plugin_fault_handler_restore ();
248 g_print ("\nERROR: ");
249 g_print ("Caught a segmentation fault while loading plugin file:\n");
250 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
251 g_print ("Please either:\n");
252 g_print ("- remove it and restart.\n");
253 g_print ("- run with --gst-disable-segtrap and debug.\n");
257 g_print ("Caught unhandled signal on plugin loading\n");
263 * _gst_plugin_fault_handler_setup:
264 * sets up the segfault handler
267 _gst_plugin_fault_handler_setup (void)
269 struct sigaction action;
271 /* if asked to leave segfaults alone, just return */
272 if (_gst_disable_segtrap)
275 if (_gst_plugin_fault_handler_is_setup)
278 memset (&action, 0, sizeof (action));
279 action.sa_handler = _gst_plugin_fault_handler_sighandler;
281 sigaction (SIGSEGV, &action, NULL);
285 _gst_plugin_fault_handler_restore (void)
290 _gst_plugin_fault_handler_setup (void)
295 static void _gst_plugin_fault_handler_setup ();
297 GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
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)
315 struct stat file_status;
316 GstRegistry *registry;
318 g_return_val_if_fail (filename != NULL, NULL);
320 registry = gst_registry_get_default ();
321 g_static_mutex_lock (&gst_plugin_loading_mutex);
323 plugin = gst_registry_lookup (registry, filename);
324 if (plugin && plugin->module) {
325 g_static_mutex_unlock (&gst_plugin_loading_mutex);
330 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
333 if (g_module_supported () == FALSE) {
334 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
337 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
341 if (stat (filename, &file_status)) {
342 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
345 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s\n", filename,
350 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
351 if (module == NULL) {
352 GST_CAT_ERROR (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
355 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed");
359 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
361 plugin->module = module;
362 plugin->filename = strdup (filename);
363 plugin->file_mtime = file_status.st_mtime;
364 plugin->file_size = file_status.st_size;
366 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
368 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
371 GST_PLUGIN_ERROR_MODULE,
372 "Could not find plugin entry point in \"%s\"", filename);
375 plugin->orig_desc = (GstPluginDesc *) ptr;
377 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
380 /* this is where we load the actual .so, so let's trap SIGSEGV */
381 _gst_plugin_fault_handler_setup ();
382 _gst_plugin_fault_handler_filename = plugin->filename;
384 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
387 if (!gst_plugin_register_func (plugin, module, plugin->orig_desc)) {
388 /* remove signal handler */
389 _gst_plugin_fault_handler_restore ();
390 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
394 GST_PLUGIN_ERROR_MODULE,
395 "gst_plugin_register_func failed for plugin \"%s\"", filename);
396 g_module_close (module);
400 /* remove signal handler */
401 _gst_plugin_fault_handler_restore ();
402 _gst_plugin_fault_handler_filename = NULL;
403 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
405 gst_default_registry_add_plugin (plugin);
407 g_static_mutex_unlock (&gst_plugin_loading_mutex);
410 g_static_mutex_unlock (&gst_plugin_loading_mutex);
415 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
417 dest->major_version = src->major_version;
418 dest->minor_version = src->minor_version;
419 dest->name = g_strdup (src->name);
420 dest->description = g_strdup (src->description);
421 dest->plugin_init = src->plugin_init;
422 dest->version = g_strdup (src->version);
423 dest->license = g_strdup (src->license);
424 dest->source = g_strdup (src->source);
425 dest->package = g_strdup (src->package);
426 dest->origin = g_strdup (src->origin);
432 gst_plugin_desc_free (GstPluginDesc * desc)
435 g_free (desc->description);
436 g_free (desc->version);
437 g_free (desc->license);
438 g_free (desc->source);
439 g_free (desc->package);
440 g_free (desc->origin);
442 memset (desc, 0, sizeof (GstPluginDesc));
447 * gst_plugin_get_name:
448 * @plugin: plugin to get the name of
450 * Get the short name of the plugin
452 * Returns: the name of the plugin
455 gst_plugin_get_name (GstPlugin * plugin)
457 g_return_val_if_fail (plugin != NULL, NULL);
459 return plugin->desc.name;
463 * gst_plugin_get_description:
464 * @plugin: plugin to get long name of
466 * Get the long descriptive name of the plugin
468 * Returns: the long name of the plugin
470 G_CONST_RETURN gchar *
471 gst_plugin_get_description (GstPlugin * plugin)
473 g_return_val_if_fail (plugin != NULL, NULL);
475 return plugin->desc.description;
479 * gst_plugin_get_filename:
480 * @plugin: plugin to get the filename of
482 * get the filename of the plugin
484 * Returns: the filename of the plugin
486 G_CONST_RETURN gchar *
487 gst_plugin_get_filename (GstPlugin * plugin)
489 g_return_val_if_fail (plugin != NULL, NULL);
491 return plugin->filename;
495 * gst_plugin_get_version:
496 * @plugin: plugin to get the version of
498 * get the version of the plugin
500 * Returns: the version of the plugin
502 G_CONST_RETURN gchar *
503 gst_plugin_get_version (GstPlugin * plugin)
505 g_return_val_if_fail (plugin != NULL, NULL);
507 return plugin->desc.version;
511 * gst_plugin_get_license:
512 * @plugin: plugin to get the license of
514 * get the license of the plugin
516 * Returns: the license of the plugin
518 G_CONST_RETURN gchar *
519 gst_plugin_get_license (GstPlugin * plugin)
521 g_return_val_if_fail (plugin != NULL, NULL);
523 return plugin->desc.license;
527 * gst_plugin_get_source:
528 * @plugin: plugin to get the source of
530 * get the source module the plugin belongs to.
532 * Returns: the source of the plugin
534 G_CONST_RETURN gchar *
535 gst_plugin_get_source (GstPlugin * plugin)
537 g_return_val_if_fail (plugin != NULL, NULL);
539 return plugin->desc.source;
543 * gst_plugin_get_package:
544 * @plugin: plugin to get the package of
546 * get the package the plugin belongs to.
548 * Returns: the package of the plugin
550 G_CONST_RETURN gchar *
551 gst_plugin_get_package (GstPlugin * plugin)
553 g_return_val_if_fail (plugin != NULL, NULL);
555 return plugin->desc.package;
559 * gst_plugin_get_origin:
560 * @plugin: plugin to get the origin of
562 * get the URL where the plugin comes from
564 * Returns: the origin of the plugin
566 G_CONST_RETURN gchar *
567 gst_plugin_get_origin (GstPlugin * plugin)
569 g_return_val_if_fail (plugin != NULL, NULL);
571 return plugin->desc.origin;
575 * gst_plugin_get_module:
576 * @plugin: plugin to query
578 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
581 * Returns: module belonging to the plugin or NULL if the plugin isn't
585 gst_plugin_get_module (GstPlugin * plugin)
587 g_return_val_if_fail (plugin != NULL, NULL);
589 return plugin->module;
593 * gst_plugin_is_loaded:
594 * @plugin: plugin to query
596 * queries if the plugin is loaded into memory
598 * Returns: TRUE is loaded, FALSE otherwise
601 gst_plugin_is_loaded (GstPlugin * plugin)
603 g_return_val_if_fail (plugin != NULL, FALSE);
605 return (plugin->module != NULL);
609 * gst_plugin_feature_list:
610 * @plugin: plugin to query
611 * @filter: the filter to use
612 * @first: only return first match
613 * @user_data: user data passed to the filter function
615 * Runs a filter against all plugin features and returns a GList with
616 * the results. If the first flag is set, only the first match is
617 * returned (as a list with a single object).
619 * Returns: a GList of features, g_list_free after use.
622 gst_plugin_feature_filter (GstPlugin * plugin,
623 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
628 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
630 for (g = list; g; g = g->next) {
631 gst_object_ref (plugin);
639 GstPluginFeatureFilter filter;
647 _feature_filter (GstPlugin * plugin, gpointer user_data)
650 FeatureFilterData *data = (FeatureFilterData *) user_data;
652 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
655 data->result = g_list_concat (data->result, result);
662 * gst_plugin_list_feature_filter:
663 * @list: a #GList of plugins to query
664 * @filter: the filter function to use
665 * @first: only return first match
666 * @user_data: user data passed to the filter function
668 * Runs a filter against all plugin features of the plugins in the given
669 * list and returns a GList with the results.
670 * If the first flag is set, only the first match is
671 * returned (as a list with a single object).
673 * Returns: a GList of features, g_list_free after use.
676 gst_plugin_list_feature_filter (GList * list,
677 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
679 FeatureFilterData data;
682 data.filter = filter;
684 data.user_data = user_data;
687 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
688 g_list_free (result);
694 * gst_plugin_name_filter:
695 * @plugin: the plugin to check
696 * @name: the name of the plugin
698 * A standard filter that returns TRUE when the plugin is of the
701 * Returns: TRUE if the plugin is of the given name.
704 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
706 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
710 * gst_plugin_find_feature:
711 * @plugin: plugin to get the feature from
712 * @name: The name of the feature to find
713 * @type: The type of the feature to find
715 * Find a feature of the given name and type in the given plugin.
717 * Returns: a GstPluginFeature or NULL if the feature was not found.
720 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
723 GstPluginFeature *result = NULL;
724 GstTypeNameData data;
726 g_return_val_if_fail (name != NULL, NULL);
731 walk = gst_filter_run (plugin->features,
732 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
735 result = GST_PLUGIN_FEATURE (walk->data);
737 gst_plugin_feature_list_free (walk);
743 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
745 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
749 * gst_plugin_find_feature_by_name:
750 * @plugin: plugin to get the feature from
751 * @name: The name of the feature to find
753 * Find a feature of the given name in the given plugin.
755 * Returns: a GstPluginFeature or NULL if the feature was not found.
758 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
761 GstPluginFeature *result = NULL;
763 g_return_val_if_fail (name != NULL, NULL);
765 walk = gst_filter_run (plugin->features,
766 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
769 result = GST_PLUGIN_FEATURE (walk->data);
771 gst_plugin_feature_list_free (walk);
777 * gst_plugin_add_feature:
778 * @plugin: plugin to add feature to
779 * @feature: feature to add
781 * Add feature to the list of those provided by the plugin.
782 * There is a separate namespace for each plugin feature type.
783 * See #gst_plugin_get_feature_list
786 gst_plugin_add_feature (GstPlugin * plugin, GstPluginFeature * feature)
788 /* FIXME 0.9: get reference counting somewhat right in here,
789 * GstPluginFeatures should probably be GstObjects that are sinked when
790 * adding them to a plugin */
791 g_return_if_fail (plugin != NULL);
792 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
793 g_return_if_fail (feature != NULL);
794 g_return_if_fail (feature->plugin == NULL);
796 /* gst_object_sink (feature); */
797 feature->plugin = plugin;
798 plugin->features = g_list_prepend (plugin->features, feature);
799 plugin->numfeatures++;
803 * gst_plugin_get_feature_list:
804 * @plugin: the plugin to get the features from
806 * get a list of all the features that this plugin provides
808 * Returns: a GList of features, use g_list_free to free the list.
811 gst_plugin_get_feature_list (GstPlugin * plugin)
816 g_return_val_if_fail (plugin != NULL, NULL);
818 list = g_list_copy (plugin->features);
819 for (g = list; g; g = g->next) {
820 gst_object_ref (plugin);
826 /* FIXME is this function necessary? */
829 * @name: name of plugin to load
831 * Load the named plugin.
833 * Returns: whether the plugin was loaded or not
836 gst_plugin_load_1 (const gchar * name)
839 GError *error = NULL;
841 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
843 plugin = gst_plugin_load_file (plugin->filename, &error);
845 GST_WARNING ("load_plugin error: %s\n", error->message);
846 g_error_free (error);
852 GST_DEBUG ("Could not find %s in registry pool", name);
857 gst_plugin_load (GstPlugin * plugin)
859 GError *error = NULL;
861 if (gst_plugin_is_loaded (plugin)) {
865 if (!plugin->filename) {
869 plugin = gst_plugin_load_file (plugin->filename, &error);
871 GST_WARNING ("load_plugin error: %s\n", error->message);
872 g_error_free (error);