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 <noreply@anthropic.com>
This commit is contained in:
2026-10-06 20:17:13 +00:00
co-authored by Claude Opus 5.5
parent 853681fcaa
commit 6cd75eb1cd
2 changed files with 45 additions and 2 deletions
@@ -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);
@@ -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<long[]> 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");
}
}