Chromium Code Reviews| 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 <limits> | |
| 6 | |
| 7 #include "media/base/audio_bus.h" | |
| 8 #include "media/base/audio_fifo.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace media { | |
| 12 | |
| 13 class AudioFifoTest : public testing::Test { | |
| 14 public: | |
| 15 AudioFifoTest() {} | |
| 16 ~AudioFifoTest() {} | |
| 17 | |
| 18 void VerifyValue(const float data[], int size, float value) { | |
| 19 for (int i = 0; i < size; ++i) | |
| 20 ASSERT_FLOAT_EQ(value, data[i]) << "i=" << i; | |
| 21 } | |
| 22 | |
| 23 protected: | |
| 24 DISALLOW_COPY_AND_ASSIGN(AudioFifoTest); | |
| 25 }; | |
| 26 | |
| 27 // Verify that construction works as intended. | |
| 28 TEST_F(AudioFifoTest, Construct) { | |
| 29 static const int kChannels = 6; | |
| 30 static const int kMaxFrameCount = 128; | |
| 31 AudioFifo fifo(kChannels, kMaxFrameCount); | |
| 32 EXPECT_EQ(fifo.frames_in_fifo(), 0); | |
| 33 } | |
| 34 | |
| 35 // Pushes audio bus objects to a FIFO and fill it up to different degrees. | |
| 36 // Also, verify that it is not possible to overflow the FIFO. | |
| 37 TEST_F(AudioFifoTest, Push) { | |
| 38 static const int kChannels = 2; | |
| 39 static const int kMaxFrameCount = 128; | |
| 40 AudioFifo fifo(kChannels, kMaxFrameCount); | |
| 41 { | |
| 42 SCOPED_TRACE("Push 50%"); | |
| 43 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount / 2); | |
| 44 EXPECT_EQ(fifo.frames_in_fifo(), 0); | |
| 45 EXPECT_TRUE(fifo.Push(bus.get())); | |
| 46 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); | |
| 47 fifo.Clear(); | |
| 48 } | |
| 49 { | |
| 50 SCOPED_TRACE("Push 100%"); | |
| 51 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); | |
| 52 EXPECT_EQ(fifo.frames_in_fifo(), 0); | |
| 53 EXPECT_TRUE(fifo.Push(bus.get())); | |
| 54 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); | |
| 55 fifo.Clear(); | |
| 56 } | |
| 57 { | |
| 58 SCOPED_TRACE("Overflow"); | |
| 59 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount + 1); | |
| 60 EXPECT_EQ(fifo.frames_in_fifo(), 0); | |
| 61 EXPECT_FALSE(fifo.Push(bus.get())); | |
| 62 EXPECT_EQ(fifo.frames_in_fifo(), 0); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 // Consumes audio bus objects from a FIFO and empty it to different degrees. | |
| 67 // Also, verify that it is not possible to ask for more data than the FIFO | |
| 68 // contains (corresponds to underrun). | |
| 69 TEST_F(AudioFifoTest, Consume) { | |
| 70 static const int kChannels = 2; | |
| 71 static const int kMaxFrameCount = 128; | |
| 72 AudioFifo fifo(kChannels, kMaxFrameCount); | |
| 73 { | |
| 74 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); | |
| 75 EXPECT_TRUE(fifo.Push(bus.get())); | |
| 76 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); | |
| 77 } | |
| 78 { | |
| 79 SCOPED_TRACE("Consume 50%"); | |
| 80 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount / 2); | |
| 81 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); | |
| 82 EXPECT_TRUE(fifo.frames_in_fifo() == bus->frames()); | |
| 83 EXPECT_TRUE(fifo.Push(bus.get())); | |
| 84 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); | |
| 85 } | |
| 86 { | |
| 87 SCOPED_TRACE("Consume 100%"); | |
| 88 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); | |
| 89 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); | |
| 90 EXPECT_EQ(fifo.frames_in_fifo(), 0); | |
| 91 EXPECT_TRUE(fifo.Push(bus.get())); | |
| 92 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); | |
| 93 } | |
| 94 { | |
| 95 SCOPED_TRACE("Underrun"); | |
| 96 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount + 1); | |
| 97 EXPECT_FALSE(fifo.Consume(bus.get(), bus->frames())); | |
| 98 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 // Verify that the frames_in_fifo() method of the FIFO works as intended while | |
| 103 // appending and removing audio bus elements to/from the FIFO. | |
| 104 TEST_F(AudioFifoTest, FramesInFifo) { | |
| 105 static const int kChannels = 2; | |
| 106 static const int kMaxFrameCount = 64; | |
| 107 AudioFifo fifo(kChannels, kMaxFrameCount); | |
| 108 | |
| 109 // Fill up the FIFO and verify that the size grows as it should while adding | |
| 110 // one audio frame each time. | |
| 111 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, 1); | |
| 112 int n = 0; | |
| 113 while (fifo.frames_in_fifo() < kMaxFrameCount) { | |
| 114 EXPECT_TRUE(fifo.Push(bus.get())); | |
| 115 EXPECT_EQ(fifo.frames_in_fifo(), ++n); | |
| 116 } | |
| 117 | |
| 118 // Ensure that we can't append more data when the FIFO is full. | |
| 119 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); | |
| 120 EXPECT_FALSE(fifo.Push(bus.get())); | |
| 121 | |
| 122 // Empty the FIFO and verify that the size decreases as it should. | |
| 123 // Reduce the size of the FIFO by one frame each time. | |
| 124 while (fifo.frames_in_fifo() > 0) { | |
| 125 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); | |
| 126 EXPECT_EQ(fifo.frames_in_fifo(), --n); | |
| 127 } | |
| 128 | |
| 129 // Ensure that we can't remove more data when the FIFO is empty. | |
| 130 EXPECT_EQ(fifo.frames_in_fifo(), 0); | |
| 131 EXPECT_FALSE(fifo.Consume(bus.get(), bus->frames())); | |
| 132 | |
| 133 // Verify that a steady-state size of #frames in the FIFO is maintained | |
| 134 // during a sequence of Push/Consume calls which involves wrapping. We ensure | |
| 135 // wrapping by selecting a buffer size which does divides the FIFO size | |
| 136 // with a remainder of one. | |
| 137 scoped_ptr<AudioBus> bus2 = | |
| 138 AudioBus::Create(kChannels, (kMaxFrameCount / 4) - 1); | |
| 139 const int frames_in_fifo = bus2->frames(); | |
| 140 EXPECT_TRUE(fifo.Push(bus2.get())); | |
| 141 EXPECT_EQ(fifo.frames_in_fifo(), frames_in_fifo); | |
| 142 for (int n = 0; n < kMaxFrameCount; ++n) { | |
| 143 EXPECT_TRUE(fifo.Push(bus2.get())); | |
| 144 EXPECT_TRUE(fifo.Consume(bus2.get(), frames_in_fifo)); | |
| 145 EXPECT_EQ(fifo.frames_in_fifo(), frames_in_fifo); | |
| 146 } | |
| 147 } | |
| 148 | |
| 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. | |
|
DaleCurtis
2012/09/06 11:14:13
I think this section is already tested by the Cons
henrika (OOO until Aug 14)
2012/09/06 11:52:46
Well, almost. I check the content here as well not
| |
| 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 | |
|
DaleCurtis
2012/09/06 11:14:13
extra line.
henrika (OOO until Aug 14)
2012/09/06 11:52:46
Done.
scherkus (not reviewing)
2012/09/06 13:53:57
nit: remove extra blank line
| |
| 213 } | |
| 214 | |
| 215 } // namespace media | |
| OLD | NEW |