Skip to content

VERY experimental Nintendo Switch 2 emulator

C++

[Audio] Tighten sink queue back pressure (#2840)

Ribbit15becaa5a327
diff --git a/src/audio_core/sink/sink_stream.cpp b/src/audio_core/sink/sink_stream.cpp
index 88e2dfa3d3..22abeb60b7 100644
--- a/src/audio_core/sink/sink_stream.cpp
+++ b/src/audio_core/sink/sink_stream.cpp
@@ -188,7 +188,7 @@ void SinkStream::ProcessAudioOutAndRender(std::span<s16> output_buffer, std::siz
}
// Successfully dequeued a new buffer.
{
- std::unique_lock lk{release_mutex};\
+ std::unique_lock lk{release_mutex};
queued_buffers--;
}
release_cv.notify_one();
@@ -237,9 +237,15 @@ u64 SinkStream::GetExpectedPlayedSampleCount() {
void SinkStream::WaitFreeSpace(std::stop_token stop_token) {
std::unique_lock lk{release_mutex};
- release_cv.wait_for(lk, std::chrono::milliseconds(5), [this]() { return paused || queued_buffers < max_queue_size; });
- if (queued_buffers > max_queue_size + 3) {
- release_cv.wait(lk, stop_token, [this] { return paused || queued_buffers < max_queue_size; });
+
+ const auto has_space = [this]() {
+ const u32 current_size = queued_buffers.load(std::memory_order_relaxed);
+ return paused || max_queue_size == 0 || current_size < max_queue_size;
+ };
+
+ if (!has_space()) {
+ // Wait until the queue falls below the configured limit or the stream is paused/stopped.
+ release_cv.wait(lk, stop_token, has_space);
}
}
diff --git a/src/audio_core/sink/sink_stream.h b/src/audio_core/sink/sink_stream.h
index cc34c3db45..b1fc91b649 100644
--- a/src/audio_core/sink/sink_stream.h
+++ b/src/audio_core/sink/sink_stream.h
@@ -240,7 +240,7 @@ private:
/// Ring buffer of the samples waiting to be played or consumed
Common::RingBuffer<s16, 0x10000> samples_buffer;
/// Audio buffers queued and waiting to play
- Common::SPSCQueue<SinkBuffer, 0x40000> queue;
+ Common::SPSCQueue<SinkBuffer, 0x10000> queue;
/// The currently-playing audio buffer
SinkBuffer playing_buffer{};
/// The last played (or received) frame of audio, used when the callback underruns