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_ERROR ("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);
338 if (plugin && plugin->module) {
339 g_static_mutex_unlock (&gst_plugin_loading_mutex);
344 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
347 if (g_module_supported () == FALSE) {
348 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
351 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
355 if (stat (filename, &file_status)) {
356 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
359 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s\n", filename,
364 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
365 if (module == NULL) {
366 GST_CAT_ERROR (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
369 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed");
373 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
375 plugin->module = module;
376 plugin->filename = strdup (filename);
377 plugin->file_mtime = file_status.st_mtime;
378 plugin->file_size = file_status.st_size;
380 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
382 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
385 GST_PLUGIN_ERROR_MODULE,
386 "Could not find plugin entry point in \"%s\"", filename);
389 plugin->orig_desc = (GstPluginDesc *) ptr;
391 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
394 /* this is where we load the actual .so, so let's trap SIGSEGV */
395 _gst_plugin_fault_handler_setup ();
396 _gst_plugin_fault_handler_filename = plugin->filename;
398 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
401 if (!gst_plugin_register_func (plugin, module, plugin->orig_desc)) {
402 /* remove signal handler */
403 _gst_plugin_fault_handler_restore ();
404 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
408 GST_PLUGIN_ERROR_MODULE,
409 "gst_plugin_register_func failed for plugin \"%s\"", filename);
410 g_module_close (module);
414 /* remove signal handler */
415 _gst_plugin_fault_handler_restore ();
416 _gst_plugin_fault_handler_filename = NULL;
417 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
419 gst_object_ref (plugin);
420 gst_default_registry_add_plugin (plugin);
422 g_static_mutex_unlock (&gst_plugin_loading_mutex);
425 g_static_mutex_unlock (&gst_plugin_loading_mutex);
430 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
432 dest->major_version = src->major_version;
433 dest->minor_version = src->minor_version;
434 dest->name = g_strdup (src->name);
435 dest->description = g_strdup (src->description);
436 dest->plugin_init = src->plugin_init;
437 dest->version = g_strdup (src->version);
438 dest->license = g_strdup (src->license);
439 dest->source = g_strdup (src->source);
440 dest->package = g_strdup (src->package);
441 dest->origin = g_strdup (src->origin);
447 gst_plugin_desc_free (GstPluginDesc * desc)
450 g_free (desc->description);
451 g_free (desc->version);
452 g_free (desc->license);
453 g_free (desc->source);
454 g_free (desc->package);
455 g_free (desc->origin);
457 memset (desc, 0, sizeof (GstPluginDesc));
462 * gst_plugin_get_name:
463 * @plugin: plugin to get the name of
465 * Get the short name of the plugin
467 * Returns: the name of the plugin
470 gst_plugin_get_name (GstPlugin * plugin)
472 g_return_val_if_fail (plugin != NULL, NULL);
474 return plugin->desc.name;
478 * gst_plugin_get_description:
479 * @plugin: plugin to get long name of
481 * Get the long descriptive name of the plugin
483 * Returns: the long name of the plugin
485 G_CONST_RETURN gchar *
486 gst_plugin_get_description (GstPlugin * plugin)
488 g_return_val_if_fail (plugin != NULL, NULL);
490 return plugin->desc.description;
494 * gst_plugin_get_filename:
495 * @plugin: plugin to get the filename of
497 * get the filename of the plugin
499 * Returns: the filename of the plugin
501 G_CONST_RETURN gchar *
502 gst_plugin_get_filename (GstPlugin * plugin)
504 g_return_val_if_fail (plugin != NULL, NULL);
506 return plugin->filename;
510 * gst_plugin_get_version:
511 * @plugin: plugin to get the version of
513 * get the version of the plugin
515 * Returns: the version of the plugin
517 G_CONST_RETURN gchar *
518 gst_plugin_get_version (GstPlugin * plugin)
520 g_return_val_if_fail (plugin != NULL, NULL);
522 return plugin->desc.version;
526 * gst_plugin_get_license:
527 * @plugin: plugin to get the license of
529 * get the license of the plugin
531 * Returns: the license of the plugin
533 G_CONST_RETURN gchar *
534 gst_plugin_get_license (GstPlugin * plugin)
536 g_return_val_if_fail (plugin != NULL, NULL);
538 return plugin->desc.license;
542 * gst_plugin_get_source:
543 * @plugin: plugin to get the source of
545 * get the source module the plugin belongs to.
547 * Returns: the source of the plugin
549 G_CONST_RETURN gchar *
550 gst_plugin_get_source (GstPlugin * plugin)
552 g_return_val_if_fail (plugin != NULL, NULL);
554 return plugin->desc.source;
558 * gst_plugin_get_package:
559 * @plugin: plugin to get the package of
561 * get the package the plugin belongs to.
563 * Returns: the package of the plugin
565 G_CONST_RETURN gchar *
566 gst_plugin_get_package (GstPlugin * plugin)
568 g_return_val_if_fail (plugin != NULL, NULL);
570 return plugin->desc.package;
574 * gst_plugin_get_origin:
575 * @plugin: plugin to get the origin of
577 * get the URL where the plugin comes from
579 * Returns: the origin of the plugin
581 G_CONST_RETURN gchar *
582 gst_plugin_get_origin (GstPlugin * plugin)
584 g_return_val_if_fail (plugin != NULL, NULL);
586 return plugin->desc.origin;
590 * gst_plugin_get_module:
591 * @plugin: plugin to query
593 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
596 * Returns: module belonging to the plugin or NULL if the plugin isn't
600 gst_plugin_get_module (GstPlugin * plugin)
602 g_return_val_if_fail (plugin != NULL, NULL);
604 return plugin->module;
608 * gst_plugin_is_loaded:
609 * @plugin: plugin to query
611 * queries if the plugin is loaded into memory
613 * Returns: TRUE is loaded, FALSE otherwise
616 gst_plugin_is_loaded (GstPlugin * plugin)
618 g_return_val_if_fail (plugin != NULL, FALSE);
620 return (plugin->module != NULL);
624 * gst_plugin_feature_list:
625 * @plugin: plugin to query
626 * @filter: the filter to use
627 * @first: only return first match
628 * @user_data: user data passed to the filter function
630 * Runs a filter against all plugin features and returns a GList with
631 * the results. If the first flag is set, only the first match is
632 * returned (as a list with a single object).
634 * Returns: a GList of features, g_list_free after use.
637 gst_plugin_feature_filter (GstPlugin * plugin,
638 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
643 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
645 for (g = list; g; g = g->next) {
646 gst_object_ref (plugin);
654 GstPluginFeatureFilter filter;
662 _feature_filter (GstPlugin * plugin, gpointer user_data)
665 FeatureFilterData *data = (FeatureFilterData *) user_data;
667 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
670 data->result = g_list_concat (data->result, result);
677 * gst_plugin_list_feature_filter:
678 * @list: a #GList of plugins to query
679 * @filter: the filter function to use
680 * @first: only return first match
681 * @user_data: user data passed to the filter function
683 * Runs a filter against all plugin features of the plugins in the given
684 * list and returns a GList with the results.
685 * If the first flag is set, only the first match is
686 * returned (as a list with a single object).
688 * Returns: a GList of features, g_list_free after use.
691 gst_plugin_list_feature_filter (GList * list,
692 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
694 FeatureFilterData data;
697 data.filter = filter;
699 data.user_data = user_data;
702 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
703 g_list_free (result);
709 * gst_plugin_name_filter:
710 * @plugin: the plugin to check
711 * @name: the name of the plugin
713 * A standard filter that returns TRUE when the plugin is of the
716 * Returns: TRUE if the plugin is of the given name.
719 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
721 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
725 * gst_plugin_find_feature:
726 * @plugin: plugin to get the feature from
727 * @name: The name of the feature to find
728 * @type: The type of the feature to find
730 * Find a feature of the given name and type in the given plugin.
732 * Returns: a GstPluginFeature or NULL if the feature was not found.
735 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
738 GstPluginFeature *result = NULL;
739 GstTypeNameData data;
741 g_return_val_if_fail (name != NULL, NULL);
746 walk = gst_filter_run (plugin->features,
747 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
750 result = GST_PLUGIN_FEATURE (walk->data);
752 gst_object_ref (result->plugin);
753 gst_plugin_feature_list_free (walk);
760 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
762 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
766 * gst_plugin_find_feature_by_name:
767 * @plugin: plugin to get the feature from
768 * @name: The name of the feature to find
770 * Find a feature of the given name in the given plugin.
772 * Returns: a GstPluginFeature or NULL if the feature was not found.
775 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
778 GstPluginFeature *result = NULL;
780 g_return_val_if_fail (name != NULL, NULL);
782 walk = gst_filter_run (plugin->features,
783 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
786 result = GST_PLUGIN_FEATURE (walk->data);
788 gst_object_ref (result->plugin);
789 gst_plugin_feature_list_free (walk);
796 * gst_plugin_add_feature:
797 * @plugin: plugin to add feature to
798 * @feature: feature to add
800 * Add feature to the list of those provided by the plugin.
801 * There is a separate namespace for each plugin feature type.
802 * See #gst_plugin_get_feature_list
805 gst_plugin_add_feature (GstPlugin * plugin, GstPluginFeature * feature)
807 /* FIXME 0.9: get reference counting somewhat right in here,
808 * GstPluginFeatures should probably be GstObjects that are sinked when
809 * adding them to a plugin */
810 g_return_if_fail (plugin != NULL);
811 g_return_if_fail (GST_IS_PLUGIN_FEATURE (feature));
812 g_return_if_fail (feature != NULL);
813 g_return_if_fail (feature->plugin == NULL);
815 /* gst_object_sink (feature); */
816 feature->plugin = plugin;
817 plugin->features = g_list_prepend (plugin->features, feature);
818 plugin->numfeatures++;
822 * gst_plugin_get_feature_list:
823 * @plugin: the plugin to get the features from
825 * get a list of all the features that this plugin provides
827 * Returns: a GList of features, use g_list_free to free the list.
830 gst_plugin_get_feature_list (GstPlugin * plugin)
835 g_return_val_if_fail (plugin != NULL, NULL);
837 list = g_list_copy (plugin->features);
838 for (g = list; g; g = g->next) {
839 gst_object_ref (plugin);
845 /* FIXME is this function necessary? */
848 * @name: name of plugin to load
850 * Load the named plugin.
852 * Returns: whether the plugin was loaded or not
855 gst_plugin_load_1 (const gchar * name)
858 GError *error = NULL;
860 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
862 plugin = gst_plugin_load_file (plugin->filename, &error);
864 GST_WARNING ("load_plugin error: %s\n", error->message);
865 g_error_free (error);
871 GST_DEBUG ("Could not find %s in registry pool", name);
876 gst_plugin_load (GstPlugin * plugin)
878 GError *error = NULL;
879 GstPlugin *newplugin;
881 if (gst_plugin_is_loaded (plugin)) {
885 if (!plugin->filename) {
889 newplugin = gst_plugin_load_file (plugin->filename, &error);
890 if (newplugin == NULL) {
891 GST_WARNING ("load_plugin error: %s\n", error->message);
892 g_error_free (error);
893 //gst_object_unref (plugin);
897 /* FIXME hack to keep plugins from disappearing */
898 //gst_object_unref (plugin);
904 gst_plugin_list_free (GList * list)
908 for (g = list; g; g = g->next) {
909 //gst_object_unref (GST_PLUGIN(g->data));