[vlc-devel] [PATCH 2/2] mmdevice: handle AUDCLNT_E_ALREADY_INITIALIZED

Thomas Guillem thomas at gllm.fr
Wed Jul 5 18:39:21 CEST 2017


Sadly, this error can happen even if the "aout stream" was well stopped. See
MSDN comments in the commit.
---
 modules/audio_output/mmdevice.c | 43 ++++++++++++++++++++++++++++++++++++-----
 1 file changed, 38 insertions(+), 5 deletions(-)

diff --git a/modules/audio_output/mmdevice.c b/modules/audio_output/mmdevice.c
index 8446c1cab3..8e38eb9442 100644
--- a/modules/audio_output/mmdevice.c
+++ b/modules/audio_output/mmdevice.c
@@ -94,6 +94,7 @@ struct aout_sys_t
     float gain; /**< Current software gain volume */
 
     wchar_t *selecting_device; /**< Requesting device identifier, NULL if none */
+    wchar_t *selected_device; /**< Requested device identifier, NULL if default */
     float volume; /**< Requested volume, negative if none */
     signed char mute; /**< Requested mute, negative if none */
     CRITICAL_SECTION lock;
@@ -718,12 +719,14 @@ static int DevicesEnum(audio_output_t *aout, IMMDeviceEnumerator *it)
     return n;
 }
 
-static int DeviceSelectLocked(audio_output_t *aout, const char *id)
+static int DeviceSelectLocked(audio_output_t *aout, const char *id, wchar_t *wid)
 {
     aout_sys_t *sys = aout->sys;
     wchar_t *device;
 
-    if (id != NULL)
+    if (wid != NULL)
+        device = wid;
+    else if (id != NULL)
     {
         device = ToWide(id);
         if (unlikely(device == NULL))
@@ -748,7 +751,7 @@ static int DeviceSelectLocked(audio_output_t *aout, const char *id)
 static int DeviceSelect(audio_output_t *aout, const char *id)
 {
     EnterCriticalSection(&aout->sys->lock);
-    int ret = DeviceSelectLocked(aout, id);
+    int ret = DeviceSelectLocked(aout, id, NULL);
     LeaveCriticalSection(&aout->sys->lock);
     return ret;
 }
@@ -790,6 +793,10 @@ static HRESULT MMSession(audio_output_t *aout, IMMDeviceEnumerator *it)
     assert(sys->selecting_device != NULL);
     assert(sys->dev == NULL);
 
+    /* Yes, it's perfectly valid to request the same device, see Start()
+     * comments. */
+    if (sys->selected_device != sys->selecting_device)
+        free(sys->selected_device);
     if (sys->selecting_device != default_device) /* Device selected explicitly */
     {
         msg_Dbg(aout, "using selected device %ls", sys->selecting_device);
@@ -797,10 +804,13 @@ static HRESULT MMSession(audio_output_t *aout, IMMDeviceEnumerator *it)
         if (FAILED(hr))
             msg_Err(aout, "cannot get selected device %ls (error 0x%lx)",
                     sys->selecting_device, hr);
-        free(sys->selecting_device);
+        sys->selected_device = sys->selecting_device;
     }
     else
+    {
+        sys->selected_device = NULL;
         hr = AUDCLNT_E_DEVICE_INVALIDATED;
+    }
 
     while (hr == AUDCLNT_E_DEVICE_INVALIDATED)
     {   /* Default device selected by policy and with stream routing.
@@ -1083,7 +1093,29 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
 
         sys->module = vlc_module_load(s, "aout stream", "$mmdevice-backend",
                                       false, aout_stream_Start, s, fmt, &hr);
-        if (hr != AUDCLNT_E_DEVICE_INVALIDATED || DeviceSelectLocked(aout, NULL))
+
+        int ret = -1;
+        if (hr == AUDCLNT_E_ALREADY_INITIALIZED)
+        {
+            /* From MSDN: "If the initial call to Initialize fails, subsequent
+             * Initialize calls might fail and return error code
+             * E_ALREADY_INITIALIZED, even though the interface has not been
+             * initialized. If this occurs, release the IAudioClient interface
+             * and obtain a new IAudioClient interface from the MMDevice API
+             * before calling Initialize again."
+             *
+             * Therefore, request to MMThread the same device and try again. */
+
+            ret = DeviceSelectLocked(aout, NULL, sys->selected_device);
+        }
+        else if (hr == AUDCLNT_E_DEVICE_INVALIDATED)
+        {
+            /* The audio endpoint device has been unplugged, request to
+             * MMThread the default device and try again. */
+
+            ret = DeviceSelectLocked(aout, NULL, NULL);
+        }
+        if (ret != 0)
             break;
     }
     LeaveCriticalSection(&sys->lock);
@@ -1135,6 +1167,7 @@ static int Open(vlc_object_t *obj)
     sys->ducks = 0;
 
     sys->selecting_device = default_device;
+    sys->selected_device = NULL;
     sys->gain = 1.f;
     sys->volume = -1.f;
     sys->mute = -1;
-- 
2.11.0



More information about the vlc-devel mailing list