| Index: media/base/audio_pull_fifo_unittest.cc
|
| diff --git a/media/base/audio_pull_fifo_unittest.cc b/media/base/audio_pull_fifo_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..ac37ed222385857a23da4218c753e5ef7904baa2
|
| --- /dev/null
|
| +++ b/media/base/audio_pull_fifo_unittest.cc
|
| @@ -0,0 +1,107 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/bind_helpers.h"
|
| +#include "media/base/audio_pull_fifo.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace media {
|
| +
|
| +// Block diagram of a possible real-world usage:
|
| +//
|
| +// | Provider | ----> | AudioPullFifo | ----> | Consumer |
|
| +// push pull
|
| +// 2048 ----> (4096) ----> ~441 [#frames]
|
| +
|
| +//DaleCurtis 2012/09/07 13:48:38
|
| +// As noted in my other comments, it's possible the Provider could ask for a 1024
|
| +// buffer size and the hardware wants a 2048 buffer.
|
| +
|
| +// Number of channels in each audio bus.
|
| +static int kChannels = 2;
|
| +
|
| +// Max number of audio framed the FIFO can contain.
|
| +static const int kMaxFramesInFifo = 2048;
|
| +
|
| +// The provider will be asked to provide audio frames of this size by the
|
| +// consumer.
|
| +//static const int kReadFramesFromProvider = 2048;
|
| +
|
| +// Max number of frames the consumer can ask for.
|
| +static const int kMaxFramesToConsume = 496;
|
| +
|
| +// List of |frames_to_consume| which will be used as input parameter to
|
| +// AudioPullFifo::Consume() when the consumer asks for data.
|
| +// Sum(kConsumeFrames)=5733 and Mean(kConsumeFrames)=441, hence the provider
|
| +// will be called three times in this example and provide in total 6144 audio
|
| +// frames.
|
| +static const int kConsumeFrames[] =
|
| + {440, 441, 442, 440, 440, 441, 442, 440, 441, 442, 441, 441, 442};
|
| +
|
| +//DaleCurtis 2012/09/07 13:48:38
|
| +// SincResampler will actually look something like 544, 512, 512, 512, 512.
|
| +// So I'd increase the spread on these test values some more.
|
| +
|
| +class AudioPullFifoTest : public testing::Test {
|
| + public:
|
| + AudioPullFifoTest()
|
| + : audio_bus_(AudioBus::Create(kChannels, kMaxFramesToConsume)),
|
| + fill_value_(0) {}
|
| + ~AudioPullFifoTest() {}
|
| +
|
| + void VerifyValue(const float data[], int size, float start_value) {
|
| + float value = start_value;
|
| + for (int i = 0; i < size; ++i) {
|
| + ASSERT_FLOAT_EQ(value++, data[i]) << "i=" << i;
|
| + }
|
| + }
|
| +
|
| + // AudioPullFifo::ReadCB implementation where we increase a value for each
|
| + // audio frame that we provide. Note that all channels are given the same
|
| + // value to simplify the verification.
|
| + virtual void ProvideInput(AudioBus* audio_bus) {
|
| + EXPECT_EQ(audio_bus->channels(), audio_bus_->channels());
|
| + EXPECT_EQ(audio_bus->frames(), kMaxFramesInFifo);
|
| + for (int i = 0; i < audio_bus->frames(); ++i) {
|
| + for (int j = 0; j < audio_bus->channels(); ++j) {
|
| + // Store same value in all channels.
|
| + audio_bus->channel(j)[i] = fill_value_;
|
| + }
|
| + fill_value_++;
|
| + }
|
| + }
|
| +
|
| + protected:
|
| + scoped_ptr<AudioBus> audio_bus_;
|
| + int fill_value_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(AudioPullFifoTest);
|
| +};
|
| +
|
| +// Emulate a scenario where the consumer ask for data using the sizes in
|
| +// kConsumeFrames at each call. The provider is asked to provide input when
|
| +// the FIFO can't fulfill the request and kReadFramesFromProvider will be
|
| +// provided in each callback. The provider updates and stores a data value for
|
| +// each audio frame it provides which makes it possible to verify that the
|
| +// consumer reads out correct values.
|
| +TEST_F(AudioPullFifoTest, Consume) {
|
| + AudioPullFifo pull_fifo(kChannels, kMaxFramesInFifo, base::Bind(
|
| + &AudioPullFifoTest::ProvideInput, base::Unretained(this)));
|
| +
|
| + // Consume data using different sizes, acquire audio frames from the FIFO
|
| + // and verify that the retrieved values matches the values written by the
|
| + // provider.
|
| + int start_value = 0;
|
| + for (int i = 0; i < sizeof(kConsumeFrames) / sizeof(kConsumeFrames[0]); ++i) {
|
| + int frames_to_consume = kConsumeFrames[i];
|
| + pull_fifo.Consume(audio_bus_.get(), frames_to_consume);
|
| + for (int j = 0; j < kChannels; ++j) {
|
| + VerifyValue(audio_bus_->channel(j), frames_to_consume, start_value);
|
| + }
|
| + start_value += frames_to_consume;
|
| + }
|
| +}
|
| +
|
| +} // namespace media
|
|
|