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

Conversation

m4burns
Copy link
Contributor

@m4burns m4burns commented May 6, 2024

When building an application that uses both an audio worklet and a normal worker, the main module wrapper (a.out.js) calls addEventListener whenever ENVIRONMENT_IS_WASM_WORKER. This flag is set for audio worklets, but addEventListener is not available in the audio worker global scope. The audio worklet load process then fails.

This doesn't occur when using only the audio worklet APIs, because _emscripten_create_wasm_worker__postset doesn't happen. There's already a check to prevent a similar error just above the lines modified by this PR: https://github.com/emscripten-core/emscripten/blob/fa80cf25b8641974d60605b8a24cb7150fd63701/src/library_wasm_worker.js#L112C5-L112C58 so the missing check here seems like an oversight.

Here's a minimal example that reproduces the failure:

#include <emscripten/webaudio.h>
#include <emscripten/wasm_worker.h>
#include <stdio.h>

void run_in_worker()
{
  printf("Hello from Worker!\n");
}

uint8_t audioThreadStack[4096];

EM_BOOL Callback(int numInputs, const AudioSampleFrame *inputs,
                 int numOutputs, AudioSampleFrame *outputs,
                 int numParams, const AudioParamFrame *params,
                 void *userData)
{
  return EM_TRUE;
}

void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL success, void *userData)
{
  if (!success) return;

  int outputChannelCounts[1] = { 1 };
  EmscriptenAudioWorkletNodeCreateOptions options = {
    .numberOfInputs = 0,
    .numberOfOutputs = 1,
    .outputChannelCounts = outputChannelCounts
  };

  EMSCRIPTEN_AUDIO_WORKLET_NODE_T wasmAudioWorklet = emscripten_create_wasm_audio_worklet_node(audioContext, "my-node", &options, &Callback, 0);

  EM_ASM({emscriptenGetAudioObject($0).connect(emscriptenGetAudioObject($1).destination)},
    wasmAudioWorklet, audioContext);
}

void AudioThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL success, void *userData)
{
  if (!success) return;

  WebAudioWorkletProcessorCreateOptions opts = {
    .name = "my-node",
  };
  emscripten_create_wasm_audio_worklet_processor_async(audioContext, &opts, &AudioWorkletProcessorCreated, 0);
}

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, audioThreadStack, sizeof(audioThreadStack), &AudioThreadInitialized, 0);
}

Build with emcc -sAUDIO_WORKLET=1 -sWASM_WORKERS=1 example.cc

@sbc100 sbc100 requested a review from juj May 6, 2024 22:22
@m4burns m4burns force-pushed the fix-audio-worklet-workers-mix branch from 3c4c631 to af002d5 Compare May 6, 2024 22:45
@m4burns m4burns requested a review from sbc100 May 6, 2024 22:56
@sbc100
Copy link
Collaborator

sbc100 commented May 7, 2024

I might be good to add a test for this to test_browser.py (a test that uses audio worklets and wasm workers together).

@m4burns
Copy link
Contributor Author

m4burns commented May 7, 2024

I just added a test that uses audio worklets and workers at the same time. Thanks for the quick reviews!

Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@juj could you take a look too?

@juj
Copy link
Collaborator

juj commented May 7, 2024

Great work on the test. LGTM with a small update.

@m4burns m4burns requested a review from juj May 7, 2024 15:23
@sbc100 sbc100 merged commit b7da6c1 into emscripten-core:main May 7, 2024
kripken added a commit to kripken/emscripten_ that referenced this pull request May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants