]> git.sesse.net Git - vlc/blob - modules/access/screen/screen.c
screen: added error handling to prevent this module from failing silently
[vlc] / modules / access / screen / screen.c
1 /*****************************************************************************
2  * screen.c: Screen capture module.
3  *****************************************************************************
4  * Copyright (C) 2004-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Antoine Cellerier <dionoea at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_modules.h>                 /* module_need for "video blending" */
36 #include "screen.h"
37
38 /*****************************************************************************
39  * Module descriptor
40  *****************************************************************************/
41 #define CACHING_TEXT N_("Caching value in ms")
42 #define CACHING_LONGTEXT N_( \
43     "Caching value for screen capture. "\
44     "This value should be set in milliseconds." )
45 #define FPS_TEXT N_("Frame rate")
46 #define FPS_LONGTEXT N_( \
47     "Desired frame rate for the capture." )
48
49 #ifdef WIN32
50 #define FRAGS_TEXT N_("Capture fragment size")
51 #define FRAGS_LONGTEXT N_( \
52     "Optimize the capture by fragmenting the screen in chunks " \
53     "of predefined height (16 might be a good value, and 0 means disabled)." )
54 #endif
55
56 #ifdef SCREEN_SUBSCREEN
57 #define TOP_TEXT N_( "Subscreen top left corner" )
58 #define TOP_LONGTEXT N_( \
59     "Top coordinate of the subscreen top left corner." )
60
61 #define LEFT_TEXT N_( "Subscreen top left corner" )
62 #define LEFT_LONGTEXT N_( \
63     "Left coordinate of the subscreen top left corner." )
64
65 #define WIDTH_TEXT N_( "Subscreen width" )
66
67 #define HEIGHT_TEXT N_( "Subscreen height" )
68
69 #define FOLLOW_MOUSE_TEXT N_( "Follow the mouse" )
70 #define FOLLOW_MOUSE_LONGTEXT N_( \
71     "Follow the mouse when capturing a subscreen." )
72 #endif
73
74 #ifdef SCREEN_MOUSE
75 #define MOUSE_TEXT N_( "Mouse pointer image" )
76 #define MOUSE_LONGTEXT N_( \
77     "If specified, will use the image to draw the mouse pointer on the " \
78     "capture." )
79 #endif
80
81 static int  Open ( vlc_object_t * );
82 static void Close( vlc_object_t * );
83
84 #ifdef WIN32
85 #   define SCREEN_FPS 1
86 #else
87 #   define SCREEN_FPS 5
88 #endif
89
90 vlc_module_begin ()
91     set_description( N_("Screen Input") )
92     set_shortname( N_("Screen" ))
93     set_category( CAT_INPUT )
94     set_subcategory( SUBCAT_INPUT_ACCESS )
95
96     add_integer( "screen-caching", DEFAULT_PTS_DELAY / 1000,
97         CACHING_TEXT, CACHING_LONGTEXT, true )
98     add_float( "screen-fps", SCREEN_FPS, FPS_TEXT, FPS_LONGTEXT, false )
99
100 #ifdef SCREEN_SUBSCREEN
101     add_integer( "screen-top", 0, TOP_TEXT, TOP_LONGTEXT, true )
102     add_integer( "screen-left", 0, LEFT_TEXT, LEFT_LONGTEXT, true )
103     add_integer( "screen-width", 0, WIDTH_TEXT, WIDTH_TEXT, true )
104     add_integer( "screen-height", 0, HEIGHT_TEXT, HEIGHT_TEXT, true )
105
106     add_bool( "screen-follow-mouse", false, FOLLOW_MOUSE_TEXT,
107               FOLLOW_MOUSE_LONGTEXT, false )
108 #endif
109
110 #ifdef SCREEN_MOUSE
111     add_loadfile( "screen-mouse-image", "", MOUSE_TEXT, MOUSE_LONGTEXT, true )
112 #endif
113
114 #ifdef WIN32
115     add_integer( "screen-fragment-size", 0, FRAGS_TEXT, FRAGS_LONGTEXT, true )
116 #endif
117
118     set_capability( "access_demux", 0 )
119     add_shortcut( "screen" )
120     set_callbacks( Open, Close )
121 vlc_module_end ()
122
123 /*****************************************************************************
124  * Local prototypes
125  *****************************************************************************/
126 static int Control( demux_t *, int, va_list );
127 static int Demux  ( demux_t * );
128
129 /*****************************************************************************
130  * DemuxOpen:
131  *****************************************************************************/
132 static int Open( vlc_object_t *p_this )
133 {
134     demux_t     *p_demux = (demux_t*)p_this;
135     demux_sys_t *p_sys;
136
137     /* Fill p_demux field */
138     p_demux->pf_demux = Demux;
139     p_demux->pf_control = Control;
140     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
141     if( !p_sys )
142         return VLC_ENOMEM;
143
144     /* Update default_pts to a suitable value for screen access */
145     var_Create( p_demux, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
146
147     p_sys->f_fps = var_CreateGetFloat( p_demux, "screen-fps" );
148     p_sys->i_incr = 1000000 / p_sys->f_fps;;
149     p_sys->i_next_date = 0;
150
151 #ifdef SCREEN_SUBSCREEN
152     p_sys->i_top = var_CreateGetInteger( p_demux, "screen-top" );
153     p_sys->i_left = var_CreateGetInteger( p_demux, "screen-left" );
154     p_sys->i_width = var_CreateGetInteger( p_demux, "screen-width" );
155     p_sys->i_height = var_CreateGetInteger( p_demux, "screen-height" );
156     if( p_sys->i_width > 0 && p_sys->i_height > 0 )
157         msg_Dbg( p_demux, "capturing subscreen top: %d, left: %d, "
158                           "width: %d, height: %d",
159                           p_sys->i_top,
160                           p_sys->i_left,
161                           p_sys->i_width,
162                           p_sys->i_height );
163 #endif
164
165     if( screen_InitCapture( p_demux ) != VLC_SUCCESS )
166     {
167         free( p_sys );
168         return VLC_EGENERIC;
169     }
170
171     msg_Dbg( p_demux, "screen width: %i, height: %i, depth: %i",
172              p_sys->fmt.video.i_width, p_sys->fmt.video.i_height,
173              p_sys->fmt.video.i_bits_per_pixel );
174
175 #ifdef SCREEN_SUBSCREEN
176     if( p_sys->i_width > 0 && p_sys->i_height > 0 )
177     {
178         if( p_sys->i_left + p_sys->i_width > p_sys->fmt.video.i_width ||
179             p_sys->i_top + p_sys->i_height > p_sys->fmt.video.i_height )
180         {
181             msg_Err( p_demux, "subscreen region overflows the screen" );
182             free( p_sys );
183             return VLC_EGENERIC;
184         }
185         else
186         {
187             p_sys->i_screen_width = p_sys->fmt.video.i_width;
188             p_sys->i_screen_height = p_sys->fmt.video.i_height;
189             p_sys->fmt.video.i_visible_width =
190             p_sys->fmt.video.i_width = p_sys->i_width;
191             p_sys->fmt.video.i_visible_height =
192             p_sys->fmt.video.i_height = p_sys->i_height;
193             p_sys->b_follow_mouse = var_CreateGetInteger( p_demux,
194                                                 "screen-follow-mouse" );
195             if( p_sys->b_follow_mouse )
196                 msg_Dbg( p_demux, "mouse following enabled" );
197         }
198     }
199 #endif
200
201 #ifdef SCREEN_MOUSE
202     char * psz_mouse = var_CreateGetNonEmptyString( p_demux,
203                                                     "screen-mouse-image" );
204     if( psz_mouse )
205     {
206         image_handler_t *p_image;
207         video_format_t fmt_in, fmt_out;
208         msg_Dbg( p_demux, "Using %s for the mouse pointer image", psz_mouse );
209         memset( &fmt_in, 0, sizeof( fmt_in ) );
210         memset( &fmt_out, 0, sizeof( fmt_out ) );
211         fmt_out.i_chroma = VLC_CODEC_RGBA;
212         p_image = image_HandlerCreate( p_demux );
213         if( p_image )
214         {
215             p_sys->p_mouse =
216                 image_ReadUrl( p_image, psz_mouse, &fmt_in, &fmt_out );
217             image_HandlerDelete( p_image );
218         }
219         if( !p_sys->p_mouse )
220             msg_Err( p_demux, "Failed to open mouse pointer image (%s)",
221                      psz_mouse );
222         free( psz_mouse );
223     }
224 #endif
225
226     p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
227
228     return VLC_SUCCESS;
229 }
230
231 /*****************************************************************************
232  * Close:
233  *****************************************************************************/
234 static void Close( vlc_object_t *p_this )
235 {
236     demux_t     *p_demux = (demux_t*)p_this;
237     demux_sys_t *p_sys = p_demux->p_sys;
238
239     screen_CloseCapture( p_demux );
240 #ifdef SCREEN_MOUSE
241     if( p_sys->p_mouse )
242         picture_Release( p_sys->p_mouse );
243 #endif
244     free( p_sys );
245 }
246
247 /*****************************************************************************
248  * Demux:
249  *****************************************************************************/
250 static int Demux( demux_t *p_demux )
251 {
252     demux_sys_t *p_sys = p_demux->p_sys;
253     block_t *p_block;
254
255     if( !p_sys->i_next_date ) p_sys->i_next_date = mdate();
256
257     /* Frame skipping if necessary */
258     while( mdate() >= p_sys->i_next_date + p_sys->i_incr )
259         p_sys->i_next_date += p_sys->i_incr;
260
261     mwait( p_sys->i_next_date );
262     p_block = screen_Capture( p_demux );
263     if( !p_block )
264     {
265         p_sys->i_next_date += p_sys->i_incr;
266         return 1;
267     }
268
269     p_block->i_dts = p_block->i_pts = p_sys->i_next_date;
270
271     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
272     es_out_Send( p_demux->out, p_sys->es, p_block );
273
274     p_sys->i_next_date += p_sys->i_incr;
275
276     return 1;
277 }
278
279 /*****************************************************************************
280  * Control:
281  *****************************************************************************/
282 static int Control( demux_t *p_demux, int i_query, va_list args )
283 {
284     bool *pb;
285     int64_t *pi64;
286
287     switch( i_query )
288     {
289         /* Special for access_demux */
290         case DEMUX_CAN_PAUSE:
291         case DEMUX_CAN_SEEK:
292         case DEMUX_CAN_CONTROL_PACE:
293             /* TODO */
294             pb = (bool*)va_arg( args, bool * );
295             *pb = false;
296             return VLC_SUCCESS;
297
298         case DEMUX_GET_PTS_DELAY:
299             pi64 = (int64_t*)va_arg( args, int64_t * );
300             *pi64 = var_GetInteger( p_demux, "screen-caching" ) *1000;
301             return VLC_SUCCESS;
302
303         case DEMUX_GET_TIME:
304             pi64 = (int64_t*)va_arg( args, int64_t * );
305             *pi64 = mdate();
306             return VLC_SUCCESS;
307
308         /* TODO implement others */
309         default:
310             return VLC_EGENERIC;
311     }
312 }
313
314 #ifdef SCREEN_SUBSCREEN
315 void FollowMouse( demux_sys_t *p_sys, int i_x, int i_y )
316 {
317     i_x -= p_sys->i_width/2;
318     if( i_x < 0 ) i_x = 0;
319     p_sys->i_left = __MIN( (unsigned int)i_x,
320     p_sys->i_screen_width - p_sys->i_width );
321
322     i_y -= p_sys->i_height/2;
323     if( i_y < 0 ) i_y = 0;
324     p_sys->i_top = __MIN( (unsigned int)i_y,
325     p_sys->i_screen_height - p_sys->i_height );
326 }
327 #endif
328
329 #ifdef SCREEN_MOUSE
330 void RenderCursor( demux_t *p_demux, int i_x, int i_y,
331                    uint8_t *p_dst )
332 {
333     demux_sys_t *p_sys = p_demux->p_sys;
334     if( !p_sys->dst.i_planes )
335         picture_Setup( &p_sys->dst,
336                        p_sys->fmt.video.i_chroma,
337                        p_sys->fmt.video.i_width,
338                        p_sys->fmt.video.i_height,
339                        p_sys->fmt.video.i_sar_num,
340                        p_sys->fmt.video.i_sar_den );
341     if( !p_sys->p_blend )
342     {
343         p_sys->p_blend = vlc_object_create( p_demux, sizeof(filter_t) );
344         if( p_sys->p_blend )
345         {
346             es_format_Init( &p_sys->p_blend->fmt_in, VIDEO_ES,
347                             VLC_CODEC_RGBA );
348             p_sys->p_blend->fmt_in.video = p_sys->p_mouse->format;
349             p_sys->p_blend->fmt_out = p_sys->fmt;
350             p_sys->p_blend->p_module =
351                 module_need( p_sys->p_blend, "video blending", NULL, false );
352             if( !p_sys->p_blend->p_module )
353             {
354                 msg_Err( p_demux, "Could not load video blending module" );
355                 vlc_object_release( p_sys->p_blend );
356                 p_sys->p_blend = NULL;
357             }
358         }
359     }
360     if( p_sys->p_blend )
361     {
362         p_sys->dst.p->p_pixels = p_dst;
363         p_sys->p_blend->pf_video_blend( p_sys->p_blend,
364                                         &p_sys->dst,
365                                         p_sys->p_mouse,
366 #ifdef SCREEN_SUBSCREEN
367                                         i_x-p_sys->i_left,
368 #else
369                                         i_x,
370 #endif
371 #ifdef SCREEN_SUBSCREEN
372                                         i_y-p_sys->i_top,
373 #else
374                                         i_y,
375 #endif
376                                         255 );
377     }
378     else
379     {
380         picture_Release( p_sys->p_mouse );
381         p_sys->p_mouse = NULL;
382     }
383 }
384 #endif