BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / media / base / audio_buffer_queue_unittest.cc
blobf467cda93a645e2cdd138e8e02679d57a0f29d51
1 // Copyright 2013 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.
5 #include "base/basictypes.h"
6 #include "base/logging.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/time/time.h"
9 #include "media/base/audio_buffer.h"
10 #include "media/base/audio_buffer_queue.h"
11 #include "media/base/audio_bus.h"
12 #include "media/base/test_helpers.h"
13 #include "media/base/timestamp_constants.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace media {
18 const int kSampleRate = 44100;
20 static void VerifyBus(AudioBus* bus,
21 int offset,
22 int frames,
23 int buffer_size,
24 float start,
25 float increment) {
26 for (int ch = 0; ch < bus->channels(); ++ch) {
27 const float v = start + ch * buffer_size * increment;
28 for (int i = offset; i < offset + frames; ++i) {
29 ASSERT_FLOAT_EQ(v + (i - offset) * increment, bus->channel(ch)[i])
30 << "i=" << i << ", ch=" << ch;
35 template <typename T>
36 static scoped_refptr<AudioBuffer> MakeTestBuffer(SampleFormat format,
37 ChannelLayout channel_layout,
38 T start,
39 T step,
40 int frames) {
41 return MakeAudioBuffer<T>(format,
42 channel_layout,
43 ChannelLayoutToChannelCount(channel_layout),
44 kSampleRate,
45 start,
46 step,
47 frames,
48 kNoTimestamp());
51 TEST(AudioBufferQueueTest, AppendAndClear) {
52 const ChannelLayout channel_layout = CHANNEL_LAYOUT_MONO;
53 AudioBufferQueue buffer;
54 EXPECT_EQ(0, buffer.frames());
55 buffer.Append(
56 MakeTestBuffer<uint8>(kSampleFormatU8, channel_layout, 10, 1, 8));
57 EXPECT_EQ(8, buffer.frames());
58 buffer.Clear();
59 EXPECT_EQ(0, buffer.frames());
60 buffer.Append(
61 MakeTestBuffer<uint8>(kSampleFormatU8, channel_layout, 20, 1, 8));
62 EXPECT_EQ(8, buffer.frames());
65 TEST(AudioBufferQueueTest, MultipleAppend) {
66 const ChannelLayout channel_layout = CHANNEL_LAYOUT_MONO;
67 AudioBufferQueue buffer;
68 buffer.Append(
69 MakeTestBuffer<uint8>(kSampleFormatU8, channel_layout, 10, 1, 8));
70 EXPECT_EQ(8, buffer.frames());
71 buffer.Append(
72 MakeTestBuffer<uint8>(kSampleFormatU8, channel_layout, 10, 1, 8));
73 EXPECT_EQ(16, buffer.frames());
74 buffer.Append(
75 MakeTestBuffer<uint8>(kSampleFormatU8, channel_layout, 10, 1, 8));
76 EXPECT_EQ(24, buffer.frames());
77 buffer.Append(
78 MakeTestBuffer<uint8>(kSampleFormatU8, channel_layout, 10, 1, 8));
79 EXPECT_EQ(32, buffer.frames());
80 buffer.Append(
81 MakeTestBuffer<uint8>(kSampleFormatU8, channel_layout, 10, 1, 8));
82 EXPECT_EQ(40, buffer.frames());
85 TEST(AudioBufferQueueTest, IteratorCheck) {
86 const ChannelLayout channel_layout = CHANNEL_LAYOUT_MONO;
87 const int channels = ChannelLayoutToChannelCount(channel_layout);
88 AudioBufferQueue buffer;
89 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
91 // Append 40 frames in 5 buffers. Intersperse ReadFrames() to make the
92 // iterator is pointing to the correct position.
93 buffer.Append(MakeTestBuffer<float>(
94 kSampleFormatF32, channel_layout, 10.0f, 1.0f, 8));
95 EXPECT_EQ(8, buffer.frames());
97 EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get()));
98 EXPECT_EQ(4, buffer.frames());
99 VerifyBus(bus.get(), 0, 4, bus->frames(), 10, 1);
101 buffer.Append(MakeTestBuffer<float>(
102 kSampleFormatF32, channel_layout, 20.0f, 1.0f, 8));
103 EXPECT_EQ(12, buffer.frames());
104 buffer.Append(MakeTestBuffer<float>(
105 kSampleFormatF32, channel_layout, 30.0f, 1.0f, 8));
106 EXPECT_EQ(20, buffer.frames());
108 buffer.SeekFrames(16);
109 EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get()));
110 EXPECT_EQ(0, buffer.frames());
111 VerifyBus(bus.get(), 0, 4, bus->frames(), 34, 1);
113 buffer.Append(MakeTestBuffer<float>(
114 kSampleFormatF32, channel_layout, 40.0f, 1.0f, 8));
115 EXPECT_EQ(8, buffer.frames());
116 buffer.Append(MakeTestBuffer<float>(
117 kSampleFormatF32, channel_layout, 50.0f, 1.0f, 8));
118 EXPECT_EQ(16, buffer.frames());
120 EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get()));
121 VerifyBus(bus.get(), 0, 4, bus->frames(), 40, 1);
123 // Read off the end of the buffer.
124 EXPECT_EQ(12, buffer.frames());
125 buffer.SeekFrames(8);
126 EXPECT_EQ(4, buffer.ReadFrames(100, 0, bus.get()));
127 VerifyBus(bus.get(), 0, 4, bus->frames(), 54, 1);
130 TEST(AudioBufferQueueTest, Seek) {
131 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
132 AudioBufferQueue buffer;
134 // Add 6 frames of data.
135 buffer.Append(MakeTestBuffer<float>(
136 kSampleFormatF32, channel_layout, 1.0f, 1.0f, 6));
137 EXPECT_EQ(6, buffer.frames());
139 // Seek past 2 frames.
140 buffer.SeekFrames(2);
141 EXPECT_EQ(4, buffer.frames());
143 // Seek to end of data.
144 buffer.SeekFrames(4);
145 EXPECT_EQ(0, buffer.frames());
147 // At end, seek now fails unless 0 specified.
148 buffer.SeekFrames(0);
151 TEST(AudioBufferQueueTest, ReadF32) {
152 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
153 const int channels = ChannelLayoutToChannelCount(channel_layout);
154 AudioBufferQueue buffer;
156 // Add 76 frames of data.
157 buffer.Append(
158 MakeTestBuffer<float>(kSampleFormatF32, channel_layout, 1.0f, 1.0f, 6));
159 buffer.Append(
160 MakeTestBuffer<float>(kSampleFormatF32, channel_layout, 13.0f, 1.0f, 10));
161 buffer.Append(
162 MakeTestBuffer<float>(kSampleFormatF32, channel_layout, 33.0f, 1.0f, 60));
163 EXPECT_EQ(76, buffer.frames());
165 // Read 3 frames from the buffer.
166 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
167 EXPECT_EQ(3, buffer.ReadFrames(3, 0, bus.get()));
168 EXPECT_EQ(73, buffer.frames());
169 VerifyBus(bus.get(), 0, 3, 6, 1, 1);
171 // Now read 5 frames, which will span buffers. Append the data into AudioBus.
172 EXPECT_EQ(5, buffer.ReadFrames(5, 3, bus.get()));
173 EXPECT_EQ(68, buffer.frames());
174 VerifyBus(bus.get(), 0, 6, 6, 1, 1);
175 VerifyBus(bus.get(), 6, 2, 10, 13, 1);
177 // Now skip into the third buffer.
178 buffer.SeekFrames(20);
179 EXPECT_EQ(48, buffer.frames());
181 // Now read 2 frames, which are in the third buffer.
182 EXPECT_EQ(2, buffer.ReadFrames(2, 0, bus.get()));
183 VerifyBus(bus.get(), 0, 2, 60, 45, 1);
186 TEST(AudioBufferQueueTest, ReadU8) {
187 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0;
188 const int channels = ChannelLayoutToChannelCount(channel_layout);
189 const int frames = 4;
190 AudioBufferQueue buffer;
192 // Add 4 frames of data.
193 buffer.Append(
194 MakeTestBuffer<uint8>(kSampleFormatU8, channel_layout, 128, 1, frames));
196 // Read all 4 frames from the buffer.
197 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, frames);
198 EXPECT_EQ(frames, buffer.ReadFrames(frames, 0, bus.get()));
199 EXPECT_EQ(0, buffer.frames());
200 VerifyBus(bus.get(), 0, frames, bus->frames(), 0, 1.0f / 127.0f);
203 TEST(AudioBufferQueueTest, ReadS16) {
204 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
205 const int channels = ChannelLayoutToChannelCount(channel_layout);
206 AudioBufferQueue buffer;
208 // Add 24 frames of data.
209 buffer.Append(
210 MakeTestBuffer<int16>(kSampleFormatS16, channel_layout, 1, 1, 4));
211 buffer.Append(
212 MakeTestBuffer<int16>(kSampleFormatS16, channel_layout, 9, 1, 20));
213 EXPECT_EQ(24, buffer.frames());
215 // Read 6 frames from the buffer.
216 const int frames = 6;
217 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, buffer.frames());
218 EXPECT_EQ(frames, buffer.ReadFrames(frames, 0, bus.get()));
219 EXPECT_EQ(18, buffer.frames());
220 VerifyBus(bus.get(), 0, 4, 4, 1.0f / kint16max, 1.0f / kint16max);
221 VerifyBus(bus.get(), 4, 2, 20, 9.0f / kint16max, 1.0f / kint16max);
224 TEST(AudioBufferQueueTest, ReadS32) {
225 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
226 const int channels = ChannelLayoutToChannelCount(channel_layout);
227 AudioBufferQueue buffer;
229 // Add 24 frames of data.
230 buffer.Append(
231 MakeTestBuffer<int32>(kSampleFormatS32, channel_layout, 1, 1, 4));
232 buffer.Append(
233 MakeTestBuffer<int32>(kSampleFormatS32, channel_layout, 9, 1, 20));
234 EXPECT_EQ(24, buffer.frames());
236 // Read 6 frames from the buffer.
237 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
238 EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get()));
239 EXPECT_EQ(18, buffer.frames());
240 VerifyBus(bus.get(), 0, 4, 4, 1.0f / kint32max, 1.0f / kint32max);
241 VerifyBus(bus.get(), 4, 2, 20, 9.0f / kint32max, 1.0f / kint32max);
243 // Read the next 2 frames.
244 EXPECT_EQ(2, buffer.ReadFrames(2, 0, bus.get()));
245 EXPECT_EQ(16, buffer.frames());
246 VerifyBus(bus.get(), 0, 2, 20, 11.0f / kint32max, 1.0f / kint32max);
249 TEST(AudioBufferQueueTest, ReadF32Planar) {
250 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
251 const int channels = ChannelLayoutToChannelCount(channel_layout);
252 AudioBufferQueue buffer;
254 // Add 14 frames of data.
255 buffer.Append(MakeTestBuffer<float>(
256 kSampleFormatPlanarF32, channel_layout, 1.0f, 1.0f, 4));
257 buffer.Append(MakeTestBuffer<float>(
258 kSampleFormatPlanarF32, channel_layout, 50.0f, 1.0f, 10));
259 EXPECT_EQ(14, buffer.frames());
261 // Read 6 frames from the buffer.
262 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
263 EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get()));
264 EXPECT_EQ(8, buffer.frames());
265 VerifyBus(bus.get(), 0, 4, 4, 1, 1);
266 VerifyBus(bus.get(), 4, 2, 10, 50, 1);
269 TEST(AudioBufferQueueTest, ReadS16Planar) {
270 const ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO;
271 const int channels = ChannelLayoutToChannelCount(channel_layout);
272 AudioBufferQueue buffer;
274 // Add 24 frames of data.
275 buffer.Append(
276 MakeTestBuffer<int16>(kSampleFormatPlanarS16, channel_layout, 1, 1, 4));
277 buffer.Append(
278 MakeTestBuffer<int16>(kSampleFormatPlanarS16, channel_layout, 5, 1, 20));
279 EXPECT_EQ(24, buffer.frames());
281 // Read 6 frames from the buffer.
282 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
283 EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get()));
284 EXPECT_EQ(18, buffer.frames());
285 VerifyBus(bus.get(), 0, 4, 4, 1.0f / kint16max, 1.0f / kint16max);
286 VerifyBus(bus.get(), 4, 2, 20, 5.0f / kint16max, 1.0f / kint16max);
289 TEST(AudioBufferQueueTest, ReadManyChannels) {
290 const ChannelLayout channel_layout = CHANNEL_LAYOUT_OCTAGONAL;
291 const int channels = ChannelLayoutToChannelCount(channel_layout);
292 AudioBufferQueue buffer;
294 // Add 76 frames of data.
295 buffer.Append(
296 MakeTestBuffer<float>(kSampleFormatF32, channel_layout, 0.0f, 1.0f, 6));
297 buffer.Append(MakeTestBuffer<float>(
298 kSampleFormatF32, channel_layout, 6.0f * channels, 1.0f, 10));
299 buffer.Append(MakeTestBuffer<float>(
300 kSampleFormatF32, channel_layout, 16.0f * channels, 1.0f, 60));
301 EXPECT_EQ(76, buffer.frames());
303 // Read 3 frames from the buffer.
304 scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
305 EXPECT_EQ(30, buffer.ReadFrames(30, 0, bus.get()));
306 EXPECT_EQ(46, buffer.frames());
307 VerifyBus(bus.get(), 0, 6, 6, 0, 1);
308 VerifyBus(bus.get(), 6, 10, 10, 6 * channels, 1);
309 VerifyBus(bus.get(), 16, 14, 60, 16 * channels, 1);
312 TEST(AudioBufferQueueTest, Peek) {
313 const ChannelLayout channel_layout = CHANNEL_LAYOUT_4_0;
314 const int channels = ChannelLayoutToChannelCount(channel_layout);
315 AudioBufferQueue buffer;
317 // Add 60 frames of data.
318 const int frames = 60;
319 buffer.Append(MakeTestBuffer<float>(
320 kSampleFormatF32, channel_layout, 0.0f, 1.0f, frames));
321 EXPECT_EQ(frames, buffer.frames());
323 // Peek at the first 30 frames.
324 scoped_ptr<AudioBus> bus1 = AudioBus::Create(channels, frames);
325 EXPECT_EQ(frames, buffer.frames());
326 EXPECT_EQ(frames, buffer.PeekFrames(60, 0, 0, bus1.get()));
327 EXPECT_EQ(30, buffer.PeekFrames(30, 0, 0, bus1.get()));
328 EXPECT_EQ(frames, buffer.frames());
329 VerifyBus(bus1.get(), 0, 30, bus1->frames(), 0, 1);
331 // Now read the next 30 frames (which should be the same as those peeked at).
332 scoped_ptr<AudioBus> bus2 = AudioBus::Create(channels, frames);
333 EXPECT_EQ(30, buffer.ReadFrames(30, 0, bus2.get()));
334 VerifyBus(bus2.get(), 0, 30, bus2->frames(), 0, 1);
336 // Peek 10 frames forward
337 bus1->Zero();
338 EXPECT_EQ(5, buffer.PeekFrames(5, 10, 0, bus1.get()));
339 VerifyBus(bus1.get(), 0, 5, bus1->frames(), 40, 1);
341 // Peek to the end of the buffer.
342 EXPECT_EQ(30, buffer.frames());
343 EXPECT_EQ(30, buffer.PeekFrames(60, 0, 0, bus1.get()));
344 EXPECT_EQ(30, buffer.PeekFrames(30, 0, 0, bus1.get()));
347 } // namespace media