OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #import "webrtc/modules/audio_device/ios/objc/RTCAudioSession.h" | |
12 | |
13 #import "webrtc/base/objc/RTCLogging.h" | |
14 #import "webrtc/modules/audio_device/ios/objc/RTCAudioSession+Private.h" | |
15 #import "webrtc/modules/audio_device/ios/objc/RTCAudioSessionConfiguration.h" | |
16 | |
17 @implementation RTCAudioSession (Configuration) | |
18 | |
19 - (BOOL)isConfiguredForWebRTC { | |
20 // Ensure that the device currently supports audio input. | |
21 if (!self.inputAvailable) { | |
22 RTCLogError(@"No audio input path is available!"); | |
23 return NO; | |
24 } | |
25 | |
26 // Only check a minimal list of requirements for whether we have | |
27 // what we want. | |
28 RTCAudioSessionConfiguration *currentConfig = | |
29 [RTCAudioSessionConfiguration currentConfiguration]; | |
30 RTCAudioSessionConfiguration *webRTCConfig = | |
31 [RTCAudioSessionConfiguration webRTCConfiguration]; | |
32 | |
33 if (![currentConfig.category isEqualToString:webRTCConfig.category]) { | |
34 RTCLog(@"Current category %@ does not match %@", | |
35 currentConfig.category, | |
36 webRTCConfig.category); | |
37 return NO; | |
38 } | |
39 | |
40 if (![currentConfig.mode isEqualToString:webRTCConfig.mode]) { | |
41 RTCLog(@"Current mode %@ does not match %@", | |
42 currentConfig.mode, | |
43 webRTCConfig.mode); | |
44 return NO; | |
45 } | |
46 | |
47 return YES; | |
48 } | |
49 | |
50 - (BOOL)setConfiguration:(RTCAudioSessionConfiguration *)configuration | |
51 active:(BOOL)active | |
52 error:(NSError **)outError { | |
53 if (![self checkLock:outError]) { | |
54 return NO; | |
55 } | |
56 // Provide an error even if there isn't one so we can log it. We will not | |
henrika_webrtc
2016/03/10 14:42:12
Nit, you have space above comment on lines 25 and
tkchin_webrtc
2016/03/10 23:07:09
Done.
| |
57 // return immediately on error in this function and instead try to set | |
58 // everything we can. | |
59 NSError *error = nil; | |
60 if (outError) { | |
61 error = *outError; | |
Chuck
2016/03/10 21:16:37
I might be reasoning about this incorrectly, but w
tkchin_webrtc
2016/03/10 23:07:09
Done.
| |
62 } | |
63 | |
64 if (self.category != configuration.category || | |
65 self.categoryOptions != configuration.categoryOptions) { | |
66 error = nil; | |
67 if (![self setCategory:configuration.category | |
68 withOptions:configuration.categoryOptions | |
69 error:&error]) { | |
70 RTCLogError(@"Failed to set category: %@", error.localizedDescription); | |
71 } | |
72 } | |
73 | |
74 if (self.mode != configuration.mode) { | |
75 error = nil; | |
76 if (![self setMode:configuration.mode error:&error]) { | |
77 RTCLogError(@"Failed to set mode: %@", error.localizedDescription); | |
78 } | |
79 } | |
80 | |
81 if (self.sampleRate != configuration.sampleRate) { | |
82 error = nil; | |
83 if (![self setPreferredSampleRate:configuration.sampleRate | |
84 error:&error]) { | |
85 RTCLogError(@"Failed to set preferred sample rate: %@", | |
86 error.localizedDescription); | |
87 } | |
88 } | |
89 | |
90 if (self.IOBufferDuration != configuration.ioBufferDuration) { | |
91 error = nil; | |
92 if (![self setPreferredIOBufferDuration:configuration.ioBufferDuration | |
93 error:&error]) { | |
94 RTCLogError(@"Failed to set preferred IO buffer duration: %@", | |
95 error.localizedDescription); | |
96 } | |
97 } | |
98 | |
99 error = nil; | |
Chuck
2016/03/10 21:16:37
This pattern is different than the other blocks of
tkchin_webrtc
2016/03/10 23:07:09
Yup. Always forcing the active.
| |
100 if (![self setActive:active error:&error]) { | |
101 RTCLogError(@"Failed to setActive to %d: %@", | |
102 active, error.localizedDescription); | |
103 } | |
104 | |
105 if (self.isActive) { | |
106 // Try to set the preferred number of hardware audio channels. These calls | |
107 // must be done after setting the audio session’s category and mode and | |
108 // activating the session. | |
109 NSInteger inputNumberOfChannels = configuration.inputNumberOfChannels; | |
110 if (self.inputNumberOfChannels != inputNumberOfChannels) { | |
111 error = nil; | |
112 if (![self setPreferredInputNumberOfChannels:inputNumberOfChannels | |
113 error:&error]) { | |
114 RTCLogError(@"Failed to set preferred input number of channels: %@", | |
115 error.localizedDescription); | |
116 } | |
117 } | |
118 NSInteger outputNumberOfChannels = configuration.outputNumberOfChannels; | |
119 if (self.outputNumberOfChannels != outputNumberOfChannels) { | |
120 error = nil; | |
121 if (![self setPreferredOutputNumberOfChannels:outputNumberOfChannels | |
122 error:&error]) { | |
123 RTCLogError(@"Failed to set preferred output number of channels: %@", | |
124 error.localizedDescription); | |
125 } | |
126 } | |
127 } | |
128 | |
129 return YES; | |
130 } | |
131 | |
132 - (BOOL)configureWebRTCSession:(NSError **)outError { | |
133 if (![self checkLock:outError]) { | |
134 return NO; | |
135 } | |
136 RTCLog(@"Configuring audio session for WebRTC."); | |
137 | |
138 // Provide an error even if there isn't one so we can log it. | |
139 NSError *error = nil; | |
140 if (outError) { | |
141 error = *outError; | |
142 } | |
143 | |
144 RTCAudioSessionConfiguration *currentConfig = | |
145 [RTCAudioSessionConfiguration currentConfiguration]; | |
146 RTCAudioSessionConfiguration *webRTCConfig = | |
147 [RTCAudioSessionConfiguration webRTCConfiguration]; | |
148 if (![self setConfiguration:webRTCConfig active:YES error:&error]) { | |
149 RTCLogError(@"Failed to set WebRTC audio configuration: %@", | |
150 error.localizedDescription); | |
151 // Attempt to restore previous state. | |
152 [self setConfiguration:currentConfig active:NO error:nil]; | |
153 return NO; | |
154 } | |
155 | |
156 // Ensure that the active audio session has the correct category and mode. | |
157 if (![self isConfiguredForWebRTC]) { | |
158 // This should never happen - this means that we succeeded earlier but | |
159 // somehow the settings didn't apply. | |
160 RTCLogError(@"Failed to configure audio session."); | |
161 // Attempt to restore previous state. | |
162 [self setConfiguration:currentConfig active:NO error:nil]; | |
163 return NO; | |
164 } | |
165 | |
166 return YES; | |
167 } | |
168 | |
169 @end | |
OLD | NEW |