Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 39 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17244,10 +17263,14 @@ function hardSeekVideo(el, mt, attempt = 0, shown = el._fcPresentedTime) {
const done = () => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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.
Expand Down
55 changes: 39 additions & 16 deletions docs/demo/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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;
Expand All @@ -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.
Expand Down
53 changes: 53 additions & 0 deletions test/export-seek.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
});
Loading