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