1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "media/base/audio_buffer.h"
#include "media/base/audio_buffer_queue.h"
#include "media/base/audio_bus.h"
#include "media/base/buffers.h"
#include "media/base/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
static void VerifyResult(float* channel_data,
int frames,
float start,
float increment) {
for (int i = 0; i < frames; ++i) {
SCOPED_TRACE(base::StringPrintf(
"i=%d/%d start=%f, increment=%f", i, frames, start, increment));
ASSERT_EQ(start, channel_data[i]);
start += increment;
}
}
TEST(AudioBufferQueueTest, AppendAndClear) {
const int channels = 1;
const int frames = 8;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
EXPECT_EQ(0, buffer.frames());
buffer.Append(MakeAudioBuffer<uint8>(
kSampleFormatU8, channels, 10, 1, frames, kNoTime, kNoTime));
EXPECT_EQ(frames, buffer.frames());
buffer.Clear();
EXPECT_EQ(0, buffer.frames());
buffer.Append(MakeAudioBuffer<uint8>(
kSampleFormatU8, channels, 20, 1, frames, kNoTime, kNoTime));
EXPECT_EQ(frames, buffer.frames());
}
TEST(AudioBufferQueueTest, MultipleAppend) {
const int channels = 1;
const int frames = 8;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
// Append 40 frames in 5 buffers.
buffer.Append(MakeAudioBuffer<uint8>(
kSampleFormatU8, channels, 10, 1, frames, kNoTime, kNoTime));
EXPECT_EQ(8, buffer.frames());
buffer.Append(MakeAudioBuffer<uint8>(
kSampleFormatU8, channels, 10, 1, frames, kNoTime, kNoTime));
EXPECT_EQ(16, buffer.frames());
buffer.Append(MakeAudioBuffer<uint8>(
kSampleFormatU8, channels, 10, 1, frames, kNoTime, kNoTime));
EXPECT_EQ(24, buffer.frames());
buffer.Append(MakeAudioBuffer<uint8>(
kSampleFormatU8, channels, 10, 1, frames, kNoTime, kNoTime));
EXPECT_EQ(32, buffer.frames());
buffer.Append(MakeAudioBuffer<uint8>(
kSampleFormatU8, channels, 10, 1, frames, kNoTime, kNoTime));
EXPECT_EQ(40, buffer.frames());
}
TEST(AudioBufferQueueTest, IteratorCheck) {
const int channels = 1;
const int frames = 8;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
// Append 40 frames in 5 buffers. Intersperse ReadFrames() to make the
// iterator is pointing to the correct position.
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatF32, channels, 10.0f, 1.0f, frames, kNoTime, kNoTime));
EXPECT_EQ(8, buffer.frames());
EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get()));
EXPECT_EQ(4, buffer.frames());
VerifyResult(bus->channel(0), 4, 10.0f, 1.0f);
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatF32, channels, 20.0f, 1.0f, frames, kNoTime, kNoTime));
EXPECT_EQ(12, buffer.frames());
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatF32, channels, 30.0f, 1.0f, frames, kNoTime, kNoTime));
EXPECT_EQ(20, buffer.frames());
buffer.SeekFrames(16);
EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get()));
EXPECT_EQ(0, buffer.frames());
VerifyResult(bus->channel(0), 4, 34.0f, 1.0f);
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatF32, channels, 40.0f, 1.0f, frames, kNoTime, kNoTime));
EXPECT_EQ(8, buffer.frames());
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatF32, channels, 50.0f, 1.0f, frames, kNoTime, kNoTime));
EXPECT_EQ(16, buffer.frames());
EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get()));
VerifyResult(bus->channel(0), 4, 40.0f, 1.0f);
// Read off the end of the buffer.
EXPECT_EQ(12, buffer.frames());
buffer.SeekFrames(8);
EXPECT_EQ(4, buffer.ReadFrames(100, 0, bus.get()));
VerifyResult(bus->channel(0), 4, 54.0f, 1.0f);
}
TEST(AudioBufferQueueTest, Seek) {
const int channels = 2;
const int frames = 6;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
// Add 6 frames of data.
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatF32, channels, 1.0f, 1.0f, frames, kNoTime, kNoTime));
EXPECT_EQ(6, buffer.frames());
// Seek past 2 frames.
buffer.SeekFrames(2);
EXPECT_EQ(4, buffer.frames());
// Seek to end of data.
buffer.SeekFrames(4);
EXPECT_EQ(0, buffer.frames());
// At end, seek now fails unless 0 specified.
buffer.SeekFrames(0);
}
TEST(AudioBufferQueueTest, ReadF32) {
const int channels = 2;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
// Add 76 frames of data.
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatF32, channels, 1.0f, 1.0f, 6, kNoTime, kNoTime));
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatF32, channels, 13.0f, 1.0f, 10, kNoTime, kNoTime));
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatF32, channels, 33.0f, 1.0f, 60, kNoTime, kNoTime));
EXPECT_EQ(76, buffer.frames());
// Read 3 frames from the buffer. F32 is interleaved, so ch[0] should be
// 1, 3, 5, and ch[1] should be 2, 4, 6.
scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
EXPECT_EQ(3, buffer.ReadFrames(3, 0, bus.get()));
EXPECT_EQ(73, buffer.frames());
VerifyResult(bus->channel(0), 3, 1.0f, 2.0f);
VerifyResult(bus->channel(1), 3, 2.0f, 2.0f);
// Now read 5 frames, which will span buffers. Append the data into AudioBus.
EXPECT_EQ(5, buffer.ReadFrames(5, 3, bus.get()));
EXPECT_EQ(68, buffer.frames());
VerifyResult(bus->channel(0), 8, 1.0f, 2.0f);
VerifyResult(bus->channel(1), 8, 2.0f, 2.0f);
// Now skip into the third buffer.
buffer.SeekFrames(20);
EXPECT_EQ(48, buffer.frames());
// Now read 2 frames, which are in the third buffer.
EXPECT_EQ(2, buffer.ReadFrames(2, 0, bus.get()));
VerifyResult(bus->channel(0), 2, 57.0f, 2.0f);
VerifyResult(bus->channel(1), 2, 58.0f, 2.0f);
}
TEST(AudioBufferQueueTest, ReadU8) {
const int channels = 4;
const int frames = 4;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
// Add 4 frames of data.
buffer.Append(MakeAudioBuffer<uint8>(
kSampleFormatU8, channels, 128, 1, frames, kNoTime, kNoTime));
// Read all 4 frames from the buffer. Data is interleaved, so ch[0] should be
// 128, 132, 136, 140, other channels similar. However, values are converted
// from [0, 255] to [-1.0, 1.0] with a bias of 128. Thus the first buffer
// value should be 0.0, then 1/127, 2/127, etc.
scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
EXPECT_EQ(4, buffer.ReadFrames(4, 0, bus.get()));
EXPECT_EQ(0, buffer.frames());
VerifyResult(bus->channel(0), 4, 0.0f, 4.0f / 127.0f);
VerifyResult(bus->channel(1), 4, 1.0f / 127.0f, 4.0f / 127.0f);
VerifyResult(bus->channel(2), 4, 2.0f / 127.0f, 4.0f / 127.0f);
VerifyResult(bus->channel(3), 4, 3.0f / 127.0f, 4.0f / 127.0f);
}
TEST(AudioBufferQueueTest, ReadS16) {
const int channels = 2;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
// Add 24 frames of data.
buffer.Append(MakeAudioBuffer<int16>(
kSampleFormatS16, channels, 1, 1, 4, kNoTime, kNoTime));
buffer.Append(MakeAudioBuffer<int16>(
kSampleFormatS16, channels, 9, 1, 20, kNoTime, kNoTime));
EXPECT_EQ(24, buffer.frames());
// Read 6 frames from the buffer. Data is interleaved, so ch[0] should be
// 1, 3, 5, 7, 9, 11, and ch[1] should be 2, 4, 6, 8, 10, 12.
// Data is converted to float from -1.0 to 1.0 based on int16 range.
scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get()));
EXPECT_EQ(18, buffer.frames());
VerifyResult(bus->channel(0), 6, 1.0f / kint16max, 2.0f / kint16max);
VerifyResult(bus->channel(1), 6, 2.0f / kint16max, 2.0f / kint16max);
}
TEST(AudioBufferQueueTest, ReadS32) {
const int channels = 2;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
// Add 24 frames of data.
buffer.Append(MakeAudioBuffer<int32>(
kSampleFormatS32, channels, 1, 1, 4, kNoTime, kNoTime));
buffer.Append(MakeAudioBuffer<int32>(
kSampleFormatS32, channels, 9, 1, 20, kNoTime, kNoTime));
EXPECT_EQ(24, buffer.frames());
// Read 6 frames from the buffer. Data is interleaved, so ch[0] should be
// 1, 3, 5, 7, 100, 106, and ch[1] should be 2, 4, 6, 8, 103, 109.
// Data is converted to float from -1.0 to 1.0 based on int32 range.
scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get()));
EXPECT_EQ(18, buffer.frames());
VerifyResult(bus->channel(0), 6, 1.0f / kint32max, 2.0f / kint32max);
VerifyResult(bus->channel(1), 6, 2.0f / kint32max, 2.0f / kint32max);
// Read the next 2 frames.
EXPECT_EQ(2, buffer.ReadFrames(2, 0, bus.get()));
EXPECT_EQ(16, buffer.frames());
VerifyResult(bus->channel(0), 2, 13.0f / kint32max, 2.0f / kint32max);
VerifyResult(bus->channel(1), 2, 14.0f / kint32max, 2.0f / kint32max);
}
TEST(AudioBufferQueueTest, ReadF32Planar) {
const int channels = 2;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
// Add 14 frames of data.
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatPlanarF32, channels, 1.0f, 1.0f, 4, kNoTime, kNoTime));
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatPlanarF32, channels, 50.0f, 1.0f, 10, kNoTime, kNoTime));
EXPECT_EQ(14, buffer.frames());
// Read 6 frames from the buffer. F32 is planar, so ch[0] should be
// 1, 2, 3, 4, 50, 51, and ch[1] should be 5, 6, 7, 8, 60, 61.
scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get()));
EXPECT_EQ(8, buffer.frames());
VerifyResult(bus->channel(0), 4, 1.0f, 1.0f);
VerifyResult(bus->channel(0) + 4, 2, 50.0f, 1.0f);
VerifyResult(bus->channel(1), 4, 5.0f, 1.0f);
VerifyResult(bus->channel(1) + 4, 2, 60.0f, 1.0f);
}
TEST(AudioBufferQueueTest, ReadS16Planar) {
const int channels = 2;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
// Add 24 frames of data.
buffer.Append(MakeAudioBuffer<int16>(
kSampleFormatPlanarS16, channels, 1, 1, 4, kNoTime, kNoTime));
buffer.Append(MakeAudioBuffer<int16>(
kSampleFormatPlanarS16, channels, 100, 5, 20, kNoTime, kNoTime));
EXPECT_EQ(24, buffer.frames());
// Read 6 frames from the buffer. Data is planar, so ch[0] should be
// 1, 2, 3, 4, 100, 105, and ch[1] should be 5, 6, 7, 8, 200, 205.
// Data is converted to float from -1.0 to 1.0 based on int16 range.
scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
EXPECT_EQ(6, buffer.ReadFrames(6, 0, bus.get()));
EXPECT_EQ(18, buffer.frames());
VerifyResult(bus->channel(0), 4, 1.0f / kint16max, 1.0f / kint16max);
VerifyResult(bus->channel(0) + 4, 2, 100.0f / kint16max, 5.0f / kint16max);
VerifyResult(bus->channel(1), 4, 5.0f / kint16max, 1.0f / kint16max);
VerifyResult(bus->channel(1) + 4, 2, 200.0f / kint16max, 5.0f / kint16max);
}
TEST(AudioBufferQueueTest, ReadManyChannels) {
const int channels = 16;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
// Add 76 frames of data.
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatF32, channels, 0.0f, 1.0f, 6, kNoTime, kNoTime));
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatF32, channels, 6.0f * channels, 1.0f, 10, kNoTime, kNoTime));
buffer.Append(MakeAudioBuffer<float>(kSampleFormatF32,
channels,
16.0f * channels,
1.0f,
60,
kNoTime,
kNoTime));
EXPECT_EQ(76, buffer.frames());
// Read 3 frames from the buffer. F32 is interleaved, so ch[0] should be
// 1, 17, 33, and ch[1] should be 2, 18, 34. Just check a few channels.
scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
EXPECT_EQ(30, buffer.ReadFrames(30, 0, bus.get()));
EXPECT_EQ(46, buffer.frames());
for (int i = 0; i < channels; ++i) {
VerifyResult(bus->channel(i), 30, static_cast<float>(i), 16.0f);
}
}
TEST(AudioBufferQueueTest, Peek) {
const int channels = 4;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
// Add 60 frames of data.
buffer.Append(MakeAudioBuffer<float>(
kSampleFormatF32, channels, 0.0f, 1.0f, 60, kNoTime, kNoTime));
EXPECT_EQ(60, buffer.frames());
// Peek at the first 30 frames.
scoped_ptr<AudioBus> bus1 = AudioBus::Create(channels, 100);
EXPECT_EQ(60, buffer.frames());
EXPECT_EQ(60, buffer.PeekFrames(100, 0, 0, bus1.get()));
EXPECT_EQ(30, buffer.PeekFrames(30, 0, 0, bus1.get()));
EXPECT_EQ(60, buffer.frames());
// Now read the next 30 frames (which should be the same as those peeked at).
scoped_ptr<AudioBus> bus2 = AudioBus::Create(channels, 100);
EXPECT_EQ(30, buffer.ReadFrames(30, 0, bus2.get()));
for (int i = 0; i < channels; ++i) {
VerifyResult(bus1->channel(i),
30,
static_cast<float>(i),
static_cast<float>(channels));
VerifyResult(bus2->channel(i),
30,
static_cast<float>(i),
static_cast<float>(channels));
}
// Peek 10 frames forward
EXPECT_EQ(5, buffer.PeekFrames(5, 10, 0, bus1.get()));
for (int i = 0; i < channels; ++i) {
VerifyResult(bus1->channel(i),
5,
static_cast<float>(i + 40 * channels),
static_cast<float>(channels));
}
// Peek to the end of the buffer.
EXPECT_EQ(30, buffer.frames());
EXPECT_EQ(30, buffer.PeekFrames(100, 0, 0, bus1.get()));
EXPECT_EQ(30, buffer.PeekFrames(30, 0, 0, bus1.get()));
}
TEST(AudioBufferQueueTest, Time) {
const int channels = 2;
const base::TimeDelta start_time1;
const base::TimeDelta start_time2 = base::TimeDelta::FromSeconds(30);
const base::TimeDelta duration = base::TimeDelta::FromSeconds(10);
AudioBufferQueue buffer;
scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
// Add two buffers (second one added later):
// first: start=0s, duration=10s
// second: start=30s, duration=10s
buffer.Append(MakeAudioBuffer<int16>(
kSampleFormatS16, channels, 1, 1, 10, start_time1, duration));
EXPECT_EQ(10, buffer.frames());
// Check starting time.
EXPECT_EQ(start_time1, buffer.current_time());
// Read 2 frames, should be 2s in (since duration is 1s per sample).
EXPECT_EQ(2, buffer.ReadFrames(2, 0, bus.get()));
EXPECT_EQ(start_time1 + base::TimeDelta::FromSeconds(2),
buffer.current_time());
// Skip 2 frames.
buffer.SeekFrames(2);
EXPECT_EQ(start_time1 + base::TimeDelta::FromSeconds(4),
buffer.current_time());
// Add second buffer for more data.
buffer.Append(MakeAudioBuffer<int16>(
kSampleFormatS16, channels, 1, 1, 10, start_time2, duration));
EXPECT_EQ(16, buffer.frames());
// Read until almost the end of buffer1.
EXPECT_EQ(5, buffer.ReadFrames(5, 0, bus.get()));
EXPECT_EQ(start_time1 + base::TimeDelta::FromSeconds(9),
buffer.current_time());
// Read 1 value, so time moved to buffer2.
EXPECT_EQ(1, buffer.ReadFrames(1, 0, bus.get()));
EXPECT_EQ(start_time2, buffer.current_time());
// Read all 10 frames in buffer2, timestamp should be last time from buffer2.
EXPECT_EQ(10, buffer.ReadFrames(10, 0, bus.get()));
EXPECT_EQ(start_time2 + base::TimeDelta::FromSeconds(10),
buffer.current_time());
// Try to read more frames (which don't exist), timestamp should remain.
EXPECT_EQ(0, buffer.ReadFrames(5, 0, bus.get()));
EXPECT_EQ(start_time2 + base::TimeDelta::FromSeconds(10),
buffer.current_time());
}
TEST(AudioBufferQueueTest, NoTime) {
const int channels = 2;
const base::TimeDelta kNoTime = kNoTimestamp();
AudioBufferQueue buffer;
scoped_ptr<AudioBus> bus = AudioBus::Create(channels, 100);
// Add two buffers with no timestamps. Time should always be unknown.
buffer.Append(MakeAudioBuffer<int16>(
kSampleFormatS16, channels, 1, 1, 10, kNoTime, kNoTime));
buffer.Append(MakeAudioBuffer<int16>(
kSampleFormatS16, channels, 1, 1, 10, kNoTime, kNoTime));
EXPECT_EQ(20, buffer.frames());
// Check starting time.
EXPECT_EQ(kNoTime, buffer.current_time());
// Read 2 frames.
EXPECT_EQ(2, buffer.ReadFrames(2, 0, bus.get()));
EXPECT_EQ(kNoTime, buffer.current_time());
// Skip 2 frames.
buffer.SeekFrames(2);
EXPECT_EQ(kNoTime, buffer.current_time());
// Read until almost the end of buffer1.
EXPECT_EQ(5, buffer.ReadFrames(5, 0, bus.get()));
EXPECT_EQ(kNoTime, buffer.current_time());
// Read 1 value, so time moved to buffer2.
EXPECT_EQ(1, buffer.ReadFrames(1, 0, bus.get()));
EXPECT_EQ(kNoTime, buffer.current_time());
// Read all 10 frames in buffer2.
EXPECT_EQ(10, buffer.ReadFrames(10, 0, bus.get()));
EXPECT_EQ(kNoTime, buffer.current_time());
// Try to read more frames (which don't exist), timestamp should remain.
EXPECT_EQ(0, buffer.ReadFrames(5, 0, bus.get()));
EXPECT_EQ(kNoTime, buffer.current_time());
}
} // namespace media
|