audio: Fix device refcounting vs ProvidesOwnCallbackThread backends.

This commit is contained in:
Ryan C. Gordon 2023-10-16 20:17:04 -04:00
parent c6f08c2553
commit b733adb503
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
5 changed files with 13 additions and 4 deletions

View File

@ -352,7 +352,7 @@ static void DestroyPhysicalAudioDevice(SDL_AudioDevice *device)
} }
// Don't hold the device lock when calling this, as we may destroy the device! // Don't hold the device lock when calling this, as we may destroy the device!
static void UnrefPhysicalAudioDevice(SDL_AudioDevice *device) void UnrefPhysicalAudioDevice(SDL_AudioDevice *device)
{ {
if (SDL_AtomicDecRef(&device->refcount)) { if (SDL_AtomicDecRef(&device->refcount)) {
// take it out of the device list. // take it out of the device list.
@ -365,7 +365,7 @@ static void UnrefPhysicalAudioDevice(SDL_AudioDevice *device)
} }
} }
static void RefPhysicalAudioDevice(SDL_AudioDevice *device) void RefPhysicalAudioDevice(SDL_AudioDevice *device)
{ {
SDL_AtomicIncRef(&device->refcount); SDL_AtomicIncRef(&device->refcount);
} }
@ -861,6 +861,7 @@ static void MixFloat32Audio(float *dst, const float *src, const int buffer_size)
void SDL_OutputAudioThreadSetup(SDL_AudioDevice *device) void SDL_OutputAudioThreadSetup(SDL_AudioDevice *device)
{ {
SDL_assert(!device->iscapture); SDL_assert(!device->iscapture);
RefPhysicalAudioDevice(device); // unref'd when the audio thread terminates (ProvidesOwnCallbackThread implementations should call SDL_AudioThreadFinalize appropriately).
current_audio.impl.ThreadInit(device); current_audio.impl.ThreadInit(device);
} }
@ -1010,6 +1011,7 @@ static int SDLCALL OutputAudioThread(void *devicep) // thread entry point
void SDL_CaptureAudioThreadSetup(SDL_AudioDevice *device) void SDL_CaptureAudioThreadSetup(SDL_AudioDevice *device)
{ {
SDL_assert(device->iscapture); SDL_assert(device->iscapture);
RefPhysicalAudioDevice(device); // unref'd when the audio thread terminates (ProvidesOwnCallbackThread implementations should call SDL_AudioThreadFinalize appropriately).
current_audio.impl.ThreadInit(device); current_audio.impl.ThreadInit(device);
} }
@ -1495,8 +1497,6 @@ static int OpenPhysicalAudioDevice(SDL_AudioDevice *device, const SDL_AudioSpec
} }
} }
RefPhysicalAudioDevice(device); // unref'd when the audio thread terminates (ProvidesOwnCallbackThread implementations should call SDL_AudioThreadFinalize appropriately).
return 0; return 0;
} }

View File

@ -106,6 +106,9 @@ extern void SDL_UpdatedAudioDeviceFormat(SDL_AudioDevice *device);
// Backends can call this to get a standardized name for a thread to power a specific audio device. // Backends can call this to get a standardized name for a thread to power a specific audio device.
extern char *SDL_GetAudioThreadName(SDL_AudioDevice *device, char *buf, size_t buflen); extern char *SDL_GetAudioThreadName(SDL_AudioDevice *device, char *buf, size_t buflen);
// Backends can call these to change a device's refcount.
extern void RefPhysicalAudioDevice(SDL_AudioDevice *device);
extern void UnrefPhysicalAudioDevice(SDL_AudioDevice *device);
// These functions are the heart of the audio threads. Backends can call them directly if they aren't using the SDL-provided thread. // These functions are the heart of the audio threads. Backends can call them directly if they aren't using the SDL-provided thread.
extern void SDL_OutputAudioThreadSetup(SDL_AudioDevice *device); extern void SDL_OutputAudioThreadSetup(SDL_AudioDevice *device);

View File

@ -181,6 +181,8 @@ static int EMSCRIPTENAUDIO_OpenDevice(SDL_AudioDevice *device)
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
RefPhysicalAudioDevice(device); // CloseDevice will always unref this through SDL_AudioThreadFinalize, even if we failed to start the thread.
// limit to native freq // limit to native freq
device->spec.freq = EM_ASM_INT({ return Module['SDL3'].audioContext.sampleRate; }); device->spec.freq = EM_ASM_INT({ return Module['SDL3'].audioContext.sampleRate; });

View File

@ -112,6 +112,8 @@ static int HAIKUAUDIO_OpenDevice(SDL_AudioDevice *device)
} }
SDL_zerop(device->hidden); SDL_zerop(device->hidden);
RefPhysicalAudioDevice(device); // CloseDevice will always unref this through SDL_AudioThreadFinalize, even if we failed to start the thread.
// Parse the audio format and fill the Be raw audio format // Parse the audio format and fill the Be raw audio format
media_raw_audio_format format; media_raw_audio_format format;
SDL_zero(format); SDL_zero(format);

View File

@ -300,6 +300,8 @@ static int JACK_OpenDevice(SDL_AudioDevice *device)
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
RefPhysicalAudioDevice(device); // CloseDevice will always unref this through SDL_AudioThreadFinalize, even if we failed to start the thread.
client = JACK_jack_client_open(GetJackAppName(), JackNoStartServer, &status, NULL); client = JACK_jack_client_open(GetJackAppName(), JackNoStartServer, &status, NULL);
device->hidden->client = client; device->hidden->client = client;
if (client == NULL) { if (client == NULL) {