SDL: wasapi: Deal with HDMI or DisplayPort-based audio devices.

From 48e71ae87be425f117dece3735b148fbc5f2606e Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Fri, 26 May 2023 19:08:24 -0400
Subject: [PATCH] wasapi: Deal with HDMI or DisplayPort-based audio devices.

They can vanish for UP TO EIGHT SECONDS...!

This is for devices that connect to HDMI/DisplayPort/etc, where it
presumably has to wait for a display to get up and running before it
can play audio through it, so one can see the audio device fail when
changing display modes, or the system returning from sleep. Since this
can be triggered by a game changing video resolutions at startup (either
before or after opening the audio device!), it's important to deal with.

In normal conditions, it shouldn't take this long to open or recover an
audio device, but this is better than unexpectedly losing the device
in this situation.

Fixes #7044.
Fixes #5571.
---
 src/core/windows/SDL_immdevice.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/src/core/windows/SDL_immdevice.c b/src/core/windows/SDL_immdevice.c
index 831d9e7a03fc..5373ce92aadd 100644
--- a/src/core/windows/SDL_immdevice.c
+++ b/src/core/windows/SDL_immdevice.c
@@ -357,19 +357,32 @@ void SDL_IMMDevice_Quit(void)
 
 int SDL_IMMDevice_Get(LPCWSTR devid, IMMDevice **device, SDL_bool iscapture)
 {
+    const Uint64 timeout = SDL_GetTicks64() + 8000;  /* intel's audio drivers can fail for up to EIGHT SECONDS after a device is connected or we wake from sleep. */
     HRESULT ret;
 
     SDL_assert(device != NULL);
 
-    if (devid == NULL) {
-        const EDataFlow dataflow = iscapture ? eCapture : eRender;
-        ret = IMMDeviceEnumerator_GetDefaultAudioEndpoint(enumerator, dataflow, SDL_IMMDevice_role, device);
-    } else {
-        ret = IMMDeviceEnumerator_GetDevice(enumerator, devid, device);
-    }
+    while (SDL_TRUE) {
+        if (devid == NULL) {
+            const EDataFlow dataflow = iscapture ? eCapture : eRender;
+            ret = IMMDeviceEnumerator_GetDefaultAudioEndpoint(enumerator, dataflow, SDL_IMMDevice_role, device);
+        } else {
+            ret = IMMDeviceEnumerator_GetDevice(enumerator, devid, device);
+        }
+
+        if (SUCCEEDED(ret)) {
+            break;
+        }
+
+        if (ret == E_NOTFOUND) {
+            const Uint64 now = SDL_GetTicks64();
+            if (timeout > now) {
+                const Uint64 ticksleft = timeout - now;
+                SDL_Delay(SDL_min(ticksleft, 300));   /* wait awhile and try again. */
+                continue;
+            }
+        }
 
-    if (FAILED(ret)) {
-        SDL_assert(*device == NULL);
         return WIN_SetErrorFromHRESULT("WASAPI can't find requested audio endpoint", ret);
     }
     return 0;