SDL: audio: backends now "find" instead of "obtain" devices by handle.

https://github.com/libsdl-org/SDL/commit/2fb122fe4690a6cd3c6f8508d73b329760cd14f5

From 2fb122fe4690a6cd3c6f8508d73b329760cd14f5 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Tue, 11 Jul 2023 13:59:54 -0400
Subject: [PATCH] audio: backends now "find" instead of "obtain" devices by
 handle.

Every single case of this didn't want the device locked, so just looking
it up without having to immediately unlock it afterwards is better here.

Often these devices are passed on to other functions that want to lock them
themselves anyhow (disconnects, default changes, etc).
---
 src/audio/SDL_audio.c                 |  8 +++-----
 src/audio/SDL_sysaudio.h              |  4 ++--
 src/audio/alsa/SDL_alsa_audio.c       |  8 +-------
 src/audio/coreaudio/SDL_coreaudio.m   | 15 ++++-----------
 src/audio/pipewire/SDL_pipewire.c     | 12 ++----------
 src/audio/pulseaudio/SDL_pulseaudio.c | 15 ++++-----------
 6 files changed, 16 insertions(+), 46 deletions(-)

diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 6ddaafe76bfc..f08531eb0b40 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_ObtainPhysicalAudioDeviceByHandle(void *handle)
+SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
 {
     if (!SDL_GetCurrentAudioDriver()) {
         SDL_SetError("Audio subsystem is not initialized");
@@ -1000,8 +1000,6 @@ SDL_AudioDevice *SDL_ObtainPhysicalAudioDeviceByHandle(void *handle)
     SDL_AudioDevice *dev = NULL;
     for (dev = current_audio.output_devices; dev != NULL; dev = dev->next) {
         if (dev->handle == handle) {  // found it?
-            SDL_LockMutex(dev->lock);  // caller must unlock.
-            SDL_assert(!SDL_AtomicGet(&dev->condemned));  // shouldn't be in the list if pending deletion.
             break;
         }
     }
@@ -1010,8 +1008,6 @@ SDL_AudioDevice *SDL_ObtainPhysicalAudioDeviceByHandle(void *handle)
         // !!! FIXME: code duplication, from above.
         for (dev = current_audio.capture_devices; dev != NULL; dev = dev->next) {
             if (dev->handle == handle) {  // found it?
-                SDL_LockMutex(dev->lock);  // caller must unlock.
-                SDL_assert(!SDL_AtomicGet(&dev->condemned));  // shouldn't be in the list if pending deletion.
                 break;
             }
         }
@@ -1023,6 +1019,8 @@ SDL_AudioDevice *SDL_ObtainPhysicalAudioDeviceByHandle(void *handle)
         SDL_SetError("Device handle not found");
     }
 
+    SDL_assert(!SDL_AtomicGet(&dev->condemned));  // shouldn't be in the list if pending deletion.
+
     return dev;
 }
 
diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h
index 1f018887c665..9d413a6cd9a1 100644
--- a/src/audio/SDL_sysaudio.h
+++ b/src/audio/SDL_sysaudio.h
@@ -85,8 +85,8 @@ extern void SDL_AudioDeviceDisconnected(SDL_AudioDevice *device);
 // Backends should call this if the system default device changes.
 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. Locks the device! You must unlock!!
-extern SDL_AudioDevice *SDL_ObtainPhysicalAudioDeviceByHandle(void *handle);
+// 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);
 
 // 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);
diff --git a/src/audio/alsa/SDL_alsa_audio.c b/src/audio/alsa/SDL_alsa_audio.c
index 4703eee4c5d2..9acab94b817a 100644
--- a/src/audio/alsa/SDL_alsa_audio.c
+++ b/src/audio/alsa/SDL_alsa_audio.c
@@ -880,13 +880,7 @@ static void ALSA_HotplugIteration(SDL_bool *has_default_output, SDL_bool *has_de
         for (ALSA_Device *dev = unseen; dev; dev = next) {
             /*printf("ALSA: removing usb %s device '%s'\n", dev->iscapture ? "capture" : "output", dev->name);*/
             next = dev->next;
-
-            SDL_AudioDevice *device = SDL_ObtainPhysicalAudioDeviceByHandle(dev->name);
-            if (device) {
-                SDL_UnlockMutex(device->lock);  // AudioDeviceDisconnected will relock and verify it's still in the list, but in case this is destroyed, unlock now.
-                SDL_AudioDeviceDisconnected(device);
-            }
-
+            SDL_AudioDeviceDisconnected(SDL_FindPhysicalAudioDeviceByHandle(dev->name));
             SDL_free(dev->name);
             SDL_free(dev);
         }
diff --git a/src/audio/coreaudio/SDL_coreaudio.m b/src/audio/coreaudio/SDL_coreaudio.m
index 44ca6a846d0e..1a666af292af 100644
--- a/src/audio/coreaudio/SDL_coreaudio.m
+++ b/src/audio/coreaudio/SDL_coreaudio.m
@@ -118,9 +118,8 @@ static void RefreshPhysicalDevices(void)
 
     const UInt32 total_devices = (UInt32) (size / sizeof(AudioDeviceID));
     for (UInt32 i = 0; i < total_devices; i++) {
-        SDL_AudioDevice *device = SDL_ObtainPhysicalAudioDeviceByHandle((void *)((size_t)devs[i]));
+        SDL_AudioDevice *device = SDL_FindPhysicalAudioDeviceByHandle((void *)((size_t)devs[i]));
         if (device) {
-            SDL_UnlockMutex(device->lock);
             devs[i] = 0;  // The system and SDL both agree it's already here, don't check it again.
         }
     }
@@ -235,11 +234,7 @@ static OSStatus DefaultAudioDeviceChangedNotification(AudioObjectID inObjectID,
     AudioDeviceID devid;
     Uint32 size = sizeof(devid);
     if (AudioObjectGetPropertyData(inObjectID, addr, 0, NULL, &size, &devid) == noErr) {
-        SDL_AudioDevice *device = SDL_ObtainPhysicalAudioDeviceByHandle((void *)((size_t)devid));
-        if (device) {
-            SDL_UnlockMutex(device->lock);
-            SDL_DefaultAudioDeviceChanged(device);
-        }
+        SDL_DefaultAudioDeviceChanged(SDL_FindPhysicalAudioDeviceByHandle((void *)((size_t)devid)));
     }
     return noErr;
 }
@@ -274,9 +269,8 @@ static void COREAUDIO_DetectDevices(SDL_AudioDevice **default_output, SDL_AudioD
 
     size = sizeof(AudioDeviceID);
     if (AudioObjectGetPropertyData(kAudioObjectSystemObject, &default_output_device_address, 0, NULL, &size, &devid) == noErr) {
-        SDL_AudioDevice *device = SDL_ObtainPhysicalAudioDeviceByHandle((void *)((size_t)devid));
+        SDL_AudioDevice *device = SDL_FindPhysicalAudioDeviceByHandle((void *)((size_t)devid));
         if (device) {
-            SDL_UnlockMutex(device->lock);
             *default_output = device;
         }
     }
@@ -284,9 +278,8 @@ static void COREAUDIO_DetectDevices(SDL_AudioDevice **default_output, SDL_AudioD
 
     size = sizeof(AudioDeviceID);
     if (AudioObjectGetPropertyData(kAudioObjectSystemObject, &default_input_device_address, 0, NULL, &size, &devid) == noErr) {
-        SDL_AudioDevice *device = SDL_ObtainPhysicalAudioDeviceByHandle((void *)((size_t)devid));
+        SDL_AudioDevice *device = SDL_FindPhysicalAudioDeviceByHandle((void *)((size_t)devid));
         if (device) {
-            SDL_UnlockMutex(device->lock);
             *default_capture = device;
         }
     }
diff --git a/src/audio/pipewire/SDL_pipewire.c b/src/audio/pipewire/SDL_pipewire.c
index 170b6d746cb5..419fbfcb8667 100644
--- a/src/audio/pipewire/SDL_pipewire.c
+++ b/src/audio/pipewire/SDL_pipewire.c
@@ -327,11 +327,7 @@ static void io_list_remove(Uint32 id)
             spa_list_remove(&n->link);
 
             if (hotplug_events_enabled) {
-                SDL_AudioDevice *device = SDL_ObtainPhysicalAudioDeviceByHandle(PW_ID_TO_HANDLE(id));
-                if (device) {
-                    SDL_UnlockMutex(device->lock);  // AudioDeviceDisconnected will relock and verify it's still in the list, but in case this is destroyed, unlock now.
-                    SDL_AudioDeviceDisconnected(device);
-                }
+                SDL_AudioDeviceDisconnected(SDL_FindPhysicalAudioDeviceByHandle(PW_ID_TO_HANDLE(id)));
             }
 
             SDL_free(n);
@@ -614,11 +610,7 @@ static void change_default_device(const char *path)
         struct io_node *n, *temp;
         spa_list_for_each_safe (n, temp, &hotplug_io_list, link) {
             if (SDL_strcmp(n->path, path) == 0) {
-                SDL_AudioDevice *device = SDL_ObtainPhysicalAudioDeviceByHandle(PW_ID_TO_HANDLE(n->id));
-                if (device) {
-                    SDL_UnlockMutex(device->lock);
-                    SDL_DefaultAudioDeviceChanged(device);
-                }
+                SDL_DefaultAudioDeviceChanged(SDL_FindPhysicalAudioDeviceByHandle(PW_ID_TO_HANDLE(n->id)));
                 return; // found it, we're done.
             }
         }
diff --git a/src/audio/pulseaudio/SDL_pulseaudio.c b/src/audio/pulseaudio/SDL_pulseaudio.c
index 50d2072e79c3..f0c33d03f250 100644
--- a/src/audio/pulseaudio/SDL_pulseaudio.c
+++ b/src/audio/pulseaudio/SDL_pulseaudio.c
@@ -825,11 +825,7 @@ static void HotplugCallback(pa_context *c, pa_subscription_event_type_t t, uint3
             PULSEAUDIO_pa_operation_unref(PULSEAUDIO_pa_context_get_source_info_by_index(pulseaudio_context, idx, SourceInfoCallback, (void *)((intptr_t)added)));
         } else if (removed && (sink || source)) {
             // removes we can handle just with the device index.
-            SDL_AudioDevice *device = SDL_ObtainPhysicalAudioDeviceByHandle((void *)((intptr_t)idx + 1));
-            if (device) {
-                SDL_UnlockMutex(device->lock);  // AudioDeviceDisconnected will relock and verify it's still in the list, but in case this is destroyed, unlock now.
-                SDL_AudioDeviceDisconnected(device);
-            }
+            SDL_AudioDeviceDisconnected(SDL_FindPhysicalAudioDeviceByHandle((void *)((intptr_t)idx + 1)));
         }
     }
     PULSEAUDIO_pa_threaded_mainloop_signal(pulseaudio_threaded_mainloop, 0);
@@ -838,9 +834,8 @@ static void HotplugCallback(pa_context *c, pa_subscription_event_type_t t, uint3
 static void CheckDefaultDevice(uint32_t *prev_default, uint32_t new_default)
 {
     if (*prev_default != new_default) {
-        SDL_AudioDevice *device = SDL_ObtainPhysicalAudioDeviceByHandle((void *)((intptr_t)new_default + 1));
+        SDL_AudioDevice *device = SDL_FindPhysicalAudioDeviceByHandle((void *)((intptr_t)new_default + 1));
         if (device) {
-            SDL_UnlockMutex(device->lock);
             *prev_default = new_default;
             SDL_DefaultAudioDeviceChanged(device);
         }
@@ -883,15 +878,13 @@ static void PULSEAUDIO_DetectDevices(SDL_AudioDevice **default_output, SDL_Audio
     PULSEAUDIO_pa_threaded_mainloop_unlock(pulseaudio_threaded_mainloop);
 
     SDL_AudioDevice *device;
-    device = SDL_ObtainPhysicalAudioDeviceByHandle((void *)((intptr_t)default_sink_index + 1));
+    device = SDL_FindPhysicalAudioDeviceByHandle((void *)((intptr_t)default_sink_index + 1));
     if (device) {
-        SDL_UnlockMutex(device->lock);
         *default_output = device;
     }
 
-    device = SDL_ObtainPhysicalAudioDeviceByHandle((void *)((intptr_t)default_source_index + 1));
+    device = SDL_FindPhysicalAudioDeviceByHandle((void *)((intptr_t)default_source_index + 1));
     if (device) {
-        SDL_UnlockMutex(device->lock);
         *default_capture = device;
     }