]> git.sesse.net Git - vlc/blob - src/control/video.c
(Forward port of [17371]) Implement Aspect Ratio property for Mozilla plugin
[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_fullscreen( libvlc_input_t *p_input, int b_fullscreen,
73                             libvlc_exception_t *p_e )
74 {
75     /* We only work on the first vout */
76     vout_thread_t *p_vout1 = GetVout( p_input, p_e );
77     vlc_value_t val; int i_ret;
78
79     /* GetVout will raise the exception for us */
80     if( !p_vout1 )
81     {
82         return;
83     }
84
85     if( b_fullscreen ) val.b_bool = VLC_TRUE;
86     else               val.b_bool = VLC_FALSE;
87
88     i_ret = var_Set( p_vout1, "fullscreen", val );
89     if( i_ret )
90         libvlc_exception_raise( p_e,
91                         "Unexpected error while setting fullscreen value" );
92
93     vlc_object_release( p_vout1 );
94 }
95
96 int libvlc_get_fullscreen( libvlc_input_t *p_input,
97                             libvlc_exception_t *p_e )
98 {
99     /* We only work on the first vout */
100     vout_thread_t *p_vout1 = GetVout( p_input, p_e );
101     vlc_value_t val; int i_ret;
102
103     /* GetVout will raise the exception for us */
104     if( !p_vout1 )
105         return 0;
106
107     i_ret = var_Get( p_vout1, "fullscreen", &val );
108     if( i_ret )
109         libvlc_exception_raise( p_e,
110                         "Unexpected error while looking up fullscreen value" );
111
112     return val.b_bool == VLC_TRUE ? 1 : 0;
113 }
114
115 void libvlc_toggle_fullscreen( libvlc_input_t *p_input,
116                                libvlc_exception_t *p_e )
117 {
118     /* We only work on the first vout */
119     vout_thread_t *p_vout1 = GetVout( p_input, p_e );
120     vlc_value_t val; int i_ret;
121
122     /* GetVout will raise the exception for us */
123     if( !p_vout1 )
124         return;
125
126     i_ret = var_Get( p_vout1, "fullscreen", &val );
127     if( i_ret )
128         libvlc_exception_raise( p_e,
129                         "Unexpected error while looking up fullscreen value" );
130
131     val.b_bool = !val.b_bool;
132     i_ret = var_Set( p_vout1, "fullscreen", val );
133     if( i_ret )
134         libvlc_exception_raise( p_e,
135                         "Unexpected error while setting fullscreen value" );
136
137     vlc_object_release( p_vout1 );
138 }
139
140 void
141 libvlc_video_take_snapshot( libvlc_input_t *p_input, char *psz_filepath,
142                        libvlc_exception_t *p_e )
143 {
144     vout_thread_t *p_vout = GetVout( p_input, p_e );
145     input_thread_t *p_input_thread;
146
147     char path[256];
148
149     /* GetVout will raise the exception for us */
150     if( !p_vout )
151     {
152         return;
153     }
154
155     p_input_thread = (input_thread_t*)vlc_object_get(
156                                  p_input->p_instance->p_libvlc_int,
157                                  p_input->i_input_id );
158     if( !p_input_thread )
159     {
160         libvlc_exception_raise( p_e, "Input does not exist" );
161         return;
162     }
163
164     snprintf( path, 255, "%s", psz_filepath );
165     var_SetString( p_vout, "snapshot-path", path );
166     var_SetString( p_vout, "snapshot-format", "png" );
167
168     vout_Control( p_vout, VOUT_SNAPSHOT );
169     vlc_object_release( p_vout );
170     vlc_object_release( p_input_thread );
171 }
172
173 int libvlc_video_get_height( libvlc_input_t *p_input,
174                              libvlc_exception_t *p_e ) 
175 {
176     vout_thread_t *p_vout1 = GetVout( p_input, p_e );
177     if( !p_vout1 )
178         return 0;
179
180     vlc_object_release( p_vout1 );
181
182     return p_vout1->i_window_height;
183 }
184
185 int libvlc_video_get_width( 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_width;
195 }
196
197 vlc_bool_t libvlc_input_has_vout( libvlc_input_t *p_input,
198                                   libvlc_exception_t *p_e )
199 {
200     vout_thread_t *p_vout = GetVout( p_input, p_e );
201
202     /* GetVout will raise the exception for us */
203     if( !p_vout )
204     {
205         return VLC_FALSE;
206     }
207
208     vlc_object_release( p_vout );
209
210     return VLC_TRUE;
211 }
212
213 int libvlc_video_reparent( libvlc_input_t *p_input, libvlc_drawable_t d,
214                            libvlc_exception_t *p_e )
215 {
216     vout_thread_t *p_vout = GetVout( p_input, p_e );
217     vout_Control( p_vout , VOUT_REPARENT, d);
218     vlc_object_release( p_vout );
219
220     return 0;
221 }
222
223 void libvlc_video_resize( libvlc_input_t *p_input, int width, int height, libvlc_exception_t *p_e )
224 {
225     vout_thread_t *p_vout = GetVout( p_input, p_e );
226     vout_Control( p_vout, VOUT_SET_SIZE, width, height );
227     vlc_object_release( p_vout );
228 }
229
230 /* global video settings */
231
232 void libvlc_video_set_parent( libvlc_instance_t *p_instance, libvlc_drawable_t d,
233                            libvlc_exception_t *p_e )
234 {
235     /* set as default for future vout instances */
236     var_SetInteger(p_instance->p_libvlc_int, "drawable", (int)d);
237
238     if( libvlc_playlist_isplaying(p_instance, p_e) )
239     {
240         libvlc_input_t *p_input = libvlc_playlist_get_input(p_instance, p_e);
241         if( p_input )
242         {
243             vout_thread_t *p_vout = GetVout( p_input, p_e );
244             if( p_vout )
245             {
246                 /* tell running vout to re-parent */
247                 vout_Control( p_vout , VOUT_REPARENT, d);
248                 vlc_object_release( p_vout );
249             }
250             libvlc_input_free(p_input);
251         }
252     }
253 }
254
255 void libvlc_video_set_size( libvlc_instance_t *p_instance, int width, int height,
256                            libvlc_exception_t *p_e )
257 {
258     /* set as default for future vout instances */
259     config_PutInt(p_instance->p_libvlc_int, "width", width);
260     config_PutInt(p_instance->p_libvlc_int, "height", height);
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-size */
271                 vout_Control( p_vout , VOUT_SET_SIZE, width, height);
272                 vlc_object_release( p_vout );
273             }
274             libvlc_input_free(p_input);
275         }
276     }
277 }
278
279 void libvlc_video_set_viewport( libvlc_instance_t *p_instance,
280                             const libvlc_rectangle_t *view, const libvlc_rectangle_t *clip,
281                            libvlc_exception_t *p_e )
282 {
283     if( NULL == view )
284     {
285         libvlc_exception_raise( p_e, "viewport is NULL" );
286     }
287
288     /* if clip is NULL, then use view rectangle as clip */
289     if( NULL == clip )
290         clip = view;
291
292     /* set as default for future vout instances */
293     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-top", view->top );
294     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-left", view->left );
295     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-bottom", view->bottom );
296     var_SetInteger( p_instance->p_libvlc_int, "drawable-view-right", view->right );
297     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-top", clip->top );
298     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-left", clip->left );
299     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-bottom", clip->bottom );
300     var_SetInteger( p_instance->p_libvlc_int, "drawable-clip-right", clip->right );
301
302     if( libvlc_playlist_isplaying(p_instance, p_e) )
303     {
304         libvlc_input_t *p_input = libvlc_playlist_get_input(p_instance, p_e);
305         if( p_input )
306         {
307             vout_thread_t *p_vout = GetVout( p_input, p_e );
308             if( p_vout )
309             {
310                 /* change viewport for running vout */
311                 vout_Control( p_vout , VOUT_SET_VIEWPORT,
312                                    view->top, view->left, view->bottom, view->right,
313                                    clip->top, clip->left, clip->bottom, clip->right );
314                 vlc_object_release( p_vout );
315             }
316             libvlc_input_free(p_input);
317         }
318     }
319 }
320
321 char *libvlc_video_get_aspect_ratio( libvlc_input_t *p_input,
322                                    libvlc_exception_t *p_e )
323 {
324     char *psz_aspect = 0;
325     vout_thread_t *p_vout = GetVout( p_input, p_e );
326
327     if( !p_vout )
328         return 0;
329
330     psz_aspect = var_GetString( p_vout1, "aspect-ratio" );
331     vlc_object_release( p_vout );
332     return psz_aspect;
333 }
334
335 void libvlc_video_set_aspect_ratio( libvlc_input_t *p_input,
336                                     char *psz_aspect, libvlc_exception_t *p_e )
337 {
338     vout_thread_t *p_vout = GetVout( p_input, p_e );
339     int i_ret = -1;
340
341     if( !p_vout )
342         return;
343
344     i_ret = var_SetString( p_vout, "aspect-ratio", psz_aspect );
345     if( i_ret )
346         libvlc_exception_raise( p_e,
347                         "Unexpected error while setting aspect-ratio value" );
348
349     vlc_object_release( p_vout );
350 }
351
352 int libvlc_video_destroy( libvlc_input_t *p_input,
353                           libvlc_exception_t *p_e )
354 {
355     vout_thread_t *p_vout = GetVout( p_input, p_e );
356     vlc_object_detach( p_vout ); 
357     vlc_object_release( p_vout );
358     vout_Destroy( p_vout );
359
360     return 0;
361 }