]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/mmdevice.c
mmdevice: avoid incorrect assertion
[vlc] / modules / audio_output / mmdevice.c
index e2ace47d5119f12bc96ee8f437c63bc9f9dbd4e5..2f41567334a62f07168dacf1f4eed1ce1d6c153b 100644 (file)
 # include <config.h>
 #endif
 
-#undef _WIN32_WINNT
-#define _WIN32_WINNT 0x600 /* Windows Vista */
 #define INITGUID
 #define COBJMACROS
 #define CONST_VTABLE
 
 #include <stdlib.h>
+#include <math.h>
 #include <assert.h>
 #include <audiopolicy.h>
 #include <mmdeviceapi.h>
@@ -40,11 +39,44 @@ DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd,
 #include <vlc_plugin.h>
 #include <vlc_aout.h>
 #include <vlc_charset.h>
+#include <vlc_modules.h>
 #include "audio_output/mmdevice.h"
 
+#if (_WIN32_WINNT < 0x600)
+static VOID WINAPI (*InitializeConditionVariable)(PCONDITION_VARIABLE);
+static BOOL WINAPI (*SleepConditionVariableCS)(PCONDITION_VARIABLE,
+                                               PCRITICAL_SECTION, DWORD);
+static VOID WINAPI (*WakeConditionVariable)(PCONDITION_VARIABLE);
+#define LOOKUP(s) \
+    if (((s) = (void *)GetProcAddress(h, #s)) == NULL) return FALSE
+
+BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID); /* avoid warning */
+BOOL WINAPI DllMain(HINSTANCE dll, DWORD reason, LPVOID reserved)
+{
+    (void) dll;
+    (void) reserved;
+
+    switch (reason)
+    {
+        case DLL_PROCESS_ATTACH:
+        {
+            HANDLE h = GetModuleHandle(TEXT("kernel32.dll"));
+            if (unlikely(h == NULL))
+                return FALSE;
+            LOOKUP(InitializeConditionVariable);
+            LOOKUP(SleepConditionVariableCS);
+            LOOKUP(WakeConditionVariable);
+            break;
+        }
+    }
+    return TRUE;
+}
+#endif
+
 DEFINE_GUID (GUID_VLC_AUD_OUT, 0x4533f59d, 0x59ee, 0x00c6,
    0xad, 0xb2, 0xc6, 0x8b, 0x50, 0x1a, 0x66, 0x55);
 
+#if !VLC_WINSTORE_APP
 static int TryEnterMTA(vlc_object_t *obj)
 {
     HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
@@ -56,6 +88,7 @@ static int TryEnterMTA(vlc_object_t *obj)
     return 0;
 }
 #define TryEnterMTA(o) TryEnterMTA(VLC_OBJECT(o))
+#endif
 
 static void EnterMTA(void)
 {
@@ -69,24 +102,30 @@ static void LeaveMTA(void)
     CoUninitialize();
 }
 
+#if !VLC_WINSTORE_APP
+static wchar_t default_device[1] = L"";
+#endif
+
 struct aout_sys_t
 {
     aout_stream_t *stream; /**< Underlying audio output stream */
+    module_t *module;
 #if !VLC_WINSTORE_APP
     audio_output_t *aout;
-
     IMMDeviceEnumerator *it; /**< Device enumerator, NULL when exiting */
-    /*TODO: IMMNotificationClient*/
-
     IMMDevice *dev; /**< Selected output device, NULL if none */
-    IAudioSessionManager *manager; /**< Session for the output device */
-    ISimpleAudioVolume *volume; /**< Volume setter */
 
+    struct IMMNotificationClient device_events;
     struct IAudioSessionEvents session_events;
 
     LONG refs;
-    HANDLE device_changed; /**< Event to reset thread */
-    HANDLE device_ready; /**< Event when thread is reset */
+
+    wchar_t *device; /**< Requested device identifier, NULL if none */
+    float volume; /**< Requested volume, negative if none */
+    signed char mute; /**< Requested mute, negative if none */
+    CRITICAL_SECTION lock;
+    CONDITION_VARIABLE work;
+    CONDITION_VARIABLE ready;
     vlc_thread_t thread; /**< Thread for audio session control */
 #else
     void *client;
@@ -97,8 +136,8 @@ struct aout_sys_t
  * safety (or lack thereof) of the interfaces. This code takes the most
  * restrictive assumption: no thread safety. The background thread (MMThread)
  * only runs at specified times, namely between the device_ready and
- * device_changed events (effectively, a thread barrier but only Windows 8
- * provides thread barriers natively).
+ * device_changed events (effectively a thread synchronization barrier, but
+ * only Windows 8 natively provides such a primitive).
  *
  * The audio output owner (i.e. the audio output core) is responsible for
  * serializing callbacks. This code only needs to be concerned with
@@ -174,36 +213,25 @@ static void Flush(audio_output_t *aout, bool wait)
 #if !VLC_WINSTORE_APP
 static int VolumeSet(audio_output_t *aout, float vol)
 {
-    ISimpleAudioVolume *volume = aout->sys->volume;
-    if (volume == NULL)
-        return -1;
-
-    if (TryEnterMTA(aout))
-        return -1;
-
-    HRESULT hr = ISimpleAudioVolume_SetMasterVolume(volume, vol, NULL);
-    if (FAILED(hr))
-        msg_Err(aout, "cannot set volume (error 0x%lx)", hr);
-    LeaveMTA();
+    aout_sys_t *sys = aout->sys;
 
-    return FAILED(hr) ? -1 : 0;
+    vol = vol * vol * vol; /* ISimpleAudioVolume is tapered linearly. */
+    EnterCriticalSection(&sys->lock);
+    sys->volume = vol;
+    WakeConditionVariable(&sys->work);
+    LeaveCriticalSection(&sys->lock);
+    return 0;
 }
 
 static int MuteSet(audio_output_t *aout, bool mute)
 {
-    ISimpleAudioVolume *volume = aout->sys->volume;
-    if (volume == NULL)
-        return -1;
-
-    if (TryEnterMTA(aout))
-        return -1;
-
-    HRESULT hr = ISimpleAudioVolume_SetMute(volume, mute ? TRUE : FALSE, NULL);
-    if (FAILED(hr))
-        msg_Err(aout, "cannot set volume (error 0x%lx)", hr);
-    LeaveMTA();
+    aout_sys_t *sys = aout->sys;
 
-    return FAILED(hr) ? -1 : 0;
+    EnterCriticalSection(&sys->lock);
+    sys->mute = mute;
+    WakeConditionVariable(&sys->work);
+    LeaveCriticalSection(&sys->lock);
+    return 0;
 }
 
 /*** Audio session events ***/
@@ -278,7 +306,7 @@ vlc_AudioSessionEvents_OnSimpleVolumeChanged(IAudioSessionEvents *this,
 
     msg_Dbg(aout, "simple volume changed: %f, muting %sabled", vol,
             mute ? "en" : "dis");
-    aout_VolumeReport(aout, vol);
+    aout_VolumeReport(aout, cbrtf(vol));
     aout_MuteReport(aout, mute == TRUE);
     (void) ctx;
     return S_OK;
@@ -373,119 +401,210 @@ static const struct IAudioSessionEventsVtbl vlc_AudioSessionEvents =
     vlc_AudioSessionEvents_OnSessionDisconnected,
 };
 
+/*** Audio devices ***/
 
-/*** Initialization / deinitialization **/
-static wchar_t *var_InheritWide(vlc_object_t *obj, const char *name)
+/** Gets the user-readable device name */
+static char *DeviceName(IMMDevice *dev)
 {
-    char *v8 = var_InheritString(obj, name);
-    if (v8 == NULL)
+    IPropertyStore *props;
+    char *name = NULL;
+    PROPVARIANT v;
+    HRESULT hr;
+
+    hr = IMMDevice_OpenPropertyStore(dev, STGM_READ, &props);
+    if (FAILED(hr))
         return NULL;
 
-    wchar_t *v16 = ToWide(v8);
-    free(v8);
-    return v16;
+    PropVariantInit(&v);
+    hr = IPropertyStore_GetValue(props, &PKEY_Device_FriendlyName, &v);
+    if (SUCCEEDED(hr))
+    {
+        name = FromWide(v.pwszVal);
+        PropVariantClear(&v);
+    }
+    IPropertyStore_Release(props);
+    return name;
 }
-#define var_InheritWide(o,n) var_InheritWide(VLC_OBJECT(o),n)
 
-static void MMSession(audio_output_t *aout, aout_sys_t *sys)
+/** Checks that a device is an output device */
+static bool DeviceIsRender(IMMDevice *dev)
 {
-    IAudioSessionControl *control;
+    void *pv;
+
+    if (FAILED(IMMDevice_QueryInterface(dev, &IID_IMMEndpoint, &pv)))
+        return false;
+
+    IMMEndpoint *ep = pv;
+    EDataFlow flow;
+
+    if (FAILED(IMMEndpoint_GetDataFlow(ep, &flow)))
+        flow = eCapture;
+
+    IMMEndpoint_Release(ep);
+    return flow == eRender;
+}
+
+static HRESULT DeviceUpdated(audio_output_t *aout, LPCWSTR wid)
+{
+    aout_sys_t *sys = aout->sys;
     HRESULT hr;
 
-    /* Register session control */
-    if (sys->manager != NULL)
+    IMMDevice *dev;
+    hr = IMMDeviceEnumerator_GetDevice(sys->it, wid, &dev);
+    if (FAILED(hr))
+        return hr;
+
+    if (!DeviceIsRender(dev))
     {
-        hr = IAudioSessionManager_GetSimpleAudioVolume(sys->manager,
-                                                       &GUID_VLC_AUD_OUT,
-                                                       FALSE, &sys->volume);
-        if (FAILED(hr))
-            msg_Err(aout, "cannot get simple volume (error 0x%lx)", hr);
+        IMMDevice_Release(dev);
+        return S_OK;
+    }
 
-        hr = IAudioSessionManager_GetAudioSessionControl(sys->manager,
-                                                         &GUID_VLC_AUD_OUT, 0,
-                                                         &control);
-        if (FAILED(hr))
-            msg_Err(aout, "cannot get session control (error 0x%lx)", hr);
+    char *id = FromWide(wid);
+    if (unlikely(id == NULL))
+    {
+        IMMDevice_Release(dev);
+        return E_OUTOFMEMORY;
+    }
+
+    char *name = DeviceName(dev);
+    IMMDevice_Release(dev);
+
+    aout_HotplugReport(aout, id, (name != NULL) ? name : id);
+    free(name);
+    free(id);
+    return S_OK;
+}
+
+static inline aout_sys_t *vlc_MMNotificationClient_sys(IMMNotificationClient *this)
+{
+    return (void *)(((char *)this) - offsetof(aout_sys_t, device_events));
+}
+
+static STDMETHODIMP
+vlc_MMNotificationClient_QueryInterface(IMMNotificationClient *this,
+                                        REFIID riid, void **ppv)
+{
+    if (IsEqualIID(riid, &IID_IUnknown)
+     || IsEqualIID(riid, &IID_IMMNotificationClient))
+    {
+        *ppv = this;
+        IUnknown_AddRef(this);
+        return S_OK;
     }
     else
     {
-        sys->volume = NULL;
-        control = NULL;
+       *ppv = NULL;
+        return E_NOINTERFACE;
     }
+}
 
-    if (control != NULL)
-    {
-        wchar_t *ua = var_InheritWide(aout, "user-agent");
-        IAudioSessionControl_SetDisplayName(control, ua, NULL);
-        free(ua);
+static STDMETHODIMP_(ULONG)
+vlc_MMNotificationClient_AddRef(IMMNotificationClient *this)
+{
+    aout_sys_t *sys = vlc_MMNotificationClient_sys(this);
+    return InterlockedIncrement(&sys->refs);
+}
 
-        IAudioSessionControl_RegisterAudioSessionNotification(control,
-                                                         &sys->session_events);
-    }
+static STDMETHODIMP_(ULONG)
+vlc_MMNotificationClient_Release(IMMNotificationClient *this)
+{
+    aout_sys_t *sys = vlc_MMNotificationClient_sys(this);
+    return InterlockedDecrement(&sys->refs);
+}
 
-    if (sys->volume != NULL)
-    {   /* Get current values (_after_ changes notification registration) */
-        BOOL mute;
-        float level;
+static STDMETHODIMP
+vlc_MMNotificationClient_OnDefaultDeviceChange(IMMNotificationClient *this,
+                                               EDataFlow flow, ERole role,
+                                               LPCWSTR wid)
+{
+    aout_sys_t *sys = vlc_MMNotificationClient_sys(this);
+    audio_output_t *aout = sys->aout;
 
-        hr = ISimpleAudioVolume_GetMute(sys->volume, &mute);
-        if (FAILED(hr))
-            msg_Err(aout, "cannot get mute (error 0x%lx)", hr);
-        else
-            aout_MuteReport(aout, mute != FALSE);
+    if (flow != eRender)
+        return S_OK;
+    if (role != eConsole)
+        return S_OK;
 
-        hr = ISimpleAudioVolume_GetMasterVolume(sys->volume, &level);
-        if (FAILED(hr))
-            msg_Err(aout, "cannot get mute (error 0x%lx)", hr);
-        else
-            aout_VolumeReport(aout, level);
-    }
+    msg_Dbg(aout, "default device changed: %ls", wid); /* TODO? migrate */
+    return S_OK;
+}
 
-    SetEvent(sys->device_ready);
-    /* Wait until device change or exit */
-    WaitForSingleObject(sys->device_changed, INFINITE);
+static STDMETHODIMP
+vlc_MMNotificationClient_OnDeviceAdded(IMMNotificationClient *this,
+                                       LPCWSTR wid)
+{
+    aout_sys_t *sys = vlc_MMNotificationClient_sys(this);
+    audio_output_t *aout = sys->aout;
 
-    /* Deregister session control */
-    if (control != NULL)
-    {
-        IAudioSessionControl_UnregisterAudioSessionNotification(control,
-                                                         &sys->session_events);
-        IAudioSessionControl_Release(control);
-    }
+    msg_Dbg(aout, "device %ls added", wid);
+    return DeviceUpdated(aout, wid);
+}
 
-    if (sys->volume != NULL)
-        ISimpleAudioVolume_Release(sys->volume);
+static STDMETHODIMP
+vlc_MMNotificationClient_OnDeviceRemoved(IMMNotificationClient *this,
+                                         LPCWSTR wid)
+{
+    aout_sys_t *sys = vlc_MMNotificationClient_sys(this);
+    audio_output_t *aout = sys->aout;
+    char *id = FromWide(wid);
+
+    msg_Dbg(aout, "device %ls removed", wid);
+    if (unlikely(id == NULL))
+        return E_OUTOFMEMORY;
+
+    aout_HotplugReport(aout, id, NULL);
+    free(id);
+    return S_OK;
 }
 
-/** MMDevice audio output thread.
- * This thread takes cares of the audio session control. Inconveniently enough,
- * the audio session control interface must:
- *  - be created and destroyed from the same thread, and
- *  - survive across VLC audio output calls.
- * The only way to reconcile both requirements is a custom thread.
- * The thread also ensure that the COM Multi-Thread Apartment is continuously
- * referenced so that MMDevice objects are not destroyed early.
- */
-static void *MMThread(void *data)
+static STDMETHODIMP
+vlc_MMNotificationClient_OnDeviceStateChanged(IMMNotificationClient *this,
+                                              LPCWSTR wid, DWORD state)
 {
-    audio_output_t *aout = data;
-    aout_sys_t *sys = aout->sys;
+    aout_sys_t *sys = vlc_MMNotificationClient_sys(this);
+    audio_output_t *aout = sys->aout;
 
-    EnterMTA();
-    while (sys->it != NULL)
-        MMSession(aout, sys);
-    LeaveMTA();
-    return NULL;
+    /* TODO: show device state / ignore missing devices */
+    msg_Dbg(aout, "device %ls state changed %08lx", wid, state);
+    return S_OK;
 }
 
-/*** Audio devices ***/
-static int DevicesEnum(audio_output_t *aout)
+static STDMETHODIMP
+vlc_MMNotificationClient_OnDevicePropertyChanged(IMMNotificationClient *this,
+                                                 LPCWSTR wid,
+                                                 const PROPERTYKEY key)
+{
+    aout_sys_t *sys = vlc_MMNotificationClient_sys(this);
+    audio_output_t *aout = sys->aout;
+
+    if (key.pid == PKEY_Device_FriendlyName.pid)
+    {
+        msg_Dbg(aout, "device %ls name changed", wid);
+        return DeviceUpdated(aout, wid);
+    }
+    return S_OK;
+}
+
+static const struct IMMNotificationClientVtbl vlc_MMNotificationClient =
+{
+    vlc_MMNotificationClient_QueryInterface,
+    vlc_MMNotificationClient_AddRef,
+    vlc_MMNotificationClient_Release,
+
+    vlc_MMNotificationClient_OnDeviceStateChanged,
+    vlc_MMNotificationClient_OnDeviceAdded,
+    vlc_MMNotificationClient_OnDeviceRemoved,
+    vlc_MMNotificationClient_OnDefaultDeviceChange,
+    vlc_MMNotificationClient_OnDevicePropertyChanged,
+};
+
+static int DevicesEnum(audio_output_t *aout, IMMDeviceEnumerator *it)
 {
-    aout_sys_t *sys = aout->sys;
     HRESULT hr;
     IMMDeviceCollection *devs;
 
-    hr = IMMDeviceEnumerator_EnumAudioEndpoints(sys->it, eRender,
+    hr = IMMDeviceEnumerator_EnumAudioEndpoints(it, eRender,
                                                 DEVICE_STATE_ACTIVE, &devs);
     if (FAILED(hr))
     {
@@ -506,7 +625,7 @@ static int DevicesEnum(audio_output_t *aout)
     for (UINT i = 0; i < count; i++)
     {
         IMMDevice *dev;
-        char *id, *name = NULL;
+        char *id, *name;
 
         hr = IMMDeviceCollection_Item(devs, i, &dev);
         if (FAILED(hr))
@@ -523,20 +642,7 @@ static int DevicesEnum(audio_output_t *aout)
         id = FromWide(devid);
         CoTaskMemFree(devid);
 
-        /* User-readable device name */
-        IPropertyStore *props;
-        hr = IMMDevice_OpenPropertyStore(dev, STGM_READ, &props);
-        if (SUCCEEDED(hr))
-        {
-            PROPVARIANT v;
-
-            PropVariantInit(&v);
-            hr = IPropertyStore_GetValue(props, &PKEY_Device_FriendlyName, &v);
-            if (SUCCEEDED(hr))
-                name = FromWide(v.pwszVal);
-            PropVariantClear(&v);
-            IPropertyStore_Release(props);
-        }
+        name = DeviceName(dev);
         IMMDevice_Release(dev);
 
         aout_HotplugReport(aout, id, (name != NULL) ? name : id);
@@ -548,107 +654,234 @@ static int DevicesEnum(audio_output_t *aout)
     return n;
 }
 
-/**
- * Opens the selected audio output device.
+static int DeviceSelect(audio_output_t *aout, const char *id)
+{
+    aout_sys_t *sys = aout->sys;
+    wchar_t *device;
+
+    if (id != NULL)
+    {
+        device = ToWide(id);
+        if (unlikely(device == NULL))
+            return -1;
+    }
+    else
+        device = default_device;
+
+    EnterCriticalSection(&sys->lock);
+    assert(sys->device == NULL);
+    sys->device = device;
+
+    WakeConditionVariable(&sys->work);
+    while (sys->device != NULL)
+        SleepConditionVariableCS(&sys->ready, &sys->lock, INFINITE);
+    LeaveCriticalSection(&sys->lock);
+
+    if (sys->stream != NULL && sys->dev != NULL)
+        /* Request restart of stream with the new device */
+        aout_RestartRequest(aout, AOUT_RESTART_OUTPUT);
+    return (sys->dev != NULL) ? 0 : -1;
+}
+
+/*** Initialization / deinitialization **/
+static wchar_t *var_InheritWide(vlc_object_t *obj, const char *name)
+{
+    char *v8 = var_InheritString(obj, name);
+    if (v8 == NULL)
+        return NULL;
+
+    wchar_t *v16 = ToWide(v8);
+    free(v8);
+    return v16;
+}
+#define var_InheritWide(o,n) var_InheritWide(VLC_OBJECT(o),n)
+
+/** MMDevice audio output thread.
+ * This thread takes cares of the audio session control. Inconveniently enough,
+ * the audio session control interface must:
+ *  - be created and destroyed from the same thread, and
+ *  - survive across VLC audio output calls.
+ * The only way to reconcile both requirements is a custom thread.
+ * The thread also ensure that the COM Multi-Thread Apartment is continuously
+ * referenced so that MMDevice objects are not destroyed early.
+ * Furthermore, VolumeSet() and MuteSet() may be called from a thread with a
+ * COM STA, so that it cannot access the COM MTA for audio controls.
  */
-static HRESULT OpenDevice(audio_output_t *aout, const char *devid)
+static HRESULT MMSession(audio_output_t *aout, IMMDeviceEnumerator *it)
 {
     aout_sys_t *sys = aout->sys;
+    IAudioSessionManager *manager;
+    IAudioSessionControl *control;
+    ISimpleAudioVolume *volume;
+    void *pv;
+    HRESULT hr;
+
+    assert(sys->device != NULL);
     assert(sys->dev == NULL);
 
-    HRESULT hr;
-    if (devid != NULL) /* Device selected explicitly */
+    if (sys->device != default_device) /* Device selected explicitly */
     {
-        msg_Dbg(aout, "using selected device %s", devid);
-
-        wchar_t *wdevid = ToWide(devid);
-        if (likely(wdevid != NULL))
-        {
-            hr = IMMDeviceEnumerator_GetDevice(sys->it, wdevid, &sys->dev);
-            free (wdevid);
-        }
-        else
-            hr = E_OUTOFMEMORY;
+        msg_Dbg(aout, "using selected device %ls", sys->device);
+        hr = IMMDeviceEnumerator_GetDevice(it, sys->device, &sys->dev);
+        free(sys->device);
     }
-    else /* Default device selected by policy */
-    {
+    else
+        hr = AUDCLNT_E_DEVICE_INVALIDATED;
+
+    while (hr == AUDCLNT_E_DEVICE_INVALIDATED)
+    {   /* Default device selected by policy and with stream routing.
+         * "Do not use eMultimedia" says MSDN. */
         msg_Dbg(aout, "using default device");
-        hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(sys->it, eRender,
+        hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(it, eRender,
                                                          eConsole, &sys->dev);
     }
-    assert(sys->manager == NULL);
-    if (FAILED(hr))
+
+    sys->device = NULL;
+    WakeConditionVariable(&sys->ready);
+
+    if (SUCCEEDED(hr))
+    {   /* Report actual device */
+        LPWSTR wdevid;
+
+        hr = IMMDevice_GetId(sys->dev, &wdevid);
+        if (SUCCEEDED(hr))
+        {
+            char *id = FromWide(wdevid);
+            CoTaskMemFree(wdevid);
+            if (likely(id != NULL))
+            {
+                aout_DeviceReport(aout, id);
+                free(id);
+            }
+        }
+    }
+    else
     {
         msg_Err(aout, "cannot get device (error 0x%lx)", hr);
-        goto out;
+        return hr;
     }
 
     /* Create session manager (for controls even w/o active audio client) */
-    void *pv;
     hr = IMMDevice_Activate(sys->dev, &IID_IAudioSessionManager,
                             CLSCTX_ALL, NULL, &pv);
-    if (FAILED(hr))
-        msg_Err(aout, "cannot activate session manager (error 0x%lx)", hr);
-    else
-        sys->manager = pv;
-
-    /* Report actual device */
-    LPWSTR wdevid;
-    hr = IMMDevice_GetId(sys->dev, &wdevid);
+    manager = pv;
     if (SUCCEEDED(hr))
     {
-        char *id = FromWide(wdevid);
-        CoTaskMemFree(wdevid);
-        if (likely(id != NULL))
+        LPCGUID guid = &GUID_VLC_AUD_OUT;
+
+        /* Register session control */
+        hr = IAudioSessionManager_GetAudioSessionControl(manager, guid, 0,
+                                                         &control);
+        if (SUCCEEDED(hr))
         {
-            aout_DeviceReport(aout, id);
-            free(id);
+            wchar_t *ua = var_InheritWide(aout, "user-agent");
+            IAudioSessionControl_SetDisplayName(control, ua, NULL);
+            free(ua);
+
+            IAudioSessionControl_RegisterAudioSessionNotification(control,
+                                                         &sys->session_events);
+        }
+        else
+            msg_Err(aout, "cannot get session control (error 0x%lx)", hr);
+
+        hr = IAudioSessionManager_GetSimpleAudioVolume(manager, guid, FALSE,
+                                                       &volume);
+        if (SUCCEEDED(hr))
+        {   /* Get current values _after_ registering for notification */
+            BOOL mute;
+            float level;
+
+            hr = ISimpleAudioVolume_GetMute(volume, &mute);
+            if (SUCCEEDED(hr))
+                aout_MuteReport(aout, mute != FALSE);
+            else
+                msg_Err(aout, "cannot get mute (error 0x%lx)", hr);
+
+            hr = ISimpleAudioVolume_GetMasterVolume(volume, &level);
+            if (SUCCEEDED(hr))
+                aout_VolumeReport(aout, level);
+            else
+                msg_Err(aout, "cannot get mute (error 0x%lx)", hr);
         }
+        else
+            msg_Err(aout, "cannot get simple volume (error 0x%lx)", hr);
+    }
+    else
+    {
+        msg_Err(aout, "cannot activate session manager (error 0x%lx)", hr);
+        control = NULL;
+        volume = NULL;
     }
-out:
-    SetEvent(sys->device_changed);
-    WaitForSingleObject(sys->device_ready, INFINITE);
-    return hr;
-}
 
-/**
- * Closes the opened audio output device (if any).
- */
-static void CloseDevice(audio_output_t *aout)
-{
-    aout_sys_t *sys = aout->sys;
+    /* Main loop (adjust volume as long as device is unchanged) */
+    while (sys->device == NULL)
+    {
+        if (volume != NULL && sys->volume >= 0.f)
+        {
+            hr = ISimpleAudioVolume_SetMasterVolume(volume, sys->volume, NULL);
+            if (FAILED(hr))
+                msg_Err(aout, "cannot set master volume (error 0x%lx)", hr);
+            sys->volume = -1.f;
+        }
+
+        if (volume != NULL && sys->mute >= 0)
+        {
+            hr = ISimpleAudioVolume_SetMute(volume,
+                                            sys->mute ? TRUE : FALSE, NULL);
+            if (FAILED(hr))
+                msg_Err(aout, "cannot set mute (error 0x%lx)", hr);
+            sys->mute = -1;
+        }
+
+        SleepConditionVariableCS(&sys->work, &sys->lock, INFINITE);
+    }
 
-    assert(sys->dev != NULL);
-    if (sys->manager != NULL)
+    if (manager != NULL)
     {
-        IAudioSessionManager_Release(sys->manager);
-        sys->manager = NULL;
+        /* Deregister session control */
+        if (volume != NULL)
+            ISimpleAudioVolume_Release(volume);
+
+        if (control != NULL)
+        {
+            IAudioSessionControl_UnregisterAudioSessionNotification(control,
+                                                         &sys->session_events);
+            IAudioSessionControl_Release(control);
+        }
+
+        IAudioSessionManager_Release(manager);
     }
 
     IMMDevice_Release(sys->dev);
     sys->dev = NULL;
+    return S_OK;
 }
 
-static int DeviceSelect(audio_output_t *aout, const char *id)
+static void *MMThread(void *data)
 {
+    audio_output_t *aout = data;
     aout_sys_t *sys = aout->sys;
-    HRESULT hr;
+    IMMDeviceEnumerator *it = sys->it;
 
-    if (TryEnterMTA(aout))
-        return -1;
+    EnterMTA();
+    IMMDeviceEnumerator_RegisterEndpointNotificationCallback(it,
+                                                          &sys->device_events);
+    DevicesEnum(aout, it);
 
-    if (sys->dev != NULL)
-        CloseDevice(aout);
+    EnterCriticalSection(&sys->lock);
 
-    hr = OpenDevice(aout, id);
-    while (hr == AUDCLNT_E_DEVICE_INVALIDATED)
-        hr = OpenDevice(aout, NULL); /* Fallback to default device */
-    LeaveMTA();
+    do
+        if (FAILED(MMSession(aout, it)))
+            SleepConditionVariableCS(&sys->work, &sys->lock, INFINITE);
+    while (sys->it != NULL);
 
-    if (sys->stream != NULL)
-        /* Request restart of stream with the new device */
-        aout_RestartRequest(aout, AOUT_RESTART_OUTPUT);
-    return FAILED(hr) ? -1 : 0;
+    LeaveCriticalSection(&sys->lock);
+
+    IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(it,
+                                                          &sys->device_events);
+    IMMDeviceEnumerator_Release(it);
+    LeaveMTA();
+    return NULL;
 }
 
 /**
@@ -673,15 +906,32 @@ static HRESULT ActivateDevice(void *opaque, REFIID iid, PROPVARIANT *actparms,
 }
 #endif /* VLC_WINSTORE_APP */
 
+static int aout_stream_Start(void *func, va_list ap)
+{
+    aout_stream_start_t start = func;
+    aout_stream_t *s = va_arg(ap, aout_stream_t *);
+    audio_sample_format_t *fmt = va_arg(ap, audio_sample_format_t *);
+    HRESULT *hr = va_arg(ap, HRESULT *);
+
+    *hr = start(s, fmt, &GUID_VLC_AUD_OUT);
+    if (*hr == AUDCLNT_E_DEVICE_INVALIDATED)
+        return VLC_ETIMEOUT;
+    return SUCCEEDED(*hr) ? VLC_SUCCESS : VLC_EGENERIC;
+}
+
+static void aout_stream_Stop(void *func, va_list ap)
+{
+    aout_stream_stop_t stop = func;
+    aout_stream_t *s = va_arg(ap, aout_stream_t *);
+
+    stop(s);
+}
+
 static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
 {
     aout_sys_t *sys = aout->sys;
-    HRESULT hr;
-
-    assert (sys->stream == NULL);
 #if !VLC_WINSTORE_APP
-    /* Open the default device if required (to deal with restarts) */
-    if (sys->dev == NULL && FAILED(DeviceSelect(aout, NULL)))
+    if (sys->dev == NULL)
         return -1;
 #endif
 
@@ -689,18 +939,38 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
     if (unlikely(s == NULL))
         return -1;
 
+#if !VLC_WINSTORE_APP
     s->owner.device = sys->dev;
+#else
+    s->owner.device = sys->client;
+#endif
     s->owner.activate = ActivateDevice;
 
     EnterMTA();
-    hr = aout_stream_Start(s, fmt, &GUID_VLC_AUD_OUT);
-    if (SUCCEEDED(hr))
-        sys->stream = s;
-    else
-        vlc_object_release(s);
+    for (;;)
+    {
+        HRESULT hr;
+
+        sys->module = vlc_module_load(s, "aout stream", NULL, false,
+                                      aout_stream_Start, s, fmt, &hr);
+        if (hr != AUDCLNT_E_DEVICE_INVALIDATED
+#if !VLC_WINSTORE_APP
+                || DeviceSelect(aout, NULL)
+#endif
+           )
+            break;
+    }
     LeaveMTA();
 
-    return vlc_FromHR(aout, hr);
+    if (sys->module == NULL)
+    {
+        vlc_object_release(s);
+        return -1;
+    }
+
+    assert (sys->stream == NULL);
+    sys->stream = s;
+    return 0;
 }
 
 static void Stop(audio_output_t *aout)
@@ -710,7 +980,7 @@ static void Stop(audio_output_t *aout)
     assert (sys->stream != NULL);
 
     EnterMTA();
-    aout_stream_Stop(sys->stream);
+    vlc_module_unload(sys->module, aout_stream_Stop, sys->stream);
     LeaveMTA();
 
     vlc_object_release(sys->stream);
@@ -721,10 +991,6 @@ static int Open(vlc_object_t *obj)
 {
     audio_output_t *aout = (audio_output_t *)obj;
 
-    if (!aout->b_force && var_InheritBool(aout, "spdif"))
-        /* Fallback to other plugin until pass-through is implemented */
-        return VLC_EGENERIC;
-
     aout_sys_t *sys = malloc(sizeof (*sys));
     if (unlikely(sys == NULL))
         return VLC_ENOMEM;
@@ -735,14 +1001,15 @@ static int Open(vlc_object_t *obj)
     sys->aout = aout;
     sys->it = NULL;
     sys->dev = NULL;
-    sys->manager = NULL;
     sys->session_events.lpVtbl = &vlc_AudioSessionEvents;
     sys->refs = 1;
 
-    sys->device_changed = CreateEvent(NULL, FALSE, FALSE, NULL);
-    sys->device_ready = CreateEvent(NULL, FALSE, FALSE, NULL);
-    if (unlikely(sys->device_changed == NULL || sys->device_ready == NULL))
-        goto error;
+    sys->device = default_device;
+    sys->volume = -1.f;
+    sys->mute = -1;
+    InitializeCriticalSection(&sys->lock);
+    InitializeConditionVariable(&sys->work);
+    InitializeConditionVariable(&sys->ready);
 
     /* Initialize MMDevice API */
     if (TryEnterMTA(aout))
@@ -750,7 +1017,7 @@ static int Open(vlc_object_t *obj)
 
     void *pv;
     HRESULT hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL,
-                          &IID_IMMDeviceEnumerator, &pv);
+                                  &IID_IMMDeviceEnumerator, &pv);
     if (FAILED(hr))
     {
         LeaveMTA();
@@ -758,12 +1025,19 @@ static int Open(vlc_object_t *obj)
         goto error;
     }
     sys->it = pv;
+
     if (vlc_clone(&sys->thread, MMThread, aout, VLC_THREAD_PRIORITY_LOW))
+    {
+        IMMDeviceEnumerator_Release(sys->it);
+        LeaveMTA();
         goto error;
-    WaitForSingleObject(sys->device_ready, INFINITE);
+    }
 
-    DeviceSelect(aout, NULL); /* Get a device to start with */
-    LeaveMTA(); /* leave MTA after thread has entered MTA */
+    EnterCriticalSection(&sys->lock);
+    while (sys->device != NULL)
+        SleepConditionVariableCS(&sys->ready, &sys->lock, INFINITE);
+    LeaveCriticalSection(&sys->lock);
+    LeaveMTA(); /* Leave MTA after thread has entered MTA */
 #else
     sys->client = var_InheritAddress(aout, "mmdevice-audioclient");
     assert(sys->client != NULL);
@@ -778,19 +1052,10 @@ static int Open(vlc_object_t *obj)
     aout->volume_set = VolumeSet;
     aout->mute_set = MuteSet;
     aout->device_select = DeviceSelect;
-    DevicesEnum(aout);
     return VLC_SUCCESS;
 
 error:
-    if (sys->it != NULL)
-    {
-        IMMDeviceEnumerator_Release(sys->it);
-        LeaveMTA();
-    }
-    if (sys->device_ready != NULL)
-        CloseHandle(sys->device_ready);
-    if (sys->device_changed != NULL)
-        CloseHandle(sys->device_changed);
+    DeleteCriticalSection(&sys->lock);
     free(sys);
     return VLC_EGENERIC;
 #else
@@ -802,22 +1067,15 @@ static void Close(vlc_object_t *obj)
 {
     audio_output_t *aout = (audio_output_t *)obj;
     aout_sys_t *sys = aout->sys;
-
 #if !VLC_WINSTORE_APP
-    EnterMTA(); /* enter MTA before thread leaves MTA */
-    if (sys->dev != NULL)
-        CloseDevice(aout);
+    EnterCriticalSection(&sys->lock);
+    sys->device = default_device; /* break out of MMSession() loop */
+    sys->it = NULL; /* break out of MMThread() loop */
+    WakeConditionVariable(&sys->work);
+    LeaveCriticalSection(&sys->lock);
 
-    IMMDeviceEnumerator_Release(sys->it);
-    sys->it = NULL;
-
-    SetEvent(sys->device_changed);
     vlc_join(sys->thread, NULL);
-
-    LeaveMTA();
-
-    CloseHandle(sys->device_ready);
-    CloseHandle(sys->device_changed);
+    DeleteCriticalSection(&sys->lock);
 #else
     free(sys->client);
 #endif
@@ -827,7 +1085,7 @@ static void Close(vlc_object_t *obj)
 vlc_module_begin()
     set_shortname("MMDevice")
     set_description(N_("Windows Multimedia Device output"))
-    set_capability("audio output", /*150*/0)
+    set_capability("audio output", 150)
 #if VLC_WINSTORE_APP
     /* Pointer to the activated AudioClient* */
     add_integer("mmdevice-audioclient", 0x0, NULL, NULL, true);