1 /*****************************************************************************
2 * libvlc.c: libvlc instances creation and deletion, interfaces handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 VLC authors and VideoLAN
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>
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.
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.
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 *****************************************************************************/
29 * This file contains functions to create and destroy libvlc instances
32 /*****************************************************************************
34 *****************************************************************************/
39 #include <vlc_common.h>
40 #include "../lib/libvlc_internal.h"
41 #include <vlc_input.h>
43 #include "modules/modules.h"
44 #include "config/configuration.h"
46 #include <stdio.h> /* sprintf() */
48 #include <stdlib.h> /* free() */
50 #include "config/vlc_getopt.h"
53 # include <unistd.h> /* isatty() */
57 /* used for one-instance mode */
58 # include <dbus/dbus.h>
62 #include <vlc_media_library.h>
63 #include <vlc_playlist.h>
64 #include <vlc_interface.h>
66 #include <vlc_charset.h>
70 #include <vlc_modules.h>
74 #include "playlist/playlist_internal.h"
79 # include <libkern/OSAtomic.h>
84 /*****************************************************************************
85 * The evil global variables. We handle them with care, don't worry.
86 *****************************************************************************/
88 #if !defined(WIN32) && !defined(__OS2__)
89 static bool b_daemon = false;
92 /*****************************************************************************
94 *****************************************************************************/
95 static void GetFilenames ( libvlc_int_t *, unsigned, const char *const [] );
98 * Allocate a libvlc instance, initialize global data if needed
99 * It also initializes the threading system
101 libvlc_int_t * libvlc_InternalCreate( void )
103 libvlc_int_t *p_libvlc;
106 /* Now that the thread system is initialized, we don't have much, but
107 * at least we have variables */
108 /* Allocate a libvlc instance object */
109 p_libvlc = vlc_custom_create( (vlc_object_t *)NULL, sizeof (*priv),
111 if( p_libvlc == NULL )
114 priv = libvlc_priv (p_libvlc);
115 priv->p_playlist = NULL;
117 priv->p_dialog_provider = 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 */
123 priv->b_color = false;
126 /* Initialize mutexes */
127 vlc_mutex_init( &priv->ml_lock );
128 vlc_ExitInit( &priv->exit );
134 * Initialize a libvlc instance
135 * This function initializes a previously allocated libvlc instance:
137 * - gettext initialization
138 * - message queue, module bank and playlist initialization
139 * - configuration and commandline parsing
141 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
142 const char *ppsz_argv[] )
144 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
145 char * psz_modules = NULL;
146 char * psz_parser = NULL;
147 char * psz_control = NULL;
148 playlist_t *p_playlist = NULL;
151 /* System specific initialization code */
154 /* Initialize the module bank and load the configuration of the
155 * main module. We need to do this at this stage to be able to display
156 * a short help if required by the user. (short help == main module
160 /* Get command line options that affect module loading. */
161 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
163 module_EndBank (false);
166 priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
168 /* Find verbosity from VLC_VERBOSE environment variable */
170 char *env = getenv( "VLC_VERBOSE" );
172 priv->i_verbose = atoi( env );
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 );
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
185 size_t module_count = module_LoadPlugins (p_libvlc);
188 * Override default configuration with config file settings
190 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
192 if( var_InheritBool( p_libvlc, "reset-config" ) )
193 config_SaveConfigFile( p_libvlc ); /* Save default config */
195 config_LoadConfigFile( p_libvlc );
199 * Override configuration with command line settings
202 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
205 MessageBox (NULL, TEXT("The command line options could not be parsed.\n"
206 "Make sure they are valid."), TEXT("VLC media player"),
209 module_EndBank (true);
212 priv->i_verbose = var_InheritInteger( p_libvlc, "verbose" );
215 * Support for gettext
217 #if defined( ENABLE_NLS ) \
218 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
219 vlc_bindtextdomain (PACKAGE_NAME);
221 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
222 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
224 if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
226 module_EndBank (true);
227 return VLC_EEXITSUCCESS;
230 if( module_count <= 1 )
232 msg_Err( p_libvlc, "No plugins found! Check your VLC installation.");
233 module_EndBank (true);
238 /* Check for daemon mode */
239 if( var_InheritBool( p_libvlc, "daemon" ) )
241 char *psz_pidfile = NULL;
243 if( daemon( 1, 0) != 0 )
245 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
246 module_EndBank (true);
251 /* lets check if we need to write the pidfile */
252 psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
253 if( psz_pidfile != NULL )
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 )
262 utf8_fprintf( pidfile, "%d", (int)i_pid );
267 msg_Err( p_libvlc, "cannot open pid file for writing: %s (%m)",
275 /* FIXME: could be replaced by using Unix sockets */
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"
283 if( var_InheritBool( p_libvlc, "one-instance" )
284 || ( var_InheritBool( p_libvlc, "one-instance-when-started-from-file" )
285 && var_InheritBool( p_libvlc, "started-from-file" ) ) )
287 for( int i = vlc_optind; i < i_argc; i++ )
288 if( ppsz_argv[i][0] == ':' )
290 msg_Err( p_libvlc, "item option %s incompatible with single instance",
295 /* Initialise D-Bus interface, check for other instances */
296 dbus_threads_init_default();
299 dbus_error_init( &err );
301 /* connect to the session bus */
302 DBusConnection *conn = dbus_bus_get( DBUS_BUS_SESSION, &err );
305 msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
307 dbus_error_free( &err );
311 /* check if VLC is available on the bus
312 * if not: D-Bus control is not enabled on the other
313 * instance and we can't pass MRLs to it */
314 /* FIXME: This check is totally brain-dead and buggy. */
315 if( !dbus_bus_name_has_owner( conn, MPRIS_BUS_NAME, &err ) )
317 dbus_connection_unref( conn );
318 if( dbus_error_is_set( &err ) )
320 msg_Err( p_libvlc, "D-Bus error: %s", err.message );
323 msg_Dbg( p_libvlc, "No media player running. Continuing normally." );
324 dbus_error_free( &err );
328 const dbus_bool_t play = !var_InheritBool( p_libvlc, "playlist-enqueue" );
330 msg_Warn( p_libvlc, "media player running. Exiting...");
331 for( int i = vlc_optind; i < i_argc; i++ )
333 DBusMessage *msg = dbus_message_new_method_call(
334 MPRIS_BUS_NAME, MPRIS_OBJECT_PATH, MPRIS_TRACKLIST_INTERFACE, "AddTrack" );
335 if( unlikely(msg == NULL) )
338 /* We need to resolve relative paths in this instance */
340 if( strstr( ppsz_argv[i], "://" ) )
341 mrl = strdup( ppsz_argv[i] );
343 mrl = vlc_path2uri( ppsz_argv[i], NULL );
346 dbus_message_unref( msg );
350 const char *after_track = MPRIS_APPEND;
353 if( !dbus_message_append_args( msg, DBUS_TYPE_STRING, &mrl,
354 DBUS_TYPE_STRING, &after_track,
355 DBUS_TYPE_BOOLEAN, &play,
356 DBUS_TYPE_INVALID ) )
358 dbus_message_unref( msg );
362 if( unlikely(msg == NULL) )
365 msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
367 /* send message and get a handle for a reply */
368 DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
370 dbus_message_unref( msg );
373 msg_Err( p_libvlc, "D-Bus error: %s", err.message );
376 dbus_message_unref( reply );
378 /* we unreference the connection when we've finished with it */
379 dbus_connection_unref( conn );
383 #undef MPRIS_BUS_NAME
384 #undef MPRIS_OBJECT_PATH
385 #undef MPRIS_TRACKLIST_INTERFACE
390 * 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" ) )
397 var_SetInteger( p_libvlc, "verbose", -1 );
398 priv->i_verbose = -1;
400 vlc_threads_setup( p_libvlc );
403 priv->b_color = var_InheritBool( p_libvlc, "color" );
405 vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
406 vlc_object_set_name( p_libvlc, "main" );
408 priv->b_stats = var_InheritBool( p_libvlc, "stats" );
411 * Initialize hotkey handling
413 priv->actions = vlc_InitActions( p_libvlc );
415 /* Create a variable for showing the fullscreen interface */
416 var_Create( p_libvlc, "intf-toggle-fscontrol", VLC_VAR_BOOL );
417 var_SetBool( p_libvlc, "intf-toggle-fscontrol", true );
419 /* Create a variable for the Boss Key */
420 var_Create( p_libvlc, "intf-boss", VLC_VAR_VOID );
422 /* Create a variable for showing the main interface */
423 var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
425 /* Create a variable for showing the right click menu */
426 var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );
428 /* variables for signalling creation of new files */
429 var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
430 var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
432 /* some default internal settings */
433 var_Create( p_libvlc, "window", VLC_VAR_STRING );
434 var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
435 var_SetString( p_libvlc, "user-agent", "(LibVLC "VERSION")" );
437 /* Initialize playlist and get commandline files */
438 p_playlist = playlist_Create( VLC_OBJECT(p_libvlc) );
441 msg_Err( p_libvlc, "playlist initialization failed" );
442 module_EndBank (true);
446 /* System specific configuration */
447 system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
449 #if defined(MEDIA_LIBRARY)
451 if( var_GetBool( p_libvlc, "load-media-library-on-startup" ) )
453 priv->p_ml = ml_Create( VLC_OBJECT( p_libvlc ), NULL );
456 msg_Err( p_libvlc, "ML initialization failed" );
466 /* Add service discovery modules */
467 psz_modules = var_InheritString( p_libvlc, "services-discovery" );
470 char *p = psz_modules, *m;
471 while( ( m = strsep( &p, " :," ) ) != NULL )
472 playlist_ServicesDiscoveryAdd( p_playlist, m );
477 /* Initialize VLM if vlm-conf is specified */
478 psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" );
481 priv->p_vlm = vlm_New( p_libvlc );
483 msg_Err( p_libvlc, "VLM initialization failed" );
489 * Load background interfaces
491 psz_modules = var_CreateGetNonEmptyString( p_libvlc, "extraintf" );
492 psz_control = var_CreateGetNonEmptyString( p_libvlc, "control" );
494 if( psz_modules && psz_control )
497 if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
500 psz_modules = psz_tmp;
503 else if( psz_control )
506 psz_modules = strdup( psz_control );
509 psz_parser = psz_modules;
510 while ( psz_parser && *psz_parser )
512 char *psz_module, *psz_temp;
513 psz_module = psz_parser;
514 psz_parser = strchr( psz_module, ':' );
520 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
522 intf_Create( p_libvlc, psz_temp );
530 * Always load the hotkeys interface if it exists
532 intf_Create( p_libvlc, "hotkeys,none" );
534 if( var_InheritBool( p_libvlc, "file-logging" )
536 && !var_InheritBool( p_libvlc, "syslog" )
540 intf_Create( p_libvlc, "logger,none" );
543 if( var_InheritBool( p_libvlc, "syslog" ) )
545 char *logmode = var_CreateGetNonEmptyString( p_libvlc, "logmode" );
546 var_SetString( p_libvlc, "logmode", "syslog" );
547 intf_Create( p_libvlc, "logger,none" );
551 var_SetString( p_libvlc, "logmode", logmode );
554 var_Destroy( p_libvlc, "logmode" );
558 if( var_InheritBool( p_libvlc, "network-synchronisation") )
560 intf_Create( p_libvlc, "netsync,none" );
564 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
565 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
566 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
567 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
568 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
569 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
570 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
571 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
572 var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
574 #if defined (WIN32) || defined (__OS2__)
575 var_Create( p_libvlc, "drawable-hwnd", VLC_VAR_INTEGER );
579 * Get input filenames given as commandline arguments.
580 * We assume that the remaining parameters are filenames
581 * and their input options.
583 GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
586 * Get --open argument
588 psz_val = var_InheritString( p_libvlc, "open" );
589 if ( psz_val != NULL )
591 playlist_AddExt( p_playlist, psz_val, NULL, PLAYLIST_INSERT, 0,
592 -1, 0, NULL, 0, true, pl_Unlocked );
600 * Cleanup a libvlc instance. The instance is not completely deallocated
601 * \param p_libvlc the instance to clean
603 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
605 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
606 playlist_t *p_playlist = libvlc_priv (p_libvlc)->p_playlist;
608 /* Deactivate the playlist */
609 msg_Dbg( p_libvlc, "deactivating the playlist" );
610 pl_Deactivate( p_libvlc );
612 /* Remove all services discovery */
613 msg_Dbg( p_libvlc, "removing all services discovery tasks" );
614 playlist_ServicesDiscoveryKillAll( p_playlist );
616 /* Ask the interfaces to stop and destroy them */
617 msg_Dbg( p_libvlc, "removing all interfaces" );
618 libvlc_Quit( p_libvlc );
619 intf_DestroyAll( p_libvlc );
622 /* Destroy VLM if created in libvlc_InternalInit */
625 vlm_Delete( priv->p_vlm );
629 #if defined(MEDIA_LIBRARY)
630 media_library_t* p_ml = priv->p_ml;
633 ml_Destroy( VLC_OBJECT( p_ml ) );
634 vlc_object_release( p_ml );
635 libvlc_priv(p_playlist->p_libvlc)->p_ml = NULL;
639 /* Free playlist now, all threads are gone */
640 playlist_Destroy( p_playlist );
642 msg_Dbg( p_libvlc, "removing stats" );
644 #if !defined( WIN32 ) && !defined( __OS2__ )
645 char* psz_pidfile = NULL;
649 psz_pidfile = var_CreateGetNonEmptyString( p_libvlc, "pidfile" );
650 if( psz_pidfile != NULL )
652 msg_Dbg( p_libvlc, "removing pid file %s", psz_pidfile );
653 if( unlink( psz_pidfile ) == -1 )
655 msg_Dbg( p_libvlc, "removing pid file %s: %m",
663 /* Save the configuration */
664 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
665 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
667 /* Free module bank. It is refcounted, so we call this each time */
668 module_EndBank (true);
670 vlc_DeinitActions( p_libvlc, priv->actions );
677 * Destroy everything.
678 * This function requests the running threads to finish, waits for their
679 * termination, and destroys their structure.
680 * It stops the thread systems: no instance can run after this has run
681 * \param p_libvlc the instance to destroy
683 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
685 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
687 /* Destroy mutexes */
688 vlc_ExitDestroy( &priv->exit );
689 vlc_mutex_destroy( &priv->ml_lock );
691 #ifndef NDEBUG /* Hack to dump leaked objects tree */
692 if( vlc_internals( p_libvlc )->i_refcount > 1 )
693 while( vlc_internals( p_libvlc )->i_refcount > 0 )
694 vlc_object_release( p_libvlc );
697 assert( vlc_internals( p_libvlc )->i_refcount == 1 );
698 vlc_object_release( p_libvlc );
702 * Add an interface plugin and run it
704 int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc, char const *psz_module )
709 if( !psz_module ) /* requesting the default interface */
711 char *psz_interface = var_CreateGetNonEmptyString( p_libvlc, "intf" );
712 if( !psz_interface ) /* "intf" has not been set */
714 #if !defined( WIN32 ) && !defined( __OS2__ )
717 * We prefer the dummy interface if none is specified. */
718 psz_module = "dummy";
721 msg_Info( p_libvlc, "%s",
722 _("Running vlc with the default interface. "
723 "Use 'cvlc' to use vlc without interface.") );
725 free( psz_interface );
726 var_Destroy( p_libvlc, "intf" );
729 /* Try to create the interface */
730 int ret = intf_Create( p_libvlc, psz_module ? psz_module : "$intf" );
732 msg_Err( p_libvlc, "interface \"%s\" initialization failed",
733 psz_module ? psz_module : "default" );
737 /*****************************************************************************
738 * GetFilenames: parse command line options which are not flags
739 *****************************************************************************
740 * Parse command line for input files as well as their associated options.
741 * An option always follows its associated input and begins with a ":".
742 *****************************************************************************/
743 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
744 const char *const args[] )
748 /* Count the input options */
749 unsigned i_options = 0;
751 while( args[--n][0] == ':' )
756 msg_Warn( p_vlc, "options %s without item", args[n] );
757 return; /* syntax!? */
762 if( strstr( args[n], "://" ) == NULL )
764 mrl = vlc_path2uri( args[n], NULL );
769 playlist_AddExt( pl_Get( p_vlc ), (mrl != NULL) ? mrl : args[n], NULL,
770 PLAYLIST_INSERT, 0, -1, i_options,
771 ( i_options ? &args[n + 1] : NULL ),
772 VLC_INPUT_OPTION_TRUSTED, true, pl_Unlocked );