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;
77 /* list of valid licenses.
78 * One of these must be specified or the plugin won't be loaded
79 * Contact gstreamer-devel@lists.sourceforge.net if your license should be
82 * GPL: http://www.gnu.org/copyleft/gpl.html
83 * LGPL: http://www.gnu.org/copyleft/lesser.html
84 * QPL: http://www.trolltech.com/licenses/qpl.html
85 * MPL: http://www.opensource.org/licenses/mozilla1.1.php
86 * MIT/X11: http://www.opensource.org/licenses/mit-license.php
87 * 3-clause BSD: http://www.opensource.org/licenses/bsd-license.php
89 static const gchar *valid_licenses[] = {
90 "LGPL", /* GNU Lesser General Public License */
91 "GPL", /* GNU General Public License */
92 "QPL", /* Trolltech Qt Public License */
93 "GPL/QPL", /* Combi-license of GPL + QPL */
94 "MPL", /* MPL 1.1 license */
95 "BSD", /* 3-clause BSD license */
96 "MIT/X11", /* MIT/X11 license */
97 "Proprietary", /* Proprietary license */
98 GST_LICENSE_UNKNOWN, /* some other license */
102 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
103 const GstPluginDesc * desc, gpointer user_data);
104 static void gst_plugin_desc_copy (GstPluginDesc * dest,
105 const GstPluginDesc * src);
106 static void gst_plugin_desc_free (GstPluginDesc * desc);
108 static void gst_plugin_ext_dep_free (GstPluginDep * dep);
110 G_DEFINE_TYPE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
113 gst_plugin_init (GstPlugin * plugin)
116 G_TYPE_INSTANCE_GET_PRIVATE (plugin, GST_TYPE_PLUGIN, GstPluginPrivate);
120 gst_plugin_finalize (GObject * object)
122 GstPlugin *plugin = GST_PLUGIN_CAST (object);
123 GstRegistry *registry = gst_registry_get_default ();
126 GST_DEBUG ("finalizing plugin %p", plugin);
127 for (g = registry->plugins; g; g = g->next) {
128 if (g->data == (gpointer) plugin) {
129 g_warning ("removing plugin that is still in registry");
132 g_free (plugin->filename);
133 g_free (plugin->basename);
134 gst_plugin_desc_free (&plugin->desc);
136 g_list_foreach (plugin->priv->deps, (GFunc) gst_plugin_ext_dep_free, NULL);
137 g_list_free (plugin->priv->deps);
138 plugin->priv->deps = NULL;
140 G_OBJECT_CLASS (gst_plugin_parent_class)->finalize (object);
144 gst_plugin_class_init (GstPluginClass * klass)
146 G_OBJECT_CLASS (klass)->finalize = GST_DEBUG_FUNCPTR (gst_plugin_finalize);
148 g_type_class_add_private (klass, sizeof (GstPluginPrivate));
152 gst_plugin_error_quark (void)
154 static GQuark quark = 0;
157 quark = g_quark_from_static_string ("gst_plugin_error");
161 #ifndef GST_REMOVE_DEPRECATED
162 /* this function can be called in the GCC constructor extension, before
163 * the _gst_plugin_initialize() was called. In that case, we store the
164 * plugin description in a list to initialize it when we open the main
166 * When the main module is known, we can register the plugin right away.
169 _gst_plugin_register_static (GstPluginDesc * desc)
171 g_return_if_fail (desc != NULL);
173 if (!_gst_plugin_inited) {
174 /* We can't use any GLib functions here, since g_thread_init hasn't been
175 * called yet, and we can't call it here either, or programs that don't
176 * guard their g_thread_init calls in main() will just abort */
177 ++_num_static_plugins;
179 realloc (_static_plugins, _num_static_plugins * sizeof (GstPluginDesc));
180 /* assume strings in the GstPluginDesc are static const or live forever */
181 _static_plugins[_num_static_plugins - 1] = *desc;
183 gst_plugin_register_static (desc->major_version, desc->minor_version,
184 desc->name, desc->description, desc->plugin_init, desc->version,
185 desc->license, desc->source, desc->package, desc->origin);
191 * gst_plugin_register_static:
192 * @major_version: the major version number of the GStreamer core that the
193 * plugin was compiled for, you can just use GST_VERSION_MAJOR here
194 * @minor_version: the minor version number of the GStreamer core that the
195 * plugin was compiled for, you can just use GST_VERSION_MINOR here
196 * @name: a unique name of the plugin (ideally prefixed with an application- or
197 * library-specific namespace prefix in order to avoid name conflicts in
198 * case a similar plugin with the same name ever gets added to GStreamer)
199 * @description: description of the plugin
200 * @init_func: pointer to the init function of this plugin.
201 * @version: version string of the plugin
202 * @license: effective license of plugin. Must be one of the approved licenses
203 * (see #GstPluginDesc above) or the plugin will not be registered.
204 * @source: source module plugin belongs to
205 * @package: shipped package plugin belongs to
206 * @origin: URL to provider of plugin
208 * Registers a static plugin, ie. a plugin which is private to an application
209 * or library and contained within the application or library (as opposed to
210 * being shipped as a separate module file).
212 * You must make sure that GStreamer has been initialised (with gst_init() or
213 * via gst_init_get_option_group()) before calling this function.
215 * Returns: TRUE if the plugin was registered correctly, otherwise FALSE.
220 gst_plugin_register_static (gint major_version, gint minor_version,
221 const gchar * name, gchar * description, GstPluginInitFunc init_func,
222 const gchar * version, const gchar * license, const gchar * source,
223 const gchar * package, const gchar * origin)
225 GstPluginDesc desc = { major_version, minor_version, name, description,
226 init_func, version, license, source, package, origin,
229 gboolean res = FALSE;
231 g_return_val_if_fail (name != NULL, FALSE);
232 g_return_val_if_fail (description != NULL, FALSE);
233 g_return_val_if_fail (init_func != NULL, FALSE);
234 g_return_val_if_fail (version != NULL, FALSE);
235 g_return_val_if_fail (license != NULL, FALSE);
236 g_return_val_if_fail (source != NULL, FALSE);
237 g_return_val_if_fail (package != NULL, FALSE);
238 g_return_val_if_fail (origin != NULL, FALSE);
240 /* make sure gst_init() has been called */
241 g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);
243 GST_LOG ("attempting to load static plugin \"%s\" now...", name);
244 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
245 if (gst_plugin_register_func (plugin, &desc, NULL) != NULL) {
246 GST_INFO ("registered static plugin \"%s\"", name);
247 res = gst_default_registry_add_plugin (plugin);
248 GST_INFO ("added static plugin \"%s\", result: %d", name, res);
254 * gst_plugin_register_static_full:
255 * @major_version: the major version number of the GStreamer core that the
256 * plugin was compiled for, you can just use GST_VERSION_MAJOR here
257 * @minor_version: the minor version number of the GStreamer core that the
258 * plugin was compiled for, you can just use GST_VERSION_MINOR here
259 * @name: a unique name of the plugin (ideally prefixed with an application- or
260 * library-specific namespace prefix in order to avoid name conflicts in
261 * case a similar plugin with the same name ever gets added to GStreamer)
262 * @description: description of the plugin
263 * @init_full_func: pointer to the init function with user data of this plugin.
264 * @version: version string of the plugin
265 * @license: effective license of plugin. Must be one of the approved licenses
266 * (see #GstPluginDesc above) or the plugin will not be registered.
267 * @source: source module plugin belongs to
268 * @package: shipped package plugin belongs to
269 * @origin: URL to provider of plugin
270 * @user_data: gpointer to user data
272 * Registers a static plugin, ie. a plugin which is private to an application
273 * or library and contained within the application or library (as opposed to
274 * being shipped as a separate module file) with a #GstPluginInitFullFunc
275 * which allows user data to be passed to the callback function (useful
278 * You must make sure that GStreamer has been initialised (with gst_init() or
279 * via gst_init_get_option_group()) before calling this function.
281 * Returns: TRUE if the plugin was registered correctly, otherwise FALSE.
287 gst_plugin_register_static_full (gint major_version, gint minor_version,
288 const gchar * name, gchar * description,
289 GstPluginInitFullFunc init_full_func, const gchar * version,
290 const gchar * license, const gchar * source, const gchar * package,
291 const gchar * origin, gpointer user_data)
293 GstPluginDesc desc = { major_version, minor_version, name, description,
294 (GstPluginInitFunc) init_full_func, version, license, source, package,
298 gboolean res = FALSE;
300 g_return_val_if_fail (name != NULL, FALSE);
301 g_return_val_if_fail (description != NULL, FALSE);
302 g_return_val_if_fail (init_full_func != NULL, FALSE);
303 g_return_val_if_fail (version != NULL, FALSE);
304 g_return_val_if_fail (license != NULL, FALSE);
305 g_return_val_if_fail (source != NULL, FALSE);
306 g_return_val_if_fail (package != NULL, FALSE);
307 g_return_val_if_fail (origin != NULL, FALSE);
309 /* make sure gst_init() has been called */
310 g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);
312 GST_LOG ("attempting to load static plugin \"%s\" now...", name);
313 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
314 if (gst_plugin_register_func (plugin, &desc, user_data) != NULL) {
315 GST_INFO ("registered static plugin \"%s\"", name);
316 res = gst_default_registry_add_plugin (plugin);
317 GST_INFO ("added static plugin \"%s\", result: %d", name, res);
323 _gst_plugin_initialize (void)
327 _gst_plugin_inited = TRUE;
329 /* now register all static plugins */
330 GST_INFO ("registering %u static plugins", _num_static_plugins);
331 for (i = 0; i < _num_static_plugins; ++i) {
332 gst_plugin_register_static (_static_plugins[i].major_version,
333 _static_plugins[i].minor_version, _static_plugins[i].name,
334 _static_plugins[i].description, _static_plugins[i].plugin_init,
335 _static_plugins[i].version, _static_plugins[i].license,
336 _static_plugins[i].source, _static_plugins[i].package,
337 _static_plugins[i].origin);
340 if (_static_plugins) {
341 free (_static_plugins);
342 _static_plugins = NULL;
343 _num_static_plugins = 0;
347 /* this function could be extended to check if the plugin license matches the
348 * applications license (would require the app to register its license somehow).
349 * We'll wait for someone who's interested in it to code it :)
352 gst_plugin_check_license (const gchar * license)
354 const gchar **check_license = valid_licenses;
356 g_assert (check_license);
358 while (*check_license) {
359 if (strcmp (license, *check_license) == 0)
367 gst_plugin_check_version (gint major, gint minor)
369 /* return NULL if the major and minor version numbers are not compatible */
371 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
378 gst_plugin_register_func (GstPlugin * plugin, const GstPluginDesc * desc,
381 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
383 GST_WARNING ("plugin \"%s\" has incompatible version, not loading",
388 if (!desc->license || !desc->description || !desc->source ||
389 !desc->package || !desc->origin) {
391 GST_WARNING ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
396 if (!gst_plugin_check_license (desc->license)) {
398 GST_WARNING ("plugin \"%s\" has invalid license \"%s\", not loading",
399 plugin->filename, desc->license);
404 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
406 gst_plugin_desc_copy (&plugin->desc, desc);
409 if (!(((GstPluginInitFullFunc) (desc->plugin_init)) (plugin, user_data))) {
411 GST_WARNING ("plugin \"%s\" failed to initialise", plugin->filename);
412 plugin->module = NULL;
416 if (!((desc->plugin_init) (plugin))) {
418 GST_WARNING ("plugin \"%s\" failed to initialise", plugin->filename);
419 plugin->module = NULL;
425 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
430 #ifdef HAVE_SIGACTION
431 static struct sigaction oldaction;
432 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
435 * _gst_plugin_fault_handler_restore:
436 * segfault handler restorer
439 _gst_plugin_fault_handler_restore (void)
441 if (!_gst_plugin_fault_handler_is_setup)
444 _gst_plugin_fault_handler_is_setup = FALSE;
446 sigaction (SIGSEGV, &oldaction, NULL);
450 * _gst_plugin_fault_handler_sighandler:
451 * segfault handler implementation
454 _gst_plugin_fault_handler_sighandler (int signum)
456 /* We need to restore the fault handler or we'll keep getting it */
457 _gst_plugin_fault_handler_restore ();
461 g_print ("\nERROR: ");
462 g_print ("Caught a segmentation fault while loading plugin file:\n");
463 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
464 g_print ("Please either:\n");
465 g_print ("- remove it and restart.\n");
466 g_print ("- run with --gst-disable-segtrap and debug.\n");
470 g_print ("Caught unhandled signal on plugin loading\n");
476 * _gst_plugin_fault_handler_setup:
477 * sets up the segfault handler
480 _gst_plugin_fault_handler_setup (void)
482 struct sigaction action;
484 /* if asked to leave segfaults alone, just return */
485 if (!gst_segtrap_is_enabled ())
488 if (_gst_plugin_fault_handler_is_setup)
491 _gst_plugin_fault_handler_is_setup = TRUE;
493 memset (&action, 0, sizeof (action));
494 action.sa_handler = _gst_plugin_fault_handler_sighandler;
496 sigaction (SIGSEGV, &action, &oldaction);
498 #else /* !HAVE_SIGACTION */
500 _gst_plugin_fault_handler_restore (void)
505 _gst_plugin_fault_handler_setup (void)
508 #endif /* HAVE_SIGACTION */
510 static void _gst_plugin_fault_handler_setup ();
512 static GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
514 #define CHECK_PLUGIN_DESC_FIELD(desc,field,fn) \
515 if (G_UNLIKELY ((desc)->field == NULL)) { \
516 GST_ERROR ("GstPluginDesc for '%s' has no %s", fn, G_STRINGIFY (field)); \
520 * gst_plugin_load_file:
521 * @filename: the plugin filename to load
522 * @error: pointer to a NULL-valued GError
524 * Loads the given plugin and refs it. Caller needs to unref after use.
526 * Returns: a reference to the existing loaded GstPlugin, a reference to the
527 * newly-loaded GstPlugin, or NULL if an error occurred.
530 gst_plugin_load_file (const gchar * filename, GError ** error)
536 struct stat file_status;
537 GstRegistry *registry;
539 g_return_val_if_fail (filename != NULL, NULL);
541 registry = gst_registry_get_default ();
542 g_static_mutex_lock (&gst_plugin_loading_mutex);
544 plugin = gst_registry_lookup (registry, filename);
546 if (plugin->module) {
547 g_static_mutex_unlock (&gst_plugin_loading_mutex);
550 gst_object_unref (plugin);
555 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
558 if (g_module_supported () == FALSE) {
559 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
562 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
566 if (g_stat (filename, &file_status)) {
567 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
570 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
575 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
576 if (module == NULL) {
577 GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
580 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
582 /* If we failed to open the shared object, then it's probably because a
583 * plugin is linked against the wrong libraries. Print out an easy-to-see
584 * message in this case. */
585 g_warning ("Failed to load plugin '%s': %s", filename, g_module_error ());
589 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
591 plugin->module = module;
592 plugin->filename = g_strdup (filename);
593 plugin->basename = g_path_get_basename (filename);
594 plugin->file_mtime = file_status.st_mtime;
595 plugin->file_size = file_status.st_size;
597 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
599 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
602 GST_PLUGIN_ERROR_MODULE,
603 "File \"%s\" is not a GStreamer plugin", filename);
604 g_module_close (module);
607 plugin->orig_desc = (GstPluginDesc *) ptr;
609 /* check plugin description: complain about bad values but accept them, to
610 * maintain backwards compatibility (FIXME: 0.11) */
611 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, name, filename);
612 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, description, filename);
613 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, version, filename);
614 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, license, filename);
615 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, source, filename);
616 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, package, filename);
617 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, origin, filename);
619 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
622 /* this is where we load the actual .so, so let's trap SIGSEGV */
623 _gst_plugin_fault_handler_setup ();
624 _gst_plugin_fault_handler_filename = plugin->filename;
626 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
629 if (!gst_plugin_register_func (plugin, plugin->orig_desc, NULL)) {
630 /* remove signal handler */
631 _gst_plugin_fault_handler_restore ();
632 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
636 GST_PLUGIN_ERROR_MODULE,
637 "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
639 g_module_close (module);
643 /* remove signal handler */
644 _gst_plugin_fault_handler_restore ();
645 _gst_plugin_fault_handler_filename = NULL;
646 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
648 gst_object_ref (plugin);
649 gst_default_registry_add_plugin (plugin);
651 g_static_mutex_unlock (&gst_plugin_loading_mutex);
657 gst_object_unref (plugin);
658 g_static_mutex_unlock (&gst_plugin_loading_mutex);
664 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
666 dest->major_version = src->major_version;
667 dest->minor_version = src->minor_version;
668 dest->name = g_intern_string (src->name);
669 /* maybe intern the description too, just for convenience? */
670 dest->description = g_strdup (src->description);
671 dest->plugin_init = src->plugin_init;
672 dest->version = g_intern_string (src->version);
673 dest->license = g_intern_string (src->license);
674 dest->source = g_intern_string (src->source);
675 dest->package = g_intern_string (src->package);
676 dest->origin = g_intern_string (src->origin);
681 gst_plugin_desc_free (GstPluginDesc * desc)
683 g_free (desc->description);
684 memset (desc, 0, sizeof (GstPluginDesc));
688 * gst_plugin_get_name:
689 * @plugin: plugin to get the name of
691 * Get the short name of the plugin
693 * Returns: the name of the plugin
696 gst_plugin_get_name (GstPlugin * plugin)
698 g_return_val_if_fail (plugin != NULL, NULL);
700 return plugin->desc.name;
704 * gst_plugin_get_description:
705 * @plugin: plugin to get long name of
707 * Get the long descriptive name of the plugin
709 * Returns: the long name of the plugin
711 G_CONST_RETURN gchar *
712 gst_plugin_get_description (GstPlugin * plugin)
714 g_return_val_if_fail (plugin != NULL, NULL);
716 return plugin->desc.description;
720 * gst_plugin_get_filename:
721 * @plugin: plugin to get the filename of
723 * get the filename of the plugin
725 * Returns: the filename of the plugin
727 G_CONST_RETURN gchar *
728 gst_plugin_get_filename (GstPlugin * plugin)
730 g_return_val_if_fail (plugin != NULL, NULL);
732 return plugin->filename;
736 * gst_plugin_get_version:
737 * @plugin: plugin to get the version of
739 * get the version of the plugin
741 * Returns: the version of the plugin
743 G_CONST_RETURN gchar *
744 gst_plugin_get_version (GstPlugin * plugin)
746 g_return_val_if_fail (plugin != NULL, NULL);
748 return plugin->desc.version;
752 * gst_plugin_get_license:
753 * @plugin: plugin to get the license of
755 * get the license of the plugin
757 * Returns: the license of the plugin
759 G_CONST_RETURN gchar *
760 gst_plugin_get_license (GstPlugin * plugin)
762 g_return_val_if_fail (plugin != NULL, NULL);
764 return plugin->desc.license;
768 * gst_plugin_get_source:
769 * @plugin: plugin to get the source of
771 * get the source module the plugin belongs to.
773 * Returns: the source of the plugin
775 G_CONST_RETURN gchar *
776 gst_plugin_get_source (GstPlugin * plugin)
778 g_return_val_if_fail (plugin != NULL, NULL);
780 return plugin->desc.source;
784 * gst_plugin_get_package:
785 * @plugin: plugin to get the package of
787 * get the package the plugin belongs to.
789 * Returns: the package of the plugin
791 G_CONST_RETURN gchar *
792 gst_plugin_get_package (GstPlugin * plugin)
794 g_return_val_if_fail (plugin != NULL, NULL);
796 return plugin->desc.package;
800 * gst_plugin_get_origin:
801 * @plugin: plugin to get the origin of
803 * get the URL where the plugin comes from
805 * Returns: the origin of the plugin
807 G_CONST_RETURN gchar *
808 gst_plugin_get_origin (GstPlugin * plugin)
810 g_return_val_if_fail (plugin != NULL, NULL);
812 return plugin->desc.origin;
816 * gst_plugin_get_module:
817 * @plugin: plugin to query
819 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
822 * Returns: module belonging to the plugin or NULL if the plugin isn't
826 gst_plugin_get_module (GstPlugin * plugin)
828 g_return_val_if_fail (plugin != NULL, NULL);
830 return plugin->module;
834 * gst_plugin_is_loaded:
835 * @plugin: plugin to query
837 * queries if the plugin is loaded into memory
839 * Returns: TRUE is loaded, FALSE otherwise
842 gst_plugin_is_loaded (GstPlugin * plugin)
844 g_return_val_if_fail (plugin != NULL, FALSE);
846 return (plugin->module != NULL || plugin->filename == NULL);
851 * gst_plugin_feature_list:
852 * @plugin: plugin to query
853 * @filter: the filter to use
854 * @first: only return first match
855 * @user_data: user data passed to the filter function
857 * Runs a filter against all plugin features and returns a GList with
858 * the results. If the first flag is set, only the first match is
859 * returned (as a list with a single object).
861 * Returns: a GList of features, g_list_free after use.
864 gst_plugin_feature_filter (GstPlugin * plugin,
865 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
870 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
872 for (g = list; g; g = g->next) {
873 gst_object_ref (plugin);
881 GstPluginFeatureFilter filter;
889 _feature_filter (GstPlugin * plugin, gpointer user_data)
892 FeatureFilterData *data = (FeatureFilterData *) user_data;
894 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
897 data->result = g_list_concat (data->result, result);
904 * gst_plugin_list_feature_filter:
905 * @list: a #GList of plugins to query
906 * @filter: the filter function to use
907 * @first: only return first match
908 * @user_data: user data passed to the filter function
910 * Runs a filter against all plugin features of the plugins in the given
911 * list and returns a GList with the results.
912 * If the first flag is set, only the first match is
913 * returned (as a list with a single object).
915 * Returns: a GList of features, g_list_free after use.
918 gst_plugin_list_feature_filter (GList * list,
919 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
921 FeatureFilterData data;
924 data.filter = filter;
926 data.user_data = user_data;
929 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
930 g_list_free (result);
937 * gst_plugin_name_filter:
938 * @plugin: the plugin to check
939 * @name: the name of the plugin
941 * A standard filter that returns TRUE when the plugin is of the
944 * Returns: TRUE if the plugin is of the given name.
947 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
949 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
954 * gst_plugin_find_feature:
955 * @plugin: plugin to get the feature from
956 * @name: The name of the feature to find
957 * @type: The type of the feature to find
959 * Find a feature of the given name and type in the given plugin.
961 * Returns: a GstPluginFeature or NULL if the feature was not found.
964 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
967 GstPluginFeature *result = NULL;
968 GstTypeNameData data;
970 g_return_val_if_fail (name != NULL, NULL);
975 walk = gst_filter_run (plugin->features,
976 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
979 result = GST_PLUGIN_FEATURE (walk->data);
981 gst_object_ref (result);
982 gst_plugin_feature_list_free (walk);
991 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
993 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
999 * gst_plugin_find_feature_by_name:
1000 * @plugin: plugin to get the feature from
1001 * @name: The name of the feature to find
1003 * Find a feature of the given name in the given plugin.
1005 * Returns: a GstPluginFeature or NULL if the feature was not found.
1008 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
1011 GstPluginFeature *result = NULL;
1013 g_return_val_if_fail (name != NULL, NULL);
1015 walk = gst_filter_run (plugin->features,
1016 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
1019 result = GST_PLUGIN_FEATURE (walk->data);
1021 gst_object_ref (result);
1022 gst_plugin_feature_list_free (walk);
1030 * gst_plugin_load_by_name:
1031 * @name: name of plugin to load
1033 * Load the named plugin. Refs the plugin.
1035 * Returns: A reference to a loaded plugin, or NULL on error.
1038 gst_plugin_load_by_name (const gchar * name)
1040 GstPlugin *plugin, *newplugin;
1041 GError *error = NULL;
1043 GST_DEBUG ("looking up plugin %s in default registry", name);
1044 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
1046 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
1047 newplugin = gst_plugin_load_file (plugin->filename, &error);
1048 gst_object_unref (plugin);
1051 GST_WARNING ("load_plugin error: %s", error->message);
1052 g_error_free (error);
1055 /* newplugin was reffed by load_file */
1059 GST_DEBUG ("Could not find plugin %s in registry", name);
1065 * @plugin: plugin to load
1067 * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
1068 * untouched. The normal use pattern of this function goes like this:
1071 * GstPlugin *loaded_plugin;
1072 * loaded_plugin = gst_plugin_load (plugin);
1073 * // presumably, we're no longer interested in the potentially-unloaded plugin
1074 * gst_object_unref (plugin);
1075 * plugin = loaded_plugin;
1078 * Returns: A reference to a loaded plugin, or NULL on error.
1081 gst_plugin_load (GstPlugin * plugin)
1083 GError *error = NULL;
1084 GstPlugin *newplugin;
1086 if (gst_plugin_is_loaded (plugin)) {
1090 if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
1097 GST_WARNING ("load_plugin error: %s", error->message);
1098 g_error_free (error);
1104 * gst_plugin_list_free:
1105 * @list: list of #GstPlugin
1107 * Unrefs each member of @list, then frees the list.
1110 gst_plugin_list_free (GList * list)
1114 for (g = list; g; g = g->next) {
1115 gst_object_unref (GST_PLUGIN_CAST (g->data));
1120 /* ===== plugin dependencies ===== */
1123 * ENV + xyz where ENV can contain multiple values separated by SEPARATOR
1124 * xyz may be "" (if ENV contains path to file rather than dir)
1125 * ENV + *xyz same as above, but xyz acts as suffix filter
1126 * ENV + xyz* same as above, but xyz acts as prefix filter (is this needed?)
1127 * ENV + *xyz* same as above, but xyz acts as strstr filter (is this needed?)
1129 * same as above, with additional paths hard-coded at compile-time:
1130 * - only check paths + ... if ENV is not set or yields not paths
1131 * - always check paths + ... in addition to ENV
1133 * When user specifies set of environment variables, he/she may also use e.g.
1134 * "HOME/.mystuff/plugins", and we'll expand the content of $HOME with the
1138 /* we store in registry:
1141 * - environment variables (array of strings)
1142 * - last hash of env variable contents (uint) (so we can avoid doing stats
1143 * if one of the env vars has changed; premature optimisation galore)
1144 * - hard-coded paths (array of strings)
1145 * - xyz filename/suffix/prefix strings (array of strings)
1147 * - last hash of file/dir stats (int)
1149 * (= struct GstPluginDep)
1153 gst_plugin_ext_dep_get_env_vars_hash (GstPlugin * plugin, GstPluginDep * dep)
1158 /* there's no deeper logic to what we do here; all we want to know (when
1159 * checking if the plugin needs to be rescanned) is whether the content of
1160 * one of the environment variables in the list is different from when it
1161 * was last scanned */
1163 for (e = dep->env_vars; e != NULL && *e != NULL; ++e) {
1167 /* order matters: "val",NULL needs to yield a different hash than
1168 * NULL,"val", so do a shift here whether the var is set or not */
1171 /* want environment variable at beginning of string */
1172 if (!g_ascii_isalnum (**e)) {
1173 GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1174 "variable string: %s", *e);
1178 /* user is allowed to specify e.g. "HOME/.pitivi/plugins" */
1179 g_strlcpy (env_var, *e, sizeof (env_var));
1180 g_strdelimit (env_var, "/\\", '\0');
1182 if ((val = g_getenv (env_var)))
1183 hash += g_str_hash (val);
1190 _priv_plugin_deps_env_vars_changed (GstPlugin * plugin)
1194 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1195 GstPluginDep *dep = l->data;
1197 if (dep->env_hash != gst_plugin_ext_dep_get_env_vars_hash (plugin, dep))
1205 gst_plugin_ext_dep_extract_env_vars_paths (GstPlugin * plugin,
1209 GList *paths = NULL;
1211 for (evars = dep->env_vars; evars != NULL && *evars != NULL; ++evars) {
1215 /* want environment variable at beginning of string */
1216 if (!g_ascii_isalnum (**evars)) {
1217 GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1218 "variable string: %s", *evars);
1222 /* user is allowed to specify e.g. "HOME/.pitivi/plugins", which we want to
1223 * split into the env_var name component and the path component */
1224 components = g_strsplit_set (*evars, "/\\", 2);
1225 g_assert (components != NULL);
1227 e = g_getenv (components[0]);
1228 GST_LOG_OBJECT (plugin, "expanding %s = '%s' (path suffix: %s)",
1229 components[0], GST_STR_NULL (e), GST_STR_NULL (components[1]));
1231 if (components[1] != NULL) {
1232 g_strdelimit (components[1], "/\\", G_DIR_SEPARATOR);
1235 if (e != NULL && *e != '\0') {
1239 arr = g_strsplit (e, G_SEARCHPATH_SEPARATOR_S, -1);
1241 for (i = 0; arr != NULL && arr[i] != NULL; ++i) {
1244 if (!g_path_is_absolute (arr[i])) {
1245 GST_INFO_OBJECT (plugin, "ignoring environment variable content '%s'"
1246 ": either not an absolute path or not a path at all", arr[i]);
1250 if (components[1] != NULL) {
1251 full_path = g_build_filename (arr[i], components[1], NULL);
1253 full_path = g_strdup (arr[i]);
1256 if (!g_list_find_custom (paths, full_path, (GCompareFunc) strcmp)) {
1257 GST_LOG_OBJECT (plugin, "path: '%s'", full_path);
1258 paths = g_list_prepend (paths, full_path);
1261 GST_LOG_OBJECT (plugin, "path: '%s' (duplicate,ignoring)", full_path);
1269 g_strfreev (components);
1272 GST_LOG_OBJECT (plugin, "Extracted %d paths from environment",
1273 g_list_length (paths));
1279 gst_plugin_ext_dep_get_hash_from_stat_entry (struct stat *s)
1281 if (!(s->st_mode & (S_IFDIR | S_IFREG)))
1284 /* completely random formula */
1285 return ((s->st_size << 3) + (s->st_mtime << 5)) ^ s->st_ctime;
1289 gst_plugin_ext_dep_direntry_matches (GstPlugin * plugin, const gchar * entry,
1290 const gchar ** filenames, GstPluginDependencyFlags flags)
1292 /* no filenames specified, match all entries for now (could probably
1293 * optimise by just taking the dir stat hash or so) */
1294 if (filenames == NULL || *filenames == NULL || **filenames == '\0')
1297 while (*filenames != NULL) {
1299 if (((flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)) &&
1300 g_str_has_suffix (entry, *filenames)) {
1302 /* else it's an exact match that's needed */
1303 } else if (strcmp (entry, *filenames) == 0) {
1306 GST_LOG ("%s does not match %s, flags=0x%04x", entry, *filenames, flags);
1313 gst_plugin_ext_dep_scan_dir_and_match_names (GstPlugin * plugin,
1314 const gchar * path, const gchar ** filenames,
1315 GstPluginDependencyFlags flags, int depth)
1318 gboolean recurse_dirs;
1323 recurse_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1325 dir = g_dir_open (path, 0, &err);
1327 GST_DEBUG_OBJECT (plugin, "g_dir_open(%s) failed: %s", path, err->message);
1332 /* FIXME: we're assuming here that we always get the directory entries in
1333 * the same order, and not in a random order */
1334 while ((entry = g_dir_read_name (dir))) {
1335 gboolean have_match;
1341 gst_plugin_ext_dep_direntry_matches (plugin, entry, filenames, flags);
1343 /* avoid the stat if possible */
1344 if (!have_match && !recurse_dirs)
1347 full_path = g_build_filename (path, entry, NULL);
1348 if (g_stat (full_path, &s) < 0) {
1349 fhash = (guint) - 1;
1350 GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1351 g_strerror (errno));
1352 } else if (have_match) {
1353 fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1354 GST_LOG_OBJECT (plugin, "stat: %s (result: %u)", full_path, fhash);
1355 } else if ((s.st_mode & (S_IFDIR))) {
1356 fhash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, full_path,
1357 filenames, flags, depth + 1);
1359 /* it's not a name match, we want to recurse, but it's not a directory */
1364 hash = (hash + fhash) << 1;
1373 gst_plugin_ext_dep_scan_path_with_filenames (GstPlugin * plugin,
1374 const gchar * path, const gchar ** filenames,
1375 GstPluginDependencyFlags flags)
1377 const gchar *empty_filenames[] = { "", NULL };
1378 gboolean recurse_into_dirs, partial_names;
1381 /* to avoid special-casing below (FIXME?) */
1382 if (filenames == NULL || *filenames == NULL)
1383 filenames = empty_filenames;
1385 recurse_into_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1386 partial_names = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX);
1388 /* if we can construct the exact paths to check with the data we have, just
1389 * stat them one by one; this is more efficient than opening the directory
1390 * and going through each entry to see if it matches one of our filenames. */
1391 if (!recurse_into_dirs && !partial_names) {
1392 for (i = 0; filenames[i] != NULL; ++i) {
1397 full_path = g_build_filename (path, filenames[i], NULL);
1398 if (g_stat (full_path, &s) < 0) {
1399 fhash = (guint) - 1;
1400 GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1401 g_strerror (errno));
1403 fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1404 GST_LOG_OBJECT (plugin, "stat: %s (result: %08x)", full_path, fhash);
1406 hash = (hash + fhash) << 1;
1410 hash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, path,
1411 filenames, flags, 0);
1418 gst_plugin_ext_dep_get_stat_hash (GstPlugin * plugin, GstPluginDep * dep)
1420 gboolean paths_are_default_only;
1422 guint scan_hash = 0;
1424 GST_LOG_OBJECT (plugin, "start");
1426 paths_are_default_only =
1427 dep->flags & GST_PLUGIN_DEPENDENCY_FLAG_PATHS_ARE_DEFAULT_ONLY;
1429 scan_paths = gst_plugin_ext_dep_extract_env_vars_paths (plugin, dep);
1431 if (scan_paths == NULL || !paths_are_default_only) {
1434 for (paths = dep->paths; paths != NULL && *paths != NULL; ++paths) {
1435 const gchar *path = *paths;
1437 if (!g_list_find_custom (scan_paths, path, (GCompareFunc) strcmp)) {
1438 GST_LOG_OBJECT (plugin, "path: '%s'", path);
1439 scan_paths = g_list_prepend (scan_paths, g_strdup (path));
1441 GST_LOG_OBJECT (plugin, "path: '%s' (duplicate, ignoring)", path);
1446 /* not that the order really matters, but it makes debugging easier */
1447 scan_paths = g_list_reverse (scan_paths);
1449 while (scan_paths != NULL) {
1450 const gchar *path = scan_paths->data;
1452 scan_hash += gst_plugin_ext_dep_scan_path_with_filenames (plugin, path,
1453 (const gchar **) dep->names, dep->flags);
1454 scan_hash = scan_hash << 1;
1456 g_free (scan_paths->data);
1457 scan_paths = g_list_delete_link (scan_paths, scan_paths);
1460 GST_LOG_OBJECT (plugin, "done, scan_hash: %08x", scan_hash);
1465 _priv_plugin_deps_files_changed (GstPlugin * plugin)
1469 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1470 GstPluginDep *dep = l->data;
1472 if (dep->stat_hash != gst_plugin_ext_dep_get_stat_hash (plugin, dep))
1480 gst_plugin_ext_dep_free (GstPluginDep * dep)
1482 g_strfreev (dep->env_vars);
1483 g_strfreev (dep->paths);
1484 g_strfreev (dep->names);
1489 gst_plugin_ext_dep_strv_equal (gchar ** arr1, gchar ** arr2)
1493 if (arr1 == NULL || arr2 == NULL)
1495 for (; *arr1 != NULL && *arr2 != NULL; ++arr1, ++arr2) {
1496 if (strcmp (*arr1, *arr2) != 0)
1499 return (*arr1 == *arr2);
1503 gst_plugin_ext_dep_equals (GstPluginDep * dep, const gchar ** env_vars,
1504 const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1506 if (dep->flags != flags)
1509 return gst_plugin_ext_dep_strv_equal (dep->env_vars, (gchar **) env_vars) &&
1510 gst_plugin_ext_dep_strv_equal (dep->paths, (gchar **) paths) &&
1511 gst_plugin_ext_dep_strv_equal (dep->names, (gchar **) names);
1515 * gst_plugin_add_dependency:
1516 * @plugin: a #GstPlugin
1517 * @env_vars: NULL-terminated array of environent variables affecting the
1518 * feature set of the plugin (e.g. an environment variable containing
1519 * paths where to look for additional modules/plugins of a library),
1520 * or NULL. Environment variable names may be followed by a path component
1521 * which will be added to the content of the environment variable, e.g.
1522 * "HOME/.mystuff/plugins".
1523 * @paths: NULL-terminated array of directories/paths where dependent files
1525 * @names: NULL-terminated array of file names (or file name suffixes,
1526 * depending on @flags) to be used in combination with the paths from
1527 * @paths and/or the paths extracted from the environment variables in
1528 * @env_vars, or NULL.
1529 * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1531 * Make GStreamer aware of external dependencies which affect the feature
1532 * set of this plugin (ie. the elements or typefinders associated with it).
1534 * GStreamer will re-inspect plugins with external dependencies whenever any
1535 * of the external dependencies change. This is useful for plugins which wrap
1536 * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1537 * library and makes visualisations available as GStreamer elements, or a
1538 * codec loader which exposes elements and/or caps dependent on what external
1539 * codec libraries are currently installed.
1544 gst_plugin_add_dependency (GstPlugin * plugin, const gchar ** env_vars,
1545 const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1550 g_return_if_fail (GST_IS_PLUGIN (plugin));
1551 g_return_if_fail (env_vars != NULL || paths != NULL);
1553 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1554 if (gst_plugin_ext_dep_equals (l->data, env_vars, paths, names, flags)) {
1555 GST_LOG_OBJECT (plugin, "dependency already registered");
1560 dep = g_new0 (GstPluginDep, 1);
1562 dep->env_vars = g_strdupv ((gchar **) env_vars);
1563 dep->paths = g_strdupv ((gchar **) paths);
1564 dep->names = g_strdupv ((gchar **) names);
1567 dep->env_hash = gst_plugin_ext_dep_get_env_vars_hash (plugin, dep);
1568 dep->stat_hash = gst_plugin_ext_dep_get_stat_hash (plugin, dep);
1570 plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
1572 GST_DEBUG_OBJECT (plugin, "added dependency:");
1573 for (; env_vars != NULL && *env_vars != NULL; ++env_vars)
1574 GST_DEBUG_OBJECT (plugin, " evar: %s", *env_vars);
1575 for (; paths != NULL && *paths != NULL; ++paths)
1576 GST_DEBUG_OBJECT (plugin, " path: %s", *paths);
1577 for (; names != NULL && *names != NULL; ++names)
1578 GST_DEBUG_OBJECT (plugin, " name: %s", *names);
1582 * gst_plugin_add_dependency_simple:
1583 * @plugin: the #GstPlugin
1584 * @env_vars: one or more environent variables (separated by ':', ';' or ','),
1585 * or NULL. Environment variable names may be followed by a path component
1586 * which will be added to the content of the environment variable, e.g.
1587 * "HOME/.mystuff/plugins:MYSTUFF_PLUGINS_PATH"
1588 * @paths: one ore more directory paths (separated by ':' or ';' or ','),
1589 * or NULL. Example: "/usr/lib/mystuff/plugins"
1590 * @names: one or more file names or file name suffixes (separated by commas),
1592 * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1594 * Make GStreamer aware of external dependencies which affect the feature
1595 * set of this plugin (ie. the elements or typefinders associated with it).
1597 * GStreamer will re-inspect plugins with external dependencies whenever any
1598 * of the external dependencies change. This is useful for plugins which wrap
1599 * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1600 * library and makes visualisations available as GStreamer elements, or a
1601 * codec loader which exposes elements and/or caps dependent on what external
1602 * codec libraries are currently installed.
1604 * Convenience wrapper function for gst_plugin_add_dependency() which
1605 * takes simple strings as arguments instead of string arrays, with multiple
1606 * arguments separated by predefined delimiters (see above).
1611 gst_plugin_add_dependency_simple (GstPlugin * plugin,
1612 const gchar * env_vars, const gchar * paths, const gchar * names,
1613 GstPluginDependencyFlags flags)
1615 gchar **a_evars = NULL;
1616 gchar **a_paths = NULL;
1617 gchar **a_names = NULL;
1620 a_evars = g_strsplit_set (env_vars, ":;,", -1);
1622 a_paths = g_strsplit_set (paths, ":;,", -1);
1624 a_names = g_strsplit_set (names, ",", -1);
1626 gst_plugin_add_dependency (plugin, (const gchar **) a_evars,
1627 (const gchar **) a_paths, (const gchar **) a_names, flags);
1630 g_strfreev (a_evars);
1632 g_strfreev (a_paths);
1634 g_strfreev (a_names);