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 "preparser/preparser.h"
47 #include <stdio.h> /* sprintf() */
49 #include <stdlib.h> /* free() */
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>
65 #include <vlc_modules.h>
66 #include <vlc_media_library.h>
67 #include <vlc_thumbnailer.h>
70 #include "playlist_legacy/playlist_internal.h"
71 #include "misc/variables.h"
72 #include "input/player.h"
78 /*****************************************************************************
80 *****************************************************************************/
81 static void GetFilenames ( libvlc_int_t
*, unsigned, const char *const [] );
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
;
92 /* Allocate a libvlc instance object */
93 p_libvlc
= (vlc_custom_create
)( NULL
, sizeof (*priv
), "libvlc" );
94 if( p_libvlc
== NULL
)
97 priv
= libvlc_priv (p_libvlc
);
98 priv
->playlist
= NULL
;
99 priv
->main_playlist
= NULL
;
102 vlc_ExitInit( &priv
->exit
);
108 PlaylistConfigureFromVariables(vlc_playlist_t
*playlist
, vlc_object_t
*obj
)
110 enum vlc_playlist_playback_order order
;
111 if (var_InheritBool(obj
, "random"))
112 order
= VLC_PLAYLIST_PLAYBACK_ORDER_RANDOM
;
114 order
= VLC_PLAYLIST_PLAYBACK_ORDER_NORMAL
;
116 /* repeat = repeat current; loop = repeat all */
117 enum vlc_playlist_playback_repeat repeat
;
118 if (var_InheritBool(obj
, "repeat"))
119 repeat
= VLC_PLAYLIST_PLAYBACK_REPEAT_CURRENT
;
120 else if (var_InheritBool(obj
, "loop"))
121 repeat
= VLC_PLAYLIST_PLAYBACK_REPEAT_ALL
;
123 repeat
= VLC_PLAYLIST_PLAYBACK_REPEAT_NONE
;
125 enum vlc_player_media_stopped_action media_stopped_action
;
126 if (var_InheritBool(obj
, "play-and-exit"))
127 media_stopped_action
= VLC_PLAYER_MEDIA_STOPPED_EXIT
;
128 else if (var_InheritBool(obj
, "play-and-stop"))
129 media_stopped_action
= VLC_PLAYER_MEDIA_STOPPED_STOP
;
130 else if (var_InheritBool(obj
, "play-and-pause"))
131 media_stopped_action
= VLC_PLAYER_MEDIA_STOPPED_PAUSE
;
133 media_stopped_action
= VLC_PLAYER_MEDIA_STOPPED_CONTINUE
;
135 bool start_paused
= var_InheritBool(obj
, "start-paused");
137 vlc_playlist_Lock(playlist
);
138 vlc_playlist_SetPlaybackOrder(playlist
, order
);
139 vlc_playlist_SetPlaybackRepeat(playlist
, repeat
);
141 vlc_player_t
*player
= vlc_playlist_GetPlayer(playlist
);
143 /* the playlist and the player share the same lock, and this is not an
144 * implementation detail */
145 vlc_player_SetMediaStoppedAction(player
, media_stopped_action
);
146 vlc_player_SetStartPaused(player
, start_paused
);
148 vlc_playlist_Unlock(playlist
);
152 * Initialize a libvlc instance
153 * This function initializes a previously allocated libvlc instance:
155 * - gettext initialization
156 * - message queue, module bank and playlist initialization
157 * - configuration and commandline parsing
159 int libvlc_InternalInit( libvlc_int_t
*p_libvlc
, int i_argc
,
160 const char *ppsz_argv
[] )
162 libvlc_priv_t
*priv
= libvlc_priv (p_libvlc
);
163 char * psz_modules
= NULL
;
164 char * psz_parser
= NULL
;
165 char * psz_control
= NULL
;
167 int i_ret
= VLC_EGENERIC
;
169 if (unlikely(vlc_LogPreinit(p_libvlc
)))
172 /* System specific initialization code */
175 /* Initialize the module bank and load the configuration of the
176 * core module. We need to do this at this stage to be able to display
177 * a short help if required by the user. (short help == core module
181 /* Get command line options that affect module loading. */
182 if( config_LoadCmdLine( p_libvlc
, i_argc
, ppsz_argv
, NULL
) )
184 module_EndBank (false);
188 vlc_threads_setup (p_libvlc
);
190 /* Load the builtins and plugins into the module_bank.
191 * We have to do it before config_Load*() because this also gets the
192 * list of configuration options exported by each module and loads their
194 module_LoadPlugins (p_libvlc
);
197 * Override default configuration with config file settings
199 if( !var_InheritBool( p_libvlc
, "ignore-config" ) )
201 if( var_InheritBool( p_libvlc
, "reset-config" ) )
202 config_SaveConfigFile( p_libvlc
); /* Save default config */
204 config_LoadConfigFile( p_libvlc
);
208 * Override configuration with command line settings
211 if( config_LoadCmdLine( p_libvlc
, i_argc
, ppsz_argv
, &vlc_optind
) )
214 vlc_LogInit(p_libvlc
);
217 * Support for gettext
219 #if defined( ENABLE_NLS ) \
220 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
221 vlc_bindtextdomain (PACKAGE_NAME
);
223 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
224 msg_Dbg( p_libvlc
, "translation test: code is \"%s\"", _("C") );
226 if (config_PrintHelp (VLC_OBJECT(p_libvlc
)))
228 libvlc_InternalCleanup (p_libvlc
);
233 /* Check for daemon mode */
234 if( var_InheritBool( p_libvlc
, "daemon" ) )
236 if( daemon( 1, 0) != 0 )
238 msg_Err( p_libvlc
, "Unable to fork vlc to daemon mode" );
242 /* lets check if we need to write the pidfile */
243 char *pidfile
= var_InheritString( p_libvlc
, "pidfile" );
244 if( pidfile
!= NULL
)
246 FILE *stream
= vlc_fopen( pidfile
, "w" );
249 fprintf( stream
, "%d", (int)getpid() );
251 msg_Dbg( p_libvlc
, "written PID file %s", pidfile
);
254 msg_Err( p_libvlc
, "cannot write PID file %s: %s",
255 pidfile
, vlc_strerror_c(errno
) );
263 if( libvlc_InternalDialogInit( p_libvlc
) != VLC_SUCCESS
)
265 if( libvlc_InternalKeystoreInit( p_libvlc
) != VLC_SUCCESS
)
266 msg_Warn( p_libvlc
, "memory keystore init failed" );
268 vlc_CPU_dump( VLC_OBJECT(p_libvlc
) );
270 if( var_InheritBool( p_libvlc
, "media-library") )
272 priv
->p_media_library
= libvlc_MlCreate( p_libvlc
);
273 if ( priv
->p_media_library
== NULL
)
274 msg_Warn( p_libvlc
, "Media library initialization failed" );
277 priv
->p_thumbnailer
= vlc_thumbnailer_Create( VLC_OBJECT( p_libvlc
) );
278 if ( priv
->p_thumbnailer
== NULL
)
279 msg_Warn( p_libvlc
, "Failed to instantiate VLC thumbnailer" );
282 * Initialize hotkey handling
284 if( libvlc_InternalActionsInit( p_libvlc
) != VLC_SUCCESS
)
290 priv
->parser
= input_preparser_New(VLC_OBJECT(p_libvlc
));
294 /* variables for signalling creation of new files */
295 var_Create( p_libvlc
, "snapshot-file", VLC_VAR_STRING
);
296 var_Create( p_libvlc
, "record-file", VLC_VAR_STRING
);
298 /* some default internal settings */
299 var_Create( p_libvlc
, "window", VLC_VAR_STRING
);
300 /* NOTE: Because the playlist and interfaces start before this function
301 * returns control to the application (DESIGN BUG!), all these variables
302 * must be created (in place of libvlc_new()) and set to VLC defaults
303 * (in place of VLC main()) *here*. */
304 var_Create( p_libvlc
, "user-agent", VLC_VAR_STRING
);
305 var_SetString( p_libvlc
, "user-agent",
306 "VLC media player (LibVLC "VERSION
")" );
307 var_Create( p_libvlc
, "http-user-agent", VLC_VAR_STRING
);
308 var_SetString( p_libvlc
, "http-user-agent",
309 "VLC/"PACKAGE_VERSION
" LibVLC/"PACKAGE_VERSION
);
310 var_Create( p_libvlc
, "app-icon-name", VLC_VAR_STRING
);
311 var_SetString( p_libvlc
, "app-icon-name", PACKAGE_NAME
);
312 var_Create( p_libvlc
, "app-id", VLC_VAR_STRING
);
313 var_SetString( p_libvlc
, "app-id", "org.VideoLAN.VLC" );
314 var_Create( p_libvlc
, "app-version", VLC_VAR_STRING
);
315 var_SetString( p_libvlc
, "app-version", PACKAGE_VERSION
);
317 /* System specific configuration */
318 system_Configure( p_libvlc
, i_argc
- vlc_optind
, ppsz_argv
+ vlc_optind
);
321 /* Initialize VLM if vlm-conf is specified */
322 psz_parser
= var_InheritString( p_libvlc
, "vlm-conf" );
325 priv
->p_vlm
= vlm_New( p_libvlc
, psz_parser
);
327 msg_Err( p_libvlc
, "VLM initialization failed" );
332 priv
->main_playlist
= vlc_playlist_New(VLC_OBJECT(p_libvlc
));
333 if (unlikely(!priv
->main_playlist
))
336 PlaylistConfigureFromVariables(priv
->main_playlist
, VLC_OBJECT(p_libvlc
));
339 * Load background interfaces
341 psz_modules
= var_InheritString( p_libvlc
, "extraintf" );
342 psz_control
= var_InheritString( p_libvlc
, "control" );
344 if( psz_modules
&& psz_control
)
347 if( asprintf( &psz_tmp
, "%s:%s", psz_modules
, psz_control
) != -1 )
350 psz_modules
= psz_tmp
;
353 else if( psz_control
)
356 psz_modules
= strdup( psz_control
);
359 psz_parser
= psz_modules
;
360 while ( psz_parser
&& *psz_parser
)
362 char *psz_module
, *psz_temp
;
363 psz_module
= psz_parser
;
364 psz_parser
= strchr( psz_module
, ':' );
370 if( asprintf( &psz_temp
, "%s,none", psz_module
) != -1)
372 libvlc_InternalAddIntf( p_libvlc
, psz_temp
);
379 if( var_InheritBool( p_libvlc
, "network-synchronisation") )
380 libvlc_InternalAddIntf( p_libvlc
, "netsync,none" );
383 var_Create( p_libvlc
, "drawable-view-top", VLC_VAR_INTEGER
);
384 var_Create( p_libvlc
, "drawable-view-left", VLC_VAR_INTEGER
);
385 var_Create( p_libvlc
, "drawable-view-bottom", VLC_VAR_INTEGER
);
386 var_Create( p_libvlc
, "drawable-view-right", VLC_VAR_INTEGER
);
387 var_Create( p_libvlc
, "drawable-clip-top", VLC_VAR_INTEGER
);
388 var_Create( p_libvlc
, "drawable-clip-left", VLC_VAR_INTEGER
);
389 var_Create( p_libvlc
, "drawable-clip-bottom", VLC_VAR_INTEGER
);
390 var_Create( p_libvlc
, "drawable-clip-right", VLC_VAR_INTEGER
);
391 var_Create( p_libvlc
, "drawable-nsobject", VLC_VAR_ADDRESS
);
395 * Get input filenames given as commandline arguments.
396 * We assume that the remaining parameters are filenames
397 * and their input options.
399 GetFilenames( p_libvlc
, i_argc
- vlc_optind
, ppsz_argv
+ vlc_optind
);
402 * Get --open argument
404 psz_val
= var_InheritString( p_libvlc
, "open" );
405 if ( psz_val
!= NULL
)
407 intf_InsertItem( p_libvlc
, psz_val
, 0, NULL
, 0 );
414 libvlc_InternalCleanup( p_libvlc
);
419 * Cleanup a libvlc instance. The instance is not completely deallocated
420 * \param p_libvlc the instance to clean
422 void libvlc_InternalCleanup( libvlc_int_t
*p_libvlc
)
424 libvlc_priv_t
*priv
= libvlc_priv (p_libvlc
);
426 if (priv
->parser
!= NULL
)
427 input_preparser_Deactivate(priv
->parser
);
429 /* Ask the interfaces to stop and destroy them */
430 msg_Dbg( p_libvlc
, "removing all interfaces" );
431 intf_DestroyAll( p_libvlc
);
433 if ( priv
->p_thumbnailer
)
434 vlc_thumbnailer_Release( priv
->p_thumbnailer
);
436 if ( priv
->p_media_library
)
437 libvlc_MlRelease( priv
->p_media_library
);
439 libvlc_InternalDialogClean( p_libvlc
);
440 libvlc_InternalKeystoreClean( p_libvlc
);
443 /* Destroy VLM if created in libvlc_InternalInit */
446 vlm_Delete( priv
->p_vlm
);
450 #if !defined( _WIN32 ) && !defined( __OS2__ )
451 char *pidfile
= var_InheritString( p_libvlc
, "pidfile" );
452 if( pidfile
!= NULL
)
454 msg_Dbg( p_libvlc
, "removing PID file %s", pidfile
);
455 if( unlink( pidfile
) )
456 msg_Warn( p_libvlc
, "cannot remove PID file %s: %s",
457 pidfile
, vlc_strerror_c(errno
) );
462 if (priv
->parser
!= NULL
)
463 input_preparser_Delete(priv
->parser
);
465 if (priv
->main_playlist
)
466 vlc_playlist_Delete(priv
->main_playlist
);
468 libvlc_InternalActionsClean( p_libvlc
);
470 /* Save the configuration */
471 if( !var_InheritBool( p_libvlc
, "ignore-config" ) )
472 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc
) );
474 /* Free module bank. It is refcounted, so we call this each time */
475 vlc_LogDeinit (p_libvlc
);
476 module_EndBank (true);
477 #if defined(_WIN32) || defined(__OS2__)
483 * Destroy everything.
484 * This function requests the running threads to finish, waits for their
485 * termination, and destroys their structure.
486 * It stops the thread systems: no instance can run after this has run
487 * \param p_libvlc the instance to destroy
489 void libvlc_InternalDestroy( libvlc_int_t
*p_libvlc
)
491 libvlc_priv_t
*priv
= libvlc_priv( p_libvlc
);
493 vlc_ExitDestroy( &priv
->exit
);
495 assert( atomic_load(&(vlc_internals(p_libvlc
)->refs
)) == 1 );
496 vlc_object_release( p_libvlc
);
499 /*****************************************************************************
500 * GetFilenames: parse command line options which are not flags
501 *****************************************************************************
502 * Parse command line for input files as well as their associated options.
503 * An option always follows its associated input and begins with a ":".
504 *****************************************************************************/
505 static void GetFilenames( libvlc_int_t
*p_vlc
, unsigned n
,
506 const char *const args
[] )
510 /* Count the input options */
511 unsigned i_options
= 0;
513 while( args
[--n
][0] == ':' )
518 msg_Warn( p_vlc
, "options %s without item", args
[n
] );
519 return; /* syntax!? */
524 if( strstr( args
[n
], "://" ) == NULL
)
526 mrl
= vlc_path2uri( args
[n
], NULL
);
531 intf_InsertItem( p_vlc
, (mrl
!= NULL
) ? mrl
: args
[n
], i_options
,
532 ( i_options
? &args
[n
+ 1] : NULL
),
533 VLC_INPUT_OPTION_TRUSTED
);
538 int vlc_MetadataRequest(libvlc_int_t
*libvlc
, input_item_t
*item
,
539 input_item_meta_request_option_t i_options
,
540 const input_preparser_callbacks_t
*cbs
,
542 int timeout
, void *id
)
544 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
546 if (unlikely(priv
->parser
== NULL
))
549 if( i_options
& META_REQUEST_OPTION_DO_INTERACT
)
551 vlc_mutex_lock( &item
->lock
);
552 item
->b_preparse_interact
= true;
553 vlc_mutex_unlock( &item
->lock
);
555 input_preparser_Push( priv
->parser
, item
, i_options
, cbs
, cbs_userdata
, timeout
, id
);
561 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
562 * The actual extraction is asynchronous. It can be cancelled with
563 * libvlc_MetadataCancel()
565 int libvlc_MetadataRequest(libvlc_int_t
*libvlc
, input_item_t
*item
,
566 input_item_meta_request_option_t i_options
,
567 const input_preparser_callbacks_t
*cbs
,
569 int timeout
, void *id
)
571 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
573 if (unlikely(priv
->parser
== NULL
))
576 vlc_mutex_lock( &item
->lock
);
577 if( item
->i_preparse_depth
== 0 )
578 item
->i_preparse_depth
= 1;
579 vlc_mutex_unlock( &item
->lock
);
581 return vlc_MetadataRequest(libvlc
, item
, i_options
, cbs
, cbs_userdata
, timeout
, id
);
585 * Requests retrieving/downloading art for an input item.
586 * The retrieval is performed asynchronously.
588 int libvlc_ArtRequest(libvlc_int_t
*libvlc
, input_item_t
*item
,
589 input_item_meta_request_option_t i_options
,
590 const input_fetcher_callbacks_t
*cbs
,
593 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
595 if (unlikely(priv
->parser
== NULL
))
598 input_preparser_fetcher_Push(priv
->parser
, item
, i_options
, cbs
, cbs_userdata
);
603 * Cancels extraction of the meta data for an input item.
605 * This does nothing if the input item is already processed or if it was not
606 * added with libvlc_MetadataRequest()
608 void libvlc_MetadataCancel(libvlc_int_t
*libvlc
, void *id
)
610 libvlc_priv_t
*priv
= libvlc_priv(libvlc
);
612 if (unlikely(priv
->parser
== NULL
))
615 input_preparser_Cancel(priv
->parser
, id
);