[client] audio: rework synchronization for low latency

Drive playback timing from audio sample positions instead of packet
arrival cadence. Use SPICE multimedia timestamps to anchor streams,
detect discontinuities, and estimate the long-term source rate. Ignore
their coarse phase corrections when steering the playback buffer.

Measure the device clock independently and wait for it to stabilize
before enabling a bounded, slew-limited phase controller. Align startup
buffering from logical producer and consumer positions so backend
startup reserves do not become persistent latency.

Size the buffer from the backend period, measured delivery jitter, and
resampler delay. Update PipeWire and PulseAudio to report their actual
startup requirements and presentation latency. Default to a 10 ms
device period with 4 ms of additional buffering, and make detailed
synchronization diagnostics opt-in through audio:debug.

This reduces startup and steady-state latency while compensating for
physical and virtual device clock drift without reacting to transport
jitter or SPICE timestamp quantization.
This commit is contained in:
Geoffrey McRae
2026-07-28 21:07:02 +10:00
parent 4ef6672e64
commit d956dd8fd3
8 changed files with 1123 additions and 383 deletions

View File

@@ -23,8 +23,10 @@
#include <pulse/pulseaudio.h>
#include <string.h>
#include <math.h>
#include <stdatomic.h>
#include "common/debug.h"
#include "common/time.h"
struct PulseAudio
{
@@ -44,10 +46,17 @@ struct PulseAudio
int sinkChannels;
int sinkStride;
LG_AudioPullFn sinkPullFn;
_Atomic(int64_t) sinkPresentationDeadline;
};
static struct PulseAudio pa = {0};
static void pulseaudio_unrefOperation(pa_operation * operation)
{
if (operation)
pa_operation_unref(operation);
}
static void pulseaudio_sink_input_cb(pa_context *c, const pa_sink_input_info *i,
int eol, void *userdata)
{
@@ -69,7 +78,7 @@ static void pulseaudio_subscribe_cb(pa_context *c,
{
pa_operation *o = pa_context_get_sink_input_info(c, index,
pulseaudio_sink_input_cb, NULL);
pa_operation_unref(o);
pulseaudio_unrefOperation(o);
}
break;
}
@@ -194,10 +203,15 @@ static void pulseaudio_sink_close_nl(void)
if (!pa.sink)
return;
pa_stream_set_state_callback(pa.sink, NULL, NULL);
pa_stream_set_write_callback(pa.sink, NULL, NULL);
pa_stream_flush(pa.sink, NULL, NULL);
pa_stream_set_underflow_callback(pa.sink, NULL, NULL);
pa_stream_set_overflow_callback(pa.sink, NULL, NULL);
pulseaudio_unrefOperation(pa_stream_flush(pa.sink, NULL, NULL));
pa_stream_unref(pa.sink);
pa.sink = NULL;
atomic_store_explicit(
&pa.sinkPresentationDeadline, 0, memory_order_release);
}
static void pulseaudio_free(void)
@@ -224,10 +238,12 @@ static void pulseaudio_state_cb(pa_stream * p, void * userdata)
{
if (pa.sinkStarting && pa_stream_get_state(pa.sink) == PA_STREAM_READY)
{
pa_stream_cork(pa.sink, 0, NULL, NULL);
pulseaudio_unrefOperation(pa_stream_cork(pa.sink, 0, NULL, NULL));
pa.sinkCorked = false;
pa.sinkStarting = false;
}
pa_threaded_mainloop_signal(pa.loop, 0);
}
static void pulseaudio_write_cb(pa_stream * p, size_t nbytes, void * userdata)
@@ -239,12 +255,38 @@ static void pulseaudio_write_cb(pa_stream * p, size_t nbytes, void * userdata)
uint8_t * dst;
pa_stream_begin_write(p, (void **)&dst, &nbytes);
if (pa_stream_begin_write(p, (void **)&dst, &nbytes) < 0)
{
DEBUG_ERROR("pa_stream_begin_write failed: %s",
pa_strerror(pa_context_errno(pa.context)));
return;
}
int frames = nbytes / pa.sinkStride;
frames = pa.sinkPullFn(dst, frames);
if (frames <= 0)
{
pa_stream_cancel_write(p);
return;
}
pa_stream_write(p, dst, frames * pa.sinkStride, NULL, 0, PA_SEEK_RELATIVE);
if (pa_stream_write(
p, dst, frames * pa.sinkStride, NULL, 0, PA_SEEK_RELATIVE) < 0)
{
DEBUG_ERROR("pa_stream_write failed: %s",
pa_strerror(pa_context_errno(pa.context)));
pa_stream_cancel_write(p);
return;
}
pa_usec_t latency;
int negative;
if (pa_stream_get_latency(p, &latency, &negative) == 0)
{
const int64_t latencyNs = negative ? 0 : (int64_t)latency * 1000;
atomic_store_explicit(&pa.sinkPresentationDeadline,
(int64_t)nanotime() + latencyNs, memory_order_release);
}
}
static void pulseaudio_underflow_cb(pa_stream * p, void * userdata)
@@ -257,7 +299,7 @@ static void pulseaudio_overflow_cb(pa_stream * p, void * userdata)
DEBUG_WARN("Overflow");
}
static void pulseaudio_setup(int channels, int sampleRate,
static bool pulseaudio_setup(int channels, int sampleRate,
int requestedPeriodFrames, int * maxPeriodFrames, int * startFrames,
LG_AudioPullFn pullFn)
{
@@ -265,7 +307,7 @@ static void pulseaudio_setup(int channels, int sampleRate,
{
*maxPeriodFrames = pa.sinkMaxPeriodFrames;
*startFrames = pa.sinkStartFrames;
return;
return true;
}
pa_sample_spec spec = {
@@ -289,30 +331,76 @@ static void pulseaudio_setup(int channels, int sampleRate,
pa.sinkChannels = channels;
pa.sinkSampleRate = sampleRate;
pa.sinkStride = stride;
pa.sinkPullFn = pullFn;
pa.sinkCorked = true;
pa.sinkStarting = false;
pa.sink = pa_stream_new(pa.context, "Looking Glass", &spec, NULL);
if (!pa.sink)
{
DEBUG_ERROR("Failed to create PulseAudio stream: %s",
pa_strerror(pa_context_errno(pa.context)));
pa_threaded_mainloop_unlock(pa.loop);
return false;
}
pa_stream_set_state_callback (pa.sink, pulseaudio_state_cb , NULL);
pa_stream_set_write_callback (pa.sink, pulseaudio_write_cb , NULL);
pa_stream_set_underflow_callback(pa.sink, pulseaudio_underflow_cb, NULL);
pa_stream_set_overflow_callback (pa.sink, pulseaudio_overflow_cb , NULL);
pa_stream_connect_playback(pa.sink, NULL, &attribs, PA_STREAM_START_CORKED,
NULL, NULL);
const pa_stream_flags_t flags =
PA_STREAM_START_CORKED |
PA_STREAM_ADJUST_LATENCY |
PA_STREAM_INTERPOLATE_TIMING |
PA_STREAM_AUTO_TIMING_UPDATE;
if (pa_stream_connect_playback(
pa.sink, NULL, &attribs, flags, NULL, NULL) < 0)
{
DEBUG_ERROR("Failed to connect PulseAudio stream: %s",
pa_strerror(pa_context_errno(pa.context)));
pulseaudio_sink_close_nl();
pa_threaded_mainloop_unlock(pa.loop);
return false;
}
pa.sinkStride = stride;
pa.sinkPullFn = pullFn;
pa.sinkMaxPeriodFrames = requestedPeriodFrames;
pa.sinkCorked = true;
pa.sinkStarting = false;
while (pa_stream_get_state(pa.sink) == PA_STREAM_CREATING)
pa_threaded_mainloop_wait(pa.loop);
// If something else is, or was recently using a small latency value,
// PulseAudio can request way more data at startup than is reasonable
pa.sinkStartFrames = requestedPeriodFrames * 4;
if (pa_stream_get_state(pa.sink) != PA_STREAM_READY)
{
DEBUG_ERROR("PulseAudio stream did not become ready: %s",
pa_strerror(pa_context_errno(pa.context)));
pulseaudio_sink_close_nl();
pa_threaded_mainloop_unlock(pa.loop);
return false;
}
*maxPeriodFrames = requestedPeriodFrames;
const pa_buffer_attr * actual = pa_stream_get_buffer_attr(pa.sink);
const uint64_t minRequestFrames =
actual && actual->minreq != UINT32_MAX ?
actual->minreq / (uint64_t)stride : requestedPeriodFrames;
const uint64_t targetFrames =
actual && actual->tlength != UINT32_MAX ?
actual->tlength / (uint64_t)stride :
(uint64_t)requestedPeriodFrames * 2;
const int actualMinRequest =
clamp(minRequestFrames, UINT64_C(1), (uint64_t)INT_MAX);
const int actualTarget =
clamp(max(targetFrames, minRequestFrames),
UINT64_C(1), (uint64_t)INT_MAX);
pa.sinkMaxPeriodFrames = actualMinRequest;
pa.sinkStartFrames = actualTarget;
*maxPeriodFrames = pa.sinkMaxPeriodFrames;
*startFrames = pa.sinkStartFrames;
atomic_store_explicit(
&pa.sinkPresentationDeadline, 0, memory_order_release);
pa_threaded_mainloop_unlock(pa.loop);
return true;
}
static void pulseaudio_start(void)
@@ -327,7 +415,7 @@ static void pulseaudio_start(void)
pa.sinkStarting = true;
else
{
pa_stream_cork(pa.sink, 0, NULL, NULL);
pulseaudio_unrefOperation(pa_stream_cork(pa.sink, 0, NULL, NULL));
pa.sinkCorked = false;
}
@@ -343,9 +431,11 @@ static void pulseaudio_stop(void)
if (needLock)
pa_threaded_mainloop_lock(pa.loop);
pa_stream_cork(pa.sink, 1, NULL, NULL);
pulseaudio_unrefOperation(pa_stream_cork(pa.sink, 1, NULL, NULL));
pa.sinkCorked = true;
pa.sinkStarting = false;
atomic_store_explicit(
&pa.sinkPresentationDeadline, 0, memory_order_release);
if (needLock)
pa_threaded_mainloop_unlock(pa.loop);
@@ -362,7 +452,8 @@ static void pulseaudio_volume(int channels, const uint16_t volume[])
9.3234e-7 * pow(1.000211902, volume[i]) - 0.000172787);
pa_threaded_mainloop_lock(pa.loop);
pa_context_set_sink_input_volume(pa.context, pa.sinkIndex, &v, NULL, NULL);
pulseaudio_unrefOperation(pa_context_set_sink_input_volume(
pa.context, pa.sinkIndex, &v, NULL, NULL));
pa_threaded_mainloop_unlock(pa.loop);
}
@@ -373,10 +464,21 @@ static void pulseaudio_mute(bool mute)
pa.sinkMuted = mute;
pa_threaded_mainloop_lock(pa.loop);
pa_context_set_sink_input_mute(pa.context, pa.sinkIndex, mute, NULL, NULL);
pulseaudio_unrefOperation(pa_context_set_sink_input_mute(
pa.context, pa.sinkIndex, mute, NULL, NULL));
pa_threaded_mainloop_unlock(pa.loop);
}
static uint64_t pulseaudio_latency(void)
{
const int64_t deadline = atomic_load_explicit(
&pa.sinkPresentationDeadline, memory_order_acquire);
if (deadline <= 0)
return 0;
return max(INT64_C(0), deadline - (int64_t)nanotime()) / 1000;
}
struct LG_AudioDevOps LGAD_PulseAudio =
{
.name = "PulseAudio",
@@ -388,6 +490,7 @@ struct LG_AudioDevOps LGAD_PulseAudio =
.start = pulseaudio_start,
.stop = pulseaudio_stop,
.volume = pulseaudio_volume,
.mute = pulseaudio_mute
.mute = pulseaudio_mute,
.latency = pulseaudio_latency
}
};