macOS: Assume in Xcode project that build exists
[vlc.git] / src / libvlc.c
blob4b220d2e31e018a33aa560e1667910733a1e2b04
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_dialog.h>
65 #include <vlc_keystore.h>
66 #include <vlc_fs.h>
67 #include <vlc_cpu.h>
68 #include <vlc_url.h>
69 #include <vlc_modules.h>
71 #include "libvlc.h"
72 #include "playlist/playlist_internal.h"
73 #include "misc/variables.h"
75 #include <vlc_vlm.h>
77 #ifdef __APPLE__
78 # include <libkern/OSAtomic.h>
79 #endif
81 #include <assert.h>
83 /*****************************************************************************
84 * Local prototypes
85 *****************************************************************************/
86 static void GetFilenames ( libvlc_int_t *, unsigned, const char *const [] );
88 /**
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;
95 libvlc_priv_t *priv;
97 /* Allocate a libvlc instance object */
98 p_libvlc = (vlc_custom_create)( NULL, sizeof (*priv), "libvlc" );
99 if( p_libvlc == NULL )
100 return NULL;
102 priv = libvlc_priv (p_libvlc);
103 priv->playlist = 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;
127 int i_ret = VLC_EGENERIC;
129 /* System specific initialization code */
130 system_Init();
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
137 * options) */
138 module_InitBank ();
140 /* Get command line options that affect module loading. */
141 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
143 module_EndBank (false);
144 return VLC_EGENERIC;
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
152 * default values. */
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 */
162 else
163 config_LoadConfigFile( p_libvlc );
167 * Override configuration with command line settings
169 int vlc_optind;
170 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
171 goto error;
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);
181 #endif
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);
188 exit(0);
191 if( module_count <= 1 )
193 msg_Err( p_libvlc, "No plugins found! Check your VLC installation.");
194 i_ret = VLC_ENOMOD;
195 goto error;
198 #ifdef HAVE_DAEMON
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" );
205 goto error;
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" );
213 if( stream != NULL )
215 fprintf( stream, "%d", (int)getpid() );
216 fclose( stream );
217 msg_Dbg( p_libvlc, "written PID file %s", pidfile );
219 else
220 msg_Err( p_libvlc, "cannot write PID file %s: %s",
221 pidfile, vlc_strerror_c(errno) );
222 free( pidfile );
225 else
227 var_Create( p_libvlc, "pidfile", VLC_VAR_STRING );
228 var_SetString( p_libvlc, "pidfile", "" );
230 #endif
232 i_ret = VLC_ENOMEM;
234 if( libvlc_InternalDialogInit( p_libvlc ) != VLC_SUCCESS )
235 goto error;
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 */
240 #ifdef HAVE_DBUS
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",
255 ppsz_argv[i] );
256 goto dbus_out;
259 /* Initialise D-Bus interface, check for other instances */
260 dbus_threads_init_default();
262 DBusError err;
263 dbus_error_init( &err );
265 /* connect to the session bus */
266 DBusConnection *conn = dbus_bus_get( DBUS_BUS_SESSION, &err );
267 if( conn == NULL )
269 msg_Err( p_libvlc, "Failed to connect to D-Bus session daemon: %s",
270 err.message );
271 dbus_error_free( &err );
272 goto dbus_out;
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 );
286 else
287 msg_Dbg( p_libvlc, "No media player running. Continuing normally." );
288 dbus_error_free( &err );
289 goto dbus_out;
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) )
300 continue;
302 /* We need to resolve relative paths in this instance */
303 char *mrl;
304 if( strstr( ppsz_argv[i], "://" ) )
305 mrl = strdup( ppsz_argv[i] );
306 else
307 mrl = vlc_path2uri( ppsz_argv[i], NULL );
308 if( mrl == NULL )
310 dbus_message_unref( msg );
311 continue;
314 const char *after_track = MPRIS_APPEND;
316 /* append MRLs */
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 );
323 msg = NULL;
324 free( mrl );
325 continue;
328 msg_Dbg( p_libvlc, "Adds %s to the running media player", mrl );
329 free( mrl );
331 /* send message and get a handle for a reply */
332 DBusMessage *reply = dbus_connection_send_with_reply_and_block( conn, msg, -1,
333 &err );
334 dbus_message_unref( msg );
335 if( reply == NULL )
337 msg_Err( p_libvlc, "D-Bus error: %s", err.message );
338 continue;
340 dbus_message_unref( reply );
342 /* we unreference the connection when we've finished with it */
343 dbus_connection_unref( conn );
344 exit( 0 );
346 #undef MPRIS_APPEND
347 #undef MPRIS_BUS_NAME
348 #undef MPRIS_OBJECT_PATH
349 #undef MPRIS_TRACKLIST_INTERFACE
350 dbus_out:
351 #endif // HAVE_DBUS
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 );
361 if( !priv->actions )
362 goto error;
365 * Meta data handling
367 priv->parser = playlist_preparser_New(VLC_OBJECT(p_libvlc));
368 if( !priv->parser )
369 goto error;
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 );
410 #ifdef ENABLE_VLM
411 /* Initialize VLM if vlm-conf is specified */
412 psz_parser = var_CreateGetNonEmptyString( p_libvlc, "vlm-conf" );
413 if( psz_parser )
415 priv->p_vlm = vlm_New( p_libvlc );
416 if( !priv->p_vlm )
417 msg_Err( p_libvlc, "VLM initialization failed" );
419 free( psz_parser );
420 #endif
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 )
430 char* psz_tmp;
431 if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
433 free( psz_modules );
434 psz_modules = psz_tmp;
437 else if( psz_control )
439 free( psz_modules );
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, ':' );
449 if ( psz_parser )
451 *psz_parser = '\0';
452 psz_parser++;
454 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
456 libvlc_InternalAddIntf( p_libvlc, psz_temp );
457 free( psz_temp );
460 free( psz_modules );
461 free( psz_control );
463 if( var_InheritBool( p_libvlc, "network-synchronisation") )
464 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
466 #ifdef __APPLE__
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 );
476 #endif
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 );
492 free( psz_val );
495 return VLC_SUCCESS;
497 error:
498 libvlc_InternalCleanup( p_libvlc );
499 return i_ret;
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 if (priv->parser != NULL)
511 playlist_preparser_Deactivate(priv->parser);
513 /* Ask the interfaces to stop and destroy them */
514 msg_Dbg( p_libvlc, "removing all interfaces" );
515 intf_DestroyAll( p_libvlc );
517 libvlc_InternalDialogClean( p_libvlc );
518 libvlc_InternalKeystoreClean( p_libvlc );
520 #ifdef ENABLE_VLM
521 /* Destroy VLM if created in libvlc_InternalInit */
522 if( priv->p_vlm )
524 vlm_Delete( priv->p_vlm );
526 #endif
528 #if !defined( _WIN32 ) && !defined( __OS2__ )
529 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
530 if( pidfile != NULL )
532 msg_Dbg( p_libvlc, "removing PID file %s", pidfile );
533 if( unlink( pidfile ) )
534 msg_Warn( p_libvlc, "cannot remove PID file %s: %s",
535 pidfile, vlc_strerror_c(errno) );
536 free( pidfile );
538 #endif
540 if (priv->parser != NULL)
541 playlist_preparser_Delete(priv->parser);
543 vlc_DeinitActions( p_libvlc, priv->actions );
545 /* Save the configuration */
546 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
547 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
549 /* Free module bank. It is refcounted, so we call this each time */
550 vlc_LogDeinit (p_libvlc);
551 module_EndBank (true);
552 #if defined(_WIN32) || defined(__OS2__)
553 system_End( );
554 #endif
558 * Destroy everything.
559 * This function requests the running threads to finish, waits for their
560 * termination, and destroys their structure.
561 * It stops the thread systems: no instance can run after this has run
562 * \param p_libvlc the instance to destroy
564 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
566 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
568 vlc_ExitDestroy( &priv->exit );
570 assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
571 vlc_object_release( p_libvlc );
574 /*****************************************************************************
575 * GetFilenames: parse command line options which are not flags
576 *****************************************************************************
577 * Parse command line for input files as well as their associated options.
578 * An option always follows its associated input and begins with a ":".
579 *****************************************************************************/
580 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
581 const char *const args[] )
583 while( n > 0 )
585 /* Count the input options */
586 unsigned i_options = 0;
588 while( args[--n][0] == ':' )
590 i_options++;
591 if( n == 0 )
593 msg_Warn( p_vlc, "options %s without item", args[n] );
594 return; /* syntax!? */
598 char *mrl = NULL;
599 if( strstr( args[n], "://" ) == NULL )
601 mrl = vlc_path2uri( args[n], NULL );
602 if( !mrl )
603 continue;
606 intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
607 ( i_options ? &args[n + 1] : NULL ),
608 VLC_INPUT_OPTION_TRUSTED );
609 free( mrl );
614 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
615 * The actual extraction is asynchronous. It can be cancelled with
616 * libvlc_MetadataCancel()
618 int libvlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
619 input_item_meta_request_option_t i_options,
620 int timeout, void *id)
622 libvlc_priv_t *priv = libvlc_priv(libvlc);
624 if (unlikely(priv->parser == NULL))
625 return VLC_ENOMEM;
627 vlc_mutex_lock( &item->lock );
628 if( item->i_preparse_depth == 0 )
629 item->i_preparse_depth = 1;
630 if( i_options & META_REQUEST_OPTION_DO_INTERACT )
631 item->b_preparse_interact = true;
632 vlc_mutex_unlock( &item->lock );
633 playlist_preparser_Push( priv->parser, item, i_options, timeout, id );
634 return VLC_SUCCESS;
638 * Requests retrieving/downloading art for an input item.
639 * The retrieval is performed asynchronously.
641 int libvlc_ArtRequest(libvlc_int_t *libvlc, input_item_t *item,
642 input_item_meta_request_option_t i_options)
644 libvlc_priv_t *priv = libvlc_priv(libvlc);
646 if (unlikely(priv->parser == NULL))
647 return VLC_ENOMEM;
649 playlist_preparser_fetcher_Push(priv->parser, item, i_options);
650 return VLC_SUCCESS;
654 * Cancels extraction of the meta data for an input item.
656 * This does nothing if the input item is already processed or if it was not
657 * added with libvlc_MetadataRequest()
659 void libvlc_MetadataCancel(libvlc_int_t *libvlc, void *id)
661 libvlc_priv_t *priv = libvlc_priv(libvlc);
663 if (unlikely(priv->parser == NULL))
664 return;
666 playlist_preparser_Cancel(priv->parser, id);