1 /*****************************************************************************
2 * win32.c: Screen capture module.
3 *****************************************************************************
4 * Copyright (C) 2004 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 *****************************************************************************/
33 # define CAPTUREBLT (DWORD)0x40000000 /* Include layered windows */
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;
55 p_sys->p_data = p_data = malloc( sizeof( screen_data_t ) );
57 /* Get the device context for the whole screen */
58 p_data->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
59 if( !p_data->hdc_src )
61 msg_Err( p_demux, "cannot get device context" );
65 p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
66 if( !p_data->hdc_dst )
68 msg_Err( p_demux, "cannot get compat device context" );
69 ReleaseDC( 0, p_data->hdc_src );
73 i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
74 switch( i_bits_per_pixel )
76 case 8: /* FIXME: set the palette */
77 i_chroma = VLC_FOURCC('R','G','B','2'); break;
79 i_chroma = VLC_FOURCC('R','V','1','5'); break;
81 i_chroma = VLC_FOURCC('R','V','1','6'); break;
83 i_chroma = VLC_FOURCC('R','V','2','4'); break;
85 i_chroma = VLC_FOURCC('R','V','3','2'); break;
87 msg_Err( p_demux, "unknown screen depth %i",
88 p_sys->fmt.video.i_bits_per_pixel );
89 ReleaseDC( 0, p_data->hdc_src );
90 ReleaseDC( 0, p_data->hdc_dst );
94 #if 1 /* For now we force RV24 because of chroma inversion in the other cases*/
95 i_chroma = VLC_FOURCC('R','V','2','4');
96 i_bits_per_pixel = 24;
99 es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
100 p_sys->fmt.video.i_width = GetDeviceCaps( p_data->hdc_src, HORZRES );
101 p_sys->fmt.video.i_height = GetDeviceCaps( p_data->hdc_src, VERTRES );
102 p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
104 /* Create the bitmap info header */
105 p_data->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
106 p_data->bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
107 p_data->bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
108 p_data->bmi.bmiHeader.biPlanes = 1;
109 p_data->bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
110 p_data->bmi.bmiHeader.biCompression = BI_RGB;
111 p_data->bmi.bmiHeader.biSizeImage = 0;
112 p_data->bmi.bmiHeader.biXPelsPerMeter =
113 p_data->bmi.bmiHeader.biYPelsPerMeter = 0;
114 p_data->bmi.bmiHeader.biClrUsed = 0;
115 p_data->bmi.bmiHeader.biClrImportant = 0;
117 if( i_chroma == VLC_FOURCC('R','V','2','4') )
119 /* This is in BGR format */
120 p_sys->fmt.video.i_bmask = 0x00ff0000;
121 p_sys->fmt.video.i_gmask = 0x0000ff00;
122 p_sys->fmt.video.i_rmask = 0x000000ff;
125 var_Create( p_demux, "screen-fragment-size",
126 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
127 var_Get( p_demux, "screen-fragment-size", &val );
128 p_data->i_fragment_size =
129 val.i_int > 0 ? val.i_int : p_sys->fmt.video.i_height;
130 p_data->i_fragment_size =
131 val.i_int > p_sys->fmt.video.i_height ? p_sys->fmt.video.i_height :
132 p_data->i_fragment_size;
133 p_sys->f_fps *= (p_sys->fmt.video.i_height/p_data->i_fragment_size);
134 p_sys->i_incr = 1000000 / p_sys->f_fps;
135 p_data->i_fragment = 0;
141 int screen_CloseCapture( demux_t *p_demux )
143 demux_sys_t *p_sys = p_demux->p_sys;
144 screen_data_t *p_data = p_sys->p_data;
146 if( p_data->p_block ) block_Release( p_data->p_block );
148 if( p_data->hgdi_backup)
149 SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
151 DeleteDC( p_data->hdc_dst );
152 ReleaseDC( 0, p_data->hdc_src );
163 static void CaptureBlockRelease( block_t *p_block )
165 DeleteObject( p_block->p_sys->hbmp );
169 static block_t *CaptureBlockNew( demux_t *p_demux )
171 demux_sys_t *p_sys = p_demux->p_sys;
172 screen_data_t *p_data = p_sys->p_data;
178 /* Create the bitmap storage space */
179 hbmp = CreateDIBSection( p_data->hdc_dst, &p_data->bmi, DIB_RGB_COLORS,
180 &p_buffer, NULL, 0 );
181 if( !hbmp || !p_buffer )
183 msg_Err( p_demux, "cannot create bitmap" );
184 if( hbmp ) DeleteObject( hbmp );
188 /* Select the bitmap into the compatible DC */
189 if( !p_data->hgdi_backup )
190 p_data->hgdi_backup = SelectObject( p_data->hdc_dst, hbmp );
192 SelectObject( p_data->hdc_dst, hbmp );
194 if( !p_data->hgdi_backup )
196 msg_Err( p_demux, "cannot select bitmap" );
197 DeleteObject( hbmp );
202 if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
204 DeleteObject( hbmp );
207 memset( p_block, 0, sizeof( block_t ) );
208 p_block->p_sys = (block_sys_t *)( (uint8_t *)p_block + sizeof( block_t ) );
210 /* Fill all fields */
211 i_buffer = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
212 p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
213 p_block->p_next = NULL;
214 p_block->i_buffer = i_buffer;
215 p_block->p_buffer = p_buffer;
216 p_block->pf_release = CaptureBlockRelease;
217 p_block->p_sys->hbmp = hbmp;
222 block_t *screen_Capture( demux_t *p_demux )
224 demux_sys_t *p_sys = p_demux->p_sys;
225 screen_data_t *p_data = p_sys->p_data;
227 if( !p_data->i_fragment )
229 if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
231 msg_Warn( p_demux, "cannot get block" );
236 if( !BitBlt( p_data->hdc_dst, 0, p_data->i_fragment *
237 p_data->i_fragment_size,
238 p_sys->fmt.video.i_width, p_data->i_fragment_size,
239 p_data->hdc_src, 0, p_data->i_fragment *
240 p_data->i_fragment_size,
241 IS_WINNT ? SRCCOPY | CAPTUREBLT : SRCCOPY ) )
243 msg_Err( p_demux, "error during BitBlt()" );
247 p_data->i_fragment++;
249 if( !( p_data->i_fragment %
250 (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
252 block_t *p_block = p_data->p_block;
253 p_data->i_fragment = 0;