blob: 74a0c7a243f250b5bb7d545f0e1e4537d59b8612 [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,
Minyue Li41759142020-01-23 16:20:52 +0000110 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);
Jerome Humbert3e3c5512020-01-13 11:58:26 +0000292 auto const value = encoder_factory_->QueryAudioEncoder(codec);
293 ASSERT_TRUE(value.has_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}}};
Jonas Olssona4d87372019-07-05 19:08:33 +0200318 const std::map<int, int> cng_payload_types = {
319 {8000, 100}, {16000, 101}, {32000, 102}};
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100320 {
321 std::map<int, SdpAudioFormat> receive_codecs = codecs;
322 for (const auto& cng_type : cng_payload_types) {
Jonas Olssona4d87372019-07-05 19:08:33 +0200323 receive_codecs.emplace(std::make_pair(
324 cng_type.second, SdpAudioFormat("CN", cng_type.first, 1)));
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100325 }
326 receiver_->SetCodecs(receive_codecs);
Karl Wiberg377a2312018-09-24 14:52:51 +0200327 }
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000328
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000329 // No audio payload is received.
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100330 EXPECT_EQ(absl::nullopt, receiver_->LastDecoder());
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000331
332 // Start with sending DTX.
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000333 packet_sent_ = false;
Karl Wiberg377a2312018-09-24 14:52:51 +0200334 InsertOnePacketOfSilence(
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100335 SetEncoder(0, codecs.at(0), cng_payload_types)); // Enough to test
Jonas Olssona4d87372019-07-05 19:08:33 +0200336 // with one codec.
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000337 ASSERT_TRUE(packet_sent_);
Niels Möllerc936cb62019-03-19 14:10:16 +0100338 EXPECT_EQ(AudioFrameType::kAudioFrameCN, last_frame_type_);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000339
340 // Has received, only, DTX. Last Audio codec is undefined.
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100341 EXPECT_EQ(absl::nullopt, receiver_->LastDecoder());
342 EXPECT_EQ(absl::nullopt, receiver_->last_packet_sample_rate_hz());
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000343
Karl Wiberg377a2312018-09-24 14:52:51 +0200344 for (size_t i = 0; i < codecs.size(); ++i) {
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000345 // Set DTX off to send audio payload.
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000346 packet_sent_ = false;
Karl Wiberg377a2312018-09-24 14:52:51 +0200347 const int payload_type = rtc::checked_cast<int>(i);
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100348 const AudioCodecInfo info_without_cng =
349 SetEncoder(payload_type, codecs.at(i));
Karl Wiberg377a2312018-09-24 14:52:51 +0200350 InsertOnePacketOfSilence(info_without_cng);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000351
352 // Sanity check if Actually an audio payload received, and it should be
353 // of type "speech."
354 ASSERT_TRUE(packet_sent_);
Niels Möllerc936cb62019-03-19 14:10:16 +0100355 ASSERT_EQ(AudioFrameType::kAudioFrameSpeech, last_frame_type_);
Karl Wiberg377a2312018-09-24 14:52:51 +0200356 EXPECT_EQ(info_without_cng.sample_rate_hz,
357 receiver_->last_packet_sample_rate_hz());
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000358
359 // Set VAD on to send DTX. Then check if the "Last Audio codec" returns
Karl Wiberg377a2312018-09-24 14:52:51 +0200360 // the expected codec. Encode repeatedly until a DTX is sent.
361 const AudioCodecInfo info_with_cng =
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100362 SetEncoder(payload_type, codecs.at(i), cng_payload_types);
Niels Möllerc936cb62019-03-19 14:10:16 +0100363 while (last_frame_type_ != AudioFrameType::kAudioFrameCN) {
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000364 packet_sent_ = false;
Karl Wiberg377a2312018-09-24 14:52:51 +0200365 InsertOnePacketOfSilence(info_with_cng);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000366 ASSERT_TRUE(packet_sent_);
367 }
Karl Wiberg377a2312018-09-24 14:52:51 +0200368 EXPECT_EQ(info_with_cng.sample_rate_hz,
369 receiver_->last_packet_sample_rate_hz());
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100370 EXPECT_EQ(codecs.at(i), receiver_->LastDecoder()->second);
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000371 }
372}
Peter Boströme2976c82016-01-04 22:44:05 +0100373#endif
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000374
Niels Möller32472442019-09-04 10:14:51 +0200375// Check if the statistics are initialized correctly. Before any call to ACM
376// all fields have to be zero.
377#if defined(WEBRTC_ANDROID)
378#define MAYBE_InitializedToZero DISABLED_InitializedToZero
379#else
380#define MAYBE_InitializedToZero InitializedToZero
381#endif
382TEST_F(AcmReceiverTestOldApi, MAYBE_InitializedToZero) {
383 AudioDecodingCallStats stats;
384 receiver_->GetDecodingCallStatistics(&stats);
385 EXPECT_EQ(0, stats.calls_to_neteq);
386 EXPECT_EQ(0, stats.calls_to_silence_generator);
387 EXPECT_EQ(0, stats.decoded_normal);
388 EXPECT_EQ(0, stats.decoded_cng);
389 EXPECT_EQ(0, stats.decoded_neteq_plc);
390 EXPECT_EQ(0, stats.decoded_plc_cng);
391 EXPECT_EQ(0, stats.decoded_muted_output);
392}
393
394// Insert some packets and pull audio. Check statistics are valid. Then,
395// simulate packet loss and check if PLC and PLC-to-CNG statistics are
396// correctly updated.
397#if defined(WEBRTC_ANDROID)
398#define MAYBE_NetEqCalls DISABLED_NetEqCalls
399#else
400#define MAYBE_NetEqCalls NetEqCalls
401#endif
402TEST_F(AcmReceiverTestOldApi, MAYBE_NetEqCalls) {
403 AudioDecodingCallStats stats;
404 const int kNumNormalCalls = 10;
405 const int kSampleRateHz = 16000;
406 const int kNumSamples10ms = kSampleRateHz / 100;
407 const int kFrameSizeMs = 10; // Multiple of 10.
408 const int kFrameSizeSamples = kFrameSizeMs / 10 * kNumSamples10ms;
409 const int kPayloadSizeBytes = kFrameSizeSamples * sizeof(int16_t);
410 const uint8_t kPayloadType = 111;
411 RTPHeader rtp_header;
412 AudioFrame audio_frame;
413 bool muted;
414
415 receiver_->SetCodecs(
416 {{kPayloadType, SdpAudioFormat("L16", kSampleRateHz, 1)}});
417 rtp_header.sequenceNumber = 0xABCD;
418 rtp_header.timestamp = 0xABCDEF01;
419 rtp_header.payloadType = kPayloadType;
420 rtp_header.markerBit = false;
421 rtp_header.ssrc = 0x1234;
422 rtp_header.numCSRCs = 0;
423 rtp_header.payload_type_frequency = kSampleRateHz;
424
425 for (int num_calls = 0; num_calls < kNumNormalCalls; ++num_calls) {
426 const uint8_t kPayload[kPayloadSizeBytes] = {0};
427 ASSERT_EQ(0, receiver_->InsertPacket(rtp_header, kPayload));
428 ++rtp_header.sequenceNumber;
429 rtp_header.timestamp += kFrameSizeSamples;
430 ASSERT_EQ(0, receiver_->GetAudio(-1, &audio_frame, &muted));
431 EXPECT_FALSE(muted);
432 }
433 receiver_->GetDecodingCallStatistics(&stats);
434 EXPECT_EQ(kNumNormalCalls, stats.calls_to_neteq);
435 EXPECT_EQ(0, stats.calls_to_silence_generator);
436 EXPECT_EQ(kNumNormalCalls, stats.decoded_normal);
437 EXPECT_EQ(0, stats.decoded_cng);
438 EXPECT_EQ(0, stats.decoded_neteq_plc);
439 EXPECT_EQ(0, stats.decoded_plc_cng);
440 EXPECT_EQ(0, stats.decoded_muted_output);
441
442 const int kNumPlc = 3;
443 const int kNumPlcCng = 5;
444
445 // Simulate packet-loss. NetEq first performs PLC then PLC fades to CNG.
446 for (int n = 0; n < kNumPlc + kNumPlcCng; ++n) {
447 ASSERT_EQ(0, receiver_->GetAudio(-1, &audio_frame, &muted));
448 EXPECT_FALSE(muted);
449 }
450 receiver_->GetDecodingCallStatistics(&stats);
451 EXPECT_EQ(kNumNormalCalls + kNumPlc + kNumPlcCng, stats.calls_to_neteq);
452 EXPECT_EQ(0, stats.calls_to_silence_generator);
453 EXPECT_EQ(kNumNormalCalls, stats.decoded_normal);
454 EXPECT_EQ(0, stats.decoded_cng);
455 EXPECT_EQ(kNumPlc, stats.decoded_neteq_plc);
456 EXPECT_EQ(kNumPlcCng, stats.decoded_plc_cng);
457 EXPECT_EQ(0, stats.decoded_muted_output);
458 // TODO(henrik.lundin) Add a test with muted state enabled.
459}
460
andresp@webrtc.org4f6f22f2014-09-23 11:37:57 +0000461} // namespace acm2
462
463} // namespace webrtc