blob: e5a7684d3452220e05e61d3b5510b6657d09d505 [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_),
pbos22993e12015-10-19 02:39:06 -070039 last_frame_type_(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
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000106 int SendData(FrameType frame_type,
107 uint8_t payload_type,
108 uint32_t timestamp,
109 const uint8_t* payload_data,
110 size_t payload_len_bytes,
111 const RTPFragmentationHeader* fragmentation) override {
pbos22993e12015-10-19 02:39:06 -0700112 if (frame_type == kEmptyFrame)
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000113 return 0;
114
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100115 rtp_header_.payloadType = payload_type;
116 rtp_header_.timestamp = timestamp;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000117
kwibergee2bac22015-11-11 10:34:00 -0800118 int ret_val = receiver_->InsertPacket(
119 rtp_header_,
120 rtc::ArrayView<const uint8_t>(payload_data, payload_len_bytes));
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000121 if (ret_val < 0) {
122 assert(false);
123 return -1;
124 }
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100125 rtp_header_.sequenceNumber++;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000126 packet_sent_ = true;
127 last_frame_type_ = frame_type;
128 return 0;
129 }
130
Karl Wiberg377a2312018-09-24 14:52:51 +0200131 const rtc::scoped_refptr<AudioEncoderFactory> encoder_factory_ =
132 CreateBuiltinAudioEncoderFactory();
133 const rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_ =
134 CreateBuiltinAudioDecoderFactory();
henrik.lundin500c04b2016-03-08 02:36:04 -0800135 AudioCodingModule::Config config_;
kwiberg16c5a962016-02-15 02:27:22 -0800136 std::unique_ptr<AcmReceiver> receiver_;
kwiberg16c5a962016-02-15 02:27:22 -0800137 std::unique_ptr<AudioCodingModule> acm_;
Niels Möllerafb5dbb2019-02-15 15:21:47 +0100138 RTPHeader rtp_header_;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000139 uint32_t timestamp_;
140 bool packet_sent_; // Set when SendData is called reset when inserting audio.
141 uint32_t last_packet_send_timestamp_;
142 FrameType last_frame_type_;
143};
144
Peter Boströme2976c82016-01-04 22:44:05 +0100145#if defined(WEBRTC_ANDROID)
Peter Boströme2976c82016-01-04 22:44:05 +0100146#define MAYBE_SampleRate DISABLED_SampleRate
147#else
148#define MAYBE_SampleRate SampleRate
149#endif
150TEST_F(AcmReceiverTestOldApi, MAYBE_SampleRate) {
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100151 const std::map<int, SdpAudioFormat> codecs = {{0, {"ISAC", 16000, 1}},
152 {1, {"ISAC", 32000, 1}}};
153 receiver_->SetCodecs(codecs);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000154
Karl Wiberg377a2312018-09-24 14:52:51 +0200155 constexpr int kOutSampleRateHz = 8000; // Different than codec sample rate.
156 for (size_t i = 0; i < codecs.size(); ++i) {
157 const int payload_type = rtc::checked_cast<int>(i);
158 const int num_10ms_frames =
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100159 InsertOnePacketOfSilence(SetEncoder(payload_type, codecs.at(i)));
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000160 for (int k = 0; k < num_10ms_frames; ++k) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200161 AudioFrame frame;
henrik.lundin834a6ea2016-05-13 03:45:24 -0700162 bool muted;
163 EXPECT_EQ(0, receiver_->GetAudio(kOutSampleRateHz, &frame, &muted));
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000164 }
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100165 EXPECT_EQ(encoder_factory_->QueryAudioEncoder(codecs.at(i))->sample_rate_hz,
Karl Wiberg377a2312018-09-24 14:52:51 +0200166 receiver_->last_output_sample_rate_hz());
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000167 }
168}
169
henrik.lundin7dc68892016-04-06 01:03:02 -0700170class AcmReceiverTestFaxModeOldApi : public AcmReceiverTestOldApi {
171 protected:
172 AcmReceiverTestFaxModeOldApi() {
Henrik Lundin7687ad52018-07-02 10:14:46 +0200173 config_.neteq_config.for_test_no_time_stretching = true;
henrik.lundin7dc68892016-04-06 01:03:02 -0700174 }
175
Karl Wiberg377a2312018-09-24 14:52:51 +0200176 void RunVerifyAudioFrame(const SdpAudioFormat& codec) {
henrik.lundin7dc68892016-04-06 01:03:02 -0700177 // Make sure "fax mode" is enabled. This will avoid delay changes unless the
178 // packet-loss concealment is made. We do this in order to make the
179 // timestamp increments predictable; in normal mode, NetEq may decide to do
180 // accelerate or pre-emptive expand operations after some time, offsetting
181 // the timestamp.
Henrik Lundin7687ad52018-07-02 10:14:46 +0200182 EXPECT_TRUE(config_.neteq_config.for_test_no_time_stretching);
henrik.lundin7dc68892016-04-06 01:03:02 -0700183
Karl Wiberg377a2312018-09-24 14:52:51 +0200184 constexpr int payload_type = 17;
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100185 receiver_->SetCodecs({{payload_type, codec}});
henrik.lundin7dc68892016-04-06 01:03:02 -0700186
Karl Wiberg377a2312018-09-24 14:52:51 +0200187 const AudioCodecInfo info = SetEncoder(payload_type, codec);
188 const int output_sample_rate_hz = info.sample_rate_hz;
189 const size_t output_channels = info.num_channels;
henrik.lundin7dc68892016-04-06 01:03:02 -0700190 const size_t samples_per_ms = rtc::checked_cast<size_t>(
191 rtc::CheckedDivExact(output_sample_rate_hz, 1000));
henrik.lundin7dc68892016-04-06 01:03:02 -0700192 const AudioFrame::VADActivity expected_vad_activity =
193 output_sample_rate_hz > 16000 ? AudioFrame::kVadActive
194 : AudioFrame::kVadPassive;
195
196 // Expect the first output timestamp to be 5*fs/8000 samples before the
197 // first inserted timestamp (because of NetEq's look-ahead). (This value is
198 // defined in Expand::overlap_length_.)
Yves Gerey665174f2018-06-19 15:03:05 +0200199 uint32_t expected_output_ts =
200 last_packet_send_timestamp_ -
henrik.lundin7dc68892016-04-06 01:03:02 -0700201 rtc::CheckedDivExact(5 * output_sample_rate_hz, 8000);
202
203 AudioFrame frame;
henrik.lundin834a6ea2016-05-13 03:45:24 -0700204 bool muted;
205 EXPECT_EQ(0, receiver_->GetAudio(output_sample_rate_hz, &frame, &muted));
henrik.lundin15c51e32016-04-06 08:38:56 -0700206 // Expect timestamp = 0 before first packet is inserted.
207 EXPECT_EQ(0u, frame.timestamp_);
henrik.lundin7dc68892016-04-06 01:03:02 -0700208 for (int i = 0; i < 5; ++i) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200209 const int num_10ms_frames = InsertOnePacketOfSilence(info);
henrik.lundin7dc68892016-04-06 01:03:02 -0700210 for (int k = 0; k < num_10ms_frames; ++k) {
henrik.lundin834a6ea2016-05-13 03:45:24 -0700211 EXPECT_EQ(0,
212 receiver_->GetAudio(output_sample_rate_hz, &frame, &muted));
henrik.lundin7dc68892016-04-06 01:03:02 -0700213 EXPECT_EQ(expected_output_ts, frame.timestamp_);
Mirko Bonadei737e0732017-10-19 09:00:17 +0200214 expected_output_ts += rtc::checked_cast<uint32_t>(10 * samples_per_ms);
henrik.lundin7dc68892016-04-06 01:03:02 -0700215 EXPECT_EQ(10 * samples_per_ms, frame.samples_per_channel_);
216 EXPECT_EQ(output_sample_rate_hz, frame.sample_rate_hz_);
217 EXPECT_EQ(output_channels, frame.num_channels_);
218 EXPECT_EQ(AudioFrame::kNormalSpeech, frame.speech_type_);
219 EXPECT_EQ(expected_vad_activity, frame.vad_activity_);
henrik.lundin834a6ea2016-05-13 03:45:24 -0700220 EXPECT_FALSE(muted);
henrik.lundin7dc68892016-04-06 01:03:02 -0700221 }
222 }
223 }
224};
225
226#if defined(WEBRTC_ANDROID)
227#define MAYBE_VerifyAudioFramePCMU DISABLED_VerifyAudioFramePCMU
228#else
229#define MAYBE_VerifyAudioFramePCMU VerifyAudioFramePCMU
230#endif
231TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFramePCMU) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200232 RunVerifyAudioFrame({"PCMU", 8000, 1});
henrik.lundin7dc68892016-04-06 01:03:02 -0700233}
234
235#if defined(WEBRTC_ANDROID)
236#define MAYBE_VerifyAudioFrameISAC DISABLED_VerifyAudioFrameISAC
237#else
238#define MAYBE_VerifyAudioFrameISAC VerifyAudioFrameISAC
239#endif
240TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFrameISAC) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200241 RunVerifyAudioFrame({"ISAC", 16000, 1});
henrik.lundin7dc68892016-04-06 01:03:02 -0700242}
243
244#if defined(WEBRTC_ANDROID)
245#define MAYBE_VerifyAudioFrameOpus DISABLED_VerifyAudioFrameOpus
246#else
247#define MAYBE_VerifyAudioFrameOpus VerifyAudioFrameOpus
248#endif
249TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFrameOpus) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200250 RunVerifyAudioFrame({"opus", 48000, 2});
henrik.lundin7dc68892016-04-06 01:03:02 -0700251}
252
Peter Boströme2976c82016-01-04 22:44:05 +0100253#if defined(WEBRTC_ANDROID)
254#define MAYBE_PostdecodingVad DISABLED_PostdecodingVad
255#else
256#define MAYBE_PostdecodingVad PostdecodingVad
257#endif
258TEST_F(AcmReceiverTestOldApi, MAYBE_PostdecodingVad) {
henrik.lundin500c04b2016-03-08 02:36:04 -0800259 EXPECT_TRUE(config_.neteq_config.enable_post_decode_vad);
Karl Wiberg377a2312018-09-24 14:52:51 +0200260 constexpr int payload_type = 34;
261 const SdpAudioFormat codec = {"L16", 16000, 1};
262 const AudioCodecInfo info = SetEncoder(payload_type, codec);
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100263 receiver_->SetCodecs({{payload_type, codec}});
Karl Wiberg377a2312018-09-24 14:52:51 +0200264 constexpr int kNumPackets = 5;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000265 AudioFrame frame;
266 for (int n = 0; n < kNumPackets; ++n) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200267 const int num_10ms_frames = InsertOnePacketOfSilence(info);
henrik.lundin834a6ea2016-05-13 03:45:24 -0700268 for (int k = 0; k < num_10ms_frames; ++k) {
269 bool muted;
Karl Wiberg377a2312018-09-24 14:52:51 +0200270 ASSERT_EQ(0, receiver_->GetAudio(info.sample_rate_hz, &frame, &muted));
henrik.lundin834a6ea2016-05-13 03:45:24 -0700271 }
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000272 }
273 EXPECT_EQ(AudioFrame::kVadPassive, frame.vad_activity_);
henrik.lundin500c04b2016-03-08 02:36:04 -0800274}
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000275
henrik.lundin500c04b2016-03-08 02:36:04 -0800276class AcmReceiverTestPostDecodeVadPassiveOldApi : public AcmReceiverTestOldApi {
277 protected:
278 AcmReceiverTestPostDecodeVadPassiveOldApi() {
279 config_.neteq_config.enable_post_decode_vad = false;
280 }
281};
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000282
henrik.lundin500c04b2016-03-08 02:36:04 -0800283#if defined(WEBRTC_ANDROID)
284#define MAYBE_PostdecodingVad DISABLED_PostdecodingVad
285#else
286#define MAYBE_PostdecodingVad PostdecodingVad
287#endif
288TEST_F(AcmReceiverTestPostDecodeVadPassiveOldApi, MAYBE_PostdecodingVad) {
289 EXPECT_FALSE(config_.neteq_config.enable_post_decode_vad);
Karl Wiberg377a2312018-09-24 14:52:51 +0200290 constexpr int payload_type = 34;
291 const SdpAudioFormat codec = {"L16", 16000, 1};
292 const AudioCodecInfo info = SetEncoder(payload_type, codec);
293 encoder_factory_->QueryAudioEncoder(codec).value();
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100294 receiver_->SetCodecs({{payload_type, codec}});
henrik.lundin500c04b2016-03-08 02:36:04 -0800295 const int kNumPackets = 5;
henrik.lundin500c04b2016-03-08 02:36:04 -0800296 AudioFrame frame;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000297 for (int n = 0; n < kNumPackets; ++n) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200298 const int num_10ms_frames = InsertOnePacketOfSilence(info);
henrik.lundin834a6ea2016-05-13 03:45:24 -0700299 for (int k = 0; k < num_10ms_frames; ++k) {
300 bool muted;
Karl Wiberg377a2312018-09-24 14:52:51 +0200301 ASSERT_EQ(0, receiver_->GetAudio(info.sample_rate_hz, &frame, &muted));
henrik.lundin834a6ea2016-05-13 03:45:24 -0700302 }
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000303 }
304 EXPECT_EQ(AudioFrame::kVadUnknown, frame.vad_activity_);
305}
306
Peter Boströme2976c82016-01-04 22:44:05 +0100307#if defined(WEBRTC_ANDROID)
308#define MAYBE_LastAudioCodec DISABLED_LastAudioCodec
kwiberg98ab3a42015-09-30 21:54:21 -0700309#else
Peter Boströme2976c82016-01-04 22:44:05 +0100310#define MAYBE_LastAudioCodec LastAudioCodec
kwiberg98ab3a42015-09-30 21:54:21 -0700311#endif
Peter Boströme2976c82016-01-04 22:44:05 +0100312#if defined(WEBRTC_CODEC_ISAC)
313TEST_F(AcmReceiverTestOldApi, MAYBE_LastAudioCodec) {
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100314 const std::map<int, SdpAudioFormat> codecs = {{0, {"ISAC", 16000, 1}},
315 {1, {"PCMA", 8000, 1}},
316 {2, {"ISAC", 32000, 1}},
317 {3, {"L16", 32000, 1}}};
318 const std::map<int, int> cng_payload_types = {{8000, 100},
319 {16000, 101},
320 {32000, 102}};
321 {
322 std::map<int, SdpAudioFormat> receive_codecs = codecs;
323 for (const auto& cng_type : cng_payload_types) {
324 receive_codecs.emplace(
325 std::make_pair(cng_type.second, SdpAudioFormat("CN", cng_type.first, 1)));
326 }
327 receiver_->SetCodecs(receive_codecs);
Karl Wiberg377a2312018-09-24 14:52:51 +0200328 }
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000329
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000330 // No audio payload is received.
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100331 EXPECT_EQ(absl::nullopt, receiver_->LastDecoder());
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000332
333 // Start with sending DTX.
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000334 packet_sent_ = false;
Karl Wiberg377a2312018-09-24 14:52:51 +0200335 InsertOnePacketOfSilence(
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100336 SetEncoder(0, codecs.at(0), cng_payload_types)); // Enough to test
Karl Wiberg377a2312018-09-24 14:52:51 +0200337 // with one codec.
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000338 ASSERT_TRUE(packet_sent_);
339 EXPECT_EQ(kAudioFrameCN, last_frame_type_);
340
341 // Has received, only, DTX. Last Audio codec is undefined.
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100342 EXPECT_EQ(absl::nullopt, receiver_->LastDecoder());
343 EXPECT_EQ(absl::nullopt, receiver_->last_packet_sample_rate_hz());
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000344
Karl Wiberg377a2312018-09-24 14:52:51 +0200345 for (size_t i = 0; i < codecs.size(); ++i) {
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000346 // Set DTX off to send audio payload.
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000347 packet_sent_ = false;
Karl Wiberg377a2312018-09-24 14:52:51 +0200348 const int payload_type = rtc::checked_cast<int>(i);
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100349 const AudioCodecInfo info_without_cng =
350 SetEncoder(payload_type, codecs.at(i));
Karl Wiberg377a2312018-09-24 14:52:51 +0200351 InsertOnePacketOfSilence(info_without_cng);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000352
353 // Sanity check if Actually an audio payload received, and it should be
354 // of type "speech."
355 ASSERT_TRUE(packet_sent_);
356 ASSERT_EQ(kAudioFrameSpeech, last_frame_type_);
Karl Wiberg377a2312018-09-24 14:52:51 +0200357 EXPECT_EQ(info_without_cng.sample_rate_hz,
358 receiver_->last_packet_sample_rate_hz());
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000359
360 // Set VAD on to send DTX. Then check if the "Last Audio codec" returns
Karl Wiberg377a2312018-09-24 14:52:51 +0200361 // the expected codec. Encode repeatedly until a DTX is sent.
362 const AudioCodecInfo info_with_cng =
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100363 SetEncoder(payload_type, codecs.at(i), cng_payload_types);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000364 while (last_frame_type_ != kAudioFrameCN) {
365 packet_sent_ = false;
Karl Wiberg377a2312018-09-24 14:52:51 +0200366 InsertOnePacketOfSilence(info_with_cng);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000367 ASSERT_TRUE(packet_sent_);
368 }
Karl Wiberg377a2312018-09-24 14:52:51 +0200369 EXPECT_EQ(info_with_cng.sample_rate_hz,
370 receiver_->last_packet_sample_rate_hz());
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100371 EXPECT_EQ(codecs.at(i), receiver_->LastDecoder()->second);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000372 }
373}
Peter Boströme2976c82016-01-04 22:44:05 +0100374#endif
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000375
376} // namespace acm2
377
378} // namespace webrtc