Skip to content

Commit d06ba27

Browse files
fix(device): Simlock viewer stops retrying an ended lease and keeps its reason (#35994)
1 parent ee64555 commit d06ba27

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

‎packages/client-runtime/src/device/stream.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,21 @@ describe("shared device stream readiness and recovery", () => {
471471
client.stop();
472472
});
473473

474+
it("stops retrying a Simlock lease that ended and keeps its reason", async () => {
475+
const { client, events, fetch, sockets, videoBodies } = recoveryFixture("ios", false, true);
476+
client.start();
477+
await vi.advanceTimersByTimeAsync(0);
478+
const reason = [...new TextEncoder().encode('{"reason":"expired"}')];
479+
videoBodies[0]!.enqueue(new Uint8Array([...envelope(4, [1]), ...envelope(7, reason)]));
480+
videoBodies[0]!.close();
481+
sockets[0]!.onclose?.({ code: 4001, reason: "expired" });
482+
await vi.advanceTimersByTimeAsync(5_000);
483+
expect(fetch.mock.calls.filter(([url]) => url.includes("stream.avcc"))).toHaveLength(1);
484+
expect(sockets).toHaveLength(1);
485+
expect(events.onStatus).toHaveBeenLastCalledWith("error", expect.stringContaining("expired"));
486+
client.stop();
487+
});
488+
474489
it("does not repaint a delayed JPEG seed over newer decoded video", async () => {
475490
const { client, videoBodies, drawImage, decodedFrame } = recoveryFixture("ios", false);
476491
let resolveBitmap!: (bitmap: ImageBitmap) => void;

‎packages/client-runtime/src/device/stream.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ const serveSimApiSchema = Schema.Struct({
7474
const decodeServeSimApi = Schema.decodeUnknownOption(serveSimApiSchema);
7575
const decodeWebRtcSettings = Schema.decodeUnknownOption(webRtcSettingsSchema);
7676
type WebRtcSettings = typeof webRtcSettingsSchema.Type;
77+
const decodeLeaseEnd = Schema.decodeUnknownOption(
78+
Schema.fromJsonString(Schema.Struct({ reason: Schema.String })),
79+
);
80+
const leaseEnded = (reason: string) => `Device lease ended: ${reason}`;
7781
const decodeTransportLocked = Schema.decodeUnknownOption(
7882
Schema.Struct({
7983
error: Schema.Literal("stream_transport_locked"),
@@ -249,7 +253,7 @@ export function scanAccessUnit(buf: Uint8Array): { isKey: boolean; sps: Uint8Arr
249253
}
250254

251255
export type AvccChunk = {
252-
readonly type: "description" | "keyframe" | "delta" | "seed";
256+
readonly type: "description" | "keyframe" | "delta" | "seed" | "end";
253257
readonly payload: Uint8Array;
254258
};
255259

@@ -258,6 +262,8 @@ const AVCC_TAGS: Record<number, AvccChunk["type"] | undefined> = {
258262
2: "keyframe",
259263
3: "delta",
260264
4: "seed",
265+
// Simlock's final frame on a lease that ended, carrying `{ reason }`.
266+
7: "end",
261267
};
262268

263269
export class AvccDemuxer {
@@ -716,6 +722,7 @@ export function createDeviceStreamClient(
716722
videoGeneration === feedGeneration &&
717723
controller === videoController;
718724
let retryDetail: string | undefined;
725+
let leaseEnd: string | undefined;
719726
try {
720727
const response = await fetch(httpUrl(videoPath), {
721728
signal: videoController.signal,
@@ -807,6 +814,11 @@ export function createDeviceStreamClient(
807814
case "delta":
808815
decode(chunk.type === "keyframe", chunk.payload);
809816
break;
817+
case "end": {
818+
const end = decodeLeaseEnd(decoder.decode(chunk.payload));
819+
leaseEnd = Option.isSome(end) ? end.value.reason : "ended";
820+
break;
821+
}
810822
}
811823
}
812824
}
@@ -817,6 +829,7 @@ export function createDeviceStreamClient(
817829
: (cause as Error).message;
818830
}
819831
if (isCurrent()) {
832+
if (leaseEnd !== undefined) return fail(leaseEnded(leaseEnd));
820833
controller = null;
821834
videoController.abort();
822835
closeDecoder();
@@ -1164,6 +1177,7 @@ export function createDeviceStreamClient(
11641177
duoControl.clear();
11651178
rotationCursor = null;
11661179
if (stopped) return;
1180+
if (event.code === 4001) return fail(leaseEnded(event.reason || "ended"));
11671181
events.onInputConnected(
11681182
false,
11691183
event.reason || (event.code === 1006 ? "input socket refused" : `closed ${event.code}`),

0 commit comments

Comments
 (0)