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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
media / audio / audio_encoders_unittest.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include <cstring>
#include <limits>
#include <memory>
#include <utility>
#include <vector>
#include "base/location.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "base/win/windows_version.h"
#include "build/build_config.h"
#include "media/audio/audio_opus_encoder.h"
#include "media/audio/simple_sources.h"
#include "media/base/audio_encoder.h"
#include "media/base/audio_timestamp_helper.h"
#include "media/base/converting_audio_fifo.h"
#include "media/base/status.h"
#include "media/media_buildflags.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/opus/src/include/opus.h"
#if BUILDFLAG(IS_WIN)
#include "base/win/scoped_com_initializer.h"
#include "media/gpu/windows/mf_audio_encoder.h"
#define HAS_AAC_ENCODER 1
#endif // IS_WIN
#if BUILDFLAG(IS_MAC) && BUILDFLAG(USE_PROPRIETARY_CODECS)
#include "media/filters/mac/audio_toolbox_audio_encoder.h"
#define HAS_AAC_ENCODER 1
#endif
#if BUILDFLAG(IS_ANDROID) && BUILDFLAG(USE_PROPRIETARY_CODECS)
#include "media/gpu/android/ndk_audio_encoder.h"
#define HAS_AAC_ENCODER 1
#endif
#if HAS_AAC_ENCODER
#include "media/base/audio_decoder.h"
#include "media/base/channel_layout.h"
#include "media/base/decoder_status.h"
#include "media/base/mock_media_log.h"
#include "media/filters/ffmpeg_audio_decoder.h"
#endif
namespace media {
namespace {
constexpr int kAudioSampleRateWithDelay = 647744;
// This is the preferred opus buffer duration (20 ms), which corresponds to a
// value of 960 frames per buffer at a sample rate of 48 khz.
constexpr base::TimeDelta kOpusBufferDuration = base::Milliseconds(20);
#if HAS_AAC_ENCODER
// AAC puts 1024 PCM samples into each AAC frame, which corresponds to a
// duration of 21 and 1/3 milliseconds at a sample rate of 48 khz.
constexpr int kAacFramesPerBuffer = 1024;
#endif // HAS_AAC_ENCODER
struct TestAudioParams {
const AudioCodec codec;
const int channels;
const int sample_rate;
};
constexpr TestAudioParams kTestAudioParamsOpus[] = {
{AudioCodec::kOpus, 2, 48000},
// Change to mono:
{AudioCodec::kOpus, 1, 48000},
// Different sampling rate as well:
{AudioCodec::kOpus, 1, 24000},
{AudioCodec::kOpus, 2, 8000},
// Using a non-default Opus sampling rate (48, 24, 16, 12, or 8 kHz).
{AudioCodec::kOpus, 1, 22050},
{AudioCodec::kOpus, 2, 44100},
{AudioCodec::kOpus, 2, 96000},
{AudioCodec::kOpus, 2, kAudioSampleRateWithDelay},
};
#if HAS_AAC_ENCODER
constexpr TestAudioParams kTestAudioParamsAAC[] = {
{AudioCodec::kAAC, 2, 48000}, {AudioCodec::kAAC, 6, 48000},
{AudioCodec::kAAC, 1, 48000}, {AudioCodec::kAAC, 2, 44100},
{AudioCodec::kAAC, 6, 44100}, {AudioCodec::kAAC, 1, 44100},
};
#endif // HAS_AAC_ENCODER
std::string EncoderStatusCodeToString(EncoderStatus::Codes code) {
switch (code) {
case EncoderStatus::Codes::kOk:
return "kOk";
case EncoderStatus::Codes::kEncoderInitializeNeverCompleted:
return "kEncoderInitializeNeverCompleted";
case EncoderStatus::Codes::kEncoderInitializeTwice:
return "kEncoderInitializeTwice";
case EncoderStatus::Codes::kEncoderFailedEncode:
return "kEncoderFailedEncode";
case EncoderStatus::Codes::kEncoderUnsupportedProfile:
return "kEncoderUnsupportedProfile";
case EncoderStatus::Codes::kEncoderUnsupportedCodec:
return "kEncoderUnsupportedCodec";
case EncoderStatus::Codes::kEncoderUnsupportedConfig:
return "kEncoderUnsupportedConfig";
case EncoderStatus::Codes::kEncoderInitializationError:
return "kEncoderInitializationError";
case EncoderStatus::Codes::kEncoderFailedFlush:
return "kEncoderFailedFlush";
case EncoderStatus::Codes::kEncoderMojoConnectionError:
return "kEncoderMojoConnectionError";
default:
NOTREACHED();
}
}
bool TimesAreNear(base::TimeTicks t1,
base::TimeTicks t2,
base::TimeDelta error) {
return (t1 - t2).magnitude() <= error;
}
} // namespace
class AudioEncodersTest : public ::testing::TestWithParam<TestAudioParams> {
public:
AudioEncodersTest()
: audio_source_(GetParam().channels,
/*freq=*/440,
GetParam().sample_rate) {
options_.codec = GetParam().codec;
options_.sample_rate = GetParam().sample_rate;
options_.channels = GetParam().channels;
expected_duration_helper_ =
std::make_unique<AudioTimestampHelper>(options_.sample_rate);
expected_duration_helper_->SetBaseTimestamp(base::Microseconds(0));
}
AudioEncodersTest(const AudioEncodersTest&) = delete;
AudioEncodersTest& operator=(const AudioEncodersTest&) = delete;
~AudioEncodersTest() override = default;
using MaybeDesc = std::optional<AudioEncoder::CodecDescription>;
AudioEncoder* encoder() const { return encoder_.get(); }
bool EncoderHasDelay() const {
return options_.sample_rate == kAudioSampleRateWithDelay;
}
void SetUp() override { CreateEncoder(); }
void CreateEncoder() {
if (options_.codec == AudioCodec::kOpus) {
encoder_ = std::make_unique<AudioOpusEncoder>();
buffer_duration_ = kOpusBufferDuration;
frames_per_buffer_ = AudioTimestampHelper::TimeToFrames(
buffer_duration_, options_.sample_rate);
} else if (options_.codec == AudioCodec::kAAC) {
#if BUILDFLAG(IS_WIN) && HAS_AAC_ENCODER
if ((base::win::OSInfo::GetInstance()->version() ==
base::win::Version::WIN11_22H2 ||
base::win::OSInfo::GetInstance()->version() ==
base::win::Version::WIN11_23H2) &&
base::win::OSInfo::GetInstance()->version_number().patch < 4112) {
GTEST_SKIP() << "https://crbug.com/325249353: AAC encoder requires "
"a fix in Win11 patch 4112.";
// GTEST_SKIP() returns.
}
EXPECT_TRUE(com_initializer_.Succeeded());
ASSERT_TRUE(base::SequencedTaskRunner::HasCurrentDefault());
encoder_ = std::make_unique<MFAudioEncoder>(
base::SequencedTaskRunner::GetCurrentDefault());
frames_per_buffer_ = kAacFramesPerBuffer;
buffer_duration_ = AudioTimestampHelper::FramesToTime(
frames_per_buffer_, options_.sample_rate);
#elif HAS_AAC_ENCODER && BUILDFLAG(IS_MAC)
encoder_ = std::make_unique<AudioToolboxAudioEncoder>();
frames_per_buffer_ = kAacFramesPerBuffer;
buffer_duration_ = AudioTimestampHelper::FramesToTime(
frames_per_buffer_, options_.sample_rate);
#elif HAS_AAC_ENCODER && BUILDFLAG(IS_ANDROID)
if (__builtin_available(android NDK_MEDIA_CODEC_MIN_API, *)) {
encoder_ = std::make_unique<NdkAudioEncoder>(
base::SequencedTaskRunner::GetCurrentDefault());
frames_per_buffer_ = kAacFramesPerBuffer;
buffer_duration_ = AudioTimestampHelper::FramesToTime(
frames_per_buffer_, options_.sample_rate);
} else {
GTEST_SKIP() << "NDK AAC encoder not supported. Skipping test.";
// GTEST_SKIP() returns.
}
#else
NOTREACHED();
#endif
} else {
NOTREACHED();
}
min_number_input_frames_needed_ = frames_per_buffer_;
}
void InitializeEncoder(
AudioEncoder::OutputCB output_cb = base::NullCallback()) {
if (!output_cb) {
output_cb =
base::BindLambdaForTesting([&](EncodedAudioBuffer output, MaybeDesc) {
observed_output_duration_ += output.duration;
});
}
bool called_done = false;
AudioEncoder::EncoderStatusCB done_cb =
base::BindLambdaForTesting([&](EncoderStatus error) {
if (!error.is_ok()) {
FAIL() << "Error code: " << EncoderStatusCodeToString(error.code())
<< "\nError message: " << error.message();
}
called_done = true;
});
encoder_->Initialize(options_, std::move(output_cb), std::move(done_cb));
task_environment_.RunUntilIdle();
EXPECT_TRUE(called_done);
if (options_.codec == AudioCodec::kOpus) {
min_number_input_frames_needed_ =
reinterpret_cast<AudioOpusEncoder*>(encoder_.get())
->fifo_->min_number_input_frames_needed_for_testing();
}
}
// Produces an audio data with |num_frames| frames. The produced data is sent
// to |encoder_| to be encoded, and the number of frames generated is
// returned.
int ProduceAudioAndEncode(
base::TimeTicks timestamp = base::TimeTicks::Now(),
int num_frames = 0,
AudioEncoder::EncoderStatusCB done_cb = base::NullCallback()) {
DCHECK(encoder_);
if (num_frames == 0)
num_frames = frames_per_buffer_;
auto audio_bus = AudioBus::Create(options_.channels, num_frames);
audio_source_.OnMoreData(base::TimeDelta(), timestamp, {}, audio_bus.get());
DoEncode(std::move(audio_bus), timestamp, std::move(done_cb));
return num_frames;
}
void DoEncode(std::unique_ptr<AudioBus> audio_bus,
base::TimeTicks timestamp,
AudioEncoder::EncoderStatusCB done_cb = base::NullCallback()) {
if (!done_cb) {
pending_callback_results_.emplace_back();
done_cb = base::BindLambdaForTesting([&](EncoderStatus error) {
if (!error.is_ok()) {
FAIL() << "Error code: " << EncoderStatusCodeToString(error.code())
<< "\nError message: " << error.message();
}
pending_callback_results_[pending_callback_count_].status_code =
error.code();
pending_callback_results_[pending_callback_count_].completed = true;
pending_callback_count_++;
});
}
int num_frames = audio_bus->frames();
encoder_->Encode(std::move(audio_bus), timestamp, std::move(done_cb));
expected_output_duration_ +=
expected_duration_helper_->GetFrameDuration(num_frames);
expected_duration_helper_->AddFrames(num_frames);
}
void FlushAndVerifyStatus(
EncoderStatus::Codes status_code = EncoderStatus::Codes::kOk) {
base::RunLoop run_loop;
bool flush_done = false;
auto flush_done_cb = base::BindLambdaForTesting([&](EncoderStatus error) {
if (error.code() != status_code) {
FAIL() << "Expected " << EncoderStatusCodeToString(status_code)
<< " but got " << EncoderStatusCodeToString(error.code());
}
flush_done = true;
});
encoder()->Flush(
std::move(flush_done_cb).Then(run_loop.QuitWhenIdleClosure()));
run_loop.Run();
EXPECT_TRUE(flush_done);
}
void ValidateDoneCallbacksRun() {
for (auto callback_result : pending_callback_results_) {
EXPECT_TRUE(callback_result.completed);
EXPECT_EQ(callback_result.status_code, EncoderStatus::Codes::kOk);
}
}
// The amount of front padding that the encoder emits.
size_t GetExpectedPadding() {
#if BUILDFLAG(IS_MAC)
if (options_.codec == AudioCodec::kAAC)
return 2112;
#endif
return 0;
}
void ValidateOutputDuration(int64_t flush_count = 1) {
// Since encoders can only output buffers of size `frames_per_buffer_`, the
// number of outputs will be larger than the number of inputs.
int64_t frame_remainder =
frames_per_buffer_ -
(expected_duration_helper_->frame_count() % frames_per_buffer_);
int64_t amount_of_padding = GetExpectedPadding() + frame_remainder;
// Padding is re-emitted after each flush.
amount_of_padding *= flush_count;
int64_t number_of_outputs = std::ceil(
(expected_duration_helper_->frame_count() + amount_of_padding) /
static_cast<double>(frames_per_buffer_));
int64_t duration_of_padding_us =
number_of_outputs * AudioTimestampHelper::FramesToTime(
frames_per_buffer_, options_.sample_rate)
.InMicroseconds();
int64_t acceptable_diff = duration_of_padding_us + 10;
EXPECT_NEAR(expected_output_duration_.InMicroseconds(),
observed_output_duration_.InMicroseconds(), acceptable_diff);
}
base::test::TaskEnvironment task_environment_;
#if BUILDFLAG(IS_WIN)
::base::win::ScopedCOMInitializer com_initializer_;
#endif // BUILDFLAG(IS_WIN)
// The input params as initialized from the test's parameter.
AudioEncoder::Options options_;
// The audio source used to generate data to give to the |encoder_|.
SineWaveAudioSource audio_source_;
// The encoder the test is verifying.
std::unique_ptr<AudioEncoder> encoder_;
base::TimeDelta buffer_duration_;
int frames_per_buffer_;
int min_number_input_frames_needed_;
std::unique_ptr<AudioTimestampHelper> expected_duration_helper_;
base::TimeDelta expected_output_duration_;
base::TimeDelta observed_output_duration_;
struct CallbackResult {
bool completed = false;
EncoderStatus::Codes status_code;
};
int pending_callback_count_ = 0;
std::vector<CallbackResult> pending_callback_results_;
};
TEST_P(AudioEncodersTest, InitializeTwice) {
InitializeEncoder();
bool called_done = false;
auto done_cb = base::BindLambdaForTesting([&](EncoderStatus error) {
if (error.code() != EncoderStatus::Codes::kEncoderInitializeTwice)
FAIL() << "Expected kEncoderInitializeTwice error but got "
<< EncoderStatusCodeToString(error.code());
called_done = true;
});
encoder_->Initialize(options_, base::DoNothing(), std::move(done_cb));
task_environment_.RunUntilIdle();
EXPECT_TRUE(called_done);
}
TEST_P(AudioEncodersTest, StopCallbackWrapping) {
bool called_done = false;
AudioEncoder::EncoderStatusCB done_cb = base::BindLambdaForTesting(
[&](EncoderStatus error) { called_done = true; });
encoder_->DisablePostedCallbacks();
encoder_->Initialize(options_, base::DoNothing(), std::move(done_cb));
EXPECT_TRUE(called_done);
}
TEST_P(AudioEncodersTest, EncodeWithoutInitialize) {
bool called_done = false;
auto done_cb = base::BindLambdaForTesting([&](EncoderStatus error) {
if (error.code() != EncoderStatus::Codes::kEncoderInitializeNeverCompleted)
FAIL() << "Expected kEncoderInitializeNeverCompleted error but got "
<< EncoderStatusCodeToString(error.code());
called_done = true;
});
auto audio_bus = AudioBus::Create(options_.channels, /*frames=*/1);
encoder()->Encode(std::move(audio_bus), base::TimeTicks::Now(),
std::move(done_cb));
task_environment_.RunUntilIdle();
EXPECT_TRUE(called_done);
}
TEST_P(AudioEncodersTest, FlushWithoutInitialize) {
FlushAndVerifyStatus(EncoderStatus::Codes::kEncoderInitializeNeverCompleted);
}
TEST_P(AudioEncodersTest, FlushWithNoInput) {
InitializeEncoder();
FlushAndVerifyStatus();
}
TEST_P(AudioEncodersTest, EncodeAndFlush) {
if (EncoderHasDelay())
return;
InitializeEncoder();
ProduceAudioAndEncode();
ProduceAudioAndEncode();
ProduceAudioAndEncode();
FlushAndVerifyStatus();
ValidateDoneCallbacksRun();
ValidateOutputDuration();
}
TEST_P(AudioEncodersTest, EncodeAndFlushTwice) {
if (EncoderHasDelay())
return;
InitializeEncoder();
constexpr int kEncodeFlushCycles = 2;
for (int cycle = 0; cycle < kEncodeFlushCycles; ++cycle) {
ProduceAudioAndEncode();
ProduceAudioAndEncode();
ProduceAudioAndEncode();
{
base::RunLoop run_loop;
bool called_flush = false;
auto flush_cb = base::BindLambdaForTesting([&](EncoderStatus error) {
if (error.code() != EncoderStatus::Codes::kOk) {
FAIL() << "Expected kOk but got "
<< EncoderStatusCodeToString(error.code());
}
called_flush = true;
});
encoder()->Flush(
std::move(flush_cb).Then(run_loop.QuitWhenIdleClosure()));
run_loop.Run();
EXPECT_TRUE(called_flush);
}
}
ValidateDoneCallbacksRun();
ValidateOutputDuration(/*flush_count=*/kEncodeFlushCycles);
}
// Instead of synchronously calling `Encode`, wait until `done_cb` is invoked
// before we provide more input.
TEST_P(AudioEncodersTest, ProvideInputAfterDoneCb) {
if (EncoderHasDelay())
return;
InitializeEncoder();
bool called_done = false;
auto done_lambda = [&](EncoderStatus error) {
if (error.code() != EncoderStatus::Codes::kOk)
FAIL() << "Expected kOk but got "
<< EncoderStatusCodeToString(error.code());
called_done = true;
};
AudioEncoder::EncoderStatusCB done_cb =
base::BindLambdaForTesting(done_lambda);
ProduceAudioAndEncode(base::TimeTicks(), frames_per_buffer_,
std::move(done_cb));
task_environment_.RunUntilIdle();
EXPECT_TRUE(called_done);
called_done = false;
done_cb = base::BindLambdaForTesting(done_lambda);
ProduceAudioAndEncode(base::TimeTicks(), frames_per_buffer_,
std::move(done_cb));
task_environment_.RunUntilIdle();
EXPECT_TRUE(called_done);
called_done = false;
done_cb = base::BindLambdaForTesting(done_lambda);
ProduceAudioAndEncode(base::TimeTicks(), frames_per_buffer_,
std::move(done_cb));
task_environment_.RunUntilIdle();
EXPECT_TRUE(called_done);
FlushAndVerifyStatus();
ValidateOutputDuration();
}
TEST_P(AudioEncodersTest, ManySmallInputs) {
if (EncoderHasDelay())
return;
InitializeEncoder();
base::TimeTicks timestamp = base::TimeTicks::Now();
int frame_count = frames_per_buffer_ / 10;
for (int i = 0; i < 100; i++)
ProduceAudioAndEncode(timestamp, frame_count);
FlushAndVerifyStatus();
ValidateDoneCallbacksRun();
ValidateOutputDuration();
}
// Check that the encoder's timestamps don't drift from the expected timestamp
// over multiple inputs.
TEST_P(AudioEncodersTest, Timestamps) {
if (EncoderHasDelay())
return;
std::vector<base::TimeTicks> timestamps;
AudioEncoder::OutputCB output_cb =
base::BindLambdaForTesting([&](EncodedAudioBuffer output, MaybeDesc) {
timestamps.push_back(output.timestamp);
});
InitializeEncoder(std::move(output_cb));
constexpr int kCount = 12;
// Try to encode buffers of different durations. The timestamps of each output
// should increase by `buffer_duration_` regardless of the size of the input.
for (base::TimeDelta duration :
{buffer_duration_ * 10, buffer_duration_, buffer_duration_ * 2 / 3}) {
timestamps.clear();
int num_frames =
AudioTimestampHelper::TimeToFrames(duration, options_.sample_rate);
size_t total_frames = num_frames * kCount;
#if HAS_AAC_ENCODER
if (options_.codec == AudioCodec::kAAC &&
total_frames % kAacFramesPerBuffer) {
// We send data in chunks of kAacFramesPerBuffer to the encoder, padding
// it with silence when flushing.
// Round `total_frames` up to the nearest multiple of kAacFramesPerBuffer.
int chunks = (total_frames / kAacFramesPerBuffer) + 1;
total_frames = chunks * kAacFramesPerBuffer;
}
#endif
total_frames += GetExpectedPadding();
base::TimeTicks current_timestamp;
for (int i = 0; i < kCount; ++i) {
ProduceAudioAndEncode(current_timestamp, num_frames);
current_timestamp += duration;
}
FlushAndVerifyStatus();
ValidateDoneCallbacksRun();
// The encoder will have multiple outputs per input if `num_frames` is
// larger than `frames_per_buffer_`, and fewer outputs per input if it is
// smaller.
size_t expected_outputs = total_frames / frames_per_buffer_;
// The encoder might output an extra buffer, due to padding.
EXPECT_TRUE(timestamps.size() == expected_outputs ||
timestamps.size() == expected_outputs + 1);
// We must use an `AudioTimestampHelper` to verify the returned timestamps
// to avoid rounding errors.
AudioTimestampHelper timestamp_tracker(options_.sample_rate);
timestamp_tracker.SetBaseTimestamp(base::Microseconds(0));
for (auto& observed_ts : timestamps) {
base::TimeTicks expected_ts =
timestamp_tracker.GetTimestamp() + base::TimeTicks();
EXPECT_TRUE(TimesAreNear(expected_ts, observed_ts, base::Microseconds(1)))
<< "expected_ts: " << expected_ts << ", observed_ts: " << observed_ts;
timestamp_tracker.AddFrames(frames_per_buffer_);
}
}
}
// Check how the encoder reacts to breaks in continuity of incoming sound.
// Under normal circumstances capture times are expected to be exactly
// a buffer's duration apart, but if they are not, the encoder just ignores
// incoming capture times. In other words the only capture times that matter
// are
// 1. timestamp of the first encoded buffer
// 2. timestamps of buffers coming immediately after Flush() calls.
TEST_P(AudioEncodersTest, TimeContinuityBreak) {
if (EncoderHasDelay())
return;
std::vector<base::TimeTicks> timestamps;
AudioEncoder::OutputCB output_cb =
base::BindLambdaForTesting([&](EncodedAudioBuffer output, MaybeDesc) {
timestamps.push_back(output.timestamp);
});
InitializeEncoder(std::move(output_cb));
// Encode first normal buffer.
base::TimeTicks current_timestamp = base::TimeTicks::Now();
auto ts0 = current_timestamp;
ProduceAudioAndEncode(current_timestamp);
current_timestamp += buffer_duration_;
// Encode another buffer after a large gap, output timestamp should
// disregard the gap.
auto ts1 = current_timestamp;
current_timestamp += base::Microseconds(1500);
ProduceAudioAndEncode(current_timestamp);
current_timestamp += buffer_duration_;
// Another buffer without a gap.
auto ts2 = ts1 + buffer_duration_;
ProduceAudioAndEncode(current_timestamp);
FlushAndVerifyStatus();
ASSERT_LE(3u, timestamps.size());
EXPECT_TRUE(TimesAreNear(ts0, timestamps[0], base::Microseconds(1)));
EXPECT_TRUE(TimesAreNear(ts1, timestamps[1], base::Microseconds(1)));
EXPECT_TRUE(TimesAreNear(ts2, timestamps[2], base::Microseconds(1)));
timestamps.clear();
// Reset output timestamp after Flush(), the encoder should start producing
// timestamps from new base 0.
current_timestamp = base::TimeTicks();
auto ts3 = current_timestamp;
ProduceAudioAndEncode(current_timestamp);
current_timestamp += buffer_duration_;
auto ts4 = current_timestamp;
ProduceAudioAndEncode(current_timestamp);
FlushAndVerifyStatus();
ASSERT_LE(2u, timestamps.size());
EXPECT_TRUE(TimesAreNear(ts3, timestamps[0], base::Microseconds(1)));
EXPECT_TRUE(TimesAreNear(ts4, timestamps[1], base::Microseconds(1)));
ValidateDoneCallbacksRun();
}
INSTANTIATE_TEST_SUITE_P(Opus,
AudioEncodersTest,
testing::ValuesIn(kTestAudioParamsOpus));
#if HAS_AAC_ENCODER
INSTANTIATE_TEST_SUITE_P(AAC,
AudioEncodersTest,
testing::ValuesIn(kTestAudioParamsAAC));
#endif // HAS_AAC_ENCODER
class AudioOpusEncoderTest : public AudioEncodersTest {
public:
AudioOpusEncoderTest() { options_.codec = AudioCodec::kOpus; }
AudioOpusEncoderTest(const AudioOpusEncoderTest&) = delete;
AudioOpusEncoderTest& operator=(const AudioOpusEncoderTest&) = delete;
~AudioOpusEncoderTest() override = default;
void SetUp() override { AudioEncodersTest::SetUp(); }
};
TEST_P(AudioOpusEncoderTest, ExtraData) {
if (EncoderHasDelay())
return;
std::vector<uint8_t> extra;
AudioEncoder::OutputCB output_cb = base::BindLambdaForTesting(
[&](EncodedAudioBuffer output, MaybeDesc desc) {
DCHECK(desc.has_value());
extra = desc.value();
});
InitializeEncoder(std::move(output_cb));
ProduceAudioAndEncode(base::TimeTicks::Now(),
min_number_input_frames_needed_);
task_environment_.RunUntilIdle();
ASSERT_GT(extra.size(), 0u);
EXPECT_EQ(extra[0], 'O');
EXPECT_EQ(extra[1], 'p');
EXPECT_EQ(extra[2], 'u');
EXPECT_EQ(extra[3], 's');
uint16_t* sample_rate_ptr = reinterpret_cast<uint16_t*>(extra.data() + 12);
if (options_.sample_rate < std::numeric_limits<uint16_t>::max())
EXPECT_EQ(*sample_rate_ptr, options_.sample_rate);
else
EXPECT_EQ(*sample_rate_ptr, 48000);
uint8_t* channels_ptr = reinterpret_cast<uint8_t*>(extra.data() + 9);
EXPECT_EQ(*channels_ptr, options_.channels);
uint16_t* skip_ptr = reinterpret_cast<uint16_t*>(extra.data() + 10);
EXPECT_GT(*skip_ptr, 0);
}
TEST_P(AudioOpusEncoderTest, FullCycleEncodeDecode) {
const int kOpusDecoderSampleRate = 48000;
const int kOpusDecoderFramesPerBuffer = AudioTimestampHelper::TimeToFrames(
kOpusBufferDuration, kOpusDecoderSampleRate);
int error;
OpusDecoder* opus_decoder =
opus_decoder_create(kOpusDecoderSampleRate, options_.channels, &error);
ASSERT_TRUE(error == OPUS_OK && opus_decoder);
int encode_callback_count = 0;
std::vector<float> buffer(kOpusDecoderFramesPerBuffer * options_.channels);
auto verify_opus_encoding = [&](EncodedAudioBuffer output, MaybeDesc) {
++encode_callback_count;
// Use the libopus decoder to decode the |encoded_data| and check we
// get the expected number of frames per buffer.
EXPECT_EQ(kOpusDecoderFramesPerBuffer,
opus_decode_float(opus_decoder, output.encoded_data.data(),
output.encoded_data.size(), buffer.data(),
kOpusDecoderFramesPerBuffer, 0));
};
InitializeEncoder(base::BindLambdaForTesting(verify_opus_encoding));
base::TimeTicks time;
int total_frames = 0;
// Push data until we have a decoded output.
while (total_frames < min_number_input_frames_needed_) {
total_frames += ProduceAudioAndEncode(time);
time += buffer_duration_;
task_environment_.RunUntilIdle();
}
EXPECT_GE(total_frames, frames_per_buffer_);
EXPECT_EQ(1, encode_callback_count);
// Flush the leftover data in the encoder, due to encoder delay.
FlushAndVerifyStatus();
opus_decoder_destroy(opus_decoder);
opus_decoder = nullptr;
}
// Tests we can configure the AudioOpusEncoder's bitrate mode.
TEST_P(AudioOpusEncoderTest, FullCycleEncodeDecode_BitrateMode) {
constexpr AudioEncoder::BitrateMode kTestOpusBitrateMode[] = {
AudioEncoder::BitrateMode::kConstant,
AudioEncoder::BitrateMode::kVariable};
for (const AudioEncoder::BitrateMode& bitrate_mode : kTestOpusBitrateMode) {
constexpr int kOpusDecoderSampleRate = 48000;
const int kOpusDecoderFramesPerBuffer = AudioTimestampHelper::TimeToFrames(
kOpusBufferDuration, kOpusDecoderSampleRate);
// Override the work done in CreateEncoder().
encoder_ = std::make_unique<AudioOpusEncoder>();
options_.bitrate_mode = bitrate_mode;
int error;
OpusDecoder* opus_decoder =
opus_decoder_create(kOpusDecoderSampleRate, options_.channels, &error);
ASSERT_TRUE(error == OPUS_OK && opus_decoder);
std::vector<float> buffer(kOpusDecoderFramesPerBuffer * options_.channels);
auto verify_opus_encoding = [&](EncodedAudioBuffer output, MaybeDesc) {
// Use the libopus decoder to decode the |encoded_data| and check we
// get the expected number of frames per buffer.
EXPECT_EQ(kOpusDecoderFramesPerBuffer,
opus_decode_float(opus_decoder, output.encoded_data.data(),
output.encoded_data.size(), buffer.data(),
kOpusDecoderFramesPerBuffer, 0));
};
InitializeEncoder(base::BindLambdaForTesting(verify_opus_encoding));
base::TimeTicks time;
int total_frames = 0;
// Push data until we have a decoded output.
while (total_frames < min_number_input_frames_needed_) {
total_frames += ProduceAudioAndEncode(time);
time += buffer_duration_;
task_environment_.RunUntilIdle();
}
EXPECT_GE(total_frames, frames_per_buffer_);
FlushAndVerifyStatus();
opus_decoder_destroy(opus_decoder);
opus_decoder = nullptr;
}
}
// Tests we can configure the AudioOpusEncoder's extra options.
TEST_P(AudioOpusEncoderTest, FullCycleEncodeDecode_OpusOptions) {
// TODO(crbug.com/40243924): Test an OpusOptions::frame_duration which forces
// repacketization.
constexpr media::AudioEncoder::OpusOptions kTestOpusOptions[] = {
// Base case
{.frame_duration = base::Milliseconds(20),
.complexity = 10,
.packet_loss_perc = 0,
.use_in_band_fec = false,
.use_dtx = false},
// Use inband-FEC
{.frame_duration = base::Microseconds(2500),
.complexity = 0,
.packet_loss_perc = 10,
.use_in_band_fec = true,
.use_dtx = false},
// Use DTX
{.frame_duration = base::Milliseconds(60),
.complexity = 5,
.packet_loss_perc = 0,
.use_in_band_fec = false,
.use_dtx = true},
// Use inband-FEC and DTX
{.frame_duration = base::Milliseconds(5),
.complexity = 5,
.packet_loss_perc = 20,
.use_in_band_fec = true,
.use_dtx = true},
};
for (const AudioEncoder::OpusOptions& opus_options : kTestOpusOptions) {
const int kOpusDecoderSampleRate = 48000;
// Override the work done in CreateEncoder().
encoder_ = std::make_unique<AudioOpusEncoder>();
options_.opus = opus_options;
buffer_duration_ = opus_options.frame_duration;
frames_per_buffer_ = AudioTimestampHelper::TimeToFrames(
buffer_duration_, options_.sample_rate);
int decoder_frames_per_buffer = AudioTimestampHelper::TimeToFrames(
buffer_duration_, kOpusDecoderSampleRate);
int error;
OpusDecoder* opus_decoder =
opus_decoder_create(kOpusDecoderSampleRate, options_.channels, &error);
ASSERT_TRUE(error == OPUS_OK && opus_decoder);
std::vector<float> buffer(decoder_frames_per_buffer * options_.channels);
auto verify_opus_encoding = [&](EncodedAudioBuffer output, MaybeDesc) {
// Use the libopus decoder to decode the |encoded_data| and check we
// get the expected number of frames per buffer.
EXPECT_EQ(decoder_frames_per_buffer,
opus_decode_float(opus_decoder, output.encoded_data.data(),
output.encoded_data.size(), buffer.data(),
decoder_frames_per_buffer, 0));
};
InitializeEncoder(base::BindLambdaForTesting(verify_opus_encoding));
base::TimeTicks time;
int total_frames = 0;
// Push data until we have a decoded output.
while (total_frames < min_number_input_frames_needed_) {
total_frames += ProduceAudioAndEncode(time);
time += buffer_duration_;
task_environment_.RunUntilIdle();
}
EXPECT_GE(total_frames, frames_per_buffer_);
FlushAndVerifyStatus();
opus_decoder_destroy(opus_decoder);
opus_decoder = nullptr;
}
}
TEST_P(AudioOpusEncoderTest, VariableChannelCounts) {
constexpr int kTestToneFrequency = 440;
SineWaveAudioSource sources[] = {
SineWaveAudioSource(1, kTestToneFrequency, options_.sample_rate),
SineWaveAudioSource(2, kTestToneFrequency, options_.sample_rate),
SineWaveAudioSource(3, kTestToneFrequency, options_.sample_rate)};
const int num_frames = options_.sample_rate * buffer_duration_.InSecondsF();
auto generate_audio = [&sources, &num_frames](
int channel_count,
base::TimeTicks current_timestamp) {
auto audio_bus = AudioBus::Create(channel_count, num_frames);
sources[channel_count - 1].OnMoreData(base::TimeDelta(), current_timestamp,
{}, audio_bus.get());
return audio_bus;
};
// Superpermutation of {1, 2, 3}, covering all transitions between upmixing,
// downmixing and not mixing.
const int kChannelCountSequence[] = {1, 2, 3, 1, 2, 2, 1, 3, 2, 1};
// Override |GetParam().channels|, to ensure that we can both upmix and
// downmix.
options_.channels = 2;
auto empty_output_cb =
base::BindLambdaForTesting([&](EncodedAudioBuffer output, MaybeDesc) {});
InitializeEncoder(std::move(empty_output_cb));
base::TimeTicks current_timestamp;
for (const int& ch : kChannelCountSequence) {
// Encode, using a different number of channels each time.
DoEncode(generate_audio(ch, current_timestamp), current_timestamp);
current_timestamp += buffer_duration_;
}
FlushAndVerifyStatus();
}
INSTANTIATE_TEST_SUITE_P(Opus,
AudioOpusEncoderTest,
testing::ValuesIn(kTestAudioParamsOpus));
#if HAS_AAC_ENCODER
class AACAudioEncoderTest : public AudioEncodersTest {
public:
AACAudioEncoderTest() = default;
AACAudioEncoderTest(const AACAudioEncoderTest&) = delete;
AACAudioEncoderTest& operator=(const AACAudioEncoderTest&) = delete;
~AACAudioEncoderTest() override = default;
#if BUILDFLAG(ENABLE_FFMPEG) && BUILDFLAG(USE_PROPRIETARY_CODECS)
void InitializeDecoder() {
decoder_ = std::make_unique<FFmpegAudioDecoder>(
base::SequencedTaskRunner::GetCurrentDefault(), &media_log);
ChannelLayout channel_layout = CHANNEL_LAYOUT_NONE;
switch (options_.channels) {
case 1:
channel_layout = CHANNEL_LAYOUT_MONO;
break;
case 2:
channel_layout = CHANNEL_LAYOUT_STEREO;
break;
case 6:
channel_layout = CHANNEL_LAYOUT_5_1_BACK;
break;
default:
NOTREACHED();
}
AudioDecoderConfig config(AudioCodec::kAAC, SampleFormat::kSampleFormatS16,
channel_layout, options_.sample_rate,
/*extra_data=*/std::vector<uint8_t>(),
EncryptionScheme::kUnencrypted);
auto init_cb = [](DecoderStatus decoder_status) {
EXPECT_EQ(decoder_status, DecoderStatus::Codes::kOk);
};
auto output_cb = [&](scoped_refptr<AudioBuffer> decoded_buffer) {
++decoder_output_callback_count;
EXPECT_EQ(decoded_buffer->frame_count(), frames_per_buffer_);
};
decoder_->Initialize(config, /*cdm_context=*/nullptr,
base::BindLambdaForTesting(init_cb),
base::BindLambdaForTesting(output_cb),
/*waiting_cb=*/base::DoNothing());
}
protected:
MockMediaLog media_log;
std::unique_ptr<FFmpegAudioDecoder> decoder_;
int decoder_output_callback_count = 0;
#endif // BUILDFLAG(ENABLE_FFMPEG) && BUILDFLAG(USE_PROPRIETARY_CODECS)
};
#if BUILDFLAG(IS_WIN)
// `MFAudioEncoder` requires `kMinSamplesForOutput` before `Flush` can be called
// successfully.
TEST_P(AACAudioEncoderTest, FlushWithTooLittleInput) {
InitializeEncoder(base::DoNothing());
ProduceAudioAndEncode();
FlushAndVerifyStatus(EncoderStatus::Codes::kEncoderFailedFlush);
ValidateDoneCallbacksRun();
}
#endif
#if BUILDFLAG(ENABLE_FFMPEG) && BUILDFLAG(USE_PROPRIETARY_CODECS)
TEST_P(AACAudioEncoderTest, FullCycleEncodeDecode) {
InitializeDecoder();
int encode_output_callback_count = 0;
int decode_status_callback_count = 0;
auto encode_output_cb = [&](EncodedAudioBuffer output, MaybeDesc) {
++encode_output_callback_count;
auto decode_cb = [&](DecoderStatus status) {
++decode_status_callback_count;
EXPECT_EQ(status, DecoderStatus::Codes::kOk);
};
scoped_refptr<DecoderBuffer> decoder_buffer =
DecoderBuffer::FromArray(std::move(output.encoded_data));
decoder_->Decode(decoder_buffer, base::BindLambdaForTesting(decode_cb));
};
InitializeEncoder(base::BindLambdaForTesting(encode_output_cb));
ProduceAudioAndEncode();
ProduceAudioAndEncode();
ProduceAudioAndEncode();
FlushAndVerifyStatus();
// Let the decoder finish decoding.
task_environment_.RunUntilIdle();
int expected_outputs = 3 + std::ceil(GetExpectedPadding() /
static_cast<double>(frames_per_buffer_));
EXPECT_EQ(expected_outputs, encode_output_callback_count);
EXPECT_EQ(expected_outputs, decode_status_callback_count);
EXPECT_EQ(expected_outputs, decoder_output_callback_count);
}
TEST_P(AACAudioEncoderTest, FullCycleEncodeDecode_BitrateMode) {
constexpr AudioEncoder::BitrateMode kTestAacBitrateMode[] = {
AudioEncoder::BitrateMode::kConstant,
AudioEncoder::BitrateMode::kVariable};
for (const AudioEncoder::BitrateMode& bitrate_mode : kTestAacBitrateMode) {
decoder_output_callback_count = 0;
options_.bitrate_mode = bitrate_mode;
// Recreate the encoder to pick up changes to `options_`.
CreateEncoder();
InitializeDecoder();
auto encode_output_cb = [&](EncodedAudioBuffer output, MaybeDesc) {
auto decode_cb = [&](DecoderStatus status) {
EXPECT_EQ(status, DecoderStatus::Codes::kOk);
};
scoped_refptr<DecoderBuffer> decoder_buffer =
DecoderBuffer::FromArray(std::move(output.encoded_data));
decoder_->Decode(decoder_buffer, base::BindLambdaForTesting(decode_cb));
};
InitializeEncoder(base::BindLambdaForTesting(encode_output_cb));
ProduceAudioAndEncode();
ProduceAudioAndEncode();
ProduceAudioAndEncode();
FlushAndVerifyStatus();
// Let the decoder finish decoding.
task_environment_.RunUntilIdle();
int expected_outputs =
3 + std::ceil(GetExpectedPadding() /
static_cast<double>(frames_per_buffer_));
EXPECT_EQ(expected_outputs, decoder_output_callback_count);
}
}
// Makes sure we get extradata on the first output when we are using AAC output
// format, and no extradata when we are using ADTS output format.
TEST_P(AACAudioEncoderTest, AacOutputFormat) {
constexpr AudioEncoder::AacOutputFormat kTestAacOutputFormat[] = {
AudioEncoder::AacOutputFormat::AAC, AudioEncoder::AacOutputFormat::ADTS};
for (const auto& output_format : kTestAacOutputFormat) {
options_.aac = {output_format};
// Recreate the encoder to pick up changes to `options_`.
CreateEncoder();
bool first_output = true;
const bool needs_description =
output_format == AudioEncoder::AacOutputFormat::AAC;
auto encode_output_cb = [&](EncodedAudioBuffer output,
MaybeDesc codec_description) {
if (first_output) {
first_output = false;
EXPECT_EQ(codec_description.has_value(), needs_description);
} else {
EXPECT_FALSE(codec_description);
}
};
InitializeEncoder(base::BindLambdaForTesting(encode_output_cb));
ProduceAudioAndEncode();
ProduceAudioAndEncode();
ProduceAudioAndEncode();
FlushAndVerifyStatus();
}
}
#endif // BUILDFLAG(ENABLE_FFMPEG) && BUILDFLAG(USE_PROPRIETARY_CODECS)
INSTANTIATE_TEST_SUITE_P(AAC,
AACAudioEncoderTest,
testing::ValuesIn(kTestAudioParamsAAC));
#endif // HAS_AAC_ENCODER
} // namespace media