twolame: use RECONF
[vlc.git] / src / libvlc.c
blob34824da89be079c84378aeb5ae619af510aff593
1 /*****************************************************************************
2 * libvlc.c: libvlc instances creation and deletion, interfaces handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 VLC authors and VideoLAN
5 * $Id$
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 *****************************************************************************/
28 /** \file
29 * This file contains functions to create and destroy libvlc instances
32 /*****************************************************************************
33 * Preamble
34 *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 # include "config.h"
37 #endif
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() */
48 #include <string.h>
49 #include <stdlib.h> /* free() */
50 #include <errno.h>
52 #include "config/vlc_getopt.h"
54 #ifdef HAVE_DBUS
55 /* used for one-instance mode */
56 # include <dbus/dbus.h>
57 #endif
60 #include <vlc_playlist.h>
61 #include <vlc_interface.h>
63 #include <vlc_charset.h>
64 #include <vlc_fs.h>
65 #include <vlc_cpu.h>
66 #include <vlc_url.h>
67 #include <vlc_modules.h>
69 #include "libvlc.h"
70 #include "playlist/playlist_internal.h"
71 #include "misc/variables.h"
73 #include <vlc_vlm.h>
75 #ifdef __APPLE__
76 # include <libkern/OSAtomic.h>
77 #endif
79 #include <assert.h>
81 /*****************************************************************************
82 * Local prototypes
83 *****************************************************************************/
84 static void GetFilenames ( libvlc_int_t *, unsigned, const char *const [] );
86 /**
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;
93 libvlc_priv_t *priv;
95 /* Allocate a libvlc instance object */
96 p_libvlc = vlc_custom_create( (vlc_object_t *)NULL, sizeof (*priv),
97 "libvlc" );
98 if( p_libvlc == NULL )
99 return NULL;
101 priv = libvlc_priv (p_libvlc);
102 priv->playlist = NULL;
103 priv->p_dialog_provider = NULL;
104 priv->p_vlm = NULL;
106 vlc_ExitInit( &priv->exit );
108 return p_libvlc;
112 * Initialize a libvlc instance
113 * This function initializes a previously allocated libvlc instance:
114 * - CPU detection
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;
126 char *psz_val;
128 /* System specific initialization code */
129 system_Init();
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
136 * options) */
137 module_InitBank ();
139 /* Get command line options that affect module loading. */
140 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
142 module_EndBank (false);
143 return VLC_EGENERIC;
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
151 * default values. */
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 */
161 else
162 config_LoadConfigFile( p_libvlc );
166 * Override configuration with command line settings
168 int vlc_optind;
169 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
171 vlc_LogDeinit (p_libvlc);
172 module_EndBank (true);
173 return VLC_EGENERIC;
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);
184 #endif
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);
191 exit(0);
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);
199 return VLC_ENOMOD;
202 #ifdef HAVE_DAEMON
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);
211 return VLC_ENOMEM;
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" );
219 if( stream != NULL )
221 fprintf( stream, "%d", (int)getpid() );
222 fclose( stream );
223 msg_Dbg( p_libvlc, "written PID file %s", pidfile );
225 else
226 msg_Err( p_libvlc, "cannot write PID file %s: %s",
227 pidfile, vlc_strerror_c(errno) );
228 free( pidfile );
231 else
233 var_Create( p_libvlc, "pidfile", VLC_VAR_STRING );
234 var_SetString( p_libvlc, "pidfile", "" );
236 #endif
238 /* FIXME: could be replaced by using Unix sockets */
239 #ifdef HAVE_DBUS
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",
254 ppsz_argv[i] );
255 goto dbus_out;
258 /* Initialise D-Bus interface, check for other instances */
259 dbus_threads_init_default();
261 DBusError err;
262 dbus_error_init( &err );
264 /* connect to the session bus */
265 DBusConnection *conn = dbus_bus_get( DBUS_BUS_SESSION, &err );
266 if( conn == NULL )
268 msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
269 err.message );
270 dbus_error_free( &err );
271 goto dbus_out;
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 );
285 else
286 msg_Dbg( p_libvlc, "No media player running. Continuing normally." );
287 dbus_error_free( &err );
288 goto dbus_out;
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) )
299 continue;
301 /* We need to resolve relative paths in this instance */
302 char *mrl;
303 if( strstr( ppsz_argv[i], "://" ) )
304 mrl = strdup( ppsz_argv[i] );
305 else
306 mrl = vlc_path2uri( ppsz_argv[i], NULL );
307 if( mrl == NULL )
309 dbus_message_unref( msg );
310 continue;
313 const char *after_track = MPRIS_APPEND;
315 /* append MRLs */
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 );
322 msg = NULL;
323 free( mrl );
324 continue;
327 msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
328 free( mrl );
330 /* send message and get a handle for a reply */
331 DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
332 &err );
333 dbus_message_unref( msg );
334 if( reply == NULL )
336 msg_Err( p_libvlc, "D-Bus error: %s", err.message );
337 continue;
339 dbus_message_unref( reply );
341 /* we unreference the connection when we've finished with it */
342 dbus_connection_unref( conn );
343 exit( 1 );
345 #undef MPRIS_APPEND
346 #undef MPRIS_BUS_NAME
347 #undef MPRIS_OBJECT_PATH
348 #undef MPRIS_TRACKLIST_INTERFACE
349 dbus_out:
350 #endif // HAVE_DBUS
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 );
362 * Meta data handling
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 );
405 #ifdef ENABLE_VLM
406 /* Initialize VLM if vlm-conf is specified */
407 psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" );
408 if( psz_parser )
410 priv->p_vlm = vlm_New( p_libvlc );
411 if( !priv->p_vlm )
412 msg_Err( p_libvlc, "VLM initialization failed" );
414 free( psz_parser );
415 #endif
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 )
425 char* psz_tmp;
426 if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
428 free( psz_modules );
429 psz_modules = psz_tmp;
432 else if( psz_control )
434 free( psz_modules );
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, ':' );
444 if ( psz_parser )
446 *psz_parser = '\0';
447 psz_parser++;
449 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
451 libvlc_InternalAddIntf( p_libvlc, psz_temp );
452 free( psz_temp );
455 free( psz_modules );
456 free( psz_control );
458 if( var_InheritBool( p_libvlc, "network-synchronisation") )
459 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
461 #ifdef __APPLE__
462 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
463 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
464 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
465 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
466 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
467 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
468 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
469 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
470 var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
471 #endif
474 * Get input filenames given as commandline arguments.
475 * We assume that the remaining parameters are filenames
476 * and their input options.
478 GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
481 * Get --open argument
483 psz_val = var_InheritString( p_libvlc, "open" );
484 if ( psz_val != NULL )
486 intf_InsertItem( p_libvlc, psz_val, 0, NULL, 0 );
487 free( psz_val );
490 return VLC_SUCCESS;
494 * Cleanup a libvlc instance. The instance is not completely deallocated
495 * \param p_libvlc the instance to clean
497 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
499 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
501 /* Ask the interfaces to stop and destroy them */
502 msg_Dbg( p_libvlc, "removing all interfaces" );
503 libvlc_Quit( p_libvlc );
504 intf_DestroyAll( p_libvlc );
506 #ifdef ENABLE_VLM
507 /* Destroy VLM if created in libvlc_InternalInit */
508 if( priv->p_vlm )
510 vlm_Delete( priv->p_vlm );
512 #endif
514 #if !defined( _WIN32 ) && !defined( __OS2__ )
515 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
516 if( pidfile != NULL )
518 msg_Dbg( p_libvlc, "removing PID file %s", pidfile );
519 if( unlink( pidfile ) )
520 msg_Warn( p_libvlc, "cannot remove PID file %s: %s",
521 pidfile, vlc_strerror_c(errno) );
522 free( pidfile );
524 #endif
526 if (priv->parser != NULL)
527 playlist_preparser_Delete(priv->parser);
529 vlc_DeinitActions( p_libvlc, priv->actions );
531 /* Save the configuration */
532 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
533 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
535 /* Free module bank. It is refcounted, so we call this each time */
536 vlc_LogDeinit (p_libvlc);
537 module_EndBank (true);
538 #if defined(_WIN32) || defined(__OS2__)
539 system_End( );
540 #endif
544 * Destroy everything.
545 * This function requests the running threads to finish, waits for their
546 * termination, and destroys their structure.
547 * It stops the thread systems: no instance can run after this has run
548 * \param p_libvlc the instance to destroy
550 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
552 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
554 vlc_ExitDestroy( &priv->exit );
556 assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
557 vlc_object_release( p_libvlc );
560 /*****************************************************************************
561 * GetFilenames: parse command line options which are not flags
562 *****************************************************************************
563 * Parse command line for input files as well as their associated options.
564 * An option always follows its associated input and begins with a ":".
565 *****************************************************************************/
566 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
567 const char *const args[] )
569 while( n > 0 )
571 /* Count the input options */
572 unsigned i_options = 0;
574 while( args[--n][0] == ':' )
576 i_options++;
577 if( n == 0 )
579 msg_Warn( p_vlc, "options %s without item", args[n] );
580 return; /* syntax!? */
584 char *mrl = NULL;
585 if( strstr( args[n], "://" ) == NULL )
587 mrl = vlc_path2uri( args[n], NULL );
588 if( !mrl )
589 continue;
592 intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
593 ( i_options ? &args[n + 1] : NULL ),
594 VLC_INPUT_OPTION_TRUSTED );
595 free( mrl );
600 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
601 * The actual extraction is asynchronous.
603 int libvlc_MetaRequest(libvlc_int_t *libvlc, input_item_t *item,
604 input_item_meta_request_option_t i_options)
606 libvlc_priv_t *priv = libvlc_priv(libvlc);
608 if (unlikely(priv->parser == NULL))
609 return VLC_ENOMEM;
611 vlc_mutex_lock( &item->lock );
612 if( item->i_preparse_depth == 0 )
613 item->i_preparse_depth = 1;
614 vlc_mutex_unlock( &item->lock );
615 playlist_preparser_Push(priv->parser, item, i_options);
616 return VLC_SUCCESS;
620 * Requests retrieving/downloading art for an input item.
621 * The retrieval is performed asynchronously.
623 int libvlc_ArtRequest(libvlc_int_t *libvlc, input_item_t *item,
624 input_item_meta_request_option_t i_options)
626 libvlc_priv_t *priv = libvlc_priv(libvlc);
628 if (unlikely(priv->parser == NULL))
629 return VLC_ENOMEM;
631 playlist_preparser_fetcher_Push(priv->parser, item, i_options);
632 return VLC_SUCCESS;