Measure Worker video delivery separately from simulation cadence

This commit is contained in:
DCCONSTRUCTIONS
2026-10-09 09:33:52 +03:00
parent c4622332e6
commit c4d4ea4cf8
4 changed files with 137 additions and 4 deletions
@@ -1,4 +1,5 @@
import { AppStreamer, StreamType, LogLevel, EventStatus } from "@nvidia/ov-web-rtc";
import { StreamDiagnostics } from "../../core/simulation/streamDiagnostics";
import type { AiStreamEndpoint } from "../../core/simulation/aiPolygon";
import "@nodedc/tokens/tokens.css";
import "./streamFrame.css";
@@ -19,17 +20,22 @@ window.addEventListener("message",(e:MessageEvent)=>{
started=true;
const endpoint=e.data.endpoint as AiStreamEndpoint;
fitVideo(endpoint.width,endpoint.height);
const diagnostics=new StreamDiagnostics();
let frames=0,measuredAt=performance.now(),decoded=0;
const frame=()=>{frames++;video.requestVideoFrameCallback(frame);};
const frame:VideoFrameRequestCallback=(now,metadata)=>{
frames++;diagnostics.observeFrame(now,metadata);video.requestVideoFrameCallback(frame);
};
video.requestVideoFrameCallback(frame);
setInterval(()=>{
const timer=setInterval(()=>{
const now=performance.now();
// Presentation callbacks can be throttled independently of decoding in a
// background view. Prefer decoded, non-dropped video frames for telemetry.
const quality=video.getVideoPlaybackQuality?.();
const total=quality?quality.totalVideoFrames-quality.droppedVideoFrames:decoded+frames;
const count=Math.max(0,total-decoded);
if(count>0)report("live",count*1000/(now-measuredAt));
const fps=count*1000/(now-measuredAt);
video.dataset.streamDiagnostics=JSON.stringify(diagnostics.snapshot(now,fps));
if(count>0)report("live",fps);
decoded=total;frames=0;measuredAt=now;
},1000);
const stream=new AppStreamer();
@@ -42,6 +48,7 @@ window.addEventListener("message",(e:MessageEvent)=>{
maxReconnects:0,connectivityTimeout:5000,
onStart:event=>{if(event.status===EventStatus.ERROR||event.status===EventStatus.CANCELED)report("failed");},
onStop:()=>report("failed"),
onStreamStats:event=>diagnostics.observeNative(performance.now(),event.data?.stats),
}}).catch(()=>report("failed"));
window.addEventListener("pagehide",()=>{void stream.terminate(false).catch(()=>undefined);},{once:true});
window.addEventListener("pagehide",()=>{clearInterval(timer);delete video.dataset.streamDiagnostics;void stream.terminate(false).catch(()=>undefined);},{once:true});
});
@@ -0,0 +1,46 @@
/** One expiring diagnostic sample; never a recording or a control input. */
export interface StreamFrameTiming {
expectedDisplayTime: number;
captureTime?: number;
receiveTime?: number;
processingDuration?: number;
}
const finite = (value: unknown): number | null =>
typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : null;
const elapsed = (later: number, earlier: number | undefined) => {
if (earlier === undefined || finite(earlier) === null) return null;
const value = later - earlier;
return value >= 0 && value <= 60_000 ? value : null;
};
export class StreamDiagnostics {
private native: { at: number; values: Record<string, number | string | null> } | null = null;
private frame: { at: number; values: Record<string, number | null> } | null = null;
observeNative(now: number, input: unknown) {
if (!input || typeof input !== "object") return;
const value = input as Record<string, unknown>;
const fields = {fps: "fps", round_trip_ms: "rtd", decode_ms: "avgDecodeTime",
bitrate_mbps: "currentBitrate", available_mbps: "totalBandwidth", bandwidth_percent: "utilizedBandwidth",
frame_loss_total: "frameLoss", packet_loss_total: "packetLoss",
width: "streamingResolutionWidth", height: "streamingResolutionHeight"};
this.native = { at: now, values: Object.fromEntries(Object.entries(fields)
.map(([key, source]) => [key, finite(value[source])])) };
this.native.values.codec = typeof value.codec === "string" && /^[a-z0-9._-]{1,32}$/i.test(value.codec) ? value.codec : null;
}
observeFrame(now: number, value: StreamFrameTiming) {
// WebRTC captureTime is the browser's RTCP-based remote clock estimate.
// Never subtract a Worker monotonic clock from performance.now().
this.frame = { at: now, values: {
estimated_capture_to_present_ms: elapsed(value.expectedDisplayTime, value.captureTime),
receive_to_present_ms: elapsed(value.expectedDisplayTime, value.receiveTime),
frame_decode_ms: finite(value.processingDuration) === null ? null : value.processingDuration! * 1000,
}};
}
snapshot(now: number, decodedFps: number) {
const fresh = <T extends {at:number}>(sample:T|null) =>
sample && now >= sample.at && now-sample.at <= 3000 ? sample : null;
const native=fresh(this.native), frame=fresh(this.frame);
return {schema_version:"missioncore.stream-diagnostics/v1", sampled_at_ms:now,
decoded_fps:finite(decodedFps), native:native ? {...native.values, age_ms:now-native.at} : null,
presentation:frame ? {...frame.values, age_ms:now-frame.at} : null};
}
}