문서: IFrame 재생을 전원 준비 보장 방식으로 보강(자동 진행 없음·소리 허용·일시정지 중 시간 정지·위치 기준 클립), 실제 브라우저 시험 결과·도구 추가, 디스크 정리에서 ~/.tmp 제외
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
70
tools/prototypes/iframe-sync/player.html
Normal file
70
tools/prototypes/iframe-sync/player.html
Normal file
@@ -0,0 +1,70 @@
|
||||
<!doctype html>
|
||||
<html><head><meta charset="utf-8"><title>iframe sync probe</title></head>
|
||||
<body>
|
||||
<button id="join">참가 (소리 켜기)</button>
|
||||
<div style="position:absolute;left:-10000px;top:0;width:200px;height:200px"><div id="yt"></div></div>
|
||||
<script>
|
||||
// Probe page for the hidden-IFrame playback design (docs/12-decisions.md Q1).
|
||||
// Exposes window.probe for the playwright runner (run.ts).
|
||||
const log = [];
|
||||
const ev = (k, v) => log.push({ t: Math.round(performance.now()), k, v });
|
||||
let player, readyResolve;
|
||||
const apiReady = new Promise(r => (readyResolve = r));
|
||||
window.onYouTubeIframeAPIReady = () => {
|
||||
player = new YT.Player('yt', {
|
||||
width: 200, height: 200,
|
||||
playerVars: { autoplay: 0, controls: 0, disablekb: 1, fs: 0, rel: 0, playsinline: 1 },
|
||||
events: {
|
||||
onReady: () => { ev('ready'); readyResolve(); },
|
||||
onStateChange: e => ev('state', e.data),
|
||||
onError: e => ev('error', e.data),
|
||||
onAutoplayBlocked: () => ev('autoplayBlocked'),
|
||||
},
|
||||
});
|
||||
};
|
||||
const s = document.createElement('script'); s.src = 'https://www.youtube.com/iframe_api'; document.head.appendChild(s);
|
||||
const waitState = (want, ms) => new Promise(res => {
|
||||
const t0 = performance.now();
|
||||
const iv = setInterval(() => {
|
||||
if (player.getPlayerState() === want) { clearInterval(iv); res(true); }
|
||||
else if (performance.now() - t0 > ms) { clearInterval(iv); res(false); }
|
||||
}, 20);
|
||||
});
|
||||
// gesture unlock: called from the join button click handler
|
||||
document.getElementById('join').onclick = () => { ev('gesture'); window.__gesture = true; };
|
||||
window.probe = {
|
||||
log, apiReady: () => apiReady,
|
||||
// Prepare: muted load at startSec, wait until really playing, then pause -> ready
|
||||
async prepare(id, startSec) {
|
||||
const t0 = performance.now();
|
||||
player.mute();
|
||||
player.loadVideoById({ videoId: id, startSeconds: startSec });
|
||||
const ok = await waitState(YT.PlayerState.PLAYING, 20000);
|
||||
player.pauseVideo();
|
||||
await waitState(YT.PlayerState.PAUSED, 3000);
|
||||
player.seekTo(startSec, true);
|
||||
return { ok, ms: Math.round(performance.now() - t0), dur: player.getDuration(), at: player.getCurrentTime() };
|
||||
},
|
||||
// Play unmuted at wall-clock epoch ms `at`, stop after clipMs (0 = keep playing)
|
||||
play(at, clipMs) {
|
||||
return new Promise(res => {
|
||||
setTimeout(async () => {
|
||||
const startWall = Date.now();
|
||||
player.unMute(); player.playVideo();
|
||||
const playing = await waitState(YT.PlayerState.PLAYING, 5000);
|
||||
const firstPlayingWall = Date.now();
|
||||
const muted = player.isMuted();
|
||||
if (!clipMs) return res({ playing, muted, lateMs: firstPlayingWall - at });
|
||||
setTimeout(() => {
|
||||
const pos = player.getCurrentTime();
|
||||
player.pauseVideo();
|
||||
res({ playing, muted, lateMs: firstPlayingWall - at, startWall, stopPos: pos });
|
||||
}, at + clipMs - Date.now());
|
||||
}, Math.max(0, at - Date.now()));
|
||||
});
|
||||
},
|
||||
pos: () => player.getCurrentTime(),
|
||||
state: () => player.getPlayerState(),
|
||||
muted: () => player.isMuted(),
|
||||
};
|
||||
</script></body></html>
|
||||
59
tools/prototypes/iframe-sync/run.ts
Normal file
59
tools/prototypes/iframe-sync/run.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
// Real-browser probe of the hidden YouTube IFrame playback design (docs/12-decisions.md Q1).
|
||||
// Usage: bun tools/prototypes/iframe-sync/run.ts (needs playwright-core + /usr/bin/google-chrome)
|
||||
// Checks: autoplay policy with/without a user gesture, prepare (muted preload) time,
|
||||
// synchronized unmuted start across 3 clients, 1-second clip accuracy, drift over 10s.
|
||||
import { chromium } from "playwright-core";
|
||||
|
||||
const html = await Bun.file(new URL("./player.html", import.meta.url)).text();
|
||||
const server = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: () => new Response(html, { headers: { "content-type": "text/html" } }) });
|
||||
const url = `http://localhost:${server.port}/`;
|
||||
const VIDEO = process.env.VIDEO ?? "VO88htcm09E"; // SG워너비 광(狂) Topic
|
||||
const START = Number(process.env.START ?? 60);
|
||||
|
||||
// Real-browser autoplay policy: sound needs a user activation.
|
||||
const browser = await chromium.launch({ executablePath: "/usr/bin/google-chrome", headless: true,
|
||||
args: ["--autoplay-policy=document-user-activation-required"] });
|
||||
|
||||
async function client(gesture: boolean) {
|
||||
const ctx = await browser.newContext();
|
||||
const page = await ctx.newPage();
|
||||
await page.goto(url);
|
||||
await page.evaluate(() => (window as any).probe.apiReady());
|
||||
if (gesture) await page.click("#join");
|
||||
return page;
|
||||
}
|
||||
const p = (page: any, fn: string, ...a: any[]) => page.evaluate(([fn, a]: any) => (window as any).probe[fn](...a), [fn, a]);
|
||||
|
||||
const out: Record<string, unknown> = {};
|
||||
|
||||
// 1) no gesture: can a muted preload run, and does unmuted play get blocked?
|
||||
{
|
||||
const page = await client(false);
|
||||
out.noGesture_prepare = await p(page, "prepare", VIDEO, START);
|
||||
out.noGesture_play = await p(page, "play", Date.now() + 300, 1500);
|
||||
out.noGesture_events = (await page.evaluate(() => (window as any).probe.log)).filter((e: any) => e.k !== "state");
|
||||
await page.context().close();
|
||||
}
|
||||
|
||||
// 2) three clients with a gesture: synchronized start, 1s clip, then 10s drift
|
||||
{
|
||||
const pages = await Promise.all([client(true), client(true), client(true)]);
|
||||
out.prepare = await Promise.all(pages.map(pg => p(pg, "prepare", VIDEO, START)));
|
||||
const at = Date.now() + 800;
|
||||
out.clip1s = await Promise.all(pages.map(pg => p(pg, "play", at, 1000)));
|
||||
// drift: re-prepare then play 10s continuously, sample positions at the same wall instants
|
||||
await Promise.all(pages.map(pg => p(pg, "prepare", VIDEO, START)));
|
||||
const at2 = Date.now() + 800;
|
||||
out.longStart = await Promise.all(pages.map(pg => p(pg, "play", at2, 0)));
|
||||
const samples: number[][] = [];
|
||||
for (let i = 0; i < 5; i++) {
|
||||
await Bun.sleep(2000);
|
||||
samples.push(await Promise.all(pages.map(pg => p(pg, "pos"))));
|
||||
}
|
||||
out.driftSamples = samples.map(s => ({ pos: s.map(x => +x.toFixed(3)), spreadMs: Math.round((Math.max(...s) - Math.min(...s)) * 1000) }));
|
||||
out.gestureEvents = (await pages[0].evaluate(() => (window as any).probe.log)).filter((e: any) => e.k !== "state");
|
||||
}
|
||||
|
||||
console.log(JSON.stringify(out, null, 1));
|
||||
await browser.close();
|
||||
server.stop();
|
||||
Reference in New Issue
Block a user