minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/test/TestRedFec.h" |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame^] | 13 | #include <memory> |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 14 | #include <utility> |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 15 | |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 16 | #include "absl/strings/match.h" |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 17 | #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 Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 29 | #include "modules/audio_coding/codecs/cng/audio_encoder_cng.h" |
| 30 | #include "modules/audio_coding/codecs/red/audio_encoder_copy_red.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "modules/audio_coding/include/audio_coding_module_typedefs.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 32 | #include "rtc_base/strings/string_builder.h" |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 33 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 34 | #include "test/testsupport/file_utils.h" |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 35 | |
| 36 | namespace webrtc { |
| 37 | |
| 38 | TestRedFec::TestRedFec() |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 39 | : 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 Wiberg | 5817d3d | 2018-04-06 10:06:42 +0200 | [diff] [blame] | 51 | _acmB(AudioCodingModule::Create( |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 52 | AudioCodingModule::Config(decoder_factory_))), |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 53 | _channelA2B(NULL), |
Karl Wiberg | 5817d3d | 2018-04-06 10:06:42 +0200 | [diff] [blame] | 54 | _testCntr(0) {} |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 55 | |
| 56 | TestRedFec::~TestRedFec() { |
| 57 | if (_channelA2B != NULL) { |
| 58 | delete _channelA2B; |
| 59 | _channelA2B = NULL; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void TestRedFec::Perform() { |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 64 | const std::string file_name = |
| 65 | webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 66 | _inFileA.Open(file_name, 32000, "rb"); |
| 67 | |
| 68 | ASSERT_EQ(0, _acmA->InitializeReceiver()); |
| 69 | ASSERT_EQ(0, _acmB->InitializeReceiver()); |
| 70 | |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 71 | // Create and connect the channel |
| 72 | _channelA2B = new Channel; |
| 73 | _acmA->RegisterTransportCallback(_channelA2B); |
| 74 | _channelA2B->RegisterReceiverACM(_acmB.get()); |
| 75 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 76 | RegisterSendCodec(_acmA, {"L16", 8000, 1}, Vad::kVadAggressive, true); |
minyue@webrtc.org | 41d2bef | 2015-03-23 12:57:45 +0000 | [diff] [blame] | 77 | |
| 78 | OpenOutFile(_testCntr); |
| 79 | Run(); |
| 80 | _outFileB.Close(); |
| 81 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 82 | // Switch to another 8 kHz codec; RED should remain switched on. |
| 83 | RegisterSendCodec(_acmA, {"PCMU", 8000, 1}, Vad::kVadAggressive, true); |
minyue@webrtc.org | 41d2bef | 2015-03-23 12:57:45 +0000 | [diff] [blame] | 84 | OpenOutFile(_testCntr); |
| 85 | Run(); |
| 86 | _outFileB.Close(); |
| 87 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 88 | // Switch to a 16 kHz codec; RED should be switched off. |
| 89 | RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false); |
minyue@webrtc.org | 41d2bef | 2015-03-23 12:57:45 +0000 | [diff] [blame] | 90 | |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 91 | OpenOutFile(_testCntr); |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 92 | RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 93 | Run(); |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 94 | RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 95 | Run(); |
| 96 | _outFileB.Close(); |
| 97 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 98 | RegisterSendCodec(_acmA, {"ISAC", 16000, 1}, Vad::kVadVeryAggressive, false); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 99 | OpenOutFile(_testCntr); |
| 100 | Run(); |
| 101 | _outFileB.Close(); |
| 102 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 103 | // Switch to a 32 kHz codec; RED should be switched off. |
| 104 | RegisterSendCodec(_acmA, {"ISAC", 32000, 1}, Vad::kVadVeryAggressive, false); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 105 | OpenOutFile(_testCntr); |
| 106 | Run(); |
| 107 | _outFileB.Close(); |
| 108 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 109 | RegisterSendCodec(_acmA, {"ISAC", 32000, 1}, absl::nullopt, false); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 110 | |
| 111 | _channelA2B->SetFECTestWithPacketLoss(true); |
minyue@webrtc.org | 41d2bef | 2015-03-23 12:57:45 +0000 | [diff] [blame] | 112 | // Following tests are under packet losses. |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 113 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 114 | // 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.org | 41d2bef | 2015-03-23 12:57:45 +0000 | [diff] [blame] | 120 | |
minyue@webrtc.org | 41d2bef | 2015-03-23 12:57:45 +0000 | [diff] [blame] | 121 | // Switch to a 16 kHz codec, RED should have been switched off. |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 122 | RegisterSendCodec(_acmA, {"ISAC", 16000, 1}, Vad::kVadVeryAggressive, false); |
minyue@webrtc.org | 41d2bef | 2015-03-23 12:57:45 +0000 | [diff] [blame] | 123 | |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 124 | OpenOutFile(_testCntr); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 125 | Run(); |
| 126 | _outFileB.Close(); |
| 127 | |
minyue@webrtc.org | 41d2bef | 2015-03-23 12:57:45 +0000 | [diff] [blame] | 128 | // Switch to a 32 kHz codec, RED should have been switched off. |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 129 | RegisterSendCodec(_acmA, {"ISAC", 32000, 1}, Vad::kVadVeryAggressive, false); |
minyue@webrtc.org | 41d2bef | 2015-03-23 12:57:45 +0000 | [diff] [blame] | 130 | |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 131 | OpenOutFile(_testCntr); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 132 | Run(); |
| 133 | _outFileB.Close(); |
| 134 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 135 | RegisterSendCodec(_acmA, {"ISAC", 32000, 1}, absl::nullopt, false); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 136 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 137 | RegisterSendCodec(_acmA, {"opus", 48000, 2}, absl::nullopt, false); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 138 | |
| 139 | // _channelA2B imposes 25% packet loss rate. |
| 140 | EXPECT_EQ(0, _acmA->SetPacketLossRate(25)); |
| 141 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 142 | _acmA->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) { |
| 143 | EXPECT_EQ(true, (*enc)->SetFec(true)); |
| 144 | }); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 145 | |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 146 | OpenOutFile(_testCntr); |
| 147 | Run(); |
| 148 | |
minyue@webrtc.org | 41d2bef | 2015-03-23 12:57:45 +0000 | [diff] [blame] | 149 | // Switch to L16 with RED. |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 150 | RegisterSendCodec(_acmA, {"L16", 8000, 1}, absl::nullopt, true); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 151 | Run(); |
| 152 | |
| 153 | // Switch to Opus again. |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 154 | 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.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 158 | Run(); |
| 159 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 160 | _acmA->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) { |
| 161 | EXPECT_EQ(true, (*enc)->SetFec(true)); |
| 162 | }); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 163 | _outFileB.Close(); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 166 | void 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.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 173 | |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 174 | auto encoder = encoder_factory_->MakeAudioEncoder(payload_type, codec_format, |
| 175 | absl::nullopt); |
| 176 | EXPECT_NE(encoder, nullptr); |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 177 | std::map<int, SdpAudioFormat> receive_codecs = {{payload_type, codec_format}}; |
Niels Möller | 2edab4c | 2018-10-22 09:48:08 +0200 | [diff] [blame] | 178 | if (!absl::EqualsIgnoreCase(codec_format.name, "opus")) { |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 179 | if (vad_mode.has_value()) { |
Karl Wiberg | 2365936 | 2018-11-01 11:13:44 +0100 | [diff] [blame] | 180 | AudioEncoderCngConfig config; |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 181 | 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 Wiberg | 2365936 | 2018-11-01 11:13:44 +0100 | [diff] [blame] | 185 | encoder = CreateComfortNoiseEncoder(std::move(config)); |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 186 | receive_codecs.emplace(std::make_pair( |
| 187 | cn_payload_type, SdpAudioFormat("CN", codec_format.clockrate_hz, 1))); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 188 | } |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 189 | if (use_red) { |
| 190 | AudioEncoderCopyRed::Config config; |
| 191 | config.payload_type = red_payload_type; |
| 192 | config.speech_encoder = std::move(encoder); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame^] | 193 | encoder = std::make_unique<AudioEncoderCopyRed>(std::move(config)); |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 194 | receive_codecs.emplace( |
| 195 | std::make_pair(red_payload_type, |
| 196 | SdpAudioFormat("red", codec_format.clockrate_hz, 1))); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 197 | } |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 198 | } |
Karl Wiberg | c2c4d04 | 2018-10-04 12:38:03 +0200 | [diff] [blame] | 199 | acm->SetEncoder(std::move(encoder)); |
Fredrik Solenberg | 657b296 | 2018-12-05 10:30:25 +0100 | [diff] [blame] | 200 | other_acm->SetReceiveCodecs(receive_codecs); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void TestRedFec::Run() { |
| 204 | AudioFrame audioFrame; |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 205 | int32_t outFreqHzB = _outFileB.SamplingFrequency(); |
Henrik Lundin | 4d68208 | 2015-12-10 16:24:39 +0100 | [diff] [blame] | 206 | // 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.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 210 | |
| 211 | while (!_inFileA.EndOfFile()) { |
| 212 | EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0); |
henrik.lundin@webrtc.org | f56c162 | 2015-03-02 12:29:30 +0000 | [diff] [blame] | 213 | EXPECT_GE(_acmA->Add10MsData(audioFrame), 0); |
henrik.lundin | d4ccb00 | 2016-05-17 12:21:55 -0700 | [diff] [blame] | 214 | bool muted; |
| 215 | EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted)); |
| 216 | ASSERT_FALSE(muted); |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 217 | _outFileB.Write10MsData(audioFrame.data(), audioFrame.samples_per_channel_); |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 218 | } |
| 219 | _inFileA.Rewind(); |
| 220 | } |
| 221 | |
| 222 | void TestRedFec::OpenOutFile(int16_t test_number) { |
| 223 | std::string file_name; |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 224 | rtc::StringBuilder file_stream; |
minyue@webrtc.org | aa5ea1c | 2014-05-23 15:16:51 +0000 | [diff] [blame] | 225 | 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 |