wasapi: Handle disconnected devices that get reconnected.

The device ID strings don't change between connects, so we need to move the
old device object out of the way if it still exists as a zombie, and let
the reconnected device get itself a fresh object.
main
Ryan C. Gordon 2023-10-27 01:30:27 -04:00
parent 5fa7b291d4
commit f63e9a8a3f
No known key found for this signature in database
GPG Key ID: 3C62BA43B5CCC412
1 changed files with 12 additions and 0 deletions

View File

@ -133,6 +133,18 @@ static SDL_AudioDevice *SDL_IMMDevice_Add(const SDL_bool iscapture, const char *
// see if we already have this one first.
SDL_AudioDevice *device = SDL_IMMDevice_FindByDevID(devid);
if (device) {
if (SDL_AtomicGet(&device->zombie)) {
// whoa, it came back! This can happen if you unplug and replug USB headphones while we're still keeping the SDL object alive.
// Kill this device's IMMDevice id; the device will go away when the app closes it, or maybe a new default device is chosen
// (possibly this reconnected device), so we just want to make sure IMMDevice doesn't try to find the old device by the existing ID string.
SDL_IMMDevice_HandleData *handle = (SDL_IMMDevice_HandleData *) device->handle;
SDL_free(handle->immdevice_id);
handle->immdevice_id = NULL;
device = NULL; // add a new device, below.
}
}
if (!device) {
// handle is freed by SDL_IMMDevice_FreeDeviceHandle!
SDL_IMMDevice_HandleData *handle = SDL_malloc(sizeof(SDL_IMMDevice_HandleData));