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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
// Copyright (c) 2012 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/memory/scoped_ptr.h"
#include "media/base/audio_buffer.h"
#include "media/base/audio_bus.h"
#include "media/base/audio_splicer.h"
#include "media/base/audio_timestamp_helper.h"
#include "media/base/buffers.h"
#include "media/base/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
// Do not change this format. AddInput() and GetValue() only work with float.
static const SampleFormat kSampleFormat = kSampleFormatF32;
COMPILE_ASSERT(kSampleFormat == kSampleFormatF32, invalid_splice_format);
static const int kChannels = 1;
static const int kDefaultSampleRate = 44100;
static const int kDefaultBufferSize = 100;
class AudioSplicerTest : public ::testing::Test {
public:
AudioSplicerTest()
: splicer_(kDefaultSampleRate),
input_timestamp_helper_(kDefaultSampleRate) {
input_timestamp_helper_.SetBaseTimestamp(base::TimeDelta());
}
scoped_refptr<AudioBuffer> GetNextInputBuffer(float value) {
return GetNextInputBuffer(value, kDefaultBufferSize);
}
scoped_refptr<AudioBuffer> GetNextInputBuffer(float value, int frame_size) {
scoped_refptr<AudioBuffer> buffer = MakeAudioBuffer<float>(
kSampleFormat,
kChannels,
value,
0.0f,
frame_size,
input_timestamp_helper_.GetTimestamp(),
input_timestamp_helper_.GetFrameDuration(frame_size));
input_timestamp_helper_.AddFrames(frame_size);
return buffer;
}
float GetValue(const scoped_refptr<AudioBuffer>& buffer) {
return reinterpret_cast<const float*>(buffer->channel_data()[0])[0];
}
bool VerifyData(const scoped_refptr<AudioBuffer>& buffer, float value) {
int frames = buffer->frame_count();
scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, frames);
buffer->ReadFrames(frames, 0, 0, bus.get());
for (int ch = 0; ch < buffer->channel_count(); ++ch) {
for (int i = 0; i < frames; ++i) {
if (bus->channel(ch)[i] != value)
return false;
}
}
return true;
}
void VerifyNextBuffer(const scoped_refptr<AudioBuffer>& input) {
ASSERT_TRUE(splicer_.HasNextBuffer());
scoped_refptr<AudioBuffer> output = splicer_.GetNextBuffer();
EXPECT_EQ(input->timestamp(), output->timestamp());
EXPECT_EQ(input->duration(), output->duration());
EXPECT_EQ(input->frame_count(), output->frame_count());
EXPECT_TRUE(VerifyData(output, GetValue(input)));
}
void VerifyPreSpliceOutput(
const scoped_refptr<AudioBuffer>& overlapped_buffer,
const scoped_refptr<AudioBuffer>& overlapping_buffer,
int expected_pre_splice_size,
base::TimeDelta expected_pre_splice_duration) {
ASSERT_TRUE(splicer_.HasNextBuffer());
scoped_refptr<AudioBuffer> pre_splice_output = splicer_.GetNextBuffer();
EXPECT_EQ(overlapped_buffer->timestamp(), pre_splice_output->timestamp());
EXPECT_EQ(expected_pre_splice_size, pre_splice_output->frame_count());
EXPECT_EQ(expected_pre_splice_duration, pre_splice_output->duration());
EXPECT_TRUE(VerifyData(pre_splice_output, GetValue(overlapped_buffer)));
}
void VerifyCrossfadeOutput(
const scoped_refptr<AudioBuffer>& overlapped_buffer,
const scoped_refptr<AudioBuffer>& overlapping_buffer,
int expected_crossfade_size,
base::TimeDelta expected_crossfade_duration) {
ASSERT_TRUE(splicer_.HasNextBuffer());
scoped_refptr<AudioBuffer> crossfade_output = splicer_.GetNextBuffer();
EXPECT_EQ(expected_crossfade_size, crossfade_output->frame_count());
EXPECT_EQ(expected_crossfade_duration, crossfade_output->duration());
// The splice timestamp may be adjusted by a microsecond.
EXPECT_NEAR(overlapping_buffer->timestamp().InMicroseconds(),
crossfade_output->timestamp().InMicroseconds(),
1);
// Verify the actual crossfade.
const int frames = crossfade_output->frame_count();
const float overlapped_value = GetValue(overlapped_buffer);
const float overlapping_value = GetValue(overlapping_buffer);
scoped_ptr<AudioBus> bus = AudioBus::Create(kChannels, frames);
crossfade_output->ReadFrames(frames, 0, 0, bus.get());
for (int ch = 0; ch < crossfade_output->channel_count(); ++ch) {
float cf_ratio = 0;
const float cf_increment = 1.0f / frames;
for (int i = 0; i < frames; ++i, cf_ratio += cf_increment) {
const float actual = bus->channel(ch)[i];
const float expected =
(1.0f - cf_ratio) * overlapped_value + cf_ratio * overlapping_value;
ASSERT_FLOAT_EQ(expected, actual) << "i=" << i;
}
}
}
bool AddInput(const scoped_refptr<AudioBuffer>& input) {
// Since the splicer doesn't make copies it's working directly on the input
// buffers. We must make a copy before adding to ensure the original buffer
// is not modified in unexpected ways.
scoped_refptr<AudioBuffer> buffer_copy =
input->end_of_stream()
? AudioBuffer::CreateEOSBuffer()
: AudioBuffer::CopyFrom(kSampleFormat,
input->channel_count(),
input->frame_count(),
&input->channel_data()[0],
input->timestamp(),
input->duration());
return splicer_.AddInput(buffer_copy);
}
base::TimeDelta max_crossfade_duration() {
return splicer_.max_crossfade_duration_;
}
protected:
AudioSplicer splicer_;
AudioTimestampHelper input_timestamp_helper_;
DISALLOW_COPY_AND_ASSIGN(AudioSplicerTest);
};
TEST_F(AudioSplicerTest, PassThru) {
EXPECT_FALSE(splicer_.HasNextBuffer());
// Test single buffer pass-thru behavior.
scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
EXPECT_TRUE(AddInput(input_1));
VerifyNextBuffer(input_1);
EXPECT_FALSE(splicer_.HasNextBuffer());
// Test that multiple buffers can be queued in the splicer.
scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f);
scoped_refptr<AudioBuffer> input_3 = GetNextInputBuffer(0.3f);
EXPECT_TRUE(AddInput(input_2));
EXPECT_TRUE(AddInput(input_3));
VerifyNextBuffer(input_2);
VerifyNextBuffer(input_3);
EXPECT_FALSE(splicer_.HasNextBuffer());
}
TEST_F(AudioSplicerTest, Reset) {
scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
EXPECT_TRUE(AddInput(input_1));
ASSERT_TRUE(splicer_.HasNextBuffer());
splicer_.Reset();
EXPECT_FALSE(splicer_.HasNextBuffer());
// Add some bytes to the timestamp helper so that the
// next buffer starts many frames beyond the end of
// |input_1|. This is to make sure that Reset() actually
// clears its state and doesn't try to insert a gap.
input_timestamp_helper_.AddFrames(100);
// Verify that a new input buffer passes through as expected.
scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f);
EXPECT_TRUE(AddInput(input_2));
ASSERT_TRUE(splicer_.HasNextBuffer());
VerifyNextBuffer(input_2);
EXPECT_FALSE(splicer_.HasNextBuffer());
}
TEST_F(AudioSplicerTest, EndOfStream) {
scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
scoped_refptr<AudioBuffer> input_2 = AudioBuffer::CreateEOSBuffer();
scoped_refptr<AudioBuffer> input_3 = GetNextInputBuffer(0.2f);
EXPECT_TRUE(input_2->end_of_stream());
EXPECT_TRUE(AddInput(input_1));
EXPECT_TRUE(AddInput(input_2));
VerifyNextBuffer(input_1);
scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer();
EXPECT_FALSE(splicer_.HasNextBuffer());
EXPECT_TRUE(output_2->end_of_stream());
// Verify that buffers can be added again after Reset().
splicer_.Reset();
EXPECT_TRUE(AddInput(input_3));
VerifyNextBuffer(input_3);
EXPECT_FALSE(splicer_.HasNextBuffer());
}
// Test the gap insertion code.
// +--------------+ +--------------+
// |11111111111111| |22222222222222|
// +--------------+ +--------------+
// Results in:
// +--------------+----+--------------+
// |11111111111111|0000|22222222222222|
// +--------------+----+--------------+
TEST_F(AudioSplicerTest, GapInsertion) {
scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
// Add bytes to the timestamp helper so that the next buffer
// will have a starting timestamp that indicates a gap is
// present.
const int kGapSize = 7;
input_timestamp_helper_.AddFrames(kGapSize);
scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f);
EXPECT_TRUE(AddInput(input_1));
EXPECT_TRUE(AddInput(input_2));
// Verify that the first input buffer passed through unmodified.
VerifyNextBuffer(input_1);
// Verify the contents of the gap buffer.
scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer();
base::TimeDelta gap_timestamp =
input_1->timestamp() + input_1->duration();
base::TimeDelta gap_duration = input_2->timestamp() - gap_timestamp;
EXPECT_GT(gap_duration, base::TimeDelta());
EXPECT_EQ(gap_timestamp, output_2->timestamp());
EXPECT_EQ(gap_duration, output_2->duration());
EXPECT_EQ(kGapSize, output_2->frame_count());
EXPECT_TRUE(VerifyData(output_2, 0.0f));
// Verify that the second input buffer passed through unmodified.
VerifyNextBuffer(input_2);
EXPECT_FALSE(splicer_.HasNextBuffer());
}
// Test that an error is signalled when the gap between input buffers is
// too large.
TEST_F(AudioSplicerTest, GapTooLarge) {
scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
// Add a seconds worth of bytes so that an unacceptably large
// gap exists between |input_1| and |input_2|.
const int kGapSize = kDefaultSampleRate;
input_timestamp_helper_.AddFrames(kGapSize);
scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f);
EXPECT_TRUE(AddInput(input_1));
EXPECT_FALSE(AddInput(input_2));
VerifyNextBuffer(input_1);
// Verify that the second buffer is not available.
EXPECT_FALSE(splicer_.HasNextBuffer());
// Reset the timestamp helper so it can generate a buffer that is
// right after |input_1|.
input_timestamp_helper_.SetBaseTimestamp(
input_1->timestamp() + input_1->duration());
// Verify that valid buffers are still accepted.
scoped_refptr<AudioBuffer> input_3 = GetNextInputBuffer(0.3f);
EXPECT_TRUE(AddInput(input_3));
VerifyNextBuffer(input_3);
EXPECT_FALSE(splicer_.HasNextBuffer());
}
// Verifies that an error is signalled if AddInput() is called
// with a timestamp that is earlier than the first buffer added.
TEST_F(AudioSplicerTest, BufferAddedBeforeBase) {
input_timestamp_helper_.SetBaseTimestamp(
base::TimeDelta::FromMicroseconds(10));
scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
// Reset the timestamp helper so the next buffer will have a timestamp earlier
// than |input_1|.
input_timestamp_helper_.SetBaseTimestamp(base::TimeDelta::FromSeconds(0));
scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.1f);
EXPECT_GT(input_1->timestamp(), input_2->timestamp());
EXPECT_TRUE(AddInput(input_1));
EXPECT_FALSE(AddInput(input_2));
}
// Test when one buffer partially overlaps another.
// +--------------+
// |11111111111111|
// +--------------+
// +--------------+
// |22222222222222|
// +--------------+
// Results in:
// +--------------+----------+
// |11111111111111|2222222222|
// +--------------+----------+
TEST_F(AudioSplicerTest, PartialOverlap) {
scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
// Reset timestamp helper so that the next buffer will have a
// timestamp that starts in the middle of |input_1|.
const int kOverlapSize = input_1->frame_count() / 4;
input_timestamp_helper_.SetBaseTimestamp(input_1->timestamp());
input_timestamp_helper_.AddFrames(input_1->frame_count() - kOverlapSize);
scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f);
EXPECT_TRUE(AddInput(input_1));
EXPECT_TRUE(AddInput(input_2));
// Verify that the first input buffer passed through unmodified.
VerifyNextBuffer(input_1);
ASSERT_TRUE(splicer_.HasNextBuffer());
scoped_refptr<AudioBuffer> output_2 = splicer_.GetNextBuffer();
EXPECT_FALSE(splicer_.HasNextBuffer());
// Verify that the second input buffer was truncated to only contain
// the samples that are after the end of |input_1|.
base::TimeDelta expected_timestamp =
input_1->timestamp() + input_1->duration();
base::TimeDelta expected_duration =
(input_2->timestamp() + input_2->duration()) - expected_timestamp;
EXPECT_EQ(expected_timestamp, output_2->timestamp());
EXPECT_EQ(expected_duration, output_2->duration());
EXPECT_TRUE(VerifyData(output_2, GetValue(input_2)));
}
// Test that an input buffer that is completely overlapped by a buffer
// that was already added is dropped.
// +--------------+
// |11111111111111|
// +--------------+
// +-----+
// |22222|
// +-----+
// +-------------+
// |3333333333333|
// +-------------+
// Results in:
// +--------------+-------------+
// |11111111111111|3333333333333|
// +--------------+-------------+
TEST_F(AudioSplicerTest, DropBuffer) {
scoped_refptr<AudioBuffer> input_1 = GetNextInputBuffer(0.1f);
// Reset timestamp helper so that the next buffer will have a
// timestamp that starts in the middle of |input_1|.
const int kOverlapOffset = input_1->frame_count() / 2;
const int kOverlapSize = input_1->frame_count() / 4;
input_timestamp_helper_.SetBaseTimestamp(input_1->timestamp());
input_timestamp_helper_.AddFrames(kOverlapOffset);
scoped_refptr<AudioBuffer> input_2 = GetNextInputBuffer(0.2f, kOverlapSize);
// Reset the timestamp helper so the next buffer will be right after
// |input_1|.
input_timestamp_helper_.SetBaseTimestamp(input_1->timestamp());
input_timestamp_helper_.AddFrames(input_1->frame_count());
scoped_refptr<AudioBuffer> input_3 = GetNextInputBuffer(0.3f);
EXPECT_TRUE(AddInput(input_1));
EXPECT_TRUE(AddInput(input_2));
EXPECT_TRUE(AddInput(input_3));
VerifyNextBuffer(input_1);
VerifyNextBuffer(input_3);
EXPECT_FALSE(splicer_.HasNextBuffer());
}
// Test crossfade when one buffer partially overlaps another.
// +--------------+
// |11111111111111|
// +--------------+
// +--------------+
// |22222222222222|
// +--------------+
// Results in:
// +----------+----+----------+
// |1111111111|xxxx|2222222222|
// +----------+----+----------+
// Where "xxxx" represents the crossfaded portion of the signal.
TEST_F(AudioSplicerTest, PartialOverlapCrossfade) {
const int kCrossfadeSize =
input_timestamp_helper_.GetFramesToTarget(max_crossfade_duration());
const int kBufferSize = kCrossfadeSize * 2;
scoped_refptr<AudioBuffer> extra_pre_splice_buffer =
GetNextInputBuffer(0.2f, kBufferSize);
scoped_refptr<AudioBuffer> overlapped_buffer =
GetNextInputBuffer(1.0f, kBufferSize);
// Reset timestamp helper so that the next buffer will have a timestamp that
// starts in the middle of |overlapped_buffer|.
input_timestamp_helper_.SetBaseTimestamp(overlapped_buffer->timestamp());
input_timestamp_helper_.AddFrames(overlapped_buffer->frame_count() -
kCrossfadeSize);
splicer_.SetSpliceTimestamp(input_timestamp_helper_.GetTimestamp());
scoped_refptr<AudioBuffer> overlapping_buffer =
GetNextInputBuffer(0.0f, kBufferSize);
// |extra_pre_splice_buffer| is entirely before the splice and should be ready
// for output.
EXPECT_TRUE(AddInput(extra_pre_splice_buffer));
VerifyNextBuffer(extra_pre_splice_buffer);
// The splicer should be internally queuing input since |overlapped_buffer| is
// part of the splice.
EXPECT_TRUE(AddInput(overlapped_buffer));
EXPECT_FALSE(splicer_.HasNextBuffer());
// |overlapping_buffer| should complete the splice, so ensure output is now
// available.
EXPECT_TRUE(AddInput(overlapping_buffer));
ASSERT_TRUE(splicer_.HasNextBuffer());
// Add one more buffer to make sure it's passed through untouched.
scoped_refptr<AudioBuffer> extra_post_splice_buffer =
GetNextInputBuffer(0.5f, kBufferSize);
EXPECT_TRUE(AddInput(extra_post_splice_buffer));
VerifyPreSpliceOutput(overlapped_buffer,
overlapping_buffer,
221,
base::TimeDelta::FromMicroseconds(5012));
// Due to rounding the crossfade size may vary by up to a frame.
const int kExpectedCrossfadeSize = 220;
EXPECT_NEAR(kExpectedCrossfadeSize, kCrossfadeSize, 1);
VerifyCrossfadeOutput(overlapped_buffer,
overlapping_buffer,
kExpectedCrossfadeSize,
base::TimeDelta::FromMicroseconds(4988));
// Retrieve the remaining portion after crossfade.
ASSERT_TRUE(splicer_.HasNextBuffer());
scoped_refptr<AudioBuffer> post_splice_output = splicer_.GetNextBuffer();
EXPECT_EQ(base::TimeDelta::FromMicroseconds(20022),
post_splice_output->timestamp());
EXPECT_EQ(overlapping_buffer->frame_count() - kExpectedCrossfadeSize,
post_splice_output->frame_count());
EXPECT_EQ(base::TimeDelta::FromMicroseconds(5034),
post_splice_output->duration());
EXPECT_TRUE(VerifyData(post_splice_output, GetValue(overlapping_buffer)));
VerifyNextBuffer(extra_post_splice_buffer);
EXPECT_FALSE(splicer_.HasNextBuffer());
}
// Test crossfade when one buffer partially overlaps another, but an end of
// stream buffer is received before the crossfade duration is reached.
// +--------------+
// |11111111111111|
// +--------------+
// +---------++---+
// |222222222||EOS|
// +---------++---+
// Results in:
// +----------+----+----++---+
// |1111111111|xxxx|2222||EOS|
// +----------+----+----++---+
// Where "x" represents the crossfaded portion of the signal.
TEST_F(AudioSplicerTest, PartialOverlapCrossfadeEndOfStream) {
const int kCrossfadeSize =
input_timestamp_helper_.GetFramesToTarget(max_crossfade_duration());
scoped_refptr<AudioBuffer> overlapped_buffer =
GetNextInputBuffer(1.0f, kCrossfadeSize * 2);
// Reset timestamp helper so that the next buffer will have a timestamp that
// starts 3/4 of the way into |overlapped_buffer|.
input_timestamp_helper_.SetBaseTimestamp(overlapped_buffer->timestamp());
input_timestamp_helper_.AddFrames(3 * overlapped_buffer->frame_count() / 4);
splicer_.SetSpliceTimestamp(input_timestamp_helper_.GetTimestamp());
scoped_refptr<AudioBuffer> overlapping_buffer =
GetNextInputBuffer(0.0f, kCrossfadeSize / 3);
// The splicer should be internally queuing input since |overlapped_buffer| is
// part of the splice.
EXPECT_TRUE(AddInput(overlapped_buffer));
EXPECT_FALSE(splicer_.HasNextBuffer());
// |overlapping_buffer| should not have enough data to complete the splice, so
// ensure output is not available.
EXPECT_TRUE(AddInput(overlapping_buffer));
EXPECT_FALSE(splicer_.HasNextBuffer());
// Now add an EOS buffer which should complete the splice.
EXPECT_TRUE(AddInput(AudioBuffer::CreateEOSBuffer()));
ASSERT_TRUE(splicer_.HasNextBuffer());
VerifyPreSpliceOutput(overlapped_buffer,
overlapping_buffer,
331,
base::TimeDelta::FromMicroseconds(7505));
VerifyCrossfadeOutput(overlapped_buffer,
overlapping_buffer,
overlapping_buffer->frame_count(),
overlapping_buffer->duration());
// Ensure the last buffer is an EOS buffer.
ASSERT_TRUE(splicer_.HasNextBuffer());
scoped_refptr<AudioBuffer> post_splice_output = splicer_.GetNextBuffer();
EXPECT_TRUE(post_splice_output->end_of_stream());
EXPECT_FALSE(splicer_.HasNextBuffer());
}
// Test crossfade when one buffer partially overlaps another, but the amount of
// overlapped data is less than the crossfade duration.
// +------------+
// |111111111111|
// +------------+
// +--------------+
// |22222222222222|
// +--------------+
// Results in:
// +----------+-+------------+
// |1111111111|x|222222222222|
// +----------+-+------------+
// Where "x" represents the crossfaded portion of the signal.
TEST_F(AudioSplicerTest, PartialOverlapCrossfadeShortPreSplice) {
const int kCrossfadeSize =
input_timestamp_helper_.GetFramesToTarget(max_crossfade_duration());
scoped_refptr<AudioBuffer> overlapped_buffer =
GetNextInputBuffer(1.0f, kCrossfadeSize / 2);
// Reset timestamp helper so that the next buffer will have a timestamp that
// starts in the middle of |overlapped_buffer|.
input_timestamp_helper_.SetBaseTimestamp(overlapped_buffer->timestamp());
input_timestamp_helper_.AddFrames(overlapped_buffer->frame_count() / 2);
splicer_.SetSpliceTimestamp(input_timestamp_helper_.GetTimestamp());
scoped_refptr<AudioBuffer> overlapping_buffer =
GetNextInputBuffer(0.0f, kCrossfadeSize * 2);
// The splicer should be internally queuing input since |overlapped_buffer| is
// part of the splice.
EXPECT_TRUE(AddInput(overlapped_buffer));
EXPECT_FALSE(splicer_.HasNextBuffer());
// |overlapping_buffer| should complete the splice, so ensure output is now
// available.
EXPECT_TRUE(AddInput(overlapping_buffer));
ASSERT_TRUE(splicer_.HasNextBuffer());
const int kExpectedPreSpliceSize = 55;
const base::TimeDelta kExpectedPreSpliceDuration =
base::TimeDelta::FromMicroseconds(1247);
VerifyPreSpliceOutput(overlapped_buffer,
overlapping_buffer,
kExpectedPreSpliceSize,
kExpectedPreSpliceDuration);
VerifyCrossfadeOutput(overlapped_buffer,
overlapping_buffer,
kExpectedPreSpliceSize,
kExpectedPreSpliceDuration);
// Retrieve the remaining portion after crossfade.
ASSERT_TRUE(splicer_.HasNextBuffer());
scoped_refptr<AudioBuffer> post_splice_output = splicer_.GetNextBuffer();
EXPECT_EQ(overlapping_buffer->timestamp() + kExpectedPreSpliceDuration,
post_splice_output->timestamp());
EXPECT_EQ(overlapping_buffer->frame_count() - kExpectedPreSpliceSize,
post_splice_output->frame_count());
EXPECT_EQ(overlapping_buffer->duration() - kExpectedPreSpliceDuration,
post_splice_output->duration());
EXPECT_TRUE(VerifyData(post_splice_output, GetValue(overlapping_buffer)));
EXPECT_FALSE(splicer_.HasNextBuffer());
}
} // namespace media
|