cocogni4d / code /evaluation-code /v0.2-0c2690ae /apps /console /web /tests /native-playback.test.ts
VEHwang's picture
Upload CoCogni4D-Bench (questions, media, scripts, render records, code) (part 6)
5afd2f7 verified
Raw History Blame Contribute Delete
4.81 kB
import test from 'node:test';
import assert from 'node:assert/strict';
import {ReviewClock} from '../src/clock.ts';
import {NativePlayback} from '../src/native-playback.ts';
import type {MediaRoute} from '../src/contract.ts';
class Video extends EventTarget {
paused=true; seeking=false; readyState=4; currentTime=0; playbackRate=1;
style={visibility:''}; baseURI='http://127.0.0.1/'; currentSrc='http://127.0.0.1/movie.mp4';
private next=0;
private callbacks=new Map<number, VideoFrameRequestCallback>();
requestVideoFrameCallback(callback: VideoFrameRequestCallback) {const id=++this.next;this.callbacks.set(id,callback);return id;}
cancelVideoFrameCallback(id: number) {this.callbacks.delete(id);}
play() {this.paused=false;return Promise.resolve();}
pause() {this.paused=true;}
present(pts: number, submitted: number, display=1016+pts*1000) {
const pending=[...this.callbacks.values()];this.callbacks.clear();this.currentTime=pts;
const info={mediaTime:pts,presentedFrames:submitted,presentationTime:display-16,
expectedDisplayTime:display,width:1280,height:720};
for(const callback of pending)callback(display-16,info);
}
pending() {return this.callbacks.size;}
}
function player(count=1) {
const videos=Array.from({length:count},()=>new Video()), status={hidden:false,textContent:''};
const canvas={hidden:false,getContext:()=>({clearRect:()=>{}})};
const nodes=videos.map((video,index)=>({dataset:{mediaObserver:`observer-00${index}`} as Record<string,string>,
querySelector:(selector: string)=>selector==='video'?video:selector==='canvas'?canvas:status}));
const panel={dataset:{} as Record<string,string>,querySelectorAll:()=>nodes};
const sources=videos.map((_,index)=>({observer_id:`observer-00${index}`,url:'/movie.mp4',fps:20,window_world_span:[10,11],
frames:Array.from({length:20},(_,i)=>({frame_index:i,control_tick:200+i,local_s:i/20,world_s:10+i/20}))} as MediaRoute));
const clock=new ReviewClock([10,11],20);clock.setPlaying(true);
const native=new NativePlayback(panel as unknown as HTMLElement,sources,clock,message=>{throw Error(message);});
return {clock,native,video:videos[0],node:nodes[0],videos};
}
test('skipped callbacks locate both clock and overlay from public PTS rows', () => {
const {clock,native,video,node}=player();
try {
video.present(.05,1001);
assert.equal(clock.snapshot().time,10.05);
// A skipped callback and a skipped source frame are different: composition
// count advances by two, while the public PTS advances by three source rows.
video.present(.20,1003);
assert.equal(clock.snapshot().time,10.20);
assert.equal(Number(node.dataset.worldS),10.20);
assert.equal(Number(node.dataset.frameIndex),4);
assert.equal(Number(node.dataset.presentedLocalS),.20);
} finally {native.stop();}
assert.equal(video.pending(),0);
});
test('resubmitting the same PTS never advances the world clock or overlay', () => {
const {clock,native,video,node}=player();
try {
video.present(.20,730);
video.present(.20,731);
assert.equal(clock.snapshot().time,10.20);
assert.equal(Number(node.dataset.worldS),10.20);
assert.equal(Number(node.dataset.frameIndex),4);
} finally {native.stop();}
});
test('an absolute composition counter is independent of source and world origin', () => {
const {clock,native,video,node}=player();
try {
video.present(.45,9000);
assert.equal(clock.snapshot().time,10.45);
assert.equal(Number(node.dataset.frameIndex),9);
assert.equal(Number(node.dataset.presentedLocalS),.45);
} finally {native.stop();}
});
test('overlapping public frame spans do not chase quantized PTS phase', () => {
for(const masterPTS of [.10,.15]) {
const {native,videos}=player(2);
try {
videos[1].present(.10,700,1000);
videos[0].present(masterPTS,9000,1000+1000/30);
assert.equal(videos[1].playbackRate,1);
assert.equal(videos[1].currentTime,.10);
} finally {native.stop();}
}
});
test('separated public spans retain the bounded small rate correction', () => {
const {native,videos}=player(2);
try {
videos[1].present(.10,700,1009);
videos[0].present(.15,9000,1000);
// Projected peer span [10.091,10.141), master [10.15,10.20):
// the 9ms separation uses the existing 8ms trigger and 0.8 gain.
assert.ok(Math.abs(videos[1].playbackRate-1.0072)<1e-10);
assert.equal(videos[1].currentTime,.10);
} finally {native.stop();}
});
test('large presented-time drift still seeks through the public mapping', () => {
const {native,videos}=player(2);
try {
videos[1].present(0,700,1000);
videos[0].present(.20,9000,1000);
assert.equal(videos[1].currentTime,.20);
assert.equal(videos[1].playbackRate,1);
} finally {native.stop();}
});