Files
NODEDC_MISSION_CORE/packages/sensor-ui/src/previewLiveness.ts
T
DCCONSTRUCTIONS fa8bde584f feat(video): recover camera previews and reduce encoding and playout cost
Improve camera recovery, shared preview lifecycle and receiver playout diagnostics. Preserve qualified X4 runtime changes and restore async admission in loopback media tests.
2026-10-29 09:10:01 +03:00

39 lines
1.9 KiB
TypeScript

/** Media progress and capture freshness are independent of DOM painting and inventory SSE. */
export class PreviewLiveness {
private lastFrame:number|null=null;
private sourceSeen=Number.NEGATIVE_INFINITY;
private staleSince:number|null=null;
private connectedAt:number|null=null;
private budgetPauseUntil=0;
private budgetResumeAt:number|null=null;
constructor(private negotiatedAt:number){}
negotiated(now:number){this.negotiatedAt=now;}
connected(now:number){this.connectedAt??=now;}
frame(now:number){this.connected(now);this.lastFrame=now;}
allocation(paused:boolean,now:number){
if(paused)this.budgetPauseUntil=now+3000;
else if(this.budgetPauseUntil){this.budgetPauseUntil=0;this.budgetResumeAt=now;}
}
source(fresh:boolean,now:number){
this.sourceSeen=now;
this.staleSince=fresh?null:this.staleSince??now;
}
state(now:number):'connecting'|'live'|'waiting'|'delayed'|'source-stalled'|'peer-stalled'{
// SDP is only signaling. ICE/DTLS can still be establishing the private route.
if(this.connectedAt===null)return now-this.negotiatedAt>30000?'peer-stalled':'connecting';
if(this.staleSince!==null&&now-this.sourceSeen<3000){
return now-this.staleSince>=12000?'source-stalled':'delayed';
}
// A fresh allocation pause is not a dead peer and never claims live imagery.
// Missing hints still expire normally; actual source-stall evidence wins above.
if(now<this.budgetPauseUntil)return 'waiting';
if(this.budgetResumeAt!==null&&(this.lastFrame===null||this.lastFrame<this.budgetResumeAt)&&now-this.budgetResumeAt<8000)return 'waiting';
const elapsed=now-(this.lastFrame??this.connectedAt);
// A short route interruption must not tear down an established decoder.
// The image is still marked delayed after 2 s; this only bounds reconnection.
if(elapsed>(this.lastFrame===null?8000:30000))return 'peer-stalled';
if(this.lastFrame===null)return 'waiting';
return elapsed>2000?'delayed':'live';
}
}