We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4993

Summary: UWP/WinRT WASAPI Audio device creation is not complete when SDL_InitSubSystem returns
Product: SDL Reporter: Jeff Loiselle <jeff>
Component: audioAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: major    
Priority: P2 CC: flibitijibibo
Version: 2.0.10Keywords: target-2.0.12
Hardware: x86   
OS: Windows 10   
Attachments: EnumerationComplete handler

Description Jeff Loiselle 2020-02-18 04:05:24 UTC
WASAPI (Which UWP/WinRT uses for audio) device creation in SDL is done via events.  Those events might have fired before the SDL init returns.  However it is not guaranteed.  We're seeing a semi-regular repro of the devices not being present on a Windows 10 PC.  We've also had one report (Via a crash upload) of a repro on Xbox One.  I was able to work around the issue by adding a 10ms sleep after the init function.  

In speaking with Ethan (We're using SDL2 via FNA) filing the bug on SDL2.
Comment 1 Ryan C. Gordon 2020-02-19 01:19:23 UTC
This should probably wait for 2.0.13, but:

I assumed initial device enumeration was synchronous (because surely the system already knows what it has), but apparently not. Likely: it already knows what it has, but sometimes the scheduler lets us move on before the enumeration thread has sent the whole list.

Windows.Devices.Enumeration.DeviceWatcher has an EnumerationCompleted event that we aren't watching.

https://docs.microsoft.com/en-us/uwp/api/windows.devices.enumeration.devicewatcher.enumerationcompleted

So I think we could make a temporary semaphore in WASAPI_EnumerateEndpoints(), start enumeration and wait, and trigger that semaphore from the EnumerationCompleted event, causing WASAPI_EnumerateEndpoints() to destroy the semaphore and return, which would solve this issue, I suspect. It would be safe even if this turns out to be synchronous on some WinRT platforms, too.

--ryan.
Comment 2 Ethan Lee 2020-03-03 17:40:43 UTC
Created attachment 4235 [details]
EnumerationComplete handler

Attached is a patch that adds a handler for EnumerationComplete, which activates a semaphore that EnumerateEndpoints now waits on before leaving the function. Confirmed to work on Windows 10 and Xbox.
Comment 3 Ryan C. Gordon 2020-03-05 14:47:07 UTC
This patch is now https://hg.libsdl.org/SDL/rev/0311c59d15f9, thanks!

--ryan.