Index: trunk/src/VBox/Main/src-client/ConsoleImpl.cpp
===================================================================
--- trunk/src/VBox/Main/src-client/ConsoleImpl.cpp	(revision 75307)
+++ trunk/src/VBox/Main/src-client/ConsoleImpl.cpp	(revision 75313)
@@ -5629,7 +5629,4 @@
                 vrc = i_videoRecCreate();
                 if (RT_SUCCESS(vrc))
-                    vrc = i_videoRecStart();
-
-                if (RT_SUCCESS(vrc))
                 {
 # ifdef VBOX_WITH_AUDIO_VIDEOREC
@@ -6899,14 +6896,19 @@
     Assert(mMachine.isNotNull());
 
+    Settings.applyDefaults();
+
     ComPtr<ICaptureSettings> pCaptureSettings;
     HRESULT hrc = mMachine->COMGETTER(CaptureSettings)(pCaptureSettings.asOutParam());
     AssertComRCReturn(hrc, VERR_INVALID_PARAMETER);
 
+    BOOL fTemp;
+    hrc = pCaptureSettings->COMGETTER(Enabled)(&fTemp);
+    AssertComRCReturn(hrc, VERR_INVALID_PARAMETER);
+    Settings.fEnabled = RT_BOOL(fTemp);
+
     SafeIfaceArray<ICaptureScreenSettings> paCaptureScreens;
     hrc = pCaptureSettings->COMGETTER(Screens)(ComSafeArrayAsOutParam(paCaptureScreens));
     AssertComRCReturn(hrc, VERR_INVALID_PARAMETER);
 
-    Settings.applyDefaults();
-
     for (unsigned long i = 0; i < (unsigned long)paCaptureScreens.size(); ++i)
     {
@@ -6914,4 +6916,7 @@
         ComPtr<ICaptureScreenSettings> pCaptureScreenSettings = paCaptureScreens[i];
 
+        hrc = pCaptureScreenSettings->COMGETTER(Enabled)(&fTemp);
+        AssertComRCReturn(hrc, VERR_INVALID_PARAMETER);
+        CaptureScreenSettings.fEnabled = RT_BOOL(fTemp);
         hrc = pCaptureScreenSettings->COMGETTER(MaxTime)((ULONG *)&CaptureScreenSettings.ulMaxTimeS);
         AssertComRCReturn(hrc, VERR_INVALID_PARAMETER);
@@ -7006,8 +7011,10 @@
     LogRel(("VideoRec: Starting ...\n"));
 
-    int rc = VINF_SUCCESS;
-
-    for (unsigned uScreen = 0; uScreen < Capture.mpVideoRecCtx->GetStreamCount(); uScreen++)
-        mDisplay->i_videoRecScreenChanged(uScreen);
+    int rc = Capture.mpVideoRecCtx->Start();
+    if (RT_SUCCESS(rc))
+    {
+        for (unsigned uScreen = 0; uScreen < Capture.mpVideoRecCtx->GetStreamCount(); uScreen++)
+            mDisplay->i_videoRecScreenChanged(uScreen);
+    }
 
     if (RT_FAILURE(rc))
@@ -7029,18 +7036,24 @@
     LogRel(("VideoRec: Stopping ...\n"));
 
-    const size_t cStreams = Capture.mpVideoRecCtx->GetStreamCount();
-    for (unsigned uScreen = 0; uScreen < cStreams; ++uScreen)
-        mDisplay->i_videoRecScreenChanged(uScreen);
-
-    ComPtr<ICaptureSettings> pCaptureSettings;
-    HRESULT hrc = mMachine->COMGETTER(CaptureSettings)(pCaptureSettings.asOutParam());
-    ComAssertComRC(hrc);
-    hrc = pCaptureSettings->COMSETTER(Enabled)(false);
-    ComAssertComRC(hrc);
-
-    LogRel(("VideoRec: Stopped\n"));
-
-    LogFlowFuncLeaveRC(VINF_SUCCESS);
-    return VINF_SUCCESS;
+    int rc = Capture.mpVideoRecCtx->Stop();
+    if (RT_SUCCESS(rc))
+    {
+        const size_t cStreams = Capture.mpVideoRecCtx->GetStreamCount();
+        for (unsigned uScreen = 0; uScreen < cStreams; ++uScreen)
+            mDisplay->i_videoRecScreenChanged(uScreen);
+
+        ComPtr<ICaptureSettings> pCaptureSettings;
+        HRESULT hrc = mMachine->COMGETTER(CaptureSettings)(pCaptureSettings.asOutParam());
+        ComAssertComRC(hrc);
+        hrc = pCaptureSettings->COMSETTER(Enabled)(false);
+        ComAssertComRC(hrc);
+
+        LogRel(("VideoRec: Stopped\n"));
+    }
+    else
+        LogRel(("VideoRec: Failed to stop video recording (%Rrc)\n", rc));
+
+    LogFlowFuncLeaveRC(rc);
+    return rc;
 }
 #endif /* VBOX_WITH_VIDEOREC */
Index: trunk/src/VBox/Main/src-client/VideoRec.cpp
===================================================================
--- trunk/src/VBox/Main/src-client/VideoRec.cpp	(revision 75307)
+++ trunk/src/VBox/Main/src-client/VideoRec.cpp	(revision 75313)
@@ -56,20 +56,4 @@
 //# define VBOX_VIDEOREC_DUMP
 #endif
-
-/**
- * Enumeration for a recording state.
- */
-enum VIDEORECSTS
-{
-    /** Not initialized. */
-    VIDEORECSTS_UNINITIALIZED = 0,
-    /** Created. */
-    VIDEORECSTS_CREATED       = 1,
-    /** Started. */
-    VIDEORECSTS_STARTED       = 2,
-    /** The usual 32-bit hack. */
-    VIDEORECSTS_32BIT_HACK    = 0x7fffffff
-};
-
 
 #ifdef VBOX_VIDEOREC_DUMP
@@ -205,5 +189,5 @@
         try
         {
-            pStream = new CaptureStream(itScreen->first /* Screen ID */, itScreen->second);
+            pStream = new CaptureStream(this, itScreen->first /* Screen ID */, itScreen->second);
             this->vecStreams.push_back(pStream);
         }
@@ -213,4 +197,6 @@
             break;
         }
+
+        ++itScreen;
     }
 
@@ -219,5 +205,4 @@
         this->tsStartMs = RTTimeMilliTS();
         this->enmState  = VIDEORECSTS_CREATED;
-        this->fStarted  = false;
         this->fShutdown = false;
 
@@ -246,5 +231,5 @@
 
     int rc = RTThreadCreate(&this->Thread, CaptureContext::threadMain, (void *)this, 0,
-                            RTTHREADTYPE_MAIN_WORKER, RTTHREADFLAGS_WAITABLE, "VideoRec");
+                            RTTHREADTYPE_MAIN_WORKER, RTTHREADFLAGS_WAITABLE, "Record");
 
     if (RT_SUCCESS(rc)) /* Wait for the thread to start. */
@@ -254,5 +239,4 @@
     {
         this->enmState = VIDEORECSTS_STARTED;
-        this->fStarted = true;
     }
 
@@ -265,5 +249,5 @@
         return VINF_SUCCESS;
 
-    LogFunc(("Shutting down thread ...\n"));
+    LogThisFunc(("Shutting down thread ...\n"));
 
     /* Set shutdown indicator. */
@@ -275,4 +259,10 @@
         rc = RTThreadWait(this->Thread, 30 * 1000 /* 10s timeout */, NULL);
 
+    if (RT_SUCCESS(rc))
+    {
+        this->enmState = VIDEORECSTS_CREATED;
+    }
+
+    LogFlowThisFunc(("%Rrc\n", rc));
     return rc;
 }
@@ -283,26 +273,12 @@
 int CaptureContext::destroyInternal(void)
 {
-    int rc = VINF_SUCCESS;
-
-    if (this->enmState == VIDEORECSTS_STARTED)
-    {
-        rc = stopInternal();
-        if (RT_SUCCESS(rc))
-        {
-            /* Disable the context. */
-            ASMAtomicWriteBool(&this->fStarted, false);
-
-            int rc2 = RTSemEventDestroy(this->WaitEvent);
-            AssertRC(rc2);
-
-            this->WaitEvent = NIL_RTSEMEVENT;
-        }
-    }
-
+    int rc = stopInternal();
     if (RT_FAILURE(rc))
-    {
-        AssertRC(rc);
         return rc;
-    }
+
+    rc = RTSemEventDestroy(this->WaitEvent);
+    AssertRC(rc);
+
+    this->WaitEvent = NIL_RTSEMEVENT;
 
     rc = RTCritSectEnter(&this->CritSect);
@@ -338,4 +314,5 @@
     }
 
+    LogFlowThisFunc(("%Rrc\n", rc));
     return rc;
 }
@@ -411,13 +388,18 @@
 }
 
+/**
+ * Returns if this recording context is ready to start recording.
+ *
+ * @returns @c true if recording context is ready, @c false if not.
+ */
 bool CaptureContext::IsReady(void) const
 {
-    return this->fStarted;
-}
-
-/**
- * Checks if recording engine is ready to accept new recording data for a given screen.
- *
- * @returns true if recording engine is ready, false if not.
+    return (this->enmState >= VIDEORECSTS_CREATED);
+}
+
+/**
+ * Returns if this recording context is ready to accept new recording data for a given screen.
+ *
+ * @returns @c true if the specified screen is ready, @c false if not.
  * @param   uScreen             Screen ID.
  * @param   uTimeStampMs        Current time stamp (in ms). Currently not being used.
@@ -450,5 +432,5 @@
 bool CaptureContext::IsStarted(void) const
 {
-    return this->fStarted;
+    return (this->enmState == VIDEORECSTS_STARTED);
 }
 
Index: trunk/src/VBox/Main/src-client/VideoRecStream.cpp
===================================================================
--- trunk/src/VBox/Main/src-client/VideoRecStream.cpp	(revision 75307)
+++ trunk/src/VBox/Main/src-client/VideoRecStream.cpp	(revision 75313)
@@ -42,6 +42,8 @@
 
 
-CaptureStream::CaptureStream(void)
-    : tsStartMs(0)
+CaptureStream::CaptureStream(CaptureContext *a_pCtx)
+    : pCtx(a_pCtx)
+    , enmState(RECORDINGSTREAMSTATE_UNINITIALIZED)
+    , tsStartMs(0)
 {
     File.pWEBM = NULL;
@@ -49,11 +51,12 @@
 }
 
-CaptureStream::CaptureStream(uint32_t uScreen, const settings::CaptureScreenSettings &Settings)
-    : tsStartMs(0)
+CaptureStream::CaptureStream(CaptureContext *a_pCtx, uint32_t uScreen, const settings::CaptureScreenSettings &Settings)
+    : enmState(RECORDINGSTREAMSTATE_UNINITIALIZED)
+    , tsStartMs(0)
 {
     File.pWEBM = NULL;
     File.hFile = NIL_RTFILE;
 
-    int rc2 = initInternal(uScreen, Settings);
+    int rc2 = initInternal(a_pCtx, uScreen, Settings);
     if (RT_FAILURE(rc2))
         throw rc2;
@@ -645,22 +648,31 @@
         VideoRecVideoFrameFree(pFrame);
 
-    lock();
+    unlock();
 
     return rc;
 }
 
-int CaptureStream::Init(uint32_t uScreen, const settings::CaptureScreenSettings &Settings)
-{
-    return initInternal(uScreen, Settings);
-}
-
 /**
  * Initializes a recording stream.
  *
  * @returns IPRT status code.
+ * @param   a_pCtx              Pointer to recording context.
  * @param   uScreen             Screen number to use for this recording stream.
  * @param   Settings            Capturing configuration to use for initialization.
  */
-int CaptureStream::initInternal(uint32_t uScreen, const settings::CaptureScreenSettings &Settings)
+int CaptureStream::Init(CaptureContext *a_pCtx, uint32_t uScreen, const settings::CaptureScreenSettings &Settings)
+{
+    return initInternal(a_pCtx, uScreen, Settings);
+}
+
+/**
+ * Initializes a recording stream, internal version.
+ *
+ * @returns IPRT status code.
+ * @param   a_pCtx              Pointer to recording context.
+ * @param   uScreen             Screen number to use for this recording stream.
+ * @param   Settings            Capturing configuration to use for initialization.
+ */
+int CaptureStream::initInternal(CaptureContext *a_pCtx, uint32_t uScreen, const settings::CaptureScreenSettings &Settings)
 {
     int rc = parseOptionsString(Settings.strOptions);
@@ -770,4 +782,6 @@
     if (RT_SUCCESS(rc))
     {
+        this->pCtx           = a_pCtx;
+        this->enmState       = RECORDINGSTREAMSTATE_INITIALIZED;
         this->fEnabled       = true;
         this->uScreenID      = uScreen;
@@ -870,4 +884,7 @@
 int CaptureStream::uninitInternal(void)
 {
+    if (this->enmState != RECORDINGSTREAMSTATE_INITIALIZED)
+        return VINF_SUCCESS;
+
     int rc = close();
     if (RT_FAILURE(rc))
@@ -883,4 +900,5 @@
     RTCritSectDelete(&this->CritSect);
 
+    this->enmState = RECORDINGSTREAMSTATE_UNINITIALIZED;
     this->fEnabled = false;
 
@@ -911,5 +929,8 @@
 int CaptureStream::uninitVideoVPX(void)
 {
-    vpx_img_free(&this->Video.Codec.VPX.RawImage);
+    PVIDEORECVIDEOCODEC pCodec = &this->Video.Codec;
+    vpx_img_free(&pCodec->VPX.RawImage);
+    pCodec->VPX.pu8YuvBuf = NULL; /* Was pointing to VPX.RawImage. */
+
     vpx_codec_err_t rcv = vpx_codec_destroy(&this->Video.Codec.VPX.Ctx);
     Assert(rcv == VPX_CODEC_OK); RT_NOREF(rcv);
