From 5a08091168782a924cd3a23baf31c5cd4cd63906 Mon Sep 17 00:00:00 2001 From: Eoin O'Neill Date: Fri, 9 Dec 2022 22:39:14 -0800 Subject: [PATCH] Fix crash caused by invalid mix_rate assignment due to bogus project settings. We'll default to a sensible value in the case that a user has somehow managed to modify the configuration file incorrectly. Closes 69819 --- drivers/alsa/audio_driver_alsa.cpp | 3 ++- drivers/coreaudio/audio_driver_coreaudio.cpp | 4 ++-- drivers/pulseaudio/audio_driver_pulseaudio.cpp | 2 +- drivers/wasapi/audio_driver_wasapi.cpp | 2 +- drivers/xaudio2/audio_driver_xaudio2.cpp | 3 ++- platform/web/audio_driver_web.cpp | 2 +- servers/audio/audio_driver_dummy.cpp | 2 +- servers/audio_server.cpp | 14 ++++++++++++++ servers/audio_server.h | 5 ++++- 9 files changed, 28 insertions(+), 9 deletions(-) diff --git a/drivers/alsa/audio_driver_alsa.cpp b/drivers/alsa/audio_driver_alsa.cpp index 689f76389b0..a6673f974ea 100644 --- a/drivers/alsa/audio_driver_alsa.cpp +++ b/drivers/alsa/audio_driver_alsa.cpp @@ -44,7 +44,8 @@ extern int initialize_pulse(int verbose); #endif Error AudioDriverALSA::init_output_device() { - mix_rate = GLOBAL_GET("audio/driver/mix_rate"); + mix_rate = _get_configured_mix_rate(); + speaker_mode = SPEAKER_MODE_STEREO; channels = 2; diff --git a/drivers/coreaudio/audio_driver_coreaudio.cpp b/drivers/coreaudio/audio_driver_coreaudio.cpp index 2c959bb07b1..40117274335 100644 --- a/drivers/coreaudio/audio_driver_coreaudio.cpp +++ b/drivers/coreaudio/audio_driver_coreaudio.cpp @@ -116,7 +116,7 @@ Error AudioDriverCoreAudio::init() { break; } - mix_rate = GLOBAL_GET("audio/driver/mix_rate"); + mix_rate = _get_configured_mix_rate(); memset(&strdesc, 0, sizeof(strdesc)); strdesc.mFormatID = kAudioFormatLinearPCM; @@ -405,7 +405,7 @@ Error AudioDriverCoreAudio::init_input_device() { break; } - mix_rate = GLOBAL_GET("audio/driver/mix_rate"); + mix_rate = _get_configured_mix_rate(); memset(&strdesc, 0, sizeof(strdesc)); strdesc.mFormatID = kAudioFormatLinearPCM; diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp index 797ffd67fe1..744d0319b5b 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp +++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp @@ -293,7 +293,7 @@ Error AudioDriverPulseAudio::init() { active.clear(); exit_thread.clear(); - mix_rate = GLOBAL_GET("audio/driver/mix_rate"); + mix_rate = _get_configured_mix_rate(); pa_ml = pa_mainloop_new(); ERR_FAIL_COND_V(pa_ml == nullptr, ERR_CANT_OPEN); diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index 72ec0c19abc..ab4d9ddd957 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -547,7 +547,7 @@ Error AudioDriverWASAPI::finish_input_device() { } Error AudioDriverWASAPI::init() { - mix_rate = GLOBAL_GET("audio/driver/mix_rate"); + mix_rate = _get_configured_mix_rate(); target_latency_ms = GLOBAL_GET("audio/driver/output_latency"); diff --git a/drivers/xaudio2/audio_driver_xaudio2.cpp b/drivers/xaudio2/audio_driver_xaudio2.cpp index 44ce01d4d74..2b5f593a079 100644 --- a/drivers/xaudio2/audio_driver_xaudio2.cpp +++ b/drivers/xaudio2/audio_driver_xaudio2.cpp @@ -39,7 +39,8 @@ Error AudioDriverXAudio2::init() { pcm_open = false; samples_in = nullptr; - mix_rate = GLOBAL_GET("audio/driver/mix_rate"); + mix_rate = _get_configured_mix_rate(); + // FIXME: speaker_mode seems unused in the Xaudio2 driver so far speaker_mode = SPEAKER_MODE_STEREO; channels = 2; diff --git a/platform/web/audio_driver_web.cpp b/platform/web/audio_driver_web.cpp index 1d7b96d707f..c6c67db3de2 100644 --- a/platform/web/audio_driver_web.cpp +++ b/platform/web/audio_driver_web.cpp @@ -103,7 +103,7 @@ void AudioDriverWeb::_audio_driver_capture(int p_from, int p_samples) { Error AudioDriverWeb::init() { int latency = GLOBAL_GET("audio/driver/output_latency"); if (!audio_context.inited) { - audio_context.mix_rate = GLOBAL_GET("audio/driver/mix_rate"); + audio_context.mix_rate = _get_configured_mix_rate(); audio_context.channel_count = godot_audio_init(&audio_context.mix_rate, latency, &_state_change_callback, &_latency_update_callback); audio_context.inited = true; } diff --git a/servers/audio/audio_driver_dummy.cpp b/servers/audio/audio_driver_dummy.cpp index e6257d92609..1d11c01b9a9 100644 --- a/servers/audio/audio_driver_dummy.cpp +++ b/servers/audio/audio_driver_dummy.cpp @@ -41,7 +41,7 @@ Error AudioDriverDummy::init() { samples_in = nullptr; if (mix_rate == -1) { - mix_rate = GLOBAL_GET("audio/driver/mix_rate"); + mix_rate = _get_configured_mix_rate(); } channels = get_channels(); diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index 0344bf322d6..49991e41d33 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -115,6 +115,20 @@ void AudioDriver::input_buffer_write(int32_t sample) { } } +int AudioDriver::_get_configured_mix_rate() { + StringName audio_driver_setting = "audio/driver/mix_rate"; + int mix_rate = GLOBAL_GET(audio_driver_setting); + + // In the case of invalid mix rate, let's default to a sensible value.. + if (mix_rate <= 0) { + WARN_PRINT(vformat("Invalid mix rate of %d, consider reassigning setting \'%s\'. \nDefaulting mix rate to value %d.", + mix_rate, audio_driver_setting, AudioDriverManager::DEFAULT_MIX_RATE)); + mix_rate = AudioDriverManager::DEFAULT_MIX_RATE; + } + + return mix_rate; +} + AudioDriver::SpeakerMode AudioDriver::get_speaker_mode_by_total_channels(int p_channels) const { switch (p_channels) { case 4: diff --git a/servers/audio_server.h b/servers/audio_server.h index 155beb20009..6585043f631 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -66,6 +66,8 @@ protected: void input_buffer_init(int driver_buffer_frames); void input_buffer_write(int32_t sample); + int _get_configured_mix_rate(); + #ifdef DEBUG_ENABLED _FORCE_INLINE_ void start_counting_ticks() { prof_ticks = OS::get_singleton()->get_ticks_usec(); } _FORCE_INLINE_ void stop_counting_ticks() { prof_time += OS::get_singleton()->get_ticks_usec() - prof_ticks; } @@ -136,7 +138,6 @@ class AudioDriverManager { MAX_DRIVERS = 10 }; - static const int DEFAULT_MIX_RATE = 44100; static const int DEFAULT_OUTPUT_LATENCY = 15; static AudioDriver *drivers[MAX_DRIVERS]; @@ -145,6 +146,8 @@ class AudioDriverManager { static AudioDriverDummy dummy_driver; public: + static const int DEFAULT_MIX_RATE = 44100; + static void add_driver(AudioDriver *p_driver); static void initialize(int p_driver); static int get_driver_count();