-
-
Notifications
You must be signed in to change notification settings - Fork 363
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
emscripten threading #855
Comments
That first note at the top of that article isn't something I find appealing. I'm not much of a web person and I don't know anything about COOP or COEP so not entirely sure what the implications are on that front, but miniaudio needs to "just work", so certainly enabling pthreads wholesale without an option to disable it sounds bad considering that note. When using pthreads with Emscripten, is it using actual real threads, or is it just emulating it? If it's just emulating it, what are the tangible real-world benefits you'd get out of it? Looking at that article they make it sound like it's real threads? |
Thanks for those questions. Looking into it a bit, this is what I understand:
So for miniaudio, I think if the emscripten builds used pthreads, everything "just works". And if anyone wants to do the extra work to compile with This blog was helpful https://unlimited3d.wordpress.com/2021/12/21/webassembly-and-multi-threading/ including the sections on "Cross-origin isolation headers" / "Isolating multi-threaded WebAssembly – what for?" to motivate why COOP/COEP are involved. |
If I'm reading the Emscripten documentation correctly, it looks like |
This seems to be the case. Building for the Emscripten worklets API with pthreads API enabled I can see an |
@teropa is the performance ok for you? My miniaudio dataCallback does some heavy lifting but curiously I didn't notice any performance difference with pthreads enabled. I've been experimenting with this in a sokol project. Also my hack for the http-server the project uses I modify
I haven't explored all the considerations in https://emscripten.org/docs/porting/pthreads.html and there's other flags like PTHREAD_POOL_SIZE https://emscripten.org/docs/tools_reference/settings_reference.html#pthread-pool-size I'm also curious about AudioWorklets per
Which sounds like audio will be in another thread (without pthread support, and no change if including pthread support), but my program freezes at runtime when I try including the |
@teropa It surprised me to read that you have a /* The Emscripten build cannot use threads. */
#if defined(MA_EMSCRIPTEN)
{
resourceManagerConfig.jobThreadCount = 0;
resourceManagerConfig.flags |= MA_RESOURCE_MANAGER_FLAG_NO_THREADING;
}
#endif Are you using |
for me I'm using
|
@digitalsignalperson Performance seems good, though I've yet to measure it systematically. My audio processing is fairly light, and this translates to the audio thread being about 98% idle most of the time. The job pthread where I'm doing opus decoding looks much busier though. Compilation flags: The pthread pool size could probably be just 1, but I'm using an additional one for my own purposes. The trickiest bit was finding out I had to increase And yeah, we do also have COOP/COEP headers enabled. I assume the shared memory via
Right, with worklets enabled audio will always be on the Web Audio thread created by the browser, not a pthread created by emscripten. I was happy to find that's all pretty transparent with the Emscripten Audio Worklet support though. It creates the audio context, thread, and worklet, and I didn't really have to think about it. I haven't experienced any freezes either. I'm not doing any capture, so I assume that also simplifies things somewhat. |
@mackron Right, yes, I'm wiring up my own |
Looking at the code, it looks like I disable threading in |
I don't think I have it working, but I'm still learning the ropes of how to actually debug things in the browser. Is the worklet part required? With just If I try to pause execution in this thread, the button greys out and says "Waiting for next execution". In the debug build without If I try in addition to enable audio worklets like with
For other things on the miniaudio side, I saw one |
@digitalsignalperson Try doing a fresh sync of the dev branch and try again. It might be fixed with this PR #888. |
@mackron on the dev branch now when I use the audio worklet flags that does seem to resolve the assert fail, but my app renders one frame and then is frozen without any error messages and not responding to inputs. I can get the devtools debugger to pause seemingly only in a |
@digitalsignalperson Does your app work if you do pure playback (no microphone activation / capture). I haven't tested that side of things and I know capture brings in a whole bunch of additional machinery on the web. Might help narrow things down. |
For our case (playback only, managed resource manager, audio worklets enabled) everything seems to be running smoothly with pthreads with the latest from dev branch. Have tested on current versions of Chrome, Firefox, Safari (Mac+iOS). On Safari, especially on iOS, I'm seeing some memory issues triggered by pthreads and shared memory but I don't believe that's a miniaudio problem: emscripten-core/emscripten#19374 |
@teropa great suggestion thank you. My app is entirely audio capture. To test with this I did a hack to my init function #ifndef NO_CAPTURE_TEST
ma_device_config deviceConfig = ma_device_config_init(ma_device_type_capture);
deviceConfig.capture.format = ma_format_f32;
deviceConfig.capture.channels = 2;
#else
ma_device_config deviceConfig = ma_device_config_init(ma_device_type_playback);
deviceConfig.playback.format = ma_format_f32;
deviceConfig.playback.channels = 2;
#endif and in my dataCallback void dataCallback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 num_frames) {
(void) pOutput; // unused
#ifndef NO_CAPTURE_TEST
float* in_buffer = (float*)pInput;
unsigned channels = pDevice->capture.channels;
#else
(void) pInput;
float in[2048];
float* in_buffer = in;
unsigned channels = 1;
for (int i = 0; i < 2048; i++) {
in[i] = float(i)/512 - 1.0f;
}
#endif So now I'm faking capture with a simple triangle wave, meanwhile it's operating in playback mode. When I do this, I'm still seeing no performance improvement with or without |
Hi, I'm curious what the challenges to move forward with emscripten threading.
As of today:
from https://emscripten.org/docs/porting/pthreads.html
The text was updated successfully, but these errors were encountered: