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.
23 #include <sys/types.h>
29 #include "gst_private.h"
31 #include "gstplugin.h"
32 #include "gstversion.h"
33 #include "gstregistrypool.h"
36 #include "gstfilter.h"
39 #ifndef GST_DISABLE_GST_DEBUG
40 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
42 #define GST_CAT_DEFAULT 0
45 static GModule *main_module = NULL;
46 static GList *_gst_plugin_static = NULL;
48 /* static variables for segfault handling of plugin loading */
49 static char *_gst_plugin_fault_handler_filename = NULL;
50 extern gboolean *_gst_disable_segtrap; /* see gst.c */
51 static gboolean *_gst_plugin_fault_handler_is_setup = FALSE;
53 /* list of valid licenses.
54 * One of these must be specified or the plugin won't be loaded
55 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
58 * GPL: http://www.gnu.org/copyleft/gpl.html
59 * LGPL: http://www.gnu.org/copyleft/lesser.html
60 * QPL: http://www.trolltech.com/licenses/qpl.html
62 static gchar *valid_licenses[] = {
63 "LGPL", /* GNU Lesser General Public License */
64 "GPL", /* GNU General Public License */
65 "QPL", /* Trolltech Qt Public License */
66 "GPL/QPL", /* Combi-license of GPL + QPL */
67 GST_LICENSE_UNKNOWN, /* some other license */
71 static void gst_plugin_desc_copy (GstPluginDesc *dest,
72 const GstPluginDesc *src);
74 static GstPlugin * gst_plugin_register_func (GstPlugin *plugin,
79 gst_plugin_copy (GstPlugin *plugin)
81 return g_memdup(plugin, sizeof(*plugin));
85 gst_plugin_get_type (void)
87 static GType plugin_type;
89 if (plugin_type == 0) {
90 plugin_type = g_boxed_type_register_static ("GstPlugin",
91 (GBoxedCopyFunc) gst_plugin_copy, g_free);
98 gst_plugin_error_quark (void)
100 static GQuark quark = 0;
102 quark = g_quark_from_static_string ("gst_plugin_error");
106 /* this function can be called in the GCC constructor extension, before
107 * the _gst_plugin_initialize() was called. In that case, we store the
108 * plugin description in a list to initialize it when we open the main
110 * When the main module is known, we can register the plugin right away.
113 _gst_plugin_register_static (GstPluginDesc *desc)
115 if (main_module == NULL) {
116 if (GST_CAT_DEFAULT) GST_LOG ("queueing static plugin \"%s\" for loading later on", desc->name);
117 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
122 if (GST_CAT_DEFAULT) GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
123 plugin = g_new0 (GstPlugin, 1);
124 if (gst_plugin_register_func (plugin, main_module, desc)) {
125 if (GST_CAT_DEFAULT) GST_INFO ("loaded static plugin \"%s\"", desc->name);
126 gst_registry_pool_add_plugin (plugin);
132 _gst_plugin_initialize (void)
134 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
136 /* now register all static plugins */
137 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static, NULL);
140 /* this function could be extended to check if the plugin license matches the
141 * applications license (would require the app to register its license somehow).
142 * We'll wait for someone who's interested in it to code it :)
145 gst_plugin_check_license (const gchar *license)
147 gchar **check_license = valid_licenses;
149 g_assert (check_license);
151 while (*check_license) {
152 if (strcmp (license, *check_license) == 0)
160 gst_plugin_check_version (gint major, gint minor)
162 /* return NULL if the major and minor version numbers are not compatible */
164 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
171 gst_plugin_register_func (GstPlugin *plugin, GModule *module, GstPluginDesc *desc)
173 g_assert (plugin->module == NULL);
175 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
176 if (GST_CAT_DEFAULT) GST_INFO ("plugin \"%s\" has incompatible version, not loading",
181 if (!desc->license || !desc->description || !desc->package ||
183 if (GST_CAT_DEFAULT) GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
188 if (!gst_plugin_check_license (desc->license)) {
189 if (GST_CAT_DEFAULT) GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
190 plugin->filename, desc->license);
194 gst_plugin_desc_copy (&plugin->desc, desc);
195 plugin->module = module;
197 if (!((desc->plugin_init) (plugin))) {
198 if (GST_CAT_DEFAULT) GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
199 plugin->module = NULL;
203 if (GST_CAT_DEFAULT) GST_DEBUG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
209 * _gst_plugin_fault_handler_restore:
210 * segfault handler restorer
213 _gst_plugin_fault_handler_restore (void)
215 struct sigaction action;
217 memset (&action, 0, sizeof (action));
218 action.sa_handler = SIG_DFL;
220 sigaction (SIGSEGV, &action, NULL);
224 * _gst_plugin_fault_handler_sighandler:
225 * segfault handler implementation
228 _gst_plugin_fault_handler_sighandler (int signum)
230 /* We need to restore the fault handler or we'll keep getting it */
231 _gst_plugin_fault_handler_restore ();
236 g_print ("\nERROR: ");
237 g_print ("Caught a segmentation fault while loading plugin file:\n");
238 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
239 g_print ("Please either:\n");
240 g_print ("- remove it and restart.\n");
241 g_print ("- run with --gst-disable-segtrap and debug.\n");
245 g_print ("Caught unhandled signal on plugin loading\n");
251 * _gst_plugin_fault_handler_setup:
252 * sets up the segfault handler
255 _gst_plugin_fault_handler_setup (void)
257 struct sigaction action;
259 /* if asked to leave segfaults alone, just return */
260 if (_gst_disable_segtrap) return;
262 if (_gst_plugin_fault_handler_is_setup) return;
264 memset (&action, 0, sizeof (action));
265 action.sa_handler = _gst_plugin_fault_handler_sighandler;
267 sigaction (SIGSEGV, &action, NULL);
271 _gst_plugin_fault_handler_setup ();
274 * gst_plugin_load_file:
275 * @plugin: The plugin to load
276 * @error: Pointer to a NULL-valued GError.
278 * Load the given plugin.
280 * Returns: a new GstPlugin or NULL, if an error occurred.
283 gst_plugin_load_file (const gchar *filename, GError **error)
288 struct stat file_status;
289 gboolean free_plugin;
291 g_return_val_if_fail (filename != NULL, NULL);
293 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"", filename);
295 if (g_module_supported () == FALSE) {
298 GST_PLUGIN_ERROR_MODULE,
299 "Dynamic loading not supported");
303 if (stat (filename, &file_status)) {
306 GST_PLUGIN_ERROR_MODULE,
307 "Problem opening file %s\n",
312 module = g_module_open (filename, G_MODULE_BIND_LAZY);
314 if (module != NULL) {
317 if (g_module_symbol (module, "gst_plugin_desc", &ptr)) {
318 desc = (GstPluginDesc *) ptr;
320 plugin = gst_registry_pool_find_plugin (desc->name);
323 plugin = g_new0 (GstPlugin, 1);
324 plugin->filename = g_strdup (filename);
325 GST_DEBUG ("created new GstPlugin %p for file \"%s\"", plugin, filename);
328 if (gst_plugin_is_loaded (plugin)) {
329 if (strcmp (plugin->filename, filename) != 0) {
330 GST_WARNING ("plugin %p from file \"%s\" with same name %s is already "
331 "loaded, aborting loading of \"%s\"",
332 plugin, plugin->filename, plugin->desc.name, filename);
335 GST_PLUGIN_ERROR_NAME_MISMATCH,
336 "already a plugin with name \"%s\" loaded",
338 if (free_plugin) g_free (plugin);
341 GST_LOG ("Plugin %p for file \"%s\" already loaded, returning it now", plugin, filename);
345 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...", plugin, filename);
347 if (g_module_symbol (module, "plugin_init", &ptr)) {
348 g_print ("plugin %p from file \"%s\" exports a symbol named plugin_init\n",
349 plugin, plugin->filename);
352 GST_PLUGIN_ERROR_NAME_MISMATCH,
353 "plugin \"%s\" exports a symbol named plugin_init",
357 /* this is where we load the actual .so, so let's trap SIGSEGV */
358 _gst_plugin_fault_handler_setup ();
359 _gst_plugin_fault_handler_filename = plugin->filename;
361 if (gst_plugin_register_func (plugin, module, desc)) {
362 /* remove signal handler */
363 _gst_plugin_fault_handler_restore ();
364 _gst_plugin_fault_handler_filename = NULL;
365 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
368 /* remove signal handler */
369 _gst_plugin_fault_handler_restore ();
370 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
374 GST_PLUGIN_ERROR_MODULE,
375 "gst_plugin_register_func failed for plugin \"%s\"",
377 if (free_plugin) g_free (plugin);
381 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
384 GST_PLUGIN_ERROR_MODULE,
385 "Could not find plugin entry point in \"%s\"",
390 GST_DEBUG ("Error loading plugin %s, reason: %s\n", filename, g_module_error());
393 GST_PLUGIN_ERROR_MODULE,
394 "Error loading plugin %s, reason: %s\n",
395 filename, g_module_error());
401 gst_plugin_desc_copy (GstPluginDesc *dest, const GstPluginDesc *src)
403 dest->major_version = src->major_version;
404 dest->minor_version = src->minor_version;
406 dest->name = g_strdup (src->name);
407 g_free (dest->description);
408 dest->description = g_strdup (src->description);
409 dest->plugin_init = src->plugin_init;
410 dest->plugin_exit = src->plugin_exit;
411 g_free (dest->version);
412 dest->version = g_strdup (src->version);
413 g_free (dest->license);
414 dest->license = g_strdup (src->license);
415 g_free (dest->package);
416 dest->package = g_strdup (src->package);
417 g_free (dest->origin);
418 dest->origin = g_strdup (src->origin);
423 gst_plugin_desc_free (GstPluginDesc *desc)
426 g_free (desc->description);
427 g_free (desc->version);
428 g_free (desc->license);
429 g_free (desc->package);
430 g_free (desc->origin);
432 memset (desc, 0, sizeof (GstPluginDesc));
436 * gst_plugin_unload_plugin:
437 * @plugin: The plugin to unload
439 * Unload the given plugin.
441 * Returns: whether or not the plugin unloaded
444 gst_plugin_unload_plugin (GstPlugin *plugin)
446 g_return_val_if_fail (plugin != NULL, FALSE);
451 if (g_module_close (plugin->module)) {
452 plugin->module = NULL;
453 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" unloaded", plugin->filename);
457 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "failed to unload plugin \"%s\"", plugin->filename);
463 * gst_plugin_get_name:
464 * @plugin: plugin to get the name of
466 * Get the short name of the plugin
468 * Returns: the name of the plugin
471 gst_plugin_get_name (GstPlugin *plugin)
473 g_return_val_if_fail (plugin != NULL, NULL);
475 return plugin->desc.name;
479 * gst_plugin_get_description:
480 * @plugin: plugin to get long name of
482 * Get the long descriptive name of the plugin
484 * Returns: the long name of the plugin
486 G_CONST_RETURN gchar*
487 gst_plugin_get_description (GstPlugin *plugin)
489 g_return_val_if_fail (plugin != NULL, NULL);
491 return plugin->desc.description;
495 * gst_plugin_get_filename:
496 * @plugin: plugin to get the filename of
498 * get the filename of the plugin
500 * Returns: the filename of the plugin
502 G_CONST_RETURN gchar*
503 gst_plugin_get_filename (GstPlugin *plugin)
505 g_return_val_if_fail (plugin != NULL, NULL);
507 return plugin->filename;
510 * gst_plugin_get_license:
511 * @plugin: plugin to get the license of
513 * get the license of the plugin
515 * Returns: the license of the plugin
517 G_CONST_RETURN gchar*
518 gst_plugin_get_license (GstPlugin *plugin)
520 g_return_val_if_fail (plugin != NULL, NULL);
522 return plugin->desc.license;
525 * gst_plugin_get_package:
526 * @plugin: plugin to get the package of
528 * get the package the plugin belongs to.
530 * Returns: the package of the plugin
532 G_CONST_RETURN gchar*
533 gst_plugin_get_package (GstPlugin *plugin)
535 g_return_val_if_fail (plugin != NULL, NULL);
537 return plugin->desc.package;
540 * gst_plugin_get_origin:
541 * @plugin: plugin to get the origin of
543 * get the URL where the plugin comes from
545 * Returns: the origin of the plugin
547 G_CONST_RETURN gchar*
548 gst_plugin_get_origin (GstPlugin *plugin)
550 g_return_val_if_fail (plugin != NULL, NULL);
552 return plugin->desc.origin;
555 * gst_plugin_get_module:
556 * @plugin: plugin to query
558 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
561 * Returns: module belonging to the plugin or NULL if the plugin isn't
565 gst_plugin_get_module (GstPlugin *plugin)
567 g_return_val_if_fail (plugin != NULL, FALSE);
569 return plugin->module;
572 * gst_plugin_is_loaded:
573 * @plugin: plugin to query
575 * queries if the plugin is loaded into memory
577 * Returns: TRUE is loaded, FALSE otherwise
580 gst_plugin_is_loaded (GstPlugin *plugin)
582 g_return_val_if_fail (plugin != NULL, FALSE);
584 return (plugin->module != NULL);
588 * gst_plugin_feature_list:
589 * @plugin: plugin to query
590 * @filter: the filter to use
591 * @first: only return first match
592 * @user_data: user data passed to the filter function
594 * Runs a filter against all plugin features and returns a GList with
595 * the results. If the first flag is set, only the first match is
596 * returned (as a list with a single object).
598 * Returns: a GList of features, g_list_free after use.
601 gst_plugin_feature_filter (GstPlugin *plugin,
602 GstPluginFeatureFilter filter,
606 return gst_filter_run (plugin->features, (GstFilterFunc) filter, first, user_data);
611 GstPluginFeatureFilter filter;
618 _feature_filter (GstPlugin *plugin, gpointer user_data)
621 FeatureFilterData *data = (FeatureFilterData *) user_data;
623 result = gst_plugin_feature_filter (plugin, data->filter, data->first, data->user_data);
625 data->result = g_list_concat (data->result, result);
632 * gst_plugin_list_feature_list:
633 * @list: a list of plugins to query
634 * @filter: the filter to use
635 * @first: only return first match
636 * @user_data: user data passed to the filter function
638 * Runs a filter against all plugin features of the plugins in the given
639 * list and returns a GList with the results.
640 * If the first flag is set, only the first match is
641 * returned (as a list with a single object).
643 * Returns: a GList of features, g_list_free after use.
646 gst_plugin_list_feature_filter (GList *list,
647 GstPluginFeatureFilter filter,
651 FeatureFilterData data;
654 data.filter = filter;
656 data.user_data = user_data;
659 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
660 g_list_free (result);
666 * gst_plugin_name_filter:
667 * @plugin: the plugin to check
668 * @name: the name of the plugin
670 * A standard filter that returns TRUE when the plugin is of the
673 * Returns: TRUE if the plugin is of the given name.
676 gst_plugin_name_filter (GstPlugin *plugin, const gchar *name)
678 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
682 * gst_plugin_find_feature:
683 * @plugin: plugin to get the feature from
684 * @name: The name of the feature to find
685 * @type: The type of the feature to find
687 * Find a feature of the given name and type in the given plugin.
689 * Returns: a GstPluginFeature or NULL if the feature was not found.
692 gst_plugin_find_feature (GstPlugin *plugin, const gchar *name, GType type)
695 GstPluginFeature *result = NULL;
696 GstTypeNameData data;
698 g_return_val_if_fail (name != NULL, NULL);
703 walk = gst_filter_run (plugin->features,
704 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE,
708 result = GST_PLUGIN_FEATURE (walk->data);
714 * gst_plugin_add_feature:
715 * @plugin: plugin to add feature to
716 * @feature: feature to add
718 * Add feature to the list of those provided by the plugin.
719 * There is a separate namespace for each plugin feature type.
720 * See #gst_plugin_get_feature_list
723 gst_plugin_add_feature (GstPlugin *plugin, GstPluginFeature *feature)
725 GstPluginFeature *oldfeature;
727 g_return_if_fail (plugin != NULL);
728 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
729 g_return_if_fail (feature != NULL);
731 oldfeature = gst_plugin_find_feature (plugin,
732 GST_PLUGIN_FEATURE_NAME (feature), G_OBJECT_TYPE (feature));
735 feature->manager = plugin;
736 plugin->features = g_list_prepend (plugin->features, feature);
737 plugin->numfeatures++;
742 * gst_plugin_get_feature_list:
743 * @plugin: the plugin to get the features from
745 * get a list of all the features that this plugin provides
747 * Returns: a GList of features, use g_list_free to free the list.
750 gst_plugin_get_feature_list (GstPlugin *plugin)
752 g_return_val_if_fail (plugin != NULL, NULL);
754 return g_list_copy (plugin->features);
759 * @name: name of plugin to load
761 * Load the named plugin.
763 * Returns: whether the plugin was loaded or not
766 gst_plugin_load (const gchar *name)
769 GError *error = NULL;
771 plugin = gst_registry_pool_find_plugin (name);
773 gst_plugin_load_file (plugin->filename, &error);
775 GST_WARNING ("load_plugin error: %s\n", error->message);
776 g_error_free (error);
782 GST_DEBUG ("Could not find %s in registry pool", name);
788 * @name: name of library to load
790 * Load the named library. Name should be given as
791 * "liblibrary.so".
793 * Returns: whether the library was loaded or not
796 gst_library_load (const gchar *name)
800 /* for now this is the same */
801 res = gst_plugin_load (name);