Index: trunk/src/VBox/Devices/Audio/DrvAudio.cpp
===================================================================
--- trunk/src/VBox/Devices/Audio/DrvAudio.cpp	(revision 88242)
+++ trunk/src/VBox/Devices/Audio/DrvAudio.cpp	(revision 88243)
@@ -142,4 +142,8 @@
     /** Shutdown indicator. */
     bool                    fTerminate;
+#ifdef VBOX_WITH_AUDIO_ENUM
+    /** Flag indicating to perform an (re-)enumeration of the host audio devices. */
+    bool                    fEnumerateDevices;
+#endif
     /** Our audio connector interface. */
     PDMIAUDIOCONNECTOR      IAudioConnector;
@@ -150,18 +154,6 @@
     /** List of audio streams. */
     RTLISTANCHOR            lstStreams;
-#ifdef VBOX_WITH_AUDIO_ENUM
-    /** Flag indicating to perform an (re-)enumeration of the host audio devices. */
-    bool                    fEnumerateDevices;
-#endif
     /** Audio configuration settings retrieved from the backend. */
     PDMAUDIOBACKENDCFG      BackendCfg;
-    /** Commonly used scratch buffer. */
-    void                   *pvScratchBuf;
-    /** Size (in bytes) of commonly used scratch buffer. */
-    size_t                  cbScratchBuf;
-#ifdef VBOX_WITH_STATISTICS
-    /** Statistics for the statistics manager (STAM). */
-    DRVAUDIOSTATS           Stats;
-#endif
     struct
     {
@@ -192,5 +184,11 @@
         DRVAUDIOCFG         Cfg;
     } Out;
-} DRVAUDIO, *PDRVAUDIO;
+#ifdef VBOX_WITH_STATISTICS
+    /** Statistics for the statistics manager (STAM). */
+    DRVAUDIOSTATS           Stats;
+#endif
+} DRVAUDIO;
+/** Pointer to the instance data of an audio driver. */
+typedef DRVAUDIO *PDRVAUDIO;
 
 
@@ -1450,5 +1448,5 @@
  * Plays an audio host output stream which has been configured for non-interleaved (layout) data.
  *
- * @return  IPRT status code.
+ * @returns VBox status code.
  * @param   pThis               Pointer to driver instance.
  * @param   pStream             Stream to play.
@@ -1459,8 +1457,13 @@
                                             PPDMAUDIOSTREAM pStream, uint32_t cfToPlay, uint32_t *pcfPlayed)
 {
-    AssertPtrReturn(pThis,   VERR_INVALID_POINTER);
+    AssertPtrReturn(pThis, VERR_INVALID_POINTER);
     AssertPtrReturn(pStream, VERR_INVALID_POINTER);
-    /* pcfPlayed is optional. */
-
+    AssertPtrNullReturn(pcfPlayed, VERR_INVALID_POINTER);
+    Assert(pStream->enmDir == PDMAUDIODIR_OUT);
+    Assert(pStream->Host.Cfg.enmLayout == PDMAUDIOSTREAMLAYOUT_NON_INTERLEAVED);
+
+    /*
+     * ...
+     */
     if (!cfToPlay)
     {
@@ -1470,22 +1473,16 @@
     }
 
-    /* Sanity. */
-    Assert(pStream->enmDir == PDMAUDIODIR_OUT);
-    Assert(pStream->Host.Cfg.enmLayout == PDMAUDIOSTREAMLAYOUT_NON_INTERLEAVED);
-
-    int rc = VINF_SUCCESS;
-
+    /*
+     * ...
+     */
+    int      rc            = VINF_SUCCESS;
     uint32_t cfPlayedTotal = 0;
-
-    uint32_t cfLeft  = cfToPlay;
-
-    uint8_t  *pvChunk = (uint8_t *)pThis->pvScratchBuf;
-    uint32_t  cbChunk = (uint32_t)pThis->cbScratchBuf;
-
+    uint32_t cfLeft        = cfToPlay;
     while (cfLeft)
     {
+        uint8_t  abChunk[_4K];
         uint32_t cfRead = 0;
         rc = AudioMixBufAcquireReadBlock(&pStream->Host.MixBuf,
-                                         pvChunk, RT_MIN(cbChunk, AUDIOMIXBUF_F2B(&pStream->Host.MixBuf, cfLeft)),
+                                         abChunk, RT_MIN(sizeof(abChunk), AUDIOMIXBUF_F2B(&pStream->Host.MixBuf, cfLeft)),
                                          &cfRead);
         if (RT_FAILURE(rc))
@@ -1493,15 +1490,14 @@
 
         uint32_t cbRead = AUDIOMIXBUF_F2B(&pStream->Host.MixBuf, cfRead);
-        Assert(cbRead <= cbChunk);
+        Assert(cbRead <= sizeof(abChunk));
 
         uint32_t cfPlayed = 0;
         uint32_t cbPlayed = 0;
-        rc = pThis->pHostDrvAudio->pfnStreamPlay(pThis->pHostDrvAudio, pStream->pvBackend,
-                                                 pvChunk, cbRead, &cbPlayed);
+        rc = pThis->pHostDrvAudio->pfnStreamPlay(pThis->pHostDrvAudio, pStream->pvBackend, abChunk, cbRead, &cbPlayed);
         if (   RT_SUCCESS(rc)
             && cbPlayed)
         {
             if (pThis->Out.Cfg.Dbg.fEnabled)
-                AudioHlpFileWrite(pStream->Out.Dbg.pFilePlayNonInterleaved, pvChunk, cbPlayed, 0 /* fFlags */);
+                AudioHlpFileWrite(pStream->Out.Dbg.pFilePlayNonInterleaved, abChunk, cbPlayed, 0 /* fFlags */);
 
             if (cbRead != cbPlayed)
@@ -1536,5 +1532,5 @@
  * Plays an audio host output stream which has been configured for raw audio (layout) data.
  *
- * @return  IPRT status code.
+ * @returns VBox status code.
  * @param   pThis               Pointer to driver instance.
  * @param   pStream             Stream to play.
@@ -1545,12 +1541,13 @@
                                  PPDMAUDIOSTREAM pStream, uint32_t cfToPlay, uint32_t *pcfPlayed)
 {
-    AssertPtrReturn(pThis,      VERR_INVALID_POINTER);
+    AssertPtrReturn(pThis, VERR_INVALID_POINTER);
     AssertPtrReturn(pStream, VERR_INVALID_POINTER);
-    /* pcfPlayed is optional. */
-
-    /* Sanity. */
+    AssertPtrNullReturn(pcfPlayed, VERR_INVALID_POINTER);
     Assert(pStream->enmDir == PDMAUDIODIR_OUT);
     Assert(pStream->Host.Cfg.enmLayout == PDMAUDIOSTREAMLAYOUT_RAW);
 
+    /*
+     * ...
+     */
     if (!cfToPlay)
     {
@@ -1560,34 +1557,26 @@
     }
 
-    int rc = VINF_SUCCESS;
-
-    uint32_t cfPlayedTotal = 0;
-
-    PPDMAUDIOFRAME paFrames = (PPDMAUDIOFRAME)pThis->pvScratchBuf;
-    const uint32_t  cFrames =      (uint32_t)(pThis->cbScratchBuf / sizeof(PDMAUDIOFRAME));
-
-    uint32_t cfLeft = cfToPlay;
+    /*
+     * ...
+     */
+    int         rc              = VINF_SUCCESS;
+    uint32_t    cfPlayedTotal   = 0;
+    uint32_t    cfLeft          = cfToPlay;
     while (cfLeft)
     {
-        uint32_t cfRead = 0;
-        rc = AudioMixBufPeek(&pStream->Host.MixBuf, cfLeft, paFrames,
-                             RT_MIN(cfLeft, cFrames), &cfRead);
-
+        PDMAUDIOFRAME   aFrames[_2K];
+        uint32_t        cfRead = 0;
+        rc = AudioMixBufPeek(&pStream->Host.MixBuf, cfLeft, aFrames, RT_MIN(cfLeft, RT_ELEMENTS(aFrames)), &cfRead);
         if (RT_SUCCESS(rc))
         {
             if (cfRead)
             {
-                uint32_t cfPlayed;
-
-                /* Note: As the stream layout is RPDMAUDIOSTREAMLAYOUT_RAW, operate on audio frames
-                 *       rather on bytes. */
-                Assert(cfRead <= cFrames);
-                rc = pThis->pHostDrvAudio->pfnStreamPlay(pThis->pHostDrvAudio, pStream->pvBackend,
-                                                         paFrames, cfRead, &cfPlayed);
+                /* Note: As the stream layout is RPDMAUDIOSTREAMLAYOUT_RAW, operate on audio frames rather on bytes. */
+                Assert(cfRead <= RT_ELEMENTS(aFrames));
+                uint32_t cfPlayed = 0;
+                rc = pThis->pHostDrvAudio->pfnStreamPlay(pThis->pHostDrvAudio, pStream->pvBackend, aFrames, cfRead, &cfPlayed);
                 if (   RT_FAILURE(rc)
                     || !cfPlayed)
-                {
                     break;
-                }
 
                 cfPlayedTotal += cfPlayed;
@@ -1599,23 +1588,18 @@
             else
             {
+/** @todo r=bird: This looks totally loopy.  Why should another call return data
+ *        when the first one didn't? */
                 if (rc == VINF_AUDIO_MORE_DATA_AVAILABLE) /* Do another peeking round if there is more data available. */
                     continue;
-
                 break;
             }
         }
-        else if (RT_FAILURE(rc))
+        else
             break;
     }
 
+    if (RT_SUCCESS(rc) && pcfPlayed)
+        *pcfPlayed = cfPlayedTotal;
     Log3Func(("[%s] Played %RU32/%RU32 frames, rc=%Rrc\n", pStream->szName, cfPlayedTotal, cfToPlay, rc));
-
-    if (RT_SUCCESS(rc))
-    {
-        if (pcfPlayed)
-            *pcfPlayed = cfPlayedTotal;
-
-    }
-
     return rc;
 }
@@ -1624,6 +1608,5 @@
  * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamPlay}
  */
-static DECLCALLBACK(int) drvAudioStreamPlay(PPDMIAUDIOCONNECTOR pInterface,
-                                            PPDMAUDIOSTREAM pStream, uint32_t *pcFramesPlayed)
+static DECLCALLBACK(int) drvAudioStreamPlay(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOSTREAM pStream, uint32_t *pcFramesPlayed)
 {
     PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
@@ -1631,5 +1614,4 @@
     AssertPtrReturn(pStream, VERR_INVALID_POINTER);
     AssertPtrNullReturn(pcFramesPlayed, VERR_INVALID_POINTER);
-
 
     int rc = RTCritSectEnter(&pThis->CritSect);
@@ -1802,9 +1784,6 @@
     RTCritSectLeave(&pThis->CritSect);
 
-    if (RT_SUCCESS(rc))
-    {
-        if (pcFramesPlayed)
-            *pcFramesPlayed = cfPlayedTotal;
-    }
+    if (RT_SUCCESS(rc) && pcFramesPlayed)
+        *pcFramesPlayed = cfPlayedTotal;
 
     if (RT_FAILURE(rc))
@@ -1823,18 +1802,14 @@
 static int drvAudioStreamCaptureNonInterleaved(PDRVAUDIO pThis, PPDMAUDIOSTREAM pStream, uint32_t *pcfCaptured)
 {
-    AssertPtrReturn(pThis,   VERR_INVALID_POINTER);
+    AssertPtrReturn(pThis, VERR_INVALID_POINTER);
     AssertPtrReturn(pStream, VERR_INVALID_POINTER);
-    /* pcfCaptured is optional. */
-
-    /* Sanity. */
+    AssertPtrNullReturn(pcfCaptured, VERR_INVALID_POINTER);
     Assert(pStream->enmDir == PDMAUDIODIR_IN);
     Assert(pStream->Host.Cfg.enmLayout == PDMAUDIOSTREAMLAYOUT_NON_INTERLEAVED);
 
-    int rc = VINF_SUCCESS;
-
-    uint32_t cfCapturedTotal = 0;
-
+    /*
+     * ...
+     */
     AssertPtr(pThis->pHostDrvAudio->pfnStreamGetReadable);
-
     uint32_t cbReadable = pThis->pHostDrvAudio->pfnStreamGetReadable(pThis->pHostDrvAudio, pStream->pvBackend);
     if (!cbReadable)
@@ -1848,20 +1823,25 @@
         cbReadable = cbFree;
 
+    /*
+     * ...
+     */
+    int      rc = VINF_SUCCESS;
+    uint32_t cfCapturedTotal = 0;
     while (cbReadable)
     {
+        uint8_t  abChunk[_4K];
         uint32_t cbCaptured;
         rc = pThis->pHostDrvAudio->pfnStreamCapture(pThis->pHostDrvAudio, pStream->pvBackend,
-                                                    pThis->pvScratchBuf, RT_MIN(cbReadable, (uint32_t)pThis->cbScratchBuf), &cbCaptured);
+                                                    abChunk, RT_MIN(cbReadable, (uint32_t)sizeof(abChunk)), &cbCaptured);
         if (RT_FAILURE(rc))
         {
             int rc2 = drvAudioStreamControlInternalBackend(pThis, pStream, PDMAUDIOSTREAMCMD_DISABLE);
             AssertRC(rc2);
-
             break;
         }
 
-        Assert(cbCaptured <= pThis->cbScratchBuf);
-        if (cbCaptured > pThis->cbScratchBuf) /* Paranoia. */
-            cbCaptured = (uint32_t)pThis->cbScratchBuf;
+        Assert(cbCaptured <= sizeof(abChunk));
+        if (cbCaptured > sizeof(abChunk)) /* Paranoia. */
+            cbCaptured = (uint32_t)sizeof(abChunk);
 
         if (!cbCaptured) /* Nothing captured? Take a shortcut. */
@@ -1871,5 +1851,5 @@
          * (first) processing (if needed), so always write the incoming data at offset 0. */
         uint32_t cfHstWritten = 0;
-        rc = AudioMixBufWriteAt(&pStream->Host.MixBuf, 0 /* offFrames */, pThis->pvScratchBuf, cbCaptured, &cfHstWritten);
+        rc = AudioMixBufWriteAt(&pStream->Host.MixBuf, 0 /* offFrames */, abChunk, cbCaptured, &cfHstWritten);
         if (   RT_FAILURE(rc)
             || !cfHstWritten)
@@ -1881,5 +1861,5 @@
 
         if (pThis->In.Cfg.Dbg.fEnabled)
-            AudioHlpFileWrite(pStream->In.Dbg.pFileCaptureNonInterleaved, pThis->pvScratchBuf, cbCaptured, 0 /* fFlags */);
+            AudioHlpFileWrite(pStream->In.Dbg.pFileCaptureNonInterleaved, abChunk, cbCaptured, 0 /* fFlags */);
 
         uint32_t cfHstMixed = 0;
@@ -1933,8 +1913,7 @@
     AssertPtr(pThis->pHostDrvAudio->pfnStreamGetReadable);
 
-    int rc = VINF_SUCCESS;
-
-    uint32_t cfCapturedTotal = 0;
-
+    /*
+     * ...
+     */
     /* Note: Raw means *audio frames*, not bytes! */
     uint32_t cfReadable = pThis->pHostDrvAudio->pfnStreamGetReadable(pThis->pHostDrvAudio, pStream->pvBackend);
@@ -1949,4 +1928,9 @@
         cfReadable = cfFree;
 
+    /*
+     * ...
+     */
+    int      rc              = VINF_SUCCESS;
+    uint32_t cfCapturedTotal = 0;
     while (cfReadable)
     {
@@ -1956,7 +1940,5 @@
         if (   RT_FAILURE(rc)
             || !cfWritable)
-        {
             break;
-        }
 
         uint32_t cfCaptured;
@@ -1967,5 +1949,4 @@
             int rc2 = drvAudioStreamControlInternalBackend(pThis, pStream, PDMAUDIOSTREAMCMD_DISABLE);
             AssertRC(rc2);
-
             break;
         }
@@ -1980,9 +1961,7 @@
     }
 
-    Log2Func(("[%s] %RU32 frames captured, rc=%Rrc\n", pStream->szName, cfCapturedTotal, rc));
-
     if (pcfCaptured)
         *pcfCaptured = cfCapturedTotal;
-
+    Log2Func(("[%s] %RU32 frames captured, rc=%Rrc\n", pStream->szName, cfCapturedTotal, rc));
     return rc;
 }
@@ -2010,5 +1989,5 @@
 
     /*
-     *
+     * ...
      */
     uint32_t cfCaptured = 0;
@@ -2031,5 +2010,4 @@
          * Do the actual capturing.
          */
-
         if (pThis->pHostDrvAudio->pfnStreamCaptureBegin)
             pThis->pHostDrvAudio->pfnStreamCaptureBegin(pThis->pHostDrvAudio, pStream->pvBackend);
@@ -2056,8 +2034,5 @@
         }
         else if (RT_UNLIKELY(RT_FAILURE(rc)))
-        {
             LogRel(("Audio: Capturing stream '%s' failed with %Rrc\n", pStream->szName, rc));
-        }
-
     } while (0);
 
@@ -3623,12 +3598,4 @@
 #endif
 
-    if (pThis->pvScratchBuf)
-    {
-        Assert(pThis->cbScratchBuf);
-
-        RTMemFree(pThis->pvScratchBuf);
-        pThis->pvScratchBuf = NULL;
-    }
-
     if (RTCritSectIsInitialized(&pThis->CritSect))
     {
@@ -3831,9 +3798,4 @@
     rc = RTCritSectInit(&pThis->CritSect);
     AssertRCReturn(rc, rc);
-
-    const size_t cbScratchBuf = _4K; /** @todo Make this configurable? */
-    pThis->pvScratchBuf = RTMemAlloc(cbScratchBuf);
-    AssertPtrReturn(pThis->pvScratchBuf, VERR_NO_MEMORY);
-    pThis->cbScratchBuf = cbScratchBuf;
 
     pThis->fTerminate                           = false;
