audio: Generalize how backends can lookup an SDL_AudioDevice.

This commit is contained in:
Ryan C. Gordon
2023-07-11 17:31:02 -04:00
parent 2fb122fe46
commit 4399b71715
2 changed files with 17 additions and 4 deletions

View File

@@ -988,7 +988,7 @@ static SDL_AudioDevice *ObtainPhysicalAudioDevice(SDL_AudioDeviceID devid)
return dev; return dev;
} }
SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle) SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByCallback(SDL_bool (*callback)(SDL_AudioDevice *device, void *userdata), void *userdata)
{ {
if (!SDL_GetCurrentAudioDriver()) { if (!SDL_GetCurrentAudioDriver()) {
SDL_SetError("Audio subsystem is not initialized"); SDL_SetError("Audio subsystem is not initialized");
@@ -999,7 +999,7 @@ SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
SDL_AudioDevice *dev = NULL; SDL_AudioDevice *dev = NULL;
for (dev = current_audio.output_devices; dev != NULL; dev = dev->next) { for (dev = current_audio.output_devices; dev != NULL; dev = dev->next) {
if (dev->handle == handle) { // found it? if (callback(dev, userdata)) { // found it?
break; break;
} }
} }
@@ -1007,7 +1007,7 @@ SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
if (!dev) { if (!dev) {
// !!! FIXME: code duplication, from above. // !!! FIXME: code duplication, from above.
for (dev = current_audio.capture_devices; dev != NULL; dev = dev->next) { for (dev = current_audio.capture_devices; dev != NULL; dev = dev->next) {
if (dev->handle == handle) { // found it? if (callback(dev, userdata)) { // found it?
break; break;
} }
} }
@@ -1016,7 +1016,7 @@ SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
SDL_UnlockRWLock(current_audio.device_list_lock); SDL_UnlockRWLock(current_audio.device_list_lock);
if (!dev) { if (!dev) {
SDL_SetError("Device handle not found"); SDL_SetError("Device not found");
} }
SDL_assert(!SDL_AtomicGet(&dev->condemned)); // shouldn't be in the list if pending deletion. SDL_assert(!SDL_AtomicGet(&dev->condemned)); // shouldn't be in the list if pending deletion.
@@ -1024,6 +1024,16 @@ SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
return dev; return dev;
} }
static SDL_bool TestDeviceHandleCallback(SDL_AudioDevice *device, void *handle)
{
return device->handle == handle;
}
SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
{
return SDL_FindPhysicalAudioDeviceByCallback(TestDeviceHandleCallback, handle);
}
char *SDL_GetAudioDeviceName(SDL_AudioDeviceID devid) char *SDL_GetAudioDeviceName(SDL_AudioDeviceID devid)
{ {
SDL_AudioDevice *device = ObtainPhysicalAudioDevice(devid); SDL_AudioDevice *device = ObtainPhysicalAudioDevice(devid);

View File

@@ -88,6 +88,9 @@ extern void SDL_DefaultAudioDeviceChanged(SDL_AudioDevice *new_default_device);
// Find the SDL_AudioDevice associated with the handle supplied to SDL_AddAudioDevice. NULL if not found. DOES NOT LOCK THE DEVICE. // Find the SDL_AudioDevice associated with the handle supplied to SDL_AddAudioDevice. NULL if not found. DOES NOT LOCK THE DEVICE.
extern SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle); extern SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle);
// Find an SDL_AudioDevice, selected by a callback. NULL if not found. DOES NOT LOCK THE DEVICE.
extern SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByCallback(SDL_bool (*callback)(SDL_AudioDevice *device, void *userdata), void *userdata);
// Backends should call this if they change the device format, channels, freq, or sample_frames to keep other state correct. // Backends should call this if they change the device format, channels, freq, or sample_frames to keep other state correct.
extern void SDL_UpdatedAudioDeviceFormat(SDL_AudioDevice *device); extern void SDL_UpdatedAudioDeviceFormat(SDL_AudioDevice *device);