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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
26 * @short_description: Container for features loaded from a shared object module
27 * @see_also: #GstPluginFeature, #GstElementFactory
29 * GStreamer is extensible, so #GstElement instances can be loaded at runtime.
30 * A plugin system can provide one or more of the basic GStreamer
31 * #GstPluginFeature subclasses.
33 * A plugin should export a symbol `gst_plugin_desc` that is a
34 * struct of type #GstPluginDesc.
35 * the plugin loader will check the version of the core library the plugin was
36 * linked against and will create a new #GstPlugin. It will then call the
37 * #GstPluginInitFunc function that was provided in the
40 * Once you have a handle to a #GstPlugin (e.g. from the #GstRegistry), you
41 * can add any object that subclasses #GstPluginFeature.
43 * Usually plugins are always automatically loaded so you don't need to call
44 * gst_plugin_load() explicitly to bring it into memory. There are options to
45 * statically link plugins to an app or even use GStreamer without a plugin
46 * repository in which case gst_plugin_load() can be needed to bring the plugin
54 #include "gst_private.h"
56 #include <glib/gstdio.h>
57 #include <sys/types.h>
68 #include "glib-compat-private.h"
74 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
75 #define GST_WINAPI_ONLY_APP
79 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
81 static guint _num_static_plugins; /* 0 */
82 static GstPluginDesc *_static_plugins; /* NULL */
83 static gboolean _gst_plugin_inited;
84 static gchar **_plugin_loading_whitelist; /* NULL */
86 /* static variables for segfault handling of plugin loading */
87 static char *_gst_plugin_fault_handler_filename = NULL;
89 /* List of known licenses:
90 * GPL: https://opensource.org/licenses/gpl-license
91 * LGPL: https://opensource.org/licenses/lgpl-license
92 * QPL: https://opensource.org/licenses/QPL-1.0
93 * MPL: https://opensource.org/licenses/MPL-1.1
94 * MPL-2.0: https://opensource.org/licenses/MPL-2.0
95 * MIT/X11: https://opensource.org/licenses/MIT
96 * 3-clause BSD: https://opensource.org/licenses/BSD-3-Clause
97 * Zero-Clause BSD: https://opensource.org/licenses/0BSD
98 * FIXME: update to use SPDX identifiers, or just remove entirely
100 static const gchar known_licenses[] = "LGPL\000" /* GNU Lesser General Public License */
101 "GPL\000" /* GNU General Public License */
102 "QPL\000" /* Trolltech Qt Public License */
103 "GPL/QPL\000" /* Combi-license of GPL + QPL */
104 "MPL\000" /* MPL 1.1 license */
105 "MPL-2.0\000" /* MPL 2.0 license */
106 "BSD\000" /* 3-clause BSD license */
107 "MIT/X11\000" /* MIT/X11 license */
108 "0BSD\000" /* Zero-Clause BSD */
109 "Proprietary\000" /* Proprietary license */
110 GST_LICENSE_UNKNOWN; /* some other license */
112 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
113 const GstPluginDesc * desc, gpointer user_data);
114 static void gst_plugin_desc_copy (GstPluginDesc * dest,
115 const GstPluginDesc * src);
117 static void gst_plugin_ext_dep_free (GstPluginDep * dep);
119 G_DEFINE_TYPE_WITH_PRIVATE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
122 gst_plugin_init (GstPlugin * plugin)
124 plugin->priv = gst_plugin_get_instance_private (plugin);
128 gst_plugin_finalize (GObject * object)
130 GstPlugin *plugin = GST_PLUGIN_CAST (object);
132 GST_DEBUG ("finalizing plugin %" GST_PTR_FORMAT, plugin);
134 /* FIXME: make registry add a weak ref instead */
136 GstRegistry *registry = gst_registry_get ();
138 for (g = registry->plugins; g; g = g->next) {
139 if (g->data == (gpointer) plugin) {
140 g_warning ("removing plugin that is still in registry");
145 g_free (plugin->filename);
146 g_free (plugin->basename);
148 g_list_foreach (plugin->priv->deps, (GFunc) gst_plugin_ext_dep_free, NULL);
149 g_list_free (plugin->priv->deps);
150 plugin->priv->deps = NULL;
152 if (plugin->priv->cache_data) {
153 gst_structure_free (plugin->priv->cache_data);
156 G_OBJECT_CLASS (gst_plugin_parent_class)->finalize (object);
160 gst_plugin_class_init (GstPluginClass * klass)
162 G_OBJECT_CLASS (klass)->finalize = gst_plugin_finalize;
166 gst_plugin_error_quark (void)
168 static GQuark quark = 0;
171 quark = g_quark_from_static_string ("gst_plugin_error");
176 * gst_plugin_register_static:
177 * @major_version: the major version number of the GStreamer core that the
178 * plugin was compiled for, you can just use GST_VERSION_MAJOR here
179 * @minor_version: the minor version number of the GStreamer core that the
180 * plugin was compiled for, you can just use GST_VERSION_MINOR here
181 * @name: a unique name of the plugin (ideally prefixed with an application- or
182 * library-specific namespace prefix in order to avoid name conflicts in
183 * case a similar plugin with the same name ever gets added to GStreamer)
184 * @description: description of the plugin
185 * @init_func: (scope call): pointer to the init function of this plugin.
186 * @version: version string of the plugin
187 * @license: effective license of plugin. Must be one of the approved licenses
188 * (see #GstPluginDesc above) or the plugin will not be registered.
189 * @source: source module plugin belongs to
190 * @package: shipped package plugin belongs to
191 * @origin: URL to provider of plugin
193 * Registers a static plugin, ie. a plugin which is private to an application
194 * or library and contained within the application or library (as opposed to
195 * being shipped as a separate module file).
197 * You must make sure that GStreamer has been initialised (with gst_init() or
198 * via gst_init_get_option_group()) before calling this function.
200 * Returns: %TRUE if the plugin was registered correctly, otherwise %FALSE.
203 gst_plugin_register_static (gint major_version, gint minor_version,
204 const gchar * name, const gchar * description, GstPluginInitFunc init_func,
205 const gchar * version, const gchar * license, const gchar * source,
206 const gchar * package, const gchar * origin)
208 GstPluginDesc desc = { major_version, minor_version, name, description,
209 init_func, version, license, source, package, origin, NULL,
212 gboolean res = FALSE;
214 g_return_val_if_fail (name != NULL, FALSE);
215 g_return_val_if_fail (description != NULL, FALSE);
216 g_return_val_if_fail (init_func != NULL, FALSE);
217 g_return_val_if_fail (version != NULL, FALSE);
218 g_return_val_if_fail (license != NULL, FALSE);
219 g_return_val_if_fail (source != NULL, FALSE);
220 g_return_val_if_fail (package != NULL, FALSE);
221 g_return_val_if_fail (origin != NULL, FALSE);
223 /* make sure gst_init() has been called */
224 g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);
226 GST_LOG ("attempting to load static plugin \"%s\" now...", name);
227 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
228 if (gst_plugin_register_func (plugin, &desc, NULL) != NULL) {
229 GST_INFO ("registered static plugin \"%s\"", name);
230 res = gst_registry_add_plugin (gst_registry_get (), plugin);
231 GST_INFO ("added static plugin \"%s\", result: %d", name, res);
237 * gst_plugin_register_static_full:
238 * @major_version: the major version number of the GStreamer core that the
239 * plugin was compiled for, you can just use GST_VERSION_MAJOR here
240 * @minor_version: the minor version number of the GStreamer core that the
241 * plugin was compiled for, you can just use GST_VERSION_MINOR here
242 * @name: a unique name of the plugin (ideally prefixed with an application- or
243 * library-specific namespace prefix in order to avoid name conflicts in
244 * case a similar plugin with the same name ever gets added to GStreamer)
245 * @description: description of the plugin
246 * @init_full_func: (scope call): pointer to the init function with user data
248 * @version: version string of the plugin
249 * @license: effective license of plugin. Must be one of the approved licenses
250 * (see #GstPluginDesc above) or the plugin will not be registered.
251 * @source: source module plugin belongs to
252 * @package: shipped package plugin belongs to
253 * @origin: URL to provider of plugin
254 * @user_data: gpointer to user data
256 * Registers a static plugin, ie. a plugin which is private to an application
257 * or library and contained within the application or library (as opposed to
258 * being shipped as a separate module file) with a #GstPluginInitFullFunc
259 * which allows user data to be passed to the callback function (useful
262 * You must make sure that GStreamer has been initialised (with gst_init() or
263 * via gst_init_get_option_group()) before calling this function.
265 * Returns: %TRUE if the plugin was registered correctly, otherwise %FALSE.
268 gst_plugin_register_static_full (gint major_version, gint minor_version,
269 const gchar * name, const gchar * description,
270 GstPluginInitFullFunc init_full_func, const gchar * version,
271 const gchar * license, const gchar * source, const gchar * package,
272 const gchar * origin, gpointer user_data)
274 GstPluginDesc desc = { major_version, minor_version, name, description,
275 (GstPluginInitFunc) init_full_func, version, license, source, package,
279 gboolean res = FALSE;
281 g_return_val_if_fail (name != NULL, FALSE);
282 g_return_val_if_fail (description != NULL, FALSE);
283 g_return_val_if_fail (init_full_func != NULL, FALSE);
284 g_return_val_if_fail (version != NULL, FALSE);
285 g_return_val_if_fail (license != NULL, FALSE);
286 g_return_val_if_fail (source != NULL, FALSE);
287 g_return_val_if_fail (package != NULL, FALSE);
288 g_return_val_if_fail (origin != NULL, FALSE);
290 /* make sure gst_init() has been called */
291 g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);
293 GST_LOG ("attempting to load static plugin \"%s\" now...", name);
294 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
295 if (gst_plugin_register_func (plugin, &desc, user_data) != NULL) {
296 GST_INFO ("registered static plugin \"%s\"", name);
297 res = gst_registry_add_plugin (gst_registry_get (), plugin);
298 GST_INFO ("added static plugin \"%s\", result: %d", name, res);
304 _priv_gst_plugin_initialize (void)
306 const gchar *whitelist;
309 _gst_plugin_inited = TRUE;
311 whitelist = g_getenv ("GST_PLUGIN_LOADING_WHITELIST");
312 if (whitelist != NULL && *whitelist != '\0') {
313 _plugin_loading_whitelist = g_strsplit (whitelist,
314 G_SEARCHPATH_SEPARATOR_S, -1);
315 for (i = 0; _plugin_loading_whitelist[i] != NULL; ++i) {
316 GST_INFO ("plugins whitelist entry: %s", _plugin_loading_whitelist[i]);
320 /* now register all static plugins */
321 GST_INFO ("registering %u static plugins", _num_static_plugins);
322 for (i = 0; i < _num_static_plugins; ++i) {
323 gst_plugin_register_static (_static_plugins[i].major_version,
324 _static_plugins[i].minor_version, _static_plugins[i].name,
325 _static_plugins[i].description, _static_plugins[i].plugin_init,
326 _static_plugins[i].version, _static_plugins[i].license,
327 _static_plugins[i].source, _static_plugins[i].package,
328 _static_plugins[i].origin);
331 if (_static_plugins) {
332 free (_static_plugins);
333 _static_plugins = NULL;
334 _num_static_plugins = 0;
338 /* Whitelist entry format:
340 * plugin1,plugin2@pathprefix or
341 * plugin1,plugin2@* or just
343 * source-package@pathprefix or
344 * source-package@* or just
347 * ie. the bit before the path will be checked against both the plugin
348 * name and the plugin's source package name, to keep the format simple.
351 gst_plugin_desc_matches_whitelist_entry (const GstPluginDesc * desc,
352 const gchar * filename, const gchar * pattern)
355 gboolean ret = FALSE;
358 GST_LOG ("Whitelist pattern '%s', plugin: %s of %s@%s", pattern, desc->name,
359 desc->source, GST_STR_NULL (filename));
361 /* do we have a path prefix? */
362 sep = strchr (pattern, '@');
363 if (sep != NULL && strcmp (sep, "@*") != 0 && strcmp (sep, "@") != 0) {
364 /* paths are not canonicalised or treated with realpath() here. This
365 * should be good enough for our use case, since we just use the paths
366 * autotools uses, and those will be constructed from the same prefix. */
367 if (filename != NULL && !g_str_has_prefix (filename, sep + 1))
370 GST_LOG ("%s matches path prefix %s", GST_STR_NULL (filename), sep + 1);
374 name = g_strndup (pattern, (gsize) (sep - pattern));
376 name = g_strdup (pattern);
380 if (!g_ascii_isalnum (*name)) {
381 GST_WARNING ("Invalid whitelist pattern: %s", pattern);
385 /* now check plugin names / source package name */
386 if (strchr (name, ',') == NULL) {
387 /* only a single name: either a plugin name or the source package name */
388 ret = (strcmp (desc->source, name) == 0 || strcmp (desc->name, name) == 0);
392 /* multiple names: assume these are plugin names */
393 names = g_strsplit (name, ",", -1);
394 for (n = names; n != NULL && *n != NULL; ++n) {
396 if (strcmp (desc->name, *n) == 0) {
404 GST_LOG ("plugin / source package name match: %d", ret);
413 priv_gst_plugin_desc_is_whitelisted (const GstPluginDesc * desc,
414 const gchar * filename)
418 if (_plugin_loading_whitelist == NULL)
421 for (entry = _plugin_loading_whitelist; *entry != NULL; ++entry) {
422 if (gst_plugin_desc_matches_whitelist_entry (desc, filename, *entry)) {
423 GST_LOG ("Plugin %s is in whitelist", filename);
428 GST_LOG ("Plugin %s (package %s, file %s) not in whitelist", desc->name,
429 desc->source, filename);
434 priv_gst_plugin_loading_have_whitelist (void)
436 return (_plugin_loading_whitelist != NULL);
440 priv_gst_plugin_loading_get_whitelist_hash (void)
444 if (_plugin_loading_whitelist != NULL) {
447 for (w = _plugin_loading_whitelist; *w != NULL; ++w)
448 hash ^= g_str_hash (*w);
454 /* this function could be extended to check if the plugin license matches the
455 * applications license (would require the app to register its license somehow).
456 * We'll wait for someone who's interested in it to code it :)
459 gst_plugin_check_license (const gchar * license)
461 const gchar *l, *end = known_licenses + sizeof (known_licenses);
463 for (l = known_licenses; l < end; l += strlen (l) + 1) {
464 if (strcmp (license, l) == 0)
472 gst_plugin_check_version (gint major, gint minor)
474 /* return NULL if the major and minor version numbers are not compatible */
476 if (major != GST_VERSION_MAJOR || minor > GST_VERSION_MINOR)
483 gst_plugin_register_func (GstPlugin * plugin, const GstPluginDesc * desc,
486 if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
488 GST_WARNING ("plugin \"%s\" has incompatible version "
489 "(plugin: %d.%d, gst: %d.%d), not loading",
490 GST_STR_NULL (plugin->filename), desc->major_version,
491 desc->minor_version, GST_VERSION_MAJOR, GST_VERSION_MINOR);
495 if (!desc->license || !desc->description || !desc->source ||
496 !desc->package || !desc->origin) {
498 GST_WARNING ("plugin \"%s\" has missing detail in GstPluginDesc, not "
499 "loading", GST_STR_NULL (plugin->filename));
503 if (!gst_plugin_check_license (desc->license)) {
505 GST_WARNING ("plugin \"%s\" has unknown license \"%s\"",
506 GST_STR_NULL (plugin->filename), desc->license);
507 /* We still want to load the plugin, it's not our job to validate licenses */
511 GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
513 gst_plugin_desc_copy (&plugin->desc, desc);
515 /* make resident so we're really sure it never gets unloaded again.
516 * Theoretically this is not needed, but practically it doesn't hurt.
517 * And we're rather safe than sorry. */
519 g_module_make_resident (plugin->module);
522 if (!(((GstPluginInitFullFunc) (desc->plugin_init)) (plugin, user_data))) {
524 GST_WARNING ("plugin \"%s\" failed to initialise",
525 GST_STR_NULL (plugin->filename));
529 if (!((desc->plugin_init) (plugin))) {
531 GST_WARNING ("plugin \"%s\" failed to initialise",
532 GST_STR_NULL (plugin->filename));
538 GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
543 #ifdef HAVE_SIGACTION
544 static struct sigaction oldaction;
545 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
548 * _gst_plugin_fault_handler_restore:
549 * segfault handler restorer
552 _gst_plugin_fault_handler_restore (void)
554 if (!_gst_plugin_fault_handler_is_setup)
557 _gst_plugin_fault_handler_is_setup = FALSE;
559 sigaction (SIGSEGV, &oldaction, NULL);
563 * _gst_plugin_fault_handler_sighandler:
564 * segfault handler implementation
567 _gst_plugin_fault_handler_sighandler (int signum)
569 /* We need to restore the fault handler or we'll keep getting it */
570 _gst_plugin_fault_handler_restore ();
574 g_print ("\nERROR: ");
575 g_print ("Caught a segmentation fault while loading plugin file:\n");
576 g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
577 g_print ("Please either:\n");
578 g_print ("- remove it and restart.\n");
580 ("- run with --gst-disable-segtrap --gst-disable-registry-fork and debug.\n");
584 g_print ("Caught unhandled signal on plugin loading\n");
590 * _gst_plugin_fault_handler_setup:
591 * sets up the segfault handler
594 _gst_plugin_fault_handler_setup (void)
596 struct sigaction action;
598 /* if asked to leave segfaults alone, just return */
599 if (!gst_segtrap_is_enabled ())
602 if (_gst_plugin_fault_handler_is_setup)
605 _gst_plugin_fault_handler_is_setup = TRUE;
607 memset (&action, 0, sizeof (action));
608 action.sa_handler = _gst_plugin_fault_handler_sighandler;
610 sigaction (SIGSEGV, &action, &oldaction);
612 #else /* !HAVE_SIGACTION */
614 _gst_plugin_fault_handler_restore (void)
619 _gst_plugin_fault_handler_setup (void)
622 #endif /* HAVE_SIGACTION */
624 /* g_time_val_from_iso8601() doesn't do quite what we want */
626 check_release_datetime (const gchar * date_time)
630 /* we require YYYY-MM-DD or YYYY-MM-DDTHH:MMZ format */
631 if (!g_ascii_isdigit (*date_time))
634 val = g_ascii_strtoull (date_time, (gchar **) & date_time, 10);
635 if (val < 2000 || val > 2100 || *date_time != '-')
638 val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
639 if (val == 0 || val > 12 || *date_time != '-')
642 val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
643 if (val == 0 || val > 32)
646 /* end of string or date/time separator + HH:MMZ */
647 if (*date_time == 'T' || *date_time == ' ') {
648 val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
649 if (val > 24 || *date_time != ':')
652 val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
653 if (val > 59 || *date_time != 'Z')
659 return (*date_time == '\0');
662 static GMutex gst_plugin_loading_mutex;
664 #define CHECK_PLUGIN_DESC_FIELD(desc,field,fn) \
665 if (G_UNLIKELY ((desc)->field == NULL || *(desc)->field == '\0')) { \
666 g_warning ("Plugin description for '%s' has no valid %s field", fn, G_STRINGIFY (field)); \
667 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, \
668 "Plugin %s has invalid plugin description field '%s'", \
669 filename, G_STRINGIFY (field)); \
674 * gst_plugin_load_file:
675 * @filename: (type filename): the plugin filename to load
676 * @error: pointer to a %NULL-valued GError
678 * Loads the given plugin and refs it. Caller needs to unref after use.
680 * Returns: (transfer full): a reference to the existing loaded GstPlugin, a
681 * reference to the newly-loaded GstPlugin, or %NULL if an error occurred.
684 gst_plugin_load_file (const gchar * filename, GError ** error)
686 return _priv_gst_plugin_load_file_for_registry (filename, NULL, error);
690 extract_symname (const char *filename)
692 gchar *bname, *name, *symname;
694 gsize prefix_len, len;
697 bname = g_path_get_basename (filename);
698 for (i = 0; bname[i]; ++i) {
703 if (g_str_has_prefix (bname, "libgst"))
705 else if (g_str_has_prefix (bname, "lib"))
707 else if (g_str_has_prefix (bname, "gst"))
710 prefix_len = 0; /* use whole name (minus suffix) as plugin name */
712 dot = g_utf8_strchr (bname, -1, '.');
714 len = dot - bname - prefix_len;
716 len = strlen (bname + prefix_len);
718 name = g_strndup (bname + prefix_len, len);
721 symname = g_strconcat ("gst_plugin_", name, "_get_desc", NULL);
729 * It is an extremely common mistake on Windows to have incorrect PATH values
730 * when loading a plugin, and the error message is very confusing in this case:
731 * 'The specified module could not be found.' which implies the plugin itself
732 * could not be found. The actual issue is that a DLL dependency could not be
733 * found. We need to detect this case and print a more useful error message.
735 * Unfortunately, g_module_open() doesn't actually give us the GetLastError()
736 * code from LoadLibraryW() and only gives us a literal message from
737 * FormatMessageW(). We can't do a string comparison on that because it is
740 * The only way out is for us to try loading the module ourselves on failure and
741 * get the error DWORD again from GetLastError().
744 get_better_module_load_error (const char *filename, const char *orig_err_msg)
750 char *err_msg = NULL;
752 wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
753 #ifdef GST_WINAPI_ONLY_APP
754 handle = LoadPackagedLibrary (wfilename, 0);
756 ret = SetThreadErrorMode (SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS,
759 handle = LoadLibraryW (wfilename);
763 if (handle == NULL) {
764 DWORD err = GetLastError ();
765 char *win32_err_msg = g_win32_error_message (err);
766 if (err == ERROR_MOD_NOT_FOUND) {
767 err_msg = g_strdup_printf ("%s\nThis usually means Windows was unable "
768 "to find a DLL dependency of the plugin. Please check that PATH is "
769 "correct.\nYou can run 'dumpbin -dependents' (provided by the "
770 "Visual Studio developer prompt) to list the DLL deps of any DLL.\n"
771 "There are also some third-party GUIs to list and debug DLL "
772 "dependencies recursively.", win32_err_msg);
773 g_free (win32_err_msg);
775 err_msg = win32_err_msg;
778 err_msg = g_strdup_printf ("g_module_open() failed on %s with \"%s\" but "
779 "manual loading succeeded; this should be impossible! Please "
780 "report this as a GStreamer bug.", filename, orig_err_msg);
781 FreeLibrary (handle);
785 SetThreadErrorMode (mode, NULL);
789 #endif /* G_OS_WIN32 */
791 /* Note: The return value is (transfer full) although we work with floating
792 * references here. If a new plugin instance is created, it is always sinked
793 * in the registry first and a new reference is returned
796 _priv_gst_plugin_load_file_for_registry (const gchar * filename,
797 GstRegistry * registry, GError ** error)
799 const GstPluginDesc *desc;
805 GStatBuf file_status;
806 gboolean new_plugin = TRUE;
809 g_return_val_if_fail (filename != NULL, NULL);
811 if (registry == NULL)
812 registry = gst_registry_get ();
814 g_mutex_lock (&gst_plugin_loading_mutex);
816 plugin = gst_registry_lookup (registry, filename);
818 if (plugin->module) {
820 g_mutex_unlock (&gst_plugin_loading_mutex);
822 } else if (g_strcmp0 (plugin->filename, filename) == 0) {
823 /* load plugin and update fields */
828 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
831 if (!g_module_supported ()) {
832 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
835 GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
838 #if defined(GST_WINAPI_ONLY_APP)
839 /* plugins loaded by filename by Universal Windows Platform apps do not use
840 * an actual file with a path, they use a packaged (asset) library */
841 file_status.st_mtime = 0;
842 file_status.st_size = 0;
844 if (g_stat (filename, &file_status)) {
845 GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
848 GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
854 flags = G_MODULE_BIND_LOCAL;
855 /* libgstpython.so is the gst-python plugin loader. It needs to be loaded with
856 * G_MODULE_BIND_LAZY.
858 * Ideally there should be a generic way for plugins to specify that they
859 * need to be loaded with _LAZY.
861 if (strstr (filename, "libgstpython"))
862 flags |= G_MODULE_BIND_LAZY;
864 module = g_module_open (filename, flags);
865 if (module == NULL) {
867 /* flags are meaningless / ignored on Windows */
868 char *err_msg = get_better_module_load_error (filename, g_module_error ());
870 const char *err_msg = g_module_error ();
872 GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s", err_msg);
874 GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
876 /* If we failed to open the shared object, then it's probably because a
877 * plugin is linked against the wrong libraries. Print out an easy-to-see
878 * message in this case. */
879 g_warning ("Failed to load plugin '%s': %s", filename, err_msg);
886 symname = extract_symname (filename);
887 ret = g_module_symbol (module, symname, &ptr);
890 GstPluginDesc *(*get_desc) (void) = ptr;
893 GST_DEBUG ("Could not find symbol '%s', falling back to gst_plugin_desc",
895 ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
901 GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
904 GST_PLUGIN_ERROR_MODULE,
905 "File \"%s\" is not a GStreamer plugin", filename);
906 g_module_close (module);
910 desc = (const GstPluginDesc *) ptr;
912 if (priv_gst_plugin_loading_have_whitelist () &&
913 !priv_gst_plugin_desc_is_whitelisted (desc, filename)) {
914 GST_INFO ("Whitelist specified and plugin not in whitelist, not loading: "
915 "name=%s, package=%s, file=%s", desc->name, desc->source, filename);
916 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
917 "Not loading plugin file \"%s\", not in whitelist", filename);
918 g_module_close (module);
923 plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
924 plugin->file_mtime = file_status.st_mtime;
925 plugin->file_size = file_status.st_size;
926 plugin->filename = g_strdup (filename);
927 plugin->basename = g_path_get_basename (filename);
930 plugin->module = module;
933 /* check plugin description: complain about bad values and fail */
934 CHECK_PLUGIN_DESC_FIELD (desc, name, filename);
935 CHECK_PLUGIN_DESC_FIELD (desc, description, filename);
936 CHECK_PLUGIN_DESC_FIELD (desc, version, filename);
937 CHECK_PLUGIN_DESC_FIELD (desc, license, filename);
938 CHECK_PLUGIN_DESC_FIELD (desc, source, filename);
939 CHECK_PLUGIN_DESC_FIELD (desc, package, filename);
940 CHECK_PLUGIN_DESC_FIELD (desc, origin, filename);
942 if (desc->name != NULL && desc->name[0] == '"') {
943 g_warning ("Invalid plugin name '%s' - fix your GST_PLUGIN_DEFINE "
944 "(remove quotes around plugin name)", desc->name);
947 if (desc->release_datetime != NULL &&
948 !check_release_datetime (desc->release_datetime)) {
949 g_warning ("GstPluginDesc for '%s' has invalid datetime '%s'",
950 filename, desc->release_datetime);
951 g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
952 "Plugin %s has invalid plugin description field 'release_datetime'",
958 GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
961 /* this is where we load the actual .so, so let's trap SIGSEGV */
962 _gst_plugin_fault_handler_setup ();
963 _gst_plugin_fault_handler_filename = plugin->filename;
965 GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
968 if (!gst_plugin_register_func (plugin, desc, NULL)) {
969 /* remove signal handler */
970 _gst_plugin_fault_handler_restore ();
971 GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
975 GST_PLUGIN_ERROR_MODULE,
976 "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
981 /* remove signal handler */
982 _gst_plugin_fault_handler_restore ();
983 _gst_plugin_fault_handler_filename = NULL;
984 GST_INFO ("plugin \"%s\" loaded", plugin->filename);
987 gst_object_ref (plugin);
988 gst_registry_add_plugin (registry, plugin);
991 g_mutex_unlock (&gst_plugin_loading_mutex);
997 gst_object_unref (plugin);
998 g_mutex_unlock (&gst_plugin_loading_mutex);
1004 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
1006 dest->major_version = src->major_version;
1007 dest->minor_version = src->minor_version;
1008 dest->name = g_intern_string (src->name);
1009 dest->description = g_intern_string (src->description);
1010 dest->plugin_init = src->plugin_init;
1011 dest->version = g_intern_string (src->version);
1012 dest->license = g_intern_string (src->license);
1013 dest->source = g_intern_string (src->source);
1014 dest->package = g_intern_string (src->package);
1015 dest->origin = g_intern_string (src->origin);
1016 dest->release_datetime = g_intern_string (src->release_datetime);
1020 * gst_plugin_get_name:
1021 * @plugin: plugin to get the name of
1023 * Get the short name of the plugin
1025 * Returns: the name of the plugin
1028 gst_plugin_get_name (GstPlugin * plugin)
1030 g_return_val_if_fail (plugin != NULL, NULL);
1032 return plugin->desc.name;
1036 * gst_plugin_get_description:
1037 * @plugin: plugin to get long name of
1039 * Get the long descriptive name of the plugin
1041 * Returns: the long name of the plugin
1044 gst_plugin_get_description (GstPlugin * plugin)
1046 g_return_val_if_fail (plugin != NULL, NULL);
1048 return plugin->desc.description;
1052 * gst_plugin_get_filename:
1053 * @plugin: plugin to get the filename of
1055 * get the filename of the plugin
1057 * Returns: (type filename) (nullable): the filename of the plugin
1060 gst_plugin_get_filename (GstPlugin * plugin)
1062 g_return_val_if_fail (plugin != NULL, NULL);
1064 return plugin->filename;
1068 * gst_plugin_get_version:
1069 * @plugin: plugin to get the version of
1071 * get the version of the plugin
1073 * Returns: the version of the plugin
1076 gst_plugin_get_version (GstPlugin * plugin)
1078 g_return_val_if_fail (plugin != NULL, NULL);
1080 return plugin->desc.version;
1084 * gst_plugin_get_license:
1085 * @plugin: plugin to get the license of
1087 * get the license of the plugin
1089 * Returns: the license of the plugin
1092 gst_plugin_get_license (GstPlugin * plugin)
1094 g_return_val_if_fail (plugin != NULL, NULL);
1096 return plugin->desc.license;
1100 * gst_plugin_get_source:
1101 * @plugin: plugin to get the source of
1103 * get the source module the plugin belongs to.
1105 * Returns: the source of the plugin
1108 gst_plugin_get_source (GstPlugin * plugin)
1110 g_return_val_if_fail (plugin != NULL, NULL);
1112 return plugin->desc.source;
1116 * gst_plugin_get_package:
1117 * @plugin: plugin to get the package of
1119 * get the package the plugin belongs to.
1121 * Returns: the package of the plugin
1124 gst_plugin_get_package (GstPlugin * plugin)
1126 g_return_val_if_fail (plugin != NULL, NULL);
1128 return plugin->desc.package;
1132 * gst_plugin_get_origin:
1133 * @plugin: plugin to get the origin of
1135 * get the URL where the plugin comes from
1137 * Returns: the origin of the plugin
1140 gst_plugin_get_origin (GstPlugin * plugin)
1142 g_return_val_if_fail (plugin != NULL, NULL);
1144 return plugin->desc.origin;
1148 * gst_plugin_get_release_date_string:
1149 * @plugin: plugin to get the release date of
1151 * Get the release date (and possibly time) in form of a string, if available.
1153 * For normal GStreamer plugin releases this will usually just be a date in
1154 * the form of "YYYY-MM-DD", while pre-releases and builds from git may contain
1155 * a time component after the date as well, in which case the string will be
1156 * formatted like "YYYY-MM-DDTHH:MMZ" (e.g. "2012-04-30T09:30Z").
1158 * There may be plugins that do not have a valid release date set on them.
1160 * Returns: (nullable): the date string of the plugin, or %NULL if not
1164 gst_plugin_get_release_date_string (GstPlugin * plugin)
1166 g_return_val_if_fail (plugin != NULL, NULL);
1168 return plugin->desc.release_datetime;
1172 * gst_plugin_is_loaded:
1173 * @plugin: plugin to query
1175 * queries if the plugin is loaded into memory
1177 * Returns: %TRUE is loaded, %FALSE otherwise
1180 gst_plugin_is_loaded (GstPlugin * plugin)
1182 g_return_val_if_fail (plugin != NULL, FALSE);
1184 return (plugin->module != NULL || plugin->filename == NULL);
1188 * gst_plugin_get_cache_data:
1191 * Gets the plugin specific data cache. If it is %NULL there is no cached data
1192 * stored. This is the case when the registry is getting rebuilt.
1194 * Returns: (transfer none) (nullable): The cached data as a
1195 * #GstStructure or %NULL.
1197 const GstStructure *
1198 gst_plugin_get_cache_data (GstPlugin * plugin)
1200 g_return_val_if_fail (GST_IS_PLUGIN (plugin), NULL);
1202 return plugin->priv->cache_data;
1206 * gst_plugin_set_cache_data:
1208 * @cache_data: (transfer full): a structure containing the data to cache
1210 * Adds plugin specific data to cache. Passes the ownership of the structure to
1213 * The cache is flushed every time the registry is rebuilt.
1216 gst_plugin_set_cache_data (GstPlugin * plugin, GstStructure * cache_data)
1218 g_return_if_fail (GST_IS_PLUGIN (plugin));
1219 g_return_if_fail (GST_IS_STRUCTURE (cache_data));
1221 if (plugin->priv->cache_data) {
1222 gst_structure_free (plugin->priv->cache_data);
1224 plugin->priv->cache_data = cache_data;
1229 * gst_plugin_feature_list:
1230 * @plugin: plugin to query
1231 * @filter: the filter to use
1232 * @first: only return first match
1233 * @user_data: user data passed to the filter function
1235 * Runs a filter against all plugin features and returns a GList with
1236 * the results. If the first flag is set, only the first match is
1237 * returned (as a list with a single object).
1239 * Returns: a GList of features, g_list_free after use.
1242 gst_plugin_feature_filter (GstPlugin * plugin,
1243 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
1248 list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
1250 for (g = list; g; g = g->next) {
1251 gst_object_ref (plugin);
1259 GstPluginFeatureFilter filter;
1267 _feature_filter (GstPlugin * plugin, gpointer user_data)
1270 FeatureFilterData *data = (FeatureFilterData *) user_data;
1272 result = gst_plugin_feature_filter (plugin, data->filter, data->first,
1275 data->result = g_list_concat (data->result, result);
1282 * gst_plugin_list_feature_filter:
1283 * @list: a #GList of plugins to query
1284 * @filter: the filter function to use
1285 * @first: only return first match
1286 * @user_data: user data passed to the filter function
1288 * Runs a filter against all plugin features of the plugins in the given
1289 * list and returns a GList with the results.
1290 * If the first flag is set, only the first match is
1291 * returned (as a list with a single object).
1293 * Returns: a GList of features, g_list_free after use.
1296 gst_plugin_list_feature_filter (GList * list,
1297 GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
1299 FeatureFilterData data;
1302 data.filter = filter;
1304 data.user_data = user_data;
1307 result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
1308 g_list_free (result);
1314 * gst_plugin_find_feature:
1315 * @plugin: plugin to get the feature from
1316 * @name: The name of the feature to find
1317 * @type: The type of the feature to find
1319 * Find a feature of the given name and type in the given plugin.
1321 * Returns: a GstPluginFeature or %NULL if the feature was not found.
1324 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
1327 GstPluginFeature *result = NULL;
1328 GstTypeNameData data;
1330 g_return_val_if_fail (name != NULL, NULL);
1335 walk = gst_filter_run (plugin->features,
1336 (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
1339 result = GST_PLUGIN_FEATURE (walk->data);
1341 gst_object_ref (result);
1342 gst_plugin_feature_list_free (walk);
1351 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
1353 return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
1359 * gst_plugin_find_feature_by_name:
1360 * @plugin: plugin to get the feature from
1361 * @name: The name of the feature to find
1363 * Find a feature of the given name in the given plugin.
1365 * Returns: a GstPluginFeature or %NULL if the feature was not found.
1368 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
1371 GstPluginFeature *result = NULL;
1373 g_return_val_if_fail (name != NULL, NULL);
1375 walk = gst_filter_run (plugin->features,
1376 (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
1379 result = GST_PLUGIN_FEATURE (walk->data);
1381 gst_object_ref (result);
1382 gst_plugin_feature_list_free (walk);
1390 * gst_plugin_load_by_name:
1391 * @name: name of plugin to load
1393 * Load the named plugin. Refs the plugin.
1395 * Returns: (transfer full) (nullable): a reference to a loaded plugin, or
1399 gst_plugin_load_by_name (const gchar * name)
1401 GstPlugin *plugin, *newplugin;
1402 GError *error = NULL;
1404 GST_DEBUG ("looking up plugin %s in default registry", name);
1405 plugin = gst_registry_find_plugin (gst_registry_get (), name);
1407 GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
1408 newplugin = gst_plugin_load_file (plugin->filename, &error);
1409 gst_object_unref (plugin);
1412 GST_WARNING ("load_plugin error: %s", error->message);
1413 g_error_free (error);
1416 /* newplugin was reffed by load_file */
1420 GST_DEBUG ("Could not find plugin %s in registry", name);
1426 * @plugin: (transfer none): plugin to load
1428 * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
1429 * untouched. The normal use pattern of this function goes like this:
1432 * GstPlugin *loaded_plugin;
1433 * loaded_plugin = gst_plugin_load (plugin);
1434 * // presumably, we're no longer interested in the potentially-unloaded plugin
1435 * gst_object_unref (plugin);
1436 * plugin = loaded_plugin;
1439 * Returns: (transfer full) (nullable): a reference to a loaded plugin, or
1443 gst_plugin_load (GstPlugin * plugin)
1445 GError *error = NULL;
1446 GstPlugin *newplugin;
1448 if (gst_plugin_is_loaded (plugin)) {
1449 return gst_object_ref (plugin);
1452 if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
1459 GST_WARNING ("load_plugin error: %s", error->message);
1460 g_error_free (error);
1466 * gst_plugin_list_free:
1467 * @list: (transfer full) (element-type Gst.Plugin): list of #GstPlugin
1469 * Unrefs each member of @list, then frees the list.
1472 gst_plugin_list_free (GList * list)
1476 for (g = list; g; g = g->next) {
1477 gst_object_unref (GST_PLUGIN_CAST (g->data));
1482 /* ===== plugin dependencies ===== */
1485 * ENV + xyz where ENV can contain multiple values separated by SEPARATOR
1486 * xyz may be "" (if ENV contains path to file rather than dir)
1487 * ENV + *xyz same as above, but xyz acts as suffix filter
1488 * ENV + xyz* same as above, but xyz acts as prefix filter (is this needed?)
1489 * ENV + *xyz* same as above, but xyz acts as strstr filter (is this needed?)
1491 * same as above, with additional paths hard-coded at compile-time:
1492 * - only check paths + ... if ENV is not set or yields not paths
1493 * - always check paths + ... in addition to ENV
1495 * When user specifies set of environment variables, he/she may also use e.g.
1496 * "HOME/.mystuff/plugins", and we'll expand the content of $HOME with the
1500 /* we store in registry:
1503 * - environment variables (array of strings)
1504 * - last hash of env variable contents (uint) (so we can avoid doing stats
1505 * if one of the env vars has changed; premature optimisation galore)
1506 * - hard-coded paths (array of strings)
1507 * - xyz filename/suffix/prefix strings (array of strings)
1509 * - last hash of file/dir stats (int)
1511 * (= struct GstPluginDep)
1515 gst_plugin_ext_dep_get_env_vars_hash (GstPlugin * plugin, GstPluginDep * dep)
1520 /* there's no deeper logic to what we do here; all we want to know (when
1521 * checking if the plugin needs to be rescanned) is whether the content of
1522 * one of the environment variables in the list is different from when it
1523 * was last scanned */
1525 for (e = dep->env_vars; e != NULL && *e != NULL; ++e) {
1529 /* order matters: "val",NULL needs to yield a different hash than
1530 * NULL,"val", so do a shift here whether the var is set or not */
1533 /* want environment variable at beginning of string */
1534 if (!g_ascii_isalnum (**e)) {
1535 GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1536 "variable string: %s", *e);
1540 /* user is allowed to specify e.g. "HOME/.pitivi/plugins" */
1541 g_strlcpy (env_var, *e, sizeof (env_var));
1542 g_strdelimit (env_var, "/\\", '\0');
1544 if ((val = g_getenv (env_var)))
1545 hash += g_str_hash (val);
1552 _priv_plugin_deps_env_vars_changed (GstPlugin * plugin)
1556 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1557 GstPluginDep *dep = l->data;
1559 if (dep->env_hash != gst_plugin_ext_dep_get_env_vars_hash (plugin, dep))
1567 gst_plugin_ext_dep_extract_env_vars_paths (GstPlugin * plugin,
1568 GstPluginDep * dep, GQueue * paths)
1572 for (evars = dep->env_vars; evars != NULL && *evars != NULL; ++evars) {
1576 /* want environment variable at beginning of string */
1577 if (!g_ascii_isalnum (**evars)) {
1578 GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1579 "variable string: %s", *evars);
1583 /* user is allowed to specify e.g. "HOME/.pitivi/plugins", which we want to
1584 * split into the env_var name component and the path component */
1585 components = g_strsplit_set (*evars, "/\\", 2);
1586 g_assert (components != NULL);
1588 e = g_getenv (components[0]);
1589 GST_LOG_OBJECT (plugin, "expanding %s = '%s' (path suffix: %s)",
1590 components[0], GST_STR_NULL (e), GST_STR_NULL (components[1]));
1592 if (components[1] != NULL) {
1593 g_strdelimit (components[1], "/\\", G_DIR_SEPARATOR);
1596 if (e != NULL && *e != '\0') {
1600 arr = g_strsplit (e, G_SEARCHPATH_SEPARATOR_S, -1);
1602 for (i = 0; arr != NULL && arr[i] != NULL; ++i) {
1605 if (!g_path_is_absolute (arr[i])) {
1606 GST_INFO_OBJECT (plugin, "ignoring environment variable content '%s'"
1607 ": either not an absolute path or not a path at all", arr[i]);
1611 if (components[1] != NULL) {
1612 full_path = g_build_filename (arr[i], components[1], NULL);
1614 full_path = g_strdup (arr[i]);
1617 if (!g_queue_find_custom (paths, full_path, (GCompareFunc) strcmp)) {
1618 GST_LOG_OBJECT (plugin, "path: '%s'", full_path);
1619 g_queue_push_tail (paths, full_path);
1622 GST_LOG_OBJECT (plugin, "path: '%s' (duplicate,ignoring)", full_path);
1630 g_strfreev (components);
1633 GST_LOG_OBJECT (plugin, "Extracted %d paths from environment", paths->length);
1637 gst_plugin_ext_dep_get_hash_from_stat_entry (GStatBuf * s)
1640 if (!(s->st_mode & (S_IFDIR | S_IFREG | S_IFBLK | S_IFCHR)))
1642 /* MSVC does not have S_IFBLK */
1643 if (!(s->st_mode & (S_IFDIR | S_IFREG | S_IFCHR)))
1647 /* completely random formula */
1648 return ((s->st_size << 3) + (s->st_mtime << 5)) ^ s->st_ctime;
1652 gst_plugin_ext_dep_direntry_matches (GstPlugin * plugin, const gchar * entry,
1653 const gchar ** filenames, GstPluginDependencyFlags flags)
1655 /* no filenames specified, match all entries for now (could probably
1656 * optimise by just taking the dir stat hash or so) */
1657 if (filenames == NULL || *filenames == NULL || **filenames == '\0')
1660 while (*filenames != NULL) {
1662 if (((flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)) &&
1663 g_str_has_suffix (entry, *filenames)) {
1665 } else if (((flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_PREFIX)) &&
1666 g_str_has_prefix (entry, *filenames)) {
1668 /* else it's an exact match that's needed */
1669 } else if (strcmp (entry, *filenames) == 0) {
1672 GST_LOG ("%s does not match %s, flags=0x%04x", entry, *filenames, flags);
1679 gst_plugin_ext_dep_scan_dir_and_match_names (GstPlugin * plugin,
1680 const gchar * path, const gchar ** filenames,
1681 GstPluginDependencyFlags flags, int depth)
1684 gboolean recurse_dirs;
1689 recurse_dirs = ! !(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1691 dir = g_dir_open (path, 0, &err);
1693 GST_DEBUG_OBJECT (plugin, "g_dir_open(%s) failed: %s", path, err->message);
1698 /* FIXME: we're assuming here that we always get the directory entries in
1699 * the same order, and not in a random order */
1700 while ((entry = g_dir_read_name (dir))) {
1701 gboolean have_match;
1707 gst_plugin_ext_dep_direntry_matches (plugin, entry, filenames, flags);
1709 /* avoid the stat if possible */
1710 if (!have_match && !recurse_dirs)
1713 full_path = g_build_filename (path, entry, NULL);
1714 if (g_stat (full_path, &s) < 0) {
1715 fhash = (guint) - 1;
1716 GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1717 g_strerror (errno));
1718 } else if (have_match) {
1719 fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1720 GST_LOG_OBJECT (plugin, "stat: %s (result: %u)", full_path, fhash);
1721 } else if ((s.st_mode & (S_IFDIR))) {
1722 fhash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, full_path,
1723 filenames, flags, depth + 1);
1725 /* it's not a name match, we want to recurse, but it's not a directory */
1730 hash = hash + fhash;
1739 gst_plugin_ext_dep_scan_path_with_filenames (GstPlugin * plugin,
1740 const gchar * path, const gchar ** filenames,
1741 GstPluginDependencyFlags flags)
1743 const gchar *empty_filenames[] = { "", NULL };
1744 gboolean recurse_into_dirs, partial_names = FALSE;
1747 /* to avoid special-casing below (FIXME?) */
1748 if (filenames == NULL || *filenames == NULL)
1749 filenames = empty_filenames;
1751 recurse_into_dirs = ! !(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1753 if ((flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX) ||
1754 (flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_PREFIX))
1755 partial_names = TRUE;
1757 /* if we can construct the exact paths to check with the data we have, just
1758 * stat them one by one; this is more efficient than opening the directory
1759 * and going through each entry to see if it matches one of our filenames. */
1760 if (!recurse_into_dirs && !partial_names) {
1761 for (i = 0; filenames[i] != NULL; ++i) {
1766 full_path = g_build_filename (path, filenames[i], NULL);
1767 if (g_stat (full_path, &s) < 0) {
1768 fhash = (guint) - 1;
1769 GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1770 g_strerror (errno));
1772 fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1773 GST_LOG_OBJECT (plugin, "stat: %s (result: %08x)", full_path, fhash);
1779 hash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, path,
1780 filenames, flags, 0);
1787 gst_plugin_ext_dep_get_stat_hash (GstPlugin * plugin, GstPluginDep * dep)
1789 gboolean paths_are_default_only;
1790 gboolean paths_are_relative_to_exe;
1791 GQueue scan_paths = G_QUEUE_INIT;
1792 guint scan_hash = 0;
1795 GST_LOG_OBJECT (plugin, "start");
1797 paths_are_default_only =
1798 dep->flags & GST_PLUGIN_DEPENDENCY_FLAG_PATHS_ARE_DEFAULT_ONLY;
1799 paths_are_relative_to_exe =
1800 dep->flags & GST_PLUGIN_DEPENDENCY_FLAG_PATHS_ARE_RELATIVE_TO_EXE;
1802 gst_plugin_ext_dep_extract_env_vars_paths (plugin, dep, &scan_paths);
1804 if (g_queue_is_empty (&scan_paths) || !paths_are_default_only) {
1807 for (paths = dep->paths; paths != NULL && *paths != NULL; ++paths) {
1808 const gchar *path = *paths;
1811 if (paths_are_relative_to_exe && !g_path_is_absolute (path)) {
1814 if (!_gst_executable_path) {
1815 GST_FIXME_OBJECT (plugin,
1816 "Path dependency %s relative to executable path but could not retrieve executable path",
1820 appdir = g_path_get_dirname (_gst_executable_path);
1821 full_path = g_build_filename (appdir, path, NULL);
1824 full_path = g_strdup (path);
1827 if (!g_queue_find_custom (&scan_paths, full_path, (GCompareFunc) strcmp)) {
1828 GST_LOG_OBJECT (plugin, "path: '%s'", full_path);
1829 g_queue_push_tail (&scan_paths, full_path);
1831 GST_LOG_OBJECT (plugin, "path: '%s' (duplicate, ignoring)", full_path);
1837 while ((path = g_queue_pop_head (&scan_paths))) {
1838 scan_hash += gst_plugin_ext_dep_scan_path_with_filenames (plugin, path,
1839 (const gchar **) dep->names, dep->flags);
1843 GST_LOG_OBJECT (plugin, "done, scan_hash: %08x", scan_hash);
1848 _priv_plugin_deps_files_changed (GstPlugin * plugin)
1852 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1853 GstPluginDep *dep = l->data;
1855 if (dep->stat_hash != gst_plugin_ext_dep_get_stat_hash (plugin, dep))
1863 gst_plugin_ext_dep_free (GstPluginDep * dep)
1865 g_strfreev (dep->env_vars);
1866 g_strfreev (dep->paths);
1867 g_strfreev (dep->names);
1868 g_slice_free (GstPluginDep, dep);
1872 gst_plugin_ext_dep_strv_equal (gchar ** arr1, gchar ** arr2)
1876 if (arr1 == NULL || arr2 == NULL)
1878 for (; *arr1 != NULL && *arr2 != NULL; ++arr1, ++arr2) {
1879 if (strcmp (*arr1, *arr2) != 0)
1882 return (*arr1 == *arr2);
1886 gst_plugin_ext_dep_equals (GstPluginDep * dep, const gchar ** env_vars,
1887 const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1889 if (dep->flags != flags)
1892 return gst_plugin_ext_dep_strv_equal (dep->env_vars, (gchar **) env_vars) &&
1893 gst_plugin_ext_dep_strv_equal (dep->paths, (gchar **) paths) &&
1894 gst_plugin_ext_dep_strv_equal (dep->names, (gchar **) names);
1898 * gst_plugin_add_dependency:
1899 * @plugin: a #GstPlugin
1900 * @env_vars: (allow-none) (array zero-terminated=1): %NULL-terminated array of environment variables affecting the
1901 * feature set of the plugin (e.g. an environment variable containing
1902 * paths where to look for additional modules/plugins of a library),
1903 * or %NULL. Environment variable names may be followed by a path component
1904 * which will be added to the content of the environment variable, e.g.
1905 * "HOME/.mystuff/plugins".
1906 * @paths: (allow-none) (array zero-terminated=1): %NULL-terminated array of directories/paths where dependent files
1908 * @names: (allow-none) (array zero-terminated=1): %NULL-terminated array of file names (or file name suffixes,
1909 * depending on @flags) to be used in combination with the paths from
1910 * @paths and/or the paths extracted from the environment variables in
1911 * @env_vars, or %NULL.
1912 * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1914 * Make GStreamer aware of external dependencies which affect the feature
1915 * set of this plugin (ie. the elements or typefinders associated with it).
1917 * GStreamer will re-inspect plugins with external dependencies whenever any
1918 * of the external dependencies change. This is useful for plugins which wrap
1919 * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1920 * library and makes visualisations available as GStreamer elements, or a
1921 * codec loader which exposes elements and/or caps dependent on what external
1922 * codec libraries are currently installed.
1925 gst_plugin_add_dependency (GstPlugin * plugin, const gchar ** env_vars,
1926 const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1931 g_return_if_fail (GST_IS_PLUGIN (plugin));
1933 if ((env_vars == NULL || env_vars[0] == NULL) &&
1934 (paths == NULL || paths[0] == NULL)) {
1935 GST_DEBUG_OBJECT (plugin,
1936 "plugin registered empty dependency set. Ignoring");
1940 for (l = plugin->priv->deps; l != NULL; l = l->next) {
1941 if (gst_plugin_ext_dep_equals (l->data, env_vars, paths, names, flags)) {
1942 GST_LOG_OBJECT (plugin, "dependency already registered");
1947 dep = g_slice_new (GstPluginDep);
1949 dep->env_vars = g_strdupv ((gchar **) env_vars);
1950 dep->paths = g_strdupv ((gchar **) paths);
1951 dep->names = g_strdupv ((gchar **) names);
1954 dep->env_hash = gst_plugin_ext_dep_get_env_vars_hash (plugin, dep);
1955 dep->stat_hash = gst_plugin_ext_dep_get_stat_hash (plugin, dep);
1957 plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
1959 GST_DEBUG_OBJECT (plugin, "added dependency:");
1960 for (; env_vars != NULL && *env_vars != NULL; ++env_vars)
1961 GST_DEBUG_OBJECT (plugin, " evar: %s", *env_vars);
1962 for (; paths != NULL && *paths != NULL; ++paths)
1963 GST_DEBUG_OBJECT (plugin, " path: %s", *paths);
1964 for (; names != NULL && *names != NULL; ++names)
1965 GST_DEBUG_OBJECT (plugin, " name: %s", *names);
1969 * gst_plugin_add_dependency_simple:
1970 * @plugin: the #GstPlugin
1971 * @env_vars: (allow-none): one or more environment variables (separated by ':', ';' or ','),
1972 * or %NULL. Environment variable names may be followed by a path component
1973 * which will be added to the content of the environment variable, e.g.
1974 * "HOME/.mystuff/plugins:MYSTUFF_PLUGINS_PATH"
1975 * @paths: (allow-none): one ore more directory paths (separated by ':' or ';' or ','),
1976 * or %NULL. Example: "/usr/lib/mystuff/plugins"
1977 * @names: (allow-none): one or more file names or file name suffixes (separated by commas),
1979 * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1981 * Make GStreamer aware of external dependencies which affect the feature
1982 * set of this plugin (ie. the elements or typefinders associated with it).
1984 * GStreamer will re-inspect plugins with external dependencies whenever any
1985 * of the external dependencies change. This is useful for plugins which wrap
1986 * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1987 * library and makes visualisations available as GStreamer elements, or a
1988 * codec loader which exposes elements and/or caps dependent on what external
1989 * codec libraries are currently installed.
1991 * Convenience wrapper function for gst_plugin_add_dependency() which
1992 * takes simple strings as arguments instead of string arrays, with multiple
1993 * arguments separated by predefined delimiters (see above).
1996 gst_plugin_add_dependency_simple (GstPlugin * plugin,
1997 const gchar * env_vars, const gchar * paths, const gchar * names,
1998 GstPluginDependencyFlags flags)
2000 gchar **a_evars = NULL;
2001 gchar **a_paths = NULL;
2002 gchar **a_names = NULL;
2005 a_evars = g_strsplit_set (env_vars, ":;,", -1);
2007 a_paths = g_strsplit_set (paths, ":;,", -1);
2009 a_names = g_strsplit_set (names, ",", -1);
2011 gst_plugin_add_dependency (plugin, (const gchar **) a_evars,
2012 (const gchar **) a_paths, (const gchar **) a_names, flags);
2015 g_strfreev (a_evars);
2017 g_strfreev (a_paths);
2019 g_strfreev (a_names);