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.

444 lines
15 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/TestAllCodecs.h"
  11. #include <cstdio>
  12. #include <limits>
  13. #include <string>
  14. #include "absl/strings/match.h"
  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/include/audio_coding_module_typedefs.h"
  18. #include "modules/include/module_common_types.h"
  19. #include "rtc_base/logging.h"
  20. #include "rtc_base/string_encode.h"
  21. #include "rtc_base/strings/string_builder.h"
  22. #include "test/gtest.h"
  23. #include "test/testsupport/file_utils.h"
  24. // Description of the test:
  25. // In this test we set up a one-way communication channel from a participant
  26. // called "a" to a participant called "b".
  27. // a -> channel_a_to_b -> b
  28. //
  29. // The test loops through all available mono codecs, encode at "a" sends over
  30. // the channel, and decodes at "b".
  31. #define CHECK_ERROR(f) \
  32. do { \
  33. EXPECT_GE(f, 0) << "Error Calling API"; \
  34. } while (0)
  35. namespace {
  36. const size_t kVariableSize = std::numeric_limits<size_t>::max();
  37. }
  38. namespace webrtc {
  39. // Class for simulating packet handling.
  40. TestPack::TestPack()
  41. : receiver_acm_(NULL),
  42. sequence_number_(0),
  43. timestamp_diff_(0),
  44. last_in_timestamp_(0),
  45. total_bytes_(0),
  46. payload_size_(0) {}
  47. TestPack::~TestPack() {}
  48. void TestPack::RegisterReceiverACM(AudioCodingModule* acm) {
  49. receiver_acm_ = acm;
  50. return;
  51. }
  52. int32_t TestPack::SendData(AudioFrameType frame_type,
  53. uint8_t payload_type,
  54. uint32_t timestamp,
  55. const uint8_t* payload_data,
  56. size_t payload_size) {
  57. RTPHeader rtp_header;
  58. int32_t status;
  59. rtp_header.markerBit = false;
  60. rtp_header.ssrc = 0;
  61. rtp_header.sequenceNumber = sequence_number_++;
  62. rtp_header.payloadType = payload_type;
  63. rtp_header.timestamp = timestamp;
  64. if (frame_type == AudioFrameType::kEmptyFrame) {
  65. // Skip this frame.
  66. return 0;
  67. }
  68. // Only run mono for all test cases.
  69. memcpy(payload_data_, payload_data, payload_size);
  70. status =
  71. receiver_acm_->IncomingPacket(payload_data_, payload_size, rtp_header);
  72. payload_size_ = payload_size;
  73. timestamp_diff_ = timestamp - last_in_timestamp_;
  74. last_in_timestamp_ = timestamp;
  75. total_bytes_ += payload_size;
  76. return status;
  77. }
  78. size_t TestPack::payload_size() {
  79. return payload_size_;
  80. }
  81. uint32_t TestPack::timestamp_diff() {
  82. return timestamp_diff_;
  83. }
  84. void TestPack::reset_payload_size() {
  85. payload_size_ = 0;
  86. }
  87. TestAllCodecs::TestAllCodecs()
  88. : acm_a_(AudioCodingModule::Create(
  89. AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
  90. acm_b_(AudioCodingModule::Create(
  91. AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
  92. channel_a_to_b_(NULL),
  93. test_count_(0),
  94. packet_size_samples_(0),
  95. packet_size_bytes_(0) {}
  96. TestAllCodecs::~TestAllCodecs() {
  97. if (channel_a_to_b_ != NULL) {
  98. delete channel_a_to_b_;
  99. channel_a_to_b_ = NULL;
  100. }
  101. }
  102. void TestAllCodecs::Perform() {
  103. const std::string file_name =
  104. webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
  105. infile_a_.Open(file_name, 32000, "rb");
  106. acm_a_->InitializeReceiver();
  107. acm_b_->InitializeReceiver();
  108. acm_b_->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
  109. {104, {"ISAC", 32000, 1}},
  110. {107, {"L16", 8000, 1}},
  111. {108, {"L16", 16000, 1}},
  112. {109, {"L16", 32000, 1}},
  113. {111, {"L16", 8000, 2}},
  114. {112, {"L16", 16000, 2}},
  115. {113, {"L16", 32000, 2}},
  116. {0, {"PCMU", 8000, 1}},
  117. {110, {"PCMU", 8000, 2}},
  118. {8, {"PCMA", 8000, 1}},
  119. {118, {"PCMA", 8000, 2}},
  120. {102, {"ILBC", 8000, 1}},
  121. {9, {"G722", 8000, 1}},
  122. {119, {"G722", 8000, 2}},
  123. {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}},
  124. {13, {"CN", 8000, 1}},
  125. {98, {"CN", 16000, 1}},
  126. {99, {"CN", 32000, 1}}});
  127. // Create and connect the channel
  128. channel_a_to_b_ = new TestPack;
  129. acm_a_->RegisterTransportCallback(channel_a_to_b_);
  130. channel_a_to_b_->RegisterReceiverACM(acm_b_.get());
  131. // All codecs are tested for all allowed sampling frequencies, rates and
  132. // packet sizes.
  133. test_count_++;
  134. OpenOutFile(test_count_);
  135. char codec_g722[] = "G722";
  136. RegisterSendCodec('A', codec_g722, 16000, 64000, 160, 0);
  137. Run(channel_a_to_b_);
  138. RegisterSendCodec('A', codec_g722, 16000, 64000, 320, 0);
  139. Run(channel_a_to_b_);
  140. RegisterSendCodec('A', codec_g722, 16000, 64000, 480, 0);
  141. Run(channel_a_to_b_);
  142. RegisterSendCodec('A', codec_g722, 16000, 64000, 640, 0);
  143. Run(channel_a_to_b_);
  144. RegisterSendCodec('A', codec_g722, 16000, 64000, 800, 0);
  145. Run(channel_a_to_b_);
  146. RegisterSendCodec('A', codec_g722, 16000, 64000, 960, 0);
  147. Run(channel_a_to_b_);
  148. outfile_b_.Close();
  149. #ifdef WEBRTC_CODEC_ILBC
  150. test_count_++;
  151. OpenOutFile(test_count_);
  152. char codec_ilbc[] = "ILBC";
  153. RegisterSendCodec('A', codec_ilbc, 8000, 13300, 240, 0);
  154. Run(channel_a_to_b_);
  155. RegisterSendCodec('A', codec_ilbc, 8000, 13300, 480, 0);
  156. Run(channel_a_to_b_);
  157. RegisterSendCodec('A', codec_ilbc, 8000, 15200, 160, 0);
  158. Run(channel_a_to_b_);
  159. RegisterSendCodec('A', codec_ilbc, 8000, 15200, 320, 0);
  160. Run(channel_a_to_b_);
  161. outfile_b_.Close();
  162. #endif
  163. #if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
  164. test_count_++;
  165. OpenOutFile(test_count_);
  166. char codec_isac[] = "ISAC";
  167. RegisterSendCodec('A', codec_isac, 16000, -1, 480, kVariableSize);
  168. Run(channel_a_to_b_);
  169. RegisterSendCodec('A', codec_isac, 16000, -1, 960, kVariableSize);
  170. Run(channel_a_to_b_);
  171. RegisterSendCodec('A', codec_isac, 16000, 15000, 480, kVariableSize);
  172. Run(channel_a_to_b_);
  173. RegisterSendCodec('A', codec_isac, 16000, 32000, 960, kVariableSize);
  174. Run(channel_a_to_b_);
  175. outfile_b_.Close();
  176. #endif
  177. #ifdef WEBRTC_CODEC_ISAC
  178. test_count_++;
  179. OpenOutFile(test_count_);
  180. RegisterSendCodec('A', codec_isac, 32000, -1, 960, kVariableSize);
  181. Run(channel_a_to_b_);
  182. RegisterSendCodec('A', codec_isac, 32000, 56000, 960, kVariableSize);
  183. Run(channel_a_to_b_);
  184. RegisterSendCodec('A', codec_isac, 32000, 37000, 960, kVariableSize);
  185. Run(channel_a_to_b_);
  186. RegisterSendCodec('A', codec_isac, 32000, 32000, 960, kVariableSize);
  187. Run(channel_a_to_b_);
  188. outfile_b_.Close();
  189. #endif
  190. test_count_++;
  191. OpenOutFile(test_count_);
  192. char codec_l16[] = "L16";
  193. RegisterSendCodec('A', codec_l16, 8000, 128000, 80, 0);
  194. Run(channel_a_to_b_);
  195. RegisterSendCodec('A', codec_l16, 8000, 128000, 160, 0);
  196. Run(channel_a_to_b_);
  197. RegisterSendCodec('A', codec_l16, 8000, 128000, 240, 0);
  198. Run(channel_a_to_b_);
  199. RegisterSendCodec('A', codec_l16, 8000, 128000, 320, 0);
  200. Run(channel_a_to_b_);
  201. outfile_b_.Close();
  202. test_count_++;
  203. OpenOutFile(test_count_);
  204. RegisterSendCodec('A', codec_l16, 16000, 256000, 160, 0);
  205. Run(channel_a_to_b_);
  206. RegisterSendCodec('A', codec_l16, 16000, 256000, 320, 0);
  207. Run(channel_a_to_b_);
  208. RegisterSendCodec('A', codec_l16, 16000, 256000, 480, 0);
  209. Run(channel_a_to_b_);
  210. RegisterSendCodec('A', codec_l16, 16000, 256000, 640, 0);
  211. Run(channel_a_to_b_);
  212. outfile_b_.Close();
  213. test_count_++;
  214. OpenOutFile(test_count_);
  215. RegisterSendCodec('A', codec_l16, 32000, 512000, 320, 0);
  216. Run(channel_a_to_b_);
  217. RegisterSendCodec('A', codec_l16, 32000, 512000, 640, 0);
  218. Run(channel_a_to_b_);
  219. outfile_b_.Close();
  220. test_count_++;
  221. OpenOutFile(test_count_);
  222. char codec_pcma[] = "PCMA";
  223. RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, 0);
  224. Run(channel_a_to_b_);
  225. RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, 0);
  226. Run(channel_a_to_b_);
  227. RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, 0);
  228. Run(channel_a_to_b_);
  229. RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, 0);
  230. Run(channel_a_to_b_);
  231. RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, 0);
  232. Run(channel_a_to_b_);
  233. RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, 0);
  234. Run(channel_a_to_b_);
  235. char codec_pcmu[] = "PCMU";
  236. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, 0);
  237. Run(channel_a_to_b_);
  238. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, 0);
  239. Run(channel_a_to_b_);
  240. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, 0);
  241. Run(channel_a_to_b_);
  242. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, 0);
  243. Run(channel_a_to_b_);
  244. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, 0);
  245. Run(channel_a_to_b_);
  246. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, 0);
  247. Run(channel_a_to_b_);
  248. outfile_b_.Close();
  249. #ifdef WEBRTC_CODEC_OPUS
  250. test_count_++;
  251. OpenOutFile(test_count_);
  252. char codec_opus[] = "OPUS";
  253. RegisterSendCodec('A', codec_opus, 48000, 6000, 480, kVariableSize);
  254. Run(channel_a_to_b_);
  255. RegisterSendCodec('A', codec_opus, 48000, 20000, 480 * 2, kVariableSize);
  256. Run(channel_a_to_b_);
  257. RegisterSendCodec('A', codec_opus, 48000, 32000, 480 * 4, kVariableSize);
  258. Run(channel_a_to_b_);
  259. RegisterSendCodec('A', codec_opus, 48000, 48000, 480, kVariableSize);
  260. Run(channel_a_to_b_);
  261. RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 4, kVariableSize);
  262. Run(channel_a_to_b_);
  263. RegisterSendCodec('A', codec_opus, 48000, 96000, 480 * 6, kVariableSize);
  264. Run(channel_a_to_b_);
  265. RegisterSendCodec('A', codec_opus, 48000, 500000, 480 * 2, kVariableSize);
  266. Run(channel_a_to_b_);
  267. outfile_b_.Close();
  268. #endif
  269. }
  270. // Register Codec to use in the test
  271. //
  272. // Input: side - which ACM to use, 'A' or 'B'
  273. // codec_name - name to use when register the codec
  274. // sampling_freq_hz - sampling frequency in Herz
  275. // rate - bitrate in bytes
  276. // packet_size - packet size in samples
  277. // extra_byte - if extra bytes needed compared to the bitrate
  278. // used when registering, can be an internal header
  279. // set to kVariableSize if the codec is a variable
  280. // rate codec
  281. void TestAllCodecs::RegisterSendCodec(char side,
  282. char* codec_name,
  283. int32_t sampling_freq_hz,
  284. int rate,
  285. int packet_size,
  286. size_t extra_byte) {
  287. // Store packet-size in samples, used to validate the received packet.
  288. // If G.722, store half the size to compensate for the timestamp bug in the
  289. // RFC for G.722.
  290. // If iSAC runs in adaptive mode, packet size in samples can change on the
  291. // fly, so we exclude this test by setting |packet_size_samples_| to -1.
  292. int clockrate_hz = sampling_freq_hz;
  293. size_t num_channels = 1;
  294. if (absl::EqualsIgnoreCase(codec_name, "G722")) {
  295. packet_size_samples_ = packet_size / 2;
  296. clockrate_hz = sampling_freq_hz / 2;
  297. } else if (absl::EqualsIgnoreCase(codec_name, "ISAC") && (rate == -1)) {
  298. packet_size_samples_ = -1;
  299. } else if (absl::EqualsIgnoreCase(codec_name, "OPUS")) {
  300. packet_size_samples_ = packet_size;
  301. num_channels = 2;
  302. } else {
  303. packet_size_samples_ = packet_size;
  304. }
  305. // Store the expected packet size in bytes, used to validate the received
  306. // packet. If variable rate codec (extra_byte == -1), set to -1.
  307. if (extra_byte != kVariableSize) {
  308. // Add 0.875 to always round up to a whole byte
  309. packet_size_bytes_ =
  310. static_cast<size_t>(static_cast<float>(packet_size * rate) /
  311. static_cast<float>(sampling_freq_hz * 8) +
  312. 0.875) +
  313. extra_byte;
  314. } else {
  315. // Packets will have a variable size.
  316. packet_size_bytes_ = kVariableSize;
  317. }
  318. // Set pointer to the ACM where to register the codec.
  319. AudioCodingModule* my_acm = NULL;
  320. switch (side) {
  321. case 'A': {
  322. my_acm = acm_a_.get();
  323. break;
  324. }
  325. case 'B': {
  326. my_acm = acm_b_.get();
  327. break;
  328. }
  329. default: {
  330. break;
  331. }
  332. }
  333. ASSERT_TRUE(my_acm != NULL);
  334. auto factory = CreateBuiltinAudioEncoderFactory();
  335. constexpr int payload_type = 17;
  336. SdpAudioFormat format = {codec_name, clockrate_hz, num_channels};
  337. format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact(
  338. packet_size, rtc::CheckedDivExact(sampling_freq_hz, 1000)));
  339. my_acm->SetEncoder(
  340. factory->MakeAudioEncoder(payload_type, format, absl::nullopt));
  341. }
  342. void TestAllCodecs::Run(TestPack* channel) {
  343. AudioFrame audio_frame;
  344. int32_t out_freq_hz = outfile_b_.SamplingFrequency();
  345. size_t receive_size;
  346. uint32_t timestamp_diff;
  347. channel->reset_payload_size();
  348. int error_count = 0;
  349. int counter = 0;
  350. // Set test length to 500 ms (50 blocks of 10 ms each).
  351. infile_a_.SetNum10MsBlocksToRead(50);
  352. // Fast-forward 1 second (100 blocks) since the file starts with silence.
  353. infile_a_.FastForward(100);
  354. while (!infile_a_.EndOfFile()) {
  355. // Add 10 msec to ACM.
  356. infile_a_.Read10MsData(audio_frame);
  357. CHECK_ERROR(acm_a_->Add10MsData(audio_frame));
  358. // Verify that the received packet size matches the settings.
  359. receive_size = channel->payload_size();
  360. if (receive_size) {
  361. if ((receive_size != packet_size_bytes_) &&
  362. (packet_size_bytes_ != kVariableSize)) {
  363. error_count++;
  364. }
  365. // Verify that the timestamp is updated with expected length. The counter
  366. // is used to avoid problems when switching codec or frame size in the
  367. // test.
  368. timestamp_diff = channel->timestamp_diff();
  369. if ((counter > 10) &&
  370. (static_cast<int>(timestamp_diff) != packet_size_samples_) &&
  371. (packet_size_samples_ > -1))
  372. error_count++;
  373. }
  374. // Run received side of ACM.
  375. bool muted;
  376. CHECK_ERROR(acm_b_->PlayoutData10Ms(out_freq_hz, &audio_frame, &muted));
  377. ASSERT_FALSE(muted);
  378. // Write output speech to file.
  379. outfile_b_.Write10MsData(audio_frame.data(),
  380. audio_frame.samples_per_channel_);
  381. // Update loop counter
  382. counter++;
  383. }
  384. EXPECT_EQ(0, error_count);
  385. if (infile_a_.EndOfFile()) {
  386. infile_a_.Rewind();
  387. }
  388. }
  389. void TestAllCodecs::OpenOutFile(int test_number) {
  390. std::string filename = webrtc::test::OutputPath();
  391. rtc::StringBuilder test_number_str;
  392. test_number_str << test_number;
  393. filename += "testallcodecs_out_";
  394. filename += test_number_str.str();
  395. filename += ".pcm";
  396. outfile_b_.Open(filename, 32000, "wb");
  397. }
  398. } // namespace webrtc