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 *****************************************************************************/
44 #include "control/libvlc_internal.h"
47 #include <vlc_playlist.h>
52 /*****************************************************************************
53 * VLC_Version: return the libvlc version.
54 *****************************************************************************
55 * This function returns full version string (numeric version and codename).
56 *****************************************************************************/
57 char const * VLC_Version( void )
59 return VERSION_MESSAGE;
62 /*****************************************************************************
63 * VLC_CompileBy, VLC_CompileHost, VLC_CompileDomain,
64 * VLC_Compiler, VLC_Changeset
65 *****************************************************************************/
66 #define DECLARE_VLC_VERSION( func, var ) \
67 char const * VLC_##func ( void ) \
72 DECLARE_VLC_VERSION( CompileBy, COMPILE_BY );
73 DECLARE_VLC_VERSION( CompileHost, COMPILE_HOST );
74 DECLARE_VLC_VERSION( CompileDomain, COMPILE_DOMAIN );
75 DECLARE_VLC_VERSION( Compiler, COMPILER );
77 extern const char psz_vlc_changeset[];
78 const char* VLC_Changeset( void )
80 return psz_vlc_changeset;
83 /*****************************************************************************
84 * VLC_Error: strerror() equivalent
85 *****************************************************************************
86 * This function returns full version string (numeric version and codename).
87 *****************************************************************************/
88 char const * VLC_Error( int i_err )
90 return vlc_error( i_err );
93 /*****************************************************************************
94 * VLC_Create: allocate a libvlc instance and intialize global libvlc stuff if needed
95 *****************************************************************************
96 * This function allocates a libvlc instance and returns a negative value
97 * in case of failure. Also, the thread system is initialized.
98 *****************************************************************************/
99 int VLC_Create( void )
101 libvlc_int_t *p_object = libvlc_InternalCreate();
102 if( p_object ) return p_object->i_object_id;
106 #define LIBVLC_FUNC \
107 libvlc_int_t * p_libvlc = vlc_current_object( i_object ); \
108 if( !p_libvlc ) return VLC_ENOOBJ;
109 #define LIBVLC_FUNC_END \
110 if( i_object ) vlc_object_release( p_libvlc );
113 /*****************************************************************************
114 * VLC_Init: initialize a libvlc instance
115 *****************************************************************************
116 * This function initializes a previously allocated libvlc instance:
118 * - gettext initialization
119 * - message queue, module bank and playlist initialization
120 * - configuration and commandline parsing
121 *****************************************************************************/
122 int VLC_Init( int i_object, int i_argc, const char *ppsz_argv[] )
126 i_ret = libvlc_InternalInit( p_libvlc, i_argc, ppsz_argv );
131 /*****************************************************************************
132 * VLC_AddIntf: add an interface
133 *****************************************************************************
134 * This function opens an interface plugin and runs it. If b_block is set
135 * to 0, VLC_AddIntf will return immediately and let the interface run in a
136 * separate thread. If b_block is set to 1, VLC_AddIntf will continue until
137 * user requests to quit. If b_play is set to 1, VLC_AddIntf will start playing
138 * the playlist when it is completely initialised.
139 *****************************************************************************/
140 int VLC_AddIntf( int i_object, char const *psz_module,
141 bool b_block, bool b_play )
145 i_ret = libvlc_InternalAddIntf( p_libvlc, psz_module, b_block, b_play,
152 /*****************************************************************************
153 * VLC_Die: ask vlc to die.
154 *****************************************************************************
155 * This function sets p_libvlc->b_die to true, but does not do any other
156 * task. It is your duty to call VLC_CleanUp and VLC_Destroy afterwards.
157 *****************************************************************************/
158 int VLC_Die( int i_object )
161 vlc_object_kill( p_libvlc );
166 /*****************************************************************************
167 * VLC_CleanUp: CleanUp all the intf, playlist, vout, aout
168 *****************************************************************************/
169 int VLC_CleanUp( int i_object )
173 i_ret = libvlc_InternalCleanup( p_libvlc );
178 /*****************************************************************************
179 * VLC_Destroy: Destroy everything.
180 *****************************************************************************
181 * This function requests the running threads to finish, waits for their
182 * termination, and destroys their structure.
183 *****************************************************************************/
184 int VLC_Destroy( int i_object )
187 return libvlc_InternalDestroy( p_libvlc, i_object ? true : false );
190 /*****************************************************************************
191 * VLC_VariableSet: set a vlc variable
192 *****************************************************************************/
193 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
198 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
199 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
200 if( !strncmp( psz_var, "conf::", 6 ) )
202 module_config_t *p_item;
203 char const *psz_newvar = psz_var + 6;
205 p_item = config_FindConfig( VLC_OBJECT(p_libvlc), psz_newvar );
209 switch( p_item->i_type )
211 case CONFIG_ITEM_BOOL:
212 config_PutInt( p_libvlc, psz_newvar, value.b_bool );
214 case CONFIG_ITEM_INTEGER:
215 config_PutInt( p_libvlc, psz_newvar, value.i_int );
217 case CONFIG_ITEM_FLOAT:
218 config_PutFloat( p_libvlc, psz_newvar, value.f_float );
221 config_PutPsz( p_libvlc, psz_newvar, value.psz_string );
224 if( i_object ) vlc_object_release( p_libvlc );
229 i_ret = var_Set( p_libvlc, psz_var, value );
235 /*****************************************************************************
236 * VLC_VariableGet: get a vlc variable
237 *****************************************************************************/
238 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
242 i_ret = var_Get( p_libvlc , psz_var, p_value );
247 /*****************************************************************************
248 * VLC_VariableType: get a vlc variable type
249 *****************************************************************************/
250 int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
254 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
255 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
256 if( !strncmp( psz_var, "conf::", 6 ) )
258 module_config_t *p_item;
259 char const *psz_newvar = psz_var + 6;
261 p_item = config_FindConfig( VLC_OBJECT(p_libvlc), psz_newvar );
265 switch( p_item->i_type )
267 case CONFIG_ITEM_BOOL:
268 i_type = VLC_VAR_BOOL;
270 case CONFIG_ITEM_INTEGER:
271 i_type = VLC_VAR_INTEGER;
273 case CONFIG_ITEM_FLOAT:
274 i_type = VLC_VAR_FLOAT;
277 i_type = VLC_VAR_STRING;
285 i_type = VLC_VAR_TYPE & var_Type( p_libvlc , psz_var );
297 #define LIBVLC_PLAYLIST_FUNC \
298 libvlc_int_t *p_libvlc = vlc_current_object( i_object );\
299 if( !p_libvlc ) return VLC_ENOOBJ; \
300 playlist_t *p_playlist = pl_Yield( p_libvlc ); \
301 if( !p_playlist ) return VLC_ENOOBJ
303 #define LIBVLC_PLAYLIST_FUNC_END \
304 pl_Release( p_libvlc ); \
305 if( i_object ) vlc_object_release( p_libvlc );
307 /*****************************************************************************
308 * VLC_AddTarget: adds a target for playing.
309 *****************************************************************************
310 * This function adds psz_target to the playlist
311 *****************************************************************************/
313 int VLC_AddTarget( int i_object, char const *psz_target,
314 char const **ppsz_options, int i_options,
315 int i_mode, int i_pos )
318 LIBVLC_PLAYLIST_FUNC;
319 i_err = playlist_AddExt( p_playlist, psz_target,
320 NULL, i_mode, i_pos, -1,
321 ppsz_options, i_options, true, false );
322 LIBVLC_PLAYLIST_FUNC_END;
326 /*****************************************************************************
327 * VLC_Play: play the playlist
328 *****************************************************************************/
329 int VLC_Play( int i_object )
331 LIBVLC_PLAYLIST_FUNC;
332 playlist_Play( p_playlist );
333 LIBVLC_PLAYLIST_FUNC_END;
337 /*****************************************************************************
338 * VLC_Pause: toggle pause
339 *****************************************************************************/
340 int VLC_Pause( int i_object )
342 LIBVLC_PLAYLIST_FUNC;
343 playlist_Pause( p_playlist );
344 LIBVLC_PLAYLIST_FUNC_END;
348 /*****************************************************************************
349 * VLC_Stop: stop playback
350 *****************************************************************************/
351 int VLC_Stop( int i_object )
353 LIBVLC_PLAYLIST_FUNC;
354 playlist_Stop( p_playlist );
355 LIBVLC_PLAYLIST_FUNC_END;
359 /*****************************************************************************
360 * VLC_IsPlaying: Query for Playlist Status
361 *****************************************************************************/
362 bool VLC_IsPlaying( int i_object )
366 LIBVLC_PLAYLIST_FUNC;
367 if( p_playlist->p_input )
370 var_Get( p_playlist->p_input, "state", &val );
371 b_playing = ( val.i_int == PLAYING_S );
375 b_playing = playlist_IsPlaying( p_playlist );
377 LIBVLC_PLAYLIST_FUNC_END;
382 * Get the current position in a input
384 * Return the current position as a float
385 * \note For some inputs, this will be unknown.
387 * \param i_object a vlc object id
388 * \return a float in the range of 0.0 - 1.0
390 float VLC_PositionGet( int i_object )
392 input_thread_t *p_input;
396 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
400 if( i_object ) vlc_object_release( p_libvlc );
404 var_Get( p_input, "position", &val );
405 vlc_object_release( p_input );
412 * Set the current position in a input
414 * Set the current position in a input and then return
415 * the current position as a float.
416 * \note For some inputs, this will be unknown.
418 * \param i_object a vlc object id
419 * \param i_position a float in the range of 0.0 - 1.0
420 * \return a float in the range of 0.0 - 1.0
422 float VLC_PositionSet( int i_object, float i_position )
424 input_thread_t *p_input;
426 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
428 /* Check that the handle is valid */
434 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
438 if( i_object ) vlc_object_release( p_libvlc );
442 val.f_float = i_position;
443 var_Set( p_input, "position", val );
444 var_Get( p_input, "position", &val );
445 vlc_object_release( p_input );
447 if( i_object ) vlc_object_release( p_libvlc );
452 * Get the current position in a input
454 * Return the current position in seconds from the start.
455 * \note For some inputs, this will be unknown.
457 * \param i_object a vlc object id
458 * \return the offset from 0:00 in seconds
460 int VLC_TimeGet( int i_object )
462 input_thread_t *p_input;
464 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
466 /* Check that the handle is valid */
472 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
476 if( i_object ) vlc_object_release( p_libvlc );
480 var_Get( p_input, "time", &val );
481 vlc_object_release( p_input );
483 if( i_object ) vlc_object_release( p_libvlc );
484 return val.i_time / 1000000;
488 * Seek to a position in the current input
490 * Seek i_seconds in the current input. If b_relative is set,
491 * then the seek will be relative to the current position, otherwise
492 * it will seek to i_seconds from the beginning of the input.
493 * \note For some inputs, this will be unknown.
495 * \param i_object a vlc object id
496 * \param i_seconds seconds from current position or from beginning of input
497 * \param b_relative seek relative from current position
498 * \return VLC_SUCCESS on success
500 int VLC_TimeSet( int i_object, int i_seconds, bool b_relative )
502 input_thread_t *p_input;
504 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
506 /* Check that the handle is valid */
512 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
516 if( i_object ) vlc_object_release( p_libvlc );
522 val.i_time = i_seconds;
523 val.i_time = val.i_time * 1000000L;
524 var_Set( p_input, "time-offset", val );
528 val.i_time = i_seconds;
529 val.i_time = val.i_time * 1000000L;
530 var_Set( p_input, "time", val );
532 vlc_object_release( p_input );
534 if( i_object ) vlc_object_release( p_libvlc );
539 * Get the total length of a input
541 * Return the total length in seconds from the current input.
542 * \note For some inputs, this will be unknown.
544 * \param i_object a vlc object id
545 * \return the length in seconds
547 int VLC_LengthGet( int i_object )
549 input_thread_t *p_input;
551 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
553 /* Check that the handle is valid */
559 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
563 if( i_object ) vlc_object_release( p_libvlc );
567 var_Get( p_input, "length", &val );
568 vlc_object_release( p_input );
570 if( i_object ) vlc_object_release( p_libvlc );
571 return val.i_time / 1000000L;
575 * Play the input faster than realtime
577 * 2x, 4x, 8x faster than realtime
578 * \note For some inputs, this will be impossible.
580 * \param i_object a vlc object id
581 * \return the current speedrate
583 float VLC_SpeedFaster( int i_object )
585 input_thread_t *p_input;
587 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
589 /* Check that the handle is valid */
595 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
599 if( i_object ) vlc_object_release( p_libvlc );
604 var_Set( p_input, "rate-faster", val );
605 var_Get( p_input, "rate", &val );
606 vlc_object_release( p_input );
608 if( i_object ) vlc_object_release( p_libvlc );
609 return val.f_float / INPUT_RATE_DEFAULT;
613 * Play the input slower than realtime
615 * 1/2x, 1/4x, 1/8x slower than realtime
616 * \note For some inputs, this will be impossible.
618 * \param i_object a vlc object id
619 * \return the current speedrate
621 float VLC_SpeedSlower( int i_object )
623 input_thread_t *p_input;
625 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
627 /* Check that the handle is valid */
633 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
637 if( i_object ) vlc_object_release( p_libvlc );
642 var_Set( p_input, "rate-slower", val );
643 var_Get( p_input, "rate", &val );
644 vlc_object_release( p_input );
646 if( i_object ) vlc_object_release( p_libvlc );
647 return val.f_float / INPUT_RATE_DEFAULT;
651 * Return the current playlist item
653 * Returns the index of the playlistitem that is currently selected for play.
654 * This is valid even if nothing is currently playing.
656 * \param i_object a vlc object id
657 * \return the current index
659 int VLC_PlaylistIndex( int i_object )
662 printf( "This function is deprecated and should not be used anymore" );
667 * Total number of items in the playlist
669 * \param i_object a vlc object id
670 * \return amount of playlist items
672 int VLC_PlaylistNumberOfItems( int i_object )
675 LIBVLC_PLAYLIST_FUNC;
676 i_size = p_playlist->items.i_size;
677 LIBVLC_PLAYLIST_FUNC_END;
682 * Go to next playlist item
683 * \param i_object a vlc object id
684 * \return VLC_SUCCESS on success
686 int VLC_PlaylistNext( int i_object )
688 LIBVLC_PLAYLIST_FUNC;
689 playlist_Next( p_playlist );
690 LIBVLC_PLAYLIST_FUNC_END;
695 * Go to previous playlist item
696 * \param i_object a vlc object id
697 * \return VLC_SUCCESS on success
699 int VLC_PlaylistPrev( int i_object )
701 LIBVLC_PLAYLIST_FUNC;
702 playlist_Prev( p_playlist );
703 LIBVLC_PLAYLIST_FUNC_END;
710 int VLC_PlaylistClear( int i_object )
712 LIBVLC_PLAYLIST_FUNC;
713 playlist_Clear( p_playlist, true );
714 LIBVLC_PLAYLIST_FUNC_END;
721 * \param i_object a vlc object id
722 * \param i_volume something in a range from 0-200
723 * \return the new volume (range 0-200 %)
725 int VLC_VolumeSet( int i_object, int i_volume )
727 audio_volume_t i_vol = 0;
730 if( i_volume >= 0 && i_volume <= 200 )
732 i_vol = i_volume * AOUT_VOLUME_MAX / 200;
733 aout_VolumeSet( p_libvlc, i_vol );
736 return i_vol * 200 / AOUT_VOLUME_MAX;
740 * Get the current volume
742 * Retrieve the current volume.
744 * \param i_object a vlc object id
745 * \return the current volume (range 0-200 %)
747 int VLC_VolumeGet( int i_object )
749 audio_volume_t i_volume;
751 aout_VolumeGet( p_libvlc, &i_volume );
753 return i_volume*200/AOUT_VOLUME_MAX;
757 * Mute/Unmute the volume
759 * \param i_object a vlc object id
760 * \return VLC_SUCCESS on success
762 int VLC_VolumeMute( int i_object )
765 aout_VolumeMute( p_libvlc, NULL );
770 /*****************************************************************************
771 * VLC_FullScreen: toggle fullscreen mode
772 *****************************************************************************/
773 int VLC_FullScreen( int i_object )
775 vout_thread_t *p_vout;
777 p_vout = vlc_object_find( p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD );
781 if( i_object ) vlc_object_release( p_libvlc );
785 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
786 vlc_object_release( p_vout );