1 /*****************************************************************************
2 * screen.c: Screen capture module.
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Antoine Cellerier <dionoea at videolan dot org>
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.
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.
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
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 #define CACHING_TEXT N_("Caching value in ms")
41 #define CACHING_LONGTEXT N_( \
42 "Caching value for screen capture. "\
43 "This value should be set in milliseconds." )
44 #define FPS_TEXT N_("Frame rate")
45 #define FPS_LONGTEXT N_( \
46 "Desired frame rate for the capture." )
49 #define FRAGS_TEXT N_("Capture fragment size")
50 #define FRAGS_LONGTEXT N_( \
51 "Optimize the capture by fragmenting the screen in chunks " \
52 "of predefined height (16 might be a good value, and 0 means disabled)." )
55 #ifdef SCREEN_SUBSCREEN
56 #define TOP_TEXT N_( "Subscreen top left corner" )
57 #define TOP_LONGTEXT N_( \
58 "Top coordinate of the subscreen top left corner." )
60 #define LEFT_TEXT N_( "Subscreen top left corner" )
61 #define LEFT_LONGTEXT N_( \
62 "Left coordinate of the subscreen top left corner." )
64 #define WIDTH_TEXT N_( "Subscreen width" )
65 #define WIDTH_LONGTEXT N_( \
68 #define HEIGHT_TEXT N_( "Subscreen height" )
69 #define HEIGHT_LONGTEXT N_( \
72 #define FOLLOW_MOUSE_TEXT N_( "Follow the mouse" )
73 #define FOLLOW_MOUSE_LONGTEXT N_( \
74 "Follow the mouse when capturing a subscreen." )
78 #define MOUSE_TEXT N_( "Mouse pointer image" )
79 #define MOUSE_LONGTEXT N_( \
80 "If specifed, will use the image to draw the mouse pointer on the " \
84 static int Open ( vlc_object_t * );
85 static void Close( vlc_object_t * );
94 set_description( N_("Screen Input") )
95 set_shortname( N_("Screen" ))
96 set_category( CAT_INPUT )
97 set_subcategory( SUBCAT_INPUT_ACCESS )
99 add_integer( "screen-caching", DEFAULT_PTS_DELAY / 1000, NULL,
100 CACHING_TEXT, CACHING_LONGTEXT, true )
101 add_float( "screen-fps", SCREEN_FPS, 0, FPS_TEXT, FPS_LONGTEXT, true )
103 #ifdef SCREEN_SUBSCREEN
104 add_integer( "screen-top", 0, NULL, TOP_TEXT, TOP_LONGTEXT, true )
105 add_integer( "screen-left", 0, NULL, LEFT_TEXT, LEFT_LONGTEXT, true )
106 add_integer( "screen-width", 0, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true )
107 add_integer( "screen-height", 0, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT, true )
108 add_bool( "screen-follow-mouse", false, NULL, FOLLOW_MOUSE_TEXT,
109 FOLLOW_MOUSE_LONGTEXT, true )
113 add_file( "screen-mouse-image", "", NULL, MOUSE_TEXT, MOUSE_LONGTEXT,
118 add_integer( "screen-fragment-size", 0, NULL, FRAGS_TEXT,
119 FRAGS_LONGTEXT, true )
122 set_capability( "access_demux", 0 )
123 add_shortcut( "screen" )
124 set_callbacks( Open, Close )
127 /*****************************************************************************
129 *****************************************************************************/
130 static int Control( demux_t *, int, va_list );
131 static int Demux ( demux_t * );
133 /*****************************************************************************
135 *****************************************************************************/
136 static int Open( vlc_object_t *p_this )
138 demux_t *p_demux = (demux_t*)p_this;
142 /* Fill p_demux field */
143 p_demux->pf_demux = Demux;
144 p_demux->pf_control = Control;
145 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
149 /* Update default_pts to a suitable value for screen access */
150 var_Create( p_demux, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
152 var_Create( p_demux, "screen-fps", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT );
153 var_Get( p_demux, "screen-fps", &val );
154 p_sys->f_fps = val.f_float;
155 p_sys->i_incr = 1000000 / val.f_float;
156 p_sys->i_next_date = 0;
158 #ifdef SCREEN_SUBSCREEN
159 p_sys->i_top = var_CreateGetInteger( p_demux, "screen-top" );
160 p_sys->i_left = var_CreateGetInteger( p_demux, "screen-left" );
161 p_sys->i_width = var_CreateGetInteger( p_demux, "screen-width" );
162 p_sys->i_height = var_CreateGetInteger( p_demux, "screen-height" );
163 if( p_sys->i_width > 0 && p_sys->i_height > 0 )
164 msg_Dbg( p_demux, "capturing subscreen top: %d, left: %d, "
165 "width: %d, height: %d",
172 if( screen_InitCapture( p_demux ) != VLC_SUCCESS )
178 msg_Dbg( p_demux, "screen width: %i, height: %i, depth: %i",
179 p_sys->fmt.video.i_width, p_sys->fmt.video.i_height,
180 p_sys->fmt.video.i_bits_per_pixel );
182 #ifdef SCREEN_SUBSCREEN
183 if( p_sys->i_width > 0 && p_sys->i_height > 0 )
185 if( p_sys->i_left + p_sys->i_width > p_sys->fmt.video.i_width ||
186 p_sys->i_top + p_sys->i_height > p_sys->fmt.video.i_height )
188 msg_Err( p_demux, "subscreen region overflows the screen" );
194 p_sys->i_screen_width = p_sys->fmt.video.i_width;
195 p_sys->i_screen_height = p_sys->fmt.video.i_height;
196 p_sys->fmt.video.i_visible_width =
197 p_sys->fmt.video.i_width = p_sys->i_width;
198 p_sys->fmt.video.i_visible_height =
199 p_sys->fmt.video.i_height = p_sys->i_height;
200 p_sys->b_follow_mouse = var_CreateGetInteger( p_demux,
201 "screen-follow-mouse" );
202 if( p_sys->b_follow_mouse )
203 msg_Dbg( p_demux, "mouse following enabled" );
209 char * psz_mouse = var_CreateGetNonEmptyString( p_demux,
210 "screen-mouse-image" );
213 image_handler_t *p_image;
214 video_format_t fmt_in, fmt_out;
215 msg_Dbg( p_demux, "Using %s for the mouse pointer image", psz_mouse );
216 memset( &fmt_in, 0, sizeof( fmt_in ) );
217 memset( &fmt_out, 0, sizeof( fmt_out ) );
218 fmt_out.i_chroma = VLC_FOURCC('R','G','B','A');
219 p_image = image_HandlerCreate( p_demux );
223 image_ReadUrl( p_image, psz_mouse, &fmt_in, &fmt_out );
224 image_HandlerDelete( p_image );
226 if( !p_sys->p_mouse )
227 msg_Err( p_demux, "Failed to open mouse pointer image (%s)",
233 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
238 /*****************************************************************************
240 *****************************************************************************/
241 static void Close( vlc_object_t *p_this )
243 demux_t *p_demux = (demux_t*)p_this;
244 demux_sys_t *p_sys = p_demux->p_sys;
246 screen_CloseCapture( p_demux );
249 picture_Release( p_sys->p_mouse );
254 /*****************************************************************************
256 *****************************************************************************/
257 static int Demux( demux_t *p_demux )
259 demux_sys_t *p_sys = p_demux->p_sys;
262 if( !p_sys->i_next_date ) p_sys->i_next_date = mdate();
264 /* Frame skipping if necessary */
265 while( mdate() >= p_sys->i_next_date + p_sys->i_incr )
266 p_sys->i_next_date += p_sys->i_incr;
268 mwait( p_sys->i_next_date );
269 p_block = screen_Capture( p_demux );
272 p_sys->i_next_date += p_sys->i_incr;
276 p_block->i_dts = p_block->i_pts = p_sys->i_next_date;
278 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
279 es_out_Send( p_demux->out, p_sys->es, p_block );
281 p_sys->i_next_date += p_sys->i_incr;
286 /*****************************************************************************
288 *****************************************************************************/
289 static int Control( demux_t *p_demux, int i_query, va_list args )
296 /* Special for access_demux */
297 case DEMUX_CAN_PAUSE:
299 case DEMUX_CAN_CONTROL_PACE:
301 pb = (bool*)va_arg( args, bool * );
305 case DEMUX_GET_PTS_DELAY:
306 pi64 = (int64_t*)va_arg( args, int64_t * );
307 *pi64 = (int64_t)var_GetInteger( p_demux, "screen-caching" ) *1000;
311 pi64 = (int64_t*)va_arg( args, int64_t * );
315 /* TODO implement others */
321 #ifdef SCREEN_SUBSCREEN
322 void FollowMouse( demux_sys_t *p_sys, int i_x, int i_y )
324 i_x -= p_sys->i_width/2;
325 if( i_x < 0 ) i_x = 0;
326 p_sys->i_left = __MIN( (unsigned int)i_x,
327 p_sys->i_screen_width - p_sys->i_width );
329 i_y -= p_sys->i_height/2;
330 if( i_y < 0 ) i_y = 0;
331 p_sys->i_top = __MIN( (unsigned int)i_y,
332 p_sys->i_screen_height - p_sys->i_height );
337 void RenderCursor( demux_t *p_demux, int i_x, int i_y,
340 demux_sys_t *p_sys = p_demux->p_sys;
341 if( !p_sys->dst.i_planes )
342 vout_InitPicture( p_demux, &p_sys->dst,
343 p_sys->fmt.video.i_chroma,
344 p_sys->fmt.video.i_width,
345 p_sys->fmt.video.i_height,
346 p_sys->fmt.video.i_aspect );
347 if( !p_sys->p_blend )
349 p_sys->p_blend = vlc_object_create( p_demux, sizeof(filter_t) );
352 es_format_Init( &p_sys->p_blend->fmt_in, VIDEO_ES,
353 VLC_FOURCC('R','G','B','A') );
354 p_sys->p_blend->fmt_in.video = p_sys->p_mouse->format;
355 p_sys->p_blend->fmt_out = p_sys->fmt;
356 p_sys->p_blend->p_module =
357 module_need( p_sys->p_blend, "video blending", NULL, false );
358 if( !p_sys->p_blend->p_module )
360 msg_Err( p_demux, "Could not load video blending module" );
361 vlc_object_detach( p_sys->p_blend );
362 vlc_object_release( p_sys->p_blend );
363 p_sys->p_blend = NULL;
369 p_sys->dst.p->p_pixels = p_dst;
370 p_sys->p_blend->pf_video_blend( p_sys->p_blend,
373 #ifdef SCREEN_SUBSCREEN
378 #ifdef SCREEN_SUBSCREEN
387 picture_Release( p_sys->p_mouse );
388 p_sys->p_mouse = NULL;