modules.c 72.9 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
#include <vlc/vlc.h>
28
#include "../libvlc.h"
29

Sam Hocevar's avatar
 
Sam Hocevar committed
30 31 32 33 34 35
/* 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
36 37
#include <stdlib.h>                                      /* free(), strtol() */
#include <stdio.h>                                              /* sprintf() */
Sam Hocevar's avatar
 
Sam Hocevar committed
38
#include <string.h>                                              /* strdup() */
Sam Hocevar's avatar
 
Sam Hocevar committed
39

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

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

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

79
#include "modules/configuration.h"
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
80
#include "libvlc.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
81

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

Clément Stenac's avatar
Clément Stenac committed
85 86 87
#include "vlc_stream.h"
#include "vlc_access.h"
#include "vlc_demux.h"
Laurent Aimar's avatar
Laurent Aimar committed
88
#include "vlc_codec.h"
Clément Stenac's avatar
Clément Stenac committed
89

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

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

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

Gildas Bazin's avatar
 
Gildas Bazin committed
103
#include "iso_lang.h"
Clément Stenac's avatar
Clément Stenac committed
104
#include "vlc_charset.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
105

106 107
#include "vlc_block.h"

108 109
#include "vlc_vlm.h"

Clément Stenac's avatar
Clément Stenac committed
110
#include "vlc_image.h"
111
#include "vlc_osd.h"
Clément Stenac's avatar
Clément Stenac committed
112

113
#include "vlc_update.h"
114
#include "vlc_strings.h"
Clément Stenac's avatar
Clément Stenac committed
115
#include "vlc_streaming.h"
116

117 118
#include "modules/modules.h"
#include "modules/builtin.h"
Clément Stenac's avatar
Clément Stenac committed
119 120

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

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

Sam Hocevar's avatar
 
Sam Hocevar committed
127 128 129
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
130
#ifdef HAVE_DYNAMIC_PLUGINS
131 132 133 134
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
135
#endif
136
static int  AllocateBuiltinModule( vlc_object_t *, int ( * ) ( module_t * ) );
137
static int  DeleteModule ( module_t *, vlc_bool_t );
Sam Hocevar's avatar
 
Sam Hocevar committed
138
#ifdef HAVE_DYNAMIC_PLUGINS
139 140 141
static void   DupModule        ( module_t * );
static void   UndupModule      ( module_t * );
static int    CallEntry        ( module_t * );
142
static int    LoadModule       ( vlc_object_t *, char *, module_handle_t * );
143 144
static void   CloseModule      ( module_handle_t );
static void * GetSymbol        ( module_handle_t, const char * );
145
static void   CacheLoad        ( vlc_object_t * );
146
static int    CacheLoadConfig  ( module_t *, FILE * );
147 148
static void   CacheSave        ( vlc_object_t * );
static void   CacheSaveConfig  ( module_t *, FILE * );
Sam Hocevar's avatar
Sam Hocevar committed
149
static char * CacheName        ( void );
150 151 152
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
153
#if defined(HAVE_DL_WINDOWS)
154 155
static char * GetWindowsError  ( void );
#endif
156
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
157

158 159
static void module_LoadMain( vlc_object_t *p_this );

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

Sam Hocevar's avatar
 
Sam Hocevar committed
164 165 166
/*****************************************************************************
 * module_InitBank: create the module bank.
 *****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
167 168
 * 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
169
 *****************************************************************************/
170
void __module_InitBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
171
{
172
    module_bank_t *p_bank = NULL;
173
    vlc_value_t  lockval;
174
    libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
175

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

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

    /* Everything worked, attach the object */
200 201
    p_libvlc_global->p_module_bank = p_bank;
    vlc_object_attach( p_bank, p_libvlc_global );
202

203
    module_LoadMain( p_this );
Gildas Bazin's avatar
 
Gildas Bazin committed
204 205
}

Sam Hocevar's avatar
 
Sam Hocevar committed
206

Sam Hocevar's avatar
 
Sam Hocevar committed
207
/*****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
208
 * module_EndBank: empty the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
209
 *****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
210
 * This function unloads all unused plugin modules and empties the module
Sam Hocevar's avatar
 
Sam Hocevar committed
211 212
 * bank in case of success.
 *****************************************************************************/
213
void __module_EndBank( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
214
{
215
    module_t * p_next = NULL;
216
    vlc_value_t lockval;
217
    libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
218

219 220
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
221
    vlc_mutex_lock( lockval.p_address );
222
    if( !p_libvlc_global->p_module_bank )
223 224
    {
        vlc_mutex_unlock( lockval.p_address );
225
        var_Destroy( p_libvlc_global, "libvlc" );
226 227
        return;
    }
228
    if( --p_libvlc_global->p_module_bank->i_usage )
229 230
    {
        vlc_mutex_unlock( lockval.p_address );
231
        var_Destroy( p_libvlc_global, "libvlc" );
232 233 234
        return;
    }
    vlc_mutex_unlock( lockval.p_address );
235
    var_Destroy( p_libvlc_global, "libvlc" );
Sam Hocevar's avatar
 
Sam Hocevar committed
236

237 238
    config_AutoSaveConfigFile( p_this );

239
#ifdef HAVE_DYNAMIC_PLUGINS
240
# define p_bank p_libvlc_global->p_module_bank
241 242 243
    if( p_bank->b_cache ) CacheSave( p_this );
    while( p_bank->i_loaded_cache-- )
    {
244 245
        if( p_bank->pp_loaded_cache[p_bank->i_loaded_cache] )
        {
246 247
            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 );
248 249 250 251
            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;
        }
252
    }
253
    if( p_bank->pp_loaded_cache )
254
    {
255
        free( p_bank->pp_loaded_cache );
256 257
        p_bank->pp_loaded_cache = NULL;
    }
258 259 260 261
    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] );
262
        p_bank->pp_cache[p_bank->i_cache] = NULL;
263
    }
264
    if( p_bank->pp_cache )
265
    {
266
        free( p_bank->pp_cache );
267 268
        p_bank->pp_cache = NULL;
    }
269
# undef p_bank
270 271
#endif

272
    vlc_object_detach( p_libvlc_global->p_module_bank );
273

274
    while( p_libvlc_global->p_module_bank->i_children )
Sam Hocevar's avatar
 
Sam Hocevar committed
275
    {
276
        p_next = (module_t *)p_libvlc_global->p_module_bank->pp_children[0];
277

278
        if( DeleteModule( p_next, VLC_TRUE ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
279 280
        {
            /* Module deletion failed */
281
            msg_Err( p_this, "module \"%s\" can't be removed, trying harder",
282
                     p_next->psz_object_name );
Sam Hocevar's avatar
 
Sam Hocevar committed
283 284

            /* We just free the module by hand. Niahahahahaha. */
285 286
            vlc_object_detach( p_next );
            vlc_object_destroy( p_next );
Sam Hocevar's avatar
 
Sam Hocevar committed
287 288 289
        }
    }

290 291
    vlc_object_destroy( p_libvlc_global->p_module_bank );
    p_libvlc_global->p_module_bank = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
292 293 294
}

/*****************************************************************************
295
 * module_LoadMain: load the main program info into the module bank.
Sam Hocevar's avatar
 
Sam Hocevar committed
296
 *****************************************************************************
297 298 299 300
 * 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
301
 *****************************************************************************/
302
static void module_LoadMain( vlc_object_t *p_this )
Sam Hocevar's avatar
 
Sam Hocevar committed
303
{
304
    vlc_value_t lockval;
305
    libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
306

307 308
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
309
    vlc_mutex_lock( lockval.p_address );
310
    if( p_libvlc_global->p_module_bank->b_main )
311 312
    {
        vlc_mutex_unlock( lockval.p_address );
313
        var_Destroy( p_libvlc_global, "libvlc" );
314 315
        return;
    }
316
    p_libvlc_global->p_module_bank->b_main = VLC_TRUE;
317
    vlc_mutex_unlock( lockval.p_address );
318
    var_Destroy( p_libvlc_global, "libvlc" );
319

320
    AllocateBuiltinModule( p_this, vlc_entry__main );
321 322 323 324 325 326 327
}

/*****************************************************************************
 * module_LoadBuiltins: load all modules which we built with.
 *****************************************************************************
 * This function fills the module bank structure with the builtin modules.
 *****************************************************************************/
328
void __module_LoadBuiltins( vlc_object_t * p_this )
329
{
330
    vlc_value_t lockval;
331
    libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
332

333 334
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
335
    vlc_mutex_lock( lockval.p_address );
336
    if( p_libvlc_global->p_module_bank->b_builtins )
337 338
    {
        vlc_mutex_unlock( lockval.p_address );
339
        var_Destroy( p_libvlc_global, "libvlc" );
340 341
        return;
    }
342
    p_libvlc_global->p_module_bank->b_builtins = VLC_TRUE;
343
    vlc_mutex_unlock( lockval.p_address );
344
    var_Destroy( p_libvlc_global, "libvlc" );
345

346 347 348 349 350 351 352 353 354
    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.
 *****************************************************************************/
355
void __module_LoadPlugins( vlc_object_t * p_this )
356 357
{
#ifdef HAVE_DYNAMIC_PLUGINS
358
    vlc_value_t lockval;
359
    libvlc_global_data_t *p_libvlc_global = vlc_global( p_this );
360

361 362
    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
    var_Get( p_libvlc_global, "libvlc", &lockval );
363
    vlc_mutex_lock( lockval.p_address );
364
    if( p_libvlc_global->p_module_bank->b_plugins )
365 366
    {
        vlc_mutex_unlock( lockval.p_address );
367
        var_Destroy( p_libvlc_global, "libvlc" );
368 369
        return;
    }
370
    p_libvlc_global->p_module_bank->b_plugins = VLC_TRUE;
371
    vlc_mutex_unlock( lockval.p_address );
372
    var_Destroy( p_libvlc_global, "libvlc" );
373

374
    msg_Dbg( p_this, "checking plugin modules" );
375 376

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

379 380
    if( p_libvlc_global->p_module_bank->b_cache ||
        p_libvlc_global->p_module_bank->b_cache_delete ) CacheLoad( p_this );
381

382 383
    AllocateAllPlugins( p_this );
#endif
Sam Hocevar's avatar
 
Sam Hocevar committed
384 385 386
}

/*****************************************************************************
387
 * module_Need: return the best module function, given a capability list.
Sam Hocevar's avatar
 
Sam Hocevar committed
388
 *****************************************************************************
389
 * This function returns the module that best fits the asked capabilities.
Sam Hocevar's avatar
 
Sam Hocevar committed
390
 *****************************************************************************/
391
module_t * __module_Need( vlc_object_t *p_this, const char *psz_capability,
Gildas Bazin's avatar
 
Gildas Bazin committed
392
                          const char *psz_name, vlc_bool_t b_strict )
Sam Hocevar's avatar
 
Sam Hocevar committed
393
{
394
    typedef struct module_list_t module_list_t;
395

396
    struct module_list_t
Sam Hocevar's avatar
 
Sam Hocevar committed
397
    {
398
        module_t *p_module;
399
        int i_score;
400
        vlc_bool_t b_force;
401 402 403 404
        module_list_t *p_next;
    };

    module_list_t *p_list, *p_first, *p_tmp;
405
    vlc_list_t *p_all;
Sam Hocevar's avatar
 
Sam Hocevar committed
406

Gildas Bazin's avatar
 
Gildas Bazin committed
407
    int i_which_module, i_index = 0;
408
    vlc_bool_t b_intf = VLC_FALSE;
Sam Hocevar's avatar
 
Sam Hocevar committed
409 410

    module_t *p_module;
411

412
    int   i_shortcuts = 0;
413
    char *psz_shortcuts = NULL, *psz_var = NULL, *psz_alias = NULL;
414
    vlc_bool_t b_force_backup = p_this->b_force;
Sam Hocevar's avatar
 
Sam Hocevar committed
415

416

417 418 419
    /* Deal with variables */
    if( psz_name && psz_name[0] == '$' )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
420 421 422 423
        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;
424
        psz_name = psz_var;
425 426
    }

427
    /* Count how many different shortcuts were asked for */
428
    if( psz_name && *psz_name )
Sam Hocevar's avatar
 
Sam Hocevar committed
429
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
430
        char *psz_parser, *psz_last_shortcut;
431

432 433 434
        /* If the user wants none, give him none. */
        if( !strcmp( psz_name, "none" ) )
        {
435
            free( psz_var );
436 437 438
            return NULL;
        }

439
        i_shortcuts++;
Gildas Bazin's avatar
 
Gildas Bazin committed
440
        psz_shortcuts = psz_last_shortcut = strdup( psz_name );
441 442

        for( psz_parser = psz_shortcuts; *psz_parser; psz_parser++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
443
        {
444
            if( *psz_parser == ',' )
Sam Hocevar's avatar
 
Sam Hocevar committed
445
            {
446 447
                 *psz_parser = '\0';
                 i_shortcuts++;
Gildas Bazin's avatar
 
Gildas Bazin committed
448
                 psz_last_shortcut = psz_parser + 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
449
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
450
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
451 452 453 454

        /* Check if the user wants to override the "strict" mode */
        if( psz_last_shortcut )
        {
455 456 457 458 459 460 461 462 463 464
            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
465
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
466
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
467

Sam Hocevar's avatar
 
Sam Hocevar committed
468
    /* Sort the modules and test them */
469 470
    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
471
    p_first = NULL;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
472
    unsigned i_cpu = vlc_CPU();
Sam Hocevar's avatar
 
Sam Hocevar committed
473

Sam Hocevar's avatar
 
Sam Hocevar committed
474
    /* Parse the module list for capabilities and probe each of them */
475
    for( i_which_module = 0; i_which_module < p_all->i_count; i_which_module++ )
Sam Hocevar's avatar
 
Sam Hocevar committed
476
    {
477
        int i_shortcut_bonus = 0;
478

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

481 482
        /* Test that this module can do what we need */
        if( strcmp( p_module->psz_capability, psz_capability ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
483
        {
484 485 486
            /* Don't recurse through the sub-modules because vlc_list_find()
             * will list them anyway. */
            continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
487 488
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
489
        /* Test if we have the required CPU */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
490
        if( (p_module->i_cpu & i_cpu) != p_module->i_cpu )
Sam Hocevar's avatar
 
Sam Hocevar committed
491
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
492
            continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
493
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
494

495
        /* If we required a shortcut, check this plugin provides it. */
496
        if( i_shortcuts > 0 )
Sam Hocevar's avatar
 
Sam Hocevar committed
497
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
498
            vlc_bool_t b_trash;
499
            const char *psz_name = psz_shortcuts;
Sam Hocevar's avatar
 
Sam Hocevar committed
500

501
            /* Let's drop modules with a <= 0 score (unless they are
Gildas Bazin's avatar
 
Gildas Bazin committed
502
             * explicitly requested) */
503
            b_trash = p_module->i_score <= 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
504

505
            for( unsigned i_short = i_shortcuts; i_short > 0; i_short-- )
Sam Hocevar's avatar
 
Sam Hocevar committed
506
            {
507
                for( unsigned i = 0; p_module->pp_shortcuts[i]; i++ )
508
                {
509 510 511 512 513
                    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
514 515
                    {
                        /* Found it */
516 517
                        if( c && c[1] )
                            psz_alias = c+1;
Gildas Bazin's avatar
 
Gildas Bazin committed
518
                        i_shortcut_bonus = i_short * 10000;
519
                        goto found_shortcut;
Gildas Bazin's avatar
 
Gildas Bazin committed
520
                    }
521 522
                }

523
                /* Go to the next shortcut... This is so lame! */
524
                psz_name += strlen( psz_name ) + 1;
Sam Hocevar's avatar
 
Sam Hocevar committed
525
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
526

527 528 529
            /* 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!!! */
530
            if( b_strict )
Sam Hocevar's avatar
 
Sam Hocevar committed
531
                continue;
Sam Hocevar's avatar
 
Sam Hocevar committed
532
        }
533 534
        /* If we didn't require a shortcut, trash <= 0 scored plugins */
        else if( p_module->i_score <= 0 )
535 536 537
        {
            continue;
        }
538

539 540
found_shortcut:

Sam Hocevar's avatar
 
Sam Hocevar committed
541
        /* Special case: test if we requested a particular intf plugin */
Gildas Bazin's avatar
 
Gildas Bazin committed
542
        if( !i_shortcuts && p_module->psz_program
Sam Hocevar's avatar
Sam Hocevar committed
543
             && !strcmp( psz_capability, "interface" )
544
             && !strcmp( p_module->psz_program,
545
                         p_this->p_libvlc->psz_object_name ) )
Sam Hocevar's avatar
 
Sam Hocevar committed
546
        {
547
            if( !b_intf )
Sam Hocevar's avatar
 
Sam Hocevar committed
548
            {
549 550 551
                /* Remove previous non-matching plugins */
                i_index = 0;
                b_intf = VLC_TRUE;
Sam Hocevar's avatar
 
Sam Hocevar committed
552
            }
Sam Hocevar's avatar
 
Sam Hocevar committed
553
        }
554 555 556 557 558
        else if( b_intf )
        {
            /* This one doesn't match */
            continue;
        }
559

Sam Hocevar's avatar
 
Sam Hocevar committed
560 561
        /* Store this new module */
        p_list[ i_index ].p_module = p_module;
562
        p_list[ i_index ].i_score = p_module->i_score + i_shortcut_bonus;
563
        p_list[ i_index ].b_force = i_shortcut_bonus && b_strict;
Sam Hocevar's avatar
 
Sam Hocevar committed
564

Sam Hocevar's avatar
 
Sam Hocevar committed
565
        /* Add it to the modules-to-probe list */
Sam Hocevar's avatar
 
Sam Hocevar committed
566 567
        if( i_index == 0 )
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
568
            p_list[ 0 ].p_next = NULL;
Sam Hocevar's avatar
 
Sam Hocevar committed
569 570 571 572 573 574 575
            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. */
576
            module_list_t *p_newlist = p_first;
Sam Hocevar's avatar
 
Sam Hocevar committed
577

578
            if( p_first->i_score < p_list[ i_index ].i_score )
Sam Hocevar's avatar
 
Sam Hocevar committed
579
            {
Sam Hocevar's avatar
 
Sam Hocevar committed
580 581
                p_list[ i_index ].p_next = p_first;
                p_first = &p_list[ i_index ];
Sam Hocevar's avatar
 
Sam Hocevar committed
582 583
            }
            else
584
            {
585 586
                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
587
                {
Sam Hocevar's avatar
 
Sam Hocevar committed
588
                    p_newlist = p_newlist->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
589 590
                }

Sam Hocevar's avatar
 
Sam Hocevar committed
591 592 593
                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
594 595
        }

Sam Hocevar's avatar
 
Sam Hocevar committed
596 597
        i_index++;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
598

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

602
    /* Lock all candidate modules */
603 604
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
605
    {
606
        vlc_object_yield( p_tmp->p_module );
607
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
608 609
    }

610
    /* We can release the list, interesting modules were yielded */
611
    vlc_list_release( p_all );
Sam Hocevar's avatar
 
Sam Hocevar committed
612

Sam Hocevar's avatar
 
Sam Hocevar committed
613
    /* Parse the linked list and use the first successful module */
614 615
    p_tmp = p_first;
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
616
    {
617 618
#ifdef HAVE_DYNAMIC_PLUGINS
        /* Make sure the module is loaded in mem */
619 620 621 622
        module_t *p_module = p_tmp->p_module;
        if( p_module->b_submodule )
            p_module = (module_t *)p_module->p_parent;

623 624 625 626 627 628 629 630
        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 );
631
                DeleteModule( p_new_module, VLC_TRUE );
632 633 634 635
            }
        }
#endif

636
        p_this->b_force = p_tmp->b_force;
637 638
        if( p_tmp->p_module->pf_activate
             && p_tmp->p_module->pf_activate( p_this ) == VLC_SUCCESS )
Sam Hocevar's avatar
 
Sam Hocevar committed
639 640
        {
            break;
641
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
642

643
        vlc_object_release( p_tmp->p_module );
644
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
645
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
646

Sam Hocevar's avatar
 
Sam Hocevar committed
647
    /* Store the locked module value */
648
    if( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
649
    {
650 651
        p_module = p_tmp->p_module;
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
652
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
653 654 655 656
    else
    {
        p_module = NULL;
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
657

Sam Hocevar's avatar
 
Sam Hocevar committed
658
    /* Unlock the remaining modules */
659
    while( p_tmp != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
660
    {
661
        vlc_object_release( p_tmp->p_module );
662
        p_tmp = p_tmp->p_next;
Sam Hocevar's avatar
 
Sam Hocevar committed
663
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
664

Sam Hocevar's avatar
 
Sam Hocevar committed
665
    free( p_list );
666
    p_this->b_force = b_force_backup;
Sam Hocevar's avatar
 
Sam Hocevar committed
667

Sam Hocevar's avatar
 
Sam Hocevar committed
668
    if( p_module != NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
669
    {
670
        msg_Dbg( p_this, "using %s module \"%s\"",
671
                 psz_capability, p_module->psz_object_name );
672 673 674
    }
    else if( p_first == NULL )
    {
675 676 677
        if( !strcmp( psz_capability, "access_demux" ) )
        {
            msg_Warn( p_this, "no %s module matched \"%s\"",
678
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
679 680
        }
        else
Clément Stenac's avatar
Clément Stenac committed
681
        {
682 683
            msg_Err( p_this, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
684 685 686

            msg_StackSet( VLC_EGENERIC, "no %s module matched \"%s\"",
                 psz_capability, (psz_name && *psz_name) ? psz_name : "any" );
687
        }
Sam Hocevar's avatar
 
Sam Hocevar committed
688
    }
Sam Hocevar's avatar
 
Sam Hocevar committed
689 690
    else if( psz_name != NULL && *psz_name )
    {
Christophe Massiot's avatar
Christophe Massiot committed
691 692
        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
693
    }
694 695
    else
        msg_StackSet( VLC_EGENERIC, "no suitable %s module", psz_capability );
Sam Hocevar's avatar
 
Sam Hocevar committed
696

697 698
    if( p_module && !p_this->psz_object_name )
    {
699 700 701
        /* 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.
         */
702 703 704 705 706
        if( psz_alias )
            p_this->psz_object_name = strdup( psz_alias );
        else
            p_this->psz_object_name = strdup( p_module->psz_object_name );
    }
707

708 709
    free( psz_shortcuts );
    free( psz_var );
710

Sam Hocevar's avatar
 
Sam Hocevar committed
711
    /* Don't forget that the module is still locked */
712
    return p_module;
Sam Hocevar's avatar
 
Sam Hocevar committed
713 714 715 716 717
}

/*****************************************************************************
 * module_Unneed: decrease the usage count of a module.
 *****************************************************************************
718 719
 * 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
720
 *****************************************************************************/
721
void __module_Unneed( vlc_object_t * p_this, module_t * p_module )
Sam Hocevar's avatar
 
Sam Hocevar committed
722
{
723 724 725 726 727 728
    /* Use the close method */
    if( p_module->pf_deactivate )
    {
        p_module->pf_deactivate( p_this );
    }

729
    msg_Dbg( p_this, "removing module \"%s\"", p_module->psz_object_name );
Sam Hocevar's avatar
 
Sam Hocevar committed
730

731
    vlc_object_release( p_module );
732 733

    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
734 735
}

736 737 738 739 740 741 742 743 744 745 746 747 748
/*****************************************************************************
 * module_Exists: tell if a module exists.
 *****************************************************************************
 * This function is a boolean function that tells if a module exist or not.
 *****************************************************************************/

vlc_bool_t __module_Exists(  vlc_object_t *p_this, const char * psz_name )
{
    vlc_list_t *p_list;
    int i;
    p_list = vlc_list_find( p_this, VLC_OBJECT_MODULE, FIND_ANYWHERE );
    for( i = 0 ; i < p_list->i_count; i++)
    {
749
        const char *psz_module_name =
750
            ((module_t *) p_list->p_values[i].p_object)->psz_shortname;
751
        if( psz_module_name && !strcmp( psz_module_name, psz_name ) )
752 753 754 755 756 757 758 759 760
        {
            /* We can release the list, and return yes */
            vlc_list_release( p_list ); return VLC_TRUE;
        }
    }
    vlc_list_release( p_list ); return VLC_FALSE;
}


Sam Hocevar's avatar
 
Sam Hocevar committed
761 762 763 764
/*****************************************************************************
 * Following functions are local.
 *****************************************************************************/

Sam Hocevar's avatar
 
Sam Hocevar committed
765 766 767
/*****************************************************************************
 * AllocateAllPlugins: load all plugin modules we can find.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
768
#ifdef HAVE_DYNAMIC_PLUGINS
769
static void AllocateAllPlugins( vlc_object_t *p_this )
Sam Hocevar's avatar