From b4ed8dcf14b9b3162ad367180c6156952c5922f5 Mon Sep 17 00:00:00 2001 From: Martin Geier Date: Wed, 27 Mar 2024 11:48:10 +0100 Subject: [PATCH] alsa-pcm: do not allow headroom plus threshold be bigger then the alsa buffer Headroom are extra samples available in alsa buffer on top of a threshold. Its use to prefill alsa buffer with silence before the playback starts and later its use to calculate target number of a frames in the alsa buffer when get_status is called. Target is calculated as headroom plus threshold, which should be smaller then buffer size to make sense. Signed-off-by: Martin Geier Signed-off-by: Carlos Rafael Giani --- spa/plugins/alsa/alsa-pcm.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spa/plugins/alsa/alsa-pcm.c b/spa/plugins/alsa/alsa-pcm.c index fee65ab5d..20f0fcd51 100644 --- a/spa/plugins/alsa/alsa-pcm.c +++ b/spa/plugins/alsa/alsa-pcm.c @@ -1939,7 +1939,10 @@ static void recalc_headroom(struct state *state) if (state->stream == SND_PCM_STREAM_CAPTURE) state->headroom = SPA_MAX(state->headroom, 32u); } - state->headroom = SPA_MIN(state->headroom, state->buffer_frames); + if (SPA_LIKELY(state->buffer_frames >= state->threshold)) + state->headroom = SPA_MIN(state->headroom, state->buffer_frames - state->threshold); + else + state->headroom = 0; latency = SPA_MAX(state->min_delay, SPA_MIN(state->max_delay, state->headroom)); if (rate != 0 && state->rate != 0)