|
| 1 | +#include <emscripten/webaudio.h> |
| 2 | +#include <emscripten/wasm_worker.h> |
| 3 | +#include <emscripten/threading.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <assert.h> |
| 7 | + |
| 8 | +// Tests that |
| 9 | +// - audioworklets and workers can be used at the same time. |
| 10 | +// - an audioworklet can emscripten_futex_wake() a waiting worker. |
| 11 | +// - global values can be shared between audioworklets and workers. |
| 12 | + |
| 13 | +int workletToWorkerFutexLocation = 0; |
| 14 | +int workletToWorkerFlag = 0; |
| 15 | + |
| 16 | +void run_in_worker() { |
| 17 | + while (0 == emscripten_futex_wait(&workletToWorkerFutexLocation, 0, 30000)) { |
| 18 | + if (workletToWorkerFlag == 1) { |
| 19 | + printf("Test success\n"); |
| 20 | + break; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | +#ifdef REPORT_RESULT |
| 25 | + REPORT_RESULT(workletToWorkerFlag == 1); |
| 26 | +#endif |
| 27 | +} |
| 28 | + |
| 29 | +// This event will fire on the audio worklet thread. |
| 30 | +void MessageReceivedInAudioWorkletThread() { |
| 31 | + assert(emscripten_current_thread_is_audio_worklet()); |
| 32 | + workletToWorkerFlag = 1; |
| 33 | + emscripten_futex_wake(&workletToWorkerFutexLocation, 1); |
| 34 | +} |
| 35 | + |
| 36 | +void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL success, void *userData) { |
| 37 | + emscripten_audio_worklet_post_function_v(audioContext, MessageReceivedInAudioWorkletThread); |
| 38 | +} |
| 39 | + |
| 40 | +uint8_t wasmAudioWorkletStack[4096]; |
| 41 | + |
| 42 | +int main() { |
| 43 | + emscripten_wasm_worker_t worker = emscripten_malloc_wasm_worker(/*stackSize: */1024); |
| 44 | + emscripten_wasm_worker_post_function_v(worker, run_in_worker); |
| 45 | + |
| 46 | + EMSCRIPTEN_WEBAUDIO_T context = emscripten_create_audio_context(0); |
| 47 | + emscripten_start_wasm_audio_worklet_thread_async(context, wasmAudioWorkletStack, sizeof(wasmAudioWorkletStack), WebAudioWorkletThreadInitialized, 0); |
| 48 | +} |
0 commit comments