From 0c5b7af2693f591cb1f68c4c2662ac5ec23f1eee Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 8 Feb 2024 12:31:03 +0100 Subject: [PATCH] Avoid "negative" average buffering Average buffering may not be negative. Since dropping samples instantly reduces buffering, it is applied immediately to the average value. However, if an audio output issue causes the SDL callback to be called too infrequently, samples will (silently) not be consumed as expected. As this will cause samples to be dropped due to overbuffering, the average value may drop below 0, which makes no sense. --- app/src/audio_player.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/src/audio_player.c b/app/src/audio_player.c index ea44e8d9..87c93233 100644 --- a/app/src/audio_player.c +++ b/app/src/audio_player.c @@ -266,6 +266,14 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink, // The compensation must apply instantly, it must not be smoothed ap->avg_buffering.avg += instant_compensation + inserted_silence - dropped; + if (ap->avg_buffering.avg < 0) { + // Negative buffering makes no sense + // This may happen when the consumer does not consume at the expected + // rate (the SDL calback is not called often enough, which is an audio + // output issue), causing many samples to be dropped due to + // overbuffering. + ap->avg_buffering.avg = 0; + } // However, the buffering level must be smoothed sc_average_push(&ap->avg_buffering, can_read);