Skip to content

Commit

Permalink
Avoid "negative" average buffering
Browse files Browse the repository at this point in the history
The assumption that underflow and overbuffering are caused by jitter
(and that the delay between the producer and consumer will becaught up)
does not always hold.

For example, if the consumer does not consume at the expected rate (the
SDL calback is not called often enough, which is an audio output issue),
many samples will be dropped due to overbuffering, decreasing the
average buffering indefinitely.

Prevent the average buffering to become negative to limit the
consequences of an unexpected behavior.
  • Loading branch information
rom1v committed Feb 16, 2024
1 parent d335683 commit 4e35761
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/src/audio_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ 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) {
// Since dropping samples instantly reduces buffering, the difference
// is applied immediately to the average value, assuming that the delay
// between the producer and the consumer will be caught up.
//
// However, when this assumption is not valid, the average buffering
// may decrease indefinitely. Prevent it to become negative to limit
// the consequences.
ap->avg_buffering.avg = 0;
}

// However, the buffering level must be smoothed
sc_average_push(&ap->avg_buffering, can_read);
Expand Down

0 comments on commit 4e35761

Please sign in to comment.