1 /*****************************************************************************
2 * screen.c: Screen capture module.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
30 #include <vlc/input.h>
33 # include <X11/Xlib.h>
34 # include <X11/Xutil.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 #define CACHING_TEXT N_("Caching value in ms")
41 #define CACHING_LONGTEXT N_( \
42 "Allows you to modify the default caching value for screen capture " \
43 "streams. This value should be set in millisecond units." )
44 #define FPS_TEXT N_("Frame rate")
45 #define FPS_LONGTEXT N_( \
46 "Allows you to set the desired frame rate for the capture." )
48 static int Open ( vlc_object_t * );
49 static void Close( vlc_object_t * );
52 set_description( _("Screen Input") );
54 add_integer( "screen-caching", DEFAULT_PTS_DELAY / 1000, NULL,
55 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
56 add_float( "screen-fps", 5, NULL, FPS_TEXT, FPS_LONGTEXT, VLC_TRUE );
58 set_capability( "access_demux", 0 );
59 add_shortcut( "screen" );
60 set_callbacks( Open, Close );
63 /*****************************************************************************
65 *****************************************************************************/
87 static int Control( demux_t *, int, va_list );
88 static int Demux ( demux_t * );
90 static int InitCapture ( demux_t * );
91 static int CloseCapture( demux_t * );
92 static block_t *Capture( demux_t * );
94 /*****************************************************************************
96 *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
99 demux_t *p_demux = (demux_t*)p_this;
103 /* Fill p_demux field */
104 p_demux->pf_demux = Demux;
105 p_demux->pf_control = Control;
106 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
107 memset( p_sys, 0, sizeof( demux_sys_t ) );
109 if( InitCapture( p_demux ) != VLC_SUCCESS )
115 msg_Dbg( p_demux, "screen width: %i, height: %i, depth: %i",
116 p_sys->fmt.video.i_width, p_sys->fmt.video.i_height,
117 p_sys->fmt.video.i_bits_per_pixel );
119 p_sys->es = es_out_Add( p_demux->out, &p_sys->fmt );
121 /* Update default_pts to a suitable value for screen access */
122 var_Create( p_demux, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
124 var_Create( p_demux, "screen-fps", VLC_VAR_FLOAT|VLC_VAR_DOINHERIT );
125 var_Get( p_demux, "screen-fps", &val );
126 p_sys->f_fps = val.f_float;
127 p_sys->i_incr = 1000000 / val.f_float;
128 p_sys->i_next_date = 0;
133 /*****************************************************************************
135 *****************************************************************************/
136 static void Close( vlc_object_t *p_this )
138 demux_t *p_demux = (demux_t*)p_this;
139 demux_sys_t *p_sys = p_demux->p_sys;
141 CloseCapture( p_demux );
145 /*****************************************************************************
147 *****************************************************************************/
148 static int Demux( demux_t *p_demux )
150 demux_sys_t *p_sys = p_demux->p_sys;
153 if( !p_sys->i_next_date ) p_sys->i_next_date = mdate();
155 /* Frame skipping if necessary */
156 while( mdate() >= p_sys->i_next_date + p_sys->i_incr )
157 p_sys->i_next_date += p_sys->i_incr;
159 mwait( p_sys->i_next_date );
160 p_block = Capture( p_demux );
161 if( !p_block ) return 0;
163 p_block->i_dts = p_block->i_pts = p_sys->i_next_date;
165 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
166 es_out_Send( p_demux->out, p_sys->es, p_block );
168 p_sys->i_next_date += p_sys->i_incr;
173 /*****************************************************************************
174 * Platform dependant capture functions
175 *****************************************************************************/
177 static int InitCapture( demux_t *p_demux )
179 demux_sys_t *p_sys = p_demux->p_sys;
180 int i_chroma, i_bits_per_pixel;
184 /* Get the device context for the whole screen */
185 p_sys->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
186 if( !p_sys->hdc_src )
188 msg_Err( p_demux, "cannot get device context" );
192 p_sys->hdc_dst = CreateCompatibleDC( p_sys->hdc_src );
193 if( !p_sys->hdc_dst )
195 msg_Err( p_demux, "cannot get compat device context" );
196 ReleaseDC( 0, p_sys->hdc_src );
200 i_bits_per_pixel = GetDeviceCaps( p_sys->hdc_src, BITSPIXEL );
201 switch( i_bits_per_pixel )
203 case 8: /* FIXME: set the palette */
204 i_chroma = VLC_FOURCC('R','G','B','2'); break;
206 i_chroma = VLC_FOURCC('R','V','1','5'); break;
208 i_chroma = VLC_FOURCC('R','V','1','6'); break;
210 i_chroma = VLC_FOURCC('R','V','2','4'); break;
212 i_chroma = VLC_FOURCC('R','V','3','2'); break;
214 msg_Err( p_demux, "unknown screen depth %i",
215 p_sys->fmt.video.i_bits_per_pixel );
216 ReleaseDC( 0, p_sys->hdc_src );
217 ReleaseDC( 0, p_sys->hdc_dst );
221 #if 1 /* For now we force RV24 because of chroma inversion in the other cases*/
222 i_chroma = VLC_FOURCC('R','V','2','4');
223 i_bits_per_pixel = 24;
226 es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
227 p_sys->fmt.video.i_width = GetDeviceCaps( p_sys->hdc_src, HORZRES );
228 p_sys->fmt.video.i_height = GetDeviceCaps( p_sys->hdc_src, VERTRES );
229 p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
231 /* Create the bitmap info header */
232 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
233 bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
234 bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
235 bmi.bmiHeader.biPlanes = 1;
236 bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
237 bmi.bmiHeader.biCompression = BI_RGB;
238 bmi.bmiHeader.biSizeImage = 0;
239 bmi.bmiHeader.biXPelsPerMeter =
240 bmi.bmiHeader.biYPelsPerMeter = 0;
241 bmi.bmiHeader.biClrUsed = 0;
242 bmi.bmiHeader.biClrImportant = 0;
244 /* Create the bitmap storage space */
245 p_sys->hbmp = CreateDIBSection( p_sys->hdc_dst, (BITMAPINFO *)&bmi,
246 DIB_RGB_COLORS, (void **)&p_sys->p_buffer, NULL, 0 );
247 if( !p_sys->hbmp || !p_sys->p_buffer )
249 msg_Err( p_demux, "cannot create bitmap" );
250 if( p_sys->hbmp ) DeleteObject( p_sys->hbmp );
251 ReleaseDC( 0, p_sys->hdc_src );
252 DeleteDC( p_sys->hdc_dst );
256 /* Select the bitmap into the compatible DC */
257 p_sys->hgdi_backup = SelectObject( p_sys->hdc_dst, p_sys->hbmp );
258 if( !p_sys->hgdi_backup )
260 msg_Err( p_demux, "cannot select bitmap" );
267 static int InitCapture( demux_t *p_demux )
269 demux_sys_t *p_sys = p_demux->p_sys;
270 XWindowAttributes win_info;
273 /* Open the display */
274 p_sys->p_display = XOpenDisplay( NULL );
275 if( !p_sys->p_display )
277 msg_Err( p_demux, "cannot open display" );
281 /* Get the parameters of the root window */
282 if( !XGetWindowAttributes( p_sys->p_display,
283 DefaultRootWindow( p_sys->p_display ),
286 msg_Err( p_demux, "can't get root window attributes" );
287 XCloseDisplay( p_sys->p_display );
291 switch( win_info.depth )
293 case 8: /* FIXME: set the palette */
294 i_chroma = VLC_FOURCC('R','G','B','2'); break;
296 i_chroma = VLC_FOURCC('R','V','1','5'); break;
298 i_chroma = VLC_FOURCC('R','V','1','6'); break;
300 i_chroma = VLC_FOURCC('R','V','2','4'); break;
302 i_chroma = VLC_FOURCC('R','V','3','2'); break;
304 msg_Err( p_demux, "unknown screen depth %i", win_info.depth );
305 XCloseDisplay( p_sys->p_display );
309 es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
310 p_sys->fmt.video.i_width = win_info.width;
311 p_sys->fmt.video.i_height = win_info.height;
312 p_sys->fmt.video.i_bits_per_pixel = win_info.depth;
315 win_info.visual->red_mask;
316 win_info.visual->green_mask;
317 win_info.visual->blue_mask;
318 win_info.visual->bits_per_rgb;
326 static int CloseCapture( demux_t *p_demux )
328 demux_sys_t *p_sys = p_demux->p_sys;
330 SelectObject( p_sys->hdc_dst, p_sys->hgdi_backup );
331 DeleteObject( p_sys->hbmp );
332 DeleteDC( p_sys->hdc_dst );
333 ReleaseDC( 0, p_sys->hdc_src );
338 static int CloseCapture( demux_t *p_demux )
340 demux_sys_t *p_sys = p_demux->p_sys;
342 XCloseDisplay( p_sys->p_display );
348 static block_t *Capture( demux_t *p_demux )
350 demux_sys_t *p_sys = p_demux->p_sys;
354 if( !BitBlt( p_sys->hdc_dst, 0, 0,
355 p_sys->fmt.video.i_width, p_sys->fmt.video.i_height,
356 p_sys->hdc_src, 0, 0, SRCCOPY ) )
358 msg_Err( p_demux, "error during BitBlt()" );
362 i_size = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
363 p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
365 if( !( p_block = block_New( p_demux, i_size ) ) )
367 msg_Warn( p_demux, "cannot get block" );
371 memcpy( p_block->p_buffer, p_sys->p_buffer, i_size );
377 static block_t *Capture( demux_t *p_demux )
379 demux_sys_t *p_sys = p_demux->p_sys;
384 image = XGetImage( p_sys->p_display, DefaultRootWindow( p_sys->p_display ),
385 0, 0, p_sys->fmt.video.i_width,
386 p_sys->fmt.video.i_height, AllPlanes, ZPixmap );
390 msg_Warn( p_demux, "cannot get image" );
394 i_size = image->bytes_per_line * image->height;
396 if( !( p_block = block_New( p_demux, i_size ) ) )
398 msg_Warn( p_demux, "cannot get block" );
399 XDestroyImage( image );
403 memcpy( p_block->p_buffer, image->data, i_size );
405 XDestroyImage( image );
411 /*****************************************************************************
413 *****************************************************************************/
414 static int Control( demux_t *p_demux, int i_query, va_list args )
421 /* Special for access_demux */
422 case DEMUX_CAN_PAUSE:
423 case DEMUX_CAN_CONTROL_PACE:
425 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
429 case DEMUX_GET_PTS_DELAY:
430 pi64 = (int64_t*)va_arg( args, int64_t * );
431 *pi64 = (int64_t)var_GetInteger( p_demux, "screen-caching" ) *1000;
434 /* TODO implement others */