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/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)setConfiguration:(RTCAudioSessionConfiguration *)configuration | |
20 error:(NSError **)outError { | |
21 return [self setConfiguration:configuration | |
22 active:NO | |
23 shouldSetActive:NO | |
24 error:outError]; | |
25 } | |
26 | |
27 - (BOOL)setConfiguration:(RTCAudioSessionConfiguration *)configuration | |
28 active:(BOOL)active | |
29 error:(NSError **)outError { | |
30 return [self setConfiguration:configuration | |
31 active:active | |
32 shouldSetActive:YES | |
33 error:outError]; | |
34 } | |
35 | |
36 #pragma mark - Private | |
37 | |
38 - (BOOL)setConfiguration:(RTCAudioSessionConfiguration *)configuration | |
39 active:(BOOL)active | |
40 shouldSetActive:(BOOL)shouldSetActive | |
41 error:(NSError **)outError { | |
42 NSParameterAssert(configuration); | |
43 if (outError) { | |
44 *outError = nil; | |
45 } | |
46 if (![self checkLock:outError]) { | |
47 return NO; | |
48 } | |
49 | |
50 // Provide an error even if there isn't one so we can log it. We will not | |
51 // return immediately on error in this function and instead try to set | |
52 // everything we can. | |
53 NSError *error = nil; | |
54 | |
55 if (self.category != configuration.category || | |
56 self.categoryOptions != configuration.categoryOptions) { | |
57 NSError *categoryError = nil; | |
58 if (![self setCategory:configuration.category | |
59 withOptions:configuration.categoryOptions | |
60 error:&categoryError]) { | |
61 RTCLogError(@"Failed to set category: %@", | |
62 categoryError.localizedDescription); | |
63 error = categoryError; | |
64 } else { | |
65 RTCLog(@"Set category to: %@", configuration.category); | |
66 } | |
67 } | |
68 | |
69 if (self.mode != configuration.mode) { | |
70 NSError *modeError = nil; | |
71 if (![self setMode:configuration.mode error:&modeError]) { | |
72 RTCLogError(@"Failed to set mode: %@", | |
73 modeError.localizedDescription); | |
74 error = modeError; | |
75 } else { | |
76 RTCLog(@"Set mode to: %@", configuration.mode); | |
77 } | |
78 } | |
79 | |
80 // Sometimes category options don't stick after setting mode. | |
81 if (self.categoryOptions != configuration.categoryOptions) { | |
82 NSError *categoryError = nil; | |
83 if (![self setCategory:configuration.category | |
84 withOptions:configuration.categoryOptions | |
85 error:&categoryError]) { | |
86 RTCLogError(@"Failed to set category options: %@", | |
87 categoryError.localizedDescription); | |
88 error = categoryError; | |
89 } else { | |
90 RTCLog(@"Set category options to: %ld", | |
91 (long)configuration.categoryOptions); | |
92 } | |
93 } | |
94 | |
95 if (self.preferredSampleRate != configuration.sampleRate) { | |
96 NSError *sampleRateError = nil; | |
97 if (![self setPreferredSampleRate:configuration.sampleRate | |
98 error:&sampleRateError]) { | |
99 RTCLogError(@"Failed to set preferred sample rate: %@", | |
100 sampleRateError.localizedDescription); | |
101 error = sampleRateError; | |
102 } else { | |
103 RTCLog(@"Set preferred sample rate to: %.2f", | |
104 configuration.sampleRate); | |
105 } | |
106 } | |
107 | |
108 if (self.preferredIOBufferDuration != configuration.ioBufferDuration) { | |
109 NSError *bufferDurationError = nil; | |
110 if (![self setPreferredIOBufferDuration:configuration.ioBufferDuration | |
111 error:&bufferDurationError]) { | |
112 RTCLogError(@"Failed to set preferred IO buffer duration: %@", | |
113 bufferDurationError.localizedDescription); | |
114 error = bufferDurationError; | |
115 } else { | |
116 RTCLog(@"Set preferred IO buffer duration to: %f", | |
117 configuration.ioBufferDuration); | |
118 } | |
119 } | |
120 | |
121 if (shouldSetActive) { | |
122 NSError *activeError = nil; | |
123 if (![self setActive:active error:&activeError]) { | |
124 RTCLogError(@"Failed to setActive to %d: %@", | |
125 active, activeError.localizedDescription); | |
126 error = activeError; | |
127 } | |
128 } | |
129 | |
130 if (self.isActive && | |
131 // TODO(tkchin): Figure out which category/mode numChannels is valid for. | |
132 [self.mode isEqualToString:AVAudioSessionModeVoiceChat]) { | |
133 // Try to set the preferred number of hardware audio channels. These calls | |
134 // must be done after setting the audio session’s category and mode and | |
135 // activating the session. | |
136 NSInteger inputNumberOfChannels = configuration.inputNumberOfChannels; | |
137 if (self.inputNumberOfChannels != inputNumberOfChannels) { | |
138 NSError *inputChannelsError = nil; | |
139 if (![self setPreferredInputNumberOfChannels:inputNumberOfChannels | |
140 error:&inputChannelsError]) { | |
141 RTCLogError(@"Failed to set preferred input number of channels: %@", | |
142 inputChannelsError.localizedDescription); | |
143 error = inputChannelsError; | |
144 } else { | |
145 RTCLog(@"Set input number of channels to: %ld", | |
146 (long)inputNumberOfChannels); | |
147 } | |
148 } | |
149 NSInteger outputNumberOfChannels = configuration.outputNumberOfChannels; | |
150 if (self.outputNumberOfChannels != outputNumberOfChannels) { | |
151 NSError *outputChannelsError = nil; | |
152 if (![self setPreferredOutputNumberOfChannels:outputNumberOfChannels | |
153 error:&outputChannelsError]) { | |
154 RTCLogError(@"Failed to set preferred output number of channels: %@", | |
155 outputChannelsError.localizedDescription); | |
156 error = outputChannelsError; | |
157 } else { | |
158 RTCLog(@"Set output number of channels to: %ld", | |
159 (long)outputNumberOfChannels); | |
160 } | |
161 } | |
162 } | |
163 | |
164 if (outError) { | |
165 *outError = error; | |
166 } | |
167 | |
168 return error == nil; | |
169 } | |
170 | |
171 @end | |
OLD | NEW |