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.
25 * @short_description: Container for features loaded from a shared object module
26 * @see_also: #GstPluginFeature, #GstElementFactory
28 * GStreamer is extensible, so #GstElement instances can be loaded at runtime.
29 * A plugin system can provide one or more of the basic
30 * <application>GStreamer</application> #GstPluginFeature subclasses.
32 * A plugin should export a symbol <symbol>gst_plugin_desc</symbol> that is a
33 * struct of type #GstPluginDesc.
34 * the plugin loader will check the version of the core library the plugin was
35 * linked against and will create a new #GstPlugin. It will then call the
36 * #GstPluginInitFunc function that was provided in the
37 * <symbol>gst_plugin_desc</symbol>.
39 * Once you have a handle to a #GstPlugin (e.g. from the #GstRegistry), you
40 * can add any object that subclasses #GstPluginFeature.
42 * Usually plugins are always automaticlly loaded so you don't need to call
43 * gst_plugin_load() explicitly to bring it into memory. There are options to
44 * statically link plugins to an app or even use GStreamer without a plugin
45 * repository in which case gst_plugin_load() can be needed to bring the plugin
52 #include <glib/gstdio.h>
53 #include <sys/types.h>
63 #include "gst_private.h"
64 #include "glib-compat-private.h"
68 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
70 static guint _num_static_plugins; /* 0 */
71 static GstPluginDesc *_static_plugins; /* NULL */
72 static gboolean _gst_plugin_inited;
74 /* static variables for segfault handling of plugin loading */
75 static char *_gst_plugin_fault_handler_filename = NULL;
78 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
81 /* list of valid licenses.
82 * One of these must be specified or the plugin won't be loaded
83 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
86 * GPL: http://www.gnu.org/copyleft/gpl.html
87 * LGPL: http://www.gnu.org/copyleft/lesser.html
88 * QPL: http://www.trolltech.com/licenses/qpl.html
89 * MPL: http://www.opensource.org/licenses/mozilla1.1.php
90 * MIT/X11: http://www.opensource.org/licenses/mit-license.php
91 * 3-clause BSD: http://www.opensource.org/licenses/bsd-license.php
93 static const gchar *valid_licenses[] = {
94 "LGPL", /* GNU Lesser General Public License */
95 "GPL", /* GNU General Public License */
96 "QPL", /* Trolltech Qt Public License */
97 "GPL/QPL", /* Combi-license of GPL + QPL */
98 "MPL", /* MPL 1.1 license */
99 "BSD", /* 3-clause BSD license */
100 "MIT/X11", /* MIT/X11 license */
101 "Proprietary", /* Proprietary license */
102 GST_LICENSE_UNKNOWN, /* some other license */
106 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
107 const GstPluginDesc * desc);
108 static void gst_plugin_desc_copy (GstPluginDesc * dest,
109 const GstPluginDesc * src);
110 static void gst_plugin_desc_free (GstPluginDesc * desc);
112 static void gst_plugin_ext_dep_free (GstPluginDep * dep);
114 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
117 gst_plugin_init (GstPlugin * plugin)
120 G_TYPE_INSTANCE_GET_PRIVATE (plugin, GST_TYPE_PLUGIN, GstPluginPrivate);
124 gst_plugin_finalize (GObject * object)
126 GstPlugin *plugin = GST_PLUGIN_CAST (object);
127 GstRegistry *registry = gst_registry_get_default ();
130 GST_DEBUG ("finalizing plugin %p", plugin);
131 for (g = registry->plugins; g; g = g->next) {
132 if (g->data == (gpointer) plugin) {
133 g_warning ("removing plugin that is still in registry");
136 g_free (plugin->filename);
137 g_free (plugin->basename);
138 gst_plugin_desc_free (&plugin->desc);
140 g_list_foreach (plugin->priv->deps, (GFunc) gst_plugin_ext_dep_free, NULL);
141 g_list_free (plugin->priv->deps);
142 plugin->priv->deps = NULL;
144 G_OBJECT_CLASS (gst_plugin_parent_class)->finalize (object);
148 gst_plugin_class_init (GstPluginClass * klass)
150 G_OBJECT_CLASS (klass)->finalize = GST_DEBUG_FUNCPTR (gst_plugin_finalize);
152 g_type_class_add_private (klass, sizeof (GstPluginPrivate));
156 gst_plugin_error_quark (void)
158 static GQuark quark = 0;
161 quark = g_quark_from_static_string ("gst_plugin_error");
165 #ifndef GST_REMOVE_DEPRECATED
166 /* this function can be called in the GCC constructor extension, before
167 * the _gst_plugin_initialize() was called. In that case, we store the
168 * plugin description in a list to initialize it when we open the main
170 * When the main module is known, we can register the plugin right away.
173 _gst_plugin_register_static (GstPluginDesc * desc)
175 g_return_if_fail (desc != NULL);
177 if (!_gst_plugin_inited) {
178 /* We can't use any GLib functions here, since g_thread_init hasn't been
179 * called yet, and we can't call it here either, or programs that don't
180 * guard their g_thread_init calls in main() will just abort */
181 ++_num_static_plugins;
183 realloc (_static_plugins, _num_static_plugins * sizeof (GstPluginDesc));
184 /* assume strings in the GstPluginDesc are static const or live forever */
185 _static_plugins[_num_static_plugins - 1] = *desc;
187 gst_plugin_register_static (desc->major_version, desc->minor_version,
188 desc->name, desc->description, desc->plugin_init, desc->version,
189 desc->license, desc->source, desc->package, desc->origin);
195 * gst_plugin_register_static:
196 * @major_version: the major version number of the GStreamer core that the
197 * plugin was compiled for, you can just use GST_VERSION_MAJOR here
198 * @minor_version: the minor version number of the GStreamer core that the
199 * plugin was compiled for, you can just use GST_VERSION_MINOR here
200 * @name: a unique name of the plugin (ideally prefixed with an application- or
201 * library-specific namespace prefix in order to avoid name conflicts in
202 * case a similar plugin with the same name ever gets added to GStreamer)
203 * @description: description of the plugin
204 * @init_func: pointer to the init function of this plugin.
205 * @version: version string of the plugin
206 * @license: effective license of plugin. Must be one of the approved licenses
207 * (see #GstPluginDesc above) or the plugin will not be registered.
208 * @source: source module plugin belongs to
209 * @package: shipped package plugin belongs to
210 * @origin: URL to provider of plugin
212 * Registers a static plugin, ie. a plugin which is private to an application
213 * or library and contained within the application or library (as opposed to
214 * being shipped as a separate module file).
216 * You must make sure that GStreamer has been initialised (with gst_init() or
217 * via gst_init_get_option_group()) before calling this function.
219 * Returns: TRUE if the plugin was registered correctly, otherwise FALSE.
224 gst_plugin_register_static (gint major_version, gint minor_version,
225 const gchar * name, gchar * description, GstPluginInitFunc init_func,
226 const gchar * version, const gchar * license, const gchar * source,
227 const gchar * package, const gchar * origin)
229 GstPluginDesc desc = { major_version, minor_version, name, description,
230 init_func, version, license, source, package, origin,
233 gboolean res = FALSE;
235 g_return_val_if_fail (name != NULL, FALSE);
236 g_return_val_if_fail (description != NULL, FALSE);
237 g_return_val_if_fail (init_func != NULL, FALSE);
238 g_return_val_if_fail (version != NULL, FALSE);
239 g_return_val_if_fail (license != NULL, FALSE);
240 g_return_val_if_fail (source != NULL, FALSE);
241 g_return_val_if_fail (package != NULL, FALSE);
242 g_return_val_if_fail (origin != NULL, FALSE);
244 /* make sure gst_init() has been called */
245 g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);
247 GST_LOG ("attempting to load static plugin \"%s\" now...", name);
248 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
249 if (gst_plugin_register_func (plugin, &desc) != NULL) {
250 GST_INFO ("registered static plugin \"%s\"", name);
251 res = gst_default_registry_add_plugin (plugin);
252 GST_INFO ("added static plugin \"%s\", result: %d", name, res);
258 _gst_plugin_initialize (void)
262 _gst_plugin_inited = TRUE;
264 /* now register all static plugins */
265 GST_INFO ("registering %u static plugins", _num_static_plugins);
266 for (i = 0; i < _num_static_plugins; ++i) {
267 gst_plugin_register_static (_static_plugins[i].major_version,
268 _static_plugins[i].minor_version, _static_plugins[i].name,
269 _static_plugins[i].description, _static_plugins[i].plugin_init,
270 _static_plugins[i].version, _static_plugins[i].license,
271 _static_plugins[i].source, _static_plugins[i].package,
272 _static_plugins[i].origin);
275 if (_static_plugins) {
276 free (_static_plugins);
277 _static_plugins = NULL;
278 _num_static_plugins = 0;
282 /* this function could be extended to check if the plugin license matches the
283 * applications license (would require the app to register its license somehow).
284 * We'll wait for someone who's interested in it to code it :)
287 gst_plugin_check_license (const gchar * license)
289 const gchar **check_license = valid_licenses;
291 g_assert (check_license);
293 while (*check_license) {
294 if (strcmp (license, *check_license) == 0)
302 gst_plugin_check_version (gint major, gint minor)
304 /* return NULL if the major and minor version numbers are not compatible */
306 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
313 gst_plugin_register_func (GstPlugin * plugin, const GstPluginDesc * desc)
315 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
317 GST_WARNING ("plugin \"%s\" has incompatible version, not loading",
322 if (!desc->license || !desc->description || !desc->source ||
323 !desc->package || !desc->origin) {
325 GST_WARNING ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
330 if (!gst_plugin_check_license (desc->license)) {
332 GST_WARNING ("plugin \"%s\" has invalid license \"%s\", not loading",
333 plugin->filename, desc->license);
338 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
340 gst_plugin_desc_copy (&plugin->desc, desc);
342 if (!((desc->plugin_init) (plugin))) {
344 GST_WARNING ("plugin \"%s\" failed to initialise", plugin->filename);
345 plugin->module = NULL;
350 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
355 #ifdef HAVE_SIGACTION
356 static struct sigaction oldaction;
359 * _gst_plugin_fault_handler_restore:
360 * segfault handler restorer
363 _gst_plugin_fault_handler_restore (void)
365 if (!_gst_plugin_fault_handler_is_setup)
368 _gst_plugin_fault_handler_is_setup = FALSE;
370 sigaction (SIGSEGV, &oldaction, NULL);
374 * _gst_plugin_fault_handler_sighandler:
375 * segfault handler implementation
378 _gst_plugin_fault_handler_sighandler (int signum)
380 /* We need to restore the fault handler or we'll keep getting it */
381 _gst_plugin_fault_handler_restore ();
385 g_print ("\nERROR: ");
386 g_print ("Caught a segmentation fault while loading plugin file:\n");
387 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
388 g_print ("Please either:\n");
389 g_print ("- remove it and restart.\n");
390 g_print ("- run with --gst-disable-segtrap and debug.\n");
394 g_print ("Caught unhandled signal on plugin loading\n");
400 * _gst_plugin_fault_handler_setup:
401 * sets up the segfault handler
404 _gst_plugin_fault_handler_setup (void)
406 struct sigaction action;
408 /* if asked to leave segfaults alone, just return */
409 if (!gst_segtrap_is_enabled ())
412 if (_gst_plugin_fault_handler_is_setup)
415 _gst_plugin_fault_handler_is_setup = TRUE;
417 memset (&action, 0, sizeof (action));
418 action.sa_handler = _gst_plugin_fault_handler_sighandler;
420 sigaction (SIGSEGV, &action, &oldaction);
424 _gst_plugin_fault_handler_restore (void)
429 _gst_plugin_fault_handler_setup (void)
434 static void _gst_plugin_fault_handler_setup ();
436 static GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
438 #define CHECK_PLUGIN_DESC_FIELD(desc,field,fn) \
439 if (G_UNLIKELY ((desc)->field == NULL)) { \
440 GST_ERROR ("GstPluginDesc for '%s' has no %s", fn, G_STRINGIFY (field)); \
444 * gst_plugin_load_file:
445 * @filename: the plugin filename to load
446 * @error: pointer to a NULL-valued GError
448 * Loads the given plugin and refs it. Caller needs to unref after use.
450 * Returns: a reference to the existing loaded GstPlugin, a reference to the
451 * newly-loaded GstPlugin, or NULL if an error occurred.
454 gst_plugin_load_file (const gchar * filename, GError ** error)
460 struct stat file_status;
461 GstRegistry *registry;
463 g_return_val_if_fail (filename != NULL, NULL);
465 registry = gst_registry_get_default ();
466 g_static_mutex_lock (&gst_plugin_loading_mutex);
468 plugin = gst_registry_lookup (registry, filename);
470 if (plugin->module) {
471 g_static_mutex_unlock (&gst_plugin_loading_mutex);
474 gst_object_unref (plugin);
479 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
482 if (g_module_supported () == FALSE) {
483 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
486 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
490 if (g_stat (filename, &file_status)) {
491 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
494 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
499 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
500 if (module == NULL) {
501 GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
504 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
506 /* If we failed to open the shared object, then it's probably because a
507 * plugin is linked against the wrong libraries. Print out an easy-to-see
508 * message in this case. */
509 g_warning ("Failed to load plugin '%s': %s", filename, g_module_error ());
513 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
515 plugin->module = module;
516 plugin->filename = g_strdup (filename);
517 plugin->basename = g_path_get_basename (filename);
518 plugin->file_mtime = file_status.st_mtime;
519 plugin->file_size = file_status.st_size;
521 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
523 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
526 GST_PLUGIN_ERROR_MODULE,
527 "File \"%s\" is not a GStreamer plugin", filename);
528 g_module_close (module);
531 plugin->orig_desc = (GstPluginDesc *) ptr;
533 /* check plugin description: complain about bad values but accept them, to
534 * maintain backwards compatibility (FIXME: 0.11) */
535 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, name, filename);
536 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, description, filename);
537 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, version, filename);
538 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, license, filename);
539 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, source, filename);
540 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, package, filename);
541 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, origin, filename);
543 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
546 /* this is where we load the actual .so, so let's trap SIGSEGV */
547 _gst_plugin_fault_handler_setup ();
548 _gst_plugin_fault_handler_filename = plugin->filename;
550 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
553 if (!gst_plugin_register_func (plugin, plugin->orig_desc)) {
554 /* remove signal handler */
555 _gst_plugin_fault_handler_restore ();
556 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
560 GST_PLUGIN_ERROR_MODULE,
561 "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
563 g_module_close (module);
567 /* remove signal handler */
568 _gst_plugin_fault_handler_restore ();
569 _gst_plugin_fault_handler_filename = NULL;
570 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
572 gst_object_ref (plugin);
573 gst_default_registry_add_plugin (plugin);
575 g_static_mutex_unlock (&gst_plugin_loading_mutex);
581 gst_object_unref (plugin);
582 g_static_mutex_unlock (&gst_plugin_loading_mutex);
588 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
590 dest->major_version = src->major_version;
591 dest->minor_version = src->minor_version;
592 dest->name = g_intern_string (src->name);
593 /* maybe intern the description too, just for convenience? */
594 dest->description = g_strdup (src->description);
595 dest->plugin_init = src->plugin_init;
596 dest->version = g_intern_string (src->version);
597 dest->license = g_intern_string (src->license);
598 dest->source = g_intern_string (src->source);
599 dest->package = g_intern_string (src->package);
600 dest->origin = g_intern_string (src->origin);
605 gst_plugin_desc_free (GstPluginDesc * desc)
607 g_free (desc->description);
608 memset (desc, 0, sizeof (GstPluginDesc));
612 * gst_plugin_get_name:
613 * @plugin: plugin to get the name of
615 * Get the short name of the plugin
617 * Returns: the name of the plugin
620 gst_plugin_get_name (GstPlugin * plugin)
622 g_return_val_if_fail (plugin != NULL, NULL);
624 return plugin->desc.name;
628 * gst_plugin_get_description:
629 * @plugin: plugin to get long name of
631 * Get the long descriptive name of the plugin
633 * Returns: the long name of the plugin
635 G_CONST_RETURN gchar *
636 gst_plugin_get_description (GstPlugin * plugin)
638 g_return_val_if_fail (plugin != NULL, NULL);
640 return plugin->desc.description;
644 * gst_plugin_get_filename:
645 * @plugin: plugin to get the filename of
647 * get the filename of the plugin
649 * Returns: the filename of the plugin
651 G_CONST_RETURN gchar *
652 gst_plugin_get_filename (GstPlugin * plugin)
654 g_return_val_if_fail (plugin != NULL, NULL);
656 return plugin->filename;
660 * gst_plugin_get_version:
661 * @plugin: plugin to get the version of
663 * get the version of the plugin
665 * Returns: the version of the plugin
667 G_CONST_RETURN gchar *
668 gst_plugin_get_version (GstPlugin * plugin)
670 g_return_val_if_fail (plugin != NULL, NULL);
672 return plugin->desc.version;
676 * gst_plugin_get_license:
677 * @plugin: plugin to get the license of
679 * get the license of the plugin
681 * Returns: the license of the plugin
683 G_CONST_RETURN gchar *
684 gst_plugin_get_license (GstPlugin * plugin)
686 g_return_val_if_fail (plugin != NULL, NULL);
688 return plugin->desc.license;
692 * gst_plugin_get_source:
693 * @plugin: plugin to get the source of
695 * get the source module the plugin belongs to.
697 * Returns: the source of the plugin
699 G_CONST_RETURN gchar *
700 gst_plugin_get_source (GstPlugin * plugin)
702 g_return_val_if_fail (plugin != NULL, NULL);
704 return plugin->desc.source;
708 * gst_plugin_get_package:
709 * @plugin: plugin to get the package of
711 * get the package the plugin belongs to.
713 * Returns: the package of the plugin
715 G_CONST_RETURN gchar *
716 gst_plugin_get_package (GstPlugin * plugin)
718 g_return_val_if_fail (plugin != NULL, NULL);
720 return plugin->desc.package;
724 * gst_plugin_get_origin:
725 * @plugin: plugin to get the origin of
727 * get the URL where the plugin comes from
729 * Returns: the origin of the plugin
731 G_CONST_RETURN gchar *
732 gst_plugin_get_origin (GstPlugin * plugin)
734 g_return_val_if_fail (plugin != NULL, NULL);
736 return plugin->desc.origin;
740 * gst_plugin_get_module:
741 * @plugin: plugin to query
743 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
746 * Returns: module belonging to the plugin or NULL if the plugin isn't
750 gst_plugin_get_module (GstPlugin * plugin)
752 g_return_val_if_fail (plugin != NULL, NULL);
754 return plugin->module;
758 * gst_plugin_is_loaded:
759 * @plugin: plugin to query
761 * queries if the plugin is loaded into memory
763 * Returns: TRUE is loaded, FALSE otherwise
766 gst_plugin_is_loaded (GstPlugin * plugin)
768 g_return_val_if_fail (plugin != NULL, FALSE);
770 return (plugin->module != NULL || plugin->filename == NULL);
775 * gst_plugin_feature_list:
776 * @plugin: plugin to query
777 * @filter: the filter to use
778 * @first: only return first match
779 * @user_data: user data passed to the filter function
781 * Runs a filter against all plugin features and returns a GList with
782 * the results. If the first flag is set, only the first match is
783 * returned (as a list with a single object).
785 * Returns: a GList of features, g_list_free after use.
788 gst_plugin_feature_filter (GstPlugin * plugin,
789 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
794 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
796 for (g = list; g; g = g->next) {
797 gst_object_ref (plugin);
805 GstPluginFeatureFilter filter;
813 _feature_filter (GstPlugin * plugin, gpointer user_data)
816 FeatureFilterData *data = (FeatureFilterData *) user_data;
818 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
821 data->result = g_list_concat (data->result, result);
828 * gst_plugin_list_feature_filter:
829 * @list: a #GList of plugins to query
830 * @filter: the filter function to use
831 * @first: only return first match
832 * @user_data: user data passed to the filter function
834 * Runs a filter against all plugin features of the plugins in the given
835 * list and returns a GList with the results.
836 * If the first flag is set, only the first match is
837 * returned (as a list with a single object).
839 * Returns: a GList of features, g_list_free after use.
842 gst_plugin_list_feature_filter (GList * list,
843 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
845 FeatureFilterData data;
848 data.filter = filter;
850 data.user_data = user_data;
853 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
854 g_list_free (result);
861 * gst_plugin_name_filter:
862 * @plugin: the plugin to check
863 * @name: the name of the plugin
865 * A standard filter that returns TRUE when the plugin is of the
868 * Returns: TRUE if the plugin is of the given name.
871 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
873 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
878 * gst_plugin_find_feature:
879 * @plugin: plugin to get the feature from
880 * @name: The name of the feature to find
881 * @type: The type of the feature to find
883 * Find a feature of the given name and type in the given plugin.
885 * Returns: a GstPluginFeature or NULL if the feature was not found.
888 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
891 GstPluginFeature *result = NULL;
892 GstTypeNameData data;
894 g_return_val_if_fail (name != NULL, NULL);
899 walk = gst_filter_run (plugin->features,
900 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
903 result = GST_PLUGIN_FEATURE (walk->data);
905 gst_object_ref (result);
906 gst_plugin_feature_list_free (walk);
915 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
917 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
923 * gst_plugin_find_feature_by_name:
924 * @plugin: plugin to get the feature from
925 * @name: The name of the feature to find
927 * Find a feature of the given name in the given plugin.
929 * Returns: a GstPluginFeature or NULL if the feature was not found.
932 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
935 GstPluginFeature *result = NULL;
937 g_return_val_if_fail (name != NULL, NULL);
939 walk = gst_filter_run (plugin->features,
940 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
943 result = GST_PLUGIN_FEATURE (walk->data);
945 gst_object_ref (result);
946 gst_plugin_feature_list_free (walk);
954 * gst_plugin_load_by_name:
955 * @name: name of plugin to load
957 * Load the named plugin. Refs the plugin.
959 * Returns: A reference to a loaded plugin, or NULL on error.
962 gst_plugin_load_by_name (const gchar * name)
964 GstPlugin *plugin, *newplugin;
965 GError *error = NULL;
967 GST_DEBUG ("looking up plugin %s in default registry", name);
968 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
970 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
971 newplugin = gst_plugin_load_file (plugin->filename, &error);
972 gst_object_unref (plugin);
975 GST_WARNING ("load_plugin error: %s", error->message);
976 g_error_free (error);
979 /* newplugin was reffed by load_file */
983 GST_DEBUG ("Could not find plugin %s in registry", name);
989 * @plugin: plugin to load
991 * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
992 * untouched. The normal use pattern of this function goes like this:
995 * GstPlugin *loaded_plugin;
996 * loaded_plugin = gst_plugin_load (plugin);
997 * // presumably, we're no longer interested in the potentially-unloaded plugin
998 * gst_object_unref (plugin);
999 * plugin = loaded_plugin;
1002 * Returns: A reference to a loaded plugin, or NULL on error.
1005 gst_plugin_load (GstPlugin * plugin)
1007 GError *error = NULL;
1008 GstPlugin *newplugin;
1010 if (gst_plugin_is_loaded (plugin)) {
1014 if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
1021 GST_WARNING ("load_plugin error: %s", error->message);
1022 g_error_free (error);
1028 * gst_plugin_list_free:
1029 * @list: list of #GstPlugin
1031 * Unrefs each member of @list, then frees the list.
1034 gst_plugin_list_free (GList * list)
1038 for (g = list; g; g = g->next) {
1039 gst_object_unref (GST_PLUGIN_CAST (g->data));
1044 /* ===== plugin dependencies ===== */
1047 * ENV + xyz where ENV can contain multiple values separated by SEPARATOR
1048 * xyz may be "" (if ENV contains path to file rather than dir)
1049 * ENV + *xyz same as above, but xyz acts as suffix filter
1050 * ENV + xyz* same as above, but xyz acts as prefix filter (is this needed?)
1051 * ENV + *xyz* same as above, but xyz acts as strstr filter (is this needed?)
1053 * same as above, with additional paths hard-coded at compile-time:
1054 * - only check paths + ... if ENV is not set or yields not paths
1055 * - always check paths + ... in addition to ENV
1057 * When user specifies set of environment variables, he/she may also use e.g.
1058 * "HOME/.mystuff/plugins", and we'll expand the content of $HOME with the
1062 /* we store in registry:
1065 * - environment variables (array of strings)
1066 * - last hash of env variable contents (uint) (so we can avoid doing stats
1067 * if one of the env vars has changed; premature optimisation galore)
1068 * - hard-coded paths (array of strings)
1069 * - xyz filename/suffix/prefix strings (array of strings)
1071 * - last hash of file/dir stats (int)
1073 * (= struct GstPluginDep)
1077 gst_plugin_ext_dep_get_env_vars_hash (GstPlugin * plugin, GstPluginDep * dep)
1082 /* there's no deeper logic to what we do here; all we want to know (when
1083 * checking if the plugin needs to be rescanned) is whether the content of
1084 * one of the environment variables in the list is different from when it
1085 * was last scanned */
1087 for (e = dep->env_vars; e != NULL && *e != NULL; ++e) {
1091 /* order matters: "val",NULL needs to yield a different hash than
1092 * NULL,"val", so do a shift here whether the var is set or not */
1095 /* want environment variable at beginning of string */
1096 if (!g_ascii_isalnum (**e)) {
1097 GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1098 "variable string: %s", *e);
1102 /* user is allowed to specify e.g. "HOME/.pitivi/plugins" */
1103 g_strlcpy (env_var, *e, sizeof (env_var));
1104 g_strdelimit (env_var, "/\\", '\0');
1106 if ((val = g_getenv (env_var)))
1107 hash += g_str_hash (val);
1114 _priv_plugin_deps_env_vars_changed (GstPlugin * plugin)
1118 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1119 GstPluginDep *dep = l->data;
1121 if (dep->env_hash != gst_plugin_ext_dep_get_env_vars_hash (plugin, dep))
1129 gst_plugin_ext_dep_extract_env_vars_paths (GstPlugin * plugin,
1133 GList *paths = NULL;
1135 for (evars = dep->env_vars; evars != NULL && *evars != NULL; ++evars) {
1139 /* want environment variable at beginning of string */
1140 if (!g_ascii_isalnum (**evars)) {
1141 GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1142 "variable string: %s", *evars);
1146 /* user is allowed to specify e.g. "HOME/.pitivi/plugins", which we want to
1147 * split into the env_var name component and the path component */
1148 components = g_strsplit_set (*evars, "/\\", 2);
1149 g_assert (components != NULL);
1151 e = g_getenv (components[0]);
1152 GST_LOG_OBJECT (plugin, "expanding %s = '%s' (path suffix: %s)",
1153 components[0], GST_STR_NULL (e), GST_STR_NULL (components[1]));
1155 if (components[1] != NULL) {
1156 g_strdelimit (components[1], "/\\", G_DIR_SEPARATOR);
1159 if (e != NULL && *e != '\0') {
1163 arr = g_strsplit (e, G_SEARCHPATH_SEPARATOR_S, -1);
1165 for (i = 0; arr != NULL && arr[i] != NULL; ++i) {
1168 if (!g_path_is_absolute (arr[i])) {
1169 GST_INFO_OBJECT (plugin, "ignoring environment variable content '%s'"
1170 ": either not an absolute path or not a path at all", arr[i]);
1174 if (components[1] != NULL) {
1175 full_path = g_build_filename (arr[i], components[1], NULL);
1177 full_path = g_strdup (arr[i]);
1180 if (!g_list_find_custom (paths, full_path, (GCompareFunc) strcmp)) {
1181 GST_LOG_OBJECT (plugin, "path: '%s'", full_path);
1182 paths = g_list_prepend (paths, full_path);
1185 GST_LOG_OBJECT (plugin, "path: '%s' (duplicate,ignoring)", full_path);
1193 g_strfreev (components);
1196 GST_LOG_OBJECT (plugin, "Extracted %d paths from environment",
1197 g_list_length (paths));
1203 gst_plugin_ext_dep_get_hash_from_stat_entry (struct stat *s)
1205 if (!(s->st_mode & (S_IFDIR | S_IFREG)))
1208 /* completely random formula */
1209 return ((s->st_size << 3) + (s->st_mtime << 5)) ^ s->st_ctime;
1213 gst_plugin_ext_dep_direntry_matches (GstPlugin * plugin, const gchar * entry,
1214 const gchar ** filenames, GstPluginDependencyFlags flags)
1216 /* no filenames specified, match all entries for now (could probably
1217 * optimise by just taking the dir stat hash or so) */
1218 if (filenames == NULL || *filenames == NULL || **filenames == '\0')
1221 while (*filenames != NULL) {
1223 if (((flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)) &&
1224 g_str_has_suffix (entry, *filenames)) {
1226 /* else it's an exact match that's needed */
1227 } else if (strcmp (entry, *filenames) == 0) {
1230 GST_LOG ("%s does not match %s, flags=0x%04x", entry, *filenames, flags);
1237 gst_plugin_ext_dep_scan_dir_and_match_names (GstPlugin * plugin,
1238 const gchar * path, const gchar ** filenames,
1239 GstPluginDependencyFlags flags, int depth)
1242 gboolean recurse_dirs;
1247 recurse_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1249 dir = g_dir_open (path, 0, &err);
1251 GST_DEBUG_OBJECT (plugin, "g_dir_open(%s) failed: %s", path, err->message);
1256 /* FIXME: we're assuming here that we always get the directory entries in
1257 * the same order, and not in a random order */
1258 while ((entry = g_dir_read_name (dir))) {
1259 gboolean have_match;
1265 gst_plugin_ext_dep_direntry_matches (plugin, entry, filenames, flags);
1267 /* avoid the stat if possible */
1268 if (!have_match && !recurse_dirs)
1271 full_path = g_build_filename (path, entry, NULL);
1272 if (g_stat (full_path, &s) < 0) {
1273 fhash = (guint) - 1;
1274 GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1275 g_strerror (errno));
1276 } else if (have_match) {
1277 fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1278 GST_LOG_OBJECT (plugin, "stat: %s (result: %u)", full_path, fhash);
1279 } else if ((s.st_mode & (S_IFDIR))) {
1280 fhash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, full_path,
1281 filenames, flags, depth + 1);
1283 /* it's not a name match, we want to recurse, but it's not a directory */
1288 hash = (hash + fhash) << 1;
1297 gst_plugin_ext_dep_scan_path_with_filenames (GstPlugin * plugin,
1298 const gchar * path, const gchar ** filenames,
1299 GstPluginDependencyFlags flags)
1301 const gchar *empty_filenames[] = { "", NULL };
1302 gboolean recurse_into_dirs, partial_names;
1305 /* to avoid special-casing below (FIXME?) */
1306 if (filenames == NULL || *filenames == NULL)
1307 filenames = empty_filenames;
1309 recurse_into_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1310 partial_names = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX);
1312 /* if we can construct the exact paths to check with the data we have, just
1313 * stat them one by one; this is more efficient than opening the directory
1314 * and going through each entry to see if it matches one of our filenames. */
1315 if (!recurse_into_dirs && !partial_names) {
1316 for (i = 0; filenames[i] != NULL; ++i) {
1321 full_path = g_build_filename (path, filenames[i], NULL);
1322 if (g_stat (full_path, &s) < 0) {
1323 fhash = (guint) - 1;
1324 GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1325 g_strerror (errno));
1327 fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1328 GST_LOG_OBJECT (plugin, "stat: %s (result: %08x)", full_path, fhash);
1330 hash = (hash + fhash) << 1;
1334 hash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, path,
1335 filenames, flags, 0);
1342 gst_plugin_ext_dep_get_stat_hash (GstPlugin * plugin, GstPluginDep * dep)
1344 gboolean paths_are_default_only;
1346 guint scan_hash = 0;
1348 GST_LOG_OBJECT (plugin, "start");
1350 paths_are_default_only =
1351 dep->flags & GST_PLUGIN_DEPENDENCY_FLAG_PATHS_ARE_DEFAULT_ONLY;
1353 scan_paths = gst_plugin_ext_dep_extract_env_vars_paths (plugin, dep);
1355 if (scan_paths == NULL || !paths_are_default_only) {
1358 for (paths = dep->paths; paths != NULL && *paths != NULL; ++paths) {
1359 const gchar *path = *paths;
1361 if (!g_list_find_custom (scan_paths, path, (GCompareFunc) strcmp)) {
1362 GST_LOG_OBJECT (plugin, "path: '%s'", path);
1363 scan_paths = g_list_prepend (scan_paths, g_strdup (path));
1365 GST_LOG_OBJECT (plugin, "path: '%s' (duplicate, ignoring)", path);
1370 /* not that the order really matters, but it makes debugging easier */
1371 scan_paths = g_list_reverse (scan_paths);
1373 while (scan_paths != NULL) {
1374 const gchar *path = scan_paths->data;
1376 scan_hash += gst_plugin_ext_dep_scan_path_with_filenames (plugin, path,
1377 (const gchar **) dep->names, dep->flags);
1378 scan_hash = scan_hash << 1;
1380 g_free (scan_paths->data);
1381 scan_paths = g_list_delete_link (scan_paths, scan_paths);
1384 GST_LOG_OBJECT (plugin, "done, scan_hash: %08x", scan_hash);
1389 _priv_plugin_deps_files_changed (GstPlugin * plugin)
1393 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1394 GstPluginDep *dep = l->data;
1396 if (dep->stat_hash != gst_plugin_ext_dep_get_stat_hash (plugin, dep))
1404 gst_plugin_ext_dep_free (GstPluginDep * dep)
1406 g_strfreev (dep->env_vars);
1407 g_strfreev (dep->paths);
1408 g_strfreev (dep->names);
1413 gst_plugin_ext_dep_strv_equal (gchar ** arr1, gchar ** arr2)
1417 if (arr1 == NULL || arr2 == NULL)
1419 for (; *arr1 != NULL && *arr2 != NULL; ++arr1, ++arr2) {
1420 if (strcmp (*arr1, *arr2) != 0)
1423 return (*arr1 == *arr2);
1427 gst_plugin_ext_dep_equals (GstPluginDep * dep, const gchar ** env_vars,
1428 const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1430 if (dep->flags != flags)
1433 return gst_plugin_ext_dep_strv_equal (dep->env_vars, (gchar **) env_vars) &&
1434 gst_plugin_ext_dep_strv_equal (dep->paths, (gchar **) paths) &&
1435 gst_plugin_ext_dep_strv_equal (dep->names, (gchar **) names);
1439 * gst_plugin_add_dependency:
1440 * @plugin: a #GstPlugin
1441 * @env_vars: NULL-terminated array of environent variables affecting the
1442 * feature set of the plugin (e.g. an environment variable containing
1443 * paths where to look for additional modules/plugins of a library),
1444 * or NULL. Environment variable names may be followed by a path component
1445 * which will be added to the content of the environment variable, e.g.
1446 * "HOME/.mystuff/plugins".
1447 * @paths: NULL-terminated array of directories/paths where dependent files
1449 * @names: NULL-terminated array of file names (or file name suffixes,
1450 * depending on @flags) to be used in combination with the paths from
1451 * @paths and/or the paths extracted from the environment variables in
1452 * @env_vars, or NULL.
1453 * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1455 * Make GStreamer aware of external dependencies which affect the feature
1456 * set of this plugin (ie. the elements or typefinders associated with it).
1458 * GStreamer will re-inspect plugins with external dependencies whenever any
1459 * of the external dependencies change. This is useful for plugins which wrap
1460 * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1461 * library and makes visualisations available as GStreamer elements, or a
1462 * codec loader which exposes elements and/or caps dependent on what external
1463 * codec libraries are currently installed.
1468 gst_plugin_add_dependency (GstPlugin * plugin, const gchar ** env_vars,
1469 const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1474 g_return_if_fail (GST_IS_PLUGIN (plugin));
1475 g_return_if_fail (env_vars != NULL || paths != NULL);
1477 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1478 if (gst_plugin_ext_dep_equals (l->data, env_vars, paths, names, flags)) {
1479 GST_LOG_OBJECT (plugin, "dependency already registered");
1484 dep = g_new0 (GstPluginDep, 1);
1486 dep->env_vars = g_strdupv ((gchar **) env_vars);
1487 dep->paths = g_strdupv ((gchar **) paths);
1488 dep->names = g_strdupv ((gchar **) names);
1491 dep->env_hash = gst_plugin_ext_dep_get_env_vars_hash (plugin, dep);
1492 dep->stat_hash = gst_plugin_ext_dep_get_stat_hash (plugin, dep);
1494 plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
1496 GST_DEBUG_OBJECT (plugin, "added dependency:");
1497 for (; env_vars != NULL && *env_vars != NULL; ++env_vars)
1498 GST_DEBUG_OBJECT (plugin, " evar: %s", *env_vars);
1499 for (; paths != NULL && *paths != NULL; ++paths)
1500 GST_DEBUG_OBJECT (plugin, " path: %s", *paths);
1501 for (; names != NULL && *names != NULL; ++names)
1502 GST_DEBUG_OBJECT (plugin, " name: %s", *names);
1506 * gst_plugin_add_dependency_simple:
1507 * @plugin: the #GstPlugin
1508 * @env_vars: one or more environent variables (separated by ':', ';' or ','),
1509 * or NULL. Environment variable names may be followed by a path component
1510 * which will be added to the content of the environment variable, e.g.
1511 * "HOME/.mystuff/plugins:MYSTUFF_PLUGINS_PATH"
1512 * @paths: one ore more directory paths (separated by ':' or ';' or ','),
1513 * or NULL. Example: "/usr/lib/mystuff/plugins"
1514 * @names: one or more file names or file name suffixes (separated by commas),
1516 * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1518 * Make GStreamer aware of external dependencies which affect the feature
1519 * set of this plugin (ie. the elements or typefinders associated with it).
1521 * GStreamer will re-inspect plugins with external dependencies whenever any
1522 * of the external dependencies change. This is useful for plugins which wrap
1523 * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1524 * library and makes visualisations available as GStreamer elements, or a
1525 * codec loader which exposes elements and/or caps dependent on what external
1526 * codec libraries are currently installed.
1528 * Convenience wrapper function for gst_plugin_add_dependency() which
1529 * takes simple strings as arguments instead of string arrays, with multiple
1530 * arguments separated by predefined delimiters (see above).
1535 gst_plugin_add_dependency_simple (GstPlugin * plugin,
1536 const gchar * env_vars, const gchar * paths, const gchar * names,
1537 GstPluginDependencyFlags flags)
1539 gchar **a_evars = NULL;
1540 gchar **a_paths = NULL;
1541 gchar **a_names = NULL;
1544 a_evars = g_strsplit_set (env_vars, ":;,", -1);
1546 a_paths = g_strsplit_set (paths, ":;,", -1);
1548 a_names = g_strsplit_set (names, ",", -1);
1550 gst_plugin_add_dependency (plugin, (const gchar **) a_evars,
1551 (const gchar **) a_paths, (const gchar **) a_names, flags);
1554 g_strfreev (a_evars);
1556 g_strfreev (a_paths);
1558 g_strfreev (a_names);