Bug 1656438 - Get rid of NUM_OF_FRAMES from MockCubeb. r=padenot

This allows us to configure the number of frames more dynamically, and update it
on the fly to simulate drift.

Differential Revision: https://phabricator.services.mozilla.com/D89763
This commit is contained in:
Andreas Pehrson 2020-09-15 14:42:16 +00:00
parent 482c467669
commit 37067e3c55
2 changed files with 16 additions and 10 deletions

View File

@ -14,7 +14,6 @@
using namespace std::chrono_literals; using namespace std::chrono_literals;
using namespace mozilla; using namespace mozilla;
const long NUM_OF_FRAMES = 512;
const uint32_t NUM_OF_CHANNELS = 2; const uint32_t NUM_OF_CHANNELS = 2;
#ifdef LOG #ifdef LOG
@ -223,13 +222,19 @@ class MockCubebStream {
void ThreadFunction() { void ThreadFunction() {
while (!mStreamStop) { while (!mStreamStop) {
// The drift factor affects the callback interval, while the callback
// always contains 10ms of audio frames.
const uint32_t audioIntervalMs = 10;
const uint32_t nrFrames = static_cast<uint32_t>(
(mHasOutput ? mOutputParams.rate : mInputParams.rate) *
audioIntervalMs / 1000);
if (mInputParams.rate) { if (mInputParams.rate) {
mAudioGenerator.GenerateInterleaved(mInputBuffer, NUM_OF_FRAMES); mAudioGenerator.GenerateInterleaved(mInputBuffer, nrFrames);
} }
cubeb_stream* stream = reinterpret_cast<cubeb_stream*>(this); cubeb_stream* stream = reinterpret_cast<cubeb_stream*>(this);
long outframes = mDataCallback( long outframes =
stream, mUserPtr, mInputParams.rate ? mInputBuffer : nullptr, mDataCallback(stream, mUserPtr, mHasInput ? mInputBuffer : nullptr,
mOutputParams.rate ? mOutputBuffer : nullptr, NUM_OF_FRAMES); mHasOutput ? mOutputBuffer : nullptr, nrFrames);
mAudioVerifier.AppendDataInterleaved(mOutputBuffer, outframes, mAudioVerifier.AppendDataInterleaved(mOutputBuffer, outframes,
NUM_OF_CHANNELS); NUM_OF_CHANNELS);
@ -238,7 +243,7 @@ class MockCubebStream {
mFramesProcessedEvent.Notify(outframes); mFramesProcessedEvent.Notify(outframes);
} }
if (outframes < NUM_OF_FRAMES) { if (outframes < nrFrames) {
mStateCallback(stream, mUserPtr, CUBEB_STATE_DRAINED); mStateCallback(stream, mUserPtr, CUBEB_STATE_DRAINED);
break; break;
} }
@ -272,8 +277,8 @@ class MockCubebStream {
// Signal to the audio thread that stream is stopped. // Signal to the audio thread that stream is stopped.
std::atomic_bool mStreamStop{true}; std::atomic_bool mStreamStop{true};
// The audio buffer used on data callback. // The audio buffer used on data callback.
AudioDataValue mOutputBuffer[NUM_OF_CHANNELS * NUM_OF_FRAMES] = {}; AudioDataValue mOutputBuffer[NUM_OF_CHANNELS * 1920] = {};
AudioDataValue mInputBuffer[NUM_OF_CHANNELS * NUM_OF_FRAMES] = {}; AudioDataValue mInputBuffer[NUM_OF_CHANNELS * 1920] = {};
// The audio callback // The audio callback
cubeb_data_callback mDataCallback = nullptr; cubeb_data_callback mDataCallback = nullptr;
// The stream state callback // The stream state callback
@ -580,7 +585,7 @@ static int cubeb_mock_stream_set_volume(cubeb_stream* stream, float volume) {
int cubeb_mock_get_min_latency(cubeb* context, cubeb_stream_params params, int cubeb_mock_get_min_latency(cubeb* context, cubeb_stream_params params,
uint32_t* latency_ms) { uint32_t* latency_ms) {
*latency_ms = NUM_OF_FRAMES; *latency_ms = 10;
return CUBEB_OK; return CUBEB_OK;
} }

View File

@ -339,6 +339,7 @@ TEST(TestAudioTrackGraph, SourceTrack)
sourceTrack->Destroy(); sourceTrack->Destroy();
}); });
uint32_t inputRate = stream->InputSampleRate();
uint32_t inputFrequency = stream->InputFrequency(); uint32_t inputFrequency = stream->InputFrequency();
uint64_t preSilenceSamples; uint64_t preSilenceSamples;
uint32_t estimatedFreq; uint32_t estimatedFreq;
@ -347,7 +348,7 @@ TEST(TestAudioTrackGraph, SourceTrack)
WaitFor(stream->OutputVerificationEvent()); WaitFor(stream->OutputVerificationEvent());
EXPECT_EQ(estimatedFreq, inputFrequency); EXPECT_EQ(estimatedFreq, inputFrequency);
EXPECT_GE(preSilenceSamples, static_cast<uint32_t>(NUM_OF_FRAMES)); EXPECT_GE(preSilenceSamples, inputRate / 100 /* 10 ms */);
// Waveform may start after the beginning. In this case, there is a gap // Waveform may start after the beginning. In this case, there is a gap
// at the beginning and the end which is counted as discontinuity. // at the beginning and the end which is counted as discontinuity.
EXPECT_GE(nrDiscontinuities, 0U); EXPECT_GE(nrDiscontinuities, 0U);