| 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 <stdint.h> | 5 #include <stdint.h> |
| 6 #include <memory> | 6 #include <memory> |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 10 #include "media/audio/audio_power_monitor.h" | 10 #include "media/audio/audio_power_monitor.h" |
| 11 #include "media/base/audio_block_fifo.h" | 11 #include "media/base/audio_block_fifo.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace media { | 14 namespace media { |
| 15 | 15 |
| 16 class AudioBlockFifoTest : public testing::Test { | 16 class AudioBlockFifoTest : public testing::Test { |
| 17 public: | 17 public: |
| 18 AudioBlockFifoTest() {} | 18 AudioBlockFifoTest() {} |
| 19 ~AudioBlockFifoTest() override {} | 19 ~AudioBlockFifoTest() override {} |
| 20 | 20 |
| 21 void PushAndVerify(AudioBlockFifo* fifo, int frames_to_push, | 21 void PushAndVerify(AudioBlockFifo* fifo, |
| 22 int channels, int block_frames, int max_frames) { | 22 int frames_to_push, |
| 23 int channels, |
| 24 int block_frames, |
| 25 int max_frames) { |
| 23 for (int filled_frames = max_frames - fifo->GetUnfilledFrames(); | 26 for (int filled_frames = max_frames - fifo->GetUnfilledFrames(); |
| 24 filled_frames + frames_to_push <= max_frames;) { | 27 filled_frames + frames_to_push <= max_frames;) { |
| 25 Push(fifo, frames_to_push, channels); | 28 Push(fifo, frames_to_push, channels); |
| 26 filled_frames += frames_to_push; | 29 filled_frames += frames_to_push; |
| 27 EXPECT_EQ(max_frames - filled_frames, fifo->GetUnfilledFrames()); | 30 EXPECT_EQ(max_frames - filled_frames, fifo->GetUnfilledFrames()); |
| 28 EXPECT_EQ(static_cast<int>(filled_frames / block_frames), | 31 EXPECT_EQ(static_cast<int>(filled_frames / block_frames), |
| 29 fifo->available_blocks()); | 32 fifo->available_blocks()); |
| 30 } | 33 } |
| 31 } | 34 } |
| 32 | 35 |
| 33 void Push(AudioBlockFifo* fifo, int frames_to_push, int channels) { | 36 void Push(AudioBlockFifo* fifo, int frames_to_push, int channels) { |
| 34 DCHECK_LE(frames_to_push, fifo->GetUnfilledFrames()); | 37 DCHECK_LE(frames_to_push, fifo->GetUnfilledFrames()); |
| 35 const int bytes_per_sample = 2; | 38 const int bytes_per_sample = 2; |
| 36 const int data_byte_size = bytes_per_sample * channels * frames_to_push; | 39 const int data_byte_size = bytes_per_sample * channels * frames_to_push; |
| 37 std::unique_ptr<uint8_t[]> data(new uint8_t[data_byte_size]); | 40 std::unique_ptr<uint8_t[]> data(new uint8_t[data_byte_size]); |
| 38 memset(data.get(), 1, data_byte_size); | 41 memset(data.get(), 1, data_byte_size); |
| 39 fifo->Push(data.get(), frames_to_push, bytes_per_sample); | 42 fifo->Push(data.get(), frames_to_push, bytes_per_sample); |
| 40 } | 43 } |
| 41 | 44 |
| 42 void ConsumeAndVerify(AudioBlockFifo* fifo, int expected_unfilled_frames, | 45 void ConsumeAndVerify(AudioBlockFifo* fifo, |
| 46 int expected_unfilled_frames, |
| 43 int expected_available_blocks) { | 47 int expected_available_blocks) { |
| 44 const AudioBus* bus = fifo->Consume(); | 48 const AudioBus* bus = fifo->Consume(); |
| 45 EXPECT_EQ(fifo->GetUnfilledFrames(), expected_unfilled_frames); | 49 EXPECT_EQ(fifo->GetUnfilledFrames(), expected_unfilled_frames); |
| 46 EXPECT_EQ(fifo->available_blocks(), expected_available_blocks); | 50 EXPECT_EQ(fifo->available_blocks(), expected_available_blocks); |
| 47 | 51 |
| 48 // Verify the audio data is not 0. | 52 // Verify the audio data is not 0. |
| 49 for (int i = 0; i < bus->channels(); ++i) { | 53 for (int i = 0; i < bus->channels(); ++i) { |
| 50 EXPECT_GT(bus->channel(i)[0], 0.0f); | 54 EXPECT_GT(bus->channel(i)[0], 0.0f); |
| 51 EXPECT_GT(bus->channel(i)[bus->frames() - 1], 0.0f); | 55 EXPECT_GT(bus->channel(i)[bus->frames() - 1], 0.0f); |
| 52 } | 56 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 EXPECT_TRUE(frames == bus->frames()); | 120 EXPECT_TRUE(frames == bus->frames()); |
| 117 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames * i); | 121 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames * i); |
| 118 EXPECT_TRUE(fifo.available_blocks() == (blocks - i)); | 122 EXPECT_TRUE(fifo.available_blocks() == (blocks - i)); |
| 119 } | 123 } |
| 120 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames * blocks); | 124 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames * blocks); |
| 121 EXPECT_TRUE(fifo.available_blocks() == 0); | 125 EXPECT_TRUE(fifo.available_blocks() == 0); |
| 122 | 126 |
| 123 fifo.Clear(); | 127 fifo.Clear(); |
| 124 int new_push_frames = 128; | 128 int new_push_frames = 128; |
| 125 // Change the input frame and try to fill up the FIFO. | 129 // Change the input frame and try to fill up the FIFO. |
| 126 PushAndVerify(&fifo, new_push_frames, channels, frames, | 130 PushAndVerify(&fifo, new_push_frames, channels, frames, frames * blocks); |
| 127 frames * blocks); | |
| 128 EXPECT_TRUE(fifo.GetUnfilledFrames() != 0); | 131 EXPECT_TRUE(fifo.GetUnfilledFrames() != 0); |
| 129 EXPECT_TRUE(fifo.available_blocks() == blocks -1); | 132 EXPECT_TRUE(fifo.available_blocks() == blocks - 1); |
| 130 | 133 |
| 131 // Consume all the existing filled blocks of data. | 134 // Consume all the existing filled blocks of data. |
| 132 while (fifo.available_blocks()) { | 135 while (fifo.available_blocks()) { |
| 133 const AudioBus* bus = fifo.Consume(); | 136 const AudioBus* bus = fifo.Consume(); |
| 134 EXPECT_TRUE(channels == bus->channels()); | 137 EXPECT_TRUE(channels == bus->channels()); |
| 135 EXPECT_TRUE(frames == bus->frames()); | 138 EXPECT_TRUE(frames == bus->frames()); |
| 136 } | 139 } |
| 137 | 140 |
| 138 // Since one block of FIFO has not been completely filled up, there should | 141 // Since one block of FIFO has not been completely filled up, there should |
| 139 // be remaining frames. | 142 // be remaining frames. |
| 140 const int number_of_push = | 143 const int number_of_push = |
| 141 static_cast<int>(frames * blocks / new_push_frames); | 144 static_cast<int>(frames * blocks / new_push_frames); |
| 142 const int remain_frames = frames * blocks - fifo.GetUnfilledFrames(); | 145 const int remain_frames = frames * blocks - fifo.GetUnfilledFrames(); |
| 143 EXPECT_EQ(number_of_push * new_push_frames - frames * (blocks - 1), | 146 EXPECT_EQ(number_of_push * new_push_frames - frames * (blocks - 1), |
| 144 remain_frames); | 147 remain_frames); |
| 145 | 148 |
| 146 // Completely fill up the buffer again. | 149 // Completely fill up the buffer again. |
| 147 new_push_frames = frames * blocks - remain_frames; | 150 new_push_frames = frames * blocks - remain_frames; |
| 148 PushAndVerify(&fifo, new_push_frames, channels, frames, | 151 PushAndVerify(&fifo, new_push_frames, channels, frames, frames * blocks); |
| 149 frames * blocks); | |
| 150 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0); | 152 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0); |
| 151 EXPECT_TRUE(fifo.available_blocks() == blocks); | 153 EXPECT_TRUE(fifo.available_blocks() == blocks); |
| 152 } | 154 } |
| 153 | 155 |
| 154 // Perform a sequence of Push/Consume calls to a 1 block FIFO. | 156 // Perform a sequence of Push/Consume calls to a 1 block FIFO. |
| 155 TEST_F(AudioBlockFifoTest, PushAndConsumeOneBlockFifo) { | 157 TEST_F(AudioBlockFifoTest, PushAndConsumeOneBlockFifo) { |
| 156 static const int channels = 2; | 158 static const int channels = 2; |
| 157 static const int frames = 441; | 159 static const int frames = 441; |
| 158 static const int blocks = 1; | 160 static const int blocks = 1; |
| 159 AudioBlockFifo fifo(channels, frames, blocks); | 161 AudioBlockFifo fifo(channels, frames, blocks); |
| 160 PushAndVerify(&fifo, frames, channels, frames, frames * blocks); | 162 PushAndVerify(&fifo, frames, channels, frames, frames * blocks); |
| 161 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0); | 163 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0); |
| 162 EXPECT_TRUE(fifo.available_blocks() == blocks); | 164 EXPECT_TRUE(fifo.available_blocks() == blocks); |
| 163 | 165 |
| 164 // Consume 1 block of data. | 166 // Consume 1 block of data. |
| 165 const AudioBus* bus = fifo.Consume(); | 167 const AudioBus* bus = fifo.Consume(); |
| 166 EXPECT_TRUE(channels == bus->channels()); | 168 EXPECT_TRUE(channels == bus->channels()); |
| 167 EXPECT_TRUE(frames == bus->frames()); | 169 EXPECT_TRUE(frames == bus->frames()); |
| 168 EXPECT_TRUE(fifo.available_blocks() == 0); | 170 EXPECT_TRUE(fifo.available_blocks() == 0); |
| 169 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames); | 171 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames); |
| 170 } | 172 } |
| 171 | 173 |
| 174 TEST_F(AudioBlockFifoTest, PushAndConsumeSilence) { |
| 175 static const int channels = 2; |
| 176 static const int frames = 441; |
| 177 static const int blocks = 2; |
| 178 AudioBlockFifo fifo(channels, frames, blocks); |
| 179 // First push non-zero data. |
| 180 Push(&fifo, frames, channels); |
| 181 // Then push silence. |
| 182 fifo.PushSilence(frames); |
| 183 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0); |
| 184 EXPECT_TRUE(fifo.available_blocks() == blocks); |
| 185 |
| 186 // Consume two blocks of data. The first should not be zero, but the second |
| 187 // should be. |
| 188 EXPECT_FALSE(fifo.Consume()->AreFramesZero()); |
| 189 EXPECT_TRUE(fifo.Consume()->AreFramesZero()); |
| 190 } |
| 191 |
| 172 // Dynamically increase the capacity of FIFO and verify buffers are correct. | 192 // Dynamically increase the capacity of FIFO and verify buffers are correct. |
| 173 TEST_F(AudioBlockFifoTest, DynamicallyIncreaseCapacity) { | 193 TEST_F(AudioBlockFifoTest, DynamicallyIncreaseCapacity) { |
| 174 // Create a FIFO with default blocks of buffers. | 194 // Create a FIFO with default blocks of buffers. |
| 175 const int channels = 2; | 195 const int channels = 2; |
| 176 const int frames = 441; | 196 const int frames = 441; |
| 177 const int default_blocks = 2; | 197 const int default_blocks = 2; |
| 178 AudioBlockFifo fifo(channels, frames, default_blocks); | 198 AudioBlockFifo fifo(channels, frames, default_blocks); |
| 179 Push(&fifo, frames, channels); | 199 Push(&fifo, frames, channels); |
| 180 int expected_unfilled_frames = frames; | 200 int expected_unfilled_frames = frames; |
| 181 int expected_available_blocks = 1; | 201 int expected_available_blocks = 1; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 expected_available_blocks); | 241 expected_available_blocks); |
| 222 } | 242 } |
| 223 | 243 |
| 224 // Fill up one block of buffer and consume it, FIFO should then be empty. | 244 // Fill up one block of buffer and consume it, FIFO should then be empty. |
| 225 const int available_frames = max_frames - expected_unfilled_frames; | 245 const int available_frames = max_frames - expected_unfilled_frames; |
| 226 Push(&fifo, frames - available_frames, channels); | 246 Push(&fifo, frames - available_frames, channels); |
| 227 ConsumeAndVerify(&fifo, max_frames, 0); | 247 ConsumeAndVerify(&fifo, max_frames, 0); |
| 228 } | 248 } |
| 229 | 249 |
| 230 } // namespace media | 250 } // namespace media |
| OLD | NEW |