OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" |
| 7 #include "base/stringprintf.h" |
| 8 #include "media/base/audio_pull_fifo.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace media { |
| 12 |
| 13 // Block diagram of a possible real-world usage: |
| 14 // |
| 15 // | Producer | ----> | AudioPullFifo | ----> | Consumer | |
| 16 // push pull |
| 17 // 2048 ----> (2048) ----> ~512 |
| 18 |
| 19 // Number of channels in each audio bus. |
| 20 static int kChannels = 2; |
| 21 |
| 22 // Max number of audio framed the FIFO can contain. |
| 23 static const int kMaxFramesInFifo = 2048; |
| 24 |
| 25 class AudioPullFifoTest |
| 26 : public testing::TestWithParam<int> { |
| 27 public: |
| 28 AudioPullFifoTest() |
| 29 : pull_fifo_(kChannels, kMaxFramesInFifo, base::Bind( |
| 30 &AudioPullFifoTest::ProvideInput, base::Unretained(this))), |
| 31 audio_bus_(AudioBus::Create(kChannels, kMaxFramesInFifo)), |
| 32 fill_value_(0) {} |
| 33 virtual ~AudioPullFifoTest() {} |
| 34 |
| 35 void VerifyValue(const float data[], int size, float start_value) { |
| 36 float value = start_value; |
| 37 for (int i = 0; i < size; ++i) { |
| 38 ASSERT_FLOAT_EQ(value++, data[i]) << "i=" << i; |
| 39 } |
| 40 } |
| 41 |
| 42 // Consume data using different sizes, acquire audio frames from the FIFO |
| 43 // and verify that the retrieved values matches the values written by the |
| 44 // producer. |
| 45 void ConsumeTest(int frames_to_consume) { |
| 46 int start_value = 0; |
| 47 SCOPED_TRACE(base::StringPrintf("Checking frames_to_consume %d", |
| 48 frames_to_consume)); |
| 49 pull_fifo_.Consume(audio_bus_.get(), frames_to_consume); |
| 50 for (int j = 0; j < kChannels; ++j) { |
| 51 VerifyValue(audio_bus_->channel(j), frames_to_consume, start_value); |
| 52 } |
| 53 start_value += frames_to_consume; |
| 54 } |
| 55 |
| 56 // AudioPullFifo::ReadCB implementation where we increase a value for each |
| 57 // audio frame that we provide. Note that all channels are given the same |
| 58 // value to simplify the verification. |
| 59 virtual void ProvideInput(AudioBus* audio_bus) { |
| 60 EXPECT_EQ(audio_bus->channels(), audio_bus_->channels()); |
| 61 EXPECT_EQ(audio_bus->frames(), kMaxFramesInFifo); |
| 62 for (int i = 0; i < audio_bus->frames(); ++i) { |
| 63 for (int j = 0; j < audio_bus->channels(); ++j) { |
| 64 // Store same value in all channels. |
| 65 audio_bus->channel(j)[i] = fill_value_; |
| 66 } |
| 67 fill_value_++; |
| 68 } |
| 69 } |
| 70 |
| 71 protected: |
| 72 AudioPullFifo pull_fifo_; |
| 73 scoped_ptr<AudioBus> audio_bus_; |
| 74 int fill_value_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(AudioPullFifoTest); |
| 77 }; |
| 78 |
| 79 TEST_P(AudioPullFifoTest, Consume) { |
| 80 ConsumeTest(GetParam()); |
| 81 } |
| 82 |
| 83 // Test common |frames_to_consume| values which will be used as input |
| 84 // parameter to AudioPullFifo::Consume() when the consumer asks for data. |
| 85 INSTANTIATE_TEST_CASE_P( |
| 86 AudioPullFifoTest, AudioPullFifoTest, |
| 87 testing::Values(544, 512, 512, 512, 512, 2048, 544, 441, 440, 433, 500)); |
| 88 |
| 89 } // namespace media |
OLD | NEW |