core: libvlc_MetadataRequest: use vlc_MetadataRequest
[vlc.git] / src / libvlc.c
blob1f4fed6107495bf49e7015c0540cee1dc3191c36
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>
65 #include <vlc_media_library.h>
67 #include "libvlc.h"
68 #include "playlist/playlist_internal.h"
69 #include "misc/variables.h"
71 #include <vlc_vlm.h>
73 #include <assert.h>
75 /*****************************************************************************
76 * Local prototypes
77 *****************************************************************************/
78 static void GetFilenames ( libvlc_int_t *, unsigned, const char *const [] );
80 /**
81 * Allocate a blank libvlc instance, also setting the exit handler.
82 * Vlc's threading system must have been initialized first
84 libvlc_int_t * libvlc_InternalCreate( void )
86 libvlc_int_t *p_libvlc;
87 libvlc_priv_t *priv;
89 /* Allocate a libvlc instance object */
90 p_libvlc = (vlc_custom_create)( NULL, sizeof (*priv), "libvlc" );
91 if( p_libvlc == NULL )
92 return NULL;
94 priv = libvlc_priv (p_libvlc);
95 priv->playlist = NULL;
96 priv->p_vlm = NULL;
98 vlc_ExitInit( &priv->exit );
100 return p_libvlc;
104 * Initialize a libvlc instance
105 * This function initializes a previously allocated libvlc instance:
106 * - CPU detection
107 * - gettext initialization
108 * - message queue, module bank and playlist initialization
109 * - configuration and commandline parsing
111 int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
112 const char *ppsz_argv[] )
114 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
115 char * psz_modules = NULL;
116 char * psz_parser = NULL;
117 char * psz_control = NULL;
118 char *psz_val;
119 int i_ret = VLC_EGENERIC;
121 /* System specific initialization code */
122 system_Init();
124 vlc_LogPreinit(p_libvlc);
126 /* Initialize the module bank and load the configuration of the
127 * core module. We need to do this at this stage to be able to display
128 * a short help if required by the user. (short help == core module
129 * options) */
130 module_InitBank ();
132 /* Get command line options that affect module loading. */
133 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, NULL ) )
135 module_EndBank (false);
136 return VLC_EGENERIC;
139 vlc_threads_setup (p_libvlc);
141 /* Load the builtins and plugins into the module_bank.
142 * We have to do it before config_Load*() because this also gets the
143 * list of configuration options exported by each module and loads their
144 * default values. */
145 module_LoadPlugins (p_libvlc);
148 * Override default configuration with config file settings
150 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
152 if( var_InheritBool( p_libvlc, "reset-config" ) )
153 config_SaveConfigFile( p_libvlc ); /* Save default config */
154 else
155 config_LoadConfigFile( p_libvlc );
159 * Override configuration with command line settings
161 int vlc_optind;
162 if( config_LoadCmdLine( p_libvlc, i_argc, ppsz_argv, &vlc_optind ) )
163 goto error;
165 vlc_LogInit(p_libvlc);
168 * Support for gettext
170 #if defined( ENABLE_NLS ) \
171 && ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
172 vlc_bindtextdomain (PACKAGE_NAME);
173 #endif
174 /*xgettext: Translate "C" to the language code: "fr", "en_GB", "nl", "ru"... */
175 msg_Dbg( p_libvlc, "translation test: code is \"%s\"", _("C") );
177 if (config_PrintHelp (VLC_OBJECT(p_libvlc)))
179 libvlc_InternalCleanup (p_libvlc);
180 exit(0);
183 #ifdef HAVE_DAEMON
184 /* Check for daemon mode */
185 if( var_InheritBool( p_libvlc, "daemon" ) )
187 if( daemon( 1, 0) != 0 )
189 msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
190 goto error;
193 /* lets check if we need to write the pidfile */
194 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
195 if( pidfile != NULL )
197 FILE *stream = vlc_fopen( pidfile, "w" );
198 if( stream != NULL )
200 fprintf( stream, "%d", (int)getpid() );
201 fclose( stream );
202 msg_Dbg( p_libvlc, "written PID file %s", pidfile );
204 else
205 msg_Err( p_libvlc, "cannot write PID file %s: %s",
206 pidfile, vlc_strerror_c(errno) );
207 free( pidfile );
210 #endif
212 i_ret = VLC_ENOMEM;
214 if( libvlc_InternalDialogInit( p_libvlc ) != VLC_SUCCESS )
215 goto error;
216 if( libvlc_InternalKeystoreInit( p_libvlc ) != VLC_SUCCESS )
217 msg_Warn( p_libvlc, "memory keystore init failed" );
219 vlc_CPU_dump( VLC_OBJECT(p_libvlc) );
221 if( var_InheritBool( p_libvlc, "media-library") )
223 priv->p_media_library = libvlc_MlCreate( p_libvlc );
224 if ( priv->p_media_library == NULL )
225 msg_Warn( p_libvlc, "Media library initialization failed" );
229 * Initialize hotkey handling
231 if( libvlc_InternalActionsInit( p_libvlc ) != VLC_SUCCESS )
232 goto error;
235 * Meta data handling
237 priv->parser = input_preparser_New(VLC_OBJECT(p_libvlc));
238 if( !priv->parser )
239 goto error;
241 /* variables for signalling creation of new files */
242 var_Create( p_libvlc, "snapshot-file", VLC_VAR_STRING );
243 var_Create( p_libvlc, "record-file", VLC_VAR_STRING );
245 /* some default internal settings */
246 var_Create( p_libvlc, "window", VLC_VAR_STRING );
247 /* NOTE: Because the playlist and interfaces start before this function
248 * returns control to the application (DESIGN BUG!), all these variables
249 * must be created (in place of libvlc_new()) and set to VLC defaults
250 * (in place of VLC main()) *here*. */
251 var_Create( p_libvlc, "user-agent", VLC_VAR_STRING );
252 var_SetString( p_libvlc, "user-agent",
253 "VLC media player (LibVLC "VERSION")" );
254 var_Create( p_libvlc, "http-user-agent", VLC_VAR_STRING );
255 var_SetString( p_libvlc, "http-user-agent",
256 "VLC/"PACKAGE_VERSION" LibVLC/"PACKAGE_VERSION );
257 var_Create( p_libvlc, "app-icon-name", VLC_VAR_STRING );
258 var_SetString( p_libvlc, "app-icon-name", PACKAGE_NAME );
259 var_Create( p_libvlc, "app-id", VLC_VAR_STRING );
260 var_SetString( p_libvlc, "app-id", "org.VideoLAN.VLC" );
261 var_Create( p_libvlc, "app-version", VLC_VAR_STRING );
262 var_SetString( p_libvlc, "app-version", PACKAGE_VERSION );
264 /* System specific configuration */
265 system_Configure( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
267 #ifdef ENABLE_VLM
268 /* Initialize VLM if vlm-conf is specified */
269 psz_parser = var_InheritString( p_libvlc, "vlm-conf" );
270 if( psz_parser )
272 priv->p_vlm = vlm_New( p_libvlc, psz_parser );
273 if( !priv->p_vlm )
274 msg_Err( p_libvlc, "VLM initialization failed" );
275 free( psz_parser );
277 #endif
280 * Load background interfaces
282 psz_modules = var_InheritString( p_libvlc, "extraintf" );
283 psz_control = var_InheritString( p_libvlc, "control" );
285 if( psz_modules && psz_control )
287 char* psz_tmp;
288 if( asprintf( &psz_tmp, "%s:%s", psz_modules, psz_control ) != -1 )
290 free( psz_modules );
291 psz_modules = psz_tmp;
294 else if( psz_control )
296 free( psz_modules );
297 psz_modules = strdup( psz_control );
300 psz_parser = psz_modules;
301 while ( psz_parser && *psz_parser )
303 char *psz_module, *psz_temp;
304 psz_module = psz_parser;
305 psz_parser = strchr( psz_module, ':' );
306 if ( psz_parser )
308 *psz_parser = '\0';
309 psz_parser++;
311 if( asprintf( &psz_temp, "%s,none", psz_module ) != -1)
313 libvlc_InternalAddIntf( p_libvlc, psz_temp );
314 free( psz_temp );
317 free( psz_modules );
318 free( psz_control );
320 if( var_InheritBool( p_libvlc, "network-synchronisation") )
321 libvlc_InternalAddIntf( p_libvlc, "netsync,none" );
323 #ifdef __APPLE__
324 var_Create( p_libvlc, "drawable-view-top", VLC_VAR_INTEGER );
325 var_Create( p_libvlc, "drawable-view-left", VLC_VAR_INTEGER );
326 var_Create( p_libvlc, "drawable-view-bottom", VLC_VAR_INTEGER );
327 var_Create( p_libvlc, "drawable-view-right", VLC_VAR_INTEGER );
328 var_Create( p_libvlc, "drawable-clip-top", VLC_VAR_INTEGER );
329 var_Create( p_libvlc, "drawable-clip-left", VLC_VAR_INTEGER );
330 var_Create( p_libvlc, "drawable-clip-bottom", VLC_VAR_INTEGER );
331 var_Create( p_libvlc, "drawable-clip-right", VLC_VAR_INTEGER );
332 var_Create( p_libvlc, "drawable-nsobject", VLC_VAR_ADDRESS );
333 #endif
336 * Get input filenames given as commandline arguments.
337 * We assume that the remaining parameters are filenames
338 * and their input options.
340 GetFilenames( p_libvlc, i_argc - vlc_optind, ppsz_argv + vlc_optind );
343 * Get --open argument
345 psz_val = var_InheritString( p_libvlc, "open" );
346 if ( psz_val != NULL )
348 intf_InsertItem( p_libvlc, psz_val, 0, NULL, 0 );
349 free( psz_val );
352 return VLC_SUCCESS;
354 error:
355 libvlc_InternalCleanup( p_libvlc );
356 return i_ret;
360 * Cleanup a libvlc instance. The instance is not completely deallocated
361 * \param p_libvlc the instance to clean
363 void libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
365 libvlc_priv_t *priv = libvlc_priv (p_libvlc);
367 if (priv->parser != NULL)
368 input_preparser_Deactivate(priv->parser);
370 /* Ask the interfaces to stop and destroy them */
371 msg_Dbg( p_libvlc, "removing all interfaces" );
372 intf_DestroyAll( p_libvlc );
374 if ( priv->p_media_library )
375 libvlc_MlRelease( priv->p_media_library );
377 libvlc_InternalDialogClean( p_libvlc );
378 libvlc_InternalKeystoreClean( p_libvlc );
380 #ifdef ENABLE_VLM
381 /* Destroy VLM if created in libvlc_InternalInit */
382 if( priv->p_vlm )
384 vlm_Delete( priv->p_vlm );
386 #endif
388 #if !defined( _WIN32 ) && !defined( __OS2__ )
389 char *pidfile = var_InheritString( p_libvlc, "pidfile" );
390 if( pidfile != NULL )
392 msg_Dbg( p_libvlc, "removing PID file %s", pidfile );
393 if( unlink( pidfile ) )
394 msg_Warn( p_libvlc, "cannot remove PID file %s: %s",
395 pidfile, vlc_strerror_c(errno) );
396 free( pidfile );
398 #endif
400 if (priv->parser != NULL)
401 input_preparser_Delete(priv->parser);
403 libvlc_InternalActionsClean( p_libvlc );
405 /* Save the configuration */
406 if( !var_InheritBool( p_libvlc, "ignore-config" ) )
407 config_AutoSaveConfigFile( VLC_OBJECT(p_libvlc) );
409 /* Free module bank. It is refcounted, so we call this each time */
410 vlc_LogDeinit (p_libvlc);
411 module_EndBank (true);
412 #if defined(_WIN32) || defined(__OS2__)
413 system_End( );
414 #endif
418 * Destroy everything.
419 * This function requests the running threads to finish, waits for their
420 * termination, and destroys their structure.
421 * It stops the thread systems: no instance can run after this has run
422 * \param p_libvlc the instance to destroy
424 void libvlc_InternalDestroy( libvlc_int_t *p_libvlc )
426 libvlc_priv_t *priv = libvlc_priv( p_libvlc );
428 vlc_ExitDestroy( &priv->exit );
430 assert( atomic_load(&(vlc_internals(p_libvlc)->refs)) == 1 );
431 vlc_object_release( p_libvlc );
434 /*****************************************************************************
435 * GetFilenames: parse command line options which are not flags
436 *****************************************************************************
437 * Parse command line for input files as well as their associated options.
438 * An option always follows its associated input and begins with a ":".
439 *****************************************************************************/
440 static void GetFilenames( libvlc_int_t *p_vlc, unsigned n,
441 const char *const args[] )
443 while( n > 0 )
445 /* Count the input options */
446 unsigned i_options = 0;
448 while( args[--n][0] == ':' )
450 i_options++;
451 if( n == 0 )
453 msg_Warn( p_vlc, "options %s without item", args[n] );
454 return; /* syntax!? */
458 char *mrl = NULL;
459 if( strstr( args[n], "://" ) == NULL )
461 mrl = vlc_path2uri( args[n], NULL );
462 if( !mrl )
463 continue;
466 intf_InsertItem( p_vlc, (mrl != NULL) ? mrl : args[n], i_options,
467 ( i_options ? &args[n + 1] : NULL ),
468 VLC_INPUT_OPTION_TRUSTED );
469 free( mrl );
473 int vlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
474 input_item_meta_request_option_t i_options,
475 int timeout, void *id)
477 libvlc_priv_t *priv = libvlc_priv(libvlc);
479 if (unlikely(priv->parser == NULL))
480 return VLC_ENOMEM;
482 if( i_options & META_REQUEST_OPTION_DO_INTERACT )
484 vlc_mutex_lock( &item->lock );
485 item->b_preparse_interact = true;
486 vlc_mutex_unlock( &item->lock );
488 input_preparser_Push( priv->parser, item, i_options, timeout, id );
489 return VLC_SUCCESS;
494 * Requests extraction of the meta data for an input item (a.k.a. preparsing).
495 * The actual extraction is asynchronous. It can be cancelled with
496 * libvlc_MetadataCancel()
498 int libvlc_MetadataRequest(libvlc_int_t *libvlc, input_item_t *item,
499 input_item_meta_request_option_t i_options,
500 int timeout, void *id)
502 libvlc_priv_t *priv = libvlc_priv(libvlc);
504 if (unlikely(priv->parser == NULL))
505 return VLC_ENOMEM;
507 vlc_mutex_lock( &item->lock );
508 if( item->i_preparse_depth == 0 )
509 item->i_preparse_depth = 1;
510 vlc_mutex_unlock( &item->lock );
512 return vlc_MetadataRequest(libvlc, item, i_options, timeout, id);
516 * Requests retrieving/downloading art for an input item.
517 * The retrieval is performed asynchronously.
519 int libvlc_ArtRequest(libvlc_int_t *libvlc, input_item_t *item,
520 input_item_meta_request_option_t i_options)
522 libvlc_priv_t *priv = libvlc_priv(libvlc);
524 if (unlikely(priv->parser == NULL))
525 return VLC_ENOMEM;
527 input_preparser_fetcher_Push(priv->parser, item, i_options);
528 return VLC_SUCCESS;
532 * Cancels extraction of the meta data for an input item.
534 * This does nothing if the input item is already processed or if it was not
535 * added with libvlc_MetadataRequest()
537 void libvlc_MetadataCancel(libvlc_int_t *libvlc, void *id)
539 libvlc_priv_t *priv = libvlc_priv(libvlc);
541 if (unlikely(priv->parser == NULL))
542 return;
544 input_preparser_Cancel(priv->parser, id);