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>
64 #include <vlc_dialog.h>
65 #include <vlc_keystore.h>
69 #include <vlc_modules.h>
72 #include "playlist/playlist_internal.h"
73 #include "misc/variables.h"
78 # include <libkern/OSAtomic.h>
83 /*****************************************************************************
85 *****************************************************************************/
86 static void GetFilenames ( libvlc_int_t
*, unsigned, const char *const [] );
89 * Allocate a blank libvlc instance, also setting the exit handler.
90 * Vlc's threading system must have been initialized first
92 libvlc_int_t
* libvlc_InternalCreate( void )
94 libvlc_int_t
*p_libvlc
;
97 /* Allocate a libvlc instance object */
98 p_libvlc
= vlc_custom_create( (vlc_object_t
*)NULL
, sizeof (*priv
),
100 if( p_libvlc
== NULL
)
103 priv
= libvlc_priv (p_libvlc
);
104 priv
->playlist
= NULL
;
107 vlc_ExitInit( &priv
->exit
);
113 * Initialize a libvlc instance
114 * This function initializes a previously allocated libvlc instance:
116 * - gettext initialization
117 * - message queue, module bank and playlist initialization
118 * - configuration and commandline parsing
120 int libvlc_InternalInit( libvlc_int_t
*p_libvlc
, int i_argc
,
121 const char *ppsz_argv
[] )
123 libvlc_priv_t
*priv
= libvlc_priv (p_libvlc
);
124 char * psz_modules
= NULL
;
125 char * psz_parser
= NULL
;
126 char * psz_control
= NULL
;
128 int i_ret
= VLC_EGENERIC
;
130 /* System specific initialization code */
133 vlc_LogPreinit(p_libvlc
);
135 /* Initialize the module bank and load the configuration of the
136 * core module. We need to do this at this stage to be able to display
137 * a short help if required by the user. (short help == core module
141 /* Get command line options that affect module loading. */
142 if( config_LoadCmdLine( p_libvlc
, i_argc
, ppsz_argv
, NULL
) )
144 module_EndBank (false);
148 vlc_threads_setup (p_libvlc
);
150 /* Load the builtins and plugins into the module_bank.
151 * We have to do it before config_Load*() because this also gets the
152 * list of configuration options exported by each module and loads their
154 size_t module_count
= module_LoadPlugins (p_libvlc
);
157 * Override default configuration with config file settings
159 if( !var_InheritBool( p_libvlc
, "ignore-config" ) )
161 if( var_InheritBool( p_libvlc
, "reset-config" ) )
162 config_SaveConfigFile( p_libvlc
); /* Save default config */
164 config_LoadConfigFile( p_libvlc
);
168 * Override configuration with command line settings
171 if( config_LoadCmdLine( p_libvlc
, i_argc
, ppsz_argv
, &vlc_optind
) )
174 vlc_LogInit(p_libvlc
);
177 * Support for gettext
179 #if defined( ENABLE_NLS ) \
180 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
181 vlc_bindtextdomain (PACKAGE_NAME
);
183 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
184 msg_Dbg( p_libvlc
, "translation test: code is \"%s\"", _("C") );
186 if (config_PrintHelp (VLC_OBJECT(p_libvlc
)))
188 libvlc_InternalCleanup (p_libvlc
);
192 if( module_count
<= 1 )
194 msg_Err( p_libvlc
, "No plugins found! Check your VLC installation.");
200 /* Check for daemon mode */
201 if( var_InheritBool( p_libvlc
, "daemon" ) )
203 if( daemon( 1, 0) != 0 )
205 msg_Err( p_libvlc
, "Unable to fork vlc to daemon mode" );
209 /* lets check if we need to write the pidfile */
210 char *pidfile
= var_InheritString( p_libvlc
, "pidfile" );
211 if( pidfile
!= NULL
)
213 FILE *stream
= vlc_fopen( pidfile
, "w" );
216 fprintf( stream
, "%d", (int)getpid() );
218 msg_Dbg( p_libvlc
, "written PID file %s", pidfile
);
221 msg_Err( p_libvlc
, "cannot write PID file %s: %s",
222 pidfile
, vlc_strerror_c(errno
) );
228 var_Create( p_libvlc
, "pidfile", VLC_VAR_STRING
);
229 var_SetString( p_libvlc
, "pidfile", "" );
235 if( libvlc_InternalDialogInit( p_libvlc
) != VLC_SUCCESS
)
237 if( libvlc_InternalKeystoreInit( p_libvlc
) != VLC_SUCCESS
)
238 msg_Warn( p_libvlc
, "memory keystore init failed" );
240 /* FIXME: could be replaced by using Unix sockets */
243 #define MPRIS_APPEND "/org/mpris/MediaPlayer2/TrackList/Append"
244 #define MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
245 #define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
246 #define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList"
248 if( var_InheritBool( p_libvlc
, "one-instance" )
249 || ( var_InheritBool( p_libvlc
, "one-instance-when-started-from-file" )
250 && var_InheritBool( p_libvlc
, "started-from-file" ) ) )
252 for( int i
= vlc_optind
; i
< i_argc
; i
++ )
253 if( ppsz_argv
[i
][0] == ':' )
255 msg_Err( p_libvlc
, "item option %s incompatible with single instance",
260 /* Initialise D-Bus interface, check for other instances */
261 dbus_threads_init_default();
264 dbus_error_init( &err
);
266 /* connect to the session bus */
267 DBusConnection
*conn
= dbus_bus_get( DBUS_BUS_SESSION
, &err
);
270 msg_Err( p_libvlc
, "Failed to connect to D-Bus session daemon: %s",
272 dbus_error_free( &err
);
276 /* check if VLC is available on the bus
277 * if not: D-Bus control is not enabled on the other
278 * instance and we can't pass MRLs to it */
279 /* FIXME: This check is totally brain-dead and buggy. */
280 if( !dbus_bus_name_has_owner( conn
, MPRIS_BUS_NAME
, &err
) )
282 dbus_connection_unref( conn
);
283 if( dbus_error_is_set( &err
) )
285 msg_Err( p_libvlc
, "D-Bus error: %s", err
.message
);
288 msg_Dbg( p_libvlc
, "No media player running. Continuing normally." );
289 dbus_error_free( &err
);
293 const dbus_bool_t play
= !var_InheritBool( p_libvlc
, "playlist-enqueue" );
295 msg_Warn( p_libvlc
, "media player running. Exiting...");
296 for( int i
= vlc_optind
; i
< i_argc
; i
++ )
298 DBusMessage
*msg
= dbus_message_new_method_call(
299 MPRIS_BUS_NAME
, MPRIS_OBJECT_PATH
, MPRIS_TRACKLIST_INTERFACE
, "AddTrack" );
300 if( unlikely(msg
== NULL
) )
303 /* We need to resolve relative paths in this instance */
305 if( strstr( ppsz_argv
[i
], "://" ) )
306 mrl
= strdup( ppsz_argv
[i
] );
308 mrl
= vlc_path2uri( ppsz_argv
[i
], NULL
);
311 dbus_message_unref( msg
);
315 const char *after_track
= MPRIS_APPEND
;
318 if( !dbus_message_append_args( msg
, DBUS_TYPE_STRING
, &mrl
,
319 DBUS_TYPE_OBJECT_PATH
, &after_track
,
320 DBUS_TYPE_BOOLEAN
, &play
,
321 DBUS_TYPE_INVALID
) )
323 dbus_message_unref( msg
);
329 msg_Dbg( p_libvlc
, "Adds %s to the running media player", mrl
);
332 /* send message and get a handle for a reply */
333 DBusMessage
*reply
= dbus_connection_send_with_reply_and_block( conn
, msg
, -1,
335 dbus_message_unref( msg
);
338 msg_Err( p_libvlc
, "D-Bus error: %s", err
.message
);
341 dbus_message_unref( reply
);
343 /* we unreference the connection when we've finished with it */
344 dbus_connection_unref( conn
);
348 #undef MPRIS_BUS_NAME
349 #undef MPRIS_OBJECT_PATH
350 #undef MPRIS_TRACKLIST_INTERFACE
354 vlc_CPU_dump( VLC_OBJECT(p_libvlc
) );
356 priv
->b_stats
= var_InheritBool( p_libvlc
, "stats" );
359 * Initialize hotkey handling
361 priv
->actions
= vlc_InitActions( p_libvlc
);
368 priv
->parser
= playlist_preparser_New(VLC_OBJECT(p_libvlc
));
372 /* Create a variable for showing the fullscreen interface */
373 var_Create( p_libvlc
, "intf-toggle-fscontrol", VLC_VAR_BOOL
);
374 var_SetBool( p_libvlc
, "intf-toggle-fscontrol", true );
376 /* Create a variable for the Boss Key */
377 var_Create( p_libvlc
, "intf-boss", VLC_VAR_VOID
);
379 /* Create a variable for showing the main interface */
380 var_Create( p_libvlc
, "intf-show", VLC_VAR_BOOL
);
382 /* Create a variable for showing the right click menu */
383 var_Create( p_libvlc
, "intf-popupmenu", VLC_VAR_BOOL
);
385 /* variables for signalling creation of new files */
386 var_Create( p_libvlc
, "snapshot-file", VLC_VAR_STRING
);
387 var_Create( p_libvlc
, "record-file", VLC_VAR_STRING
);
389 /* some default internal settings */
390 var_Create( p_libvlc
, "window", VLC_VAR_STRING
);
391 /* NOTE: Because the playlist and interfaces start before this function
392 * returns control to the application (DESIGN BUG!), all these variables
393 * must be created (in place of libvlc_new()) and set to VLC defaults
394 * (in place of VLC main()) *here*. */
395 var_Create( p_libvlc
, "user-agent", VLC_VAR_STRING
);
396 var_SetString( p_libvlc
, "user-agent",
397 "VLC media player (LibVLC "VERSION
")" );
398 var_Create( p_libvlc
, "http-user-agent", VLC_VAR_STRING
);
399 var_SetString( p_libvlc
, "http-user-agent",
400 "VLC/"PACKAGE_VERSION
" LibVLC/"PACKAGE_VERSION
);
401 var_Create( p_libvlc
, "app-icon-name", VLC_VAR_STRING
);
402 var_SetString( p_libvlc
, "app-icon-name", PACKAGE_NAME
);
403 var_Create( p_libvlc
, "app-id", VLC_VAR_STRING
);
404 var_SetString( p_libvlc
, "app-id", "org.VideoLAN.VLC" );
405 var_Create( p_libvlc
, "app-version", VLC_VAR_STRING
);
406 var_SetString( p_libvlc
, "app-version", PACKAGE_VERSION
);
408 /* System specific configuration */
409 system_Configure( p_libvlc
, i_argc
- vlc_optind
, ppsz_argv
+ vlc_optind
);
412 /* Initialize VLM if vlm-conf is specified */
413 psz_parser
= var_CreateGetNonEmptyString( p_libvlc
, "vlm-conf" );
416 priv
->p_vlm
= vlm_New( p_libvlc
);
418 msg_Err( p_libvlc
, "VLM initialization failed" );
424 * Load background interfaces
426 psz_modules
= var_CreateGetNonEmptyString( p_libvlc
, "extraintf" );
427 psz_control
= var_CreateGetNonEmptyString( p_libvlc
, "control" );
429 if( psz_modules
&& psz_control
)
432 if( asprintf( &psz_tmp
, "%s:%s", psz_modules
, psz_control
) != -1 )
435 psz_modules
= psz_tmp
;
438 else if( psz_control
)
441 psz_modules
= strdup( psz_control
);
444 psz_parser
= psz_modules
;
445 while ( psz_parser
&& *psz_parser
)
447 char *psz_module
, *psz_temp
;
448 psz_module
= psz_parser
;
449 psz_parser
= strchr( psz_module
, ':' );
455 if( asprintf( &psz_temp
, "%s,none", psz_module
) != -1)
457 libvlc_InternalAddIntf( p_libvlc
, psz_temp
);
464 if( var_InheritBool( p_libvlc
, "network-synchronisation") )
465 libvlc_InternalAddIntf( p_libvlc
, "netsync,none" );
468 var_Create( p_libvlc
, "drawable-view-top", VLC_VAR_INTEGER
);
469 var_Create( p_libvlc
, "drawable-view-left", VLC_VAR_INTEGER
);
470 var_Create( p_libvlc
, "drawable-view-bottom", VLC_VAR_INTEGER
);
471 var_Create( p_libvlc
, "drawable-view-right", VLC_VAR_INTEGER
);
472 var_Create( p_libvlc
, "drawable-clip-top", VLC_VAR_INTEGER
);
473 var_Create( p_libvlc
, "drawable-clip-left", VLC_VAR_INTEGER
);
474 var_Create( p_libvlc
, "drawable-clip-bottom", VLC_VAR_INTEGER
);
475 var_Create( p_libvlc
, "drawable-clip-right", VLC_VAR_INTEGER
);
476 var_Create( p_libvlc
, "drawable-nsobject", VLC_VAR_ADDRESS
);
480 * Get input filenames given as commandline arguments.
481 * We assume that the remaining parameters are filenames
482 * and their input options.
484 GetFilenames( p_libvlc
, i_argc
- vlc_optind
, ppsz_argv
+ vlc_optind
);
487 * Get --open argument
489 psz_val
= var_InheritString( p_libvlc
, "open" );
490 if ( psz_val
!= NULL
)
492 intf_InsertItem( p_libvlc
, psz_val
, 0, NULL
, 0 );
499 libvlc_InternalCleanup( p_libvlc
);
504 * Cleanup a libvlc instance. The instance is not completely deallocated
505 * \param p_libvlc the instance to clean
507 void libvlc_InternalCleanup( libvlc_int_t
*p_libvlc
)
509 libvlc_priv_t
*priv
= libvlc_priv (p_libvlc
);
511 /* Ask the interfaces to stop and destroy them */
512 msg_Dbg( p_libvlc
, "removing all interfaces" );
513 intf_DestroyAll( p_libvlc
);
515 libvlc_InternalDialogClean( p_libvlc
);
516 libvlc_InternalKeystoreClean( p_libvlc
);
519 /* Destroy VLM if created in libvlc_InternalInit */
522 vlm_Delete( priv
->p_vlm
);
526 #if !defined( _WIN32 ) && !defined( __OS2__ )
527 char *pidfile
= var_InheritString( p_libvlc
, "pidfile" );
528 if( pidfile
!= NULL
)
530 msg_Dbg( p_libvlc
, "removing PID file %s", pidfile
);
531 if( unlink( pidfile
) )
532 msg_Warn( p_libvlc
, "cannot remove PID file %s: %s",
533 pidfile
, vlc_strerror_c(errno
) );
538 if (priv
->parser
!= NULL
)
539 playlist_preparser_Delete(priv
->parser
);
541 vlc_DeinitActions( p_libvlc
, priv
->actions
);
543 /* Save the configuration */
544 if( !var_InheritBool( p_libvlc
, "ignore-config" ) )
545 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc
) );
547 /* Free module bank. It is refcounted, so we call this each time */
548 vlc_LogDeinit (p_libvlc
);
549 module_EndBank (true);
550 #if defined(_WIN32) || defined(__OS2__)
556 * Destroy everything.
557 * This function requests the running threads to finish, waits for their
558 * termination, and destroys their structure.
559 * It stops the thread systems: no instance can run after this has run
560 * \param p_libvlc the instance to destroy
562 void libvlc_InternalDestroy( libvlc_int_t
*p_libvlc
)
564 libvlc_priv_t
*priv
= libvlc_priv( p_libvlc
);
566 vlc_ExitDestroy( &priv
->exit
);
568 assert( atomic_load(&(vlc_internals(p_libvlc
)->refs
)) == 1 );
569 vlc_object_release( p_libvlc
);
572 /*****************************************************************************
573 * GetFilenames: parse command line options which are not flags
574 *****************************************************************************
575 * Parse command line for input files as well as their associated options.
576 * An option always follows its associated input and begins with a ":".
577 *****************************************************************************/
578 static void GetFilenames( libvlc_int_t
*p_vlc
, unsigned n
,
579 const char *const args
[] )
583 /* Count the input options */
584 unsigned i_options
= 0;
586 while( args
[--n
][0] == ':' )
591 msg_Warn( p_vlc
, "options %s without item", args
[n
] );
592 return; /* syntax!? */
597 if( strstr( args
[n
], "://" ) == NULL
)
599 mrl
= vlc_path2uri( args
[n
], NULL
);
604 intf_InsertItem( p_vlc
, (mrl
!= NULL
) ? mrl
: args
[n
], i_options
,
605 ( i_options
? &args
[n
+ 1] : NULL
),
606 VLC_INPUT_OPTION_TRUSTED
);
612 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
613 * The actual extraction is asynchronous. It can be cancelled with
614 * libvlc_MetadataCancel()
616 int libvlc_MetadataRequest(libvlc_int_t
*libvlc
, input_item_t
*item
,
617 input_item_meta_request_option_t i_options
,
618 int timeout
, void *id
)
620 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
622 if (unlikely(priv
->parser
== NULL
))
625 vlc_mutex_lock( &item
->lock
);
626 if( item
->i_preparse_depth
== 0 )
627 item
->i_preparse_depth
= 1;
628 if( i_options
& META_REQUEST_OPTION_DO_INTERACT
)
629 item
->b_preparse_interact
= true;
630 vlc_mutex_unlock( &item
->lock
);
631 playlist_preparser_Push( priv
->parser
, item
, i_options
, timeout
, id
);
636 * Requests retrieving/downloading art for an input item.
637 * The retrieval is performed asynchronously.
639 int libvlc_ArtRequest(libvlc_int_t
*libvlc
, input_item_t
*item
,
640 input_item_meta_request_option_t i_options
)
642 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
644 if (unlikely(priv
->parser
== NULL
))
647 playlist_preparser_fetcher_Push(priv
->parser
, item
, i_options
);
652 * Cancels extraction of the meta data for an input item.
654 * This does nothing if the input item is already processed or if it was not
655 * added with libvlc_MetadataRequest()
657 void libvlc_MetadataCancel(libvlc_int_t
*libvlc
, void *id
)
659 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
661 if (unlikely(priv
->parser
== NULL
))
664 playlist_preparser_Cancel(priv
->parser
, id
);