libvlc.c 23.1 KB
Newer Older
1
/*****************************************************************************
2
 * libvlc.c: libvlc instances creation and deletion, interfaces handling
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
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
LGPL  
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
LGPL  
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
LGPL  
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

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

50
#include "config/vlc_getopt.h"
51

52 53 54
#ifdef HAVE_UNISTD_H
#   include <unistd.h> /* isatty() */
#endif
55

56
#ifdef HAVE_DBUS
57 58 59
/* used for one-instance mode */
#   include <dbus/dbus.h>
#endif
60

Srikanth Raju's avatar
Srikanth Raju committed
61 62

#include <vlc_media_library.h>
63 64
#include <vlc_playlist.h>
#include <vlc_interface.h>
Sam Hocevar's avatar
Sam Hocevar committed
65

66
#include <vlc_charset.h>
67
#include <vlc_fs.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
68
#include <vlc_cpu.h>
69
#include <vlc_url.h>
70
#include <vlc_modules.h>
71

72 73
#include "libvlc.h"
#include "playlist/playlist_internal.h"
74
#include "misc/variables.h"
75

76
#include <vlc_vlm.h>
77

78 79 80 81
#ifdef __APPLE__
# include <libkern/OSAtomic.h>
#endif

82
#include <assert.h>
83

84
/*****************************************************************************
85
 * The evil global variables. We handle them with care, don't worry.
86
 *****************************************************************************/
87

KO Myung-Hun's avatar
KO Myung-Hun committed
88
#if !defined(WIN32) && !defined(__OS2__)
89
static bool b_daemon = false;
90
#endif
91 92

/*****************************************************************************
93
 * Local prototypes
94
 *****************************************************************************/
95
static void GetFilenames  ( libvlc_int_t *, unsigned, const char *const [] );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
96

97 98 99 100 101 102 103 104 105 106 107 108
/**
 * Allocate a libvlc instance, initialize global data if needed
 * It also initializes the threading system
 */
libvlc_int_t * libvlc_InternalCreate( void )
{
    libvlc_int_t *p_libvlc;
    libvlc_priv_t *priv;

    /* Now that the thread system is initialized, we don't have much, but
     * at least we have variables */
    /* Allocate a libvlc instance object */
109
    p_libvlc = vlc_custom_create( (vlc_object_t *)NULL, sizeof (*priv),
110
                                  "libvlc" );
111 112 113 114 115
    if( p_libvlc == NULL )
        return NULL;

    priv = libvlc_priv (p_libvlc);
    priv->p_playlist = NULL;
Srikanth Raju's avatar
Srikanth Raju committed
116
    priv->p_ml = NULL;
117
    priv->p_dialog_provider = NULL;
118
    priv->p_vlm = NULL;
119
    priv->i_verbose = 3; /* initial value until config is loaded */
120
#if defined( HAVE_ISATTY ) && !defined( WIN32 )
121
    priv->b_color = isatty( STDERR_FILENO ); /* 2 is for stderr */
122 123 124 125 126
#else
    priv->b_color = false;
#endif

    /* Initialize mutexes */
Srikanth Raju's avatar
Srikanth Raju committed
127
    vlc_mutex_init( &priv->ml_lock );
128
    vlc_ExitInit( &priv->exit );
129 130

    return p_libvlc;
131 132 133
}

/**
134 135 136 137 138 139
 * 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
140
 */
141
int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
142
                         const char *ppsz_argv[] )
143
{
144 145 146 147
    libvlc_priv_t *priv = libvlc_priv (p_libvlc);
    char *       psz_modules = NULL;
    char *       psz_parser = NULL;
    char *       psz_control = NULL;
148
    char        *psz_val;
149

150
    /* System specific initialization code */
151
    system_Init();
152

153 154 155 156
    /* Initialize the module bank and load the configuration of the
     * main module. We need to do this at this stage to be able to display
     * a short help if required by the user. (short help == main module
     * options) */
157
    module_InitBank ();
158

159
    /* Get command line options that affect module loading. */
160
    if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
161
    {
162
        module_EndBank (false);
163
        return VLC_EGENERIC;
164
    }
165
    priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
166

167 168 169 170 171 172 173
    /* Find verbosity from VLC_VERBOSE environment variable */
    {
        char *env = getenv( "VLC_VERBOSE" );
        if( env != NULL )
            priv->i_verbose = atoi( env );
    }

174
    /* Announce who we are (TODO: only first instance?) */
175
    msg_Dbg( p_libvlc, "VLC media player - %s", VERSION_MESSAGE );
176
    msg_Dbg( p_libvlc, "%s", COPYRIGHT_MESSAGE );
177 178
    msg_Dbg( p_libvlc, "revision %s", psz_vlc_changeset );
    msg_Dbg( p_libvlc, "configured with %s", CONFIGURE_LINE );
179
    vlc_threads_setup (p_libvlc);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

    /* 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 ) )
    {
#ifdef WIN32
205 206 207
        MessageBox (NULL, TEXT("The command line options could not be parsed.\n"
                    "Make sure they are valid."), TEXT("VLC media player"),
                    MB_OK|MB_ICONERROR);
208 209 210 211 212 213
#endif
        module_EndBank (true);
        return VLC_EGENERIC;
    }
    priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );

214 215 216 217 218 219 220
    /*
     * Support for gettext
     */
#if defined( ENABLE_NLS ) \
     && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
    vlc_bindtextdomain (PACKAGE_NAME);
#endif
221
    /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
222
    msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
223

224
    if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
225 226 227
    {
        module_EndBank (true);
        return VLC_EEXITSUCCESS;
228
    }
229

230
    if( module_count <= 1 )
231
    {
232
        msg_Err( p_libvlc, "No plugins found! Check your VLC installation.");
233 234
        module_EndBank (true);
        return VLC_ENOITEM;
235 236
    }

237 238 239
#ifdef HAVE_DAEMON
    /* Check for daemon mode */
    if( var_InheritBool( p_libvlc, "daemon" ) )
240
    {
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        char *psz_pidfile = NULL;

        if( daemon( 1, 0) != 0 )
        {
            msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
            module_EndBank (true);
            return VLC_EEXIT;
        }
        b_daemon = true;

        /* lets check if we need to write the pidfile */
        psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
        if( psz_pidfile != NULL )
        {
            FILE *pidfile;
            pid_t i_pid = getpid ();
            msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
                               i_pid, psz_pidfile );
            pidfile = vlc_fopen( psz_pidfile,"w" );
            if( pidfile != NULL )
            {
                utf8_fprintf( pidfile, "%d", (int)i_pid );
                fclose( pidfile );
            }
            else
            {
                msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
                         psz_pidfile );
            }
        }
        free( psz_pidfile );
272
    }
273
#endif
274 275

/* FIXME: could be replaced by using Unix sockets */
276
#ifdef HAVE_DBUS
277

278
#define MPRIS_APPEND "/org/mpris/MediaPlayer2/TrackList/Append"
279 280 281 282
#define MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
#define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
#define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList"

283 284 285
    if( var_InheritBool( p_libvlc, "one-instance" )
    || ( var_InheritBool( p_libvlc, "one-instance-when-started-from-file" )
      && var_InheritBool( p_libvlc, "started-from-file" ) ) )
286
    {
287 288 289 290 291 292 293 294
        for( int i = vlc_optind; i < i_argc; i++ )
            if( ppsz_argv[i][0] == ':' )
            {
                msg_Err( p_libvlc, "item option %s incompatible with single instance",
                         ppsz_argv[i] );
                goto dbus_out;
            }

295
        /* Initialise D-Bus interface, check for other instances */
296
        dbus_threads_init_default();
297

298 299
        DBusError err;
        dbus_error_init( &err );
300 301

        /* connect to the session bus */
302 303
        DBusConnection  *conn = dbus_bus_get( DBUS_BUS_SESSION, &err );
        if( conn == NULL )
304 305
        {
            msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
306 307 308
                    err.message );
            dbus_error_free( &err );
            goto dbus_out;
309
        }
310 311 312 313 314 315

        /* check if VLC is available on the bus
         * if not: D-Bus control is not enabled on the other
         * instance and we can't pass MRLs to it */
        /* FIXME: This check is totally brain-dead and buggy. */
        if( !dbus_bus_name_has_owner( conn, MPRIS_BUS_NAME, &err ) )
316
        {
317 318
            dbus_connection_unref( conn );
            if( dbus_error_is_set( &err ) )
319
            {
320
                msg_Err( p_libvlc, "D-Bus error: %s", err.message );
321 322
            }
            else
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
                msg_Dbg( p_libvlc, "No media player running. Continuing normally." );
            dbus_error_free( &err );
            goto dbus_out;
        }

        const dbus_bool_t play = !var_InheritBool( p_libvlc, "playlist-enqueue" );

        msg_Warn( p_libvlc, "media player running. Exiting...");
        for( int i = vlc_optind; i < i_argc; i++ )
        {
            DBusMessage *msg = dbus_message_new_method_call(
               MPRIS_BUS_NAME, MPRIS_OBJECT_PATH, MPRIS_TRACKLIST_INTERFACE, "AddTrack" );
            if( unlikely(msg == NULL) )
                continue;

            /* We need to resolve relative paths in this instance */
            char *mrl;
            if( strstr( ppsz_argv[i], "://" ) )
                mrl = strdup( ppsz_argv[i] );
            else
                mrl = vlc_path2uri( ppsz_argv[i], NULL );
            if( mrl == NULL )
345
            {
346 347
                dbus_message_unref( msg );
                continue;
348
            }
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

            const char *after_track = MPRIS_APPEND;

            /* append MRLs */
            if( !dbus_message_append_args( msg, DBUS_TYPE_STRING, &mrl,
                                                DBUS_TYPE_STRING, &after_track,
                                                DBUS_TYPE_BOOLEAN, &play,
                                                DBUS_TYPE_INVALID ) )
            {
                 dbus_message_unref( msg );
                 msg = NULL;
            }
            free( mrl );
            if( unlikely(msg == NULL) )
                continue;

            msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );

            /* send message and get a handle for a reply */
            DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
                                                                            &err );
            dbus_message_unref( msg );
            if( reply == NULL )
            {
                msg_Err( p_libvlc, "D-Bus error: %s", err.message );
                continue;
            }
            dbus_message_unref( reply );
377 378
        }
        /* we unreference the connection when we've finished with it */
379 380
        dbus_connection_unref( conn );
        exit( 1 );
381
    }
382
#undef MPRIS_APPEND
383 384 385
#undef MPRIS_BUS_NAME
#undef MPRIS_OBJECT_PATH
#undef MPRIS_TRACKLIST_INTERFACE
386
dbus_out:
387
#endif // HAVE_DBUS
388

389 390 391
    /*
     * Message queue options
     */
392
    /* Last chance to set the verbosity. Once we start interfaces and other
393
     * threads, verbosity becomes read-only. */
394
    var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
395
    if( var_InheritBool( p_libvlc, "quiet" ) )
396
    {
397 398
        var_SetInteger( p_libvlc, "verbose", -1 );
        priv->i_verbose = -1;
399
    }
400
    if( priv->b_color )
401
        priv->b_color = var_InheritBool( p_libvlc, "color" );
402

403
    vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
404
    vlc_object_set_name( p_libvlc, "main" );
405

406
    priv->b_stats = var_InheritBool( p_libvlc, "stats" );
407 408 409 410

    /*
     * Initialize hotkey handling
     */
411
    priv->actions = vlc_InitActions( p_libvlc );
412

413
    /* Create a variable for showing the fullscreen interface */
414 415
    var_Create( p_libvlc, "intf-toggle-fscontrol", VLC_VAR_BOOL );
    var_SetBool( p_libvlc, "intf-toggle-fscontrol", true );
416

417 418 419
    /* Create a variable for the Boss Key */
    var_Create( p_libvlc, "intf-boss", VLC_VAR_VOID );

420 421 422
    /* Create a variable for showing the main interface */
    var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );

423 424 425
    /* Create a variable for showing the right click menu */
    var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );

426 427 428 429
    /* 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 );

430
    /* some default internal settings */
431
    var_Create( p_libvlc, "window", VLC_VAR_STRING );
432 433
    var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
    var_SetString( p_libvlc, "user-agent", "(LibVLC "VERSION")" );
434

435
    /* System specific configuration */
436
    system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
437

Srikanth Raju's avatar
Srikanth Raju committed
438 439
#if defined(MEDIA_LIBRARY)
    /* Get the ML */
Rémi Duraffort's avatar
Rémi Duraffort committed
440
    if( var_GetBool( p_libvlc, "load-media-library-on-startup" ) )
Srikanth Raju's avatar
Srikanth Raju committed
441
    {
442
        priv->p_ml = ml_Create( VLC_OBJECT( p_libvlc ), NULL );
Srikanth Raju's avatar
Srikanth Raju committed
443 444 445 446 447 448 449 450 451 452 453 454
        if( !priv->p_ml )
        {
            msg_Err( p_libvlc, "ML initialization failed" );
            return VLC_EGENERIC;
        }
    }
    else
    {
        priv->p_ml = NULL;
    }
#endif

455
#ifdef ENABLE_VLM
456
    /* Initialize VLM if vlm-conf is specified */
457 458
    psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" );
    if( psz_parser )
459
    {
460 461 462
        priv->p_vlm = vlm_New( p_libvlc );
        if( !priv->p_vlm )
            msg_Err( p_libvlc, "VLM initialization failed" );
463
    }
464 465
    free( psz_parser );
#endif
466

467 468 469
    /*
     * Load background interfaces
     */
470 471
    psz_modules = var_CreateGetNonEmptyString( p_libvlc, "extraintf" );
    psz_control = var_CreateGetNonEmptyString( p_libvlc, "control" );
472

473
    if( psz_modules && psz_control )
474
    {
475 476 477 478 479 480
        char* psz_tmp;
        if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
        {
            free( psz_modules );
            psz_modules = psz_tmp;
        }
481
    }
482
    else if( psz_control )
483
    {
484 485
        free( psz_modules );
        psz_modules = strdup( psz_control );
486 487
    }

488 489 490 491 492 493 494 495 496 497 498
    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++;
        }
499
        if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
500
        {
501
            intf_Create( p_libvlc, psz_temp );
502 503 504 505 506 507
            free( psz_temp );
        }
    }
    free( psz_modules );
    free( psz_control );

508 509 510 511 512
    if( var_InheritBool( p_libvlc, "file-logging" )
#ifdef HAVE_SYSLOG_H
        && !var_InheritBool( p_libvlc, "syslog" )
#endif
        )
513
    {
514
        intf_Create( p_libvlc, "logger,none" );
515 516
    }
#ifdef HAVE_SYSLOG_H
517
    if( var_InheritBool( p_libvlc, "syslog" ) )
518
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
519
        char *logmode = var_CreateGetNonEmptyString( p_libvlc, "logmode" );
520
        var_SetString( p_libvlc, "logmode", "syslog" );
521
        intf_Create( p_libvlc, "logger,none" );
522

523 524 525 526 527
        if( logmode )
        {
            var_SetString( p_libvlc, "logmode", logmode );
            free( logmode );
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
528
        var_Destroy( p_libvlc, "logmode" );
529 530 531
    }
#endif

532
    if( var_InheritBool( p_libvlc, "network-synchronisation") )
533
    {
534
        intf_Create( p_libvlc, "netsync,none" );
535
    }
536

537
#ifdef __APPLE__
538 539 540 541 542 543 544 545
    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 );
546
    var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
547
#endif
548
#if defined (WIN32) || defined (__OS2__)
549
    var_Create( p_libvlc, "drawable-hwnd", VLC_VAR_INTEGER );
550
#endif
551 552

    /*
553 554 555
     * Get input filenames given as commandline arguments.
     * We assume that the remaining parameters are filenames
     * and their input options.
556
     */
557
    GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
558 559 560 561

    /*
     * Get --open argument
     */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
562
    psz_val = var_InheritString( p_libvlc, "open" );
563
    if ( psz_val != NULL )
564
    {
565
        playlist_AddExt( pl_Get(p_libvlc), psz_val, NULL, PLAYLIST_INSERT, 0,
566
                         -1, 0, NULL, 0, true, pl_Unlocked );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
567
        free( psz_val );
568
    }
569

570
    return VLC_SUCCESS;
571 572 573
}

/**
574 575
 * Cleanup a libvlc instance. The instance is not completely deallocated
 * \param p_libvlc the instance to clean
576
 */
577
void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
578
{
579
    libvlc_priv_t *priv = libvlc_priv (p_libvlc);
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
580

581 582
    /* Ask the interfaces to stop and destroy them */
    msg_Dbg( p_libvlc, "removing all interfaces" );
583
    libvlc_Quit( p_libvlc );
584
    intf_DestroyAll( p_libvlc );
585

586
#ifdef ENABLE_VLM
587 588 589 590 591 592 593
    /* Destroy VLM if created in libvlc_InternalInit */
    if( priv->p_vlm )
    {
        vlm_Delete( priv->p_vlm );
    }
#endif

594 595 596 597 598 599 600 601 602 603
#if defined(MEDIA_LIBRARY)
    media_library_t* p_ml = priv->p_ml;
    if( p_ml )
    {
        ml_Destroy( VLC_OBJECT( p_ml ) );
        vlc_object_release( p_ml );
        libvlc_priv(p_playlist->p_libvlc)->p_ml = NULL;
    }
#endif

604
    /* Free playlist now, all threads are gone */
605
    playlist_t *p_playlist = libvlc_priv (p_libvlc)->p_playlist;
606 607
    if( p_playlist != NULL )
        playlist_Destroy( p_playlist );
608 609 610

    msg_Dbg( p_libvlc, "removing stats" );

KO Myung-Hun's avatar
KO Myung-Hun committed
611
#if !defined( WIN32 ) && !defined( __OS2__ )
612 613 614 615
    char* psz_pidfile = NULL;

    if( b_daemon )
    {
616
        psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
617 618 619 620 621 622 623 624 625 626 627 628 629
        if( psz_pidfile != NULL )
        {
            msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
            if( unlink( psz_pidfile ) == -1 )
            {
                msg_Dbg( p_libvlc, "removing pid file %s: %m",
                        psz_pidfile );
            }
        }
        free( psz_pidfile );
    }
#endif

630 631 632 633
    /* Save the configuration */
    if( !var_InheritBool( p_libvlc, "ignore-config" ) )
        config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );

634
    /* Free module bank. It is refcounted, so we call this each time  */
635
    module_EndBank (true);
636

637
    vlc_DeinitActions( p_libvlc, priv->actions );
638
#if defined(WIN32) || defined(__OS2__)
639 640
    system_End( );
#endif
641 642 643 644 645 646 647 648 649 650 651 652
}

/**
 * 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 );
653 654

    /* Destroy mutexes */
655
    vlc_ExitDestroy( &priv->exit );
Srikanth Raju's avatar
Srikanth Raju committed
656
    vlc_mutex_destroy( &priv->ml_lock );
657

658
    assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
659
    vlc_object_release( p_libvlc );
660 661 662
}

/**
663
 * Add an interface plugin and run it
664
 */
665
int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
666
{
667 668 669 670 671
    if( !p_libvlc )
        return VLC_EGENERIC;

    if( !psz_module ) /* requesting the default interface */
    {
672 673
        char *psz_interface = var_CreateGetNonEmptyString( p_libvlc, "intf" );
        if( !psz_interface ) /* "intf" has not been set */
674
        {
KO Myung-Hun's avatar
KO Myung-Hun committed
675
#if !defined( WIN32 ) && !defined( __OS2__ )
676 677 678 679 680 681
            if( b_daemon )
                 /* Daemon mode hack.
                  * We prefer the dummy interface if none is specified. */
                psz_module = "dummy";
            else
#endif
682 683 684
                msg_Info( p_libvlc, "%s",
                          _("Running vlc with the default interface. "
                            "Use 'cvlc' to use vlc without interface.") );
685 686
        }
        free( psz_interface );
687
        var_Destroy( p_libvlc, "intf" );
688 689 690
    }

    /* Try to create the interface */
691 692
    int ret = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
    if( ret )
693
        msg_Err( p_libvlc, "interface \"%s\" initialization failed",
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
694
                 psz_module ? psz_module : "default" );
695
    return ret;
696
}
697 698 699 700 701 702 703

/*****************************************************************************
 * 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 ":".
 *****************************************************************************/
704 705
static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
                          const char *const args[] )
706
{
707
    while( n > 0 )
708 709
    {
        /* Count the input options */
710 711 712
        unsigned i_options = 0;

        while( args[--n][0] == ':' )
713 714
        {
            i_options++;
715 716 717 718 719
            if( n == 0 )
            {
                msg_Warn( p_vlc, "options %s without item", args[n] );
                return; /* syntax!? */
            }
720 721
        }

722 723 724 725 726 727 728
        char *mrl = NULL;
        if( strstr( args[n], "://" ) == NULL )
        {
            mrl = vlc_path2uri( args[n], NULL );
            if( !mrl )
                continue;
        }
729

730 731 732 733
        playlist_AddExt( pl_Get( p_vlc ), (mrl != NULL) ? mrl : args[n], NULL,
                         PLAYLIST_INSERT, 0, -1, i_options,
                         ( i_options ? &args[n + 1] : NULL ),
                         VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );
734
        free( mrl );
735
    }
736
}