File size: 2,006 Bytes
5afd2f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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;
}