Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

[iOS] fix on_focus_in() starting the CoreAudio driver when dummy driver is active

xSStephen1d3dde4a6dd4
diff --git a/drivers/apple_embedded/os_apple_embedded.mm b/drivers/apple_embedded/os_apple_embedded.mm
index 90c6f8c4da9..5229ac8c618 100644
--- a/drivers/apple_embedded/os_apple_embedded.mm
+++ b/drivers/apple_embedded/os_apple_embedded.mm
@@ -48,6 +48,7 @@
#include "drivers/sdl/joypad_sdl.h"
#endif
#include "main/main.h"
+#include "servers/audio/audio_driver.h"
#include "servers/camera/camera_server.h"
#include "servers/display/accessibility_server.h"
@@ -785,11 +786,16 @@ Error OS_AppleEmbedded::setup_remote_filesystem(const String &p_server_host, int
}
void OS_AppleEmbedded::audio_driver_start() {
- audio_driver.start();
+ // Dummy is the singleton when Core Audio init failed. Starting this unit would run a second mix thread.
+ if (AudioDriver::get_singleton() == &audio_driver) {
+ audio_driver.start();
+ }
}
void OS_AppleEmbedded::audio_driver_stop() {
- audio_driver.stop();
+ if (AudioDriver::get_singleton() == &audio_driver) {
+ audio_driver.stop();
+ }
}
void OS_AppleEmbedded::on_focus_out() {
@@ -810,7 +816,7 @@ void OS_AppleEmbedded::on_focus_out() {
[GDTAppDelegateService.viewController.godotView stopRendering];
- audio_driver.stop();
+ audio_driver_stop();
}
}
@@ -832,7 +838,7 @@ void OS_AppleEmbedded::on_focus_in() {
[GDTAppDelegateService.viewController.godotView startRendering];
- audio_driver.start();
+ audio_driver_start();
}
}
diff --git a/drivers/coreaudio/audio_driver_coreaudio.h b/drivers/coreaudio/audio_driver_coreaudio.h
index e371d679a99..d3fc0329204 100644
--- a/drivers/coreaudio/audio_driver_coreaudio.h
+++ b/drivers/coreaudio/audio_driver_coreaudio.h
@@ -89,6 +89,7 @@ class AudioDriverCoreAudio : public AudioDriver {
Error init_input_device();
void finish_input_device();
+ void _release_output_unit();
public:
virtual const char *get_name() const override {
diff --git a/drivers/coreaudio/audio_driver_coreaudio.mm b/drivers/coreaudio/audio_driver_coreaudio.mm
index 0569546c592..e0e785fb84f 100644
--- a/drivers/coreaudio/audio_driver_coreaudio.mm
+++ b/drivers/coreaudio/audio_driver_coreaudio.mm
@@ -75,6 +75,30 @@ OSStatus AudioDriverCoreAudio::output_device_address_cb(AudioObjectID inObjectID
#endif
#endif
+void AudioDriverCoreAudio::_release_output_unit() {
+ if (!audio_unit) {
+ return;
+ }
+
+#ifdef MACOS_ENABLED
+ AudioObjectPropertyAddress prop;
+ prop.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
+ prop.mScope = kAudioObjectPropertyScopeGlobal;
+ prop.mElement = kAudioObjectPropertyElementMain;
+
+ // Ignore the status. The listener is only registered after the instance
+ // exists, so this also covers a failure before it was added.
+ AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this);
+#endif
+
+ OSStatus result = AudioComponentInstanceDispose(audio_unit);
+ if (result != noErr) {
+ ERR_PRINT("AudioComponentInstanceDispose failed");
+ }
+
+ audio_unit = nullptr;
+}
+
Error AudioDriverCoreAudio::init() {
AudioComponentDescription desc;
memset(&desc, 0, sizeof(desc));
@@ -89,8 +113,10 @@ Error AudioDriverCoreAudio::init() {
AudioComponent comp = AudioComponentFindNext(nullptr, &desc);
ERR_FAIL_NULL_V(comp, FAILED);
- OSStatus result = AudioComponentInstanceNew(comp, &audio_unit);
+ AudioComponentInstance unit = nullptr;
+ OSStatus result = AudioComponentInstanceNew(comp, &unit);
ERR_FAIL_COND_V(result != noErr, FAILED);
+ audio_unit = unit;
#ifdef MACOS_ENABLED
AudioObjectPropertyAddress prop;
@@ -99,7 +125,10 @@ Error AudioDriverCoreAudio::init() {
prop.mElement = kAudioObjectPropertyElementMain;
result = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &prop, &output_device_address_cb, this);
- ERR_FAIL_COND_V(result != noErr, FAILED);
+ if (result != noErr) {
+ _release_output_unit();
+ ERR_FAIL_V(FAILED);
+ }
#endif
AudioStreamBasicDescription strdesc;
@@ -107,7 +136,10 @@ Error AudioDriverCoreAudio::init() {
memset(&strdesc, 0, sizeof(strdesc));
UInt32 size = sizeof(strdesc);
result = AudioUnitGetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, kOutputBus, &strdesc, &size);
- ERR_FAIL_COND_V(result != noErr, FAILED);
+ if (result != noErr) {
+ _release_output_unit();
+ ERR_FAIL_V(FAILED);
+ }
switch (strdesc.mChannelsPerFrame) {
case 2: // Stereo
@@ -129,14 +161,20 @@ Error AudioDriverCoreAudio::init() {
AudioObjectPropertyAddress property_dev_id = { kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMain };
result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &property_dev_id, 0, nullptr, &dev_id_size, &device_id);
- ERR_FAIL_COND_V(result != noErr, FAILED);
+ if (result != noErr) {
+ _release_output_unit();
+ ERR_FAIL_V(FAILED);
+ }
double hw_mix_rate;
UInt32 hw_mix_rate_size = sizeof(hw_mix_rate);
AudioObjectPropertyAddress property_sr = { kAudioDevicePropertyNominalSampleRate, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMain };
result = AudioObjectGetPropertyData(device_id, &property_sr, 0, nullptr, &hw_mix_rate_size, &hw_mix_rate);
- ERR_FAIL_COND_V(result != noErr, FAILED);
+ if (result != noErr) {
+ _release_output_unit();
+ ERR_FAIL_V(FAILED);
+ }
#else
double hw_mix_rate = [AVAudioSession sharedInstance].sampleRate;
#endif
@@ -153,7 +191,10 @@ Error AudioDriverCoreAudio::init() {
strdesc.mBytesPerPacket = strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, kOutputBus, &strdesc, sizeof(strdesc));
- ERR_FAIL_COND_V(result != noErr, FAILED);
+ if (result != noErr) {
+ _release_output_unit();
+ ERR_FAIL_V(FAILED);
+ }
uint32_t latency = Engine::get_singleton()->get_audio_output_latency();
// Sample rate is independent of channels (ref: https://stackoverflow.com/questions/11048825/audio-sample-frequency-rely-on-channels)
@@ -161,7 +202,10 @@ Error AudioDriverCoreAudio::init() {
#ifdef MACOS_ENABLED
result = AudioUnitSetProperty(audio_unit, kAudioDevicePropertyBufferFrameSize, kAudioUnitScope_Global, kOutputBus, &buffer_frames, sizeof(UInt32));
- ERR_FAIL_COND_V(result != noErr, FAILED);
+ if (result != noErr) {
+ _release_output_unit();
+ ERR_FAIL_V(FAILED);
+ }
#endif
unsigned int buffer_size = buffer_frames * channels;
@@ -176,13 +220,24 @@ Error AudioDriverCoreAudio::init() {
callback.inputProc = &AudioDriverCoreAudio::output_callback;
callback.inputProcRefCon = this;
result = AudioUnitSetProperty(audio_unit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, kOutputBus, &callback, sizeof(callback));
- ERR_FAIL_COND_V(result != noErr, FAILED);
+ if (result != noErr) {
+ _release_output_unit();
+ ERR_FAIL_V(FAILED);
+ }
result = AudioUnitInitialize(audio_unit);
- ERR_FAIL_COND_V(result != noErr, FAILED);
+ if (result != noErr) {
+ _release_output_unit();
+ ERR_FAIL_V(FAILED);
+ }
if (GLOBAL_GET("audio/driver/enable_input")) {
- return init_input_device();
+ Error input_err = init_input_device();
+ if (input_err != OK) {
+ finish_input_device();
+ _release_output_unit();
+ return input_err;
+ }
}
return OK;
}