Audio events not working as expected

Hello all,

I’m trying to catch the audio hot-swap events in my application. I’m doing the following when the application first launches:

SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_EVENTS);
SDL_EventState(SDL_AUDIODEVICEADDED, SDL_ENABLE);
SDL_EventState(SDL_AUDIODEVICEREMOVED, SDL_ENABLE);
SDL_AddEventWatch(OnSDLEvent, nullptr);

And I have:
int OnSDLEvent(void* userData, SDL_Event* event)
{
std::cout << "event type = " << event->type << std::endl;
}

If I start with my USB headset plugged in when I launch the application, I get “event type = 4353” printed to the screen when I unplug it. This is consistent with my understanding of the documentation.

If I plug the headset in, I don’t get any message. I can see that it is detected by Windows (running Windows 7 and SDL 2.0.5), but I don’t seem to get an SDL-generated event.

Also - Because my USB headset includes both a microphone and speaker, I expected to get two events when I unplugged it - one for the input device and another for the output device. I only seem to get the event for the input device (the iscapture parameter is always 1).

I’m calling SDL_PumpEvents() periodically to ensure the events are added to the queue.

Is there something I’m doing wrong? Or maybe my understanding of the expected behavior flawed?

In summary:

  1. Why am I not getting SDL_AUDIODEVICEADDED events?
  2. How many events should I expect when plugging/unplugging a USB headset that includes both input and output devices?

Thanks for your help.

-Kerry

I’ve been doing some more experimentation, and have some more information to share, but I don’t think I’m any closer to a solution.

I decided to write my own hot-swap event detection using Windows’ IMMNotificationClient interface (see https://msdn.microsoft.com/en-us/library/windows/desktop/dd370805(v=vs.85).aspx). In a test application that did nothing else, this worked fine. Unplugging my USB headset generated events for both the mic and the speaker, as did plugging them in.

I implemented the same wrapper class in my “real” application, and found that it only detected the first device removal, and then only generated one event (for the microphone). It behaved identically to the SDL event methods, even though it behaved differently in my test application.

After commenting out chunks of code one-by-one, I found that events seem to be generated properly (captured using my wrapper class around the Core Audio API) as long as SDL_OpenAudioDevice() was never called. Calling it just once seems to break the audio events.

If I don’t call SDL_OpenAudioDevice(), I still don’t get device added events when I use the SDL event subsystem.

Does anyone have any experience with this?

Thanks,

Kerry

I was able to get the Core Audio Interface version working by altering my callback. I was originally calling SDL_CloseAudioDevice() within the callback when I saw that a device was removed and calling SDL_AudioDeviceOpen() if the correct device was added, but moving these functions to the main thread and only updating “needs to be closed” and “ready to be opened” variables within the callback was the fix.

This fix only worked for the Core Audio Interface approach, however - the SDL event engine remains broken, so any help is appreciated.

Thanks,

Kerry