From 39c8ddcda281e5f5b125c78b55741e39a50ff254 Mon Sep 17 00:00:00 2001 From: Kelebek1 Date: Wed, 23 Aug 2023 07:14:40 +0100 Subject: Pre-test opening a stream for audio backends, fall back to null if not suitable. --- src/audio_core/sink/sdl2_sink.cpp | 40 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'src/audio_core/sink/sdl2_sink.cpp') diff --git a/src/audio_core/sink/sdl2_sink.cpp b/src/audio_core/sink/sdl2_sink.cpp index c1529d1f9..7b89151de 100644 --- a/src/audio_core/sink/sdl2_sink.cpp +++ b/src/audio_core/sink/sdl2_sink.cpp @@ -9,6 +9,7 @@ #include "audio_core/sink/sdl2_sink.h" #include "audio_core/sink/sink_stream.h" #include "common/logging/log.h" +#include "common/scope_exit.h" #include "core/core.h" namespace AudioCore::Sink { @@ -84,6 +85,7 @@ public: } Stop(); + SDL_ClearQueuedAudio(device); SDL_CloseAudioDevice(device); } @@ -227,8 +229,42 @@ std::vector ListSDLSinkDevices(bool capture) { return device_list; } -u32 GetSDLLatency() { - return TargetSampleCount * 2; +bool IsSDLSuitable() { +#if !defined(HAVE_SDL2) + return false; +#else + // Check SDL can init + if (!SDL_WasInit(SDL_INIT_AUDIO)) { + if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { + LOG_ERROR(Audio_Sink, "SDL failed to init, it is not suitable. Error: {}", + SDL_GetError()); + return false; + } + } + + // We can set any latency frequency we want with SDL, so no need to check that. + + // Check we can open a device with standard parameters + SDL_AudioSpec spec; + spec.freq = TargetSampleRate; + spec.channels = 2u; + spec.format = AUDIO_S16SYS; + spec.samples = TargetSampleCount * 2; + spec.callback = nullptr; + spec.userdata = nullptr; + + SDL_AudioSpec obtained; + auto device = SDL_OpenAudioDevice(nullptr, false, &spec, &obtained, false); + + if (device == 0) { + LOG_ERROR(Audio_Sink, "SDL failed to open a device, it is not suitable. Error: {}", + SDL_GetError()); + return false; + } + + SDL_CloseAudioDevice(device); + return true; +#endif } } // namespace AudioCore::Sink -- cgit v1.2.3