Modern Apps git

← Modern-Apps.git

maauto: unwrap mod-256 ack counter, scope 0x8004 to channel

Commit
6d1689ed925f1cc05d6a2956cf24f5b802080bdb
Author
vayun-mathur <[email protected]>
When
2026-09-12 18:45 UTC (2 months ago)
Parents
695082f

Diff

auto/protocol/src/main/java/com/vayunmathur/auto/protocol/AckTracker.kt
diff --git a/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/AckTracker.kt b/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/AckTracker.ktnew file mode 100644index 0000000000..16b5054a30--- /dev/null+++ b/auto/protocol/src/main/java/com/vayunmathur/auto/protocol/AckTracker.kt@@ -0,0 +1,47 @@+package com.vayunmathur.auto.protocol++/**+ * Unwraps the 0x8004 `MediaAck` frame counter.+ *+ * The wire `ack` field is a frame counter **mod 256**: it wraps back to 0 every+ * 256 frames, so a raw value below the previous one is a wrap, not a+ * regression. This folds each arrival into a monotonically non-decreasing+ * sequence the phone UI can track; stale arrivals (reordered or duplicated+ * acks) hold the line instead of moving it backwards.+ *+ * Pure state machine, no I/O: the Android side feeds it parsed ack values and+ * publishes the unwrapped sequence.+ */+class AckTracker {+    /** Last unwrapped counter, or null before the first ack carrying the field. */+    var lastUnwrapped: Long? = null+        private set++    /**+     * Folds [raw] (the unsigned-extended `ack` field) into the sequence.+     *+     * @return the new sequence head, or the previous head when [raw] is stale.+     */+    fun onAck(raw: Long): Long {+        val value = raw and (MOD - 1)+        val previous = lastUnwrapped+        val candidate = when {+            previous == null -> value+            value >= previous and (MOD - 1) -> previous - (previous and (MOD - 1)) + value+            else -> previous - (previous and (MOD - 1)) + value + MOD+        }+        if (previous == null || candidate > previous) {+            lastUnwrapped = candidate+        }+        return checkNotNull(lastUnwrapped)+    }++    fun reset() {+        lastUnwrapped = null+    }++    companion object {+        /** Modulus of the 0x8004 `ack` frame counter. */+        const val MOD = 256L+    }+}
auto/protocol/src/test/java/com/vayunmathur/auto/protocol/AckTrackerTest.kt
diff --git a/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/AckTrackerTest.kt b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/AckTrackerTest.ktnew file mode 100644index 0000000000..67c6edd65d--- /dev/null+++ b/auto/protocol/src/test/java/com/vayunmathur/auto/protocol/AckTrackerTest.kt@@ -0,0 +1,55 @@+package com.vayunmathur.auto.protocol++import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull++/**+ * The 0x8004 ack counter wraps mod 256; the tracker must unwrap it into a+ * monotone sequence rather than freezing at 255 or stepping backwards.+ */+class AckTrackerTest {++    @Test+    fun `starts empty`() {+        assertNull(AckTracker().lastUnwrapped)+    }++    @Test+    fun `advances in order`() {+        val tracker = AckTracker()+        assertEquals(3, tracker.onAck(3))+        assertEquals(118, tracker.onAck(118))+        assertEquals(118, tracker.lastUnwrapped)+    }++    @Test+    fun `wraps past 255 instead of regressing`() {+        val tracker = AckTracker()+        assertEquals(254, tracker.onAck(254))+        assertEquals(255, tracker.onAck(255))+        // Wrap: raw 0 continues the sequence at 256, and 1 follows at 257.+        assertEquals(256, tracker.onAck(0))+        assertEquals(257, tracker.onAck(1))+        assertEquals(257, tracker.lastUnwrapped)+    }++    @Test+    fun `stale arrivals hold the line`() {+        val tracker = AckTracker()+        assertEquals(5, tracker.onAck(5))+        // A duplicate and a reordered older value change nothing.+        assertEquals(5, tracker.onAck(5))+        assertEquals(5, tracker.onAck(2))+        assertEquals(5, tracker.lastUnwrapped)+    }++    @Test+    fun `reset clears the sequence`() {+        val tracker = AckTracker()+        tracker.onAck(10)+        tracker.reset()+        assertNull(tracker.lastUnwrapped)+        assertEquals(3, tracker.onAck(3))+    }+}
auto/src/main/java/com/vayunmathur/auto/platform/AutoSessionState.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/platform/AutoSessionState.kt b/auto/src/main/java/com/vayunmathur/auto/platform/AutoSessionState.ktindex d7ec21dc6f..889482a568 100644--- a/auto/src/main/java/com/vayunmathur/auto/platform/AutoSessionState.kt+++ b/auto/src/main/java/com/vayunmathur/auto/platform/AutoSessionState.kt@@ -1,6 +1,7 @@ package com.vayunmathur.auto.platform  import android.os.SystemClock+import com.vayunmathur.auto.protocol.AckTracker import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow@@ -40,12 +41,14 @@ sealed interface VideoEvent {     data class FrameSent(val presentationTimeUs: Long, val latencyUs: Long) : VideoEvent      /**-     * The head unit acknowledged frames (0x8004).+     * The head unit acknowledged frames (0x8004 on the video channel).      *-     * [ackSeq] is the `ack` counter from the `MediaAck`, unsigned-extended; null-     * when the head unit omits the field. [extraCount] is how many `field3`-     * entries rode along. An ack confirms receipt, not visibility โ€” the-     * sender-side proxy for "visible" is acks advancing in step with frames.+     * [ackSeq] is the `ack` frame counter from the `MediaAck`, unsigned-extended;+     * null when the head unit omits the field. The counter is **mod 256** โ€” it+     * wraps, so the session unwraps it rather than treating a lower value as a+     * regression. [extraCount] is how many `field3` entries rode along. An ack+     * confirms receipt, not visibility โ€” the sender-side proxy for "visible" is+     * acks advancing in step with frames.      */     data class AckReceived(val ackSeq: Long?, val extraCount: Int) : VideoEvent @@ -110,7 +113,7 @@ object AutoSessionState {     private val _lastAckAt = MutableStateFlow<Long?>(null)     val lastAckAt: StateFlow<Long?> = _lastAckAt.asStateFlow() -    /** Highest 0x8004 `ack` counter seen; null until an ack carries the field. */+    /** Highest 0x8004 `ack` counter seen, unwrapped across mod-256 wraps; null until an ack carries the field. */     private val _lastAckSeq = MutableStateFlow<Long?>(null)     val lastAckSeq: StateFlow<Long?> = _lastAckSeq.asStateFlow() @@ -134,6 +137,14 @@ object AutoSessionState {     private val frameTimes = ArrayDeque<Long>()     private val ackTimes = ArrayDeque<Long>() +    /**+     * Last unwrapped 0x8004 ack counter. The wire `ack` field is a frame counter+     * mod 256, so it wraps: an [AckTracker] folds arrivals into a monotonically+     * non-decreasing sequence. Single-threaded owner is the `ma-auto-projection`+     * worker thread, like the timestamp windows above.+     */+    private val ackTracker = AckTracker()+     /** Wall-clock millis when the session became active, for the phone's elapsed counter. */     private val _sessionStartedAt = MutableStateFlow<Long?>(null)     val sessionStartedAt: StateFlow<Long?> = _sessionStartedAt.asStateFlow()@@ -178,6 +189,7 @@ object AutoSessionState {     private fun resetTelemetry() {         frameTimes.clear()         ackTimes.clear()+        ackTracker.reset()         _encodedFps.value = 0.0         _ackFps.value = 0.0         _lastAckAgeMs.value = null@@ -242,8 +254,7 @@ object AutoSessionState {         }         _ackFps.value = ackTimes.size * 1_000.0 / FPS_WINDOW_MS         if (ackSeq != null) {-            val previous = _lastAckSeq.value-            if (previous == null || ackSeq > previous) _lastAckSeq.value = ackSeq+            _lastAckSeq.value = ackTracker.onAck(ackSeq)         }     } 
auto/src/main/java/com/vayunmathur/auto/platform/VideoSinkChannel.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/platform/VideoSinkChannel.kt b/auto/src/main/java/com/vayunmathur/auto/platform/VideoSinkChannel.ktindex 756b3011dc..9a24f19cbe 100644--- a/auto/src/main/java/com/vayunmathur/auto/platform/VideoSinkChannel.kt+++ b/auto/src/main/java/com/vayunmathur/auto/platform/VideoSinkChannel.kt@@ -69,7 +69,20 @@ class VideoSinkChannel(         )     } -    fun onMessage(type: Int, payload: ByteArray) {+    /**+     * Handles an inbound message for this channel.+     *+     * [channelId] scopes the parse: 0x8004 is aliased across services (MediaAck+     * on media channels, SensorError on the sensor channel), so a message for a+     * different channel is ignored rather than misparsed. The service routes+     * every service-channel message here today; a future sensor owner will take+     * its own channel's traffic.+     */+    fun onMessage(channelId: Int, type: Int, payload: ByteArray) {+        if (channelId != this.channelId) {+            Log.w(TAG, "ignoring 0x${type.toString(16)} for channel $channelId")+            return+        }         when (type) {             GalMessage.Media.CONFIG -> onSetupResponse(payload)             GalMessage.Video.FOCUS_INDICATION -> onFocus(payload)
auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt
diff --git a/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt b/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.ktindex 10dbab02dc..4e8ac39118 100644--- a/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt+++ b/auto/src/main/java/com/vayunmathur/auto/service/ProjectionService.kt@@ -66,7 +66,9 @@ class ProjectionService : Service() {                 sslContext = GalCredential.fromAssets(assets),                 deviceModel = Build.MODEL,                 deviceManufacturer = Build.MANUFACTURER,-                onChannelMessage = { message -> video?.onMessage(message.type, message.payload) },+                onChannelMessage = { message ->+                    video?.onMessage(message.channelId, message.type, message.payload)+                },                 trace = { Log.d(TAG, it) },             )