Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

win32.c

00001 /*****************************************************************************
00002  * win32.c: Screen capture module.
00003  *****************************************************************************
00004  * Copyright (C) 2004 the VideoLAN team
00005  * $Id: win32.c 11664 2005-07-09 06:17:09Z courmisch $
00006  *
00007  * Authors: Gildas Bazin <[email protected]>
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
00022  *****************************************************************************/
00023 
00024 /*****************************************************************************
00025  * Preamble
00026  *****************************************************************************/
00027 #include <stdlib.h>
00028 
00029 #include <vlc/vlc.h>
00030 #include <vlc/input.h>
00031 
00032 #include "screen.h"
00033 
00034 #ifndef CAPTUREBLT
00035 #   define CAPTUREBLT (DWORD)0x40000000 /* Include layered windows */
00036 #endif
00037 
00038 struct screen_data_t
00039 {
00040     HDC hdc_src;
00041     HDC hdc_dst;
00042     BITMAPINFO bmi;
00043     HGDIOBJ hgdi_backup;
00044 
00045     int i_fragment_size;
00046     int i_fragment;
00047     block_t *p_block;
00048 };
00049 
00050 int screen_InitCapture( demux_t *p_demux )
00051 {
00052     demux_sys_t *p_sys = p_demux->p_sys;
00053     screen_data_t *p_data;
00054     int i_chroma, i_bits_per_pixel;
00055     vlc_value_t val;
00056 
00057     p_sys->p_data = p_data = malloc( sizeof( screen_data_t ) );
00058 
00059     /* Get the device context for the whole screen */
00060     p_data->hdc_src = CreateDC( "DISPLAY", NULL, NULL, NULL );
00061     if( !p_data->hdc_src )
00062     {
00063         msg_Err( p_demux, "cannot get device context" );
00064         return VLC_EGENERIC;
00065     }
00066 
00067     p_data->hdc_dst = CreateCompatibleDC( p_data->hdc_src );
00068     if( !p_data->hdc_dst )
00069     {
00070         msg_Err( p_demux, "cannot get compat device context" );
00071         ReleaseDC( 0, p_data->hdc_src );
00072         return VLC_EGENERIC;
00073     }
00074 
00075     i_bits_per_pixel = GetDeviceCaps( p_data->hdc_src, BITSPIXEL );
00076     switch( i_bits_per_pixel )
00077     {
00078     case 8: /* FIXME: set the palette */
00079         i_chroma = VLC_FOURCC('R','G','B','2'); break;
00080     case 15:
00081         i_chroma = VLC_FOURCC('R','V','1','5'); break;
00082     case 16:
00083         i_chroma = VLC_FOURCC('R','V','1','6'); break;
00084     case 24:
00085         i_chroma = VLC_FOURCC('R','V','2','4'); break;
00086     case 32:
00087         i_chroma = VLC_FOURCC('R','V','3','2'); break;
00088     default:
00089         msg_Err( p_demux, "unknown screen depth %i",
00090                  p_sys->fmt.video.i_bits_per_pixel );
00091         ReleaseDC( 0, p_data->hdc_src );
00092         ReleaseDC( 0, p_data->hdc_dst );
00093         return VLC_EGENERIC;
00094     }
00095 
00096 #if 1 /* For now we force RV24 because of chroma inversion in the other cases*/
00097     i_chroma = VLC_FOURCC('R','V','2','4');
00098     i_bits_per_pixel = 24;
00099 #endif
00100 
00101     es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
00102     p_sys->fmt.video.i_width  = GetDeviceCaps( p_data->hdc_src, HORZRES );
00103     p_sys->fmt.video.i_height = GetDeviceCaps( p_data->hdc_src, VERTRES );
00104     p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
00105 
00106     /* Create the bitmap info header */
00107     p_data->bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
00108     p_data->bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
00109     p_data->bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
00110     p_data->bmi.bmiHeader.biPlanes = 1;
00111     p_data->bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
00112     p_data->bmi.bmiHeader.biCompression = BI_RGB;
00113     p_data->bmi.bmiHeader.biSizeImage = 0;
00114     p_data->bmi.bmiHeader.biXPelsPerMeter =
00115         p_data->bmi.bmiHeader.biYPelsPerMeter = 0;
00116     p_data->bmi.bmiHeader.biClrUsed = 0;
00117     p_data->bmi.bmiHeader.biClrImportant = 0;
00118 
00119     if( i_chroma == VLC_FOURCC('R','V','2','4') )
00120     {
00121         /* This is in BGR format */
00122         p_sys->fmt.video.i_bmask = 0x00ff0000;
00123         p_sys->fmt.video.i_gmask = 0x0000ff00;
00124         p_sys->fmt.video.i_rmask = 0x000000ff;
00125     }
00126 
00127     var_Create( p_demux, "screen-fragment-size",
00128                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
00129     var_Get( p_demux, "screen-fragment-size", &val );
00130     p_data->i_fragment_size =
00131         val.i_int > 0 ? val.i_int : p_sys->fmt.video.i_height;
00132     p_data->i_fragment_size =
00133         val.i_int > p_sys->fmt.video.i_height ? p_sys->fmt.video.i_height :
00134         p_data->i_fragment_size;
00135     p_sys->f_fps *= (p_sys->fmt.video.i_height/p_data->i_fragment_size);
00136     p_sys->i_incr = 1000000 / p_sys->f_fps;
00137     p_data->i_fragment = 0;
00138     p_data->p_block = 0;
00139 
00140     return VLC_SUCCESS;
00141 }
00142 
00143 int screen_CloseCapture( demux_t *p_demux )
00144 {
00145     demux_sys_t *p_sys = p_demux->p_sys;
00146     screen_data_t *p_data = p_sys->p_data;
00147 
00148     if( p_data->p_block ) block_Release( p_data->p_block );
00149 
00150     if( p_data->hgdi_backup)
00151         SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
00152 
00153     DeleteDC( p_data->hdc_dst );
00154     ReleaseDC( 0, p_data->hdc_src );
00155     free( p_data );
00156 
00157     return VLC_SUCCESS;
00158 }
00159 
00160 struct block_sys_t
00161 {
00162     HBITMAP hbmp;
00163 };
00164 
00165 static void CaptureBlockRelease( block_t *p_block )
00166 {
00167     DeleteObject( p_block->p_sys->hbmp );
00168     free( p_block );
00169 }
00170 
00171 static block_t *CaptureBlockNew( demux_t *p_demux )
00172 {
00173     demux_sys_t *p_sys = p_demux->p_sys;
00174     screen_data_t *p_data = p_sys->p_data;
00175     block_t *p_block;
00176     void *p_buffer;
00177     int i_buffer;
00178     HBITMAP hbmp;
00179 
00180     /* Create the bitmap storage space */
00181     hbmp = CreateDIBSection( p_data->hdc_dst, &p_data->bmi, DIB_RGB_COLORS,
00182                              &p_buffer, NULL, 0 );
00183     if( !hbmp || !p_buffer )
00184     {
00185         msg_Err( p_demux, "cannot create bitmap" );
00186         if( hbmp ) DeleteObject( hbmp );
00187         return NULL;
00188     }
00189 
00190     /* Select the bitmap into the compatible DC */
00191     if( !p_data->hgdi_backup )
00192         p_data->hgdi_backup = SelectObject( p_data->hdc_dst, hbmp );
00193     else
00194         SelectObject( p_data->hdc_dst, hbmp );
00195 
00196     if( !p_data->hgdi_backup )
00197     {
00198         msg_Err( p_demux, "cannot select bitmap" );
00199         DeleteObject( hbmp );
00200         return NULL;
00201     }
00202 
00203     /* Build block */
00204     if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
00205     {
00206         DeleteObject( hbmp );
00207         return NULL;
00208     }
00209     memset( p_block, 0, sizeof( block_t ) );
00210     p_block->p_sys = (block_sys_t *)( (uint8_t *)p_block + sizeof( block_t ) );
00211 
00212     /* Fill all fields */
00213     i_buffer = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
00214         p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
00215     p_block->p_next         = NULL;
00216     p_block->i_buffer       = i_buffer;
00217     p_block->p_buffer       = p_buffer;
00218     p_block->pf_release     = CaptureBlockRelease;
00219     p_block->p_manager      = VLC_OBJECT( p_demux->p_vlc );
00220     p_block->p_sys->hbmp    = hbmp;
00221 
00222     return p_block;
00223 }
00224 
00225 block_t *screen_Capture( demux_t *p_demux )
00226 {
00227     demux_sys_t *p_sys = p_demux->p_sys;
00228     screen_data_t *p_data = p_sys->p_data;
00229 
00230     if( !p_data->i_fragment )
00231     {
00232         if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
00233         {
00234             msg_Warn( p_demux, "cannot get block" );
00235             return 0;
00236         }
00237     }
00238 
00239     if( !BitBlt( p_data->hdc_dst, 0, p_data->i_fragment *
00240                  p_data->i_fragment_size,
00241                  p_sys->fmt.video.i_width, p_data->i_fragment_size,
00242                  p_data->hdc_src, 0, p_data->i_fragment *
00243                  p_data->i_fragment_size,
00244                  IS_WINNT ? SRCCOPY | CAPTUREBLT : SRCCOPY ) )
00245     {
00246         msg_Err( p_demux, "error during BitBlt()" );
00247         return NULL;
00248     }
00249 
00250     p_data->i_fragment++;
00251 
00252     if( !( p_data->i_fragment %
00253            (p_sys->fmt.video.i_height/p_data->i_fragment_size) ) )
00254     {
00255         block_t *p_block = p_data->p_block;
00256         p_data->i_fragment = 0;
00257         p_data->p_block = 0;
00258         return p_block;
00259     }
00260 
00261     return NULL;
00262 }

Generated on Tue Dec 20 10:14:22 2005 for vlc-0.8.4a by  doxygen 1.4.2