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.

598 lines
21 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/TestStereo.h"
  11. #include <string>
  12. #include "absl/strings/match.h"
  13. #include "api/audio_codecs/builtin_audio_decoder_factory.h"
  14. #include "api/audio_codecs/builtin_audio_encoder_factory.h"
  15. #include "modules/audio_coding/include/audio_coding_module_typedefs.h"
  16. #include "modules/include/module_common_types.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. // Class for simulating packet handling
  22. TestPackStereo::TestPackStereo()
  23. : receiver_acm_(NULL),
  24. seq_no_(0),
  25. timestamp_diff_(0),
  26. last_in_timestamp_(0),
  27. total_bytes_(0),
  28. payload_size_(0),
  29. lost_packet_(false) {}
  30. TestPackStereo::~TestPackStereo() {}
  31. void TestPackStereo::RegisterReceiverACM(AudioCodingModule* acm) {
  32. receiver_acm_ = acm;
  33. return;
  34. }
  35. int32_t TestPackStereo::SendData(const AudioFrameType frame_type,
  36. const uint8_t payload_type,
  37. const uint32_t timestamp,
  38. const uint8_t* payload_data,
  39. const size_t payload_size) {
  40. RTPHeader rtp_header;
  41. int32_t status = 0;
  42. rtp_header.markerBit = false;
  43. rtp_header.ssrc = 0;
  44. rtp_header.sequenceNumber = seq_no_++;
  45. rtp_header.payloadType = payload_type;
  46. rtp_header.timestamp = timestamp;
  47. if (frame_type == AudioFrameType::kEmptyFrame) {
  48. // Skip this frame
  49. return 0;
  50. }
  51. if (lost_packet_ == false) {
  52. status =
  53. receiver_acm_->IncomingPacket(payload_data, payload_size, rtp_header);
  54. if (frame_type != AudioFrameType::kAudioFrameCN) {
  55. payload_size_ = static_cast<int>(payload_size);
  56. } else {
  57. payload_size_ = -1;
  58. }
  59. timestamp_diff_ = timestamp - last_in_timestamp_;
  60. last_in_timestamp_ = timestamp;
  61. total_bytes_ += payload_size;
  62. }
  63. return status;
  64. }
  65. uint16_t TestPackStereo::payload_size() {
  66. return static_cast<uint16_t>(payload_size_);
  67. }
  68. uint32_t TestPackStereo::timestamp_diff() {
  69. return timestamp_diff_;
  70. }
  71. void TestPackStereo::reset_payload_size() {
  72. payload_size_ = 0;
  73. }
  74. void TestPackStereo::set_codec_mode(enum StereoMonoMode mode) {
  75. codec_mode_ = mode;
  76. }
  77. void TestPackStereo::set_lost_packet(bool lost) {
  78. lost_packet_ = lost;
  79. }
  80. TestStereo::TestStereo()
  81. : acm_a_(AudioCodingModule::Create(
  82. AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
  83. acm_b_(AudioCodingModule::Create(
  84. AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
  85. channel_a2b_(NULL),
  86. test_cntr_(0),
  87. pack_size_samp_(0),
  88. pack_size_bytes_(0),
  89. counter_(0) {}
  90. TestStereo::~TestStereo() {
  91. if (channel_a2b_ != NULL) {
  92. delete channel_a2b_;
  93. channel_a2b_ = NULL;
  94. }
  95. }
  96. void TestStereo::Perform() {
  97. uint16_t frequency_hz;
  98. int audio_channels;
  99. int codec_channels;
  100. // Open both mono and stereo test files in 32 kHz.
  101. const std::string file_name_stereo =
  102. webrtc::test::ResourcePath("audio_coding/teststereo32kHz", "pcm");
  103. const std::string file_name_mono =
  104. webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
  105. frequency_hz = 32000;
  106. in_file_stereo_ = new PCMFile();
  107. in_file_mono_ = new PCMFile();
  108. in_file_stereo_->Open(file_name_stereo, frequency_hz, "rb");
  109. in_file_stereo_->ReadStereo(true);
  110. in_file_mono_->Open(file_name_mono, frequency_hz, "rb");
  111. in_file_mono_->ReadStereo(false);
  112. // Create and initialize two ACMs, one for each side of a one-to-one call.
  113. ASSERT_TRUE((acm_a_.get() != NULL) && (acm_b_.get() != NULL));
  114. EXPECT_EQ(0, acm_a_->InitializeReceiver());
  115. EXPECT_EQ(0, acm_b_->InitializeReceiver());
  116. acm_b_->SetReceiveCodecs({{103, {"ISAC", 16000, 1}},
  117. {104, {"ISAC", 32000, 1}},
  118. {107, {"L16", 8000, 1}},
  119. {108, {"L16", 16000, 1}},
  120. {109, {"L16", 32000, 1}},
  121. {111, {"L16", 8000, 2}},
  122. {112, {"L16", 16000, 2}},
  123. {113, {"L16", 32000, 2}},
  124. {0, {"PCMU", 8000, 1}},
  125. {110, {"PCMU", 8000, 2}},
  126. {8, {"PCMA", 8000, 1}},
  127. {118, {"PCMA", 8000, 2}},
  128. {102, {"ILBC", 8000, 1}},
  129. {9, {"G722", 8000, 1}},
  130. {119, {"G722", 8000, 2}},
  131. {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}},
  132. {13, {"CN", 8000, 1}},
  133. {98, {"CN", 16000, 1}},
  134. {99, {"CN", 32000, 1}}});
  135. // Create and connect the channel.
  136. channel_a2b_ = new TestPackStereo;
  137. EXPECT_EQ(0, acm_a_->RegisterTransportCallback(channel_a2b_));
  138. channel_a2b_->RegisterReceiverACM(acm_b_.get());
  139. char codec_pcma_temp[] = "PCMA";
  140. RegisterSendCodec('A', codec_pcma_temp, 8000, 64000, 80, 2);
  141. //
  142. // Test Stereo-To-Stereo for all codecs.
  143. //
  144. audio_channels = 2;
  145. codec_channels = 2;
  146. // All codecs are tested for all allowed sampling frequencies, rates and
  147. // packet sizes.
  148. channel_a2b_->set_codec_mode(kStereo);
  149. test_cntr_++;
  150. OpenOutFile(test_cntr_);
  151. char codec_g722[] = "G722";
  152. RegisterSendCodec('A', codec_g722, 16000, 64000, 160, codec_channels);
  153. Run(channel_a2b_, audio_channels, codec_channels);
  154. RegisterSendCodec('A', codec_g722, 16000, 64000, 320, codec_channels);
  155. Run(channel_a2b_, audio_channels, codec_channels);
  156. RegisterSendCodec('A', codec_g722, 16000, 64000, 480, codec_channels);
  157. Run(channel_a2b_, audio_channels, codec_channels);
  158. RegisterSendCodec('A', codec_g722, 16000, 64000, 640, codec_channels);
  159. Run(channel_a2b_, audio_channels, codec_channels);
  160. RegisterSendCodec('A', codec_g722, 16000, 64000, 800, codec_channels);
  161. Run(channel_a2b_, audio_channels, codec_channels);
  162. RegisterSendCodec('A', codec_g722, 16000, 64000, 960, codec_channels);
  163. Run(channel_a2b_, audio_channels, codec_channels);
  164. out_file_.Close();
  165. channel_a2b_->set_codec_mode(kStereo);
  166. test_cntr_++;
  167. OpenOutFile(test_cntr_);
  168. char codec_l16[] = "L16";
  169. RegisterSendCodec('A', codec_l16, 8000, 128000, 80, codec_channels);
  170. Run(channel_a2b_, audio_channels, codec_channels);
  171. RegisterSendCodec('A', codec_l16, 8000, 128000, 160, codec_channels);
  172. Run(channel_a2b_, audio_channels, codec_channels);
  173. RegisterSendCodec('A', codec_l16, 8000, 128000, 240, codec_channels);
  174. Run(channel_a2b_, audio_channels, codec_channels);
  175. RegisterSendCodec('A', codec_l16, 8000, 128000, 320, codec_channels);
  176. Run(channel_a2b_, audio_channels, codec_channels);
  177. out_file_.Close();
  178. test_cntr_++;
  179. OpenOutFile(test_cntr_);
  180. RegisterSendCodec('A', codec_l16, 16000, 256000, 160, codec_channels);
  181. Run(channel_a2b_, audio_channels, codec_channels);
  182. RegisterSendCodec('A', codec_l16, 16000, 256000, 320, codec_channels);
  183. Run(channel_a2b_, audio_channels, codec_channels);
  184. RegisterSendCodec('A', codec_l16, 16000, 256000, 480, codec_channels);
  185. Run(channel_a2b_, audio_channels, codec_channels);
  186. RegisterSendCodec('A', codec_l16, 16000, 256000, 640, codec_channels);
  187. Run(channel_a2b_, audio_channels, codec_channels);
  188. out_file_.Close();
  189. test_cntr_++;
  190. OpenOutFile(test_cntr_);
  191. RegisterSendCodec('A', codec_l16, 32000, 512000, 320, codec_channels);
  192. Run(channel_a2b_, audio_channels, codec_channels);
  193. RegisterSendCodec('A', codec_l16, 32000, 512000, 640, codec_channels);
  194. Run(channel_a2b_, audio_channels, codec_channels);
  195. out_file_.Close();
  196. #ifdef PCMA_AND_PCMU
  197. channel_a2b_->set_codec_mode(kStereo);
  198. audio_channels = 2;
  199. codec_channels = 2;
  200. test_cntr_++;
  201. OpenOutFile(test_cntr_);
  202. char codec_pcma[] = "PCMA";
  203. RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, codec_channels);
  204. Run(channel_a2b_, audio_channels, codec_channels);
  205. RegisterSendCodec('A', codec_pcma, 8000, 64000, 160, codec_channels);
  206. Run(channel_a2b_, audio_channels, codec_channels);
  207. RegisterSendCodec('A', codec_pcma, 8000, 64000, 240, codec_channels);
  208. Run(channel_a2b_, audio_channels, codec_channels);
  209. RegisterSendCodec('A', codec_pcma, 8000, 64000, 320, codec_channels);
  210. Run(channel_a2b_, audio_channels, codec_channels);
  211. RegisterSendCodec('A', codec_pcma, 8000, 64000, 400, codec_channels);
  212. Run(channel_a2b_, audio_channels, codec_channels);
  213. RegisterSendCodec('A', codec_pcma, 8000, 64000, 480, codec_channels);
  214. Run(channel_a2b_, audio_channels, codec_channels);
  215. out_file_.Close();
  216. test_cntr_++;
  217. OpenOutFile(test_cntr_);
  218. char codec_pcmu[] = "PCMU";
  219. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, codec_channels);
  220. Run(channel_a2b_, audio_channels, codec_channels);
  221. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 160, codec_channels);
  222. Run(channel_a2b_, audio_channels, codec_channels);
  223. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 240, codec_channels);
  224. Run(channel_a2b_, audio_channels, codec_channels);
  225. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 320, codec_channels);
  226. Run(channel_a2b_, audio_channels, codec_channels);
  227. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 400, codec_channels);
  228. Run(channel_a2b_, audio_channels, codec_channels);
  229. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 480, codec_channels);
  230. Run(channel_a2b_, audio_channels, codec_channels);
  231. out_file_.Close();
  232. #endif
  233. #ifdef WEBRTC_CODEC_OPUS
  234. channel_a2b_->set_codec_mode(kStereo);
  235. audio_channels = 2;
  236. codec_channels = 2;
  237. test_cntr_++;
  238. OpenOutFile(test_cntr_);
  239. char codec_opus[] = "opus";
  240. // Run Opus with 10 ms frame size.
  241. RegisterSendCodec('A', codec_opus, 48000, 64000, 480, codec_channels);
  242. Run(channel_a2b_, audio_channels, codec_channels);
  243. // Run Opus with 20 ms frame size.
  244. RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 2, codec_channels);
  245. Run(channel_a2b_, audio_channels, codec_channels);
  246. // Run Opus with 40 ms frame size.
  247. RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 4, codec_channels);
  248. Run(channel_a2b_, audio_channels, codec_channels);
  249. // Run Opus with 60 ms frame size.
  250. RegisterSendCodec('A', codec_opus, 48000, 64000, 480 * 6, codec_channels);
  251. Run(channel_a2b_, audio_channels, codec_channels);
  252. // Run Opus with 20 ms frame size and different bitrates.
  253. RegisterSendCodec('A', codec_opus, 48000, 40000, 960, codec_channels);
  254. Run(channel_a2b_, audio_channels, codec_channels);
  255. RegisterSendCodec('A', codec_opus, 48000, 510000, 960, codec_channels);
  256. Run(channel_a2b_, audio_channels, codec_channels);
  257. out_file_.Close();
  258. #endif
  259. //
  260. // Test Mono-To-Stereo for all codecs.
  261. //
  262. audio_channels = 1;
  263. codec_channels = 2;
  264. test_cntr_++;
  265. channel_a2b_->set_codec_mode(kStereo);
  266. OpenOutFile(test_cntr_);
  267. RegisterSendCodec('A', codec_g722, 16000, 64000, 160, codec_channels);
  268. Run(channel_a2b_, audio_channels, codec_channels);
  269. out_file_.Close();
  270. test_cntr_++;
  271. channel_a2b_->set_codec_mode(kStereo);
  272. OpenOutFile(test_cntr_);
  273. RegisterSendCodec('A', codec_l16, 8000, 128000, 80, codec_channels);
  274. Run(channel_a2b_, audio_channels, codec_channels);
  275. out_file_.Close();
  276. test_cntr_++;
  277. OpenOutFile(test_cntr_);
  278. RegisterSendCodec('A', codec_l16, 16000, 256000, 160, codec_channels);
  279. Run(channel_a2b_, audio_channels, codec_channels);
  280. out_file_.Close();
  281. test_cntr_++;
  282. OpenOutFile(test_cntr_);
  283. RegisterSendCodec('A', codec_l16, 32000, 512000, 320, codec_channels);
  284. Run(channel_a2b_, audio_channels, codec_channels);
  285. out_file_.Close();
  286. #ifdef PCMA_AND_PCMU
  287. test_cntr_++;
  288. channel_a2b_->set_codec_mode(kStereo);
  289. OpenOutFile(test_cntr_);
  290. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, codec_channels);
  291. Run(channel_a2b_, audio_channels, codec_channels);
  292. RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, codec_channels);
  293. Run(channel_a2b_, audio_channels, codec_channels);
  294. out_file_.Close();
  295. #endif
  296. #ifdef WEBRTC_CODEC_OPUS
  297. // Keep encode and decode in stereo.
  298. test_cntr_++;
  299. channel_a2b_->set_codec_mode(kStereo);
  300. OpenOutFile(test_cntr_);
  301. RegisterSendCodec('A', codec_opus, 48000, 64000, 960, codec_channels);
  302. Run(channel_a2b_, audio_channels, codec_channels);
  303. // Encode in mono, decode in stereo mode.
  304. RegisterSendCodec('A', codec_opus, 48000, 64000, 960, 1);
  305. Run(channel_a2b_, audio_channels, codec_channels);
  306. out_file_.Close();
  307. #endif
  308. //
  309. // Test Stereo-To-Mono for all codecs.
  310. //
  311. audio_channels = 2;
  312. codec_channels = 1;
  313. channel_a2b_->set_codec_mode(kMono);
  314. // Run stereo audio and mono codec.
  315. test_cntr_++;
  316. OpenOutFile(test_cntr_);
  317. RegisterSendCodec('A', codec_g722, 16000, 64000, 160, codec_channels);
  318. Run(channel_a2b_, audio_channels, codec_channels);
  319. out_file_.Close();
  320. test_cntr_++;
  321. OpenOutFile(test_cntr_);
  322. RegisterSendCodec('A', codec_l16, 8000, 128000, 80, codec_channels);
  323. Run(channel_a2b_, audio_channels, codec_channels);
  324. out_file_.Close();
  325. test_cntr_++;
  326. OpenOutFile(test_cntr_);
  327. RegisterSendCodec('A', codec_l16, 16000, 256000, 160, codec_channels);
  328. Run(channel_a2b_, audio_channels, codec_channels);
  329. out_file_.Close();
  330. test_cntr_++;
  331. OpenOutFile(test_cntr_);
  332. RegisterSendCodec('A', codec_l16, 32000, 512000, 320, codec_channels);
  333. Run(channel_a2b_, audio_channels, codec_channels);
  334. out_file_.Close();
  335. #ifdef PCMA_AND_PCMU
  336. test_cntr_++;
  337. OpenOutFile(test_cntr_);
  338. RegisterSendCodec('A', codec_pcmu, 8000, 64000, 80, codec_channels);
  339. Run(channel_a2b_, audio_channels, codec_channels);
  340. RegisterSendCodec('A', codec_pcma, 8000, 64000, 80, codec_channels);
  341. Run(channel_a2b_, audio_channels, codec_channels);
  342. out_file_.Close();
  343. #endif
  344. #ifdef WEBRTC_CODEC_OPUS
  345. test_cntr_++;
  346. OpenOutFile(test_cntr_);
  347. // Encode and decode in mono.
  348. RegisterSendCodec('A', codec_opus, 48000, 32000, 960, codec_channels);
  349. acm_b_->SetReceiveCodecs({{120, {"OPUS", 48000, 2}}});
  350. Run(channel_a2b_, audio_channels, codec_channels);
  351. // Encode in stereo, decode in mono.
  352. RegisterSendCodec('A', codec_opus, 48000, 32000, 960, 2);
  353. Run(channel_a2b_, audio_channels, codec_channels);
  354. out_file_.Close();
  355. // Test switching between decoding mono and stereo for Opus.
  356. // Decode in mono.
  357. test_cntr_++;
  358. OpenOutFile(test_cntr_);
  359. Run(channel_a2b_, audio_channels, codec_channels);
  360. out_file_.Close();
  361. // Decode in stereo.
  362. test_cntr_++;
  363. OpenOutFile(test_cntr_);
  364. acm_b_->SetReceiveCodecs({{120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}});
  365. Run(channel_a2b_, audio_channels, 2);
  366. out_file_.Close();
  367. // Decode in mono.
  368. test_cntr_++;
  369. OpenOutFile(test_cntr_);
  370. acm_b_->SetReceiveCodecs({{120, {"OPUS", 48000, 2}}});
  371. Run(channel_a2b_, audio_channels, codec_channels);
  372. out_file_.Close();
  373. #endif
  374. // Delete the file pointers.
  375. delete in_file_stereo_;
  376. delete in_file_mono_;
  377. }
  378. // Register Codec to use in the test
  379. //
  380. // Input: side - which ACM to use, 'A' or 'B'
  381. // codec_name - name to use when register the codec
  382. // sampling_freq_hz - sampling frequency in Herz
  383. // rate - bitrate in bytes
  384. // pack_size - packet size in samples
  385. // channels - number of channels; 1 for mono, 2 for stereo
  386. void TestStereo::RegisterSendCodec(char side,
  387. char* codec_name,
  388. int32_t sampling_freq_hz,
  389. int rate,
  390. int pack_size,
  391. int channels) {
  392. // Store packet size in samples, used to validate the received packet
  393. pack_size_samp_ = pack_size;
  394. // Store the expected packet size in bytes, used to validate the received
  395. // packet. Add 0.875 to always round up to a whole byte.
  396. pack_size_bytes_ = (uint16_t)(static_cast<float>(pack_size * rate) /
  397. static_cast<float>(sampling_freq_hz * 8) +
  398. 0.875);
  399. // Set pointer to the ACM where to register the codec
  400. AudioCodingModule* my_acm = NULL;
  401. switch (side) {
  402. case 'A': {
  403. my_acm = acm_a_.get();
  404. break;
  405. }
  406. case 'B': {
  407. my_acm = acm_b_.get();
  408. break;
  409. }
  410. default:
  411. break;
  412. }
  413. ASSERT_TRUE(my_acm != NULL);
  414. auto encoder_factory = CreateBuiltinAudioEncoderFactory();
  415. const int clockrate_hz = absl::EqualsIgnoreCase(codec_name, "g722")
  416. ? sampling_freq_hz / 2
  417. : sampling_freq_hz;
  418. const std::string ptime = rtc::ToString(rtc::CheckedDivExact(
  419. pack_size, rtc::CheckedDivExact(sampling_freq_hz, 1000)));
  420. SdpAudioFormat::Parameters params = {{"ptime", ptime}};
  421. RTC_CHECK(channels == 1 || channels == 2);
  422. if (absl::EqualsIgnoreCase(codec_name, "opus")) {
  423. if (channels == 2) {
  424. params["stereo"] = "1";
  425. }
  426. channels = 2;
  427. params["maxaveragebitrate"] = rtc::ToString(rate);
  428. }
  429. constexpr int payload_type = 17;
  430. auto encoder = encoder_factory->MakeAudioEncoder(
  431. payload_type, SdpAudioFormat(codec_name, clockrate_hz, channels, params),
  432. absl::nullopt);
  433. EXPECT_NE(nullptr, encoder);
  434. my_acm->SetEncoder(std::move(encoder));
  435. send_codec_name_ = codec_name;
  436. }
  437. void TestStereo::Run(TestPackStereo* channel,
  438. int in_channels,
  439. int out_channels,
  440. int percent_loss) {
  441. AudioFrame audio_frame;
  442. int32_t out_freq_hz_b = out_file_.SamplingFrequency();
  443. uint16_t rec_size;
  444. uint32_t time_stamp_diff;
  445. channel->reset_payload_size();
  446. int error_count = 0;
  447. int variable_bytes = 0;
  448. int variable_packets = 0;
  449. // Set test length to 500 ms (50 blocks of 10 ms each).
  450. in_file_mono_->SetNum10MsBlocksToRead(50);
  451. in_file_stereo_->SetNum10MsBlocksToRead(50);
  452. // Fast-forward 1 second (100 blocks) since the files start with silence.
  453. in_file_stereo_->FastForward(100);
  454. in_file_mono_->FastForward(100);
  455. while (1) {
  456. // Simulate packet loss by setting |packet_loss_| to "true" in
  457. // |percent_loss| percent of the loops.
  458. if (percent_loss > 0) {
  459. if (counter_ == floor((100 / percent_loss) + 0.5)) {
  460. counter_ = 0;
  461. channel->set_lost_packet(true);
  462. } else {
  463. channel->set_lost_packet(false);
  464. }
  465. counter_++;
  466. }
  467. // Add 10 msec to ACM
  468. if (in_channels == 1) {
  469. if (in_file_mono_->EndOfFile()) {
  470. break;
  471. }
  472. in_file_mono_->Read10MsData(audio_frame);
  473. } else {
  474. if (in_file_stereo_->EndOfFile()) {
  475. break;
  476. }
  477. in_file_stereo_->Read10MsData(audio_frame);
  478. }
  479. EXPECT_GE(acm_a_->Add10MsData(audio_frame), 0);
  480. // Verify that the received packet size matches the settings.
  481. rec_size = channel->payload_size();
  482. if ((0 < rec_size) & (rec_size < 65535)) {
  483. if (strcmp(send_codec_name_, "opus") == 0) {
  484. // Opus is a variable rate codec, hence calculate the average packet
  485. // size, and later make sure the average is in the right range.
  486. variable_bytes += rec_size;
  487. variable_packets++;
  488. } else {
  489. // For fixed rate codecs, check that packet size is correct.
  490. if ((rec_size != pack_size_bytes_ * out_channels) &&
  491. (pack_size_bytes_ < 65535)) {
  492. error_count++;
  493. }
  494. }
  495. // Verify that the timestamp is updated with expected length
  496. time_stamp_diff = channel->timestamp_diff();
  497. if ((counter_ > 10) && (time_stamp_diff != pack_size_samp_)) {
  498. error_count++;
  499. }
  500. }
  501. // Run receive side of ACM
  502. bool muted;
  503. EXPECT_EQ(0, acm_b_->PlayoutData10Ms(out_freq_hz_b, &audio_frame, &muted));
  504. ASSERT_FALSE(muted);
  505. // Write output speech to file
  506. out_file_.Write10MsData(
  507. audio_frame.data(),
  508. audio_frame.samples_per_channel_ * audio_frame.num_channels_);
  509. }
  510. EXPECT_EQ(0, error_count);
  511. // Check that packet size is in the right range for variable rate codecs,
  512. // such as Opus.
  513. if (variable_packets > 0) {
  514. variable_bytes /= variable_packets;
  515. EXPECT_NEAR(variable_bytes, pack_size_bytes_, 18);
  516. }
  517. if (in_file_mono_->EndOfFile()) {
  518. in_file_mono_->Rewind();
  519. }
  520. if (in_file_stereo_->EndOfFile()) {
  521. in_file_stereo_->Rewind();
  522. }
  523. // Reset in case we ended with a lost packet
  524. channel->set_lost_packet(false);
  525. }
  526. void TestStereo::OpenOutFile(int16_t test_number) {
  527. std::string file_name;
  528. rtc::StringBuilder file_stream;
  529. file_stream << webrtc::test::OutputPath() << "teststereo_out_" << test_number
  530. << ".pcm";
  531. file_name = file_stream.str();
  532. out_file_.Open(file_name, 32000, "wb");
  533. }
  534. } // namespace webrtc