modules.c 73.6 KB
Newer Older
Sam Hocevar's avatar
 
Sam Hocevar committed
1
/*****************************************************************************
2
 * modules.c : Builtin and plugin modules management functions
Sam Hocevar's avatar
 
Sam Hocevar committed
3
 *****************************************************************************
4
 * Copyright (C) 2001-2007 the VideoLAN team
5
 * $Id$
Sam Hocevar's avatar
 
Sam Hocevar committed
6
 *
7
 * Authors: Sam Hocevar <sam@zoy.org>
Sam Hocevar's avatar
 
Sam Hocevar committed
8
 *          Ethan C. Baldridge <BaldridgeE@cadmus.com>
Sam Hocevar's avatar
 
Sam Hocevar committed
9
 *          Hans-Peter Jansen <hpj@urpla.net>
10
 *          Gildas Bazin <gbazin@videolan.org>
Sam Hocevar's avatar
 
Sam Hocevar committed
11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Sam Hocevar's avatar
 
Sam Hocevar committed
25 26
 *****************************************************************************/

27 28
#include <vlc/vlc.h>

Sam Hocevar's avatar
 
Sam Hocevar committed
29 30 31 32 33 34
/* Some faulty libcs have a broken struct dirent when _FILE_OFFSET_BITS
 * is set to 64. Don't try to be cleverer. */
#ifdef _FILE_OFFSET_BITS
#undef _FILE_OFFSET_BITS
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
35 36
#include <stdlib.h>                                      /* free(), strtol() */
#include <stdio.h>                                              /* sprintf() */
Sam Hocevar's avatar
 
Sam Hocevar committed
37
#include <string.h>                                              /* strdup() */
Sam Hocevar's avatar
 
Sam Hocevar committed
38

39 40 41 42
#ifdef HAVE_DIRENT_H
#   include <dirent.h>
#endif

43 44 45 46 47 48
#ifdef HAVE_SYS_TYPES_H
#   include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#   include <sys/stat.h>
#endif
49 50 51
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
52

53 54 55
#if !defined(HAVE_DYNAMIC_PLUGINS)
    /* no support for plugins */
#elif defined(HAVE_DL_DYLD)
Sam Hocevar's avatar
Sam Hocevar committed
56 57 58 59 60 61 62 63
#   if defined(HAVE_MACH_O_DYLD_H)
#       include <mach-o/dyld.h>
#   endif
#elif defined(HAVE_DL_BEOS)
#   if defined(HAVE_IMAGE_H)
#       include <image.h>
#   endif
#elif defined(HAVE_DL_WINDOWS)
Sam Hocevar's avatar
Sam Hocevar committed
64
#   include <windows.h>
Sam Hocevar's avatar
Sam Hocevar committed
65 66
#elif defined(HAVE_DL_DLOPEN)
#   if defined(HAVE_DLFCN_H) /* Linux, BSD, Hurd */
Sam Hocevar's avatar
Sam Hocevar committed
67 68
#       include <dlfcn.h>
#   endif
Sam Hocevar's avatar
Sam Hocevar committed
69
#   if defined(HAVE_SYS_DL_H)
Sam Hocevar's avatar
Sam Hocevar committed
70 71
#       include <sys/dl.h>
#   endif
Sam Hocevar's avatar
Sam Hocevar committed
72 73
#elif defined(HAVE_DL_SHL_LOAD)
#   if defined(HAVE_DL_H)
Sam Hocevar's avatar
Sam Hocevar committed
74 75
#       include <dl.h>
#   endif
Sam Hocevar's avatar
 
Sam Hocevar committed
76 77
#endif

78
#include "modules/configuration.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
79

Clément Stenac's avatar
Clément Stenac committed
80
#include "vlc_interface.h"
81 82
#include "vlc_playlist.h"

Clément Stenac's avatar
Clément Stenac committed
83 84 85 86
#include "vlc_stream.h"
#include "vlc_access.h"
#include "vlc_demux.h"

Clément Stenac's avatar
Clément Stenac committed
87 88
#include "vlc_vout.h"
#include "vlc_vout_synchro.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
89

Clément Stenac's avatar
Clément Stenac committed
90
#include "vlc_aout.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
91

Clément Stenac's avatar
Clément Stenac committed
92
#include "vlc_sout.h"
Sigmund Augdal Helberg's avatar
Sigmund Augdal Helberg committed
93
#include "vlc_httpd.h"
94
#include "vlc_acl.h"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
95
#include "vlc_tls.h"
96
#include "vlc_md5.h"
Gildas Bazin's avatar
Gildas Bazin committed
97
#include "vlc_xml.h"
Christophe Mutricy's avatar
Christophe Mutricy committed
98
#include "vlc_url.h"
99

Gildas Bazin's avatar
 
Gildas Bazin committed
100
#include "iso_lang.h"
Clément Stenac's avatar
Clément Stenac committed
101
#include "vlc_charset.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
102

103 104
#include "vlc_block.h"

105 106
#include "vlc_vlm.h"

Clément Stenac's avatar
Clément Stenac committed
107
#include "vlc_image.h"
108
#include "vlc_osd.h"
Clément Stenac's avatar
Clément Stenac committed
109

110
#include "vlc_update.h"
111
#include "vlc_strings.h"
Clément Stenac's avatar
Clément Stenac committed
112
#include "vlc_streaming.h"
113

114 115
#include "modules/modules.h"
#include "modules/builtin.h"
Clément Stenac's avatar
Clément Stenac committed
116 117

#include "vlc_network.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
118

119
#if defined( WIN32 ) || defined( UNDER_CE )
120
    /* Avoid name collisions */
Steve Lhomme's avatar
Steve Lhomme committed
121
#   define LoadModule(a,b,c) LoadVlcModule(a,b,c)
122 123
#endif

Sam Hocevar's avatar
 
Sam Hocevar committed
124 125 126
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
127
#ifdef HAVE_DYNAMIC_PLUGINS
128 129 130 131
static void AllocateAllPlugins  ( vlc_object_t * );
static void AllocatePluginDir   ( vlc_object_t *, const char *, int );
static int  AllocatePluginFile  ( vlc_object_t *, char *, int64_t, int64_t );
static module_t * AllocatePlugin( vlc_object_t *, char * );
Sam Hocevar's avatar
 
Sam Hocevar committed
132
#endif
133
static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
134
static int  DeleteModule ( module_t *, vlc_bool_t );
Sam Hocevar's avatar
 
Sam Hocevar committed
135
#ifdef HAVE_DYNAMIC_PLUGINS
136 137 138
static void   DupModule        ( module_t * );
static void   UndupModule      ( module_t * );
static int    CallEntry        ( module_t * );
139
static int    LoadModule       ( vlc_object_t *, char *, module_handle_t * );
140 141
static void   CloseModule      ( module_handle_t );
static void * GetSymbol        ( module_handle_t, const char * );
142
static void   CacheLoad        ( vlc_object_t * );
143
static int    CacheLoadConfig  ( module_t *, FILE * );
144 145
static void   CacheSave        ( vlc_object_t * );
static void   CacheSaveConfig  ( module_t *, FILE * );
Sam Hocevar's avatar
Sam Hocevar committed
146
static char * CacheName        ( void );
147 148 149
static void   CacheMerge       ( vlc_object_t *, module_t *, module_t * );
static module_cache_t * CacheFind( vlc_object_t *, char *, int64_t, int64_t );

Sam Hocevar's avatar
Sam Hocevar committed
150
#if defined(HAVE_DL_WINDOWS)
151 152
static char * GetWindowsError  ( void );
#endif
153
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
154

155 156
static void module_LoadMain( vlc_object_t *p_this );

157 158 159

/* Sub-version number
 * (only used to avoid breakage in dev version when cache structure changes) */
160
#define CACHE_SUBVERSION_NUM 2
161

Sam Hocevar's avatar
 
Sam Hocevar committed
162 163 164
/*****************************************************************************
 * module_InitBank: create the module bank.
 *****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
165 166
 * This function creates a module bank structure which will be filled later
 * on with all the modules found.
Sam Hocevar's avatar
 
Sam Hocevar committed
167
 *****************************************************************************/
168
void __module_InitBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
169
{
170
    module_bank_t *p_bank = NULL;
171 172
    vlc_value_t  lockval;

173 174
    var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
175
    vlc_mutex_lock( lockval.p_address );
176
    if( p_this->p_libvlc_global->p_module_bank )
177
    {
178
        p_this->p_libvlc_global->p_module_bank->i_usage++;
179
        vlc_mutex_unlock( lockval.p_address );
180
        var_Destroy( p_this->p_libvlc_global, "libvlc" );
181 182 183
        return;
    }
    vlc_mutex_unlock( lockval.p_address );
184
    var_Destroy( p_this->p_libvlc_global, "libvlc" );
185 186

    p_bank = vlc_object_create( p_this, sizeof(module_bank_t) );
187 188
    if( !p_bank )
        return;
189
    p_bank->psz_object_name = "module bank";
190
    p_bank->i_usage = 1;
191
    p_bank->i_cache = p_bank->i_loaded_cache = 0;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
192
    p_bank->pp_cache = p_bank->pp_loaded_cache = NULL;
193 194
    p_bank->b_cache = p_bank->b_cache_dirty =
        p_bank->b_cache_delete = VLC_FALSE;
195

Sam Hocevar's avatar
 
Sam Hocevar committed
196 197 198
    /*
     * Store the symbols to be exported
     */
199
#if defined (HAVE_DYNAMIC_PLUGINS) && !defined (HAVE_SHARED_LIBVLC)
200
    STORE_SYMBOLS( &p_bank->symbols );
201
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
202

203
    /* Everything worked, attach the object */
204 205
    p_this->p_libvlc_global->p_module_bank = p_bank;
    vlc_object_attach( p_bank, p_this->p_libvlc_global );
206

207
    module_LoadMain( p_this );
Gildas Bazin's avatar
 
Gildas Bazin committed
208 209 210
}

/*****************************************************************************
211
 * module_ResetBank: reset the module bank.
Gildas Bazin's avatar
 
Gildas Bazin committed
212
 *****************************************************************************
213 214
 * This function resets the module bank by unloading all unused plugin
 * modules.
Gildas Bazin's avatar
 
Gildas Bazin committed
215
 *****************************************************************************/
216
void __module_ResetBank( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
217
{
218 219
    msg_Err( p_this, "FIXME: module_ResetBank unimplemented" );
    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
220 221
}

Sam Hocevar's avatar
 
Sam Hocevar committed
222
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
223
 * module_EndBank: empty the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
224
 *****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
225
 * This function unloads all unused plugin modules and empties the module
Sam Hocevar's avatar
 
Sam Hocevar committed
226 227
 * bank in case of success.
 *****************************************************************************/
228
void __module_EndBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
229
{
230
    module_t * p_next = NULL;
231
    vlc_value_t lockval;
232

233 234
    var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
235
    vlc_mutex_lock( lockval.p_address );
236
    if( !p_this->p_libvlc_global->p_module_bank )
237 238
    {
        vlc_mutex_unlock( lockval.p_address );
239
        var_Destroy( p_this->p_libvlc_global, "libvlc" );
240 241
        return;
    }
242
    if( --p_this->p_libvlc_global->p_module_bank->i_usage )
243 244
    {
        vlc_mutex_unlock( lockval.p_address );
245
        var_Destroy( p_this->p_libvlc_global, "libvlc" );
246 247 248
        return;
    }
    vlc_mutex_unlock( lockval.p_address );
249
    var_Destroy( p_this->p_libvlc_global, "libvlc" );
Sam Hocevar's avatar
 
Sam Hocevar committed
250

251 252
    config_AutoSaveConfigFile( p_this );

253
#ifdef HAVE_DYNAMIC_PLUGINS
254
#define p_bank p_this->p_libvlc_global->p_module_bank
255 256 257
    if( p_bank->b_cache ) CacheSave( p_this );
    while( p_bank->i_loaded_cache-- )
    {
258 259
        if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
        {
260
            DeleteModule( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->p_module, p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->b_used );
261 262 263 264
            free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache]->psz_file );
            free( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] );
            p_bank->pp_loaded_cache[p_bank->i_loaded_cache] = NULL;
        }
265
    }
266
    if( p_bank->pp_loaded_cache )
267
    {
268
        free( p_bank->pp_loaded_cache );
269 270
        p_bank->pp_loaded_cache = NULL;
    }
271 272 273 274
    while( p_bank->i_cache-- )
    {
        free( p_bank->pp_cache[p_bank->i_cache]->psz_file );
        free( p_bank->pp_cache[p_bank->i_cache] );
275
        p_bank->pp_cache[p_bank->i_cache] = NULL;
276
    }
277
    if( p_bank->pp_cache )
278
    {
279
        free( p_bank->pp_cache );
280 281
        p_bank->pp_cache = NULL;
    }
282
#undef p_bank
283 284
#endif

285
    vlc_object_detach( p_this->p_libvlc_global->p_module_bank );
286

287
    while( p_this->p_libvlc_global->p_module_bank->i_children )
Sam Hocevar's avatar
 
Sam Hocevar committed
288
    {
289
        p_next = (module_t *)p_this->p_libvlc_global->p_module_bank->pp_children[0];
290

291
        if( DeleteModule( p_next, VLC_TRUE ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
292 293
        {
            /* Module deletion failed */
294
            msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
295
                     p_next->psz_object_name );
Sam Hocevar's avatar
 
Sam Hocevar committed
296 297

            /* We just free the module by hand. Niahahahahaha. */
298 299
            vlc_object_detach( p_next );
            vlc_object_destroy( p_next );
Sam Hocevar's avatar
 
Sam Hocevar committed
300 301 302
        }
    }

303 304
    vlc_object_destroy( p_this->p_libvlc_global->p_module_bank );
    p_this->p_libvlc_global->p_module_bank = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
305 306

    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
307 308 309
}

/*****************************************************************************
310
 * module_LoadMain: load the main program info into the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
311
 *****************************************************************************
312 313 314 315
 * This function fills the module bank structure with the main module infos.
 * This is very useful as it will allow us to consider the main program just
 * as another module, and for instance the configuration options of main will
 * be available in the module bank structure just as for every other module.
Sam Hocevar's avatar
 
Sam Hocevar committed
316
 *****************************************************************************/
317
static void module_LoadMain( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
318
{
319 320
    vlc_value_t lockval;

321 322
    var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
323
    vlc_mutex_lock( lockval.p_address );
324
    if( p_this->p_libvlc_global->p_module_bank->b_main )
325 326
    {
        vlc_mutex_unlock( lockval.p_address );
327
        var_Destroy( p_this->p_libvlc_global, "libvlc" );
328 329
        return;
    }
330
    p_this->p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
331
    vlc_mutex_unlock( lockval.p_address );
332
    var_Destroy( p_this->p_libvlc_global, "libvlc" );
333

334
    AllocateBuiltinModule( p_this, vlc_entry__main );
335 336 337 338 339 340 341
}

/*****************************************************************************
 * module_LoadBuiltins: load all modules which we built with.
 *****************************************************************************
 * This function fills the module bank structure with the builtin modules.
 *****************************************************************************/
342
void __module_LoadBuiltins( vlc_object_t * p_this )
343
{
344 345
    vlc_value_t lockval;

346 347
    var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
348
    vlc_mutex_lock( lockval.p_address );
349
    if( p_this->p_libvlc_global->p_module_bank->b_builtins )
350 351
    {
        vlc_mutex_unlock( lockval.p_address );
352
        var_Destroy( p_this->p_libvlc_global, "libvlc" );
353 354
        return;
    }
355
    p_this->p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
356
    vlc_mutex_unlock( lockval.p_address );
357
    var_Destroy( p_this->p_libvlc_global, "libvlc" );
358

359 360 361 362 363 364 365 366 367
    msg_Dbg( p_this, "checking builtin modules" );
    ALLOCATE_ALL_BUILTINS();
}

/*****************************************************************************
 * module_LoadPlugins: load all plugin modules we can find.
 *****************************************************************************
 * This function fills the module bank structure with the plugin modules.
 *****************************************************************************/
368
void __module_LoadPlugins( vlc_object_t * p_this )
369 370
{
#ifdef HAVE_DYNAMIC_PLUGINS
371 372
    vlc_value_t lockval;

373 374
    var_Create( p_this->p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_this->p_libvlc_global, "libvlc", &lockval );
375
    vlc_mutex_lock( lockval.p_address );
376
    if( p_this->p_libvlc_global->p_module_bank->b_plugins )
377 378
    {
        vlc_mutex_unlock( lockval.p_address );
379
        var_Destroy( p_this->p_libvlc_global, "libvlc" );
380 381
        return;
    }
382
    p_this->p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
383
    vlc_mutex_unlock( lockval.p_address );
384
    var_Destroy( p_this->p_libvlc_global, "libvlc" );
385

386
    msg_Dbg( p_this, "checking plugin modules" );
387 388

    if( config_GetInt( p_this, "plugins-cache" ) )
389
        p_this->p_libvlc_global->p_module_bank->b_cache = VLC_TRUE;
390

391 392
    if( p_this->p_libvlc_global->p_module_bank->b_cache ||
        p_this->p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
393

394 395
    AllocateAllPlugins( p_this );
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
396 397 398
}

/*****************************************************************************
399
 * module_Need: return the best module function, given a capability list.
Sam Hocevar's avatar
 
Sam Hocevar committed
400
 *****************************************************************************
401
 * This function returns the module that best fits the asked capabilities.
Sam Hocevar's avatar
 
Sam Hocevar committed
402
 *****************************************************************************/
403
module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
Gildas Bazin's avatar
 
Gildas Bazin committed
404
                          const char *psz_name, vlc_bool_t b_strict )
Sam Hocevar's avatar
 
Sam Hocevar committed
405
{
406
    typedef struct module_list_t module_list_t;
407

408
    struct module_list_t
Sam Hocevar's avatar
 
Sam Hocevar committed
409
    {
410
        module_t *p_module;
411
        int i_score;
412
        vlc_bool_t b_force;
413 414 415 416
        module_list_t *p_next;
    };

    module_list_t *p_list, *p_first, *p_tmp;
417
    vlc_list_t *p_all;
Sam Hocevar's avatar
 
Sam Hocevar committed
418

Gildas Bazin's avatar
 
Gildas Bazin committed
419
    int i_which_module, i_index = 0;
420
    vlc_bool_t b_intf = VLC_FALSE;
Sam Hocevar's avatar
 
Sam Hocevar committed
421 422

    module_t *p_module;
423

424
    int   i_shortcuts = 0;
425
    char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
426
    vlc_bool_t b_force_backup = p_this->b_force;
Sam Hocevar's avatar
 
Sam Hocevar committed
427

428

429 430 431
    /* Deal with variables */
    if( psz_name && psz_name[0] == '$' )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
432 433 434 435
        vlc_value_t val;
        var_Create( p_this, psz_name + 1, VLC_VAR_MODULE | VLC_VAR_DOINHERIT );
        var_Get( p_this, psz_name + 1, &val );
        psz_var = val.psz_string;
436
        psz_name = psz_var;
437 438
    }

439
    /* Count how many different shortcuts were asked for */
440
    if( psz_name && *psz_name )
Sam Hocevar's avatar
 
Sam Hocevar committed
441
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
442
        char *psz_parser, *psz_last_shortcut;
443

444 445 446
        /* If the user wants none, give him none. */
        if( !strcmp( psz_name, "none" ) )
        {
447
            free( psz_var );
448 449 450
            return NULL;
        }

451
        i_shortcuts++;
Gildas Bazin's avatar
 
Gildas Bazin committed
452
        psz_shortcuts = psz_last_shortcut = strdup( psz_name );
453 454

        for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
455
        {
456
            if( *psz_parser == ',' )
Sam Hocevar's avatar
 
Sam Hocevar committed
457
            {
458 459
                 *psz_parser = '\0';
                 i_shortcuts++;
Gildas Bazin's avatar
 
Gildas Bazin committed
460
                 psz_last_shortcut = psz_parser + 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
461
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
462
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
463 464 465 466

        /* Check if the user wants to override the "strict" mode */
        if( psz_last_shortcut )
        {
467 468 469 470 471 472 473 474 475 476
            if( !strcmp(psz_last_shortcut, "none") )
            {
                b_strict = VLC_TRUE;
                i_shortcuts--;
            }
            else if( !strcmp(psz_last_shortcut, "any") )
            {
                b_strict = VLC_FALSE;
                i_shortcuts--;
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
477
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
478
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
479

Sam Hocevar's avatar
 
Sam Hocevar committed
480
    /* Sort the modules and test them */
481 482
    p_all = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
    p_list = malloc( p_all->i_count * sizeof( module_list_t ) );
Sam Hocevar's avatar
 
Sam Hocevar committed
483
    p_first = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
484

Sam Hocevar's avatar
 
Sam Hocevar committed
485
    /* Parse the module list for capabilities and probe each of them */
486
    for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
487
    {
488
        int i_shortcut_bonus = 0;
489

490
        p_module = (module_t *)p_all->p_values[i_which_module].p_object;
491

492 493
        /* Test that this module can do what we need */
        if( strcmp( p_module->psz_capability, psz_capability ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
494
        {
495 496 497
            /* Don't recurse through the sub-modules because vlc_list_find()
             * will list them anyway. */
            continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
498 499
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
500
        /* Test if we have the required CPU */
501
        if( (p_module->i_cpu & p_this->p_libvlc_global->i_cpu) != p_module->i_cpu )
Sam Hocevar's avatar
 
Sam Hocevar committed
502
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
503
            continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
504
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
505

506
        /* If we required a shortcut, check this plugin provides it. */
507
        if( i_shortcuts > 0 )
Sam Hocevar's avatar
 
Sam Hocevar committed
508
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
509
            vlc_bool_t b_trash;
510
            const char *psz_name = psz_shortcuts;
Sam Hocevar's avatar
 
Sam Hocevar committed
511

512
            /* Let's drop modules with a <= 0 score (unless they are
Gildas Bazin's avatar
 
Gildas Bazin committed
513
             * explicitly requested) */
514
            b_trash = p_module->i_score <= 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
515

516
            for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
Sam Hocevar's avatar
 
Sam Hocevar committed
517
            {
518
                for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
519
                {
520 521 522 523 524
                    char *c;
                    if( ( c = strchr( psz_name, '@' ) )
                        ? !strncasecmp( psz_name, p_module->pp_shortcuts[i],
                                        c-psz_name )
                        : !strcasecmp( psz_name, p_module->pp_shortcuts[i] ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
525 526
                    {
                        /* Found it */
527 528
                        if( c && c[1] )
                            psz_alias = c+1;
Gildas Bazin's avatar
 
Gildas Bazin committed
529
                        i_shortcut_bonus = i_short * 10000;
530
                        goto found_shortcut;
Gildas Bazin's avatar
 
Gildas Bazin committed
531
                    }
532 533
                }

534
                /* Go to the next shortcut... This is so lame! */
535
                psz_name += strlen( psz_name ) + 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
536
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
537

538 539 540
            /* If we are in "strict" mode and we couldn't
             * find the module in the list of provided shortcuts,
             * then kick the bastard out of here!!! */
541
            if( b_strict )
Sam Hocevar's avatar
 
Sam Hocevar committed
542
                continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
543
        }
544 545
        /* If we didn't require a shortcut, trash <= 0 scored plugins */
        else if( p_module->i_score <= 0 )
546 547 548
        {
            continue;
        }
549

550 551
found_shortcut:

Sam Hocevar's avatar
 
Sam Hocevar committed
552
        /* Special case: test if we requested a particular intf plugin */
Gildas Bazin's avatar
 
Gildas Bazin committed
553
        if( !i_shortcuts && p_module->psz_program
Sam Hocevar's avatar
Sam Hocevar committed
554
             && !strcmp( psz_capability, "interface" )
555
             && !strcmp( p_module->psz_program,
556
                         p_this->p_libvlc->psz_object_name ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
557
        {
558
            if( !b_intf )
Sam Hocevar's avatar
 
Sam Hocevar committed
559
            {
560 561 562
                /* Remove previous non-matching plugins */
                i_index = 0;
                b_intf = VLC_TRUE;
Sam Hocevar's avatar
 
Sam Hocevar committed
563
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
564
        }
565 566 567 568 569
        else if( b_intf )
        {
            /* This one doesn't match */
            continue;
        }
570

Sam Hocevar's avatar
 
Sam Hocevar committed
571 572
        /* Store this new module */
        p_list[ i_index ].p_module = p_module;
573
        p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
574
        p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
Sam Hocevar's avatar
 
Sam Hocevar committed
575

Sam Hocevar's avatar
 
Sam Hocevar committed
576
        /* Add it to the modules-to-probe list */
Sam Hocevar's avatar
 
Sam Hocevar committed
577 578
        if( i_index == 0 )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
579
            p_list[ 0 ].p_next = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
580 581 582 583 584 585 586
            p_first = p_list;
        }
        else
        {
            /* Ok, so at school you learned that quicksort is quick, and
             * bubble sort sucks raw eggs. But that's when dealing with
             * thousands of items. Here we have barely 50. */
587
            module_list_t *p_newlist = p_first;
Sam Hocevar's avatar
 
Sam Hocevar committed
588

589
            if( p_first->i_score < p_list[ i_index ].i_score )
Sam Hocevar's avatar
 
Sam Hocevar committed
590
            {
Sam Hocevar's avatar
 
Sam Hocevar committed
591 592
                p_list[ i_index ].p_next = p_first;
                p_first = &p_list[ i_index ];
Sam Hocevar's avatar
 
Sam Hocevar committed
593 594
            }
            else
595
            {
596 597
                while( p_newlist->p_next != NULL &&
                    p_newlist->p_next->i_score >= p_list[ i_index ].i_score )
Sam Hocevar's avatar
 
Sam Hocevar committed
598
                {
Sam Hocevar's avatar
 
Sam Hocevar committed
599
                    p_newlist = p_newlist->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
600 601
                }

Sam Hocevar's avatar
 
Sam Hocevar committed
602 603 604
                p_list[ i_index ].p_next = p_newlist->p_next;
                p_newlist->p_next = &p_list[ i_index ];
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
605 606
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
607 608
        i_index++;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
609

Clément Stenac's avatar
Clément Stenac committed
610 611
    msg_Dbg( p_this, "looking for %s module: %i candidate%s", psz_capability,
                                            i_index, i_index == 1 ? "" : "s" );
612

613
    /* Lock all candidate modules */
614 615
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
616
    {
617
        vlc_object_yield( p_tmp->p_module );
618
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
619 620
    }

621
    /* We can release the list, interesting modules were yielded */
622
    vlc_list_release( p_all );
Sam Hocevar's avatar
 
Sam Hocevar committed
623

Sam Hocevar's avatar
 
Sam Hocevar committed
624
    /* Parse the linked list and use the first successful module */
625 626
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
627
    {
628 629
#ifdef HAVE_DYNAMIC_PLUGINS
        /* Make sure the module is loaded in mem */
630 631 632 633
        module_t *p_module = p_tmp->p_module;
        if( p_module->b_submodule )
            p_module = (module_t *)p_module->p_parent;

634 635 636 637 638 639 640 641
        if( !p_module->b_builtin && !p_module->b_loaded )
        {
            module_t *p_new_module =
                AllocatePlugin( p_this, p_module->psz_filename );
            if( p_new_module )
            {
                CacheMerge( p_this, p_module, p_new_module );
                vlc_object_attach( p_new_module, p_module );
642
                DeleteModule( p_new_module, VLC_TRUE );
643 644 645 646
            }
        }
#endif

647
        p_this->b_force = p_tmp->b_force;
648 649
        if( p_tmp->p_module->pf_activate
             && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
Sam Hocevar's avatar
 
Sam Hocevar committed
650 651
        {
            break;
652
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
653

654
        vlc_object_release( p_tmp->p_module );
655
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
656
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
657

Sam Hocevar's avatar
 
Sam Hocevar committed
658
    /* Store the locked module value */
659
    if( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
660
    {
661 662
        p_module = p_tmp->p_module;
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
663
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
664 665 666 667
    else
    {
        p_module = NULL;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
668

Sam Hocevar's avatar
 
Sam Hocevar committed
669
    /* Unlock the remaining modules */
670
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
671
    {
672
        vlc_object_release( p_tmp->p_module );
673
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
674
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
675

Sam Hocevar's avatar
 
Sam Hocevar committed
676
    free( p_list );
677
    p_this->b_force = b_force_backup;
Sam Hocevar's avatar
 
Sam Hocevar committed
678

Sam Hocevar's avatar
 
Sam Hocevar committed
679
    if( p_module != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
680
    {
681
        msg_Dbg( p_this, "using %s module \"%s\"",
682
                 psz_capability, p_module->psz_object_name );
683 684 685
    }
    else if( p_first == NULL )
    {
686 687 688
        if( !strcmp( psz_capability, "access_demux" ) )
        {
            msg_Warn( p_this, "no %s module matched \"%s\"",
689
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
690 691
        }
        else
Clément Stenac's avatar
Clément Stenac committed
692
        {
693 694
            msg_Err( p_this, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
695 696 697

            msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
698
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
699
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
700 701
    else if( psz_name != NULL && *psz_name )
    {
Christophe Massiot's avatar
Christophe Massiot committed
702 703
        msg_Warn( p_this, "no %s module matching \"%s\" could be loaded",
                  psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
Sam Hocevar's avatar
 
Sam Hocevar committed
704
    }
705 706
    else
        msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
Sam Hocevar's avatar
 
Sam Hocevar committed
707

708 709
    if( p_module && !p_this->psz_object_name )
    {
710 711 712
        /* This assumes that p_this is the object which will be using the
         * module. That's not always the case ... but it is in most cases.
         */
713 714 715 716 717
        if( psz_alias )
            p_this->psz_object_name = strdup( psz_alias );
        else
            p_this->psz_object_name = strdup( p_module->psz_object_name );
    }
718

719 720
    free( psz_shortcuts );
    free( psz_var );
721

Sam Hocevar's avatar
 
Sam Hocevar committed
722
    /* Don't forget that the module is still locked */
723
    return p_module;
Sam Hocevar's avatar
 
Sam Hocevar committed
724 725 726 727 728
}

/*****************************************************************************
 * module_Unneed: decrease the usage count of a module.
 *****************************************************************************
729 730
 * This function must be called by the thread that called module_Need, to
 * decrease the reference count and allow for hiding of modules.
Sam Hocevar's avatar
 
Sam Hocevar committed
731
 *****************************************************************************/
732
void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
Sam Hocevar's avatar
 
Sam Hocevar committed
733
{
734 735 736 737 738 </