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 /* Initialize the module bank and load the configuration of the
132 * core module. We need to do this at this stage to be able to display
133 * a short help if required by the user. (short help == core module
137 /* Get command line options that affect module loading. */
138 if( config_LoadCmdLine( p_libvlc
, i_argc
, ppsz_argv
, NULL
) )
140 module_EndBank (false);
144 vlc_LogInit (p_libvlc
);
145 vlc_threads_setup (p_libvlc
);
147 /* Load the builtins and plugins into the module_bank.
148 * We have to do it before config_Load*() because this also gets the
149 * list of configuration options exported by each module and loads their
151 size_t module_count
= module_LoadPlugins (p_libvlc
);
154 * Override default configuration with config file settings
156 if( !var_InheritBool( p_libvlc
, "ignore-config" ) )
158 if( var_InheritBool( p_libvlc
, "reset-config" ) )
159 config_SaveConfigFile( p_libvlc
); /* Save default config */
161 config_LoadConfigFile( p_libvlc
);
165 * Override configuration with command line settings
168 if( config_LoadCmdLine( p_libvlc
, i_argc
, ppsz_argv
, &vlc_optind
) )
170 module_EndBank (true);
171 vlc_LogDeinit (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 module_EndBank (true);
191 if( module_count
<= 1 )
193 msg_Err( p_libvlc
, "No plugins found! Check your VLC installation.");
194 module_EndBank (true);
195 vlc_LogDeinit (p_libvlc
);
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" );
206 module_EndBank (true);
207 vlc_LogDeinit (p_libvlc
);
211 /* lets check if we need to write the pidfile */
212 char *pidfile
= var_InheritString( p_libvlc
, "pidfile" );
213 if( pidfile
!= NULL
)
215 FILE *stream
= vlc_fopen( pidfile
, "w" );
218 fprintf( stream
, "%d", (int)getpid() );
220 msg_Dbg( p_libvlc
, "written PID file %s", pidfile
);
223 msg_Err( p_libvlc
, "cannot write PID file %s: %s",
224 pidfile
, vlc_strerror_c(errno
) );
230 var_Create( p_libvlc
, "pidfile", VLC_VAR_STRING
);
231 var_SetString( p_libvlc
, "pidfile", "" );
235 /* FIXME: could be replaced by using Unix sockets */
238 #define MPRIS_APPEND "/org/mpris/MediaPlayer2/TrackList/Append"
239 #define MPRIS_BUS_NAME "org.mpris.MediaPlayer2.vlc"
240 #define MPRIS_OBJECT_PATH "/org/mpris/MediaPlayer2"
241 #define MPRIS_TRACKLIST_INTERFACE "org.mpris.MediaPlayer2.TrackList"
243 if( var_InheritBool( p_libvlc
, "one-instance" )
244 || ( var_InheritBool( p_libvlc
, "one-instance-when-started-from-file" )
245 && var_InheritBool( p_libvlc
, "started-from-file" ) ) )
247 for( int i
= vlc_optind
; i
< i_argc
; i
++ )
248 if( ppsz_argv
[i
][0] == ':' )
250 msg_Err( p_libvlc
, "item option %s incompatible with single instance",
255 /* Initialise D-Bus interface, check for other instances */
256 dbus_threads_init_default();
259 dbus_error_init( &err
);
261 /* connect to the session bus */
262 DBusConnection
*conn
= dbus_bus_get( DBUS_BUS_SESSION
, &err
);
265 msg_Err( p_libvlc
, "Failed to connect to D-Bus session daemon: %s",
267 dbus_error_free( &err
);
271 /* check if VLC is available on the bus
272 * if not: D-Bus control is not enabled on the other
273 * instance and we can't pass MRLs to it */
274 /* FIXME: This check is totally brain-dead and buggy. */
275 if( !dbus_bus_name_has_owner( conn
, MPRIS_BUS_NAME
, &err
) )
277 dbus_connection_unref( conn
);
278 if( dbus_error_is_set( &err
) )
280 msg_Err( p_libvlc
, "D-Bus error: %s", err
.message
);
283 msg_Dbg( p_libvlc
, "No media player running. Continuing normally." );
284 dbus_error_free( &err
);
288 const dbus_bool_t play
= !var_InheritBool( p_libvlc
, "playlist-enqueue" );
290 msg_Warn( p_libvlc
, "media player running. Exiting...");
291 for( int i
= vlc_optind
; i
< i_argc
; i
++ )
293 DBusMessage
*msg
= dbus_message_new_method_call(
294 MPRIS_BUS_NAME
, MPRIS_OBJECT_PATH
, MPRIS_TRACKLIST_INTERFACE
, "AddTrack" );
295 if( unlikely(msg
== NULL
) )
298 /* We need to resolve relative paths in this instance */
300 if( strstr( ppsz_argv
[i
], "://" ) )
301 mrl
= strdup( ppsz_argv
[i
] );
303 mrl
= vlc_path2uri( ppsz_argv
[i
], NULL
);
306 dbus_message_unref( msg
);
310 const char *after_track
= MPRIS_APPEND
;
313 if( !dbus_message_append_args( msg
, DBUS_TYPE_STRING
, &mrl
,
314 DBUS_TYPE_OBJECT_PATH
, &after_track
,
315 DBUS_TYPE_BOOLEAN
, &play
,
316 DBUS_TYPE_INVALID
) )
318 dbus_message_unref( msg
);
324 msg_Dbg( p_libvlc
, "Adds %s to the running media player", mrl
);
327 /* send message and get a handle for a reply */
328 DBusMessage
*reply
= dbus_connection_send_with_reply_and_block( conn
, msg
, -1,
330 dbus_message_unref( msg
);
333 msg_Err( p_libvlc
, "D-Bus error: %s", err
.message
);
336 dbus_message_unref( reply
);
338 /* we unreference the connection when we've finished with it */
339 dbus_connection_unref( conn
);
343 #undef MPRIS_BUS_NAME
344 #undef MPRIS_OBJECT_PATH
345 #undef MPRIS_TRACKLIST_INTERFACE
349 vlc_CPU_dump( VLC_OBJECT(p_libvlc
) );
351 priv
->b_stats
= var_InheritBool( p_libvlc
, "stats" );
354 * Initialize hotkey handling
356 priv
->actions
= vlc_InitActions( p_libvlc
);
361 priv
->parser
= playlist_preparser_New(VLC_OBJECT(p_libvlc
));
363 /* Create a variable for showing the fullscreen interface */
364 var_Create( p_libvlc
, "intf-toggle-fscontrol", VLC_VAR_BOOL
);
365 var_SetBool( p_libvlc
, "intf-toggle-fscontrol", true );
367 /* Create a variable for the Boss Key */
368 var_Create( p_libvlc
, "intf-boss", VLC_VAR_VOID
);
370 /* Create a variable for showing the main interface */
371 var_Create( p_libvlc
, "intf-show", VLC_VAR_BOOL
);
373 /* Create a variable for showing the right click menu */
374 var_Create( p_libvlc
, "intf-popupmenu", VLC_VAR_BOOL
);
376 /* variables for signalling creation of new files */
377 var_Create( p_libvlc
, "snapshot-file", VLC_VAR_STRING
);
378 var_Create( p_libvlc
, "record-file", VLC_VAR_STRING
);
380 /* some default internal settings */
381 var_Create( p_libvlc
, "window", VLC_VAR_STRING
);
382 /* NOTE: Because the playlist and interfaces start before this function
383 * returns control to the application (DESIGN BUG!), all these variables
384 * must be created (in place of libvlc_new()) and set to VLC defaults
385 * (in place of VLC main()) *here*. */
386 var_Create( p_libvlc
, "user-agent", VLC_VAR_STRING
);
387 var_SetString( p_libvlc
, "user-agent",
388 "VLC media player (LibVLC "VERSION
")" );
389 var_Create( p_libvlc
, "http-user-agent", VLC_VAR_STRING
);
390 var_SetString( p_libvlc
, "http-user-agent",
391 "VLC/"PACKAGE_VERSION
" LibVLC/"PACKAGE_VERSION
);
392 var_Create( p_libvlc
, "app-icon-name", VLC_VAR_STRING
);
393 var_SetString( p_libvlc
, "app-icon-name", PACKAGE_NAME
);
394 var_Create( p_libvlc
, "app-id", VLC_VAR_STRING
);
395 var_SetString( p_libvlc
, "app-id", "org.VideoLAN.VLC" );
396 var_Create( p_libvlc
, "app-version", VLC_VAR_STRING
);
397 var_SetString( p_libvlc
, "app-version", PACKAGE_VERSION
);
399 /* System specific configuration */
400 system_Configure( p_libvlc
, i_argc
- vlc_optind
, ppsz_argv
+ vlc_optind
);
403 /* Initialize VLM if vlm-conf is specified */
404 psz_parser
= var_CreateGetNonEmptyString( p_libvlc
, "vlm-conf" );
407 priv
->p_vlm
= vlm_New( p_libvlc
);
409 msg_Err( p_libvlc
, "VLM initialization failed" );
415 * Load background interfaces
417 psz_modules
= var_CreateGetNonEmptyString( p_libvlc
, "extraintf" );
418 psz_control
= var_CreateGetNonEmptyString( p_libvlc
, "control" );
420 if( psz_modules
&& psz_control
)
423 if( asprintf( &psz_tmp
, "%s:%s", psz_modules
, psz_control
) != -1 )
426 psz_modules
= psz_tmp
;
429 else if( psz_control
)
432 psz_modules
= strdup( psz_control
);
435 psz_parser
= psz_modules
;
436 while ( psz_parser
&& *psz_parser
)
438 char *psz_module
, *psz_temp
;
439 psz_module
= psz_parser
;
440 psz_parser
= strchr( psz_module
, ':' );
446 if( asprintf( &psz_temp
, "%s,none", psz_module
) != -1)
448 libvlc_InternalAddIntf( p_libvlc
, psz_temp
);
456 if( var_InheritBool( p_libvlc
, "syslog" ) )
458 char *logmode
= var_CreateGetNonEmptyString( p_libvlc
, "logmode" );
459 var_SetString( p_libvlc
, "logmode", "syslog" );
460 libvlc_InternalAddIntf( p_libvlc
, "logger,none" );
464 var_SetString( p_libvlc
, "logmode", logmode
);
467 var_Destroy( p_libvlc
, "logmode" );
471 if( var_InheritBool( p_libvlc
, "file-logging" ) )
472 libvlc_InternalAddIntf( p_libvlc
, "logger,none" );
474 if( var_InheritBool( p_libvlc
, "network-synchronisation") )
475 libvlc_InternalAddIntf( p_libvlc
, "netsync,none" );
478 var_Create( p_libvlc
, "drawable-view-top", VLC_VAR_INTEGER
);
479 var_Create( p_libvlc
, "drawable-view-left", VLC_VAR_INTEGER
);
480 var_Create( p_libvlc
, "drawable-view-bottom", VLC_VAR_INTEGER
);
481 var_Create( p_libvlc
, "drawable-view-right", VLC_VAR_INTEGER
);
482 var_Create( p_libvlc
, "drawable-clip-top", VLC_VAR_INTEGER
);
483 var_Create( p_libvlc
, "drawable-clip-left", VLC_VAR_INTEGER
);
484 var_Create( p_libvlc
, "drawable-clip-bottom", VLC_VAR_INTEGER
);
485 var_Create( p_libvlc
, "drawable-clip-right", VLC_VAR_INTEGER
);
486 var_Create( p_libvlc
, "drawable-nsobject", VLC_VAR_ADDRESS
);
488 #if defined (_WIN32) || defined (__OS2__)
489 var_Create( p_libvlc
, "drawable-hwnd", VLC_VAR_INTEGER
);
493 * Get input filenames given as commandline arguments.
494 * We assume that the remaining parameters are filenames
495 * and their input options.
497 GetFilenames( p_libvlc
, i_argc
- vlc_optind
, ppsz_argv
+ vlc_optind
);
500 * Get --open argument
502 psz_val
= var_InheritString( p_libvlc
, "open" );
503 if ( psz_val
!= NULL
)
505 intf_InsertItem( p_libvlc
, psz_val
, 0, NULL
, 0 );
513 * Cleanup a libvlc instance. The instance is not completely deallocated
514 * \param p_libvlc the instance to clean
516 void libvlc_InternalCleanup( libvlc_int_t
*p_libvlc
)
518 libvlc_priv_t
*priv
= libvlc_priv (p_libvlc
);
520 /* Ask the interfaces to stop and destroy them */
521 msg_Dbg( p_libvlc
, "removing all interfaces" );
522 libvlc_Quit( p_libvlc
);
523 intf_DestroyAll( p_libvlc
);
526 /* Destroy VLM if created in libvlc_InternalInit */
529 vlm_Delete( priv
->p_vlm
);
533 #if !defined( _WIN32 ) && !defined( __OS2__ )
534 char *pidfile
= var_InheritString( p_libvlc
, "pidfile" );
535 if( pidfile
!= NULL
)
537 msg_Dbg( p_libvlc
, "removing PID file %s", pidfile
);
538 if( unlink( pidfile
) )
539 msg_Warn( p_libvlc
, "cannot remove PID file %s: %s",
540 pidfile
, vlc_strerror_c(errno
) );
545 if (priv
->parser
!= NULL
)
546 playlist_preparser_Delete(priv
->parser
);
548 vlc_DeinitActions( p_libvlc
, priv
->actions
);
550 /* Save the configuration */
551 if( !var_InheritBool( p_libvlc
, "ignore-config" ) )
552 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc
) );
554 /* Free module bank. It is refcounted, so we call this each time */
555 module_EndBank (true);
556 vlc_LogDeinit (p_libvlc
);
557 #if defined(_WIN32) || defined(__OS2__)
563 * Destroy everything.
564 * This function requests the running threads to finish, waits for their
565 * termination, and destroys their structure.
566 * It stops the thread systems: no instance can run after this has run
567 * \param p_libvlc the instance to destroy
569 void libvlc_InternalDestroy( libvlc_int_t
*p_libvlc
)
571 libvlc_priv_t
*priv
= libvlc_priv( p_libvlc
);
573 vlc_ExitDestroy( &priv
->exit
);
575 assert( atomic_load(&(vlc_internals(p_libvlc
)->refs
)) == 1 );
576 vlc_object_release( p_libvlc
);
579 /*****************************************************************************
580 * GetFilenames: parse command line options which are not flags
581 *****************************************************************************
582 * Parse command line for input files as well as their associated options.
583 * An option always follows its associated input and begins with a ":".
584 *****************************************************************************/
585 static void GetFilenames( libvlc_int_t
*p_vlc
, unsigned n
,
586 const char *const args
[] )
590 /* Count the input options */
591 unsigned i_options
= 0;
593 while( args
[--n
][0] == ':' )
598 msg_Warn( p_vlc
, "options %s without item", args
[n
] );
599 return; /* syntax!? */
604 if( strstr( args
[n
], "://" ) == NULL
)
606 mrl
= vlc_path2uri( args
[n
], NULL
);
611 intf_InsertItem( p_vlc
, (mrl
!= NULL
) ? mrl
: args
[n
], i_options
,
612 ( i_options
? &args
[n
+ 1] : NULL
),
613 VLC_INPUT_OPTION_TRUSTED
);
619 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
620 * The actual extraction is asynchronous.
622 int libvlc_MetaRequest(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_Push(priv
->parser
, item
, i_options
);
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
);