• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wtay@chello.be>
4  *
5  * gstplugin.c: Plugin subsystem for loading elements, types, and libs
6  *
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.
11  *
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.
16  *
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.
21  */
22 
23 /**
24  * SECTION:gstplugin
25  * @title: GstPlugin
26  * @short_description: Container for features loaded from a shared object module
27  * @see_also: #GstPluginFeature, #GstElementFactory
28  *
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.
32  *
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
38  * `gst_plugin_desc`.
39  *
40  * Once you have a handle to a #GstPlugin (e.g. from the #GstRegistry), you
41  * can add any object that subclasses #GstPluginFeature.
42  *
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
47  * into memory.
48  */
49 
50 #ifdef HAVE_CONFIG_H
51 #include "config.h"
52 #endif
53 
54 #include "gst_private.h"
55 
56 #include <glib/gstdio.h>
57 #include <sys/types.h>
58 #ifdef HAVE_DIRENT_H
59 #include <dirent.h>
60 #endif
61 #ifdef HAVE_UNISTD_H
62 #include <unistd.h>
63 #endif
64 #include <signal.h>
65 #include <errno.h>
66 #include <string.h>
67 
68 #include "glib-compat-private.h"
69 
70 #include <gst/gst.h>
71 
72 #ifdef G_OS_WIN32
73 #include <windows.h>
74 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
75 #define GST_WINAPI_ONLY_APP
76 #endif
77 #endif
78 
79 #define GST_CAT_DEFAULT GST_CAT_PLUGIN_LOADING
80 
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 */
85 
86 /* static variables for segfault handling of plugin loading */
87 static char *_gst_plugin_fault_handler_filename = NULL;
88 
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  * Apache License 2.0: http://www.apache.org/licenses/LICENSE-2.0 (Since: 1.22)
99 
100  * FIXME: update to use SPDX identifiers, or just remove entirely
101  */
102 static const gchar known_licenses[] = "LGPL\000"        /* GNU Lesser General Public License */
103     "GPL\000"                   /* GNU General Public License */
104     "QPL\000"                   /* Trolltech Qt Public License */
105     "GPL/QPL\000"               /* Combi-license of GPL + QPL */
106     "MPL\000"                   /* MPL 1.1 license */
107     "MPL-2.0\000"               /* MPL 2.0 license */
108     "BSD\000"                   /* 3-clause BSD license */
109     "MIT/X11\000"               /* MIT/X11 license */
110     "0BSD\000"                  /* Zero-Clause BSD */
111     "Apache 2.0\000"            /* Apache License 2.0 */
112     "Proprietary\000"           /* Proprietary license */
113     GST_LICENSE_UNKNOWN;        /* some other license */
114 
115 static GstPlugin *gst_plugin_register_func (GstPlugin * plugin,
116     const GstPluginDesc * desc, gpointer user_data);
117 static void gst_plugin_desc_copy (GstPluginDesc * dest,
118     const GstPluginDesc * src);
119 
120 static void gst_plugin_ext_dep_free (GstPluginDep * dep);
121 
122 G_DEFINE_TYPE_WITH_PRIVATE (GstPlugin, gst_plugin, GST_TYPE_OBJECT);
123 
124 static void
gst_plugin_init(GstPlugin * plugin)125 gst_plugin_init (GstPlugin * plugin)
126 {
127   plugin->priv = gst_plugin_get_instance_private (plugin);
128 }
129 
130 static void
gst_plugin_finalize(GObject * object)131 gst_plugin_finalize (GObject * object)
132 {
133   GstPlugin *plugin = GST_PLUGIN_CAST (object);
134 
135   GST_DEBUG ("finalizing plugin %" GST_PTR_FORMAT, plugin);
136 
137   /* FIXME: make registry add a weak ref instead */
138 #if 0
139   GstRegistry *registry = gst_registry_get ();
140   GList *g;
141   for (g = registry->plugins; g; g = g->next) {
142     if (g->data == (gpointer) plugin) {
143       g_warning ("removing plugin that is still in registry");
144     }
145   }
146 #endif
147 
148   g_free (plugin->filename);
149   g_free (plugin->basename);
150 
151   g_list_foreach (plugin->priv->deps, (GFunc) gst_plugin_ext_dep_free, NULL);
152   g_list_free (plugin->priv->deps);
153   plugin->priv->deps = NULL;
154 
155   if (plugin->priv->cache_data) {
156     gst_structure_free (plugin->priv->cache_data);
157   }
158 
159   G_OBJECT_CLASS (gst_plugin_parent_class)->finalize (object);
160 }
161 
162 static void
gst_plugin_class_init(GstPluginClass * klass)163 gst_plugin_class_init (GstPluginClass * klass)
164 {
165   G_OBJECT_CLASS (klass)->finalize = gst_plugin_finalize;
166 }
167 
168 GQuark
gst_plugin_error_quark(void)169 gst_plugin_error_quark (void)
170 {
171   static GQuark quark = 0;
172 
173   if (!quark)
174     quark = g_quark_from_static_string ("gst_plugin_error");
175   return quark;
176 }
177 
178 /**
179  * gst_plugin_register_static:
180  * @major_version: the major version number of the GStreamer core that the
181  *     plugin was compiled for, you can just use GST_VERSION_MAJOR here
182  * @minor_version: the minor version number of the GStreamer core that the
183  *     plugin was compiled for, you can just use GST_VERSION_MINOR here
184  * @name: a unique name of the plugin (ideally prefixed with an application- or
185  *     library-specific namespace prefix in order to avoid name conflicts in
186  *     case a similar plugin with the same name ever gets added to GStreamer)
187  * @description: description of the plugin
188  * @init_func: (scope call): pointer to the init function of this plugin.
189  * @version: version string of the plugin
190  * @license: effective license of plugin. Must be one of the approved licenses
191  *     (see #GstPluginDesc above) or the plugin will not be registered.
192  * @source: source module plugin belongs to
193  * @package: shipped package plugin belongs to
194  * @origin: URL to provider of plugin
195  *
196  * Registers a static plugin, ie. a plugin which is private to an application
197  * or library and contained within the application or library (as opposed to
198  * being shipped as a separate module file).
199  *
200  * You must make sure that GStreamer has been initialised (with gst_init() or
201  * via gst_init_get_option_group()) before calling this function.
202  *
203  * Returns: %TRUE if the plugin was registered correctly, otherwise %FALSE.
204  */
205 gboolean
gst_plugin_register_static(gint major_version,gint minor_version,const gchar * name,const gchar * description,GstPluginInitFunc init_func,const gchar * version,const gchar * license,const gchar * source,const gchar * package,const gchar * origin)206 gst_plugin_register_static (gint major_version, gint minor_version,
207     const gchar * name, const gchar * description, GstPluginInitFunc init_func,
208     const gchar * version, const gchar * license, const gchar * source,
209     const gchar * package, const gchar * origin)
210 {
211   GstPluginDesc desc = { major_version, minor_version, name, description,
212     init_func, version, license, source, package, origin, NULL,
213   };
214   GstPlugin *plugin;
215   gboolean res = FALSE;
216 
217   g_return_val_if_fail (name != NULL, FALSE);
218   g_return_val_if_fail (description != NULL, FALSE);
219   g_return_val_if_fail (init_func != NULL, FALSE);
220   g_return_val_if_fail (version != NULL, FALSE);
221   g_return_val_if_fail (license != NULL, FALSE);
222   g_return_val_if_fail (source != NULL, FALSE);
223   g_return_val_if_fail (package != NULL, FALSE);
224   g_return_val_if_fail (origin != NULL, FALSE);
225 
226   /* make sure gst_init() has been called */
227   g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);
228 
229   GST_LOG ("attempting to load static plugin \"%s\" now...", name);
230   plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
231   if (gst_plugin_register_func (plugin, &desc, NULL) != NULL) {
232     GST_INFO ("registered static plugin \"%s\"", name);
233     res = gst_registry_add_plugin (gst_registry_get (), plugin);
234     GST_INFO ("added static plugin \"%s\", result: %d", name, res);
235   }
236   return res;
237 }
238 
239 /**
240  * gst_plugin_register_static_full:
241  * @major_version: the major version number of the GStreamer core that the
242  *     plugin was compiled for, you can just use GST_VERSION_MAJOR here
243  * @minor_version: the minor version number of the GStreamer core that the
244  *     plugin was compiled for, you can just use GST_VERSION_MINOR here
245  * @name: a unique name of the plugin (ideally prefixed with an application- or
246  *     library-specific namespace prefix in order to avoid name conflicts in
247  *     case a similar plugin with the same name ever gets added to GStreamer)
248  * @description: description of the plugin
249  * @init_full_func: (scope call): pointer to the init function with user data
250  *     of this plugin.
251  * @version: version string of the plugin
252  * @license: effective license of plugin. Must be one of the approved licenses
253  *     (see #GstPluginDesc above) or the plugin will not be registered.
254  * @source: source module plugin belongs to
255  * @package: shipped package plugin belongs to
256  * @origin: URL to provider of plugin
257  * @user_data: gpointer to user data
258  *
259  * Registers a static plugin, ie. a plugin which is private to an application
260  * or library and contained within the application or library (as opposed to
261  * being shipped as a separate module file) with a #GstPluginInitFullFunc
262  * which allows user data to be passed to the callback function (useful
263  * for bindings).
264  *
265  * You must make sure that GStreamer has been initialised (with gst_init() or
266  * via gst_init_get_option_group()) before calling this function.
267  *
268  * Returns: %TRUE if the plugin was registered correctly, otherwise %FALSE.
269  */
270 gboolean
gst_plugin_register_static_full(gint major_version,gint minor_version,const gchar * name,const gchar * description,GstPluginInitFullFunc init_full_func,const gchar * version,const gchar * license,const gchar * source,const gchar * package,const gchar * origin,gpointer user_data)271 gst_plugin_register_static_full (gint major_version, gint minor_version,
272     const gchar * name, const gchar * description,
273     GstPluginInitFullFunc init_full_func, const gchar * version,
274     const gchar * license, const gchar * source, const gchar * package,
275     const gchar * origin, gpointer user_data)
276 {
277   GstPluginDesc desc = { major_version, minor_version, name, description,
278     (GstPluginInitFunc) init_full_func, version, license, source, package,
279     origin, NULL,
280   };
281   GstPlugin *plugin;
282   gboolean res = FALSE;
283 
284   g_return_val_if_fail (name != NULL, FALSE);
285   g_return_val_if_fail (description != NULL, FALSE);
286   g_return_val_if_fail (init_full_func != NULL, FALSE);
287   g_return_val_if_fail (version != NULL, FALSE);
288   g_return_val_if_fail (license != NULL, FALSE);
289   g_return_val_if_fail (source != NULL, FALSE);
290   g_return_val_if_fail (package != NULL, FALSE);
291   g_return_val_if_fail (origin != NULL, FALSE);
292 
293   /* make sure gst_init() has been called */
294   g_return_val_if_fail (_gst_plugin_inited != FALSE, FALSE);
295 
296   GST_LOG ("attempting to load static plugin \"%s\" now...", name);
297   plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
298   if (gst_plugin_register_func (plugin, &desc, user_data) != NULL) {
299     GST_INFO ("registered static plugin \"%s\"", name);
300     res = gst_registry_add_plugin (gst_registry_get (), plugin);
301     GST_INFO ("added static plugin \"%s\", result: %d", name, res);
302   }
303   return res;
304 }
305 
306 void
_priv_gst_plugin_initialize(void)307 _priv_gst_plugin_initialize (void)
308 {
309   const gchar *whitelist;
310   guint i;
311 
312   _gst_plugin_inited = TRUE;
313 
314   whitelist = g_getenv ("GST_PLUGIN_LOADING_WHITELIST");
315   if (whitelist != NULL && *whitelist != '\0') {
316     _plugin_loading_whitelist = g_strsplit (whitelist,
317         G_SEARCHPATH_SEPARATOR_S, -1);
318     for (i = 0; _plugin_loading_whitelist[i] != NULL; ++i) {
319       GST_INFO ("plugins whitelist entry: %s", _plugin_loading_whitelist[i]);
320     }
321   }
322 
323   /* now register all static plugins */
324   GST_INFO ("registering %u static plugins", _num_static_plugins);
325   for (i = 0; i < _num_static_plugins; ++i) {
326     gst_plugin_register_static (_static_plugins[i].major_version,
327         _static_plugins[i].minor_version, _static_plugins[i].name,
328         _static_plugins[i].description, _static_plugins[i].plugin_init,
329         _static_plugins[i].version, _static_plugins[i].license,
330         _static_plugins[i].source, _static_plugins[i].package,
331         _static_plugins[i].origin);
332   }
333 
334   if (_static_plugins) {
335     free (_static_plugins);
336     _static_plugins = NULL;
337     _num_static_plugins = 0;
338   }
339 }
340 
341 /* Whitelist entry format:
342  *
343  *   plugin1,plugin2@pathprefix or
344  *   plugin1,plugin2@* or just
345  *   plugin1,plugin2 or
346  *   source-package@pathprefix or
347  *   source-package@* or just
348  *   source-package
349  *
350  * ie. the bit before the path will be checked against both the plugin
351  * name and the plugin's source package name, to keep the format simple.
352  */
353 static gboolean
gst_plugin_desc_matches_whitelist_entry(const GstPluginDesc * desc,const gchar * filename,const gchar * pattern)354 gst_plugin_desc_matches_whitelist_entry (const GstPluginDesc * desc,
355     const gchar * filename, const gchar * pattern)
356 {
357   const gchar *sep;
358   gboolean ret = FALSE;
359   gchar *name;
360 
361   GST_LOG ("Whitelist pattern '%s', plugin: %s of %s@%s", pattern, desc->name,
362       desc->source, GST_STR_NULL (filename));
363 
364   /* do we have a path prefix? */
365   sep = strchr (pattern, '@');
366   if (sep != NULL && strcmp (sep, "@*") != 0 && strcmp (sep, "@") != 0) {
367     /* paths are not canonicalised or treated with realpath() here. This
368      * should be good enough for our use case, since we just use the paths
369      * autotools uses, and those will be constructed from the same prefix. */
370     if (filename != NULL && !g_str_has_prefix (filename, sep + 1))
371       return FALSE;
372 
373     GST_LOG ("%s matches path prefix %s", GST_STR_NULL (filename), sep + 1);
374   }
375 
376   if (sep != NULL) {
377     name = g_strndup (pattern, (gsize) (sep - pattern));
378   } else {
379     name = g_strdup (pattern);
380   }
381 
382   g_strstrip (name);
383   if (!g_ascii_isalnum (*name)) {
384     GST_WARNING ("Invalid whitelist pattern: %s", pattern);
385     goto done;
386   }
387 
388   /* now check plugin names / source package name */
389   if (strchr (name, ',') == NULL) {
390     /* only a single name: either a plugin name or the source package name */
391     ret = (strcmp (desc->source, name) == 0 || strcmp (desc->name, name) == 0);
392   } else {
393     gchar **n, **names;
394 
395     /* multiple names: assume these are plugin names */
396     names = g_strsplit (name, ",", -1);
397     for (n = names; n != NULL && *n != NULL; ++n) {
398       g_strstrip (*n);
399       if (strcmp (desc->name, *n) == 0) {
400         ret = TRUE;
401         break;
402       }
403     }
404     g_strfreev (names);
405   }
406 
407   GST_LOG ("plugin / source package name match: %d", ret);
408 
409 done:
410 
411   g_free (name);
412   return ret;
413 }
414 
415 gboolean
priv_gst_plugin_desc_is_whitelisted(const GstPluginDesc * desc,const gchar * filename)416 priv_gst_plugin_desc_is_whitelisted (const GstPluginDesc * desc,
417     const gchar * filename)
418 {
419   gchar **entry;
420 
421   if (_plugin_loading_whitelist == NULL)
422     return TRUE;
423 
424   for (entry = _plugin_loading_whitelist; *entry != NULL; ++entry) {
425     if (gst_plugin_desc_matches_whitelist_entry (desc, filename, *entry)) {
426       GST_LOG ("Plugin %s is in whitelist", filename);
427       return TRUE;
428     }
429   }
430 
431   GST_LOG ("Plugin %s (package %s, file %s) not in whitelist", desc->name,
432       desc->source, filename);
433   return FALSE;
434 }
435 
436 gboolean
priv_gst_plugin_loading_have_whitelist(void)437 priv_gst_plugin_loading_have_whitelist (void)
438 {
439   return (_plugin_loading_whitelist != NULL);
440 }
441 
442 guint32
priv_gst_plugin_loading_get_whitelist_hash(void)443 priv_gst_plugin_loading_get_whitelist_hash (void)
444 {
445   guint32 hash = 0;
446 
447   if (_plugin_loading_whitelist != NULL) {
448     gchar **w;
449 
450     for (w = _plugin_loading_whitelist; *w != NULL; ++w)
451       hash ^= g_str_hash (*w);
452   }
453 
454   return hash;
455 }
456 
457 /* this function could be extended to check if the plugin license matches the
458  * applications license (would require the app to register its license somehow).
459  * We'll wait for someone who's interested in it to code it :)
460  */
461 static gboolean
gst_plugin_check_license(const gchar * license)462 gst_plugin_check_license (const gchar * license)
463 {
464   const gchar *l, *end = known_licenses + sizeof (known_licenses);
465 
466   for (l = known_licenses; l < end; l += strlen (l) + 1) {
467     if (strcmp (license, l) == 0)
468       return TRUE;
469   }
470 
471   return FALSE;
472 }
473 
474 static gboolean
gst_plugin_check_version(gint major,gint minor)475 gst_plugin_check_version (gint major, gint minor)
476 {
477   /* return NULL if the major and minor version numbers are not compatible */
478   /* with ours. */
479   if (major != GST_VERSION_MAJOR || minor > GST_VERSION_MINOR)
480     return FALSE;
481 
482   return TRUE;
483 }
484 
485 static GstPlugin *
gst_plugin_register_func(GstPlugin * plugin,const GstPluginDesc * desc,gpointer user_data)486 gst_plugin_register_func (GstPlugin * plugin, const GstPluginDesc * desc,
487     gpointer user_data)
488 {
489   if (!gst_plugin_check_version (desc->major_version, desc->minor_version)) {
490     if (GST_CAT_DEFAULT)
491       GST_WARNING ("plugin \"%s\" has incompatible version "
492           "(plugin: %d.%d, gst: %d.%d), not loading",
493           GST_STR_NULL (plugin->filename), desc->major_version,
494           desc->minor_version, GST_VERSION_MAJOR, GST_VERSION_MINOR);
495     return NULL;
496   }
497 
498   if (!desc->license || !desc->description || !desc->source ||
499       !desc->package || !desc->origin) {
500     if (GST_CAT_DEFAULT)
501       GST_WARNING ("plugin \"%s\" has missing detail in GstPluginDesc, not "
502           "loading", GST_STR_NULL (plugin->filename));
503     return NULL;
504   }
505 
506   if (!gst_plugin_check_license (desc->license)) {
507     if (GST_CAT_DEFAULT)
508       GST_WARNING ("plugin \"%s\" has unknown license \"%s\"",
509           GST_STR_NULL (plugin->filename), desc->license);
510     /* We still want to load the plugin, it's not our job to validate licenses */
511   }
512 
513   if (GST_CAT_DEFAULT)
514     GST_LOG ("plugin \"%s\" looks good", GST_STR_NULL (plugin->filename));
515 
516   gst_plugin_desc_copy (&plugin->desc, desc);
517 
518   /* make resident so we're really sure it never gets unloaded again.
519    * Theoretically this is not needed, but practically it doesn't hurt.
520    * And we're rather safe than sorry. */
521   if (plugin->module)
522     g_module_make_resident (plugin->module);
523 
524   if (user_data) {
525     if (!(((GstPluginInitFullFunc) (desc->plugin_init)) (plugin, user_data))) {
526       if (GST_CAT_DEFAULT)
527         GST_WARNING ("plugin \"%s\" failed to initialise",
528             GST_STR_NULL (plugin->filename));
529       return NULL;
530     }
531   } else {
532     if (!((desc->plugin_init) (plugin))) {
533       if (GST_CAT_DEFAULT)
534         GST_WARNING ("plugin \"%s\" failed to initialise",
535             GST_STR_NULL (plugin->filename));
536       return NULL;
537     }
538   }
539 
540   if (GST_CAT_DEFAULT)
541     GST_LOG ("plugin \"%s\" initialised", GST_STR_NULL (plugin->filename));
542 
543   return plugin;
544 }
545 
546 #ifdef HAVE_SIGACTION
547 static struct sigaction oldaction;
548 static gboolean _gst_plugin_fault_handler_is_setup = FALSE;
549 
550 /*
551  * _gst_plugin_fault_handler_restore:
552  * segfault handler restorer
553  */
554 static void
_gst_plugin_fault_handler_restore(void)555 _gst_plugin_fault_handler_restore (void)
556 {
557   if (!_gst_plugin_fault_handler_is_setup)
558     return;
559 
560   _gst_plugin_fault_handler_is_setup = FALSE;
561 
562   sigaction (SIGSEGV, &oldaction, NULL);
563 }
564 
565 /*
566  * _gst_plugin_fault_handler_sighandler:
567  * segfault handler implementation
568  */
569 static void
_gst_plugin_fault_handler_sighandler(int signum)570 _gst_plugin_fault_handler_sighandler (int signum)
571 {
572   /* We need to restore the fault handler or we'll keep getting it */
573   _gst_plugin_fault_handler_restore ();
574 
575   switch (signum) {
576     case SIGSEGV:
577       g_print ("\nERROR: ");
578       g_print ("Caught a segmentation fault while loading plugin file:\n");
579       g_print ("%s\n\n", _gst_plugin_fault_handler_filename);
580       g_print ("Please either:\n");
581       g_print ("- remove it and restart.\n");
582       g_print
583           ("- run with --gst-disable-segtrap --gst-disable-registry-fork and debug.\n");
584       exit (-1);
585       break;
586     default:
587       g_print ("Caught unhandled signal on plugin loading\n");
588       break;
589   }
590 }
591 
592 /*
593  * _gst_plugin_fault_handler_setup:
594  * sets up the segfault handler
595  */
596 static void
_gst_plugin_fault_handler_setup(void)597 _gst_plugin_fault_handler_setup (void)
598 {
599   struct sigaction action;
600 
601   /* if asked to leave segfaults alone, just return */
602   if (!gst_segtrap_is_enabled ())
603     return;
604 
605   if (_gst_plugin_fault_handler_is_setup)
606     return;
607 
608   _gst_plugin_fault_handler_is_setup = TRUE;
609 
610   memset (&action, 0, sizeof (action));
611   action.sa_handler = _gst_plugin_fault_handler_sighandler;
612 
613   sigaction (SIGSEGV, &action, &oldaction);
614 }
615 #else /* !HAVE_SIGACTION */
616 static void
_gst_plugin_fault_handler_restore(void)617 _gst_plugin_fault_handler_restore (void)
618 {
619 }
620 
621 static void
_gst_plugin_fault_handler_setup(void)622 _gst_plugin_fault_handler_setup (void)
623 {
624 }
625 #endif /* HAVE_SIGACTION */
626 
627 /* g_time_val_from_iso8601() doesn't do quite what we want */
628 static gboolean
check_release_datetime(const gchar * date_time)629 check_release_datetime (const gchar * date_time)
630 {
631   guint64 val;
632 
633   /* we require YYYY-MM-DD or YYYY-MM-DDTHH:MMZ format */
634   if (!g_ascii_isdigit (*date_time))
635     return FALSE;
636 
637   val = g_ascii_strtoull (date_time, (gchar **) & date_time, 10);
638   if (val < 2000 || val > 2100 || *date_time != '-')
639     return FALSE;
640 
641   val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
642   if (val == 0 || val > 12 || *date_time != '-')
643     return FALSE;
644 
645   val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
646   if (val == 0 || val > 32)
647     return FALSE;
648 
649   /* end of string or date/time separator + HH:MMZ */
650   if (*date_time == 'T' || *date_time == ' ') {
651     val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
652     if (val > 24 || *date_time != ':')
653       return FALSE;
654 
655     val = g_ascii_strtoull (date_time + 1, (gchar **) & date_time, 10);
656     if (val > 59 || *date_time != 'Z')
657       return FALSE;
658 
659     ++date_time;
660   }
661 
662   return (*date_time == '\0');
663 }
664 
665 static GMutex gst_plugin_loading_mutex;
666 
667 #define CHECK_PLUGIN_DESC_FIELD(desc,field,fn)                               \
668   if (G_UNLIKELY ((desc)->field == NULL || *(desc)->field == '\0')) {        \
669     g_warning ("Plugin description for '%s' has no valid %s field", fn, G_STRINGIFY (field)); \
670     g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, \
671         "Plugin %s has invalid plugin description field '%s'", \
672         filename, G_STRINGIFY (field)); \
673     goto return_error;                                                       \
674   }
675 
676 /**
677  * gst_plugin_load_file:
678  * @filename: (type filename): the plugin filename to load
679  * @error: pointer to a %NULL-valued GError
680  *
681  * Loads the given plugin and refs it.  Caller needs to unref after use.
682  *
683  * Returns: (transfer full): a reference to the existing loaded GstPlugin, a
684  * reference to the newly-loaded GstPlugin, or %NULL if an error occurred.
685  */
686 GstPlugin *
gst_plugin_load_file(const gchar * filename,GError ** error)687 gst_plugin_load_file (const gchar * filename, GError ** error)
688 {
689   return _priv_gst_plugin_load_file_for_registry (filename, NULL, error);
690 }
691 
692 static gchar *
extract_symname(const char * filename)693 extract_symname (const char *filename)
694 {
695   gchar *bname, *name, *symname;
696   const gchar *dot;
697   gsize prefix_len, len;
698   int i;
699 
700   bname = g_path_get_basename (filename);
701   for (i = 0; bname[i]; ++i) {
702     if (bname[i] == '-')
703       bname[i] = '_';
704   }
705 
706   if (g_str_has_prefix (bname, "libgst"))
707     prefix_len = 6;
708   else if (g_str_has_prefix (bname, "lib"))
709     prefix_len = 3;
710   else if (g_str_has_prefix (bname, "gst"))
711     prefix_len = 3;
712   else
713     prefix_len = 0;             /* use whole name (minus suffix) as plugin name */
714 
715   dot = g_utf8_strchr (bname, -1, '.');
716   if (dot)
717     len = dot - bname - prefix_len;
718   else
719     len = strlen (bname + prefix_len);
720 
721   name = g_strndup (bname + prefix_len, len);
722   g_free (bname);
723 
724   symname = g_strconcat ("gst_plugin_", name, "_get_desc", NULL);
725   g_free (name);
726 
727   return symname;
728 }
729 
730 #ifdef G_OS_WIN32
731 /*
732  * It is an extremely common mistake on Windows to have incorrect PATH values
733  * when loading a plugin, and the error message is very confusing in this case:
734  * 'The specified module could not be found.' which implies the plugin itself
735  * could not be found. The actual issue is that a DLL dependency could not be
736  * found. We need to detect this case and print a more useful error message.
737  *
738  * Unfortunately, g_module_open() doesn't actually give us the GetLastError()
739  * code from LoadLibraryW() and only gives us a literal message from
740  * FormatMessageW(). We can't do a string comparison on that because it is
741  * locale-dependent.
742  *
743  * The only way out is for us to try loading the module ourselves on failure and
744  * get the error DWORD again from GetLastError().
745  */
746 static char *
get_better_module_load_error(const char * filename,const char * orig_err_msg)747 get_better_module_load_error (const char *filename, const char *orig_err_msg)
748 {
749   BOOL ret = 0;
750   DWORD mode;
751   wchar_t *wfilename;
752   HMODULE handle;
753   char *err_msg = NULL;
754 
755   wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
756 #ifdef GST_WINAPI_ONLY_APP
757   handle = LoadPackagedLibrary (wfilename, 0);
758 #else
759   ret = SetThreadErrorMode (SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS,
760       &mode);
761 
762   handle = LoadLibraryW (wfilename);
763 #endif
764   g_free (wfilename);
765 
766   if (handle == NULL) {
767     DWORD err = GetLastError ();
768     char *win32_err_msg = g_win32_error_message (err);
769     if (err == ERROR_MOD_NOT_FOUND) {
770       err_msg = g_strdup_printf ("%s\nThis usually means Windows was unable "
771           "to find a DLL dependency of the plugin. Please check that PATH is "
772           "correct.\nYou can run 'dumpbin -dependents' (provided by the "
773           "Visual Studio developer prompt) to list the DLL deps of any DLL.\n"
774           "There are also some third-party GUIs to list and debug DLL "
775           "dependencies recursively.", win32_err_msg);
776       g_free (win32_err_msg);
777     } else {
778       err_msg = win32_err_msg;
779     }
780   } else {
781     err_msg = g_strdup_printf ("g_module_open() failed on %s with \"%s\" but "
782         "manual loading succeeded; this should be impossible! Please "
783         "report this as a GStreamer bug.", filename, orig_err_msg);
784     FreeLibrary (handle);
785   }
786 
787   if (ret > 0)
788     SetThreadErrorMode (mode, NULL);
789 
790   return err_msg;
791 }
792 #endif /* G_OS_WIN32 */
793 
794 /* Note: The return value is (transfer full) although we work with floating
795  * references here. If a new plugin instance is created, it is always sinked
796  * in the registry first and a new reference is returned
797  */
798 GstPlugin *
_priv_gst_plugin_load_file_for_registry(const gchar * filename,GstRegistry * registry,GError ** error)799 _priv_gst_plugin_load_file_for_registry (const gchar * filename,
800     GstRegistry * registry, GError ** error)
801 {
802   const GstPluginDesc *desc;
803   GstPlugin *plugin;
804   gchar *symname;
805   GModule *module;
806   gboolean ret;
807   gpointer ptr;
808   GStatBuf file_status;
809   gboolean new_plugin = TRUE;
810   GModuleFlags flags;
811 
812   g_return_val_if_fail (filename != NULL, NULL);
813 
814   if (registry == NULL)
815     registry = gst_registry_get ();
816 
817   g_mutex_lock (&gst_plugin_loading_mutex);
818 
819   plugin = gst_registry_lookup (registry, filename);
820   if (plugin) {
821     if (plugin->module) {
822       /* already loaded */
823       g_mutex_unlock (&gst_plugin_loading_mutex);
824       return plugin;
825     } else if (g_strcmp0 (plugin->filename, filename) == 0) {
826       /* load plugin and update fields */
827       new_plugin = FALSE;
828     }
829   }
830 
831   GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "attempt to load plugin \"%s\"",
832       filename);
833 
834   if (!g_module_supported ()) {
835     GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "module loading not supported");
836     g_set_error (error,
837         GST_PLUGIN_ERROR,
838         GST_PLUGIN_ERROR_MODULE, "Dynamic loading not supported");
839     goto return_error;
840   }
841 #if defined(GST_WINAPI_ONLY_APP)
842   /* plugins loaded by filename by Universal Windows Platform apps do not use
843    * an actual file with a path, they use a packaged (asset) library */
844   file_status.st_mtime = 0;
845   file_status.st_size = 0;
846 #else
847   if (g_stat (filename, &file_status)) {
848     GST_CAT_DEBUG (GST_CAT_PLUGIN_LOADING, "problem accessing file");
849     g_set_error (error,
850         GST_PLUGIN_ERROR,
851         GST_PLUGIN_ERROR_MODULE, "Problem accessing file %s: %s", filename,
852         g_strerror (errno));
853     goto return_error;
854   }
855 #endif
856 
857   flags = G_MODULE_BIND_LOCAL;
858   /* libgstpython.so is the gst-python plugin loader. It needs to be loaded with
859    * G_MODULE_BIND_LAZY.
860    *
861    * Ideally there should be a generic way for plugins to specify that they
862    * need to be loaded with _LAZY.
863    * */
864   if (strstr (filename, "libgstpython"))
865     flags |= G_MODULE_BIND_LAZY;
866 
867   module = g_module_open (filename, flags);
868   if (module == NULL) {
869 #ifdef G_OS_WIN32
870     /* flags are meaningless / ignored on Windows */
871     char *err_msg = get_better_module_load_error (filename, g_module_error ());
872 #else
873     const char *err_msg = g_module_error ();
874 #endif
875     GST_CAT_WARNING (GST_CAT_PLUGIN_LOADING, "module_open failed: %s", err_msg);
876     g_set_error (error,
877         GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE, "Opening module failed: %s",
878         err_msg);
879     /* If we failed to open the shared object, then it's probably because a
880      * plugin is linked against the wrong libraries. Print out an easy-to-see
881      * message in this case. */
882     g_warning ("Failed to load plugin '%s': %s", filename, err_msg);
883 #ifdef G_OS_WIN32
884     g_free (err_msg);
885 #endif
886     goto return_error;
887   }
888 
889   symname = extract_symname (filename);
890   ret = g_module_symbol (module, symname, &ptr);
891 
892   if (ret) {
893     GstPluginDesc *(*get_desc) (void) = ptr;
894     ptr = get_desc ();
895   } else {
896     GST_DEBUG ("Could not find symbol '%s', falling back to gst_plugin_desc",
897         symname);
898     ret = g_module_symbol (module, "gst_plugin_desc", &ptr);
899   }
900 
901   g_free (symname);
902 
903   if (!ret) {
904     GST_DEBUG ("Could not find plugin entry point in \"%s\"", filename);
905     g_set_error (error,
906         GST_PLUGIN_ERROR,
907         GST_PLUGIN_ERROR_MODULE,
908         "File \"%s\" is not a GStreamer plugin", filename);
909     g_module_close (module);
910     goto return_error;
911   }
912 
913   desc = (const GstPluginDesc *) ptr;
914 
915   if (priv_gst_plugin_loading_have_whitelist () &&
916       !priv_gst_plugin_desc_is_whitelisted (desc, filename)) {
917     GST_INFO ("Whitelist specified and plugin not in whitelist, not loading: "
918         "name=%s, package=%s, file=%s", desc->name, desc->source, filename);
919     g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
920         "Not loading plugin file \"%s\", not in whitelist", filename);
921     g_module_close (module);
922     goto return_error;
923   }
924 
925   if (new_plugin) {
926     plugin = g_object_new (GST_TYPE_PLUGIN, NULL);
927     plugin->file_mtime = file_status.st_mtime;
928     plugin->file_size = file_status.st_size;
929     plugin->filename = g_strdup (filename);
930     plugin->basename = g_path_get_basename (filename);
931   }
932 
933   plugin->module = module;
934 
935   if (new_plugin) {
936     /* check plugin description: complain about bad values and fail */
937     CHECK_PLUGIN_DESC_FIELD (desc, name, filename);
938     CHECK_PLUGIN_DESC_FIELD (desc, description, filename);
939     CHECK_PLUGIN_DESC_FIELD (desc, version, filename);
940     CHECK_PLUGIN_DESC_FIELD (desc, license, filename);
941     CHECK_PLUGIN_DESC_FIELD (desc, source, filename);
942     CHECK_PLUGIN_DESC_FIELD (desc, package, filename);
943     CHECK_PLUGIN_DESC_FIELD (desc, origin, filename);
944 
945     if (desc->name != NULL && desc->name[0] == '"') {
946       g_warning ("Invalid plugin name '%s' - fix your GST_PLUGIN_DEFINE "
947           "(remove quotes around plugin name)", desc->name);
948     }
949 
950     if (desc->release_datetime != NULL &&
951         !check_release_datetime (desc->release_datetime)) {
952       g_warning ("GstPluginDesc for '%s' has invalid datetime '%s'",
953           filename, desc->release_datetime);
954       g_set_error (error, GST_PLUGIN_ERROR, GST_PLUGIN_ERROR_MODULE,
955           "Plugin %s has invalid plugin description field 'release_datetime'",
956           filename);
957       goto return_error;
958     }
959   }
960 
961   GST_LOG ("Plugin %p for file \"%s\" prepared, calling entry function...",
962       plugin, filename);
963 
964   /* this is where we load the actual .so, so let's trap SIGSEGV */
965   _gst_plugin_fault_handler_setup ();
966   _gst_plugin_fault_handler_filename = plugin->filename;
967 
968   GST_LOG ("Plugin %p for file \"%s\" prepared, registering...",
969       plugin, filename);
970 
971   if (!gst_plugin_register_func (plugin, desc, NULL)) {
972     /* remove signal handler */
973     _gst_plugin_fault_handler_restore ();
974     GST_DEBUG ("gst_plugin_register_func failed for plugin \"%s\"", filename);
975     /* plugin == NULL */
976     g_set_error (error,
977         GST_PLUGIN_ERROR,
978         GST_PLUGIN_ERROR_MODULE,
979         "File \"%s\" appears to be a GStreamer plugin, but it failed to initialize",
980         filename);
981     goto return_error;
982   }
983 
984   /* remove signal handler */
985   _gst_plugin_fault_handler_restore ();
986   _gst_plugin_fault_handler_filename = NULL;
987   GST_INFO ("plugin \"%s\" loaded", plugin->filename);
988 
989   if (new_plugin) {
990     gst_object_ref (plugin);
991     gst_registry_add_plugin (registry, plugin);
992   }
993 
994   g_mutex_unlock (&gst_plugin_loading_mutex);
995   return plugin;
996 
997 return_error:
998   {
999     if (plugin)
1000       gst_object_unref (plugin);
1001     g_mutex_unlock (&gst_plugin_loading_mutex);
1002     return NULL;
1003   }
1004 }
1005 
1006 static void
gst_plugin_desc_copy(GstPluginDesc * dest,const GstPluginDesc * src)1007 gst_plugin_desc_copy (GstPluginDesc * dest, const GstPluginDesc * src)
1008 {
1009   dest->major_version = src->major_version;
1010   dest->minor_version = src->minor_version;
1011   dest->name = g_intern_string (src->name);
1012   dest->description = g_intern_string (src->description);
1013   dest->plugin_init = src->plugin_init;
1014   dest->version = g_intern_string (src->version);
1015   dest->license = g_intern_string (src->license);
1016   dest->source = g_intern_string (src->source);
1017   dest->package = g_intern_string (src->package);
1018   dest->origin = g_intern_string (src->origin);
1019   dest->release_datetime = g_intern_string (src->release_datetime);
1020 }
1021 
1022 /**
1023  * gst_plugin_get_name:
1024  * @plugin: plugin to get the name of
1025  *
1026  * Get the short name of the plugin
1027  *
1028  * Returns: the name of the plugin
1029  */
1030 const gchar *
gst_plugin_get_name(GstPlugin * plugin)1031 gst_plugin_get_name (GstPlugin * plugin)
1032 {
1033   g_return_val_if_fail (plugin != NULL, NULL);
1034 
1035   return plugin->desc.name;
1036 }
1037 
1038 /**
1039  * gst_plugin_get_description:
1040  * @plugin: plugin to get long name of
1041  *
1042  * Get the long descriptive name of the plugin
1043  *
1044  * Returns: the long name of the plugin
1045  */
1046 const gchar *
gst_plugin_get_description(GstPlugin * plugin)1047 gst_plugin_get_description (GstPlugin * plugin)
1048 {
1049   g_return_val_if_fail (plugin != NULL, NULL);
1050 
1051   return plugin->desc.description;
1052 }
1053 
1054 /**
1055  * gst_plugin_get_filename:
1056  * @plugin: plugin to get the filename of
1057  *
1058  * get the filename of the plugin
1059  *
1060  * Returns: (type filename) (nullable): the filename of the plugin
1061  */
1062 const gchar *
gst_plugin_get_filename(GstPlugin * plugin)1063 gst_plugin_get_filename (GstPlugin * plugin)
1064 {
1065   g_return_val_if_fail (plugin != NULL, NULL);
1066 
1067   return plugin->filename;
1068 }
1069 
1070 /**
1071  * gst_plugin_get_version:
1072  * @plugin: plugin to get the version of
1073  *
1074  * get the version of the plugin
1075  *
1076  * Returns: the version of the plugin
1077  */
1078 const gchar *
gst_plugin_get_version(GstPlugin * plugin)1079 gst_plugin_get_version (GstPlugin * plugin)
1080 {
1081   g_return_val_if_fail (plugin != NULL, NULL);
1082 
1083   return plugin->desc.version;
1084 }
1085 
1086 /**
1087  * gst_plugin_get_license:
1088  * @plugin: plugin to get the license of
1089  *
1090  * get the license of the plugin
1091  *
1092  * Returns: the license of the plugin
1093  */
1094 const gchar *
gst_plugin_get_license(GstPlugin * plugin)1095 gst_plugin_get_license (GstPlugin * plugin)
1096 {
1097   g_return_val_if_fail (plugin != NULL, NULL);
1098 
1099   return plugin->desc.license;
1100 }
1101 
1102 /**
1103  * gst_plugin_get_source:
1104  * @plugin: plugin to get the source of
1105  *
1106  * get the source module the plugin belongs to.
1107  *
1108  * Returns: the source of the plugin
1109  */
1110 const gchar *
gst_plugin_get_source(GstPlugin * plugin)1111 gst_plugin_get_source (GstPlugin * plugin)
1112 {
1113   g_return_val_if_fail (plugin != NULL, NULL);
1114 
1115   return plugin->desc.source;
1116 }
1117 
1118 /**
1119  * gst_plugin_get_package:
1120  * @plugin: plugin to get the package of
1121  *
1122  * get the package the plugin belongs to.
1123  *
1124  * Returns: the package of the plugin
1125  */
1126 const gchar *
gst_plugin_get_package(GstPlugin * plugin)1127 gst_plugin_get_package (GstPlugin * plugin)
1128 {
1129   g_return_val_if_fail (plugin != NULL, NULL);
1130 
1131   return plugin->desc.package;
1132 }
1133 
1134 /**
1135  * gst_plugin_get_origin:
1136  * @plugin: plugin to get the origin of
1137  *
1138  * get the URL where the plugin comes from
1139  *
1140  * Returns: the origin of the plugin
1141  */
1142 const gchar *
gst_plugin_get_origin(GstPlugin * plugin)1143 gst_plugin_get_origin (GstPlugin * plugin)
1144 {
1145   g_return_val_if_fail (plugin != NULL, NULL);
1146 
1147   return plugin->desc.origin;
1148 }
1149 
1150 /**
1151  * gst_plugin_get_release_date_string:
1152  * @plugin: plugin to get the release date of
1153  *
1154  * Get the release date (and possibly time) in form of a string, if available.
1155  *
1156  * For normal GStreamer plugin releases this will usually just be a date in
1157  * the form of "YYYY-MM-DD", while pre-releases and builds from git may contain
1158  * a time component after the date as well, in which case the string will be
1159  * formatted like "YYYY-MM-DDTHH:MMZ" (e.g. "2012-04-30T09:30Z").
1160  *
1161  * There may be plugins that do not have a valid release date set on them.
1162  *
1163  * Returns: (nullable): the date string of the plugin, or %NULL if not
1164  * available.
1165  */
1166 const gchar *
gst_plugin_get_release_date_string(GstPlugin * plugin)1167 gst_plugin_get_release_date_string (GstPlugin * plugin)
1168 {
1169   g_return_val_if_fail (plugin != NULL, NULL);
1170 
1171   return plugin->desc.release_datetime;
1172 }
1173 
1174 /**
1175  * gst_plugin_is_loaded:
1176  * @plugin: plugin to query
1177  *
1178  * queries if the plugin is loaded into memory
1179  *
1180  * Returns: %TRUE is loaded, %FALSE otherwise
1181  */
1182 gboolean
gst_plugin_is_loaded(GstPlugin * plugin)1183 gst_plugin_is_loaded (GstPlugin * plugin)
1184 {
1185   g_return_val_if_fail (plugin != NULL, FALSE);
1186 
1187   return (plugin->module != NULL || plugin->filename == NULL);
1188 }
1189 
1190 /**
1191  * gst_plugin_get_cache_data:
1192  * @plugin: a plugin
1193  *
1194  * Gets the plugin specific data cache. If it is %NULL there is no cached data
1195  * stored. This is the case when the registry is getting rebuilt.
1196  *
1197  * Returns: (transfer none) (nullable): The cached data as a
1198  * #GstStructure or %NULL.
1199  */
1200 const GstStructure *
gst_plugin_get_cache_data(GstPlugin * plugin)1201 gst_plugin_get_cache_data (GstPlugin * plugin)
1202 {
1203   g_return_val_if_fail (GST_IS_PLUGIN (plugin), NULL);
1204 
1205   return plugin->priv->cache_data;
1206 }
1207 
1208 /**
1209  * gst_plugin_set_cache_data:
1210  * @plugin: a plugin
1211  * @cache_data: (transfer full): a structure containing the data to cache
1212  *
1213  * Adds plugin specific data to cache. Passes the ownership of the structure to
1214  * the @plugin.
1215  *
1216  * The cache is flushed every time the registry is rebuilt.
1217  */
1218 void
gst_plugin_set_cache_data(GstPlugin * plugin,GstStructure * cache_data)1219 gst_plugin_set_cache_data (GstPlugin * plugin, GstStructure * cache_data)
1220 {
1221   g_return_if_fail (GST_IS_PLUGIN (plugin));
1222   g_return_if_fail (GST_IS_STRUCTURE (cache_data));
1223 
1224   if (plugin->priv->cache_data) {
1225     gst_structure_free (plugin->priv->cache_data);
1226   }
1227   plugin->priv->cache_data = cache_data;
1228 }
1229 
1230 #if 0
1231 /**
1232  * gst_plugin_feature_list:
1233  * @plugin: plugin to query
1234  * @filter: the filter to use
1235  * @first: only return first match
1236  * @user_data: user data passed to the filter function
1237  *
1238  * Runs a filter against all plugin features and returns a GList with
1239  * the results. If the first flag is set, only the first match is
1240  * returned (as a list with a single object).
1241  *
1242  * Returns: a GList of features, g_list_free after use.
1243  */
1244 GList *
1245 gst_plugin_feature_filter (GstPlugin * plugin,
1246     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
1247 {
1248   GList *list;
1249   GList *g;
1250 
1251   list = gst_filter_run (plugin->features, (GstFilterFunc) filter, first,
1252       user_data);
1253   for (g = list; g; g = g->next) {
1254     gst_object_ref (plugin);
1255   }
1256 
1257   return list;
1258 }
1259 
1260 typedef struct
1261 {
1262   GstPluginFeatureFilter filter;
1263   gboolean first;
1264   gpointer user_data;
1265   GList *result;
1266 }
1267 FeatureFilterData;
1268 
1269 static gboolean
1270 _feature_filter (GstPlugin * plugin, gpointer user_data)
1271 {
1272   GList *result;
1273   FeatureFilterData *data = (FeatureFilterData *) user_data;
1274 
1275   result = gst_plugin_feature_filter (plugin, data->filter, data->first,
1276       data->user_data);
1277   if (result) {
1278     data->result = g_list_concat (data->result, result);
1279     return TRUE;
1280   }
1281   return FALSE;
1282 }
1283 
1284 /**
1285  * gst_plugin_list_feature_filter:
1286  * @list: a #GList of plugins to query
1287  * @filter: the filter function to use
1288  * @first: only return first match
1289  * @user_data: user data passed to the filter function
1290  *
1291  * Runs a filter against all plugin features of the plugins in the given
1292  * list and returns a GList with the results.
1293  * If the first flag is set, only the first match is
1294  * returned (as a list with a single object).
1295  *
1296  * Returns: a GList of features, g_list_free after use.
1297  */
1298 GList *
1299 gst_plugin_list_feature_filter (GList * list,
1300     GstPluginFeatureFilter filter, gboolean first, gpointer user_data)
1301 {
1302   FeatureFilterData data;
1303   GList *result;
1304 
1305   data.filter = filter;
1306   data.first = first;
1307   data.user_data = user_data;
1308   data.result = NULL;
1309 
1310   result = gst_filter_run (list, (GstFilterFunc) _feature_filter, first, &data);
1311   g_list_free (result);
1312 
1313   return data.result;
1314 }
1315 
1316 /**
1317  * gst_plugin_find_feature:
1318  * @plugin: plugin to get the feature from
1319  * @name: The name of the feature to find
1320  * @type: The type of the feature to find
1321  *
1322  * Find a feature of the given name and type in the given plugin.
1323  *
1324  * Returns: a GstPluginFeature or %NULL if the feature was not found.
1325  */
1326 GstPluginFeature *
1327 gst_plugin_find_feature (GstPlugin * plugin, const gchar * name, GType type)
1328 {
1329   GList *walk;
1330   GstPluginFeature *result = NULL;
1331   GstTypeNameData data;
1332 
1333   g_return_val_if_fail (name != NULL, NULL);
1334 
1335   data.type = type;
1336   data.name = name;
1337 
1338   walk = gst_filter_run (plugin->features,
1339       (GstFilterFunc) gst_plugin_feature_type_name_filter, TRUE, &data);
1340 
1341   if (walk) {
1342     result = GST_PLUGIN_FEATURE (walk->data);
1343 
1344     gst_object_ref (result);
1345     gst_plugin_feature_list_free (walk);
1346   }
1347 
1348   return result;
1349 }
1350 #endif
1351 
1352 #if 0
1353 static gboolean
1354 gst_plugin_feature_name_filter (GstPluginFeature * feature, const gchar * name)
1355 {
1356   return !strcmp (name, GST_PLUGIN_FEATURE_NAME (feature));
1357 }
1358 #endif
1359 
1360 #if 0
1361 /**
1362  * gst_plugin_find_feature_by_name:
1363  * @plugin: plugin to get the feature from
1364  * @name: The name of the feature to find
1365  *
1366  * Find a feature of the given name in the given plugin.
1367  *
1368  * Returns: a GstPluginFeature or %NULL if the feature was not found.
1369  */
1370 GstPluginFeature *
1371 gst_plugin_find_feature_by_name (GstPlugin * plugin, const gchar * name)
1372 {
1373   GList *walk;
1374   GstPluginFeature *result = NULL;
1375 
1376   g_return_val_if_fail (name != NULL, NULL);
1377 
1378   walk = gst_filter_run (plugin->features,
1379       (GstFilterFunc) gst_plugin_feature_name_filter, TRUE, (void *) name);
1380 
1381   if (walk) {
1382     result = GST_PLUGIN_FEATURE (walk->data);
1383 
1384     gst_object_ref (result);
1385     gst_plugin_feature_list_free (walk);
1386   }
1387 
1388   return result;
1389 }
1390 #endif
1391 
1392 /**
1393  * gst_plugin_load_by_name:
1394  * @name: name of plugin to load
1395  *
1396  * Load the named plugin. Refs the plugin.
1397  *
1398  * Returns: (transfer full) (nullable): a reference to a loaded plugin, or
1399  * %NULL on error.
1400  */
1401 GstPlugin *
gst_plugin_load_by_name(const gchar * name)1402 gst_plugin_load_by_name (const gchar * name)
1403 {
1404   GstPlugin *plugin, *newplugin;
1405   GError *error = NULL;
1406 
1407   GST_DEBUG ("looking up plugin %s in default registry", name);
1408   plugin = gst_registry_find_plugin (gst_registry_get (), name);
1409   if (plugin) {
1410     GST_DEBUG ("loading plugin %s from file %s", name, plugin->filename);
1411     newplugin = gst_plugin_load_file (plugin->filename, &error);
1412     gst_object_unref (plugin);
1413 
1414     if (!newplugin) {
1415       GST_WARNING ("load_plugin error: %s", error->message);
1416       g_error_free (error);
1417       return NULL;
1418     }
1419     /* newplugin was reffed by load_file */
1420     return newplugin;
1421   }
1422 
1423   GST_DEBUG ("Could not find plugin %s in registry", name);
1424   return NULL;
1425 }
1426 
1427 /**
1428  * gst_plugin_load:
1429  * @plugin: (transfer none): plugin to load
1430  *
1431  * Loads @plugin. Note that the *return value* is the loaded plugin; @plugin is
1432  * untouched. The normal use pattern of this function goes like this:
1433  *
1434  * |[
1435  * GstPlugin *loaded_plugin;
1436  * loaded_plugin = gst_plugin_load (plugin);
1437  * // presumably, we're no longer interested in the potentially-unloaded plugin
1438  * gst_object_unref (plugin);
1439  * plugin = loaded_plugin;
1440  * ]|
1441  *
1442  * Returns: (transfer full) (nullable): a reference to a loaded plugin, or
1443  * %NULL on error.
1444  */
1445 GstPlugin *
gst_plugin_load(GstPlugin * plugin)1446 gst_plugin_load (GstPlugin * plugin)
1447 {
1448   GError *error = NULL;
1449   GstPlugin *newplugin;
1450 
1451   if (gst_plugin_is_loaded (plugin)) {
1452     return gst_object_ref (plugin);
1453   }
1454 
1455   if (!(newplugin = gst_plugin_load_file (plugin->filename, &error)))
1456     goto load_error;
1457 
1458   return newplugin;
1459 
1460 load_error:
1461   {
1462     GST_WARNING ("load_plugin error: %s", error->message);
1463     g_error_free (error);
1464     return NULL;
1465   }
1466 }
1467 
1468 /**
1469  * gst_plugin_list_free:
1470  * @list: (transfer full) (element-type Gst.Plugin): list of #GstPlugin
1471  *
1472  * Unrefs each member of @list, then frees the list.
1473  */
1474 void
gst_plugin_list_free(GList * list)1475 gst_plugin_list_free (GList * list)
1476 {
1477   GList *g;
1478 
1479   for (g = list; g; g = g->next) {
1480     gst_object_unref (GST_PLUGIN_CAST (g->data));
1481   }
1482   g_list_free (list);
1483 }
1484 
1485 /* ===== plugin dependencies ===== */
1486 
1487 /* Scenarios:
1488  * ENV + xyz     where ENV can contain multiple values separated by SEPARATOR
1489  *               xyz may be "" (if ENV contains path to file rather than dir)
1490  * ENV + *xyz   same as above, but xyz acts as suffix filter
1491  * ENV + xyz*   same as above, but xyz acts as prefix filter (is this needed?)
1492  * ENV + *xyz*  same as above, but xyz acts as strstr filter (is this needed?)
1493  *
1494  * same as above, with additional paths hard-coded at compile-time:
1495  *   - only check paths + ... if ENV is not set or yields not paths
1496  *   - always check paths + ... in addition to ENV
1497  *
1498  * When user specifies set of environment variables, he/she may also use e.g.
1499  * "HOME/.mystuff/plugins", and we'll expand the content of $HOME with the
1500  * remainder
1501  */
1502 
1503 /* we store in registry:
1504  *  sets of:
1505  *   {
1506  *     - environment variables (array of strings)
1507  *     - last hash of env variable contents (uint) (so we can avoid doing stats
1508  *       if one of the env vars has changed; premature optimisation galore)
1509  *     - hard-coded paths (array of strings)
1510  *     - xyz filename/suffix/prefix strings (array of strings)
1511  *     - flags (int)
1512  *     - last hash of file/dir stats (int)
1513  *   }
1514  *   (= struct GstPluginDep)
1515  */
1516 
1517 static guint
gst_plugin_ext_dep_get_env_vars_hash(GstPlugin * plugin,GstPluginDep * dep)1518 gst_plugin_ext_dep_get_env_vars_hash (GstPlugin * plugin, GstPluginDep * dep)
1519 {
1520   gchar **e;
1521   guint hash;
1522 
1523   /* there's no deeper logic to what we do here; all we want to know (when
1524    * checking if the plugin needs to be rescanned) is whether the content of
1525    * one of the environment variables in the list is different from when it
1526    * was last scanned */
1527   hash = 0;
1528   for (e = dep->env_vars; e != NULL && *e != NULL; ++e) {
1529     const gchar *val;
1530     gchar env_var[256];
1531 
1532     /* order matters: "val",NULL needs to yield a different hash than
1533      * NULL,"val", so do a shift here whether the var is set or not */
1534     hash = hash << 5;
1535 
1536     /* want environment variable at beginning of string */
1537     if (!g_ascii_isalnum (**e)) {
1538       GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1539           "variable string: %s", *e);
1540       continue;
1541     }
1542 
1543     /* user is allowed to specify e.g. "HOME/.pitivi/plugins" */
1544     g_strlcpy (env_var, *e, sizeof (env_var));
1545     g_strdelimit (env_var, "/\\", '\0');
1546 
1547     if ((val = g_getenv (env_var)))
1548       hash += g_str_hash (val);
1549   }
1550 
1551   return hash;
1552 }
1553 
1554 gboolean
_priv_plugin_deps_env_vars_changed(GstPlugin * plugin)1555 _priv_plugin_deps_env_vars_changed (GstPlugin * plugin)
1556 {
1557   GList *l;
1558 
1559   for (l = plugin->priv->deps; l != NULL; l = l->next) {
1560     GstPluginDep *dep = l->data;
1561 
1562     if (dep->env_hash != gst_plugin_ext_dep_get_env_vars_hash (plugin, dep))
1563       return TRUE;
1564   }
1565 
1566   return FALSE;
1567 }
1568 
1569 static void
gst_plugin_ext_dep_extract_env_vars_paths(GstPlugin * plugin,GstPluginDep * dep,GQueue * paths)1570 gst_plugin_ext_dep_extract_env_vars_paths (GstPlugin * plugin,
1571     GstPluginDep * dep, GQueue * paths)
1572 {
1573   gchar **evars;
1574 
1575   for (evars = dep->env_vars; evars != NULL && *evars != NULL; ++evars) {
1576     const gchar *e;
1577     gchar **components;
1578 
1579     /* want environment variable at beginning of string */
1580     if (!g_ascii_isalnum (**evars)) {
1581       GST_WARNING_OBJECT (plugin, "string prefix is not a valid environment "
1582           "variable string: %s", *evars);
1583       continue;
1584     }
1585 
1586     /* user is allowed to specify e.g. "HOME/.pitivi/plugins", which we want to
1587      * split into the env_var name component and the path component */
1588     components = g_strsplit_set (*evars, "/\\", 2);
1589     g_assert (components != NULL);
1590 
1591     e = g_getenv (components[0]);
1592     GST_LOG_OBJECT (plugin, "expanding %s = '%s' (path suffix: %s)",
1593         components[0], GST_STR_NULL (e), GST_STR_NULL (components[1]));
1594 
1595     if (components[1] != NULL) {
1596       g_strdelimit (components[1], "/\\", G_DIR_SEPARATOR);
1597     }
1598 
1599     if (e != NULL && *e != '\0') {
1600       gchar **arr;
1601       guint i;
1602 
1603       arr = g_strsplit (e, G_SEARCHPATH_SEPARATOR_S, -1);
1604 
1605       for (i = 0; arr != NULL && arr[i] != NULL; ++i) {
1606         gchar *full_path;
1607 
1608         if (!g_path_is_absolute (arr[i])) {
1609           GST_INFO_OBJECT (plugin, "ignoring environment variable content '%s'"
1610               ": either not an absolute path or not a path at all", arr[i]);
1611           continue;
1612         }
1613 
1614         if (components[1] != NULL) {
1615           full_path = g_build_filename (arr[i], components[1], NULL);
1616         } else {
1617           full_path = g_strdup (arr[i]);
1618         }
1619 
1620         if (!g_queue_find_custom (paths, full_path, (GCompareFunc) strcmp)) {
1621           GST_LOG_OBJECT (plugin, "path: '%s'", full_path);
1622           g_queue_push_tail (paths, full_path);
1623           full_path = NULL;
1624         } else {
1625           GST_LOG_OBJECT (plugin, "path: '%s' (duplicate,ignoring)", full_path);
1626           g_free (full_path);
1627         }
1628       }
1629 
1630       g_strfreev (arr);
1631     }
1632 
1633     g_strfreev (components);
1634   }
1635 
1636   GST_LOG_OBJECT (plugin, "Extracted %d paths from environment", paths->length);
1637 }
1638 
1639 static guint
gst_plugin_ext_dep_get_hash_from_stat_entry(GStatBuf * s)1640 gst_plugin_ext_dep_get_hash_from_stat_entry (GStatBuf * s)
1641 {
1642 #ifdef S_IFBLK
1643   if (!(s->st_mode & (S_IFDIR | S_IFREG | S_IFBLK | S_IFCHR)))
1644 #else
1645   /* MSVC does not have S_IFBLK */
1646   if (!(s->st_mode & (S_IFDIR | S_IFREG | S_IFCHR)))
1647 #endif
1648     return (guint) - 1;
1649 
1650   /* completely random formula */
1651   return ((s->st_size << 3) + (s->st_mtime << 5)) ^ s->st_ctime;
1652 }
1653 
1654 static gboolean
gst_plugin_ext_dep_direntry_matches(GstPlugin * plugin,const gchar * entry,const gchar ** filenames,GstPluginDependencyFlags flags)1655 gst_plugin_ext_dep_direntry_matches (GstPlugin * plugin, const gchar * entry,
1656     const gchar ** filenames, GstPluginDependencyFlags flags)
1657 {
1658   /* no filenames specified, match all entries for now (could probably
1659    * optimise by just taking the dir stat hash or so) */
1660   if (filenames == NULL || *filenames == NULL || **filenames == '\0')
1661     return TRUE;
1662 
1663   while (*filenames != NULL) {
1664     /* suffix match? */
1665     if (((flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX)) &&
1666         g_str_has_suffix (entry, *filenames)) {
1667       return TRUE;
1668     } else if (((flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_PREFIX)) &&
1669         g_str_has_prefix (entry, *filenames)) {
1670       return TRUE;
1671       /* else it's an exact match that's needed */
1672     } else if (strcmp (entry, *filenames) == 0) {
1673       return TRUE;
1674     }
1675     GST_LOG ("%s does not match %s, flags=0x%04x", entry, *filenames, flags);
1676     ++filenames;
1677   }
1678   return FALSE;
1679 }
1680 
1681 static guint
gst_plugin_ext_dep_scan_dir_and_match_names(GstPlugin * plugin,const gchar * path,const gchar ** filenames,GstPluginDependencyFlags flags,int depth)1682 gst_plugin_ext_dep_scan_dir_and_match_names (GstPlugin * plugin,
1683     const gchar * path, const gchar ** filenames,
1684     GstPluginDependencyFlags flags, int depth)
1685 {
1686   const gchar *entry;
1687   gboolean recurse_dirs;
1688   GError *err = NULL;
1689   GDir *dir;
1690   guint hash = 0;
1691 
1692   recurse_dirs = ! !(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1693 
1694   dir = g_dir_open (path, 0, &err);
1695   if (dir == NULL) {
1696     GST_DEBUG_OBJECT (plugin, "g_dir_open(%s) failed: %s", path, err->message);
1697     g_error_free (err);
1698     return (guint) - 1;
1699   }
1700 
1701   /* FIXME: we're assuming here that we always get the directory entries in
1702    * the same order, and not in a random order */
1703   while ((entry = g_dir_read_name (dir))) {
1704     gboolean have_match;
1705     GStatBuf s;
1706     gchar *full_path;
1707     guint fhash;
1708 
1709     have_match =
1710         gst_plugin_ext_dep_direntry_matches (plugin, entry, filenames, flags);
1711 
1712     /* avoid the stat if possible */
1713     if (!have_match && !recurse_dirs)
1714       continue;
1715 
1716     full_path = g_build_filename (path, entry, NULL);
1717     if (g_stat (full_path, &s) < 0) {
1718       fhash = (guint) - 1;
1719       GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1720           g_strerror (errno));
1721     } else if (have_match) {
1722       fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1723       GST_LOG_OBJECT (plugin, "stat: %s (result: %u)", full_path, fhash);
1724     } else if ((s.st_mode & (S_IFDIR))) {
1725       fhash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, full_path,
1726           filenames, flags, depth + 1);
1727     } else {
1728       /* it's not a name match, we want to recurse, but it's not a directory */
1729       g_free (full_path);
1730       continue;
1731     }
1732 
1733     hash = hash + fhash;
1734     g_free (full_path);
1735   }
1736 
1737   g_dir_close (dir);
1738   return hash;
1739 }
1740 
1741 static guint
gst_plugin_ext_dep_scan_path_with_filenames(GstPlugin * plugin,const gchar * path,const gchar ** filenames,GstPluginDependencyFlags flags)1742 gst_plugin_ext_dep_scan_path_with_filenames (GstPlugin * plugin,
1743     const gchar * path, const gchar ** filenames,
1744     GstPluginDependencyFlags flags)
1745 {
1746   const gchar *empty_filenames[] = { "", NULL };
1747   gboolean recurse_into_dirs, partial_names = FALSE;
1748   guint i, hash = 0;
1749 
1750   /* to avoid special-casing below (FIXME?) */
1751   if (filenames == NULL || *filenames == NULL)
1752     filenames = empty_filenames;
1753 
1754   recurse_into_dirs = ! !(flags & GST_PLUGIN_DEPENDENCY_FLAG_RECURSE);
1755 
1756   if ((flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_SUFFIX) ||
1757       (flags & GST_PLUGIN_DEPENDENCY_FLAG_FILE_NAME_IS_PREFIX))
1758     partial_names = TRUE;
1759 
1760   /* if we can construct the exact paths to check with the data we have, just
1761    * stat them one by one; this is more efficient than opening the directory
1762    * and going through each entry to see if it matches one of our filenames. */
1763   if (!recurse_into_dirs && !partial_names) {
1764     for (i = 0; filenames[i] != NULL; ++i) {
1765       GStatBuf s;
1766       gchar *full_path;
1767       guint fhash;
1768 
1769       full_path = g_build_filename (path, filenames[i], NULL);
1770       if (g_stat (full_path, &s) < 0) {
1771         fhash = (guint) - 1;
1772         GST_LOG_OBJECT (plugin, "stat: %s (error: %s)", full_path,
1773             g_strerror (errno));
1774       } else {
1775         fhash = gst_plugin_ext_dep_get_hash_from_stat_entry (&s);
1776         GST_LOG_OBJECT (plugin, "stat: %s (result: %08x)", full_path, fhash);
1777       }
1778       hash += fhash;
1779       g_free (full_path);
1780     }
1781   } else {
1782     hash = gst_plugin_ext_dep_scan_dir_and_match_names (plugin, path,
1783         filenames, flags, 0);
1784   }
1785 
1786   return hash;
1787 }
1788 
1789 static guint
gst_plugin_ext_dep_get_stat_hash(GstPlugin * plugin,GstPluginDep * dep)1790 gst_plugin_ext_dep_get_stat_hash (GstPlugin * plugin, GstPluginDep * dep)
1791 {
1792   gboolean paths_are_default_only;
1793   gboolean paths_are_relative_to_exe;
1794   GQueue scan_paths = G_QUEUE_INIT;
1795   guint scan_hash = 0;
1796   gchar *path;
1797 
1798   GST_LOG_OBJECT (plugin, "start");
1799 
1800   paths_are_default_only =
1801       dep->flags & GST_PLUGIN_DEPENDENCY_FLAG_PATHS_ARE_DEFAULT_ONLY;
1802   paths_are_relative_to_exe =
1803       dep->flags & GST_PLUGIN_DEPENDENCY_FLAG_PATHS_ARE_RELATIVE_TO_EXE;
1804 
1805   gst_plugin_ext_dep_extract_env_vars_paths (plugin, dep, &scan_paths);
1806 
1807   if (g_queue_is_empty (&scan_paths) || !paths_are_default_only) {
1808     gchar **paths;
1809 
1810     for (paths = dep->paths; paths != NULL && *paths != NULL; ++paths) {
1811       const gchar *path = *paths;
1812       gchar *full_path;
1813 
1814       if (paths_are_relative_to_exe && !g_path_is_absolute (path)) {
1815         gchar *appdir;
1816 
1817         if (!_gst_executable_path) {
1818           GST_FIXME_OBJECT (plugin,
1819               "Path dependency %s relative to executable path but could not retrieve executable path",
1820               path);
1821           continue;
1822         }
1823         appdir = g_path_get_dirname (_gst_executable_path);
1824         full_path = g_build_filename (appdir, path, NULL);
1825         g_free (appdir);
1826       } else {
1827         full_path = g_strdup (path);
1828       }
1829 
1830       if (!g_queue_find_custom (&scan_paths, full_path, (GCompareFunc) strcmp)) {
1831         GST_LOG_OBJECT (plugin, "path: '%s'", full_path);
1832         g_queue_push_tail (&scan_paths, full_path);
1833       } else {
1834         GST_LOG_OBJECT (plugin, "path: '%s' (duplicate, ignoring)", full_path);
1835         g_free (full_path);
1836       }
1837     }
1838   }
1839 
1840   while ((path = g_queue_pop_head (&scan_paths))) {
1841     scan_hash += gst_plugin_ext_dep_scan_path_with_filenames (plugin, path,
1842         (const gchar **) dep->names, dep->flags);
1843     g_free (path);
1844   }
1845 
1846   GST_LOG_OBJECT (plugin, "done, scan_hash: %08x", scan_hash);
1847   return scan_hash;
1848 }
1849 
1850 gboolean
_priv_plugin_deps_files_changed(GstPlugin * plugin)1851 _priv_plugin_deps_files_changed (GstPlugin * plugin)
1852 {
1853   GList *l;
1854 
1855   for (l = plugin->priv->deps; l != NULL; l = l->next) {
1856     GstPluginDep *dep = l->data;
1857 
1858     if (dep->stat_hash != gst_plugin_ext_dep_get_stat_hash (plugin, dep))
1859       return TRUE;
1860   }
1861 
1862   return FALSE;
1863 }
1864 
1865 static void
gst_plugin_ext_dep_free(GstPluginDep * dep)1866 gst_plugin_ext_dep_free (GstPluginDep * dep)
1867 {
1868   g_strfreev (dep->env_vars);
1869   g_strfreev (dep->paths);
1870   g_strfreev (dep->names);
1871   g_slice_free (GstPluginDep, dep);
1872 }
1873 
1874 static gboolean
gst_plugin_ext_dep_strv_equal(gchar ** arr1,gchar ** arr2)1875 gst_plugin_ext_dep_strv_equal (gchar ** arr1, gchar ** arr2)
1876 {
1877   if (arr1 == arr2)
1878     return TRUE;
1879   if (arr1 == NULL || arr2 == NULL)
1880     return FALSE;
1881   for (; *arr1 != NULL && *arr2 != NULL; ++arr1, ++arr2) {
1882     if (strcmp (*arr1, *arr2) != 0)
1883       return FALSE;
1884   }
1885   return (*arr1 == *arr2);
1886 }
1887 
1888 static gboolean
gst_plugin_ext_dep_equals(GstPluginDep * dep,const gchar ** env_vars,const gchar ** paths,const gchar ** names,GstPluginDependencyFlags flags)1889 gst_plugin_ext_dep_equals (GstPluginDep * dep, const gchar ** env_vars,
1890     const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1891 {
1892   if (dep->flags != flags)
1893     return FALSE;
1894 
1895   return gst_plugin_ext_dep_strv_equal (dep->env_vars, (gchar **) env_vars) &&
1896       gst_plugin_ext_dep_strv_equal (dep->paths, (gchar **) paths) &&
1897       gst_plugin_ext_dep_strv_equal (dep->names, (gchar **) names);
1898 }
1899 
1900 /**
1901  * gst_plugin_add_dependency:
1902  * @plugin: a #GstPlugin
1903  * @env_vars: (allow-none) (array zero-terminated=1): %NULL-terminated array of environment variables affecting the
1904  *     feature set of the plugin (e.g. an environment variable containing
1905  *     paths where to look for additional modules/plugins of a library),
1906  *     or %NULL. Environment variable names may be followed by a path component
1907  *      which will be added to the content of the environment variable, e.g.
1908  *      "HOME/.mystuff/plugins".
1909  * @paths: (allow-none) (array zero-terminated=1): %NULL-terminated array of directories/paths where dependent files
1910  *     may be, or %NULL.
1911  * @names: (allow-none) (array zero-terminated=1): %NULL-terminated array of file names (or file name suffixes,
1912  *     depending on @flags) to be used in combination with the paths from
1913  *     @paths and/or the paths extracted from the environment variables in
1914  *     @env_vars, or %NULL.
1915  * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1916  *
1917  * Make GStreamer aware of external dependencies which affect the feature
1918  * set of this plugin (ie. the elements or typefinders associated with it).
1919  *
1920  * GStreamer will re-inspect plugins with external dependencies whenever any
1921  * of the external dependencies change. This is useful for plugins which wrap
1922  * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1923  * library and makes visualisations available as GStreamer elements, or a
1924  * codec loader which exposes elements and/or caps dependent on what external
1925  * codec libraries are currently installed.
1926  */
1927 void
gst_plugin_add_dependency(GstPlugin * plugin,const gchar ** env_vars,const gchar ** paths,const gchar ** names,GstPluginDependencyFlags flags)1928 gst_plugin_add_dependency (GstPlugin * plugin, const gchar ** env_vars,
1929     const gchar ** paths, const gchar ** names, GstPluginDependencyFlags flags)
1930 {
1931   GstPluginDep *dep;
1932   GList *l;
1933 
1934   g_return_if_fail (GST_IS_PLUGIN (plugin));
1935 
1936   if ((env_vars == NULL || env_vars[0] == NULL) &&
1937       (paths == NULL || paths[0] == NULL)) {
1938     GST_DEBUG_OBJECT (plugin,
1939         "plugin registered empty dependency set. Ignoring");
1940     return;
1941   }
1942 
1943   for (l = plugin->priv->deps; l != NULL; l = l->next) {
1944     if (gst_plugin_ext_dep_equals (l->data, env_vars, paths, names, flags)) {
1945       GST_LOG_OBJECT (plugin, "dependency already registered");
1946       return;
1947     }
1948   }
1949 
1950   dep = g_slice_new (GstPluginDep);
1951 
1952   dep->env_vars = g_strdupv ((gchar **) env_vars);
1953   dep->paths = g_strdupv ((gchar **) paths);
1954   dep->names = g_strdupv ((gchar **) names);
1955   dep->flags = flags;
1956 
1957   dep->env_hash = gst_plugin_ext_dep_get_env_vars_hash (plugin, dep);
1958   dep->stat_hash = gst_plugin_ext_dep_get_stat_hash (plugin, dep);
1959 
1960   plugin->priv->deps = g_list_append (plugin->priv->deps, dep);
1961 
1962   GST_DEBUG_OBJECT (plugin, "added dependency:");
1963   for (; env_vars != NULL && *env_vars != NULL; ++env_vars)
1964     GST_DEBUG_OBJECT (plugin, " evar: %s", *env_vars);
1965   for (; paths != NULL && *paths != NULL; ++paths)
1966     GST_DEBUG_OBJECT (plugin, " path: %s", *paths);
1967   for (; names != NULL && *names != NULL; ++names)
1968     GST_DEBUG_OBJECT (plugin, " name: %s", *names);
1969 }
1970 
1971 /**
1972  * gst_plugin_add_dependency_simple:
1973  * @plugin: the #GstPlugin
1974  * @env_vars: (allow-none): one or more environment variables (separated by ':', ';' or ','),
1975  *      or %NULL. Environment variable names may be followed by a path component
1976  *      which will be added to the content of the environment variable, e.g.
1977  *      "HOME/.mystuff/plugins:MYSTUFF_PLUGINS_PATH"
1978  * @paths: (allow-none): one ore more directory paths (separated by ':' or ';' or ','),
1979  *      or %NULL. Example: "/usr/lib/mystuff/plugins"
1980  * @names: (allow-none): one or more file names or file name suffixes (separated by commas),
1981  *      or %NULL
1982  * @flags: optional flags, or #GST_PLUGIN_DEPENDENCY_FLAG_NONE
1983  *
1984  * Make GStreamer aware of external dependencies which affect the feature
1985  * set of this plugin (ie. the elements or typefinders associated with it).
1986  *
1987  * GStreamer will re-inspect plugins with external dependencies whenever any
1988  * of the external dependencies change. This is useful for plugins which wrap
1989  * other plugin systems, e.g. a plugin which wraps a plugin-based visualisation
1990  * library and makes visualisations available as GStreamer elements, or a
1991  * codec loader which exposes elements and/or caps dependent on what external
1992  * codec libraries are currently installed.
1993  *
1994  * Convenience wrapper function for gst_plugin_add_dependency() which
1995  * takes simple strings as arguments instead of string arrays, with multiple
1996  * arguments separated by predefined delimiters (see above).
1997  */
1998 void
gst_plugin_add_dependency_simple(GstPlugin * plugin,const gchar * env_vars,const gchar * paths,const gchar * names,GstPluginDependencyFlags flags)1999 gst_plugin_add_dependency_simple (GstPlugin * plugin,
2000     const gchar * env_vars, const gchar * paths, const gchar * names,
2001     GstPluginDependencyFlags flags)
2002 {
2003   gchar **a_evars = NULL;
2004   gchar **a_paths = NULL;
2005   gchar **a_names = NULL;
2006 
2007   if (env_vars)
2008     a_evars = g_strsplit_set (env_vars, ":;,", -1);
2009   if (paths)
2010     a_paths = g_strsplit_set (paths, ":;,", -1);
2011   if (names)
2012     a_names = g_strsplit_set (names, ",", -1);
2013 
2014   gst_plugin_add_dependency (plugin, (const gchar **) a_evars,
2015       (const gchar **) a_paths, (const gchar **) a_names, flags);
2016 
2017   if (a_evars)
2018     g_strfreev (a_evars);
2019   if (a_paths)
2020     g_strfreev (a_paths);
2021   if (a_names)
2022     g_strfreev (a_names);
2023 }
2024