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.
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"
14 // Block diagram of a possible real-world usage:
16 // | Producer | ----> | AudioPullFifo | ----> | Consumer |
18 // 2048 ----> (2048) ----> ~512
20 // Number of channels in each audio bus.
21 static int kChannels
= 2;
23 // Max number of audio framed the FIFO can contain.
24 static const int kMaxFramesInFifo
= 2048;
26 class AudioPullFifoTest
27 : public testing::TestWithParam
<int> {
30 : pull_fifo_(kChannels
, kMaxFramesInFifo
, base::Bind(
31 &AudioPullFifoTest::ProvideInput
, base::Unretained(this))),
32 audio_bus_(AudioBus::Create(kChannels
, kMaxFramesInFifo
)),
34 last_frame_delay_(-1) {}
35 virtual ~AudioPullFifoTest() {}
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
;
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
47 void ConsumeTest(int frames_to_consume
) {
49 SCOPED_TRACE(base::StringPrintf("Checking frames_to_consume %d",
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
);
55 start_value
+= frames_to_consume
;
56 EXPECT_LT(last_frame_delay_
, audio_bus_
->frames());
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
;
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_
;
78 AudioPullFifo pull_fifo_
;
79 scoped_ptr
<AudioBus
> audio_bus_
;
81 int last_frame_delay_
;
83 DISALLOW_COPY_AND_ASSIGN(AudioPullFifoTest
);
86 TEST_P(AudioPullFifoTest
, Consume
) {
87 ConsumeTest(GetParam());
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));