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.

254 lines
8.9 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/TestVADDTX.h"
  11. #include <string>
  12. #include "absl/strings/match.h"
  13. #include "api/audio_codecs/audio_decoder_factory_template.h"
  14. #include "api/audio_codecs/audio_encoder_factory_template.h"
  15. #include "api/audio_codecs/ilbc/audio_decoder_ilbc.h"
  16. #include "api/audio_codecs/ilbc/audio_encoder_ilbc.h"
  17. #include "api/audio_codecs/isac/audio_decoder_isac_float.h"
  18. #include "api/audio_codecs/isac/audio_encoder_isac_float.h"
  19. #include "api/audio_codecs/opus/audio_decoder_opus.h"
  20. #include "api/audio_codecs/opus/audio_encoder_opus.h"
  21. #include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
  22. #include "modules/audio_coding/test/PCMFile.h"
  23. #include "rtc_base/strings/string_builder.h"
  24. #include "test/gtest.h"
  25. #include "test/testsupport/file_utils.h"
  26. namespace webrtc {
  27. ActivityMonitor::ActivityMonitor() {
  28. ResetStatistics();
  29. }
  30. int32_t ActivityMonitor::InFrameType(AudioFrameType frame_type) {
  31. counter_[static_cast<int>(frame_type)]++;
  32. return 0;
  33. }
  34. void ActivityMonitor::PrintStatistics() {
  35. printf("\n");
  36. printf("kEmptyFrame %u\n",
  37. counter_[static_cast<int>(AudioFrameType::kEmptyFrame)]);
  38. printf("kAudioFrameSpeech %u\n",
  39. counter_[static_cast<int>(AudioFrameType::kAudioFrameSpeech)]);
  40. printf("kAudioFrameCN %u\n",
  41. counter_[static_cast<int>(AudioFrameType::kAudioFrameCN)]);
  42. printf("\n\n");
  43. }
  44. void ActivityMonitor::ResetStatistics() {
  45. memset(counter_, 0, sizeof(counter_));
  46. }
  47. void ActivityMonitor::GetStatistics(uint32_t* counter) {
  48. memcpy(counter, counter_, sizeof(counter_));
  49. }
  50. TestVadDtx::TestVadDtx()
  51. : encoder_factory_(CreateAudioEncoderFactory<AudioEncoderIlbc,
  52. AudioEncoderIsacFloat,
  53. AudioEncoderOpus>()),
  54. decoder_factory_(CreateAudioDecoderFactory<AudioDecoderIlbc,
  55. AudioDecoderIsacFloat,
  56. AudioDecoderOpus>()),
  57. acm_send_(AudioCodingModule::Create(
  58. AudioCodingModule::Config(decoder_factory_))),
  59. acm_receive_(AudioCodingModule::Create(
  60. AudioCodingModule::Config(decoder_factory_))),
  61. channel_(new Channel),
  62. monitor_(new ActivityMonitor) {
  63. EXPECT_EQ(0, acm_send_->RegisterTransportCallback(channel_.get()));
  64. channel_->RegisterReceiverACM(acm_receive_.get());
  65. EXPECT_EQ(0, acm_send_->RegisterVADCallback(monitor_.get()));
  66. }
  67. bool TestVadDtx::RegisterCodec(const SdpAudioFormat& codec_format,
  68. absl::optional<Vad::Aggressiveness> vad_mode) {
  69. constexpr int payload_type = 17, cn_payload_type = 117;
  70. bool added_comfort_noise = false;
  71. auto encoder = encoder_factory_->MakeAudioEncoder(payload_type, codec_format,
  72. absl::nullopt);
  73. if (vad_mode.has_value() &&
  74. !absl::EqualsIgnoreCase(codec_format.name, "opus")) {
  75. AudioEncoderCngConfig config;
  76. config.speech_encoder = std::move(encoder);
  77. config.num_channels = 1;
  78. config.payload_type = cn_payload_type;
  79. config.vad_mode = vad_mode.value();
  80. encoder = CreateComfortNoiseEncoder(std::move(config));
  81. added_comfort_noise = true;
  82. }
  83. channel_->SetIsStereo(encoder->NumChannels() > 1);
  84. acm_send_->SetEncoder(std::move(encoder));
  85. std::map<int, SdpAudioFormat> receive_codecs = {{payload_type, codec_format}};
  86. acm_receive_->SetReceiveCodecs(receive_codecs);
  87. return added_comfort_noise;
  88. }
  89. // Encoding a file and see if the numbers that various packets occur follow
  90. // the expectation.
  91. void TestVadDtx::Run(std::string in_filename,
  92. int frequency,
  93. int channels,
  94. std::string out_filename,
  95. bool append,
  96. const int* expects) {
  97. monitor_->ResetStatistics();
  98. PCMFile in_file;
  99. in_file.Open(in_filename, frequency, "rb");
  100. in_file.ReadStereo(channels > 1);
  101. // Set test length to 1000 ms (100 blocks of 10 ms each).
  102. in_file.SetNum10MsBlocksToRead(100);
  103. // Fast-forward both files 500 ms (50 blocks). The first second of the file is
  104. // silence, but we want to keep half of that to test silence periods.
  105. in_file.FastForward(50);
  106. PCMFile out_file;
  107. if (append) {
  108. out_file.Open(out_filename, kOutputFreqHz, "ab");
  109. } else {
  110. out_file.Open(out_filename, kOutputFreqHz, "wb");
  111. }
  112. uint16_t frame_size_samples = in_file.PayloadLength10Ms();
  113. AudioFrame audio_frame;
  114. while (!in_file.EndOfFile()) {
  115. in_file.Read10MsData(audio_frame);
  116. audio_frame.timestamp_ = time_stamp_;
  117. time_stamp_ += frame_size_samples;
  118. EXPECT_GE(acm_send_->Add10MsData(audio_frame), 0);
  119. bool muted;
  120. acm_receive_->PlayoutData10Ms(kOutputFreqHz, &audio_frame, &muted);
  121. ASSERT_FALSE(muted);
  122. out_file.Write10MsData(audio_frame);
  123. }
  124. in_file.Close();
  125. out_file.Close();
  126. #ifdef PRINT_STAT
  127. monitor_->PrintStatistics();
  128. #endif
  129. uint32_t stats[3];
  130. monitor_->GetStatistics(stats);
  131. monitor_->ResetStatistics();
  132. for (const auto& st : stats) {
  133. int i = &st - stats; // Calculate the current position in stats.
  134. switch (expects[i]) {
  135. case 0: {
  136. EXPECT_EQ(0u, st) << "stats[" << i << "] error.";
  137. break;
  138. }
  139. case 1: {
  140. EXPECT_GT(st, 0u) << "stats[" << i << "] error.";
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. // Following is the implementation of TestWebRtcVadDtx.
  147. TestWebRtcVadDtx::TestWebRtcVadDtx() : output_file_num_(0) {}
  148. void TestWebRtcVadDtx::Perform() {
  149. RunTestCases({"ISAC", 16000, 1});
  150. RunTestCases({"ISAC", 32000, 1});
  151. RunTestCases({"ILBC", 8000, 1});
  152. RunTestCases({"opus", 48000, 2});
  153. }
  154. // Test various configurations on VAD/DTX.
  155. void TestWebRtcVadDtx::RunTestCases(const SdpAudioFormat& codec_format) {
  156. Test(/*new_outfile=*/true,
  157. /*expect_dtx_enabled=*/RegisterCodec(codec_format, absl::nullopt));
  158. Test(/*new_outfile=*/false,
  159. /*expect_dtx_enabled=*/RegisterCodec(codec_format, Vad::kVadAggressive));
  160. Test(/*new_outfile=*/false,
  161. /*expect_dtx_enabled=*/RegisterCodec(codec_format, Vad::kVadLowBitrate));
  162. Test(/*new_outfile=*/false, /*expect_dtx_enabled=*/RegisterCodec(
  163. codec_format, Vad::kVadVeryAggressive));
  164. Test(/*new_outfile=*/false,
  165. /*expect_dtx_enabled=*/RegisterCodec(codec_format, Vad::kVadNormal));
  166. }
  167. // Set the expectation and run the test.
  168. void TestWebRtcVadDtx::Test(bool new_outfile, bool expect_dtx_enabled) {
  169. int expects[] = {-1, 1, expect_dtx_enabled, 0, 0};
  170. if (new_outfile) {
  171. output_file_num_++;
  172. }
  173. rtc::StringBuilder out_filename;
  174. out_filename << webrtc::test::OutputPath() << "testWebRtcVadDtx_outFile_"
  175. << output_file_num_ << ".pcm";
  176. Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
  177. out_filename.str(), !new_outfile, expects);
  178. }
  179. // Following is the implementation of TestOpusDtx.
  180. void TestOpusDtx::Perform() {
  181. int expects[] = {0, 1, 0, 0, 0};
  182. // Register Opus as send codec
  183. std::string out_filename =
  184. webrtc::test::OutputPath() + "testOpusDtx_outFile_mono.pcm";
  185. RegisterCodec({"opus", 48000, 2}, absl::nullopt);
  186. acm_send_->ModifyEncoder([](std::unique_ptr<AudioEncoder>* encoder_ptr) {
  187. (*encoder_ptr)->SetDtx(false);
  188. });
  189. Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
  190. out_filename, false, expects);
  191. acm_send_->ModifyEncoder([](std::unique_ptr<AudioEncoder>* encoder_ptr) {
  192. (*encoder_ptr)->SetDtx(true);
  193. });
  194. expects[static_cast<int>(AudioFrameType::kEmptyFrame)] = 1;
  195. expects[static_cast<int>(AudioFrameType::kAudioFrameCN)] = 1;
  196. Run(webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 32000, 1,
  197. out_filename, true, expects);
  198. // Register stereo Opus as send codec
  199. out_filename = webrtc::test::OutputPath() + "testOpusDtx_outFile_stereo.pcm";
  200. RegisterCodec({"opus", 48000, 2, {{"stereo", "1"}}}, absl::nullopt);
  201. acm_send_->ModifyEncoder([](std::unique_ptr<AudioEncoder>* encoder_ptr) {
  202. (*encoder_ptr)->SetDtx(false);
  203. });
  204. expects[static_cast<int>(AudioFrameType::kEmptyFrame)] = 0;
  205. expects[static_cast<int>(AudioFrameType::kAudioFrameCN)] = 0;
  206. Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), 32000,
  207. 2, out_filename, false, expects);
  208. acm_send_->ModifyEncoder([](std::unique_ptr<AudioEncoder>* encoder_ptr) {
  209. (*encoder_ptr)->SetDtx(true);
  210. });
  211. expects[static_cast<int>(AudioFrameType::kEmptyFrame)] = 1;
  212. expects[static_cast<int>(AudioFrameType::kAudioFrameCN)] = 1;
  213. Run(webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm"), 32000,
  214. 2, out_filename, true, expects);
  215. }
  216. } // namespace webrtc