OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "base/time/time.h" |
| 6 #include "media/audio/audio_power_monitor.h" |
5 #include "media/base/audio_block_fifo.h" | 7 #include "media/base/audio_block_fifo.h" |
6 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
7 | 9 |
8 namespace media { | 10 namespace media { |
9 | 11 |
10 class AudioBlockFifoTest : public testing::Test { | 12 class AudioBlockFifoTest : public testing::Test { |
11 public: | 13 public: |
12 AudioBlockFifoTest() {} | 14 AudioBlockFifoTest() {} |
13 virtual ~AudioBlockFifoTest() {} | 15 virtual ~AudioBlockFifoTest() {} |
14 | 16 |
15 void PushAndVerify(AudioBlockFifo* fifo, int frames_to_push, | 17 void PushAndVerify(AudioBlockFifo* fifo, int frames_to_push, |
16 int channels, int block_frames, int max_frames) { | 18 int channels, int block_frames, int max_frames) { |
17 const int bytes_per_sample = 2; | |
18 const int data_byte_size = bytes_per_sample * channels * frames_to_push; | |
19 scoped_ptr<uint8[]> data(new uint8[data_byte_size]); | |
20 memset(data.get(), 0, data_byte_size); | |
21 | |
22 for (int filled_frames = max_frames - fifo->GetUnfilledFrames(); | 19 for (int filled_frames = max_frames - fifo->GetUnfilledFrames(); |
23 filled_frames + frames_to_push <= max_frames;) { | 20 filled_frames + frames_to_push <= max_frames;) { |
24 fifo->Push(data.get(), frames_to_push, bytes_per_sample); | 21 Push(fifo, frames_to_push, channels); |
25 filled_frames += frames_to_push; | 22 filled_frames += frames_to_push; |
26 EXPECT_EQ(max_frames - filled_frames, fifo->GetUnfilledFrames()); | 23 EXPECT_EQ(max_frames - filled_frames, fifo->GetUnfilledFrames()); |
27 EXPECT_EQ(static_cast<int>(filled_frames / block_frames), | 24 EXPECT_EQ(static_cast<int>(filled_frames / block_frames), |
28 fifo->available_blocks()); | 25 fifo->available_blocks()); |
29 } | 26 } |
30 } | 27 } |
31 | 28 |
| 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); |
| 36 } |
| 37 |
| 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); |
| 43 |
| 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); |
| 48 } |
| 49 } |
| 50 |
32 private: | 51 private: |
33 DISALLOW_COPY_AND_ASSIGN(AudioBlockFifoTest); | 52 DISALLOW_COPY_AND_ASSIGN(AudioBlockFifoTest); |
34 }; | 53 }; |
35 | 54 |
36 // Verify that construction works as intended. | 55 // Verify that construction works as intended. |
37 TEST_F(AudioBlockFifoTest, Construct) { | 56 TEST_F(AudioBlockFifoTest, Construct) { |
38 const int channels = 6; | 57 const int channels = 6; |
39 const int frames = 128; | 58 const int frames = 128; |
40 const int blocks = 4; | 59 const int blocks = 4; |
41 AudioBlockFifo fifo(channels, frames, blocks); | 60 AudioBlockFifo fifo(channels, frames, blocks); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 EXPECT_TRUE(fifo.available_blocks() == blocks); | 158 EXPECT_TRUE(fifo.available_blocks() == blocks); |
140 | 159 |
141 // Consume 1 block of data. | 160 // Consume 1 block of data. |
142 const AudioBus* bus = fifo.Consume(); | 161 const AudioBus* bus = fifo.Consume(); |
143 EXPECT_TRUE(channels == bus->channels()); | 162 EXPECT_TRUE(channels == bus->channels()); |
144 EXPECT_TRUE(frames == bus->frames()); | 163 EXPECT_TRUE(frames == bus->frames()); |
145 EXPECT_TRUE(fifo.available_blocks() == 0); | 164 EXPECT_TRUE(fifo.available_blocks() == 0); |
146 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames); | 165 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames); |
147 } | 166 } |
148 | 167 |
| 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()); |
| 180 |
| 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); |
| 187 |
| 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); |
| 193 |
| 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); |
| 202 |
| 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); |
| 210 |
| 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); |
| 218 } |
| 219 |
| 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); |
| 224 } |
| 225 |
149 } // namespace media | 226 } // namespace media |
OLD | NEW |