Dshow: relicense to LGPL
[vlc.git] / src / libvlc.c
blob169bc37a7de330137267ace3893d83dfaf328132
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 "preparser/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 #include <vlc_playlist.h>
55 #include <vlc_interface.h>
57 #include <vlc_actions.h>
58 #include <vlc_charset.h>
59 #include <vlc_dialog.h>
60 #include <vlc_keystore.h>
61 #include <vlc_fs.h>
62 #include <vlc_cpu.h>
63 #include <vlc_url.h>
64 #include <vlc_modules.h>
66 #include "libvlc.h"
67 #include "playlist/playlist_internal.h"
68 #include "misc/variables.h"
70 #include <vlc_vlm.h>
72 #include <assert.h>
74 /*****************************************************************************
75 * Local prototypes
76 *****************************************************************************/
77 static void GetFilenames ( libvlc_int_t *, unsigned, const char *const [] );
79 /**
80 * Allocate a blank libvlc instance, also setting the exit handler.
81 * Vlc's threading system must have been initialized first
83 libvlc_int_t * libvlc_InternalCreate( void )
85 libvlc_int_t *p_libvlc;
86 libvlc_priv_t *priv;
88 /* Allocate a libvlc instance object */
89 p_libvlc = (vlc_custom_create)( NULL, sizeof (*priv), "libvlc" );
90 if( p_libvlc == NULL )
91 return NULL;
93 priv = libvlc_priv (p_libvlc);
94 priv->playlist = NULL;
95 priv->p_vlm = NULL;
97 vlc_ExitInit( &priv->exit );
99 return p_libvlc;
103 * Initialize a libvlc instance
104 * This function initializes a previously allocated libvlc instance:
105 * - CPU detection
106 * - gettext initialization
107 * - message queue, module bank and playlist initialization
108 * - configuration and commandline parsing
110 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
111 const char *ppsz_argv[] )
113 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
114 char * psz_modules = NULL;
115 char * psz_parser = NULL;
116 char * psz_control = NULL;
117 char *psz_val;
118 int i_ret = VLC_EGENERIC;
120 /* System specific initialization code */
121 system_Init();
123 vlc_LogPreinit(p_libvlc);
125 /* Initialize the module bank and load the configuration of the
126 * core module. We need to do this at this stage to be able to display
127 * a short help if required by the user. (short help == core module
128 * options) */
129 module_InitBank ();
131 /* Get command line options that affect module loading. */
132 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
134 module_EndBank (false);
135 return VLC_EGENERIC;
138 vlc_threads_setup (p_libvlc);
140 /* Load the builtins and plugins into the module_bank.
141 * We have to do it before config_Load*() because this also gets the
142 * list of configuration options exported by each module and loads their
143 * default values. */
144 module_LoadPlugins (p_libvlc);
147 * Override default configuration with config file settings
149 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
151 if( var_InheritBool( p_libvlc, "reset-config" ) )
152 config_SaveConfigFile( p_libvlc ); /* Save default config */
153 else
154 config_LoadConfigFile( p_libvlc );
158 * Override configuration with command line settings
160 int vlc_optind;
161 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
162 goto error;
164 vlc_LogInit(p_libvlc);
167 * Support for gettext
169 #if defined( ENABLE_NLS ) \
170 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
171 vlc_bindtextdomain (PACKAGE_NAME);
172 #endif
173 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
174 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
176 if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
178 libvlc_InternalCleanup (p_libvlc);
179 exit(0);
182 #ifdef HAVE_DAEMON
183 /* Check for daemon mode */
184 if( var_InheritBool( p_libvlc, "daemon" ) )
186 if( daemon( 1, 0) != 0 )
188 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
189 goto error;
192 /* lets check if we need to write the pidfile */
193 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
194 if( pidfile != NULL )
196 FILE *stream = vlc_fopen( pidfile, "w" );
197 if( stream != NULL )
199 fprintf( stream, "%d", (int)getpid() );
200 fclose( stream );
201 msg_Dbg( p_libvlc, "written PID file %s", pidfile );
203 else
204 msg_Err( p_libvlc, "cannot write PID file %s: %s",
205 pidfile, vlc_strerror_c(errno) );
206 free( pidfile );
209 #endif
211 i_ret = VLC_ENOMEM;
213 if( libvlc_InternalDialogInit( p_libvlc ) != VLC_SUCCESS )
214 goto error;
215 if( libvlc_InternalKeystoreInit( p_libvlc ) != VLC_SUCCESS )
216 msg_Warn( p_libvlc, "memory keystore init failed" );
218 vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
221 * Initialize hotkey handling
223 if( libvlc_InternalActionsInit( p_libvlc ) != VLC_SUCCESS )
224 goto error;
227 * Meta data handling
229 priv->parser = input_preparser_New(VLC_OBJECT(p_libvlc));
230 if( !priv->parser )
231 goto error;
233 /* variables for signalling creation of new files */
234 var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
235 var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
237 /* some default internal settings */
238 var_Create( p_libvlc, "window", VLC_VAR_STRING );
239 /* NOTE: Because the playlist and interfaces start before this function
240 * returns control to the application (DESIGN BUG!), all these variables
241 * must be created (in place of libvlc_new()) and set to VLC defaults
242 * (in place of VLC main()) *here*. */
243 var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
244 var_SetString( p_libvlc, "user-agent",
245 "VLC media player (LibVLC "VERSION")" );
246 var_Create( p_libvlc, "http-user-agent", VLC_VAR_STRING );
247 var_SetString( p_libvlc, "http-user-agent",
248 "VLC/"PACKAGE_VERSION" LibVLC/"PACKAGE_VERSION );
249 var_Create( p_libvlc, "app-icon-name", VLC_VAR_STRING );
250 var_SetString( p_libvlc, "app-icon-name", PACKAGE_NAME );
251 var_Create( p_libvlc, "app-id", VLC_VAR_STRING );
252 var_SetString( p_libvlc, "app-id", "org.VideoLAN.VLC" );
253 var_Create( p_libvlc, "app-version", VLC_VAR_STRING );
254 var_SetString( p_libvlc, "app-version", PACKAGE_VERSION );
256 /* System specific configuration */
257 system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
259 #ifdef ENABLE_VLM
260 /* Initialize VLM if vlm-conf is specified */
261 psz_parser = var_InheritString( p_libvlc, "vlm-conf" );
262 if( psz_parser )
264 priv->p_vlm = vlm_New( p_libvlc, psz_parser );
265 if( !priv->p_vlm )
266 msg_Err( p_libvlc, "VLM initialization failed" );
267 free( psz_parser );
269 #endif
272 * Load background interfaces
274 psz_modules = var_InheritString( p_libvlc, "extraintf" );
275 psz_control = var_InheritString( p_libvlc, "control" );
277 if( psz_modules && psz_control )
279 char* psz_tmp;
280 if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
282 free( psz_modules );
283 psz_modules = psz_tmp;
286 else if( psz_control )
288 free( psz_modules );
289 psz_modules = strdup( psz_control );
292 psz_parser = psz_modules;
293 while ( psz_parser && *psz_parser )
295 char *psz_module, *psz_temp;
296 psz_module = psz_parser;
297 psz_parser = strchr( psz_module, ':' );
298 if ( psz_parser )
300 *psz_parser = '\0';
301 psz_parser++;
303 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
305 libvlc_InternalAddIntf( p_libvlc, psz_temp );
306 free( psz_temp );
309 free( psz_modules );
310 free( psz_control );
312 if( var_InheritBool( p_libvlc, "network-synchronisation") )
313 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
315 #ifdef __APPLE__
316 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
317 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
318 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
319 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
320 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
321 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
322 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
323 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
324 var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
325 #endif
328 * Get input filenames given as commandline arguments.
329 * We assume that the remaining parameters are filenames
330 * and their input options.
332 GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
335 * Get --open argument
337 psz_val = var_InheritString( p_libvlc, "open" );
338 if ( psz_val != NULL )
340 intf_InsertItem( p_libvlc, psz_val, 0, NULL, 0 );
341 free( psz_val );
344 return VLC_SUCCESS;
346 error:
347 libvlc_InternalCleanup( p_libvlc );
348 return i_ret;
352 * Cleanup a libvlc instance. The instance is not completely deallocated
353 * \param p_libvlc the instance to clean
355 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
357 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
359 if (priv->parser != NULL)
360 input_preparser_Deactivate(priv->parser);
362 /* Ask the interfaces to stop and destroy them */
363 msg_Dbg( p_libvlc, "removing all interfaces" );
364 intf_DestroyAll( p_libvlc );
366 libvlc_InternalDialogClean( p_libvlc );
367 libvlc_InternalKeystoreClean( p_libvlc );
369 #ifdef ENABLE_VLM
370 /* Destroy VLM if created in libvlc_InternalInit */
371 if( priv->p_vlm )
373 vlm_Delete( priv->p_vlm );
375 #endif
377 #if !defined( _WIN32 ) && !defined( __OS2__ )
378 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
379 if( pidfile != NULL )
381 msg_Dbg( p_libvlc, "removing PID file %s", pidfile );
382 if( unlink( pidfile ) )
383 msg_Warn( p_libvlc, "cannot remove PID file %s: %s",
384 pidfile, vlc_strerror_c(errno) );
385 free( pidfile );
387 #endif
389 if (priv->parser != NULL)
390 input_preparser_Delete(priv->parser);
392 libvlc_InternalActionsClean( p_libvlc );
394 /* Save the configuration */
395 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
396 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
398 /* Free module bank. It is refcounted, so we call this each time */
399 vlc_LogDeinit (p_libvlc);
400 module_EndBank (true);
401 #if defined(_WIN32) || defined(__OS2__)
402 system_End( );
403 #endif
407 * Destroy everything.
408 * This function requests the running threads to finish, waits for their
409 * termination, and destroys their structure.
410 * It stops the thread systems: no instance can run after this has run
411 * \param p_libvlc the instance to destroy
413 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
415 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
417 vlc_ExitDestroy( &priv->exit );
419 assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
420 vlc_object_release( p_libvlc );
423 /*****************************************************************************
424 * GetFilenames: parse command line options which are not flags
425 *****************************************************************************
426 * Parse command line for input files as well as their associated options.
427 * An option always follows its associated input and begins with a ":".
428 *****************************************************************************/
429 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
430 const char *const args[] )
432 while( n > 0 )
434 /* Count the input options */
435 unsigned i_options = 0;
437 while( args[--n][0] == ':' )
439 i_options++;
440 if( n == 0 )
442 msg_Warn( p_vlc, "options %s without item", args[n] );
443 return; /* syntax!? */
447 char *mrl = NULL;
448 if( strstr( args[n], "://" ) == NULL )
450 mrl = vlc_path2uri( args[n], NULL );
451 if( !mrl )
452 continue;
455 intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
456 ( i_options ? &args[n + 1] : NULL ),
457 VLC_INPUT_OPTION_TRUSTED );
458 free( mrl );
463 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
464 * The actual extraction is asynchronous. It can be cancelled with
465 * libvlc_MetadataCancel()
467 int libvlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
468 input_item_meta_request_option_t i_options,
469 int timeout, void *id)
471 libvlc_priv_t *priv = libvlc_priv(libvlc);
473 if (unlikely(priv->parser == NULL))
474 return VLC_ENOMEM;
476 vlc_mutex_lock( &item->lock );
477 if( item->i_preparse_depth == 0 )
478 item->i_preparse_depth = 1;
479 if( i_options & META_REQUEST_OPTION_DO_INTERACT )
480 item->b_preparse_interact = true;
481 vlc_mutex_unlock( &item->lock );
482 input_preparser_Push( priv->parser, item, i_options, timeout, id );
483 return VLC_SUCCESS;
487 * Requests retrieving/downloading art for an input item.
488 * The retrieval is performed asynchronously.
490 int libvlc_ArtRequest(libvlc_int_t *libvlc, input_item_t *item,
491 input_item_meta_request_option_t i_options)
493 libvlc_priv_t *priv = libvlc_priv(libvlc);
495 if (unlikely(priv->parser == NULL))
496 return VLC_ENOMEM;
498 input_preparser_fetcher_Push(priv->parser, item, i_options);
499 return VLC_SUCCESS;
503 * Cancels extraction of the meta data for an input item.
505 * This does nothing if the input item is already processed or if it was not
506 * added with libvlc_MetadataRequest()
508 void libvlc_MetadataCancel(libvlc_int_t *libvlc, void *id)
510 libvlc_priv_t *priv = libvlc_priv(libvlc);
512 if (unlikely(priv->parser == NULL))
513 return;
515 input_preparser_Cancel(priv->parser, id);