From 6cd75eb1cd9fe2d5ce15f414e3786582ed36141f Mon Sep 17 00:00:00 2001 From: ericek111 Date: Tue, 6 Oct 2026 20:17:13 +0000 Subject: [PATCH] Let late voice packets teach the jitter buffer A packet arriving after playback passed its slot was dropped before the buffer saw it, so the buffer never learned how late packets ran and did not grow to cover them. The official client puts every packet, however late; such packets now reach the buffer too, without starting a new talk burst. Co-Authored-By: Claude Opus 5.5 --- .../java/com/ts3client/audio/VoiceStream.java | 8 +++- .../com/ts3client/audio/VoiceStreamTest.java | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/ts3-client/core/src/main/java/com/ts3client/audio/VoiceStream.java b/ts3-client/core/src/main/java/com/ts3client/audio/VoiceStream.java index dac4489..2e7763a 100644 --- a/ts3-client/core/src/main/java/com/ts3client/audio/VoiceStream.java +++ b/ts3-client/core/src/main/java/com/ts3client/audio/VoiceStream.java @@ -125,8 +125,12 @@ final class VoiceStream { markStop(ticks); return false; } - // Playback already went past it; the buffer would only drop it. - if (!fresh && ticks - jitter.position() < 0) return false; + // Playback already went past it, but the buffer still learns how late it ran, as in + // the official client; that is what grows the delay to wait for redundant copies. + if (!fresh && ticks - jitter.position() < 0) { + jitter.put(data, ticks, FRAME_TICKS); + return false; + } channels = channelsFor(codec); jitter.put(data, ticks, FRAME_TICKS); diff --git a/ts3-client/core/src/test/java/com/ts3client/audio/VoiceStreamTest.java b/ts3-client/core/src/test/java/com/ts3client/audio/VoiceStreamTest.java index 49e98f4..7e39a13 100644 --- a/ts3-client/core/src/test/java/com/ts3client/audio/VoiceStreamTest.java +++ b/ts3-client/core/src/test/java/com/ts3client/audio/VoiceStreamTest.java @@ -77,6 +77,17 @@ class VoiceStreamTest { assertFalse(offer(0, 9)); } + @Test + void aLateCopyAfterTheBurstDoesNotStartAnother() { + offer(0, 1); + offerEnd(1); + pull(); + pull(); + assertFalse(offer(0, 1)); + assertFalse(stream.isPlaying()); + assertEquals(List.of(true, false), talk); + } + @Test void theTalkersStopEndsTheBurst() { offer(0, 1); @@ -166,4 +177,32 @@ class VoiceStreamTest { } assertTrue(concealedLate < packets / 2 / 50, "concealed " + concealedLate + " frames in the second half"); } + + /** + * Every packet is sent twice, the copy 120 ms after the original, and a tenth of the + * originals are lost. The copies arrive too late for a buffer tuned to the originals, so + * it has to learn from them and grow until they are in time to stand in. + */ + @Test + void growsToWaitForRedundantCopies() { + Random random = new Random(1); + int packets = 1500; + List arrivals = new ArrayList<>(); + for (int i = 0; i < packets; i++) { + if (random.nextInt(10) != 0) arrivals.add(new long[]{i * 20L, i}); + arrivals.add(new long[]{i * 20L + 120, i}); + } + arrivals.sort((x, y) -> Long.compare(x[0], y[0])); + + int next = 0; + int concealedLate = 0; + for (long now = 0; now < packets * 20L; now += 20) { + for (; next < arrivals.size() && arrivals.get(next)[0] <= now; next++) { + offer((int) arrivals.get(next)[1], 1); + } + float sample = pull(); + if (now >= packets * 10L && sample == FakeOpus.CONCEALED) concealedLate++; + } + assertTrue(concealedLate < packets / 2 / 100, "concealed " + concealedLate + " frames in the second half"); + } }