]> git.sesse.net Git - vlc/blob - src/control/video.c
Fix bug in vlc.audio.track and add new properties vlc.video.subtitle
[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: core.c 14187 2006-02-07 16:37:40Z courmisch $
7  *
8  * Authors: Cl�ent Stenac <zorglub@videolan.org>
9  *          Filippo Carone <littlejohn@videolan.org>
10  *          Jean-Paul Saman <jpsaman _at_ m2x _dot_ nl>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #include "libvlc_internal.h"
28 #include <vlc/libvlc.h>
29 #include <vlc_input.h>
30 #include <vlc_vout.h>
31
32 /*
33  * Remember to release the returned input_thread_t since it is locked at
34  * the end of this function.
35  */
36 static input_thread_t *GetInputThread( libvlc_input_t *p_input,
37                                libvlc_exception_t *p_exception )
38 {
39     input_thread_t *p_input_thread;
40
41     if( !p_input )
42     {
43         libvlc_exception_raise( p_exception, "Input is NULL" );
44         return NULL;
45     }
46
47     p_input_thread = (input_thread_t*)vlc_object_get(
48                                  p_input->p_instance->p_libvlc_int,
49                                  p_input->i_input_id );
50     if( !p_input_thread )
51     {
52         libvlc_exception_raise( p_exception, "Input does not exist" );
53         return NULL;
54     }
55
56     return p_input_thread;;
57 }
58
59 /*
60  * Remember to release the returned vout_thread_t since it is locked at
61  * the end of this function.
62  */
63 static vout_thread_t *GetVout( libvlc_input_t *p_input,
64                                libvlc_exception_t *p_exception )
65 {
66     input_thread_t *p_input_thread = GetInputThread(p_input, p_exception);
67     vout_thread_t *p_vout = NULL;
68
69     if( p_input_thread )
70     {
71         p_vout = vlc_object_find( p_input_thread, VLC_OBJECT_VOUT, FIND_CHILD );
72         if( !p_vout )
73         {
74             libvlc_exception_raise( p_exception, "No active video output" );
75         }
76         vlc_object_release( p_input_thread );
77     }
78     return p_vout;
79 }
80
81 /**********************************************************************
82  * Exported functions
83  **********************************************************************/
84
85 void libvlc_set_fullscreen( libvlc_input_t *p_input, int b_fullscreen,
86                             libvlc_exception_t *p_e )
87 {
88     /* We only work on the first vout */
89     vout_thread_t *p_vout1 = GetVout( p_input, p_e );
90     vlc_value_t val; int i_ret;
91
92     /* GetVout will raise the exception for us */
93     if( !p_vout1 )
94     {
95         return;
96     }
97
98     if( b_fullscreen ) val.b_bool = VLC_TRUE;
99     else               val.b_bool = VLC_FALSE;
100
101     i_ret = var_Set( p_vout1, "fullscreen", val );
102     if( i_ret )
103         libvlc_exception_raise( p_e,
104                         "Unexpected error while setting fullscreen value" );
105
106     vlc_object_release( p_vout1 );
107 }
108
109 int libvlc_get_fullscreen( libvlc_input_t *p_input,
110                             libvlc_exception_t *p_e )
111 {
112     /* We only work on the first vout */
113     vout_thread_t *p_vout1 = GetVout( p_input, p_e );
114     vlc_value_t val; int i_ret;
115
116     /* GetVout will raise the exception for us */
117     if( !p_vout1 )
118         return 0;
119
120     i_ret = var_Get( p_vout1, "fullscreen", &val );
121     if( i_ret )
122         libvlc_exception_raise( p_e,
123                         "Unexpected error while looking up fullscreen value" );
124
125     return val.b_bool == VLC_TRUE ? 1 : 0;
126 }
127
128 void libvlc_toggle_fullscreen( libvlc_input_t *p_input,
129                                libvlc_exception_t *p_e )
130 {
131     /* We only work on the first vout */
132     vout_thread_t *p_vout1 = GetVout( p_input, p_e );
133     vlc_value_t val; int i_ret;
134
135     /* GetVout will raise the exception for us */
136     if( !p_vout1 )
137         return;
138
139     i_ret = var_Get( p_vout1, "fullscreen", &val );
140     if( i_ret )
141         libvlc_exception_raise( p_e,
142                         "Unexpected error while looking up fullscreen value" );
143
144     val.b_bool = !val.b_bool;
145     i_ret = var_Set( p_vout1, "fullscreen", val );
146     if( i_ret )
147         libvlc_exception_raise( p_e,
148                         "Unexpected error while setting fullscreen value" );
149
150     vlc_object_release( p_vout1 );
151 }
152
153 void
154 libvlc_video_take_snapshot( libvlc_input_t *p_input, char *psz_filepath,
155                        libvlc_exception_t *p_e )
156 {
157     vout_thread_t *p_vout = GetVout( p_input, p_e );
158     input_thread_t *p_input_thread;
159
160     char path[256];
161
162     /* GetVout will raise the exception for us */
163     if( !p_vout )
164     {
165         return;
166     }
167
168     p_input_thread = (input_thread_t*)vlc_object_get(
169                                  p_input->p_instance->p_libvlc_int,
170                                  p_input->i_input_id );
171     if( !p_input_thread )
172     {
173         libvlc_exception_raise( p_e, "Input does not exist" );
174         return;
175     }
176
177     snprintf( path, 255, "%s", psz_filepath );
178     var_SetString( p_vout, "snapshot-path", path );
179     var_SetString( p_vout, "snapshot-format", "png" );
180
181     vout_Control( p_vout, VOUT_SNAPSHOT );
182     vlc_object_release( p_vout );
183     vlc_object_release( p_input_thread );
184 }
185
186 int libvlc_video_get_height( libvlc_input_t *p_input,
187                              libvlc_exception_t *p_e ) 
188 {
189     vout_thread_t *p_vout1 = GetVout( p_input, p_e );
190     if( !p_vout1 )
191         return 0;
192
193     vlc_object_release( p_vout1 );
194
195     return p_vout1->i_window_height;
196 }
197
198 int libvlc_video_get_width( libvlc_input_t *p_input,
199                             libvlc_exception_t *p_e ) 
200 {
201     vout_thread_t *p_vout1 = GetVout( p_input, p_e );
202     if( !p_vout1 )
203         return 0;
204
205     vlc_object_release( p_vout1 );
206
207     return p_vout1->i_window_width;
208 }
209
210 vlc_bool_t libvlc_input_has_vout( libvlc_input_t *p_input,
211                                   libvlc_exception_t *p_e )
212 {
213     input_thread_t *p_input_thread = GetInputThread(p_input, p_e);
214     vlc_bool_t has_vout = VLC_FALSE;
215
216     if( p_input_thread )
217     {
218         vout_thread_t *p_vout;
219
220         p_vout = vlc_object_find( p_input_thread, VLC_OBJECT_VOUT, FIND_CHILD );
221         if( p_vout )
222         {
223             has_vout = VLC_TRUE;
224             vlc_object_release( p_vout );
225         }
226         vlc_object_release( p_input_thread );
227     }
228     return has_vout;
229 }
230
231 int libvlc_video_reparent( libvlc_input_t *p_input, libvlc_drawable_t d,
232                            libvlc_exception_t *p_e )
233 {
234     vout_thread_t *p_vout = GetVout( p_input, p_e );
235
236     if( p_vout )
237     {
238         vout_Control( p_vout , VOUT_REPARENT, d);
239         vlc_object_release( p_vout );
240     }
241     return 0;
242 }
243
244 void libvlc_video_resize( libvlc_input_t *p_input, int width, int height, libvlc_exception_t *p_e )
245 {
246     vout_thread_t *p_vout = GetVout( p_input, p_e );
247     if( p_vout )
248     {
249         vout_Control( p_vout, VOUT_SET_SIZE, width, height );
250         vlc_object_release( p_vout );
251     }
252 }
253
254 /* global video settings */
255
256 void libvlc_video_set_parent( libvlc_instance_t *p_instance, libvlc_drawable_t d,
257                               libvlc_exception_t *p_e )
258 {
259     /* set as default for future vout instances */
260     var_SetInteger(p_instance->p_libvlc_int, "drawable", (int)d);
261
262     if( libvlc_playlist_isplaying(p_instance, p_e) )
263     {
264         libvlc_input_t *p_input = libvlc_playlist_get_input(p_instance, p_e);
265         if( p_input )
266         {
267             vout_thread_t *p_vout = GetVout( p_input, p_e );
268             if( p_vout )
269             {
270                 /* tell running vout to re-parent */
271                 vout_Control( p_vout , VOUT_REPARENT, d);
272                 vlc_object_release( p_vout );
273             }
274             libvlc_input_free(p_input);
275         }
276     }
277 }
278
279 libvlc_drawable_t libvlc_video_get_parent( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
280 {
281     libvlc_drawable_t result;
282     
283     result = var_GetInteger( p_instance->p_libvlc_int, "drawable" );
284     
285     return result;
286 }
287
288
289 void libvlc_video_set_size( libvlc_instance_t *p_instance, int width, int height,
290                            libvlc_exception_t *p_e )
291 {
292     /* set as default for future vout instances */
293     config_PutInt(p_instance->p_libvlc_int, "width", width);
294     config_PutInt(p_instance->p_libvlc_int, "height", height);
295
296     if( libvlc_playlist_isplaying(p_instance, p_e) )
297     {
298         libvlc_input_t *p_input = libvlc_playlist_get_input(p_instance, p_e);
299         if( p_input )
300         {
301             vout_thread_t *p_vout = GetVout( p_input, p_e );
302             if( p_vout )
303             {
304                 /* tell running vout to re-size */
305                 vout_Control( p_vout , VOUT_SET_SIZE, width, height);
306                 vlc_object_release( p_vout );
307             }
308             libvlc_input_free(p_input);
309         }
310     }
311 }
312
313 void libvlc_video_set_viewport( libvlc_instance_t *p_instance,
314                             const libvlc_rectangle_t *view, const libvlc_rectangle_t *clip,
315                            libvlc_exception_t *p_e )
316 {
317     if( NULL == view )
318     {
319         libvlc_exception_raise( p_e, "viewport is NULL" );
320     }
321
322     /* if clip is NULL, then use view rectangle as clip */
323     if( NULL == clip )
324         clip = view;
325
326     /* set as default for future vout instances */
327     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-top", view->top );
328     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-left", view->left );
329     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-bottom", view->bottom );
330     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-right", view->right );
331     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-top", clip->top );
332     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-left", clip->left );
333     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-bottom", clip->bottom );
334     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-right", clip->right );
335
336     if( libvlc_playlist_isplaying(p_instance, p_e) )
337     {
338         libvlc_input_t *p_input = libvlc_playlist_get_input(p_instance, p_e);
339         if( p_input )
340         {
341             vout_thread_t *p_vout = GetVout( p_input, p_e );
342             if( p_vout )
343             {
344                 /* change viewport for running vout */
345                 vout_Control( p_vout , VOUT_SET_VIEWPORT,
346                                    view->top, view->left, view->bottom, view->right,
347                                    clip->top, clip->left, clip->bottom, clip->right );
348                 vlc_object_release( p_vout );
349             }
350             libvlc_input_free(p_input);
351         }
352     }
353 }
354
355 char *libvlc_video_get_aspect_ratio( libvlc_input_t *p_input,
356                                      libvlc_exception_t *p_e )
357 {
358     char *psz_aspect = 0;
359     vout_thread_t *p_vout = GetVout( p_input, p_e );
360
361     if( !p_vout )
362         return 0;
363
364     psz_aspect = var_GetString( p_vout, "aspect-ratio" );
365     vlc_object_release( p_vout );
366     return psz_aspect;
367 }
368
369 void libvlc_video_set_aspect_ratio( libvlc_input_t *p_input,
370                                     char *psz_aspect, libvlc_exception_t *p_e )
371 {
372     vout_thread_t *p_vout = GetVout( p_input, p_e );
373     int i_ret = -1;
374
375     if( !p_vout )
376         return;
377
378     i_ret = var_SetString( p_vout, "aspect-ratio", psz_aspect );
379     if( i_ret )
380         libvlc_exception_raise( p_e,
381                         "Unexpected error while setting aspect-ratio value" );
382
383     vlc_object_release( p_vout );
384 }
385
386 int libvlc_video_get_spu( libvlc_input_t *p_input,
387                           libvlc_exception_t *p_e )
388 {
389     input_thread_t *p_input_thread = GetInputThread( p_input, p_e );
390     vlc_value_t val_list;
391     vlc_value_t val;
392     int i_spu = -1;
393     int i_ret = -1;
394     int i;
395
396     if( !p_input_thread )
397         return -1;
398
399     i_ret = var_Get( p_input_thread, "spu-es", &val );
400     if( i_ret < 0 )
401     {
402         libvlc_exception_raise( p_e, "Getting subtitle information failed" );
403         vlc_object_release( p_input_thread );
404         return i_ret;
405     }
406
407     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
408     for( i = 0; i < val_list.p_list->i_count; i++ )
409     {
410         vlc_value_t spu_val = val_list.p_list->p_values[i];
411         if( val.i_int == spu_val.i_int )
412         {
413             i_spu = i;
414             break;
415         }
416     }
417     vlc_object_release( p_input_thread );
418     return i_spu;
419 }
420
421 void libvlc_video_set_spu( libvlc_input_t *p_input, int i_spu,
422                            libvlc_exception_t *p_e )
423 {
424     input_thread_t *p_input_thread = GetInputThread( p_input, p_e );
425     vlc_value_t val_list;
426     int i_ret = -1;
427     int i;
428
429     if( !p_input_thread )
430         return;
431
432     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
433     for( i = 0; i < val_list.p_list->i_count; i++ )
434     {
435         vlc_value_t val = val_list.p_list->p_values[i];
436         if( i_spu == i )
437         {
438             vlc_value_t new_val;
439
440             new_val.i_int = val.i_int;
441             i_ret = var_Set( p_input_thread, "spu-es", new_val );
442             if( i_ret < 0 )
443             {
444                 libvlc_exception_raise( p_e, "Setting subtitle value failed" );
445             }
446             vlc_object_release( p_input_thread );
447             return;
448         }
449     }
450     libvlc_exception_raise( p_e, "Subtitle value out of range" );
451     vlc_object_release( p_input_thread );
452 }
453
454 char *libvlc_video_get_crop_geometry( libvlc_input_t *p_input,
455                                    libvlc_exception_t *p_e )
456 {
457     char *psz_geometry = 0;
458     vout_thread_t *p_vout = GetVout( p_input, p_e );
459
460     if( !p_vout )
461         return 0;
462
463     psz_geometry = var_GetString( p_vout, "crop" );
464     vlc_object_release( p_vout );
465     return psz_geometry;
466 }
467
468 void libvlc_video_set_crop_geometry( libvlc_input_t *p_input,
469                                     char *psz_geometry, libvlc_exception_t *p_e )
470 {
471     vout_thread_t *p_vout = GetVout( p_input, p_e );
472     int i_ret = -1;
473
474     if( !p_vout )
475         return;
476
477     i_ret = var_SetString( p_vout, "crop", psz_geometry );
478     if( i_ret )
479         libvlc_exception_raise( p_e,
480                         "Unexpected error while setting crop geometry" );
481
482     vlc_object_release( p_vout );
483 }
484
485 int libvlc_video_destroy( libvlc_input_t *p_input,
486                           libvlc_exception_t *p_e )
487 {
488     vout_thread_t *p_vout = GetVout( p_input, p_e );
489     vlc_object_detach( p_vout ); 
490     vlc_object_release( p_vout );
491     vout_Destroy( p_vout );
492
493     return 0;
494 }