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.
This commit is contained in:
Romain Vimont 2024-02-07 17:50:14 +01:00
parent 16f3246bcf
commit 48f8c128ef

View file

@ -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); bool played = atomic_load_explicit(&ap->played, memory_order_relaxed);
if (!played) { if (!played) {
uint32_t buffered_samples = sc_audiobuf_can_read(&ap->buf); uint32_t buffered_samples = sc_audiobuf_can_read(&ap->buf);
// Part of the buffering is handled by inserting initial silence. The // Wait until the buffer is filled up to at least target_buffering
// remaining (margin) last samples will be handled by compensation. // before playing
uint32_t margin = 30 * ap->sample_rate / 1000; // 30ms if (buffered_samples < ap->target_buffering) {
if (buffered_samples + margin < ap->target_buffering) {
LOGV("[Audio] Inserting initial buffering silence: %" PRIu32 LOGV("[Audio] Inserting initial buffering silence: %" PRIu32
" samples", count); " samples", count);
// Delay playback starting to reach the target buffering. Fill the // Delay playback starting to reach the target buffering. Fill the