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.

264 lines
7.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/EncodeDecodeTest.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <memory>
  14. #include "api/audio_codecs/builtin_audio_decoder_factory.h"
  15. #include "api/audio_codecs/builtin_audio_encoder_factory.h"
  16. #include "modules/audio_coding/include/audio_coding_module.h"
  17. #include "rtc_base/strings/string_builder.h"
  18. #include "test/gtest.h"
  19. #include "test/testsupport/file_utils.h"
  20. namespace webrtc {
  21. TestPacketization::TestPacketization(RTPStream* rtpStream, uint16_t frequency)
  22. : _rtpStream(rtpStream), _frequency(frequency), _seqNo(0) {}
  23. TestPacketization::~TestPacketization() {}
  24. int32_t TestPacketization::SendData(const AudioFrameType /* frameType */,
  25. const uint8_t payloadType,
  26. const uint32_t timeStamp,
  27. const uint8_t* payloadData,
  28. const size_t payloadSize) {
  29. _rtpStream->Write(payloadType, timeStamp, _seqNo++, payloadData, payloadSize,
  30. _frequency);
  31. return 1;
  32. }
  33. Sender::Sender()
  34. : _acm(NULL), _pcmFile(), _audioFrame(), _packetization(NULL) {}
  35. void Sender::Setup(AudioCodingModule* acm,
  36. RTPStream* rtpStream,
  37. std::string in_file_name,
  38. int in_sample_rate,
  39. int payload_type,
  40. SdpAudioFormat format) {
  41. // Open input file
  42. const std::string file_name = webrtc::test::ResourcePath(in_file_name, "pcm");
  43. _pcmFile.Open(file_name, in_sample_rate, "rb");
  44. if (format.num_channels == 2) {
  45. _pcmFile.ReadStereo(true);
  46. }
  47. // Set test length to 500 ms (50 blocks of 10 ms each).
  48. _pcmFile.SetNum10MsBlocksToRead(50);
  49. // Fast-forward 1 second (100 blocks) since the file starts with silence.
  50. _pcmFile.FastForward(100);
  51. acm->SetEncoder(CreateBuiltinAudioEncoderFactory()->MakeAudioEncoder(
  52. payload_type, format, absl::nullopt));
  53. _packetization = new TestPacketization(rtpStream, format.clockrate_hz);
  54. EXPECT_EQ(0, acm->RegisterTransportCallback(_packetization));
  55. _acm = acm;
  56. }
  57. void Sender::Teardown() {
  58. _pcmFile.Close();
  59. delete _packetization;
  60. }
  61. bool Sender::Add10MsData() {
  62. if (!_pcmFile.EndOfFile()) {
  63. EXPECT_GT(_pcmFile.Read10MsData(_audioFrame), 0);
  64. int32_t ok = _acm->Add10MsData(_audioFrame);
  65. EXPECT_GE(ok, 0);
  66. return ok >= 0 ? true : false;
  67. }
  68. return false;
  69. }
  70. void Sender::Run() {
  71. while (true) {
  72. if (!Add10MsData()) {
  73. break;
  74. }
  75. }
  76. }
  77. Receiver::Receiver()
  78. : _playoutLengthSmpls(WEBRTC_10MS_PCM_AUDIO),
  79. _payloadSizeBytes(MAX_INCOMING_PAYLOAD) {}
  80. void Receiver::Setup(AudioCodingModule* acm,
  81. RTPStream* rtpStream,
  82. std::string out_file_name,
  83. size_t channels,
  84. int file_num) {
  85. EXPECT_EQ(0, acm->InitializeReceiver());
  86. if (channels == 1) {
  87. acm->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
  88. {104, {"ISAC", 32000, 1}},
  89. {107, {"L16", 8000, 1}},
  90. {108, {"L16", 16000, 1}},
  91. {109, {"L16", 32000, 1}},
  92. {0, {"PCMU", 8000, 1}},
  93. {8, {"PCMA", 8000, 1}},
  94. {102, {"ILBC", 8000, 1}},
  95. {9, {"G722", 8000, 1}},
  96. {120, {"OPUS", 48000, 2}},
  97. {13, {"CN", 8000, 1}},
  98. {98, {"CN", 16000, 1}},
  99. {99, {"CN", 32000, 1}}});
  100. } else {
  101. ASSERT_EQ(channels, 2u);
  102. acm->SetReceiveCodecs({{111, {"L16", 8000, 2}},
  103. {112, {"L16", 16000, 2}},
  104. {113, {"L16", 32000, 2}},
  105. {110, {"PCMU", 8000, 2}},
  106. {118, {"PCMA", 8000, 2}},
  107. {119, {"G722", 8000, 2}},
  108. {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}});
  109. }
  110. int playSampFreq;
  111. std::string file_name;
  112. rtc::StringBuilder file_stream;
  113. file_stream << webrtc::test::OutputPath() << out_file_name << file_num
  114. << ".pcm";
  115. file_name = file_stream.str();
  116. _rtpStream = rtpStream;
  117. playSampFreq = 32000;
  118. _pcmFile.Open(file_name, 32000, "wb+");
  119. _realPayloadSizeBytes = 0;
  120. _playoutBuffer = new int16_t[WEBRTC_10MS_PCM_AUDIO];
  121. _frequency = playSampFreq;
  122. _acm = acm;
  123. _firstTime = true;
  124. }
  125. void Receiver::Teardown() {
  126. delete[] _playoutBuffer;
  127. _pcmFile.Close();
  128. }
  129. bool Receiver::IncomingPacket() {
  130. if (!_rtpStream->EndOfFile()) {
  131. if (_firstTime) {
  132. _firstTime = false;
  133. _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
  134. _payloadSizeBytes, &_nextTime);
  135. if (_realPayloadSizeBytes == 0) {
  136. if (_rtpStream->EndOfFile()) {
  137. _firstTime = true;
  138. return true;
  139. } else {
  140. return false;
  141. }
  142. }
  143. }
  144. EXPECT_EQ(0, _acm->IncomingPacket(_incomingPayload, _realPayloadSizeBytes,
  145. _rtpHeader));
  146. _realPayloadSizeBytes = _rtpStream->Read(&_rtpHeader, _incomingPayload,
  147. _payloadSizeBytes, &_nextTime);
  148. if (_realPayloadSizeBytes == 0 && _rtpStream->EndOfFile()) {
  149. _firstTime = true;
  150. }
  151. }
  152. return true;
  153. }
  154. bool Receiver::PlayoutData() {
  155. AudioFrame audioFrame;
  156. bool muted;
  157. int32_t ok = _acm->PlayoutData10Ms(_frequency, &audioFrame, &muted);
  158. if (muted) {
  159. ADD_FAILURE();
  160. return false;
  161. }
  162. EXPECT_EQ(0, ok);
  163. if (ok < 0) {
  164. return false;
  165. }
  166. if (_playoutLengthSmpls == 0) {
  167. return false;
  168. }
  169. _pcmFile.Write10MsData(audioFrame.data(), audioFrame.samples_per_channel_ *
  170. audioFrame.num_channels_);
  171. return true;
  172. }
  173. void Receiver::Run() {
  174. uint8_t counter500Ms = 50;
  175. uint32_t clock = 0;
  176. while (counter500Ms > 0) {
  177. if (clock == 0 || clock >= _nextTime) {
  178. EXPECT_TRUE(IncomingPacket());
  179. if (clock == 0) {
  180. clock = _nextTime;
  181. }
  182. }
  183. if ((clock % 10) == 0) {
  184. if (!PlayoutData()) {
  185. clock++;
  186. continue;
  187. }
  188. }
  189. if (_rtpStream->EndOfFile()) {
  190. counter500Ms--;
  191. }
  192. clock++;
  193. }
  194. }
  195. EncodeDecodeTest::EncodeDecodeTest() = default;
  196. void EncodeDecodeTest::Perform() {
  197. const std::map<int, SdpAudioFormat> send_codecs = {
  198. {103, {"ISAC", 16000, 1}}, {104, {"ISAC", 32000, 1}},
  199. {107, {"L16", 8000, 1}}, {108, {"L16", 16000, 1}},
  200. {109, {"L16", 32000, 1}}, {0, {"PCMU", 8000, 1}},
  201. {8, {"PCMA", 8000, 1}},
  202. #ifdef WEBRTC_CODEC_ILBC
  203. {102, {"ILBC", 8000, 1}},
  204. #endif
  205. {9, {"G722", 8000, 1}}};
  206. int file_num = 0;
  207. for (const auto& send_codec : send_codecs) {
  208. RTPFile rtpFile;
  209. std::unique_ptr<AudioCodingModule> acm(AudioCodingModule::Create(
  210. AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory())));
  211. std::string fileName = webrtc::test::TempFilename(
  212. webrtc::test::OutputPath(), "encode_decode_rtp");
  213. rtpFile.Open(fileName.c_str(), "wb+");
  214. rtpFile.WriteHeader();
  215. Sender sender;
  216. sender.Setup(acm.get(), &rtpFile, "audio_coding/testfile32kHz", 32000,
  217. send_codec.first, send_codec.second);
  218. sender.Run();
  219. sender.Teardown();
  220. rtpFile.Close();
  221. rtpFile.Open(fileName.c_str(), "rb");
  222. rtpFile.ReadHeader();
  223. Receiver receiver;
  224. receiver.Setup(acm.get(), &rtpFile, "encodeDecode_out", 1, file_num);
  225. receiver.Run();
  226. receiver.Teardown();
  227. rtpFile.Close();
  228. file_num++;
  229. }
  230. }
  231. } // namespace webrtc