From 6c0a9b31f6fc3c530927435792c82922fd91cef7 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Fri, 8 May 2026 17:43:23 +0200 Subject: [PATCH] alsa: write silence in smaller chunks With large quantum and many channels, the silence buffer can become quite large (many MB), which can cause a stack overflow. Use a fixed size silence buffer instead and write in smaller chunks. --- spa/plugins/alsa/alsa-pcm.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/spa/plugins/alsa/alsa-pcm.c b/spa/plugins/alsa/alsa-pcm.c index d332ea176..b31d0c2ff 100644 --- a/spa/plugins/alsa/alsa-pcm.c +++ b/spa/plugins/alsa/alsa-pcm.c @@ -2687,16 +2687,22 @@ static int spa_alsa_silence(struct state *state, snd_pcm_uframes_t silence) return res; } } else { - uint8_t buffer[silence * state->frame_size]; - memset(buffer, 0, silence * state->frame_size); + uint8_t buffer[1024 * 4]; + void *bufs[state->channels]; + snd_pcm_uframes_t chunk, remaining = silence; + snd_pcm_uframes_t max = sizeof(buffer) / state->frame_size; - if (state->planar) { - void *bufs[state->channels]; - for (i = 0; i < state->channels; i++) - bufs[i] = buffer; - snd_pcm_writen(hndl, bufs, silence); - } else { - snd_pcm_writei(hndl, buffer, silence); + memset(buffer, 0, sizeof(buffer)); + for (i = 0; i < state->channels; i++) + bufs[i] = buffer; + + while (remaining > 0) { + chunk = SPA_MIN(remaining, max); + if (state->planar) + snd_pcm_writen(hndl, bufs, chunk); + else + snd_pcm_writei(hndl, buffer, chunk); + remaining -= chunk; } } return 0;