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"
38 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
40 static GModule *main_module = NULL;
41 static GList *_gst_plugin_static = NULL;
43 /* static variables for segfault handling of plugin loading */
44 static char *_gst_plugin_fault_handler_filename = NULL;
45 extern gboolean *_gst_enable_segfault; /* see gst.c */
46 static gboolean *_gst_plugin_fault_handler_is_setup = FALSE;
48 /* list of valid licenses.
49 * One of these must be specified or the plugin won't be loaded
50 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
53 * GPL: http://www.gnu.org/copyleft/gpl.html
54 * LGPL: http://www.gnu.org/copyleft/lesser.html
55 * QPL: http://www.trolltech.com/licenses/qpl.html
57 static gchar *valid_licenses[] = {
58 "LGPL", /* GNU Lesser General Public License */
59 "GPL", /* GNU General Public License */
60 "QPL", /* Trolltech Qt Public License */
61 "GPL/QPL", /* Combi-license of GPL + QPL */
62 GST_LICENSE_UNKNOWN, /* some other license */
66 static void gst_plugin_desc_copy (GstPluginDesc *dest,
67 const GstPluginDesc *src);
69 static GstPlugin * gst_plugin_register_func (GstPlugin *plugin,
74 gst_plugin_copy (GstPlugin *plugin)
76 return g_memdup(plugin, sizeof(*plugin));
80 gst_plugin_get_type (void)
82 static GType plugin_type;
84 if (plugin_type == 0) {
85 plugin_type = g_boxed_type_register_static ("GstPlugin",
86 (GBoxedCopyFunc) gst_plugin_copy, g_free);
93 gst_plugin_error_quark (void)
95 static GQuark quark = 0;
97 quark = g_quark_from_static_string ("gst_plugin_error");
101 /* this function can be called in the GCC constructor extension, before
102 * the _gst_plugin_initialize() was called. In that case, we store the
103 * plugin description in a list to initialize it when we open the main
105 * When the main module is known, we can register the plugin right away.
108 _gst_plugin_register_static (GstPluginDesc *desc)
110 if (main_module == NULL) {
111 if (GST_CAT_DEFAULT) GST_LOG ("queueing static plugin \"%s\" for loading later on", desc->name);
112 _gst_plugin_static = g_list_prepend (_gst_plugin_static, desc);
117 if (GST_CAT_DEFAULT) GST_LOG ("attempting to load static plugin \"%s\" now...", desc->name);
118 plugin = g_new0 (GstPlugin, 1);
119 if (gst_plugin_register_func (plugin, main_module, desc)) {
120 if (GST_CAT_DEFAULT) GST_INFO ("loaded static plugin \"%s\"", desc->name);
121 gst_registry_pool_add_plugin (plugin);
127 _gst_plugin_initialize (void)
129 main_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
131 /* now register all static plugins */
132 g_list_foreach (_gst_plugin_static, (GFunc) _gst_plugin_register_static, NULL);
135 /* this function could be extended to check if the plugin license matches the
136 * applications license (would require the app to register its license somehow).
137 * We'll wait for someone who's interested in it to code it :)
140 gst_plugin_check_license (const gchar *license)
142 gchar **check_license = valid_licenses;
144 g_assert (check_license);
146 while (*check_license) {
147 if (strcmp (license, *check_license) == 0)
155 gst_plugin_check_version (gint major, gint minor)
157 /* return NULL if the major and minor version numbers are not compatible */
159 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
166 gst_plugin_register_func (GstPlugin *plugin, GModule *module, GstPluginDesc *desc)
168 g_assert (plugin->module == NULL);
170 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
171 if (GST_CAT_DEFAULT) GST_INFO ("plugin \"%s\" has incompatible version, not loading",
176 if (!desc->license || !desc->description || !desc->package ||
178 if (GST_CAT_DEFAULT) GST_INFO ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
183 if (!gst_plugin_check_license (desc->license)) {
184 if (GST_CAT_DEFAULT) GST_INFO ("plugin \"%s\" has invalid license \"%s\", not loading",
185 plugin->filename, desc->license);
189 gst_plugin_desc_copy (&plugin->desc, desc);
190 plugin->module = module;
192 if (!((desc->plugin_init) (plugin))) {
193 if (GST_CAT_DEFAULT) GST_INFO ("plugin \"%s\" failed to initialise", plugin->filename);
194 plugin->module = NULL;
198 if (GST_CAT_DEFAULT) GST_DEBUG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
204 * _gst_plugin_fault_handler_restore:
205 * segfault handler restorer
208 _gst_plugin_fault_handler_restore (void)
210 struct sigaction action;
212 memset (&action, 0, sizeof (action));
213 action.sa_handler = SIG_DFL;
215 sigaction (SIGSEGV, &action, NULL);
219 * _gst_plugin_fault_handler_sighandler:
220 * segfault handler implementation
223 _gst_plugin_fault_handler_sighandler (int signum)
225 /* We need to restore the fault handler or we'll keep getting it */
226 _gst_plugin_fault_handler_restore ();
231 g_print ("\nERROR:");
232 g_print ("Caught a segmentation fault while loading plugin file:\n");
233 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
234 g_print ("Please either:\n");
235 g_print ("- remove it and restart.\n");
236 g_print ("- run with --gst-enable-segfault and debug.\n");
240 g_print ("Caught unhandled signal on plugin loading\n");
246 * _gst_plugin_fault_handler_setup:
247 * sets up the segfault handler
250 _gst_plugin_fault_handler_setup (void)
252 struct sigaction action;
254 /* if asked to leave segfaults alone, just return */
255 if (_gst_enable_segfault) return;
257 if (_gst_plugin_fault_handler_is_setup) return;
259 memset (&action, 0, sizeof (action));
260 action.sa_handler = _gst_plugin_fault_handler_sighandler;
262 sigaction (SIGSEGV, &action, NULL);
266 _gst_plugin_fault_handler_setup ();
269 * gst_plugin_load_file:
270 * @plugin: The plugin to load
271 * @error: Pointer to a NULL-valued GError.
273 * Load the given plugin.
275 * Returns: a new GstPlugin or NULL, if an error occurred.
278 gst_plugin_load_file (const gchar *filename, GError **error)
283 struct stat file_status;
284 gboolean free_plugin;
286 g_return_val_if_fail (filename != NULL, NULL);
288 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"", filename);
290 if (g_module_supported () == FALSE) {
293 GST_PLUGIN_ERROR_MODULE,
294 "Dynamic loading not supported");
298 if (stat (filename, &file_status)) {
301 GST_PLUGIN_ERROR_MODULE,
302 "Problem opening file %s\n",
307 module = g_module_open (filename, G_MODULE_BIND_LAZY);
309 if (module != NULL) {
312 if (g_module_symbol (module, "gst_plugin_desc", &ptr)) {
313 desc = (GstPluginDesc *) ptr;
315 plugin = gst_registry_pool_find_plugin (desc->name);
318 plugin = g_new0 (GstPlugin, 1);
319 plugin->filename = g_strdup (filename);
320 GST_DEBUG ("created new GstPlugin %p for file \"%s\"", plugin, filename);
323 if (gst_plugin_is_loaded (plugin)) {
324 if (strcmp (plugin->filename, filename) != 0) {
325 GST_WARNING ("plugin %p from file \"%s\" with same name %s is already "
326 "loaded, aborting loading of \"%s\"",
327 plugin, plugin->filename, plugin->desc.name, filename);
330 GST_PLUGIN_ERROR_NAME_MISMATCH,
331 "already a plugin with name \"%s\" loaded",
333 if (free_plugin) g_free (plugin);
336 GST_LOG ("Plugin %p for file \"%s\" already loaded, returning it now", plugin, filename);
340 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...", plugin, filename);
342 if (g_module_symbol (module, "plugin_init", &ptr)) {
343 g_print ("plugin %p from file \"%s\" exports a symbol named plugin_init\n",
344 plugin, plugin->filename);
347 GST_PLUGIN_ERROR_NAME_MISMATCH,
348 "plugin \"%s\" exports a symbol named plugin_init",
352 /* this is where we load the actual .so, so let's trap SIGSEGV */
353 _gst_plugin_fault_handler_setup ();
354 _gst_plugin_fault_handler_filename = plugin->filename;
356 if (gst_plugin_register_func (plugin, module, desc)) {
357 /* remove signal handler */
358 _gst_plugin_fault_handler_restore ();
359 _gst_plugin_fault_handler_filename = NULL;
360 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
363 /* remove signal handler */
364 _gst_plugin_fault_handler_restore ();
365 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
369 GST_PLUGIN_ERROR_MODULE,
370 "gst_plugin_register_func failed for plugin \"%s\"",
372 if (free_plugin) g_free (plugin);
376 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
379 GST_PLUGIN_ERROR_MODULE,
380 "Could not find plugin entry point in \"%s\"",
385 GST_DEBUG ("Error loading plugin %s, reason: %s\n", filename, g_module_error());
388 GST_PLUGIN_ERROR_MODULE,
389 "Error loading plugin %s, reason: %s\n",
390 filename, g_module_error());
396 gst_plugin_desc_copy (GstPluginDesc *dest, const GstPluginDesc *src)
398 dest->major_version = src->major_version;
399 dest->minor_version = src->minor_version;
401 dest->name = g_strdup (src->name);
402 g_free (dest->description);
403 dest->description = g_strdup (src->description);
404 dest->plugin_init = src->plugin_init;
405 dest->plugin_exit = src->plugin_exit;
406 g_free (dest->version);
407 dest->version = g_strdup (src->version);
408 g_free (dest->license);
409 dest->license = g_strdup (src->license);
410 g_free (dest->package);
411 dest->package = g_strdup (src->package);
412 g_free (dest->origin);
413 dest->origin = g_strdup (src->origin);
418 gst_plugin_desc_free (GstPluginDesc *desc)
421 g_free (desc->description);
422 g_free (desc->version);
423 g_free (desc->license);
424 g_free (desc->package);
425 g_free (desc->origin);
427 memset (desc, 0, sizeof (GstPluginDesc));
431 * gst_plugin_unload_plugin:
432 * @plugin: The plugin to unload
434 * Unload the given plugin.
436 * Returns: whether or not the plugin unloaded
439 gst_plugin_unload_plugin (GstPlugin *plugin)
441 g_return_val_if_fail (plugin != NULL, FALSE);
446 if (g_module_close (plugin->module)) {
447 plugin->module = NULL;
448 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "plugin \"%s\" unloaded", plugin->filename);
452 GST_CAT_INFO (GST_CAT_PLUGIN_LOADING, "failed to unload plugin \"%s\"", plugin->filename);
458 * gst_plugin_get_name:
459 * @plugin: plugin to get the name of
461 * Get the short name of the plugin
463 * Returns: the name of the plugin
466 gst_plugin_get_name (GstPlugin *plugin)
468 g_return_val_if_fail (plugin != NULL, NULL);
470 return plugin->desc.name;
474 * gst_plugin_get_longname:
475 * @plugin: plugin to get long name of
477 * Get the long descriptive name of the plugin
479 * Returns: the long name of the plugin
481 G_CONST_RETURN gchar*
482 gst_plugin_get_description (GstPlugin *plugin)
484 g_return_val_if_fail (plugin != NULL, NULL);
486 return plugin->desc.description;
490 * gst_plugin_get_filename:
491 * @plugin: plugin to get the filename of
493 * get the filename of the plugin
495 * Returns: the filename of the plugin
497 G_CONST_RETURN gchar*
498 gst_plugin_get_filename (GstPlugin *plugin)
500 g_return_val_if_fail (plugin != NULL, NULL);
502 return plugin->filename;
505 * gst_plugin_get_license:
506 * @plugin: plugin to get the license of
508 * get the license of the plugin
510 * Returns: the license of the plugin
512 G_CONST_RETURN gchar*
513 gst_plugin_get_license (GstPlugin *plugin)
515 g_return_val_if_fail (plugin != NULL, NULL);
517 return plugin->desc.license;
520 * gst_plugin_get_package:
521 * @plugin: plugin to get the package of
523 * get the package the plugin belongs to.
525 * Returns: the package of the plugin
527 G_CONST_RETURN gchar*
528 gst_plugin_get_package (GstPlugin *plugin)
530 g_return_val_if_fail (plugin != NULL, NULL);
532 return plugin->desc.package;
535 * gst_plugin_get_origin:
536 * @plugin: plugin to get the origin of
538 * get the URL where the plugin comes from
540 * Returns: the origin of the plugin
542 G_CONST_RETURN gchar*
543 gst_plugin_get_origin (GstPlugin *plugin)
545 g_return_val_if_fail (plugin != NULL, NULL);
547 return plugin->desc.origin;
550 * gst_plugin_get_module:
551 * @plugin: plugin to query
553 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
556 * Returns: module belonging to the plugin or NULL if the plugin isn't
560 gst_plugin_get_module (GstPlugin *plugin)
562 g_return_val_if_fail (plugin != NULL, FALSE);
564 return plugin->module;
567 * gst_plugin_is_loaded:
568 * @plugin: plugin to query
570 * queries if the plugin is loaded into memory
572 * Returns: TRUE is loaded, FALSE otherwise
575 gst_plugin_is_loaded (GstPlugin *plugin)
577 g_return_val_if_fail (plugin != NULL, FALSE);
579 return (plugin->module != NULL);
583 * gst_plugin_feature_list:
584 * @plugin: plugin to query
585 * @filter: the filter to use
586 * @first: only return first match
587 * @user_data: user data passed to the filter function
589 * Runs a filter against all plugin features and returns a GList with
590 * the results. If the first flag is set, only the first match is
591 * returned (as a list with a single object).
593 * Returns: a GList of features, g_list_free after use.
596 gst_plugin_feature_filter (GstPlugin *plugin,
597 GstPluginFeatureFilter filter,
601 return gst_filter_run (plugin->features, (GstFilterFunc) filter, first, user_data);
606 GstPluginFeatureFilter filter;
613 _feature_filter (GstPlugin *plugin, gpointer user_data)
616 FeatureFilterData *data = (FeatureFilterData *) user_data;
618 result = gst_plugin_feature_filter (plugin, data->filter, data->first, data->user_data);
620 data->result = g_list_concat (data->result, result);
627 * gst_plugin_list_feature_list:
628 * @list: a list of plugins to query
629 * @filter: the filter to use
630 * @first: only return first match
631 * @user_data: user data passed to the filter function
633 * Runs a filter against all plugin features of the plugins in the given
634 * list and returns a GList with the results.
635 * 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_list_feature_filter (GList *list,
642 GstPluginFeatureFilter filter,
646 FeatureFilterData data;
649 data.filter = filter;
651 data.user_data = user_data;
654 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
655 g_list_free (result);
661 * gst_plugin_name_filter:
662 * @plugin: the plugin to check
663 * @name: the name of the plugin
665 * A standard filter that returns TRUE when the plugin is of the
668 * Returns: TRUE if the plugin is of the given name.
671 gst_plugin_name_filter (GstPlugin *plugin, const gchar *name)
673 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
677 * gst_plugin_find_feature:
678 * @plugin: plugin to get the feature from
679 * @name: The name of the feature to find
680 * @type: The type of the feature to find
682 * Find a feature of the given name and type in the given plugin.
684 * Returns: a GstPluginFeature or NULL if the feature was not found.
687 gst_plugin_find_feature (GstPlugin *plugin, const gchar *name, GType type)
690 GstPluginFeature *result = NULL;
691 GstTypeNameData data;
693 g_return_val_if_fail (name != NULL, NULL);
698 walk = gst_filter_run (plugin->features,
699 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE,
703 result = GST_PLUGIN_FEATURE (walk->data);
709 * gst_plugin_add_feature:
710 * @plugin: plugin to add feature to
711 * @feature: feature to add
713 * Add feature to the list of those provided by the plugin.
714 * There is a separate namespace for each plugin feature type.
715 * See #gst_plugin_get_feature_list
718 gst_plugin_add_feature (GstPlugin *plugin, GstPluginFeature *feature)
720 GstPluginFeature *oldfeature;
722 g_return_if_fail (plugin != NULL);
723 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
724 g_return_if_fail (feature != NULL);
726 oldfeature = gst_plugin_find_feature (plugin,
727 GST_PLUGIN_FEATURE_NAME (feature), G_OBJECT_TYPE (feature));
730 feature->manager = plugin;
731 plugin->features = g_list_prepend (plugin->features, feature);
732 plugin->numfeatures++;
737 * gst_plugin_get_feature_list:
738 * @plugin: the plugin to get the features from
740 * get a list of all the features that this plugin provides
742 * Returns: a GList of features, use g_list_free to free the list.
745 gst_plugin_get_feature_list (GstPlugin *plugin)
747 g_return_val_if_fail (plugin != NULL, NULL);
749 return g_list_copy (plugin->features);
754 * @name: name of plugin to load
756 * Load the named plugin.
758 * Returns: whether the plugin was loaded or not
761 gst_plugin_load (const gchar *name)
764 GError *error = NULL;
766 plugin = gst_registry_pool_find_plugin (name);
768 gst_plugin_load_file (plugin->filename, &error);
770 GST_WARNING ("load_plugin error: %s\n", error->message);
771 g_error_free (error);
777 GST_DEBUG ("Could not find %s in registry pool", name);
783 * @name: name of library to load
785 * Load the named library. Name should be given as
786 * "liblibrary.so".
788 * Returns: whether the library was loaded or not
791 gst_library_load (const gchar *name)
795 /* for now this is the same */
796 res = gst_plugin_load (name);