Set the number of audio devices to 0 if audio hasn't been initialized

This updates GetAudioDevices() to have the same behavior as SDL_GetJoysticks() where the return value will only be NULL if there is an error. Returning no devices will return a valid array containing NULL.
This commit is contained in:
Sam Lantinga
2024-01-16 12:15:13 -08:00
parent be0ba78c56
commit 9408299bad

View File

@@ -1257,22 +1257,17 @@ static int SDLCALL CaptureAudioThread(void *devicep) // thread entry point
}
static SDL_AudioDeviceID *GetAudioDevices(int *reqcount, SDL_bool iscapture)
static SDL_AudioDeviceID *GetAudioDevices(int *count, SDL_bool iscapture)
{
if (!SDL_GetCurrentAudioDriver()) {
SDL_SetError("Audio subsystem is not initialized");
return NULL;
}
SDL_AudioDeviceID *retval = NULL;
int num_devices = 0;
if (SDL_GetCurrentAudioDriver()) {
SDL_LockRWLockForReading(current_audio.device_hash_lock);
int num_devices = SDL_AtomicGet(iscapture ? &current_audio.capture_device_count : &current_audio.output_device_count);
if (num_devices > 0) {
{
num_devices = SDL_AtomicGet(iscapture ? &current_audio.capture_device_count : &current_audio.output_device_count);
retval = (SDL_AudioDeviceID *) SDL_malloc((num_devices + 1) * sizeof (SDL_AudioDeviceID));
if (!retval) {
num_devices = 0;
} else {
if (retval) {
int devs_seen = 0;
const void *key;
const void *value;
@@ -1291,14 +1286,22 @@ static SDL_AudioDeviceID *GetAudioDevices(int *reqcount, SDL_bool iscapture)
SDL_assert(devs_seen == num_devices);
retval[devs_seen] = 0; // null-terminated.
} else {
SDL_OutOfMemory();
}
}
SDL_UnlockRWLock(current_audio.device_hash_lock);
if (reqcount) {
*reqcount = num_devices;
} else {
SDL_SetError("Audio subsystem is not initialized");
}
if (count) {
if (retval) {
*count = num_devices;
} else {
*count = 0;
}
}
return retval;
}