intf: fix win32 build
[vlc.git] / src / libvlc.c
blob8200f81d9ec46377a26c2a9ded726e8f089f6806
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"
73 #include <vlc_vlm.h>
75 #include <assert.h>
77 /*****************************************************************************
78 * Local prototypes
79 *****************************************************************************/
80 static void GetFilenames ( libvlc_int_t *, unsigned, const char *const [] );
82 /**
83 * Allocate a blank libvlc instance, also setting the exit handler.
84 * Vlc's threading system must have been initialized first
86 libvlc_int_t * libvlc_InternalCreate( void )
88 libvlc_int_t *p_libvlc;
89 libvlc_priv_t *priv;
91 /* Allocate a libvlc instance object */
92 p_libvlc = (vlc_custom_create)( NULL, sizeof (*priv), "libvlc" );
93 if( p_libvlc == NULL )
94 return NULL;
96 priv = libvlc_priv (p_libvlc);
97 priv->interfaces = NULL;
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;
109 * Initialize a libvlc instance
110 * This function initializes a previously allocated libvlc instance:
111 * - CPU detection
112 * - gettext initialization
113 * - message queue, module bank and playlist initialization
114 * - configuration and commandline parsing
116 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
117 const char *ppsz_argv[] )
119 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
120 char * psz_modules = NULL;
121 char * psz_parser = NULL;
122 char * psz_control = NULL;
123 char *psz_val;
124 int i_ret = VLC_EGENERIC;
126 if (unlikely(vlc_LogPreinit(p_libvlc)))
127 return VLC_ENOMEM;
129 /* System specific initialization code */
130 system_Init();
132 /* Initialize the module bank and load the configuration of the
133 * core module. We need to do this at this stage to be able to display
134 * a short help if required by the user. (short help == core module
135 * options) */
136 module_InitBank ();
138 /* Get command line options that affect module loading. */
139 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
141 module_EndBank (false);
142 return VLC_EGENERIC;
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
150 * default values. */
151 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 */
160 else
161 config_LoadConfigFile( p_libvlc );
165 * Override configuration with command line settings
167 int vlc_optind;
168 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
169 goto error;
171 vlc_LogInit(p_libvlc);
174 * Support for gettext
176 #if defined( ENABLE_NLS ) \
177 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
178 vlc_bindtextdomain (PACKAGE_NAME);
179 #endif
180 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
181 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
183 if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
185 libvlc_InternalCleanup (p_libvlc);
186 exit(0);
189 #ifdef HAVE_DAEMON
190 /* Check for daemon mode */
191 if( var_InheritBool( p_libvlc, "daemon" ) )
193 if( daemon( 1, 0) != 0 )
195 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
196 goto error;
199 /* lets check if we need to write the pidfile */
200 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
201 if( pidfile != NULL )
203 FILE *stream = vlc_fopen( pidfile, "w" );
204 if( stream != NULL )
206 fprintf( stream, "%d", (int)getpid() );
207 fclose( stream );
208 msg_Dbg( p_libvlc, "written PID file %s", pidfile );
210 else
211 msg_Err( p_libvlc, "cannot write PID file %s: %s",
212 pidfile, vlc_strerror_c(errno) );
213 free( pidfile );
216 #endif
218 i_ret = VLC_ENOMEM;
220 if( libvlc_InternalDialogInit( p_libvlc ) != VLC_SUCCESS )
221 goto error;
222 if( libvlc_InternalKeystoreInit( p_libvlc ) != VLC_SUCCESS )
223 msg_Warn( p_libvlc, "memory keystore init failed" );
225 vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
227 if( var_InheritBool( p_libvlc, "media-library") )
229 priv->p_media_library = libvlc_MlCreate( p_libvlc );
230 if ( priv->p_media_library == NULL )
231 msg_Warn( p_libvlc, "Media library initialization failed" );
234 priv->p_thumbnailer = vlc_thumbnailer_Create( VLC_OBJECT( p_libvlc ) );
235 if ( priv->p_thumbnailer == NULL )
236 msg_Warn( p_libvlc, "Failed to instantiate VLC thumbnailer" );
239 * Initialize hotkey handling
241 if( libvlc_InternalActionsInit( p_libvlc ) != VLC_SUCCESS )
242 goto error;
245 * Meta data handling
247 priv->parser = input_preparser_New(VLC_OBJECT(p_libvlc));
248 if( !priv->parser )
249 goto error;
251 priv->media_source_provider = vlc_media_source_provider_New( VLC_OBJECT( p_libvlc ) );
252 if( !priv->media_source_provider )
253 goto error;
255 /* variables for signalling creation of new files */
256 var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
257 var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
259 /* some default internal settings */
260 var_Create( p_libvlc, "window", VLC_VAR_STRING );
261 /* NOTE: Because the playlist and interfaces start before this function
262 * returns control to the application (DESIGN BUG!), all these variables
263 * must be created (in place of libvlc_new()) and set to VLC defaults
264 * (in place of VLC main()) *here*. */
265 var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
266 var_SetString( p_libvlc, "user-agent",
267 "VLC media player (LibVLC "VERSION")" );
268 var_Create( p_libvlc, "http-user-agent", VLC_VAR_STRING );
269 var_SetString( p_libvlc, "http-user-agent",
270 "VLC/"PACKAGE_VERSION" LibVLC/"PACKAGE_VERSION );
271 var_Create( p_libvlc, "app-icon-name", VLC_VAR_STRING );
272 var_SetString( p_libvlc, "app-icon-name", PACKAGE_NAME );
273 var_Create( p_libvlc, "app-id", VLC_VAR_STRING );
274 var_SetString( p_libvlc, "app-id", "org.VideoLAN.VLC" );
275 var_Create( p_libvlc, "app-version", VLC_VAR_STRING );
276 var_SetString( p_libvlc, "app-version", PACKAGE_VERSION );
278 /* System specific configuration */
279 system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
281 #ifdef ENABLE_VLM
282 /* Initialize VLM if vlm-conf is specified */
283 psz_parser = var_InheritString( p_libvlc, "vlm-conf" );
284 if( psz_parser )
286 priv->p_vlm = vlm_New( p_libvlc, psz_parser );
287 if( !priv->p_vlm )
288 msg_Err( p_libvlc, "VLM initialization failed" );
289 free( psz_parser );
291 #endif
294 * Load background interfaces
296 psz_modules = var_InheritString( p_libvlc, "extraintf" );
297 psz_control = var_InheritString( p_libvlc, "control" );
299 if( psz_modules && psz_control )
301 char* psz_tmp;
302 if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
304 free( psz_modules );
305 psz_modules = psz_tmp;
308 else if( psz_control )
310 free( psz_modules );
311 psz_modules = strdup( psz_control );
314 psz_parser = psz_modules;
315 while ( psz_parser && *psz_parser )
317 char *psz_module, *psz_temp;
318 psz_module = psz_parser;
319 psz_parser = strchr( psz_module, ':' );
320 if ( psz_parser )
322 *psz_parser = '\0';
323 psz_parser++;
325 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
327 libvlc_InternalAddIntf( p_libvlc, psz_temp );
328 free( psz_temp );
331 free( psz_modules );
332 free( psz_control );
334 if( var_InheritBool( p_libvlc, "network-synchronisation") )
335 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
337 #ifdef __APPLE__
338 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
339 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
340 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
341 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
342 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
343 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
344 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
345 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
346 var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
347 #endif
350 * Get input filenames given as commandline arguments.
351 * We assume that the remaining parameters are filenames
352 * and their input options.
354 GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
357 * Get --open argument
359 psz_val = var_InheritString( p_libvlc, "open" );
360 if ( psz_val != NULL )
362 intf_InsertItem( p_libvlc, psz_val, 0, NULL, 0 );
363 free( psz_val );
366 return VLC_SUCCESS;
368 error:
369 libvlc_InternalCleanup( p_libvlc );
370 return i_ret;
374 * Cleanup a libvlc instance. The instance is not completely deallocated
375 * \param p_libvlc the instance to clean
377 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
379 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
381 if (priv->parser != NULL)
382 input_preparser_Deactivate(priv->parser);
384 /* Ask the interfaces to stop and destroy them */
385 msg_Dbg( p_libvlc, "removing all interfaces" );
386 intf_DestroyAll( p_libvlc );
388 if ( priv->p_thumbnailer )
389 vlc_thumbnailer_Release( priv->p_thumbnailer );
391 if ( priv->p_media_library )
392 libvlc_MlRelease( priv->p_media_library );
394 if( priv->media_source_provider )
395 vlc_media_source_provider_Delete( priv->media_source_provider );
397 libvlc_InternalDialogClean( p_libvlc );
398 libvlc_InternalKeystoreClean( p_libvlc );
400 #ifdef ENABLE_VLM
401 /* Destroy VLM if created in libvlc_InternalInit */
402 if( priv->p_vlm )
404 vlm_Delete( priv->p_vlm );
406 #endif
408 #if !defined( _WIN32 ) && !defined( __OS2__ )
409 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
410 if( pidfile != NULL )
412 msg_Dbg( p_libvlc, "removing PID file %s", pidfile );
413 if( unlink( pidfile ) )
414 msg_Warn( p_libvlc, "cannot remove PID file %s: %s",
415 pidfile, vlc_strerror_c(errno) );
416 free( pidfile );
418 #endif
420 if (priv->parser != NULL)
421 input_preparser_Delete(priv->parser);
423 if (priv->main_playlist)
424 vlc_playlist_Delete(priv->main_playlist);
426 libvlc_InternalActionsClean( p_libvlc );
428 /* Save the configuration */
429 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
430 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
432 vlc_LogDestroy(p_libvlc->obj.logger);
433 /* Free module bank. It is refcounted, so we call this each time */
434 module_EndBank (true);
435 #if defined(_WIN32) || defined(__OS2__)
436 system_End( );
437 #endif
441 * Destroy everything.
442 * This function requests the running threads to finish, waits for their
443 * termination, and destroys their structure.
444 * It stops the thread systems: no instance can run after this has run
445 * \param p_libvlc the instance to destroy
447 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
449 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
451 vlc_ExitDestroy( &priv->exit );
453 assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
454 vlc_object_delete(p_libvlc);
457 /*****************************************************************************
458 * GetFilenames: parse command line options which are not flags
459 *****************************************************************************
460 * Parse command line for input files as well as their associated options.
461 * An option always follows its associated input and begins with a ":".
462 *****************************************************************************/
463 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
464 const char *const args[] )
466 while( n > 0 )
468 /* Count the input options */
469 unsigned i_options = 0;
471 while( args[--n][0] == ':' )
473 i_options++;
474 if( n == 0 )
476 msg_Warn( p_vlc, "options %s without item", args[n] );
477 return; /* syntax!? */
481 char *mrl = NULL;
482 if( strstr( args[n], "://" ) == NULL )
484 mrl = vlc_path2uri( args[n], NULL );
485 if( !mrl )
486 continue;
489 intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
490 ( i_options ? &args[n + 1] : NULL ),
491 VLC_INPUT_OPTION_TRUSTED );
492 free( mrl );
496 int vlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
497 input_item_meta_request_option_t i_options,
498 const input_preparser_callbacks_t *cbs,
499 void *cbs_userdata,
500 int timeout, void *id)
502 libvlc_priv_t *priv = libvlc_priv(libvlc);
504 if (unlikely(priv->parser == NULL))
505 return VLC_ENOMEM;
507 if( i_options & META_REQUEST_OPTION_DO_INTERACT )
509 vlc_mutex_lock( &item->lock );
510 item->b_preparse_interact = true;
511 vlc_mutex_unlock( &item->lock );
513 input_preparser_Push( priv->parser, item, i_options, cbs, cbs_userdata, timeout, id );
514 return VLC_SUCCESS;
519 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
520 * The actual extraction is asynchronous. It can be cancelled with
521 * libvlc_MetadataCancel()
523 int libvlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
524 input_item_meta_request_option_t i_options,
525 const input_preparser_callbacks_t *cbs,
526 void *cbs_userdata,
527 int timeout, void *id)
529 libvlc_priv_t *priv = libvlc_priv(libvlc);
531 if (unlikely(priv->parser == NULL))
532 return VLC_ENOMEM;
534 vlc_mutex_lock( &item->lock );
535 if( item->i_preparse_depth == 0 )
536 item->i_preparse_depth = 1;
537 vlc_mutex_unlock( &item->lock );
539 return vlc_MetadataRequest(libvlc, item, i_options, cbs, cbs_userdata, timeout, id);
543 * Requests retrieving/downloading art for an input item.
544 * The retrieval is performed asynchronously.
546 int libvlc_ArtRequest(libvlc_int_t *libvlc, input_item_t *item,
547 input_item_meta_request_option_t i_options,
548 const input_fetcher_callbacks_t *cbs,
549 void *cbs_userdata)
551 libvlc_priv_t *priv = libvlc_priv(libvlc);
553 if (unlikely(priv->parser == NULL))
554 return VLC_ENOMEM;
556 input_preparser_fetcher_Push(priv->parser, item, i_options, cbs, cbs_userdata);
557 return VLC_SUCCESS;
561 * Cancels extraction of the meta data for an input item.
563 * This does nothing if the input item is already processed or if it was not
564 * added with libvlc_MetadataRequest()
566 void libvlc_MetadataCancel(libvlc_int_t *libvlc, void *id)
568 libvlc_priv_t *priv = libvlc_priv(libvlc);
570 if (unlikely(priv->parser == NULL))
571 return;
573 input_preparser_Cancel(priv->parser, id);