]> git.sesse.net Git - vlc/blobdiff - modules/access/screen/win32.c
Merge branch 1.0-bugfix into master
[vlc] / modules / access / screen / win32.c
index 3c2b0de5b3fad8782b48a042860bff72985d4b89..01cac097c9d8711324ea49465ffc669950ef2a04 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * win32.c: Screen capture module.
  *****************************************************************************
- * Copyright (C) 2004 VideoLAN
+ * Copyright (C) 2004-2008 the VideoLAN team
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@videolan.org>
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>
 
-#include <vlc/vlc.h>
-#include <vlc/input.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
 
 #include "screen.h"
 
+#ifndef CAPTUREBLT
+#   define CAPTUREBLT (DWORD)0x40000000 /* Include layered windows */
+#endif
+
 struct screen_data_t
 {
     HDC hdc_src;
     HDC hdc_dst;
-    HBITMAP hbmp;
+    BITMAPINFO bmi;
     HGDIOBJ hgdi_backup;
-    uint8_t *p_buffer;
+
+    int i_fragment_size;
+    int i_fragment;
+    block_t *p_block;
 };
 
 int screen_InitCapture( demux_t *p_demux )
@@ -46,15 +55,16 @@ int screen_InitCapture( demux_t *p_demux )
     screen_data_t *p_data;
     int i_chroma, i_bits_per_pixel;
 
-    BITMAPINFO bmi;
-
-    p_sys->p_data = p_data = malloc( sizeof( screen_data_t ) );
+    p_sys->p_data = p_data = calloc( 1, sizeof( screen_data_t ) );
+    if( !p_data )
+        return VLC_ENOMEM;
 
     /* 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" );
+        free( p_data );
         return VLC_EGENERIC;
     }
 
@@ -62,6 +72,7 @@ int screen_InitCapture( demux_t *p_demux )
     if( !p_data->hdc_dst )
     {
         msg_Err( p_demux, "cannot get compat device context" );
+        free( p_data );
         ReleaseDC( 0, p_data->hdc_src );
         return VLC_EGENERIC;
     }
@@ -70,64 +81,53 @@ int screen_InitCapture( demux_t *p_demux )
     switch( i_bits_per_pixel )
     {
     case 8: /* FIXME: set the palette */
-        i_chroma = VLC_FOURCC('R','G','B','2'); break;
+        i_chroma = VLC_CODEC_RGB8; break;
     case 15:
-        i_chroma = VLC_FOURCC('R','V','1','5'); break;
-    case 16:
-        i_chroma = VLC_FOURCC('R','V','1','6'); break;
+    case 16:    /* Yes it is really 15 bits (when using BI_RGB) */
+        i_chroma = VLC_CODEC_RGB15; break;
     case 24:
-        i_chroma = VLC_FOURCC('R','V','2','4'); break;
+        i_chroma = VLC_CODEC_RGB24; break;
     case 32:
-        i_chroma = VLC_FOURCC('R','V','3','2'); break;
+        i_chroma = VLC_CODEC_RGB32; break;
     default:
         msg_Err( p_demux, "unknown screen depth %i",
                  p_sys->fmt.video.i_bits_per_pixel );
+        DeleteDC( p_data->hdc_dst );
         ReleaseDC( 0, p_data->hdc_src );
-        ReleaseDC( 0, p_data->hdc_dst );
+        free( p_data );
         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_visible_width =
     p_sys->fmt.video.i_width  = GetDeviceCaps( p_data->hdc_src, HORZRES );
+    p_sys->fmt.video.i_visible_height =
     p_sys->fmt.video.i_height = GetDeviceCaps( p_data->hdc_src, VERTRES );
     p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
+    p_sys->fmt.video.i_chroma = i_chroma;
 
-    /* Create the bitmap info header */
-    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
-    bmi.bmiHeader.biWidth = p_sys->fmt.video.i_width;
-    bmi.bmiHeader.biHeight = - p_sys->fmt.video.i_height;
-    bmi.bmiHeader.biPlanes = 1;
-    bmi.bmiHeader.biBitCount = p_sys->fmt.video.i_bits_per_pixel;
-    bmi.bmiHeader.biCompression = BI_RGB;
-    bmi.bmiHeader.biSizeImage = 0;
-    bmi.bmiHeader.biXPelsPerMeter =
-        bmi.bmiHeader.biYPelsPerMeter = 0;
-    bmi.bmiHeader.biClrUsed = 0;
-    bmi.bmiHeader.biClrImportant = 0;
-
-    /* Create the bitmap storage space */
-    p_data->hbmp = CreateDIBSection( p_data->hdc_dst, (BITMAPINFO *)&bmi,
-        DIB_RGB_COLORS, (void **)&p_data->p_buffer, NULL, 0 );
-    if( !p_data->hbmp || !p_data->p_buffer )
+    switch( i_chroma )
     {
-        msg_Err( p_demux, "cannot create bitmap" );
-        if( p_data->hbmp ) DeleteObject( p_data->hbmp );
-        ReleaseDC( 0, p_data->hdc_src );
-        DeleteDC( p_data->hdc_dst );
-        return VLC_EGENERIC;
+    case VLC_CODEC_RGB15:
+        p_sys->fmt.video.i_rmask = 0x7c00;
+        p_sys->fmt.video.i_gmask = 0x03e0;
+        p_sys->fmt.video.i_bmask = 0x001f;
+        break;
+    case VLC_CODEC_RGB24:
+        p_sys->fmt.video.i_rmask = 0x00ff0000;
+        p_sys->fmt.video.i_gmask = 0x0000ff00;
+        p_sys->fmt.video.i_bmask = 0x000000ff;
+        break;
+    case VLC_CODEC_RGB32:
+        p_sys->fmt.video.i_rmask = 0x00ff0000;
+        p_sys->fmt.video.i_gmask = 0x0000ff00;
+        p_sys->fmt.video.i_bmask = 0x000000ff;
+        break;
+    default:
+        msg_Warn( p_demux, "Unknown RGB masks" );
+        break;
     }
 
-    /* Select the bitmap into the compatible DC */
-    p_data->hgdi_backup = SelectObject( p_data->hdc_dst, p_data->hbmp );
-    if( !p_data->hgdi_backup )
-    {
-        msg_Err( p_demux, "cannot select bitmap" );
-    }
 
     return VLC_SUCCESS;
 }
@@ -137,8 +137,11 @@ 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;
 
-    SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
-    DeleteObject( p_data->hbmp );
+    if( p_data->p_block ) block_Release( p_data->p_block );
+
+    if( p_data->hgdi_backup)
+        SelectObject( p_data->hdc_dst, p_data->hgdi_backup );
+
     DeleteDC( p_data->hdc_dst );
     ReleaseDC( 0, p_data->hdc_src );
     free( p_data );
@@ -146,32 +149,145 @@ int screen_CloseCapture( demux_t *p_demux )
     return VLC_SUCCESS;
 }
 
-block_t *screen_Capture( demux_t *p_demux )
+struct block_sys_t
+{
+    block_t self;
+    HBITMAP hbmp;
+};
+
+static void CaptureBlockRelease( block_t *p_block )
+{
+    DeleteObject( ((block_sys_t *)p_block)->hbmp );
+    free( p_block );
+}
+
+static block_t *CaptureBlockNew( demux_t *p_demux )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
     screen_data_t *p_data = p_sys->p_data;
-    block_t *p_block;
-    int i_size;
+    block_sys_t *p_block;
+    void *p_buffer;
+    int i_buffer;
+    HBITMAP hbmp;
 
-    if( !BitBlt( p_data->hdc_dst, 0, 0,
-                 p_sys->fmt.video.i_width, p_sys->fmt.video.i_height,
-                 p_data->hdc_src, 0, 0, SRCCOPY ) )
+    if( p_data->bmi.bmiHeader.biSize == 0 )
     {
-        msg_Err( p_demux, "error during BitBlt()" );
+        int i_val;
+        /* Create the bitmap info header */
+        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;
+
+        i_val = var_CreateGetInteger( p_demux, "screen-fragment-size" );
+        p_data->i_fragment_size = i_val > 0 ? i_val : p_sys->fmt.video.i_height;
+        p_data->i_fragment_size = i_val > 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;
+    }
+
+
+    /* 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;
     }
 
-    i_size = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
-        p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
+    /* 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_block = block_New( p_demux, i_size ) ) )
+    if( !p_data->hgdi_backup )
     {
-        msg_Warn( p_demux, "cannot get block" );
-        return 0;
+        msg_Err( p_demux, "cannot select bitmap" );
+        DeleteObject( hbmp );
+        return NULL;
     }
 
-    memcpy( p_block->p_buffer, p_data->p_buffer, i_size );
+    /* Build block */
+    if( !(p_block = malloc( sizeof( block_t ) + sizeof( block_sys_t ) )) )
+    {
+        DeleteObject( hbmp );
+        return NULL;
+    }
+    /* Fill all fields */
+    i_buffer = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
+        p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
+    block_Init( &p_block->self, p_buffer, i_buffer );
+    p_block->self.pf_release = CaptureBlockRelease;
+    p_block->hbmp            = hbmp;
 
-    return p_block;
+    return &p_block->self;
 }
 
+block_t *screen_Capture( demux_t *p_demux )
+{
+    demux_sys_t *p_sys = p_demux->p_sys;
+    screen_data_t *p_data = p_sys->p_data;
+
+    if( !p_data->i_fragment )
+    {
+        if( !( p_data->p_block = CaptureBlockNew( p_demux ) ) )
+        {
+            msg_Warn( p_demux, "cannot get block" );
+            return NULL;
+        }
+    }
+
+    if( p_sys->b_follow_mouse )
+    {
+        POINT pos;
+        GetCursorPos( &pos );
+        FollowMouse( p_sys, pos.x, pos.y );
+    }
+
+    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, p_sys->i_left, p_sys->i_top +
+                 p_data->i_fragment * p_data->i_fragment_size,
+                 SRCCOPY | CAPTUREBLT ) )
+    {
+        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;
+
+        if( p_sys->p_mouse )
+        {
+            POINT pos;
+            GetCursorPos( &pos );
+            RenderCursor( p_demux, pos.x, pos.y,
+                          p_block->p_buffer );
+        }
+
+        return p_block;
+    }
+
+    return NULL;
+}