OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/base/audio_fifo.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 |
| 8 namespace media { |
| 9 |
| 10 class AudioFifoTest : public testing::Test { |
| 11 public: |
| 12 AudioFifoTest() {} |
| 13 ~AudioFifoTest() {} |
| 14 |
| 15 void VerifyValue(const float data[], int size, float value) { |
| 16 for (int i = 0; i < size; ++i) |
| 17 ASSERT_FLOAT_EQ(value, data[i]) << "i=" << i; |
| 18 } |
| 19 |
| 20 protected: |
| 21 DISALLOW_COPY_AND_ASSIGN(AudioFifoTest); |
| 22 }; |
| 23 |
| 24 // Verify that construction works as intended. |
| 25 TEST_F(AudioFifoTest, Construct) { |
| 26 static const int kChannels = 6; |
| 27 static const int kMaxFrameCount = 128; |
| 28 AudioFifo fifo(kChannels, kMaxFrameCount); |
| 29 EXPECT_EQ(fifo.frames_in_fifo(), 0); |
| 30 } |
| 31 |
| 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) { |
| 35 static const int kChannels = 2; |
| 36 static const int kMaxFrameCount = 128; |
| 37 AudioFifo fifo(kChannels, kMaxFrameCount); |
| 38 { |
| 39 SCOPED_TRACE("Push 50%"); |
| 40 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount / 2); |
| 41 EXPECT_EQ(fifo.frames_in_fifo(), 0); |
| 42 EXPECT_TRUE(fifo.Push(bus.get())); |
| 43 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); |
| 44 fifo.Clear(); |
| 45 } |
| 46 { |
| 47 SCOPED_TRACE("Push 100%"); |
| 48 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); |
| 49 EXPECT_EQ(fifo.frames_in_fifo(), 0); |
| 50 EXPECT_TRUE(fifo.Push(bus.get())); |
| 51 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); |
| 52 fifo.Clear(); |
| 53 } |
| 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 } |
| 62 |
| 63 // 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) { |
| 67 static const int kChannels = 2; |
| 68 static const int kMaxFrameCount = 128; |
| 69 AudioFifo fifo(kChannels, kMaxFrameCount); |
| 70 { |
| 71 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); |
| 72 EXPECT_TRUE(fifo.Push(bus.get())); |
| 73 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); |
| 74 } |
| 75 { |
| 76 SCOPED_TRACE("Consume 50%"); |
| 77 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount / 2); |
| 78 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); |
| 79 EXPECT_TRUE(fifo.frames_in_fifo() == bus->frames()); |
| 80 EXPECT_TRUE(fifo.Push(bus.get())); |
| 81 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); |
| 82 } |
| 83 { |
| 84 SCOPED_TRACE("Consume 100%"); |
| 85 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); |
| 86 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); |
| 87 EXPECT_EQ(fifo.frames_in_fifo(), 0); |
| 88 EXPECT_TRUE(fifo.Push(bus.get())); |
| 89 EXPECT_EQ(fifo.frames_in_fifo(), 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 } |
| 97 } |
| 98 |
| 99 // Verify that the frames_in_fifo() method of the FIFO works as intended while |
| 100 // appending and removing audio bus elements to/from the FIFO. |
| 101 TEST_F(AudioFifoTest, FramesInFifo) { |
| 102 static const int kChannels = 2; |
| 103 static const int kMaxFrameCount = 64; |
| 104 AudioFifo fifo(kChannels, kMaxFrameCount); |
| 105 |
| 106 // Fill up the FIFO and verify that the size grows as it should while adding |
| 107 // one audio frame each time. |
| 108 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, 1); |
| 109 int n = 0; |
| 110 while (fifo.frames_in_fifo() < kMaxFrameCount) { |
| 111 EXPECT_TRUE(fifo.Push(bus.get())); |
| 112 EXPECT_EQ(fifo.frames_in_fifo(), ++n); |
| 113 } |
| 114 |
| 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 |
| 119 // Empty the FIFO and verify that the size decreases as it should. |
| 120 // Reduce the size of the FIFO by one frame each time. |
| 121 while (fifo.frames_in_fifo() > 0) { |
| 122 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); |
| 123 EXPECT_EQ(fifo.frames_in_fifo(), --n); |
| 124 } |
| 125 |
| 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 |
| 130 // 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 |
| 132 // wrapping by selecting a buffer size which does divides the FIFO size |
| 133 // with a remainder of one. |
| 134 scoped_ptr<AudioBus> bus2 = |
| 135 AudioBus::Create(kChannels, (kMaxFrameCount / 4) - 1); |
| 136 const int frames_in_fifo = bus2->frames(); |
| 137 EXPECT_TRUE(fifo.Push(bus2.get())); |
| 138 EXPECT_EQ(fifo.frames_in_fifo(), frames_in_fifo); |
| 139 for (int n = 0; n < kMaxFrameCount; ++n) { |
| 140 EXPECT_TRUE(fifo.Push(bus2.get())); |
| 141 EXPECT_TRUE(fifo.Consume(bus2.get(), frames_in_fifo)); |
| 142 EXPECT_EQ(fifo.frames_in_fifo(), frames_in_fifo); |
| 143 } |
| 144 } |
| 145 |
| 146 // 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 |
| 148 // values are correct. |
| 149 TEST_F(AudioFifoTest, VerifyDataValues) { |
| 150 static const int kChannels = 2; |
| 151 static const int kFrameCount = 2; |
| 152 static const int kFifoFrameCount = 5 * kFrameCount; |
| 153 |
| 154 AudioFifo fifo(kChannels, kFifoFrameCount); |
| 155 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); |
| 156 EXPECT_EQ(fifo.frames_in_fifo(), 0); |
| 157 EXPECT_EQ(bus->frames(), kFrameCount); |
| 158 |
| 159 // 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 |
| 161 // same value. |
| 162 int value = 1; |
| 163 while (fifo.frames_in_fifo() < kFifoFrameCount) { |
| 164 for (int j = 0; j < bus->channels(); ++j) |
| 165 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value); |
| 166 EXPECT_TRUE(fifo.Push(bus.get())); |
| 167 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames() * value); |
| 168 ++value; |
| 169 } |
| 170 |
| 171 // FIFO should be full now. |
| 172 EXPECT_EQ(fifo.frames_in_fifo(), kFifoFrameCount); |
| 173 |
| 174 // 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 |
| 176 // 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. |
| 178 value = 1; |
| 179 int n = 1; |
| 180 const int frames_to_consume = bus->frames() / 2; |
| 181 while (fifo.frames_in_fifo() > 0) { |
| 182 EXPECT_TRUE(fifo.Consume(bus.get(), frames_to_consume)); |
| 183 for (int j = 0; j < bus->channels(); ++j) |
| 184 VerifyValue(bus->channel(j), frames_to_consume, value); |
| 185 if (n++ % 2 == 0) |
| 186 ++value; // counts 1, 1, 2, 2, 3, 3,... |
| 187 } |
| 188 |
| 189 // FIFO should be empty now. |
| 190 EXPECT_EQ(fifo.frames_in_fifo(), 0); |
| 191 |
| 192 // Push one audio bus to the FIFO and fill it with 1's. |
| 193 value = 1; |
| 194 for (int j = 0; j < bus->channels(); ++j) |
| 195 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value); |
| 196 EXPECT_TRUE(fifo.Push(bus.get())); |
| 197 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); |
| 198 |
| 199 // 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 |
| 201 // this phase. |
| 202 for (int i = 0; i < 5 * kFifoFrameCount; i++) { |
| 203 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); |
| 204 for (int j = 0; j < bus->channels(); ++j) { |
| 205 VerifyValue(bus->channel(j), bus->channels(), value); |
| 206 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value + 1); |
| 207 } |
| 208 EXPECT_TRUE(fifo.Push(bus.get())); |
| 209 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); |
| 210 ++value; |
| 211 } |
| 212 } |
| 213 |
| 214 } // namespace media |
OLD | NEW |