Index: trunk/src/VBox/Devices/Audio/DrvHostCoreAudio.cpp
===================================================================
--- trunk/src/VBox/Devices/Audio/DrvHostCoreAudio.cpp	(revision 64030)
+++ trunk/src/VBox/Devices/Audio/DrvHostCoreAudio.cpp	(revision 64034)
@@ -42,5 +42,5 @@
 #endif
 
-#if 0
+#if 1
 # include <iprt/file.h>
 # define DEBUG_DUMP_PCM_DATA
@@ -1928,6 +1928,4 @@
 int coreAudioInputQueueProcBuffer(PCOREAUDIOSTREAM pCAStream, AudioQueueBufferRef audioBuffer)
 {
-    size_t cbWritten = 0;
-
     PRTCIRCBUF pCircBuf = pCAStream->pCircBuf;
     AssertPtr(pCircBuf);
@@ -1936,4 +1934,6 @@
     UInt8 *pvDst = NULL;
 
+    size_t cbWritten = 0;
+
     size_t cbToWrite = audioBuffer->mAudioDataByteSize;
     size_t cbLeft    = cbToWrite;
@@ -1959,7 +1959,6 @@
     }
 
-    audioBuffer->mAudioDataByteSize = cbWritten;
-
-    Log3Func(("pCAStream=%p, cbWritten=%zu\n", pCAStream, cbWritten));
+    Log3Func(("pCAStream=%p, cbBuffer=%RU32/%zu, cbWritten=%zu\n",
+              pCAStream, audioBuffer->mAudioDataByteSize, audioBuffer->mAudioDataBytesCapacity, cbWritten));
 
     return VINF_SUCCESS;
@@ -2096,4 +2095,9 @@
         if (pCAStream->enmDir == PDMAUDIODIR_IN)
         {
+            int rc2 = coreAudioInputQueueProcBuffer(pCAStream, pBuf);
+            if (RT_SUCCESS(rc2))
+            {
+                AudioQueueEnqueueBuffer(pCAStream->audioQueue, pBuf, 0, NULL);
+            }
         }
         else if (pCAStream->enmDir == PDMAUDIODIR_OUT)
@@ -3223,5 +3227,5 @@
 
     int rc = VINF_SUCCESS;
-    uint32_t cbWrittenTotal = 0;
+    uint32_t csWrittenTotal = 0;
 
 #ifdef VBOX_WITH_AUDIO_CA_QUEUES
@@ -3235,7 +3239,6 @@
         size_t cbToWrite = RT_MIN(cbMixBuf, RTCircBufUsed(pCAStream->pCircBuf));
 
-        uint32_t cWritten, cbWritten;
+        uint32_t csWritten, cbWritten;
         uint8_t *puBuf;
-        size_t   cbToRead = 0;
 
         Log3Func(("cbMixBuf=%zu, cbToWrite=%zu/%zu\n", cbMixBuf, cbToWrite, RTCircBufSize(pCAStream->pCircBuf)));
@@ -3244,39 +3247,41 @@
         {
             /* Try to acquire the necessary block from the ring buffer. */
-            RTCircBufAcquireReadBlock(pCAStream->pCircBuf, cbToWrite, (void **)&puBuf, &cbToRead);
-            if (!cbToRead)
+            size_t cbChunk;
+            RTCircBufAcquireReadBlock(pCAStream->pCircBuf, cbToWrite, (void **)&puBuf, &cbChunk);
+            if (cbChunk)
+            {
+#ifdef DEBUG_DUMP_PCM_DATA
+                RTFILE fh;
+                rc = RTFileOpen(&fh, DEBUG_DUMP_PCM_DATA_PATH "ca-capture.pcm",
+                                RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
+                if (RT_SUCCESS(rc))
+                {
+                    RTFileWrite(fh, puBuf, cbChunk, NULL);
+                    RTFileClose(fh);
+                }
+                else
+                    AssertFailed();
+#endif
+                rc = AudioMixBufWriteCirc(&pStream->MixBuf, puBuf, cbChunk, &csWritten);
+                if (rc == VERR_BUFFER_OVERFLOW)
+                {
+                    LogRel2(("Core Audio: Capturing host buffer full\n"));
+                    rc = VINF_SUCCESS;
+                }
+            }
+
+            /* Release the read buffer, so it could be used for new data. */
+            RTCircBufReleaseReadBlock(pCAStream->pCircBuf, cbChunk);
+
+            if (RT_FAILURE(rc))
                 break;
 
-#ifdef DEBUG_DUMP_PCM_DATA
-            RTFILE fh;
-            rc = RTFileOpen(&fh, DEBUG_DUMP_PCM_DATA_PATH "ca-capture.pcm",
-                            RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
-            if (RT_SUCCESS(rc))
-            {
-                RTFileWrite(fh, puBuf + cbWrittenTotal, cbToRead, NULL);
-                RTFileClose(fh);
-            }
-            else
-                AssertFailed();
-#endif
-            rc = AudioMixBufWriteCirc(&pStream->MixBuf, puBuf + cbWrittenTotal, cbToRead, &cWritten);
-
-            /* Release the read buffer, so it could be used for new data. */
-            RTCircBufReleaseReadBlock(pCAStream->pCircBuf, cbToRead);
-
-            if (   RT_FAILURE(rc)
-                || !cWritten)
-            {
-                break;
-            }
-
-            cbWritten = AUDIOMIXBUF_S2B(&pStream->MixBuf, cWritten);
+            cbWritten = AUDIOMIXBUF_S2B(&pStream->MixBuf, csWritten);
 
             Assert(cbToWrite >= cbWritten);
             cbToWrite      -= cbWritten;
-            cbWrittenTotal += cbWritten;
-        }
-
-        Log3Func(("cbToWrite=%zu, cbToRead=%zu, cbWrittenTotal=%RU32, rc=%Rrc\n", cbToWrite, cbToRead, cbWrittenTotal, rc));
+
+            csWrittenTotal += csWritten;
+        }
     }
     while (0);
@@ -3287,18 +3292,22 @@
 #endif
 
+    uint32_t cbWrittenTotal = AUDIOMIXBUF_S2B(&pStream->MixBuf, csWrittenTotal);
+
+    Log3Func(("csWrittenTotal=%RU32 (%RU32 bytes), rc=%Rrc\n", csWrittenTotal, cbWrittenTotal, rc));
+
     if (RT_SUCCESS(rc))
     {
-        uint32_t cCaptured     = 0;
-        uint32_t cWrittenTotal = AUDIOMIXBUF_B2S(&pStream->MixBuf, cbWrittenTotal);
-        if (cWrittenTotal)
-            rc = AudioMixBufMixToParent(&pStream->MixBuf, cWrittenTotal, &cCaptured);
-
-        Log3Func(("cWrittenTotal=%RU32 (%RU32 bytes), cCaptured=%RU32, rc=%Rrc\n", cWrittenTotal, cbWrittenTotal, cCaptured, rc));
-
-        if (cCaptured)
-            LogFlowFunc(("%RU32 samples captured\n", cCaptured));
+        uint32_t csMixed = 0;
+
+        if (csWrittenTotal)
+        {
+            RT_BREAKPOINT();
+            rc = AudioMixBufMixToParent(&pStream->MixBuf, csWrittenTotal, &csMixed);
+        }
+
+        Log3Func(("csMixed=%RU32\n", csMixed));
 
         if (pcbRead)
-            *pcbRead = cCaptured;
+            *pcbRead = csMixed;
     }
 
@@ -3462,5 +3471,19 @@
 #ifdef VBOX_WITH_AUDIO_CA_QUEUES
             LogFunc(("Queue enable\n"));
-            ASMAtomicXchgBool(&pCAStream->fRun, true);
+            if (pCAStream->enmDir == PDMAUDIODIR_IN)
+            {
+                rc = coreAudioStreamInvalidateQueue(pCAStream);
+                if (RT_SUCCESS(rc))
+                {
+                    /* Start the audio queue immediately. */
+                    AudioQueueStart(pCAStream->audioQueue, NULL);
+                }
+            }
+            if (pCAStream->enmDir == PDMAUDIODIR_OUT)
+            {
+                /* Touch the run flag to start the audio queue as soon as
+                 * we have anough data to actually play something. */
+                ASMAtomicXchgBool(&pCAStream->fRun, true);
+            }
 #else
             /* Only start the device if it is actually stopped */
