@@ -74,6 +74,10 @@ const serveSimApiSchema = Schema.Struct({
7474const decodeServeSimApi = Schema . decodeUnknownOption ( serveSimApiSchema ) ;
7575const decodeWebRtcSettings = Schema . decodeUnknownOption ( webRtcSettingsSchema ) ;
7676type 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 } ` ;
7781const 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
251255export 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
263269export 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