From 48f8c128ef904bfc05ac1aa69a17b1527063673c Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 7 Feb 2024 17:50:14 +0100 Subject: [PATCH] Minimize buffer overflow on starting If playback starts too early, insert silence until the buffer is filled up to at least target_buffering before playing. --- app/src/audio_player.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/src/audio_player.c b/app/src/audio_player.c index e978cd9f..ea44e8d9 100644 --- a/app/src/audio_player.c +++ b/app/src/audio_player.c @@ -79,10 +79,9 @@ sc_audio_player_sdl_callback(void *userdata, uint8_t *stream, int len_int) { bool played = atomic_load_explicit(&ap->played, memory_order_relaxed); if (!played) { uint32_t buffered_samples = sc_audiobuf_can_read(&ap->buf); - // Part of the buffering is handled by inserting initial silence. The - // remaining (margin) last samples will be handled by compensation. - uint32_t margin = 30 * ap->sample_rate / 1000; // 30ms - if (buffered_samples + margin < ap->target_buffering) { + // Wait until the buffer is filled up to at least target_buffering + // before playing + if (buffered_samples < ap->target_buffering) { LOGV("[Audio] Inserting initial buffering silence: %" PRIu32 " samples", count); // Delay playback starting to reach the target buffering. Fill the