Add extra receive buffering and decode Opus FEC on loss
A new Extra buffering setting (Network page) holds more playback delay than the jitter buffer measures it needs, so late packets still make it in time on a bad connection. A lost frame is now rebuilt from the FEC copy in the next packet when that one is already held, and the Connection Info shows how many were recovered. Held packets' arrival marks now move with the timeline when the delay changes; libspeex leaves them, which made a multi-frame jump count twice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,11 +10,12 @@ import java.util.List;
|
||||
|
||||
/**
|
||||
* Stands in for libopus: a "packet" of n bytes decodes to n 20 ms frames holding the first byte
|
||||
* divided by 100 in every sample. Concealment yields -1, so it is easy to spot.
|
||||
* divided by 100 in every sample. Concealment yields -1 and recovery -2, so both are easy to spot.
|
||||
*/
|
||||
final class FakeOpus implements OpusCodec {
|
||||
|
||||
static final float CONCEALED = -1f;
|
||||
static final float RECOVERED = -2f;
|
||||
|
||||
int resets;
|
||||
final List<Integer> concealedFrameSizes = new ArrayList<>();
|
||||
@@ -45,6 +46,12 @@ final class FakeOpus implements OpusCodec {
|
||||
return frameSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int recover(byte[] next, float[] out, int frameSize) {
|
||||
Arrays.fill(out, 0, frameSize * channels, RECOVERED);
|
||||
return frameSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
resets++;
|
||||
|
||||
@@ -51,6 +51,27 @@ class JitterBufferTest {
|
||||
assertTrue(real.size() >= 15, "most frames should have played: " + played);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLargerMarginBuffersThatMuchMore() {
|
||||
JitterBuffer b = buffer();
|
||||
int n = 0;
|
||||
int lastPlayed = -1;
|
||||
for (; n < 60; n++) {
|
||||
put(b, n);
|
||||
int tag = pull(b);
|
||||
if (tag >= 0) lastPlayed = tag;
|
||||
}
|
||||
int before = n - 1 - lastPlayed;
|
||||
|
||||
b.setMargin(b.margin() + 5 * SPAN);
|
||||
for (; n < 120; n++) {
|
||||
put(b, n);
|
||||
int tag = pull(b);
|
||||
if (tag >= 0) lastPlayed = tag;
|
||||
}
|
||||
assertEquals(before + 5, n - 1 - lastPlayed);
|
||||
}
|
||||
|
||||
@Test
|
||||
void reordersPacketsThatArriveOutOfOrder() {
|
||||
JitterBuffer b = buffer();
|
||||
|
||||
@@ -59,15 +59,38 @@ class VoiceStreamTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void concealsALostPacket() {
|
||||
void rebuildsALostPacketFromTheOneAfterIt() {
|
||||
offer(0, 1);
|
||||
offer(2, 3);
|
||||
assertEquals(0.01f, pull());
|
||||
assertEquals(FakeOpus.CONCEALED, pull());
|
||||
assertEquals(FakeOpus.RECOVERED, pull());
|
||||
assertEquals(0.03f, pull());
|
||||
assertEquals(List.of(), opus.concealedFrameSizes);
|
||||
}
|
||||
|
||||
@Test
|
||||
void concealsALostPacketWhenTheNextIsMissingToo() {
|
||||
offer(0, 1);
|
||||
offer(3, 4);
|
||||
assertEquals(0.01f, pull());
|
||||
assertEquals(FakeOpus.CONCEALED, pull());
|
||||
assertEquals(FakeOpus.RECOVERED, pull());
|
||||
assertEquals(0.04f, pull());
|
||||
assertEquals(List.of(VoiceFormat.FRAME_SIZE), opus.concealedFrameSizes);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anExtraDelayLeadsANewSpeakerInWithSilence() {
|
||||
stream.setExtraDelay(2 * JitterBuffer.TEAMSPEAK_TICKS_PER_FRAME);
|
||||
offer(0, 1);
|
||||
assertEquals(0f, pull());
|
||||
offer(1, 2);
|
||||
assertEquals(0f, pull());
|
||||
assertEquals(0.01f, pull());
|
||||
assertEquals(0.02f, pull());
|
||||
assertEquals(List.of(), opus.concealedFrameSizes);
|
||||
}
|
||||
|
||||
@Test
|
||||
void dropsPacketsThatArriveTooLate() {
|
||||
offer(0, 1);
|
||||
@@ -100,7 +123,7 @@ class VoiceStreamTest {
|
||||
pull(); // 3 never arrives: concealed
|
||||
offer(3, 4); // too late, dropped
|
||||
offer(3, 4); // a copy of that
|
||||
assertEquals(new VoiceReceiveStats(6, 3, 1, 1, 1, 2), stream.stats());
|
||||
assertEquals(new VoiceReceiveStats(6, 3, 1, 0, 1, 1, 2), stream.stats());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user