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