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.
This commit is contained in:
Wim Taymans 2022-09-09 16:36:33 +02:00
parent f58021ed45
commit da9a5cfa5d

View file

@ -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; diff = SPA_TIMESPEC_TO_NSEC(&ts) - pw->time.now;
elapsed = (io->rate * diff) / SPA_NSEC_PER_SEC; 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) if (io->stream == SND_PCM_STREAM_PLAYBACK)
avail = snd_pcm_ioplug_hw_avail(io, pw->hw_ptr, io->appl_ptr); avail = snd_pcm_ioplug_hw_avail(io, pw->hw_ptr, io->appl_ptr);
else else
avail = snd_pcm_ioplug_avail(io, pw->hw_ptr, io->appl_ptr); avail = snd_pcm_ioplug_avail(io, pw->hw_ptr, io->appl_ptr);
filled = pw->time.delay + avail; *delayp = filled + avail;
if (io->stream == SND_PCM_STREAM_PLAYBACK)
*delayp = filled - SPA_MIN(elapsed, filled);
else
*delayp = filled + elapsed;
pw_log_trace("avail:%"PRIi64" filled %"PRIi64" elapsed:%"PRIi64" delay:%ld hw:%lu appl:%lu", 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); avail, filled, elapsed, *delayp, pw->hw_ptr, io->appl_ptr);