blob: e73acc23382fb98465496ec230832d0a934298b4 [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.
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +000067 AudioCodecInfo info = encoder_factory_->QueryAudioEncoder(format).value();
Karl Wiberg377a2312018-09-24 14:52:51 +020068 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.
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +000072 auto it = cng_payload_types.find(info.sample_rate_hz);
Karl Wiberg377a2312018-09-24 14:52:51 +020073 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));
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +000084 return info;
Karl Wiberg377a2312018-09-24 14:52:51 +020085 }
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,
Minyue Liff0e4db2020-01-23 13:45:50 +0100110 size_t payload_len_bytes,
111 int64_t absolute_capture_timestamp_ms) override {
Niels Möllerc936cb62019-03-19 14:10:16 +0100112 if (frame_type == AudioFrameType::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) {
Artem Titovd3251962021-11-15 16:57:07 +0100122 RTC_DCHECK_NOTREACHED();
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000123 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_;
Niels Möller87e2d782019-03-07 10:18:23 +0100142 AudioFrameType last_frame_type_;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000143};
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) {
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000151 const std::map<int, SdpAudioFormat> codecs = {{0, {"ISAC", 16000, 1}},
152 {1, {"ISAC", 32000, 1}}};
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100153 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)
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000236#define MAYBE_VerifyAudioFrameISAC DISABLED_VerifyAudioFrameISAC
237#else
238#define MAYBE_VerifyAudioFrameISAC VerifyAudioFrameISAC
239#endif
240TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFrameISAC) {
241 RunVerifyAudioFrame({"ISAC", 16000, 1});
242}
243
244#if defined(WEBRTC_ANDROID)
henrik.lundin7dc68892016-04-06 01:03:02 -0700245#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);
Jerome Humbert3e3c5512020-01-13 11:58:26 +0000293 auto const value = encoder_factory_->QueryAudioEncoder(codec);
294 ASSERT_TRUE(value.has_value());
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100295 receiver_->SetCodecs({{payload_type, codec}});
henrik.lundin500c04b2016-03-08 02:36:04 -0800296 const int kNumPackets = 5;
henrik.lundin500c04b2016-03-08 02:36:04 -0800297 AudioFrame frame;
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000298 for (int n = 0; n < kNumPackets; ++n) {
Karl Wiberg377a2312018-09-24 14:52:51 +0200299 const int num_10ms_frames = InsertOnePacketOfSilence(info);
henrik.lundin834a6ea2016-05-13 03:45:24 -0700300 for (int k = 0; k < num_10ms_frames; ++k) {
301 bool muted;
Karl Wiberg377a2312018-09-24 14:52:51 +0200302 ASSERT_EQ(0, receiver_->GetAudio(info.sample_rate_hz, &frame, &muted));
henrik.lundin834a6ea2016-05-13 03:45:24 -0700303 }
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000304 }
305 EXPECT_EQ(AudioFrame::kVadUnknown, frame.vad_activity_);
306}
307
Peter Boströme2976c82016-01-04 22:44:05 +0100308#if defined(WEBRTC_ANDROID)
309#define MAYBE_LastAudioCodec DISABLED_LastAudioCodec
kwiberg98ab3a42015-09-30 21:54:21 -0700310#else
Peter Boströme2976c82016-01-04 22:44:05 +0100311#define MAYBE_LastAudioCodec LastAudioCodec
kwiberg98ab3a42015-09-30 21:54:21 -0700312#endif
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000313#if defined(WEBRTC_CODEC_ISAC)
Peter Boströme2976c82016-01-04 22:44:05 +0100314TEST_F(AcmReceiverTestOldApi, MAYBE_LastAudioCodec) {
Alessio Bazzicafbeb76a2022-11-16 19:13:25 +0000315 const std::map<int, SdpAudioFormat> codecs = {{0, {"ISAC", 16000, 1}},
316 {1, {"PCMA", 8000, 1}},
317 {2, {"ISAC", 32000, 1}},
318 {3, {"L16", 32000, 1}}};
Jonas Olssona4d87372019-07-05 19:08:33 +0200319 const std::map<int, int> cng_payload_types = {
320 {8000, 100}, {16000, 101}, {32000, 102}};
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100321 {
322 std::map<int, SdpAudioFormat> receive_codecs = codecs;
323 for (const auto& cng_type : cng_payload_types) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200324 receive_codecs.emplace(std::make_pair(
325 cng_type.second, SdpAudioFormat("CN", cng_type.first, 1)));
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100326 }
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
Jonas Olssona4d87372019-07-05 19:08:33 +0200337 // with one codec.
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000338 ASSERT_TRUE(packet_sent_);
Niels Möllerc936cb62019-03-19 14:10:16 +0100339 EXPECT_EQ(AudioFrameType::kAudioFrameCN, last_frame_type_);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000340
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_);
Niels Möllerc936cb62019-03-19 14:10:16 +0100356 ASSERT_EQ(AudioFrameType::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);
Niels Möllerc936cb62019-03-19 14:10:16 +0100364 while (last_frame_type_ != AudioFrameType::kAudioFrameCN) {
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000365 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
Niels Möller32472442019-09-04 10:14:51 +0200376// Check if the statistics are initialized correctly. Before any call to ACM
377// all fields have to be zero.
378#if defined(WEBRTC_ANDROID)
379#define MAYBE_InitializedToZero DISABLED_InitializedToZero
380#else
381#define MAYBE_InitializedToZero InitializedToZero
382#endif
383TEST_F(AcmReceiverTestOldApi, MAYBE_InitializedToZero) {
384 AudioDecodingCallStats stats;
385 receiver_->GetDecodingCallStatistics(&stats);
386 EXPECT_EQ(0, stats.calls_to_neteq);
387 EXPECT_EQ(0, stats.calls_to_silence_generator);
388 EXPECT_EQ(0, stats.decoded_normal);
389 EXPECT_EQ(0, stats.decoded_cng);
390 EXPECT_EQ(0, stats.decoded_neteq_plc);
391 EXPECT_EQ(0, stats.decoded_plc_cng);
392 EXPECT_EQ(0, stats.decoded_muted_output);
393}
394
395// Insert some packets and pull audio. Check statistics are valid. Then,
396// simulate packet loss and check if PLC and PLC-to-CNG statistics are
397// correctly updated.
398#if defined(WEBRTC_ANDROID)
399#define MAYBE_NetEqCalls DISABLED_NetEqCalls
400#else
401#define MAYBE_NetEqCalls NetEqCalls
402#endif
403TEST_F(AcmReceiverTestOldApi, MAYBE_NetEqCalls) {
404 AudioDecodingCallStats stats;
405 const int kNumNormalCalls = 10;
406 const int kSampleRateHz = 16000;
407 const int kNumSamples10ms = kSampleRateHz / 100;
408 const int kFrameSizeMs = 10; // Multiple of 10.
409 const int kFrameSizeSamples = kFrameSizeMs / 10 * kNumSamples10ms;
410 const int kPayloadSizeBytes = kFrameSizeSamples * sizeof(int16_t);
411 const uint8_t kPayloadType = 111;
412 RTPHeader rtp_header;
413 AudioFrame audio_frame;
414 bool muted;
415
416 receiver_->SetCodecs(
417 {{kPayloadType, SdpAudioFormat("L16", kSampleRateHz, 1)}});
418 rtp_header.sequenceNumber = 0xABCD;
419 rtp_header.timestamp = 0xABCDEF01;
420 rtp_header.payloadType = kPayloadType;
421 rtp_header.markerBit = false;
422 rtp_header.ssrc = 0x1234;
423 rtp_header.numCSRCs = 0;
424 rtp_header.payload_type_frequency = kSampleRateHz;
425
426 for (int num_calls = 0; num_calls < kNumNormalCalls; ++num_calls) {
427 const uint8_t kPayload[kPayloadSizeBytes] = {0};
428 ASSERT_EQ(0, receiver_->InsertPacket(rtp_header, kPayload));
429 ++rtp_header.sequenceNumber;
430 rtp_header.timestamp += kFrameSizeSamples;
431 ASSERT_EQ(0, receiver_->GetAudio(-1, &audio_frame, &muted));
432 EXPECT_FALSE(muted);
433 }
434 receiver_->GetDecodingCallStatistics(&stats);
435 EXPECT_EQ(kNumNormalCalls, stats.calls_to_neteq);
436 EXPECT_EQ(0, stats.calls_to_silence_generator);
437 EXPECT_EQ(kNumNormalCalls, stats.decoded_normal);
438 EXPECT_EQ(0, stats.decoded_cng);
439 EXPECT_EQ(0, stats.decoded_neteq_plc);
440 EXPECT_EQ(0, stats.decoded_plc_cng);
441 EXPECT_EQ(0, stats.decoded_muted_output);
442
443 const int kNumPlc = 3;
444 const int kNumPlcCng = 5;
445
446 // Simulate packet-loss. NetEq first performs PLC then PLC fades to CNG.
447 for (int n = 0; n < kNumPlc + kNumPlcCng; ++n) {
448 ASSERT_EQ(0, receiver_->GetAudio(-1, &audio_frame, &muted));
449 EXPECT_FALSE(muted);
450 }
451 receiver_->GetDecodingCallStatistics(&stats);
452 EXPECT_EQ(kNumNormalCalls + kNumPlc + kNumPlcCng, stats.calls_to_neteq);
453 EXPECT_EQ(0, stats.calls_to_silence_generator);
454 EXPECT_EQ(kNumNormalCalls, stats.decoded_normal);
455 EXPECT_EQ(0, stats.decoded_cng);
456 EXPECT_EQ(kNumPlc, stats.decoded_neteq_plc);
457 EXPECT_EQ(kNumPlcCng, stats.decoded_plc_cng);
458 EXPECT_EQ(0, stats.decoded_muted_output);
459 // TODO(henrik.lundin) Add a test with muted state enabled.
460}
461
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000462} // namespace acm2
463
464} // namespace webrtc