]> git.sesse.net Git - vlc/blob - src/control/video.c
libvlc_video_set_parent: Use libvlc_media_player_set_drawable().
[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�ent 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 = vlc_object_find( p_input_thread, VLC_OBJECT_VOUT, FIND_CHILD );
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, 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     vout_Control( p_vout, VOUT_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 = vlc_object_find( p_input_thread, VLC_OBJECT_VOUT, FIND_CHILD );
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 int libvlc_video_reparent( libvlc_media_player_t *p_mi, libvlc_drawable_t d,
191                            libvlc_exception_t *p_e )
192 {
193     vout_thread_t *p_vout = GetVout( p_mi, p_e );
194
195     if( p_vout )
196     {
197         vout_Control( p_vout , VOUT_REPARENT, d);
198         vlc_object_release( p_vout );
199     }
200     return 0;
201 }
202
203 void libvlc_video_resize( libvlc_media_player_t *p_mi, int width, int height, libvlc_exception_t *p_e )
204 {
205     vout_thread_t *p_vout = GetVout( p_mi, p_e );
206     if( p_vout )
207     {
208         vout_Control( p_vout, VOUT_SET_SIZE, width, height );
209         vlc_object_release( p_vout );
210     }
211 }
212
213 void libvlc_video_redraw_rectangle( libvlc_media_player_t *p_mi,
214                            const libvlc_rectangle_t *area,
215                            libvlc_exception_t *p_e )
216 {
217     if( (NULL != area)
218      && ((area->bottom - area->top) > 0)
219      && ((area->right - area->left) > 0) )
220     {
221         vout_thread_t *p_vout = GetVout( p_mi, p_e );
222         if( p_vout )
223         {
224             /* tell running vout to redraw area */
225             vout_Control( p_vout , VOUT_REDRAW_RECT,
226                                area->top, area->left, area->bottom, area->right );
227             vlc_object_release( p_vout );
228         }
229     }
230 }
231
232 /* global video settings */
233
234 void libvlc_video_set_parent( libvlc_instance_t *p_instance, libvlc_drawable_t d,
235                               libvlc_exception_t *p_e )
236 {
237     /* set as default for future vout instances */
238     var_SetInteger(p_instance->p_libvlc_int, "drawable", (int)d);
239
240     libvlc_media_player_t *p_mi = libvlc_playlist_get_media_player(p_instance, p_e);
241     if( p_mi )
242     {
243         libvlc_media_player_set_drawable( p_mi, d, p_e );
244         libvlc_media_player_release(p_mi);
245     }
246 }
247
248 libvlc_drawable_t libvlc_video_get_parent( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
249 {
250     VLC_UNUSED(p_e);
251
252     libvlc_drawable_t result;
253  
254     result = var_GetInteger( p_instance->p_libvlc_int, "drawable" );
255  
256     return result;
257 }
258
259
260 void libvlc_video_set_size( libvlc_instance_t *p_instance, int width, int height,
261                            libvlc_exception_t *p_e )
262 {
263     /* set as default for future vout instances */
264     config_PutInt(p_instance->p_libvlc_int, "width", width);
265     config_PutInt(p_instance->p_libvlc_int, "height", height);
266
267     libvlc_media_player_t *p_mi = libvlc_playlist_get_media_player(p_instance, p_e);
268     if( p_mi )
269     {
270         vout_thread_t *p_vout = GetVout( p_mi, p_e );
271         if( p_vout )
272         {
273             /* tell running vout to re-size */
274             vout_Control( p_vout , VOUT_SET_SIZE, width, height);
275             vlc_object_release( p_vout );
276         }
277         libvlc_media_player_release(p_mi);
278     }
279 }
280
281 void libvlc_video_set_viewport( libvlc_instance_t *p_instance,
282                             const libvlc_rectangle_t *view, const libvlc_rectangle_t *clip,
283                            libvlc_exception_t *p_e )
284 {
285     if( !view )
286     {
287         libvlc_exception_raise( p_e, "viewport is NULL" );
288         return;
289     }
290
291     /* if clip is NULL, then use view rectangle as clip */
292     if( !clip )
293         clip = view;
294
295     /* set as default for future vout instances */
296     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-top", view->top );
297     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-left", view->left );
298     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-bottom", view->bottom );
299     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-right", view->right );
300     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-top", clip->top );
301     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-left", clip->left );
302     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-bottom", clip->bottom );
303     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-right", clip->right );
304
305     libvlc_media_player_t *p_mi = libvlc_playlist_get_media_player(p_instance, p_e);
306     if( p_mi )
307     {
308         vout_thread_t *p_vout = GetVout( p_mi, p_e );
309         if( p_vout )
310         {
311             /* change viewport for running vout */
312             vout_Control( p_vout , VOUT_SET_VIEWPORT,
313                                view->top, view->left, view->bottom, view->right,
314                                clip->top, clip->left, clip->bottom, clip->right );
315             vlc_object_release( p_vout );
316         }
317         libvlc_media_player_release(p_mi);
318     }
319 }
320
321 char *libvlc_video_get_aspect_ratio( libvlc_media_player_t *p_mi,
322                                      libvlc_exception_t *p_e )
323 {
324     char *psz_aspect = 0;
325     vout_thread_t *p_vout = GetVout( p_mi, p_e );
326
327     if( !p_vout ) return 0;
328
329     psz_aspect = var_GetNonEmptyString( p_vout, "aspect-ratio" );
330     vlc_object_release( p_vout );
331     return psz_aspect ? psz_aspect : strdup("");
332 }
333
334 void libvlc_video_set_aspect_ratio( libvlc_media_player_t *p_mi,
335                                     char *psz_aspect, libvlc_exception_t *p_e )
336 {
337     vout_thread_t *p_vout = GetVout( p_mi, p_e );
338     int i_ret = -1;
339
340     if( !p_vout ) return;
341
342     i_ret = var_SetString( p_vout, "aspect-ratio", psz_aspect );
343     if( i_ret )
344         libvlc_exception_raise( p_e,
345                         "Unexpected error while setting aspect-ratio value" );
346
347     vlc_object_release( p_vout );
348 }
349
350 int libvlc_video_get_spu( libvlc_media_player_t *p_mi,
351                           libvlc_exception_t *p_e )
352 {
353     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
354     vlc_value_t val_list;
355     vlc_value_t val;
356     int i_spu = -1;
357     int i_ret = -1;
358     int i;
359
360     if( !p_input_thread ) return -1;
361
362     i_ret = var_Get( p_input_thread, "spu-es", &val );
363     if( i_ret < 0 )
364     {
365         libvlc_exception_raise( p_e, "Getting subtitle information failed" );
366         vlc_object_release( p_input_thread );
367         return i_ret;
368     }
369
370     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
371     for( i = 0; i < val_list.p_list->i_count; i++ )
372     {
373         vlc_value_t spu_val = val_list.p_list->p_values[i];
374         if( val.i_int == spu_val.i_int )
375         {
376             i_spu = i;
377             break;
378         }
379     }
380     vlc_object_release( p_input_thread );
381     return i_spu;
382 }
383
384 void libvlc_video_set_spu( libvlc_media_player_t *p_mi, int i_spu,
385                            libvlc_exception_t *p_e )
386 {
387     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
388     vlc_value_t val_list;
389     int i_ret = -1;
390     int i;
391
392     if( !p_input_thread ) return;
393
394     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
395     for( i = 0; i < val_list.p_list->i_count; i++ )
396     {
397         vlc_value_t val = val_list.p_list->p_values[i];
398         if( i_spu == i )
399         {
400             vlc_value_t new_val;
401
402             new_val.i_int = val.i_int;
403             i_ret = var_Set( p_input_thread, "spu-es", new_val );
404             if( i_ret < 0 )
405             {
406                 libvlc_exception_raise( p_e, "Setting subtitle value failed" );
407             }
408             vlc_object_release( p_input_thread );
409             return;
410         }
411     }
412     libvlc_exception_raise( p_e, "Subtitle value out of range" );
413     vlc_object_release( p_input_thread );
414 }
415
416 int libvlc_video_set_subtitle_file( libvlc_media_player_t *p_mi,
417                                     char *psz_subtitle,
418                                     libvlc_exception_t *p_e )
419 {
420     input_thread_t *p_input_thread = libvlc_get_input_thread ( p_mi, p_e );
421     bool b_ret = false;
422
423     if( p_input_thread )
424     {
425         if( input_AddSubtitles( p_input_thread, psz_subtitle, true ) )
426             b_ret = true;
427         vlc_object_release( p_input_thread );
428     }
429     return b_ret;
430 }
431
432 char *libvlc_video_get_crop_geometry( libvlc_media_player_t *p_mi,
433                                    libvlc_exception_t *p_e )
434 {
435     char *psz_geometry = 0;
436     vout_thread_t *p_vout = GetVout( p_mi, p_e );
437
438     if( !p_vout ) return 0;
439
440     psz_geometry = var_GetNonEmptyString( p_vout, "crop" );
441     vlc_object_release( p_vout );
442     return psz_geometry ? psz_geometry : strdup("");
443 }
444
445 void libvlc_video_set_crop_geometry( libvlc_media_player_t *p_mi,
446                                     char *psz_geometry, libvlc_exception_t *p_e )
447 {
448     vout_thread_t *p_vout = GetVout( p_mi, p_e );
449     int i_ret = -1;
450
451     if( !p_vout ) return;
452
453     i_ret = var_SetString( p_vout, "crop", psz_geometry );
454     if( i_ret )
455         libvlc_exception_raise( p_e,
456                         "Unexpected error while setting crop geometry" );
457
458     vlc_object_release( p_vout );
459 }
460
461 int libvlc_video_get_teletext( libvlc_media_player_t *p_mi,
462                                libvlc_exception_t *p_e )
463 {
464     vout_thread_t *p_vout = GetVout( p_mi, p_e );
465     vlc_object_t *p_vbi;
466     int i_ret = -1;
467
468     if( !p_vout ) return i_ret;
469
470     p_vbi = (vlc_object_t *) vlc_object_find_name( p_vout, "zvbi",
471                                                    FIND_ANYWHERE );
472     if( p_vbi )
473     {
474         i_ret = var_GetInteger( p_vbi, "vbi-page" );
475         vlc_object_release( p_vbi );
476     }
477
478     vlc_object_release( p_vout );
479     return i_ret;
480 }
481
482 void libvlc_video_set_teletext( libvlc_media_player_t *p_mi, int i_page,
483                                 libvlc_exception_t *p_e )
484 {
485     vout_thread_t *p_vout = GetVout( p_mi, p_e );
486     vlc_object_t *p_vbi;
487     int i_ret = -1;
488
489     if( !p_vout ) return;
490
491     p_vbi = (vlc_object_t *) vlc_object_find_name( p_vout, "zvbi",
492                                                    FIND_ANYWHERE );
493     if( p_vbi )
494     {
495         i_ret = var_SetInteger( p_vbi, "vbi-page", i_page );
496         vlc_object_release( p_vbi );
497     }
498     if( i_ret )
499         libvlc_exception_raise( p_e,
500                         "Unexpected error while setting teletext page" );
501     vlc_object_release( p_vout );
502 }
503
504 void libvlc_toggle_teletext( libvlc_media_player_t *p_mi,
505                              libvlc_exception_t *p_e )
506 {
507     /* We only work on the first vout */
508     vout_thread_t *p_vout = GetVout( p_mi, p_e );
509     bool opaque; int i_ret;
510
511     /* GetVout will raise the exception for us */
512     if( !p_vout ) return;
513
514     opaque = var_GetBool( p_vout, "vbi-opaque" );
515     i_ret = var_SetBool( p_vout, "vbi-opaque", !opaque );
516     if( i_ret )
517         libvlc_exception_raise( p_e,
518                         "Unexpected error while setting teletext value" );
519
520     vlc_object_release( p_vout );
521 }
522
523 int libvlc_video_destroy( libvlc_media_player_t *p_mi,
524                           libvlc_exception_t *p_e )
525 {
526     vout_thread_t *p_vout = GetVout( p_mi, p_e );
527     vlc_object_detach( p_vout );
528     vlc_object_release( p_vout );
529     vlc_object_release( p_vout );
530
531     return 0;
532 }