1 /*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/audio_coding/include/audio_coding_module.h"
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <atomic>
17 #include <memory>
18 #include <vector>
19
20 #include "api/audio_codecs/audio_encoder.h"
21 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
22 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
23 #include "api/audio_codecs/opus/audio_decoder_multi_channel_opus.h"
24 #include "api/audio_codecs/opus/audio_decoder_opus.h"
25 #include "api/audio_codecs/opus/audio_encoder_multi_channel_opus.h"
26 #include "api/audio_codecs/opus/audio_encoder_opus.h"
27 #include "modules/audio_coding/acm2/acm_receive_test.h"
28 #include "modules/audio_coding/acm2/acm_send_test.h"
29 #include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
30 #include "modules/audio_coding/codecs/g711/audio_decoder_pcm.h"
31 #include "modules/audio_coding/codecs/g711/audio_encoder_pcm.h"
32 #include "modules/audio_coding/codecs/isac/main/include/audio_encoder_isac.h"
33 #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
34 #include "modules/audio_coding/neteq/tools/audio_checksum.h"
35 #include "modules/audio_coding/neteq/tools/audio_loop.h"
36 #include "modules/audio_coding/neteq/tools/constant_pcm_packet_source.h"
37 #include "modules/audio_coding/neteq/tools/input_audio_file.h"
38 #include "modules/audio_coding/neteq/tools/output_audio_file.h"
39 #include "modules/audio_coding/neteq/tools/output_wav_file.h"
40 #include "modules/audio_coding/neteq/tools/packet.h"
41 #include "modules/audio_coding/neteq/tools/rtp_file_source.h"
42 #include "rtc_base/event.h"
43 #include "rtc_base/message_digest.h"
44 #include "rtc_base/numerics/safe_conversions.h"
45 #include "rtc_base/platform_thread.h"
46 #include "rtc_base/ref_counted_object.h"
47 #include "rtc_base/synchronization/mutex.h"
48 #include "rtc_base/system/arch.h"
49 #include "rtc_base/thread_annotations.h"
50 #include "system_wrappers/include/clock.h"
51 #include "system_wrappers/include/sleep.h"
52 #include "test/audio_decoder_proxy_factory.h"
53 #include "test/gtest.h"
54 #include "test/mock_audio_decoder.h"
55 #include "test/mock_audio_encoder.h"
56 #include "test/testsupport/file_utils.h"
57 #include "test/testsupport/rtc_expect_death.h"
58
59 using ::testing::_;
60 using ::testing::AtLeast;
61 using ::testing::Invoke;
62
63 namespace webrtc {
64
65 namespace {
66 const int kSampleRateHz = 16000;
67 const int kNumSamples10ms = kSampleRateHz / 100;
68 const int kFrameSizeMs = 10; // Multiple of 10.
69 const int kFrameSizeSamples = kFrameSizeMs / 10 * kNumSamples10ms;
70 const int kPayloadSizeBytes = kFrameSizeSamples * sizeof(int16_t);
71 const uint8_t kPayloadType = 111;
72 } // namespace
73
74 class RtpData {
75 public:
RtpData(int samples_per_packet,uint8_t payload_type)76 RtpData(int samples_per_packet, uint8_t payload_type)
77 : samples_per_packet_(samples_per_packet), payload_type_(payload_type) {}
78
~RtpData()79 virtual ~RtpData() {}
80
Populate(RTPHeader * rtp_header)81 void Populate(RTPHeader* rtp_header) {
82 rtp_header->sequenceNumber = 0xABCD;
83 rtp_header->timestamp = 0xABCDEF01;
84 rtp_header->payloadType = payload_type_;
85 rtp_header->markerBit = false;
86 rtp_header->ssrc = 0x1234;
87 rtp_header->numCSRCs = 0;
88
89 rtp_header->payload_type_frequency = kSampleRateHz;
90 }
91
Forward(RTPHeader * rtp_header)92 void Forward(RTPHeader* rtp_header) {
93 ++rtp_header->sequenceNumber;
94 rtp_header->timestamp += samples_per_packet_;
95 }
96
97 private:
98 int samples_per_packet_;
99 uint8_t payload_type_;
100 };
101
102 class PacketizationCallbackStubOldApi : public AudioPacketizationCallback {
103 public:
PacketizationCallbackStubOldApi()104 PacketizationCallbackStubOldApi()
105 : num_calls_(0),
106 last_frame_type_(AudioFrameType::kEmptyFrame),
107 last_payload_type_(-1),
108 last_timestamp_(0) {}
109
SendData(AudioFrameType frame_type,uint8_t payload_type,uint32_t timestamp,const uint8_t * payload_data,size_t payload_len_bytes,int64_t absolute_capture_timestamp_ms)110 int32_t SendData(AudioFrameType frame_type,
111 uint8_t payload_type,
112 uint32_t timestamp,
113 const uint8_t* payload_data,
114 size_t payload_len_bytes,
115 int64_t absolute_capture_timestamp_ms) override {
116 MutexLock lock(&mutex_);
117 ++num_calls_;
118 last_frame_type_ = frame_type;
119 last_payload_type_ = payload_type;
120 last_timestamp_ = timestamp;
121 last_payload_vec_.assign(payload_data, payload_data + payload_len_bytes);
122 return 0;
123 }
124
num_calls() const125 int num_calls() const {
126 MutexLock lock(&mutex_);
127 return num_calls_;
128 }
129
last_payload_len_bytes() const130 int last_payload_len_bytes() const {
131 MutexLock lock(&mutex_);
132 return rtc::checked_cast<int>(last_payload_vec_.size());
133 }
134
last_frame_type() const135 AudioFrameType last_frame_type() const {
136 MutexLock lock(&mutex_);
137 return last_frame_type_;
138 }
139
last_payload_type() const140 int last_payload_type() const {
141 MutexLock lock(&mutex_);
142 return last_payload_type_;
143 }
144
last_timestamp() const145 uint32_t last_timestamp() const {
146 MutexLock lock(&mutex_);
147 return last_timestamp_;
148 }
149
SwapBuffers(std::vector<uint8_t> * payload)150 void SwapBuffers(std::vector<uint8_t>* payload) {
151 MutexLock lock(&mutex_);
152 last_payload_vec_.swap(*payload);
153 }
154
155 private:
156 int num_calls_ RTC_GUARDED_BY(mutex_);
157 AudioFrameType last_frame_type_ RTC_GUARDED_BY(mutex_);
158 int last_payload_type_ RTC_GUARDED_BY(mutex_);
159 uint32_t last_timestamp_ RTC_GUARDED_BY(mutex_);
160 std::vector<uint8_t> last_payload_vec_ RTC_GUARDED_BY(mutex_);
161 mutable Mutex mutex_;
162 };
163
164 class AudioCodingModuleTestOldApi : public ::testing::Test {
165 protected:
AudioCodingModuleTestOldApi()166 AudioCodingModuleTestOldApi()
167 : rtp_utility_(new RtpData(kFrameSizeSamples, kPayloadType)),
168 clock_(Clock::GetRealTimeClock()) {}
169
~AudioCodingModuleTestOldApi()170 ~AudioCodingModuleTestOldApi() {}
171
TearDown()172 void TearDown() {}
173
SetUp()174 void SetUp() {
175 acm_.reset(AudioCodingModule::Create([this] {
176 AudioCodingModule::Config config;
177 config.clock = clock_;
178 config.decoder_factory = CreateBuiltinAudioDecoderFactory();
179 return config;
180 }()));
181
182 rtp_utility_->Populate(&rtp_header_);
183
184 input_frame_.sample_rate_hz_ = kSampleRateHz;
185 input_frame_.num_channels_ = 1;
186 input_frame_.samples_per_channel_ = kSampleRateHz * 10 / 1000; // 10 ms.
187 static_assert(kSampleRateHz * 10 / 1000 <= AudioFrame::kMaxDataSizeSamples,
188 "audio frame too small");
189 input_frame_.Mute();
190
191 ASSERT_EQ(0, acm_->RegisterTransportCallback(&packet_cb_));
192
193 SetUpL16Codec();
194 }
195
196 // Set up L16 codec.
SetUpL16Codec()197 virtual void SetUpL16Codec() {
198 audio_format_ = SdpAudioFormat("L16", kSampleRateHz, 1);
199 pac_size_ = 160;
200 }
201
RegisterCodec()202 virtual void RegisterCodec() {
203 acm_->SetReceiveCodecs({{kPayloadType, *audio_format_}});
204 acm_->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
205 kPayloadType, *audio_format_, absl::nullopt));
206 }
207
InsertPacketAndPullAudio()208 virtual void InsertPacketAndPullAudio() {
209 InsertPacket();
210 PullAudio();
211 }
212
InsertPacket()213 virtual void InsertPacket() {
214 const uint8_t kPayload[kPayloadSizeBytes] = {0};
215 ASSERT_EQ(0,
216 acm_->IncomingPacket(kPayload, kPayloadSizeBytes, rtp_header_));
217 rtp_utility_->Forward(&rtp_header_);
218 }
219
PullAudio()220 virtual void PullAudio() {
221 AudioFrame audio_frame;
222 bool muted;
223 ASSERT_EQ(0, acm_->PlayoutData10Ms(-1, &audio_frame, &muted));
224 ASSERT_FALSE(muted);
225 }
226
InsertAudio()227 virtual void InsertAudio() {
228 ASSERT_GE(acm_->Add10MsData(input_frame_), 0);
229 input_frame_.timestamp_ += kNumSamples10ms;
230 }
231
VerifyEncoding()232 virtual void VerifyEncoding() {
233 int last_length = packet_cb_.last_payload_len_bytes();
234 EXPECT_TRUE(last_length == 2 * pac_size_ || last_length == 0)
235 << "Last encoded packet was " << last_length << " bytes.";
236 }
237
InsertAudioAndVerifyEncoding()238 virtual void InsertAudioAndVerifyEncoding() {
239 InsertAudio();
240 VerifyEncoding();
241 }
242
243 std::unique_ptr<RtpData> rtp_utility_;
244 std::unique_ptr<AudioCodingModule> acm_;
245 PacketizationCallbackStubOldApi packet_cb_;
246 RTPHeader rtp_header_;
247 AudioFrame input_frame_;
248
249 absl::optional<SdpAudioFormat> audio_format_;
250 int pac_size_ = -1;
251
252 Clock* clock_;
253 };
254
255 class AudioCodingModuleTestOldApiDeathTest
256 : public AudioCodingModuleTestOldApi {};
257
TEST_F(AudioCodingModuleTestOldApi,VerifyOutputFrame)258 TEST_F(AudioCodingModuleTestOldApi, VerifyOutputFrame) {
259 AudioFrame audio_frame;
260 const int kSampleRateHz = 32000;
261 bool muted;
262 EXPECT_EQ(0, acm_->PlayoutData10Ms(kSampleRateHz, &audio_frame, &muted));
263 ASSERT_FALSE(muted);
264 EXPECT_EQ(0u, audio_frame.timestamp_);
265 EXPECT_GT(audio_frame.num_channels_, 0u);
266 EXPECT_EQ(static_cast<size_t>(kSampleRateHz / 100),
267 audio_frame.samples_per_channel_);
268 EXPECT_EQ(kSampleRateHz, audio_frame.sample_rate_hz_);
269 }
270
271 // The below test is temporarily disabled on Windows due to problems
272 // with clang debug builds.
273 // TODO(tommi): Re-enable when we've figured out what the problem is.
274 // http://crbug.com/615050
275 #if !defined(WEBRTC_WIN) && defined(__clang__) && RTC_DCHECK_IS_ON && \
276 GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
TEST_F(AudioCodingModuleTestOldApiDeathTest,FailOnZeroDesiredFrequency)277 TEST_F(AudioCodingModuleTestOldApiDeathTest, FailOnZeroDesiredFrequency) {
278 AudioFrame audio_frame;
279 bool muted;
280 RTC_EXPECT_DEATH(acm_->PlayoutData10Ms(0, &audio_frame, &muted),
281 "dst_sample_rate_hz");
282 }
283 #endif
284
285 // Checks that the transport callback is invoked once for each speech packet.
286 // Also checks that the frame type is kAudioFrameSpeech.
TEST_F(AudioCodingModuleTestOldApi,TransportCallbackIsInvokedForEachPacket)287 TEST_F(AudioCodingModuleTestOldApi, TransportCallbackIsInvokedForEachPacket) {
288 const int k10MsBlocksPerPacket = 3;
289 pac_size_ = k10MsBlocksPerPacket * kSampleRateHz / 100;
290 audio_format_->parameters["ptime"] = "30";
291 RegisterCodec();
292 const int kLoops = 10;
293 for (int i = 0; i < kLoops; ++i) {
294 EXPECT_EQ(i / k10MsBlocksPerPacket, packet_cb_.num_calls());
295 if (packet_cb_.num_calls() > 0)
296 EXPECT_EQ(AudioFrameType::kAudioFrameSpeech,
297 packet_cb_.last_frame_type());
298 InsertAudioAndVerifyEncoding();
299 }
300 EXPECT_EQ(kLoops / k10MsBlocksPerPacket, packet_cb_.num_calls());
301 EXPECT_EQ(AudioFrameType::kAudioFrameSpeech, packet_cb_.last_frame_type());
302 }
303
304 #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
305 // Verifies that the RTP timestamp series is not reset when the codec is
306 // changed.
TEST_F(AudioCodingModuleTestOldApi,TimestampSeriesContinuesWhenCodecChanges)307 TEST_F(AudioCodingModuleTestOldApi, TimestampSeriesContinuesWhenCodecChanges) {
308 RegisterCodec(); // This registers the default codec.
309 uint32_t expected_ts = input_frame_.timestamp_;
310 int blocks_per_packet = pac_size_ / (kSampleRateHz / 100);
311 // Encode 5 packets of the first codec type.
312 const int kNumPackets1 = 5;
313 for (int j = 0; j < kNumPackets1; ++j) {
314 for (int i = 0; i < blocks_per_packet; ++i) {
315 EXPECT_EQ(j, packet_cb_.num_calls());
316 InsertAudio();
317 }
318 EXPECT_EQ(j + 1, packet_cb_.num_calls());
319 EXPECT_EQ(expected_ts, packet_cb_.last_timestamp());
320 expected_ts += pac_size_;
321 }
322
323 // Change codec.
324 audio_format_ = SdpAudioFormat("ISAC", kSampleRateHz, 1);
325 pac_size_ = 480;
326 RegisterCodec();
327 blocks_per_packet = pac_size_ / (kSampleRateHz / 100);
328 // Encode another 5 packets.
329 const int kNumPackets2 = 5;
330 for (int j = 0; j < kNumPackets2; ++j) {
331 for (int i = 0; i < blocks_per_packet; ++i) {
332 EXPECT_EQ(kNumPackets1 + j, packet_cb_.num_calls());
333 InsertAudio();
334 }
335 EXPECT_EQ(kNumPackets1 + j + 1, packet_cb_.num_calls());
336 EXPECT_EQ(expected_ts, packet_cb_.last_timestamp());
337 expected_ts += pac_size_;
338 }
339 }
340 #endif
341
342 // Introduce this class to set different expectations on the number of encoded
343 // bytes. This class expects all encoded packets to be 9 bytes (matching one
344 // CNG SID frame) or 0 bytes. This test depends on |input_frame_| containing
345 // (near-)zero values. It also introduces a way to register comfort noise with
346 // a custom payload type.
347 class AudioCodingModuleTestWithComfortNoiseOldApi
348 : public AudioCodingModuleTestOldApi {
349 protected:
RegisterCngCodec(int rtp_payload_type)350 void RegisterCngCodec(int rtp_payload_type) {
351 acm_->SetReceiveCodecs({{kPayloadType, *audio_format_},
352 {rtp_payload_type, {"cn", kSampleRateHz, 1}}});
353 acm_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
354 AudioEncoderCngConfig config;
355 config.speech_encoder = std::move(*enc);
356 config.num_channels = 1;
357 config.payload_type = rtp_payload_type;
358 config.vad_mode = Vad::kVadNormal;
359 *enc = CreateComfortNoiseEncoder(std::move(config));
360 });
361 }
362
VerifyEncoding()363 void VerifyEncoding() override {
364 int last_length = packet_cb_.last_payload_len_bytes();
365 EXPECT_TRUE(last_length == 9 || last_length == 0)
366 << "Last encoded packet was " << last_length << " bytes.";
367 }
368
DoTest(int blocks_per_packet,int cng_pt)369 void DoTest(int blocks_per_packet, int cng_pt) {
370 const int kLoops = 40;
371 // This array defines the expected frame types, and when they should arrive.
372 // We expect a frame to arrive each time the speech encoder would have
373 // produced a packet, and once every 100 ms the frame should be non-empty,
374 // that is contain comfort noise.
375 const struct {
376 int ix;
377 AudioFrameType type;
378 } expectation[] = {{2, AudioFrameType::kAudioFrameCN},
379 {5, AudioFrameType::kEmptyFrame},
380 {8, AudioFrameType::kEmptyFrame},
381 {11, AudioFrameType::kAudioFrameCN},
382 {14, AudioFrameType::kEmptyFrame},
383 {17, AudioFrameType::kEmptyFrame},
384 {20, AudioFrameType::kAudioFrameCN},
385 {23, AudioFrameType::kEmptyFrame},
386 {26, AudioFrameType::kEmptyFrame},
387 {29, AudioFrameType::kEmptyFrame},
388 {32, AudioFrameType::kAudioFrameCN},
389 {35, AudioFrameType::kEmptyFrame},
390 {38, AudioFrameType::kEmptyFrame}};
391 for (int i = 0; i < kLoops; ++i) {
392 int num_calls_before = packet_cb_.num_calls();
393 EXPECT_EQ(i / blocks_per_packet, num_calls_before);
394 InsertAudioAndVerifyEncoding();
395 int num_calls = packet_cb_.num_calls();
396 if (num_calls == num_calls_before + 1) {
397 EXPECT_EQ(expectation[num_calls - 1].ix, i);
398 EXPECT_EQ(expectation[num_calls - 1].type, packet_cb_.last_frame_type())
399 << "Wrong frame type for lap " << i;
400 EXPECT_EQ(cng_pt, packet_cb_.last_payload_type());
401 } else {
402 EXPECT_EQ(num_calls, num_calls_before);
403 }
404 }
405 }
406 };
407
408 // Checks that the transport callback is invoked once per frame period of the
409 // underlying speech encoder, even when comfort noise is produced.
410 // Also checks that the frame type is kAudioFrameCN or kEmptyFrame.
TEST_F(AudioCodingModuleTestWithComfortNoiseOldApi,TransportCallbackTestForComfortNoiseRegisterCngLast)411 TEST_F(AudioCodingModuleTestWithComfortNoiseOldApi,
412 TransportCallbackTestForComfortNoiseRegisterCngLast) {
413 const int k10MsBlocksPerPacket = 3;
414 pac_size_ = k10MsBlocksPerPacket * kSampleRateHz / 100;
415 audio_format_->parameters["ptime"] = "30";
416 RegisterCodec();
417 const int kCngPayloadType = 105;
418 RegisterCngCodec(kCngPayloadType);
419 DoTest(k10MsBlocksPerPacket, kCngPayloadType);
420 }
421
422 // A multi-threaded test for ACM. This base class is using the PCM16b 16 kHz
423 // codec, while the derive class AcmIsacMtTest is using iSAC.
424 class AudioCodingModuleMtTestOldApi : public AudioCodingModuleTestOldApi {
425 protected:
426 static const int kNumPackets = 500;
427 static const int kNumPullCalls = 500;
428
AudioCodingModuleMtTestOldApi()429 AudioCodingModuleMtTestOldApi()
430 : AudioCodingModuleTestOldApi(),
431 send_thread_(CbSendThread, this, "send", rtc::kRealtimePriority),
432 insert_packet_thread_(CbInsertPacketThread,
433 this,
434 "insert_packet",
435 rtc::kRealtimePriority),
436 pull_audio_thread_(CbPullAudioThread,
437 this,
438 "pull_audio",
439 rtc::kRealtimePriority),
440 send_count_(0),
441 insert_packet_count_(0),
442 pull_audio_count_(0),
443 next_insert_packet_time_ms_(0),
444 fake_clock_(new SimulatedClock(0)) {
445 clock_ = fake_clock_.get();
446 }
447
SetUp()448 void SetUp() {
449 AudioCodingModuleTestOldApi::SetUp();
450 RegisterCodec(); // Must be called before the threads start below.
451 StartThreads();
452 }
453
StartThreads()454 void StartThreads() {
455 quit_.store(false);
456 send_thread_.Start();
457 insert_packet_thread_.Start();
458 pull_audio_thread_.Start();
459 }
460
TearDown()461 void TearDown() {
462 AudioCodingModuleTestOldApi::TearDown();
463 quit_.store(true);
464 pull_audio_thread_.Stop();
465 send_thread_.Stop();
466 insert_packet_thread_.Stop();
467 }
468
RunTest()469 bool RunTest() {
470 return test_complete_.Wait(10 * 60 * 1000); // 10 minutes' timeout.
471 }
472
TestDone()473 virtual bool TestDone() {
474 if (packet_cb_.num_calls() > kNumPackets) {
475 MutexLock lock(&mutex_);
476 if (pull_audio_count_ > kNumPullCalls) {
477 // Both conditions for completion are met. End the test.
478 return true;
479 }
480 }
481 return false;
482 }
483
CbSendThread(void * context)484 static void CbSendThread(void* context) {
485 AudioCodingModuleMtTestOldApi* fixture =
486 reinterpret_cast<AudioCodingModuleMtTestOldApi*>(context);
487 while (!fixture->quit_.load()) {
488 fixture->CbSendImpl();
489 }
490 }
491
492 // The send thread doesn't have to care about the current simulated time,
493 // since only the AcmReceiver is using the clock.
CbSendImpl()494 void CbSendImpl() {
495 SleepMs(1);
496 if (HasFatalFailure()) {
497 // End the test early if a fatal failure (ASSERT_*) has occurred.
498 test_complete_.Set();
499 }
500 ++send_count_;
501 InsertAudioAndVerifyEncoding();
502 if (TestDone()) {
503 test_complete_.Set();
504 }
505 }
506
CbInsertPacketThread(void * context)507 static void CbInsertPacketThread(void* context) {
508 AudioCodingModuleMtTestOldApi* fixture =
509 reinterpret_cast<AudioCodingModuleMtTestOldApi*>(context);
510 while (!fixture->quit_.load()) {
511 fixture->CbInsertPacketImpl();
512 }
513 }
514
CbInsertPacketImpl()515 void CbInsertPacketImpl() {
516 SleepMs(1);
517 {
518 MutexLock lock(&mutex_);
519 if (clock_->TimeInMilliseconds() < next_insert_packet_time_ms_) {
520 return;
521 }
522 next_insert_packet_time_ms_ += 10;
523 }
524 // Now we're not holding the crit sect when calling ACM.
525 ++insert_packet_count_;
526 InsertPacket();
527 }
528
CbPullAudioThread(void * context)529 static void CbPullAudioThread(void* context) {
530 AudioCodingModuleMtTestOldApi* fixture =
531 reinterpret_cast<AudioCodingModuleMtTestOldApi*>(context);
532 while (!fixture->quit_.load()) {
533 fixture->CbPullAudioImpl();
534 }
535 }
536
CbPullAudioImpl()537 void CbPullAudioImpl() {
538 SleepMs(1);
539 {
540 MutexLock lock(&mutex_);
541 // Don't let the insert thread fall behind.
542 if (next_insert_packet_time_ms_ < clock_->TimeInMilliseconds()) {
543 return;
544 }
545 ++pull_audio_count_;
546 }
547 // Now we're not holding the crit sect when calling ACM.
548 PullAudio();
549 fake_clock_->AdvanceTimeMilliseconds(10);
550 }
551
552 rtc::PlatformThread send_thread_;
553 rtc::PlatformThread insert_packet_thread_;
554 rtc::PlatformThread pull_audio_thread_;
555 // Used to force worker threads to stop looping.
556 std::atomic<bool> quit_;
557
558 rtc::Event test_complete_;
559 int send_count_;
560 int insert_packet_count_;
561 int pull_audio_count_ RTC_GUARDED_BY(mutex_);
562 Mutex mutex_;
563 int64_t next_insert_packet_time_ms_ RTC_GUARDED_BY(mutex_);
564 std::unique_ptr<SimulatedClock> fake_clock_;
565 };
566
567 #if defined(WEBRTC_IOS)
568 #define MAYBE_DoTest DISABLED_DoTest
569 #else
570 #define MAYBE_DoTest DoTest
571 #endif
TEST_F(AudioCodingModuleMtTestOldApi,MAYBE_DoTest)572 TEST_F(AudioCodingModuleMtTestOldApi, MAYBE_DoTest) {
573 EXPECT_TRUE(RunTest());
574 }
575
576 // This is a multi-threaded ACM test using iSAC. The test encodes audio
577 // from a PCM file. The most recent encoded frame is used as input to the
578 // receiving part. Depending on timing, it may happen that the same RTP packet
579 // is inserted into the receiver multiple times, but this is a valid use-case,
580 // and simplifies the test code a lot.
581 class AcmIsacMtTestOldApi : public AudioCodingModuleMtTestOldApi {
582 protected:
583 static const int kNumPackets = 500;
584 static const int kNumPullCalls = 500;
585
AcmIsacMtTestOldApi()586 AcmIsacMtTestOldApi()
587 : AudioCodingModuleMtTestOldApi(), last_packet_number_(0) {}
588
~AcmIsacMtTestOldApi()589 ~AcmIsacMtTestOldApi() {}
590
SetUp()591 void SetUp() override {
592 AudioCodingModuleTestOldApi::SetUp();
593 RegisterCodec(); // Must be called before the threads start below.
594
595 // Set up input audio source to read from specified file, loop after 5
596 // seconds, and deliver blocks of 10 ms.
597 const std::string input_file_name =
598 webrtc::test::ResourcePath("audio_coding/speech_mono_16kHz", "pcm");
599 audio_loop_.Init(input_file_name, 5 * kSampleRateHz, kNumSamples10ms);
600
601 // Generate one packet to have something to insert.
602 int loop_counter = 0;
603 while (packet_cb_.last_payload_len_bytes() == 0) {
604 InsertAudio();
605 ASSERT_LT(loop_counter++, 10);
606 }
607 // Set |last_packet_number_| to one less that |num_calls| so that the packet
608 // will be fetched in the next InsertPacket() call.
609 last_packet_number_ = packet_cb_.num_calls() - 1;
610
611 StartThreads();
612 }
613
RegisterCodec()614 void RegisterCodec() override {
615 static_assert(kSampleRateHz == 16000, "test designed for iSAC 16 kHz");
616 audio_format_ = SdpAudioFormat("isac", kSampleRateHz, 1);
617 pac_size_ = 480;
618
619 // Register iSAC codec in ACM, effectively unregistering the PCM16B codec
620 // registered in AudioCodingModuleTestOldApi::SetUp();
621 acm_->SetReceiveCodecs({{kPayloadType, *audio_format_}});
622 acm_->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
623 kPayloadType, *audio_format_, absl::nullopt));
624 }
625
InsertPacket()626 void InsertPacket() override {
627 int num_calls = packet_cb_.num_calls(); // Store locally for thread safety.
628 if (num_calls > last_packet_number_) {
629 // Get the new payload out from the callback handler.
630 // Note that since we swap buffers here instead of directly inserting
631 // a pointer to the data in |packet_cb_|, we avoid locking the callback
632 // for the duration of the IncomingPacket() call.
633 packet_cb_.SwapBuffers(&last_payload_vec_);
634 ASSERT_GT(last_payload_vec_.size(), 0u);
635 rtp_utility_->Forward(&rtp_header_);
636 last_packet_number_ = num_calls;
637 }
638 ASSERT_GT(last_payload_vec_.size(), 0u);
639 ASSERT_EQ(0, acm_->IncomingPacket(&last_payload_vec_[0],
640 last_payload_vec_.size(), rtp_header_));
641 }
642
InsertAudio()643 void InsertAudio() override {
644 // TODO(kwiberg): Use std::copy here. Might be complications because AFAICS
645 // this call confuses the number of samples with the number of bytes, and
646 // ends up copying only half of what it should.
647 memcpy(input_frame_.mutable_data(), audio_loop_.GetNextBlock().data(),
648 kNumSamples10ms);
649 AudioCodingModuleTestOldApi::InsertAudio();
650 }
651
652 // Override the verification function with no-op, since iSAC produces variable
653 // payload sizes.
VerifyEncoding()654 void VerifyEncoding() override {}
655
656 // This method is the same as AudioCodingModuleMtTestOldApi::TestDone(), but
657 // here it is using the constants defined in this class (i.e., shorter test
658 // run).
TestDone()659 bool TestDone() override {
660 if (packet_cb_.num_calls() > kNumPackets) {
661 MutexLock lock(&mutex_);
662 if (pull_audio_count_ > kNumPullCalls) {
663 // Both conditions for completion are met. End the test.
664 return true;
665 }
666 }
667 return false;
668 }
669
670 int last_packet_number_;
671 std::vector<uint8_t> last_payload_vec_;
672 test::AudioLoop audio_loop_;
673 };
674
675 #if defined(WEBRTC_IOS)
676 #define MAYBE_DoTest DISABLED_DoTest
677 #else
678 #define MAYBE_DoTest DoTest
679 #endif
680 #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
TEST_F(AcmIsacMtTestOldApi,MAYBE_DoTest)681 TEST_F(AcmIsacMtTestOldApi, MAYBE_DoTest) {
682 EXPECT_TRUE(RunTest());
683 }
684 #endif
685
686 class AcmReRegisterIsacMtTestOldApi : public AudioCodingModuleTestOldApi {
687 protected:
688 static const int kRegisterAfterNumPackets = 5;
689 static const int kNumPackets = 10;
690 static const int kPacketSizeMs = 30;
691 static const int kPacketSizeSamples = kPacketSizeMs * 16;
692
AcmReRegisterIsacMtTestOldApi()693 AcmReRegisterIsacMtTestOldApi()
694 : AudioCodingModuleTestOldApi(),
695 receive_thread_(CbReceiveThread,
696 this,
697 "receive",
698 rtc::kRealtimePriority),
699 codec_registration_thread_(CbCodecRegistrationThread,
700 this,
701 "codec_registration",
702 rtc::kRealtimePriority),
703 codec_registered_(false),
704 receive_packet_count_(0),
705 next_insert_packet_time_ms_(0),
706 fake_clock_(new SimulatedClock(0)) {
707 AudioEncoderIsacFloatImpl::Config config;
708 config.payload_type = kPayloadType;
709 isac_encoder_.reset(new AudioEncoderIsacFloatImpl(config));
710 clock_ = fake_clock_.get();
711 }
712
SetUp()713 void SetUp() override {
714 AudioCodingModuleTestOldApi::SetUp();
715 // Set up input audio source to read from specified file, loop after 5
716 // seconds, and deliver blocks of 10 ms.
717 const std::string input_file_name =
718 webrtc::test::ResourcePath("audio_coding/speech_mono_16kHz", "pcm");
719 audio_loop_.Init(input_file_name, 5 * kSampleRateHz, kNumSamples10ms);
720 RegisterCodec(); // Must be called before the threads start below.
721 StartThreads();
722 }
723
RegisterCodec()724 void RegisterCodec() override {
725 // Register iSAC codec in ACM, effectively unregistering the PCM16B codec
726 // registered in AudioCodingModuleTestOldApi::SetUp();
727 // Only register the decoder for now. The encoder is registered later.
728 static_assert(kSampleRateHz == 16000, "test designed for iSAC 16 kHz");
729 acm_->SetReceiveCodecs({{kPayloadType, {"ISAC", kSampleRateHz, 1}}});
730 }
731
StartThreads()732 void StartThreads() {
733 quit_.store(false);
734 receive_thread_.Start();
735 codec_registration_thread_.Start();
736 }
737
TearDown()738 void TearDown() override {
739 AudioCodingModuleTestOldApi::TearDown();
740 quit_.store(true);
741 receive_thread_.Stop();
742 codec_registration_thread_.Stop();
743 }
744
RunTest()745 bool RunTest() {
746 return test_complete_.Wait(10 * 60 * 1000); // 10 minutes' timeout.
747 }
748
CbReceiveThread(void * context)749 static void CbReceiveThread(void* context) {
750 AcmReRegisterIsacMtTestOldApi* fixture =
751 reinterpret_cast<AcmReRegisterIsacMtTestOldApi*>(context);
752 while (!fixture->quit_.load() && fixture->CbReceiveImpl()) {
753 }
754 }
755
CbReceiveImpl()756 bool CbReceiveImpl() {
757 SleepMs(1);
758 rtc::Buffer encoded;
759 AudioEncoder::EncodedInfo info;
760 {
761 MutexLock lock(&mutex_);
762 if (clock_->TimeInMilliseconds() < next_insert_packet_time_ms_) {
763 return true;
764 }
765 next_insert_packet_time_ms_ += kPacketSizeMs;
766 ++receive_packet_count_;
767
768 // Encode new frame.
769 uint32_t input_timestamp = rtp_header_.timestamp;
770 while (info.encoded_bytes == 0) {
771 info = isac_encoder_->Encode(input_timestamp,
772 audio_loop_.GetNextBlock(), &encoded);
773 input_timestamp += 160; // 10 ms at 16 kHz.
774 }
775 EXPECT_EQ(rtp_header_.timestamp + kPacketSizeSamples, input_timestamp);
776 EXPECT_EQ(rtp_header_.timestamp, info.encoded_timestamp);
777 EXPECT_EQ(rtp_header_.payloadType, info.payload_type);
778 }
779 // Now we're not holding the crit sect when calling ACM.
780
781 // Insert into ACM.
782 EXPECT_EQ(0, acm_->IncomingPacket(encoded.data(), info.encoded_bytes,
783 rtp_header_));
784
785 // Pull audio.
786 for (int i = 0; i < rtc::CheckedDivExact(kPacketSizeMs, 10); ++i) {
787 AudioFrame audio_frame;
788 bool muted;
789 EXPECT_EQ(0, acm_->PlayoutData10Ms(-1 /* default output frequency */,
790 &audio_frame, &muted));
791 if (muted) {
792 ADD_FAILURE();
793 return false;
794 }
795 fake_clock_->AdvanceTimeMilliseconds(10);
796 }
797 rtp_utility_->Forward(&rtp_header_);
798 return true;
799 }
800
CbCodecRegistrationThread(void * context)801 static void CbCodecRegistrationThread(void* context) {
802 AcmReRegisterIsacMtTestOldApi* fixture =
803 reinterpret_cast<AcmReRegisterIsacMtTestOldApi*>(context);
804 while (!fixture->quit_.load()) {
805 fixture->CbCodecRegistrationImpl();
806 }
807 }
808
CbCodecRegistrationImpl()809 void CbCodecRegistrationImpl() {
810 SleepMs(1);
811 if (HasFatalFailure()) {
812 // End the test early if a fatal failure (ASSERT_*) has occurred.
813 test_complete_.Set();
814 }
815 MutexLock lock(&mutex_);
816 if (!codec_registered_ &&
817 receive_packet_count_ > kRegisterAfterNumPackets) {
818 // Register the iSAC encoder.
819 acm_->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
820 kPayloadType, *audio_format_, absl::nullopt));
821 codec_registered_ = true;
822 }
823 if (codec_registered_ && receive_packet_count_ > kNumPackets) {
824 test_complete_.Set();
825 }
826 }
827
828 rtc::PlatformThread receive_thread_;
829 rtc::PlatformThread codec_registration_thread_;
830 // Used to force worker threads to stop looping.
831 std::atomic<bool> quit_;
832
833 rtc::Event test_complete_;
834 Mutex mutex_;
835 bool codec_registered_ RTC_GUARDED_BY(mutex_);
836 int receive_packet_count_ RTC_GUARDED_BY(mutex_);
837 int64_t next_insert_packet_time_ms_ RTC_GUARDED_BY(mutex_);
838 std::unique_ptr<AudioEncoderIsacFloatImpl> isac_encoder_;
839 std::unique_ptr<SimulatedClock> fake_clock_;
840 test::AudioLoop audio_loop_;
841 };
842
843 #if defined(WEBRTC_IOS)
844 #define MAYBE_DoTest DISABLED_DoTest
845 #else
846 #define MAYBE_DoTest DoTest
847 #endif
848 #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
TEST_F(AcmReRegisterIsacMtTestOldApi,MAYBE_DoTest)849 TEST_F(AcmReRegisterIsacMtTestOldApi, MAYBE_DoTest) {
850 EXPECT_TRUE(RunTest());
851 }
852 #endif
853
854 // Disabling all of these tests on iOS until file support has been added.
855 // See https://code.google.com/p/webrtc/issues/detail?id=4752 for details.
856 #if !defined(WEBRTC_IOS)
857
858 class AcmReceiverBitExactnessOldApi : public ::testing::Test {
859 public:
PlatformChecksum(std::string others,std::string win64,std::string android_arm32,std::string android_arm64,std::string android_arm64_clang)860 static std::string PlatformChecksum(std::string others,
861 std::string win64,
862 std::string android_arm32,
863 std::string android_arm64,
864 std::string android_arm64_clang) {
865 #if defined(_WIN32) && defined(WEBRTC_ARCH_64_BITS)
866 return win64;
867 #elif defined(WEBRTC_ANDROID) && defined(WEBRTC_ARCH_ARM)
868 return android_arm32;
869 #elif defined(WEBRTC_ANDROID) && defined(WEBRTC_ARCH_ARM64)
870 #if defined(__clang__)
871 // Android ARM64 with Clang compiler
872 return android_arm64_clang;
873 #else
874 // Android ARM64 with non-Clang compiler
875 return android_arm64;
876 #endif // __clang__
877 #else
878 return others;
879 #endif
880 }
881
882 protected:
883 struct ExternalDecoder {
884 int rtp_payload_type;
885 AudioDecoder* external_decoder;
886 int sample_rate_hz;
887 int num_channels;
888 std::string name;
889 };
890
Run(int output_freq_hz,const std::string & checksum_ref)891 void Run(int output_freq_hz, const std::string& checksum_ref) {
892 Run(output_freq_hz, checksum_ref, CreateBuiltinAudioDecoderFactory(),
893 [](AudioCodingModule*) {});
894 }
895
Run(int output_freq_hz,const std::string & checksum_ref,rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,rtc::FunctionView<void (AudioCodingModule *)> decoder_reg)896 void Run(int output_freq_hz,
897 const std::string& checksum_ref,
898 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
899 rtc::FunctionView<void(AudioCodingModule*)> decoder_reg) {
900 const std::string input_file_name =
901 webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp");
902 std::unique_ptr<test::RtpFileSource> packet_source(
903 test::RtpFileSource::Create(input_file_name));
904 #ifdef WEBRTC_ANDROID
905 // Filter out iLBC and iSAC-swb since they are not supported on Android.
906 packet_source->FilterOutPayloadType(102); // iLBC.
907 packet_source->FilterOutPayloadType(104); // iSAC-swb.
908 #endif
909
910 test::AudioChecksum checksum;
911 const std::string output_file_name =
912 webrtc::test::OutputPath() +
913 ::testing::UnitTest::GetInstance()
914 ->current_test_info()
915 ->test_case_name() +
916 "_" + ::testing::UnitTest::GetInstance()->current_test_info()->name() +
917 "_output.wav";
918 test::OutputWavFile output_file(output_file_name, output_freq_hz, 1);
919 test::AudioSinkFork output(&checksum, &output_file);
920
921 test::AcmReceiveTestOldApi test(
922 packet_source.get(), &output, output_freq_hz,
923 test::AcmReceiveTestOldApi::kArbitraryChannels,
924 std::move(decoder_factory));
925 ASSERT_NO_FATAL_FAILURE(test.RegisterNetEqTestCodecs());
926 decoder_reg(test.get_acm());
927 test.Run();
928
929 std::string checksum_string = checksum.Finish();
930 EXPECT_EQ(checksum_ref, checksum_string);
931
932 // Delete the output file.
933 remove(output_file_name.c_str());
934 }
935 };
936
937 #if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \
938 defined(WEBRTC_CODEC_ILBC)
939 TEST_F(AcmReceiverBitExactnessOldApi, 8kHzOutput) {
940 Run(8000, PlatformChecksum("6c204b289486b0695b08a9e94fab1948",
941 "ff5ffee2ee92f8fe61d9f2010b8a68a3",
942 "53494a96f3db4a5b07d723e0cbac0ad7",
943 "4598140b5e4f7ee66c5adad609e65a3e",
944 "516c2859126ea4913f30d51af4a4f3dc"));
945 }
946
947 TEST_F(AcmReceiverBitExactnessOldApi, 16kHzOutput) {
948 Run(16000, PlatformChecksum("226dbdbce2354399c6df05371042cda3",
949 "9c80bf5ec496c41ce8112e1523bf8c83",
950 "11a6f170fdaffa81a2948af121f370af",
951 "f2aad418af974a3b1694d5ae5cc2c3c7",
952 "6133301a18be95c416984182816d859f"));
953 }
954
955 TEST_F(AcmReceiverBitExactnessOldApi, 32kHzOutput) {
956 Run(32000, PlatformChecksum("f94665cc0e904d5d5cf0394e30ee4edd",
957 "697934bcf0849f80d76ce20854161220",
958 "3609aa5288c1d512e8e652ceabecb495",
959 "100869c8dcde51346c2073e52a272d98",
960 "55363bc9cdda6464a58044919157827b"));
961 }
962
963 TEST_F(AcmReceiverBitExactnessOldApi, 48kHzOutput) {
964 Run(48000, PlatformChecksum("2955d0b83602541fd92d9b820ebce68d",
965 "f4a8386a6a49439ced60ed9a7c7f75fd",
966 "d8169dfeba708b5212bdc365e08aee9d",
967 "bd44bf97e7899186532f91235cef444d",
968 "47594deaab5d9166cfbf577203b2563e"));
969 }
970
971 TEST_F(AcmReceiverBitExactnessOldApi, 48kHzOutputExternalDecoder) {
972 class ADFactory : public AudioDecoderFactory {
973 public:
ADFactory()974 ADFactory()
975 : mock_decoder_(new MockAudioDecoder()),
976 pcmu_decoder_(1),
977 decode_forwarder_(&pcmu_decoder_),
978 fact_(CreateBuiltinAudioDecoderFactory()) {
979 // Set expectations on the mock decoder and also delegate the calls to
980 // the real decoder.
981 EXPECT_CALL(*mock_decoder_, SampleRateHz())
982 .Times(AtLeast(1))
983 .WillRepeatedly(
984 Invoke(&pcmu_decoder_, &AudioDecoderPcmU::SampleRateHz));
985 EXPECT_CALL(*mock_decoder_, Channels())
986 .Times(AtLeast(1))
987 .WillRepeatedly(Invoke(&pcmu_decoder_, &AudioDecoderPcmU::Channels));
988 EXPECT_CALL(*mock_decoder_, DecodeInternal(_, _, _, _, _))
989 .Times(AtLeast(1))
990 .WillRepeatedly(Invoke(&decode_forwarder_, &DecodeForwarder::Decode));
991 EXPECT_CALL(*mock_decoder_, HasDecodePlc())
992 .Times(AtLeast(1))
993 .WillRepeatedly(
994 Invoke(&pcmu_decoder_, &AudioDecoderPcmU::HasDecodePlc));
995 EXPECT_CALL(*mock_decoder_, PacketDuration(_, _))
996 .Times(AtLeast(1))
997 .WillRepeatedly(
998 Invoke(&pcmu_decoder_, &AudioDecoderPcmU::PacketDuration));
999 EXPECT_CALL(*mock_decoder_, Die());
1000 }
GetSupportedDecoders()1001 std::vector<AudioCodecSpec> GetSupportedDecoders() override {
1002 return fact_->GetSupportedDecoders();
1003 }
IsSupportedDecoder(const SdpAudioFormat & format)1004 bool IsSupportedDecoder(const SdpAudioFormat& format) override {
1005 return format.name == "MockPCMu" ? true
1006 : fact_->IsSupportedDecoder(format);
1007 }
MakeAudioDecoder(const SdpAudioFormat & format,absl::optional<AudioCodecPairId> codec_pair_id)1008 std::unique_ptr<AudioDecoder> MakeAudioDecoder(
1009 const SdpAudioFormat& format,
1010 absl::optional<AudioCodecPairId> codec_pair_id) override {
1011 return format.name == "MockPCMu"
1012 ? std::move(mock_decoder_)
1013 : fact_->MakeAudioDecoder(format, codec_pair_id);
1014 }
1015
1016 private:
1017 // Class intended to forward a call from a mock DecodeInternal to Decode on
1018 // the real decoder's Decode. DecodeInternal for the real decoder isn't
1019 // public.
1020 class DecodeForwarder {
1021 public:
DecodeForwarder(AudioDecoder * decoder)1022 explicit DecodeForwarder(AudioDecoder* decoder) : decoder_(decoder) {}
Decode(const uint8_t * encoded,size_t encoded_len,int sample_rate_hz,int16_t * decoded,AudioDecoder::SpeechType * speech_type)1023 int Decode(const uint8_t* encoded,
1024 size_t encoded_len,
1025 int sample_rate_hz,
1026 int16_t* decoded,
1027 AudioDecoder::SpeechType* speech_type) {
1028 return decoder_->Decode(encoded, encoded_len, sample_rate_hz,
1029 decoder_->PacketDuration(encoded, encoded_len) *
1030 decoder_->Channels() * sizeof(int16_t),
1031 decoded, speech_type);
1032 }
1033
1034 private:
1035 AudioDecoder* const decoder_;
1036 };
1037
1038 std::unique_ptr<MockAudioDecoder> mock_decoder_;
1039 AudioDecoderPcmU pcmu_decoder_;
1040 DecodeForwarder decode_forwarder_;
1041 rtc::scoped_refptr<AudioDecoderFactory> fact_; // Fallback factory.
1042 };
1043
1044 rtc::scoped_refptr<rtc::RefCountedObject<ADFactory>> factory(
1045 new rtc::RefCountedObject<ADFactory>);
1046 Run(48000,
1047 PlatformChecksum("2955d0b83602541fd92d9b820ebce68d",
1048 "f4a8386a6a49439ced60ed9a7c7f75fd",
1049 "d8169dfeba708b5212bdc365e08aee9d",
1050 "bd44bf97e7899186532f91235cef444d",
1051 "47594deaab5d9166cfbf577203b2563e"),
__anon30f8ba990602(AudioCodingModule* acm) 1052 factory, [](AudioCodingModule* acm) {
1053 acm->SetReceiveCodecs({{0, {"MockPCMu", 8000, 1}},
1054 {103, {"ISAC", 16000, 1}},
1055 {104, {"ISAC", 32000, 1}},
1056 {93, {"L16", 8000, 1}},
1057 {94, {"L16", 16000, 1}},
1058 {95, {"L16", 32000, 1}},
1059 {8, {"PCMA", 8000, 1}},
1060 {102, {"ILBC", 8000, 1}},
1061 {13, {"CN", 8000, 1}},
1062 {98, {"CN", 16000, 1}},
1063 {99, {"CN", 32000, 1}}});
1064 });
1065 }
1066 #endif
1067
1068 // This test verifies bit exactness for the send-side of ACM. The test setup is
1069 // a chain of three different test classes:
1070 //
1071 // test::AcmSendTest -> AcmSenderBitExactness -> test::AcmReceiveTest
1072 //
1073 // The receiver side is driving the test by requesting new packets from
1074 // AcmSenderBitExactness::NextPacket(). This method, in turn, asks for the
1075 // packet from test::AcmSendTest::NextPacket, which inserts audio from the
1076 // input file until one packet is produced. (The input file loops indefinitely.)
1077 // Before passing the packet to the receiver, this test class verifies the
1078 // packet header and updates a payload checksum with the new payload. The
1079 // decoded output from the receiver is also verified with a (separate) checksum.
1080 class AcmSenderBitExactnessOldApi : public ::testing::Test,
1081 public test::PacketSource {
1082 protected:
1083 static const int kTestDurationMs = 1000;
1084
AcmSenderBitExactnessOldApi()1085 AcmSenderBitExactnessOldApi()
1086 : frame_size_rtp_timestamps_(0),
1087 packet_count_(0),
1088 payload_type_(0),
1089 last_sequence_number_(0),
1090 last_timestamp_(0),
1091 payload_checksum_(rtc::MessageDigestFactory::Create(rtc::DIGEST_MD5)) {}
1092
1093 // Sets up the test::AcmSendTest object. Returns true on success, otherwise
1094 // false.
SetUpSender(std::string input_file_name,int source_rate)1095 bool SetUpSender(std::string input_file_name, int source_rate) {
1096 // Note that |audio_source_| will loop forever. The test duration is set
1097 // explicitly by |kTestDurationMs|.
1098 audio_source_.reset(new test::InputAudioFile(input_file_name));
1099 send_test_.reset(new test::AcmSendTestOldApi(audio_source_.get(),
1100 source_rate, kTestDurationMs));
1101 return send_test_.get() != NULL;
1102 }
1103
1104 // Registers a send codec in the test::AcmSendTest object. Returns true on
1105 // success, false on failure.
RegisterSendCodec(const char * payload_name,int sampling_freq_hz,int channels,int payload_type,int frame_size_samples,int frame_size_rtp_timestamps)1106 bool RegisterSendCodec(const char* payload_name,
1107 int sampling_freq_hz,
1108 int channels,
1109 int payload_type,
1110 int frame_size_samples,
1111 int frame_size_rtp_timestamps) {
1112 payload_type_ = payload_type;
1113 frame_size_rtp_timestamps_ = frame_size_rtp_timestamps;
1114 return send_test_->RegisterCodec(payload_name, sampling_freq_hz, channels,
1115 payload_type, frame_size_samples);
1116 }
1117
RegisterExternalSendCodec(std::unique_ptr<AudioEncoder> external_speech_encoder,int payload_type)1118 void RegisterExternalSendCodec(
1119 std::unique_ptr<AudioEncoder> external_speech_encoder,
1120 int payload_type) {
1121 payload_type_ = payload_type;
1122 frame_size_rtp_timestamps_ = rtc::checked_cast<uint32_t>(
1123 external_speech_encoder->Num10MsFramesInNextPacket() *
1124 external_speech_encoder->RtpTimestampRateHz() / 100);
1125 send_test_->RegisterExternalCodec(std::move(external_speech_encoder));
1126 }
1127
1128 // Runs the test. SetUpSender() and RegisterSendCodec() must have been called
1129 // before calling this method.
Run(const std::string & audio_checksum_ref,const std::string & payload_checksum_ref,int expected_packets,test::AcmReceiveTestOldApi::NumOutputChannels expected_channels,rtc::scoped_refptr<AudioDecoderFactory> decoder_factory=nullptr)1130 void Run(const std::string& audio_checksum_ref,
1131 const std::string& payload_checksum_ref,
1132 int expected_packets,
1133 test::AcmReceiveTestOldApi::NumOutputChannels expected_channels,
1134 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory = nullptr) {
1135 if (!decoder_factory) {
1136 decoder_factory = CreateBuiltinAudioDecoderFactory();
1137 }
1138 // Set up the receiver used to decode the packets and verify the decoded
1139 // output.
1140 test::AudioChecksum audio_checksum;
1141 const std::string output_file_name =
1142 webrtc::test::OutputPath() +
1143 ::testing::UnitTest::GetInstance()
1144 ->current_test_info()
1145 ->test_case_name() +
1146 "_" + ::testing::UnitTest::GetInstance()->current_test_info()->name() +
1147 "_output.wav";
1148 const int kOutputFreqHz = 8000;
1149 test::OutputWavFile output_file(output_file_name, kOutputFreqHz,
1150 expected_channels);
1151 // Have the output audio sent both to file and to the checksum calculator.
1152 test::AudioSinkFork output(&audio_checksum, &output_file);
1153 test::AcmReceiveTestOldApi receive_test(this, &output, kOutputFreqHz,
1154 expected_channels, decoder_factory);
1155 ASSERT_NO_FATAL_FAILURE(receive_test.RegisterDefaultCodecs());
1156
1157 // This is where the actual test is executed.
1158 receive_test.Run();
1159
1160 // Extract and verify the audio checksum.
1161 std::string checksum_string = audio_checksum.Finish();
1162 ExpectChecksumEq(audio_checksum_ref, checksum_string);
1163
1164 // Extract and verify the payload checksum.
1165 rtc::Buffer checksum_result(payload_checksum_->Size());
1166 payload_checksum_->Finish(checksum_result.data(), checksum_result.size());
1167 checksum_string =
1168 rtc::hex_encode(checksum_result.data<char>(), checksum_result.size());
1169 ExpectChecksumEq(payload_checksum_ref, checksum_string);
1170
1171 // Verify number of packets produced.
1172 EXPECT_EQ(expected_packets, packet_count_);
1173
1174 // Delete the output file.
1175 remove(output_file_name.c_str());
1176 }
1177
1178 // Helper: result must be one the "|"-separated checksums.
ExpectChecksumEq(std::string ref,std::string result)1179 void ExpectChecksumEq(std::string ref, std::string result) {
1180 if (ref.size() == result.size()) {
1181 // Only one checksum: clearer message.
1182 EXPECT_EQ(ref, result);
1183 } else {
1184 EXPECT_NE(ref.find(result), std::string::npos)
1185 << result << " must be one of these:\n"
1186 << ref;
1187 }
1188 }
1189
1190 // Inherited from test::PacketSource.
NextPacket()1191 std::unique_ptr<test::Packet> NextPacket() override {
1192 auto packet = send_test_->NextPacket();
1193 if (!packet)
1194 return NULL;
1195
1196 VerifyPacket(packet.get());
1197 // TODO(henrik.lundin) Save the packet to file as well.
1198
1199 // Pass it on to the caller. The caller becomes the owner of |packet|.
1200 return packet;
1201 }
1202
1203 // Verifies the packet.
VerifyPacket(const test::Packet * packet)1204 void VerifyPacket(const test::Packet* packet) {
1205 EXPECT_TRUE(packet->valid_header());
1206 // (We can check the header fields even if valid_header() is false.)
1207 EXPECT_EQ(payload_type_, packet->header().payloadType);
1208 if (packet_count_ > 0) {
1209 // This is not the first packet.
1210 uint16_t sequence_number_diff =
1211 packet->header().sequenceNumber - last_sequence_number_;
1212 EXPECT_EQ(1, sequence_number_diff);
1213 uint32_t timestamp_diff = packet->header().timestamp - last_timestamp_;
1214 EXPECT_EQ(frame_size_rtp_timestamps_, timestamp_diff);
1215 }
1216 ++packet_count_;
1217 last_sequence_number_ = packet->header().sequenceNumber;
1218 last_timestamp_ = packet->header().timestamp;
1219 // Update the checksum.
1220 payload_checksum_->Update(packet->payload(),
1221 packet->payload_length_bytes());
1222 }
1223
SetUpTest(const char * codec_name,int codec_sample_rate_hz,int channels,int payload_type,int codec_frame_size_samples,int codec_frame_size_rtp_timestamps)1224 void SetUpTest(const char* codec_name,
1225 int codec_sample_rate_hz,
1226 int channels,
1227 int payload_type,
1228 int codec_frame_size_samples,
1229 int codec_frame_size_rtp_timestamps) {
1230 ASSERT_TRUE(SetUpSender(
1231 channels == 1 ? kTestFileMono32kHz : kTestFileFakeStereo32kHz, 32000));
1232 ASSERT_TRUE(RegisterSendCodec(codec_name, codec_sample_rate_hz, channels,
1233 payload_type, codec_frame_size_samples,
1234 codec_frame_size_rtp_timestamps));
1235 }
1236
SetUpTestExternalEncoder(std::unique_ptr<AudioEncoder> external_speech_encoder,int payload_type)1237 void SetUpTestExternalEncoder(
1238 std::unique_ptr<AudioEncoder> external_speech_encoder,
1239 int payload_type) {
1240 ASSERT_TRUE(send_test_);
1241 RegisterExternalSendCodec(std::move(external_speech_encoder), payload_type);
1242 }
1243
1244 std::unique_ptr<test::AcmSendTestOldApi> send_test_;
1245 std::unique_ptr<test::InputAudioFile> audio_source_;
1246 uint32_t frame_size_rtp_timestamps_;
1247 int packet_count_;
1248 uint8_t payload_type_;
1249 uint16_t last_sequence_number_;
1250 uint32_t last_timestamp_;
1251 std::unique_ptr<rtc::MessageDigest> payload_checksum_;
1252 const std::string kTestFileMono32kHz =
1253 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
1254 const std::string kTestFileFakeStereo32kHz =
1255 webrtc::test::ResourcePath("audio_coding/testfile_fake_stereo_32kHz",
1256 "pcm");
1257 const std::string kTestFileQuad48kHz = webrtc::test::ResourcePath(
1258 "audio_coding/speech_4_channels_48k_one_second",
1259 "wav");
1260 };
1261
1262 class AcmSenderBitExactnessNewApi : public AcmSenderBitExactnessOldApi {};
1263
1264 #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)
TEST_F(AcmSenderBitExactnessOldApi,IsacWb30ms)1265 TEST_F(AcmSenderBitExactnessOldApi, IsacWb30ms) {
1266 ASSERT_NO_FATAL_FAILURE(SetUpTest("ISAC", 16000, 1, 103, 480, 480));
1267 Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
1268 "2c9cb15d4ed55b5a0cadd04883bc73b0",
1269 "9336a9b993cbd8a751f0e8958e66c89c",
1270 "5c2eb46199994506236f68b2c8e51b0d",
1271 "343f1f42be0607c61e6516aece424609",
1272 "2c9cb15d4ed55b5a0cadd04883bc73b0"),
1273 AcmReceiverBitExactnessOldApi::PlatformChecksum(
1274 "3c79f16f34218271f3dca4e2b1dfe1bb",
1275 "d42cb5195463da26c8129bbfe73a22e6",
1276 "83de248aea9c3c2bd680b6952401b4ca",
1277 "3c79f16f34218271f3dca4e2b1dfe1bb",
1278 "3c79f16f34218271f3dca4e2b1dfe1bb"),
1279 33, test::AcmReceiveTestOldApi::kMonoOutput);
1280 }
1281
TEST_F(AcmSenderBitExactnessOldApi,IsacWb60ms)1282 TEST_F(AcmSenderBitExactnessOldApi, IsacWb60ms) {
1283 ASSERT_NO_FATAL_FAILURE(SetUpTest("ISAC", 16000, 1, 103, 960, 960));
1284 Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
1285 "f59760fa000991ee5fa81f2e607db254",
1286 "986aa16d7097a26e32e212e39ec58517",
1287 "9a81e467eb1485f84aca796f8ea65011",
1288 "ef75e900e6f375e3061163c53fd09a63",
1289 "f59760fa000991ee5fa81f2e607db254"),
1290 AcmReceiverBitExactnessOldApi::PlatformChecksum(
1291 "9e0a0ab743ad987b55b8e14802769c56",
1292 "ebe04a819d3a9d83a83a17f271e1139a",
1293 "97aeef98553b5a4b5a68f8b716e8eaf0",
1294 "9e0a0ab743ad987b55b8e14802769c56",
1295 "9e0a0ab743ad987b55b8e14802769c56"),
1296 16, test::AcmReceiveTestOldApi::kMonoOutput);
1297 }
1298 #endif
1299
1300 #if defined(WEBRTC_ANDROID)
1301 #define MAYBE_IsacSwb30ms DISABLED_IsacSwb30ms
1302 #else
1303 #define MAYBE_IsacSwb30ms IsacSwb30ms
1304 #endif
1305 #if defined(WEBRTC_CODEC_ISAC)
TEST_F(AcmSenderBitExactnessOldApi,MAYBE_IsacSwb30ms)1306 TEST_F(AcmSenderBitExactnessOldApi, MAYBE_IsacSwb30ms) {
1307 ASSERT_NO_FATAL_FAILURE(SetUpTest("ISAC", 32000, 1, 104, 960, 960));
1308 Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
1309 "5683b58da0fbf2063c7adc2e6bfb3fb8",
1310 "2b3c387d06f00b7b7aad4c9be56fb83d", "android_arm32_audio",
1311 "android_arm64_audio", "android_arm64_clang_audio"),
1312 AcmReceiverBitExactnessOldApi::PlatformChecksum(
1313 "ce86106a93419aefb063097108ec94ab",
1314 "bcc2041e7744c7ebd9f701866856849c", "android_arm32_payload",
1315 "android_arm64_payload", "android_arm64_clang_payload"),
1316 33, test::AcmReceiveTestOldApi::kMonoOutput);
1317 }
1318 #endif
1319
TEST_F(AcmSenderBitExactnessOldApi,Pcm16_8000khz_10ms)1320 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_8000khz_10ms) {
1321 ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 8000, 1, 107, 80, 80));
1322 Run("de4a98e1406f8b798d99cd0704e862e2", "c1edd36339ce0326cc4550041ad719a0",
1323 100, test::AcmReceiveTestOldApi::kMonoOutput);
1324 }
1325
TEST_F(AcmSenderBitExactnessOldApi,Pcm16_16000khz_10ms)1326 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_16000khz_10ms) {
1327 ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 16000, 1, 108, 160, 160));
1328 Run("ae646d7b68384a1269cc080dd4501916", "ad786526383178b08d80d6eee06e9bad",
1329 100, test::AcmReceiveTestOldApi::kMonoOutput);
1330 }
1331
TEST_F(AcmSenderBitExactnessOldApi,Pcm16_32000khz_10ms)1332 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_32000khz_10ms) {
1333 ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 32000, 1, 109, 320, 320));
1334 Run("7fe325e8fbaf755e3c5df0b11a4774fb", "5ef82ea885e922263606c6fdbc49f651",
1335 100, test::AcmReceiveTestOldApi::kMonoOutput);
1336 }
1337
TEST_F(AcmSenderBitExactnessOldApi,Pcm16_stereo_8000khz_10ms)1338 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_stereo_8000khz_10ms) {
1339 ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 8000, 2, 111, 80, 80));
1340 Run("fb263b74e7ac3de915474d77e4744ceb", "62ce5adb0d4965d0a52ec98ae7f98974",
1341 100, test::AcmReceiveTestOldApi::kStereoOutput);
1342 }
1343
TEST_F(AcmSenderBitExactnessOldApi,Pcm16_stereo_16000khz_10ms)1344 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_stereo_16000khz_10ms) {
1345 ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 16000, 2, 112, 160, 160));
1346 Run("d09e9239553649d7ac93e19d304281fd", "41ca8edac4b8c71cd54fd9f25ec14870",
1347 100, test::AcmReceiveTestOldApi::kStereoOutput);
1348 }
1349
TEST_F(AcmSenderBitExactnessOldApi,Pcm16_stereo_32000khz_10ms)1350 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_stereo_32000khz_10ms) {
1351 ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 32000, 2, 113, 320, 320));
1352 Run("5f025d4f390982cc26b3d92fe02e3044", "50e58502fb04421bf5b857dda4c96879",
1353 100, test::AcmReceiveTestOldApi::kStereoOutput);
1354 }
1355
TEST_F(AcmSenderBitExactnessOldApi,Pcmu_20ms)1356 TEST_F(AcmSenderBitExactnessOldApi, Pcmu_20ms) {
1357 ASSERT_NO_FATAL_FAILURE(SetUpTest("PCMU", 8000, 1, 0, 160, 160));
1358 Run("81a9d4c0bb72e9becc43aef124c981e9", "8f9b8750bd80fe26b6cbf6659b89f0f9",
1359 50, test::AcmReceiveTestOldApi::kMonoOutput);
1360 }
1361
TEST_F(AcmSenderBitExactnessOldApi,Pcma_20ms)1362 TEST_F(AcmSenderBitExactnessOldApi, Pcma_20ms) {
1363 ASSERT_NO_FATAL_FAILURE(SetUpTest("PCMA", 8000, 1, 8, 160, 160));
1364 Run("39611f798969053925a49dc06d08de29", "6ad745e55aa48981bfc790d0eeef2dd1",
1365 50, test::AcmReceiveTestOldApi::kMonoOutput);
1366 }
1367
TEST_F(AcmSenderBitExactnessOldApi,Pcmu_stereo_20ms)1368 TEST_F(AcmSenderBitExactnessOldApi, Pcmu_stereo_20ms) {
1369 ASSERT_NO_FATAL_FAILURE(SetUpTest("PCMU", 8000, 2, 110, 160, 160));
1370 Run("437bec032fdc5cbaa0d5175430af7b18", "60b6f25e8d1e74cb679cfe756dd9bca5",
1371 50, test::AcmReceiveTestOldApi::kStereoOutput);
1372 }
1373
TEST_F(AcmSenderBitExactnessOldApi,Pcma_stereo_20ms)1374 TEST_F(AcmSenderBitExactnessOldApi, Pcma_stereo_20ms) {
1375 ASSERT_NO_FATAL_FAILURE(SetUpTest("PCMA", 8000, 2, 118, 160, 160));
1376 Run("a5c6d83c5b7cedbeff734238220a4b0c", "92b282c83efd20e7eeef52ba40842cf7",
1377 50, test::AcmReceiveTestOldApi::kStereoOutput);
1378 }
1379
1380 #if defined(WEBRTC_ANDROID)
1381 #define MAYBE_Ilbc_30ms DISABLED_Ilbc_30ms
1382 #else
1383 #define MAYBE_Ilbc_30ms Ilbc_30ms
1384 #endif
1385 #if defined(WEBRTC_CODEC_ILBC)
TEST_F(AcmSenderBitExactnessOldApi,MAYBE_Ilbc_30ms)1386 TEST_F(AcmSenderBitExactnessOldApi, MAYBE_Ilbc_30ms) {
1387 ASSERT_NO_FATAL_FAILURE(SetUpTest("ILBC", 8000, 1, 102, 240, 240));
1388 Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
1389 "7b6ec10910debd9af08011d3ed5249f7",
1390 "7b6ec10910debd9af08011d3ed5249f7", "android_arm32_audio",
1391 "android_arm64_audio", "android_arm64_clang_audio"),
1392 AcmReceiverBitExactnessOldApi::PlatformChecksum(
1393 "cfae2e9f6aba96e145f2bcdd5050ce78",
1394 "cfae2e9f6aba96e145f2bcdd5050ce78", "android_arm32_payload",
1395 "android_arm64_payload", "android_arm64_clang_payload"),
1396 33, test::AcmReceiveTestOldApi::kMonoOutput);
1397 }
1398 #endif
1399
1400 #if defined(WEBRTC_ANDROID)
1401 #define MAYBE_G722_20ms DISABLED_G722_20ms
1402 #else
1403 #define MAYBE_G722_20ms G722_20ms
1404 #endif
TEST_F(AcmSenderBitExactnessOldApi,MAYBE_G722_20ms)1405 TEST_F(AcmSenderBitExactnessOldApi, MAYBE_G722_20ms) {
1406 ASSERT_NO_FATAL_FAILURE(SetUpTest("G722", 16000, 1, 9, 320, 160));
1407 Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
1408 "e99c89be49a46325d03c0d990c292d68",
1409 "e99c89be49a46325d03c0d990c292d68", "android_arm32_audio",
1410 "android_arm64_audio", "android_arm64_clang_audio"),
1411 AcmReceiverBitExactnessOldApi::PlatformChecksum(
1412 "fc68a87e1380614e658087cb35d5ca10",
1413 "fc68a87e1380614e658087cb35d5ca10", "android_arm32_payload",
1414 "android_arm64_payload", "android_arm64_clang_payload"),
1415 50, test::AcmReceiveTestOldApi::kMonoOutput);
1416 }
1417
1418 #if defined(WEBRTC_ANDROID)
1419 #define MAYBE_G722_stereo_20ms DISABLED_G722_stereo_20ms
1420 #else
1421 #define MAYBE_G722_stereo_20ms G722_stereo_20ms
1422 #endif
TEST_F(AcmSenderBitExactnessOldApi,MAYBE_G722_stereo_20ms)1423 TEST_F(AcmSenderBitExactnessOldApi, MAYBE_G722_stereo_20ms) {
1424 ASSERT_NO_FATAL_FAILURE(SetUpTest("G722", 16000, 2, 119, 320, 160));
1425 Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
1426 "e280aed283e499d37091b481ca094807",
1427 "e280aed283e499d37091b481ca094807", "android_arm32_audio",
1428 "android_arm64_audio", "android_arm64_clang_audio"),
1429 AcmReceiverBitExactnessOldApi::PlatformChecksum(
1430 "66516152eeaa1e650ad94ff85f668dac",
1431 "66516152eeaa1e650ad94ff85f668dac", "android_arm32_payload",
1432 "android_arm64_payload", "android_arm64_clang_payload"),
1433 50, test::AcmReceiveTestOldApi::kStereoOutput);
1434 }
1435
1436 namespace {
1437 // Checksum depends on libopus being compiled with or without SSE.
1438 const std::string audio_maybe_sse =
1439 "e0ddf36854059151cdb7a0c4af3d282a"
1440 "|32574e78db4eab0c467d3c0785e3b484";
1441 const std::string payload_maybe_sse =
1442 "b43bdf7638b2bc2a5a6f30bdc640b9ed"
1443 "|c30d463e7ed10bdd1da9045f80561f27";
1444 // Common checksums.
1445 const std::string audio_checksum =
1446 AcmReceiverBitExactnessOldApi::PlatformChecksum(
1447 audio_maybe_sse,
1448 audio_maybe_sse,
1449 "6fcceb83acf427730570bc13eeac920c",
1450 "fd96f15d547c4e155daeeef4253b174e",
1451 "fd96f15d547c4e155daeeef4253b174e");
1452 const std::string payload_checksum =
1453 AcmReceiverBitExactnessOldApi::PlatformChecksum(
1454 payload_maybe_sse,
1455 payload_maybe_sse,
1456 "4bd846d0aa5656ecd5dfd85701a1b78c",
1457 "7efbfc9f8e3b4b2933ae2d01ab919028",
1458 "7efbfc9f8e3b4b2933ae2d01ab919028");
1459 } // namespace
1460
TEST_F(AcmSenderBitExactnessOldApi,Opus_stereo_20ms)1461 TEST_F(AcmSenderBitExactnessOldApi, Opus_stereo_20ms) {
1462 ASSERT_NO_FATAL_FAILURE(SetUpTest("opus", 48000, 2, 120, 960, 960));
1463 Run(audio_checksum, payload_checksum, 50,
1464 test::AcmReceiveTestOldApi::kStereoOutput);
1465 }
1466
TEST_F(AcmSenderBitExactnessNewApi,MAYBE_OpusFromFormat_stereo_20ms)1467 TEST_F(AcmSenderBitExactnessNewApi, MAYBE_OpusFromFormat_stereo_20ms) {
1468 const auto config = AudioEncoderOpus::SdpToConfig(
1469 SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}}));
1470 ASSERT_TRUE(SetUpSender(kTestFileFakeStereo32kHz, 32000));
1471 ASSERT_NO_FATAL_FAILURE(SetUpTestExternalEncoder(
1472 AudioEncoderOpus::MakeAudioEncoder(*config, 120), 120));
1473 Run(audio_checksum, payload_checksum, 50,
1474 test::AcmReceiveTestOldApi::kStereoOutput);
1475 }
1476
1477 // TODO(webrtc:8649): Disabled until the Encoder counterpart of
1478 // https://webrtc-review.googlesource.com/c/src/+/129768 lands.
TEST_F(AcmSenderBitExactnessNewApi,DISABLED_OpusManyChannels)1479 TEST_F(AcmSenderBitExactnessNewApi, DISABLED_OpusManyChannels) {
1480 constexpr int kNumChannels = 4;
1481 constexpr int kOpusPayloadType = 120;
1482
1483 // Read a 4 channel file at 48kHz.
1484 ASSERT_TRUE(SetUpSender(kTestFileQuad48kHz, 48000));
1485
1486 const auto sdp_format = SdpAudioFormat("multiopus", 48000, kNumChannels,
1487 {{"channel_mapping", "0,1,2,3"},
1488 {"coupled_streams", "2"},
1489 {"num_streams", "2"}});
1490 const auto encoder_config =
1491 AudioEncoderMultiChannelOpus::SdpToConfig(sdp_format);
1492
1493 ASSERT_TRUE(encoder_config.has_value());
1494
1495 ASSERT_NO_FATAL_FAILURE(
1496 SetUpTestExternalEncoder(AudioEncoderMultiChannelOpus::MakeAudioEncoder(
1497 *encoder_config, kOpusPayloadType),
1498 kOpusPayloadType));
1499
1500 const auto decoder_config =
1501 AudioDecoderMultiChannelOpus::SdpToConfig(sdp_format);
1502 const auto opus_decoder =
1503 AudioDecoderMultiChannelOpus::MakeAudioDecoder(*decoder_config);
1504
1505 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
1506 new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(
1507 opus_decoder.get());
1508
1509 // Set up an EXTERNAL DECODER to parse 4 channels.
1510 Run(AcmReceiverBitExactnessOldApi::PlatformChecksum( // audio checksum
1511 "audio checksum check downstream|8051617907766bec5f4e4a4f7c6d5291",
1512 "8051617907766bec5f4e4a4f7c6d5291",
1513 "6183752a62dc1368f959eb3a8c93b846", "android arm64 audio checksum",
1514 "48bf1f3ca0b72f3c9cdfbe79956122b1"),
1515 // payload_checksum,
1516 AcmReceiverBitExactnessOldApi::PlatformChecksum( // payload checksum
1517 "payload checksum check downstream|b09c52e44b2bdd9a0809e3a5b1623a76",
1518 "b09c52e44b2bdd9a0809e3a5b1623a76",
1519 "2ea535ef60f7d0c9d89e3002d4c2124f", "android arm64 payload checksum",
1520 "e87995a80f50a0a735a230ca8b04a67d"),
1521 50, test::AcmReceiveTestOldApi::kQuadOutput, decoder_factory);
1522 }
1523
TEST_F(AcmSenderBitExactnessNewApi,OpusFromFormat_stereo_20ms_voip)1524 TEST_F(AcmSenderBitExactnessNewApi, OpusFromFormat_stereo_20ms_voip) {
1525 auto config = AudioEncoderOpus::SdpToConfig(
1526 SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}}));
1527 // If not set, default will be kAudio in case of stereo.
1528 config->application = AudioEncoderOpusConfig::ApplicationMode::kVoip;
1529 ASSERT_TRUE(SetUpSender(kTestFileFakeStereo32kHz, 32000));
1530 ASSERT_NO_FATAL_FAILURE(SetUpTestExternalEncoder(
1531 AudioEncoderOpus::MakeAudioEncoder(*config, 120), 120));
1532 const std::string audio_maybe_sse =
1533 "2d7e5797444f75e5bfeaffbd8c25176b"
1534 "|408d4bdc05a8c23e46c6ac06c5b917ee";
1535 const std::string payload_maybe_sse =
1536 "b38b5584cfa7b6999b2e8e996c950c88"
1537 "|eb0752ce1b6f2436fefc2e19bd084fb5";
1538 Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(
1539 audio_maybe_sse, audio_maybe_sse, "f1cefe107ffdced7694d7f735342adf3",
1540 "3b1bfe5dd8ed16ee5b04b93a5b5e7e48",
1541 "3b1bfe5dd8ed16ee5b04b93a5b5e7e48"),
1542 AcmReceiverBitExactnessOldApi::PlatformChecksum(
1543 payload_maybe_sse, payload_maybe_sse,
1544 "5e79a2f51c633fe145b6c10ae198d1aa",
1545 "e730050cb304d54d853fd285ab0424fa",
1546 "e730050cb304d54d853fd285ab0424fa"),
1547 50, test::AcmReceiveTestOldApi::kStereoOutput);
1548 }
1549
1550 // This test is for verifying the SetBitRate function. The bitrate is changed at
1551 // the beginning, and the number of generated bytes are checked.
1552 class AcmSetBitRateTest : public ::testing::Test {
1553 protected:
1554 static const int kTestDurationMs = 1000;
1555
1556 // Sets up the test::AcmSendTest object. Returns true on success, otherwise
1557 // false.
SetUpSender()1558 bool SetUpSender() {
1559 const std::string input_file_name =
1560 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
1561 // Note that |audio_source_| will loop forever. The test duration is set
1562 // explicitly by |kTestDurationMs|.
1563 audio_source_.reset(new test::InputAudioFile(input_file_name));
1564 static const int kSourceRateHz = 32000;
1565 send_test_.reset(new test::AcmSendTestOldApi(
1566 audio_source_.get(), kSourceRateHz, kTestDurationMs));
1567 return send_test_.get();
1568 }
1569
1570 // Registers a send codec in the test::AcmSendTest object. Returns true on
1571 // success, false on failure.
RegisterSendCodec(const char * payload_name,int sampling_freq_hz,int channels,int payload_type,int frame_size_samples,int frame_size_rtp_timestamps)1572 virtual bool RegisterSendCodec(const char* payload_name,
1573 int sampling_freq_hz,
1574 int channels,
1575 int payload_type,
1576 int frame_size_samples,
1577 int frame_size_rtp_timestamps) {
1578 return send_test_->RegisterCodec(payload_name, sampling_freq_hz, channels,
1579 payload_type, frame_size_samples);
1580 }
1581
RegisterExternalSendCodec(std::unique_ptr<AudioEncoder> external_speech_encoder,int payload_type)1582 void RegisterExternalSendCodec(
1583 std::unique_ptr<AudioEncoder> external_speech_encoder,
1584 int payload_type) {
1585 send_test_->RegisterExternalCodec(std::move(external_speech_encoder));
1586 }
1587
RunInner(int min_expected_total_bits,int max_expected_total_bits)1588 void RunInner(int min_expected_total_bits, int max_expected_total_bits) {
1589 int nr_bytes = 0;
1590 while (std::unique_ptr<test::Packet> next_packet =
1591 send_test_->NextPacket()) {
1592 nr_bytes += rtc::checked_cast<int>(next_packet->payload_length_bytes());
1593 }
1594 EXPECT_LE(min_expected_total_bits, nr_bytes * 8);
1595 EXPECT_GE(max_expected_total_bits, nr_bytes * 8);
1596 }
1597
SetUpTest(const char * codec_name,int codec_sample_rate_hz,int channels,int payload_type,int codec_frame_size_samples,int codec_frame_size_rtp_timestamps)1598 void SetUpTest(const char* codec_name,
1599 int codec_sample_rate_hz,
1600 int channels,
1601 int payload_type,
1602 int codec_frame_size_samples,
1603 int codec_frame_size_rtp_timestamps) {
1604 ASSERT_TRUE(SetUpSender());
1605 ASSERT_TRUE(RegisterSendCodec(codec_name, codec_sample_rate_hz, channels,
1606 payload_type, codec_frame_size_samples,
1607 codec_frame_size_rtp_timestamps));
1608 }
1609
1610 std::unique_ptr<test::AcmSendTestOldApi> send_test_;
1611 std::unique_ptr<test::InputAudioFile> audio_source_;
1612 };
1613
1614 class AcmSetBitRateNewApi : public AcmSetBitRateTest {
1615 protected:
1616 // Runs the test. SetUpSender() must have been called and a codec must be set
1617 // up before calling this method.
Run(int min_expected_total_bits,int max_expected_total_bits)1618 void Run(int min_expected_total_bits, int max_expected_total_bits) {
1619 RunInner(min_expected_total_bits, max_expected_total_bits);
1620 }
1621 };
1622
TEST_F(AcmSetBitRateNewApi,OpusFromFormat_48khz_20ms_10kbps)1623 TEST_F(AcmSetBitRateNewApi, OpusFromFormat_48khz_20ms_10kbps) {
1624 const auto config = AudioEncoderOpus::SdpToConfig(
1625 SdpAudioFormat("opus", 48000, 2, {{"maxaveragebitrate", "10000"}}));
1626 ASSERT_TRUE(SetUpSender());
1627 RegisterExternalSendCodec(AudioEncoderOpus::MakeAudioEncoder(*config, 107),
1628 107);
1629 RunInner(7000, 12000);
1630 }
1631
TEST_F(AcmSetBitRateNewApi,OpusFromFormat_48khz_20ms_50kbps)1632 TEST_F(AcmSetBitRateNewApi, OpusFromFormat_48khz_20ms_50kbps) {
1633 const auto config = AudioEncoderOpus::SdpToConfig(
1634 SdpAudioFormat("opus", 48000, 2, {{"maxaveragebitrate", "50000"}}));
1635 ASSERT_TRUE(SetUpSender());
1636 RegisterExternalSendCodec(AudioEncoderOpus::MakeAudioEncoder(*config, 107),
1637 107);
1638 RunInner(40000, 60000);
1639 }
1640
1641 // Verify that it works when the data to send is mono and the encoder is set to
1642 // send surround audio.
TEST_F(AudioCodingModuleTestOldApi,SendingMultiChannelForMonoInput)1643 TEST_F(AudioCodingModuleTestOldApi, SendingMultiChannelForMonoInput) {
1644 constexpr int kSampleRateHz = 48000;
1645 constexpr int kSamplesPerChannel = kSampleRateHz * 10 / 1000;
1646
1647 audio_format_ = SdpAudioFormat({"multiopus",
1648 kSampleRateHz,
1649 6,
1650 {{"minptime", "10"},
1651 {"useinbandfec", "1"},
1652 {"channel_mapping", "0,4,1,2,3,5"},
1653 {"num_streams", "4"},
1654 {"coupled_streams", "2"}}});
1655
1656 RegisterCodec();
1657
1658 input_frame_.sample_rate_hz_ = kSampleRateHz;
1659 input_frame_.num_channels_ = 1;
1660 input_frame_.samples_per_channel_ = kSamplesPerChannel;
1661 for (size_t k = 0; k < 10; ++k) {
1662 ASSERT_GE(acm_->Add10MsData(input_frame_), 0);
1663 input_frame_.timestamp_ += kSamplesPerChannel;
1664 }
1665 }
1666
1667 // Verify that it works when the data to send is stereo and the encoder is set
1668 // to send surround audio.
TEST_F(AudioCodingModuleTestOldApi,SendingMultiChannelForStereoInput)1669 TEST_F(AudioCodingModuleTestOldApi, SendingMultiChannelForStereoInput) {
1670 constexpr int kSampleRateHz = 48000;
1671 constexpr int kSamplesPerChannel = (kSampleRateHz * 10) / 1000;
1672
1673 audio_format_ = SdpAudioFormat({"multiopus",
1674 kSampleRateHz,
1675 6,
1676 {{"minptime", "10"},
1677 {"useinbandfec", "1"},
1678 {"channel_mapping", "0,4,1,2,3,5"},
1679 {"num_streams", "4"},
1680 {"coupled_streams", "2"}}});
1681
1682 RegisterCodec();
1683
1684 input_frame_.sample_rate_hz_ = kSampleRateHz;
1685 input_frame_.num_channels_ = 2;
1686 input_frame_.samples_per_channel_ = kSamplesPerChannel;
1687 for (size_t k = 0; k < 10; ++k) {
1688 ASSERT_GE(acm_->Add10MsData(input_frame_), 0);
1689 input_frame_.timestamp_ += kSamplesPerChannel;
1690 }
1691 }
1692
1693 // Verify that it works when the data to send is mono and the encoder is set to
1694 // send stereo audio.
TEST_F(AudioCodingModuleTestOldApi,SendingStereoForMonoInput)1695 TEST_F(AudioCodingModuleTestOldApi, SendingStereoForMonoInput) {
1696 constexpr int kSampleRateHz = 48000;
1697 constexpr int kSamplesPerChannel = (kSampleRateHz * 10) / 1000;
1698
1699 audio_format_ = SdpAudioFormat("L16", kSampleRateHz, 2);
1700
1701 RegisterCodec();
1702
1703 input_frame_.sample_rate_hz_ = kSampleRateHz;
1704 input_frame_.num_channels_ = 1;
1705 input_frame_.samples_per_channel_ = kSamplesPerChannel;
1706 for (size_t k = 0; k < 10; ++k) {
1707 ASSERT_GE(acm_->Add10MsData(input_frame_), 0);
1708 input_frame_.timestamp_ += kSamplesPerChannel;
1709 }
1710 }
1711
1712 // Verify that it works when the data to send is stereo and the encoder is set
1713 // to send mono audio.
TEST_F(AudioCodingModuleTestOldApi,SendingMonoForStereoInput)1714 TEST_F(AudioCodingModuleTestOldApi, SendingMonoForStereoInput) {
1715 constexpr int kSampleRateHz = 48000;
1716 constexpr int kSamplesPerChannel = (kSampleRateHz * 10) / 1000;
1717
1718 audio_format_ = SdpAudioFormat("L16", kSampleRateHz, 1);
1719
1720 RegisterCodec();
1721
1722 input_frame_.sample_rate_hz_ = kSampleRateHz;
1723 input_frame_.num_channels_ = 1;
1724 input_frame_.samples_per_channel_ = kSamplesPerChannel;
1725 for (size_t k = 0; k < 10; ++k) {
1726 ASSERT_GE(acm_->Add10MsData(input_frame_), 0);
1727 input_frame_.timestamp_ += kSamplesPerChannel;
1728 }
1729 }
1730
1731 // The result on the Android platforms is inconsistent for this test case.
1732 // On android_rel the result is different from android and android arm64 rel.
1733 #if defined(WEBRTC_ANDROID)
1734 #define MAYBE_OpusFromFormat_48khz_20ms_100kbps \
1735 DISABLED_OpusFromFormat_48khz_20ms_100kbps
1736 #else
1737 #define MAYBE_OpusFromFormat_48khz_20ms_100kbps \
1738 OpusFromFormat_48khz_20ms_100kbps
1739 #endif
TEST_F(AcmSetBitRateNewApi,MAYBE_OpusFromFormat_48khz_20ms_100kbps)1740 TEST_F(AcmSetBitRateNewApi, MAYBE_OpusFromFormat_48khz_20ms_100kbps) {
1741 const auto config = AudioEncoderOpus::SdpToConfig(
1742 SdpAudioFormat("opus", 48000, 2, {{"maxaveragebitrate", "100000"}}));
1743 ASSERT_TRUE(SetUpSender());
1744 RegisterExternalSendCodec(AudioEncoderOpus::MakeAudioEncoder(*config, 107),
1745 107);
1746 RunInner(80000, 120000);
1747 }
1748
TEST_F(AcmSenderBitExactnessOldApi,External_Pcmu_20ms)1749 TEST_F(AcmSenderBitExactnessOldApi, External_Pcmu_20ms) {
1750 AudioEncoderPcmU::Config config;
1751 config.frame_size_ms = 20;
1752 config.num_channels = 1;
1753 config.payload_type = 0;
1754 AudioEncoderPcmU encoder(config);
1755 auto mock_encoder = std::make_unique<MockAudioEncoder>();
1756 // Set expectations on the mock encoder and also delegate the calls to the
1757 // real encoder.
1758 EXPECT_CALL(*mock_encoder, SampleRateHz())
1759 .Times(AtLeast(1))
1760 .WillRepeatedly(Invoke(&encoder, &AudioEncoderPcmU::SampleRateHz));
1761 EXPECT_CALL(*mock_encoder, NumChannels())
1762 .Times(AtLeast(1))
1763 .WillRepeatedly(Invoke(&encoder, &AudioEncoderPcmU::NumChannels));
1764 EXPECT_CALL(*mock_encoder, RtpTimestampRateHz())
1765 .Times(AtLeast(1))
1766 .WillRepeatedly(Invoke(&encoder, &AudioEncoderPcmU::RtpTimestampRateHz));
1767 EXPECT_CALL(*mock_encoder, Num10MsFramesInNextPacket())
1768 .Times(AtLeast(1))
1769 .WillRepeatedly(
1770 Invoke(&encoder, &AudioEncoderPcmU::Num10MsFramesInNextPacket));
1771 EXPECT_CALL(*mock_encoder, GetTargetBitrate())
1772 .Times(AtLeast(1))
1773 .WillRepeatedly(Invoke(&encoder, &AudioEncoderPcmU::GetTargetBitrate));
1774 EXPECT_CALL(*mock_encoder, EncodeImpl(_, _, _))
1775 .Times(AtLeast(1))
1776 .WillRepeatedly(Invoke(
1777 &encoder, static_cast<AudioEncoder::EncodedInfo (AudioEncoder::*)(
1778 uint32_t, rtc::ArrayView<const int16_t>, rtc::Buffer*)>(
1779 &AudioEncoderPcmU::Encode)));
1780 ASSERT_TRUE(SetUpSender(kTestFileMono32kHz, 32000));
1781 ASSERT_NO_FATAL_FAILURE(
1782 SetUpTestExternalEncoder(std::move(mock_encoder), config.payload_type));
1783 Run("81a9d4c0bb72e9becc43aef124c981e9", "8f9b8750bd80fe26b6cbf6659b89f0f9",
1784 50, test::AcmReceiveTestOldApi::kMonoOutput);
1785 }
1786
1787 // This test fixture is implemented to run ACM and change the desired output
1788 // frequency during the call. The input packets are simply PCM16b-wb encoded
1789 // payloads with a constant value of |kSampleValue|. The test fixture itself
1790 // acts as PacketSource in between the receive test class and the constant-
1791 // payload packet source class. The output is both written to file, and analyzed
1792 // in this test fixture.
1793 class AcmSwitchingOutputFrequencyOldApi : public ::testing::Test,
1794 public test::PacketSource,
1795 public test::AudioSink {
1796 protected:
1797 static const size_t kTestNumPackets = 50;
1798 static const int kEncodedSampleRateHz = 16000;
1799 static const size_t kPayloadLenSamples = 30 * kEncodedSampleRateHz / 1000;
1800 static const int kPayloadType = 108; // Default payload type for PCM16b-wb.
1801
AcmSwitchingOutputFrequencyOldApi()1802 AcmSwitchingOutputFrequencyOldApi()
1803 : first_output_(true),
1804 num_packets_(0),
1805 packet_source_(kPayloadLenSamples,
1806 kSampleValue,
1807 kEncodedSampleRateHz,
1808 kPayloadType),
1809 output_freq_2_(0),
1810 has_toggled_(false) {}
1811
Run(int output_freq_1,int output_freq_2,int toggle_period_ms)1812 void Run(int output_freq_1, int output_freq_2, int toggle_period_ms) {
1813 // Set up the receiver used to decode the packets and verify the decoded
1814 // output.
1815 const std::string output_file_name =
1816 webrtc::test::OutputPath() +
1817 ::testing::UnitTest::GetInstance()
1818 ->current_test_info()
1819 ->test_case_name() +
1820 "_" + ::testing::UnitTest::GetInstance()->current_test_info()->name() +
1821 "_output.pcm";
1822 test::OutputAudioFile output_file(output_file_name);
1823 // Have the output audio sent both to file and to the WriteArray method in
1824 // this class.
1825 test::AudioSinkFork output(this, &output_file);
1826 test::AcmReceiveTestToggleOutputFreqOldApi receive_test(
1827 this, &output, output_freq_1, output_freq_2, toggle_period_ms,
1828 test::AcmReceiveTestOldApi::kMonoOutput);
1829 ASSERT_NO_FATAL_FAILURE(receive_test.RegisterDefaultCodecs());
1830 output_freq_2_ = output_freq_2;
1831
1832 // This is where the actual test is executed.
1833 receive_test.Run();
1834
1835 // Delete output file.
1836 remove(output_file_name.c_str());
1837 }
1838
1839 // Inherited from test::PacketSource.
NextPacket()1840 std::unique_ptr<test::Packet> NextPacket() override {
1841 // Check if it is time to terminate the test. The packet source is of type
1842 // ConstantPcmPacketSource, which is infinite, so we must end the test
1843 // "manually".
1844 if (num_packets_++ > kTestNumPackets) {
1845 EXPECT_TRUE(has_toggled_);
1846 return NULL; // Test ended.
1847 }
1848
1849 // Get the next packet from the source.
1850 return packet_source_.NextPacket();
1851 }
1852
1853 // Inherited from test::AudioSink.
WriteArray(const int16_t * audio,size_t num_samples)1854 bool WriteArray(const int16_t* audio, size_t num_samples) override {
1855 // Skip checking the first output frame, since it has a number of zeros
1856 // due to how NetEq is initialized.
1857 if (first_output_) {
1858 first_output_ = false;
1859 return true;
1860 }
1861 for (size_t i = 0; i < num_samples; ++i) {
1862 EXPECT_EQ(kSampleValue, audio[i]);
1863 }
1864 if (num_samples ==
1865 static_cast<size_t>(output_freq_2_ / 100)) // Size of 10 ms frame.
1866 has_toggled_ = true;
1867 // The return value does not say if the values match the expectation, just
1868 // that the method could process the samples.
1869 return true;
1870 }
1871
1872 const int16_t kSampleValue = 1000;
1873 bool first_output_;
1874 size_t num_packets_;
1875 test::ConstantPcmPacketSource packet_source_;
1876 int output_freq_2_;
1877 bool has_toggled_;
1878 };
1879
TEST_F(AcmSwitchingOutputFrequencyOldApi,TestWithoutToggling)1880 TEST_F(AcmSwitchingOutputFrequencyOldApi, TestWithoutToggling) {
1881 Run(16000, 16000, 1000);
1882 }
1883
TEST_F(AcmSwitchingOutputFrequencyOldApi,Toggle16KhzTo32Khz)1884 TEST_F(AcmSwitchingOutputFrequencyOldApi, Toggle16KhzTo32Khz) {
1885 Run(16000, 32000, 1000);
1886 }
1887
TEST_F(AcmSwitchingOutputFrequencyOldApi,Toggle32KhzTo16Khz)1888 TEST_F(AcmSwitchingOutputFrequencyOldApi, Toggle32KhzTo16Khz) {
1889 Run(32000, 16000, 1000);
1890 }
1891
TEST_F(AcmSwitchingOutputFrequencyOldApi,Toggle16KhzTo8Khz)1892 TEST_F(AcmSwitchingOutputFrequencyOldApi, Toggle16KhzTo8Khz) {
1893 Run(16000, 8000, 1000);
1894 }
1895
TEST_F(AcmSwitchingOutputFrequencyOldApi,Toggle8KhzTo16Khz)1896 TEST_F(AcmSwitchingOutputFrequencyOldApi, Toggle8KhzTo16Khz) {
1897 Run(8000, 16000, 1000);
1898 }
1899
1900 #endif
1901
1902 } // namespace webrtc
1903