From c1eacce3fc39bdf0ce7914f0492cb48e69532223 Mon Sep 17 00:00:00 2001 From: Dimitrios Katsaros Date: Fri, 1 Mar 2024 21:36:47 +0100 Subject: [PATCH] alsa: Use hi-res timestamps in irq mode when available We know in IRQ mode that any valid hi-res timestamp that the driver privides will be before the wakeup event in pipewire. This makes it so in IRQ mode we use better timestamping when possible, which decreases jitter injected into the DLL, which in turn reduces the amount of oscillations the resampler is exposed to. --- spa/plugins/alsa/alsa-pcm.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/spa/plugins/alsa/alsa-pcm.c b/spa/plugins/alsa/alsa-pcm.c index 55734024e..8c8dc2366 100644 --- a/spa/plugins/alsa/alsa-pcm.c +++ b/spa/plugins/alsa/alsa-pcm.c @@ -3239,8 +3239,19 @@ static void alsa_irq_wakeup_event(struct spa_source *source) uint64_t current_time; int res, err; unsigned short revents; + snd_pcm_uframes_t havail; + snd_htimestamp_t tstamp; + // First, take a snapshot of the wakeup time current_time = get_time_ns(state); + // If the hi-res timestamps are working, we will get a timestamp that + // is earlier then current_time + if ((res = snd_pcm_htimestamp(state->hndl, &havail, &tstamp)) == 0) { + uint64_t htime = SPA_TIMESPEC_TO_NSEC(&tstamp); + if (htime < current_time) { + current_time = htime; + } + } for (int i = 0; i < state->n_fds; i++) { state->pfds[i].revents = state->source[i].rmask;