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);
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) {
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_initialize (void)
258 _gst_plugin_inited = TRUE;
260 /* now register all static plugins */
261 GST_INFO ("registering %u static plugins", _num_static_plugins);
262 for (i = 0; i < _num_static_plugins; ++i) {
263 gst_plugin_register_static (_static_plugins[i].major_version,
264 _static_plugins[i].minor_version, _static_plugins[i].name,
265 _static_plugins[i].description, _static_plugins[i].plugin_init,
266 _static_plugins[i].version, _static_plugins[i].license,
267 _static_plugins[i].source, _static_plugins[i].package,
268 _static_plugins[i].origin);
271 if (_static_plugins) {
272 free (_static_plugins);
273 _static_plugins = NULL;
274 _num_static_plugins = 0;
278 /* this function could be extended to check if the plugin license matches the
279 * applications license (would require the app to register its license somehow).
280 * We'll wait for someone who's interested in it to code it :)
283 gst_plugin_check_license (const gchar * license)
285 const gchar **check_license = valid_licenses;
287 g_assert (check_license);
289 while (*check_license) {
290 if (strcmp (license, *check_license) == 0)
298 gst_plugin_check_version (gint major, gint minor)
300 /* return NULL if the major and minor version numbers are not compatible */
302 if (major != GST_VERSION_MAJOR || minor != GST_VERSION_MINOR)
309 gst_plugin_register_func (GstPlugin * plugin, const GstPluginDesc * desc)
311 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
313 GST_WARNING ("plugin \"%s\" has incompatible version, not loading",
318 if (!desc->license || !desc->description || !desc->source ||
319 !desc->package || !desc->origin) {
321 GST_WARNING ("plugin \"%s\" has incorrect GstPluginDesc, not loading",
326 if (!gst_plugin_check_license (desc->license)) {
328 GST_WARNING ("plugin \"%s\" has invalid license \"%s\", not loading",
329 plugin->filename, desc->license);
334 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
336 gst_plugin_desc_copy (&plugin->desc, desc);
338 if (!((desc->plugin_init) (plugin))) {
340 GST_WARNING ("plugin \"%s\" failed to initialise", plugin->filename);
341 plugin->module = NULL;
346 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
351 #ifdef HAVE_SIGACTION
352 static struct sigaction oldaction;
353 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
356 * _gst_plugin_fault_handler_restore:
357 * segfault handler restorer
360 _gst_plugin_fault_handler_restore (void)
362 if (!_gst_plugin_fault_handler_is_setup)
365 _gst_plugin_fault_handler_is_setup = FALSE;
367 sigaction (SIGSEGV, &oldaction, NULL);
371 * _gst_plugin_fault_handler_sighandler:
372 * segfault handler implementation
375 _gst_plugin_fault_handler_sighandler (int signum)
377 /* We need to restore the fault handler or we'll keep getting it */
378 _gst_plugin_fault_handler_restore ();
382 g_print ("\nERROR: ");
383 g_print ("Caught a segmentation fault while loading plugin file:\n");
384 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
385 g_print ("Please either:\n");
386 g_print ("- remove it and restart.\n");
387 g_print ("- run with --gst-disable-segtrap and debug.\n");
391 g_print ("Caught unhandled signal on plugin loading\n");
397 * _gst_plugin_fault_handler_setup:
398 * sets up the segfault handler
401 _gst_plugin_fault_handler_setup (void)
403 struct sigaction action;
405 /* if asked to leave segfaults alone, just return */
406 if (!gst_segtrap_is_enabled ())
409 if (_gst_plugin_fault_handler_is_setup)
412 _gst_plugin_fault_handler_is_setup = TRUE;
414 memset (&action, 0, sizeof (action));
415 action.sa_handler = _gst_plugin_fault_handler_sighandler;
417 sigaction (SIGSEGV, &action, &oldaction);
419 #else /* !HAVE_SIGACTION */
421 _gst_plugin_fault_handler_restore (void)
426 _gst_plugin_fault_handler_setup (void)
429 #endif /* HAVE_SIGACTION */
431 static void _gst_plugin_fault_handler_setup ();
433 static GStaticMutex gst_plugin_loading_mutex = G_STATIC_MUTEX_INIT;
435 #define CHECK_PLUGIN_DESC_FIELD(desc,field,fn) \
436 if (G_UNLIKELY ((desc)->field == NULL)) { \
437 GST_ERROR ("GstPluginDesc for '%s' has no %s", fn, G_STRINGIFY (field)); \
441 * gst_plugin_load_file:
442 * @filename: the plugin filename to load
443 * @error: pointer to a NULL-valued GError
445 * Loads the given plugin and refs it. Caller needs to unref after use.
447 * Returns: a reference to the existing loaded GstPlugin, a reference to the
448 * newly-loaded GstPlugin, or NULL if an error occurred.
451 gst_plugin_load_file (const gchar * filename, GError ** error)
457 struct stat file_status;
458 GstRegistry *registry;
460 g_return_val_if_fail (filename != NULL, NULL);
462 registry = gst_registry_get_default ();
463 g_static_mutex_lock (&gst_plugin_loading_mutex);
465 plugin = gst_registry_lookup (registry, filename);
467 if (plugin->module) {
468 g_static_mutex_unlock (&gst_plugin_loading_mutex);
471 gst_object_unref (plugin);
476 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
479 if (g_module_supported () == FALSE) {
480 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
483 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
487 if (g_stat (filename, &file_status)) {
488 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
491 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
496 module = g_module_open (filename, G_MODULE_BIND_LOCAL);
497 if (module == NULL) {
498 GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s",
501 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
503 /* If we failed to open the shared object, then it's probably because a
504 * plugin is linked against the wrong libraries. Print out an easy-to-see
505 * message in this case. */
506 g_warning ("Failed to load plugin '%s': %s", filename, g_module_error ());
510 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
512 plugin->module = module;
513 plugin->filename = g_strdup (filename);
514 plugin->basename = g_path_get_basename (filename);
515 plugin->file_mtime = file_status.st_mtime;
516 plugin->file_size = file_status.st_size;
518 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
520 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
523 GST_PLUGIN_ERROR_MODULE,
524 "File \"%s\" is not a GStreamer plugin", filename);
525 g_module_close (module);
528 plugin->orig_desc = (GstPluginDesc *) ptr;
530 /* check plugin description: complain about bad values but accept them, to
531 * maintain backwards compatibility (FIXME: 0.11) */
532 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, name, filename);
533 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, description, filename);
534 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, version, filename);
535 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, license, filename);
536 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, source, filename);
537 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, package, filename);
538 CHECK_PLUGIN_DESC_FIELD (plugin->orig_desc, origin, filename);
540 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
543 /* this is where we load the actual .so, so let's trap SIGSEGV */
544 _gst_plugin_fault_handler_setup ();
545 _gst_plugin_fault_handler_filename = plugin->filename;
547 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
550 if (!gst_plugin_register_func (plugin, plugin->orig_desc)) {
551 /* remove signal handler */
552 _gst_plugin_fault_handler_restore ();
553 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
557 GST_PLUGIN_ERROR_MODULE,
558 "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
560 g_module_close (module);
564 /* remove signal handler */
565 _gst_plugin_fault_handler_restore ();
566 _gst_plugin_fault_handler_filename = NULL;
567 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
569 gst_object_ref (plugin);
570 gst_default_registry_add_plugin (plugin);
572 g_static_mutex_unlock (&gst_plugin_loading_mutex);
578 gst_object_unref (plugin);
579 g_static_mutex_unlock (&gst_plugin_loading_mutex);
585 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
587 dest->major_version = src->major_version;
588 dest->minor_version = src->minor_version;
589 dest->name = g_intern_string (src->name);
590 /* maybe intern the description too, just for convenience? */
591 dest->description = g_strdup (src->description);
592 dest->plugin_init = src->plugin_init;
593 dest->version = g_intern_string (src->version);
594 dest->license = g_intern_string (src->license);
595 dest->source = g_intern_string (src->source);
596 dest->package = g_intern_string (src->package);
597 dest->origin = g_intern_string (src->origin);
602 gst_plugin_desc_free (GstPluginDesc * desc)
604 g_free (desc->description);
605 memset (desc, 0, sizeof (GstPluginDesc));
609 * gst_plugin_get_name:
610 * @plugin: plugin to get the name of
612 * Get the short name of the plugin
614 * Returns: the name of the plugin
617 gst_plugin_get_name (GstPlugin * plugin)
619 g_return_val_if_fail (plugin != NULL, NULL);
621 return plugin->desc.name;
625 * gst_plugin_get_description:
626 * @plugin: plugin to get long name of
628 * Get the long descriptive name of the plugin
630 * Returns: the long name of the plugin
632 G_CONST_RETURN gchar *
633 gst_plugin_get_description (GstPlugin * plugin)
635 g_return_val_if_fail (plugin != NULL, NULL);
637 return plugin->desc.description;
641 * gst_plugin_get_filename:
642 * @plugin: plugin to get the filename of
644 * get the filename of the plugin
646 * Returns: the filename of the plugin
648 G_CONST_RETURN gchar *
649 gst_plugin_get_filename (GstPlugin * plugin)
651 g_return_val_if_fail (plugin != NULL, NULL);
653 return plugin->filename;
657 * gst_plugin_get_version:
658 * @plugin: plugin to get the version of
660 * get the version of the plugin
662 * Returns: the version of the plugin
664 G_CONST_RETURN gchar *
665 gst_plugin_get_version (GstPlugin * plugin)
667 g_return_val_if_fail (plugin != NULL, NULL);
669 return plugin->desc.version;
673 * gst_plugin_get_license:
674 * @plugin: plugin to get the license of
676 * get the license of the plugin
678 * Returns: the license of the plugin
680 G_CONST_RETURN gchar *
681 gst_plugin_get_license (GstPlugin * plugin)
683 g_return_val_if_fail (plugin != NULL, NULL);
685 return plugin->desc.license;
689 * gst_plugin_get_source:
690 * @plugin: plugin to get the source of
692 * get the source module the plugin belongs to.
694 * Returns: the source of the plugin
696 G_CONST_RETURN gchar *
697 gst_plugin_get_source (GstPlugin * plugin)
699 g_return_val_if_fail (plugin != NULL, NULL);
701 return plugin->desc.source;
705 * gst_plugin_get_package:
706 * @plugin: plugin to get the package of
708 * get the package the plugin belongs to.
710 * Returns: the package of the plugin
712 G_CONST_RETURN gchar *
713 gst_plugin_get_package (GstPlugin * plugin)
715 g_return_val_if_fail (plugin != NULL, NULL);
717 return plugin->desc.package;
721 * gst_plugin_get_origin:
722 * @plugin: plugin to get the origin of
724 * get the URL where the plugin comes from
726 * Returns: the origin of the plugin
728 G_CONST_RETURN gchar *
729 gst_plugin_get_origin (GstPlugin * plugin)
731 g_return_val_if_fail (plugin != NULL, NULL);
733 return plugin->desc.origin;
737 * gst_plugin_get_module:
738 * @plugin: plugin to query
740 * Gets the #GModule of the plugin. If the plugin isn't loaded yet, NULL is
743 * Returns: module belonging to the plugin or NULL if the plugin isn't
747 gst_plugin_get_module (GstPlugin * plugin)
749 g_return_val_if_fail (plugin != NULL, NULL);
751 return plugin->module;
755 * gst_plugin_is_loaded:
756 * @plugin: plugin to query
758 * queries if the plugin is loaded into memory
760 * Returns: TRUE is loaded, FALSE otherwise
763 gst_plugin_is_loaded (GstPlugin * plugin)
765 g_return_val_if_fail (plugin != NULL, FALSE);
767 return (plugin->module != NULL || plugin->filename == NULL);
772 * gst_plugin_feature_list:
773 * @plugin: plugin to query
774 * @filter: the filter to use
775 * @first: only return first match
776 * @user_data: user data passed to the filter function
778 * Runs a filter against all plugin features and returns a GList with
779 * the results. If the first flag is set, only the first match is
780 * returned (as a list with a single object).
782 * Returns: a GList of features, g_list_free after use.
785 gst_plugin_feature_filter (GstPlugin * plugin,
786 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
791 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
793 for (g = list; g; g = g->next) {
794 gst_object_ref (plugin);
802 GstPluginFeatureFilter filter;
810 _feature_filter (GstPlugin * plugin, gpointer user_data)
813 FeatureFilterData *data = (FeatureFilterData *) user_data;
815 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
818 data->result = g_list_concat (data->result, result);
825 * gst_plugin_list_feature_filter:
826 * @list: a #GList of plugins to query
827 * @filter: the filter function to use
828 * @first: only return first match
829 * @user_data: user data passed to the filter function
831 * Runs a filter against all plugin features of the plugins in the given
832 * list and returns a GList with the results.
833 * If the first flag is set, only the first match is
834 * returned (as a list with a single object).
836 * Returns: a GList of features, g_list_free after use.
839 gst_plugin_list_feature_filter (GList * list,
840 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
842 FeatureFilterData data;
845 data.filter = filter;
847 data.user_data = user_data;
850 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
851 g_list_free (result);
858 * gst_plugin_name_filter:
859 * @plugin: the plugin to check
860 * @name: the name of the plugin
862 * A standard filter that returns TRUE when the plugin is of the
865 * Returns: TRUE if the plugin is of the given name.
868 gst_plugin_name_filter (GstPlugin * plugin, const gchar * name)
870 return (plugin->desc.name && !strcmp (plugin->desc.name, name));
875 * gst_plugin_find_feature:
876 * @plugin: plugin to get the feature from
877 * @name: The name of the feature to find
878 * @type: The type of the feature to find
880 * Find a feature of the given name and type in the given plugin.
882 * Returns: a GstPluginFeature or NULL if the feature was not found.
885 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
888 GstPluginFeature *result = NULL;
889 GstTypeNameData data;
891 g_return_val_if_fail (name != NULL, NULL);
896 walk = gst_filter_run (plugin->features,
897 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
900 result = GST_PLUGIN_FEATURE (walk->data);
902 gst_object_ref (result);
903 gst_plugin_feature_list_free (walk);
912 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
914 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
920 * gst_plugin_find_feature_by_name:
921 * @plugin: plugin to get the feature from
922 * @name: The name of the feature to find
924 * Find a feature of the given name in the given plugin.
926 * Returns: a GstPluginFeature or NULL if the feature was not found.
929 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
932 GstPluginFeature *result = NULL;
934 g_return_val_if_fail (name != NULL, NULL);
936 walk = gst_filter_run (plugin->features,
937 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
940 result = GST_PLUGIN_FEATURE (walk->data);
942 gst_object_ref (result);
943 gst_plugin_feature_list_free (walk);
951 * gst_plugin_load_by_name:
952 * @name: name of plugin to load
954 * Load the named plugin. Refs the plugin.
956 * Returns: A reference to a loaded plugin, or NULL on error.
959 gst_plugin_load_by_name (const gchar * name)
961 GstPlugin *plugin, *newplugin;
962 GError *error = NULL;
964 GST_DEBUG ("looking up plugin %s in default registry", name);
965 plugin = gst_registry_find_plugin (gst_registry_get_default (), name);
967 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
968 newplugin = gst_plugin_load_file (plugin->filename, &error);
969 gst_object_unref (plugin);
972 GST_WARNING ("load_plugin error: %s", error->message);
973 g_error_free (error);
976 /* newplugin was reffed by load_file */
980 GST_DEBUG ("Could not find plugin %s in registry", name);
986 * @plugin: plugin to load
988 * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
989 * untouched. The normal use pattern of this function goes like this:
992 * GstPlugin *loaded_plugin;
993 * loaded_plugin = gst_plugin_load (plugin);
994 * // presumably, we're no longer interested in the potentially-unloaded plugin
995 * gst_object_unref (plugin);
996 * plugin = loaded_plugin;
999 * Returns: A reference to a loaded plugin, or NULL on error.
1002 gst_plugin_load (GstPlugin * plugin)
1004 GError *error = NULL;
1005 GstPlugin *newplugin;
1007 if (gst_plugin_is_loaded (plugin)) {
1011 if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
1018 GST_WARNING ("load_plugin error: %s", error->message);
1019 g_error_free (error);
1025 * gst_plugin_list_free:
1026 * @list: list of #GstPlugin
1028 * Unrefs each member of @list, then frees the list.
1031 gst_plugin_list_free (GList * list)
1035 for (g = list; g; g = g->next) {
1036 gst_object_unref (GST_PLUGIN_CAST (g->data));
1041 /* ===== plugin dependencies ===== */
1044 * ENV + xyz where ENV can contain multiple values separated by SEPARATOR
1045 * xyz may be "" (if ENV contains path to file rather than dir)
1046 * ENV + *xyz same as above, but xyz acts as suffix filter
1047 * ENV + xyz* same as above, but xyz acts as prefix filter (is this needed?)
1048 * ENV + *xyz* same as above, but xyz acts as strstr filter (is this needed?)
1050 * same as above, with additional paths hard-coded at compile-time:
1051 * - only check paths + ... if ENV is not set or yields not paths
1052 * - always check paths + ... in addition to ENV
1054 * When user specifies set of environment variables, he/she may also use e.g.
1055 * "HOME/.mystuff/plugins", and we'll expand the content of $HOME with the
1059 /* we store in registry:
1062 * - environment variables (array of strings)
1063 * - last hash of env variable contents (uint) (so we can avoid doing stats
1064 * if one of the env vars has changed; premature optimisation galore)
1065 * - hard-coded paths (array of strings)
1066 * - xyz filename/suffix/prefix strings (array of strings)
1068 * - last hash of file/dir stats (int)
1070 * (= struct GstPluginDep)
1074 gst_plugin_ext_dep_get_env_vars_hash (GstPlugin * plugin, GstPluginDep * dep)
1079 /* there's no deeper logic to what we do here; all we want to know (when
1080 * checking if the plugin needs to be rescanned) is whether the content of
1081 * one of the environment variables in the list is different from when it
1082 * was last scanned */
1084 for (e = dep->env_vars; e != NULL && *e != NULL; ++e) {
1088 /* order matters: "val",NULL needs to yield a different hash than
1089 * NULL,"val", so do a shift here whether the var is set or not */
1092 /* want environment variable at beginning of string */
1093 if (!g_ascii_isalnum (**e)) {
1094 GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1095 "variable string: %s", *e);
1099 /* user is allowed to specify e.g. "HOME/.pitivi/plugins" */
1100 g_strlcpy (env_var, *e, sizeof (env_var));
1101 g_strdelimit (env_var, "/\\", '\0');
1103 if ((val = g_getenv (env_var)))
1104 hash += g_str_hash (val);
1111 _priv_plugin_deps_env_vars_changed (GstPlugin * plugin)
1115 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1116 GstPluginDep *dep = l->data;
1118 if (dep->env_hash != gst_plugin_ext_dep_get_env_vars_hash (plugin, dep))
1126 gst_plugin_ext_dep_extract_env_vars_paths (GstPlugin * plugin,
1130 GList *paths = NULL;
1132 for (evars = dep->env_vars; evars != NULL && *evars != NULL; ++evars) {
1136 /* want environment variable at beginning of string */
1137 if (!g_ascii_isalnum (**evars)) {
1138 GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1139 "variable string: %s", *evars);
1143 /* user is allowed to specify e.g. "HOME/.pitivi/plugins", which we want to
1144 * split into the env_var name component and the path component */
1145 components = g_strsplit_set (*evars, "/\\", 2);
1146 g_assert (components != NULL);
1148 e = g_getenv (components[0]);
1149 GST_LOG_OBJECT (plugin, "expanding %s = '%s' (path suffix: %s)",
1150 components[0], GST_STR_NULL (e), GST_STR_NULL (components[1]));
1152 if (components[1] != NULL) {
1153 g_strdelimit (components[1], "/\\", G_DIR_SEPARATOR);
1156 if (e != NULL && *e != '\0') {
1160 arr = g_strsplit (e, G_SEARCHPATH_SEPARATOR_S, -1);
1162 for (i = 0; arr != NULL && arr[i] != NULL; ++i) {
1165 if (!g_path_is_absolute (arr[i])) {
1166 GST_INFO_OBJECT (plugin, "ignoring environment variable content '%s'"
1167 ": either not an absolute path or not a path at all", arr[i]);
1171 if (components[1] != NULL) {
1172 full_path = g_build_filename (arr[i], components[1], NULL);
1174 full_path = g_strdup (arr[i]);
1177 if (!g_list_find_custom (paths, full_path, (GCompareFunc) strcmp)) {
1178 GST_LOG_OBJECT (plugin, "path: '%s'", full_path);
1179 paths = g_list_prepend (paths, full_path);
1182 GST_LOG_OBJECT (plugin, "path: '%s' (duplicate,ignoring)", full_path);
1190 g_strfreev (components);
1193 GST_LOG_OBJECT (plugin, "Extracted %d paths from environment",
1194 g_list_length (paths));
1200 gst_plugin_ext_dep_get_hash_from_stat_entry (struct stat *s)
1202 if (!(s->st_mode & (S_IFDIR | S_IFREG)))
1205 /* completely random formula */
1206 return ((s->st_size << 3) + (s->st_mtime << 5)) ^ s->st_ctime;
1210 gst_plugin_ext_dep_direntry_matches (GstPlugin * plugin, const gchar * entry,
1211 const gchar ** filenames, GstPluginDependencyFlags flags)
1213 /* no filenames specified, match all entries for now (could probably
1214 * optimise by just taking the dir stat hash or so) */
1215 if (filenames == NULL || *filenames == NULL || **filenames == '\0')
1218 while (*filenames != NULL) {
1220 if (((flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)) &&
1221 g_str_has_suffix (entry, *filenames)) {
1223 /* else it's an exact match that's needed */
1224 } else if (strcmp (entry, *filenames) == 0) {
1227 GST_LOG ("%s does not match %s, flags=0x%04x", entry, *filenames, flags);
1234 gst_plugin_ext_dep_scan_dir_and_match_names (GstPlugin * plugin,
1235 const gchar * path, const gchar ** filenames,
1236 GstPluginDependencyFlags flags, int depth)
1239 gboolean recurse_dirs;
1244 recurse_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1246 dir = g_dir_open (path, 0, &err);
1248 GST_DEBUG_OBJECT (plugin, "g_dir_open(%s) failed: %s", path, err->message);
1253 /* FIXME: we're assuming here that we always get the directory entries in
1254 * the same order, and not in a random order */
1255 while ((entry = g_dir_read_name (dir))) {
1256 gboolean have_match;
1262 gst_plugin_ext_dep_direntry_matches (plugin, entry, filenames, flags);
1264 /* avoid the stat if possible */
1265 if (!have_match && !recurse_dirs)
1268 full_path = g_build_filename (path, entry, NULL);
1269 if (g_stat (full_path, &s) < 0) {
1270 fhash = (guint) - 1;
1271 GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1272 g_strerror (errno));
1273 } else if (have_match) {
1274 fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1275 GST_LOG_OBJECT (plugin, "stat: %s (result: %u)", full_path, fhash);
1276 } else if ((s.st_mode & (S_IFDIR))) {
1277 fhash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, full_path,
1278 filenames, flags, depth + 1);
1280 /* it's not a name match, we want to recurse, but it's not a directory */
1285 hash = (hash + fhash) << 1;
1294 gst_plugin_ext_dep_scan_path_with_filenames (GstPlugin * plugin,
1295 const gchar * path, const gchar ** filenames,
1296 GstPluginDependencyFlags flags)
1298 const gchar *empty_filenames[] = { "", NULL };
1299 gboolean recurse_into_dirs, partial_names;
1302 /* to avoid special-casing below (FIXME?) */
1303 if (filenames == NULL || *filenames == NULL)
1304 filenames = empty_filenames;
1306 recurse_into_dirs = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1307 partial_names = !!(flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX);
1309 /* if we can construct the exact paths to check with the data we have, just
1310 * stat them one by one; this is more efficient than opening the directory
1311 * and going through each entry to see if it matches one of our filenames. */
1312 if (!recurse_into_dirs && !partial_names) {
1313 for (i = 0; filenames[i] != NULL; ++i) {
1318 full_path = g_build_filename (path, filenames[i], NULL);
1319 if (g_stat (full_path, &s) < 0) {
1320 fhash = (guint) - 1;
1321 GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1322 g_strerror (errno));
1324 fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1325 GST_LOG_OBJECT (plugin, "stat: %s (result: %08x)", full_path, fhash);
1327 hash = (hash + fhash) << 1;
1331 hash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, path,
1332 filenames, flags, 0);
1339 gst_plugin_ext_dep_get_stat_hash (GstPlugin * plugin, GstPluginDep * dep)
1341 gboolean paths_are_default_only;
1343 guint scan_hash = 0;
1345 GST_LOG_OBJECT (plugin, "start");
1347 paths_are_default_only =
1348 dep->flags & GST_PLUGIN_DEPENDENCY_FLAG_PATHS_ARE_DEFAULT_ONLY;
1350 scan_paths = gst_plugin_ext_dep_extract_env_vars_paths (plugin, dep);
1352 if (scan_paths == NULL || !paths_are_default_only) {
1355 for (paths = dep->paths; paths != NULL && *paths != NULL; ++paths) {
1356 const gchar *path = *paths;
1358 if (!g_list_find_custom (scan_paths, path, (GCompareFunc) strcmp)) {
1359 GST_LOG_OBJECT (plugin, "path: '%s'", path);
1360 scan_paths = g_list_prepend (scan_paths, g_strdup (path));
1362 GST_LOG_OBJECT (plugin, "path: '%s' (duplicate, ignoring)", path);
1367 /* not that the order really matters, but it makes debugging easier */
1368 scan_paths = g_list_reverse (scan_paths);
1370 while (scan_paths != NULL) {
1371 const gchar *path = scan_paths->data;
1373 scan_hash += gst_plugin_ext_dep_scan_path_with_filenames (plugin, path,
1374 (const gchar **) dep->names, dep->flags);
1375 scan_hash = scan_hash << 1;
1377 g_free (scan_paths->data);
1378 scan_paths = g_list_delete_link (scan_paths, scan_paths);
1381 GST_LOG_OBJECT (plugin, "done, scan_hash: %08x", scan_hash);
1386 _priv_plugin_deps_files_changed (GstPlugin * plugin)
1390 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1391 GstPluginDep *dep = l->data;
1393 if (dep->stat_hash != gst_plugin_ext_dep_get_stat_hash (plugin, dep))
1401 gst_plugin_ext_dep_free (GstPluginDep * dep)
1403 g_strfreev (dep->env_vars);
1404 g_strfreev (dep->paths);
1405 g_strfreev (dep->names);
1410 gst_plugin_ext_dep_strv_equal (gchar ** arr1, gchar ** arr2)
1414 if (arr1 == NULL || arr2 == NULL)
1416 for (; *arr1 != NULL && *arr2 != NULL; ++arr1, ++arr2) {
1417 if (strcmp (*arr1, *arr2) != 0)
1420 return (*arr1 == *arr2);
1424 gst_plugin_ext_dep_equals (GstPluginDep * dep, const gchar ** env_vars,
1425 const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1427 if (dep->flags != flags)
1430 return gst_plugin_ext_dep_strv_equal (dep->env_vars, (gchar **) env_vars) &&
1431 gst_plugin_ext_dep_strv_equal (dep->paths, (gchar **) paths) &&
1432 gst_plugin_ext_dep_strv_equal (dep->names, (gchar **) names);
1436 * gst_plugin_add_dependency:
1437 * @plugin: a #GstPlugin
1438 * @env_vars: NULL-terminated array of environent variables affecting the
1439 * feature set of the plugin (e.g. an environment variable containing
1440 * paths where to look for additional modules/plugins of a library),
1441 * or NULL. Environment variable names may be followed by a path component
1442 * which will be added to the content of the environment variable, e.g.
1443 * "HOME/.mystuff/plugins".
1444 * @paths: NULL-terminated array of directories/paths where dependent files
1446 * @names: NULL-terminated array of file names (or file name suffixes,
1447 * depending on @flags) to be used in combination with the paths from
1448 * @paths and/or the paths extracted from the environment variables in
1449 * @env_vars, or NULL.
1450 * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1452 * Make GStreamer aware of external dependencies which affect the feature
1453 * set of this plugin (ie. the elements or typefinders associated with it).
1455 * GStreamer will re-inspect plugins with external dependencies whenever any
1456 * of the external dependencies change. This is useful for plugins which wrap
1457 * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1458 * library and makes visualisations available as GStreamer elements, or a
1459 * codec loader which exposes elements and/or caps dependent on what external
1460 * codec libraries are currently installed.
1465 gst_plugin_add_dependency (GstPlugin * plugin, const gchar ** env_vars,
1466 const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1471 g_return_if_fail (GST_IS_PLUGIN (plugin));
1472 g_return_if_fail (env_vars != NULL || paths != NULL);
1474 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1475 if (gst_plugin_ext_dep_equals (l->data, env_vars, paths, names, flags)) {
1476 GST_LOG_OBJECT (plugin, "dependency already registered");
1481 dep = g_new0 (GstPluginDep, 1);
1483 dep->env_vars = g_strdupv ((gchar **) env_vars);
1484 dep->paths = g_strdupv ((gchar **) paths);
1485 dep->names = g_strdupv ((gchar **) names);
1488 dep->env_hash = gst_plugin_ext_dep_get_env_vars_hash (plugin, dep);
1489 dep->stat_hash = gst_plugin_ext_dep_get_stat_hash (plugin, dep);
1491 plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
1493 GST_DEBUG_OBJECT (plugin, "added dependency:");
1494 for (; env_vars != NULL && *env_vars != NULL; ++env_vars)
1495 GST_DEBUG_OBJECT (plugin, " evar: %s", *env_vars);
1496 for (; paths != NULL && *paths != NULL; ++paths)
1497 GST_DEBUG_OBJECT (plugin, " path: %s", *paths);
1498 for (; names != NULL && *names != NULL; ++names)
1499 GST_DEBUG_OBJECT (plugin, " name: %s", *names);
1503 * gst_plugin_add_dependency_simple:
1504 * @plugin: the #GstPlugin
1505 * @env_vars: one or more environent variables (separated by ':', ';' or ','),
1506 * or NULL. Environment variable names may be followed by a path component
1507 * which will be added to the content of the environment variable, e.g.
1508 * "HOME/.mystuff/plugins:MYSTUFF_PLUGINS_PATH"
1509 * @paths: one ore more directory paths (separated by ':' or ';' or ','),
1510 * or NULL. Example: "/usr/lib/mystuff/plugins"
1511 * @names: one or more file names or file name suffixes (separated by commas),
1513 * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1515 * Make GStreamer aware of external dependencies which affect the feature
1516 * set of this plugin (ie. the elements or typefinders associated with it).
1518 * GStreamer will re-inspect plugins with external dependencies whenever any
1519 * of the external dependencies change. This is useful for plugins which wrap
1520 * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1521 * library and makes visualisations available as GStreamer elements, or a
1522 * codec loader which exposes elements and/or caps dependent on what external
1523 * codec libraries are currently installed.
1525 * Convenience wrapper function for gst_plugin_add_dependency() which
1526 * takes simple strings as arguments instead of string arrays, with multiple
1527 * arguments separated by predefined delimiters (see above).
1532 gst_plugin_add_dependency_simple (GstPlugin * plugin,
1533 const gchar * env_vars, const gchar * paths, const gchar * names,
1534 GstPluginDependencyFlags flags)
1536 gchar **a_evars = NULL;
1537 gchar **a_paths = NULL;
1538 gchar **a_names = NULL;
1541 a_evars = g_strsplit_set (env_vars, ":;,", -1);
1543 a_paths = g_strsplit_set (paths, ":;,", -1);
1545 a_names = g_strsplit_set (names, ",", -1);
1547 gst_plugin_add_dependency (plugin, (const gchar **) a_evars,
1548 (const gchar **) a_paths, (const gchar **) a_names, flags);
1551 g_strfreev (a_evars);
1553 g_strfreev (a_paths);
1555 g_strfreev (a_names);