You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
8.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "modules/audio_coding/test/TestRedFec.h"
  11. #include <memory>
  12. #include <utility>
  13. #include "absl/strings/match.h"
  14. #include "api/audio_codecs/L16/audio_decoder_L16.h"
  15. #include "api/audio_codecs/L16/audio_encoder_L16.h"
  16. #include "api/audio_codecs/audio_decoder_factory_template.h"
  17. #include "api/audio_codecs/audio_encoder_factory_template.h"
  18. #include "api/audio_codecs/g711/audio_decoder_g711.h"
  19. #include "api/audio_codecs/g711/audio_encoder_g711.h"
  20. #include "api/audio_codecs/g722/audio_decoder_g722.h"
  21. #include "api/audio_codecs/g722/audio_encoder_g722.h"
  22. #include "api/audio_codecs/isac/audio_decoder_isac_float.h"
  23. #include "api/audio_codecs/isac/audio_encoder_isac_float.h"
  24. #include "api/audio_codecs/opus/audio_decoder_opus.h"
  25. #include "api/audio_codecs/opus/audio_encoder_opus.h"
  26. #include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
  27. #include "modules/audio_coding/codecs/red/audio_encoder_copy_red.h"
  28. #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
  29. #include "rtc_base/strings/string_builder.h"
  30. #include "test/gtest.h"
  31. #include "test/testsupport/file_utils.h"
  32. namespace webrtc {
  33. TestRedFec::TestRedFec()
  34. : encoder_factory_(CreateAudioEncoderFactory<AudioEncoderG711,
  35. AudioEncoderG722,
  36. AudioEncoderIsacFloat,
  37. AudioEncoderL16,
  38. AudioEncoderOpus>()),
  39. decoder_factory_(CreateAudioDecoderFactory<AudioDecoderG711,
  40. AudioDecoderG722,
  41. AudioDecoderIsacFloat,
  42. AudioDecoderL16,
  43. AudioDecoderOpus>()),
  44. _acmA(AudioCodingModule::Create(
  45. AudioCodingModule::Config(decoder_factory_))),
  46. _acmB(AudioCodingModule::Create(
  47. AudioCodingModule::Config(decoder_factory_))),
  48. _channelA2B(NULL),
  49. _testCntr(0) {}
  50. TestRedFec::~TestRedFec() {
  51. if (_channelA2B != NULL) {
  52. delete _channelA2B;
  53. _channelA2B = NULL;
  54. }
  55. }
  56. void TestRedFec::Perform() {
  57. const std::string file_name =
  58. webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
  59. _inFileA.Open(file_name, 32000, "rb");
  60. ASSERT_EQ(0, _acmA->InitializeReceiver());
  61. ASSERT_EQ(0, _acmB->InitializeReceiver());
  62. // Create and connect the channel
  63. _channelA2B = new Channel;
  64. _acmA->RegisterTransportCallback(_channelA2B);
  65. _channelA2B->RegisterReceiverACM(_acmB.get());
  66. RegisterSendCodec(_acmA, {"L16", 8000, 1}, Vad::kVadAggressive, true);
  67. OpenOutFile(_testCntr);
  68. Run();
  69. _outFileB.Close();
  70. // Switch to another 8 kHz codec; RED should remain switched on.
  71. RegisterSendCodec(_acmA, {"PCMU", 8000, 1}, Vad::kVadAggressive, true);
  72. OpenOutFile(_testCntr);
  73. Run();
  74. _outFileB.Close();
  75. // Switch to a 16 kHz codec; RED should be switched off.
  76. RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false);
  77. OpenOutFile(_testCntr);
  78. RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false);
  79. Run();
  80. RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false);
  81. Run();
  82. _outFileB.Close();
  83. RegisterSendCodec(_acmA, {"ISAC", 16000, 1}, Vad::kVadVeryAggressive, false);
  84. OpenOutFile(_testCntr);
  85. Run();
  86. _outFileB.Close();
  87. // Switch to a 32 kHz codec; RED should be switched off.
  88. RegisterSendCodec(_acmA, {"ISAC", 32000, 1}, Vad::kVadVeryAggressive, false);
  89. OpenOutFile(_testCntr);
  90. Run();
  91. _outFileB.Close();
  92. RegisterSendCodec(_acmA, {"ISAC", 32000, 1}, absl::nullopt, false);
  93. _channelA2B->SetFECTestWithPacketLoss(true);
  94. // Following tests are under packet losses.
  95. // Switch to a 16 kHz codec; RED should be switched off.
  96. RegisterSendCodec(_acmA, {"G722", 8000, 1}, Vad::kVadAggressive, false);
  97. OpenOutFile(_testCntr);
  98. Run();
  99. _outFileB.Close();
  100. // Switch to a 16 kHz codec, RED should have been switched off.
  101. RegisterSendCodec(_acmA, {"ISAC", 16000, 1}, Vad::kVadVeryAggressive, false);
  102. OpenOutFile(_testCntr);
  103. Run();
  104. _outFileB.Close();
  105. // Switch to a 32 kHz codec, RED should have been switched off.
  106. RegisterSendCodec(_acmA, {"ISAC", 32000, 1}, Vad::kVadVeryAggressive, false);
  107. OpenOutFile(_testCntr);
  108. Run();
  109. _outFileB.Close();
  110. RegisterSendCodec(_acmA, {"ISAC", 32000, 1}, absl::nullopt, false);
  111. RegisterSendCodec(_acmA, {"opus", 48000, 2}, absl::nullopt, false);
  112. // _channelA2B imposes 25% packet loss rate.
  113. EXPECT_EQ(0, _acmA->SetPacketLossRate(25));
  114. _acmA->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
  115. EXPECT_EQ(true, (*enc)->SetFec(true));
  116. });
  117. OpenOutFile(_testCntr);
  118. Run();
  119. // Switch to L16 with RED.
  120. RegisterSendCodec(_acmA, {"L16", 8000, 1}, absl::nullopt, true);
  121. Run();
  122. // Switch to Opus again.
  123. RegisterSendCodec(_acmA, {"opus", 48000, 2}, absl::nullopt, false);
  124. _acmA->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
  125. EXPECT_EQ(true, (*enc)->SetFec(false));
  126. });
  127. Run();
  128. _acmA->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* enc) {
  129. EXPECT_EQ(true, (*enc)->SetFec(true));
  130. });
  131. _outFileB.Close();
  132. }
  133. void TestRedFec::RegisterSendCodec(
  134. const std::unique_ptr<AudioCodingModule>& acm,
  135. const SdpAudioFormat& codec_format,
  136. absl::optional<Vad::Aggressiveness> vad_mode,
  137. bool use_red) {
  138. constexpr int payload_type = 17, cn_payload_type = 27, red_payload_type = 37;
  139. const auto& other_acm = &acm == &_acmA ? _acmB : _acmA;
  140. auto encoder = encoder_factory_->MakeAudioEncoder(payload_type, codec_format,
  141. absl::nullopt);
  142. EXPECT_NE(encoder, nullptr);
  143. std::map<int, SdpAudioFormat> receive_codecs = {{payload_type, codec_format}};
  144. if (!absl::EqualsIgnoreCase(codec_format.name, "opus")) {
  145. if (vad_mode.has_value()) {
  146. AudioEncoderCngConfig config;
  147. config.speech_encoder = std::move(encoder);
  148. config.num_channels = 1;
  149. config.payload_type = cn_payload_type;
  150. config.vad_mode = vad_mode.value();
  151. encoder = CreateComfortNoiseEncoder(std::move(config));
  152. receive_codecs.emplace(std::make_pair(
  153. cn_payload_type, SdpAudioFormat("CN", codec_format.clockrate_hz, 1)));
  154. }
  155. if (use_red) {
  156. AudioEncoderCopyRed::Config config;
  157. config.payload_type = red_payload_type;
  158. config.speech_encoder = std::move(encoder);
  159. encoder = std::make_unique<AudioEncoderCopyRed>(std::move(config));
  160. receive_codecs.emplace(
  161. std::make_pair(red_payload_type,
  162. SdpAudioFormat("red", codec_format.clockrate_hz, 1)));
  163. }
  164. }
  165. acm->SetEncoder(std::move(encoder));
  166. other_acm->SetReceiveCodecs(receive_codecs);
  167. }
  168. void TestRedFec::Run() {
  169. AudioFrame audioFrame;
  170. int32_t outFreqHzB = _outFileB.SamplingFrequency();
  171. // Set test length to 500 ms (50 blocks of 10 ms each).
  172. _inFileA.SetNum10MsBlocksToRead(50);
  173. // Fast-forward 1 second (100 blocks) since the file starts with silence.
  174. _inFileA.FastForward(100);
  175. while (!_inFileA.EndOfFile()) {
  176. EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
  177. EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
  178. bool muted;
  179. EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
  180. ASSERT_FALSE(muted);
  181. _outFileB.Write10MsData(audioFrame.data(), audioFrame.samples_per_channel_);
  182. }
  183. _inFileA.Rewind();
  184. }
  185. void TestRedFec::OpenOutFile(int16_t test_number) {
  186. std::string file_name;
  187. rtc::StringBuilder file_stream;
  188. file_stream << webrtc::test::OutputPath();
  189. file_stream << "TestRedFec_outFile_";
  190. file_stream << test_number << ".pcm";
  191. file_name = file_stream.str();
  192. _outFileB.Open(file_name, 16000, "wb");
  193. }
  194. } // namespace webrtc