mirror of
https://github.com/twinksoftware/Gearslip.git
synced 2026-11-30 23:30:32 +01:00
A car that is slow to ack no longer gets sent more than it has room for, and holds no more than half a second of music, so the picture and the sound stop falling behind
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4e3f8e6657
commit
5be99fbe0e
3 files changed
+102
-10
No files matched your search
@@ -121,6 +121,19 @@ class GearslipRunner(
|
|||||||
private var touchHeight = 0
|
private var touchHeight = 0
|
||||||
private var touchesSeen = 0
|
private var touchesSeen = 0
|
||||||
@Volatile private var acksSeen = 0
|
@Volatile private var acksSeen = 0
|
||||||
|
/**
|
||||||
|
* Frames a window reset gave up on. Their acks still come, late, and free no room: counted
|
||||||
|
* as fresh room, they let the car's queue grow by a window on every reset. A SOOEMAKA that
|
||||||
|
* stalled often ran its picture up to half a minute behind. Acks come in order, so by the
|
||||||
|
* next reset any still owed were lost with the frames: a reset forgets the ones before it.
|
||||||
|
* Acks carry no number, so owed acks that were in fact lost take the acks of the frames
|
||||||
|
* sent after the reset, and with no room freed the window fills and resets again, owing as
|
||||||
|
* many again: four frames every ten seconds for good. So a reset with no room freed since
|
||||||
|
* the one before owes nothing.
|
||||||
|
*/
|
||||||
|
private var unackedAtReset = 0
|
||||||
|
/** Whether an ack has freed room since the last window reset: see [unackedAtReset]. */
|
||||||
|
private var freedSinceReset = true
|
||||||
@Volatile private var lastAckAt = 0L
|
@Volatile private var lastAckAt = 0L
|
||||||
@Volatile private var splitMessages = 0
|
@Volatile private var splitMessages = 0
|
||||||
@Volatile private var resyncRequested = false
|
@Volatile private var resyncRequested = false
|
||||||
@@ -1110,7 +1123,11 @@ class GearslipRunner(
|
|||||||
val fields = Wire.fields(body)
|
val fields = Wire.fields(body)
|
||||||
val acked = Wire.varint(fields, 2)?.toInt() ?: 1
|
val acked = Wire.varint(fields, 2)?.toInt() ?: 1
|
||||||
synchronized(ackLock) {
|
synchronized(ackLock) {
|
||||||
inFlight = maxOf(0, inFlight - maxOf(1, acked))
|
val count = maxOf(1, acked)
|
||||||
|
val late = minOf(unackedAtReset, count)
|
||||||
|
unackedAtReset -= late
|
||||||
|
inFlight = maxOf(0, inFlight - (count - late))
|
||||||
|
if (count > late) freedSinceReset = true
|
||||||
acksSeen++
|
acksSeen++
|
||||||
lastAckAt = System.currentTimeMillis()
|
lastAckAt = System.currentTimeMillis()
|
||||||
ackLock.notifyAll()
|
ackLock.notifyAll()
|
||||||
@@ -1192,6 +1209,8 @@ class GearslipRunner(
|
|||||||
|
|
||||||
private fun startVideoSource() {
|
private fun startVideoSource() {
|
||||||
lastKeyframeSentAt = System.currentTimeMillis() // frame #1 is always a keyframe
|
lastKeyframeSentAt = System.currentTimeMillis() // frame #1 is always a keyframe
|
||||||
|
// From the start of the stream, not 1970: the first full window reset at once.
|
||||||
|
synchronized(ackLock) { lastAckAt = System.currentTimeMillis() }
|
||||||
val index = selectedConfigIndex ?: 0
|
val index = selectedConfigIndex ?: 0
|
||||||
val config = videoConfigs.getOrNull(index)
|
val config = videoConfigs.getOrNull(index)
|
||||||
if (config == null) {
|
if (config == null) {
|
||||||
@@ -1291,15 +1310,20 @@ class GearslipRunner(
|
|||||||
val waitStart = System.currentTimeMillis()
|
val waitStart = System.currentTimeMillis()
|
||||||
while (running && inFlight >= maxUnacked) {
|
while (running && inFlight >= maxUnacked) {
|
||||||
// A lost ack would otherwise hold the stream forever, which looks like a frozen
|
// A lost ack would otherwise hold the stream forever, which looks like a frozen
|
||||||
// picture. After a long silence, assume the window cleared and carry on.
|
// picture. After a long silence, assume the window cleared and carry on. A car
|
||||||
|
// that has acked is slow rather than losing them, so it gets a longer wait: each
|
||||||
|
// early reset sent a stalled SOOEMAKA frames it had no room for.
|
||||||
val since = System.currentTimeMillis() - lastAckAt
|
val since = System.currentTimeMillis() - lastAckAt
|
||||||
if (since > ACK_STALL_MS) {
|
val stallMs = if (acksSeen > 0) ACK_LOST_MS else ACK_STALL_MS
|
||||||
|
if (since > stallMs) {
|
||||||
log.w("no ack for ${since}ms with $inFlight in flight - resetting the window")
|
log.w("no ack for ${since}ms with $inFlight in flight - resetting the window")
|
||||||
|
unackedAtReset = if (freedSinceReset) inFlight else 0
|
||||||
|
freedSinceReset = false
|
||||||
inFlight = 0
|
inFlight = 0
|
||||||
lastAckAt = System.currentTimeMillis() // one warning per stall, not one per frame
|
lastAckAt = System.currentTimeMillis() // one warning per stall, not one per frame
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
ackLock.wait(ACK_STALL_MS - since + 1)
|
ackLock.wait(stallMs - since + 1)
|
||||||
}
|
}
|
||||||
ackWaitMs += System.currentTimeMillis() - waitStart
|
ackWaitMs += System.currentTimeMillis() - waitStart
|
||||||
}
|
}
|
||||||
@@ -1360,6 +1384,8 @@ class GearslipRunner(
|
|||||||
// Frames sent just before the car took over may never be acked.
|
// Frames sent just before the car took over may never be acked.
|
||||||
synchronized(ackLock) {
|
synchronized(ackLock) {
|
||||||
inFlight = 0
|
inFlight = 0
|
||||||
|
unackedAtReset = 0
|
||||||
|
freedSinceReset = true
|
||||||
ackLock.notifyAll()
|
ackLock.notifyAll()
|
||||||
}
|
}
|
||||||
val start = Protobuf.varintField(1, SESSION_ID.toLong()) +
|
val start = Protobuf.varintField(1, SESSION_ID.toLong()) +
|
||||||
@@ -1752,6 +1778,7 @@ class GearslipRunner(
|
|||||||
const val ACTION_UP = 1
|
const val ACTION_UP = 1
|
||||||
|
|
||||||
const val ACK_STALL_MS = 2_000L
|
const val ACK_STALL_MS = 2_000L
|
||||||
|
const val ACK_LOST_MS = 10_000L
|
||||||
const val WITHHELD_SCREEN_MS = 5_000L
|
const val WITHHELD_SCREEN_MS = 5_000L
|
||||||
const val AUDIO_TRACE_MESSAGES = 6
|
const val AUDIO_TRACE_MESSAGES = 6
|
||||||
const val GUIDANCE_SESSION_ID = 1
|
const val GUIDANCE_SESSION_ID = 1
|
||||||
|
|||||||
@@ -68,6 +68,19 @@ class AudioLink(
|
|||||||
private var slowAckMs = 0L
|
private var slowAckMs = 0L
|
||||||
private var lastAckFull = false
|
private var lastAckFull = false
|
||||||
private var stalls = 0
|
private var stalls = 0
|
||||||
|
/** Acks this stream has had: a car that sends any is slow when they stop, not deaf. */
|
||||||
|
private var acksSeen = 0
|
||||||
|
/**
|
||||||
|
* Chunks a reopened window gave up on. Their acks still come, late, and free no room:
|
||||||
|
* counted as fresh room, each reopen could leave a car that plays what it holds a whole
|
||||||
|
* window, two seconds on a SOOEMAKA, further behind for the rest of the drive. Acks come in
|
||||||
|
* order, so by the next reopen any still owed were lost: a reopen forgets the ones before it.
|
||||||
|
* Owed acks that were in fact lost take the acks of the chunks sent after, so a reopen with
|
||||||
|
* no room freed since the one before owes nothing, or the stream would stall every time.
|
||||||
|
*/
|
||||||
|
private var unackedAtReset = 0
|
||||||
|
/** Whether an ack has freed room since the last reopen: see [unackedAtReset]. */
|
||||||
|
private var freedSinceReset = true
|
||||||
private var chunks = 0
|
private var chunks = 0
|
||||||
private var bytes = 0L
|
private var bytes = 0L
|
||||||
private var dropped = 0
|
private var dropped = 0
|
||||||
@@ -230,13 +243,18 @@ class AudioLink(
|
|||||||
synchronized(ackLock) {
|
synchronized(ackLock) {
|
||||||
// A full window means the sender is waiting on this ack. When it was waiting on
|
// A full window means the sender is waiting on this ack. When it was waiting on
|
||||||
// the one before too, the gap between them is the car's own pace.
|
// the one before too, the gap between them is the car's own pace.
|
||||||
val full = inFlight >= maxUnacked
|
val full = inFlight >= window()
|
||||||
if (full && lastAckFull) {
|
if (full && lastAckFull) {
|
||||||
slowAcks++
|
slowAcks++
|
||||||
slowAckMs += now - lastAckAt
|
slowAckMs += now - lastAckAt
|
||||||
}
|
}
|
||||||
lastAckFull = full
|
lastAckFull = full
|
||||||
inFlight = maxOf(0, inFlight - maxOf(1, acked))
|
val count = maxOf(1, acked)
|
||||||
|
val late = minOf(unackedAtReset, count)
|
||||||
|
unackedAtReset -= late
|
||||||
|
inFlight = maxOf(0, inFlight - (count - late))
|
||||||
|
if (count > late) freedSinceReset = true
|
||||||
|
acksSeen++
|
||||||
lastAckAt = now
|
lastAckAt = now
|
||||||
ackLock.notifyAll()
|
ackLock.notifyAll()
|
||||||
}
|
}
|
||||||
@@ -349,6 +367,23 @@ class AudioLink(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Chunks the car may hold at once: its own window, and for a car that acks, no more than
|
||||||
|
* [MAX_HELD_MS] of audio. A car that acks as it plays holds all that is in flight, so a
|
||||||
|
* stall that filled the window left a SOOEMAKA's music seconds behind for the rest of the
|
||||||
|
* drive. Past this the sender waits, and a long wait overflows the capture buffer behind
|
||||||
|
* it: a short skip, once, instead of lag from then on. Call with [ackLock] held.
|
||||||
|
*/
|
||||||
|
private fun window(): Int {
|
||||||
|
// AAC chunks are compressed, so their bytes say nothing of how long they play.
|
||||||
|
if (acksSeen == 0 || aac != null) return maxUnacked
|
||||||
|
// What is in flight, not what is planned: bigger chunks wait for their turn.
|
||||||
|
val chunk = if (lastPayloadBytes > 0) lastPayloadBytes else chunkBytes
|
||||||
|
if (chunk <= 0) return maxUnacked
|
||||||
|
val chunkMs = chunk * 1000.0 / (sampleRate * channels * 2)
|
||||||
|
return (MAX_HELD_MS / chunkMs).toInt().coerceIn(1, maxUnacked)
|
||||||
|
}
|
||||||
|
|
||||||
/** Silence for [ms], whole frames of this sink's format. */
|
/** Silence for [ms], whole frames of this sink's format. */
|
||||||
private fun silence(ms: Int): ByteArray = ByteArray(sampleRate * ms / 1000 * channels * 2)
|
private fun silence(ms: Int): ByteArray = ByteArray(sampleRate * ms / 1000 * channels * 2)
|
||||||
|
|
||||||
@@ -392,6 +427,9 @@ class AudioLink(
|
|||||||
lastAckAt = System.currentTimeMillis()
|
lastAckAt = System.currentTimeMillis()
|
||||||
pendingStale = true
|
pendingStale = true
|
||||||
synchronized(ackLock) {
|
synchronized(ackLock) {
|
||||||
|
acksSeen = 0
|
||||||
|
unackedAtReset = 0
|
||||||
|
freedSinceReset = true
|
||||||
slowAcks = 0
|
slowAcks = 0
|
||||||
slowAckMs = 0
|
slowAckMs = 0
|
||||||
lastAckFull = false
|
lastAckFull = false
|
||||||
@@ -478,19 +516,23 @@ class AudioLink(
|
|||||||
// Wait for the car to catch up rather than throw the chunk away: a dropped chunk is a gap
|
// Wait for the car to catch up rather than throw the chunk away: a dropped chunk is a gap
|
||||||
// the driver hears, while a short wait is soaked up by the capture buffer behind us.
|
// the driver hears, while a short wait is soaked up by the capture buffer behind us.
|
||||||
synchronized(ackLock) {
|
synchronized(ackLock) {
|
||||||
while (started && inFlight >= maxUnacked) {
|
while (started && inFlight >= window()) {
|
||||||
val waited = System.currentTimeMillis() - lastAckAt
|
val waited = System.currentTimeMillis() - lastAckAt
|
||||||
if (waited > STALL_MS) {
|
// A car that acks gets longer before its acks count as lost; see unackedAtReset.
|
||||||
|
val stallMs = if (acksSeen > 0) LOST_ACK_MS else STALL_MS
|
||||||
|
if (waited > stallMs) {
|
||||||
// A lost ack must not silence the stream for good; after a stall assume the window cleared.
|
// A lost ack must not silence the stream for good; after a stall assume the window cleared.
|
||||||
// A car that never acks (the 2024 VW, the Bolt) hits this every chunk: say so
|
// A car that never acks (the 2024 VW, the Bolt) hits this every chunk: say so
|
||||||
// once, then now and then, so the rest of the log stays readable.
|
// once, then now and then, so the rest of the log stays readable.
|
||||||
if (stalls++ % 500 == 0) {
|
if (stalls++ % 500 == 0) {
|
||||||
GearslipLog.w("$tag: no ack for ${STALL_MS}ms with $inFlight in flight - reopening the window ($stalls times)")
|
GearslipLog.w("$tag: no ack for ${stallMs}ms with $inFlight in flight - reopening the window ($stalls times)")
|
||||||
}
|
}
|
||||||
|
unackedAtReset = if (freedSinceReset) inFlight else 0
|
||||||
|
freedSinceReset = false
|
||||||
inFlight = 0
|
inFlight = 0
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
ackLock.wait(STALL_MS - waited + 1)
|
ackLock.wait(stallMs - waited + 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!started) {
|
if (!started) {
|
||||||
@@ -557,6 +599,8 @@ class AudioLink(
|
|||||||
private const val FOCUS_STATE_LOSS_TRANSIENT = 5
|
private const val FOCUS_STATE_LOSS_TRANSIENT = 5
|
||||||
private const val SESSION_ID = 2
|
private const val SESSION_ID = 2
|
||||||
private const val STALL_MS = 2_000L
|
private const val STALL_MS = 2_000L
|
||||||
|
private const val LOST_ACK_MS = 10_000L
|
||||||
|
private const val MAX_HELD_MS = 500
|
||||||
/** Acks measured with the window full before the chunk size is judged. */
|
/** Acks measured with the window full before the chunk size is judged. */
|
||||||
private const val ADAPT_ACKS = 20
|
private const val ADAPT_ACKS = 20
|
||||||
/** Acks this much further apart than a chunk lasts mean the car is the one holding back. */
|
/** Acks this much further apart than a chunk lasts mean the car is the one holding back. */
|
||||||
|
|||||||
@@ -70,6 +70,27 @@ class AudioLinkChunkTest {
|
|||||||
assertEquals(16_384, sent.last())
|
assertEquals(16_384, sent.last())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun aCarThatAcksHoldsNoMoreThanHalfASecond() {
|
||||||
|
// A SOOEMAKA offers 96 chunks, two seconds of music, and plays all it holds in order: once
|
||||||
|
// a stall filled them, every song played that far behind for the rest of the drive.
|
||||||
|
val (link, sent) = startedLink(window = 96)
|
||||||
|
val read = ByteArray(CAPTURE_BYTES)
|
||||||
|
link.write(read, read.size)
|
||||||
|
ack(link)
|
||||||
|
val capture = Thread {
|
||||||
|
while (!Thread.currentThread().isInterrupted) link.write(read, read.size)
|
||||||
|
}.apply { isDaemon = true; start() }
|
||||||
|
try {
|
||||||
|
Thread.sleep(300)
|
||||||
|
} finally {
|
||||||
|
capture.interrupt()
|
||||||
|
link.stop()
|
||||||
|
}
|
||||||
|
// Half a second of 21 ms chunks is 23, after the one the car acked.
|
||||||
|
assertEquals(1 + 23, sent.size)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun aCarThatKeepsUpGetsTheCaptureAsItComes() {
|
fun aCarThatKeepsUpGetsTheCaptureAsItComes() {
|
||||||
val (link, sent) = startedLink(window = 16)
|
val (link, sent) = startedLink(window = 16)
|
||||||
|
|||||||
Reference in new issue
Block a user