11#!/usr/bin/env bun
2- // Plays an edit in WebKit and counts how often the playhead stands still at the cuts.
3- // Flags: --edit tentake|primary, --runs 3, --browser webkit|manual, --port 8099.
2+ // Plays an edit in WebKit and counts how often the playhead stands still at the cuts, how often audio is seeked,
3+ // and how much of the time sounding audio stands still.
4+ // Flags: --edit tentake|primary, --runs 3, --browser webkit|chromium|manual, --port 8099.
45// webkit drives Playwright's WebKit (`playwright-core` and `playwright-core install webkit`); manual only serves
56// /host.html and waits for a real Safari to open it, click Play and post the result.
67import { existsSync } from "node:fs" ;
@@ -32,15 +33,17 @@ const HOST = `<!doctype html><meta charset="utf-8">
3233<button id="go">Play</button>
3334<hyperframes-player id="p" src="/comp.html" width="720" height="1280"></hyperframes-player>
3435<script>
35- var p = document.getElementById("p"), samples = [], t0 = 0, done = false;
36+ var p = document.getElementById("p"), samples = [], t0 = 0, done = false, seeks = 0 ;
3637 function finish() {
3738 if (done) return; done = true;
38- var body = JSON.stringify({ t0: t0, end: performance.now(), samples: samples });
39+ var body = JSON.stringify({ t0: t0, end: performance.now(), samples: samples, seeks: seeks });
3940 fetch("/result", { method: "POST", body: body }).then(function () { window.__done = true; });
4041 }
4142 p.addEventListener("ended", finish);
4243 document.getElementById("go").onclick = function () {
4344 t0 = performance.now(); p.play();
45+ var doc = p.shadowRoot.querySelector("iframe").contentWindow.document;
46+ [].forEach.call(doc.querySelectorAll("audio"), function (a) { a.addEventListener("seeking", function () { seeks++; }); });
4447 (function tick() {
4548 if (done) return;
4649 var w = p.shadowRoot.querySelector("iframe").contentWindow, t = w.__player && w.__player.getTime ? w.__player.getTime() : p.currentTime;
@@ -53,7 +56,28 @@ const HOST = `<!doctype html><meta charset="utf-8">
5356</script>
5457<script src="/player.js"></script>` ;
5558
56- type Result = { t0 : number ; end : number ; samples : [ number , number , string ?] [ ] } ;
59+ type Result = { t0 : number ; end : number ; samples : [ number , number , string ?] [ ] ; seeks ?: number } ;
60+
61+ /** Share of sounding-audio time an element's clock stood still for more than STOP_MS. */
62+ function audioFrozenPct ( s : Result [ "samples" ] ) : number {
63+ let playing = 0 ;
64+ let frozen = 0 ;
65+ const since = new Map < string , { time : string ; at : number ; prev : number } > ( ) ;
66+ for ( const [ at , , els ] of s ) {
67+ for ( const entry of ( els ?? "" ) . split ( " " ) . filter ( Boolean ) ) {
68+ const [ id , rest ] = entry . split ( ":" ) ;
69+ const time = rest ! . replace ( / S ? \d $ / , "" ) ;
70+ const last = since . get ( id ! ) ;
71+ if ( last ) playing += at - last . prev ;
72+ if ( ! last || last . time !== time ) since . set ( id ! , { time, at, prev : at } ) ;
73+ else {
74+ if ( at - last . at > STOP_MS ) frozen += at - Math . max ( last . prev , last . at + STOP_MS ) ;
75+ last . prev = at ;
76+ }
77+ }
78+ }
79+ return playing > 0 ? Math . round ( ( frozen / playing ) * 1000 ) / 10 : 0 ;
80+ }
5781
5882/** Stops are runs where the playhead stood still for more than STOP_MS, each tied to the cut it started at. */
5983export function measure ( r : Result , cuts : number [ ] ) {
@@ -69,6 +93,8 @@ export function measure(r: Result, cuts: number[]) {
6993 const perCut = cuts . map ( ( c ) => stops . filter ( ( x ) => x . at >= c - 0.3 && x . at < c + 1.5 ) ) ;
7094 const played = ( r . end - r . t0 ) / 1000 ;
7195 return {
96+ seeks : r . seeks ?? 0 ,
97+ audioFrozenPct : audioFrozenPct ( s ) ,
7298 stops : stops . length ,
7399 stopsPerCut : stops . length / cuts . length ,
74100 longestMs : Math . round ( Math . max ( 0 , ...stops . map ( ( x ) => x . ms ) ) ) ,
@@ -119,11 +145,14 @@ const server = Bun.serve({
119145const cuts = edit . takes . slice ( 1 ) . map ( ( t ) => t . start ) ;
120146const origin = `http://127.0.0.1:${ server . port } ` ;
121147
122- async function drivePlaywright ( ) {
148+ async function drivePlaywright ( engine : "webkit" | "chromium" ) {
123149 const name = "playwright-core" ; // not a dependency: installed by hand, so kept out of the type check
124- const { webkit } = ( await import ( name ) ) as { webkit : any } ; // oxlint-disable-line no-explicit-any
125- const browser = await webkit . launch ( ) ;
126- console . log ( `WebKit ${ browser . version ( ) } ` ) ;
150+ const pw = ( await import ( name ) ) as Record < string , any > ; // oxlint-disable-line no-explicit-any
151+ const browser = await pw [ engine ] ! . launch ( {
152+ executablePath : process . env . PLAYBENCH_BROWSER_PATH ,
153+ args : engine === "chromium" ? [ "--autoplay-policy=no-user-gesture-required" ] : [ ] ,
154+ } ) ;
155+ console . log ( `${ engine } ${ browser . version ( ) } ` ) ;
127156 for ( let i = 0 ; i < RUNS ; i ++ ) {
128157 const page = await browser . newPage ( ) ;
129158 await page . goto ( `${ origin } /host.html` ) ;
@@ -137,7 +166,8 @@ async function drivePlaywright() {
137166 await browser . close ( ) ;
138167}
139168
140- if ( arg ( "browser" , "webkit" ) === "webkit" ) await drivePlaywright ( ) ;
169+ const engine = arg ( "browser" , "webkit" ) ;
170+ if ( engine === "webkit" || engine === "chromium" ) await drivePlaywright ( engine ) ;
141171else {
142172 console . log ( `open ${ origin } /host.html in Safari, click Play (${ RUNS } runs)` ) ;
143173 while ( results . length < RUNS ) await new Promise < void > ( ( r ) => ( resolveResult = r ) ) ;
0 commit comments