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

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: Changes based on review by Dale 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;
30 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 const int kChannels = 2;
39 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 const int kChannels = 2;
71 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()));
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()));
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()));
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 const int kChannels = 2;
106 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()));
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()));
132
133 // Verify that a steady-state size of one frame in the FIFO is maintained
134 // during a sequence of Push/Consume calls which involves wrapping.
135 EXPECT_TRUE(fifo.Push(bus.get()));
136 EXPECT_EQ(fifo.frames_in_fifo(), 1);
137 for (int n = 0; n < 10 * kMaxFrameCount; ++n) {
138 EXPECT_TRUE(fifo.Push(bus.get()));
139 EXPECT_TRUE(fifo.Consume(bus.get()));
140 EXPECT_EQ(fifo.frames_in_fifo(), 1);
141 }
142 }
143
144 // Do a more realistic test of how the FIFO will be used and also verify that
145 // the actual data content is correct.
146 TEST_F(AudioFifoTest, PushAndConsume) {
147 const int kChannels = 2;
148 const int kFrameCount = 256;
149 const int kMaxAudioBusesInFifoCount = 128;
150
151 scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, kFrameCount);
152 AudioFifo fifo(kChannels, bus->frames() * kMaxAudioBusesInFifoCount);
153
154 // Start by filling up half the FIFO with audio frames. The first audio frame
155 // will contain all 1's, the second all 2's etc. All channels contain the
156 // same value.
157 int value = 1;
158 while (fifo.frames_in_fifo() <
159 bus->frames() * (kMaxAudioBusesInFifoCount / 2)) {
160 // Fill all channels in the bus with dummy values.
161 for (int j = 0; j < bus->channels(); ++j)
162 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value);
163 EXPECT_TRUE(fifo.Push(bus.get()));
164 EXPECT_EQ(fifo.frames_in_fifo(), bus->frames() * value);
165 ++value;
166 }
167
168 // Do a sequence of Consume/Push calls and verify that the acquired values
169 // are correct and that the wrap-around mechanism works as intended.
170 // The size of the FIFO will be constant during this step.
171 const int steady_state_offset = fifo.frames_in_fifo() / bus->frames();
172 for (int i = 0; i < kMaxAudioBusesInFifoCount; ++i) {
173 EXPECT_TRUE(fifo.Consume(bus.get()));
174 value = steady_state_offset + i + 1;
175 for (int j = 0; j < bus->channels(); ++j) {
176 VerifyValue(bus->channel(j), bus->frames(), value - steady_state_offset);
177 std::fill(bus->channel(j), bus->channel(j) + bus->frames(), value);
178 }
179 EXPECT_TRUE(fifo.Push(bus.get()));
180 EXPECT_EQ(fifo.frames_in_fifo(),
181 bus->frames() * (kMaxAudioBusesInFifoCount / 2));
182 }
183
184 // As a last step, flush the FIFO and verify that the retrieved values
185 // are correct.
186 value = value - steady_state_offset + 1;
187 while (fifo.frames_in_fifo() > 0) {
188 EXPECT_TRUE(fifo.Consume(bus.get()));
189 for (int j = 0; j < bus->channels(); ++j) {
190 VerifyValue(bus->channel(j), bus->frames(), value);
191 }
192 value++;
193 }
194 }
195
196 } // namespace media
OLDNEW
« media/base/audio_fifo.h ('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