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.

593 lines
25 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2018 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. #import <XCTest/XCTest.h>
  11. #if defined(WEBRTC_IOS)
  12. #import "sdk/objc/native/api/audio_device_module.h"
  13. #endif
  14. #include "api/scoped_refptr.h"
  15. typedef int32_t(^NeedMorePlayDataBlock)(const size_t nSamples,
  16. const size_t nBytesPerSample,
  17. const size_t nChannels,
  18. const uint32_t samplesPerSec,
  19. void* audioSamples,
  20. size_t& nSamplesOut,
  21. int64_t* elapsed_time_ms,
  22. int64_t* ntp_time_ms);
  23. typedef int32_t(^RecordedDataIsAvailableBlock)(const void* audioSamples,
  24. const size_t nSamples,
  25. const size_t nBytesPerSample,
  26. const size_t nChannels,
  27. const uint32_t samplesPerSec,
  28. const uint32_t totalDelayMS,
  29. const int32_t clockDrift,
  30. const uint32_t currentMicLevel,
  31. const bool keyPressed,
  32. uint32_t& newMicLevel);
  33. // This class implements the AudioTransport API and forwards all methods to the appropriate blocks.
  34. class MockAudioTransport : public webrtc::AudioTransport {
  35. public:
  36. MockAudioTransport() {}
  37. ~MockAudioTransport() override {}
  38. void expectNeedMorePlayData(NeedMorePlayDataBlock block) {
  39. needMorePlayDataBlock = block;
  40. }
  41. void expectRecordedDataIsAvailable(RecordedDataIsAvailableBlock block) {
  42. recordedDataIsAvailableBlock = block;
  43. }
  44. int32_t NeedMorePlayData(const size_t nSamples,
  45. const size_t nBytesPerSample,
  46. const size_t nChannels,
  47. const uint32_t samplesPerSec,
  48. void* audioSamples,
  49. size_t& nSamplesOut,
  50. int64_t* elapsed_time_ms,
  51. int64_t* ntp_time_ms) override {
  52. return needMorePlayDataBlock(nSamples,
  53. nBytesPerSample,
  54. nChannels,
  55. samplesPerSec,
  56. audioSamples,
  57. nSamplesOut,
  58. elapsed_time_ms,
  59. ntp_time_ms);
  60. }
  61. int32_t RecordedDataIsAvailable(const void* audioSamples,
  62. const size_t nSamples,
  63. const size_t nBytesPerSample,
  64. const size_t nChannels,
  65. const uint32_t samplesPerSec,
  66. const uint32_t totalDelayMS,
  67. const int32_t clockDrift,
  68. const uint32_t currentMicLevel,
  69. const bool keyPressed,
  70. uint32_t& newMicLevel) override {
  71. return recordedDataIsAvailableBlock(audioSamples,
  72. nSamples,
  73. nBytesPerSample,
  74. nChannels,
  75. samplesPerSec,
  76. totalDelayMS,
  77. clockDrift,
  78. currentMicLevel,
  79. keyPressed,
  80. newMicLevel);
  81. }
  82. void PullRenderData(int bits_per_sample,
  83. int sample_rate,
  84. size_t number_of_channels,
  85. size_t number_of_frames,
  86. void* audio_data,
  87. int64_t* elapsed_time_ms,
  88. int64_t* ntp_time_ms) override {}
  89. private:
  90. NeedMorePlayDataBlock needMorePlayDataBlock;
  91. RecordedDataIsAvailableBlock recordedDataIsAvailableBlock;
  92. };
  93. // Number of callbacks (input or output) the tests waits for before we set
  94. // an event indicating that the test was OK.
  95. static const NSUInteger kNumCallbacks = 10;
  96. // Max amount of time we wait for an event to be set while counting callbacks.
  97. static const NSTimeInterval kTestTimeOutInSec = 20.0;
  98. // Number of bits per PCM audio sample.
  99. static const NSUInteger kBitsPerSample = 16;
  100. // Number of bytes per PCM audio sample.
  101. static const NSUInteger kBytesPerSample = kBitsPerSample / 8;
  102. // Average number of audio callbacks per second assuming 10ms packet size.
  103. static const NSUInteger kNumCallbacksPerSecond = 100;
  104. // Play out a test file during this time (unit is in seconds).
  105. static const NSUInteger kFilePlayTimeInSec = 15;
  106. // Run the full-duplex test during this time (unit is in seconds).
  107. // Note that first |kNumIgnoreFirstCallbacks| are ignored.
  108. static const NSUInteger kFullDuplexTimeInSec = 10;
  109. // Wait for the callback sequence to stabilize by ignoring this amount of the
  110. // initial callbacks (avoids initial FIFO access).
  111. // Only used in the RunPlayoutAndRecordingInFullDuplex test.
  112. static const NSUInteger kNumIgnoreFirstCallbacks = 50;
  113. @interface RTCAudioDeviceModuleTests : XCTestCase {
  114. rtc::scoped_refptr<webrtc::AudioDeviceModule> audioDeviceModule;
  115. MockAudioTransport mock;
  116. }
  117. @property(nonatomic, assign) webrtc::AudioParameters playoutParameters;
  118. @property(nonatomic, assign) webrtc::AudioParameters recordParameters;
  119. @end
  120. @implementation RTCAudioDeviceModuleTests
  121. @synthesize playoutParameters;
  122. @synthesize recordParameters;
  123. - (void)setUp {
  124. [super setUp];
  125. audioDeviceModule = webrtc::CreateAudioDeviceModule();
  126. XCTAssertEqual(0, audioDeviceModule->Init());
  127. XCTAssertEqual(0, audioDeviceModule->GetPlayoutAudioParameters(&playoutParameters));
  128. XCTAssertEqual(0, audioDeviceModule->GetRecordAudioParameters(&recordParameters));
  129. }
  130. - (void)tearDown {
  131. XCTAssertEqual(0, audioDeviceModule->Terminate());
  132. audioDeviceModule = nullptr;
  133. [super tearDown];
  134. }
  135. - (void)startPlayout {
  136. XCTAssertFalse(audioDeviceModule->Playing());
  137. XCTAssertEqual(0, audioDeviceModule->InitPlayout());
  138. XCTAssertTrue(audioDeviceModule->PlayoutIsInitialized());
  139. XCTAssertEqual(0, audioDeviceModule->StartPlayout());
  140. XCTAssertTrue(audioDeviceModule->Playing());
  141. }
  142. - (void)stopPlayout {
  143. XCTAssertEqual(0, audioDeviceModule->StopPlayout());
  144. XCTAssertFalse(audioDeviceModule->Playing());
  145. }
  146. - (void)startRecording{
  147. XCTAssertFalse(audioDeviceModule->Recording());
  148. XCTAssertEqual(0, audioDeviceModule->InitRecording());
  149. XCTAssertTrue(audioDeviceModule->RecordingIsInitialized());
  150. XCTAssertEqual(0, audioDeviceModule->StartRecording());
  151. XCTAssertTrue(audioDeviceModule->Recording());
  152. }
  153. - (void)stopRecording{
  154. XCTAssertEqual(0, audioDeviceModule->StopRecording());
  155. XCTAssertFalse(audioDeviceModule->Recording());
  156. }
  157. - (NSURL*)fileURLForSampleRate:(int)sampleRate {
  158. XCTAssertTrue(sampleRate == 48000 || sampleRate == 44100 || sampleRate == 16000);
  159. NSString *filename = [NSString stringWithFormat:@"audio_short%d", sampleRate / 1000];
  160. NSURL *url = [[NSBundle mainBundle] URLForResource:filename withExtension:@"pcm"];
  161. XCTAssertNotNil(url);
  162. return url;
  163. }
  164. #pragma mark - Tests
  165. - (void)testConstructDestruct {
  166. // Using the test fixture to create and destruct the audio device module.
  167. }
  168. - (void)testInitTerminate {
  169. // Initialization is part of the test fixture.
  170. XCTAssertTrue(audioDeviceModule->Initialized());
  171. XCTAssertEqual(0, audioDeviceModule->Terminate());
  172. XCTAssertFalse(audioDeviceModule->Initialized());
  173. }
  174. // Tests that playout can be initiated, started and stopped. No audio callback
  175. // is registered in this test.
  176. - (void)testStartStopPlayout {
  177. [self startPlayout];
  178. [self stopPlayout];
  179. [self startPlayout];
  180. [self stopPlayout];
  181. }
  182. // Tests that recording can be initiated, started and stopped. No audio callback
  183. // is registered in this test.
  184. - (void)testStartStopRecording {
  185. [self startRecording];
  186. [self stopRecording];
  187. [self startRecording];
  188. [self stopRecording];
  189. }
  190. // Verify that calling StopPlayout() will leave us in an uninitialized state
  191. // which will require a new call to InitPlayout(). This test does not call
  192. // StartPlayout() while being uninitialized since doing so will hit a
  193. // RTC_DCHECK.
  194. - (void)testStopPlayoutRequiresInitToRestart {
  195. XCTAssertEqual(0, audioDeviceModule->InitPlayout());
  196. XCTAssertEqual(0, audioDeviceModule->StartPlayout());
  197. XCTAssertEqual(0, audioDeviceModule->StopPlayout());
  198. XCTAssertFalse(audioDeviceModule->PlayoutIsInitialized());
  199. }
  200. // Verify that we can create two ADMs and start playing on the second ADM.
  201. // Only the first active instance shall activate an audio session and the
  202. // last active instance shall deactivate the audio session. The test does not
  203. // explicitly verify correct audio session calls but instead focuses on
  204. // ensuring that audio starts for both ADMs.
  205. - (void)testStartPlayoutOnTwoInstances {
  206. // Create and initialize a second/extra ADM instance. The default ADM is
  207. // created by the test harness.
  208. rtc::scoped_refptr<webrtc::AudioDeviceModule> secondAudioDeviceModule =
  209. webrtc::CreateAudioDeviceModule();
  210. XCTAssertNotEqual(secondAudioDeviceModule.get(), nullptr);
  211. XCTAssertEqual(0, secondAudioDeviceModule->Init());
  212. // Start playout for the default ADM but don't wait here. Instead use the
  213. // upcoming second stream for that. We set the same expectation on number
  214. // of callbacks as for the second stream.
  215. mock.expectNeedMorePlayData(^int32_t(const size_t nSamples,
  216. const size_t nBytesPerSample,
  217. const size_t nChannels,
  218. const uint32_t samplesPerSec,
  219. void *audioSamples,
  220. size_t &nSamplesOut,
  221. int64_t *elapsed_time_ms,
  222. int64_t *ntp_time_ms) {
  223. nSamplesOut = nSamples;
  224. XCTAssertEqual(nSamples, self.playoutParameters.frames_per_10ms_buffer());
  225. XCTAssertEqual(nBytesPerSample, kBytesPerSample);
  226. XCTAssertEqual(nChannels, self.playoutParameters.channels());
  227. XCTAssertEqual((int)samplesPerSec, self.playoutParameters.sample_rate());
  228. XCTAssertNotEqual((void*)NULL, audioSamples);
  229. return 0;
  230. });
  231. XCTAssertEqual(0, audioDeviceModule->RegisterAudioCallback(&mock));
  232. [self startPlayout];
  233. // Initialize playout for the second ADM. If all is OK, the second ADM shall
  234. // reuse the audio session activated when the first ADM started playing.
  235. // This call will also ensure that we avoid a problem related to initializing
  236. // two different audio unit instances back to back (see webrtc:5166 for
  237. // details).
  238. XCTAssertEqual(0, secondAudioDeviceModule->InitPlayout());
  239. XCTAssertTrue(secondAudioDeviceModule->PlayoutIsInitialized());
  240. // Start playout for the second ADM and verify that it starts as intended.
  241. // Passing this test ensures that initialization of the second audio unit
  242. // has been done successfully and that there is no conflict with the already
  243. // playing first ADM.
  244. XCTestExpectation *playoutExpectation = [self expectationWithDescription:@"NeedMorePlayoutData"];
  245. __block int num_callbacks = 0;
  246. MockAudioTransport mock2;
  247. mock2.expectNeedMorePlayData(^int32_t(const size_t nSamples,
  248. const size_t nBytesPerSample,
  249. const size_t nChannels,
  250. const uint32_t samplesPerSec,
  251. void *audioSamples,
  252. size_t &nSamplesOut,
  253. int64_t *elapsed_time_ms,
  254. int64_t *ntp_time_ms) {
  255. nSamplesOut = nSamples;
  256. XCTAssertEqual(nSamples, self.playoutParameters.frames_per_10ms_buffer());
  257. XCTAssertEqual(nBytesPerSample, kBytesPerSample);
  258. XCTAssertEqual(nChannels, self.playoutParameters.channels());
  259. XCTAssertEqual((int)samplesPerSec, self.playoutParameters.sample_rate());
  260. XCTAssertNotEqual((void*)NULL, audioSamples);
  261. if (++num_callbacks == kNumCallbacks) {
  262. [playoutExpectation fulfill];
  263. }
  264. return 0;
  265. });
  266. XCTAssertEqual(0, secondAudioDeviceModule->RegisterAudioCallback(&mock2));
  267. XCTAssertEqual(0, secondAudioDeviceModule->StartPlayout());
  268. XCTAssertTrue(secondAudioDeviceModule->Playing());
  269. [self waitForExpectationsWithTimeout:kTestTimeOutInSec handler:nil];
  270. [self stopPlayout];
  271. XCTAssertEqual(0, secondAudioDeviceModule->StopPlayout());
  272. XCTAssertFalse(secondAudioDeviceModule->Playing());
  273. XCTAssertFalse(secondAudioDeviceModule->PlayoutIsInitialized());
  274. XCTAssertEqual(0, secondAudioDeviceModule->Terminate());
  275. }
  276. // Start playout and verify that the native audio layer starts asking for real
  277. // audio samples to play out using the NeedMorePlayData callback.
  278. - (void)testStartPlayoutVerifyCallbacks {
  279. XCTestExpectation *playoutExpectation = [self expectationWithDescription:@"NeedMorePlayoutData"];
  280. __block int num_callbacks = 0;
  281. mock.expectNeedMorePlayData(^int32_t(const size_t nSamples,
  282. const size_t nBytesPerSample,
  283. const size_t nChannels,
  284. const uint32_t samplesPerSec,
  285. void *audioSamples,
  286. size_t &nSamplesOut,
  287. int64_t *elapsed_time_ms,
  288. int64_t *ntp_time_ms) {
  289. nSamplesOut = nSamples;
  290. XCTAssertEqual(nSamples, self.playoutParameters.frames_per_10ms_buffer());
  291. XCTAssertEqual(nBytesPerSample, kBytesPerSample);
  292. XCTAssertEqual(nChannels, self.playoutParameters.channels());
  293. XCTAssertEqual((int)samplesPerSec, self.playoutParameters.sample_rate());
  294. XCTAssertNotEqual((void*)NULL, audioSamples);
  295. if (++num_callbacks == kNumCallbacks) {
  296. [playoutExpectation fulfill];
  297. }
  298. return 0;
  299. });
  300. XCTAssertEqual(0, audioDeviceModule->RegisterAudioCallback(&mock));
  301. [self startPlayout];
  302. [self waitForExpectationsWithTimeout:kTestTimeOutInSec handler:nil];
  303. [self stopPlayout];
  304. }
  305. // Start recording and verify that the native audio layer starts feeding real
  306. // audio samples via the RecordedDataIsAvailable callback.
  307. - (void)testStartRecordingVerifyCallbacks {
  308. XCTestExpectation *recordExpectation =
  309. [self expectationWithDescription:@"RecordedDataIsAvailable"];
  310. __block int num_callbacks = 0;
  311. mock.expectRecordedDataIsAvailable(^(const void* audioSamples,
  312. const size_t nSamples,
  313. const size_t nBytesPerSample,
  314. const size_t nChannels,
  315. const uint32_t samplesPerSec,
  316. const uint32_t totalDelayMS,
  317. const int32_t clockDrift,
  318. const uint32_t currentMicLevel,
  319. const bool keyPressed,
  320. uint32_t& newMicLevel) {
  321. XCTAssertNotEqual((void*)NULL, audioSamples);
  322. XCTAssertEqual(nSamples, self.recordParameters.frames_per_10ms_buffer());
  323. XCTAssertEqual(nBytesPerSample, kBytesPerSample);
  324. XCTAssertEqual(nChannels, self.recordParameters.channels());
  325. XCTAssertEqual((int)samplesPerSec, self.recordParameters.sample_rate());
  326. XCTAssertEqual(0, clockDrift);
  327. XCTAssertEqual(0u, currentMicLevel);
  328. XCTAssertFalse(keyPressed);
  329. if (++num_callbacks == kNumCallbacks) {
  330. [recordExpectation fulfill];
  331. }
  332. return 0;
  333. });
  334. XCTAssertEqual(0, audioDeviceModule->RegisterAudioCallback(&mock));
  335. [self startRecording];
  336. [self waitForExpectationsWithTimeout:kTestTimeOutInSec handler:nil];
  337. [self stopRecording];
  338. }
  339. // Start playout and recording (full-duplex audio) and verify that audio is
  340. // active in both directions.
  341. - (void)testStartPlayoutAndRecordingVerifyCallbacks {
  342. XCTestExpectation *playoutExpectation = [self expectationWithDescription:@"NeedMorePlayoutData"];
  343. __block NSUInteger callbackCount = 0;
  344. XCTestExpectation *recordExpectation =
  345. [self expectationWithDescription:@"RecordedDataIsAvailable"];
  346. recordExpectation.expectedFulfillmentCount = kNumCallbacks;
  347. mock.expectNeedMorePlayData(^int32_t(const size_t nSamples,
  348. const size_t nBytesPerSample,
  349. const size_t nChannels,
  350. const uint32_t samplesPerSec,
  351. void *audioSamples,
  352. size_t &nSamplesOut,
  353. int64_t *elapsed_time_ms,
  354. int64_t *ntp_time_ms) {
  355. nSamplesOut = nSamples;
  356. XCTAssertEqual(nSamples, self.playoutParameters.frames_per_10ms_buffer());
  357. XCTAssertEqual(nBytesPerSample, kBytesPerSample);
  358. XCTAssertEqual(nChannels, self.playoutParameters.channels());
  359. XCTAssertEqual((int)samplesPerSec, self.playoutParameters.sample_rate());
  360. XCTAssertNotEqual((void*)NULL, audioSamples);
  361. if (callbackCount++ >= kNumCallbacks) {
  362. [playoutExpectation fulfill];
  363. }
  364. return 0;
  365. });
  366. mock.expectRecordedDataIsAvailable(^(const void* audioSamples,
  367. const size_t nSamples,
  368. const size_t nBytesPerSample,
  369. const size_t nChannels,
  370. const uint32_t samplesPerSec,
  371. const uint32_t totalDelayMS,
  372. const int32_t clockDrift,
  373. const uint32_t currentMicLevel,
  374. const bool keyPressed,
  375. uint32_t& newMicLevel) {
  376. XCTAssertNotEqual((void*)NULL, audioSamples);
  377. XCTAssertEqual(nSamples, self.recordParameters.frames_per_10ms_buffer());
  378. XCTAssertEqual(nBytesPerSample, kBytesPerSample);
  379. XCTAssertEqual(nChannels, self.recordParameters.channels());
  380. XCTAssertEqual((int)samplesPerSec, self.recordParameters.sample_rate());
  381. XCTAssertEqual(0, clockDrift);
  382. XCTAssertEqual(0u, currentMicLevel);
  383. XCTAssertFalse(keyPressed);
  384. [recordExpectation fulfill];
  385. return 0;
  386. });
  387. XCTAssertEqual(0, audioDeviceModule->RegisterAudioCallback(&mock));
  388. [self startPlayout];
  389. [self startRecording];
  390. [self waitForExpectationsWithTimeout:kTestTimeOutInSec handler:nil];
  391. [self stopRecording];
  392. [self stopPlayout];
  393. }
  394. // Start playout and read audio from an external PCM file when the audio layer
  395. // asks for data to play out. Real audio is played out in this test but it does
  396. // not contain any explicit verification that the audio quality is perfect.
  397. - (void)testRunPlayoutWithFileAsSource {
  398. XCTAssertEqual(1u, playoutParameters.channels());
  399. // Using XCTestExpectation to count callbacks is very slow.
  400. XCTestExpectation *playoutExpectation = [self expectationWithDescription:@"NeedMorePlayoutData"];
  401. const int expectedCallbackCount = kFilePlayTimeInSec * kNumCallbacksPerSecond;
  402. __block int callbackCount = 0;
  403. NSURL *fileURL = [self fileURLForSampleRate:playoutParameters.sample_rate()];
  404. NSInputStream *inputStream = [[NSInputStream alloc] initWithURL:fileURL];
  405. mock.expectNeedMorePlayData(^int32_t(const size_t nSamples,
  406. const size_t nBytesPerSample,
  407. const size_t nChannels,
  408. const uint32_t samplesPerSec,
  409. void *audioSamples,
  410. size_t &nSamplesOut,
  411. int64_t *elapsed_time_ms,
  412. int64_t *ntp_time_ms) {
  413. [inputStream read:(uint8_t *)audioSamples maxLength:nSamples*nBytesPerSample*nChannels];
  414. nSamplesOut = nSamples;
  415. if (callbackCount++ == expectedCallbackCount) {
  416. [playoutExpectation fulfill];
  417. }
  418. return 0;
  419. });
  420. XCTAssertEqual(0, audioDeviceModule->RegisterAudioCallback(&mock));
  421. [self startPlayout];
  422. NSTimeInterval waitTimeout = kFilePlayTimeInSec * 2.0;
  423. [self waitForExpectationsWithTimeout:waitTimeout handler:nil];
  424. [self stopPlayout];
  425. }
  426. - (void)testDevices {
  427. // Device enumeration is not supported. Verify fixed values only.
  428. XCTAssertEqual(1, audioDeviceModule->PlayoutDevices());
  429. XCTAssertEqual(1, audioDeviceModule->RecordingDevices());
  430. }
  431. // Start playout and recording and store recorded data in an intermediate FIFO
  432. // buffer from which the playout side then reads its samples in the same order
  433. // as they were stored. Under ideal circumstances, a callback sequence would
  434. // look like: ...+-+-+-+-+-+-+-..., where '+' means 'packet recorded' and '-'
  435. // means 'packet played'. Under such conditions, the FIFO would only contain
  436. // one packet on average. However, under more realistic conditions, the size
  437. // of the FIFO will vary more due to an unbalance between the two sides.
  438. // This test tries to verify that the device maintains a balanced callback-
  439. // sequence by running in loopback for ten seconds while measuring the size
  440. // (max and average) of the FIFO. The size of the FIFO is increased by the
  441. // recording side and decreased by the playout side.
  442. // TODO(henrika): tune the final test parameters after running tests on several
  443. // different devices.
  444. - (void)testRunPlayoutAndRecordingInFullDuplex {
  445. XCTAssertEqual(recordParameters.channels(), playoutParameters.channels());
  446. XCTAssertEqual(recordParameters.sample_rate(), playoutParameters.sample_rate());
  447. XCTestExpectation *playoutExpectation = [self expectationWithDescription:@"NeedMorePlayoutData"];
  448. __block NSUInteger playoutCallbacks = 0;
  449. NSUInteger expectedPlayoutCallbacks = kFullDuplexTimeInSec * kNumCallbacksPerSecond;
  450. // FIFO queue and measurements
  451. NSMutableArray *fifoBuffer = [NSMutableArray arrayWithCapacity:20];
  452. __block NSUInteger fifoMaxSize = 0;
  453. __block NSUInteger fifoTotalWrittenElements = 0;
  454. __block NSUInteger fifoWriteCount = 0;
  455. mock.expectRecordedDataIsAvailable(^(const void* audioSamples,
  456. const size_t nSamples,
  457. const size_t nBytesPerSample,
  458. const size_t nChannels,
  459. const uint32_t samplesPerSec,
  460. const uint32_t totalDelayMS,
  461. const int32_t clockDrift,
  462. const uint32_t currentMicLevel,
  463. const bool keyPressed,
  464. uint32_t& newMicLevel) {
  465. if (fifoWriteCount++ < kNumIgnoreFirstCallbacks) {
  466. return 0;
  467. }
  468. NSData *data = [NSData dataWithBytes:audioSamples length:nSamples*nBytesPerSample*nChannels];
  469. @synchronized(fifoBuffer) {
  470. [fifoBuffer addObject:data];
  471. fifoMaxSize = MAX(fifoMaxSize, fifoBuffer.count);
  472. fifoTotalWrittenElements += fifoBuffer.count;
  473. }
  474. return 0;
  475. });
  476. mock.expectNeedMorePlayData(^int32_t(const size_t nSamples,
  477. const size_t nBytesPerSample,
  478. const size_t nChannels,
  479. const uint32_t samplesPerSec,
  480. void *audioSamples,
  481. size_t &nSamplesOut,
  482. int64_t *elapsed_time_ms,
  483. int64_t *ntp_time_ms) {
  484. nSamplesOut = nSamples;
  485. NSData *data;
  486. @synchronized(fifoBuffer) {
  487. data = fifoBuffer.firstObject;
  488. if (data) {
  489. [fifoBuffer removeObjectAtIndex:0];
  490. }
  491. }
  492. if (data) {
  493. memcpy(audioSamples, (char*) data.bytes, data.length);
  494. } else {
  495. memset(audioSamples, 0, nSamples*nBytesPerSample*nChannels);
  496. }
  497. if (playoutCallbacks++ == expectedPlayoutCallbacks) {
  498. [playoutExpectation fulfill];
  499. }
  500. return 0;
  501. });
  502. XCTAssertEqual(0, audioDeviceModule->RegisterAudioCallback(&mock));
  503. [self startRecording];
  504. [self startPlayout];
  505. NSTimeInterval waitTimeout = kFullDuplexTimeInSec * 2.0;
  506. [self waitForExpectationsWithTimeout:waitTimeout handler:nil];
  507. size_t fifoAverageSize =
  508. (fifoTotalWrittenElements == 0)
  509. ? 0.0
  510. : 0.5 + (double)fifoTotalWrittenElements / (fifoWriteCount - kNumIgnoreFirstCallbacks);
  511. [self stopPlayout];
  512. [self stopRecording];
  513. XCTAssertLessThan(fifoAverageSize, 10u);
  514. XCTAssertLessThan(fifoMaxSize, 20u);
  515. }
  516. @end