]> git.sesse.net Git - vlc/blob - src/libvlc.c
libvlc: use vlc_common.h (libvlccore) instead of vlc/vlc.h
[vlc] / src / libvlc.c
1 /*****************************************************************************
2  * libvlc.c: Implementation of the old libvlc API
3  *****************************************************************************
4  * Copyright (C) 1998-2007 the VideoLAN team
5  * $Id$
6  *
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>
12  *
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.
17  *
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.
22  *
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  *****************************************************************************/
27
28 /*****************************************************************************
29  * Pretend we are a builtin module
30  *****************************************************************************/
31 #define MODULE_NAME main
32 #define MODULE_PATH main
33 #define __BUILTIN__
34
35 /*****************************************************************************
36  * Preamble
37  *****************************************************************************/
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41
42 #include <vlc_common.h>
43
44 #include "control/libvlc_internal.h"
45 #include "libvlc.h"
46
47 #include <vlc_playlist.h>
48
49 #include <vlc_aout.h>
50 #include <vlc_vout.h>
51
52 #define LIBVLC_FUNC \
53     libvlc_int_t * p_libvlc = vlc_current_object( i_object ); \
54     if( !p_libvlc ) return VLC_ENOOBJ;
55 #define LIBVLC_FUNC_END \
56     if( i_object ) vlc_object_release( p_libvlc );
57
58
59 /*****************************************************************************
60  * VLC_VariableSet: set a "safe" vlc variable
61  *****************************************************************************/
62 int VLC_VariableSet( int i_object, char const *psz_var, vlc_value_t value )
63 {
64     int i_ret;
65     LIBVLC_FUNC;
66
67     /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
68      * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
69     if( !strncmp( psz_var, "conf::", 6 ) )
70     {
71         module_config_t *p_item;
72         char const *psz_newvar = psz_var + 6;
73
74         p_item = config_FindConfig( VLC_OBJECT(p_libvlc), psz_newvar );
75
76         if( p_item )
77         {
78             /* VLC_VariableSet is only used from the browser plugins, so we
79              *  can pretty much assume that the input is _not_ trusted. */
80             if( !p_item->b_safe )
81                 return VLC_EGENERIC;
82
83             switch( p_item->i_type )
84             {
85                 case CONFIG_ITEM_BOOL:
86                     config_PutInt( p_libvlc, psz_newvar, value.b_bool );
87                     break;
88                 case CONFIG_ITEM_INTEGER:
89                     config_PutInt( p_libvlc, psz_newvar, value.i_int );
90                     break;
91                 case CONFIG_ITEM_FLOAT:
92                     config_PutFloat( p_libvlc, psz_newvar, value.f_float );
93                     break;
94                 default:
95                     config_PutPsz( p_libvlc, psz_newvar, value.psz_string );
96                     break;
97             }
98             if( i_object ) vlc_object_release( p_libvlc );
99             return VLC_SUCCESS;
100         }
101     }
102     /* EXPLICIT HACK (this is the legacy API anyway):
103      * VLC_VariableSet is only used from the browser plugins, so we
104      *  can pretty much assume that the input is _not_ trusted. */
105     module_config_t *p_item;
106     p_item = config_FindConfig( VLC_OBJECT(p_libvlc), psz_var );
107     if( !p_item )
108         return VLC_ENOVAR;
109     if( !p_item->b_safe )
110         return VLC_EGENERIC;
111
112     i_ret = var_Set( p_libvlc, psz_var, value );
113
114     LIBVLC_FUNC_END;
115     return i_ret;
116 }
117
118 /*****************************************************************************
119  * VLC_VariableGet: get a vlc variable
120  *****************************************************************************/
121 int VLC_VariableGet( int i_object, char const *psz_var, vlc_value_t *p_value )
122 {
123     int i_ret;
124     LIBVLC_FUNC;
125     i_ret = var_Get( p_libvlc , psz_var, p_value );
126     LIBVLC_FUNC_END;
127     return i_ret;
128 }
129
130 /*****************************************************************************
131  * VLC_VariableType: get a vlc variable type
132  *****************************************************************************/
133 int VLC_VariableType( int i_object, char const *psz_var, int *pi_type )
134 {
135     int i_type;
136     LIBVLC_FUNC;
137     /* FIXME: Temporary hack for Mozilla, if variable starts with conf:: then
138      * we handle it as a configuration variable. Don't tell Gildas :) -- sam */
139     if( !strncmp( psz_var, "conf::", 6 ) )
140     {
141         module_config_t *p_item;
142         char const *psz_newvar = psz_var + 6;
143
144         p_item = config_FindConfig( VLC_OBJECT(p_libvlc), psz_newvar );
145
146         if( p_item )
147         {
148             switch( p_item->i_type )
149             {
150                 case CONFIG_ITEM_BOOL:
151                     i_type = VLC_VAR_BOOL;
152                     break;
153                 case CONFIG_ITEM_INTEGER:
154                     i_type = VLC_VAR_INTEGER;
155                     break;
156                 case CONFIG_ITEM_FLOAT:
157                     i_type = VLC_VAR_FLOAT;
158                     break;
159                 default:
160                     i_type = VLC_VAR_STRING;
161                     break;
162             }
163         }
164         else
165             i_type = 0;
166     }
167     else
168         i_type = VLC_VAR_TYPE & var_Type( p_libvlc , psz_var );
169
170     LIBVLC_FUNC_END;
171
172     if( i_type > 0 )
173     {
174         *pi_type = i_type;
175         return VLC_SUCCESS;
176     }
177     return VLC_ENOVAR;
178 }
179
180 #define LIBVLC_PLAYLIST_FUNC \
181     libvlc_int_t *p_libvlc = vlc_current_object( i_object );\
182     if( !p_libvlc ) return VLC_ENOOBJ; \
183     playlist_t *p_playlist = pl_Yield( p_libvlc ); \
184     if( !p_playlist ) return VLC_ENOOBJ
185
186 #define LIBVLC_PLAYLIST_FUNC_END \
187     pl_Release( p_libvlc ); \
188     if( i_object ) vlc_object_release( p_libvlc );
189
190 /*****************************************************************************
191  * VLC_AddTarget: adds a target for playing.
192  *****************************************************************************
193  * This function adds psz_target to the playlist
194  *****************************************************************************/
195
196 int VLC_AddTarget( int i_object, char const *psz_target,
197                    char const **ppsz_options, int i_options,
198                    int i_mode, int i_pos )
199 {
200     int i_err;
201     LIBVLC_PLAYLIST_FUNC;
202     i_err = playlist_AddExt( p_playlist, psz_target,
203                              NULL,  i_mode, i_pos, -1,
204                              ppsz_options, i_options, true, false );
205     LIBVLC_PLAYLIST_FUNC_END;
206     return i_err;
207 }
208
209 /*****************************************************************************
210  * VLC_Play: play the playlist
211  *****************************************************************************/
212 int VLC_Play( int i_object )
213 {
214     LIBVLC_PLAYLIST_FUNC;
215     playlist_Play( p_playlist );
216     LIBVLC_PLAYLIST_FUNC_END;
217     return VLC_SUCCESS;
218 }
219
220 /*****************************************************************************
221  * VLC_Pause: toggle pause
222  *****************************************************************************/
223 int VLC_Pause( int i_object )
224 {
225     LIBVLC_PLAYLIST_FUNC;
226     playlist_Pause( p_playlist );
227     LIBVLC_PLAYLIST_FUNC_END;
228     return VLC_SUCCESS;
229 }
230
231 /*****************************************************************************
232  * VLC_Stop: stop playback
233  *****************************************************************************/
234 int VLC_Stop( int i_object )
235 {
236     LIBVLC_PLAYLIST_FUNC;
237     playlist_Stop( p_playlist );
238     LIBVLC_PLAYLIST_FUNC_END;
239     return VLC_SUCCESS;
240 }
241
242 /*****************************************************************************
243  * VLC_IsPlaying: Query for Playlist Status
244  *****************************************************************************/
245 bool VLC_IsPlaying( int i_object )
246 {
247     bool   b_playing;
248
249     LIBVLC_PLAYLIST_FUNC;
250     if( p_playlist->p_input )
251     {
252         vlc_value_t  val;
253         var_Get( p_playlist->p_input, "state", &val );
254         b_playing = ( val.i_int == PLAYING_S );
255     }
256     else
257     {
258         b_playing = playlist_IsPlaying( p_playlist );
259     }
260     LIBVLC_PLAYLIST_FUNC_END;
261     return b_playing;
262 }
263
264 /**
265  * Get the current position in a input
266  *
267  * Return the current position as a float
268  * \note For some inputs, this will be unknown.
269  *
270  * \param i_object a vlc object id
271  * \return a float in the range of 0.0 - 1.0
272  */
273 float VLC_PositionGet( int i_object )
274 {
275     input_thread_t *p_input;
276     vlc_value_t val;
277     LIBVLC_FUNC;
278
279     p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
280
281     if( !p_input )
282     {
283         if( i_object ) vlc_object_release( p_libvlc );
284         return VLC_ENOOBJ;
285     }
286
287     var_Get( p_input, "position", &val );
288     vlc_object_release( p_input );
289
290     LIBVLC_FUNC_END;
291     return val.f_float;
292 }
293
294 /**
295  * Set the current position in a input
296  *
297  * Set the current position in a input and then return
298  * the current position as a float.
299  * \note For some inputs, this will be unknown.
300  *
301  * \param i_object a vlc object id
302  * \param i_position a float in the range of 0.0 - 1.0
303  * \return a float in the range of 0.0 - 1.0
304  */
305 float VLC_PositionSet( int i_object, float i_position )
306 {
307     input_thread_t *p_input;
308     vlc_value_t val;
309     libvlc_int_t *p_libvlc = vlc_current_object( i_object );
310
311     /* Check that the handle is valid */
312     if( !p_libvlc )
313     {
314         return VLC_ENOOBJ;
315     }
316
317     p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
318
319     if( !p_input )
320     {
321         if( i_object ) vlc_object_release( p_libvlc );
322         return VLC_ENOOBJ;
323     }
324
325     val.f_float = i_position;
326     var_Set( p_input, "position", val );
327     var_Get( p_input, "position", &val );
328     vlc_object_release( p_input );
329
330     if( i_object ) vlc_object_release( p_libvlc );
331     return val.f_float;
332 }
333
334 /**
335  * Get the current position in a input
336  *
337  * Return the current position in seconds from the start.
338  * \note For some inputs, this will be unknown.
339  *
340  * \param i_object a vlc object id
341  * \return the offset from 0:00 in seconds
342  */
343 int VLC_TimeGet( int i_object )
344 {
345     input_thread_t *p_input;
346     vlc_value_t val;
347     libvlc_int_t *p_libvlc = vlc_current_object( i_object );
348
349     /* Check that the handle is valid */
350     if( !p_libvlc )
351     {
352         return VLC_ENOOBJ;
353     }
354
355     p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
356
357     if( !p_input )
358     {
359         if( i_object ) vlc_object_release( p_libvlc );
360         return VLC_ENOOBJ;
361     }
362
363     var_Get( p_input, "time", &val );
364     vlc_object_release( p_input );
365
366     if( i_object ) vlc_object_release( p_libvlc );
367     return val.i_time  / 1000000;
368 }
369
370 /**
371  * Seek to a position in the current input
372  *
373  * Seek i_seconds in the current input. If b_relative is set,
374  * then the seek will be relative to the current position, otherwise
375  * it will seek to i_seconds from the beginning of the input.
376  * \note For some inputs, this will be unknown.
377  *
378  * \param i_object a vlc object id
379  * \param i_seconds seconds from current position or from beginning of input
380  * \param b_relative seek relative from current position
381  * \return VLC_SUCCESS on success
382  */
383 int VLC_TimeSet( int i_object, int i_seconds, bool b_relative )
384 {
385     input_thread_t *p_input;
386     vlc_value_t val;
387     libvlc_int_t *p_libvlc = vlc_current_object( i_object );
388
389     /* Check that the handle is valid */
390     if( !p_libvlc )
391     {
392         return VLC_ENOOBJ;
393     }
394
395     p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
396
397     if( !p_input )
398     {
399         if( i_object ) vlc_object_release( p_libvlc );
400         return VLC_ENOOBJ;
401     }
402
403     if( b_relative )
404     {
405         val.i_time = i_seconds;
406         val.i_time = val.i_time * 1000000L;
407         var_Set( p_input, "time-offset", val );
408     }
409     else
410     {
411         val.i_time = i_seconds;
412         val.i_time = val.i_time * 1000000L;
413         var_Set( p_input, "time", val );
414     }
415     vlc_object_release( p_input );
416
417     if( i_object ) vlc_object_release( p_libvlc );
418     return VLC_SUCCESS;
419 }
420
421 /**
422  * Get the total length of a input
423  *
424  * Return the total length in seconds from the current input.
425  * \note For some inputs, this will be unknown.
426  *
427  * \param i_object a vlc object id
428  * \return the length in seconds
429  */
430 int VLC_LengthGet( int i_object )
431 {
432     input_thread_t *p_input;
433     vlc_value_t val;
434     libvlc_int_t *p_libvlc = vlc_current_object( i_object );
435
436     /* Check that the handle is valid */
437     if( !p_libvlc )
438     {
439         return VLC_ENOOBJ;
440     }
441
442     p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
443
444     if( !p_input )
445     {
446         if( i_object ) vlc_object_release( p_libvlc );
447         return VLC_ENOOBJ;
448     }
449
450     var_Get( p_input, "length", &val );
451     vlc_object_release( p_input );
452
453     if( i_object ) vlc_object_release( p_libvlc );
454     return val.i_time  / 1000000L;
455 }
456
457 /**
458  * Play the input faster than realtime
459  *
460  * 2x, 4x, 8x faster than realtime
461  * \note For some inputs, this will be impossible.
462  *
463  * \param i_object a vlc object id
464  * \return the current speedrate
465  */
466 float VLC_SpeedFaster( int i_object )
467 {
468     input_thread_t *p_input;
469     vlc_value_t val;
470     libvlc_int_t *p_libvlc = vlc_current_object( i_object );
471
472     /* Check that the handle is valid */
473     if( !p_libvlc )
474     {
475         return VLC_ENOOBJ;
476     }
477
478     p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
479
480     if( !p_input )
481     {
482         if( i_object ) vlc_object_release( p_libvlc );
483         return VLC_ENOOBJ;
484     }
485
486     val.b_bool = true;
487     var_Set( p_input, "rate-faster", val );
488     var_Get( p_input, "rate", &val );
489     vlc_object_release( p_input );
490
491     if( i_object ) vlc_object_release( p_libvlc );
492     return val.f_float / INPUT_RATE_DEFAULT;
493 }
494
495 /**
496  * Play the input slower than realtime
497  *
498  * 1/2x, 1/4x, 1/8x slower than realtime
499  * \note For some inputs, this will be impossible.
500  *
501  * \param i_object a vlc object id
502  * \return the current speedrate
503  */
504 float VLC_SpeedSlower( int i_object )
505 {
506     input_thread_t *p_input;
507     vlc_value_t val;
508     libvlc_int_t *p_libvlc = vlc_current_object( i_object );
509
510     /* Check that the handle is valid */
511     if( !p_libvlc )
512     {
513         return VLC_ENOOBJ;
514     }
515
516     p_input = vlc_object_find( p_libvlc, VLC_OBJECT_INPUT, FIND_CHILD );
517
518     if( !p_input )
519     {
520         if( i_object ) vlc_object_release( p_libvlc );
521         return VLC_ENOOBJ;
522     }
523
524     val.b_bool = true;
525     var_Set( p_input, "rate-slower", val );
526     var_Get( p_input, "rate", &val );
527     vlc_object_release( p_input );
528
529     if( i_object ) vlc_object_release( p_libvlc );
530     return val.f_float / INPUT_RATE_DEFAULT;
531 }
532
533 /**
534  * Return the current playlist item
535  *
536  * Returns the index of the playlistitem that is currently selected for play.
537  * This is valid even if nothing is currently playing.
538  *
539  * \param i_object a vlc object id
540  * \return the current index
541  */
542 int VLC_PlaylistIndex( int i_object )
543 {
544     (void)i_object;
545     printf( "This function is deprecated and should not be used anymore" );
546     return -1;
547 }
548
549 /**
550  * Total number of items in the playlist
551  *
552  * \param i_object a vlc object id
553  * \return amount of playlist items
554  */
555 int VLC_PlaylistNumberOfItems( int i_object )
556 {
557     int i_size;
558     LIBVLC_PLAYLIST_FUNC;
559     i_size = p_playlist->items.i_size;
560     LIBVLC_PLAYLIST_FUNC_END;
561     return i_size;
562 }
563
564 /**
565  * Go to next playlist item
566  * \param i_object a vlc object id
567  * \return VLC_SUCCESS on success
568  */
569 int VLC_PlaylistNext( int i_object )
570 {
571     LIBVLC_PLAYLIST_FUNC;
572     playlist_Next( p_playlist );
573     LIBVLC_PLAYLIST_FUNC_END;
574     return VLC_SUCCESS;
575 }
576
577 /**
578  * Go to previous playlist item
579  * \param i_object a vlc object id
580  * \return VLC_SUCCESS on success
581  */
582 int VLC_PlaylistPrev( int i_object )
583 {
584     LIBVLC_PLAYLIST_FUNC;
585     playlist_Prev( p_playlist );
586     LIBVLC_PLAYLIST_FUNC_END;
587     return VLC_SUCCESS;
588 }
589
590 /**
591  * Empty the playlist
592  */
593 int VLC_PlaylistClear( int i_object )
594 {
595     LIBVLC_PLAYLIST_FUNC;
596     playlist_Clear( p_playlist, true );
597     LIBVLC_PLAYLIST_FUNC_END;
598     return VLC_SUCCESS;
599 }
600
601 /**
602  * Change the volume
603  *
604  * \param i_object a vlc object id
605  * \param i_volume something in a range from 0-200
606  * \return the new volume (range 0-200 %)
607  */
608 int VLC_VolumeSet( int i_object, int i_volume )
609 {
610     audio_volume_t i_vol = 0;
611     LIBVLC_FUNC;
612
613     if( i_volume >= 0 && i_volume <= 200 )
614     {
615         i_vol = i_volume * AOUT_VOLUME_MAX / 200;
616         aout_VolumeSet( p_libvlc, i_vol );
617     }
618     LIBVLC_FUNC_END;
619     return i_vol * 200 / AOUT_VOLUME_MAX;
620 }
621
622 /**
623  * Get the current volume
624  *
625  * Retrieve the current volume.
626  *
627  * \param i_object a vlc object id
628  * \return the current volume (range 0-200 %)
629  */
630 int VLC_VolumeGet( int i_object )
631 {
632     audio_volume_t i_volume;
633     LIBVLC_FUNC;
634     aout_VolumeGet( p_libvlc, &i_volume );
635     LIBVLC_FUNC_END;
636     return i_volume*200/AOUT_VOLUME_MAX;
637 }
638
639 /**
640  * Mute/Unmute the volume
641  *
642  * \param i_object a vlc object id
643  * \return VLC_SUCCESS on success
644  */
645 int VLC_VolumeMute( int i_object )
646 {
647     LIBVLC_FUNC;
648     aout_VolumeMute( p_libvlc, NULL );
649     LIBVLC_FUNC_END;
650     return VLC_SUCCESS;
651 }
652
653 /*****************************************************************************
654  * VLC_FullScreen: toggle fullscreen mode
655  *****************************************************************************/
656 int VLC_FullScreen( int i_object )
657 {
658     vout_thread_t *p_vout;
659     LIBVLC_FUNC;
660     p_vout = vlc_object_find( p_libvlc, VLC_OBJECT_VOUT, FIND_CHILD );
661
662     if( !p_vout )
663     {
664         if( i_object ) vlc_object_release( p_libvlc );
665         return VLC_ENOOBJ;
666     }
667
668     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
669     vlc_object_release( p_vout );
670     LIBVLC_FUNC_END;
671     return VLC_SUCCESS;
672 }