cast: key-frame resync past the 8-bit wrap recovers on the right lap
- Commit
9402fcbb55bb3f27db7367dec8166495070f5e26- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 10:24 UTC (4 days ago)
- Parents
865110f
Resync (stall/starve, or a refused frame) called assembler.reset(), clearing the 8-bit expansion reference, while ReceiverSession.checkpoint correctly kept its floor (e.g. 300). The next key frame 301 exists on the wire only as byte 45; with no reference it expanded literally to frame 45, which is <= floor, so add() dropped it — and every following frame likewise until the counter wrapped past the checkpoint, after which frames were accepted a lap 256 below the sender's and decrypted to garbage permanently (the CTR IV derives from the frame id). Pre-wrap the same reset worked by accident, which is why it survived (~8.5 s at 30 fps before any resync never recovers). FrameAssembler.reset now takes the session checkpoint as its expansion seed (reset(reference)), and requestKeyFrame passes it: the next key frame expands onto the sender's actual lap with the correct IV, retransmissions still drop, and a pre-sync reset no longer emits a spurious frame-0 NACK. Zero wire change; no consumer references FrameAssembler, so compatibility is unaffected by construction. Regression test delivers frames 0-300, resyncs, and asserts frames 301-302 deliver and decrypt byte-exact (released [] pre-fix). Pairing, negotiation and RTCP symmetry were audited and cleared. Verified: :cast:protocol:testDebugUnitTest 179 tests and lint BUILD SUCCESSFUL.
Diff
cast/protocol/src/main/java/com/vayunmathur/cast/protocol/FrameAssembler.kt
diff --git a/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/FrameAssembler.kt b/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/FrameAssembler.ktindex 20854fdc0a..3ac19f41b0 100644--- a/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/FrameAssembler.kt+++ b/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/FrameAssembler.kt@@ -28,7 +28,7 @@ class FrameAssembler(private val maxPending: Int = MAX_PENDING_FRAMES) { * * Starts null, so the very first packet's 8-bit id is taken literally: a sender always begins a * session at [FrameId.First], but a receiver that came up a few frames late would otherwise- * expand 250 to -6.+ * expand 250 to -6. [reset] seeds it rather than clearing it - see there for why. */ private var reference: FrameId? = null @@ -104,10 +104,21 @@ class FrameAssembler(private val maxPending: Int = MAX_PENDING_FRAMES) { return out } - fun reset() {+ /**+ * Forget every partial frame and start assembly again from [reference].+ *+ * The reference is *kept*, seeded by the caller with the highest frame already delivered:+ * past frame 255 the 8-bit wire id only means anything relative to it, so clearing it would+ * expand the next key frame onto the wrong lap - at or below the delivered floor, where+ * [add] drops it, and a lap lower than the sender's once the counter wraps, where the id+ * the frame is delivered under is not the id it was encrypted under and it decrypts to+ * noise. A fresh assembler is the only state with no reference, because a session that+ * has delivered nothing has no lap to preserve.+ */+ fun reset(reference: FrameId) { pending.clear()- reference = null- highest = FrameId.First+ this.reference = reference+ highest = reference } private fun expand(truncated: Int): FrameId {
cast/protocol/src/main/java/com/vayunmathur/cast/protocol/ReceiverSession.kt
diff --git a/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/ReceiverSession.kt b/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/ReceiverSession.ktindex 7b2765406a..272d059e9e 100644--- a/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/ReceiverSession.kt+++ b/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/ReceiverSession.kt@@ -167,7 +167,12 @@ class ReceiverSession( fun requestKeyFrame() { synchronised = false ready.clear()- assembler.reset()+ // The checkpoint is the expansion reference: it is the highest frame delivered, so the+ // next key frame expands onto the lap the sender is actually on. Resetting it to+ // nothing would put that frame at or below the floor [onPacket] enforces and drop the+ // very frame this asks for - for good once the 8-bit counter wraps, because from there+ // every id is a lap behind the one it was encrypted under.+ assembler.reset(checkpoint) stalledRounds = 0 starvedRounds = 0 }
cast/protocol/src/test/java/com/vayunmathur/cast/protocol/MediaRoundTripTest.kt
diff --git a/cast/protocol/src/test/java/com/vayunmathur/cast/protocol/MediaRoundTripTest.kt b/cast/protocol/src/test/java/com/vayunmathur/cast/protocol/MediaRoundTripTest.ktindex da17f85439..64bf45aef4 100644--- a/cast/protocol/src/test/java/com/vayunmathur/cast/protocol/MediaRoundTripTest.kt+++ b/cast/protocol/src/test/java/com/vayunmathur/cast/protocol/MediaRoundTripTest.kt@@ -316,6 +316,35 @@ class MediaRoundTripTest { assertTrue(assertNotNull(afterResume).pictureLoss, "resuming must ask again") } + @Test+ fun `resynchronising after the eight-bit wrap still delivers and decrypts`() {+ // requestKeyFrame() is the recovery path: a stalled or decoder-refused session clears+ // its assembler and waits for the next key frame. Frame ids only exist on the wire+ // truncated to 8 bits, so past frame 255 the expansion reference IS the frame counter:+ // clearing it makes the next key frame expand onto the wrong lap, where the delivered+ // checkpoint floor drops it - and once the counter wraps again it is accepted under an+ // id 256 lower than the sender used, so the IV derived from it decrypts to noise.+ val receiver = ReceiverSession(videoStream())+ val packetizer = packetizer()+ for (id in 0L..300L) {+ val original = frame(id, size = 400, isKeyFrame = id % 30 == 0L)+ for (packet in packetizer.packetize(original)) receiver.onPacket(packet)+ }+ assertEquals(FrameId(300), receiver.checkpoint)++ receiver.requestKeyFrame()++ val key = frame(301, size = 400, isKeyFrame = true)+ val releasedKey = packetizer.packetize(key).flatMap { receiver.onPacket(it) }+ assertEquals(listOf(FrameId(301)), releasedKey.map { it.frameId })+ assertContentEquals(plaintextOf(301, 400), releasedKey.single().payload)++ val delta = frame(302, size = 400, isKeyFrame = false)+ val releasedDelta = packetizer.packetize(delta).flatMap { receiver.onPacket(it) }+ assertEquals(listOf(FrameId(302)), releasedDelta.map { it.frameId })+ assertContentEquals(plaintextOf(302, 400), releasedDelta.single().payload)+ }+ private fun videoStream() = NegotiatedStream( kind = StreamKind.Video, senderSsrc = SENDER_SSRC,