]> 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_thread = libvlc_get_input_thread( p_mi, p_exception );
41     vout_thread_t *p_vout = NULL;
42
43     if( p_input_thread )
44     {
45         p_vout = input_GetVout( p_input_thread );
46         if( !p_vout )
47         {
48             libvlc_exception_raise( p_exception, "No active video output" );
49         }
50         vlc_object_release( p_input_thread );
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 = GetVout( p_mi, p_e );
111     input_thread_t *p_input_thread;
112
113     /* GetVout will raise the exception for us */
114     if( !p_vout ) return;
115
116     if( !psz_filepath )
117     {
118         libvlc_exception_raise( p_e, "filepath is null" );
119         return;
120     }
121
122     var_SetInteger( p_vout, "snapshot-width", i_width );
123     var_SetInteger( p_vout, "snapshot-height", i_height );
124
125     p_input_thread = p_mi->p_input_thread;
126     if( !p_mi->p_input_thread )
127     {
128         libvlc_exception_raise( p_e, "Input does not exist" );
129         return;
130     }
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 char *libvlc_video_get_aspect_ratio( libvlc_media_player_t *p_mi,
191                                      libvlc_exception_t *p_e )
192 {
193     char *psz_aspect = 0;
194     vout_thread_t *p_vout = GetVout( p_mi, p_e );
195
196     if( !p_vout ) return 0;
197
198     psz_aspect = var_GetNonEmptyString( p_vout, "aspect-ratio" );
199     vlc_object_release( p_vout );
200     return psz_aspect ? psz_aspect : strdup("");
201 }
202
203 void libvlc_video_set_aspect_ratio( libvlc_media_player_t *p_mi,
204                                     char *psz_aspect, libvlc_exception_t *p_e )
205 {
206     vout_thread_t *p_vout = GetVout( p_mi, p_e );
207     int i_ret = -1;
208
209     if( !p_vout ) return;
210
211     i_ret = var_SetString( p_vout, "aspect-ratio", psz_aspect );
212     if( i_ret )
213         libvlc_exception_raise( p_e,
214                         "Unexpected error while setting aspect-ratio value" );
215
216     vlc_object_release( p_vout );
217 }
218
219 int libvlc_video_get_spu( libvlc_media_player_t *p_mi,
220                           libvlc_exception_t *p_e )
221 {
222     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
223     vlc_value_t val_list;
224     vlc_value_t val;
225     int i_spu = -1;
226     int i_ret = -1;
227     int i;
228
229     if( !p_input_thread ) return -1;
230
231     i_ret = var_Get( p_input_thread, "spu-es", &val );
232     if( i_ret < 0 )
233     {
234         libvlc_exception_raise( p_e, "Getting subtitle information failed" );
235         vlc_object_release( p_input_thread );
236         return i_ret;
237     }
238
239     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
240     for( i = 0; i < val_list.p_list->i_count; i++ )
241     {
242         vlc_value_t spu_val = val_list.p_list->p_values[i];
243         if( val.i_int == spu_val.i_int )
244         {
245             i_spu = i;
246             break;
247         }
248     }
249     vlc_object_release( p_input_thread );
250     return i_spu;
251 }
252
253 int libvlc_video_get_spu_count( libvlc_media_player_t *p_mi,
254                                 libvlc_exception_t *p_e )
255 {
256     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
257     vlc_value_t val_list;
258
259     if( !p_input_thread )
260         return -1;
261
262     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
263     vlc_object_release( p_input_thread );
264     return val_list.p_list->i_count;
265 }
266
267 libvlc_track_description_t *
268         libvlc_video_get_spu_description( libvlc_media_player_t *p_mi,
269                                           libvlc_exception_t *p_e )
270 {
271     return libvlc_get_track_description( p_mi, "spu-es", p_e);
272 }
273
274 void libvlc_video_set_spu( libvlc_media_player_t *p_mi, int i_spu,
275                            libvlc_exception_t *p_e )
276 {
277     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
278     vlc_value_t val_list;
279     vlc_value_t newval;
280     int i_ret = -1;
281
282     if( !p_input_thread ) return;
283
284     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
285
286     if( val_list.p_list->i_count == 0 )
287     {
288         libvlc_exception_raise( p_e, "Subtitle value out of range" );
289         vlc_object_release( p_input_thread );
290         return;
291     }
292
293     if( (i_spu < 0) && (i_spu > val_list.p_list->i_count) )
294     {
295         libvlc_exception_raise( p_e, "Subtitle value out of range" );
296         vlc_object_release( p_input_thread );
297         return;
298     }
299
300     newval = val_list.p_list->p_values[i_spu];
301     i_ret = var_Set( p_input_thread, "spu-es", newval );
302     if( i_ret < 0 )
303     {
304         libvlc_exception_raise( p_e, "Setting subtitle value failed" );
305     }
306     vlc_object_release( p_input_thread );
307 }
308
309 int libvlc_video_set_subtitle_file( libvlc_media_player_t *p_mi,
310                                     char *psz_subtitle,
311                                     libvlc_exception_t *p_e )
312 {
313     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
314     bool b_ret = false;
315
316     if( p_input_thread )
317     {
318         if( !input_AddSubtitle( p_input_thread, psz_subtitle, true ) )
319             b_ret = true;
320         vlc_object_release( p_input_thread );
321     }
322     return b_ret;
323 }
324
325 libvlc_track_description_t *
326         libvlc_video_get_title_description( libvlc_media_player_t *p_mi,
327                                             libvlc_exception_t * p_e )
328 {
329     return libvlc_get_track_description( p_mi, "title", p_e);
330 }
331
332 libvlc_track_description_t *
333         libvlc_video_get_chapter_description( libvlc_media_player_t *p_mi,
334                                               int i_title,
335                                               libvlc_exception_t *p_e )
336 {
337     char psz_title[12];
338     sprintf( psz_title,  "title %2i", i_title );
339     return libvlc_get_track_description( p_mi, psz_title, p_e);
340 }
341
342 char *libvlc_video_get_crop_geometry( libvlc_media_player_t *p_mi,
343                                    libvlc_exception_t *p_e )
344 {
345     char *psz_geometry = 0;
346     vout_thread_t *p_vout = GetVout( p_mi, p_e );
347
348     if( !p_vout ) return 0;
349
350     psz_geometry = var_GetNonEmptyString( p_vout, "crop" );
351     vlc_object_release( p_vout );
352     return psz_geometry ? psz_geometry : strdup("");
353 }
354
355 void libvlc_video_set_crop_geometry( libvlc_media_player_t *p_mi,
356                                     char *psz_geometry, libvlc_exception_t *p_e )
357 {
358     vout_thread_t *p_vout = GetVout( p_mi, p_e );
359     int i_ret = -1;
360
361     if( !p_vout ) return;
362
363     i_ret = var_SetString( p_vout, "crop", psz_geometry );
364     if( i_ret )
365         libvlc_exception_raise( p_e,
366                         "Unexpected error while setting crop geometry" );
367
368     vlc_object_release( p_vout );
369 }
370
371 int libvlc_video_get_teletext( libvlc_media_player_t *p_mi,
372                                libvlc_exception_t *p_e )
373 {
374     vout_thread_t *p_vout = GetVout( p_mi, p_e );
375     vlc_object_t *p_vbi;
376     int i_ret = -1;
377
378     if( !p_vout ) return i_ret;
379
380     p_vbi = (vlc_object_t *) vlc_object_find_name( p_vout, "zvbi",
381                                                    FIND_CHILD );
382     if( p_vbi )
383     {
384         i_ret = var_GetInteger( p_vbi, "vbi-page" );
385         vlc_object_release( p_vbi );
386     }
387
388     vlc_object_release( p_vout );
389     return i_ret;
390 }
391
392 void libvlc_video_set_teletext( libvlc_media_player_t *p_mi, int i_page,
393                                 libvlc_exception_t *p_e )
394 {
395     vout_thread_t *p_vout = GetVout( p_mi, p_e );
396     vlc_object_t *p_vbi;
397     int i_ret = -1;
398
399     if( !p_vout ) return;
400
401     p_vbi = (vlc_object_t *) vlc_object_find_name( p_vout, "zvbi",
402                                                    FIND_CHILD );
403     if( p_vbi )
404     {
405         i_ret = var_SetInteger( p_vbi, "vbi-page", i_page );
406         vlc_object_release( p_vbi );
407         if( i_ret )
408             libvlc_exception_raise( p_e,
409                             "Unexpected error while setting teletext page" );
410     }
411     vlc_object_release( p_vout );
412 }
413
414 void libvlc_toggle_teletext( libvlc_media_player_t *p_mi,
415                              libvlc_exception_t *p_e )
416 {
417     input_thread_t *p_input_thread;
418     vlc_object_t *p_vbi;
419     int i_ret;
420
421     p_input_thread = libvlc_get_input_thread(p_mi, p_e);
422     if( !p_input_thread ) return;
423
424     if( var_CountChoices( p_input_thread, "teletext-es" ) <= 0 )
425     {
426         vlc_object_release( p_input_thread );
427         return;
428     }
429     const bool b_selected = var_GetInteger( p_input_thread, "teletext-es" ) >= 0;
430
431     p_vbi = (vlc_object_t *)vlc_object_find_name( p_input_thread, "zvbi",
432                                                   FIND_CHILD );
433     if( p_vbi )
434     {
435         if( b_selected )
436         {
437             /* FIXME Gni, why that ? */
438             i_ret = var_SetInteger( p_vbi, "vbi-page",
439                                     var_GetInteger( p_vbi, "vbi-page" ) );
440             if( i_ret )
441                 libvlc_exception_raise( p_e,
442                                 "Unexpected error while setting teletext page" );
443         }
444         else
445         {
446             /* FIXME Gni^2 */
447             i_ret = var_SetBool( p_vbi, "vbi-opaque",
448                                  !var_GetBool( p_vbi, "vbi-opaque" ) );
449             if( i_ret )
450                 libvlc_exception_raise( p_e,
451                                 "Unexpected error while setting teletext transparency" );
452         }
453         vlc_object_release( p_vbi );
454     }
455     else if( b_selected )
456     {
457         var_SetInteger( p_input_thread, "spu-es", -1 );
458     }
459     else
460     {
461         vlc_value_t list;
462         if( !var_Change( p_input_thread, "teletext-es", VLC_VAR_GETLIST, &list, NULL ) )
463         {
464             if( list.p_list->i_count > 0 )
465                 var_SetInteger( p_input_thread, "spu-es", list.p_list->p_values[0].i_int );
466
467             var_Change( p_input_thread, "teletext-es", VLC_VAR_FREELIST, &list, NULL );
468         }
469     }
470     vlc_object_release( p_input_thread );
471 }
472
473 int libvlc_video_get_track_count( libvlc_media_player_t *p_mi,
474                                   libvlc_exception_t *p_e )
475 {
476     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
477     vlc_value_t val_list;
478
479     if( !p_input_thread )
480         return -1;
481
482     var_Change( p_input_thread, "video-es", VLC_VAR_GETCHOICES, &val_list, NULL );
483     vlc_object_release( p_input_thread );
484     return val_list.p_list->i_count;
485 }
486
487 libvlc_track_description_t *
488         libvlc_video_get_track_description( libvlc_media_player_t *p_mi,
489                                             libvlc_exception_t *p_e )
490 {
491     return libvlc_get_track_description( p_mi, "video-es", p_e);
492 }
493
494 int libvlc_video_get_track( libvlc_media_player_t *p_mi,
495                             libvlc_exception_t *p_e )
496 {
497     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
498     vlc_value_t val_list;
499     vlc_value_t val;
500     int i_track = -1;
501     int i_ret = -1;
502     int i;
503
504     if( !p_input_thread )
505         return -1;
506
507     i_ret = var_Get( p_input_thread, "video-es", &val );
508     if( i_ret < 0 )
509     {
510         libvlc_exception_raise( p_e, "Getting Video track information failed" );
511         vlc_object_release( p_input_thread );
512         return i_ret;
513     }
514
515     var_Change( p_input_thread, "video-es", VLC_VAR_GETCHOICES, &val_list, NULL );
516     for( i = 0; i < val_list.p_list->i_count; i++ )
517     {
518         vlc_value_t track_val = val_list.p_list->p_values[i];
519         if( track_val.i_int == val.i_int )
520         {
521             i_track = i;
522             break;
523        }
524     }
525     vlc_object_release( p_input_thread );
526     return i_track;
527 }
528
529 void libvlc_video_set_track( libvlc_media_player_t *p_mi, int i_track,
530                              libvlc_exception_t *p_e )
531 {
532     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
533     vlc_value_t val_list;
534     int i_ret = -1;
535     int i;
536
537     if( !p_input_thread )
538         return;
539
540     var_Change( p_input_thread, "video-es", VLC_VAR_GETCHOICES, &val_list, NULL );
541     for( i = 0; i < val_list.p_list->i_count; i++ )
542     {
543         vlc_value_t val = val_list.p_list->p_values[i];
544         if( i_track == val.i_int )
545         {
546             i_ret = var_Set( p_input_thread, "audio-es", val );
547             if( i_ret < 0 )
548                 libvlc_exception_raise( p_e, "Setting video track failed" );
549             vlc_object_release( p_input_thread );
550             return;
551         }
552     }
553     libvlc_exception_raise( p_e, "Video track out of range" );
554     vlc_object_release( p_input_thread );
555 }