static DECLCALLBACK(int) drvHostALSAAudioCaptureIn(PPDMIHOSTAUDIO pInterface, PPDMAUDIOHSTSTRMIN pHstStrmIn, uint32_t *pcSamplesCaptured) { NOREF(pInterface); AssertPtrReturn(pHstStrmIn, VERR_INVALID_POINTER); PALSAAUDIOSTREAMIN pThisStrmIn = (PALSAAUDIOSTREAMIN)pHstStrmIn; snd_pcm_sframes_t cAvail; int rc = drvHostALSAAudioGetAvail(pThisStrmIn->phPCM, &cAvail); if (RT_FAILURE(rc)) { LogFunc(("Error getting number of captured frames, rc=%Rrc\n", rc)); return rc; } if (!cAvail) /* No data yet? */ { snd_pcm_state_t state = snd_pcm_state(pThisStrmIn->phPCM); switch (state) { case SND_PCM_STATE_PREPARED: cAvail = AudioMixBufFree(&pHstStrmIn->MixBuf); break; case SND_PCM_STATE_SUSPENDED: { rc = drvHostALSAAudioResume(pThisStrmIn->phPCM); if (RT_FAILURE(rc)) break; LogFlow(("Resuming suspended input stream\n")); break; } default: LogFlow(("No frames available, state=%d\n", state)); break; } if (!cAvail) { if (pcSamplesCaptured) *pcSamplesCaptured = 0; return VINF_SUCCESS; } } /* * Check how much we can read from the capture device without overflowing * the mixer buffer. */ Assert(cAvail); size_t cbMixFree = AudioMixBufFreeBytes(&pHstStrmIn->MixBuf); size_t cbToRead = RT_MIN((size_t)AUDIOMIXBUF_S2B(&pHstStrmIn->MixBuf, cAvail), cbMixFree); LogFlowFunc(("cbToRead=%zu, cAvail=%RI32\n", cbToRead, cAvail)); uint32_t cWrittenTotal = 0; snd_pcm_uframes_t cToRead; snd_pcm_sframes_t cRead; while ( cbToRead && RT_SUCCESS(rc)) { cToRead = RT_MIN(AUDIOMIXBUF_B2S(&pHstStrmIn->MixBuf, cbToRead), AUDIOMIXBUF_B2S(&pHstStrmIn->MixBuf, pThisStrmIn->cbBuf)); AssertBreakStmt(cToRead, rc = VERR_NO_DATA); cRead = snd_pcm_readi(pThisStrmIn->phPCM, pThisStrmIn->pvBuf, cToRead); if (cRead <= 0) { switch (cRead) { case 0: { LogFunc(("No input frames available\n")); rc = VERR_ACCESS_DENIED; break; } case -EAGAIN: /* * Don't set error here because EAGAIN means there are no further frames * available at the moment, try later. As we might have read some frames * already these need to be processed instead. */ cbToRead = 0; break; case -EPIPE: { rc = drvHostALSAAudioRecover(pThisStrmIn->phPCM); if (RT_FAILURE(rc)) break; LogFlowFunc(("Recovered from capturing\n")); continue; } default: LogFunc(("Failed to read input frames: %s\n", snd_strerror(cRead))); rc = VERR_GENERAL_FAILURE; /** @todo Fudge! */ break; } } else { uint32_t cWritten; rc = AudioMixBufWriteCirc(&pHstStrmIn->MixBuf, pThisStrmIn->pvBuf, AUDIOMIXBUF_S2B(&pHstStrmIn->MixBuf, cRead), &cWritten); if (RT_FAILURE(rc)) break; /* * We should not run into a full mixer buffer or we loose samples and * run into an endless loop if ALSA keeps producing samples ("null" * capture device for example). */ AssertLogRelMsgBreakStmt(cWritten > 0, ("Mixer buffer shouldn't be full at this point!\n"), rc = VERR_INTERNAL_ERROR); uint32_t cbWritten = AUDIOMIXBUF_S2B(&pHstStrmIn->MixBuf, cWritten); Assert(cbToRead >= cbWritten); cbToRead -= cbWritten; cWrittenTotal += cWritten; } } if (RT_SUCCESS(rc)) { uint32_t cProcessed = 0; if (cWrittenTotal) rc = AudioMixBufMixToParent(&pHstStrmIn->MixBuf, cWrittenTotal, &cProcessed); if (pcSamplesCaptured) *pcSamplesCaptured = cWrittenTotal; LogFlowFunc(("cWrittenTotal=%RU32 (%RU32 processed), rc=%Rrc\n", cWrittenTotal, cProcessed, rc)); } LogFlowFuncLeaveRC(rc); return rc; }
/* Test volume control. */ static int tstVolume(RTTEST hTest) { unsigned i; uint32_t cBufSize = 256; PDMPCMPROPS props; RTTestSubF(hTest, "Volume control"); /* Same for parent/child. */ PDMAUDIOSTREAMCFG cfg = { 44100, /* Hz */ 2 /* Channels */, AUD_FMT_S16 /* Format */, PDMAUDIOENDIANNESS_LITTLE /* ENDIANNESS */ }; int rc = DrvAudioStreamCfgToProps(&cfg, &props); AssertRC(rc); PDMAUDIOVOLUME vol = { false, 0, 0 }; /* Not muted. */ PDMAUDIOMIXBUF parent; RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &props, cBufSize)); PDMAUDIOMIXBUF child; RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child, "Child", &props, cBufSize)); RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child, &parent)); /* A few 16-bit signed samples. */ int16_t samples[16] = { INT16_MIN, INT16_MIN + 1, -128, -64, -4, -1, 0, 1, 2, 255, 256, INT16_MAX / 2, INT16_MAX - 2, INT16_MAX - 1, INT16_MAX, 0 }; /* * Writing + mixing from child -> parent. */ uint32_t cbBuf = 256; char achBuf[256]; uint32_t read, written, mixed; uint32_t cChildFree = cBufSize; uint32_t cChildMixed = 0; uint32_t cSamplesChild = 8; uint32_t cSamplesParent = cSamplesChild; uint32_t cSamplesRead; int16_t *pSrc16; int16_t *pDst16; /**** Volume control test ****/ RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Volume control test %uHz %uch \n", cfg.uHz, cfg.cChannels); /* 1) Full volume/0dB attenuation (255). */ vol.uLeft = vol.uRight = 255; AudioMixBufSetVolume(&child, &vol); RTTESTI_CHECK_RC_OK(AudioMixBufWriteAt(&child, 0, &samples, sizeof(samples), &written)); RTTESTI_CHECK_MSG(written == cSamplesChild, ("Child: Expected %RU32 written samples, got %RU32\n", cSamplesChild, written)); RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, written, &mixed)); cSamplesRead = 0; for (;;) { RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufReadCirc(&parent, achBuf, cbBuf, &read)); if (!read) break; cSamplesRead += read; AudioMixBufFinish(&parent, read); } RTTESTI_CHECK_MSG(cSamplesRead == cSamplesParent, ("Parent: Expected %RU32 mixed samples, got %RU32\n", cSamplesParent, cSamplesRead)); /* Check that at 0dB the samples came out unharmed. */ pSrc16 = &samples[0]; pDst16 = (int16_t *)achBuf; for (i = 0; i < cSamplesParent * 2 /* stereo */; ++i) { RTTESTI_CHECK_MSG(*pSrc16 == *pDst16, ("index %u: Dst=%d, Src=%d\n", i, *pDst16, *pSrc16)); ++pSrc16; ++pDst16; } AudioMixBufReset(&child); /* 2) Half volume/-6dB attenuation (16 steps down). */ vol.uLeft = vol.uRight = 255 - 16; AudioMixBufSetVolume(&child, &vol); RTTESTI_CHECK_RC_OK(AudioMixBufWriteAt(&child, 0, &samples, sizeof(samples), &written)); RTTESTI_CHECK_MSG(written == cSamplesChild, ("Child: Expected %RU32 written samples, got %RU32\n", cSamplesChild, written)); RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, written, &mixed)); cSamplesRead = 0; for (;;) { RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufReadCirc(&parent, achBuf, cbBuf, &read)); if (!read) break; cSamplesRead += read; AudioMixBufFinish(&parent, read); } RTTESTI_CHECK_MSG(cSamplesRead == cSamplesParent, ("Parent: Expected %RU32 mixed samples, got %RU32\n", cSamplesParent, cSamplesRead)); /* Check that at -6dB the sample values are halved. */ pSrc16 = &samples[0]; pDst16 = (int16_t *)achBuf; for (i = 0; i < cSamplesParent * 2 /* stereo */; ++i) { /* Watch out! For negative values, x >> 1 is not the same as x / 2. */ RTTESTI_CHECK_MSG(*pSrc16 >> 1 == *pDst16, ("index %u: Dst=%d, Src=%d\n", i, *pDst16, *pSrc16)); ++pSrc16; ++pDst16; } AudioMixBufDestroy(&parent); AudioMixBufDestroy(&child); return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS; }
/* Test 8-bit sample conversion (8-bit -> internal -> 8-bit). */ static int tstConversion8(RTTEST hTest) { unsigned i; uint32_t cBufSize = 256; PDMPCMPROPS props; RTTestSubF(hTest, "Sample conversion"); PDMAUDIOSTREAMCFG cfg_p = { 44100, /* Hz */ 1 /* Channels */, AUD_FMT_U8 /* Format */, PDMAUDIOENDIANNESS_LITTLE /* ENDIANNESS */ }; int rc = DrvAudioStreamCfgToProps(&cfg_p, &props); AssertRC(rc); PDMAUDIOMIXBUF parent; RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &props, cBufSize)); /* Child uses half the sample rate; that ensures the mixing engine can't * take shortcuts and performs conversion. Because conversion to double * the sample rate effectively inserts one additional sample between every * two source samples, N source samples will be converted to N * 2 - 1 * samples. However, the last source sample will be saved for later * interpolation and not immediately output. */ PDMAUDIOSTREAMCFG cfg_c = /* Upmixing to parent */ { 22050, /* Hz */ 1 /* Channels */, AUD_FMT_U8 /* Format */, PDMAUDIOENDIANNESS_LITTLE /* ENDIANNESS */ }; rc = DrvAudioStreamCfgToProps(&cfg_c, &props); AssertRC(rc); PDMAUDIOMIXBUF child; RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child, "Child", &props, cBufSize)); RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child, &parent)); /* 8-bit unsigned samples. Often used with SB16 device. */ uint8_t samples[16] = { 0xAA, 0xBB, 0, 1, 43, 125, 126, 127, 128, 129, 130, 131, 132, UINT8_MAX - 1, UINT8_MAX, 0 }; /* * Writing + mixing from child -> parent, sequential. */ uint32_t cbBuf = 256; char achBuf[256]; uint32_t read, written, mixed, temp; uint32_t cChildFree = cBufSize; uint32_t cChildMixed = 0; uint32_t cSamplesChild = 16; uint32_t cSamplesParent = cSamplesChild * 2 - 2; uint32_t cSamplesRead = 0; /**** 8-bit unsigned samples ****/ RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Conversion test %uHz %uch 8-bit\n", cfg_c.uHz, cfg_c.cChannels); RTTESTI_CHECK_RC_OK(AudioMixBufWriteAt(&child, 0, &samples, sizeof(samples), &written)); RTTESTI_CHECK_MSG(written == cSamplesChild, ("Child: Expected %RU32 written samples, got %RU32\n", cSamplesChild, written)); RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, written, &mixed)); temp = AudioMixBufProcessed(&parent); RTTESTI_CHECK_MSG(AudioMixBufMixed(&child) == temp, ("Child: Expected %RU32 mixed samples, got %RU32\n", AudioMixBufMixed(&child), temp)); RTTESTI_CHECK(AudioMixBufProcessed(&parent) == AudioMixBufMixed(&child)); for (;;) { RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufReadCirc(&parent, achBuf, cbBuf, &read)); if (!read) break; cSamplesRead += read; AudioMixBufFinish(&parent, read); } RTTESTI_CHECK_MSG(cSamplesRead == cSamplesParent, ("Parent: Expected %RU32 mixed samples, got %RU32\n", cSamplesParent, cSamplesRead)); /* Check that the samples came out unharmed. Every other sample is interpolated and we ignore it. */ /* NB: This also checks that the default volume setting is 0dB attenuation. */ uint8_t *pSrc8 = &samples[0]; uint8_t *pDst8 = (uint8_t *)achBuf; for (i = 0; i < cSamplesChild - 1; ++i) { RTTESTI_CHECK_MSG(*pSrc8 == *pDst8, ("index %u: Dst=%d, Src=%d\n", i, *pDst8, *pSrc8)); pSrc8 += 1; pDst8 += 2; } RTTESTI_CHECK(AudioMixBufProcessed(&parent) == 0); RTTESTI_CHECK(AudioMixBufMixed(&child) == 0); AudioMixBufDestroy(&parent); AudioMixBufDestroy(&child); return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS; }
/* Test 16-bit sample conversion (16-bit -> internal -> 16-bit). */ static int tstConversion16(RTTEST hTest) { unsigned i; uint32_t cBufSize = 256; PDMPCMPROPS props; RTTestSubF(hTest, "Sample conversion 16-bit"); PDMAUDIOSTREAMCFG cfg_p = { 44100, /* Hz */ 1 /* Channels */, AUD_FMT_S16 /* Format */, PDMAUDIOENDIANNESS_LITTLE /* ENDIANNESS */ }; int rc = DrvAudioStreamCfgToProps(&cfg_p, &props); AssertRC(rc); PDMAUDIOMIXBUF parent; RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &props, cBufSize)); PDMAUDIOSTREAMCFG cfg_c = /* Upmixing to parent */ { 22050, /* Hz */ 1 /* Channels */, AUD_FMT_S16 /* Format */, PDMAUDIOENDIANNESS_LITTLE /* ENDIANNESS */ }; rc = DrvAudioStreamCfgToProps(&cfg_c, &props); AssertRC(rc); PDMAUDIOMIXBUF child; RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child, "Child", &props, cBufSize)); RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child, &parent)); /* 16-bit signed. More or less exclusively used as output, and usually as input, too. */ int16_t samples[16] = { 0xAA, 0xBB, INT16_MIN, INT16_MIN + 1, INT16_MIN / 2, -3, -2, -1, 0, 1, 2, 3, INT16_MAX / 2, INT16_MAX - 1, INT16_MAX, 0 }; /* * Writing + mixing from child -> parent, sequential. */ uint32_t cbBuf = 256; char achBuf[256]; uint32_t read, written, mixed, temp; uint32_t cChildFree = cBufSize; uint32_t cChildMixed = 0; uint32_t cSamplesChild = 16; uint32_t cSamplesParent = cSamplesChild * 2 - 2; uint32_t cSamplesRead = 0; /**** 16-bit signed samples ****/ RTTestPrintf(hTest, RTTESTLVL_DEBUG, "Conversion test %uHz %uch 16-bit\n", cfg_c.uHz, cfg_c.cChannels); RTTESTI_CHECK_RC_OK(AudioMixBufWriteAt(&child, 0, &samples, sizeof(samples), &written)); RTTESTI_CHECK_MSG(written == cSamplesChild, ("Child: Expected %RU32 written samples, got %RU32\n", cSamplesChild, written)); RTTESTI_CHECK_RC_OK(AudioMixBufMixToParent(&child, written, &mixed)); temp = AudioMixBufProcessed(&parent); RTTESTI_CHECK_MSG(AudioMixBufMixed(&child) == temp, ("Child: Expected %RU32 mixed samples, got %RU32\n", AudioMixBufMixed(&child), temp)); RTTESTI_CHECK(AudioMixBufProcessed(&parent) == AudioMixBufMixed(&child)); for (;;) { RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufReadCirc(&parent, achBuf, cbBuf, &read)); if (!read) break; cSamplesRead += read; AudioMixBufFinish(&parent, read); } RTTESTI_CHECK_MSG(cSamplesRead == cSamplesParent, ("Parent: Expected %RU32 mixed samples, got %RU32\n", cSamplesParent, cSamplesRead)); /* Check that the samples came out unharmed. Every other sample is interpolated and we ignore it. */ /* NB: This also checks that the default volume setting is 0dB attenuation. */ int16_t *pSrc16 = &samples[0]; int16_t *pDst16 = (int16_t *)achBuf; for (i = 0; i < cSamplesChild - 1; ++i) { RTTESTI_CHECK_MSG(*pSrc16 == *pDst16, ("index %u: Dst=%d, Src=%d\n", i, *pDst16, *pSrc16)); pSrc16 += 1; pDst16 += 2; } RTTESTI_CHECK(AudioMixBufProcessed(&parent) == 0); RTTESTI_CHECK(AudioMixBufMixed(&child) == 0); AudioMixBufDestroy(&parent); AudioMixBufDestroy(&child); return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS; }
static int tstParentChild(RTTEST hTest) { RTTestSubF(hTest, "2 Children -> Parent"); uint32_t cBufSize = _1K; PDMAUDIOSTREAMCFG cfg_p = { 44100, /* Hz */ 2 /* Channels */, AUD_FMT_S16 /* Format */, PDMAUDIOENDIANNESS_LITTLE /* ENDIANNESS */ }; PDMPCMPROPS props; int rc = DrvAudioStreamCfgToProps(&cfg_p, &props); AssertRC(rc); PDMAUDIOMIXBUF parent; RTTESTI_CHECK_RC_OK(AudioMixBufInit(&parent, "Parent", &props, cBufSize)); PDMAUDIOSTREAMCFG cfg_c1 = /* Upmixing to parent */ { 22100, /* Hz */ 2 /* Channels */, AUD_FMT_S16 /* Format */, PDMAUDIOENDIANNESS_LITTLE /* ENDIANNESS */ }; rc = DrvAudioStreamCfgToProps(&cfg_c1, &props); AssertRC(rc); PDMAUDIOMIXBUF child1; RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child1, "Child1", &props, cBufSize)); RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child1, &parent)); PDMAUDIOSTREAMCFG cfg_c2 = /* Downmixing to parent */ { 48000, /* Hz */ 2 /* Channels */, AUD_FMT_S16 /* Format */, PDMAUDIOENDIANNESS_LITTLE /* ENDIANNESS */ }; rc = DrvAudioStreamCfgToProps(&cfg_c2, &props); AssertRC(rc); PDMAUDIOMIXBUF child2; RTTESTI_CHECK_RC_OK(AudioMixBufInit(&child2, "Child2", &props, cBufSize)); RTTESTI_CHECK_RC_OK(AudioMixBufLinkTo(&child2, &parent)); /* * Writing + mixing from child/children -> parent, sequential. */ uint32_t cbBuf = _1K; char pvBuf[_1K]; int16_t samples[32] = { 0xAA, 0xBB }; uint32_t read , written, mixed, temp; uint32_t cChild1Free = cBufSize; uint32_t cChild1Mixed = 0; uint32_t cSamplesParent1 = 16; uint32_t cSamplesChild1 = 16; uint32_t cChild2Free = cBufSize; uint32_t cChild2Mixed = 0; uint32_t cSamplesParent2 = 16; uint32_t cSamplesChild2 = 16; uint32_t t = RTRandU32() % 64; for (uint32_t i = 0; i < t; i++) { RTTestPrintf(hTest, RTTESTLVL_DEBUG, "i=%RU32\n", i); RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufWriteAt(&child1, 0, &samples, sizeof(samples), &written)); RTTESTI_CHECK_MSG_BREAK(written == cSamplesChild1, ("Child1: Expected %RU32 written samples, got %RU32\n", cSamplesChild1, written)); RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufMixToParent(&child1, written, &mixed)); temp = AudioMixBufProcessed(&parent) - AudioMixBufMixed(&child2); RTTESTI_CHECK_MSG_BREAK(AudioMixBufMixed(&child1) == temp, ("Child1: Expected %RU32 mixed samples, got %RU32\n", AudioMixBufMixed(&child1), temp)); RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufWriteAt(&child2, 0, &samples, sizeof(samples), &written)); RTTESTI_CHECK_MSG_BREAK(written == cSamplesChild2, ("Child2: Expected %RU32 written samples, got %RU32\n", cSamplesChild2, written)); RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufMixToParent(&child2, written, &mixed)); temp = AudioMixBufProcessed(&parent) - AudioMixBufMixed(&child1); RTTESTI_CHECK_MSG_BREAK(AudioMixBufMixed(&child2) == temp, ("Child2: Expected %RU32 mixed samples, got %RU32\n", AudioMixBufMixed(&child2), temp)); } RTTESTI_CHECK(AudioMixBufProcessed(&parent) == AudioMixBufMixed(&child1) + AudioMixBufMixed(&child2)); for (;;) { RTTESTI_CHECK_RC_OK_BREAK(AudioMixBufReadCirc(&parent, pvBuf, cbBuf, &read)); if (!read) break; AudioMixBufFinish(&parent, read); } RTTESTI_CHECK(AudioMixBufProcessed(&parent) == 0); RTTESTI_CHECK(AudioMixBufMixed(&child1) == 0); RTTESTI_CHECK(AudioMixBufMixed(&child2) == 0); AudioMixBufDestroy(&parent); AudioMixBufDestroy(&child1); AudioMixBufDestroy(&child2); return RTTestSubErrorCount(hTest) ? VERR_GENERAL_FAILURE : VINF_SUCCESS; }