From 552a3959524b120e3f866b24f7692bc9e04bb21a Mon Sep 17 00:00:00 2001 From: Sebastian Parborg Date: Wed, 7 May 2025 14:05:56 +0200 Subject: [PATCH] Audaspace: Wait for the PulseAudio stream to be ready when initializing Otherwise, the stream might be in an invalid state when we are trying to start playback. Pull Request: https://projects.blender.org/blender/blender/pulls/136845 --- .../plugins/pulseaudio/PulseAudioDevice.cpp | 25 +++++++++++++++++++ .../plugins/pulseaudio/PulseAudioSymbols.h | 1 + 2 files changed, 26 insertions(+) diff --git a/extern/audaspace/plugins/pulseaudio/PulseAudioDevice.cpp b/extern/audaspace/plugins/pulseaudio/PulseAudioDevice.cpp index 6d4b71220de..ecc9df4f6e8 100644 --- a/extern/audaspace/plugins/pulseaudio/PulseAudioDevice.cpp +++ b/extern/audaspace/plugins/pulseaudio/PulseAudioDevice.cpp @@ -77,6 +77,7 @@ void PulseAudioDevice::PulseAudio_request(pa_stream* stream, size_t total_bytes, total_bytes -= num_bytes; } + AUD_pa_threaded_mainloop_signal(device->m_mainloop, 0); } void PulseAudioDevice::playing(bool playing) @@ -218,6 +219,30 @@ PulseAudioDevice::PulseAudioDevice(const std::string& name, DeviceSpecs specs, i AUD_THROW(DeviceException, "Could not connect PulseAudio stream."); } + /* Make sure that the stream is ready to be used before we proceed. */ + int stream_state; + while((stream_state = AUD_pa_stream_get_state(m_stream)) != PA_STREAM_READY) + { + switch(stream_state) + { + case PA_STREAM_FAILED: + case PA_STREAM_TERMINATED: + AUD_pa_threaded_mainloop_unlock(m_mainloop); + AUD_pa_threaded_mainloop_stop(m_mainloop); + + AUD_pa_context_disconnect(m_context); + AUD_pa_context_unref(m_context); + + AUD_pa_threaded_mainloop_free(m_mainloop); + + AUD_THROW(DeviceException, "Could not connect to PulseAudio."); + break; + default: + AUD_pa_threaded_mainloop_wait(m_mainloop); + break; + } + } + AUD_pa_threaded_mainloop_unlock(m_mainloop); create(); diff --git a/extern/audaspace/plugins/pulseaudio/PulseAudioSymbols.h b/extern/audaspace/plugins/pulseaudio/PulseAudioSymbols.h index 574ed4115c9..505f5d13c74 100644 --- a/extern/audaspace/plugins/pulseaudio/PulseAudioSymbols.h +++ b/extern/audaspace/plugins/pulseaudio/PulseAudioSymbols.h @@ -25,6 +25,7 @@ PULSEAUDIO_SYMBOL(pa_stream_begin_write); PULSEAUDIO_SYMBOL(pa_stream_connect_playback); PULSEAUDIO_SYMBOL(pa_stream_cork); PULSEAUDIO_SYMBOL(pa_stream_flush); +PULSEAUDIO_SYMBOL(pa_stream_get_state); PULSEAUDIO_SYMBOL(pa_stream_get_time); PULSEAUDIO_SYMBOL(pa_stream_is_corked); PULSEAUDIO_SYMBOL(pa_stream_new);