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);
81 static void gst_plugin_desc_free (GstPluginDesc * desc);
84 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
86 static GstObjectClass *parent_class = NULL;
89 gst_plugin_init (GstPlugin * plugin)
95 gst_plugin_finalize (GObject * object)
97 GstPlugin *plugin = GST_PLUGIN (object);
98 GstRegistry *registry = gst_registry_get_default ();
101 GST_DEBUG ("finalizing plugin %p", plugin);
102 for (g = registry->plugins; g; g = g->next) {
103 if (g->data == (gpointer) plugin) {
104 g_warning ("removing plugin that is still in registry");
107 g_free (plugin->filename);
108 gst_plugin_desc_free (&plugin->desc);
110 G_OBJECT_CLASS (parent_class)->finalize (object);
114 gst_plugin_class_init (GstPluginClass * klass)
116 parent_class = g_type_class_ref (GST_TYPE_OBJECT);
118 G_OBJECT_CLASS (klass)->finalize = GST_DEBUG_FUNCPTR (gst_plugin_finalize);
122 gst_plugin_error_quark (void)
124 static GQuark quark = 0;
127 quark = g_quark_from_static_string ("gst_plugin_error");
131 /* this function can be called in the GCC constructor extension, before
132 * the _gst_plugin_initialize() was called. In that case, we store the
133 * plugin description in a list to initialize it when we open the main
135 * When the main module is known, we can register the plugin right away.
138 _gst_plugin_register_static (GstPluginDesc * desc)
140 if (main_module == NULL) {
142 GST_LOG ("queueing static plugin \"%s\" for loading later on",
144 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
149 GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
150 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
151 if (gst_plugin_register_func (plugin, main_module, desc)) {
153 GST_INFO ("loaded static plugin \"%s\"", desc->name);
154 gst_default_registry_add_plugin (plugin);
160 _gst_plugin_initialize (void)
162 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
164 /* now register all static plugins */
165 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static,
169 /* this function could be extended to check if the plugin license matches the
170 * applications license (would require the app to register its license somehow).
171 * We'll wait for someone who's interested in it to code it :)
174 gst_plugin_check_license (const gchar * license)
176 gchar **check_license = valid_licenses;
178 g_assert (check_license);
180 while (*check_license) {
181 if (strcmp (license, *check_license) == 0)
189 gst_plugin_check_version (gint major, gint minor)
191 /* return NULL if the major and minor version numbers are not compatible */
193 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
200 gst_plugin_register_func (GstPlugin * plugin, GModule * module,
201 GstPluginDesc * desc)
203 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
205 GST_INFO ("plugin \"%s\" has incompatible version, not loading",
210 if (!desc->license || !desc->description || !desc->source ||
211 !desc->package || !desc->origin) {
213 GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
218 if (!gst_plugin_check_license (desc->license)) {
220 GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
221 plugin->filename, desc->license);
226 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
228 gst_plugin_desc_copy (&plugin->desc, desc);
230 if (!((desc->plugin_init) (plugin))) {
232 GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
233 plugin->module = NULL;
238 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
245 * _gst_plugin_fault_handler_restore:
246 * segfault handler restorer
249 _gst_plugin_fault_handler_restore (void)
251 struct sigaction action;
253 memset (&action, 0, sizeof (action));
254 action.sa_handler = SIG_DFL;
256 sigaction (SIGSEGV, &action, NULL);
260 * _gst_plugin_fault_handler_sighandler:
261 * segfault handler implementation
264 _gst_plugin_fault_handler_sighandler (int signum)
266 /* We need to restore the fault handler or we'll keep getting it */
267 _gst_plugin_fault_handler_restore ();
271 g_print ("\nERROR: ");
272 g_print ("Caught a segmentation fault while loading plugin file:\n");
273 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
274 g_print ("Please either:\n");
275 g_print ("- remove it and restart.\n");
276 g_print ("- run with --gst-disable-segtrap and debug.\n");
280 g_print ("Caught unhandled signal on plugin loading\n");
286 * _gst_plugin_fault_handler_setup:
287 * sets up the segfault handler
290 _gst_plugin_fault_handler_setup (void)
292 struct sigaction action;
294 /* if asked to leave segfaults alone, just return */
295 if (_gst_disable_segtrap)
298 if (_gst_plugin_fault_handler_is_setup)
301 memset (&action, 0, sizeof (action));
302 action.sa_handler = _gst_plugin_fault_handler_sighandler;
304 sigaction (SIGSEGV, &action, NULL);
308 _gst_plugin_fault_handler_restore (void)
313 _gst_plugin_fault_handler_setup (void)
318 static void _gst_plugin_fault_handler_setup ();
320 GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
323 * gst_plugin_load_file:
324 * @filename: the plugin filename to load
325 * @error: pointer to a NULL-valued GError
327 * Loads the given plugin and refs it. Caller needs to unref after use.
329 * Returns: a new GstPlugin or NULL, if an error occurred.
332 gst_plugin_load_file (const gchar * filename, GError ** error)
338 struct stat file_status;
339 GstRegistry *registry;
341 g_return_val_if_fail (filename != NULL, NULL);
343 registry = gst_registry_get_default ();
344 g_static_mutex_lock (&gst_plugin_loading_mutex);
346 plugin = gst_registry_lookup (registry, filename);
348 if (plugin->module) {
349 g_static_mutex_unlock (&gst_plugin_loading_mutex);
352 gst_object_unref (plugin);
356 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
359 if (g_module_supported () == FALSE) {
360 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
363 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
367 if (stat (filename, &file_status)) {
368 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
371 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s\n", filename,
376 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
377 if (module == NULL) {
378 GST_CAT_ERROR (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
381 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed");
385 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
387 plugin->module = module;
388 plugin->filename = strdup (filename);
389 plugin->file_mtime = file_status.st_mtime;
390 plugin->file_size = file_status.st_size;
392 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
394 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
397 GST_PLUGIN_ERROR_MODULE,
398 "Could not find plugin entry point in \"%s\"", filename);
401 plugin->orig_desc = (GstPluginDesc *) ptr;
403 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
406 /* this is where we load the actual .so, so let's trap SIGSEGV */
407 _gst_plugin_fault_handler_setup ();
408 _gst_plugin_fault_handler_filename = plugin->filename;
410 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
413 if (!gst_plugin_register_func (plugin, module, plugin->orig_desc)) {
414 /* remove signal handler */
415 _gst_plugin_fault_handler_restore ();
416 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
420 GST_PLUGIN_ERROR_MODULE,
421 "gst_plugin_register_func failed for plugin \"%s\"", filename);
422 g_module_close (module);
426 /* remove signal handler */
427 _gst_plugin_fault_handler_restore ();
428 _gst_plugin_fault_handler_filename = NULL;
429 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
431 gst_object_ref (plugin);
432 gst_default_registry_add_plugin (plugin);
434 g_static_mutex_unlock (&gst_plugin_loading_mutex);
438 gst_object_unref (plugin);
439 g_static_mutex_unlock (&gst_plugin_loading_mutex);
444 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
446 dest->major_version = src->major_version;
447 dest->minor_version = src->minor_version;
448 dest->name = g_strdup (src->name);
449 dest->description = g_strdup (src->description);
450 dest->plugin_init = src->plugin_init;
451 dest->version = g_strdup (src->version);
452 dest->license = g_strdup (src->license);
453 dest->source = g_strdup (src->source);
454 dest->package = g_strdup (src->package);
455 dest->origin = g_strdup (src->origin);
460 gst_plugin_desc_free (GstPluginDesc * desc)
463 g_free (desc->description);
464 g_free (desc->version);
465 g_free (desc->license);
466 g_free (desc->source);
467 g_free (desc->package);
468 g_free (desc->origin);
470 memset (desc, 0, sizeof (GstPluginDesc));
474 * gst_plugin_get_name:
475 * @plugin: plugin to get the name of
477 * Get the short name of the plugin
479 * Returns: the name of the plugin
482 gst_plugin_get_name (GstPlugin * plugin)
484 g_return_val_if_fail (plugin != NULL, NULL);
486 return plugin->desc.name;
490 * gst_plugin_get_description:
491 * @plugin: plugin to get long name of
493 * Get the long descriptive name of the plugin
495 * Returns: the long name of the plugin
497 G_CONST_RETURN gchar *
498 gst_plugin_get_description (GstPlugin * plugin)
500 g_return_val_if_fail (plugin != NULL, NULL);
502 return plugin->desc.description;
506 * gst_plugin_get_filename:
507 * @plugin: plugin to get the filename of
509 * get the filename of the plugin
511 * Returns: the filename of the plugin
513 G_CONST_RETURN gchar *
514 gst_plugin_get_filename (GstPlugin * plugin)
516 g_return_val_if_fail (plugin != NULL, NULL);
518 return plugin->filename;
522 * gst_plugin_get_version:
523 * @plugin: plugin to get the version of
525 * get the version of the plugin
527 * Returns: the version of the plugin
529 G_CONST_RETURN gchar *
530 gst_plugin_get_version (GstPlugin * plugin)
532 g_return_val_if_fail (plugin != NULL, NULL);
534 return plugin->desc.version;
538 * gst_plugin_get_license:
539 * @plugin: plugin to get the license of
541 * get the license of the plugin
543 * Returns: the license of the plugin
545 G_CONST_RETURN gchar *
546 gst_plugin_get_license (GstPlugin * plugin)
548 g_return_val_if_fail (plugin != NULL, NULL);
550 return plugin->desc.license;
554 * gst_plugin_get_source:
555 * @plugin: plugin to get the source of
557 * get the source module the plugin belongs to.
559 * Returns: the source of the plugin
561 G_CONST_RETURN gchar *
562 gst_plugin_get_source (GstPlugin * plugin)
564 g_return_val_if_fail (plugin != NULL, NULL);
566 return plugin->desc.source;
570 * gst_plugin_get_package:
571 * @plugin: plugin to get the package of
573 * get the package the plugin belongs to.
575 * Returns: the package of the plugin
577 G_CONST_RETURN gchar *
578 gst_plugin_get_package (GstPlugin * plugin)
580 g_return_val_if_fail (plugin != NULL, NULL);
582 return plugin->desc.package;
586 * gst_plugin_get_origin:
587 * @plugin: plugin to get the origin of
589 * get the URL where the plugin comes from
591 * Returns: the origin of the plugin
593 G_CONST_RETURN gchar *
594 gst_plugin_get_origin (GstPlugin * plugin)
596 g_return_val_if_fail (plugin != NULL, NULL);
598 return plugin->desc.origin;
602 * gst_plugin_get_module:
603 * @plugin: plugin to query
605 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
608 * Returns: module belonging to the plugin or NULL if the plugin isn't
612 gst_plugin_get_module (GstPlugin * plugin)
614 g_return_val_if_fail (plugin != NULL, NULL);
616 return plugin->module;
620 * gst_plugin_is_loaded:
621 * @plugin: plugin to query
623 * queries if the plugin is loaded into memory
625 * Returns: TRUE is loaded, FALSE otherwise
628 gst_plugin_is_loaded (GstPlugin * plugin)
630 g_return_val_if_fail (plugin != NULL, FALSE);
632 return (plugin->module != NULL || plugin->filename == NULL);
637 * gst_plugin_feature_list:
638 * @plugin: plugin to query
639 * @filter: the filter to use
640 * @first: only return first match
641 * @user_data: user data passed to the filter function
643 * Runs a filter against all plugin features and returns a GList with
644 * the results. If the first flag is set, only the first match is
645 * returned (as a list with a single object).
647 * Returns: a GList of features, g_list_free after use.
650 gst_plugin_feature_filter (GstPlugin * plugin,
651 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
656 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
658 for (g = list; g; g = g->next) {
659 gst_object_ref (plugin);
667 GstPluginFeatureFilter filter;
675 _feature_filter (GstPlugin * plugin, gpointer user_data)
678 FeatureFilterData *data = (FeatureFilterData *) user_data;
680 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
683 data->result = g_list_concat (data->result, result);
690 * gst_plugin_list_feature_filter:
691 * @list: a #GList of plugins to query
692 * @filter: the filter function to use
693 * @first: only return first match
694 * @user_data: user data passed to the filter function
696 * Runs a filter against all plugin features of the plugins in the given
697 * list and returns a GList with the results.
698 * If the first flag is set, only the first match is
699 * returned (as a list with a single object).
701 * Returns: a GList of features, g_list_free after use.
704 gst_plugin_list_feature_filter (GList * list,
705 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
707 FeatureFilterData data;
710 data.filter = filter;
712 data.user_data = user_data;
715 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
716 g_list_free (result);
723 * gst_plugin_name_filter:
724 * @plugin: the plugin to check
725 * @name: the name of the plugin
727 * A standard filter that returns TRUE when the plugin is of the
730 * Returns: TRUE if the plugin is of the given name.
733 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
735 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
740 * gst_plugin_find_feature:
741 * @plugin: plugin to get the feature from
742 * @name: The name of the feature to find
743 * @type: The type of the feature to find
745 * Find a feature of the given name and type in the given plugin.
747 * Returns: a GstPluginFeature or NULL if the feature was not found.
750 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
753 GstPluginFeature *result = NULL;
754 GstTypeNameData data;
756 g_return_val_if_fail (name != NULL, NULL);
761 walk = gst_filter_run (plugin->features,
762 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
765 result = GST_PLUGIN_FEATURE (walk->data);
767 gst_object_ref (result);
768 gst_plugin_feature_list_free (walk);
777 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
779 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
785 * gst_plugin_find_feature_by_name:
786 * @plugin: plugin to get the feature from
787 * @name: The name of the feature to find
789 * Find a feature of the given name in the given plugin.
791 * Returns: a GstPluginFeature or NULL if the feature was not found.
794 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
797 GstPluginFeature *result = NULL;
799 g_return_val_if_fail (name != NULL, NULL);
801 walk = gst_filter_run (plugin->features,
802 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
805 result = GST_PLUGIN_FEATURE (walk->data);
807 gst_object_ref (result);
808 gst_plugin_feature_list_free (walk);
816 * gst_plugin_load_by_name:
817 * @name: name of plugin to load
819 * Load the named plugin. Refs the plugin.
821 * Returns: whether the plugin was loaded or not
824 gst_plugin_load_by_name (const gchar * name)
826 GstPlugin *plugin, *newplugin;
827 GError *error = NULL;
829 GST_DEBUG ("looking up plugin %s in default registry", name);
830 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
832 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
833 newplugin = gst_plugin_load_file (plugin->filename, &error);
834 gst_object_unref (plugin);
837 GST_WARNING ("load_plugin error: %s\n", error->message);
838 g_error_free (error);
841 /* newplugin was reffed by load_file */
845 GST_DEBUG ("Could not find plugin %s in registry", name);
850 gst_plugin_load (GstPlugin * plugin)
852 GError *error = NULL;
853 GstPlugin *newplugin;
855 if (gst_plugin_is_loaded (plugin)) {
859 newplugin = gst_plugin_load_file (plugin->filename, &error);
860 if (newplugin == NULL) {
861 GST_WARNING ("load_plugin error: %s\n", error->message);
862 g_error_free (error);
863 gst_object_unref (plugin);
867 gst_object_unref (plugin);
873 gst_plugin_list_free (GList * list)
877 for (g = list; g; g = g->next) {
878 gst_object_unref (GST_PLUGIN (g->data));