Skip to content

Commit

Permalink
feat(ffmpeg): abort signal (#573)
Browse files Browse the repository at this point in the history
* feat(ffmpeg): abort signal

* with test
  • Loading branch information
abernier authored Oct 9, 2023
1 parent cf9cf11 commit efaae60
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 45 deletions.
165 changes: 120 additions & 45 deletions packages/ffmpeg/src/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import {
import { getMessageID } from "./utils.js";
import { ERROR_TERMINATED, ERROR_NOT_LOADED } from "./errors.js";

type FFMessageOptions = {
signal?: AbortSignal;
};

/**
* Provides APIs to interact with ffmpeg web worker.
*
Expand Down Expand Up @@ -85,7 +89,8 @@ export class FFmpeg {
*/
#send = (
{ type, data }: Message,
trans: Transferable[] = []
trans: Transferable[] = [],
signal?: AbortSignal
): Promise<CallbackData> => {
if (!this.#worker) {
return Promise.reject(ERROR_NOT_LOADED);
Expand All @@ -96,6 +101,14 @@ export class FFmpeg {
this.#worker && this.#worker.postMessage({ id, type, data }, trans);
this.#resolves[id] = resolve;
this.#rejects[id] = reject;

signal?.addEventListener(
"abort",
() => {
reject(new DOMException(`Message # ${id} was aborted`, "AbortError"));
},
{ once: true }
);
});
};

Expand Down Expand Up @@ -148,9 +161,13 @@ export class FFmpeg {
callback: LogEventCallback | ProgressEventCallback
) {
if (event === "log") {
this.#logEventCallbacks = this.#logEventCallbacks.filter((f) => f !== callback);
this.#logEventCallbacks = this.#logEventCallbacks.filter(
(f) => f !== callback
);
} else if (event === "progress") {
this.#progressEventCallbacks = this.#progressEventCallbacks.filter((f) => f !== callback);
this.#progressEventCallbacks = this.#progressEventCallbacks.filter(
(f) => f !== callback
);
}
}

Expand All @@ -161,17 +178,24 @@ export class FFmpeg {
* @category FFmpeg
* @returns `true` if ffmpeg core is loaded for the first time.
*/
public load = (config: FFMessageLoadConfig = {}): Promise<IsFirst> => {
public load = (
config: FFMessageLoadConfig = {},
{ signal }: FFMessageOptions = {}
): Promise<IsFirst> => {
if (!this.#worker) {
this.#worker = new Worker(new URL("./worker.js", import.meta.url), {
type: "module",
});
this.#registerHandlers();
}
return this.#send({
type: FFMessageType.LOAD,
data: config,
}) as Promise<IsFirst>;
return this.#send(
{
type: FFMessageType.LOAD,
data: config,
},
undefined,
signal
) as Promise<IsFirst>;
};

/**
Expand Down Expand Up @@ -202,12 +226,17 @@ export class FFmpeg {
*
* @defaultValue -1
*/
timeout = -1
timeout = -1,
{ signal }: FFMessageOptions = {}
): Promise<number> =>
this.#send({
type: FFMessageType.EXEC,
data: { args, timeout },
}) as Promise<number>;
this.#send(
{
type: FFMessageType.EXEC,
data: { args, timeout },
},
undefined,
signal
) as Promise<number>;

/**
* Terminate all ongoing API calls and terminate web worker.
Expand Down Expand Up @@ -244,7 +273,11 @@ export class FFmpeg {
*
* @category File System
*/
public writeFile = (path: string, data: FileData): Promise<OK> => {
public writeFile = (
path: string,
data: FileData,
{ signal }: FFMessageOptions = {}
): Promise<OK> => {
const trans: Transferable[] = [];
if (data instanceof Uint8Array) {
trans.push(data.buffer);
Expand All @@ -254,7 +287,8 @@ export class FFmpeg {
type: FFMessageType.WRITE_FILE,
data: { path, data },
},
trans
trans,
signal
) as Promise<OK>;
};

Expand All @@ -279,65 +313,106 @@ export class FFmpeg {
*
* @defaultValue binary
*/
encoding = "binary"
encoding = "binary",
{ signal }: FFMessageOptions = {}
): Promise<FileData> =>
this.#send({
type: FFMessageType.READ_FILE,
data: { path, encoding },
}) as Promise<FileData>;
this.#send(
{
type: FFMessageType.READ_FILE,
data: { path, encoding },
},
undefined,
signal
) as Promise<FileData>;

/**
* Delete a file.
*
* @category File System
*/
public deleteFile = (path: string): Promise<OK> =>
this.#send({
type: FFMessageType.DELETE_FILE,
data: { path },
}) as Promise<OK>;
public deleteFile = (
path: string,
{ signal }: FFMessageOptions = {}
): Promise<OK> =>
this.#send(
{
type: FFMessageType.DELETE_FILE,
data: { path },
},
undefined,
signal
) as Promise<OK>;

/**
* Rename a file or directory.
*
* @category File System
*/
public rename = (oldPath: string, newPath: string): Promise<OK> =>
this.#send({
type: FFMessageType.RENAME,
data: { oldPath, newPath },
}) as Promise<OK>;
public rename = (
oldPath: string,
newPath: string,
{ signal }: FFMessageOptions = {}
): Promise<OK> =>
this.#send(
{
type: FFMessageType.RENAME,
data: { oldPath, newPath },
},
undefined,
signal
) as Promise<OK>;

/**
* Create a directory.
*
* @category File System
*/
public createDir = (path: string): Promise<OK> =>
this.#send({
type: FFMessageType.CREATE_DIR,
data: { path },
}) as Promise<OK>;
public createDir = (
path: string,
{ signal }: FFMessageOptions = {}
): Promise<OK> =>
this.#send(
{
type: FFMessageType.CREATE_DIR,
data: { path },
},
undefined,
signal
) as Promise<OK>;

/**
* List directory contents.
*
* @category File System
*/
public listDir = (path: string): Promise<FSNode[]> =>
this.#send({
type: FFMessageType.LIST_DIR,
data: { path },
}) as Promise<FSNode[]>;
public listDir = (
path: string,
{ signal }: FFMessageOptions = {}
): Promise<FSNode[]> =>
this.#send(
{
type: FFMessageType.LIST_DIR,
data: { path },
},
undefined,
signal
) as Promise<FSNode[]>;

/**
* Delete an empty directory.
*
* @category File System
*/
public deleteDir = (path: string): Promise<OK> =>
this.#send({
type: FFMessageType.DELETE_DIR,
data: { path },
}) as Promise<OK>;
public deleteDir = (
path: string,
{ signal }: FFMessageOptions = {}
): Promise<OK> =>
this.#send(
{
type: FFMessageType.DELETE_DIR,
data: { path },
},
undefined,
signal
) as Promise<OK>;
}
14 changes: 14 additions & 0 deletions tests/ffmpeg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,18 @@ describe(genName("FFmpeg.exec()"), function () {
const ret = await ffmpeg.exec(["-i", "video.mp4", "video.avi"], 1);
expect(ret).to.equal(1);
});

it("should abort", () => {
const controller = new AbortController();
const { signal } = controller;

const promise = ffmpeg.exec(["-i", "video.mp4", "video.avi"], undefined, {
signal,
});
controller.abort();

return promise.catch((err) => {
expect(err.name).to.equal("AbortError");
});
});
});

0 comments on commit efaae60

Please sign in to comment.