]> git.sesse.net Git - vlc/blob - src/modules/bank.c
Revert "deinterlace: add basic support for YUY2 and NV12 (fixes #2206)"
[vlc] / src / modules / bank.c
1 /*****************************************************************************
2  * bank.c : Modules list
3  *****************************************************************************
4  * Copyright (C) 2001-2011 VLC authors and VideoLAN
5  *
6  * Authors: Sam Hocevar <sam@zoy.org>
7  *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
8  *          Hans-Peter Jansen <hpj@urpla.net>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *          RĂ©mi Denis-Courmont
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU Lesser General Public License as published by
14  * the Free Software Foundation; either version 2.1 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program; if not, write to the Free Software Foundation,
24  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <assert.h>
35
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_modules.h>
43 #include <vlc_fs.h>
44 #include "libvlc.h"
45 #include "config/configuration.h"
46 #include "modules/modules.h"
47
48 static struct
49 {
50     vlc_mutex_t lock;
51     module_t *head;
52     unsigned usage;
53 } modules = { VLC_STATIC_MUTEX, NULL, 0 };
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 #ifdef HAVE_DYNAMIC_PLUGINS
59 static void AllocateAllPlugins (vlc_object_t *);
60 #endif
61 static module_t *module_InitStatic (vlc_plugin_cb);
62
63 static void module_StoreBank (module_t *module)
64 {
65     /*vlc_assert_locked (&modules.lock);*/
66     module->next = modules.head;
67     modules.head = module;
68 }
69
70 #if defined(__ELF__) || !HAVE_DYNAMIC_PLUGINS
71 # ifdef __GNUC__
72 __attribute__((weak))
73 # else
74 #  pragma weak vlc_static_modules
75 # endif
76 extern vlc_plugin_cb vlc_static_modules[];
77
78 static void module_InitStaticModules(void)
79 {
80     if (!vlc_static_modules)
81         return;
82
83     for (unsigned i = 0; vlc_static_modules[i]; i++) {
84         module_t *module = module_InitStatic (vlc_static_modules[i]);
85         if (likely(module != NULL))
86             module_StoreBank (module);
87     }
88 }
89 #else
90 static void module_InitStaticModules(void) { }
91 #endif
92
93 /**
94  * Init bank
95  *
96  * Creates a module bank structure which will be filled later
97  * on with all the modules found.
98  */
99 void module_InitBank (void)
100 {
101     vlc_mutex_lock (&modules.lock);
102
103     if (modules.usage == 0)
104     {
105         /* Fills the module bank structure with the core module infos.
106          * This is very useful as it will allow us to consider the core
107          * library just as another module, and for instance the configuration
108          * options of core will be available in the module bank structure just
109          * as for every other module. */
110         module_t *module = module_InitStatic (vlc_entry__core);
111         if (likely(module != NULL))
112             module_StoreBank (module);
113         config_SortConfig ();
114     }
115     modules.usage++;
116
117     /* We do retain the module bank lock until the plugins are loaded as well.
118      * This is ugly, this staged loading approach is needed: LibVLC gets
119      * some configuration parameters relevant to loading the plugins from
120      * the core (builtin) module. The module bank becomes shared read-only data
121      * once it is ready, so we need to fully serialize initialization.
122      * DO NOT UNCOMMENT the following line unless you managed to squeeze
123      * module_LoadPlugins() before you unlock the mutex. */
124     /*vlc_mutex_unlock (&modules.lock);*/
125 }
126
127 /**
128  * Unloads all unused plugin modules and empties the module
129  * bank in case of success.
130  */
131 void module_EndBank (bool b_plugins)
132 {
133     module_t *head = NULL;
134
135     /* If plugins were _not_ loaded, then the caller still has the bank lock
136      * from module_InitBank(). */
137     if( b_plugins )
138         vlc_mutex_lock (&modules.lock);
139     /*else
140         vlc_assert_locked (&modules.lock); not for static mutexes :( */
141
142     assert (modules.usage > 0);
143     if (--modules.usage == 0)
144     {
145         config_UnsortConfig ();
146         head = modules.head;
147         modules.head = NULL;
148     }
149     vlc_mutex_unlock (&modules.lock);
150
151     while (head != NULL)
152     {
153         module_t *module = head;
154
155         head = module->next;
156 #ifdef HAVE_DYNAMIC_PLUGINS
157         if (module->b_loaded && module->b_unloadable)
158         {
159             module_Unload (module->handle);
160             module->b_loaded = false;
161         }
162 #endif
163         vlc_module_destroy (module);
164     }
165 }
166
167 #undef module_LoadPlugins
168 /**
169  * Loads module descriptions for all available plugins.
170  * Fills the module bank structure with the plugin modules.
171  *
172  * \param p_this vlc object structure
173  * \return total number of modules in bank after loading all plug-ins
174  */
175 size_t module_LoadPlugins (vlc_object_t *obj)
176 {
177     /*vlc_assert_locked (&modules.lock); not for static mutexes :( */
178
179     if (modules.usage == 1)
180     {
181         module_InitStaticModules ();
182 #ifdef HAVE_DYNAMIC_PLUGINS
183         msg_Dbg (obj, "searching plug-in modules");
184         AllocateAllPlugins (obj);
185 #endif
186         config_UnsortConfig ();
187         config_SortConfig ();
188     }
189     vlc_mutex_unlock (&modules.lock);
190
191     size_t count;
192     module_t **list = module_list_get (&count);
193     module_list_free (list);
194     msg_Dbg (obj, "plug-ins loaded: %zu modules", count);
195     return count;
196 }
197
198 /**
199  * Frees the flat list of VLC modules.
200  * @param list list obtained by module_list_get()
201  * @param length number of items on the list
202  * @return nothing.
203  */
204 void module_list_free (module_t **list)
205 {
206     free (list);
207 }
208
209 /**
210  * Gets the flat list of VLC modules.
211  * @param n [OUT] pointer to the number of modules
212  * @return table of module pointers (release with module_list_free()),
213  *         or NULL in case of error (in that case, *n is zeroed).
214  */
215 module_t **module_list_get (size_t *n)
216 {
217     module_t **tab = NULL;
218     size_t i = 0;
219
220     assert (n != NULL);
221
222     for (module_t *mod = modules.head; mod; mod = mod->next)
223     {
224          module_t **nt;
225          nt  = realloc (tab, (i + 1 + mod->submodule_count) * sizeof (*tab));
226          if (unlikely(nt == NULL))
227          {
228              free (tab);
229              *n = 0;
230              return NULL;
231          }
232
233          tab = nt;
234          tab[i++] = mod;
235          for (module_t *subm = mod->submodule; subm; subm = subm->next)
236              tab[i++] = subm;
237     }
238     *n = i;
239     return tab;
240 }
241
242 static int modulecmp (const void *a, const void *b)
243 {
244     const module_t *const *ma = a, *const *mb = b;
245     /* Note that qsort() uses _ascending_ order,
246      * so the smallest module is the one with the biggest score. */
247     return (*mb)->i_score - (*ma)->i_score;
248 }
249
250 /**
251  * Builds a sorted list of all VLC modules with a given capability.
252  * The list is sorted from the highest module score to the lowest.
253  * @param list pointer to the table of modules [OUT]
254  * @param cap capability of modules to look for
255  * @return the number of matching found, or -1 on error (*list is then NULL).
256  * @note *list must be freed with module_list_free().
257  */
258 ssize_t module_list_cap (module_t ***restrict list, const char *cap)
259 {
260     /* TODO: This is quite inefficient. List should be sorted by capability. */
261     ssize_t n = 0;
262
263     assert (list != NULL);
264
265     for (module_t *mod = modules.head; mod != NULL; mod = mod->next)
266     {
267          if (module_provides (mod, cap))
268              n++;
269          for (module_t *subm = mod->submodule; subm != NULL; subm = subm->next)
270              if (module_provides (subm, cap))
271                  n++;
272     }
273
274     module_t **tab = malloc (sizeof (*tab) * n);
275     *list = tab;
276     if (unlikely(tab == NULL))
277         return -1;
278
279     for (module_t *mod = modules.head; mod != NULL; mod = mod->next)
280     {
281          if (module_provides (mod, cap))
282              *(tab++)= mod;
283          for (module_t *subm = mod->submodule; subm != NULL; subm = subm->next)
284              if (module_provides (subm, cap))
285                  *(tab++) = subm;
286     }
287
288     assert (tab == *list + n);
289     qsort (*list, n, sizeof (*tab), modulecmp);
290     return n;
291 }
292
293 #ifdef HAVE_DYNAMIC_PLUGINS
294 typedef enum { CACHE_USE, CACHE_RESET, CACHE_IGNORE } cache_mode_t;
295
296 static void AllocatePluginPath (vlc_object_t *, const char *, cache_mode_t);
297
298 /**
299  * Enumerates all dynamic plug-ins that can be found.
300  *
301  * This function will recursively browse the default plug-ins directory and any
302  * directory listed in the VLC_PLUGIN_PATH environment variable.
303  * For performance reasons, a cache is normally used so that plug-in shared
304  * objects do not need to loaded and linked into the process.
305  */
306 static void AllocateAllPlugins (vlc_object_t *p_this)
307 {
308     char *paths;
309     cache_mode_t mode;
310
311     if( !var_InheritBool( p_this, "plugins-cache" ) )
312         mode = CACHE_IGNORE;
313     else if( var_InheritBool( p_this, "reset-plugins-cache" ) )
314         mode = CACHE_RESET;
315     else
316         mode = CACHE_USE;
317
318 #if VLC_WINSTORE_APP
319     /* Windows Store Apps can not load external plugins with absolute paths. */
320     AllocatePluginPath (p_this, "plugins", mode);
321 #else
322     /* Contruct the special search path for system that have a relocatable
323      * executable. Set it to <vlc path>/plugins. */
324     char *vlcpath = config_GetLibDir ();
325     if (likely(vlcpath != NULL)
326      && likely(asprintf (&paths, "%s" DIR_SEP "plugins", vlcpath) != -1))
327     {
328         AllocatePluginPath (p_this, paths, mode);
329         free( paths );
330     }
331     free (vlcpath);
332 #endif /* VLC_WINSTORE_APP */
333
334     /* If the user provided a plugin path, we add it to the list */
335     paths = getenv( "VLC_PLUGIN_PATH" );
336     if( paths == NULL )
337         return;
338
339     paths = strdup( paths ); /* don't harm the environment ! :) */
340     if( unlikely(paths == NULL) )
341         return;
342
343     for( char *buf, *path = strtok_r( paths, PATH_SEP, &buf );
344          path != NULL;
345          path = strtok_r( NULL, PATH_SEP, &buf ) )
346         AllocatePluginPath (p_this, path, mode);
347
348     free( paths );
349 }
350
351 typedef struct module_bank
352 {
353     vlc_object_t *obj;
354     const char   *base;
355     cache_mode_t  mode;
356
357     size_t         i_cache;
358     module_cache_t *cache;
359
360     int            i_loaded_cache;
361     module_cache_t *loaded_cache;
362 } module_bank_t;
363
364 static void AllocatePluginDir (module_bank_t *, unsigned,
365                                const char *, const char *);
366
367 /**
368  * Scans for plug-ins within a file system hierarchy.
369  * \param path base directory to browse
370  */
371 static void AllocatePluginPath (vlc_object_t *p_this, const char *path,
372                                 cache_mode_t mode)
373 {
374     module_bank_t bank;
375     module_cache_t *cache = NULL;
376     size_t count = 0;
377
378     switch( mode )
379     {
380         case CACHE_USE:
381             count = CacheLoad( p_this, path, &cache );
382             break;
383         case CACHE_RESET:
384             CacheDelete( p_this, path );
385             break;
386         case CACHE_IGNORE:
387             msg_Dbg( p_this, "ignoring plugins cache file" );
388     }
389
390     msg_Dbg( p_this, "recursively browsing `%s'", path );
391
392     bank.obj = p_this;
393     bank.base = path;
394     bank.mode = mode;
395     bank.cache = NULL;
396     bank.i_cache = 0;
397     bank.loaded_cache = cache;
398     bank.i_loaded_cache = count;
399
400     /* Don't go deeper than 5 subdirectories */
401     AllocatePluginDir (&bank, 5, path, NULL);
402
403     switch( mode )
404     {
405         case CACHE_USE:
406             /* Discard unmatched cache entries */
407             for( size_t i = 0; i < count; i++ )
408             {
409                 if (cache[i].p_module != NULL)
410                    vlc_module_destroy (cache[i].p_module);
411                 free (cache[i].path);
412             }
413             free( cache );
414         case CACHE_RESET:
415             CacheSave (p_this, path, bank.cache, bank.i_cache);
416         case CACHE_IGNORE:
417             break;
418     }
419 }
420
421 static int AllocatePluginFile (module_bank_t *, const char *,
422                                const char *, const struct stat *);
423
424 /**
425  * Recursively browses a directory to look for plug-ins.
426  */
427 static void AllocatePluginDir (module_bank_t *bank, unsigned maxdepth,
428                                const char *absdir, const char *reldir)
429 {
430     if (maxdepth == 0)
431         return;
432     maxdepth--;
433
434     DIR *dh = vlc_opendir (absdir);
435     if (dh == NULL)
436         return;
437
438     /* Parse the directory and try to load all files it contains. */
439     for (;;)
440     {
441         char *file = vlc_readdir (dh), *relpath = NULL, *abspath = NULL;
442         if (file == NULL)
443             break;
444
445         /* Skip ".", ".." */
446         if (!strcmp (file, ".") || !strcmp (file, ".."))
447             continue;
448
449         /* Compute path relative to plug-in base directory */
450         if (reldir != NULL)
451         {
452             if (asprintf (&relpath, "%s"DIR_SEP"%s", reldir, file) == -1)
453                 relpath = NULL;
454         }
455         else
456             relpath = strdup (file);
457         if (unlikely(relpath == NULL))
458             continue;
459
460         /* Compute absolute path */
461         if (asprintf (&abspath, "%s"DIR_SEP"%s", bank->base, relpath) == -1)
462         {
463             abspath = NULL;
464             goto skip;
465         }
466
467         struct stat st;
468         if (vlc_stat (abspath, &st) == -1)
469             goto skip;
470
471         if (S_ISREG (st.st_mode))
472         {
473             static const char prefix[] = "lib";
474             static const char suffix[] = "_plugin"LIBEXT;
475             size_t len = strlen (file);
476
477 #ifndef __OS2__
478             /* Check that file matches the "lib*_plugin"LIBEXT pattern */
479             if (len > strlen (suffix)
480              && !strncmp (file, prefix, strlen (prefix))
481              && !strcmp (file + len - strlen (suffix), suffix))
482 #else
483             /* We load all the files ending with LIBEXT on OS/2,
484              * because OS/2 has a 8.3 length limitation for DLL name */
485             if (len > strlen (LIBEXT)
486              && !strcasecmp (file + len - strlen (LIBEXT), LIBEXT))
487 #endif
488                 AllocatePluginFile (bank, abspath, relpath, &st);
489         }
490         else if (S_ISDIR (st.st_mode))
491             /* Recurse into another directory */
492             AllocatePluginDir (bank, maxdepth, abspath, relpath);
493     skip:
494         free (relpath);
495         free (abspath);
496     }
497     closedir (dh);
498 }
499
500 static module_t *module_InitDynamic (vlc_object_t *, const char *, bool);
501
502 /**
503  * Scans a plug-in from a file.
504  */
505 static int AllocatePluginFile (module_bank_t *bank, const char *abspath,
506                                const char *relpath, const struct stat *st)
507 {
508     module_t *module = NULL;
509
510     /* Check our plugins cache first then load plugin if needed */
511     if (bank->mode == CACHE_USE)
512     {
513         module = CacheFind (bank->loaded_cache, bank->i_loaded_cache,
514                             relpath, st);
515         if (module != NULL)
516         {
517             module->psz_filename = strdup (abspath);
518             if (unlikely(module->psz_filename == NULL))
519             {
520                 vlc_module_destroy (module);
521                 module = NULL;
522             }
523         }
524     }
525     if (module == NULL)
526         module = module_InitDynamic (bank->obj, abspath, true);
527     if (module == NULL)
528         return -1;
529
530     /* We have not already scanned and inserted this module */
531     assert (module->next == NULL);
532
533     /* Unload plugin until we really need it */
534     if (module->b_loaded && module->b_unloadable)
535     {
536         module_Unload (module->handle);
537         module->b_loaded = false;
538     }
539
540     /* For now we force loading if the module's config contains callbacks.
541      * Could be optimized by adding an API call.*/
542     for (size_t n = module->confsize, i = 0; i < n; i++)
543          if (module->p_config[i].list_count == 0
544           && (module->p_config[i].list.psz_cb != NULL || module->p_config[i].list.i_cb != NULL))
545          {
546              /* !unloadable not allowed for plugins with callbacks */
547              vlc_module_destroy (module);
548              module = module_InitDynamic (bank->obj, abspath, false);
549              if (unlikely(module == NULL))
550                  return -1;
551              break;
552          }
553
554     module_StoreBank (module);
555
556     if (bank->mode != CACHE_IGNORE) /* Add entry to cache */
557         CacheAdd (&bank->cache, &bank->i_cache, relpath, st, module);
558     /* TODO: deal with errors */
559     return  0;
560 }
561
562 #ifdef __OS2__
563 #   define EXTERN_PREFIX "_"
564 #else
565 #   define EXTERN_PREFIX
566 #endif
567
568
569 /**
570  * Loads a dynamically-linked plug-in into memory and initialize it.
571  *
572  * The module can then be handled by module_need() and module_unneed().
573  *
574  * \param path file path of the shared object
575  * \param fast whether to optimize loading for speed or safety
576  *             (fast is used when the plug-in is registered but not used)
577  */
578 static module_t *module_InitDynamic (vlc_object_t *obj, const char *path,
579                                      bool fast)
580 {
581     module_handle_t handle;
582
583     if (module_Load (obj, path, &handle, fast))
584         return NULL;
585
586     /* Try to resolve the symbol */
587     static const char entry_name[] = EXTERN_PREFIX "vlc_entry" MODULE_SUFFIX;
588     vlc_plugin_cb entry =
589         (vlc_plugin_cb) module_Lookup (handle, entry_name);
590     if (entry == NULL)
591     {
592         msg_Warn (obj, "cannot find plug-in entry point in %s", path);
593         goto error;
594     }
595
596     /* We can now try to call the symbol */
597     module_t *module = vlc_plugin_describe (entry);
598     if (unlikely(module == NULL))
599     {
600         /* With a well-written module we shouldn't have to print an
601          * additional error message here, but just make sure. */
602         msg_Err (obj, "cannot initialize plug-in %s", path);
603         goto error;
604     }
605
606     module->psz_filename = strdup (path);
607     if (unlikely(module->psz_filename == NULL))
608     {
609         vlc_module_destroy (module);
610         goto error;
611     }
612     module->handle = handle;
613     module->b_loaded = true;
614     return module;
615 error:
616     module_Unload( handle );
617     return NULL;
618 }
619 #endif /* HAVE_DYNAMIC_PLUGINS */
620
621 /**
622  * Registers a statically-linked plug-in.
623  */
624 static module_t *module_InitStatic (vlc_plugin_cb entry)
625 {
626     /* Initializes the module */
627     module_t *module = vlc_plugin_describe (entry);
628     if (unlikely(module == NULL))
629         return NULL;
630
631     module->b_loaded = true;
632     module->b_unloadable = false;
633     return module;
634 }
635
636 /**
637  * Makes sure the module is loaded in memory.
638  * \return 0 on success, -1 on failure
639  */
640 int module_Map (vlc_object_t *obj, module_t *module)
641 {
642     static vlc_mutex_t lock = VLC_STATIC_MUTEX;
643
644     if (module->parent != NULL)
645         module = module->parent;
646
647     vlc_mutex_lock(&lock);
648     if (!module->b_loaded)
649     {
650         module_t *uncache;
651
652         assert (module->psz_filename != NULL);
653 #ifdef HAVE_DYNAMIC_PLUGINS
654         uncache = module_InitDynamic (obj, module->psz_filename, false);
655         if (uncache != NULL)
656         {
657             CacheMerge (obj, module, uncache);
658             vlc_module_destroy (uncache);
659         }
660         else
661 #endif
662         {
663             msg_Err (obj, "corrupt module: %s", module->psz_filename);
664             module = NULL;
665         }
666     }
667     vlc_mutex_unlock(&lock);
668     return -(module == NULL);
669 }