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.

363 lines
11 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/iSACTest.h"
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "absl/strings/match.h"
  15. #include "api/audio_codecs/builtin_audio_decoder_factory.h"
  16. #include "api/audio_codecs/isac/audio_encoder_isac_float.h"
  17. #include "rtc_base/strings/string_builder.h"
  18. #include "rtc_base/time_utils.h"
  19. #include "system_wrappers/include/sleep.h"
  20. #include "test/gmock.h"
  21. #include "test/gtest.h"
  22. #include "test/testsupport/file_utils.h"
  23. namespace webrtc {
  24. using ::testing::AnyOf;
  25. using ::testing::Eq;
  26. using ::testing::StrCaseEq;
  27. namespace {
  28. constexpr int kISAC16kPayloadType = 103;
  29. constexpr int kISAC32kPayloadType = 104;
  30. const SdpAudioFormat kISAC16kFormat = {"ISAC", 16000, 1};
  31. const SdpAudioFormat kISAC32kFormat = {"ISAC", 32000, 1};
  32. AudioEncoderIsacFloat::Config TweakConfig(
  33. AudioEncoderIsacFloat::Config config,
  34. const ACMTestISACConfig& test_config) {
  35. if (test_config.currentRateBitPerSec > 0) {
  36. config.bit_rate = test_config.currentRateBitPerSec;
  37. }
  38. if (test_config.currentFrameSizeMsec != 0) {
  39. config.frame_size_ms = test_config.currentFrameSizeMsec;
  40. }
  41. EXPECT_THAT(config.IsOk(), Eq(true));
  42. return config;
  43. }
  44. void SetISACConfigDefault(ACMTestISACConfig& isacConfig) {
  45. isacConfig.currentRateBitPerSec = 0;
  46. isacConfig.currentFrameSizeMsec = 0;
  47. isacConfig.encodingMode = -1;
  48. isacConfig.initRateBitPerSec = 0;
  49. isacConfig.initFrameSizeInMsec = 0;
  50. isacConfig.enforceFrameSize = false;
  51. }
  52. } // namespace
  53. ISACTest::ACMTestTimer::ACMTestTimer() : _msec(0), _sec(0), _min(0), _hour(0) {
  54. return;
  55. }
  56. ISACTest::ACMTestTimer::~ACMTestTimer() {
  57. return;
  58. }
  59. void ISACTest::ACMTestTimer::Reset() {
  60. _msec = 0;
  61. _sec = 0;
  62. _min = 0;
  63. _hour = 0;
  64. return;
  65. }
  66. void ISACTest::ACMTestTimer::Tick10ms() {
  67. _msec += 10;
  68. Adjust();
  69. return;
  70. }
  71. void ISACTest::ACMTestTimer::Tick1ms() {
  72. _msec++;
  73. Adjust();
  74. return;
  75. }
  76. void ISACTest::ACMTestTimer::Tick100ms() {
  77. _msec += 100;
  78. Adjust();
  79. return;
  80. }
  81. void ISACTest::ACMTestTimer::Tick1sec() {
  82. _sec++;
  83. Adjust();
  84. return;
  85. }
  86. void ISACTest::ACMTestTimer::CurrentTimeHMS(char* currTime) {
  87. sprintf(currTime, "%4lu:%02u:%06.3f", _hour, _min,
  88. (double)_sec + (double)_msec / 1000.);
  89. return;
  90. }
  91. void ISACTest::ACMTestTimer::CurrentTime(unsigned long& h,
  92. unsigned char& m,
  93. unsigned char& s,
  94. unsigned short& ms) {
  95. h = _hour;
  96. m = _min;
  97. s = _sec;
  98. ms = _msec;
  99. return;
  100. }
  101. void ISACTest::ACMTestTimer::Adjust() {
  102. unsigned int n;
  103. if (_msec >= 1000) {
  104. n = _msec / 1000;
  105. _msec -= (1000 * n);
  106. _sec += n;
  107. }
  108. if (_sec >= 60) {
  109. n = _sec / 60;
  110. _sec -= (n * 60);
  111. _min += n;
  112. }
  113. if (_min >= 60) {
  114. n = _min / 60;
  115. _min -= (n * 60);
  116. _hour += n;
  117. }
  118. }
  119. ISACTest::ISACTest()
  120. : _acmA(AudioCodingModule::Create(
  121. AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))),
  122. _acmB(AudioCodingModule::Create(
  123. AudioCodingModule::Config(CreateBuiltinAudioDecoderFactory()))) {}
  124. ISACTest::~ISACTest() {}
  125. void ISACTest::Setup() {
  126. // Register both iSAC-wb & iSAC-swb in both sides as receiver codecs.
  127. std::map<int, SdpAudioFormat> receive_codecs = {
  128. {kISAC16kPayloadType, kISAC16kFormat},
  129. {kISAC32kPayloadType, kISAC32kFormat}};
  130. _acmA->SetReceiveCodecs(receive_codecs);
  131. _acmB->SetReceiveCodecs(receive_codecs);
  132. //--- Set A-to-B channel
  133. _channel_A2B.reset(new Channel);
  134. EXPECT_EQ(0, _acmA->RegisterTransportCallback(_channel_A2B.get()));
  135. _channel_A2B->RegisterReceiverACM(_acmB.get());
  136. //--- Set B-to-A channel
  137. _channel_B2A.reset(new Channel);
  138. EXPECT_EQ(0, _acmB->RegisterTransportCallback(_channel_B2A.get()));
  139. _channel_B2A->RegisterReceiverACM(_acmA.get());
  140. file_name_swb_ =
  141. webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm");
  142. _acmB->SetEncoder(AudioEncoderIsacFloat::MakeAudioEncoder(
  143. *AudioEncoderIsacFloat::SdpToConfig(kISAC16kFormat),
  144. kISAC16kPayloadType));
  145. _acmA->SetEncoder(AudioEncoderIsacFloat::MakeAudioEncoder(
  146. *AudioEncoderIsacFloat::SdpToConfig(kISAC32kFormat),
  147. kISAC32kPayloadType));
  148. _inFileA.Open(file_name_swb_, 32000, "rb");
  149. // Set test length to 500 ms (50 blocks of 10 ms each).
  150. _inFileA.SetNum10MsBlocksToRead(50);
  151. // Fast-forward 1 second (100 blocks) since the files start with silence.
  152. _inFileA.FastForward(100);
  153. std::string fileNameA = webrtc::test::OutputPath() + "testisac_a.pcm";
  154. std::string fileNameB = webrtc::test::OutputPath() + "testisac_b.pcm";
  155. _outFileA.Open(fileNameA, 32000, "wb");
  156. _outFileB.Open(fileNameB, 32000, "wb");
  157. while (!_inFileA.EndOfFile()) {
  158. Run10ms();
  159. }
  160. _inFileA.Close();
  161. _outFileA.Close();
  162. _outFileB.Close();
  163. }
  164. void ISACTest::Perform() {
  165. Setup();
  166. int16_t testNr = 0;
  167. ACMTestISACConfig wbISACConfig;
  168. ACMTestISACConfig swbISACConfig;
  169. SetISACConfigDefault(wbISACConfig);
  170. SetISACConfigDefault(swbISACConfig);
  171. wbISACConfig.currentRateBitPerSec = -1;
  172. swbISACConfig.currentRateBitPerSec = -1;
  173. testNr++;
  174. EncodeDecode(testNr, wbISACConfig, swbISACConfig);
  175. SetISACConfigDefault(wbISACConfig);
  176. SetISACConfigDefault(swbISACConfig);
  177. testNr++;
  178. EncodeDecode(testNr, wbISACConfig, swbISACConfig);
  179. testNr++;
  180. SwitchingSamplingRate(testNr, 4);
  181. }
  182. void ISACTest::Run10ms() {
  183. AudioFrame audioFrame;
  184. EXPECT_GT(_inFileA.Read10MsData(audioFrame), 0);
  185. EXPECT_GE(_acmA->Add10MsData(audioFrame), 0);
  186. EXPECT_GE(_acmB->Add10MsData(audioFrame), 0);
  187. bool muted;
  188. EXPECT_EQ(0, _acmA->PlayoutData10Ms(32000, &audioFrame, &muted));
  189. ASSERT_FALSE(muted);
  190. _outFileA.Write10MsData(audioFrame);
  191. EXPECT_EQ(0, _acmB->PlayoutData10Ms(32000, &audioFrame, &muted));
  192. ASSERT_FALSE(muted);
  193. _outFileB.Write10MsData(audioFrame);
  194. }
  195. void ISACTest::EncodeDecode(int testNr,
  196. ACMTestISACConfig& wbISACConfig,
  197. ACMTestISACConfig& swbISACConfig) {
  198. // Files in Side A and B
  199. _inFileA.Open(file_name_swb_, 32000, "rb", true);
  200. _inFileB.Open(file_name_swb_, 32000, "rb", true);
  201. std::string file_name_out;
  202. rtc::StringBuilder file_stream_a;
  203. rtc::StringBuilder file_stream_b;
  204. file_stream_a << webrtc::test::OutputPath();
  205. file_stream_b << webrtc::test::OutputPath();
  206. file_stream_a << "out_iSACTest_A_" << testNr << ".pcm";
  207. file_stream_b << "out_iSACTest_B_" << testNr << ".pcm";
  208. file_name_out = file_stream_a.str();
  209. _outFileA.Open(file_name_out, 32000, "wb");
  210. file_name_out = file_stream_b.str();
  211. _outFileB.Open(file_name_out, 32000, "wb");
  212. // Side A is sending super-wideband, and side B is sending wideband.
  213. _acmA->SetEncoder(AudioEncoderIsacFloat::MakeAudioEncoder(
  214. TweakConfig(*AudioEncoderIsacFloat::SdpToConfig(kISAC32kFormat),
  215. swbISACConfig),
  216. kISAC32kPayloadType));
  217. _acmB->SetEncoder(AudioEncoderIsacFloat::MakeAudioEncoder(
  218. TweakConfig(*AudioEncoderIsacFloat::SdpToConfig(kISAC16kFormat),
  219. wbISACConfig),
  220. kISAC16kPayloadType));
  221. bool adaptiveMode = false;
  222. if ((swbISACConfig.currentRateBitPerSec == -1) ||
  223. (wbISACConfig.currentRateBitPerSec == -1)) {
  224. adaptiveMode = true;
  225. }
  226. _myTimer.Reset();
  227. _channel_A2B->ResetStats();
  228. _channel_B2A->ResetStats();
  229. char currentTime[500];
  230. while (!(_inFileA.EndOfFile() || _inFileA.Rewinded())) {
  231. Run10ms();
  232. _myTimer.Tick10ms();
  233. _myTimer.CurrentTimeHMS(currentTime);
  234. }
  235. _channel_A2B->ResetStats();
  236. _channel_B2A->ResetStats();
  237. _outFileA.Close();
  238. _outFileB.Close();
  239. _inFileA.Close();
  240. _inFileB.Close();
  241. }
  242. void ISACTest::SwitchingSamplingRate(int testNr, int maxSampRateChange) {
  243. // Files in Side A
  244. _inFileA.Open(file_name_swb_, 32000, "rb");
  245. _inFileB.Open(file_name_swb_, 32000, "rb");
  246. std::string file_name_out;
  247. rtc::StringBuilder file_stream_a;
  248. rtc::StringBuilder file_stream_b;
  249. file_stream_a << webrtc::test::OutputPath();
  250. file_stream_b << webrtc::test::OutputPath();
  251. file_stream_a << "out_iSACTest_A_" << testNr << ".pcm";
  252. file_stream_b << "out_iSACTest_B_" << testNr << ".pcm";
  253. file_name_out = file_stream_a.str();
  254. _outFileA.Open(file_name_out, 32000, "wb");
  255. file_name_out = file_stream_b.str();
  256. _outFileB.Open(file_name_out, 32000, "wb");
  257. // Start with side A sending super-wideband and side B seding wideband.
  258. // Toggle sending wideband/super-wideband in this test.
  259. _acmA->SetEncoder(AudioEncoderIsacFloat::MakeAudioEncoder(
  260. *AudioEncoderIsacFloat::SdpToConfig(kISAC32kFormat),
  261. kISAC32kPayloadType));
  262. _acmB->SetEncoder(AudioEncoderIsacFloat::MakeAudioEncoder(
  263. *AudioEncoderIsacFloat::SdpToConfig(kISAC16kFormat),
  264. kISAC16kPayloadType));
  265. int numSendCodecChanged = 0;
  266. _myTimer.Reset();
  267. char currentTime[50];
  268. while (numSendCodecChanged < (maxSampRateChange << 1)) {
  269. Run10ms();
  270. _myTimer.Tick10ms();
  271. _myTimer.CurrentTimeHMS(currentTime);
  272. if (_inFileA.EndOfFile()) {
  273. if (_inFileA.SamplingFrequency() == 16000) {
  274. // Switch side A to send super-wideband.
  275. _inFileA.Close();
  276. _inFileA.Open(file_name_swb_, 32000, "rb");
  277. _acmA->SetEncoder(AudioEncoderIsacFloat::MakeAudioEncoder(
  278. *AudioEncoderIsacFloat::SdpToConfig(kISAC32kFormat),
  279. kISAC32kPayloadType));
  280. } else {
  281. // Switch side A to send wideband.
  282. _inFileA.Close();
  283. _inFileA.Open(file_name_swb_, 32000, "rb");
  284. _acmA->SetEncoder(AudioEncoderIsacFloat::MakeAudioEncoder(
  285. *AudioEncoderIsacFloat::SdpToConfig(kISAC16kFormat),
  286. kISAC16kPayloadType));
  287. }
  288. numSendCodecChanged++;
  289. }
  290. if (_inFileB.EndOfFile()) {
  291. if (_inFileB.SamplingFrequency() == 16000) {
  292. // Switch side B to send super-wideband.
  293. _inFileB.Close();
  294. _inFileB.Open(file_name_swb_, 32000, "rb");
  295. _acmB->SetEncoder(AudioEncoderIsacFloat::MakeAudioEncoder(
  296. *AudioEncoderIsacFloat::SdpToConfig(kISAC32kFormat),
  297. kISAC32kPayloadType));
  298. } else {
  299. // Switch side B to send wideband.
  300. _inFileB.Close();
  301. _inFileB.Open(file_name_swb_, 32000, "rb");
  302. _acmB->SetEncoder(AudioEncoderIsacFloat::MakeAudioEncoder(
  303. *AudioEncoderIsacFloat::SdpToConfig(kISAC16kFormat),
  304. kISAC16kPayloadType));
  305. }
  306. numSendCodecChanged++;
  307. }
  308. }
  309. _outFileA.Close();
  310. _outFileB.Close();
  311. _inFileA.Close();
  312. _inFileB.Close();
  313. }
  314. } // namespace webrtc