Update options in Worker.run() to make it more clear
This commit is contained in:
@@ -106,33 +106,35 @@ module.exports = (_options = {}) => {
|
||||
}))
|
||||
);
|
||||
|
||||
const transcode = (inputPath, outputPath, opts = '', del = true, jobId) => (
|
||||
const transcode = (input, output, opts = '', del = true, jobId) => (
|
||||
run(
|
||||
`${opts} -i /data/${inputPath} ${outputPath}`,
|
||||
{ inputPath, outputPath, del },
|
||||
`${opts} -i /data/${input} ${output}`,
|
||||
{ input, output, del },
|
||||
jobId,
|
||||
)
|
||||
);
|
||||
|
||||
const trim = (inputPath, outputPath, from, to, opts = '', del = true, jobId) => (
|
||||
const trim = (input, output, from, to, opts = '', del = true, jobId) => (
|
||||
run(
|
||||
`${opts} -i /data/${inputPath} -ss ${from} -to ${to} -c copy ${outputPath}`,
|
||||
{ inputPath, outputPath, del },
|
||||
`${opts} -i /data/${input} -ss ${from} -to ${to} -c copy ${output}`,
|
||||
{ input, output, del },
|
||||
jobId,
|
||||
)
|
||||
);
|
||||
|
||||
const concatDemuxer = async (inputPaths, outputPath, opts = '', del = true, jobId) => {
|
||||
const text = inputPaths.reduce((acc, input) => `${acc}\nfile ${input}`, '');
|
||||
const concatDemuxer = async (input, output, opts = '', del = true, jobId) => {
|
||||
const text = input.reduce((acc, path) => `${acc}\nfile ${path}`, '');
|
||||
await writeText('concat_list.txt', text);
|
||||
return run(`${opts} -f concat -safe 0 -i /data/concat_list.txt -c copy ${outputPath}`,
|
||||
{ del, outputPath, inputPaths: [...inputPaths, 'concat_list.txt'] },
|
||||
return run(`${opts} -f concat -safe 0 -i /data/concat_list.txt -c copy ${output}`,
|
||||
{ del, output, input: [...input, 'concat_list.txt'] },
|
||||
jobId);
|
||||
};
|
||||
|
||||
const ls = (path, jobId) => (
|
||||
startJob(createJob({
|
||||
id: jobId, action: 'ls', payload: { path },
|
||||
id: jobId,
|
||||
action: 'FS',
|
||||
payload: { method: 'readdir', args: [path] },
|
||||
}))
|
||||
);
|
||||
|
||||
@@ -154,10 +156,13 @@ module.exports = (_options = {}) => {
|
||||
if (status === 'resolve') {
|
||||
log(`[${workerId}]: Complete ${jobId}`);
|
||||
let d = data;
|
||||
if (action === 'read') {
|
||||
d = Uint8Array.from({ ...data, length: Object.keys(data).length });
|
||||
} else {
|
||||
logger(d);
|
||||
if (action === 'FS') {
|
||||
const { method, data: _data } = data;
|
||||
if (method === 'readFile') {
|
||||
d = Uint8Array.from({ ..._data, length: Object.keys(_data).length });
|
||||
} else {
|
||||
d = _data;
|
||||
}
|
||||
}
|
||||
resolves[action]({ jobId, data: d });
|
||||
} else if (status === 'reject') {
|
||||
|
||||
@@ -1,31 +1,12 @@
|
||||
require('regenerator-runtime/runtime');
|
||||
const defaultArgs = require('./constants/defaultArgs');
|
||||
const strList2ptr = require('./utils/strList2ptr');
|
||||
|
||||
let action = 'unknown';
|
||||
let Module = null;
|
||||
let adapter = null;
|
||||
let ffmpeg = null;
|
||||
|
||||
const str2ptr = (s) => {
|
||||
const ptr = Module._malloc((s.length + 1) * Uint8Array.BYTES_PER_ELEMENT);
|
||||
for (let i = 0; i < s.length; i += 1) {
|
||||
Module.setValue(ptr + i, s.charCodeAt(i), 'i8');
|
||||
}
|
||||
Module.setValue(ptr + s.length, 0, 'i8');
|
||||
return ptr;
|
||||
};
|
||||
|
||||
const strList2ptr = (strList) => {
|
||||
const listPtr = Module._malloc(strList.length * Uint32Array.BYTES_PER_ELEMENT);
|
||||
|
||||
strList.forEach((s, idx) => {
|
||||
const strPtr = str2ptr(s);
|
||||
Module.setValue(listPtr + (4 * idx), strPtr, 'i32');
|
||||
});
|
||||
|
||||
return listPtr;
|
||||
};
|
||||
|
||||
const load = ({ workerId, payload: { options: { corePath } } }, res) => {
|
||||
if (Module == null) {
|
||||
const Core = adapter.getCore(corePath);
|
||||
@@ -54,33 +35,57 @@ const syncfs = async ({
|
||||
res.resolve({ message: `Sync file system with populate=${populate}` });
|
||||
};
|
||||
|
||||
const ls = ({
|
||||
const FS = ({
|
||||
payload: {
|
||||
path,
|
||||
method,
|
||||
args,
|
||||
},
|
||||
}, res) => {
|
||||
const dirs = Module.FS.readdir(path);
|
||||
res.resolve({ message: `List path ${path}`, dirs });
|
||||
const data = Module.FS[method](...args);
|
||||
res.resolve({
|
||||
message: `${method} ${args.join(',')}`,
|
||||
method,
|
||||
data,
|
||||
});
|
||||
};
|
||||
|
||||
const run = async ({
|
||||
payload: {
|
||||
args: _args,
|
||||
options: {
|
||||
inputPath, inputPaths, outputPath, del,
|
||||
input, output, del = true,
|
||||
},
|
||||
},
|
||||
}, res) => {
|
||||
const args = [...defaultArgs, ..._args.trim().split(' ')];
|
||||
ffmpeg(args.length, strList2ptr(args));
|
||||
await adapter.fs.writeFile(outputPath, Module.FS.readFile(outputPath));
|
||||
Module.FS.unlink(outputPath);
|
||||
if (del && typeof inputPath === 'string') {
|
||||
await adapter.fs.deleteFile(inputPath);
|
||||
} else if (del && Array.isArray(inputPaths)) {
|
||||
inputPaths.reduce((promise, input) => promise.then(() => adapter.fs.deleteFile(input)),
|
||||
Promise.resolve());
|
||||
ffmpeg(args.length, strList2ptr(Module, args));
|
||||
|
||||
/*
|
||||
* After executing the ffmpeg command, the data is saved in MEMFS,
|
||||
* if `output` is specified in the options, here ffmpeg.js will move
|
||||
* these files to IDBFS or NODEFS here.
|
||||
*/
|
||||
if (typeof output === 'string') {
|
||||
await adapter.fs.writeFile(output, Module.FS.readFile(output));
|
||||
Module.FS.unlink(output);
|
||||
} else if (Array.isArray(output)) {
|
||||
await Promise.all(output.map(async (p) => {
|
||||
await adapter.fs.writeFile(p, Module.FS.readFile(p));
|
||||
Module.FS.unlink(p);
|
||||
}));
|
||||
}
|
||||
|
||||
/*
|
||||
* To prevent input files occupy filesystem without notice,
|
||||
* if `input` is specified in the options, ffmpeg.js cleans these
|
||||
* files for you
|
||||
*/
|
||||
if (del && typeof input === 'string') {
|
||||
await adapter.fs.deleteFile(input);
|
||||
} else if (del && Array.isArray(input)) {
|
||||
await Promise.all(input.map((p) => adapter.fs.deleteFile(p)));
|
||||
}
|
||||
|
||||
res.resolve({ message: `Complete ${args.join(' ')}` });
|
||||
};
|
||||
|
||||
@@ -100,8 +105,8 @@ exports.dispatchHandlers = (packet, send) => {
|
||||
try {
|
||||
({
|
||||
load,
|
||||
ls,
|
||||
syncfs,
|
||||
FS,
|
||||
run,
|
||||
})[packet.action](packet, res);
|
||||
} catch (err) {
|
||||
|
||||
8
src/worker-script/utils/str2ptr.js
Normal file
8
src/worker-script/utils/str2ptr.js
Normal file
@@ -0,0 +1,8 @@
|
||||
module.exports = (Module, s) => {
|
||||
const ptr = Module._malloc((s.length + 1) * Uint8Array.BYTES_PER_ELEMENT);
|
||||
for (let i = 0; i < s.length; i += 1) {
|
||||
Module.setValue(ptr + i, s.charCodeAt(i), 'i8');
|
||||
}
|
||||
Module.setValue(ptr + s.length, 0, 'i8');
|
||||
return ptr;
|
||||
};
|
||||
12
src/worker-script/utils/strList2ptr.js
Normal file
12
src/worker-script/utils/strList2ptr.js
Normal file
@@ -0,0 +1,12 @@
|
||||
const str2ptr = require('./str2ptr');
|
||||
|
||||
module.exports = (Module, strList) => {
|
||||
const listPtr = Module._malloc(strList.length * Uint32Array.BYTES_PER_ELEMENT);
|
||||
|
||||
strList.forEach((s, idx) => {
|
||||
const strPtr = str2ptr(Module, s);
|
||||
Module.setValue(listPtr + (4 * idx), strPtr, 'i32');
|
||||
});
|
||||
|
||||
return listPtr;
|
||||
};
|
||||
Reference in New Issue
Block a user