blob: c936399f34125a5c94dd3ce686c6bffd0160d4f1 [file] [log] [blame]
Peter Hanspers8d95e3b2018-05-15 10:22:36 +02001/*
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
11#import <XCTest/XCTest.h>
Byoungchan Lee43bd7602019-10-08 23:58:41 +090012
13#include "api/task_queue/default_task_queue_factory.h"
14
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020015#import "sdk/objc/components/audio/RTCAudioSession+Private.h"
16#import "sdk/objc/native/api/audio_device_module.h"
17#import "sdk/objc/native/src/audio/audio_device_ios.h"
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020018
Byoungchan Lee43bd7602019-10-08 23:58:41 +090019@interface RTCAudioDeviceTests : XCTestCase {
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020020 rtc::scoped_refptr<webrtc::AudioDeviceModule> _audioDeviceModule;
21 std::unique_ptr<webrtc::ios_adm::AudioDeviceIOS> _audio_device;
22}
23
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020024@property(nonatomic) RTC_OBJC_TYPE(RTCAudioSession) * audioSession;
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020025
26@end
27
28@implementation RTCAudioDeviceTests
29
30@synthesize audioSession = _audioSession;
31
32- (void)setUp {
33 [super setUp];
34
35 _audioDeviceModule = webrtc::CreateAudioDeviceModule();
36 _audio_device.reset(new webrtc::ios_adm::AudioDeviceIOS());
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020037 self.audioSession = [RTC_OBJC_TYPE(RTCAudioSession) sharedInstance];
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020038
39 NSError *error = nil;
40 [self.audioSession lockForConfiguration];
Byoungchan Lee43bd7602019-10-08 23:58:41 +090041 [self.audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:0 error:&error];
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020042 XCTAssertNil(error);
43
44 [self.audioSession setMode:AVAudioSessionModeVoiceChat error:&error];
45 XCTAssertNil(error);
46
47 [self.audioSession setActive:YES error:&error];
48 XCTAssertNil(error);
49
50 [self.audioSession unlockForConfiguration];
51}
52
53- (void)tearDown {
54 _audio_device->Terminate();
55 _audio_device.reset(nullptr);
56 _audioDeviceModule = nullptr;
57 [self.audioSession notifyDidEndInterruptionWithShouldResumeSession:NO];
58
59 [super tearDown];
60}
61
62// Verifies that the AudioDeviceIOS is_interrupted_ flag is reset correctly
63// after an iOS AVAudioSessionInterruptionTypeEnded notification event.
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020064// AudioDeviceIOS listens to RTC_OBJC_TYPE(RTCAudioSession) interrupted notifications by:
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020065// - In AudioDeviceIOS.InitPlayOrRecord registers its audio_session_observer_
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020066// callback with RTC_OBJC_TYPE(RTCAudioSession)'s delegate list.
67// - When RTC_OBJC_TYPE(RTCAudioSession) receives an iOS audio interrupted notification, it
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020068// passes the notification to callbacks in its delegate list which sets
69// AudioDeviceIOS's is_interrupted_ flag to true.
70// - When AudioDeviceIOS.ShutdownPlayOrRecord is called, its
71// audio_session_observer_ callback is removed from RTCAudioSessions's
72// delegate list.
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020073// So if RTC_OBJC_TYPE(RTCAudioSession) receives an iOS end audio interruption notification,
74// AudioDeviceIOS is not notified as its callback is not in RTC_OBJC_TYPE(RTCAudioSession)'s
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020075// delegate list. This causes AudioDeviceIOS's is_interrupted_ flag to be in
76// the wrong (true) state and the audio session will ignore audio changes.
Mirko Bonadeia81e9c82020-05-04 16:14:32 +020077// As RTC_OBJC_TYPE(RTCAudioSession) keeps its own interrupted state, the fix is to initialize
78// AudioDeviceIOS's is_interrupted_ flag to RTC_OBJC_TYPE(RTCAudioSession)'s isInterrupted
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020079// flag in AudioDeviceIOS.InitPlayOrRecord.
80- (void)testInterruptedAudioSession {
81 XCTAssertTrue(self.audioSession.isActive);
82 XCTAssertTrue([self.audioSession.category isEqual:AVAudioSessionCategoryPlayAndRecord] ||
83 [self.audioSession.category isEqual:AVAudioSessionCategoryPlayback]);
84 XCTAssertEqual(AVAudioSessionModeVoiceChat, self.audioSession.mode);
85
Byoungchan Lee43bd7602019-10-08 23:58:41 +090086 std::unique_ptr<webrtc::TaskQueueFactory> task_queue_factory =
87 webrtc::CreateDefaultTaskQueueFactory();
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020088 std::unique_ptr<webrtc::AudioDeviceBuffer> audio_buffer;
Byoungchan Lee43bd7602019-10-08 23:58:41 +090089 audio_buffer.reset(new webrtc::AudioDeviceBuffer(task_queue_factory.get()));
Peter Hanspers8d95e3b2018-05-15 10:22:36 +020090 _audio_device->AttachAudioBuffer(audio_buffer.get());
91 XCTAssertEqual(webrtc::AudioDeviceGeneric::InitStatus::OK, _audio_device->Init());
92 XCTAssertEqual(0, _audio_device->InitPlayout());
93 XCTAssertEqual(0, _audio_device->StartPlayout());
94
95 // Force interruption.
96 [self.audioSession notifyDidBeginInterruption];
97
98 // Wait for notification to propagate.
Sebastian Jansson6ea2c6a2020-01-13 14:07:22 +010099 rtc::ThreadManager::ProcessAllMessageQueuesForTesting();
Peter Hanspers8d95e3b2018-05-15 10:22:36 +0200100 XCTAssertTrue(_audio_device->IsInterrupted());
101
102 // Force it for testing.
103 _audio_device->StopPlayout();
104
105 [self.audioSession notifyDidEndInterruptionWithShouldResumeSession:YES];
106 // Wait for notification to propagate.
Sebastian Jansson6ea2c6a2020-01-13 14:07:22 +0100107 rtc::ThreadManager::ProcessAllMessageQueuesForTesting();
Peter Hanspers8d95e3b2018-05-15 10:22:36 +0200108 XCTAssertTrue(_audio_device->IsInterrupted());
109
110 _audio_device->Init();
111 _audio_device->InitPlayout();
112 XCTAssertFalse(_audio_device->IsInterrupted());
113}
114
115@end