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

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

Issue 10912079: Adds AudioFifo class to Chrome media. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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
OLDNEW
(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 const int kChannels = 6;
DaleCurtis 2012/09/05 10:34:18 static here and below. Used elsewhere too, so mayb
henrika (OOO until Aug 14) 2012/09/05 12:49:04 I actually use different for each test. OK to kee
DaleCurtis 2012/09/05 13:09:22 Should still be static, non-global is fine with me
30 const int kMaxFrameCount = 128;
31 AudioFifo fifo(kChannels, kMaxFrameCount);
32 EXPECT_EQ(fifo.channels(), kChannels);
33 EXPECT_EQ(fifo.max_size(), kMaxFrameCount);
34 EXPECT_EQ(fifo.size(), 0);
35 }
36
37 // Append audio bus objects to a FIFO and fill it up to different degrees.
38 // Also, verify that it is not possible to overflow the FIFO.
39 TEST_F(AudioFifoTest, Append) {
40 const int kChannels = 2;
41 const int kMaxFrameCount = 128;
42 AudioFifo fifo(kChannels, kMaxFrameCount);
43 {
44 SCOPED_TRACE("append-50%");
45 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size() / 2);
46 EXPECT_TRUE(fifo.empty());
47 EXPECT_TRUE(fifo.Append(bus.get()));
48 EXPECT_EQ(fifo.size(), bus->frames());
49 fifo.Clear();
50 }
51 {
52 SCOPED_TRACE("append-100%");
53 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size());
54 EXPECT_TRUE(fifo.empty());
55 EXPECT_TRUE(fifo.Append(bus.get()));
56 EXPECT_EQ(fifo.size(), bus->frames());
57 EXPECT_TRUE(fifo.full());
58 fifo.Clear();
59 }
60 {
61 SCOPED_TRACE("overflow");
62 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size() + 1);
63 EXPECT_TRUE(fifo.empty());
64 EXPECT_FALSE(fifo.Append(bus.get()));
65 EXPECT_TRUE(fifo.empty());
66 }
67 }
68
69 // Remove audio bus objects from a FIFO and empty it to different degrees.
70 // Also, verify that it is not possible to ask for more data than the FIFO
71 // contains (corresponds to underrun).
72 TEST_F(AudioFifoTest, Remove) {
73 const int kChannels = 2;
74 const int kMaxFrameCount = 128;
75 AudioFifo fifo(kChannels, kMaxFrameCount);
76 {
77 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size());
78 EXPECT_TRUE(fifo.Append(bus.get()));
79 EXPECT_TRUE(fifo.full());
80 }
81 {
82 SCOPED_TRACE("remove-50%");
83 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size() / 2);
84 EXPECT_TRUE(fifo.Remove(bus.get()));
85 EXPECT_TRUE(fifo.size() == bus->frames());
86 EXPECT_TRUE(fifo.Append(bus.get()));
87 EXPECT_TRUE(fifo.full());
88 }
89 {
90 SCOPED_TRACE("remove-100%");
91 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size());
92 EXPECT_TRUE(fifo.Remove(bus.get()));
93 EXPECT_TRUE(fifo.empty());
94 EXPECT_TRUE(fifo.Append(bus.get()));
95 EXPECT_TRUE(fifo.full());
96 }
97 {
98 SCOPED_TRACE("underrun");
99 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, fifo.max_size() + 1);
100 EXPECT_FALSE(fifo.Remove(bus.get()));
101 EXPECT_TRUE(fifo.full());
102 }
103 }
104
105 // Verify that the size() method of the FIFO works as intended while appending
106 // and removing audio bus elements to/from the FIFO.
107 TEST_F(AudioFifoTest, Size) {
108 const int kChannels = 2;
109 const int kMaxFrameCount = 480;
110 AudioFifo fifo(kChannels, kMaxFrameCount);
111
112 // Fill up the FIFO and verify that the size grows as it should while adding
113 // one audio frame each time.
114 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, 1);
115 int n = 0;
116 while (!fifo.full()) {
DaleCurtis 2012/09/05 10:34:18 How long does this take to run? Not sure we need 4
henrika (OOO until Aug 14) 2012/09/05 12:49:04 It takes 4ms on my machine. Reduced it to 64 => 1m
117 EXPECT_TRUE(fifo.Append(bus.get()));
118 EXPECT_EQ(fifo.size(), ++n);
119 }
120
121 // Ensure that we can't append more data when the FIFO is full.
122 EXPECT_TRUE(fifo.full());
123 EXPECT_FALSE(fifo.Append(bus.get()));
124
125 // Empty the FIFO and verify that the size decreases as it should.
126 // Reduce the size of the FIFO by one frame each time.
127 while (!fifo.empty()) {
128 EXPECT_TRUE(fifo.Remove(bus.get()));
129 EXPECT_EQ(fifo.size(), --n);
130 }
131
132 // Ensure that we can't remove more data when the FIFO is empty.
133 EXPECT_TRUE(fifo.empty());
134 EXPECT_FALSE(fifo.Remove(bus.get()));
135
136 // Verify that a steady-state size of one frame in the FIFO is maintained
137 // during a sequence of Append/Remove calls.
138 EXPECT_TRUE(fifo.Append(bus.get()));
139 EXPECT_EQ(fifo.size(), 1);
140 for (int n = 0; n < 100; ++n) {
141 EXPECT_TRUE(fifo.Append(bus.get()));
142 EXPECT_TRUE(fifo.Remove(bus.get()));
143 EXPECT_TRUE(fifo.size() == 1);
144 }
145 }
146
147 // Do a more realistic test of how the FIFO will be used and also verify that
148 // the actual data content is correct.
149 TEST_F(AudioFifoTest, AppdendAndRemove) {
Chris Rogers 2012/09/04 20:07:20 typo: AppdendAndRemove
henrika (OOO until Aug 14) 2012/09/05 10:14:36 Done.
150 const int kChannels = 2;
151 const int kFrameCount = 256;
152 const int kMaxAudioBusesInFifoCount = 128;
153
154 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount);
155 AudioFifo fifo(kChannels, bus->frames() * kMaxAudioBusesInFifoCount);
156 EXPECT_EQ(fifo.max_size(), kFrameCount * kMaxAudioBusesInFifoCount);
157
158 // Start by filling up half the FIFO with audio frames. The first audio frame
159 // will contain all 1's, the second all 2's etc. All channels contain the
160 // same value.
161 int value = 1;
162 while (fifo.size() < bus->frames() * (kMaxAudioBusesInFifoCount / 2)) {
163 // Fill all channels in the bus with dummy values.
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.Append(bus.get()));
167 EXPECT_EQ(fifo.size(), bus->frames() * value);
168 ++value;
169 }
170
171 // Do a sequence of Remove/Append calls and verify that the acquired values
172 // are correct and that the wrap-around mechanism works as intended.
173 // The size of the FIFO will be constant during this step.
174 const int steady_state_offset = fifo.size() / bus->frames();
175 for (int i = 0; i < kMaxAudioBusesInFifoCount; ++i) {
176 EXPECT_TRUE(fifo.Remove(bus.get()));
177 value = steady_state_offset + i + 1;
178 for (int j = 0; j < bus->channels(); ++j) {
179 VerifyValue(bus->channel(j), bus->frames(), value - steady_state_offset);
180 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value);
181 }
182 EXPECT_TRUE(fifo.Append(bus.get()));
183 EXPECT_EQ(fifo.size(), bus->frames() * (kMaxAudioBusesInFifoCount / 2));
184 }
185
186 // As a last step, flush the FIFO and verify that the retrieved values
187 // are correct.
188 value = value - steady_state_offset + 1;
189 while (!fifo.empty()) {
190 EXPECT_TRUE(fifo.Remove(bus.get()));
191 for (int j = 0; j < bus->channels(); ++j) {
192 VerifyValue(bus->channel(j), bus->frames(), value);
193 }
194 value++;
195 }
196 }
197
198 } // namespace media
OLDNEW
« media/base/audio_fifo.cc ('K') | « media/base/audio_fifo.cc ('k') | media/media.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698