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"); + } }