blob: d2c8d8a132939f5129445273a96ab14a1fa0ac66 [file] [log] [blame]
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00001/*
2 * Copyright (c) 2012 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/test/TestRedFec.h"
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000012
Mirko Bonadei317a1f02019-09-17 17:06:18 +020013#include <memory>
Fredrik Solenberg657b2962018-12-05 10:30:25 +010014#include <utility>
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000015
Fredrik Solenberg657b2962018-12-05 10:30:25 +010016#include "absl/strings/match.h"
Karl Wibergc2c4d042018-10-04 12:38:03 +020017#include "api/audio_codecs/L16/audio_decoder_L16.h"
18#include "api/audio_codecs/L16/audio_encoder_L16.h"
19#include "api/audio_codecs/audio_decoder_factory_template.h"
20#include "api/audio_codecs/audio_encoder_factory_template.h"
21#include "api/audio_codecs/g711/audio_decoder_g711.h"
22#include "api/audio_codecs/g711/audio_encoder_g711.h"
23#include "api/audio_codecs/g722/audio_decoder_g722.h"
24#include "api/audio_codecs/g722/audio_encoder_g722.h"
25#include "api/audio_codecs/isac/audio_decoder_isac_float.h"
26#include "api/audio_codecs/isac/audio_encoder_isac_float.h"
27#include "api/audio_codecs/opus/audio_decoder_opus.h"
28#include "api/audio_codecs/opus/audio_encoder_opus.h"
Karl Wibergc2c4d042018-10-04 12:38:03 +020029#include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
30#include "modules/audio_coding/codecs/red/audio_encoder_copy_red.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/audio_coding/include/audio_coding_module_typedefs.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020032#include "rtc_base/strings/string_builder.h"
Fredrik Solenberg657b2962018-12-05 10:30:25 +010033#include "test/gtest.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "test/testsupport/file_utils.h"
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000035
36namespace webrtc {
37
38TestRedFec::TestRedFec()
Karl Wibergc2c4d042018-10-04 12:38:03 +020039 : encoder_factory_(CreateAudioEncoderFactory<AudioEncoderG711,
40 AudioEncoderG722,
41 AudioEncoderIsacFloat,
42 AudioEncoderL16,
43 AudioEncoderOpus>()),
44 decoder_factory_(CreateAudioDecoderFactory<AudioDecoderG711,
45 AudioDecoderG722,
46 AudioDecoderIsacFloat,
47 AudioDecoderL16,
48 AudioDecoderOpus>()),
49 _acmA(AudioCodingModule::Create(
50 AudioCodingModule::Config(decoder_factory_))),
Karl Wiberg5817d3d2018-04-06 10:06:42 +020051 _acmB(AudioCodingModule::Create(
Karl Wibergc2c4d042018-10-04 12:38:03 +020052 AudioCodingModule::Config(decoder_factory_))),
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000053 _channelA2B(NULL),
Karl Wiberg5817d3d2018-04-06 10:06:42 +020054 _testCntr(0) {}
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000055
56TestRedFec::~TestRedFec() {
57 if (_channelA2B != NULL) {
58 delete _channelA2B;
59 _channelA2B = NULL;
60 }
61}
62
63void TestRedFec::Perform() {
Jonas Olssona4d87372019-07-05 19:08:33 +020064 const std::string file_name =
65 webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000066 _inFileA.Open(file_name, 32000, "rb");
67
68 ASSERT_EQ(0, _acmA->InitializeReceiver());
69 ASSERT_EQ(0, _acmB->InitializeReceiver());
70
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000071 // Create and connect the channel
72 _channelA2B = new Channel;
73 _acmA->RegisterTransportCallback(_channelA2B);
74 _channelA2B->RegisterReceiverACM(_acmB.get());
75
Karl Wibergc2c4d042018-10-04 12:38:03 +020076 RegisterSendCodec(_acmA, {"L16", 8000, 1}, Vad::kVadAggressive, true);
minyue@webrtc.org41d2bef2015-03-23 12:57:45 +000077
78 OpenOutFile(_testCntr);
79 Run();
80 _outFileB.Close();
81
Karl Wibergc2c4d042018-10-04 12:38:03 +020082 // Switch to another 8 kHz codec; RED should remain switched on.
83 RegisterSendCodec(_acmA, {"PCMU", 8000, 1}, Vad::kVadAggressive, true);
minyue@webrtc.org41d2bef2015-03-23 12:57:45 +000084 OpenOutFile(_testCntr);
85 Run();
86 _outFileB.Close();
87
Karl Wibergc2c4d042018-10-04 12:38:03 +020088 // Switch to a 16 kHz codec; RED should be switched off.
89 RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false);
minyue@webrtc.org41d2bef2015-03-23 12:57:45 +000090
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000091 OpenOutFile(_testCntr);
Karl Wibergc2c4d042018-10-04 12:38:03 +020092 RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000093 Run();
Karl Wibergc2c4d042018-10-04 12:38:03 +020094 RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000095 Run();
96 _outFileB.Close();
97
Karl Wibergc2c4d042018-10-04 12:38:03 +020098 RegisterSendCodec(_acmA, {"ISAC", 16000, 1}, Vad::kVadVeryAggressive, false);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +000099 OpenOutFile(_testCntr);
100 Run();
101 _outFileB.Close();
102
Karl Wibergc2c4d042018-10-04 12:38:03 +0200103 // Switch to a 32 kHz codec; RED should be switched off.
104 RegisterSendCodec(_acmA, {"ISAC", 32000, 1}, Vad::kVadVeryAggressive, false);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000105 OpenOutFile(_testCntr);
106 Run();
107 _outFileB.Close();
108
Karl Wibergc2c4d042018-10-04 12:38:03 +0200109 RegisterSendCodec(_acmA, {"ISAC", 32000, 1}, absl::nullopt, false);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000110
111 _channelA2B->SetFECTestWithPacketLoss(true);
minyue@webrtc.org41d2bef2015-03-23 12:57:45 +0000112 // Following tests are under packet losses.
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000113
Karl Wibergc2c4d042018-10-04 12:38:03 +0200114 // Switch to a 16 kHz codec; RED should be switched off.
115 RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false);
116
117 OpenOutFile(_testCntr);
118 Run();
119 _outFileB.Close();
minyue@webrtc.org41d2bef2015-03-23 12:57:45 +0000120
minyue@webrtc.org41d2bef2015-03-23 12:57:45 +0000121 // Switch to a 16 kHz codec, RED should have been switched off.
Karl Wibergc2c4d042018-10-04 12:38:03 +0200122 RegisterSendCodec(_acmA, {"ISAC", 16000, 1}, Vad::kVadVeryAggressive, false);
minyue@webrtc.org41d2bef2015-03-23 12:57:45 +0000123
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000124 OpenOutFile(_testCntr);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000125 Run();
126 _outFileB.Close();
127
minyue@webrtc.org41d2bef2015-03-23 12:57:45 +0000128 // Switch to a 32 kHz codec, RED should have been switched off.
Karl Wibergc2c4d042018-10-04 12:38:03 +0200129 RegisterSendCodec(_acmA, {"ISAC", 32000, 1}, Vad::kVadVeryAggressive, false);
minyue@webrtc.org41d2bef2015-03-23 12:57:45 +0000130
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000131 OpenOutFile(_testCntr);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000132 Run();
133 _outFileB.Close();
134
Karl Wibergc2c4d042018-10-04 12:38:03 +0200135 RegisterSendCodec(_acmA, {"ISAC", 32000, 1}, absl::nullopt, false);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000136
Karl Wibergc2c4d042018-10-04 12:38:03 +0200137 RegisterSendCodec(_acmA, {"opus", 48000, 2}, absl::nullopt, false);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000138
139 // _channelA2B imposes 25% packet loss rate.
140 EXPECT_EQ(0, _acmA->SetPacketLossRate(25));
141
Karl Wibergc2c4d042018-10-04 12:38:03 +0200142 _acmA->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
143 EXPECT_EQ(true, (*enc)->SetFec(true));
144 });
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000145
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000146 OpenOutFile(_testCntr);
147 Run();
148
minyue@webrtc.org41d2bef2015-03-23 12:57:45 +0000149 // Switch to L16 with RED.
Karl Wibergc2c4d042018-10-04 12:38:03 +0200150 RegisterSendCodec(_acmA, {"L16", 8000, 1}, absl::nullopt, true);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000151 Run();
152
153 // Switch to Opus again.
Karl Wibergc2c4d042018-10-04 12:38:03 +0200154 RegisterSendCodec(_acmA, {"opus", 48000, 2}, absl::nullopt, false);
155 _acmA->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
156 EXPECT_EQ(true, (*enc)->SetFec(false));
157 });
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000158 Run();
159
Karl Wibergc2c4d042018-10-04 12:38:03 +0200160 _acmA->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
161 EXPECT_EQ(true, (*enc)->SetFec(true));
162 });
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000163 _outFileB.Close();
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000164}
165
Karl Wibergc2c4d042018-10-04 12:38:03 +0200166void TestRedFec::RegisterSendCodec(
167 const std::unique_ptr<AudioCodingModule>& acm,
168 const SdpAudioFormat& codec_format,
169 absl::optional<Vad::Aggressiveness> vad_mode,
170 bool use_red) {
171 constexpr int payload_type = 17, cn_payload_type = 27, red_payload_type = 37;
172 const auto& other_acm = &acm == &_acmA ? _acmB : _acmA;
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000173
Karl Wibergc2c4d042018-10-04 12:38:03 +0200174 auto encoder = encoder_factory_->MakeAudioEncoder(payload_type, codec_format,
175 absl::nullopt);
176 EXPECT_NE(encoder, nullptr);
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100177 std::map<int, SdpAudioFormat> receive_codecs = {{payload_type, codec_format}};
Niels Möller2edab4c2018-10-22 09:48:08 +0200178 if (!absl::EqualsIgnoreCase(codec_format.name, "opus")) {
Karl Wibergc2c4d042018-10-04 12:38:03 +0200179 if (vad_mode.has_value()) {
Karl Wiberg23659362018-11-01 11:13:44 +0100180 AudioEncoderCngConfig config;
Karl Wibergc2c4d042018-10-04 12:38:03 +0200181 config.speech_encoder = std::move(encoder);
182 config.num_channels = 1;
183 config.payload_type = cn_payload_type;
184 config.vad_mode = vad_mode.value();
Karl Wiberg23659362018-11-01 11:13:44 +0100185 encoder = CreateComfortNoiseEncoder(std::move(config));
Jonas Olssona4d87372019-07-05 19:08:33 +0200186 receive_codecs.emplace(std::make_pair(
187 cn_payload_type, SdpAudioFormat("CN", codec_format.clockrate_hz, 1)));
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000188 }
Karl Wibergc2c4d042018-10-04 12:38:03 +0200189 if (use_red) {
190 AudioEncoderCopyRed::Config config;
191 config.payload_type = red_payload_type;
192 config.speech_encoder = std::move(encoder);
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200193 encoder = std::make_unique<AudioEncoderCopyRed>(std::move(config));
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100194 receive_codecs.emplace(
195 std::make_pair(red_payload_type,
196 SdpAudioFormat("red", codec_format.clockrate_hz, 1)));
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000197 }
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000198 }
Karl Wibergc2c4d042018-10-04 12:38:03 +0200199 acm->SetEncoder(std::move(encoder));
Fredrik Solenberg657b2962018-12-05 10:30:25 +0100200 other_acm->SetReceiveCodecs(receive_codecs);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000201}
202
203void TestRedFec::Run() {
204 AudioFrame audioFrame;
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000205 int32_t outFreqHzB = _outFileB.SamplingFrequency();
Henrik Lundin4d682082015-12-10 16:24:39 +0100206 // Set test length to 500 ms (50 blocks of 10 ms each).
207 _inFileA.SetNum10MsBlocksToRead(50);
208 // Fast-forward 1 second (100 blocks) since the file starts with silence.
209 _inFileA.FastForward(100);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000210
211 while (!_inFileA.EndOfFile()) {
212 EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000213 EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
henrik.lundind4ccb002016-05-17 12:21:55 -0700214 bool muted;
215 EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
216 ASSERT_FALSE(muted);
yujo36b1a5f2017-06-12 12:45:32 -0700217 _outFileB.Write10MsData(audioFrame.data(), audioFrame.samples_per_channel_);
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000218 }
219 _inFileA.Rewind();
220}
221
222void TestRedFec::OpenOutFile(int16_t test_number) {
223 std::string file_name;
Jonas Olsson366a50c2018-09-06 13:41:30 +0200224 rtc::StringBuilder file_stream;
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +0000225 file_stream << webrtc::test::OutputPath();
226 file_stream << "TestRedFec_outFile_";
227 file_stream << test_number << ".pcm";
228 file_name = file_stream.str();
229 _outFileB.Open(file_name, 16000, "wb");
230}
231
232} // namespace webrtc