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"
45 #include "playlist/preparser.h"
47 #include <stdio.h> /* sprintf() */
49 #include <stdlib.h> /* free() */
52 #include "config/vlc_getopt.h"
55 /* used for one-instance mode */
56 # include <dbus/dbus.h>
60 #include <vlc_playlist.h>
61 #include <vlc_interface.h>
63 #include <vlc_charset.h>
67 #include <vlc_modules.h>
70 #include "playlist/playlist_internal.h"
71 #include "misc/variables.h"
76 # include <libkern/OSAtomic.h>
81 /*****************************************************************************
83 *****************************************************************************/
84 static void GetFilenames ( libvlc_int_t *, unsigned, const char *const [] );
87 * Allocate a blank libvlc instance, also setting the exit handler.
88 * Vlc's threading system must have been initialized first
90 libvlc_int_t * libvlc_InternalCreate( void )
92 libvlc_int_t *p_libvlc;
95 /* Allocate a libvlc instance object */
96 p_libvlc = vlc_custom_create( (vlc_object_t *)NULL, sizeof (*priv),
98 if( p_libvlc == NULL )
101 priv = libvlc_priv (p_libvlc);
102 priv->playlist = NULL;
103 priv->p_dialog_provider = NULL;
106 vlc_ExitInit( &priv->exit );
112 * Initialize a libvlc instance
113 * This function initializes a previously allocated libvlc instance:
115 * - gettext initialization
116 * - message queue, module bank and playlist initialization
117 * - configuration and commandline parsing
119 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
120 const char *ppsz_argv[] )
122 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
123 char * psz_modules = NULL;
124 char * psz_parser = NULL;
125 char * psz_control = NULL;
128 /* System specific initialization code */
131 vlc_LogPreinit(p_libvlc);
133 /* Initialize the module bank and load the configuration of the
134 * core module. We need to do this at this stage to be able to display
135 * a short help if required by the user. (short help == core module
139 /* Get command line options that affect module loading. */
140 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
142 module_EndBank (false);
146 vlc_threads_setup (p_libvlc);
148 /* Load the builtins and plugins into the module_bank.
149 * We have to do it before config_Load*() because this also gets the
150 * list of configuration options exported by each module and loads their
152 size_t module_count = module_LoadPlugins (p_libvlc);
155 * Override default configuration with config file settings
157 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
159 if( var_InheritBool( p_libvlc, "reset-config" ) )
160 config_SaveConfigFile( p_libvlc ); /* Save default config */
162 config_LoadConfigFile( p_libvlc );
166 * Override configuration with command line settings
169 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
171 vlc_LogDeinit (p_libvlc);
172 module_EndBank (true);
176 vlc_LogInit(p_libvlc);
179 * Support for gettext
181 #if defined( ENABLE_NLS ) \
182 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
183 vlc_bindtextdomain (PACKAGE_NAME);
185 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
186 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
188 if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
190 module_EndBank (true);
194 if( module_count <= 1 )
196 msg_Err( p_libvlc, "No plugins found! Check your VLC installation.");
197 vlc_LogDeinit (p_libvlc);
198 module_EndBank (true);
203 /* Check for daemon mode */
204 if( var_InheritBool( p_libvlc, "daemon" ) )
206 if( daemon( 1, 0) != 0 )
208 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
209 vlc_LogDeinit (p_libvlc);
210 module_EndBank (true);
214 /* lets check if we need to write the pidfile */
215 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
216 if( pidfile != NULL )
218 FILE *stream = vlc_fopen( pidfile, "w" );
221 fprintf( stream, "%d", (int)getpid() );
223 msg_Dbg( p_libvlc, "written PID file %s", pidfile );
226 msg_Err( p_libvlc, "cannot write PID file %s: %s",
227 pidfile, vlc_strerror_c(errno) );
233 var_Create( p_libvlc, "pidfile", VLC_VAR_STRING );
234 var_SetString( p_libvlc, "pidfile", "" );
238 /* FIXME: could be replaced by using Unix sockets */
241 #define MPRIS_APPEND "/org/mpris/MediaPlayer2/TrackList/Append"
242 #define MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
243 #define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
244 #define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList"
246 if( var_InheritBool( p_libvlc, "one-instance" )
247 || ( var_InheritBool( p_libvlc, "one-instance-when-started-from-file" )
248 && var_InheritBool( p_libvlc, "started-from-file" ) ) )
250 for( int i = vlc_optind; i < i_argc; i++ )
251 if( ppsz_argv[i][0] == ':' )
253 msg_Err( p_libvlc, "item option %s incompatible with single instance",
258 /* Initialise D-Bus interface, check for other instances */
259 dbus_threads_init_default();
262 dbus_error_init( &err );
264 /* connect to the session bus */
265 DBusConnection *conn = dbus_bus_get( DBUS_BUS_SESSION, &err );
268 msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
270 dbus_error_free( &err );
274 /* check if VLC is available on the bus
275 * if not: D-Bus control is not enabled on the other
276 * instance and we can't pass MRLs to it */
277 /* FIXME: This check is totally brain-dead and buggy. */
278 if( !dbus_bus_name_has_owner( conn, MPRIS_BUS_NAME, &err ) )
280 dbus_connection_unref( conn );
281 if( dbus_error_is_set( &err ) )
283 msg_Err( p_libvlc, "D-Bus error: %s", err.message );
286 msg_Dbg( p_libvlc, "No media player running. Continuing normally." );
287 dbus_error_free( &err );
291 const dbus_bool_t play = !var_InheritBool( p_libvlc, "playlist-enqueue" );
293 msg_Warn( p_libvlc, "media player running. Exiting...");
294 for( int i = vlc_optind; i < i_argc; i++ )
296 DBusMessage *msg = dbus_message_new_method_call(
297 MPRIS_BUS_NAME, MPRIS_OBJECT_PATH, MPRIS_TRACKLIST_INTERFACE, "AddTrack" );
298 if( unlikely(msg == NULL) )
301 /* We need to resolve relative paths in this instance */
303 if( strstr( ppsz_argv[i], "://" ) )
304 mrl = strdup( ppsz_argv[i] );
306 mrl = vlc_path2uri( ppsz_argv[i], NULL );
309 dbus_message_unref( msg );
313 const char *after_track = MPRIS_APPEND;
316 if( !dbus_message_append_args( msg, DBUS_TYPE_STRING, &mrl,
317 DBUS_TYPE_OBJECT_PATH, &after_track,
318 DBUS_TYPE_BOOLEAN, &play,
319 DBUS_TYPE_INVALID ) )
321 dbus_message_unref( msg );
327 msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
330 /* send message and get a handle for a reply */
331 DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
333 dbus_message_unref( msg );
336 msg_Err( p_libvlc, "D-Bus error: %s", err.message );
339 dbus_message_unref( reply );
341 /* we unreference the connection when we've finished with it */
342 dbus_connection_unref( conn );
346 #undef MPRIS_BUS_NAME
347 #undef MPRIS_OBJECT_PATH
348 #undef MPRIS_TRACKLIST_INTERFACE
352 vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
354 priv->b_stats = var_InheritBool( p_libvlc, "stats" );
357 * Initialize hotkey handling
359 priv->actions = vlc_InitActions( p_libvlc );
364 priv->parser = playlist_preparser_New(VLC_OBJECT(p_libvlc));
366 /* Create a variable for showing the fullscreen interface */
367 var_Create( p_libvlc, "intf-toggle-fscontrol", VLC_VAR_BOOL );
368 var_SetBool( p_libvlc, "intf-toggle-fscontrol", true );
370 /* Create a variable for the Boss Key */
371 var_Create( p_libvlc, "intf-boss", VLC_VAR_VOID );
373 /* Create a variable for showing the main interface */
374 var_Create( p_libvlc, "intf-show", VLC_VAR_BOOL );
376 /* Create a variable for showing the right click menu */
377 var_Create( p_libvlc, "intf-popupmenu", VLC_VAR_BOOL );
379 /* variables for signalling creation of new files */
380 var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
381 var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
383 /* some default internal settings */
384 var_Create( p_libvlc, "window", VLC_VAR_STRING );
385 /* NOTE: Because the playlist and interfaces start before this function
386 * returns control to the application (DESIGN BUG!), all these variables
387 * must be created (in place of libvlc_new()) and set to VLC defaults
388 * (in place of VLC main()) *here*. */
389 var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
390 var_SetString( p_libvlc, "user-agent",
391 "VLC media player (LibVLC "VERSION")" );
392 var_Create( p_libvlc, "http-user-agent", VLC_VAR_STRING );
393 var_SetString( p_libvlc, "http-user-agent",
394 "VLC/"PACKAGE_VERSION" LibVLC/"PACKAGE_VERSION );
395 var_Create( p_libvlc, "app-icon-name", VLC_VAR_STRING );
396 var_SetString( p_libvlc, "app-icon-name", PACKAGE_NAME );
397 var_Create( p_libvlc, "app-id", VLC_VAR_STRING );
398 var_SetString( p_libvlc, "app-id", "org.VideoLAN.VLC" );
399 var_Create( p_libvlc, "app-version", VLC_VAR_STRING );
400 var_SetString( p_libvlc, "app-version", PACKAGE_VERSION );
402 /* System specific configuration */
403 system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
406 /* Initialize VLM if vlm-conf is specified */
407 psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" );
410 priv->p_vlm = vlm_New( p_libvlc );
412 msg_Err( p_libvlc, "VLM initialization failed" );
418 * Load background interfaces
420 psz_modules = var_CreateGetNonEmptyString( p_libvlc, "extraintf" );
421 psz_control = var_CreateGetNonEmptyString( p_libvlc, "control" );
423 if( psz_modules && psz_control )
426 if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
429 psz_modules = psz_tmp;
432 else if( psz_control )
435 psz_modules = strdup( psz_control );
438 psz_parser = psz_modules;
439 while ( psz_parser && *psz_parser )
441 char *psz_module, *psz_temp;
442 psz_module = psz_parser;
443 psz_parser = strchr( psz_module, ':' );
449 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
451 libvlc_InternalAddIntf( p_libvlc, psz_temp );
458 if( var_InheritBool( p_libvlc, "file-logging" ) )
459 libvlc_InternalAddIntf( p_libvlc, "logger,none" );
461 if( var_InheritBool( p_libvlc, "network-synchronisation") )
462 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
465 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
466 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
467 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
468 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
469 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
470 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
471 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
472 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
473 var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
477 * Get input filenames given as commandline arguments.
478 * We assume that the remaining parameters are filenames
479 * and their input options.
481 GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
484 * Get --open argument
486 psz_val = var_InheritString( p_libvlc, "open" );
487 if ( psz_val != NULL )
489 intf_InsertItem( p_libvlc, psz_val, 0, NULL, 0 );
497 * Cleanup a libvlc instance. The instance is not completely deallocated
498 * \param p_libvlc the instance to clean
500 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
502 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
504 /* Ask the interfaces to stop and destroy them */
505 msg_Dbg( p_libvlc, "removing all interfaces" );
506 libvlc_Quit( p_libvlc );
507 intf_DestroyAll( p_libvlc );
510 /* Destroy VLM if created in libvlc_InternalInit */
513 vlm_Delete( priv->p_vlm );
517 #if !defined( _WIN32 ) && !defined( __OS2__ )
518 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
519 if( pidfile != NULL )
521 msg_Dbg( p_libvlc, "removing PID file %s", pidfile );
522 if( unlink( pidfile ) )
523 msg_Warn( p_libvlc, "cannot remove PID file %s: %s",
524 pidfile, vlc_strerror_c(errno) );
529 if (priv->parser != NULL)
530 playlist_preparser_Delete(priv->parser);
532 vlc_DeinitActions( p_libvlc, priv->actions );
534 /* Save the configuration */
535 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
536 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
538 /* Free module bank. It is refcounted, so we call this each time */
539 vlc_LogDeinit (p_libvlc);
540 module_EndBank (true);
541 #if defined(_WIN32) || defined(__OS2__)
547 * Destroy everything.
548 * This function requests the running threads to finish, waits for their
549 * termination, and destroys their structure.
550 * It stops the thread systems: no instance can run after this has run
551 * \param p_libvlc the instance to destroy
553 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
555 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
557 vlc_ExitDestroy( &priv->exit );
559 assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
560 vlc_object_release( p_libvlc );
563 /*****************************************************************************
564 * GetFilenames: parse command line options which are not flags
565 *****************************************************************************
566 * Parse command line for input files as well as their associated options.
567 * An option always follows its associated input and begins with a ":".
568 *****************************************************************************/
569 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
570 const char *const args[] )
574 /* Count the input options */
575 unsigned i_options = 0;
577 while( args[--n][0] == ':' )
582 msg_Warn( p_vlc, "options %s without item", args[n] );
583 return; /* syntax!? */
588 if( strstr( args[n], "://" ) == NULL )
590 mrl = vlc_path2uri( args[n], NULL );
595 intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
596 ( i_options ? &args[n + 1] : NULL ),
597 VLC_INPUT_OPTION_TRUSTED );
603 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
604 * The actual extraction is asynchronous.
606 int libvlc_MetaRequest(libvlc_int_t *libvlc, input_item_t *item,
607 input_item_meta_request_option_t i_options)
609 libvlc_priv_t *priv = libvlc_priv(libvlc);
611 if (unlikely(priv->parser == NULL))
614 playlist_preparser_Push(priv->parser, item, i_options);
619 * Requests retrieving/downloading art for an input item.
620 * The retrieval is performed asynchronously.
622 int libvlc_ArtRequest(libvlc_int_t *libvlc, input_item_t *item,
623 input_item_meta_request_option_t i_options)
625 libvlc_priv_t *priv = libvlc_priv(libvlc);
627 if (unlikely(priv->parser == NULL))
630 playlist_preparser_fetcher_Push(priv->parser, item, i_options);