From ef9b2958ec0b467737674645cf43e8d1e028d30b Mon Sep 17 00:00:00 2001 From: Chris Spencer Date: Mon, 10 Jan 2022 21:27:35 +0000 Subject: [PATCH] [client] audio/pw: set maximum node latency This prevents severe buffer underruns if the PipeWire quantum is bigger than the ring buffer size. This could happen if a media player is running at the same time as Looking Glass if it requests a very large quantum size, for example. --- client/audiodevs/PipeWire/pipewire.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/client/audiodevs/PipeWire/pipewire.c b/client/audiodevs/PipeWire/pipewire.c index d390c512..a12f1c72 100644 --- a/client/audiodevs/PipeWire/pipewire.c +++ b/client/audiodevs/PipeWire/pipewire.c @@ -27,6 +27,7 @@ #include "common/debug.h" #include "common/ringbuffer.h" +#include "common/stringutils.h" #include "common/util.h" typedef enum @@ -202,21 +203,29 @@ static void pipewire_playbackStart(int channels, int sampleRate) pipewire_playbackStopStream(); + int bufferFrames = sampleRate / 10; + pw.playback.channels = channels; pw.playback.sampleRate = sampleRate; pw.playback.stride = sizeof(uint16_t) * channels; - pw.playback.buffer = ringbuffer_new(sampleRate / 10, + pw.playback.buffer = ringbuffer_new(bufferFrames, channels * sizeof(uint16_t)); + int maxLatencyFrames = bufferFrames / 2; + char maxLatency[32]; + snprintf(maxLatency, sizeof(maxLatency), "%d/%d", maxLatencyFrames, + sampleRate); + pw_thread_loop_lock(pw.thread); pw.playback.stream = pw_stream_new_simple( pw.loop, "Looking Glass", pw_properties_new( - PW_KEY_NODE_NAME , "Looking Glass", - PW_KEY_MEDIA_TYPE , "Audio", - PW_KEY_MEDIA_CATEGORY, "Playback", - PW_KEY_MEDIA_ROLE , "Music", + PW_KEY_NODE_NAME , "Looking Glass", + PW_KEY_MEDIA_TYPE , "Audio", + PW_KEY_MEDIA_CATEGORY , "Playback", + PW_KEY_MEDIA_ROLE , "Music", + PW_KEY_NODE_MAX_LATENCY, maxLatency, NULL ), &events,