Index: /trunk/src/VBox/Devices/Audio/DrvHostCoreAudio.cpp
===================================================================
--- /trunk/src/VBox/Devices/Audio/DrvHostCoreAudio.cpp	(revision 59377)
+++ /trunk/src/VBox/Devices/Audio/DrvHostCoreAudio.cpp	(revision 59378)
@@ -562,5 +562,5 @@
             if (err != noErr)
             {
-                LogFlowFunc(("Failed rendering audio data (%RI32)\n", err));
+                LogRel2(("CoreAudio: Failed rendering converted audio input data (%RI32)\n", err));
                 rc = VERR_IO_GEN_FAILURE; /** @todo Improve this. */
                 break;
@@ -623,7 +623,12 @@
         else /* No converter being used. */
         {
+            AssertBreakStmt(pStreamIn->streamFormat.mChannelsPerFrame >= 1,    rc = VERR_INVALID_PARAMETER);
+            AssertBreakStmt(pStreamIn->streamFormat.mBytesPerFrame >= 1,       rc = VERR_INVALID_PARAMETER);
+
+            AssertBreakStmt(pStreamIn->bufferList.mNumberBuffers >= 1,         rc = VERR_INVALID_PARAMETER);
+            AssertBreakStmt(pStreamIn->bufferList.mBuffers[0].mNumberChannels, rc = VERR_INVALID_PARAMETER);
+
             pStreamIn->bufferList.mBuffers[0].mNumberChannels = pStreamIn->streamFormat.mChannelsPerFrame;
             pStreamIn->bufferList.mBuffers[0].mDataByteSize   = pStreamIn->streamFormat.mBytesPerFrame * cFrames;
-            AssertBreakStmt(pStreamIn->bufferList.mBuffers[0].mDataByteSize, rc = VERR_INVALID_PARAMETER);
             pStreamIn->bufferList.mBuffers[0].mData           = RTMemAlloc(pStreamIn->bufferList.mBuffers[0].mDataByteSize);
             if (!pStreamIn->bufferList.mBuffers[0].mData)
@@ -636,15 +641,19 @@
             if (err != noErr)
             {
-                LogFlowFunc(("Failed rendering audio data (%RI32)\n", err));
+                LogRel2(("CoreAudio: Failed rendering non-coverted audio input data (%RI32)\n", err));
                 rc = VERR_IO_GEN_FAILURE; /** @todo Improve this. */
                 break;
             }
 
-            size_t cbAvail = RT_MIN(RTCircBufFree(pStreamIn->pBuf), pStreamIn->bufferList.mBuffers[0].mDataByteSize);
+            const uint32_t cbDataSize = pStreamIn->bufferList.mBuffers[0].mDataByteSize;
+            const size_t   cbBufFree  = RTCircBufFree(pStreamIn->pBuf);
+                  size_t   cbAvail    = RT_MIN(cbDataSize, cbBufFree);
+
+            LogFlowFunc(("cbDataSize=%RU32, cbBufFree=%zu, cbAvail=%zu\n", cbDataSize, cbBufFree, cbAvail));
 
             /* Iterate as long as data is available. */
             uint8_t *puDst = NULL;
             uint32_t cbWrittenTotal = 0;
-            while(cbAvail)
+            while (cbAvail)
             {
                 /* Try to acquire the necessary space from the ring buffer. */
@@ -654,5 +663,5 @@
                     break;
 
-                /* Copy the data from the core audio buffer to the ring buffer. */
+                /* Copy the data from the Core Audio buffer to the ring buffer. */
                 memcpy(puDst, (uint8_t *)pStreamIn->bufferList.mBuffers[0].mData + cbWrittenTotal, cbToWrite);
 
@@ -665,4 +674,6 @@
                 cbAvail -= cbToWrite;
             }
+
+            LogFlowFunc(("cbWrittenTotal=%RU32, cbLeft=%zu\n", cbWrittenTotal, cbAvail));
         }
 
@@ -675,4 +686,5 @@
     }
 
+    LogFlowFuncLeaveRC(rc);
     return err;
 }
@@ -760,4 +772,6 @@
         return VERR_AUDIO_BACKEND_INIT_FAILED;
     }
+
+    LogFlowFunc(("cFrames=%RU32\n", cFrames));
 
     ComponentDescription cd;
@@ -882,5 +896,5 @@
         /* Set the new input format description for the stream. */
         err = AudioUnitSetProperty(pStreamIn->audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input,
-                                   1, &pStreamIn->deviceFormat, sizeof(pStreamIn->deviceFormat));
+                                   1, &pStreamIn->streamFormat, sizeof(pStreamIn->streamFormat));
         if (err != noErr)
         {
@@ -898,12 +912,14 @@
         LogRel(("CoreAudio: Input converter is active\n"));
     }
-
-    /* Set the new output format description for the stream. */
-    err = AudioUnitSetProperty(pStreamIn->audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output,
-                               1, &pStreamIn->deviceFormat, sizeof(pStreamIn->deviceFormat));
-    if (err != noErr)
-    {
-        LogRel(("CoreAudio: Failed to set output format for input stream (%RI32)\n", err));
-        return VERR_AUDIO_BACKEND_INIT_FAILED;
+    else
+    {
+        /* Set the new output format description for the stream. */
+        err = AudioUnitSetProperty(pStreamIn->audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output,
+                                   1, &pStreamIn->streamFormat, sizeof(pStreamIn->streamFormat));
+        if (err != noErr)
+        {
+            LogRel(("CoreAudio: Failed to set output format for input stream (%RI32)\n", err));
+            return VERR_AUDIO_BACKEND_INIT_FAILED; /** @todo Fudge! */
+        }
     }
 
@@ -943,5 +959,5 @@
      */
     uSize = sizeof(cFrames);
-    err = AudioUnitGetProperty(pStreamIn->audioUnit, kAudioUnitProperty_MaximumFramesPerSlice,kAudioUnitScope_Global,
+    err = AudioUnitGetProperty(pStreamIn->audioUnit, kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global,
                                0, &cFrames, &uSize);
     if (err != noErr)
@@ -972,5 +988,8 @@
      * samples count.
      */
-    UInt32 cSamples = cFrames * pStreamIn->streamFormat.mChannelsPerFrame;
+    UInt32 cSamples = RT_MAX(cFrames,
+                             (cFrames * pStreamIn->deviceFormat.mBytesPerFrame * pStreamIn->sampleRatio)
+                              / pStreamIn->streamFormat.mBytesPerFrame)
+                             * pStreamIn->streamFormat.mChannelsPerFrame;
     if (!cSamples)
     {
@@ -1338,6 +1357,5 @@
     {
         size_t cbBuf = AudioMixBufSizeBytes(&pHstStrmIn->MixBuf);
-        size_t cbToWrite = RT_MIN(cbBuf, RTCircBufFree(pStreamIn->pBuf));
-        LogFlowFunc(("cbToWrite=%zu\n", cbToWrite));
+        size_t cbToWrite = RT_MIN(cbBuf, RTCircBufUsed(pStreamIn->pBuf));
 
         uint32_t cWritten, cbWritten;
@@ -1345,4 +1363,6 @@
         size_t   cbToRead;
 
+        LogFlowFunc(("cbBuf=%zu, cbToWrite=%zu\n", cbBuf, cbToWrite));
+
         while (cbToWrite)
         {
@@ -1365,7 +1385,9 @@
 
             Assert(cbToWrite >= cbWritten);
-            cbToWrite -= cbWritten;
+            cbToWrite      -= cbWritten;
             cbWrittenTotal += cbWritten;
         }
+
+        LogFlowFunc(("cbToWrite=%zu, cbWrittenTotal=%RU32\n", cbToWrite, cbWrittenTotal));
     }
     while (0);
@@ -1373,14 +1395,16 @@
     if (RT_SUCCESS(rc))
     {
+        uint32_t cCaptured     = 0;
         uint32_t cWrittenTotal = AUDIOMIXBUF_B2S(&pHstStrmIn->MixBuf, cbWrittenTotal);
         if (cWrittenTotal)
-            AudioMixBufFinish(&pHstStrmIn->MixBuf, cWrittenTotal);
-
-        LogFlowFunc(("cWrittenTotal=%RU32 (%RU32 bytes)\n", cWrittenTotal, cbWrittenTotal));
+            rc = AudioMixBufMixToParent(&pHstStrmIn->MixBuf, cWrittenTotal, &cCaptured);
+
+        LogFlowFunc(("cWrittenTotal=%RU32 (%RU32 bytes), cCaptured, rc=%Rrc\n", cWrittenTotal, cbWrittenTotal, cCaptured, rc));
 
         if (pcSamplesCaptured)
-            *pcSamplesCaptured = cWrittenTotal;
-    }
-
+            *pcSamplesCaptured = cCaptured;
+    }
+
+    LogFlowFuncLeaveRC(rc);
     return rc;
 }
