Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(845)

Side by Side Diff: webrtc/modules/audio_device/ios/audio_device_ios.mm

Issue 1435293003: Improved error handling in iOS ADM to avoid race during init (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: nits Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/modules/audio_device/ios/audio_device_ios.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #if !defined(__has_feature) || !__has_feature(objc_arc) 11 #if !defined(__has_feature) || !__has_feature(objc_arc)
12 #error "This file requires ARC support." 12 #error "This file requires ARC support."
13 #endif 13 #endif
14 14
15 #import <AVFoundation/AVFoundation.h> 15 #import <AVFoundation/AVFoundation.h>
16 #import <Foundation/Foundation.h> 16 #import <Foundation/Foundation.h>
17 17
18 #include "webrtc/modules/audio_device/ios/audio_device_ios.h" 18 #include "webrtc/modules/audio_device/ios/audio_device_ios.h"
19 19
20 #include "webrtc/base/atomicops.h" 20 #include "webrtc/base/atomicops.h"
21 #include "webrtc/base/checks.h" 21 #include "webrtc/base/checks.h"
22 #include "webrtc/base/criticalsection.h"
22 #include "webrtc/base/logging.h" 23 #include "webrtc/base/logging.h"
24 #include "webrtc/base/thread_annotations.h"
23 #include "webrtc/modules/audio_device/fine_audio_buffer.h" 25 #include "webrtc/modules/audio_device/fine_audio_buffer.h"
24 #include "webrtc/modules/utility/include/helpers_ios.h" 26 #include "webrtc/modules/utility/include/helpers_ios.h"
25 27
26 namespace webrtc { 28 namespace webrtc {
27 29
30 // Protects |g_audio_session_users|.
31 static rtc::GlobalLockPod g_lock;
32
33 // Counts number of users (=instances of this object) who needs an active
34 // audio session. This variable is used to ensure that we only activate an audio
35 // session for the first user and deactivate it for the last.
36 // Member is static to ensure that the value is counted for all instances
37 // and not per instance.
38 static int g_audio_session_users GUARDED_BY(g_lock) = 0;
39
28 #define LOGI() LOG(LS_INFO) << "AudioDeviceIOS::" 40 #define LOGI() LOG(LS_INFO) << "AudioDeviceIOS::"
29 41
30 #define LOG_AND_RETURN_IF_ERROR(error, message) \ 42 #define LOG_AND_RETURN_IF_ERROR(error, message) \
31 do { \ 43 do { \
32 OSStatus err = error; \ 44 OSStatus err = error; \
33 if (err) { \ 45 if (err) { \
34 LOG(LS_ERROR) << message << ": " << err; \ 46 LOG(LS_ERROR) << message << ": " << err; \
35 return false; \ 47 return false; \
36 } \ 48 } \
37 } while (0) 49 } while (0)
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 // Number of bytes per audio sample for 16-bit signed integer representation. 82 // Number of bytes per audio sample for 16-bit signed integer representation.
71 const UInt32 kBytesPerSample = 2; 83 const UInt32 kBytesPerSample = 2;
72 // Hardcoded delay estimates based on real measurements. 84 // Hardcoded delay estimates based on real measurements.
73 // TODO(henrika): these value is not used in combination with built-in AEC. 85 // TODO(henrika): these value is not used in combination with built-in AEC.
74 // Can most likely be removed. 86 // Can most likely be removed.
75 const UInt16 kFixedPlayoutDelayEstimate = 30; 87 const UInt16 kFixedPlayoutDelayEstimate = 30;
76 const UInt16 kFixedRecordDelayEstimate = 30; 88 const UInt16 kFixedRecordDelayEstimate = 30;
77 89
78 using ios::CheckAndLogError; 90 using ios::CheckAndLogError;
79 91
92 // Verifies that the current audio session supports input audio and that the
93 // required category and mode are enabled.
94 static bool VerifyAudioSession(AVAudioSession* session) {
95 LOG(LS_INFO) << "VerifyAudioSession";
96 // Ensure that the device currently supports audio input.
97 if (!session.isInputAvailable) {
98 LOG(LS_ERROR) << "No audio input path is available!";
99 return false;
100 }
101
102 // Ensure that the required category and mode are actually activated.
103 if (![session.category isEqualToString:AVAudioSessionCategoryPlayAndRecord]) {
104 LOG(LS_ERROR)
105 << "Failed to set category to AVAudioSessionCategoryPlayAndRecord";
106 return false;
107 }
108 if (![session.mode isEqualToString:AVAudioSessionModeVoiceChat]) {
109 LOG(LS_ERROR) << "Failed to set mode to AVAudioSessionModeVoiceChat";
110 return false;
111 }
112 return true;
113 }
114
80 // Activates an audio session suitable for full duplex VoIP sessions when 115 // Activates an audio session suitable for full duplex VoIP sessions when
81 // |activate| is true. Also sets the preferred sample rate and IO buffer 116 // |activate| is true. Also sets the preferred sample rate and IO buffer
82 // duration. Deactivates an active audio session if |activate| is set to false. 117 // duration. Deactivates an active audio session if |activate| is set to false.
83 static void ActivateAudioSession(AVAudioSession* session, bool activate) { 118 static bool ActivateAudioSession(AVAudioSession* session, bool activate)
119 EXCLUSIVE_LOCKS_REQUIRED(g_lock) {
84 LOG(LS_INFO) << "ActivateAudioSession(" << activate << ")"; 120 LOG(LS_INFO) << "ActivateAudioSession(" << activate << ")";
85 @autoreleasepool { 121 @autoreleasepool {
86 NSError* error = nil; 122 NSError* error = nil;
87 BOOL success = NO; 123 BOOL success = NO;
88 124
89 if (!activate) { 125 if (!activate) {
90 // Deactivate the audio session using an extra option and then return. 126 // Deactivate the audio session using an extra option and then return.
91 // AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation is used to 127 // AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation is used to
92 // ensure that other audio sessions that were interrupted by our session 128 // ensure that other audio sessions that were interrupted by our session
93 // can return to their active state. It is recommended for VoIP apps to 129 // can return to their active state. It is recommended for VoIP apps to
94 // use this option. 130 // use this option.
95 success = [session 131 success = [session
96 setActive:NO 132 setActive:NO
97 withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation 133 withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
98 error:&error]; 134 error:&error];
99 RTC_DCHECK(CheckAndLogError(success, error)); 135 return CheckAndLogError(success, error);
100 return;
101 } 136 }
102 137
103 // Go ahead and active our own audio session since |activate| is true. 138 // Go ahead and active our own audio session since |activate| is true.
104 // Use a category which supports simultaneous recording and playback. 139 // Use a category which supports simultaneous recording and playback.
105 // By default, using this category implies that our app’s audio is 140 // By default, using this category implies that our app’s audio is
106 // nonmixable, hence activating the session will interrupt any other 141 // nonmixable, hence activating the session will interrupt any other
107 // audio sessions which are also nonmixable. 142 // audio sessions which are also nonmixable.
108 if (session.category != AVAudioSessionCategoryPlayAndRecord) { 143 if (session.category != AVAudioSessionCategoryPlayAndRecord) {
109 error = nil; 144 error = nil;
110 success = [session setCategory:AVAudioSessionCategoryPlayAndRecord 145 success = [session setCategory:AVAudioSessionCategoryPlayAndRecord
(...skipping 11 matching lines...) Expand all
122 157
123 // Set the session's sample rate or the hardware sample rate. 158 // Set the session's sample rate or the hardware sample rate.
124 // It is essential that we use the same sample rate as stream format 159 // It is essential that we use the same sample rate as stream format
125 // to ensure that the I/O unit does not have to do sample rate conversion. 160 // to ensure that the I/O unit does not have to do sample rate conversion.
126 error = nil; 161 error = nil;
127 success = 162 success =
128 [session setPreferredSampleRate:kPreferredSampleRate error:&error]; 163 [session setPreferredSampleRate:kPreferredSampleRate error:&error];
129 RTC_DCHECK(CheckAndLogError(success, error)); 164 RTC_DCHECK(CheckAndLogError(success, error));
130 165
131 // Set the preferred audio I/O buffer duration, in seconds. 166 // Set the preferred audio I/O buffer duration, in seconds.
132 // TODO(henrika): add more comments here.
133 error = nil; 167 error = nil;
134 success = [session setPreferredIOBufferDuration:kPreferredIOBufferDuration 168 success = [session setPreferredIOBufferDuration:kPreferredIOBufferDuration
135 error:&error]; 169 error:&error];
136 RTC_DCHECK(CheckAndLogError(success, error)); 170 RTC_DCHECK(CheckAndLogError(success, error));
137 171
138 // Activate the audio session. Activation can fail if another active audio 172 // Activate the audio session. Activation can fail if another active audio
139 // session (e.g. phone call) has higher priority than ours. 173 // session (e.g. phone call) has higher priority than ours.
140 error = nil; 174 error = nil;
141 success = [session setActive:YES error:&error]; 175 success = [session setActive:YES error:&error];
142 RTC_DCHECK(CheckAndLogError(success, error)); 176 if (!CheckAndLogError(success, error)) {
143 RTC_CHECK(session.isInputAvailable) << "No input path is available!"; 177 return false;
178 }
144 179
145 // Ensure that category and mode are actually activated. 180 // Ensure that the active audio session has the correct category and mode.
146 RTC_DCHECK( 181 if (!VerifyAudioSession(session)) {
147 [session.category isEqualToString:AVAudioSessionCategoryPlayAndRecord]); 182 LOG(LS_ERROR) << "Failed to verify audio session category and mode";
148 RTC_DCHECK([session.mode isEqualToString:AVAudioSessionModeVoiceChat]); 183 return false;
184 }
149 185
150 // Try to set the preferred number of hardware audio channels. These calls 186 // Try to set the preferred number of hardware audio channels. These calls
151 // must be done after setting the audio session’s category and mode and 187 // must be done after setting the audio session’s category and mode and
152 // activating the session. 188 // activating the session.
153 // We try to use mono in both directions to save resources and format 189 // We try to use mono in both directions to save resources and format
154 // conversions in the audio unit. Some devices does only support stereo; 190 // conversions in the audio unit. Some devices does only support stereo;
155 // e.g. wired headset on iPhone 6. 191 // e.g. wired headset on iPhone 6.
156 // TODO(henrika): add support for stereo if needed. 192 // TODO(henrika): add support for stereo if needed.
157 error = nil; 193 error = nil;
158 success = 194 success =
159 [session setPreferredInputNumberOfChannels:kPreferredNumberOfChannels 195 [session setPreferredInputNumberOfChannels:kPreferredNumberOfChannels
160 error:&error]; 196 error:&error];
161 RTC_DCHECK(CheckAndLogError(success, error)); 197 RTC_DCHECK(CheckAndLogError(success, error));
162 error = nil; 198 error = nil;
163 success = 199 success =
164 [session setPreferredOutputNumberOfChannels:kPreferredNumberOfChannels 200 [session setPreferredOutputNumberOfChannels:kPreferredNumberOfChannels
165 error:&error]; 201 error:&error];
166 RTC_DCHECK(CheckAndLogError(success, error)); 202 RTC_DCHECK(CheckAndLogError(success, error));
203 return true;
167 } 204 }
168 } 205 }
169 206
207 // An application can create more than one ADM and start audio streaming
208 // for all of them. It is essential that we only activate the app's audio
209 // session once (for the first one) and deactivate it once (for the last).
210 static bool ActivateAudioSession() {
211 LOGI() << "ActivateAudioSession";
212 rtc::GlobalLockScope ls(&g_lock);
213 if (g_audio_session_users == 0) {
214 // The system provides an audio session object upon launch of an
215 // application. However, we must initialize the session in order to
216 // handle interruptions. Implicit initialization occurs when obtaining
217 // a reference to the AVAudioSession object.
218 AVAudioSession* session = [AVAudioSession sharedInstance];
219 // Try to activate the audio session and ask for a set of preferred audio
220 // parameters.
221 if (!ActivateAudioSession(session, true)) {
222 LOG(LS_ERROR) << "Failed to activate the audio session";
223 return false;
224 }
225 LOG(LS_INFO) << "The audio session is now activated";
226 }
227 ++g_audio_session_users;
228 LOG(LS_INFO) << "Number of audio session users: " << g_audio_session_users;
229 return true;
230 }
231
232 // If more than one object is using the audio session, ensure that only the
233 // last object deactivates. Apple recommends: "activate your audio session
234 // only as needed and deactivate it when you are not using audio".
235 static bool DeactivateAudioSession() {
236 LOGI() << "DeactivateAudioSession";
237 rtc::GlobalLockScope ls(&g_lock);
238 if (g_audio_session_users == 1) {
239 AVAudioSession* session = [AVAudioSession sharedInstance];
240 if (!ActivateAudioSession(session, false)) {
241 LOG(LS_ERROR) << "Failed to deactivate the audio session";
242 return false;
243 }
244 LOG(LS_INFO) << "Our audio session is now deactivated";
245 }
246 --g_audio_session_users;
247 LOG(LS_INFO) << "Number of audio session users: " << g_audio_session_users;
248 return true;
249 }
250
170 #if !defined(NDEBUG) 251 #if !defined(NDEBUG)
171 // Helper method for printing out an AudioStreamBasicDescription structure. 252 // Helper method for printing out an AudioStreamBasicDescription structure.
172 static void LogABSD(AudioStreamBasicDescription absd) { 253 static void LogABSD(AudioStreamBasicDescription absd) {
173 char formatIDString[5]; 254 char formatIDString[5];
174 UInt32 formatID = CFSwapInt32HostToBig(absd.mFormatID); 255 UInt32 formatID = CFSwapInt32HostToBig(absd.mFormatID);
175 bcopy(&formatID, formatIDString, 4); 256 bcopy(&formatID, formatIDString, 4);
176 formatIDString[4] = '\0'; 257 formatIDString[4] = '\0';
177 LOG(LS_INFO) << "LogABSD"; 258 LOG(LS_INFO) << "LogABSD";
178 LOG(LS_INFO) << " sample rate: " << absd.mSampleRate; 259 LOG(LS_INFO) << " sample rate: " << absd.mSampleRate;
179 LOG(LS_INFO) << " format ID: " << formatIDString; 260 LOG(LS_INFO) << " format ID: " << formatIDString;
(...skipping 25 matching lines...) Expand all
205 playing_(0), 286 playing_(0),
206 initialized_(false), 287 initialized_(false),
207 rec_is_initialized_(false), 288 rec_is_initialized_(false),
208 play_is_initialized_(false), 289 play_is_initialized_(false),
209 audio_interruption_observer_(nullptr), 290 audio_interruption_observer_(nullptr),
210 route_change_observer_(nullptr) { 291 route_change_observer_(nullptr) {
211 LOGI() << "ctor" << ios::GetCurrentThreadDescription(); 292 LOGI() << "ctor" << ios::GetCurrentThreadDescription();
212 } 293 }
213 294
214 AudioDeviceIOS::~AudioDeviceIOS() { 295 AudioDeviceIOS::~AudioDeviceIOS() {
215 LOGI() << "~dtor"; 296 LOGI() << "~dtor" << ios::GetCurrentThreadDescription();
216 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 297 RTC_DCHECK(thread_checker_.CalledOnValidThread());
217 Terminate(); 298 Terminate();
218 } 299 }
219 300
220 void AudioDeviceIOS::AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) { 301 void AudioDeviceIOS::AttachAudioBuffer(AudioDeviceBuffer* audioBuffer) {
221 LOGI() << "AttachAudioBuffer"; 302 LOGI() << "AttachAudioBuffer";
222 RTC_DCHECK(audioBuffer); 303 RTC_DCHECK(audioBuffer);
223 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 304 RTC_DCHECK(thread_checker_.CalledOnValidThread());
224 audio_device_buffer_ = audioBuffer; 305 audio_device_buffer_ = audioBuffer;
225 } 306 }
(...skipping 21 matching lines...) Expand all
247 initialized_ = true; 328 initialized_ = true;
248 return 0; 329 return 0;
249 } 330 }
250 331
251 int32_t AudioDeviceIOS::Terminate() { 332 int32_t AudioDeviceIOS::Terminate() {
252 LOGI() << "Terminate"; 333 LOGI() << "Terminate";
253 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 334 RTC_DCHECK(thread_checker_.CalledOnValidThread());
254 if (!initialized_) { 335 if (!initialized_) {
255 return 0; 336 return 0;
256 } 337 }
257 ShutdownPlayOrRecord(); 338 StopPlayout();
339 StopRecording();
258 initialized_ = false; 340 initialized_ = false;
341 {
342 rtc::GlobalLockScope ls(&g_lock);
343 if (g_audio_session_users != 0) {
344 LOG(LS_WARNING) << "Object is destructed with an active audio session";
345 }
346 RTC_DCHECK_GE(g_audio_session_users, 0);
347 }
259 return 0; 348 return 0;
260 } 349 }
261 350
262 int32_t AudioDeviceIOS::InitPlayout() { 351 int32_t AudioDeviceIOS::InitPlayout() {
263 LOGI() << "InitPlayout"; 352 LOGI() << "InitPlayout";
264 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 353 RTC_DCHECK(thread_checker_.CalledOnValidThread());
265 RTC_DCHECK(initialized_); 354 RTC_DCHECK(initialized_);
266 RTC_DCHECK(!play_is_initialized_); 355 RTC_DCHECK(!play_is_initialized_);
267 RTC_DCHECK(!playing_); 356 RTC_DCHECK(!playing_);
268 if (!rec_is_initialized_) { 357 if (!rec_is_initialized_) {
269 if (!InitPlayOrRecord()) { 358 if (!InitPlayOrRecord()) {
270 LOG_F(LS_ERROR) << "InitPlayOrRecord failed!"; 359 LOG_F(LS_ERROR) << "InitPlayOrRecord failed for InitPlayout!";
271 return -1; 360 return -1;
272 } 361 }
273 } 362 }
274 play_is_initialized_ = true; 363 play_is_initialized_ = true;
275 return 0; 364 return 0;
276 } 365 }
277 366
278 int32_t AudioDeviceIOS::InitRecording() { 367 int32_t AudioDeviceIOS::InitRecording() {
279 LOGI() << "InitRecording"; 368 LOGI() << "InitRecording";
280 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 369 RTC_DCHECK(thread_checker_.CalledOnValidThread());
281 RTC_DCHECK(initialized_); 370 RTC_DCHECK(initialized_);
282 RTC_DCHECK(!rec_is_initialized_); 371 RTC_DCHECK(!rec_is_initialized_);
283 RTC_DCHECK(!recording_); 372 RTC_DCHECK(!recording_);
284 if (!play_is_initialized_) { 373 if (!play_is_initialized_) {
285 if (!InitPlayOrRecord()) { 374 if (!InitPlayOrRecord()) {
286 LOG_F(LS_ERROR) << "InitPlayOrRecord failed!"; 375 LOG_F(LS_ERROR) << "InitPlayOrRecord failed for InitRecording!";
287 return -1; 376 return -1;
288 } 377 }
289 } 378 }
290 rec_is_initialized_ = true; 379 rec_is_initialized_ = true;
291 return 0; 380 return 0;
292 } 381 }
293 382
294 int32_t AudioDeviceIOS::StartPlayout() { 383 int32_t AudioDeviceIOS::StartPlayout() {
295 LOGI() << "StartPlayout"; 384 LOGI() << "StartPlayout";
296 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 385 RTC_DCHECK(thread_checker_.CalledOnValidThread());
297 RTC_DCHECK(play_is_initialized_); 386 RTC_DCHECK(play_is_initialized_);
298 RTC_DCHECK(!playing_); 387 RTC_DCHECK(!playing_);
299 fine_audio_buffer_->ResetPlayout(); 388 fine_audio_buffer_->ResetPlayout();
300 if (!recording_) { 389 if (!recording_) {
301 OSStatus result = AudioOutputUnitStart(vpio_unit_); 390 OSStatus result = AudioOutputUnitStart(vpio_unit_);
302 if (result != noErr) { 391 if (result != noErr) {
303 LOG_F(LS_ERROR) << "AudioOutputUnitStart failed: " << result; 392 LOG_F(LS_ERROR) << "AudioOutputUnitStart failed for StartPlayout: "
393 << result;
304 return -1; 394 return -1;
305 } 395 }
396 LOG(LS_INFO) << "Voice-Processing I/O audio unit is now started";
306 } 397 }
307 rtc::AtomicOps::ReleaseStore(&playing_, 1); 398 rtc::AtomicOps::ReleaseStore(&playing_, 1);
308 return 0; 399 return 0;
309 } 400 }
310 401
311 int32_t AudioDeviceIOS::StopPlayout() { 402 int32_t AudioDeviceIOS::StopPlayout() {
312 LOGI() << "StopPlayout"; 403 LOGI() << "StopPlayout";
313 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 404 RTC_DCHECK(thread_checker_.CalledOnValidThread());
314 if (!play_is_initialized_ || !playing_) { 405 if (!play_is_initialized_ || !playing_) {
315 return 0; 406 return 0;
316 } 407 }
317 if (!recording_) { 408 if (!recording_) {
318 ShutdownPlayOrRecord(); 409 ShutdownPlayOrRecord();
319 } 410 }
320 play_is_initialized_ = false; 411 play_is_initialized_ = false;
321 rtc::AtomicOps::ReleaseStore(&playing_, 0); 412 rtc::AtomicOps::ReleaseStore(&playing_, 0);
322 return 0; 413 return 0;
323 } 414 }
324 415
325 int32_t AudioDeviceIOS::StartRecording() { 416 int32_t AudioDeviceIOS::StartRecording() {
326 LOGI() << "StartRecording"; 417 LOGI() << "StartRecording";
327 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 418 RTC_DCHECK(thread_checker_.CalledOnValidThread());
328 RTC_DCHECK(rec_is_initialized_); 419 RTC_DCHECK(rec_is_initialized_);
329 RTC_DCHECK(!recording_); 420 RTC_DCHECK(!recording_);
330 fine_audio_buffer_->ResetRecord(); 421 fine_audio_buffer_->ResetRecord();
331 if (!playing_) { 422 if (!playing_) {
332 OSStatus result = AudioOutputUnitStart(vpio_unit_); 423 OSStatus result = AudioOutputUnitStart(vpio_unit_);
333 if (result != noErr) { 424 if (result != noErr) {
334 LOG_F(LS_ERROR) << "AudioOutputUnitStart failed: " << result; 425 LOG_F(LS_ERROR) << "AudioOutputUnitStart failed for StartRecording: "
426 << result;
335 return -1; 427 return -1;
336 } 428 }
429 LOG(LS_INFO) << "Voice-Processing I/O audio unit is now started";
337 } 430 }
338 rtc::AtomicOps::ReleaseStore(&recording_, 1); 431 rtc::AtomicOps::ReleaseStore(&recording_, 1);
339 return 0; 432 return 0;
340 } 433 }
341 434
342 int32_t AudioDeviceIOS::StopRecording() { 435 int32_t AudioDeviceIOS::StopRecording() {
343 LOGI() << "StopRecording"; 436 LOGI() << "StopRecording";
344 RTC_DCHECK(thread_checker_.CalledOnValidThread()); 437 RTC_DCHECK(thread_checker_.CalledOnValidThread());
345 if (!rec_is_initialized_ || !recording_) { 438 if (!rec_is_initialized_ || !recording_) {
346 return 0; 439 return 0;
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 record_audio_buffer_.reset(new SInt8[data_byte_size]); 725 record_audio_buffer_.reset(new SInt8[data_byte_size]);
633 audio_record_buffer_list_.mNumberBuffers = 1; 726 audio_record_buffer_list_.mNumberBuffers = 1;
634 AudioBuffer* audio_buffer = &audio_record_buffer_list_.mBuffers[0]; 727 AudioBuffer* audio_buffer = &audio_record_buffer_list_.mBuffers[0];
635 audio_buffer->mNumberChannels = record_parameters_.channels(); 728 audio_buffer->mNumberChannels = record_parameters_.channels();
636 audio_buffer->mDataByteSize = data_byte_size; 729 audio_buffer->mDataByteSize = data_byte_size;
637 audio_buffer->mData = record_audio_buffer_.get(); 730 audio_buffer->mData = record_audio_buffer_.get();
638 } 731 }
639 732
640 bool AudioDeviceIOS::SetupAndInitializeVoiceProcessingAudioUnit() { 733 bool AudioDeviceIOS::SetupAndInitializeVoiceProcessingAudioUnit() {
641 LOGI() << "SetupAndInitializeVoiceProcessingAudioUnit"; 734 LOGI() << "SetupAndInitializeVoiceProcessingAudioUnit";
642 RTC_DCHECK(!vpio_unit_); 735 RTC_DCHECK(!vpio_unit_) << "VoiceProcessingIO audio unit already exists";
643 // Create an audio component description to identify the Voice-Processing 736 // Create an audio component description to identify the Voice-Processing
644 // I/O audio unit. 737 // I/O audio unit.
645 AudioComponentDescription vpio_unit_description; 738 AudioComponentDescription vpio_unit_description;
646 vpio_unit_description.componentType = kAudioUnitType_Output; 739 vpio_unit_description.componentType = kAudioUnitType_Output;
647 vpio_unit_description.componentSubType = kAudioUnitSubType_VoiceProcessingIO; 740 vpio_unit_description.componentSubType = kAudioUnitSubType_VoiceProcessingIO;
648 vpio_unit_description.componentManufacturer = kAudioUnitManufacturer_Apple; 741 vpio_unit_description.componentManufacturer = kAudioUnitManufacturer_Apple;
649 vpio_unit_description.componentFlags = 0; 742 vpio_unit_description.componentFlags = 0;
650 vpio_unit_description.componentFlagsMask = 0; 743 vpio_unit_description.componentFlagsMask = 0;
651 // Obtain an audio unit instance given the description. 744 // Obtain an audio unit instance given the description.
652 AudioComponent found_vpio_unit_ref = 745 AudioComponent found_vpio_unit_ref =
653 AudioComponentFindNext(nullptr, &vpio_unit_description); 746 AudioComponentFindNext(nullptr, &vpio_unit_description);
654 747
655 // Create a Voice-Processing IO audio unit. 748 // Create a Voice-Processing IO audio unit.
656 LOG_AND_RETURN_IF_ERROR( 749 OSStatus result = noErr;
657 AudioComponentInstanceNew(found_vpio_unit_ref, &vpio_unit_), 750 result = AudioComponentInstanceNew(found_vpio_unit_ref, &vpio_unit_);
658 "Failed to create a VoiceProcessingIO audio unit"); 751 if (result != noErr) {
752 vpio_unit_ = nullptr;
753 LOG(LS_ERROR) << "AudioComponentInstanceNew failed: " << result;
754 return false;
755 }
659 756
660 // A VP I/O unit's bus 1 connects to input hardware (microphone). Enable 757 // A VP I/O unit's bus 1 connects to input hardware (microphone). Enable
661 // input on the input scope of the input element. 758 // input on the input scope of the input element.
662 AudioUnitElement input_bus = 1; 759 AudioUnitElement input_bus = 1;
663 UInt32 enable_input = 1; 760 UInt32 enable_input = 1;
664 LOG_AND_RETURN_IF_ERROR( 761 result = AudioUnitSetProperty(vpio_unit_, kAudioOutputUnitProperty_EnableIO,
665 AudioUnitSetProperty(vpio_unit_, kAudioOutputUnitProperty_EnableIO, 762 kAudioUnitScope_Input, input_bus, &enable_input,
666 kAudioUnitScope_Input, input_bus, &enable_input, 763 sizeof(enable_input));
667 sizeof(enable_input)), 764 if (result != noErr) {
668 "Failed to enable input on input scope of input element"); 765 DisposeAudioUnit();
766 LOG(LS_ERROR) << "Failed to enable input on input scope of input element: "
767 << result;
768 return false;
769 }
669 770
670 // A VP I/O unit's bus 0 connects to output hardware (speaker). Enable 771 // A VP I/O unit's bus 0 connects to output hardware (speaker). Enable
671 // output on the output scope of the output element. 772 // output on the output scope of the output element.
672 AudioUnitElement output_bus = 0; 773 AudioUnitElement output_bus = 0;
673 UInt32 enable_output = 1; 774 UInt32 enable_output = 1;
674 LOG_AND_RETURN_IF_ERROR( 775 result = AudioUnitSetProperty(vpio_unit_, kAudioOutputUnitProperty_EnableIO,
675 AudioUnitSetProperty(vpio_unit_, kAudioOutputUnitProperty_EnableIO, 776 kAudioUnitScope_Output, output_bus,
676 kAudioUnitScope_Output, output_bus, &enable_output, 777 &enable_output, sizeof(enable_output));
677 sizeof(enable_output)), 778 if (result != noErr) {
678 "Failed to enable output on output scope of output element"); 779 DisposeAudioUnit();
780 LOG(LS_ERROR)
781 << "Failed to enable output on output scope of output element: "
782 << result;
783 return false;
784 }
679 785
680 // Set the application formats for input and output: 786 // Set the application formats for input and output:
681 // - use same format in both directions 787 // - use same format in both directions
682 // - avoid resampling in the I/O unit by using the hardware sample rate 788 // - avoid resampling in the I/O unit by using the hardware sample rate
683 // - linear PCM => noncompressed audio data format with one frame per packet 789 // - linear PCM => noncompressed audio data format with one frame per packet
684 // - no need to specify interleaving since only mono is supported 790 // - no need to specify interleaving since only mono is supported
685 AudioStreamBasicDescription application_format = {0}; 791 AudioStreamBasicDescription application_format = {0};
686 UInt32 size = sizeof(application_format); 792 UInt32 size = sizeof(application_format);
687 RTC_DCHECK_EQ(playout_parameters_.sample_rate(), 793 RTC_DCHECK_EQ(playout_parameters_.sample_rate(),
688 record_parameters_.sample_rate()); 794 record_parameters_.sample_rate());
689 RTC_DCHECK_EQ(1, kPreferredNumberOfChannels); 795 RTC_DCHECK_EQ(1, kPreferredNumberOfChannels);
690 application_format.mSampleRate = playout_parameters_.sample_rate(); 796 application_format.mSampleRate = playout_parameters_.sample_rate();
691 application_format.mFormatID = kAudioFormatLinearPCM; 797 application_format.mFormatID = kAudioFormatLinearPCM;
692 application_format.mFormatFlags = 798 application_format.mFormatFlags =
693 kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked; 799 kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
694 application_format.mBytesPerPacket = kBytesPerSample; 800 application_format.mBytesPerPacket = kBytesPerSample;
695 application_format.mFramesPerPacket = 1; // uncompressed 801 application_format.mFramesPerPacket = 1; // uncompressed
696 application_format.mBytesPerFrame = kBytesPerSample; 802 application_format.mBytesPerFrame = kBytesPerSample;
697 application_format.mChannelsPerFrame = kPreferredNumberOfChannels; 803 application_format.mChannelsPerFrame = kPreferredNumberOfChannels;
698 application_format.mBitsPerChannel = 8 * kBytesPerSample; 804 application_format.mBitsPerChannel = 8 * kBytesPerSample;
699 // Store the new format. 805 // Store the new format.
700 application_format_ = application_format; 806 application_format_ = application_format;
701 #if !defined(NDEBUG) 807 #if !defined(NDEBUG)
702 LogABSD(application_format_); 808 LogABSD(application_format_);
703 #endif 809 #endif
704 810
705 // Set the application format on the output scope of the input element/bus. 811 // Set the application format on the output scope of the input element/bus.
706 LOG_AND_RETURN_IF_ERROR( 812 result = AudioUnitSetProperty(vpio_unit_, kAudioUnitProperty_StreamFormat,
707 AudioUnitSetProperty(vpio_unit_, kAudioUnitProperty_StreamFormat, 813 kAudioUnitScope_Output, input_bus,
708 kAudioUnitScope_Output, input_bus, 814 &application_format, size);
709 &application_format, size), 815 if (result != noErr) {
710 "Failed to set application format on output scope of input element"); 816 DisposeAudioUnit();
817 LOG(LS_ERROR)
818 << "Failed to set application format on output scope of input bus: "
819 << result;
820 return false;
821 }
711 822
712 // Set the application format on the input scope of the output element/bus. 823 // Set the application format on the input scope of the output element/bus.
713 LOG_AND_RETURN_IF_ERROR( 824 result = AudioUnitSetProperty(vpio_unit_, kAudioUnitProperty_StreamFormat,
714 AudioUnitSetProperty(vpio_unit_, kAudioUnitProperty_StreamFormat, 825 kAudioUnitScope_Input, output_bus,
715 kAudioUnitScope_Input, output_bus, 826 &application_format, size);
716 &application_format, size), 827 if (result != noErr) {
717 "Failed to set application format on input scope of output element"); 828 DisposeAudioUnit();
829 LOG(LS_ERROR)
830 << "Failed to set application format on input scope of output bus: "
831 << result;
832 return false;
833 }
718 834
719 // Specify the callback function that provides audio samples to the audio 835 // Specify the callback function that provides audio samples to the audio
720 // unit. 836 // unit.
721 AURenderCallbackStruct render_callback; 837 AURenderCallbackStruct render_callback;
722 render_callback.inputProc = GetPlayoutData; 838 render_callback.inputProc = GetPlayoutData;
723 render_callback.inputProcRefCon = this; 839 render_callback.inputProcRefCon = this;
724 LOG_AND_RETURN_IF_ERROR( 840 result = AudioUnitSetProperty(
725 AudioUnitSetProperty(vpio_unit_, kAudioUnitProperty_SetRenderCallback, 841 vpio_unit_, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input,
726 kAudioUnitScope_Input, output_bus, &render_callback, 842 output_bus, &render_callback, sizeof(render_callback));
727 sizeof(render_callback)), 843 if (result != noErr) {
728 "Failed to specify the render callback on the output element"); 844 DisposeAudioUnit();
845 LOG(LS_ERROR) << "Failed to specify the render callback on the output bus: "
846 << result;
847 return false;
848 }
729 849
730 // Disable AU buffer allocation for the recorder, we allocate our own. 850 // Disable AU buffer allocation for the recorder, we allocate our own.
731 // TODO(henrika): not sure that it actually saves resource to make this call. 851 // TODO(henrika): not sure that it actually saves resource to make this call.
732 UInt32 flag = 0; 852 UInt32 flag = 0;
733 LOG_AND_RETURN_IF_ERROR( 853 result = AudioUnitSetProperty(
734 AudioUnitSetProperty(vpio_unit_, kAudioUnitProperty_ShouldAllocateBuffer, 854 vpio_unit_, kAudioUnitProperty_ShouldAllocateBuffer,
735 kAudioUnitScope_Output, input_bus, &flag, 855 kAudioUnitScope_Output, input_bus, &flag, sizeof(flag));
736 sizeof(flag)), 856 if (result != noErr) {
737 "Failed to disable buffer allocation on the input element"); 857 DisposeAudioUnit();
858 LOG(LS_ERROR) << "Failed to disable buffer allocation on the input bus: "
859 << result;
860 }
738 861
739 // Specify the callback to be called by the I/O thread to us when input audio 862 // Specify the callback to be called by the I/O thread to us when input audio
740 // is available. The recorded samples can then be obtained by calling the 863 // is available. The recorded samples can then be obtained by calling the
741 // AudioUnitRender() method. 864 // AudioUnitRender() method.
742 AURenderCallbackStruct input_callback; 865 AURenderCallbackStruct input_callback;
743 input_callback.inputProc = RecordedDataIsAvailable; 866 input_callback.inputProc = RecordedDataIsAvailable;
744 input_callback.inputProcRefCon = this; 867 input_callback.inputProcRefCon = this;
745 LOG_AND_RETURN_IF_ERROR( 868 result = AudioUnitSetProperty(vpio_unit_,
746 AudioUnitSetProperty(vpio_unit_, 869 kAudioOutputUnitProperty_SetInputCallback,
747 kAudioOutputUnitProperty_SetInputCallback, 870 kAudioUnitScope_Global, input_bus,
748 kAudioUnitScope_Global, input_bus, &input_callback, 871 &input_callback, sizeof(input_callback));
749 sizeof(input_callback)), 872 if (result != noErr) {
750 "Failed to specify the input callback on the input element"); 873 DisposeAudioUnit();
874 LOG(LS_ERROR) << "Failed to specify the input callback on the input bus: "
875 << result;
876 }
751 877
752 // Initialize the Voice-Processing I/O unit instance. 878 // Initialize the Voice-Processing I/O unit instance.
753 LOG_AND_RETURN_IF_ERROR(AudioUnitInitialize(vpio_unit_), 879 result = AudioUnitInitialize(vpio_unit_);
754 "Failed to initialize the Voice-Processing I/O unit"); 880 if (result != noErr) {
881 result = AudioUnitUninitialize(vpio_unit_);
882 if (result != noErr) {
883 LOG_F(LS_ERROR) << "AudioUnitUninitialize failed: " << result;
884 }
885 DisposeAudioUnit();
886 LOG(LS_ERROR) << "Failed to initialize the Voice-Processing I/O unit: "
887 << result;
888 return false;
889 }
755 return true; 890 return true;
756 } 891 }
757 892
758 bool AudioDeviceIOS::RestartAudioUnitWithNewFormat(float sample_rate) { 893 bool AudioDeviceIOS::RestartAudioUnitWithNewFormat(float sample_rate) {
759 LOGI() << "RestartAudioUnitWithNewFormat(sample_rate=" << sample_rate << ")"; 894 LOGI() << "RestartAudioUnitWithNewFormat(sample_rate=" << sample_rate << ")";
760 // Stop the active audio unit. 895 // Stop the active audio unit.
761 LOG_AND_RETURN_IF_ERROR(AudioOutputUnitStop(vpio_unit_), 896 LOG_AND_RETURN_IF_ERROR(AudioOutputUnitStop(vpio_unit_),
762 "Failed to stop the the Voice-Processing I/O unit"); 897 "Failed to stop the the Voice-Processing I/O unit");
763 898
764 // The stream format is about to be changed and it requires that we first 899 // The stream format is about to be changed and it requires that we first
(...skipping 18 matching lines...) Expand all
783 "Failed to initialize the Voice-Processing I/O unit"); 918 "Failed to initialize the Voice-Processing I/O unit");
784 919
785 // Start rendering audio using the new format. 920 // Start rendering audio using the new format.
786 LOG_AND_RETURN_IF_ERROR(AudioOutputUnitStart(vpio_unit_), 921 LOG_AND_RETURN_IF_ERROR(AudioOutputUnitStart(vpio_unit_),
787 "Failed to start the Voice-Processing I/O unit"); 922 "Failed to start the Voice-Processing I/O unit");
788 return true; 923 return true;
789 } 924 }
790 925
791 bool AudioDeviceIOS::InitPlayOrRecord() { 926 bool AudioDeviceIOS::InitPlayOrRecord() {
792 LOGI() << "InitPlayOrRecord"; 927 LOGI() << "InitPlayOrRecord";
928 // Activate the audio session if not already activated.
929 if (!ActivateAudioSession()) {
930 return false;
931 }
932
933 // Ensure that the active audio session has the correct category and mode.
793 AVAudioSession* session = [AVAudioSession sharedInstance]; 934 AVAudioSession* session = [AVAudioSession sharedInstance];
794 // Activate the audio session and ask for a set of preferred audio parameters. 935 if (!VerifyAudioSession(session)) {
795 ActivateAudioSession(session, true); 936 DeactivateAudioSession();
937 LOG(LS_ERROR) << "Failed to verify audio session category and mode";
938 return false;
939 }
796 940
797 // Start observing audio session interruptions and route changes. 941 // Start observing audio session interruptions and route changes.
798 RegisterNotificationObservers(); 942 RegisterNotificationObservers();
799 943
800 // Ensure that we got what what we asked for in our active audio session. 944 // Ensure that we got what what we asked for in our active audio session.
801 SetupAudioBuffersForActiveAudioSession(); 945 SetupAudioBuffersForActiveAudioSession();
802 946
803 // Create, setup and initialize a new Voice-Processing I/O unit. 947 // Create, setup and initialize a new Voice-Processing I/O unit.
804 if (!SetupAndInitializeVoiceProcessingAudioUnit()) { 948 if (!SetupAndInitializeVoiceProcessingAudioUnit()) {
949 // Reduce usage count for the audio session and possibly deactivate it if
950 // this object is the only user.
951 DeactivateAudioSession();
805 return false; 952 return false;
806 } 953 }
807 return true; 954 return true;
808 } 955 }
809 956
810 bool AudioDeviceIOS::ShutdownPlayOrRecord() { 957 void AudioDeviceIOS::ShutdownPlayOrRecord() {
811 LOGI() << "ShutdownPlayOrRecord"; 958 LOGI() << "ShutdownPlayOrRecord";
812 // Remove audio session notification observers.
813 UnregisterNotificationObservers();
814
815 // Close and delete the voice-processing I/O unit. 959 // Close and delete the voice-processing I/O unit.
816 OSStatus result = -1; 960 OSStatus result = -1;
817 if (nullptr != vpio_unit_) { 961 if (nullptr != vpio_unit_) {
818 result = AudioOutputUnitStop(vpio_unit_); 962 result = AudioOutputUnitStop(vpio_unit_);
819 if (result != noErr) { 963 if (result != noErr) {
820 LOG_F(LS_ERROR) << "AudioOutputUnitStop failed: " << result; 964 LOG_F(LS_ERROR) << "AudioOutputUnitStop failed: " << result;
821 } 965 }
822 result = AudioUnitUninitialize(vpio_unit_); 966 result = AudioUnitUninitialize(vpio_unit_);
823 if (result != noErr) { 967 if (result != noErr) {
824 LOG_F(LS_ERROR) << "AudioUnitUninitialize failed: " << result; 968 LOG_F(LS_ERROR) << "AudioUnitUninitialize failed: " << result;
825 } 969 }
826 result = AudioComponentInstanceDispose(vpio_unit_); 970 DisposeAudioUnit();
827 if (result != noErr) {
828 LOG_F(LS_ERROR) << "AudioComponentInstanceDispose failed: " << result;
829 }
830 vpio_unit_ = nullptr;
831 } 971 }
832 972
973 // Remove audio session notification observers.
974 UnregisterNotificationObservers();
975
833 // All I/O should be stopped or paused prior to deactivating the audio 976 // All I/O should be stopped or paused prior to deactivating the audio
834 // session, hence we deactivate as last action. 977 // session, hence we deactivate as last action.
835 AVAudioSession* session = [AVAudioSession sharedInstance]; 978 DeactivateAudioSession();
836 ActivateAudioSession(session, false); 979 }
837 return true; 980
981 void AudioDeviceIOS::DisposeAudioUnit() {
982 if (nullptr == vpio_unit_)
983 return;
984 OSStatus result = AudioComponentInstanceDispose(vpio_unit_);
985 if (result != noErr) {
986 LOG(LS_ERROR) << "AudioComponentInstanceDispose failed:" << result;
987 }
988 vpio_unit_ = nullptr;
838 } 989 }
839 990
840 OSStatus AudioDeviceIOS::RecordedDataIsAvailable( 991 OSStatus AudioDeviceIOS::RecordedDataIsAvailable(
841 void* in_ref_con, 992 void* in_ref_con,
842 AudioUnitRenderActionFlags* io_action_flags, 993 AudioUnitRenderActionFlags* io_action_flags,
843 const AudioTimeStamp* in_time_stamp, 994 const AudioTimeStamp* in_time_stamp,
844 UInt32 in_bus_number, 995 UInt32 in_bus_number,
845 UInt32 in_number_frames, 996 UInt32 in_number_frames,
846 AudioBufferList* io_data) { 997 AudioBufferList* io_data) {
847 RTC_DCHECK_EQ(1u, in_bus_number); 998 RTC_DCHECK_EQ(1u, in_bus_number);
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 // Read decoded 16-bit PCM samples from WebRTC (using a size that matches 1077 // Read decoded 16-bit PCM samples from WebRTC (using a size that matches
927 // the native I/O audio unit) to a preallocated intermediate buffer and 1078 // the native I/O audio unit) to a preallocated intermediate buffer and
928 // copy the result to the audio buffer in the |io_data| destination. 1079 // copy the result to the audio buffer in the |io_data| destination.
929 SInt8* source = playout_audio_buffer_.get(); 1080 SInt8* source = playout_audio_buffer_.get();
930 fine_audio_buffer_->GetPlayoutData(source); 1081 fine_audio_buffer_->GetPlayoutData(source);
931 memcpy(destination, source, dataSizeInBytes); 1082 memcpy(destination, source, dataSizeInBytes);
932 return noErr; 1083 return noErr;
933 } 1084 }
934 1085
935 } // namespace webrtc 1086 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/audio_device/ios/audio_device_ios.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698