| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_fifo.h" | 5 #include "media/base/audio_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 AudioFifoTest : public testing::Test { | 10 class AudioFifoTest : public testing::Test { |
| 11 public: | 11 public: |
| 12 AudioFifoTest() {} | 12 AudioFifoTest() {} |
| 13 ~AudioFifoTest() {} | 13 ~AudioFifoTest() {} |
| 14 | 14 |
| 15 void VerifyValue(const float data[], int size, float value) { | 15 void VerifyValue(const float data[], int size, float value) { |
| 16 for (int i = 0; i < size; ++i) | 16 for (int i = 0; i < size; ++i) |
| 17 ASSERT_FLOAT_EQ(value, data[i]) << "i=" << i; | 17 ASSERT_FLOAT_EQ(value, data[i]) << "i=" << i; |
| 18 } | 18 } |
| 19 | 19 |
| 20 protected: | 20 protected: |
| 21 DISALLOW_COPY_AND_ASSIGN(AudioFifoTest); | 21 DISALLOW_COPY_AND_ASSIGN(AudioFifoTest); |
| 22 }; | 22 }; |
| 23 | 23 |
| 24 // Verify that construction works as intended. | 24 // Verify that construction works as intended. |
| 25 TEST_F(AudioFifoTest, Construct) { | 25 TEST_F(AudioFifoTest, Construct) { |
| 26 static const int kChannels = 6; | 26 static const int kChannels = 6; |
| 27 static const int kMaxFrameCount = 128; | 27 static const int kMaxFrameCount = 128; |
| 28 AudioFifo fifo(kChannels, kMaxFrameCount); | 28 AudioFifo fifo(kChannels, kMaxFrameCount); |
| 29 EXPECT_EQ(fifo.frames_in_fifo(), 0); | 29 EXPECT_EQ(fifo.frames(), 0); |
| 30 } | 30 } |
| 31 | 31 |
| 32 // Pushes audio bus objects to a FIFO and fill it up to different degrees. | 32 // Pushes audio bus objects to a FIFO and fill it up to different degrees. |
| 33 // Also, verify that it is not possible to overflow the FIFO. | |
| 34 TEST_F(AudioFifoTest, Push) { | 33 TEST_F(AudioFifoTest, Push) { |
| 35 static const int kChannels = 2; | 34 static const int kChannels = 2; |
| 36 static const int kMaxFrameCount = 128; | 35 static const int kMaxFrameCount = 128; |
| 37 AudioFifo fifo(kChannels, kMaxFrameCount); | 36 AudioFifo fifo(kChannels, kMaxFrameCount); |
| 38 { | 37 { |
| 39 SCOPED_TRACE("Push 50%"); | 38 SCOPED_TRACE("Push 50%"); |
| 40 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount / 2); | 39 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount / 2); |
| 41 EXPECT_EQ(fifo.frames_in_fifo(), 0); | 40 EXPECT_EQ(fifo.frames(), 0); |
| 42 EXPECT_TRUE(fifo.Push(bus.get())); | 41 fifo.Push(bus.get()); |
| 43 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); | 42 EXPECT_EQ(fifo.frames(), bus->frames()); |
| 44 fifo.Clear(); | 43 fifo.Clear(); |
| 45 } | 44 } |
| 46 { | 45 { |
| 47 SCOPED_TRACE("Push 100%"); | 46 SCOPED_TRACE("Push 100%"); |
| 48 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); | 47 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); |
| 49 EXPECT_EQ(fifo.frames_in_fifo(), 0); | 48 EXPECT_EQ(fifo.frames(), 0); |
| 50 EXPECT_TRUE(fifo.Push(bus.get())); | 49 fifo.Push(bus.get()); |
| 51 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); | 50 EXPECT_EQ(fifo.frames(), bus->frames()); |
| 52 fifo.Clear(); | 51 fifo.Clear(); |
| 53 } | 52 } |
| 54 { | |
| 55 SCOPED_TRACE("Overflow"); | |
| 56 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount + 1); | |
| 57 EXPECT_EQ(fifo.frames_in_fifo(), 0); | |
| 58 EXPECT_FALSE(fifo.Push(bus.get())); | |
| 59 EXPECT_EQ(fifo.frames_in_fifo(), 0); | |
| 60 } | |
| 61 } | 53 } |
| 62 | 54 |
| 63 // Consumes audio bus objects from a FIFO and empty it to different degrees. | 55 // Consumes audio bus objects from a FIFO and empty it to different degrees. |
| 64 // Also, verify that it is not possible to ask for more data than the FIFO | |
| 65 // contains (corresponds to underrun). | |
| 66 TEST_F(AudioFifoTest, Consume) { | 56 TEST_F(AudioFifoTest, Consume) { |
| 67 static const int kChannels = 2; | 57 static const int kChannels = 2; |
| 68 static const int kMaxFrameCount = 128; | 58 static const int kMaxFrameCount = 128; |
| 69 AudioFifo fifo(kChannels, kMaxFrameCount); | 59 AudioFifo fifo(kChannels, kMaxFrameCount); |
| 70 { | 60 { |
| 71 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); | 61 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); |
| 72 EXPECT_TRUE(fifo.Push(bus.get())); | 62 fifo.Push(bus.get()); |
| 73 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); | 63 EXPECT_EQ(fifo.frames(), kMaxFrameCount); |
| 74 } | 64 } |
| 75 { | 65 { |
| 76 SCOPED_TRACE("Consume 50%"); | 66 SCOPED_TRACE("Consume 50%"); |
| 77 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount / 2); | 67 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount / 2); |
| 78 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); | 68 fifo.Consume(bus.get(), bus->frames()); |
| 79 EXPECT_TRUE(fifo.frames_in_fifo() == bus->frames()); | 69 EXPECT_TRUE(fifo.frames() == bus->frames()); |
| 80 EXPECT_TRUE(fifo.Push(bus.get())); | 70 fifo.Push(bus.get()); |
| 81 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); | 71 EXPECT_EQ(fifo.frames(), kMaxFrameCount); |
| 82 } | 72 } |
| 83 { | 73 { |
| 84 SCOPED_TRACE("Consume 100%"); | 74 SCOPED_TRACE("Consume 100%"); |
| 85 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); | 75 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); |
| 86 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); | 76 fifo.Consume(bus.get(), bus->frames()); |
| 87 EXPECT_EQ(fifo.frames_in_fifo(), 0); | 77 EXPECT_EQ(fifo.frames(), 0); |
| 88 EXPECT_TRUE(fifo.Push(bus.get())); | 78 fifo.Push(bus.get()); |
| 89 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); | 79 EXPECT_EQ(fifo.frames(), kMaxFrameCount); |
| 90 } | |
| 91 { | |
| 92 SCOPED_TRACE("Underrun"); | |
| 93 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount + 1); | |
| 94 EXPECT_FALSE(fifo.Consume(bus.get(), bus->frames())); | |
| 95 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); | |
| 96 } | 80 } |
| 97 } | 81 } |
| 98 | 82 |
| 99 // Verify that the frames_in_fifo() method of the FIFO works as intended while | 83 // Verify that the frames() method of the FIFO works as intended while |
| 100 // appending and removing audio bus elements to/from the FIFO. | 84 // appending and removing audio bus elements to/from the FIFO. |
| 101 TEST_F(AudioFifoTest, FramesInFifo) { | 85 TEST_F(AudioFifoTest, FramesInFifo) { |
| 102 static const int kChannels = 2; | 86 static const int kChannels = 2; |
| 103 static const int kMaxFrameCount = 64; | 87 static const int kMaxFrameCount = 64; |
| 104 AudioFifo fifo(kChannels, kMaxFrameCount); | 88 AudioFifo fifo(kChannels, kMaxFrameCount); |
| 105 | 89 |
| 106 // Fill up the FIFO and verify that the size grows as it should while adding | 90 // Fill up the FIFO and verify that the size grows as it should while adding |
| 107 // one audio frame each time. | 91 // one audio frame each time. |
| 108 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, 1); | 92 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, 1); |
| 109 int n = 0; | 93 int n = 0; |
| 110 while (fifo.frames_in_fifo() < kMaxFrameCount) { | 94 while (fifo.frames() < kMaxFrameCount) { |
| 111 EXPECT_TRUE(fifo.Push(bus.get())); | 95 fifo.Push(bus.get()); |
| 112 EXPECT_EQ(fifo.frames_in_fifo(), ++n); | 96 EXPECT_EQ(fifo.frames(), ++n); |
| 113 } | 97 } |
| 114 | 98 EXPECT_EQ(fifo.frames(), kMaxFrameCount); |
| 115 // Ensure that we can't append more data when the FIFO is full. | |
| 116 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); | |
| 117 EXPECT_FALSE(fifo.Push(bus.get())); | |
| 118 | 99 |
| 119 // Empty the FIFO and verify that the size decreases as it should. | 100 // Empty the FIFO and verify that the size decreases as it should. |
| 120 // Reduce the size of the FIFO by one frame each time. | 101 // Reduce the size of the FIFO by one frame each time. |
| 121 while (fifo.frames_in_fifo() > 0) { | 102 while (fifo.frames() > 0) { |
| 122 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); | 103 fifo.Consume(bus.get(), bus->frames()); |
| 123 EXPECT_EQ(fifo.frames_in_fifo(), --n); | 104 EXPECT_EQ(fifo.frames(), --n); |
| 124 } | 105 } |
| 125 | 106 EXPECT_EQ(fifo.frames(), 0); |
| 126 // Ensure that we can't remove more data when the FIFO is empty. | |
| 127 EXPECT_EQ(fifo.frames_in_fifo(), 0); | |
| 128 EXPECT_FALSE(fifo.Consume(bus.get(), bus->frames())); | |
| 129 | 107 |
| 130 // Verify that a steady-state size of #frames in the FIFO is maintained | 108 // Verify that a steady-state size of #frames in the FIFO is maintained |
| 131 // during a sequence of Push/Consume calls which involves wrapping. We ensure | 109 // during a sequence of Push/Consume calls which involves wrapping. We ensure |
| 132 // wrapping by selecting a buffer size which does divides the FIFO size | 110 // wrapping by selecting a buffer size which does divides the FIFO size |
| 133 // with a remainder of one. | 111 // with a remainder of one. |
| 134 scoped_ptr<AudioBus> bus2 = | 112 scoped_ptr<AudioBus> bus2 = |
| 135 AudioBus::Create(kChannels, (kMaxFrameCount / 4) - 1); | 113 AudioBus::Create(kChannels, (kMaxFrameCount / 4) - 1); |
| 136 const int frames_in_fifo = bus2->frames(); | 114 const int frames_in_fifo = bus2->frames(); |
| 137 EXPECT_TRUE(fifo.Push(bus2.get())); | 115 fifo.Push(bus2.get()); |
| 138 EXPECT_EQ(fifo.frames_in_fifo(), frames_in_fifo); | 116 EXPECT_EQ(fifo.frames(), frames_in_fifo); |
| 139 for (int n = 0; n < kMaxFrameCount; ++n) { | 117 for (int n = 0; n < kMaxFrameCount; ++n) { |
| 140 EXPECT_TRUE(fifo.Push(bus2.get())); | 118 fifo.Push(bus2.get()); |
| 141 EXPECT_TRUE(fifo.Consume(bus2.get(), frames_in_fifo)); | 119 fifo.Consume(bus2.get(), frames_in_fifo); |
| 142 EXPECT_EQ(fifo.frames_in_fifo(), frames_in_fifo); | 120 EXPECT_EQ(fifo.frames(), frames_in_fifo); |
| 143 } | 121 } |
| 144 } | 122 } |
| 145 | 123 |
| 146 // Perform a sequence of Push/Consume calls and verify that the data written | 124 // Perform a sequence of Push/Consume calls and verify that the data written |
| 147 // to the FIFO is correctly retrieved, i.e., that the order is correct and the | 125 // to the FIFO is correctly retrieved, i.e., that the order is correct and the |
| 148 // values are correct. | 126 // values are correct. |
| 149 TEST_F(AudioFifoTest, VerifyDataValues) { | 127 TEST_F(AudioFifoTest, VerifyDataValues) { |
| 150 static const int kChannels = 2; | 128 static const int kChannels = 2; |
| 151 static const int kFrameCount = 2; | 129 static const int kFrameCount = 2; |
| 152 static const int kFifoFrameCount = 5 * kFrameCount; | 130 static const int kFifoFrameCount = 5 * kFrameCount; |
| 153 | 131 |
| 154 AudioFifo fifo(kChannels, kFifoFrameCount); | 132 AudioFifo fifo(kChannels, kFifoFrameCount); |
| 155 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); | 133 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); |
| 156 EXPECT_EQ(fifo.frames_in_fifo(), 0); | 134 EXPECT_EQ(fifo.frames(), 0); |
| 157 EXPECT_EQ(bus->frames(), kFrameCount); | 135 EXPECT_EQ(bus->frames(), kFrameCount); |
| 158 | 136 |
| 159 // Start by filling up the FIFO with audio frames. The first audio frame | 137 // Start by filling up the FIFO with audio frames. The first audio frame |
| 160 // will contain all 1's, the second all 2's etc. All channels contain the | 138 // will contain all 1's, the second all 2's etc. All channels contain the |
| 161 // same value. | 139 // same value. |
| 162 int value = 1; | 140 int value = 1; |
| 163 while (fifo.frames_in_fifo() < kFifoFrameCount) { | 141 while (fifo.frames() < kFifoFrameCount) { |
| 164 for (int j = 0; j < bus->channels(); ++j) | 142 for (int j = 0; j < bus->channels(); ++j) |
| 165 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value); | 143 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value); |
| 166 EXPECT_TRUE(fifo.Push(bus.get())); | 144 fifo.Push(bus.get()); |
| 167 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames() * value); | 145 EXPECT_EQ(fifo.frames(), bus->frames() * value); |
| 168 ++value; | 146 ++value; |
| 169 } | 147 } |
| 170 | 148 |
| 171 // FIFO should be full now. | 149 // FIFO should be full now. |
| 172 EXPECT_EQ(fifo.frames_in_fifo(), kFifoFrameCount); | 150 EXPECT_EQ(fifo.frames(), kFifoFrameCount); |
| 173 | 151 |
| 174 // Consume all audio frames in the FIFO and verify that the stored values | 152 // Consume all audio frames in the FIFO and verify that the stored values |
| 175 // are correct. In this example, we shall read out: 1, 2, 3, 4, 5 in that | 153 // are correct. In this example, we shall read out: 1, 2, 3, 4, 5 in that |
| 176 // order. Note that we set |frames_to_consume| to half the size of the bus. | 154 // order. Note that we set |frames_to_consume| to half the size of the bus. |
| 177 // It means that we shall read out the same value two times in row. | 155 // It means that we shall read out the same value two times in row. |
| 178 value = 1; | 156 value = 1; |
| 179 int n = 1; | 157 int n = 1; |
| 180 const int frames_to_consume = bus->frames() / 2; | 158 const int frames_to_consume = bus->frames() / 2; |
| 181 while (fifo.frames_in_fifo() > 0) { | 159 while (fifo.frames() > 0) { |
| 182 EXPECT_TRUE(fifo.Consume(bus.get(), frames_to_consume)); | 160 fifo.Consume(bus.get(), frames_to_consume); |
| 183 for (int j = 0; j < bus->channels(); ++j) | 161 for (int j = 0; j < bus->channels(); ++j) |
| 184 VerifyValue(bus->channel(j), frames_to_consume, value); | 162 VerifyValue(bus->channel(j), frames_to_consume, value); |
| 185 if (n++ % 2 == 0) | 163 if (n++ % 2 == 0) |
| 186 ++value; // counts 1, 1, 2, 2, 3, 3,... | 164 ++value; // counts 1, 1, 2, 2, 3, 3,... |
| 187 } | 165 } |
| 188 | 166 |
| 189 // FIFO should be empty now. | 167 // FIFO should be empty now. |
| 190 EXPECT_EQ(fifo.frames_in_fifo(), 0); | 168 EXPECT_EQ(fifo.frames(), 0); |
| 191 | 169 |
| 192 // Push one audio bus to the FIFO and fill it with 1's. | 170 // Push one audio bus to the FIFO and fill it with 1's. |
| 193 value = 1; | 171 value = 1; |
| 194 for (int j = 0; j < bus->channels(); ++j) | 172 for (int j = 0; j < bus->channels(); ++j) |
| 195 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value); | 173 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value); |
| 196 EXPECT_TRUE(fifo.Push(bus.get())); | 174 fifo.Push(bus.get()); |
| 197 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); | 175 EXPECT_EQ(fifo.frames(), bus->frames()); |
| 198 | 176 |
| 199 // Keep calling Consume/Push a few rounds and verify that we read out the | 177 // Keep calling Consume/Push a few rounds and verify that we read out the |
| 200 // correct values. The number of elements shall be fixed (kFrameCount) during | 178 // correct values. The number of elements shall be fixed (kFrameCount) during |
| 201 // this phase. | 179 // this phase. |
| 202 for (int i = 0; i < 5 * kFifoFrameCount; i++) { | 180 for (int i = 0; i < 5 * kFifoFrameCount; i++) { |
| 203 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); | 181 fifo.Consume(bus.get(), bus->frames()); |
| 204 for (int j = 0; j < bus->channels(); ++j) { | 182 for (int j = 0; j < bus->channels(); ++j) { |
| 205 VerifyValue(bus->channel(j), bus->channels(), value); | 183 VerifyValue(bus->channel(j), bus->channels(), value); |
| 206 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value + 1); | 184 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value + 1); |
| 207 } | 185 } |
| 208 EXPECT_TRUE(fifo.Push(bus.get())); | 186 fifo.Push(bus.get()); |
| 209 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); | 187 EXPECT_EQ(fifo.frames(), bus->frames()); |
| 210 ++value; | 188 ++value; |
| 211 } | 189 } |
| 212 } | 190 } |
| 213 | 191 |
| 214 } // namespace media | 192 } // namespace media |
| OLD | NEW |