]> git.sesse.net Git - vlc/blob - modules/access/screen/win32.c
For consistency, remove references to vlc from libvlc
[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 #include <stdlib.h>
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "screen.h"
33
34 #ifndef CAPTUREBLT
35 #   define CAPTUREBLT (DWORD)0x40000000 /* Include layered windows */
36 #endif
37
38 struct screen_data_t
39 {
40     HDC hdc_src;
41     HDC hdc_dst;
42     BITMAPINFO bmi;
43     HGDIOBJ hgdi_backup;
44
45     int i_fragment_size;
46     int i_fragment;
47     block_t *p_block;
48 };
49
50 int screen_InitCapture( demux_t *p_demux )
51 {
52     demux_sys_t *p_sys = p_demux->p_sys;
53     screen_data_t *p_data;
54     int i_chroma, i_bits_per_pixel;
55     vlc_value_t val;
56
57     p_sys->p_data = p_data = malloc( sizeof( screen_data_t ) );
58
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 )
62     {
63         msg_Err( p_demux, "cannot get device context" );
64         return VLC_EGENERIC;
65     }
66
67     p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
68     if( !p_data->hdc_dst )
69     {
70         msg_Err( p_demux, "cannot get compat device context" );
71         ReleaseDC( 0, p_data->hdc_src );
72         return VLC_EGENERIC;
73     }
74
75     i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
76     switch( i_bits_per_pixel )
77     {
78     case 8: /* FIXME: set the palette */
79         i_chroma = VLC_FOURCC('R','G','B','2'); break;
80     case 15:
81         i_chroma = VLC_FOURCC('R','V','1','5'); break;
82     case 16:
83         i_chroma = VLC_FOURCC('R','V','1','6'); break;
84     case 24:
85         i_chroma = VLC_FOURCC('R','V','2','4'); break;
86     case 32:
87         i_chroma = VLC_FOURCC('R','V','3','2'); break;
88     default:
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 );
93         return VLC_EGENERIC;
94     }
95
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;
99 #endif
100
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;
105
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;
118
119     if( i_chroma == VLC_FOURCC('R','V','2','4') )
120     {
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;
125     }
126
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;
138     p_data->p_block = 0;
139
140     return VLC_SUCCESS;
141 }
142
143 int screen_CloseCapture( demux_t *p_demux )
144 {
145     demux_sys_t *p_sys = p_demux->p_sys;
146     screen_data_t *p_data = p_sys->p_data;
147
148     if( p_data->p_block ) block_Release( p_data->p_block );
149
150     if( p_data->hgdi_backup)
151         SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
152
153     DeleteDC( p_data->hdc_dst );
154     ReleaseDC( 0, p_data->hdc_src );
155     free( p_data );
156
157     return VLC_SUCCESS;
158 }
159
160 struct block_sys_t
161 {
162     HBITMAP hbmp;
163 };
164
165 static void CaptureBlockRelease( block_t *p_block )
166 {
167     DeleteObject( p_block->p_sys->hbmp );
168     free( p_block );
169 }
170
171 static block_t *CaptureBlockNew( demux_t *p_demux )
172 {
173     demux_sys_t *p_sys = p_demux->p_sys;
174     screen_data_t *p_data = p_sys->p_data;
175     block_t *p_block;
176     void *p_buffer;
177     int i_buffer;
178     HBITMAP hbmp;
179
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 )
184     {
185         msg_Err( p_demux, "cannot create bitmap" );
186         if( hbmp ) DeleteObject( hbmp );
187         return NULL;
188     }
189
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 );
193     else
194         SelectObject( p_data->hdc_dst, hbmp );
195
196     if( !p_data->hgdi_backup )
197     {
198         msg_Err( p_demux, "cannot select bitmap" );
199         DeleteObject( hbmp );
200         return NULL;
201     }
202
203     /* Build block */
204     if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
205     {
206         DeleteObject( hbmp );
207         return NULL;
208     }
209     memset( p_block, 0, sizeof( block_t ) );
210     p_block->p_sys = (block_sys_t *)( (uint8_t *)p_block + sizeof( block_t ) );
211
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;
221
222     return p_block;
223 }
224
225 block_t *screen_Capture( demux_t *p_demux )
226 {
227     demux_sys_t *p_sys = p_demux->p_sys;
228     screen_data_t *p_data = p_sys->p_data;
229
230     if( !p_data->i_fragment )
231     {
232         if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
233         {
234             msg_Warn( p_demux, "cannot get block" );
235             return 0;
236         }
237     }
238
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 ) )
245     {
246         msg_Err( p_demux, "error during BitBlt()" );
247         return NULL;
248     }
249
250     p_data->i_fragment++;
251
252     if( !( p_data->i_fragment %
253            (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
254     {
255         block_t *p_block = p_data->p_block;
256         p_data->i_fragment = 0;
257         p_data->p_block = 0;
258         return p_block;
259     }
260
261     return NULL;
262 }