]> git.sesse.net Git - vlc/blob - modules/access/screen/win32.c
Remove the unused manager from block_t
[vlc] / modules / access / screen / win32.c
1 /*****************************************************************************
2  * win32.c: Screen capture module.
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29
30 #include "screen.h"
31
32 #ifndef CAPTUREBLT
33 #   define CAPTUREBLT (DWORD)0x40000000 /* Include layered windows */
34 #endif
35
36 struct screen_data_t
37 {
38     HDC hdc_src;
39     HDC hdc_dst;
40     BITMAPINFO bmi;
41     HGDIOBJ hgdi_backup;
42
43     int i_fragment_size;
44     int i_fragment;
45     block_t *p_block;
46 };
47
48 int screen_InitCapture( demux_t *p_demux )
49 {
50     demux_sys_t *p_sys = p_demux->p_sys;
51     screen_data_t *p_data;
52     int i_chroma, i_bits_per_pixel;
53     vlc_value_t val;
54
55     p_sys->p_data = p_data = malloc( sizeof( screen_data_t ) );
56
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 )
60     {
61         msg_Err( p_demux, "cannot get device context" );
62         return VLC_EGENERIC;
63     }
64
65     p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
66     if( !p_data->hdc_dst )
67     {
68         msg_Err( p_demux, "cannot get compat device context" );
69         ReleaseDC( 0, p_data->hdc_src );
70         return VLC_EGENERIC;
71     }
72
73     i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
74     switch( i_bits_per_pixel )
75     {
76     case 8: /* FIXME: set the palette */
77         i_chroma = VLC_FOURCC('R','G','B','2'); break;
78     case 15:
79         i_chroma = VLC_FOURCC('R','V','1','5'); break;
80     case 16:
81         i_chroma = VLC_FOURCC('R','V','1','6'); break;
82     case 24:
83         i_chroma = VLC_FOURCC('R','V','2','4'); break;
84     case 32:
85         i_chroma = VLC_FOURCC('R','V','3','2'); break;
86     default:
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 );
91         return VLC_EGENERIC;
92     }
93
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;
97 #endif
98
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;
103
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;
116
117     if( i_chroma == VLC_FOURCC('R','V','2','4') )
118     {
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;
123     }
124
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;
136     p_data->p_block = 0;
137
138     return VLC_SUCCESS;
139 }
140
141 int screen_CloseCapture( demux_t *p_demux )
142 {
143     demux_sys_t *p_sys = p_demux->p_sys;
144     screen_data_t *p_data = p_sys->p_data;
145
146     if( p_data->p_block ) block_Release( p_data->p_block );
147
148     if( p_data->hgdi_backup)
149         SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
150
151     DeleteDC( p_data->hdc_dst );
152     ReleaseDC( 0, p_data->hdc_src );
153     free( p_data );
154
155     return VLC_SUCCESS;
156 }
157
158 struct block_sys_t
159 {
160     HBITMAP hbmp;
161 };
162
163 static void CaptureBlockRelease( block_t *p_block )
164 {
165     DeleteObject( p_block->p_sys->hbmp );
166     free( p_block );
167 }
168
169 static block_t *CaptureBlockNew( demux_t *p_demux )
170 {
171     demux_sys_t *p_sys = p_demux->p_sys;
172     screen_data_t *p_data = p_sys->p_data;
173     block_t *p_block;
174     void *p_buffer;
175     int i_buffer;
176     HBITMAP hbmp;
177
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 )
182     {
183         msg_Err( p_demux, "cannot create bitmap" );
184         if( hbmp ) DeleteObject( hbmp );
185         return NULL;
186     }
187
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 );
191     else
192         SelectObject( p_data->hdc_dst, hbmp );
193
194     if( !p_data->hgdi_backup )
195     {
196         msg_Err( p_demux, "cannot select bitmap" );
197         DeleteObject( hbmp );
198         return NULL;
199     }
200
201     /* Build block */
202     if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
203     {
204         DeleteObject( hbmp );
205         return NULL;
206     }
207     memset( p_block, 0, sizeof( block_t ) );
208     p_block->p_sys = (block_sys_t *)( (uint8_t *)p_block + sizeof( block_t ) );
209
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;
218
219     return p_block;
220 }
221
222 block_t *screen_Capture( demux_t *p_demux )
223 {
224     demux_sys_t *p_sys = p_demux->p_sys;
225     screen_data_t *p_data = p_sys->p_data;
226
227     if( !p_data->i_fragment )
228     {
229         if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
230         {
231             msg_Warn( p_demux, "cannot get block" );
232             return 0;
233         }
234     }
235
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 ) )
242     {
243         msg_Err( p_demux, "error during BitBlt()" );
244         return NULL;
245     }
246
247     p_data->i_fragment++;
248
249     if( !( p_data->i_fragment %
250            (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
251     {
252         block_t *p_block = p_data->p_block;
253         p_data->i_fragment = 0;
254         p_data->p_block = 0;
255         return p_block;
256     }
257
258     return NULL;
259 }