Browse Source

audio: Generalize how backends can lookup an SDL_AudioDevice.

Ryan C. Gordon 1 year ago
parent
commit
4399b71715
2 changed files with 17 additions and 4 deletions
  1. 14 4
      src/audio/SDL_audio.c
  2. 3 0
      src/audio/SDL_sysaudio.h

+ 14 - 4
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);

+ 3 - 0
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);