blob: 5d40fc18ef6f5aaf47965668ee93bdf6f7ad108c [file] [log] [blame]
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +00001/*
2 * Copyright (c) 2013 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/acm2/acm_receiver.h"
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000012
13#include <algorithm> // std::min
kwiberg16c5a962016-02-15 02:27:22 -080014#include <memory>
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Karl Wiberg377a2312018-09-24 14:52:51 +020017#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Karl Wiberg377a2312018-09-24 14:52:51 +020018#include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/include/audio_coding_module.h"
20#include "modules/audio_coding/neteq/tools/rtp_generator.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020021#include "modules/include/module_common_types.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
Karl Wiberge40468b2017-11-22 10:42:26 +010023#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "system_wrappers/include/clock.h"
25#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080026#include "test/testsupport/file_utils.h"
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000027
28namespace webrtc {
29
30namespace acm2 {
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000031
32class AcmReceiverTestOldApi : public AudioPacketizationCallback,
33 public ::testing::Test {
34 protected:
35 AcmReceiverTestOldApi()
36 : timestamp_(0),
37 packet_sent_(false),
38 last_packet_send_timestamp_(timestamp_),
Niels Möllerc936cb62019-03-19 14:10:16 +010039 last_frame_type_(AudioFrameType::kEmptyFrame) {
Karl Wiberg377a2312018-09-24 14:52:51 +020040 config_.decoder_factory = decoder_factory_;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000041 }
42
43 ~AcmReceiverTestOldApi() {}
44
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000045 void SetUp() override {
kwibergc13ded52016-06-17 06:00:45 -070046 acm_.reset(AudioCodingModule::Create(config_));
henrik.lundin500c04b2016-03-08 02:36:04 -080047 receiver_.reset(new AcmReceiver(config_));
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000048 ASSERT_TRUE(receiver_.get() != NULL);
49 ASSERT_TRUE(acm_.get() != NULL);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000050 acm_->InitializeReceiver();
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000051 acm_->RegisterTransportCallback(this);
52
Niels Möllerafb5dbb2019-02-15 15:21:47 +010053 rtp_header_.sequenceNumber = 0;
54 rtp_header_.timestamp = 0;
55 rtp_header_.markerBit = false;
56 rtp_header_.ssrc = 0x12345678; // Arbitrary.
57 rtp_header_.numCSRCs = 0;
58 rtp_header_.payloadType = 0;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000059 }
60
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000061 void TearDown() override {}
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000062
Karl Wiberg377a2312018-09-24 14:52:51 +020063 AudioCodecInfo SetEncoder(int payload_type,
64 const SdpAudioFormat& format,
65 const std::map<int, int> cng_payload_types = {}) {
66 // Create the speech encoder.
67 AudioCodecInfo info = encoder_factory_->QueryAudioEncoder(format).value();
68 std::unique_ptr<AudioEncoder> enc =
69 encoder_factory_->MakeAudioEncoder(payload_type, format, absl::nullopt);
70
71 // If we have a compatible CN specification, stack a CNG on top.
72 auto it = cng_payload_types.find(info.sample_rate_hz);
73 if (it != cng_payload_types.end()) {
Karl Wiberg23659362018-11-01 11:13:44 +010074 AudioEncoderCngConfig config;
Karl Wiberg377a2312018-09-24 14:52:51 +020075 config.speech_encoder = std::move(enc);
76 config.num_channels = 1;
77 config.payload_type = it->second;
78 config.vad_mode = Vad::kVadNormal;
Karl Wiberg23659362018-11-01 11:13:44 +010079 enc = CreateComfortNoiseEncoder(std::move(config));
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000080 }
Karl Wiberg377a2312018-09-24 14:52:51 +020081
82 // Actually start using the new encoder.
83 acm_->SetEncoder(std::move(enc));
84 return info;
85 }
86
87 int InsertOnePacketOfSilence(const AudioCodecInfo& info) {
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000088 // Frame setup according to the codec.
Karl Wiberg377a2312018-09-24 14:52:51 +020089 AudioFrame frame;
90 frame.sample_rate_hz_ = info.sample_rate_hz;
91 frame.samples_per_channel_ = info.sample_rate_hz / 100; // 10 ms.
92 frame.num_channels_ = info.num_channels;
yujo36b1a5f2017-06-12 12:45:32 -070093 frame.Mute();
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000094 packet_sent_ = false;
95 last_packet_send_timestamp_ = timestamp_;
Karl Wiberg377a2312018-09-24 14:52:51 +020096 int num_10ms_frames = 0;
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +000097 while (!packet_sent_) {
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +000098 frame.timestamp_ = timestamp_;
Mirko Bonadei737e0732017-10-19 09:00:17 +020099 timestamp_ += rtc::checked_cast<uint32_t>(frame.samples_per_channel_);
Karl Wiberg377a2312018-09-24 14:52:51 +0200100 EXPECT_GE(acm_->Add10MsData(frame), 0);
101 ++num_10ms_frames;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000102 }
Karl Wiberg377a2312018-09-24 14:52:51 +0200103 return num_10ms_frames;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000104 }
105
Niels Möller87e2d782019-03-07 10:18:23 +0100106 int SendData(AudioFrameType frame_type,
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000107 uint8_t payload_type,
108 uint32_t timestamp,
109 const uint8_t* payload_data,
Niels Möllerc35b6e62019-04-25 16:31:18 +0200110 size_t payload_len_bytes) override {
Niels Möllerc936cb62019-03-19 14:10:16 +0100111 if (frame_type == AudioFrameType::kEmptyFrame)
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000112 return 0;
113
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100114 rtp_header_.payloadType = payload_type;
115 rtp_header_.timestamp = timestamp;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000116
kwibergee2bac22015-11-11 10:34:00 -0800117 int ret_val = receiver_->InsertPacket(
118 rtp_header_,
119 rtc::ArrayView<const uint8_t>(payload_data, payload_len_bytes));
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000120 if (ret_val < 0) {
121 assert(false);
122 return -1;
123 }
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100124 rtp_header_.sequenceNumber++;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000125 packet_sent_ = true;
126 last_frame_type_ = frame_type;
127 return 0;
128 }
129
Karl Wiberg377a2312018-09-24 14:52:51 +0200130 const rtc::scoped_refptr<AudioEncoderFactory> encoder_factory_ =
131 CreateBuiltinAudioEncoderFactory();
132 const rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_ =
133 CreateBuiltinAudioDecoderFactory();
henrik.lundin500c04b2016-03-08 02:36:04 -0800134 AudioCodingModule::Config config_;
kwiberg16c5a962016-02-15 02:27:22 -0800135 std::unique_ptr<AcmReceiver> receiver_;
kwiberg16c5a962016-02-15 02:27:22 -0800136 std::unique_ptr<AudioCodingModule> acm_;
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100137 RTPHeader rtp_header_;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000138 uint32_t timestamp_;
139 bool packet_sent_; // Set when SendData is called reset when inserting audio.
140 uint32_t last_packet_send_timestamp_;
Niels Möller87e2d782019-03-07 10:18:23 +0100141 AudioFrameType last_frame_type_;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000142};
143
Peter Boströme2976c82016-01-04 22:44:05 +0100144#if defined(WEBRTC_ANDROID)
Peter Boströme2976c82016-01-04 22:44:05 +0100145#define MAYBE_SampleRate DISABLED_SampleRate
146#else
147#define MAYBE_SampleRate SampleRate
148#endif
149TEST_F(AcmReceiverTestOldApi, MAYBE_SampleRate) {
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100150 const std::map<int, SdpAudioFormat> codecs = {{0, {"ISAC", 16000, 1}},
151 {1, {"ISAC", 32000, 1}}};
152 receiver_->SetCodecs(codecs);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000153
Karl Wiberg377a2312018-09-24 14:52:51 +0200154 constexpr int kOutSampleRateHz = 8000; // Different than codec sample rate.
155 for (size_t i = 0; i < codecs.size(); ++i) {
156 const int payload_type = rtc::checked_cast<int>(i);
157 const int num_10ms_frames =
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100158 InsertOnePacketOfSilence(SetEncoder(payload_type, codecs.at(i)));
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000159 for (int k = 0; k < num_10ms_frames; ++k) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200160 AudioFrame frame;
henrik.lundin834a6ea2016-05-13 03:45:24 -0700161 bool muted;
162 EXPECT_EQ(0, receiver_->GetAudio(kOutSampleRateHz, &frame, &muted));
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000163 }
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100164 EXPECT_EQ(encoder_factory_->QueryAudioEncoder(codecs.at(i))->sample_rate_hz,
Karl Wiberg377a2312018-09-24 14:52:51 +0200165 receiver_->last_output_sample_rate_hz());
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000166 }
167}
168
henrik.lundin7dc68892016-04-06 01:03:02 -0700169class AcmReceiverTestFaxModeOldApi : public AcmReceiverTestOldApi {
170 protected:
171 AcmReceiverTestFaxModeOldApi() {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200172 config_.neteq_config.for_test_no_time_stretching = true;
henrik.lundin7dc68892016-04-06 01:03:02 -0700173 }
174
Karl Wiberg377a2312018-09-24 14:52:51 +0200175 void RunVerifyAudioFrame(const SdpAudioFormat& codec) {
henrik.lundin7dc68892016-04-06 01:03:02 -0700176 // Make sure "fax mode" is enabled. This will avoid delay changes unless the
177 // packet-loss concealment is made. We do this in order to make the
178 // timestamp increments predictable; in normal mode, NetEq may decide to do
179 // accelerate or pre-emptive expand operations after some time, offsetting
180 // the timestamp.
Henrik Lundin7687ad52018-07-02 10:14:46 +0200181 EXPECT_TRUE(config_.neteq_config.for_test_no_time_stretching);
henrik.lundin7dc68892016-04-06 01:03:02 -0700182
Karl Wiberg377a2312018-09-24 14:52:51 +0200183 constexpr int payload_type = 17;
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100184 receiver_->SetCodecs({{payload_type, codec}});
henrik.lundin7dc68892016-04-06 01:03:02 -0700185
Karl Wiberg377a2312018-09-24 14:52:51 +0200186 const AudioCodecInfo info = SetEncoder(payload_type, codec);
187 const int output_sample_rate_hz = info.sample_rate_hz;
188 const size_t output_channels = info.num_channels;
henrik.lundin7dc68892016-04-06 01:03:02 -0700189 const size_t samples_per_ms = rtc::checked_cast<size_t>(
190 rtc::CheckedDivExact(output_sample_rate_hz, 1000));
henrik.lundin7dc68892016-04-06 01:03:02 -0700191 const AudioFrame::VADActivity expected_vad_activity =
192 output_sample_rate_hz > 16000 ? AudioFrame::kVadActive
193 : AudioFrame::kVadPassive;
194
195 // Expect the first output timestamp to be 5*fs/8000 samples before the
196 // first inserted timestamp (because of NetEq's look-ahead). (This value is
197 // defined in Expand::overlap_length_.)
Yves Gerey665174f2018-06-19 15:03:05 +0200198 uint32_t expected_output_ts =
199 last_packet_send_timestamp_ -
henrik.lundin7dc68892016-04-06 01:03:02 -0700200 rtc::CheckedDivExact(5 * output_sample_rate_hz, 8000);
201
202 AudioFrame frame;
henrik.lundin834a6ea2016-05-13 03:45:24 -0700203 bool muted;
204 EXPECT_EQ(0, receiver_->GetAudio(output_sample_rate_hz, &frame, &muted));
henrik.lundin15c51e32016-04-06 08:38:56 -0700205 // Expect timestamp = 0 before first packet is inserted.
206 EXPECT_EQ(0u, frame.timestamp_);
henrik.lundin7dc68892016-04-06 01:03:02 -0700207 for (int i = 0; i < 5; ++i) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200208 const int num_10ms_frames = InsertOnePacketOfSilence(info);
henrik.lundin7dc68892016-04-06 01:03:02 -0700209 for (int k = 0; k < num_10ms_frames; ++k) {
henrik.lundin834a6ea2016-05-13 03:45:24 -0700210 EXPECT_EQ(0,
211 receiver_->GetAudio(output_sample_rate_hz, &frame, &muted));
henrik.lundin7dc68892016-04-06 01:03:02 -0700212 EXPECT_EQ(expected_output_ts, frame.timestamp_);
Mirko Bonadei737e0732017-10-19 09:00:17 +0200213 expected_output_ts += rtc::checked_cast<uint32_t>(10 * samples_per_ms);
henrik.lundin7dc68892016-04-06 01:03:02 -0700214 EXPECT_EQ(10 * samples_per_ms, frame.samples_per_channel_);
215 EXPECT_EQ(output_sample_rate_hz, frame.sample_rate_hz_);
216 EXPECT_EQ(output_channels, frame.num_channels_);
217 EXPECT_EQ(AudioFrame::kNormalSpeech, frame.speech_type_);
218 EXPECT_EQ(expected_vad_activity, frame.vad_activity_);
henrik.lundin834a6ea2016-05-13 03:45:24 -0700219 EXPECT_FALSE(muted);
henrik.lundin7dc68892016-04-06 01:03:02 -0700220 }
221 }
222 }
223};
224
225#if defined(WEBRTC_ANDROID)
226#define MAYBE_VerifyAudioFramePCMU DISABLED_VerifyAudioFramePCMU
227#else
228#define MAYBE_VerifyAudioFramePCMU VerifyAudioFramePCMU
229#endif
230TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFramePCMU) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200231 RunVerifyAudioFrame({"PCMU", 8000, 1});
henrik.lundin7dc68892016-04-06 01:03:02 -0700232}
233
234#if defined(WEBRTC_ANDROID)
235#define MAYBE_VerifyAudioFrameISAC DISABLED_VerifyAudioFrameISAC
236#else
237#define MAYBE_VerifyAudioFrameISAC VerifyAudioFrameISAC
238#endif
239TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFrameISAC) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200240 RunVerifyAudioFrame({"ISAC", 16000, 1});
henrik.lundin7dc68892016-04-06 01:03:02 -0700241}
242
243#if defined(WEBRTC_ANDROID)
244#define MAYBE_VerifyAudioFrameOpus DISABLED_VerifyAudioFrameOpus
245#else
246#define MAYBE_VerifyAudioFrameOpus VerifyAudioFrameOpus
247#endif
248TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFrameOpus) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200249 RunVerifyAudioFrame({"opus", 48000, 2});
henrik.lundin7dc68892016-04-06 01:03:02 -0700250}
251
Peter Boströme2976c82016-01-04 22:44:05 +0100252#if defined(WEBRTC_ANDROID)
253#define MAYBE_PostdecodingVad DISABLED_PostdecodingVad
254#else
255#define MAYBE_PostdecodingVad PostdecodingVad
256#endif
257TEST_F(AcmReceiverTestOldApi, MAYBE_PostdecodingVad) {
henrik.lundin500c04b2016-03-08 02:36:04 -0800258 EXPECT_TRUE(config_.neteq_config.enable_post_decode_vad);
Karl Wiberg377a2312018-09-24 14:52:51 +0200259 constexpr int payload_type = 34;
260 const SdpAudioFormat codec = {"L16", 16000, 1};
261 const AudioCodecInfo info = SetEncoder(payload_type, codec);
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100262 receiver_->SetCodecs({{payload_type, codec}});
Karl Wiberg377a2312018-09-24 14:52:51 +0200263 constexpr int kNumPackets = 5;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000264 AudioFrame frame;
265 for (int n = 0; n < kNumPackets; ++n) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200266 const int num_10ms_frames = InsertOnePacketOfSilence(info);
henrik.lundin834a6ea2016-05-13 03:45:24 -0700267 for (int k = 0; k < num_10ms_frames; ++k) {
268 bool muted;
Karl Wiberg377a2312018-09-24 14:52:51 +0200269 ASSERT_EQ(0, receiver_->GetAudio(info.sample_rate_hz, &frame, &muted));
henrik.lundin834a6ea2016-05-13 03:45:24 -0700270 }
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000271 }
272 EXPECT_EQ(AudioFrame::kVadPassive, frame.vad_activity_);
henrik.lundin500c04b2016-03-08 02:36:04 -0800273}
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000274
henrik.lundin500c04b2016-03-08 02:36:04 -0800275class AcmReceiverTestPostDecodeVadPassiveOldApi : public AcmReceiverTestOldApi {
276 protected:
277 AcmReceiverTestPostDecodeVadPassiveOldApi() {
278 config_.neteq_config.enable_post_decode_vad = false;
279 }
280};
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000281
henrik.lundin500c04b2016-03-08 02:36:04 -0800282#if defined(WEBRTC_ANDROID)
283#define MAYBE_PostdecodingVad DISABLED_PostdecodingVad
284#else
285#define MAYBE_PostdecodingVad PostdecodingVad
286#endif
287TEST_F(AcmReceiverTestPostDecodeVadPassiveOldApi, MAYBE_PostdecodingVad) {
288 EXPECT_FALSE(config_.neteq_config.enable_post_decode_vad);
Karl Wiberg377a2312018-09-24 14:52:51 +0200289 constexpr int payload_type = 34;
290 const SdpAudioFormat codec = {"L16", 16000, 1};
291 const AudioCodecInfo info = SetEncoder(payload_type, codec);
292 encoder_factory_->QueryAudioEncoder(codec).value();
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100293 receiver_->SetCodecs({{payload_type, codec}});
henrik.lundin500c04b2016-03-08 02:36:04 -0800294 const int kNumPackets = 5;
henrik.lundin500c04b2016-03-08 02:36:04 -0800295 AudioFrame frame;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000296 for (int n = 0; n < kNumPackets; ++n) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200297 const int num_10ms_frames = InsertOnePacketOfSilence(info);
henrik.lundin834a6ea2016-05-13 03:45:24 -0700298 for (int k = 0; k < num_10ms_frames; ++k) {
299 bool muted;
Karl Wiberg377a2312018-09-24 14:52:51 +0200300 ASSERT_EQ(0, receiver_->GetAudio(info.sample_rate_hz, &frame, &muted));
henrik.lundin834a6ea2016-05-13 03:45:24 -0700301 }
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000302 }
303 EXPECT_EQ(AudioFrame::kVadUnknown, frame.vad_activity_);
304}
305
Peter Boströme2976c82016-01-04 22:44:05 +0100306#if defined(WEBRTC_ANDROID)
307#define MAYBE_LastAudioCodec DISABLED_LastAudioCodec
kwiberg98ab3a42015-09-30 21:54:21 -0700308#else
Peter Boströme2976c82016-01-04 22:44:05 +0100309#define MAYBE_LastAudioCodec LastAudioCodec
kwiberg98ab3a42015-09-30 21:54:21 -0700310#endif
Peter Boströme2976c82016-01-04 22:44:05 +0100311#if defined(WEBRTC_CODEC_ISAC)
312TEST_F(AcmReceiverTestOldApi, MAYBE_LastAudioCodec) {
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100313 const std::map<int, SdpAudioFormat> codecs = {{0, {"ISAC", 16000, 1}},
314 {1, {"PCMA", 8000, 1}},
315 {2, {"ISAC", 32000, 1}},
316 {3, {"L16", 32000, 1}}};
Jonas Olssona4d87372019-07-05 19:08:33 +0200317 const std::map<int, int> cng_payload_types = {
318 {8000, 100}, {16000, 101}, {32000, 102}};
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100319 {
320 std::map<int, SdpAudioFormat> receive_codecs = codecs;
321 for (const auto& cng_type : cng_payload_types) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200322 receive_codecs.emplace(std::make_pair(
323 cng_type.second, SdpAudioFormat("CN", cng_type.first, 1)));
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100324 }
325 receiver_->SetCodecs(receive_codecs);
Karl Wiberg377a2312018-09-24 14:52:51 +0200326 }
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000327
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000328 // No audio payload is received.
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100329 EXPECT_EQ(absl::nullopt, receiver_->LastDecoder());
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000330
331 // Start with sending DTX.
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000332 packet_sent_ = false;
Karl Wiberg377a2312018-09-24 14:52:51 +0200333 InsertOnePacketOfSilence(
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100334 SetEncoder(0, codecs.at(0), cng_payload_types)); // Enough to test
Jonas Olssona4d87372019-07-05 19:08:33 +0200335 // with one codec.
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000336 ASSERT_TRUE(packet_sent_);
Niels Möllerc936cb62019-03-19 14:10:16 +0100337 EXPECT_EQ(AudioFrameType::kAudioFrameCN, last_frame_type_);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000338
339 // Has received, only, DTX. Last Audio codec is undefined.
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100340 EXPECT_EQ(absl::nullopt, receiver_->LastDecoder());
341 EXPECT_EQ(absl::nullopt, receiver_->last_packet_sample_rate_hz());
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000342
Karl Wiberg377a2312018-09-24 14:52:51 +0200343 for (size_t i = 0; i < codecs.size(); ++i) {
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000344 // Set DTX off to send audio payload.
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000345 packet_sent_ = false;
Karl Wiberg377a2312018-09-24 14:52:51 +0200346 const int payload_type = rtc::checked_cast<int>(i);
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100347 const AudioCodecInfo info_without_cng =
348 SetEncoder(payload_type, codecs.at(i));
Karl Wiberg377a2312018-09-24 14:52:51 +0200349 InsertOnePacketOfSilence(info_without_cng);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000350
351 // Sanity check if Actually an audio payload received, and it should be
352 // of type "speech."
353 ASSERT_TRUE(packet_sent_);
Niels Möllerc936cb62019-03-19 14:10:16 +0100354 ASSERT_EQ(AudioFrameType::kAudioFrameSpeech, last_frame_type_);
Karl Wiberg377a2312018-09-24 14:52:51 +0200355 EXPECT_EQ(info_without_cng.sample_rate_hz,
356 receiver_->last_packet_sample_rate_hz());
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000357
358 // Set VAD on to send DTX. Then check if the "Last Audio codec" returns
Karl Wiberg377a2312018-09-24 14:52:51 +0200359 // the expected codec. Encode repeatedly until a DTX is sent.
360 const AudioCodecInfo info_with_cng =
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100361 SetEncoder(payload_type, codecs.at(i), cng_payload_types);
Niels Möllerc936cb62019-03-19 14:10:16 +0100362 while (last_frame_type_ != AudioFrameType::kAudioFrameCN) {
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000363 packet_sent_ = false;
Karl Wiberg377a2312018-09-24 14:52:51 +0200364 InsertOnePacketOfSilence(info_with_cng);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000365 ASSERT_TRUE(packet_sent_);
366 }
Karl Wiberg377a2312018-09-24 14:52:51 +0200367 EXPECT_EQ(info_with_cng.sample_rate_hz,
368 receiver_->last_packet_sample_rate_hz());
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100369 EXPECT_EQ(codecs.at(i), receiver_->LastDecoder()->second);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000370 }
371}
Peter Boströme2976c82016-01-04 22:44:05 +0100372#endif
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000373
374} // namespace acm2
375
376} // namespace webrtc