sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 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 | |
tkchin@webrtc.org | 122caa5 | 2014-07-15 20:20:47 +0000 | [diff] [blame] | 11 | #import <AVFoundation/AVFoundation.h> |
| 12 | #import <Foundation/Foundation.h> |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/audio_device/ios/audio_device_ios.h" |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 15 | |
Henrik Andreasson | 6e286cb | 2017-06-13 15:20:52 +0000 | [diff] [blame] | 16 | #include <cmath> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/array_view.h" |
| 19 | #include "modules/audio_device/fine_audio_buffer.h" |
| 20 | #include "rtc_base/atomicops.h" |
| 21 | #include "rtc_base/bind.h" |
| 22 | #include "rtc_base/checks.h" |
| 23 | #include "rtc_base/criticalsection.h" |
| 24 | #include "rtc_base/logging.h" |
| 25 | #include "rtc_base/thread.h" |
| 26 | #include "rtc_base/thread_annotations.h" |
| 27 | #include "rtc_base/timeutils.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 28 | #include "sdk/objc/native/src/audio/helpers.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "system_wrappers/include/metrics.h" |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 30 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #import "modules/audio_device/ios/objc/RTCAudioSessionDelegateAdapter.h" |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 32 | #import "sdk/objc/base/RTCLogging.h" |
| 33 | #import "sdk/objc/components/audio/RTCAudioSession+Private.h" |
| 34 | #import "sdk/objc/components/audio/RTCAudioSession.h" |
| 35 | #import "sdk/objc/components/audio/RTCAudioSessionConfiguration.h" |
denicija | 59ee91b | 2017-06-05 05:48:47 -0700 | [diff] [blame] | 36 | |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 37 | namespace webrtc { |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 38 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 39 | #define LOGI() RTC_LOG(LS_INFO) << "AudioDeviceIOS::" |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 40 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 41 | #define LOG_AND_RETURN_IF_ERROR(error, message) \ |
| 42 | do { \ |
| 43 | OSStatus err = error; \ |
| 44 | if (err) { \ |
| 45 | RTC_LOG(LS_ERROR) << message << ": " << err; \ |
| 46 | return false; \ |
| 47 | } \ |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 48 | } while (0) |
| 49 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 50 | #define LOG_IF_ERROR(error, message) \ |
| 51 | do { \ |
| 52 | OSStatus err = error; \ |
| 53 | if (err) { \ |
| 54 | RTC_LOG(LS_ERROR) << message << ": " << err; \ |
| 55 | } \ |
henrika | 45c136b | 2015-10-21 04:11:53 -0700 | [diff] [blame] | 56 | } while (0) |
| 57 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 58 | // Hardcoded delay estimates based on real measurements. |
| 59 | // TODO(henrika): these value is not used in combination with built-in AEC. |
| 60 | // Can most likely be removed. |
| 61 | const UInt16 kFixedPlayoutDelayEstimate = 30; |
| 62 | const UInt16 kFixedRecordDelayEstimate = 30; |
henrika | c729032 | 2015-12-02 10:46:46 +0100 | [diff] [blame] | 63 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 64 | enum AudioDeviceMessageType : uint32_t { |
| 65 | kMessageTypeInterruptionBegin, |
| 66 | kMessageTypeInterruptionEnd, |
| 67 | kMessageTypeValidRouteChange, |
| 68 | kMessageTypeCanPlayOrRecordChange, |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 69 | kMessageTypePlayoutGlitchDetected, |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 70 | kMessageOutputVolumeChange, |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 73 | using ios::CheckAndLogError; |
| 74 | |
henrika | 324d9c9 | 2015-07-20 13:09:23 +0200 | [diff] [blame] | 75 | #if !defined(NDEBUG) |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 76 | // Returns true when the code runs on a device simulator. |
| 77 | static bool DeviceIsSimulator() { |
| 78 | return ios::GetDeviceName() == "x86_64"; |
| 79 | } |
| 80 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 81 | // Helper method that logs essential device information strings. |
henrika | 324d9c9 | 2015-07-20 13:09:23 +0200 | [diff] [blame] | 82 | static void LogDeviceInfo() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 83 | RTC_LOG(LS_INFO) << "LogDeviceInfo"; |
henrika | 324d9c9 | 2015-07-20 13:09:23 +0200 | [diff] [blame] | 84 | @autoreleasepool { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 85 | RTC_LOG(LS_INFO) << " system name: " << ios::GetSystemName(); |
Kári Tristan Helgason | 86f8047 | 2017-12-01 14:55:01 +0100 | [diff] [blame] | 86 | RTC_LOG(LS_INFO) << " system version: " << ios::GetSystemVersionAsString(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 87 | RTC_LOG(LS_INFO) << " device type: " << ios::GetDeviceType(); |
| 88 | RTC_LOG(LS_INFO) << " device name: " << ios::GetDeviceName(); |
| 89 | RTC_LOG(LS_INFO) << " process name: " << ios::GetProcessName(); |
| 90 | RTC_LOG(LS_INFO) << " process ID: " << ios::GetProcessID(); |
| 91 | RTC_LOG(LS_INFO) << " OS version: " << ios::GetOSVersionString(); |
| 92 | RTC_LOG(LS_INFO) << " processing cores: " << ios::GetProcessorCount(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 93 | RTC_LOG(LS_INFO) << " low power mode: " << ios::GetLowPowerModeEnabled(); |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 94 | #if TARGET_IPHONE_SIMULATOR |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 95 | RTC_LOG(LS_INFO) << " TARGET_IPHONE_SIMULATOR is defined"; |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 96 | #endif |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 97 | RTC_LOG(LS_INFO) << " DeviceIsSimulator: " << DeviceIsSimulator(); |
henrika | 324d9c9 | 2015-07-20 13:09:23 +0200 | [diff] [blame] | 98 | } |
| 99 | } |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 100 | #endif // !defined(NDEBUG) |
henrika | 324d9c9 | 2015-07-20 13:09:23 +0200 | [diff] [blame] | 101 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 102 | AudioDeviceIOS::AudioDeviceIOS() |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 103 | : audio_device_buffer_(nullptr), |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 104 | audio_unit_(nullptr), |
| 105 | recording_(0), |
| 106 | playing_(0), |
| 107 | initialized_(false), |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 108 | audio_is_initialized_(false), |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 109 | is_interrupted_(false), |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 110 | has_configured_session_(false), |
| 111 | num_detected_playout_glitches_(0), |
| 112 | last_playout_time_(0), |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 113 | num_playout_callbacks_(0), |
| 114 | last_output_volume_change_time_(0) { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 115 | LOGI() << "ctor" << ios::GetCurrentThreadDescription(); |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 116 | io_thread_checker_.DetachFromThread(); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 117 | thread_ = rtc::Thread::Current(); |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 118 | audio_session_observer_ = [[RTCAudioSessionDelegateAdapter alloc] initWithObserver:this]; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 119 | } |
| 120 | |
tkchin@webrtc.org | 122caa5 | 2014-07-15 20:20:47 +0000 | [diff] [blame] | 121 | AudioDeviceIOS::~AudioDeviceIOS() { |
henrika | 34911ad | 2015-11-20 15:47:09 +0100 | [diff] [blame] | 122 | LOGI() << "~dtor" << ios::GetCurrentThreadDescription(); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 123 | audio_session_observer_ = nil; |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 124 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 125 | Terminate(); |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 126 | } |
| 127 | |
tkchin@webrtc.org | 122caa5 | 2014-07-15 20:20:47 +0000 | [diff] [blame] | 128 | void AudioDeviceIOS::AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 129 | LOGI() << "AttachAudioBuffer"; |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 130 | RTC_DCHECK(audioBuffer); |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 131 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 132 | audio_device_buffer_ = audioBuffer; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 135 | AudioDeviceGeneric::InitStatus AudioDeviceIOS::Init() { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 136 | LOGI() << "Init"; |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 137 | RTC_DCHECK_RUN_ON(&thread_checker_); |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 138 | if (initialized_) { |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 139 | return InitStatus::OK; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 140 | } |
| 141 | #if !defined(NDEBUG) |
| 142 | LogDeviceInfo(); |
| 143 | #endif |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 144 | // Store the preferred sample rate and preferred number of channels already |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 145 | // here. They have not been set and confirmed yet since configureForWebRTC |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 146 | // is not called until audio is about to start. However, it makes sense to |
| 147 | // store the parameters now and then verify at a later stage. |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 148 | RTCAudioSessionConfiguration* config = [RTCAudioSessionConfiguration webRTCConfiguration]; |
| 149 | playout_parameters_.reset(config.sampleRate, config.outputNumberOfChannels); |
| 150 | record_parameters_.reset(config.sampleRate, config.inputNumberOfChannels); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 151 | // Ensure that the audio device buffer (ADB) knows about the internal audio |
| 152 | // parameters. Note that, even if we are unable to get a mono audio session, |
| 153 | // we will always tell the I/O audio unit to do a channel format conversion |
| 154 | // to guarantee mono on the "input side" of the audio unit. |
| 155 | UpdateAudioDeviceBuffer(); |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 156 | initialized_ = true; |
Max Morin | 84cab20 | 2016-07-01 13:35:19 +0200 | [diff] [blame] | 157 | return InitStatus::OK; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 158 | } |
| 159 | |
tkchin@webrtc.org | 122caa5 | 2014-07-15 20:20:47 +0000 | [diff] [blame] | 160 | int32_t AudioDeviceIOS::Terminate() { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 161 | LOGI() << "Terminate"; |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 162 | RTC_DCHECK_RUN_ON(&thread_checker_); |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 163 | if (!initialized_) { |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 164 | return 0; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 165 | } |
henrika | 34911ad | 2015-11-20 15:47:09 +0100 | [diff] [blame] | 166 | StopPlayout(); |
| 167 | StopRecording(); |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 168 | initialized_ = false; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 169 | return 0; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 170 | } |
| 171 | |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 172 | bool AudioDeviceIOS::Initialized() const { |
| 173 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 174 | return initialized_; |
| 175 | } |
| 176 | |
tkchin@webrtc.org | 122caa5 | 2014-07-15 20:20:47 +0000 | [diff] [blame] | 177 | int32_t AudioDeviceIOS::InitPlayout() { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 178 | LOGI() << "InitPlayout"; |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 179 | RTC_DCHECK_RUN_ON(&thread_checker_); |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 180 | RTC_DCHECK(initialized_); |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 181 | RTC_DCHECK(!audio_is_initialized_); |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 182 | RTC_DCHECK(!playing_); |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 183 | if (!audio_is_initialized_) { |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 184 | if (!InitPlayOrRecord()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 185 | RTC_LOG_F(LS_ERROR) << "InitPlayOrRecord failed for InitPlayout!"; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 186 | return -1; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 187 | } |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 188 | } |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 189 | audio_is_initialized_ = true; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 190 | return 0; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 191 | } |
| 192 | |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 193 | bool AudioDeviceIOS::PlayoutIsInitialized() const { |
| 194 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 195 | return audio_is_initialized_; |
| 196 | } |
| 197 | |
| 198 | bool AudioDeviceIOS::RecordingIsInitialized() const { |
| 199 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 200 | return audio_is_initialized_; |
| 201 | } |
| 202 | |
tkchin@webrtc.org | 122caa5 | 2014-07-15 20:20:47 +0000 | [diff] [blame] | 203 | int32_t AudioDeviceIOS::InitRecording() { |
henrika | 324d9c9 | 2015-07-20 13:09:23 +0200 | [diff] [blame] | 204 | LOGI() << "InitRecording"; |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 205 | RTC_DCHECK_RUN_ON(&thread_checker_); |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 206 | RTC_DCHECK(initialized_); |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 207 | RTC_DCHECK(!audio_is_initialized_); |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 208 | RTC_DCHECK(!recording_); |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 209 | if (!audio_is_initialized_) { |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 210 | if (!InitPlayOrRecord()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 211 | RTC_LOG_F(LS_ERROR) << "InitPlayOrRecord failed for InitRecording!"; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 212 | return -1; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 213 | } |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 214 | } |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 215 | audio_is_initialized_ = true; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 216 | return 0; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 217 | } |
| 218 | |
tkchin@webrtc.org | 122caa5 | 2014-07-15 20:20:47 +0000 | [diff] [blame] | 219 | int32_t AudioDeviceIOS::StartPlayout() { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 220 | LOGI() << "StartPlayout"; |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 221 | RTC_DCHECK_RUN_ON(&thread_checker_); |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 222 | RTC_DCHECK(audio_is_initialized_); |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 223 | RTC_DCHECK(!playing_); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 224 | RTC_DCHECK(audio_unit_); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 225 | if (fine_audio_buffer_) { |
| 226 | fine_audio_buffer_->ResetPlayout(); |
| 227 | } |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 228 | if (!recording_ && audio_unit_->GetState() == VoiceProcessingAudioUnit::kInitialized) { |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 229 | if (!audio_unit_->Start()) { |
| 230 | RTCLogError(@"StartPlayout failed to start audio unit."); |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 231 | return -1; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 232 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 233 | RTC_LOG(LS_INFO) << "Voice-Processing I/O audio unit is now started"; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 234 | } |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 235 | rtc::AtomicOps::ReleaseStore(&playing_, 1); |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 236 | num_playout_callbacks_ = 0; |
henrika | 070efc0 | 2017-07-05 02:34:31 -0700 | [diff] [blame] | 237 | num_detected_playout_glitches_ = 0; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 238 | return 0; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 239 | } |
| 240 | |
tkchin@webrtc.org | 122caa5 | 2014-07-15 20:20:47 +0000 | [diff] [blame] | 241 | int32_t AudioDeviceIOS::StopPlayout() { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 242 | LOGI() << "StopPlayout"; |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 243 | RTC_DCHECK_RUN_ON(&thread_checker_); |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 244 | if (!audio_is_initialized_ || !playing_) { |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 245 | return 0; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 246 | } |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 247 | if (!recording_) { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 248 | ShutdownPlayOrRecord(); |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 249 | audio_is_initialized_ = false; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 250 | } |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 251 | rtc::AtomicOps::ReleaseStore(&playing_, 0); |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 252 | |
| 253 | // Derive average number of calls to OnGetPlayoutData() between detected |
| 254 | // audio glitches and add the result to a histogram. |
| 255 | int average_number_of_playout_callbacks_between_glitches = 100000; |
henrika | 070efc0 | 2017-07-05 02:34:31 -0700 | [diff] [blame] | 256 | RTC_DCHECK_GE(num_playout_callbacks_, num_detected_playout_glitches_); |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 257 | if (num_detected_playout_glitches_ > 0) { |
| 258 | average_number_of_playout_callbacks_between_glitches = |
| 259 | num_playout_callbacks_ / num_detected_playout_glitches_; |
| 260 | } |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 261 | RTC_HISTOGRAM_COUNTS_100000("WebRTC.Audio.AveragePlayoutCallbacksBetweenGlitches", |
| 262 | average_number_of_playout_callbacks_between_glitches); |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 263 | RTCLog(@"Average number of playout callbacks between glitches: %d", |
| 264 | average_number_of_playout_callbacks_between_glitches); |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 265 | return 0; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 266 | } |
| 267 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 268 | int32_t AudioDeviceIOS::StartRecording() { |
| 269 | LOGI() << "StartRecording"; |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 270 | RTC_DCHECK_RUN_ON(&thread_checker_); |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 271 | RTC_DCHECK(audio_is_initialized_); |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 272 | RTC_DCHECK(!recording_); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 273 | RTC_DCHECK(audio_unit_); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 274 | if (fine_audio_buffer_) { |
| 275 | fine_audio_buffer_->ResetRecord(); |
| 276 | } |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 277 | if (!playing_ && audio_unit_->GetState() == VoiceProcessingAudioUnit::kInitialized) { |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 278 | if (!audio_unit_->Start()) { |
| 279 | RTCLogError(@"StartRecording failed to start audio unit."); |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 280 | return -1; |
| 281 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 282 | RTC_LOG(LS_INFO) << "Voice-Processing I/O audio unit is now started"; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 283 | } |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 284 | rtc::AtomicOps::ReleaseStore(&recording_, 1); |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 285 | return 0; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 286 | } |
| 287 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 288 | int32_t AudioDeviceIOS::StopRecording() { |
| 289 | LOGI() << "StopRecording"; |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 290 | RTC_DCHECK_RUN_ON(&thread_checker_); |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 291 | if (!audio_is_initialized_ || !recording_) { |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 292 | return 0; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 293 | } |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 294 | if (!playing_) { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 295 | ShutdownPlayOrRecord(); |
henrika | 17802ae | 2016-09-21 04:55:04 -0700 | [diff] [blame] | 296 | audio_is_initialized_ = false; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 297 | } |
pbos | 46ad542 | 2015-12-07 14:29:14 -0800 | [diff] [blame] | 298 | rtc::AtomicOps::ReleaseStore(&recording_, 0); |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
tkchin@webrtc.org | 122caa5 | 2014-07-15 20:20:47 +0000 | [diff] [blame] | 302 | int32_t AudioDeviceIOS::PlayoutDelay(uint16_t& delayMS) const { |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 303 | delayMS = kFixedPlayoutDelayEstimate; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 304 | return 0; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 305 | } |
| 306 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 307 | int AudioDeviceIOS::GetPlayoutAudioParameters(AudioParameters* params) const { |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 308 | LOGI() << "GetPlayoutAudioParameters"; |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 309 | RTC_DCHECK(playout_parameters_.is_valid()); |
| 310 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 311 | *params = playout_parameters_; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 312 | return 0; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 313 | } |
| 314 | |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 315 | int AudioDeviceIOS::GetRecordAudioParameters(AudioParameters* params) const { |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 316 | LOGI() << "GetRecordAudioParameters"; |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 317 | RTC_DCHECK(record_parameters_.is_valid()); |
| 318 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 319 | *params = record_parameters_; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 320 | return 0; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 321 | } |
| 322 | |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 323 | void AudioDeviceIOS::OnInterruptionBegin() { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 324 | RTC_DCHECK(thread_); |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame] | 325 | LOGI() << "OnInterruptionBegin"; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 326 | thread_->Post(RTC_FROM_HERE, this, kMessageTypeInterruptionBegin); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void AudioDeviceIOS::OnInterruptionEnd() { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 330 | RTC_DCHECK(thread_); |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame] | 331 | LOGI() << "OnInterruptionEnd"; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 332 | thread_->Post(RTC_FROM_HERE, this, kMessageTypeInterruptionEnd); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void AudioDeviceIOS::OnValidRouteChange() { |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 336 | RTC_DCHECK(thread_); |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 337 | thread_->Post(RTC_FROM_HERE, this, kMessageTypeValidRouteChange); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 338 | } |
| 339 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 340 | void AudioDeviceIOS::OnCanPlayOrRecordChange(bool can_play_or_record) { |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 341 | RTC_DCHECK(thread_); |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 342 | thread_->Post(RTC_FROM_HERE, |
| 343 | this, |
| 344 | kMessageTypeCanPlayOrRecordChange, |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 345 | new rtc::TypedMessageData<bool>(can_play_or_record)); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 346 | } |
| 347 | |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 348 | void AudioDeviceIOS::OnChangedOutputVolume() { |
| 349 | RTC_DCHECK(thread_); |
| 350 | thread_->Post(RTC_FROM_HERE, this, kMessageOutputVolumeChange); |
| 351 | } |
| 352 | |
henrika | bc9ffad | 2017-06-01 14:25:45 +0200 | [diff] [blame] | 353 | OSStatus AudioDeviceIOS::OnDeliverRecordedData(AudioUnitRenderActionFlags* flags, |
| 354 | const AudioTimeStamp* time_stamp, |
| 355 | UInt32 bus_number, |
| 356 | UInt32 num_frames, |
| 357 | AudioBufferList* /* io_data */) { |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 358 | RTC_DCHECK_RUN_ON(&io_thread_checker_); |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 359 | OSStatus result = noErr; |
| 360 | // Simply return if recording is not enabled. |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 361 | if (!rtc::AtomicOps::AcquireLoad(&recording_)) return result; |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 362 | |
henrika | bc9ffad | 2017-06-01 14:25:45 +0200 | [diff] [blame] | 363 | // Set the size of our own audio buffer and clear it first to avoid copying |
| 364 | // in combination with potential reallocations. |
| 365 | // On real iOS devices, the size will only be set once (at first callback). |
| 366 | record_audio_buffer_.Clear(); |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 367 | record_audio_buffer_.SetSize(num_frames); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 368 | |
henrika | bc9ffad | 2017-06-01 14:25:45 +0200 | [diff] [blame] | 369 | // Allocate AudioBuffers to be used as storage for the received audio. |
| 370 | // The AudioBufferList structure works as a placeholder for the |
| 371 | // AudioBuffer structure, which holds a pointer to the actual data buffer |
| 372 | // in |record_audio_buffer_|. Recorded audio will be rendered into this memory |
| 373 | // at each input callback when calling AudioUnitRender(). |
| 374 | AudioBufferList audio_buffer_list; |
| 375 | audio_buffer_list.mNumberBuffers = 1; |
| 376 | AudioBuffer* audio_buffer = &audio_buffer_list.mBuffers[0]; |
| 377 | audio_buffer->mNumberChannels = record_parameters_.channels(); |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 378 | audio_buffer->mDataByteSize = |
| 379 | record_audio_buffer_.size() * VoiceProcessingAudioUnit::kBytesPerSample; |
| 380 | audio_buffer->mData = reinterpret_cast<int8_t*>(record_audio_buffer_.data()); |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 381 | |
| 382 | // Obtain the recorded audio samples by initiating a rendering cycle. |
| 383 | // Since it happens on the input bus, the |io_data| parameter is a reference |
| 384 | // to the preallocated audio buffer list that the audio unit renders into. |
| 385 | // We can make the audio unit provide a buffer instead in io_data, but we |
| 386 | // currently just use our own. |
| 387 | // TODO(henrika): should error handling be improved? |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 388 | result = audio_unit_->Render(flags, time_stamp, bus_number, num_frames, &audio_buffer_list); |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 389 | if (result != noErr) { |
| 390 | RTCLogError(@"Failed to render audio."); |
| 391 | return result; |
| 392 | } |
| 393 | |
| 394 | // Get a pointer to the recorded audio and send it to the WebRTC ADB. |
| 395 | // Use the FineAudioBuffer instance to convert between native buffer size |
| 396 | // and the 10ms buffer size used by WebRTC. |
henrika | 883d00f | 2018-03-16 10:09:49 +0100 | [diff] [blame] | 397 | fine_audio_buffer_->DeliverRecordedData(record_audio_buffer_, kFixedRecordDelayEstimate); |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 398 | return noErr; |
| 399 | } |
| 400 | |
| 401 | OSStatus AudioDeviceIOS::OnGetPlayoutData(AudioUnitRenderActionFlags* flags, |
| 402 | const AudioTimeStamp* time_stamp, |
| 403 | UInt32 bus_number, |
| 404 | UInt32 num_frames, |
| 405 | AudioBufferList* io_data) { |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 406 | RTC_DCHECK_RUN_ON(&io_thread_checker_); |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 407 | // Verify 16-bit, noninterleaved mono PCM signal format. |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 408 | RTC_DCHECK_EQ(1, io_data->mNumberBuffers); |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 409 | AudioBuffer* audio_buffer = &io_data->mBuffers[0]; |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 410 | RTC_DCHECK_EQ(1, audio_buffer->mNumberChannels); |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 411 | |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 412 | // Produce silence and give audio unit a hint about it if playout is not |
| 413 | // activated. |
| 414 | if (!rtc::AtomicOps::AcquireLoad(&playing_)) { |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 415 | const size_t size_in_bytes = audio_buffer->mDataByteSize; |
| 416 | RTC_CHECK_EQ(size_in_bytes / VoiceProcessingAudioUnit::kBytesPerSample, num_frames); |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 417 | *flags |= kAudioUnitRenderAction_OutputIsSilence; |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 418 | memset(static_cast<int8_t*>(audio_buffer->mData), 0, size_in_bytes); |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 419 | return noErr; |
| 420 | } |
henrika | 2d014be | 2016-06-16 14:26:55 +0200 | [diff] [blame] | 421 | |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 422 | // Measure time since last call to OnGetPlayoutData() and see if it is larger |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 423 | // than a well defined threshold which depends on the current IO buffer size. |
| 424 | // If so, we have an indication of a glitch in the output audio since the |
| 425 | // core audio layer will most likely run dry in this state. |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 426 | ++num_playout_callbacks_; |
| 427 | const int64_t now_time = rtc::TimeMillis(); |
| 428 | if (time_stamp->mSampleTime != num_frames) { |
| 429 | const int64_t delta_time = now_time - last_playout_time_; |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 430 | const int glitch_threshold = 1.6 * playout_parameters_.GetBufferSizeInMilliseconds(); |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 431 | if (delta_time > glitch_threshold) { |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 432 | RTCLogWarning(@"Possible playout audio glitch detected.\n" |
| 433 | " Time since last OnGetPlayoutData was %lld ms.\n", |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 434 | delta_time); |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 435 | // Exclude extreme delta values since they do most likely not correspond |
| 436 | // to a real glitch. Instead, the most probable cause is that a headset |
| 437 | // has been plugged in or out. There are more direct ways to detect |
| 438 | // audio device changes (see HandleValidRouteChange()) but experiments |
| 439 | // show that using it leads to more complex implementations. |
| 440 | // TODO(henrika): more tests might be needed to come up with an even |
| 441 | // better upper limit. |
| 442 | if (glitch_threshold < 120 && delta_time > 120) { |
| 443 | RTCLog(@"Glitch warning is ignored. Probably caused by device switch."); |
| 444 | } else { |
| 445 | thread_->Post(RTC_FROM_HERE, this, kMessageTypePlayoutGlitchDetected); |
| 446 | } |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | last_playout_time_ = now_time; |
| 450 | |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 451 | // Read decoded 16-bit PCM samples from WebRTC (using a size that matches |
henrika | bb6f752 | 2017-05-30 02:01:30 -0700 | [diff] [blame] | 452 | // the native I/O audio unit) and copy the result to the audio buffer in the |
| 453 | // |io_data| destination. |
henrika | 8d7393b | 2018-04-19 13:40:15 +0200 | [diff] [blame] | 454 | fine_audio_buffer_->GetPlayoutData( |
| 455 | rtc::ArrayView<int16_t>(static_cast<int16_t*>(audio_buffer->mData), num_frames), |
| 456 | kFixedPlayoutDelayEstimate); |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 457 | return noErr; |
| 458 | } |
| 459 | |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 460 | void AudioDeviceIOS::OnMessage(rtc::Message* msg) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 461 | switch (msg->message_id) { |
| 462 | case kMessageTypeInterruptionBegin: |
| 463 | HandleInterruptionBegin(); |
| 464 | break; |
| 465 | case kMessageTypeInterruptionEnd: |
| 466 | HandleInterruptionEnd(); |
| 467 | break; |
| 468 | case kMessageTypeValidRouteChange: |
| 469 | HandleValidRouteChange(); |
| 470 | break; |
| 471 | case kMessageTypeCanPlayOrRecordChange: { |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 472 | rtc::TypedMessageData<bool>* data = static_cast<rtc::TypedMessageData<bool>*>(msg->pdata); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 473 | HandleCanPlayOrRecordChange(data->data()); |
| 474 | delete data; |
| 475 | break; |
| 476 | } |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 477 | case kMessageTypePlayoutGlitchDetected: |
| 478 | HandlePlayoutGlitchDetected(); |
| 479 | break; |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 480 | case kMessageOutputVolumeChange: |
| 481 | HandleOutputVolumeChange(); |
| 482 | break; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 486 | void AudioDeviceIOS::HandleInterruptionBegin() { |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 487 | RTC_DCHECK_RUN_ON(&thread_checker_); |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 488 | RTCLog(@"Interruption begin. IsInterrupted changed from %d to 1.", is_interrupted_); |
| 489 | if (audio_unit_ && audio_unit_->GetState() == VoiceProcessingAudioUnit::kStarted) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 490 | RTCLog(@"Stopping the audio unit due to interruption begin."); |
| 491 | if (!audio_unit_->Stop()) { |
| 492 | RTCLogError(@"Failed to stop the audio unit for interruption begin."); |
henrika | 09a7619 | 2017-08-23 15:04:40 +0200 | [diff] [blame] | 493 | } else { |
henrika | 79445ea | 2018-05-29 16:04:16 +0200 | [diff] [blame] | 494 | PrepareForNewStart(); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 495 | } |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 496 | } |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 497 | is_interrupted_ = true; |
| 498 | } |
| 499 | |
| 500 | void AudioDeviceIOS::HandleInterruptionEnd() { |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 501 | RTC_DCHECK_RUN_ON(&thread_checker_); |
haysc | 7735b1e | 2017-03-29 14:53:32 -0700 | [diff] [blame] | 502 | RTCLog(@"Interruption ended. IsInterrupted changed from %d to 0. " |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 503 | "Updating audio unit state.", |
| 504 | is_interrupted_); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 505 | is_interrupted_ = false; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 506 | UpdateAudioUnit([RTCAudioSession sharedInstance].canPlayOrRecord); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | void AudioDeviceIOS::HandleValidRouteChange() { |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 510 | RTC_DCHECK_RUN_ON(&thread_checker_); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 511 | RTCAudioSession* session = [RTCAudioSession sharedInstance]; |
henrika | 051d151 | 2016-09-22 08:48:04 -0700 | [diff] [blame] | 512 | RTCLog(@"%@", session); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 513 | HandleSampleRateChange(session.sampleRate); |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 514 | } |
| 515 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 516 | void AudioDeviceIOS::HandleCanPlayOrRecordChange(bool can_play_or_record) { |
| 517 | RTCLog(@"Handling CanPlayOrRecord change to: %d", can_play_or_record); |
| 518 | UpdateAudioUnit(can_play_or_record); |
| 519 | } |
| 520 | |
| 521 | void AudioDeviceIOS::HandleSampleRateChange(float sample_rate) { |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 522 | RTC_DCHECK_RUN_ON(&thread_checker_); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 523 | RTCLog(@"Handling sample rate change to %f.", sample_rate); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 524 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 525 | // Don't do anything if we're interrupted. |
| 526 | if (is_interrupted_) { |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 527 | RTCLog(@"Ignoring sample rate change to %f due to interruption.", sample_rate); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 528 | return; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 529 | } |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 530 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 531 | // If we don't have an audio unit yet, or the audio unit is uninitialized, |
| 532 | // there is no work to do. |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 533 | if (!audio_unit_ || audio_unit_->GetState() < VoiceProcessingAudioUnit::kInitialized) { |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 534 | return; |
| 535 | } |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 536 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 537 | // The audio unit is already initialized or started. |
| 538 | // Check to see if the sample rate or buffer size has changed. |
| 539 | RTCAudioSession* session = [RTCAudioSession sharedInstance]; |
| 540 | const double session_sample_rate = session.sampleRate; |
| 541 | const NSTimeInterval session_buffer_duration = session.IOBufferDuration; |
| 542 | const size_t session_frames_per_buffer = |
| 543 | static_cast<size_t>(session_sample_rate * session_buffer_duration + .5); |
| 544 | const double current_sample_rate = playout_parameters_.sample_rate(); |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 545 | const size_t current_frames_per_buffer = playout_parameters_.frames_per_buffer(); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 546 | RTCLog(@"Handling playout sample rate change to: %f\n" |
| 547 | " Session sample rate: %f frames_per_buffer: %lu\n" |
| 548 | " ADM sample rate: %f frames_per_buffer: %lu", |
| 549 | sample_rate, |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 550 | session_sample_rate, |
| 551 | (unsigned long)session_frames_per_buffer, |
| 552 | current_sample_rate, |
| 553 | (unsigned long)current_frames_per_buffer); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 554 | |
| 555 | // Sample rate and buffer size are the same, no work to do. |
jianjun.zhu | a84aa57 | 2016-10-05 19:19:23 -0700 | [diff] [blame] | 556 | if (std::abs(current_sample_rate - session_sample_rate) <= DBL_EPSILON && |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 557 | current_frames_per_buffer == session_frames_per_buffer) { |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 558 | RTCLog(@"Ignoring sample rate change since audio parameters are intact."); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 559 | return; |
| 560 | } |
| 561 | |
henrika | abcf112 | 2017-07-13 04:42:50 -0700 | [diff] [blame] | 562 | // Extra sanity check to ensure that the new sample rate is valid. |
| 563 | if (session_sample_rate <= 0.0) { |
| 564 | RTCLogError(@"Sample rate is invalid: %f", session_sample_rate); |
| 565 | return; |
| 566 | } |
| 567 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 568 | // We need to adjust our format and buffer sizes. |
| 569 | // The stream format is about to be changed and it requires that we first |
| 570 | // stop and uninitialize the audio unit to deallocate its resources. |
| 571 | RTCLog(@"Stopping and uninitializing audio unit to adjust buffers."); |
| 572 | bool restart_audio_unit = false; |
| 573 | if (audio_unit_->GetState() == VoiceProcessingAudioUnit::kStarted) { |
| 574 | audio_unit_->Stop(); |
| 575 | restart_audio_unit = true; |
henrika | 79445ea | 2018-05-29 16:04:16 +0200 | [diff] [blame] | 576 | PrepareForNewStart(); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 577 | } |
| 578 | if (audio_unit_->GetState() == VoiceProcessingAudioUnit::kInitialized) { |
| 579 | audio_unit_->Uninitialize(); |
| 580 | } |
| 581 | |
| 582 | // Allocate new buffers given the new stream format. |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 583 | SetupAudioBuffersForActiveAudioSession(); |
| 584 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 585 | // Initialize the audio unit again with the new sample rate. |
| 586 | RTC_DCHECK_EQ(playout_parameters_.sample_rate(), session_sample_rate); |
| 587 | if (!audio_unit_->Initialize(session_sample_rate)) { |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 588 | RTCLogError(@"Failed to initialize the audio unit with sample rate: %f", session_sample_rate); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 589 | return; |
| 590 | } |
| 591 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 592 | // Restart the audio unit if it was already running. |
| 593 | if (restart_audio_unit && !audio_unit_->Start()) { |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 594 | RTCLogError(@"Failed to start audio unit with sample rate: %f", session_sample_rate); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 595 | return; |
| 596 | } |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 597 | RTCLog(@"Successfully handled sample rate change."); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 598 | } |
| 599 | |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 600 | void AudioDeviceIOS::HandlePlayoutGlitchDetected() { |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 601 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 602 | // Don't update metrics if we're interrupted since a "glitch" is expected |
| 603 | // in this state. |
| 604 | if (is_interrupted_) { |
| 605 | RTCLog(@"Ignoring audio glitch due to interruption."); |
| 606 | return; |
| 607 | } |
| 608 | // Avoid doing glitch detection for two seconds after a volume change |
| 609 | // has been detected to reduce the risk of false alarm. |
| 610 | if (last_output_volume_change_time_ > 0 && |
| 611 | rtc::TimeSince(last_output_volume_change_time_) < 2000) { |
| 612 | RTCLog(@"Ignoring audio glitch due to recent output volume change."); |
| 613 | return; |
| 614 | } |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 615 | num_detected_playout_glitches_++; |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 616 | RTCLog(@"Number of detected playout glitches: %lld", num_detected_playout_glitches_); |
Anders Carlsson | 121ea32 | 2017-06-26 15:34:47 +0200 | [diff] [blame] | 617 | |
| 618 | int64_t glitch_count = num_detected_playout_glitches_; |
| 619 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 620 | RTCAudioSession* session = [RTCAudioSession sharedInstance]; |
| 621 | [session notifyDidDetectPlayoutGlitch:glitch_count]; |
| 622 | }); |
henrika | 7be7883 | 2017-06-13 17:34:16 +0200 | [diff] [blame] | 623 | } |
| 624 | |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 625 | void AudioDeviceIOS::HandleOutputVolumeChange() { |
| 626 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 627 | RTCLog(@"Output volume change detected."); |
| 628 | // Store time of this detection so it can be used to defer detection of |
| 629 | // glitches too close in time to this event. |
| 630 | last_output_volume_change_time_ = rtc::TimeMillis(); |
| 631 | } |
| 632 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 633 | void AudioDeviceIOS::UpdateAudioDeviceBuffer() { |
| 634 | LOGI() << "UpdateAudioDevicebuffer"; |
| 635 | // AttachAudioBuffer() is called at construction by the main class but check |
| 636 | // just in case. |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 637 | RTC_DCHECK(audio_device_buffer_) << "AttachAudioBuffer must be called first"; |
henrika | e6aca63 | 2018-01-03 13:58:58 +0100 | [diff] [blame] | 638 | RTC_DCHECK_GT(playout_parameters_.sample_rate(), 0); |
| 639 | RTC_DCHECK_GT(record_parameters_.sample_rate(), 0); |
| 640 | RTC_DCHECK_EQ(playout_parameters_.channels(), 1); |
| 641 | RTC_DCHECK_EQ(record_parameters_.channels(), 1); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 642 | // Inform the audio device buffer (ADB) about the new audio format. |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 643 | audio_device_buffer_->SetPlayoutSampleRate(playout_parameters_.sample_rate()); |
| 644 | audio_device_buffer_->SetPlayoutChannels(playout_parameters_.channels()); |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 645 | audio_device_buffer_->SetRecordingSampleRate(record_parameters_.sample_rate()); |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 646 | audio_device_buffer_->SetRecordingChannels(record_parameters_.channels()); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 647 | } |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 648 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 649 | void AudioDeviceIOS::SetupAudioBuffersForActiveAudioSession() { |
| 650 | LOGI() << "SetupAudioBuffersForActiveAudioSession"; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 651 | // Verify the current values once the audio session has been activated. |
Zeke Chin | b3fb71c | 2016-02-18 15:44:07 -0800 | [diff] [blame] | 652 | RTCAudioSession* session = [RTCAudioSession sharedInstance]; |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 653 | double sample_rate = session.sampleRate; |
| 654 | NSTimeInterval io_buffer_duration = session.IOBufferDuration; |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 655 | RTCLog(@"%@", session); |
henrika | 45c136b | 2015-10-21 04:11:53 -0700 | [diff] [blame] | 656 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 657 | // Log a warning message for the case when we are unable to set the preferred |
| 658 | // hardware sample rate but continue and use the non-ideal sample rate after |
henrika | 45c136b | 2015-10-21 04:11:53 -0700 | [diff] [blame] | 659 | // reinitializing the audio parameters. Most BT headsets only support 8kHz or |
| 660 | // 16kHz. |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 661 | RTCAudioSessionConfiguration* webRTCConfig = [RTCAudioSessionConfiguration webRTCConfiguration]; |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 662 | if (sample_rate != webRTCConfig.sampleRate) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 663 | RTC_LOG(LS_WARNING) << "Unable to set the preferred sample rate"; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 664 | } |
andrew@webrtc.org | 73a702c | 2013-01-30 21:18:31 +0000 | [diff] [blame] | 665 | |
henrika | fb09eeb | 2017-11-22 14:25:25 +0100 | [diff] [blame] | 666 | // Crash reports indicates that it can happen in rare cases that the reported |
| 667 | // sample rate is less than or equal to zero. If that happens and if a valid |
| 668 | // sample rate has already been set during initialization, the best guess we |
| 669 | // can do is to reuse the current sample rate. |
| 670 | if (sample_rate <= DBL_EPSILON && playout_parameters_.sample_rate() > 0) { |
| 671 | RTCLogError(@"Reported rate is invalid: %f. " |
| 672 | "Using %d as sample rate instead.", |
| 673 | sample_rate, playout_parameters_.sample_rate()); |
| 674 | sample_rate = playout_parameters_.sample_rate(); |
| 675 | } |
| 676 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 677 | // At this stage, we also know the exact IO buffer duration and can add |
| 678 | // that info to the existing audio parameters where it is converted into |
| 679 | // number of audio frames. |
| 680 | // Example: IO buffer size = 0.008 seconds <=> 128 audio frames at 16kHz. |
| 681 | // Hence, 128 is the size we expect to see in upcoming render callbacks. |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 682 | playout_parameters_.reset(sample_rate, playout_parameters_.channels(), io_buffer_duration); |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 683 | RTC_DCHECK(playout_parameters_.is_complete()); |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 684 | record_parameters_.reset(sample_rate, record_parameters_.channels(), io_buffer_duration); |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 685 | RTC_DCHECK(record_parameters_.is_complete()); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 686 | RTC_LOG(LS_INFO) << " frames per I/O buffer: " << playout_parameters_.frames_per_buffer(); |
| 687 | RTC_LOG(LS_INFO) << " bytes per I/O buffer: " << playout_parameters_.GetBytesPerBuffer(); |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 688 | RTC_DCHECK_EQ(playout_parameters_.GetBytesPerBuffer(), record_parameters_.GetBytesPerBuffer()); |
andrew@webrtc.org | 73a702c | 2013-01-30 21:18:31 +0000 | [diff] [blame] | 689 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 690 | // Update the ADB parameters since the sample rate might have changed. |
| 691 | UpdateAudioDeviceBuffer(); |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 692 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 693 | // Create a modified audio buffer class which allows us to ask for, |
| 694 | // or deliver, any number of samples (and not only multiple of 10ms) to match |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 695 | // the native audio unit buffer size. |
henrika | 8c471e7 | 2015-10-01 07:36:45 -0700 | [diff] [blame] | 696 | RTC_DCHECK(audio_device_buffer_); |
henrika | 29e865a | 2018-04-24 13:22:31 +0200 | [diff] [blame] | 697 | fine_audio_buffer_.reset(new FineAudioBuffer(audio_device_buffer_)); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 698 | } |
| 699 | |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 700 | bool AudioDeviceIOS::CreateAudioUnit() { |
| 701 | RTC_DCHECK(!audio_unit_); |
henrika | c729032 | 2015-12-02 10:46:46 +0100 | [diff] [blame] | 702 | |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 703 | audio_unit_.reset(new VoiceProcessingAudioUnit(this)); |
| 704 | if (!audio_unit_->Init()) { |
| 705 | audio_unit_.reset(); |
henrika | 34911ad | 2015-11-20 15:47:09 +0100 | [diff] [blame] | 706 | return false; |
| 707 | } |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 708 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 709 | return true; |
| 710 | } |
| 711 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 712 | void AudioDeviceIOS::UpdateAudioUnit(bool can_play_or_record) { |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 713 | RTC_DCHECK_RUN_ON(&thread_checker_); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 714 | RTCLog(@"Updating audio unit state. CanPlayOrRecord=%d IsInterrupted=%d", |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 715 | can_play_or_record, |
| 716 | is_interrupted_); |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 717 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 718 | if (is_interrupted_) { |
| 719 | RTCLog(@"Ignoring audio unit update due to interruption."); |
| 720 | return; |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 721 | } |
henrika | 45c136b | 2015-10-21 04:11:53 -0700 | [diff] [blame] | 722 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 723 | // If we're not initialized we don't need to do anything. Audio unit will |
| 724 | // be initialized on initialization. |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 725 | if (!audio_is_initialized_) return; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 726 | |
| 727 | // If we're initialized, we must have an audio unit. |
| 728 | RTC_DCHECK(audio_unit_); |
| 729 | |
| 730 | bool should_initialize_audio_unit = false; |
| 731 | bool should_uninitialize_audio_unit = false; |
| 732 | bool should_start_audio_unit = false; |
| 733 | bool should_stop_audio_unit = false; |
| 734 | |
| 735 | switch (audio_unit_->GetState()) { |
| 736 | case VoiceProcessingAudioUnit::kInitRequired: |
henrika | 86eff72 | 2016-06-21 11:26:47 +0200 | [diff] [blame] | 737 | RTCLog(@"VPAU state: InitRequired"); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 738 | RTC_NOTREACHED(); |
| 739 | break; |
| 740 | case VoiceProcessingAudioUnit::kUninitialized: |
henrika | 86eff72 | 2016-06-21 11:26:47 +0200 | [diff] [blame] | 741 | RTCLog(@"VPAU state: Uninitialized"); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 742 | should_initialize_audio_unit = can_play_or_record; |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 743 | should_start_audio_unit = should_initialize_audio_unit && (playing_ || recording_); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 744 | break; |
| 745 | case VoiceProcessingAudioUnit::kInitialized: |
henrika | 86eff72 | 2016-06-21 11:26:47 +0200 | [diff] [blame] | 746 | RTCLog(@"VPAU state: Initialized"); |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 747 | should_start_audio_unit = can_play_or_record && (playing_ || recording_); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 748 | should_uninitialize_audio_unit = !can_play_or_record; |
| 749 | break; |
| 750 | case VoiceProcessingAudioUnit::kStarted: |
henrika | 86eff72 | 2016-06-21 11:26:47 +0200 | [diff] [blame] | 751 | RTCLog(@"VPAU state: Started"); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 752 | RTC_DCHECK(playing_ || recording_); |
| 753 | should_stop_audio_unit = !can_play_or_record; |
| 754 | should_uninitialize_audio_unit = should_stop_audio_unit; |
| 755 | break; |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 756 | } |
henrika | 45c136b | 2015-10-21 04:11:53 -0700 | [diff] [blame] | 757 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 758 | if (should_initialize_audio_unit) { |
| 759 | RTCLog(@"Initializing audio unit for UpdateAudioUnit"); |
| 760 | ConfigureAudioSession(); |
| 761 | SetupAudioBuffersForActiveAudioSession(); |
| 762 | if (!audio_unit_->Initialize(playout_parameters_.sample_rate())) { |
| 763 | RTCLogError(@"Failed to initialize audio unit."); |
| 764 | return; |
| 765 | } |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 766 | } |
henrika | 45c136b | 2015-10-21 04:11:53 -0700 | [diff] [blame] | 767 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 768 | if (should_start_audio_unit) { |
| 769 | RTCLog(@"Starting audio unit for UpdateAudioUnit"); |
henrika | 86eff72 | 2016-06-21 11:26:47 +0200 | [diff] [blame] | 770 | // Log session settings before trying to start audio streaming. |
| 771 | RTCAudioSession* session = [RTCAudioSession sharedInstance]; |
| 772 | RTCLog(@"%@", session); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 773 | if (!audio_unit_->Start()) { |
| 774 | RTCLogError(@"Failed to start audio unit."); |
| 775 | return; |
| 776 | } |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 777 | } |
henrika | 45c136b | 2015-10-21 04:11:53 -0700 | [diff] [blame] | 778 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 779 | if (should_stop_audio_unit) { |
| 780 | RTCLog(@"Stopping audio unit for UpdateAudioUnit"); |
| 781 | if (!audio_unit_->Stop()) { |
| 782 | RTCLogError(@"Failed to stop audio unit."); |
| 783 | return; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | if (should_uninitialize_audio_unit) { |
| 788 | RTCLog(@"Uninitializing audio unit for UpdateAudioUnit"); |
| 789 | audio_unit_->Uninitialize(); |
| 790 | UnconfigureAudioSession(); |
| 791 | } |
| 792 | } |
| 793 | |
jtteh | f84c1d6 | 2017-04-21 13:56:39 -0700 | [diff] [blame] | 794 | bool AudioDeviceIOS::ConfigureAudioSession() { |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 795 | RTC_DCHECK_RUN_ON(&thread_checker_); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 796 | RTCLog(@"Configuring audio session."); |
| 797 | if (has_configured_session_) { |
| 798 | RTCLogWarning(@"Audio session already configured."); |
jtteh | f84c1d6 | 2017-04-21 13:56:39 -0700 | [diff] [blame] | 799 | return false; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 800 | } |
| 801 | RTCAudioSession* session = [RTCAudioSession sharedInstance]; |
| 802 | [session lockForConfiguration]; |
jtteh | f84c1d6 | 2017-04-21 13:56:39 -0700 | [diff] [blame] | 803 | bool success = [session configureWebRTCSession:nil]; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 804 | [session unlockForConfiguration]; |
jtteh | f84c1d6 | 2017-04-21 13:56:39 -0700 | [diff] [blame] | 805 | if (success) { |
| 806 | has_configured_session_ = true; |
| 807 | RTCLog(@"Configured audio session."); |
| 808 | } else { |
| 809 | RTCLog(@"Failed to configure audio session."); |
| 810 | } |
| 811 | return success; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | void AudioDeviceIOS::UnconfigureAudioSession() { |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 815 | RTC_DCHECK_RUN_ON(&thread_checker_); |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 816 | RTCLog(@"Unconfiguring audio session."); |
| 817 | if (!has_configured_session_) { |
| 818 | RTCLogWarning(@"Audio session already unconfigured."); |
| 819 | return; |
| 820 | } |
| 821 | RTCAudioSession* session = [RTCAudioSession sharedInstance]; |
| 822 | [session lockForConfiguration]; |
| 823 | [session unconfigureWebRTCSession:nil]; |
| 824 | [session unlockForConfiguration]; |
| 825 | has_configured_session_ = false; |
| 826 | RTCLog(@"Unconfigured audio session."); |
henrika | 45c136b | 2015-10-21 04:11:53 -0700 | [diff] [blame] | 827 | } |
| 828 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 829 | bool AudioDeviceIOS::InitPlayOrRecord() { |
| 830 | LOGI() << "InitPlayOrRecord"; |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 831 | RTC_DCHECK_RUN_ON(&thread_checker_); |
henrika | 34911ad | 2015-11-20 15:47:09 +0100 | [diff] [blame] | 832 | |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 833 | // There should be no audio unit at this point. |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 834 | if (!CreateAudioUnit()) { |
henrika | 34911ad | 2015-11-20 15:47:09 +0100 | [diff] [blame] | 835 | return false; |
| 836 | } |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 837 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 838 | RTCAudioSession* session = [RTCAudioSession sharedInstance]; |
| 839 | // Subscribe to audio session events. |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 840 | [session pushDelegate:audio_session_observer_]; |
jtteh | 5171a7f | 2017-05-09 15:09:37 -0700 | [diff] [blame] | 841 | is_interrupted_ = session.isInterrupted ? true : false; |
henrika | 45c136b | 2015-10-21 04:11:53 -0700 | [diff] [blame] | 842 | |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 843 | // Lock the session to make configuration changes. |
| 844 | [session lockForConfiguration]; |
| 845 | NSError* error = nil; |
| 846 | if (![session beginWebRTCSession:&error]) { |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 847 | [session unlockForConfiguration]; |
Mirko Bonadei | 72c4250 | 2017-11-09 09:33:23 +0100 | [diff] [blame] | 848 | RTCLogError(@"Failed to begin WebRTC session: %@", error.localizedDescription); |
Jiawei Ou | 5f7d00e | 2018-07-30 09:44:16 -0700 | [diff] [blame] | 849 | audio_unit_.reset(); |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 850 | return false; |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 851 | } |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 852 | |
henrika | e6aca63 | 2018-01-03 13:58:58 +0100 | [diff] [blame] | 853 | // If we are ready to play or record, and if the audio session can be |
| 854 | // configured, then initialize the audio unit. |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 855 | if (session.canPlayOrRecord) { |
henrika | e6aca63 | 2018-01-03 13:58:58 +0100 | [diff] [blame] | 856 | if (!ConfigureAudioSession()) { |
| 857 | // One possible reason for failure is if an attempt was made to use the |
| 858 | // audio session during or after a Media Services failure. |
| 859 | // See AVAudioSessionErrorCodeMediaServicesFailed for details. |
| 860 | [session unlockForConfiguration]; |
Jiawei Ou | 5f7d00e | 2018-07-30 09:44:16 -0700 | [diff] [blame] | 861 | audio_unit_.reset(); |
henrika | e6aca63 | 2018-01-03 13:58:58 +0100 | [diff] [blame] | 862 | return false; |
| 863 | } |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 864 | SetupAudioBuffersForActiveAudioSession(); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 865 | audio_unit_->Initialize(playout_parameters_.sample_rate()); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | // Release the lock. |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 869 | [session unlockForConfiguration]; |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 870 | return true; |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 871 | } |
| 872 | |
henrika | 34911ad | 2015-11-20 15:47:09 +0100 | [diff] [blame] | 873 | void AudioDeviceIOS::ShutdownPlayOrRecord() { |
henrika | ba35d05 | 2015-07-14 17:04:08 +0200 | [diff] [blame] | 874 | LOGI() << "ShutdownPlayOrRecord"; |
henrika | af35f83 | 2017-06-16 13:22:13 +0200 | [diff] [blame] | 875 | RTC_DCHECK_RUN_ON(&thread_checker_); |
Zeke Chin | 1300caa | 2016-03-18 14:39:11 -0700 | [diff] [blame] | 876 | |
henrika | 41ed7e1 | 2016-06-21 11:41:07 +0200 | [diff] [blame] | 877 | // Stop the audio unit to prevent any additional audio callbacks. |
| 878 | audio_unit_->Stop(); |
| 879 | |
henrika | 86d907c | 2015-09-07 16:09:50 +0200 | [diff] [blame] | 880 | // Close and delete the voice-processing I/O unit. |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 881 | audio_unit_.reset(); |
henrika | 45c136b | 2015-10-21 04:11:53 -0700 | [diff] [blame] | 882 | |
henrika | 3d0e7bb | 2017-06-28 15:17:17 +0200 | [diff] [blame] | 883 | // Detach thread checker for the AURemoteIO::IOThread to ensure that the |
| 884 | // next session uses a fresh thread id. |
| 885 | io_thread_checker_.DetachFromThread(); |
| 886 | |
henrika | 34911ad | 2015-11-20 15:47:09 +0100 | [diff] [blame] | 887 | // Remove audio session notification observers. |
tkchin | e54467f | 2016-03-15 16:54:03 -0700 | [diff] [blame] | 888 | RTCAudioSession* session = [RTCAudioSession sharedInstance]; |
| 889 | [session removeDelegate:audio_session_observer_]; |
henrika | 34911ad | 2015-11-20 15:47:09 +0100 | [diff] [blame] | 890 | |
henrika | 324d9c9 | 2015-07-20 13:09:23 +0200 | [diff] [blame] | 891 | // All I/O should be stopped or paused prior to deactivating the audio |
| 892 | // session, hence we deactivate as last action. |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 893 | [session lockForConfiguration]; |
tkchin | d251196 | 2016-05-06 18:54:15 -0700 | [diff] [blame] | 894 | UnconfigureAudioSession(); |
Tze Kwang Chin | 307a092 | 2016-03-21 13:57:40 -0700 | [diff] [blame] | 895 | [session endWebRTCSession:nil]; |
tkchin | 9f987d3 | 2016-03-12 20:06:28 -0800 | [diff] [blame] | 896 | [session unlockForConfiguration]; |
henrika | 34911ad | 2015-11-20 15:47:09 +0100 | [diff] [blame] | 897 | } |
| 898 | |
henrika | 79445ea | 2018-05-29 16:04:16 +0200 | [diff] [blame] | 899 | void AudioDeviceIOS::PrepareForNewStart() { |
| 900 | LOGI() << "PrepareForNewStart"; |
| 901 | // The audio unit has been stopped and preparations are needed for an upcoming |
| 902 | // restart. It will result in audio callbacks from a new native I/O thread |
| 903 | // which means that we must detach thread checkers here to be prepared for an |
| 904 | // upcoming new audio stream. |
| 905 | io_thread_checker_.DetachFromThread(); |
| 906 | // The audio device buffer must also be informed about the interrupted |
| 907 | // state so it can detach its thread checkers as well. |
| 908 | if (audio_device_buffer_) { |
| 909 | audio_device_buffer_->NativeAudioPlayoutInterrupted(); |
| 910 | audio_device_buffer_->NativeAudioRecordingInterrupted(); |
| 911 | } |
| 912 | } |
| 913 | |
sjlee@webrtc.org | 4b42508 | 2012-09-10 17:58:21 +0000 | [diff] [blame] | 914 | } // namespace webrtc |