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
@@ -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};
}
}