Skip to content
Open
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
14 changes: 14 additions & 0 deletions packages/ffmpeg/src/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ export class FFmpeg {
"abort",
() => {
reject(new DOMException(`Message # ${id} was aborted`, "AbortError"));
// If we are aborting an exec or ffprobe, send a CANCEL message so
// the worker's ffmpeg timeout is set to 1 ms, causing the running
// command to stop as quickly as possible. For other message types
// the cancel is a no-op on the worker side.
if (
(type === FFMessageType.EXEC ||
type === FFMessageType.FFPROBE) &&
this.#worker
) {
this.#worker.postMessage({
id: getMessageID(),
type: FFMessageType.CANCEL,
});
}
},
{ once: true }
);
Expand Down
1 change: 1 addition & 0 deletions packages/ffmpeg/src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum FFMessageType {
DELETE_DIR = "DELETE_DIR",
ERROR = "ERROR",

CANCEL = "CANCEL",
DOWNLOAD = "DOWNLOAD",
PROGRESS = "PROGRESS",
LOG = "LOG",
Expand Down
17 changes: 17 additions & 0 deletions packages/ffmpeg/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ const ffprobe = ({ args, timeout = -1 }: FFMessageExecData): ExitCode => {
return ret;
};

/**
* Interrupt any running exec/ffprobe by setting the ffmpeg timeout to 1 ms.
* This causes the WASM-side watchdog to fire almost immediately, stopping
* the current command and returning exit-code 1 (timeout). The exec()
* call on the main thread has already been rejected with an AbortError by
* the time this message arrives, so the exit-code reply is simply discarded.
*/
const cancel = (): void => {
if (ffmpeg) {
ffmpeg.setTimeout(1);
}
};

const writeFile = ({ path, data }: FFMessageWriteFileData): OK => {
ffmpeg.FS.writeFile(path, data);
return true;
Expand Down Expand Up @@ -181,6 +194,10 @@ self.onmessage = async ({
case FFMessageType.FFPROBE:
data = ffprobe(_data as FFMessageExecData);
break;
case FFMessageType.CANCEL:
cancel();
// CANCEL is fire-and-forget: no reply needed.
return;
case FFMessageType.WRITE_FILE:
data = writeFile(_data as FFMessageWriteFileData);
break;
Expand Down