]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/mmdevice.c
mmdevice: avoid incorrect assertion
[vlc] / modules / audio_output / mmdevice.c
index 757481ea94065377ca073e7dfe89a37b3293039f..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>
@@ -43,9 +42,41 @@ DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd,
 #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);
@@ -57,6 +88,7 @@ static int TryEnterMTA(vlc_object_t *obj)
     return 0;
 }
 #define TryEnterMTA(o) TryEnterMTA(VLC_OBJECT(o))
+#endif
 
 static void EnterMTA(void)
 {
@@ -70,7 +102,9 @@ static void LeaveMTA(void)
     CoUninitialize();
 }
 
+#if !VLC_WINSTORE_APP
 static wchar_t default_device[1] = L"";
+#endif
 
 struct aout_sys_t
 {
@@ -181,6 +215,7 @@ static int VolumeSet(audio_output_t *aout, float vol)
 {
     aout_sys_t *sys = aout->sys;
 
+    vol = vol * vol * vol; /* ISimpleAudioVolume is tapered linearly. */
     EnterCriticalSection(&sys->lock);
     sys->volume = vol;
     WakeConditionVariable(&sys->work);
@@ -271,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;
@@ -488,7 +523,7 @@ vlc_MMNotificationClient_OnDefaultDeviceChange(IMMNotificationClient *this,
 
     if (flow != eRender)
         return S_OK;
-    if (role != eConsole) /* FIXME? use eMultimedia instead */
+    if (role != eConsole)
         return S_OK;
 
     msg_Dbg(aout, "default device changed: %ls", wid); /* TODO? migrate */
@@ -642,7 +677,7 @@ static int DeviceSelect(audio_output_t *aout, const char *id)
         SleepConditionVariableCS(&sys->ready, &sys->lock, INFINITE);
     LeaveCriticalSection(&sys->lock);
 
-    if (sys->stream != NULL)
+    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;
@@ -694,7 +729,8 @@ static HRESULT MMSession(audio_output_t *aout, IMMDeviceEnumerator *it)
         hr = AUDCLNT_E_DEVICE_INVALIDATED;
 
     while (hr == AUDCLNT_E_DEVICE_INVALIDATED)
-    {   /* Default device selected by policy */
+    {   /* Default device selected by policy and with stream routing.
+         * "Do not use eMultimedia" says MSDN. */
         msg_Dbg(aout, "using default device");
         hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(it, eRender,
                                                          eConsole, &sys->dev);
@@ -825,15 +861,25 @@ static void *MMThread(void *data)
 {
     audio_output_t *aout = data;
     aout_sys_t *sys = aout->sys;
+    IMMDeviceEnumerator *it = sys->it;
 
     EnterMTA();
+    IMMDeviceEnumerator_RegisterEndpointNotificationCallback(it,
+                                                          &sys->device_events);
+    DevicesEnum(aout, it);
+
     EnterCriticalSection(&sys->lock);
 
-    while (sys->it != NULL)
-        if (FAILED(MMSession(aout, sys->it)))
+    do
+        if (FAILED(MMSession(aout, it)))
             SleepConditionVariableCS(&sys->work, &sys->lock, INFINITE);
+    while (sys->it != NULL);
 
     LeaveCriticalSection(&sys->lock);
+
+    IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(it,
+                                                          &sys->device_events);
+    IMMDeviceEnumerator_Release(it);
     LeaveMTA();
     return NULL;
 }
@@ -865,9 +911,12 @@ 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 *);
 
-    return SUCCEEDED(start(s, fmt, &GUID_VLC_AUD_OUT))
-        ? VLC_SUCCESS : VLC_EGENERIC;
+    *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)
@@ -881,11 +930,8 @@ static void aout_stream_Stop(void *func, va_list ap)
 static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
 {
     aout_sys_t *sys = aout->sys;
-
-    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
 
@@ -901,8 +947,19 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
     s->owner.activate = ActivateDevice;
 
     EnterMTA();
-    sys->module = vlc_module_load(s, "aout stream", NULL, false,
-                                  aout_stream_Start, s, fmt);
+    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();
 
     if (sys->module == NULL)
@@ -911,6 +968,7 @@ static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
         return -1;
     }
 
+    assert (sys->stream == NULL);
     sys->stream = s;
     return 0;
 }
@@ -933,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;
@@ -979,7 +1033,10 @@ static int Open(vlc_object_t *obj)
         goto error;
     }
 
-    DeviceSelect(aout, NULL);
+    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");
@@ -995,11 +1052,6 @@ static int Open(vlc_object_t *obj)
     aout->volume_set = VolumeSet;
     aout->mute_set = MuteSet;
     aout->device_select = DeviceSelect;
-    EnterMTA();
-    IMMDeviceEnumerator_RegisterEndpointNotificationCallback(sys->it,
-                                                          &sys->device_events);
-    DevicesEnum(aout, sys->it);
-    LeaveMTA();
     return VLC_SUCCESS;
 
 error:
@@ -1016,20 +1068,12 @@ 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
-    IMMDeviceEnumerator *it = sys->it;
-
-    EnterMTA(); /* Enter MTA before thread leaves MTA */
     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_UnregisterEndpointNotificationCallback(it,
-                                                          &sys->device_events);
-    IMMDeviceEnumerator_Release(it);
-    LeaveMTA();
-
     vlc_join(sys->thread, NULL);
     DeleteCriticalSection(&sys->lock);
 #else
@@ -1041,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);