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 "safe" 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 /* VLC_VariableSet is only used from the browser plugins, so we
210 * can pretty much assume that the input is _not_ trusted. */
211 if( !p_item->b_safe )
214 switch( p_item->i_type )
216 case CONFIG_ITEM_BOOL:
217 config_PutInt( p_libvlc, psz_newvar, value.b_bool );
219 case CONFIG_ITEM_INTEGER:
220 config_PutInt( p_libvlc, psz_newvar, value.i_int );
222 case CONFIG_ITEM_FLOAT:
223 config_PutFloat( p_libvlc, psz_newvar, value.f_float );
226 config_PutPsz( p_libvlc, psz_newvar, value.psz_string );
229 if( i_object ) vlc_object_release( p_libvlc );
233 /* EXPLICIT HACK (this is the legacy API anyway):
234 * VLC_VariableSet is only used from the browser plugins, so we
235 * can pretty much assume that the input is _not_ trusted. */
236 module_config_t *p_item;
237 p_item = config_FindConfig( VLC_OBJECT(p_libvlc), psz_var );
240 if( !p_item->b_safe )
243 i_ret = var_Set( p_libvlc, psz_var, value );
249 /*****************************************************************************
250 * VLC_VariableGet: get a vlc variable
251 *****************************************************************************/
252 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
256 i_ret = var_Get( p_libvlc , psz_var, p_value );
261 /*****************************************************************************
262 * VLC_VariableType: get a vlc variable type
263 *****************************************************************************/
264 int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
268 /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
269 * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
270 if( !strncmp( psz_var, "conf::", 6 ) )
272 module_config_t *p_item;
273 char const *psz_newvar = psz_var + 6;
275 p_item = config_FindConfig( VLC_OBJECT(p_libvlc), psz_newvar );
279 switch( p_item->i_type )
281 case CONFIG_ITEM_BOOL:
282 i_type = VLC_VAR_BOOL;
284 case CONFIG_ITEM_INTEGER:
285 i_type = VLC_VAR_INTEGER;
287 case CONFIG_ITEM_FLOAT:
288 i_type = VLC_VAR_FLOAT;
291 i_type = VLC_VAR_STRING;
299 i_type = VLC_VAR_TYPE & var_Type( p_libvlc , psz_var );
311 #define LIBVLC_PLAYLIST_FUNC \
312 libvlc_int_t *p_libvlc = vlc_current_object( i_object );\
313 if( !p_libvlc ) return VLC_ENOOBJ; \
314 playlist_t *p_playlist = pl_Yield( p_libvlc ); \
315 if( !p_playlist ) return VLC_ENOOBJ
317 #define LIBVLC_PLAYLIST_FUNC_END \
318 pl_Release( p_libvlc ); \
319 if( i_object ) vlc_object_release( p_libvlc );
321 /*****************************************************************************
322 * VLC_AddTarget: adds a target for playing.
323 *****************************************************************************
324 * This function adds psz_target to the playlist
325 *****************************************************************************/
327 int VLC_AddTarget( int i_object, char const *psz_target,
328 char const **ppsz_options, int i_options,
329 int i_mode, int i_pos )
332 LIBVLC_PLAYLIST_FUNC;
333 i_err = playlist_AddExt( p_playlist, psz_target,
334 NULL, i_mode, i_pos, -1,
335 ppsz_options, i_options, true, false );
336 LIBVLC_PLAYLIST_FUNC_END;
340 /*****************************************************************************
341 * VLC_Play: play the playlist
342 *****************************************************************************/
343 int VLC_Play( int i_object )
345 LIBVLC_PLAYLIST_FUNC;
346 playlist_Play( p_playlist );
347 LIBVLC_PLAYLIST_FUNC_END;
351 /*****************************************************************************
352 * VLC_Pause: toggle pause
353 *****************************************************************************/
354 int VLC_Pause( int i_object )
356 LIBVLC_PLAYLIST_FUNC;
357 playlist_Pause( p_playlist );
358 LIBVLC_PLAYLIST_FUNC_END;
362 /*****************************************************************************
363 * VLC_Stop: stop playback
364 *****************************************************************************/
365 int VLC_Stop( int i_object )
367 LIBVLC_PLAYLIST_FUNC;
368 playlist_Stop( p_playlist );
369 LIBVLC_PLAYLIST_FUNC_END;
373 /*****************************************************************************
374 * VLC_IsPlaying: Query for Playlist Status
375 *****************************************************************************/
376 bool VLC_IsPlaying( int i_object )
380 LIBVLC_PLAYLIST_FUNC;
381 if( p_playlist->p_input )
384 var_Get( p_playlist->p_input, "state", &val );
385 b_playing = ( val.i_int == PLAYING_S );
389 b_playing = playlist_IsPlaying( p_playlist );
391 LIBVLC_PLAYLIST_FUNC_END;
396 * Get the current position in a input
398 * Return the current position as a float
399 * \note For some inputs, this will be unknown.
401 * \param i_object a vlc object id
402 * \return a float in the range of 0.0 - 1.0
404 float VLC_PositionGet( int i_object )
406 input_thread_t *p_input;
410 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
414 if( i_object ) vlc_object_release( p_libvlc );
418 var_Get( p_input, "position", &val );
419 vlc_object_release( p_input );
426 * Set the current position in a input
428 * Set the current position in a input and then return
429 * the current position as a float.
430 * \note For some inputs, this will be unknown.
432 * \param i_object a vlc object id
433 * \param i_position a float in the range of 0.0 - 1.0
434 * \return a float in the range of 0.0 - 1.0
436 float VLC_PositionSet( int i_object, float i_position )
438 input_thread_t *p_input;
440 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
442 /* Check that the handle is valid */
448 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
452 if( i_object ) vlc_object_release( p_libvlc );
456 val.f_float = i_position;
457 var_Set( p_input, "position", val );
458 var_Get( p_input, "position", &val );
459 vlc_object_release( p_input );
461 if( i_object ) vlc_object_release( p_libvlc );
466 * Get the current position in a input
468 * Return the current position in seconds from the start.
469 * \note For some inputs, this will be unknown.
471 * \param i_object a vlc object id
472 * \return the offset from 0:00 in seconds
474 int VLC_TimeGet( int i_object )
476 input_thread_t *p_input;
478 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
480 /* Check that the handle is valid */
486 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
490 if( i_object ) vlc_object_release( p_libvlc );
494 var_Get( p_input, "time", &val );
495 vlc_object_release( p_input );
497 if( i_object ) vlc_object_release( p_libvlc );
498 return val.i_time / 1000000;
502 * Seek to a position in the current input
504 * Seek i_seconds in the current input. If b_relative is set,
505 * then the seek will be relative to the current position, otherwise
506 * it will seek to i_seconds from the beginning of the input.
507 * \note For some inputs, this will be unknown.
509 * \param i_object a vlc object id
510 * \param i_seconds seconds from current position or from beginning of input
511 * \param b_relative seek relative from current position
512 * \return VLC_SUCCESS on success
514 int VLC_TimeSet( int i_object, int i_seconds, bool b_relative )
516 input_thread_t *p_input;
518 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
520 /* Check that the handle is valid */
526 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
530 if( i_object ) vlc_object_release( p_libvlc );
536 val.i_time = i_seconds;
537 val.i_time = val.i_time * 1000000L;
538 var_Set( p_input, "time-offset", val );
542 val.i_time = i_seconds;
543 val.i_time = val.i_time * 1000000L;
544 var_Set( p_input, "time", val );
546 vlc_object_release( p_input );
548 if( i_object ) vlc_object_release( p_libvlc );
553 * Get the total length of a input
555 * Return the total length in seconds from the current input.
556 * \note For some inputs, this will be unknown.
558 * \param i_object a vlc object id
559 * \return the length in seconds
561 int VLC_LengthGet( int i_object )
563 input_thread_t *p_input;
565 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
567 /* Check that the handle is valid */
573 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
577 if( i_object ) vlc_object_release( p_libvlc );
581 var_Get( p_input, "length", &val );
582 vlc_object_release( p_input );
584 if( i_object ) vlc_object_release( p_libvlc );
585 return val.i_time / 1000000L;
589 * Play the input faster than realtime
591 * 2x, 4x, 8x faster than realtime
592 * \note For some inputs, this will be impossible.
594 * \param i_object a vlc object id
595 * \return the current speedrate
597 float VLC_SpeedFaster( int i_object )
599 input_thread_t *p_input;
601 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
603 /* Check that the handle is valid */
609 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
613 if( i_object ) vlc_object_release( p_libvlc );
618 var_Set( p_input, "rate-faster", val );
619 var_Get( p_input, "rate", &val );
620 vlc_object_release( p_input );
622 if( i_object ) vlc_object_release( p_libvlc );
623 return val.f_float / INPUT_RATE_DEFAULT;
627 * Play the input slower than realtime
629 * 1/2x, 1/4x, 1/8x slower than realtime
630 * \note For some inputs, this will be impossible.
632 * \param i_object a vlc object id
633 * \return the current speedrate
635 float VLC_SpeedSlower( int i_object )
637 input_thread_t *p_input;
639 libvlc_int_t *p_libvlc = vlc_current_object( i_object );
641 /* Check that the handle is valid */
647 p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
651 if( i_object ) vlc_object_release( p_libvlc );
656 var_Set( p_input, "rate-slower", val );
657 var_Get( p_input, "rate", &val );
658 vlc_object_release( p_input );
660 if( i_object ) vlc_object_release( p_libvlc );
661 return val.f_float / INPUT_RATE_DEFAULT;
665 * Return the current playlist item
667 * Returns the index of the playlistitem that is currently selected for play.
668 * This is valid even if nothing is currently playing.
670 * \param i_object a vlc object id
671 * \return the current index
673 int VLC_PlaylistIndex( int i_object )
676 printf( "This function is deprecated and should not be used anymore" );
681 * Total number of items in the playlist
683 * \param i_object a vlc object id
684 * \return amount of playlist items
686 int VLC_PlaylistNumberOfItems( int i_object )
689 LIBVLC_PLAYLIST_FUNC;
690 i_size = p_playlist->items.i_size;
691 LIBVLC_PLAYLIST_FUNC_END;
696 * Go to next playlist item
697 * \param i_object a vlc object id
698 * \return VLC_SUCCESS on success
700 int VLC_PlaylistNext( int i_object )
702 LIBVLC_PLAYLIST_FUNC;
703 playlist_Next( p_playlist );
704 LIBVLC_PLAYLIST_FUNC_END;
709 * Go to previous playlist item
710 * \param i_object a vlc object id
711 * \return VLC_SUCCESS on success
713 int VLC_PlaylistPrev( int i_object )
715 LIBVLC_PLAYLIST_FUNC;
716 playlist_Prev( p_playlist );
717 LIBVLC_PLAYLIST_FUNC_END;
724 int VLC_PlaylistClear( int i_object )
726 LIBVLC_PLAYLIST_FUNC;
727 playlist_Clear( p_playlist, true );
728 LIBVLC_PLAYLIST_FUNC_END;
735 * \param i_object a vlc object id
736 * \param i_volume something in a range from 0-200
737 * \return the new volume (range 0-200 %)
739 int VLC_VolumeSet( int i_object, int i_volume )
741 audio_volume_t i_vol = 0;
744 if( i_volume >= 0 && i_volume <= 200 )
746 i_vol = i_volume * AOUT_VOLUME_MAX / 200;
747 aout_VolumeSet( p_libvlc, i_vol );
750 return i_vol * 200 / AOUT_VOLUME_MAX;
754 * Get the current volume
756 * Retrieve the current volume.
758 * \param i_object a vlc object id
759 * \return the current volume (range 0-200 %)
761 int VLC_VolumeGet( int i_object )
763 audio_volume_t i_volume;
765 aout_VolumeGet( p_libvlc, &i_volume );
767 return i_volume*200/AOUT_VOLUME_MAX;
771 * Mute/Unmute the volume
773 * \param i_object a vlc object id
774 * \return VLC_SUCCESS on success
776 int VLC_VolumeMute( int i_object )
779 aout_VolumeMute( p_libvlc, NULL );
784 /*****************************************************************************
785 * VLC_FullScreen: toggle fullscreen mode
786 *****************************************************************************/
787 int VLC_FullScreen( int i_object )
789 vout_thread_t *p_vout;
791 p_vout = vlc_object_find( p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD );
795 if( i_object ) vlc_object_release( p_libvlc );
799 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
800 vlc_object_release( p_vout );