Skip to content

Commit

Permalink
Add WORKERFS support
Browse files Browse the repository at this point in the history
  • Loading branch information
LostBeard committed Sep 12, 2023
1 parent faf13d1 commit 8d7246c
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 9 deletions.
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
37 changes: 36 additions & 1 deletion packages/ffmpeg/src/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,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 @@ -171,7 +173,7 @@ export class FFmpeg {
this.#worker = new Worker(config.workerLoadURL, {
type: "module",
});
}
}
this.#registerHandlers();
}
return this.#send({
Expand Down Expand Up @@ -264,6 +266,39 @@ export class FFmpeg {
) as Promise<OK>;
};

public mount = (path: string, data: WorkerFSMountData): Promise<OK> => {
const trans: Transferable[] = [];
// TODO - handle transferables
if (data.blobs){
for(let blob in data.blobs){
trans.push(data.buffer);
}
}
if (data.files){
for(let file in data.files){

}
}
return this.#send(
{
type: FFMessageType.MOUNT,
data: { path, data },
},
trans
) as Promise<OK>;
};

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

/**
* Read data from ffmpeg.wasm.
*
Expand Down
4 changes: 4 additions & 0 deletions packages/ffmpeg/src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ export enum FFMessageType {
DOWNLOAD = "DOWNLOAD",
PROGRESS = "PROGRESS",
LOG = "LOG",

MOUNT = "MOUNT",
UNMOUNT = "UNMOUNT",
FILESYSTEMS = "FILESYSTEMS",
}
40 changes: 32 additions & 8 deletions packages/ffmpeg/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ export interface FFMessageLoadConfig {
* @defaultValue `https://unpkg.com/@ffmpeg/core-mt@${CORE_VERSION}/dist/umd/ffmpeg-core.worker.js`;
*/
workerURL?: string;
/**
* `worker.js` URL.
*
* @defaultValue `new URL('./worker.js')`;
*/
workerLoadURL?: string;
/**
* `worker.js` URL.
*
* @defaultValue `new URL('./worker.js')`;
*/
workerLoadURL?: string;
}

export interface FFMessageExecData {
Expand Down Expand Up @@ -70,6 +70,28 @@ export interface FFMessageDeleteDirData {
path: FFFSPath;
}

export type WorkerFSFileEntry =
| File;

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

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

export interface FFMessageMountData {
path: FFFSPath;
data: WorkerFSMountData;
}

export interface FFMessageUnmountData {
path: FFFSPath;
}

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

export interface Message {
type: string;
Expand Down Expand Up @@ -140,4 +164,4 @@ export interface FFMessageEventCallback {
type: string;
data: CallbackData;
};
}
}
16 changes: 16 additions & 0 deletions packages/ffmpeg/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ const deleteDir = ({ path }: FFMessageDeleteDirData): OK => {
return true;
};

const mount = ({ path, data }: FFMessageMountData): OK => {
ffmpeg.FS.mount(ffmpeg.FS.WORKERFS, data, path);
return true;
};

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

self.onmessage = async ({
data: { id, type, data: _data },
}: FFMessageEvent): Promise<void> => {
Expand Down Expand Up @@ -173,6 +183,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
3 changes: 3 additions & 0 deletions packages/types/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,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: number, data: WorkerFSMountConfig, path: string) => void;
unmount: (path: string) => void;
WORKERFS: number;
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/bind/ffmpeg/export-runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const EXPORTED_RUNTIME_METHODS = [
"FS",
"FS_mount",
"FS_unmount",
"FS_filesystems",
"setValue",
"getValue",
"UTF8ToString",
Expand Down

0 comments on commit 8d7246c

Please sign in to comment.