Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: media/base/audio_fifo_unittest.cc

Issue 10915123: Adds media::AudioPullFifo class to Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: test Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/base/audio_fifo.cc ('k') | media/base/audio_pull_fifo.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // TODO(henrika): add test which included |start_frame| in Consume() call.
6
5 #include "media/base/audio_fifo.h" 7 #include "media/base/audio_fifo.h"
6 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
7 9
8 namespace media { 10 namespace media {
9 11
10 class AudioFifoTest : public testing::Test { 12 class AudioFifoTest : public testing::Test {
11 public: 13 public:
12 AudioFifoTest() {} 14 AudioFifoTest() {}
13 ~AudioFifoTest() {} 15 ~AudioFifoTest() {}
14 16
15 void VerifyValue(const float data[], int size, float value) { 17 void VerifyValue(const float data[], int size, float value) {
16 for (int i = 0; i < size; ++i) 18 for (int i = 0; i < size; ++i)
17 ASSERT_FLOAT_EQ(value, data[i]) << "i=" << i; 19 ASSERT_FLOAT_EQ(value, data[i]) << "i=" << i;
18 } 20 }
19 21
20 protected: 22 protected:
21 DISALLOW_COPY_AND_ASSIGN(AudioFifoTest); 23 DISALLOW_COPY_AND_ASSIGN(AudioFifoTest);
22 }; 24 };
23 25
24 // Verify that construction works as intended. 26 // Verify that construction works as intended.
25 TEST_F(AudioFifoTest, Construct) { 27 TEST_F(AudioFifoTest, Construct) {
26 static const int kChannels = 6; 28 static const int kChannels = 6;
27 static const int kMaxFrameCount = 128; 29 static const int kMaxFrameCount = 128;
28 AudioFifo fifo(kChannels, kMaxFrameCount); 30 AudioFifo fifo(kChannels, kMaxFrameCount);
29 EXPECT_EQ(fifo.frames_in_fifo(), 0); 31 EXPECT_EQ(fifo.frames(), 0);
30 } 32 }
31 33
32 // Pushes audio bus objects to a FIFO and fill it up to different degrees. 34 // 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 TEST_F(AudioFifoTest, Push) {
35 static const int kChannels = 2; 36 static const int kChannels = 2;
36 static const int kMaxFrameCount = 128; 37 static const int kMaxFrameCount = 128;
37 AudioFifo fifo(kChannels, kMaxFrameCount); 38 AudioFifo fifo(kChannels, kMaxFrameCount);
38 { 39 {
39 SCOPED_TRACE("Push 50%"); 40 SCOPED_TRACE("Push 50%");
40 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount / 2); 41 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount / 2);
41 EXPECT_EQ(fifo.frames_in_fifo(), 0); 42 EXPECT_EQ(fifo.frames(), 0);
42 EXPECT_TRUE(fifo.Push(bus.get())); 43 fifo.Push(bus.get());
43 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); 44 EXPECT_EQ(fifo.frames(), bus->frames());
44 fifo.Clear(); 45 fifo.Clear();
45 } 46 }
46 { 47 {
47 SCOPED_TRACE("Push 100%"); 48 SCOPED_TRACE("Push 100%");
48 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); 49 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount);
49 EXPECT_EQ(fifo.frames_in_fifo(), 0); 50 EXPECT_EQ(fifo.frames(), 0);
50 EXPECT_TRUE(fifo.Push(bus.get())); 51 fifo.Push(bus.get());
51 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); 52 EXPECT_EQ(fifo.frames(), bus->frames());
52 fifo.Clear(); 53 fifo.Clear();
53 } 54 }
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 } 55 }
62 56
63 // Consumes audio bus objects from a FIFO and empty it to different degrees. 57 // 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) { 58 TEST_F(AudioFifoTest, Consume) {
67 static const int kChannels = 2; 59 static const int kChannels = 2;
68 static const int kMaxFrameCount = 128; 60 static const int kMaxFrameCount = 128;
69 AudioFifo fifo(kChannels, kMaxFrameCount); 61 AudioFifo fifo(kChannels, kMaxFrameCount);
70 { 62 {
71 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); 63 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount);
72 EXPECT_TRUE(fifo.Push(bus.get())); 64 fifo.Push(bus.get());
73 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); 65 EXPECT_EQ(fifo.frames(), kMaxFrameCount);
74 } 66 }
75 { 67 {
76 SCOPED_TRACE("Consume 50%"); 68 SCOPED_TRACE("Consume 50%");
77 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount / 2); 69 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount / 2);
78 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); 70 fifo.Consume(bus.get(), 0, bus->frames());
79 EXPECT_TRUE(fifo.frames_in_fifo() == bus->frames()); 71 EXPECT_TRUE(fifo.frames() == bus->frames());
80 EXPECT_TRUE(fifo.Push(bus.get())); 72 fifo.Push(bus.get());
81 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); 73 EXPECT_EQ(fifo.frames(), kMaxFrameCount);
82 } 74 }
83 { 75 {
84 SCOPED_TRACE("Consume 100%"); 76 SCOPED_TRACE("Consume 100%");
85 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount); 77 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kMaxFrameCount);
86 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); 78 fifo.Consume(bus.get(), 0, bus->frames());
87 EXPECT_EQ(fifo.frames_in_fifo(), 0); 79 EXPECT_EQ(fifo.frames(), 0);
88 EXPECT_TRUE(fifo.Push(bus.get())); 80 fifo.Push(bus.get());
89 EXPECT_EQ(fifo.frames_in_fifo(), kMaxFrameCount); 81 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 } 82 }
97 } 83 }
98 84
99 // Verify that the frames_in_fifo() method of the FIFO works as intended while 85 // Verify that the frames() method of the FIFO works as intended while
100 // appending and removing audio bus elements to/from the FIFO. 86 // appending and removing audio bus elements to/from the FIFO.
101 TEST_F(AudioFifoTest, FramesInFifo) { 87 TEST_F(AudioFifoTest, FramesInFifo) {
102 static const int kChannels = 2; 88 static const int kChannels = 2;
103 static const int kMaxFrameCount = 64; 89 static const int kMaxFrameCount = 64;
104 AudioFifo fifo(kChannels, kMaxFrameCount); 90 AudioFifo fifo(kChannels, kMaxFrameCount);
105 91
106 // Fill up the FIFO and verify that the size grows as it should while adding 92 // Fill up the FIFO and verify that the size grows as it should while adding
107 // one audio frame each time. 93 // one audio frame each time.
108 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, 1); 94 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, 1);
109 int n = 0; 95 int n = 0;
110 while (fifo.frames_in_fifo() < kMaxFrameCount) { 96 while (fifo.frames() < kMaxFrameCount) {
111 EXPECT_TRUE(fifo.Push(bus.get())); 97 fifo.Push(bus.get());
112 EXPECT_EQ(fifo.frames_in_fifo(), ++n); 98 EXPECT_EQ(fifo.frames(), ++n);
113 } 99 }
114 100 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 101
119 // Empty the FIFO and verify that the size decreases as it should. 102 // Empty the FIFO and verify that the size decreases as it should.
120 // Reduce the size of the FIFO by one frame each time. 103 // Reduce the size of the FIFO by one frame each time.
121 while (fifo.frames_in_fifo() > 0) { 104 while (fifo.frames() > 0) {
122 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); 105 fifo.Consume(bus.get(), 0, bus->frames());
123 EXPECT_EQ(fifo.frames_in_fifo(), --n); 106 EXPECT_EQ(fifo.frames(), --n);
124 } 107 }
125 108 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 109
130 // Verify that a steady-state size of #frames in the FIFO is maintained 110 // 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 111 // 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 112 // wrapping by selecting a buffer size which does divides the FIFO size
133 // with a remainder of one. 113 // with a remainder of one.
134 scoped_ptr<AudioBus> bus2 = 114 scoped_ptr<AudioBus> bus2 =
135 AudioBus::Create(kChannels, (kMaxFrameCount / 4) - 1); 115 AudioBus::Create(kChannels, (kMaxFrameCount / 4) - 1);
136 const int frames_in_fifo = bus2->frames(); 116 const int frames_in_fifo = bus2->frames();
137 EXPECT_TRUE(fifo.Push(bus2.get())); 117 fifo.Push(bus2.get());
138 EXPECT_EQ(fifo.frames_in_fifo(), frames_in_fifo); 118 EXPECT_EQ(fifo.frames(), frames_in_fifo);
139 for (int n = 0; n < kMaxFrameCount; ++n) { 119 for (int n = 0; n < kMaxFrameCount; ++n) {
140 EXPECT_TRUE(fifo.Push(bus2.get())); 120 fifo.Push(bus2.get());
141 EXPECT_TRUE(fifo.Consume(bus2.get(), frames_in_fifo)); 121 fifo.Consume(bus2.get(), 0, frames_in_fifo);
142 EXPECT_EQ(fifo.frames_in_fifo(), frames_in_fifo); 122 EXPECT_EQ(fifo.frames(), frames_in_fifo);
143 } 123 }
144 } 124 }
145 125
146 // Perform a sequence of Push/Consume calls and verify that the data written 126 // 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 127 // to the FIFO is correctly retrieved, i.e., that the order is correct and the
148 // values are correct. 128 // values are correct.
149 TEST_F(AudioFifoTest, VerifyDataValues) { 129 TEST_F(AudioFifoTest, VerifyDataValues) {
150 static const int kChannels = 2; 130 static const int kChannels = 2;
151 static const int kFrameCount = 2; 131 static const int kFrameCount = 2;
152 static const int kFifoFrameCount = 5 * kFrameCount; 132 static const int kFifoFrameCount = 5 * kFrameCount;
153 133
154 AudioFifo fifo(kChannels, kFifoFrameCount); 134 AudioFifo fifo(kChannels, kFifoFrameCount);
155 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount); 135 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount);
156 EXPECT_EQ(fifo.frames_in_fifo(), 0); 136 EXPECT_EQ(fifo.frames(), 0);
157 EXPECT_EQ(bus->frames(), kFrameCount); 137 EXPECT_EQ(bus->frames(), kFrameCount);
158 138
159 // Start by filling up the FIFO with audio frames. The first audio frame 139 // 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 140 // will contain all 1's, the second all 2's etc. All channels contain the
161 // same value. 141 // same value.
162 int value = 1; 142 int value = 1;
163 while (fifo.frames_in_fifo() < kFifoFrameCount) { 143 while (fifo.frames() < kFifoFrameCount) {
164 for (int j = 0; j < bus->channels(); ++j) 144 for (int j = 0; j < bus->channels(); ++j)
165 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value); 145 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value);
166 EXPECT_TRUE(fifo.Push(bus.get())); 146 fifo.Push(bus.get());
167 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames() * value); 147 EXPECT_EQ(fifo.frames(), bus->frames() * value);
168 ++value; 148 ++value;
169 } 149 }
170 150
171 // FIFO should be full now. 151 // FIFO should be full now.
172 EXPECT_EQ(fifo.frames_in_fifo(), kFifoFrameCount); 152 EXPECT_EQ(fifo.frames(), kFifoFrameCount);
173 153
174 // Consume all audio frames in the FIFO and verify that the stored values 154 // 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 155 // 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. 156 // 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. 157 // It means that we shall read out the same value two times in row.
178 value = 1; 158 value = 1;
179 int n = 1; 159 int n = 1;
180 const int frames_to_consume = bus->frames() / 2; 160 const int frames_to_consume = bus->frames() / 2;
181 while (fifo.frames_in_fifo() > 0) { 161 while (fifo.frames() > 0) {
182 EXPECT_TRUE(fifo.Consume(bus.get(), frames_to_consume)); 162 fifo.Consume(bus.get(), 0, frames_to_consume);
183 for (int j = 0; j < bus->channels(); ++j) 163 for (int j = 0; j < bus->channels(); ++j)
184 VerifyValue(bus->channel(j), frames_to_consume, value); 164 VerifyValue(bus->channel(j), frames_to_consume, value);
185 if (n++ % 2 == 0) 165 if (n++ % 2 == 0)
186 ++value; // counts 1, 1, 2, 2, 3, 3,... 166 ++value; // counts 1, 1, 2, 2, 3, 3,...
187 } 167 }
188 168
189 // FIFO should be empty now. 169 // FIFO should be empty now.
190 EXPECT_EQ(fifo.frames_in_fifo(), 0); 170 EXPECT_EQ(fifo.frames(), 0);
191 171
192 // Push one audio bus to the FIFO and fill it with 1's. 172 // Push one audio bus to the FIFO and fill it with 1's.
193 value = 1; 173 value = 1;
194 for (int j = 0; j < bus->channels(); ++j) 174 for (int j = 0; j < bus->channels(); ++j)
195 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value); 175 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value);
196 EXPECT_TRUE(fifo.Push(bus.get())); 176 fifo.Push(bus.get());
197 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); 177 EXPECT_EQ(fifo.frames(), bus->frames());
198 178
199 // Keep calling Consume/Push a few rounds and verify that we read out the 179 // 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 180 // correct values. The number of elements shall be fixed (kFrameCount) during
201 // this phase. 181 // this phase.
202 for (int i = 0; i < 5 * kFifoFrameCount; i++) { 182 for (int i = 0; i < 5 * kFifoFrameCount; i++) {
203 EXPECT_TRUE(fifo.Consume(bus.get(), bus->frames())); 183 fifo.Consume(bus.get(), 0, bus->frames());
204 for (int j = 0; j < bus->channels(); ++j) { 184 for (int j = 0; j < bus->channels(); ++j) {
205 VerifyValue(bus->channel(j), bus->channels(), value); 185 VerifyValue(bus->channel(j), bus->channels(), value);
206 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value + 1); 186 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value + 1);
207 } 187 }
208 EXPECT_TRUE(fifo.Push(bus.get())); 188 fifo.Push(bus.get());
209 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames()); 189 EXPECT_EQ(fifo.frames(), bus->frames());
210 ++value; 190 ++value;
211 } 191 }
212 } 192 }
213 193
214 } // namespace media 194 } // namespace media
OLDNEW
« no previous file with comments | « media/base/audio_fifo.cc ('k') | media/base/audio_pull_fifo.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698