From 2aafe172d60b8a61c3c0793eb39b8c0c93f26c41 Mon Sep 17 00:00:00 2001 From: evgeny Date: Wed, 14 Oct 2026 00:11:31 +0200 Subject: [PATCH] Reject large audio timestamp jumps before narrowing PLC packet counts --- src/stream/stream_playback_audio.cpp | 12 ++++++++---- tools/chatgui/tests/test_stream_audio.cpp | 3 +++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/stream/stream_playback_audio.cpp b/src/stream/stream_playback_audio.cpp index 8613b999..949362ad 100644 --- a/src/stream/stream_playback_audio.cpp +++ b/src/stream/stream_playback_audio.cpp @@ -118,12 +118,16 @@ int stream_playback_audio_feed(struct stream_playback_audio* a, int64_t pts, con DEBUG_WARN(DEBUG_CATEGORY_STREAM, "playback audio late packet pts=%lld expected=%lld", (long long)pts, (long long)a->expectedPts); return 1; } - int missing = a->expectedPts < 0 ? 0 : (int)((pts - a->expectedPts + 10000) / 20000); - if (missing > 5) { - DEBUG_WARN(DEBUG_CATEGORY_STREAM, "playback audio discontinuity pts=%lld expected=%lld missing=%d", - (long long)pts, (long long)a->expectedPts, missing); return -2; + int64_t difference = a->expectedPts < 0 ? 0 : pts - a->expectedPts; + if (difference >= 110000) { + DEBUG_WARN(DEBUG_CATEGORY_STREAM, "playback audio discontinuity pts=%lld expected=%lld gap_us=%lld", + (long long)pts, (long long)a->expectedPts, (long long)difference); return -2; } + int missing = a->expectedPts < 0 ? 0 : (int)((difference + 10000) / 20000); int64_t effectivePts = a->expectedPts < 0 ? pts : a->expectedPts + missing * 20000; + if (effectivePts > INT64_MAX - 20000) { + DEBUG_ERROR(DEBUG_CATEGORY_STREAM, "playback audio normalized PTS overflow pts=%lld", (long long)effectivePts); return -1; + } for (int i = 0; i < missing; i++) if (feedPcm(a, a->expectedPts + i * 20000, nullptr, 0) < 0) return -1; if (feedPcm(a, effectivePts, packet, (int)length) < 0) return -1; a->expectedPts = effectivePts + 20000; diff --git a/tools/chatgui/tests/test_stream_audio.cpp b/tools/chatgui/tests/test_stream_audio.cpp index 8a6b0e60..14ee57bf 100644 --- a/tools/chatgui/tests/test_stream_audio.cpp +++ b/tools/chatgui/tests/test_stream_audio.cpp @@ -68,6 +68,9 @@ static void test_silence_clock() { int length = opus_codec_encode(encoder, pcm, 960, packet, sizeof(packet)); CHECK(length > 0); CHECK(stream_playback_audio_feed(audio, 1000000 + i * 20000, packet, (size_t)length) == 0); } + int length = opus_codec_encode(encoder, pcm, 960, packet, sizeof(packet)); CHECK(length > 0); + CHECK(stream_playback_audio_feed(audio, INT64_MAX - 20000, packet, (size_t)length) == -2); + CHECK(stream_playback_audio_feed(audio, 1080000, packet, (size_t)length) == 0); int got = stream_playback_audio_pull(audio, out, 960, INT64_MAX); CHECK(got > 0); uint64_t before = stream_playback_audio_written(audio); int64_t pts = stream_playback_audio_position(audio, before);