Revert of Cleanup NQE (patchset #5 id:80001 of https://codereview.chromium.org/127748...
[chromium-blink-merge.git] / media / base / audio_block_fifo_unittest.cc
blobb46a139ebb074a8726505dfeed00d8cf46f4bce1
1 // Copyright 2014 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.
5 #include "base/time/time.h"
6 #include "media/audio/audio_power_monitor.h"
7 #include "media/base/audio_block_fifo.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace media {
12 class AudioBlockFifoTest : public testing::Test {
13 public:
14 AudioBlockFifoTest() {}
15 ~AudioBlockFifoTest() override {}
17 void PushAndVerify(AudioBlockFifo* fifo, int frames_to_push,
18 int channels, int block_frames, int max_frames) {
19 for (int filled_frames = max_frames - fifo->GetUnfilledFrames();
20 filled_frames + frames_to_push <= max_frames;) {
21 Push(fifo, frames_to_push, channels);
22 filled_frames += frames_to_push;
23 EXPECT_EQ(max_frames - filled_frames, fifo->GetUnfilledFrames());
24 EXPECT_EQ(static_cast<int>(filled_frames / block_frames),
25 fifo->available_blocks());
29 void Push(AudioBlockFifo* fifo, int frames_to_push, int channels) {
30 DCHECK_LE(frames_to_push, fifo->GetUnfilledFrames());
31 const int bytes_per_sample = 2;
32 const int data_byte_size = bytes_per_sample * channels * frames_to_push;
33 scoped_ptr<uint8[]> data(new uint8[data_byte_size]);
34 memset(data.get(), 1, data_byte_size);
35 fifo->Push(data.get(), frames_to_push, bytes_per_sample);
38 void ConsumeAndVerify(AudioBlockFifo* fifo, int expected_unfilled_frames,
39 int expected_available_blocks) {
40 const AudioBus* bus = fifo->Consume();
41 EXPECT_EQ(fifo->GetUnfilledFrames(), expected_unfilled_frames);
42 EXPECT_EQ(fifo->available_blocks(), expected_available_blocks);
44 // Verify the audio data is not 0.
45 for (int i = 0; i < bus->channels(); ++i) {
46 EXPECT_GT(bus->channel(i)[0], 0.0f);
47 EXPECT_GT(bus->channel(i)[bus->frames() - 1], 0.0f);
51 private:
52 DISALLOW_COPY_AND_ASSIGN(AudioBlockFifoTest);
55 // Verify that construction works as intended.
56 TEST_F(AudioBlockFifoTest, Construct) {
57 const int channels = 6;
58 const int frames = 128;
59 const int blocks = 4;
60 AudioBlockFifo fifo(channels, frames, blocks);
61 EXPECT_EQ(0, fifo.available_blocks());
62 EXPECT_EQ(frames * blocks, fifo.GetUnfilledFrames());
65 // Pushes audio bus objects to/from a FIFO up to different degrees.
66 TEST_F(AudioBlockFifoTest, Push) {
67 const int channels = 2;
68 const int frames = 128;
69 const int blocks = 2;
70 AudioBlockFifo fifo(channels, frames, blocks);
72 // Push frames / 2 of data until FIFO is full.
73 PushAndVerify(&fifo, frames / 2, channels, frames, frames * blocks);
74 fifo.Clear();
76 // Push frames of data until FIFO is full.
77 PushAndVerify(&fifo, frames, channels, frames, frames * blocks);
78 fifo.Clear();
80 // Push 1.5 * frames of data.
81 PushAndVerify(&fifo, frames * 1.5, channels, frames, frames * blocks);
82 fifo.Clear();
85 // Perform a sequence of Push/Consume calls to different degrees, and verify
86 // things are correct.
87 TEST_F(AudioBlockFifoTest, PushAndConsume) {
88 const int channels = 2;
89 const int frames = 441;
90 const int blocks = 4;
91 AudioBlockFifo fifo(channels, frames, blocks);
92 PushAndVerify(&fifo, frames, channels, frames, frames * blocks);
93 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0);
94 EXPECT_TRUE(fifo.available_blocks() == blocks);
96 // Consume 1 block of data.
97 const AudioBus* bus = fifo.Consume();
98 EXPECT_TRUE(channels == bus->channels());
99 EXPECT_TRUE(frames == bus->frames());
100 EXPECT_TRUE(fifo.available_blocks() == (blocks - 1));
101 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames);
103 // Fill it up again.
104 PushAndVerify(&fifo, frames, channels, frames, frames * blocks);
105 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0);
106 EXPECT_TRUE(fifo.available_blocks() == blocks);
108 // Consume all blocks of data.
109 for (int i = 1; i <= blocks; ++i) {
110 const AudioBus* bus = fifo.Consume();
111 EXPECT_TRUE(channels == bus->channels());
112 EXPECT_TRUE(frames == bus->frames());
113 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames * i);
114 EXPECT_TRUE(fifo.available_blocks() == (blocks - i));
116 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames * blocks);
117 EXPECT_TRUE(fifo.available_blocks() == 0);
119 fifo.Clear();
120 int new_push_frames = 128;
121 // Change the input frame and try to fill up the FIFO.
122 PushAndVerify(&fifo, new_push_frames, channels, frames,
123 frames * blocks);
124 EXPECT_TRUE(fifo.GetUnfilledFrames() != 0);
125 EXPECT_TRUE(fifo.available_blocks() == blocks -1);
127 // Consume all the existing filled blocks of data.
128 while (fifo.available_blocks()) {
129 const AudioBus* bus = fifo.Consume();
130 EXPECT_TRUE(channels == bus->channels());
131 EXPECT_TRUE(frames == bus->frames());
134 // Since one block of FIFO has not been completely filled up, there should
135 // be remaining frames.
136 const int number_of_push =
137 static_cast<int>(frames * blocks / new_push_frames);
138 const int remain_frames = frames * blocks - fifo.GetUnfilledFrames();
139 EXPECT_EQ(number_of_push * new_push_frames - frames * (blocks - 1),
140 remain_frames);
142 // Completely fill up the buffer again.
143 new_push_frames = frames * blocks - remain_frames;
144 PushAndVerify(&fifo, new_push_frames, channels, frames,
145 frames * blocks);
146 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0);
147 EXPECT_TRUE(fifo.available_blocks() == blocks);
150 // Perform a sequence of Push/Consume calls to a 1 block FIFO.
151 TEST_F(AudioBlockFifoTest, PushAndConsumeOneBlockFifo) {
152 static const int channels = 2;
153 static const int frames = 441;
154 static const int blocks = 1;
155 AudioBlockFifo fifo(channels, frames, blocks);
156 PushAndVerify(&fifo, frames, channels, frames, frames * blocks);
157 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0);
158 EXPECT_TRUE(fifo.available_blocks() == blocks);
160 // Consume 1 block of data.
161 const AudioBus* bus = fifo.Consume();
162 EXPECT_TRUE(channels == bus->channels());
163 EXPECT_TRUE(frames == bus->frames());
164 EXPECT_TRUE(fifo.available_blocks() == 0);
165 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames);
168 // Dynamically increase the capacity of FIFO and verify buffers are correct.
169 TEST_F(AudioBlockFifoTest, DynamicallyIncreaseCapacity) {
170 // Create a FIFO with default blocks of buffers.
171 const int channels = 2;
172 const int frames = 441;
173 const int default_blocks = 2;
174 AudioBlockFifo fifo(channels, frames, default_blocks);
175 Push(&fifo, frames, channels);
176 int expected_unfilled_frames = frames;
177 int expected_available_blocks = 1;
178 EXPECT_EQ(expected_unfilled_frames, fifo.GetUnfilledFrames());
179 EXPECT_EQ(expected_available_blocks, fifo.available_blocks());
181 // Increase the capacity dynamically for the first time.
182 const int new_blocks_1 = 3;
183 fifo.IncreaseCapacity(new_blocks_1);
184 expected_unfilled_frames += new_blocks_1 * frames;
185 EXPECT_EQ(fifo.GetUnfilledFrames(), expected_unfilled_frames);
186 EXPECT_EQ(fifo.available_blocks(), expected_available_blocks);
188 // Verify the previous buffer is not affected by the dynamic capacity
189 // increment.
190 expected_unfilled_frames += frames;
191 expected_available_blocks -= 1;
192 ConsumeAndVerify(&fifo, expected_unfilled_frames, expected_available_blocks);
194 // Fill another |new_blocks_1 + 0.5| blocks of data to the FIFO.
195 const int frames_to_push = static_cast<int>((new_blocks_1 + 0.5) * frames);
196 int max_frames = frames * (default_blocks + new_blocks_1);
197 Push(&fifo, frames_to_push, channels);
198 expected_unfilled_frames = max_frames - frames_to_push;
199 expected_available_blocks = new_blocks_1;
200 EXPECT_EQ(fifo.GetUnfilledFrames(), expected_unfilled_frames);
201 EXPECT_EQ(fifo.available_blocks(), expected_available_blocks);
203 // Increase the capacity dynamically for the second time.
204 const int new_blocks_2 = 2;
205 fifo.IncreaseCapacity(new_blocks_2);
206 max_frames += new_blocks_2 * frames;
207 expected_unfilled_frames += new_blocks_2 * frames;
208 EXPECT_EQ(fifo.GetUnfilledFrames(), expected_unfilled_frames);
209 EXPECT_EQ(fifo.available_blocks(), expected_available_blocks);
211 // Verify the previous buffers are not affected by the dynamic capacity
212 // increment.
213 while (fifo.available_blocks()) {
214 expected_unfilled_frames += frames;
215 expected_available_blocks -= 1;
216 ConsumeAndVerify(&fifo, expected_unfilled_frames,
217 expected_available_blocks);
220 // Fill up one block of buffer and consume it, FIFO should then be empty.
221 const int available_frames = max_frames - expected_unfilled_frames;
222 Push(&fifo, frames - available_frames, channels);
223 ConsumeAndVerify(&fifo, max_frames, 0);
226 } // namespace media