blob: 7c04b228c6f87dfb42bf9361089bf84cb19c8634 [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
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000032ActivityMonitor::ActivityMonitor() {
minyue@webrtc.org05617162015-03-03 12:02:30 +000033 ResetStatistics();
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000034}
35
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +000036int32_t ActivityMonitor::InFrameType(FrameType frame_type) {
minyue@webrtc.org05617162015-03-03 12:02:30 +000037 counter_[frame_type]++;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000038 return 0;
39}
40
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000041void ActivityMonitor::PrintStatistics() {
42 printf("\n");
pbos22993e12015-10-19 02:39:06 -070043 printf("kEmptyFrame %u\n", counter_[kEmptyFrame]);
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +000044 printf("kAudioFrameSpeech %u\n", counter_[kAudioFrameSpeech]);
45 printf("kAudioFrameCN %u\n", counter_[kAudioFrameCN]);
46 printf("kVideoFrameKey %u\n", counter_[kVideoFrameKey]);
47 printf("kVideoFrameDelta %u\n", counter_[kVideoFrameDelta]);
tina.legrand@webrtc.orgee92b662013-08-27 07:33:51 +000048 printf("\n\n");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000049}
50
51void ActivityMonitor::ResetStatistics() {
minyue@webrtc.org05617162015-03-03 12:02:30 +000052 memset(counter_, 0, sizeof(counter_));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000053}
54
minyue@webrtc.org05617162015-03-03 12:02:30 +000055void ActivityMonitor::GetStatistics(uint32_t* counter) {
56 memcpy(counter, counter_, sizeof(counter_));
57}
58
59TestVadDtx::TestVadDtx()
Karl Wiberg895ce822018-10-01 17:26:11 +020060 : encoder_factory_(CreateAudioEncoderFactory<AudioEncoderIlbc,
61 AudioEncoderIsacFloat,
62 AudioEncoderOpus>()),
63 decoder_factory_(CreateAudioDecoderFactory<AudioDecoderIlbc,
64 AudioDecoderIsacFloat,
65 AudioDecoderOpus>()),
66 acm_send_(AudioCodingModule::Create(
67 AudioCodingModule::Config(decoder_factory_))),
Karl Wiberg5817d3d2018-04-06 10:06:42 +020068 acm_receive_(AudioCodingModule::Create(
Karl Wiberg895ce822018-10-01 17:26:11 +020069 AudioCodingModule::Config(decoder_factory_))),
minyue@webrtc.org05617162015-03-03 12:02:30 +000070 channel_(new Channel),
71 monitor_(new ActivityMonitor) {
72 EXPECT_EQ(0, acm_send_->RegisterTransportCallback(channel_.get()));
73 channel_->RegisterReceiverACM(acm_receive_.get());
74 EXPECT_EQ(0, acm_send_->RegisterVADCallback(monitor_.get()));
minyue@webrtc.org05617162015-03-03 12:02:30 +000075}
76
Karl Wiberg895ce822018-10-01 17:26:11 +020077bool TestVadDtx::RegisterCodec(const SdpAudioFormat& codec_format,
78 absl::optional<Vad::Aggressiveness> vad_mode) {
79 constexpr int payload_type = 17, cn_payload_type = 117;
80 bool added_comfort_noise = false;
81
82 auto encoder = encoder_factory_->MakeAudioEncoder(payload_type, codec_format,
83 absl::nullopt);
84 if (vad_mode.has_value() &&
Niels Möller2edab4c2018-10-22 09:48:08 +020085 !absl::EqualsIgnoreCase(codec_format.name, "opus")) {
Karl Wiberg23659362018-11-01 11:13:44 +010086 AudioEncoderCngConfig config;
Karl Wiberg895ce822018-10-01 17:26:11 +020087 config.speech_encoder = std::move(encoder);
88 config.num_channels = 1;
89 config.payload_type = cn_payload_type;
90 config.vad_mode = vad_mode.value();
Karl Wiberg23659362018-11-01 11:13:44 +010091 encoder = CreateComfortNoiseEncoder(std::move(config));
Karl Wiberg895ce822018-10-01 17:26:11 +020092 added_comfort_noise = true;
93 }
94 channel_->SetIsStereo(encoder->NumChannels() > 1);
95 acm_send_->SetEncoder(std::move(encoder));
96
Fredrik Solenberg657b2962018-12-05 10:30:25 +010097 std::map<int, SdpAudioFormat> receive_codecs = {{payload_type, codec_format}};
98 acm_receive_->SetReceiveCodecs(receive_codecs);
99
Karl Wiberg895ce822018-10-01 17:26:11 +0200100 return added_comfort_noise;
minyue@webrtc.org05617162015-03-03 12:02:30 +0000101}
102
103// Encoding a file and see if the numbers that various packets occur follow
104// the expectation.
Yves Gerey665174f2018-06-19 15:03:05 +0200105void TestVadDtx::Run(std::string in_filename,
106 int frequency,
107 int channels,
108 std::string out_filename,
109 bool append,
minyue@webrtc.org05617162015-03-03 12:02:30 +0000110 const int* expects) {
111 monitor_->ResetStatistics();
112
113 PCMFile in_file;
114 in_file.Open(in_filename, frequency, "rb");
115 in_file.ReadStereo(channels > 1);
Henrik Lundin4d682082015-12-10 16:24:39 +0100116 // Set test length to 1000 ms (100 blocks of 10 ms each).
117 in_file.SetNum10MsBlocksToRead(100);
118 // Fast-forward both files 500 ms (50 blocks). The first second of the file is
119 // silence, but we want to keep half of that to test silence periods.
120 in_file.FastForward(50);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000121
122 PCMFile out_file;
123 if (append) {
124 out_file.Open(out_filename, kOutputFreqHz, "ab");
125 } else {
126 out_file.Open(out_filename, kOutputFreqHz, "wb");
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000127 }
minyue@webrtc.org05617162015-03-03 12:02:30 +0000128
129 uint16_t frame_size_samples = in_file.PayloadLength10Ms();
minyue@webrtc.org05617162015-03-03 12:02:30 +0000130 AudioFrame audio_frame;
131 while (!in_file.EndOfFile()) {
132 in_file.Read10MsData(audio_frame);
ossu63fb95a2016-07-06 09:34:22 -0700133 audio_frame.timestamp_ = time_stamp_;
134 time_stamp_ += frame_size_samples;
minyue@webrtc.org05617162015-03-03 12:02:30 +0000135 EXPECT_GE(acm_send_->Add10MsData(audio_frame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700136 bool muted;
137 acm_receive_->PlayoutData10Ms(kOutputFreqHz, &audio_frame, &muted);
138 ASSERT_FALSE(muted);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000139 out_file.Write10MsData(audio_frame);
140 }
141
142 in_file.Close();
143 out_file.Close();
144
145#ifdef PRINT_STAT
146 monitor_->PrintStatistics();
147#endif
148
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000149 uint32_t stats[5];
minyue@webrtc.org05617162015-03-03 12:02:30 +0000150 monitor_->GetStatistics(stats);
151 monitor_->ResetStatistics();
152
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000153 for (const auto& st : stats) {
154 int i = &st - stats; // Calculate the current position in stats.
minyue@webrtc.org05617162015-03-03 12:02:30 +0000155 switch (expects[i]) {
156 case 0: {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000157 EXPECT_EQ(0u, st) << "stats[" << i << "] error.";
minyue@webrtc.org05617162015-03-03 12:02:30 +0000158 break;
159 }
160 case 1: {
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000161 EXPECT_GT(st, 0u) << "stats[" << i << "] error.";
minyue@webrtc.org05617162015-03-03 12:02:30 +0000162 break;
163 }
164 }
165 }
166}
167
168// Following is the implementation of TestWebRtcVadDtx.
Karl Wiberg895ce822018-10-01 17:26:11 +0200169TestWebRtcVadDtx::TestWebRtcVadDtx() : output_file_num_(0) {}
minyue@webrtc.org05617162015-03-03 12:02:30 +0000170
171void TestWebRtcVadDtx::Perform() {
Karl Wiberg895ce822018-10-01 17:26:11 +0200172 RunTestCases({"ISAC", 16000, 1});
173 RunTestCases({"ISAC", 32000, 1});
174 RunTestCases({"ILBC", 8000, 1});
175 RunTestCases({"opus", 48000, 2});
minyue@webrtc.org05617162015-03-03 12:02:30 +0000176}
177
178// Test various configurations on VAD/DTX.
Karl Wiberg895ce822018-10-01 17:26:11 +0200179void TestWebRtcVadDtx::RunTestCases(const SdpAudioFormat& codec_format) {
180 Test(/*new_outfile=*/true,
181 /*expect_dtx_enabled=*/RegisterCodec(codec_format, absl::nullopt));
minyue@webrtc.org05617162015-03-03 12:02:30 +0000182
Karl Wiberg895ce822018-10-01 17:26:11 +0200183 Test(/*new_outfile=*/false,
184 /*expect_dtx_enabled=*/RegisterCodec(codec_format, Vad::kVadAggressive));
minyue@webrtc.org05617162015-03-03 12:02:30 +0000185
Karl Wiberg895ce822018-10-01 17:26:11 +0200186 Test(/*new_outfile=*/false,
187 /*expect_dtx_enabled=*/RegisterCodec(codec_format, Vad::kVadLowBitrate));
minyue@webrtc.org05617162015-03-03 12:02:30 +0000188
Karl Wiberg895ce822018-10-01 17:26:11 +0200189 Test(/*new_outfile=*/false, /*expect_dtx_enabled=*/RegisterCodec(
190 codec_format, Vad::kVadVeryAggressive));
minyue@webrtc.org05617162015-03-03 12:02:30 +0000191
Karl Wiberg895ce822018-10-01 17:26:11 +0200192 Test(/*new_outfile=*/false,
193 /*expect_dtx_enabled=*/RegisterCodec(codec_format, Vad::kVadNormal));
minyue@webrtc.org05617162015-03-03 12:02:30 +0000194}
195
196// Set the expectation and run the test.
Karl Wiberg895ce822018-10-01 17:26:11 +0200197void TestWebRtcVadDtx::Test(bool new_outfile, bool expect_dtx_enabled) {
198 int expects[] = {-1, 1, expect_dtx_enabled, 0, 0};
minyue@webrtc.org05617162015-03-03 12:02:30 +0000199 if (new_outfile) {
200 output_file_num_++;
201 }
Jonas Olsson366a50c2018-09-06 13:41:30 +0200202 rtc::StringBuilder out_filename;
Yves Gerey665174f2018-06-19 15:03:05 +0200203 out_filename << webrtc::test::OutputPath() << "testWebRtcVadDtx_outFile_"
204 << output_file_num_ << ".pcm";
205 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
206 out_filename.str(), !new_outfile, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000207}
208
minyue@webrtc.org05617162015-03-03 12:02:30 +0000209// Following is the implementation of TestOpusDtx.
210void TestOpusDtx::Perform() {
kwiberg44307632015-12-16 06:24:05 -0800211 // If we set other codec than Opus, DTX cannot be switched on.
Karl Wiberg895ce822018-10-01 17:26:11 +0200212 RegisterCodec({"ISAC", 16000, 1}, absl::nullopt);
Minyue Li092041c2015-05-11 12:19:35 +0200213 EXPECT_EQ(-1, acm_send_->EnableOpusDtx());
kwiberg44307632015-12-16 06:24:05 -0800214 EXPECT_EQ(0, acm_send_->DisableOpusDtx());
minyue@webrtc.orge16bfde2015-03-12 15:28:41 +0000215
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000216 int expects[] = {0, 1, 0, 0, 0};
minyue@webrtc.org05617162015-03-03 12:02:30 +0000217
218 // Register Opus as send codec
Yves Gerey665174f2018-06-19 15:03:05 +0200219 std::string out_filename =
220 webrtc::test::OutputPath() + "testOpusDtx_outFile_mono.pcm";
Karl Wiberg895ce822018-10-01 17:26:11 +0200221 RegisterCodec({"opus", 48000, 2}, absl::nullopt);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000222 EXPECT_EQ(0, acm_send_->DisableOpusDtx());
223
Yves Gerey665174f2018-06-19 15:03:05 +0200224 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
225 out_filename, false, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000226
Minyue Li092041c2015-05-11 12:19:35 +0200227 EXPECT_EQ(0, acm_send_->EnableOpusDtx());
pbos22993e12015-10-19 02:39:06 -0700228 expects[kEmptyFrame] = 1;
Gustaf Ullberg36de62e2017-11-20 14:55:41 +0100229 expects[kAudioFrameCN] = 1;
Yves Gerey665174f2018-06-19 15:03:05 +0200230 Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
231 out_filename, true, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000232
233 // Register stereo Opus as send codec
234 out_filename = webrtc::test::OutputPath() + "testOpusDtx_outFile_stereo.pcm";
Karl Wiberg895ce822018-10-01 17:26:11 +0200235 RegisterCodec({"opus", 48000, 2, {{"stereo", "1"}}}, absl::nullopt);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000236 EXPECT_EQ(0, acm_send_->DisableOpusDtx());
pbos22993e12015-10-19 02:39:06 -0700237 expects[kEmptyFrame] = 0;
Gustaf Ullberg36de62e2017-11-20 14:55:41 +0100238 expects[kAudioFrameCN] = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200239 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), 32000,
240 2, out_filename, false, expects);
minyue@webrtc.org05617162015-03-03 12:02:30 +0000241
Minyue Li092041c2015-05-11 12:19:35 +0200242 EXPECT_EQ(0, acm_send_->EnableOpusDtx());
minyue@webrtc.org05617162015-03-03 12:02:30 +0000243
pbos22993e12015-10-19 02:39:06 -0700244 expects[kEmptyFrame] = 1;
Gustaf Ullberg36de62e2017-11-20 14:55:41 +0100245 expects[kAudioFrameCN] = 1;
Yves Gerey665174f2018-06-19 15:03:05 +0200246 Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), 32000,
247 2, out_filename, true, expects);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000248}
249
250} // namespace webrtc