xref: /aosp_15_r20/external/webrtc/modules/audio_coding/acm2/audio_coding_module_unittest.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "absl/strings/string_view.h"
21 #include "api/audio_codecs/audio_encoder.h"
22 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
23 #include "api/audio_codecs/builtin_audio_encoder_factory.h"
24 #include "api/audio_codecs/opus/audio_decoder_multi_channel_opus.h"
25 #include "api/audio_codecs/opus/audio_decoder_opus.h"
26 #include "api/audio_codecs/opus/audio_encoder_multi_channel_opus.h"
27 #include "api/audio_codecs/opus/audio_encoder_opus.h"
28 #include "modules/audio_coding/acm2/acm_receive_test.h"
29 #include "modules/audio_coding/acm2/acm_send_test.h"
30 #include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
31 #include "modules/audio_coding/codecs/g711/audio_decoder_pcm.h"
32 #include "modules/audio_coding/codecs/g711/audio_encoder_pcm.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/synchronization/mutex.h"
47 #include "rtc_base/system/arch.h"
48 #include "rtc_base/thread_annotations.h"
49 #include "system_wrappers/include/clock.h"
50 #include "system_wrappers/include/cpu_features_wrapper.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 // Introduce this class to set different expectations on the number of encoded
305 // bytes. This class expects all encoded packets to be 9 bytes (matching one
306 // CNG SID frame) or 0 bytes. This test depends on `input_frame_` containing
307 // (near-)zero values. It also introduces a way to register comfort noise with
308 // a custom payload type.
309 class AudioCodingModuleTestWithComfortNoiseOldApi
310     : public AudioCodingModuleTestOldApi {
311  protected:
RegisterCngCodec(int rtp_payload_type)312   void RegisterCngCodec(int rtp_payload_type) {
313     acm_->SetReceiveCodecs({{kPayloadType, *audio_format_},
314                             {rtp_payload_type, {"cn", kSampleRateHz, 1}}});
315     acm_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
316       AudioEncoderCngConfig config;
317       config.speech_encoder = std::move(*enc);
318       config.num_channels = 1;
319       config.payload_type = rtp_payload_type;
320       config.vad_mode = Vad::kVadNormal;
321       *enc = CreateComfortNoiseEncoder(std::move(config));
322     });
323   }
324 
VerifyEncoding()325   void VerifyEncoding() override {
326     int last_length = packet_cb_.last_payload_len_bytes();
327     EXPECT_TRUE(last_length == 9 || last_length == 0)
328         << "Last encoded packet was " << last_length << " bytes.";
329   }
330 
DoTest(int blocks_per_packet,int cng_pt)331   void DoTest(int blocks_per_packet, int cng_pt) {
332     const int kLoops = 40;
333     // This array defines the expected frame types, and when they should arrive.
334     // We expect a frame to arrive each time the speech encoder would have
335     // produced a packet, and once every 100 ms the frame should be non-empty,
336     // that is contain comfort noise.
337     const struct {
338       int ix;
339       AudioFrameType type;
340     } expectation[] = {{2, AudioFrameType::kAudioFrameCN},
341                        {5, AudioFrameType::kEmptyFrame},
342                        {8, AudioFrameType::kEmptyFrame},
343                        {11, AudioFrameType::kAudioFrameCN},
344                        {14, AudioFrameType::kEmptyFrame},
345                        {17, AudioFrameType::kEmptyFrame},
346                        {20, AudioFrameType::kAudioFrameCN},
347                        {23, AudioFrameType::kEmptyFrame},
348                        {26, AudioFrameType::kEmptyFrame},
349                        {29, AudioFrameType::kEmptyFrame},
350                        {32, AudioFrameType::kAudioFrameCN},
351                        {35, AudioFrameType::kEmptyFrame},
352                        {38, AudioFrameType::kEmptyFrame}};
353     for (int i = 0; i < kLoops; ++i) {
354       int num_calls_before = packet_cb_.num_calls();
355       EXPECT_EQ(i / blocks_per_packet, num_calls_before);
356       InsertAudioAndVerifyEncoding();
357       int num_calls = packet_cb_.num_calls();
358       if (num_calls == num_calls_before + 1) {
359         EXPECT_EQ(expectation[num_calls - 1].ix, i);
360         EXPECT_EQ(expectation[num_calls - 1].type, packet_cb_.last_frame_type())
361             << "Wrong frame type for lap " << i;
362         EXPECT_EQ(cng_pt, packet_cb_.last_payload_type());
363       } else {
364         EXPECT_EQ(num_calls, num_calls_before);
365       }
366     }
367   }
368 };
369 
370 // Checks that the transport callback is invoked once per frame period of the
371 // underlying speech encoder, even when comfort noise is produced.
372 // Also checks that the frame type is kAudioFrameCN or kEmptyFrame.
TEST_F(AudioCodingModuleTestWithComfortNoiseOldApi,TransportCallbackTestForComfortNoiseRegisterCngLast)373 TEST_F(AudioCodingModuleTestWithComfortNoiseOldApi,
374        TransportCallbackTestForComfortNoiseRegisterCngLast) {
375   const int k10MsBlocksPerPacket = 3;
376   pac_size_ = k10MsBlocksPerPacket * kSampleRateHz / 100;
377   audio_format_->parameters["ptime"] = "30";
378   RegisterCodec();
379   const int kCngPayloadType = 105;
380   RegisterCngCodec(kCngPayloadType);
381   DoTest(k10MsBlocksPerPacket, kCngPayloadType);
382 }
383 
384 // A multi-threaded test for ACM that uses the PCM16b 16 kHz codec.
385 class AudioCodingModuleMtTestOldApi : public AudioCodingModuleTestOldApi {
386  protected:
387   static const int kNumPackets = 500;
388   static const int kNumPullCalls = 500;
389 
AudioCodingModuleMtTestOldApi()390   AudioCodingModuleMtTestOldApi()
391       : AudioCodingModuleTestOldApi(),
392         send_count_(0),
393         insert_packet_count_(0),
394         pull_audio_count_(0),
395         next_insert_packet_time_ms_(0),
396         fake_clock_(new SimulatedClock(0)) {
397     clock_ = fake_clock_.get();
398   }
399 
SetUp()400   void SetUp() {
401     AudioCodingModuleTestOldApi::SetUp();
402     RegisterCodec();  // Must be called before the threads start below.
403     StartThreads();
404   }
405 
StartThreads()406   void StartThreads() {
407     quit_.store(false);
408 
409     const auto attributes =
410         rtc::ThreadAttributes().SetPriority(rtc::ThreadPriority::kRealtime);
411     send_thread_ = rtc::PlatformThread::SpawnJoinable(
412         [this] {
413           while (!quit_.load()) {
414             CbSendImpl();
415           }
416         },
417         "send", attributes);
418     insert_packet_thread_ = rtc::PlatformThread::SpawnJoinable(
419         [this] {
420           while (!quit_.load()) {
421             CbInsertPacketImpl();
422           }
423         },
424         "insert_packet", attributes);
425     pull_audio_thread_ = rtc::PlatformThread::SpawnJoinable(
426         [this] {
427           while (!quit_.load()) {
428             CbPullAudioImpl();
429           }
430         },
431         "pull_audio", attributes);
432   }
433 
TearDown()434   void TearDown() {
435     AudioCodingModuleTestOldApi::TearDown();
436     quit_.store(true);
437     pull_audio_thread_.Finalize();
438     send_thread_.Finalize();
439     insert_packet_thread_.Finalize();
440   }
441 
RunTest()442   bool RunTest() { return test_complete_.Wait(TimeDelta::Minutes(10)); }
443 
TestDone()444   virtual bool TestDone() {
445     if (packet_cb_.num_calls() > kNumPackets) {
446       MutexLock lock(&mutex_);
447       if (pull_audio_count_ > kNumPullCalls) {
448         // Both conditions for completion are met. End the test.
449         return true;
450       }
451     }
452     return false;
453   }
454 
455   // The send thread doesn't have to care about the current simulated time,
456   // since only the AcmReceiver is using the clock.
CbSendImpl()457   void CbSendImpl() {
458     SleepMs(1);
459     if (HasFatalFailure()) {
460       // End the test early if a fatal failure (ASSERT_*) has occurred.
461       test_complete_.Set();
462     }
463     ++send_count_;
464     InsertAudioAndVerifyEncoding();
465     if (TestDone()) {
466       test_complete_.Set();
467     }
468   }
469 
CbInsertPacketImpl()470   void CbInsertPacketImpl() {
471     SleepMs(1);
472     {
473       MutexLock lock(&mutex_);
474       if (clock_->TimeInMilliseconds() < next_insert_packet_time_ms_) {
475         return;
476       }
477       next_insert_packet_time_ms_ += 10;
478     }
479     // Now we're not holding the crit sect when calling ACM.
480     ++insert_packet_count_;
481     InsertPacket();
482   }
483 
CbPullAudioImpl()484   void CbPullAudioImpl() {
485     SleepMs(1);
486     {
487       MutexLock lock(&mutex_);
488       // Don't let the insert thread fall behind.
489       if (next_insert_packet_time_ms_ < clock_->TimeInMilliseconds()) {
490         return;
491       }
492       ++pull_audio_count_;
493     }
494     // Now we're not holding the crit sect when calling ACM.
495     PullAudio();
496     fake_clock_->AdvanceTimeMilliseconds(10);
497   }
498 
499   rtc::PlatformThread send_thread_;
500   rtc::PlatformThread insert_packet_thread_;
501   rtc::PlatformThread pull_audio_thread_;
502   // Used to force worker threads to stop looping.
503   std::atomic<bool> quit_;
504 
505   rtc::Event test_complete_;
506   int send_count_;
507   int insert_packet_count_;
508   int pull_audio_count_ RTC_GUARDED_BY(mutex_);
509   Mutex mutex_;
510   int64_t next_insert_packet_time_ms_ RTC_GUARDED_BY(mutex_);
511   std::unique_ptr<SimulatedClock> fake_clock_;
512 };
513 
514 #if defined(WEBRTC_IOS)
515 #define MAYBE_DoTest DISABLED_DoTest
516 #else
517 #define MAYBE_DoTest DoTest
518 #endif
TEST_F(AudioCodingModuleMtTestOldApi,MAYBE_DoTest)519 TEST_F(AudioCodingModuleMtTestOldApi, MAYBE_DoTest) {
520   EXPECT_TRUE(RunTest());
521 }
522 
523 // Disabling all of these tests on iOS until file support has been added.
524 // See https://code.google.com/p/webrtc/issues/detail?id=4752 for details.
525 #if !defined(WEBRTC_IOS)
526 
527 // This test verifies bit exactness for the send-side of ACM. The test setup is
528 // a chain of three different test classes:
529 //
530 // test::AcmSendTest -> AcmSenderBitExactness -> test::AcmReceiveTest
531 //
532 // The receiver side is driving the test by requesting new packets from
533 // AcmSenderBitExactness::NextPacket(). This method, in turn, asks for the
534 // packet from test::AcmSendTest::NextPacket, which inserts audio from the
535 // input file until one packet is produced. (The input file loops indefinitely.)
536 // Before passing the packet to the receiver, this test class verifies the
537 // packet header and updates a payload checksum with the new payload. The
538 // decoded output from the receiver is also verified with a (separate) checksum.
539 class AcmSenderBitExactnessOldApi : public ::testing::Test,
540                                     public test::PacketSource {
541  protected:
542   static const int kTestDurationMs = 1000;
543 
AcmSenderBitExactnessOldApi()544   AcmSenderBitExactnessOldApi()
545       : frame_size_rtp_timestamps_(0),
546         packet_count_(0),
547         payload_type_(0),
548         last_sequence_number_(0),
549         last_timestamp_(0),
550         payload_checksum_(rtc::MessageDigestFactory::Create(rtc::DIGEST_MD5)) {}
551 
552   // Sets up the test::AcmSendTest object. Returns true on success, otherwise
553   // false.
SetUpSender(absl::string_view input_file_name,int source_rate)554   bool SetUpSender(absl::string_view input_file_name, int source_rate) {
555     // Note that `audio_source_` will loop forever. The test duration is set
556     // explicitly by `kTestDurationMs`.
557     audio_source_.reset(new test::InputAudioFile(input_file_name));
558     send_test_.reset(new test::AcmSendTestOldApi(audio_source_.get(),
559                                                  source_rate, kTestDurationMs));
560     return send_test_.get() != NULL;
561   }
562 
563   // Registers a send codec in the test::AcmSendTest object. Returns true on
564   // success, false on failure.
RegisterSendCodec(absl::string_view payload_name,int sampling_freq_hz,int channels,int payload_type,int frame_size_samples,int frame_size_rtp_timestamps)565   bool RegisterSendCodec(absl::string_view payload_name,
566                          int sampling_freq_hz,
567                          int channels,
568                          int payload_type,
569                          int frame_size_samples,
570                          int frame_size_rtp_timestamps) {
571     payload_type_ = payload_type;
572     frame_size_rtp_timestamps_ = frame_size_rtp_timestamps;
573     return send_test_->RegisterCodec(payload_name, sampling_freq_hz, channels,
574                                      payload_type, frame_size_samples);
575   }
576 
RegisterExternalSendCodec(std::unique_ptr<AudioEncoder> external_speech_encoder,int payload_type)577   void RegisterExternalSendCodec(
578       std::unique_ptr<AudioEncoder> external_speech_encoder,
579       int payload_type) {
580     payload_type_ = payload_type;
581     frame_size_rtp_timestamps_ = rtc::checked_cast<uint32_t>(
582         external_speech_encoder->Num10MsFramesInNextPacket() *
583         external_speech_encoder->RtpTimestampRateHz() / 100);
584     send_test_->RegisterExternalCodec(std::move(external_speech_encoder));
585   }
586 
587   // Runs the test. SetUpSender() and RegisterSendCodec() must have been called
588   // before calling this method.
Run(absl::string_view audio_checksum_ref,absl::string_view payload_checksum_ref,int expected_packets,test::AcmReceiveTestOldApi::NumOutputChannels expected_channels,rtc::scoped_refptr<AudioDecoderFactory> decoder_factory=nullptr)589   void Run(absl::string_view audio_checksum_ref,
590            absl::string_view payload_checksum_ref,
591            int expected_packets,
592            test::AcmReceiveTestOldApi::NumOutputChannels expected_channels,
593            rtc::scoped_refptr<AudioDecoderFactory> decoder_factory = nullptr) {
594     if (!decoder_factory) {
595       decoder_factory = CreateBuiltinAudioDecoderFactory();
596     }
597     // Set up the receiver used to decode the packets and verify the decoded
598     // output.
599     test::AudioChecksum audio_checksum;
600     const std::string output_file_name =
601         webrtc::test::OutputPath() +
602         ::testing::UnitTest::GetInstance()
603             ->current_test_info()
604             ->test_case_name() +
605         "_" + ::testing::UnitTest::GetInstance()->current_test_info()->name() +
606         "_output.wav";
607     const int kOutputFreqHz = 8000;
608     test::OutputWavFile output_file(output_file_name, kOutputFreqHz,
609                                     expected_channels);
610     // Have the output audio sent both to file and to the checksum calculator.
611     test::AudioSinkFork output(&audio_checksum, &output_file);
612     test::AcmReceiveTestOldApi receive_test(this, &output, kOutputFreqHz,
613                                             expected_channels, decoder_factory);
614     ASSERT_NO_FATAL_FAILURE(receive_test.RegisterDefaultCodecs());
615 
616     // This is where the actual test is executed.
617     receive_test.Run();
618 
619     // Extract and verify the audio checksum.
620     std::string checksum_string = audio_checksum.Finish();
621     ExpectChecksumEq(audio_checksum_ref, checksum_string);
622 
623     // Extract and verify the payload checksum.
624     rtc::Buffer checksum_result(payload_checksum_->Size());
625     payload_checksum_->Finish(checksum_result.data(), checksum_result.size());
626     checksum_string = rtc::hex_encode(checksum_result);
627     ExpectChecksumEq(payload_checksum_ref, checksum_string);
628 
629     // Verify number of packets produced.
630     EXPECT_EQ(expected_packets, packet_count_);
631 
632     // Delete the output file.
633     remove(output_file_name.c_str());
634   }
635 
636   // Helper: result must be one the "|"-separated checksums.
ExpectChecksumEq(absl::string_view ref,absl::string_view result)637   void ExpectChecksumEq(absl::string_view ref, absl::string_view result) {
638     if (ref.size() == result.size()) {
639       // Only one checksum: clearer message.
640       EXPECT_EQ(ref, result);
641     } else {
642       EXPECT_NE(ref.find(result), absl::string_view::npos)
643           << result << " must be one of these:\n"
644           << ref;
645     }
646   }
647 
648   // Inherited from test::PacketSource.
NextPacket()649   std::unique_ptr<test::Packet> NextPacket() override {
650     auto packet = send_test_->NextPacket();
651     if (!packet)
652       return NULL;
653 
654     VerifyPacket(packet.get());
655     // TODO(henrik.lundin) Save the packet to file as well.
656 
657     // Pass it on to the caller. The caller becomes the owner of `packet`.
658     return packet;
659   }
660 
661   // Verifies the packet.
VerifyPacket(const test::Packet * packet)662   void VerifyPacket(const test::Packet* packet) {
663     EXPECT_TRUE(packet->valid_header());
664     // (We can check the header fields even if valid_header() is false.)
665     EXPECT_EQ(payload_type_, packet->header().payloadType);
666     if (packet_count_ > 0) {
667       // This is not the first packet.
668       uint16_t sequence_number_diff =
669           packet->header().sequenceNumber - last_sequence_number_;
670       EXPECT_EQ(1, sequence_number_diff);
671       uint32_t timestamp_diff = packet->header().timestamp - last_timestamp_;
672       EXPECT_EQ(frame_size_rtp_timestamps_, timestamp_diff);
673     }
674     ++packet_count_;
675     last_sequence_number_ = packet->header().sequenceNumber;
676     last_timestamp_ = packet->header().timestamp;
677     // Update the checksum.
678     payload_checksum_->Update(packet->payload(),
679                               packet->payload_length_bytes());
680   }
681 
SetUpTest(absl::string_view codec_name,int codec_sample_rate_hz,int channels,int payload_type,int codec_frame_size_samples,int codec_frame_size_rtp_timestamps)682   void SetUpTest(absl::string_view codec_name,
683                  int codec_sample_rate_hz,
684                  int channels,
685                  int payload_type,
686                  int codec_frame_size_samples,
687                  int codec_frame_size_rtp_timestamps) {
688     ASSERT_TRUE(SetUpSender(
689         channels == 1 ? kTestFileMono32kHz : kTestFileFakeStereo32kHz, 32000));
690     ASSERT_TRUE(RegisterSendCodec(codec_name, codec_sample_rate_hz, channels,
691                                   payload_type, codec_frame_size_samples,
692                                   codec_frame_size_rtp_timestamps));
693   }
694 
SetUpTestExternalEncoder(std::unique_ptr<AudioEncoder> external_speech_encoder,int payload_type)695   void SetUpTestExternalEncoder(
696       std::unique_ptr<AudioEncoder> external_speech_encoder,
697       int payload_type) {
698     ASSERT_TRUE(send_test_);
699     RegisterExternalSendCodec(std::move(external_speech_encoder), payload_type);
700   }
701 
702   std::unique_ptr<test::AcmSendTestOldApi> send_test_;
703   std::unique_ptr<test::InputAudioFile> audio_source_;
704   uint32_t frame_size_rtp_timestamps_;
705   int packet_count_;
706   uint8_t payload_type_;
707   uint16_t last_sequence_number_;
708   uint32_t last_timestamp_;
709   std::unique_ptr<rtc::MessageDigest> payload_checksum_;
710   const std::string kTestFileMono32kHz =
711       webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
712   const std::string kTestFileFakeStereo32kHz =
713       webrtc::test::ResourcePath("audio_coding/testfile_fake_stereo_32kHz",
714                                  "pcm");
715   const std::string kTestFileQuad48kHz = webrtc::test::ResourcePath(
716       "audio_coding/speech_4_channels_48k_one_second",
717       "wav");
718 };
719 
720 class AcmSenderBitExactnessNewApi : public AcmSenderBitExactnessOldApi {};
721 
TEST_F(AcmSenderBitExactnessOldApi,Pcm16_8000khz_10ms)722 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_8000khz_10ms) {
723   ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 8000, 1, 107, 80, 80));
724   Run(/*audio_checksum_ref=*/"69118ed438ac76252d023e0463819471",
725       /*payload_checksum_ref=*/"c1edd36339ce0326cc4550041ad719a0",
726       /*expected_packets=*/100,
727       /*expected_channels=*/test::AcmReceiveTestOldApi::kMonoOutput);
728 }
729 
TEST_F(AcmSenderBitExactnessOldApi,Pcm16_16000khz_10ms)730 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_16000khz_10ms) {
731   ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 16000, 1, 108, 160, 160));
732   Run(/*audio_checksum_ref=*/"f95c87bdd33f631bcf80f4b19445bbd2",
733       /*payload_checksum_ref=*/"ad786526383178b08d80d6eee06e9bad",
734       /*expected_packets=*/100,
735       /*expected_channels=*/test::AcmReceiveTestOldApi::kMonoOutput);
736 }
737 
TEST_F(AcmSenderBitExactnessOldApi,Pcm16_32000khz_10ms)738 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_32000khz_10ms) {
739   ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 32000, 1, 109, 320, 320));
740   Run(/*audio_checksum_ref=*/"c50244419c5c3a2f04cc69a022c266a2",
741       /*payload_checksum_ref=*/"5ef82ea885e922263606c6fdbc49f651",
742       /*expected_packets=*/100,
743       /*expected_channels=*/test::AcmReceiveTestOldApi::kMonoOutput);
744 }
745 
TEST_F(AcmSenderBitExactnessOldApi,Pcm16_stereo_8000khz_10ms)746 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_stereo_8000khz_10ms) {
747   ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 8000, 2, 111, 80, 80));
748   Run(/*audio_checksum_ref=*/"4fccf4cc96f1e8e8de4b9fadf62ded9e",
749       /*payload_checksum_ref=*/"62ce5adb0d4965d0a52ec98ae7f98974",
750       /*expected_packets=*/100,
751       /*expected_channels=*/test::AcmReceiveTestOldApi::kStereoOutput);
752 }
753 
TEST_F(AcmSenderBitExactnessOldApi,Pcm16_stereo_16000khz_10ms)754 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_stereo_16000khz_10ms) {
755   ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 16000, 2, 112, 160, 160));
756   Run(/*audio_checksum_ref=*/"e15e388d9d4af8c02a59fe1552fedee3",
757       /*payload_checksum_ref=*/"41ca8edac4b8c71cd54fd9f25ec14870",
758       /*expected_packets=*/100,
759       /*expected_channels=*/test::AcmReceiveTestOldApi::kStereoOutput);
760 }
761 
TEST_F(AcmSenderBitExactnessOldApi,Pcm16_stereo_32000khz_10ms)762 TEST_F(AcmSenderBitExactnessOldApi, Pcm16_stereo_32000khz_10ms) {
763   ASSERT_NO_FATAL_FAILURE(SetUpTest("L16", 32000, 2, 113, 320, 320));
764   Run(/*audio_checksum_ref=*/"b240520c0d05003fde7a174ae5957286",
765       /*payload_checksum_ref=*/"50e58502fb04421bf5b857dda4c96879",
766       /*expected_packets=*/100,
767       /*expected_channels=*/test::AcmReceiveTestOldApi::kStereoOutput);
768 }
769 
TEST_F(AcmSenderBitExactnessOldApi,Pcmu_20ms)770 TEST_F(AcmSenderBitExactnessOldApi, Pcmu_20ms) {
771   ASSERT_NO_FATAL_FAILURE(SetUpTest("PCMU", 8000, 1, 0, 160, 160));
772   Run(/*audio_checksum_ref=*/"c8d1fc677f33c2022ec5f83c7f302280",
773       /*payload_checksum_ref=*/"8f9b8750bd80fe26b6cbf6659b89f0f9",
774       /*expected_packets=*/50,
775       /*expected_channels=*/test::AcmReceiveTestOldApi::kMonoOutput);
776 }
777 
TEST_F(AcmSenderBitExactnessOldApi,Pcma_20ms)778 TEST_F(AcmSenderBitExactnessOldApi, Pcma_20ms) {
779   ASSERT_NO_FATAL_FAILURE(SetUpTest("PCMA", 8000, 1, 8, 160, 160));
780   Run(/*audio_checksum_ref=*/"47eb60e855eb12d1b0e6da9c975754a4",
781       /*payload_checksum_ref=*/"6ad745e55aa48981bfc790d0eeef2dd1",
782       /*expected_packets=*/50,
783       /*expected_channels=*/test::AcmReceiveTestOldApi::kMonoOutput);
784 }
785 
TEST_F(AcmSenderBitExactnessOldApi,Pcmu_stereo_20ms)786 TEST_F(AcmSenderBitExactnessOldApi, Pcmu_stereo_20ms) {
787   ASSERT_NO_FATAL_FAILURE(SetUpTest("PCMU", 8000, 2, 110, 160, 160));
788   Run(/*audio_checksum_ref=*/"6ef2f57d4934714787fd0a834e3ea18e",
789       /*payload_checksum_ref=*/"60b6f25e8d1e74cb679cfe756dd9bca5",
790       /*expected_packets=*/50,
791       /*expected_channels=*/test::AcmReceiveTestOldApi::kStereoOutput);
792 }
793 
TEST_F(AcmSenderBitExactnessOldApi,Pcma_stereo_20ms)794 TEST_F(AcmSenderBitExactnessOldApi, Pcma_stereo_20ms) {
795   ASSERT_NO_FATAL_FAILURE(SetUpTest("PCMA", 8000, 2, 118, 160, 160));
796   Run(/*audio_checksum_ref=*/"a84d75e098d87ab6b260687eb4b612a2",
797       /*payload_checksum_ref=*/"92b282c83efd20e7eeef52ba40842cf7",
798       /*expected_packets=*/50,
799       /*expected_channels=*/test::AcmReceiveTestOldApi::kStereoOutput);
800 }
801 
802 #if defined(WEBRTC_CODEC_ILBC) && defined(WEBRTC_LINUX) && \
803     defined(WEBRTC_ARCH_X86_64)
TEST_F(AcmSenderBitExactnessOldApi,Ilbc_30ms)804 TEST_F(AcmSenderBitExactnessOldApi, Ilbc_30ms) {
805   ASSERT_NO_FATAL_FAILURE(SetUpTest("ILBC", 8000, 1, 102, 240, 240));
806   Run(/*audio_checksum_ref=*/"b14dba0de36efa5ec88a32c0b320b70f",
807       /*payload_checksum_ref=*/"cfae2e9f6aba96e145f2bcdd5050ce78",
808       /*expected_packets=*/33,
809       /*expected_channels=*/test::AcmReceiveTestOldApi::kMonoOutput);
810 }
811 #endif
812 
813 #if defined(WEBRTC_LINUX) && defined(WEBRTC_ARCH_X86_64)
TEST_F(AcmSenderBitExactnessOldApi,G722_20ms)814 TEST_F(AcmSenderBitExactnessOldApi, G722_20ms) {
815   ASSERT_NO_FATAL_FAILURE(SetUpTest("G722", 16000, 1, 9, 320, 160));
816   Run(/*audio_checksum_ref=*/"f5264affff25cf2cbd2e1e8a5217f9a3",
817       /*payload_checksum_ref=*/"fc68a87e1380614e658087cb35d5ca10",
818       /*expected_packets=*/50,
819       /*expected_channels=*/test::AcmReceiveTestOldApi::kMonoOutput);
820 }
821 #endif
822 
823 #if defined(WEBRTC_LINUX) && defined(WEBRTC_ARCH_X86_64)
TEST_F(AcmSenderBitExactnessOldApi,G722_stereo_20ms)824 TEST_F(AcmSenderBitExactnessOldApi, G722_stereo_20ms) {
825   ASSERT_NO_FATAL_FAILURE(SetUpTest("G722", 16000, 2, 119, 320, 160));
826   Run(/*audio_checksum_ref=*/"be0b8528ff9db3a2219f55ddd36faf7f",
827       /*payload_checksum_ref=*/"66516152eeaa1e650ad94ff85f668dac",
828       /*expected_packets=*/50,
829       /*expected_channels=*/test::AcmReceiveTestOldApi::kStereoOutput);
830 }
831 #endif
832 
833 namespace {
834 // Checksum depends on libopus being compiled with or without SSE.
835 const std::string audio_checksum =
836     "6a76fe2ffba057c06eb63239b3c47abe"
837     "|0c4f9d33b4a7379a34ee0c0d5718afe6";
838 const std::string payload_checksum =
839     "b43bdf7638b2bc2a5a6f30bdc640b9ed"
840     "|c30d463e7ed10bdd1da9045f80561f27";
841 }  // namespace
842 
843 #if defined(WEBRTC_LINUX) && defined(WEBRTC_ARCH_X86_64)
TEST_F(AcmSenderBitExactnessOldApi,Opus_stereo_20ms)844 TEST_F(AcmSenderBitExactnessOldApi, Opus_stereo_20ms) {
845   ASSERT_NO_FATAL_FAILURE(SetUpTest("opus", 48000, 2, 120, 960, 960));
846   Run(audio_checksum, payload_checksum, /*expected_packets=*/50,
847       /*expected_channels=*/test::AcmReceiveTestOldApi::kStereoOutput);
848 }
849 #endif
850 
851 #if defined(WEBRTC_LINUX) && defined(WEBRTC_ARCH_X86_64)
TEST_F(AcmSenderBitExactnessNewApi,OpusFromFormat_stereo_20ms)852 TEST_F(AcmSenderBitExactnessNewApi, OpusFromFormat_stereo_20ms) {
853   const auto config = AudioEncoderOpus::SdpToConfig(
854       SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}}));
855   ASSERT_TRUE(SetUpSender(kTestFileFakeStereo32kHz, 32000));
856   ASSERT_NO_FATAL_FAILURE(SetUpTestExternalEncoder(
857       AudioEncoderOpus::MakeAudioEncoder(*config, 120), 120));
858   Run(audio_checksum, payload_checksum, /*expected_packets=*/50,
859       /*expected_channels=*/test::AcmReceiveTestOldApi::kStereoOutput);
860 }
861 #endif
862 
863 // TODO(webrtc:8649): Disabled until the Encoder counterpart of
864 // https://webrtc-review.googlesource.com/c/src/+/129768 lands.
865 #if defined(WEBRTC_LINUX) && defined(WEBRTC_ARCH_X86_64)
TEST_F(AcmSenderBitExactnessNewApi,DISABLED_OpusManyChannels)866 TEST_F(AcmSenderBitExactnessNewApi, DISABLED_OpusManyChannels) {
867   constexpr int kNumChannels = 4;
868   constexpr int kOpusPayloadType = 120;
869 
870   // Read a 4 channel file at 48kHz.
871   ASSERT_TRUE(SetUpSender(kTestFileQuad48kHz, 48000));
872 
873   const auto sdp_format = SdpAudioFormat("multiopus", 48000, kNumChannels,
874                                          {{"channel_mapping", "0,1,2,3"},
875                                           {"coupled_streams", "2"},
876                                           {"num_streams", "2"}});
877   const auto encoder_config =
878       AudioEncoderMultiChannelOpus::SdpToConfig(sdp_format);
879 
880   ASSERT_TRUE(encoder_config.has_value());
881 
882   ASSERT_NO_FATAL_FAILURE(
883       SetUpTestExternalEncoder(AudioEncoderMultiChannelOpus::MakeAudioEncoder(
884                                    *encoder_config, kOpusPayloadType),
885                                kOpusPayloadType));
886 
887   const auto decoder_config =
888       AudioDecoderMultiChannelOpus::SdpToConfig(sdp_format);
889   const auto opus_decoder =
890       AudioDecoderMultiChannelOpus::MakeAudioDecoder(*decoder_config);
891 
892   rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
893       rtc::make_ref_counted<test::AudioDecoderProxyFactory>(opus_decoder.get());
894 
895   // Set up an EXTERNAL DECODER to parse 4 channels.
896   Run("audio checksum check downstream|8051617907766bec5f4e4a4f7c6d5291",
897       "payload checksum check downstream|b09c52e44b2bdd9a0809e3a5b1623a76",
898       /*expected_packets=*/50,
899       /*expected_channels=*/test::AcmReceiveTestOldApi::kQuadOutput,
900       decoder_factory);
901 }
902 #endif
903 
904 #if defined(WEBRTC_LINUX) && defined(WEBRTC_ARCH_X86_64)
TEST_F(AcmSenderBitExactnessNewApi,OpusFromFormat_stereo_20ms_voip)905 TEST_F(AcmSenderBitExactnessNewApi, OpusFromFormat_stereo_20ms_voip) {
906   auto config = AudioEncoderOpus::SdpToConfig(
907       SdpAudioFormat("opus", 48000, 2, {{"stereo", "1"}}));
908   // If not set, default will be kAudio in case of stereo.
909   config->application = AudioEncoderOpusConfig::ApplicationMode::kVoip;
910   ASSERT_TRUE(SetUpSender(kTestFileFakeStereo32kHz, 32000));
911   ASSERT_NO_FATAL_FAILURE(SetUpTestExternalEncoder(
912       AudioEncoderOpus::MakeAudioEncoder(*config, 120), 120));
913   const std::string audio_maybe_sse =
914       "1010e60ad34cee73c939edaf563d0593"
915       "|c05b4523d4c3fad2bab96d2a56baa2d0";
916 
917   const std::string payload_maybe_sse =
918       "ea48d94e43217793af9b7e15ece94e54"
919       "|bd93c492087093daf662cdd968f6cdda";
920 
921   Run(audio_maybe_sse, payload_maybe_sse, /*expected_packets=*/50,
922       /*expected_channels=*/test::AcmReceiveTestOldApi::kStereoOutput);
923 }
924 #endif
925 
926 // This test is for verifying the SetBitRate function. The bitrate is changed at
927 // the beginning, and the number of generated bytes are checked.
928 class AcmSetBitRateTest : public ::testing::Test {
929  protected:
930   static const int kTestDurationMs = 1000;
931 
932   // Sets up the test::AcmSendTest object. Returns true on success, otherwise
933   // false.
SetUpSender()934   bool SetUpSender() {
935     const std::string input_file_name =
936         webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
937     // Note that `audio_source_` will loop forever. The test duration is set
938     // explicitly by `kTestDurationMs`.
939     audio_source_.reset(new test::InputAudioFile(input_file_name));
940     static const int kSourceRateHz = 32000;
941     send_test_.reset(new test::AcmSendTestOldApi(
942         audio_source_.get(), kSourceRateHz, kTestDurationMs));
943     return send_test_.get();
944   }
945 
946   // Registers a send codec in the test::AcmSendTest object. Returns true on
947   // success, false on failure.
RegisterSendCodec(absl::string_view payload_name,int sampling_freq_hz,int channels,int payload_type,int frame_size_samples,int frame_size_rtp_timestamps)948   virtual bool RegisterSendCodec(absl::string_view payload_name,
949                                  int sampling_freq_hz,
950                                  int channels,
951                                  int payload_type,
952                                  int frame_size_samples,
953                                  int frame_size_rtp_timestamps) {
954     return send_test_->RegisterCodec(payload_name, sampling_freq_hz, channels,
955                                      payload_type, frame_size_samples);
956   }
957 
RegisterExternalSendCodec(std::unique_ptr<AudioEncoder> external_speech_encoder,int payload_type)958   void RegisterExternalSendCodec(
959       std::unique_ptr<AudioEncoder> external_speech_encoder,
960       int payload_type) {
961     send_test_->RegisterExternalCodec(std::move(external_speech_encoder));
962   }
963 
RunInner(int min_expected_total_bits,int max_expected_total_bits)964   void RunInner(int min_expected_total_bits, int max_expected_total_bits) {
965     int nr_bytes = 0;
966     while (std::unique_ptr<test::Packet> next_packet =
967                send_test_->NextPacket()) {
968       nr_bytes += rtc::checked_cast<int>(next_packet->payload_length_bytes());
969     }
970     EXPECT_LE(min_expected_total_bits, nr_bytes * 8);
971     EXPECT_GE(max_expected_total_bits, nr_bytes * 8);
972   }
973 
SetUpTest(absl::string_view codec_name,int codec_sample_rate_hz,int channels,int payload_type,int codec_frame_size_samples,int codec_frame_size_rtp_timestamps)974   void SetUpTest(absl::string_view codec_name,
975                  int codec_sample_rate_hz,
976                  int channels,
977                  int payload_type,
978                  int codec_frame_size_samples,
979                  int codec_frame_size_rtp_timestamps) {
980     ASSERT_TRUE(SetUpSender());
981     ASSERT_TRUE(RegisterSendCodec(codec_name, codec_sample_rate_hz, channels,
982                                   payload_type, codec_frame_size_samples,
983                                   codec_frame_size_rtp_timestamps));
984   }
985 
986   std::unique_ptr<test::AcmSendTestOldApi> send_test_;
987   std::unique_ptr<test::InputAudioFile> audio_source_;
988 };
989 
990 class AcmSetBitRateNewApi : public AcmSetBitRateTest {
991  protected:
992   // Runs the test. SetUpSender() must have been called and a codec must be set
993   // up before calling this method.
Run(int min_expected_total_bits,int max_expected_total_bits)994   void Run(int min_expected_total_bits, int max_expected_total_bits) {
995     RunInner(min_expected_total_bits, max_expected_total_bits);
996   }
997 };
998 
TEST_F(AcmSetBitRateNewApi,OpusFromFormat_48khz_20ms_10kbps)999 TEST_F(AcmSetBitRateNewApi, OpusFromFormat_48khz_20ms_10kbps) {
1000   const auto config = AudioEncoderOpus::SdpToConfig(
1001       SdpAudioFormat("opus", 48000, 2, {{"maxaveragebitrate", "10000"}}));
1002   ASSERT_TRUE(SetUpSender());
1003   RegisterExternalSendCodec(AudioEncoderOpus::MakeAudioEncoder(*config, 107),
1004                             107);
1005   RunInner(7000, 12000);
1006 }
1007 
TEST_F(AcmSetBitRateNewApi,OpusFromFormat_48khz_20ms_50kbps)1008 TEST_F(AcmSetBitRateNewApi, OpusFromFormat_48khz_20ms_50kbps) {
1009   const auto config = AudioEncoderOpus::SdpToConfig(
1010       SdpAudioFormat("opus", 48000, 2, {{"maxaveragebitrate", "50000"}}));
1011   ASSERT_TRUE(SetUpSender());
1012   RegisterExternalSendCodec(AudioEncoderOpus::MakeAudioEncoder(*config, 107),
1013                             107);
1014   RunInner(40000, 60000);
1015 }
1016 
1017 // Verify that it works when the data to send is mono and the encoder is set to
1018 // send surround audio.
TEST_F(AudioCodingModuleTestOldApi,SendingMultiChannelForMonoInput)1019 TEST_F(AudioCodingModuleTestOldApi, SendingMultiChannelForMonoInput) {
1020   constexpr int kSampleRateHz = 48000;
1021   constexpr int kSamplesPerChannel = kSampleRateHz * 10 / 1000;
1022 
1023   audio_format_ = SdpAudioFormat({"multiopus",
1024                                   kSampleRateHz,
1025                                   6,
1026                                   {{"minptime", "10"},
1027                                    {"useinbandfec", "1"},
1028                                    {"channel_mapping", "0,4,1,2,3,5"},
1029                                    {"num_streams", "4"},
1030                                    {"coupled_streams", "2"}}});
1031 
1032   RegisterCodec();
1033 
1034   input_frame_.sample_rate_hz_ = kSampleRateHz;
1035   input_frame_.num_channels_ = 1;
1036   input_frame_.samples_per_channel_ = kSamplesPerChannel;
1037   for (size_t k = 0; k < 10; ++k) {
1038     ASSERT_GE(acm_->Add10MsData(input_frame_), 0);
1039     input_frame_.timestamp_ += kSamplesPerChannel;
1040   }
1041 }
1042 
1043 // Verify that it works when the data to send is stereo and the encoder is set
1044 // to send surround audio.
TEST_F(AudioCodingModuleTestOldApi,SendingMultiChannelForStereoInput)1045 TEST_F(AudioCodingModuleTestOldApi, SendingMultiChannelForStereoInput) {
1046   constexpr int kSampleRateHz = 48000;
1047   constexpr int kSamplesPerChannel = (kSampleRateHz * 10) / 1000;
1048 
1049   audio_format_ = SdpAudioFormat({"multiopus",
1050                                   kSampleRateHz,
1051                                   6,
1052                                   {{"minptime", "10"},
1053                                    {"useinbandfec", "1"},
1054                                    {"channel_mapping", "0,4,1,2,3,5"},
1055                                    {"num_streams", "4"},
1056                                    {"coupled_streams", "2"}}});
1057 
1058   RegisterCodec();
1059 
1060   input_frame_.sample_rate_hz_ = kSampleRateHz;
1061   input_frame_.num_channels_ = 2;
1062   input_frame_.samples_per_channel_ = kSamplesPerChannel;
1063   for (size_t k = 0; k < 10; ++k) {
1064     ASSERT_GE(acm_->Add10MsData(input_frame_), 0);
1065     input_frame_.timestamp_ += kSamplesPerChannel;
1066   }
1067 }
1068 
1069 // Verify that it works when the data to send is mono and the encoder is set to
1070 // send stereo audio.
TEST_F(AudioCodingModuleTestOldApi,SendingStereoForMonoInput)1071 TEST_F(AudioCodingModuleTestOldApi, SendingStereoForMonoInput) {
1072   constexpr int kSampleRateHz = 48000;
1073   constexpr int kSamplesPerChannel = (kSampleRateHz * 10) / 1000;
1074 
1075   audio_format_ = SdpAudioFormat("L16", kSampleRateHz, 2);
1076 
1077   RegisterCodec();
1078 
1079   input_frame_.sample_rate_hz_ = kSampleRateHz;
1080   input_frame_.num_channels_ = 1;
1081   input_frame_.samples_per_channel_ = kSamplesPerChannel;
1082   for (size_t k = 0; k < 10; ++k) {
1083     ASSERT_GE(acm_->Add10MsData(input_frame_), 0);
1084     input_frame_.timestamp_ += kSamplesPerChannel;
1085   }
1086 }
1087 
1088 // Verify that it works when the data to send is stereo and the encoder is set
1089 // to send mono audio.
TEST_F(AudioCodingModuleTestOldApi,SendingMonoForStereoInput)1090 TEST_F(AudioCodingModuleTestOldApi, SendingMonoForStereoInput) {
1091   constexpr int kSampleRateHz = 48000;
1092   constexpr int kSamplesPerChannel = (kSampleRateHz * 10) / 1000;
1093 
1094   audio_format_ = SdpAudioFormat("L16", kSampleRateHz, 1);
1095 
1096   RegisterCodec();
1097 
1098   input_frame_.sample_rate_hz_ = kSampleRateHz;
1099   input_frame_.num_channels_ = 1;
1100   input_frame_.samples_per_channel_ = kSamplesPerChannel;
1101   for (size_t k = 0; k < 10; ++k) {
1102     ASSERT_GE(acm_->Add10MsData(input_frame_), 0);
1103     input_frame_.timestamp_ += kSamplesPerChannel;
1104   }
1105 }
1106 
1107 // The result on the Android platforms is inconsistent for this test case.
1108 // On android_rel the result is different from android and android arm64 rel.
1109 #if defined(WEBRTC_ANDROID)
1110 #define MAYBE_OpusFromFormat_48khz_20ms_100kbps \
1111   DISABLED_OpusFromFormat_48khz_20ms_100kbps
1112 #else
1113 #define MAYBE_OpusFromFormat_48khz_20ms_100kbps \
1114   OpusFromFormat_48khz_20ms_100kbps
1115 #endif
TEST_F(AcmSetBitRateNewApi,MAYBE_OpusFromFormat_48khz_20ms_100kbps)1116 TEST_F(AcmSetBitRateNewApi, MAYBE_OpusFromFormat_48khz_20ms_100kbps) {
1117   const auto config = AudioEncoderOpus::SdpToConfig(
1118       SdpAudioFormat("opus", 48000, 2, {{"maxaveragebitrate", "100000"}}));
1119   ASSERT_TRUE(SetUpSender());
1120   RegisterExternalSendCodec(AudioEncoderOpus::MakeAudioEncoder(*config, 107),
1121                             107);
1122   RunInner(80000, 120000);
1123 }
1124 
TEST_F(AcmSenderBitExactnessOldApi,External_Pcmu_20ms)1125 TEST_F(AcmSenderBitExactnessOldApi, External_Pcmu_20ms) {
1126   AudioEncoderPcmU::Config config;
1127   config.frame_size_ms = 20;
1128   config.num_channels = 1;
1129   config.payload_type = 0;
1130   AudioEncoderPcmU encoder(config);
1131   auto mock_encoder = std::make_unique<MockAudioEncoder>();
1132   // Set expectations on the mock encoder and also delegate the calls to the
1133   // real encoder.
1134   EXPECT_CALL(*mock_encoder, SampleRateHz())
1135       .Times(AtLeast(1))
1136       .WillRepeatedly(Invoke(&encoder, &AudioEncoderPcmU::SampleRateHz));
1137   EXPECT_CALL(*mock_encoder, NumChannels())
1138       .Times(AtLeast(1))
1139       .WillRepeatedly(Invoke(&encoder, &AudioEncoderPcmU::NumChannels));
1140   EXPECT_CALL(*mock_encoder, RtpTimestampRateHz())
1141       .Times(AtLeast(1))
1142       .WillRepeatedly(Invoke(&encoder, &AudioEncoderPcmU::RtpTimestampRateHz));
1143   EXPECT_CALL(*mock_encoder, Num10MsFramesInNextPacket())
1144       .Times(AtLeast(1))
1145       .WillRepeatedly(
1146           Invoke(&encoder, &AudioEncoderPcmU::Num10MsFramesInNextPacket));
1147   EXPECT_CALL(*mock_encoder, GetTargetBitrate())
1148       .Times(AtLeast(1))
1149       .WillRepeatedly(Invoke(&encoder, &AudioEncoderPcmU::GetTargetBitrate));
1150   EXPECT_CALL(*mock_encoder, EncodeImpl(_, _, _))
1151       .Times(AtLeast(1))
1152       .WillRepeatedly(Invoke(
1153           &encoder, static_cast<AudioEncoder::EncodedInfo (AudioEncoder::*)(
1154                         uint32_t, rtc::ArrayView<const int16_t>, rtc::Buffer*)>(
1155                         &AudioEncoderPcmU::Encode)));
1156   ASSERT_TRUE(SetUpSender(kTestFileMono32kHz, 32000));
1157   ASSERT_NO_FATAL_FAILURE(
1158       SetUpTestExternalEncoder(std::move(mock_encoder), config.payload_type));
1159   Run("c8d1fc677f33c2022ec5f83c7f302280", "8f9b8750bd80fe26b6cbf6659b89f0f9",
1160       50, test::AcmReceiveTestOldApi::kMonoOutput);
1161 }
1162 
1163 // This test fixture is implemented to run ACM and change the desired output
1164 // frequency during the call. The input packets are simply PCM16b-wb encoded
1165 // payloads with a constant value of `kSampleValue`. The test fixture itself
1166 // acts as PacketSource in between the receive test class and the constant-
1167 // payload packet source class. The output is both written to file, and analyzed
1168 // in this test fixture.
1169 class AcmSwitchingOutputFrequencyOldApi : public ::testing::Test,
1170                                           public test::PacketSource,
1171                                           public test::AudioSink {
1172  protected:
1173   static const size_t kTestNumPackets = 50;
1174   static const int kEncodedSampleRateHz = 16000;
1175   static const size_t kPayloadLenSamples = 30 * kEncodedSampleRateHz / 1000;
1176   static const int kPayloadType = 108;  // Default payload type for PCM16b-wb.
1177 
AcmSwitchingOutputFrequencyOldApi()1178   AcmSwitchingOutputFrequencyOldApi()
1179       : first_output_(true),
1180         num_packets_(0),
1181         packet_source_(kPayloadLenSamples,
1182                        kSampleValue,
1183                        kEncodedSampleRateHz,
1184                        kPayloadType),
1185         output_freq_2_(0),
1186         has_toggled_(false) {}
1187 
Run(int output_freq_1,int output_freq_2,int toggle_period_ms)1188   void Run(int output_freq_1, int output_freq_2, int toggle_period_ms) {
1189     // Set up the receiver used to decode the packets and verify the decoded
1190     // output.
1191     const std::string output_file_name =
1192         webrtc::test::OutputPath() +
1193         ::testing::UnitTest::GetInstance()
1194             ->current_test_info()
1195             ->test_case_name() +
1196         "_" + ::testing::UnitTest::GetInstance()->current_test_info()->name() +
1197         "_output.pcm";
1198     test::OutputAudioFile output_file(output_file_name);
1199     // Have the output audio sent both to file and to the WriteArray method in
1200     // this class.
1201     test::AudioSinkFork output(this, &output_file);
1202     test::AcmReceiveTestToggleOutputFreqOldApi receive_test(
1203         this, &output, output_freq_1, output_freq_2, toggle_period_ms,
1204         test::AcmReceiveTestOldApi::kMonoOutput);
1205     ASSERT_NO_FATAL_FAILURE(receive_test.RegisterDefaultCodecs());
1206     output_freq_2_ = output_freq_2;
1207 
1208     // This is where the actual test is executed.
1209     receive_test.Run();
1210 
1211     // Delete output file.
1212     remove(output_file_name.c_str());
1213   }
1214 
1215   // Inherited from test::PacketSource.
NextPacket()1216   std::unique_ptr<test::Packet> NextPacket() override {
1217     // Check if it is time to terminate the test. The packet source is of type
1218     // ConstantPcmPacketSource, which is infinite, so we must end the test
1219     // "manually".
1220     if (num_packets_++ > kTestNumPackets) {
1221       EXPECT_TRUE(has_toggled_);
1222       return NULL;  // Test ended.
1223     }
1224 
1225     // Get the next packet from the source.
1226     return packet_source_.NextPacket();
1227   }
1228 
1229   // Inherited from test::AudioSink.
WriteArray(const int16_t * audio,size_t num_samples)1230   bool WriteArray(const int16_t* audio, size_t num_samples) override {
1231     // Skip checking the first output frame, since it has a number of zeros
1232     // due to how NetEq is initialized.
1233     if (first_output_) {
1234       first_output_ = false;
1235       return true;
1236     }
1237     for (size_t i = 0; i < num_samples; ++i) {
1238       EXPECT_EQ(kSampleValue, audio[i]);
1239     }
1240     if (num_samples ==
1241         static_cast<size_t>(output_freq_2_ / 100))  // Size of 10 ms frame.
1242       has_toggled_ = true;
1243     // The return value does not say if the values match the expectation, just
1244     // that the method could process the samples.
1245     return true;
1246   }
1247 
1248   const int16_t kSampleValue = 1000;
1249   bool first_output_;
1250   size_t num_packets_;
1251   test::ConstantPcmPacketSource packet_source_;
1252   int output_freq_2_;
1253   bool has_toggled_;
1254 };
1255 
TEST_F(AcmSwitchingOutputFrequencyOldApi,TestWithoutToggling)1256 TEST_F(AcmSwitchingOutputFrequencyOldApi, TestWithoutToggling) {
1257   Run(16000, 16000, 1000);
1258 }
1259 
TEST_F(AcmSwitchingOutputFrequencyOldApi,Toggle16KhzTo32Khz)1260 TEST_F(AcmSwitchingOutputFrequencyOldApi, Toggle16KhzTo32Khz) {
1261   Run(16000, 32000, 1000);
1262 }
1263 
TEST_F(AcmSwitchingOutputFrequencyOldApi,Toggle32KhzTo16Khz)1264 TEST_F(AcmSwitchingOutputFrequencyOldApi, Toggle32KhzTo16Khz) {
1265   Run(32000, 16000, 1000);
1266 }
1267 
TEST_F(AcmSwitchingOutputFrequencyOldApi,Toggle16KhzTo8Khz)1268 TEST_F(AcmSwitchingOutputFrequencyOldApi, Toggle16KhzTo8Khz) {
1269   Run(16000, 8000, 1000);
1270 }
1271 
TEST_F(AcmSwitchingOutputFrequencyOldApi,Toggle8KhzTo16Khz)1272 TEST_F(AcmSwitchingOutputFrequencyOldApi, Toggle8KhzTo16Khz) {
1273   Run(8000, 16000, 1000);
1274 }
1275 
1276 #endif
1277 
1278 }  // namespace webrtc
1279