dummyaudio: single-threaded Emscripten support.
On Emscripten without pthreads, this would fail because SDL can't spin the audio device thread, and the dummy backend didn't manage a thread itself. With this patch, we use setInterval to fire the usual audio thread iterators between iterations of the Emscripten mainloop on the main thread. Fixes #10573.
This commit is contained in:
@@ -25,6 +25,10 @@
|
|||||||
#include "../SDL_sysaudio.h"
|
#include "../SDL_sysaudio.h"
|
||||||
#include "SDL_dummyaudio.h"
|
#include "SDL_dummyaudio.h"
|
||||||
|
|
||||||
|
#if defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__)
|
||||||
|
#include <emscripten/emscripten.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
static int DUMMYAUDIO_WaitDevice(SDL_AudioDevice *device)
|
static int DUMMYAUDIO_WaitDevice(SDL_AudioDevice *device)
|
||||||
{
|
{
|
||||||
SDL_Delay(device->hidden->io_delay);
|
SDL_Delay(device->hidden->io_delay);
|
||||||
@@ -54,12 +58,30 @@ static int DUMMYAUDIO_OpenDevice(SDL_AudioDevice *device)
|
|||||||
device->hidden->io_delay = (Uint32)SDL_round(device->hidden->io_delay * scale);
|
device->hidden->io_delay = (Uint32)SDL_round(device->hidden->io_delay * scale);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// on Emscripten without threads, we just fire a repeating timer to consume audio.
|
||||||
|
#if defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__)
|
||||||
|
MAIN_THREAD_EM_ASM({
|
||||||
|
var a = Module['SDL3'].dummy_audio;
|
||||||
|
if (a.timers[$0] !== undefined) { clearInterval(a.timers[$0]); }
|
||||||
|
a.timers[$0] = setInterval(function() { dynCall('vi', $3, [$4]); }, ($1 / $2) * 1000);
|
||||||
|
}, device->recording ? 1 : 0, device->sample_frames, device->spec.freq, device->recording ? SDL_RecordingAudioThreadIterate : SDL_PlaybackAudioThreadIterate, device);
|
||||||
|
#endif
|
||||||
|
|
||||||
return 0; // we're good; don't change reported device format.
|
return 0; // we're good; don't change reported device format.
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DUMMYAUDIO_CloseDevice(SDL_AudioDevice *device)
|
static void DUMMYAUDIO_CloseDevice(SDL_AudioDevice *device)
|
||||||
{
|
{
|
||||||
if (device->hidden) {
|
if (device->hidden) {
|
||||||
|
// on Emscripten without threads, we just fire a repeating timer to consume audio.
|
||||||
|
#if defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__)
|
||||||
|
MAIN_THREAD_EM_ASM({
|
||||||
|
var a = Module['SDL3'].dummy_audio;
|
||||||
|
if (a.timers[$0] !== undefined) { clearInterval(a.timers[$0]); }
|
||||||
|
a.timers[$0] = undefined;
|
||||||
|
}, device->recording ? 1 : 0);
|
||||||
|
#endif
|
||||||
SDL_free(device->hidden->mixbuf);
|
SDL_free(device->hidden->mixbuf);
|
||||||
SDL_free(device->hidden);
|
SDL_free(device->hidden);
|
||||||
device->hidden = NULL;
|
device->hidden = NULL;
|
||||||
@@ -91,6 +113,20 @@ static bool DUMMYAUDIO_Init(SDL_AudioDriverImpl *impl)
|
|||||||
impl->OnlyHasDefaultRecordingDevice = true;
|
impl->OnlyHasDefaultRecordingDevice = true;
|
||||||
impl->HasRecordingSupport = true;
|
impl->HasRecordingSupport = true;
|
||||||
|
|
||||||
|
// on Emscripten without threads, we just fire a repeating timer to consume audio.
|
||||||
|
#if defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(__EMSCRIPTEN_PTHREADS__)
|
||||||
|
MAIN_THREAD_EM_ASM({
|
||||||
|
if (typeof(Module['SDL3']) === 'undefined') {
|
||||||
|
Module['SDL3'] = {};
|
||||||
|
}
|
||||||
|
Module['SDL3'].dummy_audio = {};
|
||||||
|
Module['SDL3'].dummy_audio.timers = [];
|
||||||
|
Module['SDL3'].dummy_audio.timers[0] = undefined;
|
||||||
|
Module['SDL3'].dummy_audio.timers[1] = undefined;
|
||||||
|
});
|
||||||
|
impl->ProvidesOwnCallbackThread = true;
|
||||||
|
#endif
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user