libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.46k stars 1.76k forks source link

Failed to open audio recording device [name]. SDL Error: CoreAudio error (AudioQueueStart): -66681 [MacOS] #10432

Open mail2mhossain opened 1 month ago

mail2mhossain commented 1 month ago

When using our app, if an audio input or output device is unplugged, I can switch to another available device on all platforms (Windows, MacOS, Linux). However, after plugging the device back in, the app can only switch back to it on Windows.

On MacOS, I encounter the following error: "Failed to open audio recording device [name]. SDL Error: CoreAudio error (AudioQueueStart): -66681".

This indicates that the unplugged device is still in use or blocked by SDL2 or MacOS. I have tried the following code in the "InitRecordingDevice" method after calling "CloseAudio()", but it still does not work:

SDL_QuitSubSystem(SDL_INIT_AUDIO); SDL_InitSubSystem(SDL_INIT_AUDIO);

I have also added "SDL_ClearQueuedAudio(dev)" in the "CloseAudio" method before calling "CloseAudioRecordingDevice", but this has not resolved the issue either.

Are there any solutions for this problem?

icculus commented 1 month ago

Are there any solutions for this problem?

This sounds like a bug in SDL; I'll try to reproduce it here tomorrow morning.

icculus commented 1 month ago

This reproduces for me with testaudiohotplug on both SDL2 and SDL3 (just unplug USB headphones and plug them back in while the test program is running). Digging deeper.

icculus commented 1 month ago

Looks like (in SDL3, at least) device->currently_opened is zero when DeviceAliveNotification is fired...so when we tell SDL that the device is disconnected, it doesn't try to close it, so all sorts of stuff is still dangling.

Not sure why yet, looking into it still.

icculus commented 1 month ago

Wait, no, that was the recording half of the USB headset. But I'm also noticing the testaudiohotplug doesn't close the device at all, so I bet that's what's happening.

icculus commented 1 month ago

Nope, that didn't fix it.

I'm starting to believe this is a macOS bug; the internet (since Mac OS X 10.5, apparently) has been asking "why does this error pop up some times?" and no one seems to know.

Notably, it seems to hang all the other playing devices too, for several seconds, when replugging the USB headset. If I start the device as unplugged and plug it in after everything else, it picks it up fine and works (but then, an unplug/replug after will still cause failure).

I added a 5 second delay to see if it just needed a little time to get itself together on hotplug; didn't fix it.

Feels like the CoreAudio framework is holding some state from the previous device connection that confuses it on replugging. I could be wrong. I'll ask our Apple contact.

icculus commented 1 month ago

Okay, I've moved the SDL coreaudio code out to a standalone program that still reproduces the issue, which is to say this could still be our bug, since it's still our code, but now I can hand this to Apple as a ~500 line Objective-C program without any external dependencies, including SDL itself:

https://gist.github.com/icculus/b021a110eb1bcdfa72f169a620e2d46e

Stay tuned!

mail2mhossain commented 3 days ago

Any Update