| 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 "media/base/audio_block_fifo.h" | 5 #include "media/base/audio_block_fifo.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 | 7 |
| 8 namespace media { | 8 namespace media { |
| 9 | 9 |
| 10 class AudioBlockFifoTest : public testing::Test { | 10 class AudioBlockFifoTest : public testing::Test { |
| 11 public: | 11 protected: |
| 12 AudioBlockFifoTest() {} | 12 AudioBlockFifoTest() {} |
| 13 virtual ~AudioBlockFifoTest() {} | 13 virtual ~AudioBlockFifoTest() {} |
| 14 | 14 |
| 15 void PushAndVerify(AudioBlockFifo* fifo, int frames_to_push, | |
| 16 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(); | |
| 23 filled_frames + frames_to_push <= max_frames;) { | |
| 24 fifo->Push(data.get(), frames_to_push, bytes_per_sample); | |
| 25 filled_frames += frames_to_push; | |
| 26 EXPECT_EQ(max_frames - filled_frames, fifo->GetUnfilledFrames()); | |
| 27 EXPECT_EQ(static_cast<int>(filled_frames / block_frames), | |
| 28 fifo->available_blocks()); | |
| 29 } | |
| 30 } | |
| 31 | |
| 32 private: | 15 private: |
| 33 DISALLOW_COPY_AND_ASSIGN(AudioBlockFifoTest); | 16 DISALLOW_COPY_AND_ASSIGN(AudioBlockFifoTest); |
| 34 }; | 17 }; |
| 35 | 18 |
| 19 class AudioBlockFifoFormatTest : public AudioBlockFifoTest, |
| 20 public testing::WithParamInterface<bool> { |
| 21 protected: |
| 22 void PushAndVerify(AudioBlockFifo* fifo, |
| 23 int frames_to_push, |
| 24 int channels, |
| 25 int block_frames, |
| 26 int max_frames) { |
| 27 const int bytes_per_sample = 2; |
| 28 const int data_byte_size = bytes_per_sample * channels * frames_to_push; |
| 29 if (GetParam()) { |
| 30 scoped_ptr<media::AudioBus> data = |
| 31 AudioBus::Create(channels, frames_to_push); |
| 32 for (int filled_frames = max_frames - fifo->GetUnfilledFrames(); |
| 33 filled_frames + frames_to_push <= max_frames;) { |
| 34 fifo->Push(data.get()); |
| 35 filled_frames += frames_to_push; |
| 36 EXPECT_EQ(max_frames - filled_frames, fifo->GetUnfilledFrames()); |
| 37 EXPECT_EQ(static_cast<int>(filled_frames / block_frames), |
| 38 fifo->available_blocks()); |
| 39 } |
| 40 } else { |
| 41 scoped_ptr<uint8[]> data(new uint8[data_byte_size]); |
| 42 memset(data.get(), 0, data_byte_size); |
| 43 for (int filled_frames = max_frames - fifo->GetUnfilledFrames(); |
| 44 filled_frames + frames_to_push <= max_frames;) { |
| 45 fifo->Push(data.get(), frames_to_push, bytes_per_sample); |
| 46 filled_frames += frames_to_push; |
| 47 EXPECT_EQ(max_frames - filled_frames, fifo->GetUnfilledFrames()); |
| 48 EXPECT_EQ(static_cast<int>(filled_frames / block_frames), |
| 49 fifo->available_blocks()); |
| 50 } |
| 51 } |
| 52 } |
| 53 }; |
| 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); |
| 42 EXPECT_EQ(0, fifo.available_blocks()); | 61 EXPECT_EQ(0, fifo.available_blocks()); |
| 43 EXPECT_EQ(frames * blocks, fifo.GetUnfilledFrames()); | 62 EXPECT_EQ(frames * blocks, fifo.GetUnfilledFrames()); |
| 44 } | 63 } |
| 45 | 64 |
| 46 // Pushes audio bus objects to/from a FIFO up to different degrees. | 65 // Pushes audio bus objects to/from a FIFO up to different degrees. |
| 47 TEST_F(AudioBlockFifoTest, Push) { | 66 TEST_P(AudioBlockFifoFormatTest, Push) { |
| 48 const int channels = 2; | 67 const int channels = 2; |
| 49 const int frames = 128; | 68 const int frames = 128; |
| 50 const int blocks = 2; | 69 const int blocks = 2; |
| 51 AudioBlockFifo fifo(channels, frames, blocks); | 70 AudioBlockFifo fifo(channels, frames, blocks); |
| 52 | 71 |
| 53 // Push frames / 2 of data until FIFO is full. | 72 // Push frames / 2 of data until FIFO is full. |
| 54 PushAndVerify(&fifo, frames / 2, channels, frames, frames * blocks); | 73 PushAndVerify(&fifo, frames / 2, channels, frames, frames * blocks); |
| 55 fifo.Clear(); | 74 fifo.Clear(); |
| 56 | 75 |
| 57 // Push frames of data until FIFO is full. | 76 // Push frames of data until FIFO is full. |
| 58 PushAndVerify(&fifo, frames, channels, frames, frames * blocks); | 77 PushAndVerify(&fifo, frames, channels, frames, frames * blocks); |
| 59 fifo.Clear(); | 78 fifo.Clear(); |
| 60 | 79 |
| 61 // Push 1.5 * frames of data. | 80 // Push 1.5 * frames of data. |
| 62 PushAndVerify(&fifo, frames * 1.5, channels, frames, frames * blocks); | 81 PushAndVerify(&fifo, frames * 1.5, channels, frames, frames * blocks); |
| 63 fifo.Clear(); | 82 fifo.Clear(); |
| 64 } | 83 } |
| 65 | 84 |
| 66 // Perform a sequence of Push/Consume calls to different degrees, and verify | 85 // Perform a sequence of Push/Consume calls to different degrees, and verify |
| 67 // things are correct. | 86 // things are correct. |
| 68 TEST_F(AudioBlockFifoTest, PushAndConsume) { | 87 TEST_P(AudioBlockFifoFormatTest, PushAndConsume) { |
| 69 const int channels = 2; | 88 const int channels = 2; |
| 70 const int frames = 441; | 89 const int frames = 441; |
| 71 const int blocks = 4; | 90 const int blocks = 4; |
| 72 AudioBlockFifo fifo(channels, frames, blocks); | 91 AudioBlockFifo fifo(channels, frames, blocks); |
| 73 PushAndVerify(&fifo, frames, channels, frames, frames * blocks); | 92 PushAndVerify(&fifo, frames, channels, frames, frames * blocks); |
| 74 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0); | 93 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0); |
| 75 EXPECT_TRUE(fifo.available_blocks() == blocks); | 94 EXPECT_TRUE(fifo.available_blocks() == blocks); |
| 76 | 95 |
| 77 // Consume 1 block of data. | 96 // Consume 1 block of data. |
| 78 const AudioBus* bus = fifo.Consume(); | 97 const AudioBus* bus = fifo.Consume(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 93 EXPECT_TRUE(frames == bus->frames()); | 112 EXPECT_TRUE(frames == bus->frames()); |
| 94 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames * i); | 113 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames * i); |
| 95 EXPECT_TRUE(fifo.available_blocks() == (blocks - i)); | 114 EXPECT_TRUE(fifo.available_blocks() == (blocks - i)); |
| 96 } | 115 } |
| 97 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames * blocks); | 116 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames * blocks); |
| 98 EXPECT_TRUE(fifo.available_blocks() == 0); | 117 EXPECT_TRUE(fifo.available_blocks() == 0); |
| 99 | 118 |
| 100 fifo.Clear(); | 119 fifo.Clear(); |
| 101 int new_push_frames = 128; | 120 int new_push_frames = 128; |
| 102 // Change the input frame and try to fill up the FIFO. | 121 // Change the input frame and try to fill up the FIFO. |
| 103 PushAndVerify(&fifo, new_push_frames, channels, frames, | 122 PushAndVerify(&fifo, new_push_frames, channels, frames, frames * blocks); |
| 104 frames * blocks); | |
| 105 EXPECT_TRUE(fifo.GetUnfilledFrames() != 0); | 123 EXPECT_TRUE(fifo.GetUnfilledFrames() != 0); |
| 106 EXPECT_TRUE(fifo.available_blocks() == blocks -1); | 124 EXPECT_TRUE(fifo.available_blocks() == blocks - 1); |
| 107 | 125 |
| 108 // Consume all the existing filled blocks of data. | 126 // Consume all the existing filled blocks of data. |
| 109 while (fifo.available_blocks()) { | 127 while (fifo.available_blocks()) { |
| 110 const AudioBus* bus = fifo.Consume(); | 128 const AudioBus* bus = fifo.Consume(); |
| 111 EXPECT_TRUE(channels == bus->channels()); | 129 EXPECT_TRUE(channels == bus->channels()); |
| 112 EXPECT_TRUE(frames == bus->frames()); | 130 EXPECT_TRUE(frames == bus->frames()); |
| 113 } | 131 } |
| 114 | 132 |
| 115 // Since one block of FIFO has not been completely filled up, there should | 133 // Since one block of FIFO has not been completely filled up, there should |
| 116 // be remaining frames. | 134 // be remaining frames. |
| 117 const int number_of_push = | 135 const int number_of_push = |
| 118 static_cast<int>(frames * blocks / new_push_frames); | 136 static_cast<int>(frames * blocks / new_push_frames); |
| 119 const int remain_frames = frames * blocks - fifo.GetUnfilledFrames(); | 137 const int remain_frames = frames * blocks - fifo.GetUnfilledFrames(); |
| 120 EXPECT_EQ(number_of_push * new_push_frames - frames * (blocks - 1), | 138 EXPECT_EQ(number_of_push * new_push_frames - frames * (blocks - 1), |
| 121 remain_frames); | 139 remain_frames); |
| 122 | 140 |
| 123 // Completely fill up the buffer again. | 141 // Completely fill up the buffer again. |
| 124 new_push_frames = frames * blocks - remain_frames; | 142 new_push_frames = frames * blocks - remain_frames; |
| 125 PushAndVerify(&fifo, new_push_frames, channels, frames, | 143 PushAndVerify(&fifo, new_push_frames, channels, frames, frames * blocks); |
| 126 frames * blocks); | |
| 127 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0); | 144 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0); |
| 128 EXPECT_TRUE(fifo.available_blocks() == blocks); | 145 EXPECT_TRUE(fifo.available_blocks() == blocks); |
| 129 } | 146 } |
| 130 | 147 |
| 131 // Perform a sequence of Push/Consume calls to a 1 block FIFO. | 148 // Perform a sequence of Push/Consume calls to a 1 block FIFO. |
| 132 TEST_F(AudioBlockFifoTest, PushAndConsumeOneBlockFifo) { | 149 TEST_P(AudioBlockFifoFormatTest, PushAndConsumeOneBlockFifo) { |
| 133 static const int channels = 2; | 150 static const int channels = 2; |
| 134 static const int frames = 441; | 151 static const int frames = 441; |
| 135 static const int blocks = 1; | 152 static const int blocks = 1; |
| 136 AudioBlockFifo fifo(channels, frames, blocks); | 153 AudioBlockFifo fifo(channels, frames, blocks); |
| 137 PushAndVerify(&fifo, frames, channels, frames, frames * blocks); | 154 PushAndVerify(&fifo, frames, channels, frames, frames * blocks); |
| 138 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0); | 155 EXPECT_TRUE(fifo.GetUnfilledFrames() == 0); |
| 139 EXPECT_TRUE(fifo.available_blocks() == blocks); | 156 EXPECT_TRUE(fifo.available_blocks() == blocks); |
| 140 | 157 |
| 141 // Consume 1 block of data. | 158 // Consume 1 block of data. |
| 142 const AudioBus* bus = fifo.Consume(); | 159 const AudioBus* bus = fifo.Consume(); |
| 143 EXPECT_TRUE(channels == bus->channels()); | 160 EXPECT_TRUE(channels == bus->channels()); |
| 144 EXPECT_TRUE(frames == bus->frames()); | 161 EXPECT_TRUE(frames == bus->frames()); |
| 145 EXPECT_TRUE(fifo.available_blocks() == 0); | 162 EXPECT_TRUE(fifo.available_blocks() == 0); |
| 146 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames); | 163 EXPECT_TRUE(fifo.GetUnfilledFrames() == frames); |
| 147 } | 164 } |
| 148 | 165 |
| 166 INSTANTIATE_TEST_CASE_P(AudioBlockFifoTests, |
| 167 AudioBlockFifoFormatTest, |
| 168 ::testing::Values(false, true)); |
| 169 |
| 149 } // namespace media | 170 } // namespace media |
| OLD | NEW |