blob: 17baef60de969119f0b83e69f1e76d613f429528 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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/test/TestVADDTX.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
minyue@webrtc.org05617162015-03-03 12:02:30 +000013#include <string>
kjellander@webrtc.org5490c712011-12-21 13:34:18 +000014
Niels Möller2edab4c2018-10-22 09:48:08 +020015#include "absl/strings/match.h"
Karl Wiberg895ce822018-10-01 17:26:11 +020016#include "api/audio_codecs/audio_decoder_factory_template.h"
17#include "api/audio_codecs/audio_encoder_factory_template.h"
18#include "api/audio_codecs/ilbc/audio_decoder_ilbc.h"
19#include "api/audio_codecs/ilbc/audio_encoder_ilbc.h"
20#include "api/audio_codecs/isac/audio_decoder_isac_float.h"
21#include "api/audio_codecs/isac/audio_encoder_isac_float.h"
22#include "api/audio_codecs/opus/audio_decoder_opus.h"
23#include "api/audio_codecs/opus/audio_encoder_opus.h"
24#include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_coding/test/PCMFile.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020026#include "rtc_base/strings/string_builder.h"
Fredrik Solenberg657b2962018-12-05 10:30:25 +010027#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080028#include "test/testsupport/file_utils.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000029
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000030namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000031
Niels Möller3bdc5e92020-03-10 09:28:33 +010032MonitoringAudioPacketizationCallback::MonitoringAudioPacketizationCallback(
33 AudioPacketizationCallback* next)
34 : next_(next) {
minyue@webrtc.org05617162015-03-03 12:02:30 +000035 ResetStatistics();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000036}
37
Niels Möller3bdc5e92020-03-10 09:28:33 +010038int32_t MonitoringAudioPacketizationCallback::SendData(
39 AudioFrameType frame_type,
40 uint8_t payload_type,
41 uint32_t timestamp,
42 const uint8_t* payload_data,
43 size_t payload_len_bytes,
44 int64_t absolute_capture_timestamp_ms) {
Niels Möllerc936cb62019-03-19 14:10:16 +010045 counter_[static_cast<int>(frame_type)]++;
Niels Möller3bdc5e92020-03-10 09:28:33 +010046 return next_->SendData(frame_type, payload_type, timestamp, payload_data,
47 payload_len_bytes, absolute_capture_timestamp_ms);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000048}
49
Niels Möller3bdc5e92020-03-10 09:28:33 +010050void MonitoringAudioPacketizationCallback::PrintStatistics() {
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000051 printf("\n");
Niels Möllerc936cb62019-03-19 14:10:16 +010052 printf("kEmptyFrame %u\n",
53 counter_[static_cast<int>(AudioFrameType::kEmptyFrame)]);
54 printf("kAudioFrameSpeech %u\n",
55 counter_[static_cast<int>(AudioFrameType::kAudioFrameSpeech)]);
56 printf("kAudioFrameCN %u\n",
57 counter_[static_cast<int>(AudioFrameType::kAudioFrameCN)]);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000058 printf("\n\n");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000059}
60
Niels Möller3bdc5e92020-03-10 09:28:33 +010061void MonitoringAudioPacketizationCallback::ResetStatistics() {
minyue@webrtc.org05617162015-03-03 12:02:30 +000062 memset(counter_, 0, sizeof(counter_));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000063}
64
Niels Möller3bdc5e92020-03-10 09:28:33 +010065void MonitoringAudioPacketizationCallback::GetStatistics(uint32_t* counter) {
minyue@webrtc.org05617162015-03-03 12:02:30 +000066 memcpy(counter, counter_, sizeof(counter_));
67}
68
69TestVadDtx::TestVadDtx()
Karl Wiberg895ce822018-10-01 17:26:11 +020070 : encoder_factory_(CreateAudioEncoderFactory<AudioEncoderIlbc,
71 AudioEncoderIsacFloat,
72 AudioEncoderOpus>()),
73 decoder_factory_(CreateAudioDecoderFactory<AudioDecoderIlbc,
74 AudioDecoderIsacFloat,
75 AudioDecoderOpus>()),
76 acm_send_(AudioCodingModule::Create(
77 AudioCodingModule::Config(decoder_factory_))),
Karl Wiberg5817d3d2018-04-06 10:06:42 +020078 acm_receive_(AudioCodingModule::Create(
Karl Wiberg895ce822018-10-01 17:26:11 +020079 AudioCodingModule::Config(decoder_factory_))),
Niels Möller3bdc5e92020-03-10 09:28:33 +010080 channel_(std::make_unique<Channel>()),
81 packetization_callback_(
82 std::make_unique<MonitoringAudioPacketizationCallback>(
83 channel_.get())) {
84 EXPECT_EQ(
85 0, acm_send_->RegisterTransportCallback(packetization_callback_.get()));
minyue@webrtc.org05617162015-03-03 12:02:30 +000086 channel_->RegisterReceiverACM(acm_receive_.get());
minyue@webrtc.org05617162015-03-03 12:02:30 +000087}
88
Karl Wiberg895ce822018-10-01 17:26:11 +020089bool TestVadDtx::RegisterCodec(const SdpAudioFormat& codec_format,
90 absl::optional<Vad::Aggressiveness> vad_mode) {
91 constexpr int payload_type = 17, cn_payload_type = 117;
92 bool added_comfort_noise = false;
93
94 auto encoder = encoder_factory_->MakeAudioEncoder(payload_type, codec_format,
95 absl::nullopt);
96 if (vad_mode.has_value() &&
Niels Möller2edab4c2018-10-22 09:48:08 +020097 !absl::EqualsIgnoreCase(codec_format.name, "opus")) {
Karl Wiberg23659362018-11-01 11:13:44 +010098 AudioEncoderCngConfig config;
Karl Wiberg895ce822018-10-01 17:26:11 +020099 config.speech_encoder = std::move(encoder);
100 config.num_channels = 1;
101 config.payload_type = cn_payload_type;
102 config.vad_mode = vad_mode.value();
Karl Wiberg23659362018-11-01 11:13:44 +0100103 encoder = CreateComfortNoiseEncoder(std::move(config));
Karl Wiberg895ce822018-10-01 17:26:11 +0200104 added_comfort_noise = true;
105 }
106 channel_->SetIsStereo(encoder->NumChannels() > 1);
107 acm_send_->SetEncoder(std::move(encoder));
108
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100109 std::map<int, SdpAudioFormat> receive_codecs = {{payload_type, codec_format}};
110 acm_receive_->SetReceiveCodecs(receive_codecs);
111
Karl Wiberg895ce822018-10-01 17:26:11 +0200112 return added_comfort_noise;
minyue@webrtc.org05617162015-03-03 12:02:30 +0000113}
114
115// Encoding a file and see if the numbers that various packets occur follow
116// the expectation.
Yves Gerey665174f2018-06-19 15:03:05 +0200117void TestVadDtx::Run(std::string in_filename,
118 int frequency,
119 int channels,
120 std::string out_filename,
121 bool append,
minyue@webrtc.org05617162015-03-03 12:02:30 +0000122 const int* expects) {
Niels Möller3bdc5e92020-03-10 09:28:33 +0100123 packetization_callback_->ResetStatistics();
minyue@webrtc.org05617162015-03-03 12:02:30 +0000124
125 PCMFile in_file;
126 in_file.Open(in_filename, frequency, "rb");
127 in_file.ReadStereo(channels > 1);
Henrik Lundin4d682082015-12-10 16:24:39 +0100128 // Set test length to 1000 ms (100 blocks of 10 ms each).
129 in_file.SetNum10MsBlocksToRead(100);
130 // Fast-forward both files 500 ms (50 blocks). The first second of the file is
131 // silence, but we want to keep half of that to test silence periods.
132 in_file.FastForward(50);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000133
134 PCMFile out_file;
135 if (append) {
136 out_file.Open(out_filename, kOutputFreqHz, "ab");
137 } else {
138 out_file.Open(out_filename, kOutputFreqHz, "wb");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000139 }
minyue@webrtc.org05617162015-03-03 12:02:30 +0000140
141 uint16_t frame_size_samples = in_file.PayloadLength10Ms();
minyue@webrtc.org05617162015-03-03 12:02:30 +0000142 AudioFrame audio_frame;
143 while (!in_file.EndOfFile()) {
144 in_file.Read10MsData(audio_frame);
ossu63fb95a2016-07-06 09:34:22 -0700145 audio_frame.timestamp_ = time_stamp_;
146 time_stamp_ += frame_size_samples;
minyue@webrtc.org05617162015-03-03 12:02:30 +0000147 EXPECT_GE(acm_send_->Add10MsData(audio_frame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700148 bool muted;
149 acm_receive_->PlayoutData10Ms(kOutputFreqHz, &audio_frame, &muted);
150 ASSERT_FALSE(muted);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000151 out_file.Write10MsData(audio_frame);
152 }
153
154 in_file.Close();
155 out_file.Close();
156
157#ifdef PRINT_STAT
Niels Möller3bdc5e92020-03-10 09:28:33 +0100158 packetization_callback_->PrintStatistics();
minyue@webrtc.org05617162015-03-03 12:02:30 +0000159#endif
160
Niels Möllerc936cb62019-03-19 14:10:16 +0100161 uint32_t stats[3];
Niels Möller3bdc5e92020-03-10 09:28:33 +0100162 packetization_callback_->GetStatistics(stats);
163 packetization_callback_->ResetStatistics();
minyue@webrtc.org05617162015-03-03 12:02:30 +0000164
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000165 for (const auto& st : stats) {
166 int i = &st - stats; // Calculate the current position in stats.
minyue@webrtc.org05617162015-03-03 12:02:30 +0000167 switch (expects[i]) {
168 case 0: {
Minyue Lic9408702020-11-03 23:22:26 +0100169 EXPECT_EQ(0u, st) << "stats[" << i << "] error.";
minyue@webrtc.org05617162015-03-03 12:02:30 +0000170 break;
171 }
172 case 1: {
Minyue Lic9408702020-11-03 23:22:26 +0100173 EXPECT_GT(st, 0u) << "stats[" << i << "] error.";
minyue@webrtc.org05617162015-03-03 12:02:30 +0000174 break;
175 }
176 }
177 }
178}
179
180// Following is the implementation of TestWebRtcVadDtx.
Karl Wiberg895ce822018-10-01 17:26:11 +0200181TestWebRtcVadDtx::TestWebRtcVadDtx() : output_file_num_(0) {}
minyue@webrtc.org05617162015-03-03 12:02:30 +0000182
183void TestWebRtcVadDtx::Perform() {
Karl Wiberg895ce822018-10-01 17:26:11 +0200184 RunTestCases({"ISAC", 16000, 1});
185 RunTestCases({"ISAC", 32000, 1});
186 RunTestCases({"ILBC", 8000, 1});
187 RunTestCases({"opus", 48000, 2});
minyue@webrtc.org05617162015-03-03 12:02:30 +0000188}
189
190// Test various configurations on VAD/DTX.
Karl Wiberg895ce822018-10-01 17:26:11 +0200191void TestWebRtcVadDtx::RunTestCases(const SdpAudioFormat& codec_format) {
192 Test(/*new_outfile=*/true,
Minyue Lic9408702020-11-03 23:22:26 +0100193 /*expect_dtx_enabled=*/RegisterCodec(codec_format, absl::nullopt));
minyue@webrtc.org05617162015-03-03 12:02:30 +0000194
Karl Wiberg895ce822018-10-01 17:26:11 +0200195 Test(/*new_outfile=*/false,
Minyue Lic9408702020-11-03 23:22:26 +0100196 /*expect_dtx_enabled=*/RegisterCodec(codec_format, Vad::kVadAggressive));
minyue@webrtc.org05617162015-03-03 12:02:30 +0000197
Karl Wiberg895ce822018-10-01 17:26:11 +0200198 Test(/*new_outfile=*/false,
Minyue Lic9408702020-11-03 23:22:26 +0100199 /*expect_dtx_enabled=*/RegisterCodec(codec_format, Vad::kVadLowBitrate));
minyue@webrtc.org05617162015-03-03 12:02:30 +0000200
Minyue Lic9408702020-11-03 23:22:26 +0100201 Test(/*new_outfile=*/false, /*expect_dtx_enabled=*/RegisterCodec(
202 codec_format, Vad::kVadVeryAggressive));
minyue@webrtc.org05617162015-03-03 12:02:30 +0000203
Karl Wiberg895ce822018-10-01 17:26:11 +0200204 Test(/*new_outfile=*/false,
Minyue Lic9408702020-11-03 23:22:26 +0100205 /*expect_dtx_enabled=*/RegisterCodec(codec_format, Vad::kVadNormal));
minyue@webrtc.org05617162015-03-03 12:02:30 +0000206}
207
208// Set the expectation and run the test.
Minyue Lic9408702020-11-03 23:22:26 +0100209void TestWebRtcVadDtx::Test(bool new_outfile, bool expect_dtx_enabled) {
210 int expects[] = {-1, 1, expect_dtx_enabled, 0, 0};
minyue@webrtc.org05617162015-03-03 12:02:30 +0000211 if (new_outfile) {
212 output_file_num_++;
213 }
Jonas Olsson366a50c2018-09-06 13:41:30 +0200214 rtc::StringBuilder out_filename;
Yves Gerey665174f2018-06-19 15:03:05 +0200215 out_filename << webrtc::test::OutputPath() << "testWebRtcVadDtx_outFile_"
216 << output_file_num_ << ".pcm";
217 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
218 out_filename.str(), !new_outfile, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000219}
220
minyue@webrtc.org05617162015-03-03 12:02:30 +0000221// Following is the implementation of TestOpusDtx.
222void TestOpusDtx::Perform() {
Minyue Lic9408702020-11-03 23:22:26 +0100223 int expects[] = {0, 1, 0, 0, 0};
minyue@webrtc.org05617162015-03-03 12:02:30 +0000224
225 // Register Opus as send codec
Yves Gerey665174f2018-06-19 15:03:05 +0200226 std::string out_filename =
227 webrtc::test::OutputPath() + "testOpusDtx_outFile_mono.pcm";
Karl Wiberg895ce822018-10-01 17:26:11 +0200228 RegisterCodec({"opus", 48000, 2}, absl::nullopt);
Niels Möllerb90d38a2019-08-07 12:40:40 +0200229 acm_send_->ModifyEncoder([](std::unique_ptr<AudioEncoder>* encoder_ptr) {
230 (*encoder_ptr)->SetDtx(false);
231 });
minyue@webrtc.org05617162015-03-03 12:02:30 +0000232
Yves Gerey665174f2018-06-19 15:03:05 +0200233 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
234 out_filename, false, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000235
Niels Möllerb90d38a2019-08-07 12:40:40 +0200236 acm_send_->ModifyEncoder([](std::unique_ptr<AudioEncoder>* encoder_ptr) {
237 (*encoder_ptr)->SetDtx(true);
238 });
Niels Möller8f7ce222019-03-21 15:43:58 +0100239 expects[static_cast<int>(AudioFrameType::kEmptyFrame)] = 1;
240 expects[static_cast<int>(AudioFrameType::kAudioFrameCN)] = 1;
Yves Gerey665174f2018-06-19 15:03:05 +0200241 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
242 out_filename, true, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000243
244 // Register stereo Opus as send codec
245 out_filename = webrtc::test::OutputPath() + "testOpusDtx_outFile_stereo.pcm";
Karl Wiberg895ce822018-10-01 17:26:11 +0200246 RegisterCodec({"opus", 48000, 2, {{"stereo", "1"}}}, absl::nullopt);
Niels Möllerb90d38a2019-08-07 12:40:40 +0200247 acm_send_->ModifyEncoder([](std::unique_ptr<AudioEncoder>* encoder_ptr) {
248 (*encoder_ptr)->SetDtx(false);
249 });
Niels Möller8f7ce222019-03-21 15:43:58 +0100250 expects[static_cast<int>(AudioFrameType::kEmptyFrame)] = 0;
251 expects[static_cast<int>(AudioFrameType::kAudioFrameCN)] = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200252 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), 32000,
253 2, out_filename, false, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000254
Niels Möllerb90d38a2019-08-07 12:40:40 +0200255 acm_send_->ModifyEncoder([](std::unique_ptr<AudioEncoder>* encoder_ptr) {
256 (*encoder_ptr)->SetDtx(true);
Philipp Hanckefc4668d2020-07-16 17:22:03 +0200257 // The default bitrate will not generate frames recognized as CN on desktop
258 // since the frames will be encoded as CELT. Set a low target bitrate to get
259 // consistent behaviour across platforms.
260 (*encoder_ptr)->OnReceivedTargetAudioBitrate(24000);
Niels Möllerb90d38a2019-08-07 12:40:40 +0200261 });
minyue@webrtc.org05617162015-03-03 12:02:30 +0000262
Niels Möller8f7ce222019-03-21 15:43:58 +0100263 expects[static_cast<int>(AudioFrameType::kEmptyFrame)] = 1;
264 expects[static_cast<int>(AudioFrameType::kAudioFrameCN)] = 1;
Yves Gerey665174f2018-06-19 15:03:05 +0200265 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), 32000,
266 2, out_filename, true, expects);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000267}
268
269} // namespace webrtc