]> git.sesse.net Git - vlc/blob - src/control/video.c
Merge branch 1.0-bugfix
[vlc] / src / control / video.c
1 /*****************************************************************************
2  * video.c: libvlc new API video functions
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * $Id$
7  *
8  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
9  *          Filippo Carone <littlejohn@videolan.org>
10  *          Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
11  *          Damien Fouilleul <damienf a_t videolan dot 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 #include "libvlc_internal.h"
29
30 #include <vlc/libvlc.h>
31 #include <vlc_input.h>
32 #include <vlc_vout.h>
33
34 /*
35  * Remember to release the returned vout_thread_t.
36  */
37 static vout_thread_t *GetVout( libvlc_media_player_t *p_mi,
38                                libvlc_exception_t *p_exception )
39 {
40     input_thread_t *p_input = libvlc_get_input_thread( p_mi, p_exception );
41     vout_thread_t *p_vout = NULL;
42
43     if( p_input )
44     {
45         p_vout = input_GetVout( p_input );
46         if( !p_vout )
47         {
48             libvlc_exception_raise( p_exception, "No active video output" );
49         }
50         vlc_object_release( p_input );
51     }
52     return p_vout;
53 }
54
55 /**********************************************************************
56  * Exported functions
57  **********************************************************************/
58
59 void libvlc_set_fullscreen( libvlc_media_player_t *p_mi, int b_fullscreen,
60                             libvlc_exception_t *p_e )
61 {
62     /* We only work on the first vout */
63     vout_thread_t *p_vout = GetVout( p_mi, p_e );
64
65     /* GetVout will raise the exception for us */
66     if( !p_vout ) return;
67
68     var_SetBool( p_vout, "fullscreen", b_fullscreen );
69
70     vlc_object_release( p_vout );
71 }
72
73 int libvlc_get_fullscreen( libvlc_media_player_t *p_mi,
74                             libvlc_exception_t *p_e )
75 {
76     /* We only work on the first vout */
77     vout_thread_t *p_vout = GetVout( p_mi, p_e );
78     int i_ret;
79
80     /* GetVout will raise the exception for us */
81     if( !p_vout ) return 0;
82
83     i_ret = var_GetBool( p_vout, "fullscreen" );
84
85     vlc_object_release( p_vout );
86
87     return i_ret;
88 }
89
90 void libvlc_toggle_fullscreen( libvlc_media_player_t *p_mi,
91                                libvlc_exception_t *p_e )
92 {
93     /* We only work on the first vout */
94     vout_thread_t *p_vout = GetVout( p_mi, p_e );
95     bool ret;
96
97     /* GetVout will raise the exception for us */
98     if( !p_vout ) return;
99
100     ret = var_GetBool( p_vout, "fullscreen" );
101     var_SetBool( p_vout, "fullscreen", !ret );
102
103     vlc_object_release( p_vout );
104 }
105
106 void
107 libvlc_video_take_snapshot( libvlc_media_player_t *p_mi, const char *psz_filepath,
108         unsigned int i_width, unsigned int i_height, libvlc_exception_t *p_e )
109 {
110     vout_thread_t *p_vout;
111
112     /* The filepath must be not NULL */
113     if( !psz_filepath )
114     {
115         libvlc_exception_raise( p_e, "filepath is null" );
116         return;
117     }
118     /* We must have an input */
119     if( !p_mi->p_input_thread )
120     {
121         libvlc_exception_raise( p_e, "Input does not exist" );
122         return;
123     }
124
125     /* GetVout will raise the exception for us */
126     p_vout = GetVout( p_mi, p_e );
127     if( !p_vout ) return;
128
129     var_SetInteger( p_vout, "snapshot-width", i_width );
130     var_SetInteger( p_vout, "snapshot-height", i_height );
131
132     var_SetString( p_vout, "snapshot-path", psz_filepath );
133     var_SetString( p_vout, "snapshot-format", "png" );
134
135     var_TriggerCallback( p_vout, "video-snapshot" );
136     vlc_object_release( p_vout );
137 }
138
139 int libvlc_video_get_height( libvlc_media_player_t *p_mi,
140                              libvlc_exception_t *p_e )
141 {
142     int height;
143
144     vout_thread_t *p_vout = GetVout( p_mi, p_e );
145     if( !p_vout ) return 0;
146
147     height = p_vout->i_window_height;
148
149     vlc_object_release( p_vout );
150
151     return height;
152 }
153
154 int libvlc_video_get_width( libvlc_media_player_t *p_mi,
155                             libvlc_exception_t *p_e )
156 {
157     int width;
158
159     vout_thread_t *p_vout = GetVout( p_mi, p_e );
160     if( !p_vout ) return 0;
161
162     width = p_vout->i_window_width;
163
164     vlc_object_release( p_vout );
165
166     return width;
167 }
168
169 int libvlc_media_player_has_vout( libvlc_media_player_t *p_mi,
170                                      libvlc_exception_t *p_e )
171 {
172     input_thread_t *p_input_thread = libvlc_get_input_thread(p_mi, p_e);
173     bool has_vout = false;
174
175     if( p_input_thread )
176     {
177         vout_thread_t *p_vout;
178
179         p_vout = input_GetVout( p_input_thread );
180         if( p_vout )
181         {
182             has_vout = true;
183             vlc_object_release( p_vout );
184         }
185         vlc_object_release( p_input_thread );
186     }
187     return has_vout;
188 }
189
190 float libvlc_video_get_scale( libvlc_media_player_t *p_mp,
191                               libvlc_exception_t *p_e )
192 {
193     vout_thread_t *p_vout = GetVout( p_mp, p_e );
194     if( !p_vout )
195         return 0.;
196
197     float f_scale = var_GetFloat( p_vout, "scale" );
198     if( var_GetBool( p_vout, "autoscale" ) )
199         f_scale = 0.;
200     vlc_object_release( p_vout );
201     return f_scale;
202 }
203
204 void libvlc_video_set_scale( libvlc_media_player_t *p_mp, float f_scale,
205                              libvlc_exception_t *p_e )
206 {
207     vout_thread_t *p_vout = GetVout( p_mp, p_e );
208     if( !p_vout )
209         return;
210
211     if( f_scale != 0. )
212         var_SetFloat( p_vout, "scale", f_scale );
213     var_SetBool( p_vout, "autoscale", f_scale != 0. );
214     vlc_object_release( p_vout );
215 }
216
217 char *libvlc_video_get_aspect_ratio( libvlc_media_player_t *p_mi,
218                                      libvlc_exception_t *p_e )
219 {
220     char *psz_aspect = 0;
221     vout_thread_t *p_vout = GetVout( p_mi, p_e );
222
223     if( !p_vout ) return 0;
224
225     psz_aspect = var_GetNonEmptyString( p_vout, "aspect-ratio" );
226     vlc_object_release( p_vout );
227     return psz_aspect ? psz_aspect : strdup("");
228 }
229
230 void libvlc_video_set_aspect_ratio( libvlc_media_player_t *p_mi,
231                                     char *psz_aspect, libvlc_exception_t *p_e )
232 {
233     vout_thread_t *p_vout = GetVout( p_mi, p_e );
234     int i_ret = -1;
235
236     if( !p_vout ) return;
237
238     i_ret = var_SetString( p_vout, "aspect-ratio", psz_aspect );
239     if( i_ret )
240         libvlc_exception_raise( p_e,
241                         "Unexpected error while setting aspect-ratio value" );
242
243     vlc_object_release( p_vout );
244 }
245
246 int libvlc_video_get_spu( libvlc_media_player_t *p_mi,
247                           libvlc_exception_t *p_e )
248 {
249     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
250     vlc_value_t val_list;
251     vlc_value_t val;
252     int i_spu = -1;
253     int i_ret = -1;
254     int i;
255
256     if( !p_input_thread ) return -1;
257
258     i_ret = var_Get( p_input_thread, "spu-es", &val );
259     if( i_ret < 0 )
260     {
261         libvlc_exception_raise( p_e, "Getting subtitle information failed" );
262         vlc_object_release( p_input_thread );
263         return i_ret;
264     }
265
266     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
267     for( i = 0; i < val_list.p_list->i_count; i++ )
268     {
269         if( val.i_int == val_list.p_list->p_values[i].i_int )
270         {
271             i_spu = i;
272             break;
273         }
274     }
275     var_Change( p_input_thread, "spu-es", VLC_VAR_FREELIST, &val_list, NULL );
276     vlc_object_release( p_input_thread );
277     return i_spu;
278 }
279
280 int libvlc_video_get_spu_count( libvlc_media_player_t *p_mi,
281                                 libvlc_exception_t *p_e )
282 {
283     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
284     vlc_value_t val_list;
285     int i_spu_count;
286
287     if( !p_input_thread )
288         return -1;
289
290     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
291     i_spu_count = val_list.p_list->i_count;
292     var_Change( p_input_thread, "spu-es", VLC_VAR_FREELIST, &val_list, NULL );
293
294     vlc_object_release( p_input_thread );
295     return i_spu_count;
296 }
297
298 libvlc_track_description_t *
299         libvlc_video_get_spu_description( libvlc_media_player_t *p_mi,
300                                           libvlc_exception_t *p_e )
301 {
302     return libvlc_get_track_description( p_mi, "spu-es", p_e);
303 }
304
305 void libvlc_video_set_spu( libvlc_media_player_t *p_mi, int i_spu,
306                            libvlc_exception_t *p_e )
307 {
308     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
309     vlc_value_t val_list;
310     vlc_value_t newval;
311     int i_ret = -1;
312
313     if( !p_input_thread ) return;
314
315     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
316
317     if( val_list.p_list->i_count == 0 )
318     {
319         libvlc_exception_raise( p_e, "Subtitle value out of range" );
320         goto end;
321     }
322
323     if( (i_spu < 0) && (i_spu > val_list.p_list->i_count) )
324     {
325         libvlc_exception_raise( p_e, "Subtitle value out of range" );
326         goto end;
327     }
328
329     newval = val_list.p_list->p_values[i_spu];
330     i_ret = var_Set( p_input_thread, "spu-es", newval );
331     if( i_ret < 0 )
332     {
333         libvlc_exception_raise( p_e, "Setting subtitle value failed" );
334     }
335
336 end:
337     var_Change( p_input_thread, "spu-es", VLC_VAR_FREELIST, &val_list, NULL );
338     vlc_object_release( p_input_thread );
339 }
340
341 int libvlc_video_set_subtitle_file( libvlc_media_player_t *p_mi,
342                                     char *psz_subtitle,
343                                     libvlc_exception_t *p_e )
344 {
345     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
346     bool b_ret = false;
347
348     if( p_input_thread )
349     {
350         if( !input_AddSubtitle( p_input_thread, psz_subtitle, true ) )
351             b_ret = true;
352         vlc_object_release( p_input_thread );
353     }
354     return b_ret;
355 }
356
357 libvlc_track_description_t *
358         libvlc_video_get_title_description( libvlc_media_player_t *p_mi,
359                                             libvlc_exception_t * p_e )
360 {
361     return libvlc_get_track_description( p_mi, "title", p_e);
362 }
363
364 libvlc_track_description_t *
365         libvlc_video_get_chapter_description( libvlc_media_player_t *p_mi,
366                                               int i_title,
367                                               libvlc_exception_t *p_e )
368 {
369     char psz_title[12];
370     sprintf( psz_title,  "title %2i", i_title );
371     return libvlc_get_track_description( p_mi, psz_title, p_e);
372 }
373
374 char *libvlc_video_get_crop_geometry( libvlc_media_player_t *p_mi,
375                                    libvlc_exception_t *p_e )
376 {
377     char *psz_geometry = 0;
378     vout_thread_t *p_vout = GetVout( p_mi, p_e );
379
380     if( !p_vout ) return 0;
381
382     psz_geometry = var_GetNonEmptyString( p_vout, "crop" );
383     vlc_object_release( p_vout );
384     return psz_geometry ? psz_geometry : strdup("");
385 }
386
387 void libvlc_video_set_crop_geometry( libvlc_media_player_t *p_mi,
388                                     char *psz_geometry, libvlc_exception_t *p_e )
389 {
390     vout_thread_t *p_vout = GetVout( p_mi, p_e );
391     int i_ret = -1;
392
393     if( !p_vout ) return;
394
395     i_ret = var_SetString( p_vout, "crop", psz_geometry );
396     if( i_ret )
397         libvlc_exception_raise( p_e,
398                         "Unexpected error while setting crop geometry" );
399
400     vlc_object_release( p_vout );
401 }
402
403 int libvlc_video_get_teletext( libvlc_media_player_t *p_mi,
404                                libvlc_exception_t *p_e )
405 {
406     vout_thread_t *p_vout = GetVout( p_mi, p_e );
407     vlc_object_t *p_vbi;
408     int i_ret = -1;
409
410     if( !p_vout ) return i_ret;
411
412     p_vbi = (vlc_object_t *) vlc_object_find_name( p_vout, "zvbi",
413                                                    FIND_CHILD );
414     if( p_vbi )
415     {
416         i_ret = var_GetInteger( p_vbi, "vbi-page" );
417         vlc_object_release( p_vbi );
418     }
419
420     vlc_object_release( p_vout );
421     return i_ret;
422 }
423
424 void libvlc_video_set_teletext( libvlc_media_player_t *p_mi, int i_page,
425                                 libvlc_exception_t *p_e )
426 {
427     vout_thread_t *p_vout = GetVout( p_mi, p_e );
428     vlc_object_t *p_vbi;
429     int i_ret = -1;
430
431     if( !p_vout ) return;
432
433     p_vbi = (vlc_object_t *) vlc_object_find_name( p_vout, "zvbi",
434                                                    FIND_CHILD );
435     if( p_vbi )
436     {
437         i_ret = var_SetInteger( p_vbi, "vbi-page", i_page );
438         vlc_object_release( p_vbi );
439         if( i_ret )
440             libvlc_exception_raise( p_e,
441                             "Unexpected error while setting teletext page" );
442     }
443     vlc_object_release( p_vout );
444 }
445
446 void libvlc_toggle_teletext( libvlc_media_player_t *p_mi,
447                              libvlc_exception_t *p_e )
448 {
449     input_thread_t *p_input_thread;
450     vlc_object_t *p_vbi;
451     int i_ret;
452
453     p_input_thread = libvlc_get_input_thread(p_mi, p_e);
454     if( !p_input_thread ) return;
455
456     if( var_CountChoices( p_input_thread, "teletext-es" ) <= 0 )
457     {
458         vlc_object_release( p_input_thread );
459         return;
460     }
461     const bool b_selected = var_GetInteger( p_input_thread, "teletext-es" ) >= 0;
462
463     p_vbi = (vlc_object_t *)vlc_object_find_name( p_input_thread, "zvbi",
464                                                   FIND_CHILD );
465     if( p_vbi )
466     {
467         if( b_selected )
468         {
469             /* FIXME Gni, why that ? */
470             i_ret = var_SetInteger( p_vbi, "vbi-page",
471                                     var_GetInteger( p_vbi, "vbi-page" ) );
472             if( i_ret )
473                 libvlc_exception_raise( p_e,
474                                 "Unexpected error while setting teletext page" );
475         }
476         else
477         {
478             /* FIXME Gni^2 */
479             i_ret = var_SetBool( p_vbi, "vbi-opaque",
480                                  !var_GetBool( p_vbi, "vbi-opaque" ) );
481             if( i_ret )
482                 libvlc_exception_raise( p_e,
483                                 "Unexpected error while setting teletext transparency" );
484         }
485         vlc_object_release( p_vbi );
486     }
487     else if( b_selected )
488     {
489         var_SetInteger( p_input_thread, "spu-es", -1 );
490     }
491     else
492     {
493         vlc_value_t list;
494         if( !var_Change( p_input_thread, "teletext-es", VLC_VAR_GETLIST, &list, NULL ) )
495         {
496             if( list.p_list->i_count > 0 )
497                 var_SetInteger( p_input_thread, "spu-es", list.p_list->p_values[0].i_int );
498
499             var_Change( p_input_thread, "teletext-es", VLC_VAR_FREELIST, &list, NULL );
500         }
501     }
502     vlc_object_release( p_input_thread );
503 }
504
505 int libvlc_video_get_track_count( libvlc_media_player_t *p_mi,
506                                   libvlc_exception_t *p_e )
507 {
508     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
509     vlc_value_t val_list;
510     int i_track_count;
511
512     if( !p_input_thread )
513         return -1;
514
515     var_Change( p_input_thread, "video-es", VLC_VAR_GETCHOICES, &val_list, NULL );
516     i_track_count = val_list.p_list->i_count;
517     var_Change( p_input_thread, "video-es", VLC_VAR_FREELIST, &val_list, NULL );
518
519     vlc_object_release( p_input_thread );
520     return i_track_count;
521 }
522
523 libvlc_track_description_t *
524         libvlc_video_get_track_description( libvlc_media_player_t *p_mi,
525                                             libvlc_exception_t *p_e )
526 {
527     return libvlc_get_track_description( p_mi, "video-es", p_e);
528 }
529
530 int libvlc_video_get_track( libvlc_media_player_t *p_mi,
531                             libvlc_exception_t *p_e )
532 {
533     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
534     vlc_value_t val_list;
535     vlc_value_t val;
536     int i_track = -1;
537     int i_ret = -1;
538     int i;
539
540     if( !p_input_thread )
541         return -1;
542
543     i_ret = var_Get( p_input_thread, "video-es", &val );
544     if( i_ret < 0 )
545     {
546         libvlc_exception_raise( p_e, "Getting Video track information failed" );
547         vlc_object_release( p_input_thread );
548         return i_ret;
549     }
550
551     var_Change( p_input_thread, "video-es", VLC_VAR_GETCHOICES, &val_list, NULL );
552     for( i = 0; i < val_list.p_list->i_count; i++ )
553     {
554         if( val_list.p_list->p_values[i].i_int == val.i_int )
555         {
556             i_track = i;
557             break;
558         }
559     }
560     var_Change( p_input_thread, "video-es", VLC_VAR_FREELIST, &val_list, NULL );
561     vlc_object_release( p_input_thread );
562     return i_track;
563 }
564
565 void libvlc_video_set_track( libvlc_media_player_t *p_mi, int i_track,
566                              libvlc_exception_t *p_e )
567 {
568     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
569     vlc_value_t val_list;
570     int i_ret = -1;
571     int i;
572
573     if( !p_input_thread )
574         return;
575
576     var_Change( p_input_thread, "video-es", VLC_VAR_GETCHOICES, &val_list, NULL );
577     for( i = 0; i < val_list.p_list->i_count; i++ )
578     {
579         vlc_value_t val = val_list.p_list->p_values[i];
580         if( i_track == val.i_int )
581         {
582             i_ret = var_Set( p_input_thread, "audio-es", val );
583             if( i_ret < 0 )
584                 libvlc_exception_raise( p_e, "Setting video track failed" );
585             goto end;
586         }
587     }
588     libvlc_exception_raise( p_e, "Video track out of range" );
589
590 end:
591     var_Change( p_input_thread, "video-es", VLC_VAR_FREELIST, &val_list, NULL );
592     vlc_object_release( p_input_thread );
593 }