Skip to content

Avoid calling addEventListener in audio worklet #21897

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 4 commits into from
May 7, 2024
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
7 changes: 6 additions & 1 deletion src/library_wasm_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ addToLibrary({
#endif
],
_emscripten_create_wasm_worker__postset: `
if (ENVIRONMENT_IS_WASM_WORKER) {
if (ENVIRONMENT_IS_WASM_WORKER
// AudioWorkletGlobalScope does not contain addEventListener
#if AUDIO_WORKLET
&& !ENVIRONMENT_IS_AUDIO_WORKLET
#endif
) {
_wasmWorkers[0] = this;
addEventListener("message", _wasmWorkerAppendToQueue);
}`,
Expand Down
7 changes: 7 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5479,6 +5479,13 @@ def test_audio_worklet(self, args):
self.skipTest('https://github.com/emscripten-core/emscripten/issues/19161')
self.btest_exit('webaudio/audioworklet.c', args=['-sAUDIO_WORKLET', '-sWASM_WORKERS'] + args)

# Tests that audioworklets and workers can be used at the same time
@parameterized({
'': ([],),
})
def test_audio_worklet_worker(self, args):
self.btest('webaudio/audioworklet_worker.c', args=['-sAUDIO_WORKLET', '-sWASM_WORKERS'] + args, expected='1')

# Tests that posting functions between the main thread and the audioworklet thread works
@parameterized({
'': ([],),
Expand Down
48 changes: 48 additions & 0 deletions test/webaudio/audioworklet_worker.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <emscripten/webaudio.h>
#include <emscripten/wasm_worker.h>
#include <emscripten/threading.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

// Tests that
// - audioworklets and workers can be used at the same time.
// - an audioworklet can emscripten_futex_wake() a waiting worker.
// - global values can be shared between audioworklets and workers.

int workletToWorkerFutexLocation = 0;
int workletToWorkerFlag = 0;

void run_in_worker() {
while (0 == emscripten_futex_wait(&workletToWorkerFutexLocation, 0, 30000)) {
if (workletToWorkerFlag == 1) {
printf("Test success\n");
break;
}
}

#ifdef REPORT_RESULT
REPORT_RESULT(workletToWorkerFlag == 1);
#endif
}

// This event will fire on the audio worklet thread.
void MessageReceivedInAudioWorkletThread() {
assert(emscripten_current_thread_is_audio_worklet());
workletToWorkerFlag = 1;
emscripten_futex_wake(&workletToWorkerFutexLocation, 1);
}

void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL success, void *userData) {
emscripten_audio_worklet_post_function_v(audioContext, MessageReceivedInAudioWorkletThread);
}

uint8_t wasmAudioWorkletStack[4096];

int main() {
emscripten_wasm_worker_t worker = emscripten_malloc_wasm_worker(/*stackSize: */1024);
emscripten_wasm_worker_post_function_v(worker, run_in_worker);

EMSCRIPTEN_WEBAUDIO_T context = emscripten_create_audio_context(0);
emscripten_start_wasm_audio_worklet_thread_async(context, wasmAudioWorkletStack, sizeof(wasmAudioWorkletStack), WebAudioWorkletThreadInitialized, 0);
}