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).
main
Ryan C. Gordon 2023-07-11 13:59:54 -04:00
parent c3f5a5fc72
commit 2fb122fe46
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
6 changed files with 16 additions and 46 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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.
}
}

View File

@ -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;
}