|
84 | 84 | channels:1
|
85 | 85 | interleaved:YES];
|
86 | 86 |
|
87 |
| -#if TARGET_OS_OSX |
88 | 87 | // Initial engine state
|
89 | 88 | engine_state_.voice_processing_bypassed = voice_processing_bypassed;
|
90 |
| - std::optional<AudioDeviceID> default_output_device_id = |
91 |
| - mac_audio_utils::GetDefaultOutputDeviceID(); |
92 |
| - std::optional<AudioDeviceID> default_input_device_id = mac_audio_utils::GetDefaultInputDeviceID(); |
93 |
| - if (default_output_device_id) { |
94 |
| - engine_state_.default_output_device_id = *default_output_device_id; |
95 |
| - } |
96 |
| - if (default_input_device_id) { |
97 |
| - engine_state_.default_input_device_id = *default_input_device_id; |
98 |
| - } |
99 |
| -#endif |
100 | 89 | }
|
101 | 90 |
|
102 | 91 | AudioEngineDevice::~AudioEngineDevice() {
|
103 | 92 | RTC_DCHECK_RUN_ON(thread_);
|
104 | 93 |
|
105 | 94 | safety_->SetNotAlive();
|
| 95 | +#if TARGET_OS_OSX |
| 96 | + default_device_update_safety_->SetNotAlive(); |
| 97 | +#endif |
106 | 98 |
|
107 | 99 | Terminate();
|
108 | 100 |
|
|
175 | 167 | observer_->OnDevicesUpdated();
|
176 | 168 | }
|
177 | 169 | }
|
178 |
| - } else if (selector == kAudioHardwarePropertyDefaultOutputDevice) { |
179 |
| - LOGI() << "Did update default output device"; |
180 |
| - std::optional<AudioDeviceID> device_id = mac_audio_utils::GetDefaultOutputDeviceID(); |
181 |
| - if (device_id) { |
182 |
| - int32_t result = ModifyEngineState([device_id](EngineState state) -> EngineState { |
183 |
| - state.default_output_device_id = *device_id; |
184 |
| - return state; |
185 |
| - }); |
186 |
| - if (result != 0) { |
187 |
| - LOGE() << "Failed to update default output device ID, error: " << result; |
188 |
| - } |
189 |
| - } |
190 |
| - } else if (selector == kAudioHardwarePropertyDefaultInputDevice) { |
191 |
| - LOGI() << "Did update default input device"; |
192 |
| - std::optional<AudioDeviceID> device_id = mac_audio_utils::GetDefaultInputDeviceID(); |
193 |
| - if (device_id) { |
194 |
| - int32_t result = ModifyEngineState([device_id](EngineState state) -> EngineState { |
195 |
| - state.default_input_device_id = *device_id; |
196 |
| - return state; |
197 |
| - }); |
198 |
| - if (result != 0) { |
199 |
| - LOGE() << "Failed to update default input device ID, error: " << result; |
200 |
| - } |
201 |
| - } |
| 170 | + } else if (selector == kAudioHardwarePropertyDefaultOutputDevice || |
| 171 | + selector == kAudioHardwarePropertyDefaultInputDevice) { |
| 172 | + // Cancel any pending updates |
| 173 | + default_device_update_safety_->SetNotAlive(); |
| 174 | + default_device_update_safety_ = PendingTaskSafetyFlag::Create(); |
| 175 | + |
| 176 | + // Schedule a new debounced update |
| 177 | + thread_->PostDelayedTask( |
| 178 | + SafeTask(default_device_update_safety_, |
| 179 | + [this, selector] { |
| 180 | + RTC_DCHECK_RUN_ON(thread_); |
| 181 | + LOGI() << "Processing debounced default device update for selector: " |
| 182 | + << selector; |
| 183 | + |
| 184 | + if (selector == kAudioHardwarePropertyDefaultOutputDevice) { |
| 185 | + LOGI() << "Did update default output device"; |
| 186 | + int32_t result = ModifyEngineState([](EngineState state) -> EngineState { |
| 187 | + state.default_output_device_update_count++; |
| 188 | + return state; |
| 189 | + }); |
| 190 | + if (result != 0) { |
| 191 | + LOGE() << "Failed to update default output device update count, error: " |
| 192 | + << result; |
| 193 | + } |
| 194 | + } else if (selector == kAudioHardwarePropertyDefaultInputDevice) { |
| 195 | + LOGI() << "Did update default input device"; |
| 196 | + int32_t result = ModifyEngineState([](EngineState state) -> EngineState { |
| 197 | + state.default_input_device_update_count++; |
| 198 | + return state; |
| 199 | + }); |
| 200 | + if (result != 0) { |
| 201 | + LOGE() << "Failed to update default input device update count, error: " |
| 202 | + << result; |
| 203 | + } |
| 204 | + } |
| 205 | + }), |
| 206 | + TimeDelta::Millis(kDefaultDeviceUpdateDebounceMs)); |
202 | 207 | }
|
203 | 208 | }));
|
204 | 209 | }
|
|
1250 | 1255 | // ----------------------------------------------------------------------------------------------------
|
1251 | 1256 | // Private - Engine Related
|
1252 | 1257 |
|
1253 |
| -void AudioEngineDevice::ReconfigureEngine(bool is_required) { |
1254 |
| - LOGI() << "ReconfigureEngine is_required: " << is_required; |
| 1258 | +void AudioEngineDevice::ReconfigureEngine() { |
| 1259 | + LOGI() << "ReconfigureEngine"; |
1255 | 1260 |
|
1256 | 1261 | // TODO: More optimizations
|
1257 | 1262 | // We only need to re-attach the input / output nodes with updated sample rate etc.
|
|
1264 | 1269 | // Re-configure is only for device mode
|
1265 | 1270 | if (current_state.render_mode != RenderMode::Device) return;
|
1266 | 1271 |
|
1267 |
| - // bool is_reconfigure_required = is_required; |
1268 |
| - |
1269 |
| - // #if defined(WEBRTC_IOS) |
1270 |
| - // RTC_OBJC_TYPE(RTCAudioSession)* session = [RTC_OBJC_TYPE(RTCAudioSession) |
1271 |
| - // sharedInstance]; const double new_sample_rate = session.sampleRate; if |
1272 |
| - // (!is_reconfigure_required && current_state.output_enabled) { |
1273 |
| - // AVAudioFormat* format = [thisoutputNode() outputFormatForBus:0]; |
1274 |
| - // if (format.sampleRate != new_sample_rate) { |
1275 |
| - // is_reconfigure_required = true; |
1276 |
| - // } |
1277 |
| - // } |
1278 |
| - // #endif |
1279 |
| - |
1280 |
| - // // No configuration required |
1281 |
| - // if (!is_reconfigure_required) { |
1282 |
| - // LOGI() << "ReconfigureEngine no configuration required"; |
1283 |
| - // return; |
1284 |
| - // } |
1285 |
| - |
1286 | 1272 | EngineState shutdown_state = this->engine_state_;
|
1287 | 1273 | shutdown_state.input_enabled = false;
|
1288 | 1274 | shutdown_state.input_running = false;
|
|
2193 | 2179 | //
|
2194 | 2180 | #if TARGET_OS_OSX
|
2195 | 2181 | if (state.next.IsAnyEnabled() &&
|
2196 |
| - (!state.prev.IsAnyEnabled() || state.IsEngineRecreateRequired() || |
2197 |
| - state.DidUpdateInputDevice() || state.DidUpdateOutputDevice() || |
2198 |
| - ((state.DidUpdateDefaultOutputDevice() && state.next.IsOutputDefaultDevice()) || |
2199 |
| - (state.DidUpdateDefaultInputDevice() && state.next.IsInputDefaultDevice())))) { |
| 2182 | + (!state.prev.IsAnyEnabled() || state.IsEngineRecreateRequired())) { |
2200 | 2183 | if (state.next.IsInputEnabled()) {
|
2201 | 2184 | uint32_t input_device_id = state.next.input_device_id;
|
2202 | 2185 | if (input_device_id == kAudioObjectUnknown) {
|
2203 |
| - input_device_id = state.next.default_input_device_id; |
2204 |
| - LOGI() << "Using default input device: " << input_device_id; |
2205 |
| - } |
2206 |
| - |
2207 |
| - LOGI() << "Setting input device: " << input_device_id; |
2208 |
| - AudioUnit inputUnit = inputNode().audioUnit; |
2209 |
| - OSStatus err = AudioUnitSetProperty(inputUnit, kAudioOutputUnitProperty_CurrentDevice, |
2210 |
| - kAudioUnitScope_Global, 1, &input_device_id, |
2211 |
| - sizeof(input_device_id)); |
2212 |
| - if (err != noErr) { |
2213 |
| - LOGE() << "Failed to set input device: " << input_device_id; |
| 2186 | + LOGI() << "Using default input device"; |
| 2187 | + } else { |
| 2188 | + auto input_device_name = mac_audio_utils::GetDeviceName(input_device_id); |
| 2189 | + LOGI() << "Setting input device: " << input_device_name.value_or("Unknown") << " (" |
| 2190 | + << input_device_id << ")"; |
| 2191 | + AudioUnit inputUnit = inputNode().audioUnit; |
| 2192 | + OSStatus err = AudioUnitSetProperty(inputUnit, kAudioOutputUnitProperty_CurrentDevice, |
| 2193 | + kAudioUnitScope_Global, 1, &input_device_id, |
| 2194 | + sizeof(input_device_id)); |
| 2195 | + if (err != noErr) { |
| 2196 | + LOGE() << "Failed to set input device: " << input_device_id; |
| 2197 | + } |
2214 | 2198 | }
|
2215 | 2199 | }
|
2216 | 2200 |
|
2217 | 2201 | if (state.next.IsOutputEnabled()) {
|
2218 | 2202 | uint32_t output_deviceId = state.next.output_device_id;
|
2219 | 2203 | if (output_deviceId == kAudioObjectUnknown) {
|
2220 |
| - output_deviceId = state.next.default_output_device_id; |
2221 |
| - LOGI() << "Using default output device: " << output_deviceId; |
2222 |
| - } |
2223 |
| - |
2224 |
| - LOGI() << "Setting output device: " << output_deviceId; |
2225 |
| - AudioUnit outputUnit = outputNode().audioUnit; |
2226 |
| - OSStatus err = AudioUnitSetProperty(outputUnit, kAudioOutputUnitProperty_CurrentDevice, |
2227 |
| - kAudioUnitScope_Global, 0, &output_deviceId, |
2228 |
| - sizeof(output_deviceId)); |
2229 |
| - if (err != noErr) { |
2230 |
| - LOGE() << "Failed to set output device: " << output_deviceId; |
| 2204 | + LOGI() << "Using default output device"; |
| 2205 | + } else { |
| 2206 | + auto output_device_name = mac_audio_utils::GetDeviceName(output_deviceId); |
| 2207 | + LOGI() << "Setting output device: " << output_device_name.value_or("Unknown") << " (" |
| 2208 | + << output_deviceId << ")"; |
| 2209 | + AudioUnit outputUnit = outputNode().audioUnit; |
| 2210 | + OSStatus err = AudioUnitSetProperty(outputUnit, kAudioOutputUnitProperty_CurrentDevice, |
| 2211 | + kAudioUnitScope_Global, 0, &output_deviceId, |
| 2212 | + sizeof(output_deviceId)); |
| 2213 | + if (err != noErr) { |
| 2214 | + LOGE() << "Failed to set output device: " << output_deviceId; |
| 2215 | + } |
2231 | 2216 | }
|
2232 | 2217 | }
|
2233 | 2218 | }
|
|
2290 | 2275 | NSString* error_string = nil;
|
2291 | 2276 |
|
2292 | 2277 | @try {
|
| 2278 | +#if TARGET_OS_OSX |
| 2279 | + // Workaround for engine not starting in some cases when other apps are using voice |
| 2280 | + // processing already. |
| 2281 | + [engine_device_ prepare]; |
| 2282 | + usleep(1000); |
| 2283 | +#endif |
| 2284 | + |
2293 | 2285 | NSError* error = nil;
|
2294 | 2286 | start_result = [engine_device_ startAndReturnError:&error];
|
2295 | 2287 | if (!start_result && error != nil) {
|
|
2317 | 2309 | object:engine_device_
|
2318 | 2310 | queue:nil
|
2319 | 2311 | usingBlock:^(NSNotification* notification) {
|
2320 |
| - ReconfigureEngine(true); |
| 2312 | + LOGI() << "AVAudioEngineConfigurationChangeNotification engineIsRunning: " |
| 2313 | + << engine_device_.running; |
| 2314 | + // Only re-configure if engine stopped. |
| 2315 | + if (!engine_device_.running) { |
| 2316 | + ReconfigureEngine(); |
| 2317 | + } |
2321 | 2318 | }];
|
2322 | 2319 |
|
2323 | 2320 | } else {
|
|
0 commit comments