libvlc: Rename input to media_instance. And add the possibility to create a medi_inst...
[vlc.git] / 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  *          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 #include <vlc/libvlc.h>
30 #include <vlc_input.h>
31 #include <vlc_vout.h>
32
33 /*
34  * Remember to release the returned vout_thread_t since it is locked at
35  * the end of this function.
36  */
37 static vout_thread_t *GetVout( libvlc_media_instance_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_instance_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_vout1 = GetVout( p_mi, p_e );
64     vlc_value_t val; int i_ret;
65
66     /* GetVout will raise the exception for us */
67     if( !p_vout1 )
68     {
69         return;
70     }
71
72     if( b_fullscreen ) val.b_bool = VLC_TRUE;
73     else               val.b_bool = VLC_FALSE;
74
75     i_ret = var_Set( p_vout1, "fullscreen", val );
76     if( i_ret )
77         libvlc_exception_raise( p_e,
78                         "Unexpected error while setting fullscreen value" );
79
80     vlc_object_release( p_vout1 );
81 }
82
83 int libvlc_get_fullscreen( libvlc_media_instance_t *p_mi,
84                             libvlc_exception_t *p_e )
85 {
86     /* We only work on the first vout */
87     vout_thread_t *p_vout1 = GetVout( p_mi, p_e );
88     vlc_value_t val; int i_ret;
89
90     /* GetVout will raise the exception for us */
91     if( !p_vout1 )
92         return 0;
93
94     i_ret = var_Get( p_vout1, "fullscreen", &val );
95     if( i_ret )
96         libvlc_exception_raise( p_e,
97                         "Unexpected error while looking up fullscreen value" );
98
99     return val.b_bool == VLC_TRUE ? 1 : 0;
100 }
101
102 void libvlc_toggle_fullscreen( libvlc_media_instance_t *p_mi,
103                                libvlc_exception_t *p_e )
104 {
105     /* We only work on the first vout */
106     vout_thread_t *p_vout1 = GetVout( p_mi, p_e );
107     vlc_value_t val; int i_ret;
108
109     /* GetVout will raise the exception for us */
110     if( !p_vout1 )
111         return;
112
113     i_ret = var_Get( p_vout1, "fullscreen", &val );
114     if( i_ret )
115         libvlc_exception_raise( p_e,
116                         "Unexpected error while looking up fullscreen value" );
117
118     val.b_bool = !val.b_bool;
119     i_ret = var_Set( p_vout1, "fullscreen", val );
120     if( i_ret )
121         libvlc_exception_raise( p_e,
122                         "Unexpected error while setting fullscreen value" );
123
124     vlc_object_release( p_vout1 );
125 }
126
127 void
128 libvlc_video_take_snapshot( libvlc_media_instance_t *p_mi, char *psz_filepath,
129                        libvlc_exception_t *p_e )
130 {
131     vout_thread_t *p_vout = GetVout( p_mi, p_e );
132     input_thread_t *p_input_thread;
133
134     /* GetVout will raise the exception for us */
135     if( !p_vout )
136     {
137         return;
138     }
139
140     if( !psz_filepath )
141     {
142         libvlc_exception_raise( p_e, "filepath is null" );
143         return;
144     }
145
146
147     p_input_thread = (input_thread_t*)vlc_object_get(
148                                  p_mi->p_libvlc_instance->p_libvlc_int,
149                                  p_mi->i_input_id );
150     if( !p_input_thread )
151     {
152         libvlc_exception_raise( p_e, "Input does not exist" );
153         return;
154     }
155
156     var_SetString( p_vout, "snapshot-path", psz_filepath );
157     var_SetString( p_vout, "snapshot-format", "png" );
158
159     vout_Control( p_vout, VOUT_SNAPSHOT );
160     vlc_object_release( p_vout );
161     vlc_object_release( p_input_thread );
162 }
163
164 int libvlc_video_get_height( libvlc_media_instance_t *p_mi,
165                              libvlc_exception_t *p_e ) 
166 {
167     vout_thread_t *p_vout1 = GetVout( p_mi, p_e );
168     if( !p_vout1 )
169         return 0;
170
171     vlc_object_release( p_vout1 );
172
173     return p_vout1->i_window_height;
174 }
175
176 int libvlc_video_get_width( libvlc_media_instance_t *p_mi,
177                             libvlc_exception_t *p_e ) 
178 {
179     vout_thread_t *p_vout1 = GetVout( p_mi, p_e );
180     if( !p_vout1 )
181         return 0;
182
183     vlc_object_release( p_vout1 );
184
185     return p_vout1->i_window_width;
186 }
187
188 vlc_bool_t libvlc_input_has_vout( libvlc_media_instance_t *p_mi,
189                                   libvlc_exception_t *p_e )
190 {
191     input_thread_t *p_input_thread = libvlc_get_input_thread(p_mi, p_e);
192     vlc_bool_t has_vout = VLC_FALSE;
193
194     if( p_input_thread )
195     {
196         vout_thread_t *p_vout;
197
198         p_vout = vlc_object_find( p_input_thread, VLC_OBJECT_VOUT, FIND_CHILD );
199         if( p_vout )
200         {
201             has_vout = VLC_TRUE;
202             vlc_object_release( p_vout );
203         }
204         vlc_object_release( p_input_thread );
205     }
206     return has_vout;
207 }
208
209 int libvlc_video_reparent( libvlc_media_instance_t *p_mi, libvlc_drawable_t d,
210                            libvlc_exception_t *p_e )
211 {
212     vout_thread_t *p_vout = GetVout( p_mi, p_e );
213
214     if( p_vout )
215     {
216         vout_Control( p_vout , VOUT_REPARENT, d);
217         vlc_object_release( p_vout );
218     }
219     return 0;
220 }
221
222 void libvlc_video_resize( libvlc_media_instance_t *p_mi, int width, int height, libvlc_exception_t *p_e )
223 {
224     vout_thread_t *p_vout = GetVout( p_mi, p_e );
225     if( p_vout )
226     {
227         vout_Control( p_vout, VOUT_SET_SIZE, width, height );
228         vlc_object_release( p_vout );
229     }
230 }
231
232 void libvlc_video_redraw_rectangle( libvlc_media_instance_t *p_mi,
233                            const libvlc_rectangle_t *area,
234                            libvlc_exception_t *p_e )
235 {
236     if( (NULL != area)
237      && ((area->bottom - area->top) > 0)
238      && ((area->right - area->left) > 0) )
239     {
240         vout_thread_t *p_vout = GetVout( p_mi, p_e );
241         if( p_vout )
242         {
243             /* tell running vout to redraw area */
244             vout_Control( p_vout , VOUT_REDRAW_RECT,
245                                area->top, area->left, area->bottom, area->right );
246             vlc_object_release( p_vout );
247         }
248     }
249 }
250
251 /* global video settings */
252
253 void libvlc_video_set_parent( libvlc_instance_t *p_instance, libvlc_drawable_t d,
254                               libvlc_exception_t *p_e )
255 {
256     /* set as default for future vout instances */
257     var_SetInteger(p_instance->p_libvlc_int, "drawable", (int)d);
258
259     if( libvlc_playlist_isplaying(p_instance, p_e) )
260     {
261         libvlc_media_instance_t *p_mi = libvlc_playlist_get_media_instance(p_instance, p_e);
262         if( p_mi )
263         {
264             vout_thread_t *p_vout = GetVout( p_mi, p_e );
265             if( p_vout )
266             {
267                 /* tell running vout to re-parent */
268                 vout_Control( p_vout , VOUT_REPARENT, d);
269                 vlc_object_release( p_vout );
270             }
271             libvlc_media_instance_destroy_and_detach(p_mi);
272         }
273     }
274 }
275
276 libvlc_drawable_t libvlc_video_get_parent( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
277 {
278     libvlc_drawable_t result;
279     
280     result = var_GetInteger( p_instance->p_libvlc_int, "drawable" );
281     
282     return result;
283 }
284
285
286 void libvlc_video_set_size( libvlc_instance_t *p_instance, int width, int height,
287                            libvlc_exception_t *p_e )
288 {
289     /* set as default for future vout instances */
290     config_PutInt(p_instance->p_libvlc_int, "width", width);
291     config_PutInt(p_instance->p_libvlc_int, "height", height);
292
293     if( libvlc_playlist_isplaying(p_instance, p_e) )
294     {
295         libvlc_media_instance_t *p_mi = libvlc_playlist_get_media_instance(p_instance, p_e);
296         if( p_mi )
297         {
298             vout_thread_t *p_vout = GetVout( p_mi, p_e );
299             if( p_vout )
300             {
301                 /* tell running vout to re-size */
302                 vout_Control( p_vout , VOUT_SET_SIZE, width, height);
303                 vlc_object_release( p_vout );
304             }
305             libvlc_media_instance_destroy_and_detach(p_mi);
306         }
307     }
308 }
309
310 void libvlc_video_set_viewport( libvlc_instance_t *p_instance,
311                             const libvlc_rectangle_t *view, const libvlc_rectangle_t *clip,
312                            libvlc_exception_t *p_e )
313 {
314     if( NULL == view )
315     {
316         libvlc_exception_raise( p_e, "viewport is NULL" );
317     }
318
319     /* if clip is NULL, then use view rectangle as clip */
320     if( NULL == clip )
321         clip = view;
322
323     /* set as default for future vout instances */
324     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-top", view->top );
325     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-left", view->left );
326     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-bottom", view->bottom );
327     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-right", view->right );
328     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-top", clip->top );
329     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-left", clip->left );
330     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-bottom", clip->bottom );
331     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-right", clip->right );
332
333     if( libvlc_playlist_isplaying(p_instance, p_e) )
334     {
335         libvlc_media_instance_t *p_mi = libvlc_playlist_get_media_instance(p_instance, p_e);
336         if( p_mi )
337         {
338             vout_thread_t *p_vout = GetVout( p_mi, p_e );
339             if( p_vout )
340             {
341                 /* change viewport for running vout */
342                 vout_Control( p_vout , VOUT_SET_VIEWPORT,
343                                    view->top, view->left, view->bottom, view->right,
344                                    clip->top, clip->left, clip->bottom, clip->right );
345                 vlc_object_release( p_vout );
346             }
347             libvlc_media_instance_destroy_and_detach(p_mi);
348         }
349     }
350 }
351
352 char *libvlc_video_get_aspect_ratio( libvlc_media_instance_t *p_mi,
353                                      libvlc_exception_t *p_e )
354 {
355     char *psz_aspect = 0;
356     vout_thread_t *p_vout = GetVout( p_mi, p_e );
357
358     if( !p_vout )
359         return 0;
360
361     psz_aspect = var_GetString( p_vout, "aspect-ratio" );
362     vlc_object_release( p_vout );
363     return psz_aspect;
364 }
365
366 void libvlc_video_set_aspect_ratio( libvlc_media_instance_t *p_mi,
367                                     char *psz_aspect, libvlc_exception_t *p_e )
368 {
369     vout_thread_t *p_vout = GetVout( p_mi, p_e );
370     int i_ret = -1;
371
372     if( !p_vout )
373         return;
374
375     i_ret = var_SetString( p_vout, "aspect-ratio", psz_aspect );
376     if( i_ret )
377         libvlc_exception_raise( p_e,
378                         "Unexpected error while setting aspect-ratio value" );
379
380     vlc_object_release( p_vout );
381 }
382
383 int libvlc_video_get_spu( libvlc_media_instance_t *p_mi,
384                           libvlc_exception_t *p_e )
385 {
386     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
387     vlc_value_t val_list;
388     vlc_value_t val;
389     int i_spu = -1;
390     int i_ret = -1;
391     int i;
392
393     if( !p_input_thread )
394         return -1;
395
396     i_ret = var_Get( p_input_thread, "spu-es", &val );
397     if( i_ret < 0 )
398     {
399         libvlc_exception_raise( p_e, "Getting subtitle information failed" );
400         vlc_object_release( p_input_thread );
401         return i_ret;
402     }
403
404     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
405     for( i = 0; i < val_list.p_list->i_count; i++ )
406     {
407         vlc_value_t spu_val = val_list.p_list->p_values[i];
408         if( val.i_int == spu_val.i_int )
409         {
410             i_spu = i;
411             break;
412         }
413     }
414     vlc_object_release( p_input_thread );
415     return i_spu;
416 }
417
418 void libvlc_video_set_spu( libvlc_media_instance_t *p_mi, int i_spu,
419                            libvlc_exception_t *p_e )
420 {
421     input_thread_t *p_input_thread = libvlc_get_input_thread( p_mi, p_e );
422     vlc_value_t val_list;
423     int i_ret = -1;
424     int i;
425
426     if( !p_input_thread )
427         return;
428
429     var_Change( p_input_thread, "spu-es", VLC_VAR_GETCHOICES, &val_list, NULL );
430     for( i = 0; i < val_list.p_list->i_count; i++ )
431     {
432         vlc_value_t val = val_list.p_list->p_values[i];
433         if( i_spu == i )
434         {
435             vlc_value_t new_val;
436
437             new_val.i_int = val.i_int;
438             i_ret = var_Set( p_input_thread, "spu-es", new_val );
439             if( i_ret < 0 )
440             {
441                 libvlc_exception_raise( p_e, "Setting subtitle value failed" );
442             }
443             vlc_object_release( p_input_thread );
444             return;
445         }
446     }
447     libvlc_exception_raise( p_e, "Subtitle value out of range" );
448     vlc_object_release( p_input_thread );
449 }
450
451 char *libvlc_video_get_crop_geometry( libvlc_media_instance_t *p_mi,
452                                    libvlc_exception_t *p_e )
453 {
454     char *psz_geometry = 0;
455     vout_thread_t *p_vout = GetVout( p_mi, p_e );
456
457     if( !p_vout )
458         return 0;
459
460     psz_geometry = var_GetString( p_vout, "crop" );
461     vlc_object_release( p_vout );
462     return psz_geometry;
463 }
464
465 void libvlc_video_set_crop_geometry( libvlc_media_instance_t *p_mi,
466                                     char *psz_geometry, libvlc_exception_t *p_e )
467 {
468     vout_thread_t *p_vout = GetVout( p_mi, p_e );
469     int i_ret = -1;
470
471     if( !p_vout )
472         return;
473
474     i_ret = var_SetString( p_vout, "crop", psz_geometry );
475     if( i_ret )
476         libvlc_exception_raise( p_e,
477                         "Unexpected error while setting crop geometry" );
478
479     vlc_object_release( p_vout );
480 }
481
482 int libvlc_video_destroy( libvlc_media_instance_t *p_mi,
483                           libvlc_exception_t *p_e )
484 {
485     vout_thread_t *p_vout = GetVout( p_mi, p_e );
486     vlc_object_detach( p_vout ); 
487     vlc_object_release( p_vout );
488     vout_Destroy( p_vout );
489
490     return 0;
491 }