libvlc.c 16.9 KB
Newer Older
1
/*****************************************************************************
2
 * libvlc.c: libvlc instances creation and deletion, interfaces handling
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 1998-2008 VLC authors and VideoLAN
5
 * $Id$
6 7 8
 *
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
9
 *          Gildas Bazin <gbazin@videolan.org>
10
 *          Derk-Jan Hartman <hartman at videolan dot org>
11
 *          Rémi Denis-Courmont <rem # videolan : org>
12
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
13 14 15
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
16 17 18 19
 * (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
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
20 21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
22
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
23 24 25
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 27
 *****************************************************************************/

28 29 30
/** \file
 * This file contains functions to create and destroy libvlc instances
 */
31 32 33 34

/*****************************************************************************
 * Preamble
 *****************************************************************************/
35 36 37 38
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

39
#include <vlc_common.h>
40
#include "../lib/libvlc_internal.h"
41
#include <vlc_input.h>
42

43 44
#include "modules/modules.h"
#include "config/configuration.h"
45
#include "playlist/preparser.h"
46

47 48 49
#include <stdio.h>                                              /* sprintf() */
#include <string.h>
#include <stdlib.h>                                                /* free() */
50
#include <errno.h>
51

52
#include "config/vlc_getopt.h"
53

54 55
#include <vlc_playlist.h>
#include <vlc_interface.h>
Sam Hocevar's avatar
Sam Hocevar committed
56

57
#include <vlc_actions.h>
58
#include <vlc_charset.h>
59
#include <vlc_dialog.h>
60
#include <vlc_keystore.h>
61
#include <vlc_fs.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
62
#include <vlc_cpu.h>
63
#include <vlc_url.h>
64
#include <vlc_modules.h>
65

66 67
#include "libvlc.h"
#include "playlist/playlist_internal.h"
68
#include "misc/variables.h"
69

70
#include <vlc_vlm.h>
71

72
#include <assert.h>
73

74
/*****************************************************************************
75
 * Local prototypes
76
 *****************************************************************************/
77
static void GetFilenames  ( libvlc_int_t *, unsigned, const char *const [] );
78

79
/**
80 81
 * Allocate a blank libvlc instance, also setting the exit handler.
 * Vlc's threading system must have been initialized first
82 83 84 85 86 87 88
 */
libvlc_int_t * libvlc_InternalCreate( void )
{
    libvlc_int_t *p_libvlc;
    libvlc_priv_t *priv;

    /* Allocate a libvlc instance object */
89
    p_libvlc = (vlc_custom_create)( NULL, sizeof (*priv), "libvlc" );
90 91 92 93
    if( p_libvlc == NULL )
        return NULL;

    priv = libvlc_priv (p_libvlc);
94
    priv->playlist = NULL;
95 96
    priv->p_vlm = NULL;

97
    vlc_ExitInit( &priv->exit );
98 99

    return p_libvlc;
100 101 102
}

/**
103 104 105 106 107 108
 * Initialize a libvlc instance
 * This function initializes a previously allocated libvlc instance:
 *  - CPU detection
 *  - gettext initialization
 *  - message queue, module bank and playlist initialization
 *  - configuration and commandline parsing
109
 */
110
int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
111
                         const char *ppsz_argv[] )
112
{
113 114 115 116
    libvlc_priv_t *priv = libvlc_priv (p_libvlc);
    char *       psz_modules = NULL;
    char *       psz_parser = NULL;
    char *       psz_control = NULL;
117
    char        *psz_val;
118
    int          i_ret = VLC_EGENERIC;
119

120
    /* System specific initialization code */
121
    system_Init();
122

123 124
    vlc_LogPreinit(p_libvlc);

125
    /* Initialize the module bank and load the configuration of the
126 127
     * core module. We need to do this at this stage to be able to display
     * a short help if required by the user. (short help == core module
128
     * options) */
129
    module_InitBank ();
130

131
    /* Get command line options that affect module loading. */
132
    if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
133
    {
134
        module_EndBank (false);
135
        return VLC_EGENERIC;
136
    }
137

138
    vlc_threads_setup (p_libvlc);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

    /* Load the builtins and plugins into the module_bank.
     * We have to do it before config_Load*() because this also gets the
     * list of configuration options exported by each module and loads their
     * default values. */
    size_t module_count = module_LoadPlugins (p_libvlc);

    /*
     * Override default configuration with config file settings
     */
    if( !var_InheritBool( p_libvlc, "ignore-config" ) )
    {
        if( var_InheritBool( p_libvlc, "reset-config" ) )
            config_SaveConfigFile( p_libvlc ); /* Save default config */
        else
            config_LoadConfigFile( p_libvlc );
    }

    /*
     * Override configuration with command line settings
     */
    int vlc_optind;
    if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
162
        goto error;
163

164 165
    vlc_LogInit(p_libvlc);

166 167 168 169 170 171 172
    /*
     * Support for gettext
     */
#if defined( ENABLE_NLS ) \
     && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
    vlc_bindtextdomain (PACKAGE_NAME);
#endif
173
    /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
174
    msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
175

176
    if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
177
    {
178
        libvlc_InternalCleanup (p_libvlc);
179
        exit(0);
180
    }
181

182
    if( module_count <= 1 )
183
    {
184
        msg_Err( p_libvlc, "No plugins found! Check your VLC installation.");
185 186
        i_ret = VLC_ENOMOD;
        goto error;
187 188
    }

189 190 191
#ifdef HAVE_DAEMON
    /* Check for daemon mode */
    if( var_InheritBool( p_libvlc, "daemon" ) )
192
    {
193 194 195
        if( daemon( 1, 0) != 0 )
        {
            msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
196
            goto error;
197 198 199
        }

        /* lets check if we need to write the pidfile */
200 201
        char *pidfile = var_InheritString( p_libvlc, "pidfile" );
        if( pidfile != NULL )
202
        {
203 204
            FILE *stream = vlc_fopen( pidfile, "w" );
            if( stream != NULL )
205
            {
206 207 208
                fprintf( stream, "%d", (int)getpid() );
                fclose( stream );
                msg_Dbg( p_libvlc, "written PID file %s", pidfile );
209 210
            }
            else
211 212 213
                msg_Err( p_libvlc, "cannot write PID file %s: %s",
                         pidfile, vlc_strerror_c(errno) );
            free( pidfile );
214
        }
215 216 217 218 219
    }
    else
    {
        var_Create( p_libvlc, "pidfile", VLC_VAR_STRING );
        var_SetString( p_libvlc, "pidfile", "" );
220
    }
221
#endif
222

223 224
    i_ret = VLC_ENOMEM;

225
    if( libvlc_InternalDialogInit( p_libvlc ) != VLC_SUCCESS )
226
        goto error;
227 228
    if( libvlc_InternalKeystoreInit( p_libvlc ) != VLC_SUCCESS )
        msg_Warn( p_libvlc, "memory keystore init failed" );
229

230
    vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
231

232
    priv->b_stats = var_InheritBool( p_libvlc, "stats" );
233 234 235 236

    /*
     * Initialize hotkey handling
     */
237
    if( libvlc_InternalActionsInit( p_libvlc ) != VLC_SUCCESS )
238
        goto error;
239

240 241 242 243
    /*
     * Meta data handling
     */
    priv->parser = playlist_preparser_New(VLC_OBJECT(p_libvlc));
244 245
    if( !priv->parser )
        goto error;
246

247
    /* Create a variable for showing the fullscreen interface */
248 249
    var_Create( p_libvlc, "intf-toggle-fscontrol", VLC_VAR_BOOL );
    var_SetBool( p_libvlc, "intf-toggle-fscontrol", true );
250

251 252 253
    /* Create a variable for the Boss Key */
    var_Create( p_libvlc, "intf-boss", VLC_VAR_VOID );

254 255 256
    /* Create a variable for showing the main interface */
    var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );

257 258 259
    /* Create a variable for showing the right click menu */
    var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );

260 261 262 263
    /* variables for signalling creation of new files */
    var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
    var_Create( p_libvlc, "record-file", VLC_VAR_STRING );

264
    /* some default internal settings */
265
    var_Create( p_libvlc, "window", VLC_VAR_STRING );
266 267 268 269
    /* NOTE: Because the playlist and interfaces start before this function
     * returns control to the application (DESIGN BUG!), all these variables
     * must be created (in place of libvlc_new()) and set to VLC defaults
     * (in place of VLC main()) *here*. */
270
    var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
271 272 273 274 275
    var_SetString( p_libvlc, "user-agent",
                   "VLC media player (LibVLC "VERSION")" );
    var_Create( p_libvlc, "http-user-agent", VLC_VAR_STRING );
    var_SetString( p_libvlc, "http-user-agent",
                   "VLC/"PACKAGE_VERSION" LibVLC/"PACKAGE_VERSION );
276 277 278 279 280 281
    var_Create( p_libvlc, "app-icon-name", VLC_VAR_STRING );
    var_SetString( p_libvlc, "app-icon-name", PACKAGE_NAME );
    var_Create( p_libvlc, "app-id", VLC_VAR_STRING );
    var_SetString( p_libvlc, "app-id", "org.VideoLAN.VLC" );
    var_Create( p_libvlc, "app-version", VLC_VAR_STRING );
    var_SetString( p_libvlc, "app-version", PACKAGE_VERSION );
282

283
    /* System specific configuration */
284
    system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
285

286
#ifdef ENABLE_VLM
287
    /* Initialize VLM if vlm-conf is specified */
288 289
    psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" );
    if( psz_parser )
290
    {
291 292 293
        priv->p_vlm = vlm_New( p_libvlc );
        if( !priv->p_vlm )
            msg_Err( p_libvlc, "VLM initialization failed" );
294
    }
295 296
    free( psz_parser );
#endif
297

298 299 300
    /*
     * Load background interfaces
     */
301 302
    psz_modules = var_CreateGetNonEmptyString( p_libvlc, "extraintf" );
    psz_control = var_CreateGetNonEmptyString( p_libvlc, "control" );
303

304
    if( psz_modules && psz_control )
305
    {
306 307 308 309 310 311
        char* psz_tmp;
        if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
        {
            free( psz_modules );
            psz_modules = psz_tmp;
        }
312
    }
313
    else if( psz_control )
314
    {
315 316
        free( psz_modules );
        psz_modules = strdup( psz_control );
317 318
    }

319 320 321 322 323 324 325 326 327 328 329
    psz_parser = psz_modules;
    while ( psz_parser && *psz_parser )
    {
        char *psz_module, *psz_temp;
        psz_module = psz_parser;
        psz_parser = strchr( psz_module, ':' );
        if ( psz_parser )
        {
            *psz_parser = '\0';
            psz_parser++;
        }
330
        if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
331
        {
332
            libvlc_InternalAddIntf( p_libvlc, psz_temp );
333 334 335 336 337 338
            free( psz_temp );
        }
    }
    free( psz_modules );
    free( psz_control );

339
    if( var_InheritBool( p_libvlc, "network-synchronisation") )
340
        libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
341

342
#ifdef __APPLE__
343 344 345 346 347 348 349 350
    var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
    var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
    var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
    var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
    var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
    var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
    var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
    var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
351
    var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
352
#endif
353 354

    /*
355 356 357
     * Get input filenames given as commandline arguments.
     * We assume that the remaining parameters are filenames
     * and their input options.
358
     */
359
    GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
360 361 362 363

    /*
     * Get --open argument
     */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
364
    psz_val = var_InheritString( p_libvlc, "open" );
365
    if ( psz_val != NULL )
366
    {
367
        intf_InsertItem( p_libvlc, psz_val, 0, NULL, 0 );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
368
        free( psz_val );
369
    }
370

371
    return VLC_SUCCESS;
372 373 374 375

error:
    libvlc_InternalCleanup( p_libvlc );
    return i_ret;
376 377 378
}

/**
379 380
 * Cleanup a libvlc instance. The instance is not completely deallocated
 * \param p_libvlc the instance to clean
381
 */
382
void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
383
{
384
    libvlc_priv_t *priv = libvlc_priv (p_libvlc);
385

386 387 388
    if (priv->parser != NULL)
        playlist_preparser_Deactivate(priv->parser);

389 390
    /* Ask the interfaces to stop and destroy them */
    msg_Dbg( p_libvlc, "removing all interfaces" );
391
    intf_DestroyAll( p_libvlc );
392

393
    libvlc_InternalDialogClean( p_libvlc );
394
    libvlc_InternalKeystoreClean( p_libvlc );
395

396
#ifdef ENABLE_VLM
397 398 399 400 401 402 403
    /* Destroy VLM if created in libvlc_InternalInit */
    if( priv->p_vlm )
    {
        vlm_Delete( priv->p_vlm );
    }
#endif

404
#if !defined( _WIN32 ) && !defined( __OS2__ )
405 406
    char *pidfile = var_InheritString( p_libvlc, "pidfile" );
    if( pidfile != NULL )
407
    {
408 409 410 411 412
        msg_Dbg( p_libvlc, "removing PID file %s", pidfile );
        if( unlink( pidfile ) )
            msg_Warn( p_libvlc, "cannot remove PID file %s: %s",
                      pidfile, vlc_strerror_c(errno) );
        free( pidfile );
413 414 415
    }
#endif

416 417 418
    if (priv->parser != NULL)
        playlist_preparser_Delete(priv->parser);

419
    libvlc_InternalActionsClean( p_libvlc );
420

421 422 423 424
    /* Save the configuration */
    if( !var_InheritBool( p_libvlc, "ignore-config" ) )
        config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );

425
    /* Free module bank. It is refcounted, so we call this each time  */
426
    vlc_LogDeinit (p_libvlc);
427
    module_EndBank (true);
428
#if defined(_WIN32) || defined(__OS2__)
429 430
    system_End( );
#endif
431 432 433 434 435 436 437 438 439 440 441 442
}

/**
 * Destroy everything.
 * This function requests the running threads to finish, waits for their
 * termination, and destroys their structure.
 * It stops the thread systems: no instance can run after this has run
 * \param p_libvlc the instance to destroy
 */
void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
{
    libvlc_priv_t *priv = libvlc_priv( p_libvlc );
443

444
    vlc_ExitDestroy( &priv->exit );
445

446
    assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
447
    vlc_object_release( p_libvlc );
448 449
}

450 451 452 453 454 455
/*****************************************************************************
 * GetFilenames: parse command line options which are not flags
 *****************************************************************************
 * Parse command line for input files as well as their associated options.
 * An option always follows its associated input and begins with a ":".
 *****************************************************************************/
456 457
static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
                          const char *const args[] )
458
{
459
    while( n > 0 )
460 461
    {
        /* Count the input options */
462 463 464
        unsigned i_options = 0;

        while( args[--n][0] == ':' )
465 466
        {
            i_options++;
467 468 469 470 471
            if( n == 0 )
            {
                msg_Warn( p_vlc, "options %s without item", args[n] );
                return; /* syntax!? */
            }
472 473
        }

474 475 476 477 478 479 480
        char *mrl = NULL;
        if( strstr( args[n], "://" ) == NULL )
        {
            mrl = vlc_path2uri( args[n], NULL );
            if( !mrl )
                continue;
        }
481

482
        intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
483
                         ( i_options ? &args[n + 1] : NULL ),
484
                         VLC_INPUT_OPTION_TRUSTED );
485
        free( mrl );
486
    }
487
}
488 489 490

/**
 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
491 492
 * The actual extraction is asynchronous. It can be cancelled with
 * libvlc_MetadataCancel()
493
 */
494 495 496
int libvlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
                           input_item_meta_request_option_t i_options,
                           int timeout, void *id)
497 498 499 500 501 502
{
    libvlc_priv_t *priv = libvlc_priv(libvlc);

    if (unlikely(priv->parser == NULL))
        return VLC_ENOMEM;

503 504 505
    vlc_mutex_lock( &item->lock );
    if( item->i_preparse_depth == 0 )
        item->i_preparse_depth = 1;
506 507
    if( i_options & META_REQUEST_OPTION_DO_INTERACT )
        item->b_preparse_interact = true;
508
    vlc_mutex_unlock( &item->lock );
509
    playlist_preparser_Push( priv->parser, item, i_options, timeout, id );
510 511 512 513 514 515 516
    return VLC_SUCCESS;
}

/**
 * Requests retrieving/downloading art for an input item.
 * The retrieval is performed asynchronously.
 */
517 518
int libvlc_ArtRequest(libvlc_int_t *libvlc, input_item_t *item,
                      input_item_meta_request_option_t i_options)
519 520 521 522 523 524
{
    libvlc_priv_t *priv = libvlc_priv(libvlc);

    if (unlikely(priv->parser == NULL))
        return VLC_ENOMEM;

525
    playlist_preparser_fetcher_Push(priv->parser, item, i_options);
526 527
    return VLC_SUCCESS;
}
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

/**
 * Cancels extraction of the meta data for an input item.
 *
 * This does nothing if the input item is already processed or if it was not
 * added with libvlc_MetadataRequest()
 */
void libvlc_MetadataCancel(libvlc_int_t *libvlc, void *id)
{
    libvlc_priv_t *priv = libvlc_priv(libvlc);

    if (unlikely(priv->parser == NULL))
        return;

    playlist_preparser_Cancel(priv->parser, id);
}