From 0bf866fa8d98e8d35c08de5c69e33b80ccda8c44 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sat, 11 Mar 2023 23:00:48 +0100 Subject: [PATCH] Apply new compensation only if it changed If the compensation is the same (typically when it is 0), do not reapply it. --- app/src/audio_player.c | 14 ++++++++++---- app/src/audio_player.h | 3 +++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/src/audio_player.c b/app/src/audio_player.c index aa34c316..0511ec1f 100644 --- a/app/src/audio_player.c +++ b/app/src/audio_player.c @@ -258,10 +258,15 @@ sc_audio_player_frame_sink_push(struct sc_frame_sink *sink, LOGV("[Audio] Buffering: target=%" PRIu32 " avg=%f cur=%" PRIu32 " compensation=%d", ap->target_buffering, avg, buffered_samples, diff); - int ret = swr_set_compensation(swr_ctx, diff, distance); - if (ret < 0) { - LOGW("Resampling compensation failed: %d", ret); - // not fatal + + if (diff != ap->compensation) { + int ret = swr_set_compensation(swr_ctx, diff, distance); + if (ret < 0) { + LOGW("Resampling compensation failed: %d", ret); + // not fatal + } else { + ap->compensation = diff; + } } } } @@ -369,6 +374,7 @@ sc_audio_player_frame_sink_open(struct sc_frame_sink *sink, ap->received = false; ap->played = false; ap->underflow = 0; + ap->compensation = 0; // The thread calling open() is the thread calling push(), which fills the // audio buffer consumed by the SDL audio thread. diff --git a/app/src/audio_player.h b/app/src/audio_player.h index 3227f2fe..4dd9c4dc 100644 --- a/app/src/audio_player.h +++ b/app/src/audio_player.h @@ -60,6 +60,9 @@ struct sc_audio_player { // (protected by SDL_AudioDeviceLock()) uint32_t underflow; + // Current applied compensation value (only used by the receiver thread) + int compensation; + // Set to true the first time a sample is received (protected by // SDL_AudioDeviceLock()) bool received;