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 *****************************************************************************/
30 #include <vlc/input.h>
35 # define CAPTUREBLT (DWORD)0x40000000 /* Include layered windows */
50 int screen_InitCapture( demux_t *p_demux )
52 demux_sys_t *p_sys = p_demux->p_sys;
53 screen_data_t *p_data;
54 int i_chroma, i_bits_per_pixel;
57 p_sys->p_data = p_data = malloc( sizeof( screen_data_t ) );
59 /* Get the device context for the whole screen */
60 p_data->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
61 if( !p_data->hdc_src )
63 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" );
71 ReleaseDC( 0, p_data->hdc_src );
75 i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
76 switch( i_bits_per_pixel )
78 case 8: /* FIXME: set the palette */
79 i_chroma = VLC_FOURCC('R','G','B','2'); break;
81 i_chroma = VLC_FOURCC('R','V','1','5'); break;
83 i_chroma = VLC_FOURCC('R','V','1','6'); break;
85 i_chroma = VLC_FOURCC('R','V','2','4'); break;
87 i_chroma = VLC_FOURCC('R','V','3','2'); break;
89 msg_Err( p_demux, "unknown screen depth %i",
90 p_sys->fmt.video.i_bits_per_pixel );
91 ReleaseDC( 0, p_data->hdc_src );
92 ReleaseDC( 0, p_data->hdc_dst );
96 #if 1 /* For now we force RV24 because of chroma inversion in the other cases*/
97 i_chroma = VLC_FOURCC('R','V','2','4');
98 i_bits_per_pixel = 24;
101 es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
102 p_sys->fmt.video.i_width = GetDeviceCaps( p_data->hdc_src, HORZRES );
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;
106 /* Create the bitmap info header */
107 p_data->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
108 p_data->bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
109 p_data->bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
110 p_data->bmi.bmiHeader.biPlanes = 1;
111 p_data->bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
112 p_data->bmi.bmiHeader.biCompression = BI_RGB;
113 p_data->bmi.bmiHeader.biSizeImage = 0;
114 p_data->bmi.bmiHeader.biXPelsPerMeter =
115 p_data->bmi.bmiHeader.biYPelsPerMeter = 0;
116 p_data->bmi.bmiHeader.biClrUsed = 0;
117 p_data->bmi.bmiHeader.biClrImportant = 0;
119 if( i_chroma == VLC_FOURCC('R','V','2','4') )
121 /* This is in BGR format */
122 p_sys->fmt.video.i_bmask = 0x00ff0000;
123 p_sys->fmt.video.i_gmask = 0x0000ff00;
124 p_sys->fmt.video.i_rmask = 0x000000ff;
127 var_Create( p_demux, "screen-fragment-size",
128 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
129 var_Get( p_demux, "screen-fragment-size", &val );
130 p_data->i_fragment_size =
131 val.i_int > 0 ? val.i_int : p_sys->fmt.video.i_height;
132 p_data->i_fragment_size =
133 val.i_int > p_sys->fmt.video.i_height ? p_sys->fmt.video.i_height :
134 p_data->i_fragment_size;
135 p_sys->f_fps *= (p_sys->fmt.video.i_height/p_data->i_fragment_size);
136 p_sys->i_incr = 1000000 / p_sys->f_fps;
137 p_data->i_fragment = 0;
143 int screen_CloseCapture( demux_t *p_demux )
145 demux_sys_t *p_sys = p_demux->p_sys;
146 screen_data_t *p_data = p_sys->p_data;
148 if( p_data->p_block ) block_Release( p_data->p_block );
150 if( p_data->hgdi_backup)
151 SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
153 DeleteDC( p_data->hdc_dst );
154 ReleaseDC( 0, p_data->hdc_src );
165 static void CaptureBlockRelease( block_t *p_block )
167 DeleteObject( p_block->p_sys->hbmp );
171 static block_t *CaptureBlockNew( demux_t *p_demux )
173 demux_sys_t *p_sys = p_demux->p_sys;
174 screen_data_t *p_data = p_sys->p_data;
180 /* Create the bitmap storage space */
181 hbmp = CreateDIBSection( p_data->hdc_dst, &p_data->bmi, DIB_RGB_COLORS,
182 &p_buffer, NULL, 0 );
183 if( !hbmp || !p_buffer )
185 msg_Err( p_demux, "cannot create bitmap" );
186 if( hbmp ) DeleteObject( hbmp );
190 /* Select the bitmap into the compatible DC */
191 if( !p_data->hgdi_backup )
192 p_data->hgdi_backup = SelectObject( p_data->hdc_dst, hbmp );
194 SelectObject( p_data->hdc_dst, hbmp );
196 if( !p_data->hgdi_backup )
198 msg_Err( p_demux, "cannot select bitmap" );
199 DeleteObject( hbmp );
204 if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
206 DeleteObject( hbmp );
209 memset( p_block, 0, sizeof( block_t ) );
210 p_block->p_sys = (block_sys_t *)( (uint8_t *)p_block + sizeof( block_t ) );
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 p_block->p_next = NULL;
216 p_block->i_buffer = i_buffer;
217 p_block->p_buffer = p_buffer;
218 p_block->pf_release = CaptureBlockRelease;
219 p_block->p_manager = VLC_OBJECT( p_demux->p_libvlc );
220 p_block->p_sys->hbmp = hbmp;
225 block_t *screen_Capture( demux_t *p_demux )
227 demux_sys_t *p_sys = p_demux->p_sys;
228 screen_data_t *p_data = p_sys->p_data;
230 if( !p_data->i_fragment )
232 if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
234 msg_Warn( p_demux, "cannot get block" );
239 if( !BitBlt( p_data->hdc_dst, 0, p_data->i_fragment *
240 p_data->i_fragment_size,
241 p_sys->fmt.video.i_width, p_data->i_fragment_size,
242 p_data->hdc_src, 0, p_data->i_fragment *
243 p_data->i_fragment_size,
244 IS_WINNT ? SRCCOPY | CAPTUREBLT : SRCCOPY ) )
246 msg_Err( p_demux, "error during BitBlt()" );
250 p_data->i_fragment++;
252 if( !( p_data->i_fragment %
253 (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
255 block_t *p_block = p_data->p_block;
256 p_data->i_fragment = 0;