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
)( NULL
, sizeof (*priv
), "libvlc" );
99 if( p_libvlc
== NULL
)
102 priv
= libvlc_priv (p_libvlc
);
103 priv
->playlist
= 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
;
127 int i_ret
= VLC_EGENERIC
;
129 /* System specific initialization code */
132 vlc_LogPreinit(p_libvlc
);
134 /* Initialize the module bank and load the configuration of the
135 * core module. We need to do this at this stage to be able to display
136 * a short help if required by the user. (short help == core module
140 /* Get command line options that affect module loading. */
141 if( config_LoadCmdLine( p_libvlc
, i_argc
, ppsz_argv
, NULL
) )
143 module_EndBank (false);
147 vlc_threads_setup (p_libvlc
);
149 /* Load the builtins and plugins into the module_bank.
150 * We have to do it before config_Load*() because this also gets the
151 * list of configuration options exported by each module and loads their
153 size_t module_count
= module_LoadPlugins (p_libvlc
);
156 * Override default configuration with config file settings
158 if( !var_InheritBool( p_libvlc
, "ignore-config" ) )
160 if( var_InheritBool( p_libvlc
, "reset-config" ) )
161 config_SaveConfigFile( p_libvlc
); /* Save default config */
163 config_LoadConfigFile( p_libvlc
);
167 * Override configuration with command line settings
170 if( config_LoadCmdLine( p_libvlc
, i_argc
, ppsz_argv
, &vlc_optind
) )
173 vlc_LogInit(p_libvlc
);
176 * Support for gettext
178 #if defined( ENABLE_NLS ) \
179 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
180 vlc_bindtextdomain (PACKAGE_NAME
);
182 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
183 msg_Dbg( p_libvlc
, "translation test: code is \"%s\"", _("C") );
185 if (config_PrintHelp (VLC_OBJECT(p_libvlc
)))
187 libvlc_InternalCleanup (p_libvlc
);
191 if( module_count
<= 1 )
193 msg_Err( p_libvlc
, "No plugins found! Check your VLC installation.");
199 /* Check for daemon mode */
200 if( var_InheritBool( p_libvlc
, "daemon" ) )
202 if( daemon( 1, 0) != 0 )
204 msg_Err( p_libvlc
, "Unable to fork vlc to daemon mode" );
208 /* lets check if we need to write the pidfile */
209 char *pidfile
= var_InheritString( p_libvlc
, "pidfile" );
210 if( pidfile
!= NULL
)
212 FILE *stream
= vlc_fopen( pidfile
, "w" );
215 fprintf( stream
, "%d", (int)getpid() );
217 msg_Dbg( p_libvlc
, "written PID file %s", pidfile
);
220 msg_Err( p_libvlc
, "cannot write PID file %s: %s",
221 pidfile
, vlc_strerror_c(errno
) );
227 var_Create( p_libvlc
, "pidfile", VLC_VAR_STRING
);
228 var_SetString( p_libvlc
, "pidfile", "" );
234 if( libvlc_InternalDialogInit( p_libvlc
) != VLC_SUCCESS
)
236 if( libvlc_InternalKeystoreInit( p_libvlc
) != VLC_SUCCESS
)
237 msg_Warn( p_libvlc
, "memory keystore init failed" );
239 /* FIXME: could be replaced by using Unix sockets */
242 #define MPRIS_APPEND "/org/mpris/MediaPlayer2/TrackList/Append"
243 #define MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
244 #define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
245 #define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList"
247 if( var_InheritBool( p_libvlc
, "one-instance" )
248 || ( var_InheritBool( p_libvlc
, "one-instance-when-started-from-file" )
249 && var_InheritBool( p_libvlc
, "started-from-file" ) ) )
251 for( int i
= vlc_optind
; i
< i_argc
; i
++ )
252 if( ppsz_argv
[i
][0] == ':' )
254 msg_Err( p_libvlc
, "item option %s incompatible with single instance",
259 /* Initialise D-Bus interface, check for other instances */
260 dbus_threads_init_default();
263 dbus_error_init( &err
);
265 /* connect to the session bus */
266 DBusConnection
*conn
= dbus_bus_get( DBUS_BUS_SESSION
, &err
);
269 msg_Err( p_libvlc
, "Failed to connect to D-Bus session daemon: %s",
271 dbus_error_free( &err
);
275 /* check if VLC is available on the bus
276 * if not: D-Bus control is not enabled on the other
277 * instance and we can't pass MRLs to it */
278 /* FIXME: This check is totally brain-dead and buggy. */
279 if( !dbus_bus_name_has_owner( conn
, MPRIS_BUS_NAME
, &err
) )
281 dbus_connection_unref( conn
);
282 if( dbus_error_is_set( &err
) )
284 msg_Err( p_libvlc
, "D-Bus error: %s", err
.message
);
287 msg_Dbg( p_libvlc
, "No media player running. Continuing normally." );
288 dbus_error_free( &err
);
292 const dbus_bool_t play
= !var_InheritBool( p_libvlc
, "playlist-enqueue" );
294 msg_Warn( p_libvlc
, "media player running. Exiting...");
295 for( int i
= vlc_optind
; i
< i_argc
; i
++ )
297 DBusMessage
*msg
= dbus_message_new_method_call(
298 MPRIS_BUS_NAME
, MPRIS_OBJECT_PATH
, MPRIS_TRACKLIST_INTERFACE
, "AddTrack" );
299 if( unlikely(msg
== NULL
) )
302 /* We need to resolve relative paths in this instance */
304 if( strstr( ppsz_argv
[i
], "://" ) )
305 mrl
= strdup( ppsz_argv
[i
] );
307 mrl
= vlc_path2uri( ppsz_argv
[i
], NULL
);
310 dbus_message_unref( msg
);
314 const char *after_track
= MPRIS_APPEND
;
317 if( !dbus_message_append_args( msg
, DBUS_TYPE_STRING
, &mrl
,
318 DBUS_TYPE_OBJECT_PATH
, &after_track
,
319 DBUS_TYPE_BOOLEAN
, &play
,
320 DBUS_TYPE_INVALID
) )
322 dbus_message_unref( msg
);
328 msg_Dbg( p_libvlc
, "Adds %s to the running media player", mrl
);
331 /* send message and get a handle for a reply */
332 DBusMessage
*reply
= dbus_connection_send_with_reply_and_block( conn
, msg
, -1,
334 dbus_message_unref( msg
);
337 msg_Err( p_libvlc
, "D-Bus error: %s", err
.message
);
340 dbus_message_unref( reply
);
342 /* we unreference the connection when we've finished with it */
343 dbus_connection_unref( conn
);
347 #undef MPRIS_BUS_NAME
348 #undef MPRIS_OBJECT_PATH
349 #undef MPRIS_TRACKLIST_INTERFACE
353 vlc_CPU_dump( VLC_OBJECT(p_libvlc
) );
355 priv
->b_stats
= var_InheritBool( p_libvlc
, "stats" );
358 * Initialize hotkey handling
360 priv
->actions
= vlc_InitActions( p_libvlc
);
367 priv
->parser
= playlist_preparser_New(VLC_OBJECT(p_libvlc
));
371 /* Create a variable for showing the fullscreen interface */
372 var_Create( p_libvlc
, "intf-toggle-fscontrol", VLC_VAR_BOOL
);
373 var_SetBool( p_libvlc
, "intf-toggle-fscontrol", true );
375 /* Create a variable for the Boss Key */
376 var_Create( p_libvlc
, "intf-boss", VLC_VAR_VOID
);
378 /* Create a variable for showing the main interface */
379 var_Create( p_libvlc
, "intf-show", VLC_VAR_BOOL
);
381 /* Create a variable for showing the right click menu */
382 var_Create( p_libvlc
, "intf-popupmenu", VLC_VAR_BOOL
);
384 /* variables for signalling creation of new files */
385 var_Create( p_libvlc
, "snapshot-file", VLC_VAR_STRING
);
386 var_Create( p_libvlc
, "record-file", VLC_VAR_STRING
);
388 /* some default internal settings */
389 var_Create( p_libvlc
, "window", VLC_VAR_STRING
);
390 /* NOTE: Because the playlist and interfaces start before this function
391 * returns control to the application (DESIGN BUG!), all these variables
392 * must be created (in place of libvlc_new()) and set to VLC defaults
393 * (in place of VLC main()) *here*. */
394 var_Create( p_libvlc
, "user-agent", VLC_VAR_STRING
);
395 var_SetString( p_libvlc
, "user-agent",
396 "VLC media player (LibVLC "VERSION
")" );
397 var_Create( p_libvlc
, "http-user-agent", VLC_VAR_STRING
);
398 var_SetString( p_libvlc
, "http-user-agent",
399 "VLC/"PACKAGE_VERSION
" LibVLC/"PACKAGE_VERSION
);
400 var_Create( p_libvlc
, "app-icon-name", VLC_VAR_STRING
);
401 var_SetString( p_libvlc
, "app-icon-name", PACKAGE_NAME
);
402 var_Create( p_libvlc
, "app-id", VLC_VAR_STRING
);
403 var_SetString( p_libvlc
, "app-id", "org.VideoLAN.VLC" );
404 var_Create( p_libvlc
, "app-version", VLC_VAR_STRING
);
405 var_SetString( p_libvlc
, "app-version", PACKAGE_VERSION
);
407 /* System specific configuration */
408 system_Configure( p_libvlc
, i_argc
- vlc_optind
, ppsz_argv
+ vlc_optind
);
411 /* Initialize VLM if vlm-conf is specified */
412 psz_parser
= var_CreateGetNonEmptyString( p_libvlc
, "vlm-conf" );
415 priv
->p_vlm
= vlm_New( p_libvlc
);
417 msg_Err( p_libvlc
, "VLM initialization failed" );
423 * Load background interfaces
425 psz_modules
= var_CreateGetNonEmptyString( p_libvlc
, "extraintf" );
426 psz_control
= var_CreateGetNonEmptyString( p_libvlc
, "control" );
428 if( psz_modules
&& psz_control
)
431 if( asprintf( &psz_tmp
, "%s:%s", psz_modules
, psz_control
) != -1 )
434 psz_modules
= psz_tmp
;
437 else if( psz_control
)
440 psz_modules
= strdup( psz_control
);
443 psz_parser
= psz_modules
;
444 while ( psz_parser
&& *psz_parser
)
446 char *psz_module
, *psz_temp
;
447 psz_module
= psz_parser
;
448 psz_parser
= strchr( psz_module
, ':' );
454 if( asprintf( &psz_temp
, "%s,none", psz_module
) != -1)
456 libvlc_InternalAddIntf( p_libvlc
, psz_temp
);
463 if( var_InheritBool( p_libvlc
, "network-synchronisation") )
464 libvlc_InternalAddIntf( p_libvlc
, "netsync,none" );
467 var_Create( p_libvlc
, "drawable-view-top", VLC_VAR_INTEGER
);
468 var_Create( p_libvlc
, "drawable-view-left", VLC_VAR_INTEGER
);
469 var_Create( p_libvlc
, "drawable-view-bottom", VLC_VAR_INTEGER
);
470 var_Create( p_libvlc
, "drawable-view-right", VLC_VAR_INTEGER
);
471 var_Create( p_libvlc
, "drawable-clip-top", VLC_VAR_INTEGER
);
472 var_Create( p_libvlc
, "drawable-clip-left", VLC_VAR_INTEGER
);
473 var_Create( p_libvlc
, "drawable-clip-bottom", VLC_VAR_INTEGER
);
474 var_Create( p_libvlc
, "drawable-clip-right", VLC_VAR_INTEGER
);
475 var_Create( p_libvlc
, "drawable-nsobject", VLC_VAR_ADDRESS
);
479 * Get input filenames given as commandline arguments.
480 * We assume that the remaining parameters are filenames
481 * and their input options.
483 GetFilenames( p_libvlc
, i_argc
- vlc_optind
, ppsz_argv
+ vlc_optind
);
486 * Get --open argument
488 psz_val
= var_InheritString( p_libvlc
, "open" );
489 if ( psz_val
!= NULL
)
491 intf_InsertItem( p_libvlc
, psz_val
, 0, NULL
, 0 );
498 libvlc_InternalCleanup( p_libvlc
);
503 * Cleanup a libvlc instance. The instance is not completely deallocated
504 * \param p_libvlc the instance to clean
506 void libvlc_InternalCleanup( libvlc_int_t
*p_libvlc
)
508 libvlc_priv_t
*priv
= libvlc_priv (p_libvlc
);
510 /* Ask the interfaces to stop and destroy them */
511 msg_Dbg( p_libvlc
, "removing all interfaces" );
512 intf_DestroyAll( p_libvlc
);
514 libvlc_InternalDialogClean( p_libvlc
);
515 libvlc_InternalKeystoreClean( p_libvlc
);
518 /* Destroy VLM if created in libvlc_InternalInit */
521 vlm_Delete( priv
->p_vlm
);
525 #if !defined( _WIN32 ) && !defined( __OS2__ )
526 char *pidfile
= var_InheritString( p_libvlc
, "pidfile" );
527 if( pidfile
!= NULL
)
529 msg_Dbg( p_libvlc
, "removing PID file %s", pidfile
);
530 if( unlink( pidfile
) )
531 msg_Warn( p_libvlc
, "cannot remove PID file %s: %s",
532 pidfile
, vlc_strerror_c(errno
) );
537 if (priv
->parser
!= NULL
)
538 playlist_preparser_Delete(priv
->parser
);
540 vlc_DeinitActions( p_libvlc
, priv
->actions
);
542 /* Save the configuration */
543 if( !var_InheritBool( p_libvlc
, "ignore-config" ) )
544 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc
) );
546 /* Free module bank. It is refcounted, so we call this each time */
547 vlc_LogDeinit (p_libvlc
);
548 module_EndBank (true);
549 #if defined(_WIN32) || defined(__OS2__)
555 * Destroy everything.
556 * This function requests the running threads to finish, waits for their
557 * termination, and destroys their structure.
558 * It stops the thread systems: no instance can run after this has run
559 * \param p_libvlc the instance to destroy
561 void libvlc_InternalDestroy( libvlc_int_t
*p_libvlc
)
563 libvlc_priv_t
*priv
= libvlc_priv( p_libvlc
);
565 vlc_ExitDestroy( &priv
->exit
);
567 assert( atomic_load(&(vlc_internals(p_libvlc
)->refs
)) == 1 );
568 vlc_object_release( p_libvlc
);
571 /*****************************************************************************
572 * GetFilenames: parse command line options which are not flags
573 *****************************************************************************
574 * Parse command line for input files as well as their associated options.
575 * An option always follows its associated input and begins with a ":".
576 *****************************************************************************/
577 static void GetFilenames( libvlc_int_t
*p_vlc
, unsigned n
,
578 const char *const args
[] )
582 /* Count the input options */
583 unsigned i_options
= 0;
585 while( args
[--n
][0] == ':' )
590 msg_Warn( p_vlc
, "options %s without item", args
[n
] );
591 return; /* syntax!? */
596 if( strstr( args
[n
], "://" ) == NULL
)
598 mrl
= vlc_path2uri( args
[n
], NULL
);
603 intf_InsertItem( p_vlc
, (mrl
!= NULL
) ? mrl
: args
[n
], i_options
,
604 ( i_options
? &args
[n
+ 1] : NULL
),
605 VLC_INPUT_OPTION_TRUSTED
);
611 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
612 * The actual extraction is asynchronous. It can be cancelled with
613 * libvlc_MetadataCancel()
615 int libvlc_MetadataRequest(libvlc_int_t
*libvlc
, input_item_t
*item
,
616 input_item_meta_request_option_t i_options
,
617 int timeout
, void *id
)
619 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
621 if (unlikely(priv
->parser
== NULL
))
624 vlc_mutex_lock( &item
->lock
);
625 if( item
->i_preparse_depth
== 0 )
626 item
->i_preparse_depth
= 1;
627 if( i_options
& META_REQUEST_OPTION_DO_INTERACT
)
628 item
->b_preparse_interact
= true;
629 vlc_mutex_unlock( &item
->lock
);
630 playlist_preparser_Push( priv
->parser
, item
, i_options
, timeout
, id
);
635 * Requests retrieving/downloading art for an input item.
636 * The retrieval is performed asynchronously.
638 int libvlc_ArtRequest(libvlc_int_t
*libvlc
, input_item_t
*item
,
639 input_item_meta_request_option_t i_options
)
641 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
643 if (unlikely(priv
->parser
== NULL
))
646 playlist_preparser_fetcher_Push(priv
->parser
, item
, i_options
);
651 * Cancels extraction of the meta data for an input item.
653 * This does nothing if the input item is already processed or if it was not
654 * added with libvlc_MetadataRequest()
656 void libvlc_MetadataCancel(libvlc_int_t
*libvlc
, void *id
)
658 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
660 if (unlikely(priv
->parser
== NULL
))
663 playlist_preparser_Cancel(priv
->parser
, id
);