1 /*****************************************************************************
2 * libvlc.c: Implementation of the old libvlc API
3 *****************************************************************************
4 * Copyright (C) 1998-2007 the VideoLAN team
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
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 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 General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 /*****************************************************************************
29 * Pretend we are a builtin module
30 *****************************************************************************/
31 #define MODULE_NAME main
32 #define MODULE_PATH main
35 /*****************************************************************************
37 *****************************************************************************/
40 #include "control/libvlc_internal.h"
42 #include <vlc_playlist.h>
47 /*****************************************************************************
48 * VLC_Version: return the libvlc version.
49 *****************************************************************************
50 * This function returns full version string (numeric version and codename).
51 *****************************************************************************/
52 char const * VLC_Version( void )
54 return VERSION_MESSAGE;
57 /*****************************************************************************
58 * VLC_CompileBy, VLC_CompileHost, VLC_CompileDomain,
59 * VLC_Compiler, VLC_Changeset
60 *****************************************************************************/
61 #define DECLARE_VLC_VERSION( func, var ) \
62 char const * VLC_##func ( void ) \
67 DECLARE_VLC_VERSION( CompileBy, COMPILE_BY );
68 DECLARE_VLC_VERSION( CompileHost, COMPILE_HOST );
69 DECLARE_VLC_VERSION( CompileDomain, COMPILE_DOMAIN );
70 DECLARE_VLC_VERSION( Compiler, COMPILER );
72 extern const char psz_vlc_changeset[];
73 const char* VLC_Changeset( void )
75 return psz_vlc_changeset;
78 /*****************************************************************************
79 * VLC_Error: strerror() equivalent
80 *****************************************************************************
81 * This function returns full version string (numeric version and codename).
82 *****************************************************************************/
83 char const * VLC_Error( int i_err )
85 return vlc_error( i_err );
88 /*****************************************************************************
89 * VLC_Create: allocate a libvlc instance and intialize global libvlc stuff if needed
90 *****************************************************************************
91 * This function allocates a libvlc instance and returns a negative value
92 * in case of failure. Also, the thread system is initialized.
93 *****************************************************************************/
94 int VLC_Create( void )
96 libvlc_int_t *p_object = libvlc_InternalCreate();
97 if( p_object ) return p_object->i_object_id;
101 #define LIBVLC_FUNC \
102 libvlc_int_t * p_libvlc = vlc_current_object( i_object ); \
103 if( !p_libvlc ) return VLC_ENOOBJ;
104 #define LIBVLC_FUNC_END \
105 if( i_object ) vlc_object_release( p_libvlc );
108 /*****************************************************************************
109 * VLC_Init: initialize a libvlc instance
110 *****************************************************************************
111 * This function initializes a previously allocated libvlc instance:
113 * - gettext initialization
114 * - message queue, module bank and playlist initialization
115 * - configuration and commandline parsing
116 *****************************************************************************/
117 int VLC_Init( int i_object, int i_argc, char *ppsz_argv[] )
121 i_ret = libvlc_InternalInit( p_libvlc, i_argc, ppsz_argv );
126 /*****************************************************************************
127 * VLC_AddIntf: add an interface
128 *****************************************************************************
129 * This function opens an interface plugin and runs it. If b_block is set
130 * to 0, VLC_AddIntf will return immediately and let the interface run in a
131 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
132 * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
133 * the playlist when it is completely initialised.
134 *****************************************************************************/
135 int VLC_AddIntf( int i_object, char const *psz_module,
136 vlc_bool_t b_block, vlc_bool_t b_play )
140 i_ret = libvlc_InternalAddIntf( p_libvlc, psz_module, b_block, b_play,
147 /*****************************************************************************
148 * VLC_Die: ask vlc to die.
149 *****************************************************************************
150 * This function sets p_vlc->b_die to VLC_TRUE, but does not do any other
151 * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
152 *****************************************************************************/
153 int VLC_Die( int i_object )
156 vlc_object_kill( p_libvlc );
161 /*****************************************************************************
162 * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
163 *****************************************************************************/
164 int VLC_CleanUp( int i_object )
168 i_ret = libvlc_InternalCleanup( p_libvlc );
173 /*****************************************************************************
174 * VLC_Destroy: Destroy everything.
175 *****************************************************************************
176 * This function requests the running threads to finish, waits for their
177 * termination, and destroys their structure.
178 *****************************************************************************/
179 int VLC_Destroy( int i_object )
182 return libvlc_InternalDestroy( p_libvlc, i_object ? VLC_TRUE : VLC_FALSE );
185 /*****************************************************************************
186 * VLC_VariableSet: set a vlc variable
187 *****************************************************************************/
188 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
193 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
194 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
195 if( !strncmp( psz_var, "conf::", 6 ) )
197 module_config_t *p_item;
198 char const *psz_newvar = psz_var + 6;
200 p_item = config_FindConfig( VLC_OBJECT(p_libvlc), psz_newvar );
204 switch( p_item->i_type )
206 case CONFIG_ITEM_BOOL:
207 config_PutInt( p_libvlc, psz_newvar, value.b_bool );
209 case CONFIG_ITEM_INTEGER:
210 config_PutInt( p_libvlc, psz_newvar, value.i_int );
212 case CONFIG_ITEM_FLOAT:
213 config_PutFloat( p_libvlc, psz_newvar, value.f_float );
216 config_PutPsz( p_libvlc, psz_newvar, value.psz_string );
219 if( i_object ) vlc_object_release( p_libvlc );
224 i_ret = var_Set( p_libvlc, psz_var, value );
230 /*****************************************************************************
231 * VLC_VariableGet: get a vlc variable
232 *****************************************************************************/
233 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
237 i_ret = var_Get( p_libvlc , psz_var, p_value );
242 /*****************************************************************************
243 * VLC_VariableType: get a vlc variable type
244 *****************************************************************************/
245 int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
249 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
250 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
251 if( !strncmp( psz_var, "conf::", 6 ) )
253 module_config_t *p_item;
254 char const *psz_newvar = psz_var + 6;
256 p_item = config_FindConfig( VLC_OBJECT(p_libvlc), psz_newvar );
260 switch( p_item->i_type )
262 case CONFIG_ITEM_BOOL:
263 i_type = VLC_VAR_BOOL;
265 case CONFIG_ITEM_INTEGER:
266 i_type = VLC_VAR_INTEGER;
268 case CONFIG_ITEM_FLOAT:
269 i_type = VLC_VAR_FLOAT;
272 i_type = VLC_VAR_STRING;
280 i_type = VLC_VAR_TYPE & var_Type( p_libvlc , psz_var );
292 #define LIBVLC_PLAYLIST_FUNC \
293 libvlc_int_t *p_libvlc = vlc_current_object( i_object );\
294 if( !p_libvlc || !p_libvlc->p_playlist ) return VLC_ENOOBJ; \
295 vlc_object_yield( p_libvlc->p_playlist );
297 #define LIBVLC_PLAYLIST_FUNC_END \
298 vlc_object_release( p_libvlc->p_playlist ); \
299 if( i_object ) vlc_object_release( p_libvlc );
301 /*****************************************************************************
302 * VLC_AddTarget: adds a target for playing.
303 *****************************************************************************
304 * This function adds psz_target to the playlist
305 *****************************************************************************/
307 int VLC_AddTarget( int i_object, char const *psz_target,
308 char const **ppsz_options, int i_options,
309 int i_mode, int i_pos )
312 LIBVLC_PLAYLIST_FUNC;
313 i_err = playlist_AddExt( p_libvlc->p_playlist, psz_target,
314 NULL, i_mode, i_pos, -1,
315 ppsz_options, i_options, VLC_TRUE, VLC_FALSE );
316 LIBVLC_PLAYLIST_FUNC_END;
320 /*****************************************************************************
321 * VLC_Play: play the playlist
322 *****************************************************************************/
323 int VLC_Play( int i_object )
325 LIBVLC_PLAYLIST_FUNC;
326 playlist_Play( p_libvlc->p_playlist );
327 LIBVLC_PLAYLIST_FUNC_END;
331 /*****************************************************************************
332 * VLC_Pause: toggle pause
333 *****************************************************************************/
334 int VLC_Pause( int i_object )
336 LIBVLC_PLAYLIST_FUNC;
337 playlist_Pause( p_libvlc->p_playlist );
338 LIBVLC_PLAYLIST_FUNC_END;
342 /*****************************************************************************
343 * VLC_Stop: stop playback
344 *****************************************************************************/
345 int VLC_Stop( int i_object )
347 LIBVLC_PLAYLIST_FUNC;
348 playlist_Stop( p_libvlc->p_playlist );
349 LIBVLC_PLAYLIST_FUNC_END;
353 /*****************************************************************************
354 * VLC_IsPlaying: Query for Playlist Status
355 *****************************************************************************/
356 vlc_bool_t VLC_IsPlaying( int i_object )
358 vlc_bool_t b_playing;
360 LIBVLC_PLAYLIST_FUNC;
361 if( p_libvlc->p_playlist->p_input )
364 var_Get( p_libvlc->p_playlist->p_input, "state", &val );
365 b_playing = ( val.i_int == PLAYING_S );
369 b_playing = playlist_IsPlaying( p_libvlc->p_playlist );
371 LIBVLC_PLAYLIST_FUNC_END;
376 * Get the current position in a input
378 * Return the current position as a float
379 * \note For some inputs, this will be unknown.
381 * \param i_object a vlc object id
382 * \return a float in the range of 0.0 - 1.0
384 float VLC_PositionGet( int i_object )
386 input_thread_t *p_input;
390 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
394 if( i_object ) vlc_object_release( p_libvlc );
398 var_Get( p_input, "position", &val );
399 vlc_object_release( p_input );
406 * Set the current position in a input
408 * Set the current position in a input and then return
409 * the current position as a float.
410 * \note For some inputs, this will be unknown.
412 * \param i_object a vlc object id
413 * \param i_position a float in the range of 0.0 - 1.0
414 * \return a float in the range of 0.0 - 1.0
416 float VLC_PositionSet( int i_object, float i_position )
418 input_thread_t *p_input;
420 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
422 /* Check that the handle is valid */
428 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
432 if( i_object ) vlc_object_release( p_libvlc );
436 val.f_float = i_position;
437 var_Set( p_input, "position", val );
438 var_Get( p_input, "position", &val );
439 vlc_object_release( p_input );
441 if( i_object ) vlc_object_release( p_libvlc );
446 * Get the current position in a input
448 * Return the current position in seconds from the start.
449 * \note For some inputs, this will be unknown.
451 * \param i_object a vlc object id
452 * \return the offset from 0:00 in seconds
454 int VLC_TimeGet( int i_object )
456 input_thread_t *p_input;
458 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
460 /* Check that the handle is valid */
466 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
470 if( i_object ) vlc_object_release( p_libvlc );
474 var_Get( p_input, "time", &val );
475 vlc_object_release( p_input );
477 if( i_object ) vlc_object_release( p_libvlc );
478 return val.i_time / 1000000;
482 * Seek to a position in the current input
484 * Seek i_seconds in the current input. If b_relative is set,
485 * then the seek will be relative to the current position, otherwise
486 * it will seek to i_seconds from the beginning of the input.
487 * \note For some inputs, this will be unknown.
489 * \param i_object a vlc object id
490 * \param i_seconds seconds from current position or from beginning of input
491 * \param b_relative seek relative from current position
492 * \return VLC_SUCCESS on success
494 int VLC_TimeSet( int i_object, int i_seconds, vlc_bool_t b_relative )
496 input_thread_t *p_input;
498 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
500 /* Check that the handle is valid */
506 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
510 if( i_object ) vlc_object_release( p_libvlc );
516 val.i_time = i_seconds;
517 val.i_time = val.i_time * 1000000L;
518 var_Set( p_input, "time-offset", val );
522 val.i_time = i_seconds;
523 val.i_time = val.i_time * 1000000L;
524 var_Set( p_input, "time", val );
526 vlc_object_release( p_input );
528 if( i_object ) vlc_object_release( p_libvlc );
533 * Get the total length of a input
535 * Return the total length in seconds from the current input.
536 * \note For some inputs, this will be unknown.
538 * \param i_object a vlc object id
539 * \return the length in seconds
541 int VLC_LengthGet( int i_object )
543 input_thread_t *p_input;
545 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
547 /* Check that the handle is valid */
553 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
557 if( i_object ) vlc_object_release( p_libvlc );
561 var_Get( p_input, "length", &val );
562 vlc_object_release( p_input );
564 if( i_object ) vlc_object_release( p_libvlc );
565 return val.i_time / 1000000L;
569 * Play the input faster than realtime
571 * 2x, 4x, 8x faster than realtime
572 * \note For some inputs, this will be impossible.
574 * \param i_object a vlc object id
575 * \return the current speedrate
577 float VLC_SpeedFaster( int i_object )
579 input_thread_t *p_input;
581 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
583 /* Check that the handle is valid */
589 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
593 if( i_object ) vlc_object_release( p_libvlc );
597 val.b_bool = VLC_TRUE;
598 var_Set( p_input, "rate-faster", val );
599 var_Get( p_input, "rate", &val );
600 vlc_object_release( p_input );
602 if( i_object ) vlc_object_release( p_libvlc );
603 return val.f_float / INPUT_RATE_DEFAULT;
607 * Play the input slower than realtime
609 * 1/2x, 1/4x, 1/8x slower than realtime
610 * \note For some inputs, this will be impossible.
612 * \param i_object a vlc object id
613 * \return the current speedrate
615 float VLC_SpeedSlower( int i_object )
617 input_thread_t *p_input;
619 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
621 /* Check that the handle is valid */
627 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
631 if( i_object ) vlc_object_release( p_libvlc );
635 val.b_bool = VLC_TRUE;
636 var_Set( p_input, "rate-slower", val );
637 var_Get( p_input, "rate", &val );
638 vlc_object_release( p_input );
640 if( i_object ) vlc_object_release( p_libvlc );
641 return val.f_float / INPUT_RATE_DEFAULT;
645 * Return the current playlist item
647 * Returns the index of the playlistitem that is currently selected for play.
648 * This is valid even if nothing is currently playing.
650 * \param i_object a vlc object id
651 * \return the current index
653 int VLC_PlaylistIndex( int i_object )
656 printf( "This function is deprecated and should not be used anymore" );
661 * Total number of items in the playlist
663 * \param i_object a vlc object id
664 * \return amount of playlist items
666 int VLC_PlaylistNumberOfItems( int i_object )
669 LIBVLC_PLAYLIST_FUNC;
670 i_size = p_libvlc->p_playlist->items.i_size;
671 LIBVLC_PLAYLIST_FUNC_END;
676 * Go to next playlist item
677 * \param i_object a vlc object id
678 * \return VLC_SUCCESS on success
680 int VLC_PlaylistNext( int i_object )
682 LIBVLC_PLAYLIST_FUNC;
683 playlist_Next( p_libvlc->p_playlist );
684 LIBVLC_PLAYLIST_FUNC_END;
689 * Go to previous playlist item
690 * \param i_object a vlc object id
691 * \return VLC_SUCCESS on success
693 int VLC_PlaylistPrev( int i_object )
695 LIBVLC_PLAYLIST_FUNC;
696 playlist_Prev( p_libvlc->p_playlist );
697 LIBVLC_PLAYLIST_FUNC_END;
704 int VLC_PlaylistClear( int i_object )
706 LIBVLC_PLAYLIST_FUNC;
707 playlist_Clear( p_libvlc->p_playlist, VLC_TRUE );
708 LIBVLC_PLAYLIST_FUNC_END;
715 * \param i_object a vlc object id
716 * \param i_volume something in a range from 0-200
717 * \return the new volume (range 0-200 %)
719 int VLC_VolumeSet( int i_object, int i_volume )
721 audio_volume_t i_vol = 0;
724 if( i_volume >= 0 && i_volume <= 200 )
726 i_vol = i_volume * AOUT_VOLUME_MAX / 200;
727 aout_VolumeSet( p_libvlc, i_vol );
730 return i_vol * 200 / AOUT_VOLUME_MAX;
734 * Get the current volume
736 * Retrieve the current volume.
738 * \param i_object a vlc object id
739 * \return the current volume (range 0-200 %)
741 int VLC_VolumeGet( int i_object )
743 audio_volume_t i_volume;
745 aout_VolumeGet( p_libvlc, &i_volume );
747 return i_volume*200/AOUT_VOLUME_MAX;
751 * Mute/Unmute the volume
753 * \param i_object a vlc object id
754 * \return VLC_SUCCESS on success
756 int VLC_VolumeMute( int i_object )
759 aout_VolumeMute( p_libvlc, NULL );
764 /*****************************************************************************
765 * VLC_FullScreen: toggle fullscreen mode
766 *****************************************************************************/
767 int VLC_FullScreen( int i_object )
769 vout_thread_t *p_vout;
771 p_vout = vlc_object_find( p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD );
775 if( i_object ) vlc_object_release( p_libvlc );
779 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
780 vlc_object_release( p_vout );