Skip to content

Add WORKERFS support #581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Oct 9, 2023
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
1 change: 1 addition & 0 deletions build/ffmpeg-wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ CONF_FLAGS=(
-sEXPORT_NAME="$EXPORT_NAME" # required in browser env, so that user can access this module from window object
-sEXPORTED_FUNCTIONS=$(node src/bind/ffmpeg/export.js) # exported functions
-sEXPORTED_RUNTIME_METHODS=$(node src/bind/ffmpeg/export-runtime.js) # exported built-in functions
-lworkerfs.js
--pre-js src/bind/ffmpeg/bind.js # extra bindings, contains most of the ffmpeg.wasm javascript code
# ffmpeg source code
src/fftools/cmdutils.c
Expand Down
28 changes: 28 additions & 0 deletions packages/ffmpeg/src/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
LogEventCallback,
ProgressEventCallback,
FileData,
WorkerFSMountData,
FFFSType,
FFFSMountOptions,
FFFSPath,
} from "./types.js";
import { getMessageID } from "./utils.js";
import { ERROR_TERMINATED, ERROR_NOT_LOADED } from "./errors.js";
Expand Down Expand Up @@ -52,6 +56,8 @@ export class FFmpeg {
this.loaded = true;
this.#resolves[id](data);
break;
case FFMessageType.MOUNT:
case FFMessageType.UNMOUNT:
case FFMessageType.EXEC:
case FFMessageType.WRITE_FILE:
case FFMessageType.READ_FILE:
Expand Down Expand Up @@ -258,6 +264,28 @@ export class FFmpeg {
) as Promise<OK>;
};

public mount = (fsType: FFFSType, options: FFFSMountOptions, mountPoint: FFFSPath, ): Promise<OK> => {
const trans: Transferable[] = [];
return this.#send(
{
type: FFMessageType.MOUNT,
data: { fsType, options, mountPoint },
},
trans
) as Promise<OK>;
};

public unmount = (mountPoint: FFFSPath): Promise<OK> => {
const trans: Transferable[] = [];
return this.#send(
{
type: FFMessageType.UNMOUNT,
data: { mountPoint },
},
trans
) as Promise<OK>;
};

/**
* Read data from ffmpeg.wasm.
*
Expand Down
4 changes: 3 additions & 1 deletion packages/ffmpeg/src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ export enum FFMessageType {
DOWNLOAD = "DOWNLOAD",
PROGRESS = "PROGRESS",
LOG = "LOG",
}
MOUNT = "MOUNT",
UNMOUNT = "UNMOUNT",
}
41 changes: 39 additions & 2 deletions packages/ffmpeg/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,41 @@ export interface FFMessageDeleteDirData {
path: FFFSPath;
}

export enum FFFSType {
MEMFS = "MEMFS",
NODEFS = "NODEFS",
NODERAWFS = "NODERAWFS",
IDBFS = "IDBFS",
WORKERFS = "WORKERFS",
PROXYFS = "PROXYFS",
}

export type WorkerFSFileEntry =
| File;

export interface WorkerFSBlobEntry {
name: string;
data: Blob;
}

export interface WorkerFSMountData {
blobs?: WorkerFSBlobEntry[];
files?: WorkerFSFileEntry[];
}

export type FFFSMountOptions =
| WorkerFSMountData;

export interface FFMessageMountData {
fsType: FFFSType;
options: FFFSMountOptions;
mountPoint: FFFSPath;
}

export interface FFMessageUnmountData {
mountPoint: FFFSPath;
}

export type FFMessageData =
| FFMessageLoadConfig
| FFMessageExecData
Expand All @@ -73,7 +108,9 @@ export type FFMessageData =
| FFMessageRenameData
| FFMessageCreateDirData
| FFMessageListDirData
| FFMessageDeleteDirData;
| FFMessageDeleteDirData
| FFMessageMountData
| FFMessageUnmountData;

export interface Message {
type: string;
Expand Down Expand Up @@ -134,4 +171,4 @@ export interface FFMessageEventCallback {
type: string;
data: CallbackData;
};
}
}
21 changes: 21 additions & 0 deletions packages/ffmpeg/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import type {
FFMessageCreateDirData,
FFMessageListDirData,
FFMessageDeleteDirData,
FFMessageMountData,
FFMessageUnmountData,
CallbackData,
IsFirst,
OK,
Expand Down Expand Up @@ -137,6 +139,19 @@ const deleteDir = ({ path }: FFMessageDeleteDirData): OK => {
return true;
};

const mount = ({ fsType, options, mountPoint }: FFMessageMountData): OK => {
let str = fsType as keyof typeof ffmpeg.FS.filesystems;
let fs = ffmpeg.FS.filesystems[str];
if (!fs) return false;
ffmpeg.FS.mount(fs, options, mountPoint);
return true;
};

const unmount = ({ mountPoint }: FFMessageUnmountData): OK => {
ffmpeg.FS.unmount(mountPoint);
return true;
};

self.onmessage = async ({
data: { id, type, data: _data },
}: FFMessageEvent): Promise<void> => {
Expand Down Expand Up @@ -173,6 +188,12 @@ self.onmessage = async ({
case FFMessageType.DELETE_DIR:
data = deleteDir(_data as FFMessageDeleteDirData);
break;
case FFMessageType.MOUNT:
data = mount(_data as FFMessageMountData);
break;
case FFMessageType.UNMOUNT:
data = unmount(_data as FFMessageUnmountData);
break;
default:
throw ERROR_UNKNOWN_MESSAGE_TYPE;
}
Expand Down
20 changes: 20 additions & 0 deletions packages/types/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ export interface Stat {
blocks: number;
}

export interface FSFilesystemWORKERFS {

}

export interface FSFilesystemMEMFS {

}

export interface FSFilesystems {
WORKERFS: FSFilesystemWORKERFS;
MEMFS: FSFilesystemMEMFS;
}

export type FSFilesystem =
| FSFilesystemWORKERFS
| FSFilesystemMEMFS;

/**
* Functions to interact with Emscripten FS library.
*
Expand All @@ -58,6 +75,9 @@ export interface FS {
isFile: (mode: number) => boolean;
/** mode is a numeric notation of permission, @see [Numeric Notation](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) */
isDir: (mode: number) => boolean;
mount: (fileSystemType: FSFilesystem, data: WorkerFSMountConfig, path: string) => void;
unmount: (path: string) => void;
filesystems: FSFilesystems;
}

/**
Expand Down