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.

192 lines
6.6 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 "TwoWayCommunication.h"
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <memory>
  15. #include "api/audio_codecs/builtin_audio_decoder_factory.h"
  16. #include "api/audio_codecs/builtin_audio_encoder_factory.h"
  17. #include "modules/audio_coding/test/PCMFile.h"
  18. #include "test/gtest.h"
  19. #include "test/testsupport/file_utils.h"
  20. namespace webrtc {
  21. #define MAX_FILE_NAME_LENGTH_BYTE 500
  22. TwoWayCommunication::TwoWayCommunication()
  23. : _acmA(AudioCodingModule::Create(
  24. AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
  25. _acmRefA(AudioCodingModule::Create(
  26. AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))) {
  27. AudioCodingModule::Config config;
  28. // The clicks will be more obvious if time-stretching is not allowed.
  29. // TODO(henrik.lundin) Really?
  30. config.neteq_config.for_test_no_time_stretching = true;
  31. config.decoder_factory = CreateBuiltinAudioDecoderFactory();
  32. _acmB.reset(AudioCodingModule::Create(config));
  33. _acmRefB.reset(AudioCodingModule::Create(config));
  34. }
  35. TwoWayCommunication::~TwoWayCommunication() {
  36. delete _channel_A2B;
  37. delete _channel_B2A;
  38. delete _channelRef_A2B;
  39. delete _channelRef_B2A;
  40. _inFileA.Close();
  41. _inFileB.Close();
  42. _outFileA.Close();
  43. _outFileB.Close();
  44. _outFileRefA.Close();
  45. _outFileRefB.Close();
  46. }
  47. void TwoWayCommunication::SetUpAutotest(
  48. AudioEncoderFactory* const encoder_factory,
  49. const SdpAudioFormat& format1,
  50. const int payload_type1,
  51. const SdpAudioFormat& format2,
  52. const int payload_type2) {
  53. //--- Set A codecs
  54. _acmA->SetEncoder(
  55. encoder_factory->MakeAudioEncoder(payload_type1, format1, absl::nullopt));
  56. _acmA->SetReceiveCodecs({{payload_type2, format2}});
  57. //--- Set ref-A codecs
  58. _acmRefA->SetEncoder(
  59. encoder_factory->MakeAudioEncoder(payload_type1, format1, absl::nullopt));
  60. _acmRefA->SetReceiveCodecs({{payload_type2, format2}});
  61. //--- Set B codecs
  62. _acmB->SetEncoder(
  63. encoder_factory->MakeAudioEncoder(payload_type2, format2, absl::nullopt));
  64. _acmB->SetReceiveCodecs({{payload_type1, format1}});
  65. //--- Set ref-B codecs
  66. _acmRefB->SetEncoder(
  67. encoder_factory->MakeAudioEncoder(payload_type2, format2, absl::nullopt));
  68. _acmRefB->SetReceiveCodecs({{payload_type1, format1}});
  69. uint16_t frequencyHz;
  70. //--- Input A and B
  71. std::string in_file_name =
  72. webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
  73. frequencyHz = 16000;
  74. _inFileA.Open(in_file_name, frequencyHz, "rb");
  75. _inFileB.Open(in_file_name, frequencyHz, "rb");
  76. //--- Output A
  77. std::string output_file_a = webrtc::test::OutputPath() + "outAutotestA.pcm";
  78. frequencyHz = 16000;
  79. _outFileA.Open(output_file_a, frequencyHz, "wb");
  80. std::string output_ref_file_a =
  81. webrtc::test::OutputPath() + "ref_outAutotestA.pcm";
  82. _outFileRefA.Open(output_ref_file_a, frequencyHz, "wb");
  83. //--- Output B
  84. std::string output_file_b = webrtc::test::OutputPath() + "outAutotestB.pcm";
  85. frequencyHz = 16000;
  86. _outFileB.Open(output_file_b, frequencyHz, "wb");
  87. std::string output_ref_file_b =
  88. webrtc::test::OutputPath() + "ref_outAutotestB.pcm";
  89. _outFileRefB.Open(output_ref_file_b, frequencyHz, "wb");
  90. //--- Set A-to-B channel
  91. _channel_A2B = new Channel;
  92. _acmA->RegisterTransportCallback(_channel_A2B);
  93. _channel_A2B->RegisterReceiverACM(_acmB.get());
  94. //--- Do the same for the reference
  95. _channelRef_A2B = new Channel;
  96. _acmRefA->RegisterTransportCallback(_channelRef_A2B);
  97. _channelRef_A2B->RegisterReceiverACM(_acmRefB.get());
  98. //--- Set B-to-A channel
  99. _channel_B2A = new Channel;
  100. _acmB->RegisterTransportCallback(_channel_B2A);
  101. _channel_B2A->RegisterReceiverACM(_acmA.get());
  102. //--- Do the same for reference
  103. _channelRef_B2A = new Channel;
  104. _acmRefB->RegisterTransportCallback(_channelRef_B2A);
  105. _channelRef_B2A->RegisterReceiverACM(_acmRefA.get());
  106. }
  107. void TwoWayCommunication::Perform() {
  108. const SdpAudioFormat format1("ISAC", 16000, 1);
  109. const SdpAudioFormat format2("L16", 8000, 1);
  110. constexpr int payload_type1 = 17, payload_type2 = 18;
  111. auto encoder_factory = CreateBuiltinAudioEncoderFactory();
  112. SetUpAutotest(encoder_factory.get(), format1, payload_type1, format2,
  113. payload_type2);
  114. unsigned int msecPassed = 0;
  115. unsigned int secPassed = 0;
  116. int32_t outFreqHzA = _outFileA.SamplingFrequency();
  117. int32_t outFreqHzB = _outFileB.SamplingFrequency();
  118. AudioFrame audioFrame;
  119. // In the following loop we tests that the code can handle misuse of the APIs.
  120. // In the middle of a session with data flowing between two sides, called A
  121. // and B, APIs will be called, and the code should continue to run, and be
  122. // able to recover.
  123. while (!_inFileA.EndOfFile() && !_inFileB.EndOfFile()) {
  124. msecPassed += 10;
  125. EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
  126. EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
  127. EXPECT_GE(_acmRefA->Add10MsData(audioFrame), 0);
  128. EXPECT_GT(_inFileB.Read10MsData(audioFrame), 0);
  129. EXPECT_GE(_acmB->Add10MsData(audioFrame), 0);
  130. EXPECT_GE(_acmRefB->Add10MsData(audioFrame), 0);
  131. bool muted;
  132. EXPECT_EQ(0, _acmA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
  133. ASSERT_FALSE(muted);
  134. _outFileA.Write10MsData(audioFrame);
  135. EXPECT_EQ(0, _acmRefA->PlayoutData10Ms(outFreqHzA, &audioFrame, &muted));
  136. ASSERT_FALSE(muted);
  137. _outFileRefA.Write10MsData(audioFrame);
  138. EXPECT_EQ(0, _acmB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
  139. ASSERT_FALSE(muted);
  140. _outFileB.Write10MsData(audioFrame);
  141. EXPECT_EQ(0, _acmRefB->PlayoutData10Ms(outFreqHzB, &audioFrame, &muted));
  142. ASSERT_FALSE(muted);
  143. _outFileRefB.Write10MsData(audioFrame);
  144. // Update time counters each time a second of data has passed.
  145. if (msecPassed >= 1000) {
  146. msecPassed = 0;
  147. secPassed++;
  148. }
  149. // Re-register send codec on side B.
  150. if (((secPassed % 5) == 4) && (msecPassed >= 990)) {
  151. _acmB->SetEncoder(encoder_factory->MakeAudioEncoder(
  152. payload_type2, format2, absl::nullopt));
  153. }
  154. // Initialize receiver on side A.
  155. if (((secPassed % 7) == 6) && (msecPassed == 0))
  156. EXPECT_EQ(0, _acmA->InitializeReceiver());
  157. // Re-register codec on side A.
  158. if (((secPassed % 7) == 6) && (msecPassed >= 990)) {
  159. _acmA->SetReceiveCodecs({{payload_type2, format2}});
  160. }
  161. }
  162. }
  163. } // namespace webrtc