]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
* src/libvlc.h: Added a "snapshot-format" option to be able to select png or jpg...
[vlc] / src / video_output / vout_intf.c
1 /*****************************************************************************
2  * vout_intf.c : video output interface
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                                /* free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31
32 #include "vlc_video.h"
33 #include "video_output.h"
34 #include "vlc_image.h"
35 #include "vlc_spu.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40
41 /* Object variables callbacks */
42 static int ZoomCallback( vlc_object_t *, char const *,
43                          vlc_value_t, vlc_value_t, void * );
44 static int OnTopCallback( vlc_object_t *, char const *,
45                           vlc_value_t, vlc_value_t, void * );
46 static int FullscreenCallback( vlc_object_t *, char const *,
47                                vlc_value_t, vlc_value_t, void * );
48 static int SnapshotCallback( vlc_object_t *, char const *,
49                              vlc_value_t, vlc_value_t, void * );
50
51 /*****************************************************************************
52  * vout_RequestWindow: Create/Get a video window if possible.
53  *****************************************************************************
54  * This function looks for the main interface and tries to request
55  * a new video window. If it fails then the vout will still need to create the
56  * window by itself.
57  *****************************************************************************/
58 void *vout_RequestWindow( vout_thread_t *p_vout,
59                           int *pi_x_hint, int *pi_y_hint,
60                           unsigned int *pi_width_hint,
61                           unsigned int *pi_height_hint )
62 {
63     intf_thread_t *p_intf = NULL;
64     vlc_list_t *p_list;
65     void *p_window;
66     vlc_value_t val;
67     int i;
68
69     /* Small kludge */
70     if( !var_Type( p_vout, "aspect-ratio" ) ) vout_IntfInit( p_vout );
71
72     /* Get requested coordinates */
73     var_Get( p_vout, "video-x", &val );
74     *pi_x_hint = val.i_int ;
75     var_Get( p_vout, "video-y", &val );
76     *pi_y_hint = val.i_int;
77
78     *pi_width_hint = p_vout->i_window_width;
79     *pi_height_hint = p_vout->i_window_height;
80
81     /* Check whether someone provided us with a window ID */
82     var_Get( p_vout->p_vlc, "drawable", &val );
83     if( val.i_int ) return (void *)val.i_int;
84
85     /* Find if the main interface supports embedding */
86     p_list = vlc_list_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
87     if( !p_list ) return NULL;
88
89     for( i = 0; i < p_list->i_count; i++ )
90     {
91         p_intf = (intf_thread_t *)p_list->p_values[i].p_object;
92         if( p_intf->b_block && p_intf->pf_request_window ) break;
93         p_intf = NULL;
94     }
95
96     if( !p_intf )
97     {
98         vlc_list_release( p_list );
99         return NULL;
100     }
101
102     vlc_object_yield( p_intf );
103     vlc_list_release( p_list );
104
105     p_window = p_intf->pf_request_window( p_intf, p_vout, pi_x_hint, pi_y_hint,
106                                           pi_width_hint, pi_height_hint );
107
108     if( !p_window ) vlc_object_release( p_intf );
109     else p_vout->p_parent_intf = p_intf;
110
111     return p_window;
112 }
113
114 void vout_ReleaseWindow( vout_thread_t *p_vout, void *p_window )
115 {
116     intf_thread_t *p_intf = p_vout->p_parent_intf;
117
118     if( !p_intf ) return;
119
120     vlc_mutex_lock( &p_intf->object_lock );
121     if( p_intf->b_dead )
122     {
123         vlc_mutex_unlock( &p_intf->object_lock );
124         return;
125     }
126
127     if( !p_intf->pf_release_window )
128     {
129         msg_Err( p_vout, "no pf_release_window");
130         vlc_mutex_unlock( &p_intf->object_lock );
131         vlc_object_release( p_intf );
132         return;
133     }
134
135     p_intf->pf_release_window( p_intf, p_window );
136
137     p_vout->p_parent_intf = NULL;
138     vlc_mutex_unlock( &p_intf->object_lock );
139     vlc_object_release( p_intf );
140 }
141
142 int vout_ControlWindow( vout_thread_t *p_vout, void *p_window,
143                         int i_query, va_list args )
144 {
145     intf_thread_t *p_intf = p_vout->p_parent_intf;
146     int i_ret;
147
148     if( !p_intf ) return VLC_EGENERIC;
149
150     vlc_mutex_lock( &p_intf->object_lock );
151     if( p_intf->b_dead )
152     {
153         vlc_mutex_unlock( &p_intf->object_lock );
154         return VLC_EGENERIC;
155     }
156
157     if( !p_intf->pf_control_window )
158     {
159         msg_Err( p_vout, "no pf_control_window");
160         vlc_mutex_unlock( &p_intf->object_lock );
161         return VLC_EGENERIC;
162     }
163
164     i_ret = p_intf->pf_control_window( p_intf, p_window, i_query, args );
165     vlc_mutex_unlock( &p_intf->object_lock );
166     return i_ret;
167 }
168
169 /*****************************************************************************
170  * vout_IntfInit: called during the vout creation to initialise misc things.
171  *****************************************************************************/
172 void vout_IntfInit( vout_thread_t *p_vout )
173 {
174     vlc_value_t val, text, old_val;
175
176     /* Create a few object variables we'll need later on */
177     var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
178     var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
179     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
180     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
181     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
182     var_Create( p_vout, "align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
183     var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
184     var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
185
186     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
187                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
188
189     text.psz_string = _("Zoom");
190     var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
191
192     var_Get( p_vout, "zoom", &old_val );
193     if( old_val.f_float == 0.25 ||
194         old_val.f_float == 0.5 ||
195         old_val.f_float == 1 ||
196         old_val.f_float == 2 )
197     {
198         var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
199     }
200
201     val.f_float = 0.25; text.psz_string = _("1:4 Quarter");
202     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
203     val.f_float = 0.5; text.psz_string = _("1:2 Half");
204     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
205     val.f_float = 1; text.psz_string = _("1:1 Original");
206     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
207     val.f_float = 2; text.psz_string = _("2:1 Double");
208     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
209
210     var_Set( p_vout, "zoom", old_val );
211
212     var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
213
214     /* Add a variable to indicate if the window should be on top of others */
215     var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
216     text.psz_string = _("Always on top");
217     var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
218     var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
219
220     /* Add a variable to indicate whether we want window decoration or not */
221     var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
222
223     /* Add a fullscreen variable */
224     var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
225     text.psz_string = _("Fullscreen");
226     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
227     var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
228     if( val.b_bool )
229     {
230         /* user requested fullscreen */
231         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
232     }
233     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
234
235     /* Add a snapshot variable */
236     var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
237     text.psz_string = _("Snapshot");
238     var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
239     var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
240
241     /* Mouse coordinates */
242     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
243     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
244     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
245     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
246     var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
247
248     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
249     val.b_bool = VLC_TRUE;
250     var_Set( p_vout, "intf-change", val );
251 }
252
253 /*****************************************************************************
254  * vout_Snapshot: generates a snapshot.
255  *****************************************************************************/
256 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
257 {
258     image_handler_t *p_image = image_HandlerCreate( p_vout );
259     video_format_t fmt_in = {0}, fmt_out = {0};
260     char *psz_filename;
261     subpicture_t *p_subpic;
262     picture_t *p_pif;
263     vlc_value_t val, format;
264     int i_ret;
265
266     var_Get( p_vout, "snapshot-path", &val );
267     if( val.psz_string && !*val.psz_string )
268     {
269         free( val.psz_string );
270         val.psz_string = 0;
271     }
272 #ifdef SYS_DARWIN
273     if( !val.psz_string && p_vout->p_vlc->psz_homedir )
274     {
275         asprintf( &val.psz_string, "%s/Desktop",
276                   p_vout->p_vlc->psz_homedir );
277     }
278 #else
279     if( !val.psz_string && p_vout->p_vlc->psz_homedir )
280     {
281         asprintf( &val.psz_string, "%s/" CONFIG_DIR,
282                   p_vout->p_vlc->psz_homedir );
283     }
284 #endif
285     if( !val.psz_string )
286     {
287         msg_Err( p_vout, "no directory specified for snapshots" );
288         return VLC_EGENERIC;
289     }
290     var_Get( p_vout, "snapshot-format", &format );
291     if( format.psz_string && !*format.psz_string )
292     {
293         free( format.psz_string );
294         format.psz_string = strdup( "png" );
295     }
296
297     asprintf( &psz_filename, "%s/vlcsnap-%u.%s", val.psz_string,
298               (unsigned int)(p_pic->date / 100000) & 0xFFFFFF, format.psz_string );
299     free( val.psz_string );
300     free( format.psz_string );
301
302     /* Save the snapshot */
303     fmt_in.i_chroma = p_vout->render.i_chroma;
304     fmt_in.i_width = p_vout->render.i_width;
305     fmt_in.i_height = p_vout->render.i_height;
306     i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
307     if( i_ret != VLC_SUCCESS )
308     {
309         msg_Err( p_vout, "could not create snapshot %s", psz_filename );
310         free( psz_filename );
311         image_HandlerDelete( p_image );
312         return VLC_EGENERIC;
313     }
314
315     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
316     free( psz_filename );
317
318     /* Inject a subpicture with the snapshot */
319     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
320     fmt_out.i_width = fmt_out.i_visible_width = p_vout->render.i_width;
321     fmt_out.i_height = fmt_out.i_visible_height = p_vout->render.i_height;
322     fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
323     p_pif = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
324     image_HandlerDelete( p_image );
325     if( !p_pif ) return VLC_EGENERIC;
326
327     p_subpic = spu_CreateSubpicture( p_vout->p_spu );
328     if( p_subpic == NULL )
329     {
330          p_pif->pf_release( p_pif );
331          return VLC_EGENERIC;
332     }
333
334     p_subpic->i_channel = 0;
335     p_subpic->i_start = mdate();
336     p_subpic->i_stop = mdate() + 4000000;
337     p_subpic->b_ephemer = VLC_TRUE;
338     p_subpic->b_fade = VLC_TRUE;
339     p_subpic->i_original_picture_width = p_vout->render.i_width * 4;
340     p_subpic->i_original_picture_height = p_vout->render.i_height * 4;
341
342     p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
343     vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture, p_pif );
344     p_pif->pf_release( p_pif );
345
346     spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
347
348     return VLC_SUCCESS;
349 }
350
351 /*****************************************************************************
352  * vout_ControlDefault: default methods for video output control.
353  *****************************************************************************/
354 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
355 {
356     switch( i_query )
357     {
358     case VOUT_REPARENT:
359     case VOUT_CLOSE:
360         if( p_vout->p_parent_intf )
361         {
362             vlc_object_release( p_vout->p_parent_intf );
363             p_vout->p_parent_intf = NULL;
364         }
365         return VLC_SUCCESS;
366         break;
367
368     case VOUT_SNAPSHOT:
369         p_vout->b_snapshot = VLC_TRUE;
370         return VLC_SUCCESS;
371         break;
372
373     default:
374         msg_Dbg( p_vout, "control query not supported" );
375         return VLC_EGENERIC;
376     }
377 }
378
379 /*****************************************************************************
380  * Object variables callbacks
381  *****************************************************************************/
382 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
383                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
384 {
385     vout_thread_t *p_vout = (vout_thread_t *)p_this;
386     vout_Control( p_vout, VOUT_SET_ZOOM, newval.f_float );
387     return VLC_SUCCESS;
388 }
389
390 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
391                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
392 {
393     vout_thread_t *p_vout = (vout_thread_t *)p_this;
394     playlist_t *p_playlist;
395     vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
396
397     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
398                                                  FIND_PARENT );
399     if( p_playlist )
400     {
401         /* Modify playlist as well because the vout might have to be restarted */
402         var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL );
403         var_Set( p_playlist, "video-on-top", newval );
404
405         vlc_object_release( p_playlist );
406     }
407     return VLC_SUCCESS;
408 }
409
410 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
411                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
412 {
413     vout_thread_t *p_vout = (vout_thread_t *)p_this;
414     playlist_t *p_playlist;
415     vlc_value_t val;
416
417     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
418
419     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
420                                                  FIND_PARENT );
421     if( p_playlist )
422     {
423         /* Modify playlist as well because the vout might have to be restarted */
424         var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL );
425         var_Set( p_playlist, "fullscreen", newval );
426
427         vlc_object_release( p_playlist );
428     }
429
430     /* Disable "always on top" in fullscreen mode */
431     var_Get( p_vout, "video-on-top", &val );
432     if( newval.b_bool && val.b_bool )
433     {
434         val.b_bool = VLC_FALSE;
435         vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
436     }
437     else if( !newval.b_bool && val.b_bool )
438     {
439         vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
440     }
441
442     val.b_bool = VLC_TRUE;
443     var_Set( p_vout, "intf-change", val );
444     return VLC_SUCCESS;
445 }
446
447 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
448                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
449 {
450     vout_thread_t *p_vout = (vout_thread_t *)p_this;
451     vout_Control( p_vout, VOUT_SNAPSHOT );
452     return VLC_SUCCESS;
453 }