https://github.com/libsdl-org/SDL/commit/4399b7171543d13ce89739d74ceb397fea562a13
From 4399b7171543d13ce89739d74ceb397fea562a13 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Tue, 11 Jul 2023 17:31:02 -0400
Subject: [PATCH] audio: Generalize how backends can lookup an SDL_AudioDevice.
---
src/audio/SDL_audio.c | 18 ++++++++++++++----
src/audio/SDL_sysaudio.h | 3 +++
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index f08531eb0b40..cdf7a164aca7 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -988,7 +988,7 @@ static SDL_AudioDevice *ObtainPhysicalAudioDevice(SDL_AudioDeviceID devid)
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()) {
SDL_SetError("Audio subsystem is not initialized");
@@ -999,7 +999,7 @@ SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
SDL_AudioDevice *dev = NULL;
for (dev = current_audio.output_devices; dev != NULL; dev = dev->next) {
- if (dev->handle == handle) { // found it?
+ if (callback(dev, userdata)) { // found it?
break;
}
}
@@ -1007,7 +1007,7 @@ SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
if (!dev) {
// !!! FIXME: code duplication, from above.
for (dev = current_audio.capture_devices; dev != NULL; dev = dev->next) {
- if (dev->handle == handle) { // found it?
+ if (callback(dev, userdata)) { // found it?
break;
}
}
@@ -1016,7 +1016,7 @@ SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
SDL_UnlockRWLock(current_audio.device_list_lock);
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.
@@ -1024,6 +1024,16 @@ SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
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)
{
SDL_AudioDevice *device = ObtainPhysicalAudioDevice(devid);
diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h
index 9d413a6cd9a1..f2016fe7536f 100644
--- a/src/audio/SDL_sysaudio.h
+++ b/src/audio/SDL_sysaudio.h
@@ -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.
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.
extern void SDL_UpdatedAudioDeviceFormat(SDL_AudioDevice *device);