[client] audio: redesign playback synchronization

Model playback latency from the device period, arrival jitter, source
packet phase, and resampler delay. Treat audio:latencyOffset as an
explicit addition to this minimum and align the first device pull to
the next packet deadline. This starts playback near its steady-state
target without unnecessary prefill or startup underruns. Use a
512-frame default period as a practical low-latency baseline.

Replace the startup clock hold with a one-sided proportional
acquisition controller, then hand off to source/device rate
feed-forward and a slow phase loop. Calibrate the logical device
timeline at handoff, discard correction that opposes the current
error, and slew-limit rate changes. This prevents startup drain,
integral wind-up, overshoot, and long convergence while preserving
clock-drift compensation.

Allow audio backends to expose a real-time resampler and use
PipeWire's adaptive resampler when version 1.4 or newer supports it.
Retain libsamplerate as the fallback and add audio:resampler to select
the implementation. Wait for PipeWire stream setup to complete and
propagate rate-control failures cleanly.

Track PipeWire input-consumption and output-equivalent clocks
separately. The input clock measures ring latency while the output
clock drives feed-forward using the ratio that governed each request.
This removes delayed self-feedback that made adaptive resampling
oscillate between the correction limits.

Reduce audio:debug output to useful latency, clock, jitter, and xrun
values, and scale the playback graph from the startup estimate. Update
the option names and documentation for the new latency model.
This commit is contained in:
Geoffrey McRae
2026-07-29 04:55:16 +10:00
parent 814f1797fe
commit 737614bc69
7 changed files with 642 additions and 286 deletions

View File

@@ -60,6 +60,12 @@ struct PipeWire
atomic_bool latencyUpdateRequested;
atomic_uint bufferErrors;
atomic_uint timingErrors;
#if PW_CHECK_VERSION(1, 4, 0)
double appliedResampleRatio;
atomic_int resampleError;
#endif
enum pw_stream_state connectionState;
bool resamplerEnabled;
int channels;
int sampleRate;
@@ -105,6 +111,10 @@ static void pipewire_reportPlaybackErrors(void)
&pw.playback.bufferErrors, 0, memory_order_relaxed);
const unsigned int timingErrors = atomic_exchange_explicit(
&pw.playback.timingErrors, 0, memory_order_relaxed);
#if PW_CHECK_VERSION(1, 4, 0)
const int resampleError = atomic_exchange_explicit(
&pw.playback.resampleError, 0, memory_order_relaxed);
#endif
if (bufferErrors)
DEBUG_WARN("PipeWire playback ran out of buffers %u time(s)",
@@ -112,6 +122,11 @@ static void pipewire_reportPlaybackErrors(void)
if (timingErrors)
DEBUG_WARN("PipeWire playback timing query failed %u time(s)",
timingErrors);
#if PW_CHECK_VERSION(1, 4, 0)
if (resampleError)
DEBUG_WARN("PipeWire resampler rate update failed: %s",
spa_strerror(resampleError));
#endif
}
static void pipewire_reportRecordErrors(void)
@@ -175,6 +190,50 @@ static inline void pipewire_updatePlaybackLatency(void)
time.now + latencyNs, memory_order_release);
}
#if PW_CHECK_VERSION(1, 4, 0)
static bool pipewire_playbackSetRate(double ratio)
{
if (ratio <= 0.0 ||
!pw.playback.resamplerEnabled)
return false;
if (ratio == pw.playback.appliedResampleRatio)
return true;
const int result = pw_stream_set_rate(pw.playback.stream, ratio);
if (result < 0)
{
int expected = 0;
atomic_compare_exchange_strong_explicit(
&pw.playback.resampleError, &expected, result,
memory_order_relaxed, memory_order_relaxed);
return false;
}
pw.playback.appliedResampleRatio = ratio;
return true;
}
static bool pipewire_configurePlaybackResampler(bool enable)
{
pw.playback.resamplerEnabled = false;
pw.playback.appliedResampleRatio = 0.0;
if (!enable)
{
pw_stream_set_rate(pw.playback.stream, 0.0);
return false;
}
const int result = pw_stream_set_rate(pw.playback.stream, 1.0);
if (result < 0)
return false;
pw.playback.resamplerEnabled = true;
pw.playback.appliedResampleRatio = 1.0;
return true;
}
#endif
static void pipewire_onPlaybackIoChanged(void * userdata, uint32_t id,
void * data, uint32_t size)
{
@@ -186,6 +245,17 @@ static void pipewire_onPlaybackIoChanged(void * userdata, uint32_t id,
}
}
static void pipewire_onPlaybackStateChanged(void * userdata,
enum pw_stream_state old, enum pw_stream_state state,
const char * error)
{
pw.playback.connectionState = state;
if (state == PW_STREAM_STATE_ERROR)
DEBUG_ERROR("PipeWire playback stream failed: %s",
error ? error : "unknown error");
pw_thread_loop_signal(pw.thread, false);
}
static void pipewire_onPlaybackProcess(void * userdata)
{
struct pw_buffer * pbuf;
@@ -332,6 +402,7 @@ static void pipewire_playbackStopStream(void)
pw_stream_destroy(pw.playback.stream);
pw.playback.stream = NULL;
pw.playback.rateMatch = NULL;
pw.playback.resamplerEnabled = false;
atomic_store_explicit(
&pw.playback.presentationDeadline, 0, memory_order_release);
atomic_store_explicit(
@@ -341,24 +412,34 @@ static void pipewire_playbackStopStream(void)
}
static bool pipewire_playbackSetup(int channels, int sampleRate,
int requestedPeriodFrames, int * maxPeriodFrames, int * startFrames,
int requestedPeriodFrames, bool requestResampler,
bool * resamplerEnabled, int * maxPeriodFrames, int * startFrames,
LG_AudioPullFn pullFn)
{
*resamplerEnabled = false;
const struct spa_pod * params[1];
uint8_t buffer[1024];
struct spa_pod_builder b = SPA_POD_BUILDER_INIT(buffer, sizeof(buffer));
static const struct pw_stream_events events =
{
.version = PW_VERSION_STREAM_EVENTS,
.io_changed = pipewire_onPlaybackIoChanged,
.process = pipewire_onPlaybackProcess,
.drained = pipewire_onPlaybackDrained
.version = PW_VERSION_STREAM_EVENTS,
.state_changed = pipewire_onPlaybackStateChanged,
.io_changed = pipewire_onPlaybackIoChanged,
.process = pipewire_onPlaybackProcess,
.drained = pipewire_onPlaybackDrained
};
if (pw.playback.stream &&
pw.playback.channels == channels &&
pw.playback.sampleRate == sampleRate)
{
#if PW_CHECK_VERSION(1, 4, 0)
atomic_store_explicit(
&pw.playback.resampleError, 0, memory_order_relaxed);
*resamplerEnabled =
pipewire_configurePlaybackResampler(requestResampler);
#endif
*maxPeriodFrames = pw.playback.maxPeriodFrames;
*startFrames = pw.playback.startFrames;
return true;
@@ -464,6 +545,7 @@ static bool pipewire_playbackSetup(int channels, int sampleRate,
.rate = sampleRate
));
pw.playback.connectionState = PW_STREAM_STATE_CONNECTING;
const int result = pw_stream_connect(
pw.playback.stream,
PW_DIRECTION_OUTPUT,
@@ -484,6 +566,19 @@ static bool pipewire_playbackSetup(int channels, int sampleRate,
return false;
}
while (pw.playback.connectionState == PW_STREAM_STATE_CONNECTING)
pw_thread_loop_wait(pw.thread);
if (pw.playback.connectionState != PW_STREAM_STATE_PAUSED)
{
DEBUG_ERROR("PipeWire playback stream did not become ready");
pw_stream_destroy(pw.playback.stream);
pw.playback.stream = NULL;
pw.playback.rateMatch = NULL;
pw_thread_loop_unlock(pw.thread);
return false;
}
pw.playback.state = STREAM_STATE_INACTIVE;
atomic_store_explicit(
&pw.playback.presentationDeadline, 0, memory_order_release);
@@ -493,6 +588,14 @@ static bool pipewire_playbackSetup(int channels, int sampleRate,
&pw.playback.bufferErrors, 0, memory_order_relaxed);
atomic_store_explicit(
&pw.playback.timingErrors, 0, memory_order_relaxed);
#if PW_CHECK_VERSION(1, 4, 0)
atomic_store_explicit(
&pw.playback.resampleError, 0, memory_order_relaxed);
*resamplerEnabled =
pipewire_configurePlaybackResampler(requestResampler);
#else
(void)requestResampler;
#endif
pw_thread_loop_unlock(pw.thread);
return true;
}
@@ -932,6 +1035,9 @@ struct LG_AudioDevOps LGAD_PipeWire =
.stop = pipewire_playbackStop,
.volume = pipewire_playbackVolume,
.mute = pipewire_playbackMute,
#if PW_CHECK_VERSION(1, 4, 0)
.setRate = pipewire_playbackSetRate,
#endif
.latency = pipewire_playbackLatency
},
.record =

View File

@@ -300,9 +300,12 @@ static void pulseaudio_overflow_cb(pa_stream * p, void * userdata)
}
static bool pulseaudio_setup(int channels, int sampleRate,
int requestedPeriodFrames, int * maxPeriodFrames, int * startFrames,
int requestedPeriodFrames, bool requestResampler,
bool * resamplerEnabled, int * maxPeriodFrames, int * startFrames,
LG_AudioPullFn pullFn)
{
*resamplerEnabled = false;
if (pa.sink && pa.sinkChannels == channels && pa.sinkSampleRate == sampleRate)
{
*maxPeriodFrames = pa.sinkMaxPeriodFrames;

View File

@@ -45,10 +45,13 @@ struct LG_AudioDevOps
struct
{
/* setup the stream for playback but don't start it yet, returning false
* if the stream could not be configured
* if the stream could not be configured. If backend resampling is
* requested, resamplerEnabled reports whether it was activated for this
* stream.
* Note: the pull function returns f32 samples
*/
bool (*setup)(int channels, int sampleRate, int requestedPeriodFrames,
bool requestResampler, bool * resamplerEnabled,
int * maxPeriodFrames, int * startFrames, LG_AudioPullFn pullFn);
/* called when there is data available to start playback */
@@ -63,6 +66,10 @@ struct LG_AudioDevOps
/* [optional] called to set muting of the output */
void (*mute)(bool mute);
/* [optional] update the active backend resampler's output/input ratio.
* Called from the backend's playback callback and must be realtime safe. */
bool (*setRate)(double ratio);
/* return the current total playback latency in microseconds */
uint64_t (*latency)(void);
}

File diff suppressed because it is too large Load Diff

View File

@@ -50,6 +50,10 @@ static bool optRotateValidate (struct Option * opt, const char ** error
static bool optMicDefaultParse (struct Option * opt, const char * str);
static StringList optMicDefaultValues (struct Option * opt);
static char * optMicDefaultToString(struct Option * opt);
static bool optAudioResamplerParse (struct Option * opt,
const char * str);
static StringList optAudioResamplerValues (struct Option * opt);
static char * optAudioResamplerToString(struct Option * opt);
static void doLicense(void);
@@ -542,14 +546,23 @@ static struct Option options[] =
.name = "periodSize",
.description = "Requested audio device period size in samples (0 = 10 ms)",
.type = OPTION_TYPE_INT,
.value.x_int = 0
.value.x_int = 512
},
{
.module = "audio",
.name = "bufferLatency",
.description = "Additional buffer latency in milliseconds",
.name = "latencyOffset",
.description = "Latency offset added to the calculated minimum in milliseconds",
.type = OPTION_TYPE_INT,
.value.x_int = 4
.value.x_int = 6
},
{
.module = "audio",
.name = "resampler",
.description = "Audio resampler to use (auto, libsamplerate, backend)",
.type = OPTION_TYPE_CUSTOM,
.parser = optAudioResamplerParse,
.getValues = optAudioResamplerValues,
.toString = optAudioResamplerToString
},
{
.module = "audio",
@@ -772,7 +785,7 @@ bool config_load(int argc, char * argv[])
g_params.audioDebug = option_get_bool("audio", "debug");
g_params.audioPeriodSize = option_get_int("audio", "periodSize");
g_params.audioBufferLatency = option_get_int("audio", "bufferLatency");
g_params.audioLatencyOffset = option_get_int("audio", "latencyOffset");
g_params.micShowIndicator = option_get_bool("audio", "micShowIndicator");
g_params.audioSyncVolume = option_get_bool("audio", "syncVolume");
@@ -1055,3 +1068,48 @@ static char * optMicDefaultToString(struct Option * opt)
return NULL;
}
static bool optAudioResamplerParse(
struct Option * opt, const char * str)
{
if (!str)
return false;
if (strcasecmp(str, "auto") == 0)
g_params.audioResampler = AUDIO_RESAMPLER_AUTO;
else if (strcasecmp(str, "libsamplerate") == 0)
g_params.audioResampler = AUDIO_RESAMPLER_LIBSAMPLERATE;
else if (strcasecmp(str, "backend") == 0)
g_params.audioResampler = AUDIO_RESAMPLER_BACKEND;
else
return false;
return true;
}
static StringList optAudioResamplerValues(struct Option * opt)
{
StringList sl = stringlist_new(false);
if (!sl)
return NULL;
stringlist_push(sl, (char *)"auto");
stringlist_push(sl, (char *)"libsamplerate");
stringlist_push(sl, (char *)"backend");
return sl;
}
static char * optAudioResamplerToString(struct Option * opt)
{
switch (g_params.audioResampler)
{
case AUDIO_RESAMPLER_AUTO:
return strdup("auto");
case AUDIO_RESAMPLER_LIBSAMPLERATE:
return strdup("libsamplerate");
case AUDIO_RESAMPLER_BACKEND:
return strdup("backend");
}
return NULL;
}

View File

@@ -53,6 +53,12 @@ enum MicDefaultState {
};
#define MIC_DEFAULT_MAX (MIC_DEFAULT_DENY + 1)
enum AudioResampler {
AUDIO_RESAMPLER_AUTO,
AUDIO_RESAMPLER_LIBSAMPLERATE,
AUDIO_RESAMPLER_BACKEND
};
struct AppState
{
_Atomic(enum RunState) state;
@@ -239,7 +245,8 @@ struct AppParams
bool audioDebug;
int audioPeriodSize;
int audioBufferLatency;
int audioLatencyOffset;
enum AudioResampler audioResampler;
bool micShowIndicator;
enum MicDefaultState micDefaultState;
bool audioSyncVolume;

View File

@@ -370,9 +370,9 @@ All command line options
+------------------------+-------+--------+-------------------------------------------------------------------------------+
| Long | Short | Value | Description |
+========================+=======+========+===============================================================================+
| audio:periodSize | | 2048 | Requested audio device period size in samples |
| audio:periodSize | | 512 | Requested audio device period size in samples |
+------------------------+-------+--------+-------------------------------------------------------------------------------+
| audio:bufferLatency | | 13 | Additional buffer latency in milliseconds |
| audio:latencyOffset | | 4 | Latency offset added to the calculated minimum in milliseconds |
+------------------------+-------+--------+-------------------------------------------------------------------------------+
| audio:micDefault | | prompt | Default action when an application opens the microphone (prompt, allow, deny) |
+------------------------+-------+--------+-------------------------------------------------------------------------------+