diff --git a/CHANGELOG.md b/CHANGELOG.md index c15b275c7..22761d07a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed +- **Frames and exports from a background tab.** When the editor tab was in the + background, every seek waited about 3 s for a frame the browser never shows + there, and a slow seek could be restarted until it failed, so + `fablecut_frame {time}`, scopes and exports from that tab were slow or + failed. A finished seek now counts at once in a background tab. + ## [1.20.0] - 2026-11-20 ### Added diff --git a/app.js b/app.js index 82e35d8ea..62d11f632 100644 --- a/app.js +++ b/app.js @@ -17210,25 +17210,44 @@ function sameSourceFrame(el, mt, shown) { return el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002 && mt >= shown - 1e-4 && mt < shown + dur - 1e-4; } -function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { +/** A hidden tab renders nothing, so it presents no frames and runs no frame callbacks. */ +function tabHidden() { return typeof document !== "undefined" && document.hidden === true; } +/** The running export's cancel signal, if any. */ +function exportSignal() { return typeof exportAbort !== "undefined" && exportAbort ? exportAbort.signal : null; } +function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime, signal = exportSignal()) { const hasRvfc = typeof el.requestVideoFrameCallback === "function"; const maxAttempts = 3; + // an export's Cancel stops a seek that is still waiting (a hidden tab waits up to 10 s); + // retries keep the signal of the export that started them, even once it is cleared + if (signal && signal.aborted) return Promise.reject(new Error("cancelled")); return new Promise((res, rej) => { // a busy frame (grades, masks, blur) can present late: each retry waits longer - const after = () => waitForPresentedFrame(el, [80, 250, 600][attempt] || 600) - .then(res) - .catch((err) => { - // Slow motion lands several output frames on one source frame: seeking within the - // frame already on screen presents nothing new, so no frame callback ever comes. - // The seek finished on mt and the frame shown just before is within one source frame - // of it (≥ 20 fps footage): that frame is the right picture. - if (sameSourceFrame(el, mt, shown)) { notePresented(el, shown); res(); return; } - if (attempt + 1 < maxAttempts) { hardSeekVideo(el, mt, attempt + 1, shown).then(res, rej); return; } - // Last try: the seek finished on mt with the frame's data loaded, so the element holds - // that frame even though no callback reported it. Better than failing the whole export. - if (el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { notePresented(el, mt); res(); return; } - rej(err); - }); + const after = () => { + if (signal && signal.aborted) { rej(new Error("cancelled")); return; } + // a hidden tab renders nothing, so no frame callback comes: a seek that finished on mt + // is the signal. It is no presented frame, so it teaches nothing about frame durations. + if (tabHidden() && el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { + el._fcPresentedTime = el.currentTime; + el._fcLastShown = null; + res(); + return; + } + waitForPresentedFrame(el, [80, 250, 600][attempt] || 600) + .then(res) + .catch((err) => { + // Slow motion lands several output frames on one source frame: seeking within the + // frame already on screen presents nothing new, so no frame callback ever comes. + // The seek finished on mt and the frame shown just before is within one source frame + // of it (≥ 20 fps footage): that frame is the right picture. + if (sameSourceFrame(el, mt, shown)) { notePresented(el, shown); res(); return; } + if (signal && signal.aborted) { rej(new Error("cancelled")); return; } + if (attempt + 1 < maxAttempts) { hardSeekVideo(el, mt, attempt + 1, shown, signal).then(res, rej); return; } + // Last try: the seek finished on mt with the frame's data loaded, so the element holds + // that frame even though no callback reported it. Better than failing the whole export. + if (el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { notePresented(el, mt); res(); return; } + rej(err); + }); + }; if (presentedClose(el, mt, 0.002) && el.readyState >= 2) { res(); return; @@ -17244,10 +17263,14 @@ function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { const done = () => { clearTimeout(tm); el.removeEventListener("seeked", done); + if (signal) signal.removeEventListener("abort", done); after(); }; - const tm = setTimeout(done, 1500); + // Chrome slows a hidden tab's decoding: re-seeking on a short timeout would restart a seek + // that is still on its way, so wait longer for it there + const tm = setTimeout(done, tabHidden() ? 10000 : 1500); el.addEventListener("seeked", done); + if (signal) signal.addEventListener("abort", done); try { if (!el.paused) el.pause(); // Same currentTime does not fire seeked — nudge so the decoder must present mt. diff --git a/docs/demo/app.js b/docs/demo/app.js index 82e35d8ea..62d11f632 100644 --- a/docs/demo/app.js +++ b/docs/demo/app.js @@ -17210,25 +17210,44 @@ function sameSourceFrame(el, mt, shown) { return el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002 && mt >= shown - 1e-4 && mt < shown + dur - 1e-4; } -function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { +/** A hidden tab renders nothing, so it presents no frames and runs no frame callbacks. */ +function tabHidden() { return typeof document !== "undefined" && document.hidden === true; } +/** The running export's cancel signal, if any. */ +function exportSignal() { return typeof exportAbort !== "undefined" && exportAbort ? exportAbort.signal : null; } +function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime, signal = exportSignal()) { const hasRvfc = typeof el.requestVideoFrameCallback === "function"; const maxAttempts = 3; + // an export's Cancel stops a seek that is still waiting (a hidden tab waits up to 10 s); + // retries keep the signal of the export that started them, even once it is cleared + if (signal && signal.aborted) return Promise.reject(new Error("cancelled")); return new Promise((res, rej) => { // a busy frame (grades, masks, blur) can present late: each retry waits longer - const after = () => waitForPresentedFrame(el, [80, 250, 600][attempt] || 600) - .then(res) - .catch((err) => { - // Slow motion lands several output frames on one source frame: seeking within the - // frame already on screen presents nothing new, so no frame callback ever comes. - // The seek finished on mt and the frame shown just before is within one source frame - // of it (≥ 20 fps footage): that frame is the right picture. - if (sameSourceFrame(el, mt, shown)) { notePresented(el, shown); res(); return; } - if (attempt + 1 < maxAttempts) { hardSeekVideo(el, mt, attempt + 1, shown).then(res, rej); return; } - // Last try: the seek finished on mt with the frame's data loaded, so the element holds - // that frame even though no callback reported it. Better than failing the whole export. - if (el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { notePresented(el, mt); res(); return; } - rej(err); - }); + const after = () => { + if (signal && signal.aborted) { rej(new Error("cancelled")); return; } + // a hidden tab renders nothing, so no frame callback comes: a seek that finished on mt + // is the signal. It is no presented frame, so it teaches nothing about frame durations. + if (tabHidden() && el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { + el._fcPresentedTime = el.currentTime; + el._fcLastShown = null; + res(); + return; + } + waitForPresentedFrame(el, [80, 250, 600][attempt] || 600) + .then(res) + .catch((err) => { + // Slow motion lands several output frames on one source frame: seeking within the + // frame already on screen presents nothing new, so no frame callback ever comes. + // The seek finished on mt and the frame shown just before is within one source frame + // of it (≥ 20 fps footage): that frame is the right picture. + if (sameSourceFrame(el, mt, shown)) { notePresented(el, shown); res(); return; } + if (signal && signal.aborted) { rej(new Error("cancelled")); return; } + if (attempt + 1 < maxAttempts) { hardSeekVideo(el, mt, attempt + 1, shown, signal).then(res, rej); return; } + // Last try: the seek finished on mt with the frame's data loaded, so the element holds + // that frame even though no callback reported it. Better than failing the whole export. + if (el.readyState >= 2 && !el.seeking && Math.abs(el.currentTime - mt) < 0.002) { notePresented(el, mt); res(); return; } + rej(err); + }); + }; if (presentedClose(el, mt, 0.002) && el.readyState >= 2) { res(); return; @@ -17244,10 +17263,14 @@ function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) { const done = () => { clearTimeout(tm); el.removeEventListener("seeked", done); + if (signal) signal.removeEventListener("abort", done); after(); }; - const tm = setTimeout(done, 1500); + // Chrome slows a hidden tab's decoding: re-seeking on a short timeout would restart a seek + // that is still on its way, so wait longer for it there + const tm = setTimeout(done, tabHidden() ? 10000 : 1500); el.addEventListener("seeked", done); + if (signal) signal.addEventListener("abort", done); try { if (!el.paused) el.pause(); // Same currentTime does not fire seeked — nudge so the decoder must present mt. diff --git a/test/export-seek.test.js b/test/export-seek.test.js index 73289473e..a3b894763 100644 --- a/test/export-seek.test.js +++ b/test/export-seek.test.js @@ -192,3 +192,56 @@ test("hardSeekVideo: a seek that never lands still fails", async () => { const el = seekingVideo({ currentTime: 0, presented: 0, completes: false }); await assert.rejects(hardSeekVideo(el, 2.0), /presented frame timeout/); }); + +/* A hidden tab (the user switched away while an agent works) runs no frame + callbacks: a finished seek must resolve at once, not wait out every retry. */ +test("hardSeekVideo: in a hidden tab a finished seek resolves without a frame callback", async () => { + const { hardSeekVideo } = loadHard(); + const el = seekingVideo({ currentTime: 0, presented: 0, frameDur: 0 }); + globalThis.document = { hidden: true }; + try { + const t0 = Date.now(); + await hardSeekVideo(el, 2.0); + assert.ok(Date.now() - t0 < 500, "resolved without the frame-callback retries"); + assert.equal(el._fcPresentedTime, 2.0); + assert.equal(el.rvfcCalls, 0); + } finally { delete globalThis.document; } +}); + +test("hardSeekVideo: a hidden-tab seek teaches nothing about frame durations", async () => { + const { hardSeekVideo } = loadHard(); + const el = seekingVideo({ currentTime: 0, presented: 1.0, frameDur: 1 / 30 }); + globalThis.document = { hidden: true }; + try { + await hardSeekVideo(el, 1.01); + assert.equal(el._fcFrameDur, 1 / 30); + assert.equal(el._fcLastShown, null); + } finally { delete globalThis.document; } +}); + +test("hardSeekVideo: an export's Cancel stops a seek still waiting in a hidden tab", async () => { + const { hardSeekVideo } = loadHard(); + const el = seekingVideo({ currentTime: 0, presented: 0, completes: false }); + globalThis.document = { hidden: true }; + globalThis.exportAbort = new AbortController(); + try { + const t0 = Date.now(); + const p = hardSeekVideo(el, 2.0); + setTimeout(() => globalThis.exportAbort.abort(), 20); + await assert.rejects(p, /cancelled/); + assert.ok(Date.now() - t0 < 1000, "rejected on Cancel, not after the 10 s wait"); + } finally { delete globalThis.document; delete globalThis.exportAbort; } +}); + +test("hardSeekVideo: retries keep the export's cancel signal after it is cleared", async () => { + const { hardSeekVideo } = loadHard(); + const el = seekingVideo({ currentTime: 0, presented: 0, frameDur: 0 }); // seeks land, no frame callback + const ctl = new AbortController(); + globalThis.exportAbort = ctl; + try { + const p = hardSeekVideo(el, 2.0); + globalThis.exportAbort = null; // export cleanup after a sibling seek was cancelled + ctl.abort(); + await assert.rejects(p, /cancelled/); + } finally { delete globalThis.exportAbort; } +});