1 /*****************************************************************************
2 * win32.c: Screen capture module.
3 *****************************************************************************
4 * Copyright (C) 2004-2008 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
37 # define CAPTUREBLT (DWORD)0x40000000 /* Include layered windows */
52 int screen_InitCapture( demux_t *p_demux )
54 demux_sys_t *p_sys = p_demux->p_sys;
55 screen_data_t *p_data;
56 int i_chroma, i_bits_per_pixel;
58 p_sys->p_data = p_data = malloc( sizeof( screen_data_t ) );
61 memset( p_data, 0, sizeof( screen_data_t ) );
63 /* Get the device context for the whole screen */
64 p_data->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
65 if( !p_data->hdc_src )
67 msg_Err( p_demux, "cannot get device context" );
71 p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
72 if( !p_data->hdc_dst )
74 msg_Err( p_demux, "cannot get compat device context" );
75 ReleaseDC( 0, p_data->hdc_src );
79 i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
80 switch( i_bits_per_pixel )
82 case 8: /* FIXME: set the palette */
83 i_chroma = VLC_FOURCC('R','G','B','2'); break;
85 case 16: /* Yes it is really 15 bits (when using BI_RGB) */
86 i_chroma = VLC_FOURCC('R','V','1','5'); break;
88 i_chroma = VLC_FOURCC('R','V','2','4'); break;
90 i_chroma = VLC_FOURCC('R','V','3','2'); break;
92 msg_Err( p_demux, "unknown screen depth %i",
93 p_sys->fmt.video.i_bits_per_pixel );
94 ReleaseDC( 0, p_data->hdc_src );
95 ReleaseDC( 0, p_data->hdc_dst );
99 es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
100 p_sys->fmt.video.i_visible_width =
101 p_sys->fmt.video.i_width = GetDeviceCaps( p_data->hdc_src, HORZRES );
102 p_sys->fmt.video.i_visible_height =
103 p_sys->fmt.video.i_height = GetDeviceCaps( p_data->hdc_src, VERTRES );
104 p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
105 p_sys->fmt.video.i_chroma = i_chroma;
109 case VLC_FOURCC('R','V','1','5'):
110 p_sys->fmt.video.i_rmask = 0x7c00;
111 p_sys->fmt.video.i_gmask = 0x03e0;
112 p_sys->fmt.video.i_bmask = 0x001f;
114 case VLC_FOURCC('R','V','2','4'):
115 p_sys->fmt.video.i_rmask = 0x00ff0000;
116 p_sys->fmt.video.i_gmask = 0x0000ff00;
117 p_sys->fmt.video.i_bmask = 0x000000ff;
119 case VLC_FOURCC('R','V','3','2'):
120 p_sys->fmt.video.i_rmask = 0x00ff0000;
121 p_sys->fmt.video.i_gmask = 0x0000ff00;
122 p_sys->fmt.video.i_bmask = 0x000000ff;
125 msg_Warn( p_demux, "Unknown RGB masks" );
133 int screen_CloseCapture( demux_t *p_demux )
135 demux_sys_t *p_sys = p_demux->p_sys;
136 screen_data_t *p_data = p_sys->p_data;
138 if( p_data->p_block ) block_Release( p_data->p_block );
140 if( p_data->hgdi_backup)
141 SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
143 DeleteDC( p_data->hdc_dst );
144 ReleaseDC( 0, p_data->hdc_src );
156 static void CaptureBlockRelease( block_t *p_block )
158 DeleteObject( ((block_sys_t *)p_block)->hbmp );
162 static block_t *CaptureBlockNew( demux_t *p_demux )
164 demux_sys_t *p_sys = p_demux->p_sys;
165 screen_data_t *p_data = p_sys->p_data;
166 block_sys_t *p_block;
171 if( p_data->bmi.bmiHeader.biSize == 0 )
174 /* Create the bitmap info header */
175 p_data->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
176 p_data->bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
177 p_data->bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
178 p_data->bmi.bmiHeader.biPlanes = 1;
179 p_data->bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
180 p_data->bmi.bmiHeader.biCompression = BI_RGB;
181 p_data->bmi.bmiHeader.biSizeImage = 0;
182 p_data->bmi.bmiHeader.biXPelsPerMeter =
183 p_data->bmi.bmiHeader.biYPelsPerMeter = 0;
184 p_data->bmi.bmiHeader.biClrUsed = 0;
185 p_data->bmi.bmiHeader.biClrImportant = 0;
187 var_Create( p_demux, "screen-fragment-size",
188 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
189 var_Get( p_demux, "screen-fragment-size", &val );
190 p_data->i_fragment_size =
191 val.i_int > 0 ? val.i_int : p_sys->fmt.video.i_height;
192 p_data->i_fragment_size =
193 val.i_int > p_sys->fmt.video.i_height ? p_sys->fmt.video.i_height :
194 p_data->i_fragment_size;
195 p_sys->f_fps *= (p_sys->fmt.video.i_height/p_data->i_fragment_size);
196 p_sys->i_incr = 1000000 / p_sys->f_fps;
197 p_data->i_fragment = 0;
202 /* Create the bitmap storage space */
203 hbmp = CreateDIBSection( p_data->hdc_dst, &p_data->bmi, DIB_RGB_COLORS,
204 &p_buffer, NULL, 0 );
205 if( !hbmp || !p_buffer )
207 msg_Err( p_demux, "cannot create bitmap" );
208 if( hbmp ) DeleteObject( hbmp );
212 /* Select the bitmap into the compatible DC */
213 if( !p_data->hgdi_backup )
214 p_data->hgdi_backup = SelectObject( p_data->hdc_dst, hbmp );
216 SelectObject( p_data->hdc_dst, hbmp );
218 if( !p_data->hgdi_backup )
220 msg_Err( p_demux, "cannot select bitmap" );
221 DeleteObject( hbmp );
226 if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
228 DeleteObject( hbmp );
231 /* Fill all fields */
232 i_buffer = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
233 p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
234 block_Init( &p_block->self, p_buffer, i_buffer );
235 p_block->self.pf_release = CaptureBlockRelease;
236 p_block->hbmp = hbmp;
238 return &p_block->self;
241 block_t *screen_Capture( demux_t *p_demux )
243 demux_sys_t *p_sys = p_demux->p_sys;
244 screen_data_t *p_data = p_sys->p_data;
246 if( !p_data->i_fragment )
248 if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
250 msg_Warn( p_demux, "cannot get block" );
255 if( p_sys->b_follow_mouse )
258 GetCursorPos( &pos );
259 FollowMouse( p_sys, pos.x, pos.y );
262 if( !BitBlt( p_data->hdc_dst, 0,
263 p_data->i_fragment * p_data->i_fragment_size,
264 p_sys->fmt.video.i_width, p_data->i_fragment_size,
265 p_data->hdc_src, p_sys->i_left, p_sys->i_top +
266 p_data->i_fragment * p_data->i_fragment_size,
267 IS_WINNT ? SRCCOPY | CAPTUREBLT : SRCCOPY ) )
269 msg_Err( p_demux, "error during BitBlt()" );
273 p_data->i_fragment++;
275 if( !( p_data->i_fragment %
276 (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
278 block_t *p_block = p_data->p_block;
279 p_data->i_fragment = 0;
285 GetCursorPos( &pos );
286 RenderCursor( p_demux, pos.x, pos.y,