From da9a5cfa5d09972783a23de066a67b9663d8070a Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Fri, 9 Sep 2022 16:36:33 +0200 Subject: [PATCH] alsa: subtract elapsed time from server delay only Only subtract the elapsed time from the server delay. Our reported delay should always at least still include the data that we have buffered or else read and write operations might think they can read or write more than they actually can. --- pipewire-alsa/alsa-plugins/pcm_pipewire.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pipewire-alsa/alsa-plugins/pcm_pipewire.c b/pipewire-alsa/alsa-plugins/pcm_pipewire.c index 9ae767ee5..9fc2d2be8 100644 --- a/pipewire-alsa/alsa-plugins/pcm_pipewire.c +++ b/pipewire-alsa/alsa-plugins/pcm_pipewire.c @@ -231,17 +231,20 @@ static int snd_pcm_pipewire_delay(snd_pcm_ioplug_t *io, snd_pcm_sframes_t *delay diff = SPA_TIMESPEC_TO_NSEC(&ts) - pw->time.now; elapsed = (io->rate * diff) / SPA_NSEC_PER_SEC; } + + filled = pw->time.delay; + + if (io->stream == SND_PCM_STREAM_PLAYBACK) + filled -= SPA_MIN(elapsed, filled); + else + filled += elapsed; + if (io->stream == SND_PCM_STREAM_PLAYBACK) avail = snd_pcm_ioplug_hw_avail(io, pw->hw_ptr, io->appl_ptr); else avail = snd_pcm_ioplug_avail(io, pw->hw_ptr, io->appl_ptr); - filled = pw->time.delay + avail; - - if (io->stream == SND_PCM_STREAM_PLAYBACK) - *delayp = filled - SPA_MIN(elapsed, filled); - else - *delayp = filled + elapsed; + *delayp = filled + avail; pw_log_trace("avail:%"PRIi64" filled %"PRIi64" elapsed:%"PRIi64" delay:%ld hw:%lu appl:%lu", avail, filled, elapsed, *delayp, pw->hw_ptr, io->appl_ptr);