VEHwang's picture
Upload CoCogni4D-Bench (questions, media, scripts, render records, code) (part 6)
5afd2f7 verified
Raw History Blame Contribute Delete
2.01 kB
import type {MediaRoute} from './contract.ts';
type Presentation = {pts: number; url: string; source: string};
const presentations = new WeakMap<HTMLVideoElement, Presentation>();
// Paused seeking and native playback share actual callback metadata, while
// only the active mode owns an application rVFC loop for each video.
export function recordPresentation(video: HTMLVideoElement, url: string, pts: number) {
if (!Number.isFinite(pts) || video.currentSrc !== new URL(url, video.baseURI).href) return;
presentations.set(video, {pts, url, source: video.currentSrc});
}
export function lastPresentation(video: HTMLVideoElement, url: string) {
const value=presentations.get(video);
return value?.url===url && value.source===video.currentSrc ? value : null;
}
// Both axes are the public adapter's timestamp columns. Interpolation changes
// display time only; frame identities and recording windows are never inferred.
export function mediaTime(route: MediaRoute, value: number, from: 'world_s' | 'local_s'): number {
if (!Number.isFinite(value) || route.frames.length < 2) throw Error('录像时钟表无效');
const to = from === 'world_s' ? 'local_s' : 'world_s', rows = route.frames;
let left = 0, right = rows.length - 1;
while (right - left > 1) {
const middle = (left + right) >>> 1;
if (rows[middle][from] <= value) left = middle; else right = middle;
}
const a = rows[left], b = rows[right];
return a[to] + (value - a[from]) / (b[from] - a[from]) * (b[to] - a[to]);
}
export function presentedFrame(route: MediaRoute, local: number) {
const rows=route.frames;
let left=0, right=rows.length-1;
while (right-left>1) {
const middle=(left+right)>>>1;
if (rows[middle].local_s<=local) left=middle; else right=middle;
}
const frame=Math.abs(rows[left].local_s-local)<=Math.abs(rows[right].local_s-local)?rows[left]:rows[right];
if (Math.abs(frame.local_s-local)>=.5/route.fps) throw Error('呈现时刻与录像时钟表不符');
return frame;
}