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 *****************************************************************************/
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;
59 p_sys->p_data = p_data = malloc( sizeof( screen_data_t ) );
61 /* Get the device context for the whole screen */
62 p_data->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
63 if( !p_data->hdc_src )
65 msg_Err( p_demux, "cannot get device context" );
69 p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
70 if( !p_data->hdc_dst )
72 msg_Err( p_demux, "cannot get compat device context" );
73 ReleaseDC( 0, p_data->hdc_src );
77 i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
78 switch( i_bits_per_pixel )
80 case 8: /* FIXME: set the palette */
81 i_chroma = VLC_FOURCC('R','G','B','2'); break;
83 i_chroma = VLC_FOURCC('R','V','1','5'); break;
85 i_chroma = VLC_FOURCC('R','V','1','6'); break;
87 i_chroma = VLC_FOURCC('R','V','2','4'); break;
89 i_chroma = VLC_FOURCC('R','V','3','2'); break;
91 msg_Err( p_demux, "unknown screen depth %i",
92 p_sys->fmt.video.i_bits_per_pixel );
93 ReleaseDC( 0, p_data->hdc_src );
94 ReleaseDC( 0, p_data->hdc_dst );
98 #if 1 /* For now we force RV24 because of chroma inversion in the other cases*/
99 i_chroma = VLC_FOURCC('R','V','2','4');
100 i_bits_per_pixel = 24;
103 es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
104 p_sys->fmt.video.i_width = GetDeviceCaps( p_data->hdc_src, HORZRES );
105 p_sys->fmt.video.i_height = GetDeviceCaps( p_data->hdc_src, VERTRES );
106 p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
108 /* Create the bitmap info header */
109 p_data->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
110 p_data->bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
111 p_data->bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
112 p_data->bmi.bmiHeader.biPlanes = 1;
113 p_data->bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
114 p_data->bmi.bmiHeader.biCompression = BI_RGB;
115 p_data->bmi.bmiHeader.biSizeImage = 0;
116 p_data->bmi.bmiHeader.biXPelsPerMeter =
117 p_data->bmi.bmiHeader.biYPelsPerMeter = 0;
118 p_data->bmi.bmiHeader.biClrUsed = 0;
119 p_data->bmi.bmiHeader.biClrImportant = 0;
121 if( i_chroma == VLC_FOURCC('R','V','2','4') )
123 /* This is in BGR format */
124 p_sys->fmt.video.i_bmask = 0x00ff0000;
125 p_sys->fmt.video.i_gmask = 0x0000ff00;
126 p_sys->fmt.video.i_rmask = 0x000000ff;
129 var_Create( p_demux, "screen-fragment-size",
130 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
131 var_Get( p_demux, "screen-fragment-size", &val );
132 p_data->i_fragment_size =
133 val.i_int > 0 ? val.i_int : p_sys->fmt.video.i_height;
134 p_data->i_fragment_size =
135 val.i_int > p_sys->fmt.video.i_height ? p_sys->fmt.video.i_height :
136 p_data->i_fragment_size;
137 p_sys->f_fps *= (p_sys->fmt.video.i_height/p_data->i_fragment_size);
138 p_sys->i_incr = 1000000 / p_sys->f_fps;
139 p_data->i_fragment = 0;
145 int screen_CloseCapture( demux_t *p_demux )
147 demux_sys_t *p_sys = p_demux->p_sys;
148 screen_data_t *p_data = p_sys->p_data;
150 if( p_data->p_block ) block_Release( p_data->p_block );
152 if( p_data->hgdi_backup)
153 SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
155 DeleteDC( p_data->hdc_dst );
156 ReleaseDC( 0, p_data->hdc_src );
168 static void CaptureBlockRelease( block_t *p_block )
170 DeleteObject( ((block_sys_t *)p_block)->hbmp );
174 static block_t *CaptureBlockNew( demux_t *p_demux )
176 demux_sys_t *p_sys = p_demux->p_sys;
177 screen_data_t *p_data = p_sys->p_data;
178 block_sys_t *p_block;
183 /* Create the bitmap storage space */
184 hbmp = CreateDIBSection( p_data->hdc_dst, &p_data->bmi, DIB_RGB_COLORS,
185 &p_buffer, NULL, 0 );
186 if( !hbmp || !p_buffer )
188 msg_Err( p_demux, "cannot create bitmap" );
189 if( hbmp ) DeleteObject( hbmp );
193 /* Select the bitmap into the compatible DC */
194 if( !p_data->hgdi_backup )
195 p_data->hgdi_backup = SelectObject( p_data->hdc_dst, hbmp );
197 SelectObject( p_data->hdc_dst, hbmp );
199 if( !p_data->hgdi_backup )
201 msg_Err( p_demux, "cannot select bitmap" );
202 DeleteObject( hbmp );
207 if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
209 DeleteObject( hbmp );
212 /* Fill all fields */
213 i_buffer = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
214 p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
215 block_Init( &p_block->self, p_buffer, i_buffer );
216 p_block->self.pf_release = CaptureBlockRelease;
217 p_block->hbmp = hbmp;
219 return &p_block->self;
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;