]> git.sesse.net Git - vlc/blob - src/libvlc.c
arm_neon: updated syntax to suit Apple's current, more strict assembler
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: libvlc instances creation and deletion, interfaces handling
3  *****************************************************************************
4  * Copyright (C) 1998-2008 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Vincent Seguin <seguin@via.ecp.fr>
8  *          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  *
13  * This program is free software; you can redistribute it and/or modify it
14  * under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation; either version 2.1 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 /** \file
29  * This file contains functions to create and destroy libvlc instances
30  */
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <vlc_common.h>
40 #include "../lib/libvlc_internal.h"
41 #include <vlc_input.h>
42
43 #include "modules/modules.h"
44 #include "config/configuration.h"
45
46 #include <stdio.h>                                              /* sprintf() */
47 #include <string.h>
48 #include <stdlib.h>                                                /* free() */
49
50 #include "config/vlc_getopt.h"
51
52 #ifdef HAVE_UNISTD_H
53 #   include <unistd.h> /* isatty() */
54 #endif
55
56 #ifdef HAVE_DBUS
57 /* used for one-instance mode */
58 #   include <dbus/dbus.h>
59 #endif
60
61
62 #include <vlc_media_library.h>
63 #include <vlc_playlist.h>
64 #include <vlc_interface.h>
65
66 #include <vlc_charset.h>
67 #include <vlc_fs.h>
68 #include <vlc_cpu.h>
69 #include <vlc_url.h>
70 #include <vlc_modules.h>
71
72 #include "libvlc.h"
73
74 #include "playlist/playlist_internal.h"
75
76 #include <vlc_vlm.h>
77
78 #ifdef __APPLE__
79 # include <libkern/OSAtomic.h>
80 #endif
81
82 #include <assert.h>
83
84 /*****************************************************************************
85  * The evil global variables. We handle them with care, don't worry.
86  *****************************************************************************/
87
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 [] );
96
97 /**
98  * Allocate a libvlc instance, initialize global data if needed
99  * It also initializes the threading system
100  */
101 libvlc_int_t * libvlc_InternalCreate( void )
102 {
103     libvlc_int_t *p_libvlc;
104     libvlc_priv_t *priv;
105     char *psz_env = NULL;
106
107     /* Now that the thread system is initialized, we don't have much, but
108      * at least we have variables */
109     /* Allocate a libvlc instance object */
110     p_libvlc = vlc_custom_create( (vlc_object_t *)NULL, sizeof (*priv),
111                                   "libvlc" );
112     if( p_libvlc == NULL )
113         return NULL;
114
115     priv = libvlc_priv (p_libvlc);
116     priv->p_playlist = NULL;
117     priv->p_ml = NULL;
118     priv->p_dialog_provider = NULL;
119     priv->p_vlm = NULL;
120
121     /* Find verbosity from VLC_VERBOSE environment variable */
122     psz_env = getenv( "VLC_VERBOSE" );
123     if( psz_env != NULL )
124         priv->i_verbose = atoi( psz_env );
125     else
126         priv->i_verbose = 3;
127 #if defined( HAVE_ISATTY ) && !defined( WIN32 )
128     priv->b_color = isatty( 2 ); /* 2 is for stderr */
129 #else
130     priv->b_color = false;
131 #endif
132
133     /* Initialize mutexes */
134     vlc_mutex_init( &priv->ml_lock );
135     vlc_ExitInit( &priv->exit );
136
137     return p_libvlc;
138 }
139
140 /**
141  * Initialize a libvlc instance
142  * This function initializes a previously allocated libvlc instance:
143  *  - CPU detection
144  *  - gettext initialization
145  *  - message queue, module bank and playlist initialization
146  *  - configuration and commandline parsing
147  */
148 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
149                          const char *ppsz_argv[] )
150 {
151     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
152     char *       psz_modules = NULL;
153     char *       psz_parser = NULL;
154     char *       psz_control = NULL;
155     playlist_t  *p_playlist = NULL;
156     char        *psz_val;
157
158     /* System specific initialization code */
159     system_Init();
160
161     /* Initialize the module bank and load the configuration of the
162      * main module. We need to do this at this stage to be able to display
163      * a short help if required by the user. (short help == main module
164      * options) */
165     module_InitBank ();
166
167     /* Get command line options that affect module loading. */
168     if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
169     {
170         module_EndBank (false);
171         return VLC_EGENERIC;
172     }
173     priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
174
175     /* Announce who we are (TODO: only first instance?) */
176     msg_Dbg( p_libvlc, "VLC media player - %s", VERSION_MESSAGE );
177     msg_Dbg( p_libvlc, "%s", COPYRIGHT_MESSAGE );
178     msg_Dbg( p_libvlc, "revision %s", psz_vlc_changeset );
179     msg_Dbg( p_libvlc, "configured with %s", CONFIGURE_LINE );
180
181     /* Load the builtins and plugins into the module_bank.
182      * We have to do it before config_Load*() because this also gets the
183      * list of configuration options exported by each module and loads their
184      * default values. */
185     size_t module_count = module_LoadPlugins (p_libvlc);
186
187     /*
188      * Override default configuration with config file settings
189      */
190     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
191     {
192         if( var_InheritBool( p_libvlc, "reset-config" ) )
193             config_SaveConfigFile( p_libvlc ); /* Save default config */
194         else
195             config_LoadConfigFile( p_libvlc );
196     }
197
198     /*
199      * Override configuration with command line settings
200      */
201     int vlc_optind;
202     if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
203     {
204 #ifdef WIN32
205         MessageBox (NULL, TEXT("The command line options could not be parsed.\n"
206                     "Make sure they are valid."), TEXT("VLC media player"),
207                     MB_OK|MB_ICONERROR);
208 #endif
209         module_EndBank (true);
210         return VLC_EGENERIC;
211     }
212     priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
213
214     /*
215      * Support for gettext
216      */
217 #if defined( ENABLE_NLS ) \
218      && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
219     vlc_bindtextdomain (PACKAGE_NAME);
220 #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         module_EndBank (true);
227         return VLC_EEXITSUCCESS;
228     }
229
230     if( module_count <= 1 )
231     {
232         msg_Err( p_libvlc, "No plugins found! Check your VLC installation.");
233         module_EndBank (true);
234         return VLC_ENOITEM;
235     }
236
237 #ifdef HAVE_DAEMON
238     /* Check for daemon mode */
239     if( var_InheritBool( p_libvlc, "daemon" ) )
240     {
241         char *psz_pidfile = NULL;
242
243         if( daemon( 1, 0) != 0 )
244         {
245             msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
246             module_EndBank (true);
247             return VLC_EEXIT;
248         }
249         b_daemon = true;
250
251         /* lets check if we need to write the pidfile */
252         psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
253         if( psz_pidfile != NULL )
254         {
255             FILE *pidfile;
256             pid_t i_pid = getpid ();
257             msg_Dbg( p_libvlc, "PID is %d, writing it to %s",
258                                i_pid, psz_pidfile );
259             pidfile = vlc_fopen( psz_pidfile,"w" );
260             if( pidfile != NULL )
261             {
262                 utf8_fprintf( pidfile, "%d", (int)i_pid );
263                 fclose( pidfile );
264             }
265             else
266             {
267                 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
268                          psz_pidfile );
269             }
270         }
271         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 #define MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
280 #define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
281 #define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList"
282
283     dbus_threads_init_default();
284
285     if( var_InheritBool( p_libvlc, "one-instance" )
286     || ( var_InheritBool( p_libvlc, "one-instance-when-started-from-file" )
287       && var_InheritBool( p_libvlc, "started-from-file" ) ) )
288     {
289         /* Initialise D-Bus interface, check for other instances */
290         DBusConnection  *p_conn = NULL;
291         DBusError       dbus_error;
292
293         dbus_error_init( &dbus_error );
294
295         /* connect to the session bus */
296         p_conn = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
297         if( !p_conn )
298         {
299             msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
300                     dbus_error.message );
301             dbus_error_free( &dbus_error );
302         }
303         else
304         {
305             /* check if VLC is available on the bus
306              * if not: D-Bus control is not enabled on the other
307              * instance and we can't pass MRLs to it */
308             if( !dbus_bus_name_has_owner( p_conn, MPRIS_BUS_NAME, &dbus_error ) )
309             {
310                 if( dbus_error_is_set( &dbus_error ) )
311                 {
312                     msg_Err( p_libvlc, "D-Bus error: %s", dbus_error.message );
313                     dbus_error_free( &dbus_error );
314                 }
315                 else
316                     msg_Dbg( p_libvlc, "No Media Player is running. "
317                             "Continuing normally." );
318             }
319             else
320             {
321                 int i_input;
322                 DBusMessage* p_dbus_msg = NULL;
323                 DBusMessageIter dbus_args;
324                 DBusPendingCall* p_dbus_pending = NULL;
325                 dbus_bool_t b_play;
326
327                 msg_Warn( p_libvlc, "Another Media Player is running. Exiting");
328
329                 for( i_input = vlc_optind; i_input < i_argc;i_input++ )
330                 {
331                     /* Skip input options, we can't pass them through D-Bus */
332                     if( ppsz_argv[i_input][0] == ':' )
333                     {
334                         msg_Warn( p_libvlc, "Ignoring option %s",
335                                   ppsz_argv[i_input] );
336                         continue;
337                     }
338
339                     /* We need to resolve relative paths in this instance */
340                     char *psz_mrl = make_URI( ppsz_argv[i_input], NULL );
341                     const char *psz_after_track = MPRIS_APPEND;
342
343                     if( psz_mrl == NULL )
344                         continue;
345                     msg_Dbg( p_libvlc, "Adds %s to the running Media Player",
346                              psz_mrl );
347
348                     p_dbus_msg = dbus_message_new_method_call(
349                         MPRIS_BUS_NAME, MPRIS_OBJECT_PATH,
350                         MPRIS_TRACKLIST_INTERFACE, "AddTrack" );
351
352                     if ( NULL == p_dbus_msg )
353                     {
354                         msg_Err( p_libvlc, "D-Bus problem" );
355                         free( psz_mrl );
356                         system_End( );
357                         exit( 1 );
358                     }
359
360                     /* append MRLs */
361                     dbus_message_iter_init_append( p_dbus_msg, &dbus_args );
362                     if ( !dbus_message_iter_append_basic( &dbus_args,
363                                 DBUS_TYPE_STRING, &psz_mrl ) )
364                     {
365                         dbus_message_unref( p_dbus_msg );
366                         free( psz_mrl );
367                         system_End( );
368                         exit( 1 );
369                     }
370                     free( psz_mrl );
371
372                     if( !dbus_message_iter_append_basic( &dbus_args,
373                                 DBUS_TYPE_OBJECT_PATH, &psz_after_track ) )
374                     {
375                         dbus_message_unref( p_dbus_msg );
376                         system_End( );
377                         exit( 1 );
378                     }
379
380                     b_play = TRUE;
381                     if( var_InheritBool( p_libvlc, "playlist-enqueue" ) )
382                         b_play = FALSE;
383
384                     if ( !dbus_message_iter_append_basic( &dbus_args,
385                                 DBUS_TYPE_BOOLEAN, &b_play ) )
386                     {
387                         dbus_message_unref( p_dbus_msg );
388                         system_End( );
389                         exit( 1 );
390                     }
391
392                     /* send message and get a handle for a reply */
393                     if ( !dbus_connection_send_with_reply ( p_conn,
394                                 p_dbus_msg, &p_dbus_pending, -1 ) )
395                     {
396                         msg_Err( p_libvlc, "D-Bus problem" );
397                         dbus_message_unref( p_dbus_msg );
398                         system_End( );
399                         exit( 1 );
400                     }
401
402                     if ( NULL == p_dbus_pending )
403                     {
404                         msg_Err( p_libvlc, "D-Bus problem" );
405                         dbus_message_unref( p_dbus_msg );
406                         system_End( );
407                         exit( 1 );
408                     }
409                     dbus_connection_flush( p_conn );
410                     dbus_message_unref( p_dbus_msg );
411                     /* block until we receive a reply */
412                     dbus_pending_call_block( p_dbus_pending );
413                     dbus_pending_call_unref( p_dbus_pending );
414                 } /* processes all command line MRLs */
415
416                 /* bye bye */
417                 system_End( );
418                 exit( 0 );
419             }
420         }
421         /* we unreference the connection when we've finished with it */
422         if( p_conn ) dbus_connection_unref( p_conn );
423     }
424
425 #undef MPRIS_APPEND
426 #undef MPRIS_BUS_NAME
427 #undef MPRIS_OBJECT_PATH
428 #undef MPRIS_TRACKLIST_INTERFACE
429
430 #endif // HAVE_DBUS
431
432     /*
433      * Message queue options
434      */
435     /* Last chance to set the verbosity. Once we start interfaces and other
436      * threads, verbosity becomes read-only. */
437     var_Create( p_libvlc, "verbose", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
438     if( var_InheritBool( p_libvlc, "quiet" ) )
439     {
440         var_SetInteger( p_libvlc, "verbose", -1 );
441         priv->i_verbose = -1;
442     }
443     vlc_threads_setup( p_libvlc );
444
445     if( priv->b_color )
446         priv->b_color = var_InheritBool( p_libvlc, "color" );
447
448     vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
449     /*
450      * Choose the best memcpy module
451      */
452     priv->p_memcpy_module = module_need( p_libvlc, "memcpy", "$memcpy", false );
453     /* Avoid being called "memcpy":*/
454     vlc_object_set_name( p_libvlc, "main" );
455
456     priv->b_stats = var_InheritBool( p_libvlc, "stats" );
457
458     /*
459      * Initialize hotkey handling
460      */
461     priv->actions = vlc_InitActions( p_libvlc );
462
463     /* Create a variable for showing the fullscreen interface */
464     var_Create( p_libvlc, "intf-toggle-fscontrol", VLC_VAR_BOOL );
465     var_SetBool( p_libvlc, "intf-toggle-fscontrol", true );
466
467     /* Create a variable for the Boss Key */
468     var_Create( p_libvlc, "intf-boss", VLC_VAR_VOID );
469
470     /* Create a variable for showing the main interface */
471     var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
472
473     /* Create a variable for showing the right click menu */
474     var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );
475
476     /* variables for signalling creation of new files */
477     var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
478     var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
479
480     /* some default internal settings */
481     var_Create( p_libvlc, "window", VLC_VAR_STRING );
482     var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
483     var_SetString( p_libvlc, "user-agent", "(LibVLC "VERSION")" );
484
485     /* Initialize playlist and get commandline files */
486     p_playlist = playlist_Create( VLC_OBJECT(p_libvlc) );
487     if( !p_playlist )
488     {
489         msg_Err( p_libvlc, "playlist initialization failed" );
490         if( priv->p_memcpy_module != NULL )
491         {
492             module_unneed( p_libvlc, priv->p_memcpy_module );
493         }
494         module_EndBank (true);
495         return VLC_EGENERIC;
496     }
497
498     /* System specific configuration */
499     system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
500
501 #if defined(MEDIA_LIBRARY)
502     /* Get the ML */
503     if( var_GetBool( p_libvlc, "load-media-library-on-startup" ) )
504     {
505         priv->p_ml = ml_Create( VLC_OBJECT( p_libvlc ), NULL );
506         if( !priv->p_ml )
507         {
508             msg_Err( p_libvlc, "ML initialization failed" );
509             return VLC_EGENERIC;
510         }
511     }
512     else
513     {
514         priv->p_ml = NULL;
515     }
516 #endif
517
518     /* Add service discovery modules */
519     psz_modules = var_InheritString( p_libvlc, "services-discovery" );
520     if( psz_modules )
521     {
522         char *p = psz_modules, *m;
523         while( ( m = strsep( &p, " :," ) ) != NULL )
524             playlist_ServicesDiscoveryAdd( p_playlist, m );
525         free( psz_modules );
526     }
527
528 #ifdef ENABLE_VLM
529     /* Initialize VLM if vlm-conf is specified */
530     psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" );
531     if( psz_parser )
532     {
533         priv->p_vlm = vlm_New( p_libvlc );
534         if( !priv->p_vlm )
535             msg_Err( p_libvlc, "VLM initialization failed" );
536     }
537     free( psz_parser );
538 #endif
539
540     /*
541      * Load background interfaces
542      */
543     psz_modules = var_CreateGetNonEmptyString( p_libvlc, "extraintf" );
544     psz_control = var_CreateGetNonEmptyString( p_libvlc, "control" );
545
546     if( psz_modules && psz_control )
547     {
548         char* psz_tmp;
549         if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
550         {
551             free( psz_modules );
552             psz_modules = psz_tmp;
553         }
554     }
555     else if( psz_control )
556     {
557         free( psz_modules );
558         psz_modules = strdup( psz_control );
559     }
560
561     psz_parser = psz_modules;
562     while ( psz_parser && *psz_parser )
563     {
564         char *psz_module, *psz_temp;
565         psz_module = psz_parser;
566         psz_parser = strchr( psz_module, ':' );
567         if ( psz_parser )
568         {
569             *psz_parser = '\0';
570             psz_parser++;
571         }
572         if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
573         {
574             intf_Create( p_libvlc, psz_temp );
575             free( psz_temp );
576         }
577     }
578     free( psz_modules );
579     free( psz_control );
580
581     /*
582      * Always load the hotkeys interface if it exists
583      */
584     intf_Create( p_libvlc, "hotkeys,none" );
585
586     if( var_InheritBool( p_libvlc, "file-logging" )
587 #ifdef HAVE_SYSLOG_H
588         && !var_InheritBool( p_libvlc, "syslog" )
589 #endif
590         )
591     {
592         intf_Create( p_libvlc, "logger,none" );
593     }
594 #ifdef HAVE_SYSLOG_H
595     if( var_InheritBool( p_libvlc, "syslog" ) )
596     {
597         char *logmode = var_CreateGetNonEmptyString( p_libvlc, "logmode" );
598         var_SetString( p_libvlc, "logmode", "syslog" );
599         intf_Create( p_libvlc, "logger,none" );
600
601         if( logmode )
602         {
603             var_SetString( p_libvlc, "logmode", logmode );
604             free( logmode );
605         }
606         var_Destroy( p_libvlc, "logmode" );
607     }
608 #endif
609
610     if( var_InheritBool( p_libvlc, "network-synchronisation") )
611     {
612         intf_Create( p_libvlc, "netsync,none" );
613     }
614
615 #ifdef __APPLE__
616     var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
617     var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
618     var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
619     var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
620     var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
621     var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
622     var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
623     var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
624     var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
625 #endif
626 #if defined (WIN32) || defined (__OS2__)
627     var_Create( p_libvlc, "drawable-hwnd", VLC_VAR_INTEGER );
628 #endif
629
630     /*
631      * Get input filenames given as commandline arguments.
632      * We assume that the remaining parameters are filenames
633      * and their input options.
634      */
635     GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
636
637     /*
638      * Get --open argument
639      */
640     psz_val = var_InheritString( p_libvlc, "open" );
641     if ( psz_val != NULL )
642     {
643         playlist_AddExt( p_playlist, psz_val, NULL, PLAYLIST_INSERT, 0,
644                          -1, 0, NULL, 0, true, pl_Unlocked );
645         free( psz_val );
646     }
647
648     return VLC_SUCCESS;
649 }
650
651 /**
652  * Cleanup a libvlc instance. The instance is not completely deallocated
653  * \param p_libvlc the instance to clean
654  */
655 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
656 {
657     libvlc_priv_t *priv = libvlc_priv (p_libvlc);
658     playlist_t    *p_playlist = libvlc_priv (p_libvlc)->p_playlist;
659
660     /* Deactivate the playlist */
661     msg_Dbg( p_libvlc, "deactivating the playlist" );
662     pl_Deactivate( p_libvlc );
663
664     /* Remove all services discovery */
665     msg_Dbg( p_libvlc, "removing all services discovery tasks" );
666     playlist_ServicesDiscoveryKillAll( p_playlist );
667
668     /* Ask the interfaces to stop and destroy them */
669     msg_Dbg( p_libvlc, "removing all interfaces" );
670     libvlc_Quit( p_libvlc );
671     intf_DestroyAll( p_libvlc );
672
673 #ifdef ENABLE_VLM
674     /* Destroy VLM if created in libvlc_InternalInit */
675     if( priv->p_vlm )
676     {
677         vlm_Delete( priv->p_vlm );
678     }
679 #endif
680
681 #if defined(MEDIA_LIBRARY)
682     media_library_t* p_ml = priv->p_ml;
683     if( p_ml )
684     {
685         ml_Destroy( VLC_OBJECT( p_ml ) );
686         vlc_object_release( p_ml );
687         libvlc_priv(p_playlist->p_libvlc)->p_ml = NULL;
688     }
689 #endif
690
691     /* Free playlist now, all threads are gone */
692     playlist_Destroy( p_playlist );
693
694     msg_Dbg( p_libvlc, "removing stats" );
695
696 #if !defined( WIN32 ) && !defined( __OS2__ )
697     char* psz_pidfile = NULL;
698
699     if( b_daemon )
700     {
701         psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
702         if( psz_pidfile != NULL )
703         {
704             msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
705             if( unlink( psz_pidfile ) == -1 )
706             {
707                 msg_Dbg( p_libvlc, "removing pid file %s: %m",
708                         psz_pidfile );
709             }
710         }
711         free( psz_pidfile );
712     }
713 #endif
714
715     if( priv->p_memcpy_module )
716     {
717         module_unneed( p_libvlc, priv->p_memcpy_module );
718         priv->p_memcpy_module = NULL;
719     }
720
721     /* Save the configuration */
722     if( !var_InheritBool( p_libvlc, "ignore-config" ) )
723         config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
724
725     /* Free module bank. It is refcounted, so we call this each time  */
726     module_EndBank (true);
727
728     vlc_DeinitActions( p_libvlc, priv->actions );
729 }
730
731 /**
732  * Destroy everything.
733  * This function requests the running threads to finish, waits for their
734  * termination, and destroys their structure.
735  * It stops the thread systems: no instance can run after this has run
736  * \param p_libvlc the instance to destroy
737  */
738 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
739 {
740     libvlc_priv_t *priv = libvlc_priv( p_libvlc );
741
742     system_End( );
743
744     /* Destroy mutexes */
745     vlc_ExitDestroy( &priv->exit );
746     vlc_mutex_destroy( &priv->ml_lock );
747
748 #ifndef NDEBUG /* Hack to dump leaked objects tree */
749     if( vlc_internals( p_libvlc )->i_refcount > 1 )
750         while( vlc_internals( p_libvlc )->i_refcount > 0 )
751             vlc_object_release( p_libvlc );
752 #endif
753
754     assert( vlc_internals( p_libvlc )->i_refcount == 1 );
755     vlc_object_release( p_libvlc );
756 }
757
758 /**
759  * Add an interface plugin and run it
760  */
761 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
762 {
763     if( !p_libvlc )
764         return VLC_EGENERIC;
765
766     if( !psz_module ) /* requesting the default interface */
767     {
768         char *psz_interface = var_CreateGetNonEmptyString( p_libvlc, "intf" );
769         if( !psz_interface ) /* "intf" has not been set */
770         {
771 #if !defined( WIN32 ) && !defined( __OS2__ )
772             if( b_daemon )
773                  /* Daemon mode hack.
774                   * We prefer the dummy interface if none is specified. */
775                 psz_module = "dummy";
776             else
777 #endif
778                 msg_Info( p_libvlc, "%s",
779                           _("Running vlc with the default interface. "
780                             "Use 'cvlc' to use vlc without interface.") );
781         }
782         free( psz_interface );
783         var_Destroy( p_libvlc, "intf" );
784     }
785
786     /* Try to create the interface */
787     int ret = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
788     if( ret )
789         msg_Err( p_libvlc, "interface \"%s\" initialization failed",
790                  psz_module ? psz_module : "default" );
791     return ret;
792 }
793
794 /*****************************************************************************
795  * GetFilenames: parse command line options which are not flags
796  *****************************************************************************
797  * Parse command line for input files as well as their associated options.
798  * An option always follows its associated input and begins with a ":".
799  *****************************************************************************/
800 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
801                           const char *const args[] )
802 {
803     while( n > 0 )
804     {
805         /* Count the input options */
806         unsigned i_options = 0;
807
808         while( args[--n][0] == ':' )
809         {
810             i_options++;
811             if( n == 0 )
812             {
813                 msg_Warn( p_vlc, "options %s without item", args[n] );
814                 return; /* syntax!? */
815             }
816         }
817
818         char *mrl = make_URI( args[n], NULL );
819         if( !mrl )
820             continue;
821
822         playlist_AddExt( pl_Get( p_vlc ), mrl, NULL, PLAYLIST_INSERT,
823                 0, -1, i_options, ( i_options ? &args[n + 1] : NULL ),
824                 VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );
825         free( mrl );
826     }
827 }