1 /*****************************************************************************
2 * win32.c: Screen capture module.
3 *****************************************************************************
4 * Copyright (C) 2004-2011 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 Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc_common.h>
48 int screen_InitCapture( demux_t *p_demux )
50 demux_sys_t *p_sys = p_demux->p_sys;
51 screen_data_t *p_data;
52 int i_chroma, i_bits_per_pixel;
54 p_sys->p_data = p_data = calloc( 1, sizeof( screen_data_t ) );
58 /* Get the device context for the whole screen */
59 p_data->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
60 if( !p_data->hdc_src )
62 msg_Err( p_demux, "cannot get device context" );
67 p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
68 if( !p_data->hdc_dst )
70 msg_Err( p_demux, "cannot get compat device context" );
72 ReleaseDC( 0, p_data->hdc_src );
76 i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
77 switch( i_bits_per_pixel )
79 case 8: /* FIXME: set the palette */
80 i_chroma = VLC_CODEC_RGB8; break;
82 case 16: /* Yes it is really 15 bits (when using BI_RGB) */
83 i_chroma = VLC_CODEC_RGB15; break;
85 i_chroma = VLC_CODEC_RGB24; break;
87 i_chroma = VLC_CODEC_RGB32; break;
89 msg_Err( p_demux, "unknown screen depth %i", i_bits_per_pixel );
90 DeleteDC( p_data->hdc_dst );
91 ReleaseDC( 0, p_data->hdc_src );
96 es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
97 p_sys->fmt.video.i_visible_width =
98 p_sys->fmt.video.i_width = GetDeviceCaps( p_data->hdc_src, HORZRES );
99 p_sys->fmt.video.i_visible_height =
100 p_sys->fmt.video.i_height = GetDeviceCaps( p_data->hdc_src, VERTRES );
101 p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
102 p_sys->fmt.video.i_chroma = i_chroma;
106 case VLC_CODEC_RGB15:
107 p_sys->fmt.video.i_rmask = 0x7c00;
108 p_sys->fmt.video.i_gmask = 0x03e0;
109 p_sys->fmt.video.i_bmask = 0x001f;
111 case VLC_CODEC_RGB24:
112 p_sys->fmt.video.i_rmask = 0x00ff0000;
113 p_sys->fmt.video.i_gmask = 0x0000ff00;
114 p_sys->fmt.video.i_bmask = 0x000000ff;
116 case VLC_CODEC_RGB32:
117 p_sys->fmt.video.i_rmask = 0x00ff0000;
118 p_sys->fmt.video.i_gmask = 0x0000ff00;
119 p_sys->fmt.video.i_bmask = 0x000000ff;
122 msg_Warn( p_demux, "Unknown RGB masks" );
129 int screen_CloseCapture( demux_t *p_demux )
131 demux_sys_t *p_sys = p_demux->p_sys;
132 screen_data_t *p_data = p_sys->p_data;
134 if( p_data->p_block ) block_Release( p_data->p_block );
136 if( p_data->hgdi_backup)
137 SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
139 DeleteDC( p_data->hdc_dst );
140 ReleaseDC( 0, p_data->hdc_src );
152 static void CaptureBlockRelease( block_t *p_block )
154 DeleteObject( ((block_sys_t *)p_block)->hbmp );
158 static block_t *CaptureBlockNew( demux_t *p_demux )
160 demux_sys_t *p_sys = p_demux->p_sys;
161 screen_data_t *p_data = p_sys->p_data;
162 block_sys_t *p_block;
167 if( p_data->bmi.bmiHeader.biSize == 0 )
170 /* Create the bitmap info header */
171 p_data->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
172 p_data->bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
173 p_data->bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
174 p_data->bmi.bmiHeader.biPlanes = 1;
175 p_data->bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
176 p_data->bmi.bmiHeader.biCompression = BI_RGB;
177 p_data->bmi.bmiHeader.biSizeImage = 0;
178 p_data->bmi.bmiHeader.biXPelsPerMeter = 0;
179 p_data->bmi.bmiHeader.biYPelsPerMeter = 0;
180 p_data->bmi.bmiHeader.biClrUsed = 0;
181 p_data->bmi.bmiHeader.biClrImportant = 0;
183 i_val = var_CreateGetInteger( p_demux, "screen-fragment-size" );
184 p_data->i_fragment_size = i_val > 0 ? i_val : (int)p_sys->fmt.video.i_height;
185 p_data->i_fragment_size = i_val > (int)p_sys->fmt.video.i_height ?
186 (int)p_sys->fmt.video.i_height :
187 p_data->i_fragment_size;
188 p_sys->f_fps *= (p_sys->fmt.video.i_height/p_data->i_fragment_size);
189 p_sys->i_incr = 1000000 / p_sys->f_fps;
190 p_data->i_fragment = 0;
195 /* Create the bitmap storage space */
196 hbmp = CreateDIBSection( p_data->hdc_dst, &p_data->bmi, DIB_RGB_COLORS,
197 &p_buffer, NULL, 0 );
198 if( !hbmp || !p_buffer )
200 msg_Err( p_demux, "cannot create bitmap" );
204 /* Select the bitmap into the compatible DC */
205 if( !p_data->hgdi_backup )
206 p_data->hgdi_backup = SelectObject( p_data->hdc_dst, hbmp );
208 SelectObject( p_data->hdc_dst, hbmp );
210 if( !p_data->hgdi_backup )
212 msg_Err( p_demux, "cannot select bitmap" );
217 if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
220 /* Fill all fields */
221 i_buffer = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
222 p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
223 block_Init( &p_block->self, p_buffer, i_buffer );
224 p_block->self.pf_release = CaptureBlockRelease;
225 p_block->hbmp = hbmp;
227 return &p_block->self;
230 if( hbmp ) DeleteObject( hbmp );
234 block_t *screen_Capture( demux_t *p_demux )
236 demux_sys_t *p_sys = p_demux->p_sys;
237 screen_data_t *p_data = p_sys->p_data;
239 if( !p_data->i_fragment )
241 if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
243 msg_Warn( p_demux, "cannot get block" );
248 if( p_sys->b_follow_mouse )
251 GetCursorPos( &pos );
252 FollowMouse( p_sys, pos.x, pos.y );
255 if( !BitBlt( p_data->hdc_dst, 0,
256 p_data->i_fragment * p_data->i_fragment_size,
257 p_sys->fmt.video.i_width, p_data->i_fragment_size,
258 p_data->hdc_src, p_sys->i_left, p_sys->i_top +
259 p_data->i_fragment * p_data->i_fragment_size,
260 SRCCOPY | CAPTUREBLT ) )
262 msg_Err( p_demux, "error during BitBlt()" );
266 p_data->i_fragment++;
268 if( !( p_data->i_fragment %
269 (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
271 block_t *p_block = p_data->p_block;
272 p_data->i_fragment = 0;
278 GetCursorPos( &pos );
279 RenderCursor( p_demux, pos.x, pos.y,