win32.c 7.95 KB
Newer Older
1 2 3
/*****************************************************************************
 * win32.c: Screen capture module.
 *****************************************************************************
4
 * Copyright (C) 2004 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * $Id$
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27 28 29 30 31 32 33
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>

#include <vlc/vlc.h>
#include <vlc/input.h>

#include "screen.h"

34 35 36 37
#ifndef CAPTUREBLT
#   define CAPTUREBLT (DWORD)0x40000000 /* Include layered windows */
#endif

38 39 40 41
struct screen_data_t
{
    HDC hdc_src;
    HDC hdc_dst;
42
    BITMAPINFO bmi;
43
    HGDIOBJ hgdi_backup;
44 45 46 47

    int i_fragment_size;
    int i_fragment;
    block_t *p_block;
48 49 50 51 52 53 54
};

int screen_InitCapture( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    screen_data_t *p_data;
    int i_chroma, i_bits_per_pixel;
55
    vlc_value_t val;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

    p_sys->p_data = p_data = malloc( sizeof( screen_data_t ) );

    /* Get the device context for the whole screen */
    p_data->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
    if( !p_data->hdc_src )
    {
        msg_Err( p_demux, "cannot get device context" );
        return VLC_EGENERIC;
    }

    p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
    if( !p_data->hdc_dst )
    {
        msg_Err( p_demux, "cannot get compat device context" );
        ReleaseDC( 0, p_data->hdc_src );
        return VLC_EGENERIC;
    }

    i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
    switch( i_bits_per_pixel )
    {
    case 8: /* FIXME: set the palette */
        i_chroma = VLC_FOURCC('R','G','B','2'); break;
    case 15:
        i_chroma = VLC_FOURCC('R','V','1','5'); break;
    case 16:
        i_chroma = VLC_FOURCC('R','V','1','6'); break;
    case 24:
        i_chroma = VLC_FOURCC('R','V','2','4'); break;
    case 32:
        i_chroma = VLC_FOURCC('R','V','3','2'); break;
    default:
        msg_Err( p_demux, "unknown screen depth %i",
                 p_sys->fmt.video.i_bits_per_pixel );
        ReleaseDC( 0, p_data->hdc_src );
        ReleaseDC( 0, p_data->hdc_dst );
        return VLC_EGENERIC;
    }

#if 1 /* For now we force RV24 because of chroma inversion in the other cases*/
    i_chroma = VLC_FOURCC('R','V','2','4');
    i_bits_per_pixel = 24;
#endif

    es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
    p_sys->fmt.video.i_width  = GetDeviceCaps( p_data->hdc_src, HORZRES );
    p_sys->fmt.video.i_height = GetDeviceCaps( p_data->hdc_src, VERTRES );
    p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;

    /* Create the bitmap info header */
107 108 109 110 111 112 113 114 115 116 117
    p_data->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    p_data->bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
    p_data->bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
    p_data->bmi.bmiHeader.biPlanes = 1;
    p_data->bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
    p_data->bmi.bmiHeader.biCompression = BI_RGB;
    p_data->bmi.bmiHeader.biSizeImage = 0;
    p_data->bmi.bmiHeader.biXPelsPerMeter =
        p_data->bmi.bmiHeader.biYPelsPerMeter = 0;
    p_data->bmi.bmiHeader.biClrUsed = 0;
    p_data->bmi.bmiHeader.biClrImportant = 0;
118

119 120 121 122 123 124 125 126
    if( i_chroma == VLC_FOURCC('R','V','2','4') )
    {
        /* This is in BGR format */
        p_sys->fmt.video.i_bmask = 0x00ff0000;
        p_sys->fmt.video.i_gmask = 0x0000ff00;
        p_sys->fmt.video.i_rmask = 0x000000ff;
    }

127 128 129 130 131 132 133 134 135 136 137 138 139
    var_Create( p_demux, "screen-fragment-size",
                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
    var_Get( p_demux, "screen-fragment-size", &val );
    p_data->i_fragment_size =
        val.i_int > 0 ? val.i_int : p_sys->fmt.video.i_height;
    p_data->i_fragment_size =
        val.i_int > p_sys->fmt.video.i_height ? p_sys->fmt.video.i_height :
        p_data->i_fragment_size;
    p_sys->f_fps *= (p_sys->fmt.video.i_height/p_data->i_fragment_size);
    p_sys->i_incr = 1000000 / p_sys->f_fps;
    p_data->i_fragment = 0;
    p_data->p_block = 0;

140 141 142 143 144 145 146 147
    return VLC_SUCCESS;
}

int screen_CloseCapture( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    screen_data_t *p_data = p_sys->p_data;

148 149
    if( p_data->p_block ) block_Release( p_data->p_block );

150 151 152
    if( p_data->hgdi_backup)
        SelectObject( p_data->hdc_dst, p_data->hgdi_backup );

153 154 155 156 157 158 159
    DeleteDC( p_data->hdc_dst );
    ReleaseDC( 0, p_data->hdc_src );
    free( p_data );

    return VLC_SUCCESS;
}

160 161 162 163 164 165 166 167 168 169 170 171
struct block_sys_t
{
    HBITMAP hbmp;
};

static void CaptureBlockRelease( block_t *p_block )
{
    DeleteObject( p_block->p_sys->hbmp );
    free( p_block );
}

static block_t *CaptureBlockNew( demux_t *p_demux )
172 173 174 175
{
    demux_sys_t *p_sys = p_demux->p_sys;
    screen_data_t *p_data = p_sys->p_data;
    block_t *p_block;
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    void *p_buffer;
    int i_buffer;
    HBITMAP hbmp;

    /* Create the bitmap storage space */
    hbmp = CreateDIBSection( p_data->hdc_dst, &p_data->bmi, DIB_RGB_COLORS,
                             &p_buffer, NULL, 0 );
    if( !hbmp || !p_buffer )
    {
        msg_Err( p_demux, "cannot create bitmap" );
        if( hbmp ) DeleteObject( hbmp );
        return NULL;
    }

    /* Select the bitmap into the compatible DC */
    if( !p_data->hgdi_backup )
        p_data->hgdi_backup = SelectObject( p_data->hdc_dst, hbmp );
    else
        SelectObject( p_data->hdc_dst, hbmp );

    if( !p_data->hgdi_backup )
    {
        msg_Err( p_demux, "cannot select bitmap" );
        DeleteObject( hbmp );
        return NULL;
    }
202

203 204 205 206 207 208 209 210 211 212 213
    /* Build block */
    if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
    {
        DeleteObject( hbmp );
        return NULL;
    }
    memset( p_block, 0, sizeof( block_t ) );
    p_block->p_sys = (block_sys_t *)( (uint8_t *)p_block + sizeof( block_t ) );

    /* Fill all fields */
    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 216 217 218 219 220 221 222 223
    p_block->p_next         = NULL;
    p_block->i_buffer       = i_buffer;
    p_block->p_buffer       = p_buffer;
    p_block->pf_release     = CaptureBlockRelease;
    p_block->p_manager      = VLC_OBJECT( p_demux->p_vlc );
    p_block->p_sys->hbmp    = hbmp;

    return p_block;
}
224

225 226
block_t *screen_Capture( demux_t *p_demux )
{
227 228
    demux_sys_t *p_sys = p_demux->p_sys;
    screen_data_t *p_data = p_sys->p_data;
229

230
    if( !p_data->i_fragment )
231
    {
232 233 234 235 236
        if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
        {
            msg_Warn( p_demux, "cannot get block" );
            return 0;
        }
237 238
    }

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    if( !BitBlt( p_data->hdc_dst, 0, p_data->i_fragment *
                 p_data->i_fragment_size,
                 p_sys->fmt.video.i_width, p_data->i_fragment_size,
                 p_data->hdc_src, 0, p_data->i_fragment *
                 p_data->i_fragment_size,
                 IS_WINNT ? SRCCOPY | CAPTUREBLT : SRCCOPY ) )
    {
        msg_Err( p_demux, "error during BitBlt()" );
        return NULL;
    }

    p_data->i_fragment++;

    if( !( p_data->i_fragment %
           (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
    {
        block_t *p_block = p_data->p_block;
        p_data->i_fragment = 0;
        p_data->p_block = 0;
        return p_block;
    }

    return NULL;
262
}