Closed Night12138 closed 2 years ago
I fixed this issue by init custom worker stack, just check code below, holp it can help you!
main worker
import createMyModule from './wasm.js';
createMyModule().then(( _m) => {
Module = _m;
const { InitPlayback, wasmModule, wasmMemory, _malloc } = Module; // wasmModule is not export currently, we made some modify to generated code to let wasmModule export
const ptr = InitPlayback(SLICE_LEN, MAX_CHANNEL);
const stack = _malloc(16384); // alloc a stack space for worker use
postMessage({
type: 'SEND_TO_SUB_WORKER',
data: { ptr, wasmModule, wasmMemory, stack },
});
}).catch(e => console.log(e));
custom worker
// ... skip
Module['wasmModule'] = e.data.wasmModule;
Module['wasmMemory'] = e.data.wasmMemory;
Module['buffer'] = Module['wasmMemory'].buffer;
Module['ENVIRONMENT_IS_PTHREAD'] = true;
//if (typeof e.data.urlOrBlob == 'string') {
// importScripts(e.data.urlOrBlob);
//} else {
// var objectUrl = URL.createObjectURL(e.data.urlOrBlob);
// importScripts(objectUrl);
// URL.revokeObjectURL(objectUrl);
//}
createMyModule(Module).then(function (instance) {
Module = instance;
const max = e.data.stack;
const top = max + 16384;
Module._emscripten_stack_set_limits(top, max); // set stack limit
Module.stackRestore(top); // restore stack to worker
setInterval(()=>Module._pullBuffer(128,2), 10); // now main worker can run normally
});
} else if (e.data.cmd === 'run') {
// ... skip
@Night12138 What the value of SLICE_LEN, MAX_CHANNEL, InitPlayback(SLICE_LEN, MAX_CHANNEL) ???
Do u know the right valur of threadInfoStruct
? )
This question seems a bit odd, but it's no doubt that I'm working on it. The reason is that we need to send audio to
AudioWorklet
, which does not support multi-threading, so we useWorker
for audio processing. We found that we can instantiate Wasm by sendingwasmModule
andwasmMemory
to theAudioWorklet
, just according to theworker.js
generated by Emscripten. But we currently have a problem: when theAudioWorklet
tries to modify the data in WasmMemory, it will throw an exception from the mainWorker
:null function or function signature mismatch
, we also try to run this function with asetInterval
in the mainWorker
, it did not throw that exception. At the same time, not onlyAudioWorklet
, we try to inspect theWorker
created by Emscripten, running this function will also cause an exception on the main Worker. So what my question here is: how can I do to access Wasm functions from the derived Worker? Here is some useful reproducible codeC++ a MessageDispatcher, which can run a sub worker function in main worker
C++ init a buffer
main worker
sub worker, same to generated
worker.js
file, load in self way and do inspect at devTools, like runModule._pullBuffer(128, 2)