gui: macos: use float for rate
[vlc.git] / src / libvlc.c
blob1ef103085c295259f6b98928e51cd2f093c0a255
1 /*****************************************************************************
2 * libvlc.c: libvlc instances creation and deletion, interfaces handling
3 *****************************************************************************
4 * Copyright (C) 1998-2008 VLC authors and VideoLAN
6 * Authors: Vincent Seguin <seguin@via.ecp.fr>
7 * Samuel Hocevar <sam@zoy.org>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Derk-Jan Hartman <hartman at videolan dot org>
10 * RĂ©mi Denis-Courmont <rem # videolan : org>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /** \file
28 * This file contains functions to create and destroy libvlc instances
31 /*****************************************************************************
32 * Preamble
33 *****************************************************************************/
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
38 #include <vlc_common.h>
39 #include "../lib/libvlc_internal.h"
40 #include <vlc_input.h>
42 #include "modules/modules.h"
43 #include "config/configuration.h"
44 #include "preparser/preparser.h"
45 #include "media_source/media_source.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 #include <vlc_playlist_legacy.h>
55 #include <vlc_playlist.h>
56 #include <vlc_interface.h>
58 #include <vlc_actions.h>
59 #include <vlc_charset.h>
60 #include <vlc_dialog.h>
61 #include <vlc_keystore.h>
62 #include <vlc_fs.h>
63 #include <vlc_cpu.h>
64 #include <vlc_url.h>
65 #include <vlc_modules.h>
66 #include <vlc_media_library.h>
67 #include <vlc_thumbnailer.h>
69 #include "libvlc.h"
70 #include "playlist_legacy/playlist_internal.h"
71 #include "misc/variables.h"
72 #include "input/player.h"
74 #include <vlc_vlm.h>
76 #include <assert.h>
78 /*****************************************************************************
79 * Local prototypes
80 *****************************************************************************/
81 static void GetFilenames ( libvlc_int_t *, unsigned, const char *const [] );
83 /**
84 * Allocate a blank libvlc instance, also setting the exit handler.
85 * Vlc's threading system must have been initialized first
87 libvlc_int_t * libvlc_InternalCreate( void )
89 libvlc_int_t *p_libvlc;
90 libvlc_priv_t *priv;
92 /* Allocate a libvlc instance object */
93 p_libvlc = (vlc_custom_create)( NULL, sizeof (*priv), "libvlc" );
94 if( p_libvlc == NULL )
95 return NULL;
97 priv = libvlc_priv (p_libvlc);
98 priv->playlist = NULL;
99 priv->main_playlist = NULL;
100 priv->p_vlm = NULL;
101 priv->media_source_provider = NULL;
103 vlc_ExitInit( &priv->exit );
105 return p_libvlc;
108 static void
109 PlaylistConfigureFromVariables(vlc_playlist_t *playlist, vlc_object_t *obj)
111 enum vlc_playlist_playback_order order;
112 if (var_InheritBool(obj, "random"))
113 order = VLC_PLAYLIST_PLAYBACK_ORDER_RANDOM;
114 else
115 order = VLC_PLAYLIST_PLAYBACK_ORDER_NORMAL;
117 /* repeat = repeat current; loop = repeat all */
118 enum vlc_playlist_playback_repeat repeat;
119 if (var_InheritBool(obj, "repeat"))
120 repeat = VLC_PLAYLIST_PLAYBACK_REPEAT_CURRENT;
121 else if (var_InheritBool(obj, "loop"))
122 repeat = VLC_PLAYLIST_PLAYBACK_REPEAT_ALL;
123 else
124 repeat = VLC_PLAYLIST_PLAYBACK_REPEAT_NONE;
126 enum vlc_player_media_stopped_action media_stopped_action;
127 if (var_InheritBool(obj, "play-and-exit"))
128 media_stopped_action = VLC_PLAYER_MEDIA_STOPPED_EXIT;
129 else if (var_InheritBool(obj, "play-and-stop"))
130 media_stopped_action = VLC_PLAYER_MEDIA_STOPPED_STOP;
131 else if (var_InheritBool(obj, "play-and-pause"))
132 media_stopped_action = VLC_PLAYER_MEDIA_STOPPED_PAUSE;
133 else
134 media_stopped_action = VLC_PLAYER_MEDIA_STOPPED_CONTINUE;
136 bool start_paused = var_InheritBool(obj, "start-paused");
138 vlc_playlist_Lock(playlist);
139 vlc_playlist_SetPlaybackOrder(playlist, order);
140 vlc_playlist_SetPlaybackRepeat(playlist, repeat);
142 vlc_player_t *player = vlc_playlist_GetPlayer(playlist);
144 /* the playlist and the player share the same lock, and this is not an
145 * implementation detail */
146 vlc_player_SetMediaStoppedAction(player, media_stopped_action);
147 vlc_player_SetStartPaused(player, start_paused);
149 vlc_playlist_Unlock(playlist);
153 * Initialize a libvlc instance
154 * This function initializes a previously allocated libvlc instance:
155 * - CPU detection
156 * - gettext initialization
157 * - message queue, module bank and playlist initialization
158 * - configuration and commandline parsing
160 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
161 const char *ppsz_argv[] )
163 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
164 char * psz_modules = NULL;
165 char * psz_parser = NULL;
166 char * psz_control = NULL;
167 char *psz_val;
168 int i_ret = VLC_EGENERIC;
170 if (unlikely(vlc_LogPreinit(p_libvlc)))
171 return VLC_ENOMEM;
173 /* System specific initialization code */
174 system_Init();
176 /* Initialize the module bank and load the configuration of the
177 * core module. We need to do this at this stage to be able to display
178 * a short help if required by the user. (short help == core module
179 * options) */
180 module_InitBank ();
182 /* Get command line options that affect module loading. */
183 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
185 module_EndBank (false);
186 return VLC_EGENERIC;
189 vlc_threads_setup (p_libvlc);
191 /* Load the builtins and plugins into the module_bank.
192 * We have to do it before config_Load*() because this also gets the
193 * list of configuration options exported by each module and loads their
194 * default values. */
195 module_LoadPlugins (p_libvlc);
198 * Override default configuration with config file settings
200 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
202 if( var_InheritBool( p_libvlc, "reset-config" ) )
203 config_SaveConfigFile( p_libvlc ); /* Save default config */
204 else
205 config_LoadConfigFile( p_libvlc );
209 * Override configuration with command line settings
211 int vlc_optind;
212 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
213 goto error;
215 vlc_LogInit(p_libvlc);
218 * Support for gettext
220 #if defined( ENABLE_NLS ) \
221 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
222 vlc_bindtextdomain (PACKAGE_NAME);
223 #endif
224 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
225 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
227 if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
229 libvlc_InternalCleanup (p_libvlc);
230 exit(0);
233 #ifdef HAVE_DAEMON
234 /* Check for daemon mode */
235 if( var_InheritBool( p_libvlc, "daemon" ) )
237 if( daemon( 1, 0) != 0 )
239 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
240 goto error;
243 /* lets check if we need to write the pidfile */
244 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
245 if( pidfile != NULL )
247 FILE *stream = vlc_fopen( pidfile, "w" );
248 if( stream != NULL )
250 fprintf( stream, "%d", (int)getpid() );
251 fclose( stream );
252 msg_Dbg( p_libvlc, "written PID file %s", pidfile );
254 else
255 msg_Err( p_libvlc, "cannot write PID file %s: %s",
256 pidfile, vlc_strerror_c(errno) );
257 free( pidfile );
260 #endif
262 i_ret = VLC_ENOMEM;
264 if( libvlc_InternalDialogInit( p_libvlc ) != VLC_SUCCESS )
265 goto error;
266 if( libvlc_InternalKeystoreInit( p_libvlc ) != VLC_SUCCESS )
267 msg_Warn( p_libvlc, "memory keystore init failed" );
269 vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
271 if( var_InheritBool( p_libvlc, "media-library") )
273 priv->p_media_library = libvlc_MlCreate( p_libvlc );
274 if ( priv->p_media_library == NULL )
275 msg_Warn( p_libvlc, "Media library initialization failed" );
278 priv->p_thumbnailer = vlc_thumbnailer_Create( VLC_OBJECT( p_libvlc ) );
279 if ( priv->p_thumbnailer == NULL )
280 msg_Warn( p_libvlc, "Failed to instantiate VLC thumbnailer" );
283 * Initialize hotkey handling
285 if( libvlc_InternalActionsInit( p_libvlc ) != VLC_SUCCESS )
286 goto error;
289 * Meta data handling
291 priv->parser = input_preparser_New(VLC_OBJECT(p_libvlc));
292 if( !priv->parser )
293 goto error;
295 priv->media_source_provider = vlc_media_source_provider_New( VLC_OBJECT( p_libvlc ) );
296 if( !priv->media_source_provider )
297 goto error;
299 /* variables for signalling creation of new files */
300 var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
301 var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
303 /* some default internal settings */
304 var_Create( p_libvlc, "window", VLC_VAR_STRING );
305 /* NOTE: Because the playlist and interfaces start before this function
306 * returns control to the application (DESIGN BUG!), all these variables
307 * must be created (in place of libvlc_new()) and set to VLC defaults
308 * (in place of VLC main()) *here*. */
309 var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
310 var_SetString( p_libvlc, "user-agent",
311 "VLC media player (LibVLC "VERSION")" );
312 var_Create( p_libvlc, "http-user-agent", VLC_VAR_STRING );
313 var_SetString( p_libvlc, "http-user-agent",
314 "VLC/"PACKAGE_VERSION" LibVLC/"PACKAGE_VERSION );
315 var_Create( p_libvlc, "app-icon-name", VLC_VAR_STRING );
316 var_SetString( p_libvlc, "app-icon-name", PACKAGE_NAME );
317 var_Create( p_libvlc, "app-id", VLC_VAR_STRING );
318 var_SetString( p_libvlc, "app-id", "org.VideoLAN.VLC" );
319 var_Create( p_libvlc, "app-version", VLC_VAR_STRING );
320 var_SetString( p_libvlc, "app-version", PACKAGE_VERSION );
322 /* System specific configuration */
323 system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
325 #ifdef ENABLE_VLM
326 /* Initialize VLM if vlm-conf is specified */
327 psz_parser = var_InheritString( p_libvlc, "vlm-conf" );
328 if( psz_parser )
330 priv->p_vlm = vlm_New( p_libvlc, psz_parser );
331 if( !priv->p_vlm )
332 msg_Err( p_libvlc, "VLM initialization failed" );
333 free( psz_parser );
335 #endif
337 priv->main_playlist = vlc_playlist_New(VLC_OBJECT(p_libvlc));
338 if (unlikely(!priv->main_playlist))
339 goto error;
341 PlaylistConfigureFromVariables(priv->main_playlist, VLC_OBJECT(p_libvlc));
344 * Load background interfaces
346 psz_modules = var_InheritString( p_libvlc, "extraintf" );
347 psz_control = var_InheritString( p_libvlc, "control" );
349 if( psz_modules && psz_control )
351 char* psz_tmp;
352 if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
354 free( psz_modules );
355 psz_modules = psz_tmp;
358 else if( psz_control )
360 free( psz_modules );
361 psz_modules = strdup( psz_control );
364 psz_parser = psz_modules;
365 while ( psz_parser && *psz_parser )
367 char *psz_module, *psz_temp;
368 psz_module = psz_parser;
369 psz_parser = strchr( psz_module, ':' );
370 if ( psz_parser )
372 *psz_parser = '\0';
373 psz_parser++;
375 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
377 libvlc_InternalAddIntf( p_libvlc, psz_temp );
378 free( psz_temp );
381 free( psz_modules );
382 free( psz_control );
384 if( var_InheritBool( p_libvlc, "network-synchronisation") )
385 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
387 #ifdef __APPLE__
388 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
389 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
390 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
391 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
392 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
393 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
394 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
395 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
396 var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
397 #endif
400 * Get input filenames given as commandline arguments.
401 * We assume that the remaining parameters are filenames
402 * and their input options.
404 GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
407 * Get --open argument
409 psz_val = var_InheritString( p_libvlc, "open" );
410 if ( psz_val != NULL )
412 intf_InsertItem( p_libvlc, psz_val, 0, NULL, 0 );
413 free( psz_val );
416 return VLC_SUCCESS;
418 error:
419 libvlc_InternalCleanup( p_libvlc );
420 return i_ret;
424 * Cleanup a libvlc instance. The instance is not completely deallocated
425 * \param p_libvlc the instance to clean
427 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
429 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
431 if (priv->parser != NULL)
432 input_preparser_Deactivate(priv->parser);
434 /* Ask the interfaces to stop and destroy them */
435 msg_Dbg( p_libvlc, "removing all interfaces" );
436 intf_DestroyAll( p_libvlc );
438 if ( priv->p_thumbnailer )
439 vlc_thumbnailer_Release( priv->p_thumbnailer );
441 if ( priv->p_media_library )
442 libvlc_MlRelease( priv->p_media_library );
444 if( priv->media_source_provider )
445 vlc_media_source_provider_Delete( priv->media_source_provider );
447 libvlc_InternalDialogClean( p_libvlc );
448 libvlc_InternalKeystoreClean( p_libvlc );
450 #ifdef ENABLE_VLM
451 /* Destroy VLM if created in libvlc_InternalInit */
452 if( priv->p_vlm )
454 vlm_Delete( priv->p_vlm );
456 #endif
458 #if !defined( _WIN32 ) && !defined( __OS2__ )
459 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
460 if( pidfile != NULL )
462 msg_Dbg( p_libvlc, "removing PID file %s", pidfile );
463 if( unlink( pidfile ) )
464 msg_Warn( p_libvlc, "cannot remove PID file %s: %s",
465 pidfile, vlc_strerror_c(errno) );
466 free( pidfile );
468 #endif
470 if (priv->parser != NULL)
471 input_preparser_Delete(priv->parser);
473 if (priv->main_playlist)
474 vlc_playlist_Delete(priv->main_playlist);
476 libvlc_InternalActionsClean( p_libvlc );
478 /* Save the configuration */
479 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
480 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
482 /* Free module bank. It is refcounted, so we call this each time */
483 vlc_LogDeinit (p_libvlc);
484 module_EndBank (true);
485 #if defined(_WIN32) || defined(__OS2__)
486 system_End( );
487 #endif
491 * Destroy everything.
492 * This function requests the running threads to finish, waits for their
493 * termination, and destroys their structure.
494 * It stops the thread systems: no instance can run after this has run
495 * \param p_libvlc the instance to destroy
497 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
499 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
501 vlc_ExitDestroy( &priv->exit );
503 assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
504 vlc_object_release( p_libvlc );
507 /*****************************************************************************
508 * GetFilenames: parse command line options which are not flags
509 *****************************************************************************
510 * Parse command line for input files as well as their associated options.
511 * An option always follows its associated input and begins with a ":".
512 *****************************************************************************/
513 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
514 const char *const args[] )
516 while( n > 0 )
518 /* Count the input options */
519 unsigned i_options = 0;
521 while( args[--n][0] == ':' )
523 i_options++;
524 if( n == 0 )
526 msg_Warn( p_vlc, "options %s without item", args[n] );
527 return; /* syntax!? */
531 char *mrl = NULL;
532 if( strstr( args[n], "://" ) == NULL )
534 mrl = vlc_path2uri( args[n], NULL );
535 if( !mrl )
536 continue;
539 intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
540 ( i_options ? &args[n + 1] : NULL ),
541 VLC_INPUT_OPTION_TRUSTED );
542 free( mrl );
546 int vlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
547 input_item_meta_request_option_t i_options,
548 const input_preparser_callbacks_t *cbs,
549 void *cbs_userdata,
550 int timeout, void *id)
552 libvlc_priv_t *priv = libvlc_priv(libvlc);
554 if (unlikely(priv->parser == NULL))
555 return VLC_ENOMEM;
557 if( i_options & META_REQUEST_OPTION_DO_INTERACT )
559 vlc_mutex_lock( &item->lock );
560 item->b_preparse_interact = true;
561 vlc_mutex_unlock( &item->lock );
563 input_preparser_Push( priv->parser, item, i_options, cbs, cbs_userdata, timeout, id );
564 return VLC_SUCCESS;
569 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
570 * The actual extraction is asynchronous. It can be cancelled with
571 * libvlc_MetadataCancel()
573 int libvlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
574 input_item_meta_request_option_t i_options,
575 const input_preparser_callbacks_t *cbs,
576 void *cbs_userdata,
577 int timeout, void *id)
579 libvlc_priv_t *priv = libvlc_priv(libvlc);
581 if (unlikely(priv->parser == NULL))
582 return VLC_ENOMEM;
584 vlc_mutex_lock( &item->lock );
585 if( item->i_preparse_depth == 0 )
586 item->i_preparse_depth = 1;
587 vlc_mutex_unlock( &item->lock );
589 return vlc_MetadataRequest(libvlc, item, i_options, cbs, cbs_userdata, timeout, id);
593 * Requests retrieving/downloading art for an input item.
594 * The retrieval is performed asynchronously.
596 int libvlc_ArtRequest(libvlc_int_t *libvlc, input_item_t *item,
597 input_item_meta_request_option_t i_options,
598 const input_fetcher_callbacks_t *cbs,
599 void *cbs_userdata)
601 libvlc_priv_t *priv = libvlc_priv(libvlc);
603 if (unlikely(priv->parser == NULL))
604 return VLC_ENOMEM;
606 input_preparser_fetcher_Push(priv->parser, item, i_options, cbs, cbs_userdata);
607 return VLC_SUCCESS;
611 * Cancels extraction of the meta data for an input item.
613 * This does nothing if the input item is already processed or if it was not
614 * added with libvlc_MetadataRequest()
616 void libvlc_MetadataCancel(libvlc_int_t *libvlc, void *id)
618 libvlc_priv_t *priv = libvlc_priv(libvlc);
620 if (unlikely(priv->parser == NULL))
621 return;
623 input_preparser_Cancel(priv->parser, id);