]> git.sesse.net Git - vlc/blob - src/control/video.c
avoid VLC_VariableSet to set the drawable, use libvlc_set_video_drawable instead.
[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
29 #include <vlc/vout.h>
30 #include <vlc/intf.h>
31
32 /*
33  * Remember to release the returned vout_thread_t since it is locked at
34  * the end of this function.
35  */
36 static vout_thread_t *GetVout( libvlc_input_t *p_input,
37                                libvlc_exception_t *p_exception )
38 {
39     input_thread_t *p_input_thread;
40     vout_thread_t *p_vout;
41
42     if( !p_input )
43     {
44         libvlc_exception_raise( p_exception, "Input is NULL" );
45         return NULL;
46     }
47
48     p_input_thread = (input_thread_t*)vlc_object_get(
49                                  p_input->p_instance->p_libvlc_int,
50                                  p_input->i_input_id );
51     if( !p_input_thread )
52     {
53         libvlc_exception_raise( p_exception, "Input does not exist" );
54         return NULL;
55     }
56
57     p_vout = vlc_object_find( p_input_thread, VLC_OBJECT_VOUT, FIND_CHILD );
58     if( !p_vout )
59     {
60         vlc_object_release( p_input_thread );
61         libvlc_exception_raise( p_exception, "No active video output" );
62         return NULL;
63     }
64     vlc_object_release( p_input_thread );
65
66     return p_vout;
67 }
68 /**********************************************************************
69  * Exported functions
70  **********************************************************************/
71
72 void libvlc_set_video_drawable( libvlc_instance_t *p_instance,
73                                 libvlc_drawable_t drawable,
74                                 libvlc_exception_t *p_e )
75 {
76     vlc_value_t value;
77     value.i_int = drawable;
78     
79     var_Set( p_instance->p_libvlc_int, "drawable", value);
80 }
81
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     vout_thread_t *p_vout = GetVout( p_input, p_e );
213
214     /* GetVout will raise the exception for us */
215     if( !p_vout )
216     {
217         return VLC_FALSE;
218     }
219
220     vlc_object_release( p_vout );
221
222     return VLC_TRUE;
223 }
224
225 int libvlc_video_reparent( libvlc_input_t *p_input, libvlc_drawable_t d,
226                            libvlc_exception_t *p_e )
227 {
228     vout_thread_t *p_vout = GetVout( p_input, p_e );
229     vout_Control( p_vout , VOUT_REPARENT, d);
230     vlc_object_release( p_vout );
231
232     return 0;
233 }
234
235 void libvlc_video_resize( libvlc_input_t *p_input, int width, int height, libvlc_exception_t *p_e )
236 {
237     vout_thread_t *p_vout = GetVout( p_input, p_e );
238     vout_Control( p_vout, VOUT_SET_SIZE, width, height );
239     vlc_object_release( p_vout );
240 }
241
242 /* global video settings */
243
244 void libvlc_video_set_parent( libvlc_instance_t *p_instance, libvlc_drawable_t d,
245                            libvlc_exception_t *p_e )
246 {
247     /* set as default for future vout instances */
248     var_SetInteger(p_instance->p_libvlc_int, "drawable", (int)d);
249
250     if( libvlc_playlist_isplaying(p_instance, p_e) )
251     {
252         libvlc_input_t *p_input = libvlc_playlist_get_input(p_instance, p_e);
253         if( p_input )
254         {
255             vout_thread_t *p_vout = GetVout( p_input, p_e );
256             if( p_vout )
257             {
258                 /* tell running vout to re-parent */
259                 vout_Control( p_vout , VOUT_REPARENT, d);
260                 vlc_object_release( p_vout );
261             }
262             libvlc_input_free(p_input);
263         }
264     }
265 }
266
267 void libvlc_video_set_size( libvlc_instance_t *p_instance, int width, int height,
268                            libvlc_exception_t *p_e )
269 {
270     /* set as default for future vout instances */
271     config_PutInt(p_instance->p_libvlc_int, "width", width);
272     config_PutInt(p_instance->p_libvlc_int, "height", height);
273
274     if( libvlc_playlist_isplaying(p_instance, p_e) )
275     {
276         libvlc_input_t *p_input = libvlc_playlist_get_input(p_instance, p_e);
277         if( p_input )
278         {
279             vout_thread_t *p_vout = GetVout( p_input, p_e );
280             if( p_vout )
281             {
282                 /* tell running vout to re-size */
283                 vout_Control( p_vout , VOUT_SET_SIZE, width, height);
284                 vlc_object_release( p_vout );
285             }
286             libvlc_input_free(p_input);
287         }
288     }
289 }
290
291 void libvlc_video_set_viewport( libvlc_instance_t *p_instance,
292                             const libvlc_rectangle_t *view, const libvlc_rectangle_t *clip,
293                            libvlc_exception_t *p_e )
294 {
295     if( NULL == view )
296     {
297         libvlc_exception_raise( p_e, "viewport is NULL" );
298     }
299
300     /* if clip is NULL, then use view rectangle as clip */
301     if( NULL == clip )
302         clip = view;
303
304     /* set as default for future vout instances */
305     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-top", view->top );
306     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-left", view->left );
307     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-bottom", view->bottom );
308     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-right", view->right );
309     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-top", clip->top );
310     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-left", clip->left );
311     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-bottom", clip->bottom );
312     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-right", clip->right );
313
314     if( libvlc_playlist_isplaying(p_instance, p_e) )
315     {
316         libvlc_input_t *p_input = libvlc_playlist_get_input(p_instance, p_e);
317         if( p_input )
318         {
319             vout_thread_t *p_vout = GetVout( p_input, p_e );
320             if( p_vout )
321             {
322                 /* change viewport for running vout */
323                 vout_Control( p_vout , VOUT_SET_VIEWPORT,
324                                    view->top, view->left, view->bottom, view->right,
325                                    clip->top, clip->left, clip->bottom, clip->right );
326                 vlc_object_release( p_vout );
327             }
328             libvlc_input_free(p_input);
329         }
330     }
331 }
332
333 char *libvlc_video_get_aspect_ratio( libvlc_input_t *p_input,
334                                    libvlc_exception_t *p_e )
335 {
336     char *psz_aspect = 0;
337     vout_thread_t *p_vout = GetVout( p_input, p_e );
338
339     if( !p_vout )
340         return 0;
341
342     psz_aspect = var_GetString( p_vout, "aspect-ratio" );
343     vlc_object_release( p_vout );
344     return psz_aspect;
345 }
346
347 void libvlc_video_set_aspect_ratio( libvlc_input_t *p_input,
348                                     char *psz_aspect, libvlc_exception_t *p_e )
349 {
350     vout_thread_t *p_vout = GetVout( p_input, p_e );
351     int i_ret = -1;
352
353     if( !p_vout )
354         return;
355
356     i_ret = var_SetString( p_vout, "aspect-ratio", psz_aspect );
357     if( i_ret )
358         libvlc_exception_raise( p_e,
359                         "Unexpected error while setting aspect-ratio value" );
360
361     vlc_object_release( p_vout );
362 }
363
364 int libvlc_video_destroy( libvlc_input_t *p_input,
365                           libvlc_exception_t *p_e )
366 {
367     vout_thread_t *p_vout = GetVout( p_input, p_e );
368     vlc_object_detach( p_vout ); 
369     vlc_object_release( p_vout );
370     vout_Destroy( p_vout );
371
372     return 0;
373 }