Index: trunk/src/VBox/Main/src-client/DisplayImpl.cpp
===================================================================
--- trunk/src/VBox/Main/src-client/DisplayImpl.cpp	(revision 42867)
+++ trunk/src/VBox/Main/src-client/DisplayImpl.cpp	(revision 42878)
@@ -50,6 +50,6 @@
 #ifdef VBOX_WITH_VPX
 # include "VideoRec.h"
-  PVIDEORECCONTEXT pVideoRecContext;
 #endif
+
 /**
  * Display driver instance data.
@@ -428,8 +428,7 @@
  * @param qemuConsoleData address of common console data structure
  */
-HRESULT Display::init (Console *aParent)
+HRESULT Display::init(Console *aParent)
 {
     ComAssertRet(aParent, E_INVALIDARG);
-    HRESULT res;
     /* Enclose the state transition NotReady->InInit->Ready */
     AutoInitSpan autoInitSpan(this);
@@ -444,33 +443,36 @@
     ULONG ul;
     mParent->machine()->COMGETTER(MonitorCount)(&ul);
+
 #ifdef VBOX_WITH_VPX
-    ULONG ulVideoCaptureHorzRes;
-    ULONG ulVideoCaptureVertRes;
-    BSTR strVideoCaptureFile;
     BOOL fEnabled = false;
-    if (VideoRecContextCreate(&pVideoRecContext))
-    {
-        LogFlow(("Failed to create Video Recording Context \n"));
+    if (VideoRecContextCreate(&mpVideoRecContext))
+    {
+        LogFlow(("Failed to create Video Recording Context\n"));
         return E_FAIL;
     }
-    res = RTCritSectInit(&pVideoRecContext->CritSect);
-    AssertReturn(res == S_OK, E_UNEXPECTED);
-    pVideoRecContext->fEnabled = false;
-    mParent->machine()->COMGETTER(VideoCaptureWidth)(&ulVideoCaptureHorzRes);
-    mParent->machine()->COMGETTER(VideoCaptureHeight)(&ulVideoCaptureVertRes);
-    mParent->machine()->COMGETTER(VideoCaptureFile)(&strVideoCaptureFile);
+    int rc = RTCritSectInit(&mpVideoRecContext->CritSect);
+    AssertRCReturn(rc, E_UNEXPECTED);
+
+    mpVideoRecContext->fEnabled = false;
     mParent->machine()->COMGETTER(VideoCaptureEnabled)(&fEnabled);
     if (fEnabled)
     {
-        LogFlow(("VidoeRecording VPX enabled \n"));
-        if (VideoRecContextInit(pVideoRecContext,strVideoCaptureFile,
-                                  ulVideoCaptureHorzRes, ulVideoCaptureVertRes))
-        {
-            LogFlow(("Failed to initialize video recording context \n"));
+        ULONG ulVideoCaptureHorzRes;
+        mParent->machine()->COMGETTER(VideoCaptureWidth)(&ulVideoCaptureHorzRes);
+        ULONG ulVideoCaptureVertRes;
+        mParent->machine()->COMGETTER(VideoCaptureHeight)(&ulVideoCaptureVertRes);
+        BSTR strVideoCaptureFile;
+        mParent->machine()->COMGETTER(VideoCaptureFile)(&strVideoCaptureFile);
+        LogFlow(("VidoeRecording VPX enabled\n"));
+        if (VideoRecContextInit(mpVideoRecContext,strVideoCaptureFile,
+                                ulVideoCaptureHorzRes, ulVideoCaptureVertRes))
+        {
+            LogFlow(("Failed to initialize video recording context\n"));
             return E_FAIL;
         }
-        pVideoRecContext->fEnabled = true;
+        mpVideoRecContext->fEnabled = true;
     }
 #endif
+
     mcMonitors = ul;
 
@@ -562,5 +564,7 @@
 
 #ifdef VBOX_WITH_VPX
-    VideoRecContextClose(pVideoRecContext);
+    if (   mpVideoRecContext
+        && mpVideoRecContext->fEnabled)
+        VideoRecContextClose(mpVideoRecContext);
 #endif
 }
@@ -3204,5 +3208,5 @@
     unsigned uScreenId;
 
-    LogFlow(("DisplayRefreshCallback \n"));
+    LogFlow(("DisplayRefreshCallback\n"));
     for (uScreenId = 0; uScreenId < pDisplay->mcMonitors; uScreenId++)
     {
@@ -3277,67 +3281,70 @@
 
 #ifdef VBOX_WITH_VPX
-    uint32_t u32VideoRecImgFormat = VPX_IMG_FMT_NONE;
-    DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
-    int rc;
-
-    if (!pFBInfo->pFramebuffer.isNull() && !(pFBInfo->fDisabled)
-         && pFBInfo->u32ResizeStatus==ResizeStatus_Void)
-    {
-        HRESULT rc;
-        ULONG ulPixelFormat = 0;
-        rc = pFBInfo->pFramebuffer->COMGETTER(PixelFormat)(&ulPixelFormat);
-        AssertComRC (rc);
-
-        ULONG ulBitsPerPixel;
-        rc = pFBInfo->pFramebuffer->COMGETTER(BitsPerPixel)(&ulBitsPerPixel);
-        AssertComRC (rc);
-
-        ULONG ulGuestHeight = 0;
-        rc = pFBInfo->pFramebuffer->COMGETTER(Height)(&ulGuestHeight);
-        AssertComRC (rc);
-
-        ULONG ulGuestWidth = 0;
-        rc = pFBInfo->pFramebuffer->COMGETTER(Width)(&ulGuestWidth);
-        AssertComRC (rc);
-
-        BYTE *address = NULL;
-        rc = pFBInfo->pFramebuffer->COMGETTER(Address) (&address);
-        AssertComRC (rc);
-
-        ULONG ulBytesPerLine = 0;
-        rc = pFBInfo->pFramebuffer->COMGETTER(BytesPerLine) (&ulBytesPerLine);
-        AssertComRC (rc);
-
-        if(ulPixelFormat == FramebufferPixelFormat_FOURCC_RGB)
-        {
-            switch (ulBitsPerPixel)
+    if (pDisplay->mpVideoRecContext->fEnabled)
+    {
+        uint32_t u32VideoRecImgFormat = VPX_IMG_FMT_NONE;
+        DISPLAYFBINFO *pFBInfo = &pDisplay->maFramebuffers[VBOX_VIDEO_PRIMARY_SCREEN];
+
+        if (    !pFBInfo->pFramebuffer.isNull()
+            && !(pFBInfo->fDisabled)
+            && pFBInfo->u32ResizeStatus == ResizeStatus_Void)
+        {
+            HRESULT rc;
+            ULONG ulPixelFormat = 0;
+            rc = pFBInfo->pFramebuffer->COMGETTER(PixelFormat)(&ulPixelFormat);
+            AssertComRC(rc);
+
+            ULONG ulBitsPerPixel;
+            rc = pFBInfo->pFramebuffer->COMGETTER(BitsPerPixel)(&ulBitsPerPixel);
+            AssertComRC(rc);
+
+            ULONG ulGuestHeight = 0;
+            rc = pFBInfo->pFramebuffer->COMGETTER(Height)(&ulGuestHeight);
+            AssertComRC(rc);
+
+            ULONG ulGuestWidth = 0;
+            rc = pFBInfo->pFramebuffer->COMGETTER(Width)(&ulGuestWidth);
+            AssertComRC(rc);
+
+            BYTE *address = NULL;
+            rc = pFBInfo->pFramebuffer->COMGETTER(Address)(&address);
+            AssertComRC(rc);
+
+            ULONG ulBytesPerLine = 0;
+            rc = pFBInfo->pFramebuffer->COMGETTER(BytesPerLine)(&ulBytesPerLine);
+            AssertComRC(rc);
+
+            if (ulPixelFormat == FramebufferPixelFormat_FOURCC_RGB)
             {
-                case 32:
-                    u32VideoRecImgFormat = VPX_IMG_FMT_RGB32;
-                    Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to VPX_IMG_FMT_RGB32\n"));
-                    break;
-                case 24:
-                    u32VideoRecImgFormat = VPX_IMG_FMT_RGB24;
-                    Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to VPX_IMG_FMT_RGB24\n"));
-                    break;
-                case 16:
-                    u32VideoRecImgFormat = VPX_IMG_FMT_RGB565;
-                    Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to VPX_IMG_FMT_RGB565\n"));
-                    break;
-                default:
-                    Log2(("No Proper Format detected\n"));
-
+                switch (ulBitsPerPixel)
+                {
+                    case 32:
+                        u32VideoRecImgFormat = VPX_IMG_FMT_RGB32;
+                        Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to VPX_IMG_FMT_RGB32\n"));
+                        break;
+                    case 24:
+                        u32VideoRecImgFormat = VPX_IMG_FMT_RGB24;
+                        Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to VPX_IMG_FMT_RGB24\n"));
+                        break;
+                    case 16:
+                        u32VideoRecImgFormat = VPX_IMG_FMT_RGB565;
+                        Log2(("FFmpeg::RequestResize: setting ffmpeg pixel format to VPX_IMG_FMT_RGB565\n"));
+                        break;
+                    default:
+                        Log2(("No Proper Format detected\n"));
+                        break;
+                }
             }
-        }
-
-        if (u32VideoRecImgFormat != VPX_IMG_FMT_NONE && address != NULL
-            && pVideoRecContext->fEnabled)
-        {
-            VideoRecCopyToIntBuffer(pVideoRecContext, pFBInfo->xOrigin, pFBInfo->yOrigin,
-                              ulPixelFormat, ulBitsPerPixel, ulBytesPerLine,
-                              ulGuestWidth, ulGuestHeight, address);
-
-            VideoRecDoRGBToYUV(pVideoRecContext, u32VideoRecImgFormat);
-            VideoRecEncodeAndWrite(pVideoRecContext, ulGuestWidth, ulGuestHeight);
+
+            if (   u32VideoRecImgFormat != VPX_IMG_FMT_NONE
+                && address)
+            {
+                VideoRecCopyToIntBuffer(pDisplay->mpVideoRecContext, pFBInfo->xOrigin, pFBInfo->yOrigin,
+                                        ulPixelFormat, ulBitsPerPixel, ulBytesPerLine,
+                                        ulGuestWidth, ulGuestHeight, address);
+
+                VideoRecDoRGBToYUV(pDisplay->mpVideoRecContext, u32VideoRecImgFormat);
+                VideoRecEncodeAndWrite(pDisplay->mpVideoRecContext, ulGuestWidth, ulGuestHeight);
+            }
         }
     }
