fix(m0): get a stopped or reloaded YouTube player going again and measure the window again
On the gaming PC the player errored about 38 s into the first window with Jarvis off; the kit had no way to notice, so every later window measured a paused, empty player, and after the user reloaded the page the injected counters were gone and the snapshot script threw, ending the stage early. - The snapshot reports a lost page (counters gone, or the script could not run while the page loaded) and the player's own error screen (first line only, bracketed IDs removed) instead of throwing. - Before each window the kit presses play on a stopped player and loads the page again when that is not enough, or when the page was lost or errored. - A window the player spoilt (error, reload, pause, ad, seek, buffering, no frames) is measured again, up to 3 times in all, with the earlier attempts and the fixes kept in the row; a lower quality or frame rate is not, since a busy GPU can cause it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,6 +18,7 @@ from __future__ import annotations
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import socket
|
||||
import struct
|
||||
@@ -43,6 +44,10 @@ SETTLE_S = 5 # after a quality change or a seek, before a window starts
|
||||
MIN_LEFT_S = 240 # a window that could reach the end starts the video again first
|
||||
GAP_MS = (50, 100) # a gap between shown frames this long is a visible hitch at 60 fps
|
||||
ROUNDS = ("busy", "busy_cap95", "busy_cap95_2", "busy_2")
|
||||
# A window spoilt by the player (an error, a reload, a pause, an ad, buffering) is measured again, at most
|
||||
# this many times in all; one spoilt by a lower quality or frame rate is not (a busy GPU can cause that).
|
||||
ATTEMPTS = 3
|
||||
REPLAY_S = 30 # pressing play on a stopped player, before the page is loaded again
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
@@ -181,15 +186,22 @@ _INSTALL = """(() => {
|
||||
return true;
|
||||
})()""" % {"g0": GAP_MS[0], "g1": GAP_MS[1]}
|
||||
|
||||
_RESET = "(() => { const m = window.__m0; m.shown = 0; m.last = null; m.maxGap = 0; m.gaps = [0, 0]; " \
|
||||
_RESET = "(() => { const m = window.__m0; if (!m) return false; m.shown = 0; m.last = null; m.maxGap = 0; m.gaps = [0, 0]; " \
|
||||
"m.waits = 0; m.pauses = 0; m.seeks = 0; m.ad = false; m.minH = m.maxH = m.v.videoHeight; return true; })()"
|
||||
|
||||
# ``lost`` when the counters are gone (the page was loaded again, or YouTube swapped the <video>);
|
||||
# ``error`` when the player shows its own error screen (only its first line is kept, without the IDs).
|
||||
_SNAPSHOT = """(() => {
|
||||
const m = window.__m0, v = m.v, p = document.getElementById('movie_player');
|
||||
const q = v.getVideoPlaybackQuality();
|
||||
const m = window.__m0, p = document.getElementById('movie_player');
|
||||
const e = p && p.querySelector('.ytp-error');
|
||||
const error = !!(e && e.offsetParent !== null);
|
||||
const reason = error ? (e.querySelector('.ytp-error-content-wrap-reason') || e).textContent : null;
|
||||
if (!m || !m.v.isConnected) return {lost: true, error, error_text: reason};
|
||||
const v = m.v, q = v.getVideoPlaybackQuality();
|
||||
return {dropped: q.droppedVideoFrames, total: q.totalVideoFrames, shown: m.shown, max_gap_ms: m.maxGap,
|
||||
gaps: m.gaps, waits: m.waits, pauses: m.pauses, seeks: m.seeks, min_height: m.minH, max_height: m.maxH,
|
||||
ad: m.ad || !!(p && p.classList.contains('ad-showing')),
|
||||
ad_now: !!(p && p.classList.contains('ad-showing')), error, error_text: reason,
|
||||
paused: v.paused, ended: v.ended, time: v.currentTime, duration: v.duration,
|
||||
width: v.videoWidth, height: v.videoHeight,
|
||||
quality: p && p.getPlaybackQuality ? p.getPlaybackQuality() : null};
|
||||
@@ -218,11 +230,14 @@ def _js(value: Any) -> str:
|
||||
class VideoPage:
|
||||
"""The watch page in the throwaway browser."""
|
||||
|
||||
def __init__(self, cdp: Cdp, proc: subprocess.Popen | None = None, profile: Path | None = None):
|
||||
self.cdp, self.proc, self.profile = cdp, proc, profile
|
||||
def __init__(self, cdp: Cdp, proc: subprocess.Popen | None = None, profile: Path | None = None,
|
||||
url: str = VIDEO_URL):
|
||||
self.cdp, self.proc, self.profile, self.url = cdp, proc, profile, url
|
||||
self.quality = QUALITY
|
||||
|
||||
def start(self, quality: str = QUALITY, timeout: float = 180) -> dict:
|
||||
"""Wait for the video to play past any ad, then ask for ``quality``. Returns what happened."""
|
||||
self.quality = quality
|
||||
out: dict = {"started": False, "ad_seen": False, "ad_skipped": False, "quality_asked": quality}
|
||||
t0 = time.monotonic()
|
||||
while time.monotonic() - t0 < timeout:
|
||||
@@ -253,19 +268,45 @@ class VideoPage:
|
||||
out.update(quality=snap.get("quality"), width=snap.get("width"), height=snap.get("height"))
|
||||
return out
|
||||
|
||||
def keep_room(self, min_left: float = MIN_LEFT_S) -> None:
|
||||
"""Start the video again when a window could run into its end (YouTube then plays the next one)."""
|
||||
def reload(self) -> dict:
|
||||
"""Load the watch page again (its counters go with it) and get the video going as at the start."""
|
||||
self.cdp.call("Page.navigate", {"url": self.url})
|
||||
time.sleep(SETTLE_S)
|
||||
return self.start(self.quality)
|
||||
|
||||
def keep_room(self, min_left: float = MIN_LEFT_S) -> list[str]:
|
||||
"""Before a window: get a stopped, broken or reloaded player going again (pressing play first,
|
||||
loading the page again if that is not enough), and start the video again when the window could
|
||||
run into its end (YouTube then plays the next one). Returns what had to be done."""
|
||||
did: list[str] = []
|
||||
snap = self.snapshot()
|
||||
if snap.get("error"):
|
||||
did.append(f"the player showed an error: {_short(snap.get('error_text'))}")
|
||||
if snap.get("lost") or snap.get("error"):
|
||||
did.append("loaded the page again")
|
||||
self.reload()
|
||||
elif snap.get("paused") or snap.get("ended") or snap.get("ad_now") or not snap.get("height"):
|
||||
did.append("pressed play")
|
||||
if not self.start(self.quality, timeout=REPLAY_S)["started"]:
|
||||
did.append("loaded the page again")
|
||||
self.reload()
|
||||
if did:
|
||||
snap = self.snapshot()
|
||||
if (snap.get("duration") or 0) - (snap.get("time") or 0) < min_left:
|
||||
self.cdp.evaluate("(() => { document.getElementById('movie_player').seekTo(5, true); return true; })()")
|
||||
time.sleep(SETTLE_S)
|
||||
self.cdp.evaluate(_INSTALL)
|
||||
return did
|
||||
|
||||
def reset(self) -> None:
|
||||
self.cdp.evaluate(_RESET)
|
||||
|
||||
def snapshot(self) -> dict:
|
||||
return self.cdp.evaluate(_SNAPSHOT) or {}
|
||||
"""The counters; ``lost`` too when the page script could not run (the page was being loaded)."""
|
||||
try:
|
||||
return self.cdp.evaluate(_SNAPSHOT) or {}
|
||||
except RuntimeError as e:
|
||||
return {"lost": True, "script_error": _short(str(e))}
|
||||
|
||||
def close(self) -> None:
|
||||
try:
|
||||
@@ -282,6 +323,11 @@ class VideoPage:
|
||||
shutil.rmtree(self.profile, ignore_errors=True)
|
||||
|
||||
|
||||
def _short(text: Any, n: int = 120) -> str:
|
||||
"""One line of a player message, without its bracketed IDs (YouTube adds a playback ID)."""
|
||||
return re.sub(r"\s+", " ", re.sub(r"[(\[][^)\]]*[)\]]", "", str(text or ""))).strip()[:n]
|
||||
|
||||
|
||||
def find_browser() -> str | None:
|
||||
if IS_WINDOWS:
|
||||
cands = [os.path.expandvars(p) for p in (
|
||||
@@ -317,7 +363,7 @@ def open_video_page(work: Path, url: str = VIDEO_URL, extra_args: tuple[str, ...
|
||||
page = next((p for p in (pages or []) if p.get("type") == "page" and "youtube" in p.get("url", "")),
|
||||
None) if code == 200 else None
|
||||
if page:
|
||||
return VideoPage(Cdp(page["webSocketDebuggerUrl"]), proc, profile)
|
||||
return VideoPage(Cdp(page["webSocketDebuggerUrl"]), proc, profile, url)
|
||||
time.sleep(1)
|
||||
break
|
||||
if proc.poll() is not None:
|
||||
@@ -337,7 +383,16 @@ def window_stats(before: dict, after: dict, seconds: float, want: dict | None =
|
||||
any point, buffering, the end, a reload of the counters, or (with ``want``) another quality,
|
||||
height or a lower frame rate than the source's."""
|
||||
out: dict = {"seconds": round(seconds, 1)}
|
||||
if before.get("lost") or after.get("lost"):
|
||||
# The page was loaded again (by hand or by YouTube): its counters do not relate to ``before``.
|
||||
return {**out, "frames": None, "dropped": None, "gaps_over_ms": {str(g): None for g in GAP_MS},
|
||||
"valid": False, "retry": True,
|
||||
"invalid_because": ["the page was loaded again, so its counters were gone"]
|
||||
+ ([f"the player showed an error: {_short(after.get('error_text'))}"]
|
||||
if after.get("error") else [])}
|
||||
reasons = []
|
||||
if after.get("error"):
|
||||
reasons.append(f"the player showed an error: {_short(after.get('error_text'))}")
|
||||
if after.get("total", 0) < before.get("total", 0) or after.get("dropped", 0) < before.get("dropped", 0):
|
||||
reasons.append("the video's counters started again (another video was loaded)")
|
||||
for key, why in (("ad", "an ad was showing"), ("paused", "the video was paused"), ("ended", "the video ended")):
|
||||
@@ -359,6 +414,8 @@ def window_stats(before: dict, after: dict, seconds: float, want: dict | None =
|
||||
quality=after.get("quality"), height=after.get("height"))
|
||||
if total <= 0 and not reasons:
|
||||
reasons.append("the video showed no frames")
|
||||
# Everything so far comes from the player, not from the GPU: measuring again is fair.
|
||||
out["retry"] = bool(reasons)
|
||||
if want:
|
||||
got = {before.get("quality"), after.get("quality")}
|
||||
if got != {want["quality"]}:
|
||||
@@ -410,20 +467,32 @@ def cap_problems(duty: dict, pids: list[int]) -> list[str]:
|
||||
|
||||
def measure_window(page: VideoPage, work: Callable[[], dict] | None = None,
|
||||
seconds: float | None = None, want: dict | None = WANT) -> tuple[dict, dict]:
|
||||
"""The video's stats over ``work()`` (or ``seconds`` of nothing), with the GPU's use meanwhile."""
|
||||
page.keep_room()
|
||||
page.reset()
|
||||
before = page.snapshot()
|
||||
t0 = time.perf_counter()
|
||||
done: dict = {}
|
||||
with gpucap.UtilSampler() as util:
|
||||
if work:
|
||||
done = work()
|
||||
else:
|
||||
time.sleep(QUIET_S if seconds is None else seconds)
|
||||
after = page.snapshot()
|
||||
stats = window_stats(before, after, time.perf_counter() - t0, want)
|
||||
stats["gpu_util"] = util.summary()
|
||||
"""The video's stats over ``work()`` (or ``seconds`` of nothing), with the GPU's use meanwhile.
|
||||
A window the player spoilt is measured again (``work()`` too), up to ``ATTEMPTS`` in all; the
|
||||
spoilt ones are listed under ``earlier_attempts`` and what was done to the player under ``player_fixes``."""
|
||||
earlier: list[dict] = []
|
||||
fixes: list[str] = []
|
||||
while True:
|
||||
fixes += page.keep_room()
|
||||
page.reset()
|
||||
before = page.snapshot()
|
||||
t0 = time.perf_counter()
|
||||
done: dict = {}
|
||||
with gpucap.UtilSampler() as util:
|
||||
if work:
|
||||
done = work()
|
||||
else:
|
||||
time.sleep(QUIET_S if seconds is None else seconds)
|
||||
after = page.snapshot()
|
||||
stats = window_stats(before, after, time.perf_counter() - t0, want)
|
||||
stats["gpu_util"] = util.summary()
|
||||
if stats["valid"] or not stats.get("retry") or len(earlier) + 1 >= ATTEMPTS:
|
||||
break
|
||||
earlier.append({"seconds": stats["seconds"], "invalid_because": stats["invalid_because"]})
|
||||
if earlier:
|
||||
stats["earlier_attempts"] = earlier
|
||||
if fixes:
|
||||
stats["player_fixes"] = fixes
|
||||
return stats, done
|
||||
|
||||
|
||||
@@ -441,6 +510,7 @@ def measure(r: StageResult, s: "llm.Strata", preset: dict, context: int, stt_pyt
|
||||
r["video_rows"] = rows
|
||||
r["video_source"] = {"url": url, "quality_asked": quality}
|
||||
say(" 브라우저(새 임시 프로필)로 유튜브 영상을 틉니다. 끝날 때까지 PC에 손대지 말아 주세요.")
|
||||
say(" 영상이 멈추거나 오류가 떠도 키트가 알아서 다시 틀고 그 구간을 다시 잽니다. 새로고침하지 않으셔도 됩니다.")
|
||||
try:
|
||||
page = open_video_page(s.log.parent.parent)
|
||||
except Exception as e:
|
||||
@@ -473,6 +543,8 @@ def measure(r: StageResult, s: "llm.Strata", preset: dict, context: int, stt_pyt
|
||||
x["video"] = stats
|
||||
if not stats["valid"]:
|
||||
r.note(f"{x['key']}: not counted ({'; '.join(stats['invalid_because'])})")
|
||||
for a in stats.get("earlier_attempts") or []:
|
||||
r.note(f"{x['key']}: measured again after ({'; '.join(a['invalid_because'])})")
|
||||
v = stats
|
||||
say(f" {x['key']}: 끊긴 프레임 {v['dropped']}/{v['frames']}, {GAP_MS[1]}ms 넘는 멈춤 "
|
||||
f"{v['gaps_over_ms'][str(GAP_MS[1])]}번")
|
||||
|
||||
Reference in New Issue
Block a user