From 351d6d478443fe8a1ede1e993712f07cb369d483 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 6 Dec 2017 12:24:32 -0500 Subject: [PATCH] audio: Port WASAPI to WinRT, remove XAudio2 backend. XAudio2 doesn't have capture support, so WASAPI was to replace it; the holdout was WinRT, which still needed it as its primary audio target until the WASAPI code code be made to work. The support matrix now looks like: WinXP: directsound by default, winmm as a fallback for buggy drivers. Vista+: WASAPI (directsound and winmm as fallbacks for debugging). WinRT: WASAPI --- CMakeLists.txt | 9 +- VisualC-WinRT/UWP_VS2015/SDL-UWP.vcxproj | 7 +- .../UWP_VS2015/SDL-UWP.vcxproj.filters | 9 +- .../WinPhone80_VS2012/SDL-WinPhone80.vcxproj | 14 +- .../SDL-WinPhone80.vcxproj.filters | 6 +- .../WinPhone81_VS2013/SDL-WinPhone81.vcxproj | 14 +- .../SDL-WinPhone81.vcxproj.filters | 6 +- .../WinRT80_VS2012/SDL-WinRT80.vcxproj | 18 +- .../SDL-WinRT80.vcxproj.filters | 6 +- .../WinRT81_VS2013/SDL-WinRT81.vcxproj | 18 +- .../SDL-WinRT81.vcxproj.filters | 6 +- VisualC/SDL/SDL.vcxproj | 5 +- VisualC/SDL/SDL.vcxproj.filters | 3 +- configure.in | 5 - docs/README-winrt.md | 5 +- include/SDL_config.h.cmake | 2 - include/SDL_config.h.in | 1 - include/SDL_config_windows.h | 1 - include/SDL_config_winrt.h | 2 +- src/SDL_internal.h | 2 +- src/audio/SDL_audio.c | 15 +- src/audio/SDL_sysaudio.h | 2 +- src/audio/wasapi/SDL_wasapi.c | 537 +++++------------- src/audio/wasapi/SDL_wasapi.h | 38 +- src/audio/wasapi/SDL_wasapi_win32.c | 418 ++++++++++++++ src/audio/wasapi/SDL_wasapi_winrt.cpp | 276 +++++++++ src/audio/xaudio2/SDL_xaudio2.c | 505 ---------------- src/audio/xaudio2/SDL_xaudio2.h | 386 ------------- .../xaudio2/SDL_xaudio2_winrthelpers.cpp | 90 --- src/audio/xaudio2/SDL_xaudio2_winrthelpers.h | 70 --- 30 files changed, 958 insertions(+), 1518 deletions(-) create mode 100644 src/audio/wasapi/SDL_wasapi_win32.c create mode 100644 src/audio/wasapi/SDL_wasapi_winrt.cpp delete mode 100644 src/audio/xaudio2/SDL_xaudio2.c delete mode 100644 src/audio/xaudio2/SDL_xaudio2.h delete mode 100644 src/audio/xaudio2/SDL_xaudio2_winrthelpers.cpp delete mode 100644 src/audio/xaudio2/SDL_xaudio2_winrthelpers.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b67d9dace..bfda8a1c2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1180,11 +1180,10 @@ elseif(WINDOWS) check_include_file(ddraw.h HAVE_DDRAW_H) check_include_file(dsound.h HAVE_DSOUND_H) check_include_file(dinput.h HAVE_DINPUT_H) - check_include_file(xaudio2.h HAVE_XAUDIO2_H) check_include_file(mmdeviceapi.h HAVE_MMDEVICEAPI_H) check_include_file(audioclient.h HAVE_AUDIOCLIENT_H) check_include_file(dxgi.h HAVE_DXGI_H) - if(HAVE_D3D_H OR HAVE_D3D11_H OR HAVE_DDRAW_H OR HAVE_DSOUND_H OR HAVE_DINPUT_H OR HAVE_XAUDIO2_H) + if(HAVE_D3D_H OR HAVE_D3D11_H OR HAVE_DDRAW_H OR HAVE_DSOUND_H OR HAVE_DINPUT_H) set(HAVE_DIRECTX TRUE) if(NOT CMAKE_COMPILER_IS_MINGW AND NOT USE_WINSDK_DIRECTX) # TODO: change $ENV{DXSDL_DIR} to get the path from the include checks @@ -1207,12 +1206,6 @@ elseif(WINDOWS) set(SOURCE_FILES ${SOURCE_FILES} ${DSOUND_AUDIO_SOURCES}) endif() - if(HAVE_XAUDIO2_H) - set(SDL_AUDIO_DRIVER_XAUDIO2 1) - file(GLOB XAUDIO2_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/xaudio2/*.c) - set(SOURCE_FILES ${SOURCE_FILES} ${XAUDIO2_AUDIO_SOURCES}) - endif() - if(HAVE_AUDIOCLIENT_H AND HAVE_MMDEVICEAPI_H) set(SDL_AUDIO_DRIVER_WASAPI 1) file(GLOB WASAPI_AUDIO_SOURCES ${SDL2_SOURCE_DIR}/src/audio/wasapi/*.c) diff --git a/VisualC-WinRT/UWP_VS2015/SDL-UWP.vcxproj b/VisualC-WinRT/UWP_VS2015/SDL-UWP.vcxproj index 841a0b966..5e1878712 100644 --- a/VisualC-WinRT/UWP_VS2015/SDL-UWP.vcxproj +++ b/VisualC-WinRT/UWP_VS2015/SDL-UWP.vcxproj @@ -84,8 +84,7 @@ - - + @@ -175,8 +174,8 @@ - - + + true true true diff --git a/VisualC-WinRT/UWP_VS2015/SDL-UWP.vcxproj.filters b/VisualC-WinRT/UWP_VS2015/SDL-UWP.vcxproj.filters index 5a2068996..9020a7418 100644 --- a/VisualC-WinRT/UWP_VS2015/SDL-UWP.vcxproj.filters +++ b/VisualC-WinRT/UWP_VS2015/SDL-UWP.vcxproj.filters @@ -183,9 +183,6 @@ Source Files - - Source Files - Source Files @@ -390,7 +387,7 @@ Source Files - + Source Files @@ -449,7 +446,7 @@ Source Files - + Source Files @@ -722,7 +719,7 @@ Source Files - + Source Files diff --git a/VisualC-WinRT/WinPhone80_VS2012/SDL-WinPhone80.vcxproj b/VisualC-WinRT/WinPhone80_VS2012/SDL-WinPhone80.vcxproj index 2091df090..40b0d26da 100644 --- a/VisualC-WinRT/WinPhone80_VS2012/SDL-WinPhone80.vcxproj +++ b/VisualC-WinRT/WinPhone80_VS2012/SDL-WinPhone80.vcxproj @@ -91,7 +91,7 @@ false false true - DXGI.lib;d3d11.lib;xaudio2.lib;WindowsPhoneCore.lib;RuntimeObject.lib;PhoneAppModelHost.lib;%(AdditionalDependencies) + DXGI.lib;d3d11.lib;WindowsPhoneCore.lib;RuntimeObject.lib;PhoneAppModelHost.lib;%(AdditionalDependencies) @@ -108,7 +108,7 @@ false false true - DXGI.lib;d3d11.lib;xaudio2.lib;WindowsPhoneCore.lib;RuntimeObject.lib;PhoneAppModelHost.lib;%(AdditionalDependencies) + DXGI.lib;d3d11.lib;WindowsPhoneCore.lib;RuntimeObject.lib;PhoneAppModelHost.lib;%(AdditionalDependencies) @@ -126,7 +126,7 @@ false false true - DXGI.lib;d3d11.lib;xaudio2.lib;WindowsPhoneCore.lib;RuntimeObject.lib;PhoneAppModelHost.lib;%(AdditionalDependencies) + DXGI.lib;d3d11.lib;WindowsPhoneCore.lib;RuntimeObject.lib;PhoneAppModelHost.lib;%(AdditionalDependencies) @@ -143,7 +143,7 @@ false false true - DXGI.lib;d3d11.lib;xaudio2.lib;WindowsPhoneCore.lib;RuntimeObject.lib;PhoneAppModelHost.lib;%(AdditionalDependencies) + DXGI.lib;d3d11.lib;WindowsPhoneCore.lib;RuntimeObject.lib;PhoneAppModelHost.lib;%(AdditionalDependencies) @@ -211,7 +211,7 @@ - + @@ -292,8 +292,8 @@ - - + + true true true diff --git a/VisualC-WinRT/WinPhone80_VS2012/SDL-WinPhone80.vcxproj.filters b/VisualC-WinRT/WinPhone80_VS2012/SDL-WinPhone80.vcxproj.filters index 9aafdce6c..64f4baa52 100644 --- a/VisualC-WinRT/WinPhone80_VS2012/SDL-WinPhone80.vcxproj.filters +++ b/VisualC-WinRT/WinPhone80_VS2012/SDL-WinPhone80.vcxproj.filters @@ -168,7 +168,7 @@ Source Files - + Source Files @@ -413,10 +413,10 @@ Source Files - + Source Files - + Source Files diff --git a/VisualC-WinRT/WinPhone81_VS2013/SDL-WinPhone81.vcxproj b/VisualC-WinRT/WinPhone81_VS2013/SDL-WinPhone81.vcxproj index f7b887cc6..4a04cc73e 100644 --- a/VisualC-WinRT/WinPhone81_VS2013/SDL-WinPhone81.vcxproj +++ b/VisualC-WinRT/WinPhone81_VS2013/SDL-WinPhone81.vcxproj @@ -76,7 +76,7 @@ - + @@ -158,8 +158,8 @@ - - + + true true true @@ -407,7 +407,7 @@ Console false false - DXGI.lib;d3d11.lib;xaudio2.lib;%(AdditionalDependencies) + DXGI.lib;d3d11.lib;%(AdditionalDependencies) @@ -421,7 +421,7 @@ Console false false - DXGI.lib;d3d11.lib;xaudio2.lib;%(AdditionalDependencies) + DXGI.lib;d3d11.lib;%(AdditionalDependencies) @@ -435,7 +435,7 @@ Console false false - DXGI.lib;d3d11.lib;xaudio2.lib;%(AdditionalDependencies) + DXGI.lib;d3d11.lib;%(AdditionalDependencies) @@ -449,7 +449,7 @@ Console false false - DXGI.lib;d3d11.lib;xaudio2.lib;%(AdditionalDependencies) + DXGI.lib;d3d11.lib;%(AdditionalDependencies) diff --git a/VisualC-WinRT/WinPhone81_VS2013/SDL-WinPhone81.vcxproj.filters b/VisualC-WinRT/WinPhone81_VS2013/SDL-WinPhone81.vcxproj.filters index 4bfbe8302..cd1af249b 100644 --- a/VisualC-WinRT/WinPhone81_VS2013/SDL-WinPhone81.vcxproj.filters +++ b/VisualC-WinRT/WinPhone81_VS2013/SDL-WinPhone81.vcxproj.filters @@ -183,7 +183,7 @@ Source Files - + Source Files @@ -420,10 +420,10 @@ Source Files - + Source Files - + Source Files diff --git a/VisualC-WinRT/WinRT80_VS2012/SDL-WinRT80.vcxproj b/VisualC-WinRT/WinRT80_VS2012/SDL-WinRT80.vcxproj index 2e1fc8ba6..b9e8f3c94 100644 --- a/VisualC-WinRT/WinRT80_VS2012/SDL-WinRT80.vcxproj +++ b/VisualC-WinRT/WinRT80_VS2012/SDL-WinRT80.vcxproj @@ -37,8 +37,8 @@ - - + + true true true @@ -293,7 +293,7 @@ - + @@ -483,7 +483,7 @@ Console false false - xinput.lib;xaudio2.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) + xinput.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) @@ -497,7 +497,7 @@ Console false false - xinput.lib;xaudio2.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) + xinput.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) @@ -511,7 +511,7 @@ Console false false - xinput.lib;xaudio2.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) + xinput.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) @@ -525,7 +525,7 @@ Console false false - xinput.lib;xaudio2.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) + xinput.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) @@ -539,7 +539,7 @@ Console false false - xinput.lib;xaudio2.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) + xinput.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) @@ -553,7 +553,7 @@ Console false false - xinput.lib;xaudio2.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) + xinput.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) diff --git a/VisualC-WinRT/WinRT80_VS2012/SDL-WinRT80.vcxproj.filters b/VisualC-WinRT/WinRT80_VS2012/SDL-WinRT80.vcxproj.filters index 3e2f4c68e..a59397241 100644 --- a/VisualC-WinRT/WinRT80_VS2012/SDL-WinRT80.vcxproj.filters +++ b/VisualC-WinRT/WinRT80_VS2012/SDL-WinRT80.vcxproj.filters @@ -214,10 +214,10 @@ Source Files - + Source Files - + Source Files @@ -618,7 +618,7 @@ Source Files - + Source Files diff --git a/VisualC-WinRT/WinRT81_VS2013/SDL-WinRT81.vcxproj b/VisualC-WinRT/WinRT81_VS2013/SDL-WinRT81.vcxproj index d3f34d7a2..83b57e90c 100644 --- a/VisualC-WinRT/WinRT81_VS2013/SDL-WinRT81.vcxproj +++ b/VisualC-WinRT/WinRT81_VS2013/SDL-WinRT81.vcxproj @@ -84,7 +84,7 @@ - + @@ -172,8 +172,8 @@ - - + + true true true @@ -484,7 +484,7 @@ Console false false - xinput.lib;xaudio2.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) + xinput.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) @@ -498,7 +498,7 @@ Console false false - xinput.lib;xaudio2.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) + xinput.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) @@ -512,7 +512,7 @@ Console false false - xinput.lib;xaudio2.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) + xinput.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) @@ -526,7 +526,7 @@ Console false false - xinput.lib;xaudio2.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) + xinput.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) @@ -540,7 +540,7 @@ Console false false - xinput.lib;xaudio2.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) + xinput.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) @@ -554,7 +554,7 @@ Console false false - xinput.lib;xaudio2.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) + xinput.lib;d2d1.lib;d3d11.lib;dxgi.lib;ole32.lib;windowscodecs.lib;dwrite.lib;kernel32.lib;%(AdditionalDependencies) diff --git a/VisualC-WinRT/WinRT81_VS2013/SDL-WinRT81.vcxproj.filters b/VisualC-WinRT/WinRT81_VS2013/SDL-WinRT81.vcxproj.filters index b499c70a8..dafd707e8 100644 --- a/VisualC-WinRT/WinRT81_VS2013/SDL-WinRT81.vcxproj.filters +++ b/VisualC-WinRT/WinRT81_VS2013/SDL-WinRT81.vcxproj.filters @@ -183,7 +183,7 @@ Source Files - + Source Files @@ -440,10 +440,10 @@ Source Files - + Source Files - + Source Files diff --git a/VisualC/SDL/SDL.vcxproj b/VisualC/SDL/SDL.vcxproj index a4fb4478d..a3d6f86d8 100644 --- a/VisualC/SDL/SDL.vcxproj +++ b/VisualC/SDL/SDL.vcxproj @@ -291,6 +291,7 @@ + @@ -389,9 +390,9 @@ - - + + diff --git a/VisualC/SDL/SDL.vcxproj.filters b/VisualC/SDL/SDL.vcxproj.filters index dcfd74a70..ce33f0599 100644 --- a/VisualC/SDL/SDL.vcxproj.filters +++ b/VisualC/SDL/SDL.vcxproj.filters @@ -438,7 +438,8 @@ - + + diff --git a/configure.in b/configure.in index e55d02e3e..3d42b8410 100644 --- a/configure.in +++ b/configure.in @@ -2978,7 +2978,6 @@ AC_HELP_STRING([--enable-directx], [use DirectX for Windows audio/video [[defaul AC_CHECK_HEADER(dsound.h, have_dsound=yes) AC_CHECK_HEADER(dinput.h, have_dinput=yes) AC_CHECK_HEADER(dxgi.h, have_dxgi=yes) - AC_CHECK_HEADER(xaudio2.h, have_xaudio2=yes) AC_CHECK_HEADER(xinput.h, have_xinput=yes) AC_CHECK_HEADER(mmdeviceapi.h, have_wasapi=yes) AC_CHECK_HEADER(audioclient.h,,have_wasapi=no) @@ -3486,10 +3485,6 @@ AC_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau AC_DEFINE(SDL_AUDIO_DRIVER_DSOUND, 1, [ ]) SOURCES="$SOURCES $srcdir/src/audio/directsound/*.c" fi - if test x$have_xaudio2 = xyes; then - AC_DEFINE(SDL_AUDIO_DRIVER_XAUDIO2, 1, [ ]) - SOURCES="$SOURCES $srcdir/src/audio/xaudio2/*.c" - fi if test x$have_wasapi = xyes; then AC_DEFINE(SDL_AUDIO_DRIVER_WASAPI, 1, [ ]) SOURCES="$SOURCES $srcdir/src/audio/wasapi/*.c" diff --git a/docs/README-winrt.md b/docs/README-winrt.md index 655b2a05d..d04f710fc 100644 --- a/docs/README-winrt.md +++ b/docs/README-winrt.md @@ -70,7 +70,10 @@ Here is a rough list of what works, and what doesn't: SDL_GetPerformanceFrequency(), etc.) * file I/O via SDL_RWops * mouse input (unsupported on Windows Phone) - * audio, via a modified version of SDL's XAudio2 backend + * audio, via SDL's WASAPI backend (if you want to record, your app must + have "Microphone" capabilities enabled in its manifest, and the user must + not have blocked access. Otherwise, capture devices will fail to work, + presenting as a device disconnect shortly after opening it.) * .DLL file loading. Libraries *MUST* be packaged inside applications. Loading anything outside of the app is not supported. * system path retrieval via SDL's filesystem APIs diff --git a/include/SDL_config.h.cmake b/include/SDL_config.h.cmake index 551e999d4..0ddaffebd 100644 --- a/include/SDL_config.h.cmake +++ b/include/SDL_config.h.cmake @@ -202,7 +202,6 @@ #cmakedefine HAVE_DDRAW_H @HAVE_DDRAW_H@ #cmakedefine HAVE_DSOUND_H @HAVE_DSOUND_H@ #cmakedefine HAVE_DINPUT_H @HAVE_DINPUT_H@ -#cmakedefine HAVE_XAUDIO2_H @HAVE_XAUDIO2_H@ #cmakedefine HAVE_XINPUT_H @HAVE_XINPUT_H@ #cmakedefine HAVE_DXGI_H @HAVE_DXGI_H@ #cmakedefine HAVE_XINPUT_GAMEPAD_EX @HAVE_XINPUT_GAMEPAD_EX@ @@ -259,7 +258,6 @@ #cmakedefine SDL_AUDIO_DRIVER_SUNAUDIO @SDL_AUDIO_DRIVER_SUNAUDIO@ #cmakedefine SDL_AUDIO_DRIVER_WASAPI @SDL_AUDIO_DRIVER_WASAPI@ #cmakedefine SDL_AUDIO_DRIVER_WINMM @SDL_AUDIO_DRIVER_WINMM@ -#cmakedefine SDL_AUDIO_DRIVER_XAUDIO2 @SDL_AUDIO_DRIVER_XAUDIO2@ /* Enable various input drivers */ #cmakedefine SDL_INPUT_LINUXEV @SDL_INPUT_LINUXEV@ diff --git a/include/SDL_config.h.in b/include/SDL_config.h.in index b137429f0..cd38126f6 100644 --- a/include/SDL_config.h.in +++ b/include/SDL_config.h.in @@ -259,7 +259,6 @@ #undef SDL_AUDIO_DRIVER_SUNAUDIO #undef SDL_AUDIO_DRIVER_WASAPI #undef SDL_AUDIO_DRIVER_WINMM -#undef SDL_AUDIO_DRIVER_XAUDIO2 /* Enable various input drivers */ #undef SDL_INPUT_LINUXEV diff --git a/include/SDL_config_windows.h b/include/SDL_config_windows.h index 61881eeda..990993ba6 100644 --- a/include/SDL_config_windows.h +++ b/include/SDL_config_windows.h @@ -179,7 +179,6 @@ typedef unsigned int uintptr_t; /* Enable various audio drivers */ #define SDL_AUDIO_DRIVER_WASAPI 1 #define SDL_AUDIO_DRIVER_DSOUND 1 -#define SDL_AUDIO_DRIVER_XAUDIO2 0 #define SDL_AUDIO_DRIVER_WINMM 1 #define SDL_AUDIO_DRIVER_DISK 1 #define SDL_AUDIO_DRIVER_DUMMY 1 diff --git a/include/SDL_config_winrt.h b/include/SDL_config_winrt.h index 510535e2e..aadd74c69 100644 --- a/include/SDL_config_winrt.h +++ b/include/SDL_config_winrt.h @@ -175,7 +175,7 @@ typedef unsigned int uintptr_t; #define HAVE__FSEEKI64 1 /* Enable various audio drivers */ -#define SDL_AUDIO_DRIVER_XAUDIO2 1 +#define SDL_AUDIO_DRIVER_WASAPI 1 #define SDL_AUDIO_DRIVER_DISK 1 #define SDL_AUDIO_DRIVER_DUMMY 1 diff --git a/src/SDL_internal.h b/src/SDL_internal.h index 654895cbe..5088926da 100644 --- a/src/SDL_internal.h +++ b/src/SDL_internal.h @@ -29,7 +29,7 @@ /* This is for a variable-length array at the end of a struct: struct x { int y; char z[SDL_VARIABLE_LENGTH_ARRAY]; }; Use this because GCC 2 needs different magic than other compilers. */ -#if (defined(__GNUC__) && (__GNUC__ <= 2)) || defined(__CC_ARM) +#if (defined(__GNUC__) && (__GNUC__ <= 2)) || defined(__CC_ARM) || defined(__cplusplus) #define SDL_VARIABLE_LENGTH_ARRAY 1 #else #define SDL_VARIABLE_LENGTH_ARRAY diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c index 19cbf72c9..52007935c 100644 --- a/src/audio/SDL_audio.c +++ b/src/audio/SDL_audio.c @@ -71,9 +71,6 @@ static const AudioBootStrap *const bootstrap[] = { #if SDL_AUDIO_DRIVER_WASAPI &WASAPI_bootstrap, #endif -#if SDL_AUDIO_DRIVER_XAUDIO2 - &XAUDIO2_bootstrap, -#endif #if SDL_AUDIO_DRIVER_DSOUND &DSOUND_bootstrap, #endif @@ -233,6 +230,11 @@ SDL_AudioThreadDeinit_Default(_THIS) { /* no-op. */ } +static void +SDL_AudioBeginLoopIteration_Default(_THIS) +{ /* no-op. */ +} + static void SDL_AudioWaitDevice_Default(_THIS) { /* no-op. */ @@ -353,6 +355,7 @@ finish_audio_entry_points_init(void) FILL_STUB(OpenDevice); FILL_STUB(ThreadInit); FILL_STUB(ThreadDeinit); + FILL_STUB(BeginLoopIteration); FILL_STUB(WaitDevice); FILL_STUB(PlayDevice); FILL_STUB(GetPendingBytes); @@ -642,6 +645,7 @@ SDL_RunAudio(void *devicep) SDL_AudioDevice *device = (SDL_AudioDevice *) devicep; void *udata = device->callbackspec.userdata; SDL_AudioCallback callback = device->callbackspec.callback; + int data_len = 0; Uint8 *data; SDL_assert(!device->iscapture); @@ -655,7 +659,8 @@ SDL_RunAudio(void *devicep) /* Loop, filling the audio buffers */ while (!SDL_AtomicGet(&device->shutdown)) { - const int data_len = device->callbackspec.size; + current_audio.impl.BeginLoopIteration(device); + data_len = device->callbackspec.size; /* Fill the current buffer with sound */ if (!device->stream && SDL_AtomicGet(&device->enabled)) { @@ -754,6 +759,8 @@ SDL_CaptureAudio(void *devicep) int still_need; Uint8 *ptr; + current_audio.impl.BeginLoopIteration(device); + if (SDL_AtomicGet(&device->paused)) { SDL_Delay(delay); /* just so we don't cook the CPU. */ if (device->stream) { diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h index d095b779f..6a6ecb595 100644 --- a/src/audio/SDL_sysaudio.h +++ b/src/audio/SDL_sysaudio.h @@ -68,6 +68,7 @@ typedef struct SDL_AudioDriverImpl int (*OpenDevice) (_THIS, void *handle, const char *devname, int iscapture); void (*ThreadInit) (_THIS); /* Called by audio thread at start */ void (*ThreadDeinit) (_THIS); /* Called by audio thread at end */ + void (*BeginLoopIteration)(_THIS); /* Called by audio thread at top of loop */ void (*WaitDevice) (_THIS); void (*PlayDevice) (_THIS); int (*GetPendingBytes) (_THIS); @@ -193,7 +194,6 @@ extern AudioBootStrap ESD_bootstrap; extern AudioBootStrap NACLAUDIO_bootstrap; extern AudioBootStrap NAS_bootstrap; extern AudioBootStrap WASAPI_bootstrap; -extern AudioBootStrap XAUDIO2_bootstrap; extern AudioBootStrap DSOUND_bootstrap; extern AudioBootStrap WINMM_bootstrap; extern AudioBootStrap PAUDIO_bootstrap; diff --git a/src/audio/wasapi/SDL_wasapi.c b/src/audio/wasapi/SDL_wasapi.c index c448c370c..133f8fe83 100644 --- a/src/audio/wasapi/SDL_wasapi.c +++ b/src/audio/wasapi/SDL_wasapi.c @@ -37,14 +37,9 @@ #include "SDL_wasapi.h" -static const ERole SDL_WASAPI_role = eConsole; /* !!! FIXME: should this be eMultimedia? Should be a hint? */ - -/* This is global to the WASAPI target, to handle hotplug and default device lookup. */ -static IMMDeviceEnumerator *enumerator = NULL; - /* these increment as default devices change. Opened default devices pick up changes in their threads. */ -static SDL_atomic_t default_playback_generation; -static SDL_atomic_t default_capture_generation; +SDL_atomic_t WASAPI_DefaultPlaybackGeneration; +SDL_atomic_t WASAPI_DefaultCaptureGeneration; /* This is a list of device id strings we have inflight, so we have consistent pointers to the same device. */ typedef struct DevIdList @@ -55,173 +50,11 @@ typedef struct DevIdList static DevIdList *deviceid_list = NULL; -/* handle to Avrt.dll--Vista and later!--for flagging the callback thread as "Pro Audio" (low latency). */ -#ifndef __WINRT__ -static HMODULE libavrt = NULL; -#endif -typedef HANDLE (WINAPI *pfnAvSetMmThreadCharacteristicsW)(LPWSTR,LPDWORD); -typedef BOOL (WINAPI *pfnAvRevertMmThreadCharacteristics)(HANDLE); -static pfnAvSetMmThreadCharacteristicsW pAvSetMmThreadCharacteristicsW = NULL; -static pfnAvRevertMmThreadCharacteristics pAvRevertMmThreadCharacteristics = NULL; - /* Some GUIDs we need to know without linking to libraries that aren't available before Vista. */ -static const CLSID SDL_CLSID_MMDeviceEnumerator = { 0xbcde0395, 0xe52f, 0x467c, { 0x8e, 0x3d, 0xc4, 0x57, 0x92, 0x91, 0x69, 0x2e } }; -static const IID SDL_IID_IMMDeviceEnumerator = { 0xa95664d2, 0x9614, 0x4f35, { 0xa7, 0x46, 0xde, 0x8d, 0xb6, 0x36, 0x17, 0xe6 } }; -static const IID SDL_IID_IMMNotificationClient = { 0x7991eec9, 0x7e89, 0x4d85, { 0x83, 0x90, 0x6c, 0x70, 0x3c, 0xec, 0x60, 0xc0 } }; -static const IID SDL_IID_IMMEndpoint = { 0x1be09788, 0x6894, 0x4089, { 0x85, 0x86, 0x9a, 0x2a, 0x6c, 0x26, 0x5a, 0xc5 } }; -static const IID SDL_IID_IAudioClient = { 0x1cb9ad4c, 0xdbfa, 0x4c32, { 0xb1, 0x78, 0xc2, 0xf5, 0x68, 0xa7, 0x03, 0xb2 } }; -static const IID SDL_IID_IAudioRenderClient = { 0xf294acfc, 0x3146, 0x4483, { 0xa7, 0xbf, 0xad, 0xdc, 0xa7, 0xc2, 0x60, 0xe2 } }; -static const IID SDL_IID_IAudioCaptureClient = { 0xc8adbd64, 0xe71e, 0x48a0, { 0xa4, 0xde, 0x18, 0x5c, 0x39, 0x5c, 0xd3, 0x17 } }; -static const GUID SDL_KSDATAFORMAT_SUBTYPE_PCM = { 0x00000001, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } }; -static const GUID SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = { 0x00000003, 0x0000, 0x0010, { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } }; -static const PROPERTYKEY SDL_PKEY_Device_FriendlyName = { { 0xa45c254e, 0xdf1c, 0x4efd, { 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, } }, 14 }; - -/* PropVariantInit() is an inline function/macro in PropIdl.h that calls the C runtime's memset() directly. Use ours instead, to avoid dependency. */ -#ifdef PropVariantInit -#undef PropVariantInit -#endif -#define PropVariantInit(p) SDL_zerop(p) - -static void AddWASAPIDevice(const SDL_bool iscapture, IMMDevice *device, LPCWSTR devid); -static void RemoveWASAPIDevice(const SDL_bool iscapture, LPCWSTR devid); - -/* We need a COM subclass of IMMNotificationClient for hotplug support, which is - easy in C++, but we have to tapdance more to make work in C. - Thanks to this page for coaching on how to make this work: - https://www.codeproject.com/Articles/13601/COM-in-plain-C */ - -typedef struct SDLMMNotificationClient -{ - const IMMNotificationClientVtbl *lpVtbl; - SDL_atomic_t refcount; -} SDLMMNotificationClient; - -static HRESULT STDMETHODCALLTYPE -SDLMMNotificationClient_QueryInterface(IMMNotificationClient *this, REFIID iid, void **ppv) -{ - if ((WIN_IsEqualIID(iid, &IID_IUnknown)) || (WIN_IsEqualIID(iid, &SDL_IID_IMMNotificationClient))) - { - *ppv = this; - this->lpVtbl->AddRef(this); - return S_OK; - } - - *ppv = NULL; - return E_NOINTERFACE; -} - -static ULONG STDMETHODCALLTYPE -SDLMMNotificationClient_AddRef(IMMNotificationClient *ithis) -{ - SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis; - return (ULONG) (SDL_AtomicIncRef(&this->refcount) + 1); -} - -static ULONG STDMETHODCALLTYPE -SDLMMNotificationClient_Release(IMMNotificationClient *ithis) -{ - /* this is a static object; we don't ever free it. */ - SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis; - const ULONG retval = SDL_AtomicDecRef(&this->refcount); - if (retval == 0) { - SDL_AtomicSet(&this->refcount, 0); /* uhh... */ - return 0; - } - return retval - 1; -} - -/* These are the entry points called when WASAPI device endpoints change. */ -static HRESULT STDMETHODCALLTYPE -SDLMMNotificationClient_OnDefaultDeviceChanged(IMMNotificationClient *ithis, EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) -{ - if (role != SDL_WASAPI_role) { - return S_OK; /* ignore it. */ - } - - /* Increment the "generation," so opened devices will pick this up in their threads. */ - switch (flow) { - case eRender: - SDL_AtomicAdd(&default_playback_generation, 1); - break; - - case eCapture: - SDL_AtomicAdd(&default_capture_generation, 1); - break; - - case eAll: - SDL_AtomicAdd(&default_playback_generation, 1); - SDL_AtomicAdd(&default_capture_generation, 1); - break; - - default: - SDL_assert(!"uhoh, unexpected OnDefaultDeviceChange flow!"); - break; - } - - return S_OK; -} - -static HRESULT STDMETHODCALLTYPE -SDLMMNotificationClient_OnDeviceAdded(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId) -{ - /* we ignore this; devices added here then progress to ACTIVE, if appropriate, in - OnDeviceStateChange, making that a better place to deal with device adds. More - importantly: the first time you plug in a USB audio device, this callback will - fire, but when you unplug it, it isn't removed (it's state changes to NOTPRESENT). - Plugging it back in won't fire this callback again. */ - return S_OK; -} - -static HRESULT STDMETHODCALLTYPE -SDLMMNotificationClient_OnDeviceRemoved(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId) -{ - /* See notes in OnDeviceAdded handler about why we ignore this. */ - return S_OK; -} - -static HRESULT STDMETHODCALLTYPE -SDLMMNotificationClient_OnDeviceStateChanged(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId, DWORD dwNewState) -{ - IMMDevice *device = NULL; - - if (SUCCEEDED(IMMDeviceEnumerator_GetDevice(enumerator, pwstrDeviceId, &device))) { - IMMEndpoint *endpoint = NULL; - if (SUCCEEDED(IMMDevice_QueryInterface(device, &SDL_IID_IMMEndpoint, (void **) &endpoint))) { - EDataFlow flow; - if (SUCCEEDED(IMMEndpoint_GetDataFlow(endpoint, &flow))) { - const SDL_bool iscapture = (flow == eCapture); - if (dwNewState == DEVICE_STATE_ACTIVE) { - AddWASAPIDevice(iscapture, device, pwstrDeviceId); - } else { - RemoveWASAPIDevice(iscapture, pwstrDeviceId); - } - } - IMMEndpoint_Release(endpoint); - } - IMMDevice_Release(device); - } - - return S_OK; -} - -static HRESULT STDMETHODCALLTYPE -SDLMMNotificationClient_OnPropertyValueChanged(IMMNotificationClient *this, LPCWSTR pwstrDeviceId, const PROPERTYKEY key) -{ - return S_OK; /* we don't care about these. */ -} - -static const IMMNotificationClientVtbl notification_client_vtbl = { - SDLMMNotificationClient_QueryInterface, - SDLMMNotificationClient_AddRef, - SDLMMNotificationClient_Release, - SDLMMNotificationClient_OnDeviceStateChanged, - SDLMMNotificationClient_OnDeviceAdded, - SDLMMNotificationClient_OnDeviceRemoved, - SDLMMNotificationClient_OnDefaultDeviceChanged, - SDLMMNotificationClient_OnPropertyValueChanged -}; - -static SDLMMNotificationClient notification_client = { ¬ification_client_vtbl, { 1 } }; +static const IID SDL_IID_IAudioRenderClient = { 0xf294acfc, 0x3146, 0x4483,{ 0xa7, 0xbf, 0xad, 0xdc, 0xa7, 0xc2, 0x60, 0xe2 } }; +static const IID SDL_IID_IAudioCaptureClient = { 0xc8adbd64, 0xe71e, 0x48a0,{ 0xa4, 0xde, 0x18, 0x5c, 0x39, 0x5c, 0xd3, 0x17 } }; +static const GUID SDL_KSDATAFORMAT_SUBTYPE_PCM = { 0x00000001, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } }; +static const GUID SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = { 0x00000003, 0x0000, 0x0010,{ 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 } }; static SDL_bool WStrEqual(const WCHAR *a, const WCHAR *b) @@ -236,10 +69,22 @@ WStrEqual(const WCHAR *a, const WCHAR *b) return *b == 0; } +static size_t +WStrLen(const WCHAR *wstr) +{ + size_t retval = 0; + if (wstr) { + while (*(wstr++)) { + retval++; + } + } + return retval; +} + static WCHAR * WStrDupe(const WCHAR *wstr) { - const int len = (lstrlenW(wstr) + 1) * sizeof (WCHAR); + const int len = (WStrLen(wstr) + 1) * sizeof (WCHAR); WCHAR *retval = (WCHAR *) SDL_malloc(len); if (retval) { SDL_memcpy(retval, wstr, len); @@ -247,8 +92,9 @@ WStrDupe(const WCHAR *wstr) return retval; } -static void -RemoveWASAPIDevice(const SDL_bool iscapture, LPCWSTR devid) + +void +WASAPI_RemoveDevice(const SDL_bool iscapture, LPCWSTR devid) { DevIdList *i; DevIdList *next; @@ -269,23 +115,16 @@ RemoveWASAPIDevice(const SDL_bool iscapture, LPCWSTR devid) } } -static void -AddWASAPIDevice(const SDL_bool iscapture, IMMDevice *device, LPCWSTR devid) +void +WASAPI_AddDevice(const SDL_bool iscapture, const char *devname, LPCWSTR devid) { - IPropertyStore *props = NULL; - char *utf8dev = NULL; DevIdList *devidlist; - PROPVARIANT var; /* You can have multiple endpoints on a device that are mutually exclusive ("Speakers" vs "Line Out" or whatever). In a perfect world, things that are unplugged won't be in this collection. The only gotcha is probably for phones and tablets, where you might have an internal speaker and a headphone jack and expect both to be available and switch automatically. (!!! FIXME...?) */ - /* PKEY_Device_FriendlyName gives you "Speakers (SoundBlaster Pro)" which drives me nuts. I'd rather it be - "SoundBlaster Pro (Speakers)" but I guess that's developers vs users. Windows uses the FriendlyName in - its own UIs, like Volume Control, etc. */ - /* see if we already have this one. */ for (devidlist = deviceid_list; devidlist; devidlist = devidlist->next) { if (WStrEqual(devidlist->str, devid)) { @@ -308,61 +147,13 @@ AddWASAPIDevice(const SDL_bool iscapture, IMMDevice *device, LPCWSTR devid) devidlist->next = deviceid_list; deviceid_list = devidlist; - if (SUCCEEDED(IMMDevice_OpenPropertyStore(device, STGM_READ, &props))) { - PropVariantInit(&var); - if (SUCCEEDED(IPropertyStore_GetValue(props, &SDL_PKEY_Device_FriendlyName, &var))) { - utf8dev = WIN_StringToUTF8(var.pwszVal); - if (utf8dev) { - SDL_AddAudioDevice(iscapture, utf8dev, (void *) devid); - SDL_free(utf8dev); - } - } - PropVariantClear(&var); - IPropertyStore_Release(props); - } -} - -static void -EnumerateEndpoints(const SDL_bool iscapture) -{ - IMMDeviceCollection *collection = NULL; - UINT i, total; - - /* Note that WASAPI separates "adapter devices" from "audio endpoint devices" - ...one adapter device ("SoundBlaster Pro") might have multiple endpoint devices ("Speakers", "Line-Out"). */ - - if (FAILED(IMMDeviceEnumerator_EnumAudioEndpoints(enumerator, iscapture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &collection))) { - return; - } - - if (FAILED(IMMDeviceCollection_GetCount(collection, &total))) { - IMMDeviceCollection_Release(collection); - return; - } - - for (i = 0; i < total; i++) { - IMMDevice *device = NULL; - if (SUCCEEDED(IMMDeviceCollection_Item(collection, i, &device))) { - LPWSTR devid = NULL; - if (SUCCEEDED(IMMDevice_GetId(device, &devid))) { - AddWASAPIDevice(iscapture, device, devid); - CoTaskMemFree(devid); - } - IMMDevice_Release(device); - } - } - - IMMDeviceCollection_Release(collection); + SDL_AddAudioDevice(iscapture, devname, (void *) devid); } static void WASAPI_DetectDevices(void) { - EnumerateEndpoints(SDL_FALSE); /* playback */ - EnumerateEndpoints(SDL_TRUE); /* capture */ - - /* if this fails, we just won't get hotplug events. Carry on anyhow. */ - IMMDeviceEnumerator_RegisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *) ¬ification_client); + WASAPI_EnumerateEndpoints(); } static int @@ -372,10 +163,11 @@ WASAPI_GetPendingBytes(_THIS) /* it's okay to fail here; we'll deal with failures in the audio thread. */ /* FIXME: need a lock around checking this->hidden->client */ - if (!this->hidden->client || FAILED(IAudioClient_GetCurrentPadding(this->hidden->client, &frames))) { - return 0; /* oh well. */ + if (this->hidden->client != NULL) { /* definitely activated? */ + if (FAILED(IAudioClient_GetCurrentPadding(this->hidden->client, &frames))) { + return 0; /* oh well. */ + } } - return ((int) frames) * this->hidden->framesize; } @@ -397,41 +189,9 @@ WasapiFailed(_THIS, const HRESULT err) return SDL_TRUE; } -static int PrepWasapiDevice(_THIS, const int iscapture, IMMDevice *device); -static void ReleaseWasapiDevice(_THIS); - -static SDL_bool -RecoverWasapiDevice(_THIS) +static int +UpdateAudioStream(_THIS, const SDL_AudioSpec *oldspec) { - const SDL_AudioSpec oldspec = this->spec; - IMMDevice *device = NULL; - HRESULT ret = S_OK; - - if (this->hidden->default_device_generation) { - const EDataFlow dataflow = this->iscapture ? eCapture : eRender; - ReleaseWasapiDevice(this); /* dump the lost device's handles. */ - this->hidden->default_device_generation = SDL_AtomicGet(this->iscapture ? &default_capture_generation : &default_playback_generation); - ret = IMMDeviceEnumerator_GetDefaultAudioEndpoint(enumerator, dataflow, SDL_WASAPI_role, &device); - if (FAILED(ret)) { - return SDL_FALSE; /* can't find a new default device! */ - } - } else { - device = this->hidden->device; - this->hidden->device = NULL; /* don't release this in ReleaseWasapiDevice(). */ - ReleaseWasapiDevice(this); /* dump the lost device's handles. */ - } - - SDL_assert(device != NULL); - - /* this can fail for lots of reasons, but the most likely is we had a - non-default device that was disconnected, so we can't recover. Default - devices try to reinitialize whatever the new default is, so it's more - likely to carry on here, but this handles a non-default device that - simply had its format changed in the Windows Control Panel. */ - if (PrepWasapiDevice(this, this->iscapture, device) == -1) { - return SDL_FALSE; - } - /* Since WASAPI requires us to handle all audio conversion, and our device format might have changed, we might have to add/remove/change the audio stream that the higher level uses to convert data, so @@ -444,9 +204,9 @@ RecoverWasapiDevice(_THIS) /* no need to buffer/convert in an AudioStream! */ SDL_FreeAudioStream(this->stream); this->stream = NULL; - } else if ( (oldspec.channels == this->spec.channels) && - (oldspec.format == this->spec.format) && - (oldspec.freq == this->spec.freq) ) { + } else if ( (oldspec->channels == this->spec.channels) && + (oldspec->format == this->spec.format) && + (oldspec->freq == this->spec.freq) ) { /* The existing audio stream is okay to keep using. */ } else { /* replace the audiostream for new format */ @@ -465,7 +225,7 @@ RecoverWasapiDevice(_THIS) } if (!this->stream) { - return SDL_FALSE; + return -1; } } @@ -473,13 +233,37 @@ RecoverWasapiDevice(_THIS) if (this->spec.size > this->work_buffer_len) { Uint8 *ptr = (Uint8 *) SDL_realloc(this->work_buffer, this->spec.size); if (ptr == NULL) { - SDL_OutOfMemory(); - return SDL_FALSE; + return SDL_OutOfMemory(); } this->work_buffer = ptr; this->work_buffer_len = this->spec.size; } + return 0; +} + + +static void ReleaseWasapiDevice(_THIS); + +static SDL_bool +RecoverWasapiDevice(_THIS) +{ + ReleaseWasapiDevice(this); /* dump the lost device's handles. */ + + if (this->hidden->default_device_generation) { + this->hidden->default_device_generation = SDL_AtomicGet(this->iscapture ? &WASAPI_DefaultCaptureGeneration : &WASAPI_DefaultPlaybackGeneration); + } + + /* this can fail for lots of reasons, but the most likely is we had a + non-default device that was disconnected, so we can't recover. Default + devices try to reinitialize whatever the new default is, so it's more + likely to carry on here, but this handles a non-default device that + simply had its format changed in the Windows Control Panel. */ + if (WASAPI_ActivateDevice(this, SDL_TRUE) == -1) { + SDL_OpenedAudioDeviceDisconnected(this); + return SDL_FALSE; + } + this->hidden->device_lost = SDL_FALSE; return SDL_TRUE; /* okay, carry on with new device details! */ @@ -495,8 +279,12 @@ RecoverWasapiIfLost(_THIS) return SDL_FALSE; /* already failed. */ } + if (!this->hidden->client) { + return SDL_TRUE; /* still waiting for activation. */ + } + if (!lost && (generation > 0)) { /* is a default device? */ - const int newgen = SDL_AtomicGet(this->iscapture ? &default_capture_generation : &default_playback_generation); + const int newgen = SDL_AtomicGet(this->iscapture ? &WASAPI_DefaultCaptureGeneration : &WASAPI_DefaultPlaybackGeneration); if (generation != newgen) { /* the desired default device was changed, jump over to it. */ lost = SDL_TRUE; } @@ -511,7 +299,7 @@ WASAPI_GetDeviceBuf(_THIS) /* get an endpoint buffer from WASAPI. */ BYTE *buffer = NULL; - while (RecoverWasapiIfLost(this)) { + while (RecoverWasapiIfLost(this) && this->hidden->render) { if (!WasapiFailed(this, IAudioRenderClient_GetBuffer(this->hidden->render, this->spec.samples, &buffer))) { return (Uint8 *) buffer; } @@ -524,17 +312,18 @@ WASAPI_GetDeviceBuf(_THIS) static void WASAPI_PlayDevice(_THIS) { - /* WasapiFailed() will mark the device for reacquisition or removal elsewhere. */ - WasapiFailed(this, IAudioRenderClient_ReleaseBuffer(this->hidden->render, this->spec.samples, 0)); + if (this->hidden->render != NULL) { /* definitely activated? */ + /* WasapiFailed() will mark the device for reacquisition or removal elsewhere. */ + WasapiFailed(this, IAudioRenderClient_ReleaseBuffer(this->hidden->render, this->spec.samples, 0)); + } } static void WASAPI_WaitDevice(_THIS) { - const UINT32 maxpadding = this->spec.samples; - while (RecoverWasapiIfLost(this)) { + while (RecoverWasapiIfLost(this) && this->hidden->client) { + const UINT32 maxpadding = this->spec.samples; UINT32 padding = 0; - if (!WasapiFailed(this, IAudioClient_GetCurrentPadding(this->hidden->client, &padding))) { if (padding <= maxpadding) { break; @@ -562,6 +351,14 @@ WASAPI_CaptureFromDevice(_THIS, void *buffer, int buflen) UINT32 frames = 0; DWORD flags = 0; + /* uhoh, client isn't activated yet, just return silence. */ + if (!this->hidden->capture) { + /* Delay so we run at about the speed that audio would be arriving. */ + SDL_Delay(((this->spec.samples * 1000) / this->spec.freq)); + SDL_memset(buffer, this->spec.silence, buflen); + return buflen; + } + ret = IAudioCaptureClient_GetBuffer(this->hidden->capture, &ptr, &frames, &flags, NULL, NULL); if (ret != AUDCLNT_S_BUFFER_EMPTY) { WasapiFailed(this, ret); /* mark device lost/failed if necessary. */ @@ -609,6 +406,10 @@ WASAPI_FlushCapture(_THIS) UINT32 frames = 0; DWORD flags = 0; + if (!this->hidden->capture) { + return; /* not activated yet? */ + } + /* just read until we stop getting packets, throwing them away. */ while (SDL_TRUE) { const HRESULT ret = IAudioCaptureClient_GetBuffer(this->hidden->capture, &ptr, &frames, &flags, NULL, NULL); @@ -646,30 +447,49 @@ ReleaseWasapiDevice(_THIS) this->hidden->waveformat = NULL; } - if (this->hidden->device) { - IMMDevice_Release(this->hidden->device); - this->hidden->device = NULL; - } - if (this->hidden->capturestream) { SDL_FreeAudioStream(this->hidden->capturestream); this->hidden->capturestream = NULL; } + + if (this->hidden->activation_handler) { + WASAPI_PlatformDeleteActivationHandler(this->hidden->activation_handler); + this->hidden->activation_handler = NULL; + } } static void WASAPI_CloseDevice(_THIS) { + WASAPI_UnrefDevice(this); +} + +void +WASAPI_RefDevice(_THIS) +{ + SDL_AtomicIncRef(&this->hidden->refcount); +} + +void +WASAPI_UnrefDevice(_THIS) +{ + if (!SDL_AtomicDecRef(&this->hidden->refcount)) { + return; + } + + /* actual closing happens here. */ + /* don't touch this->hidden->task in here; it has to be reverted from - our callback thread. We do that in WASAPI_ThreadDeinit(). - (likewise for this->hidden->coinitialized). */ + our callback thread. We do that in WASAPI_ThreadDeinit(). + (likewise for this->hidden->coinitialized). */ ReleaseWasapiDevice(this); + SDL_free(this->hidden->devid); SDL_free(this->hidden); } - -static int -PrepWasapiDevice(_THIS, const int iscapture, IMMDevice *device) +/* This is called once a device is activated, possibly asynchronously. */ +int +WASAPI_PrepDevice(_THIS, const SDL_bool updatestream) { /* !!! FIXME: we could request an exclusive mode stream, which is lower latency; !!! it will write into the kernel's audio buffer directly instead of @@ -682,10 +502,11 @@ PrepWasapiDevice(_THIS, const int iscapture, IMMDevice *device) !!! wins actually look like. Maybe add a hint to force exclusive mode at !!! some point. To be sure, defaulting to shared mode is the right thing to !!! do in any case. */ + const SDL_AudioSpec oldspec = this->spec; const AUDCLNT_SHAREMODE sharemode = AUDCLNT_SHAREMODE_SHARED; UINT32 bufsize = 0; /* this is in sample frames, not samples, not bytes. */ REFERENCE_TIME duration = 0; - IAudioClient *client = NULL; + IAudioClient *client = this->hidden->client; IAudioRenderClient *render = NULL; IAudioCaptureClient *capture = NULL; WAVEFORMATEX *waveformat = NULL; @@ -694,15 +515,7 @@ PrepWasapiDevice(_THIS, const int iscapture, IMMDevice *device) SDL_bool valid_format = SDL_FALSE; HRESULT ret = S_OK; - this->hidden->device = device; - - ret = IMMDevice_Activate(device, &SDL_IID_IAudioClient, CLSCTX_ALL, NULL, (void **) &client); - if (FAILED(ret)) { - return WIN_SetErrorFromHRESULT("WASAPI can't activate audio endpoint", ret); - } - SDL_assert(client != NULL); - this->hidden->client = client; ret = IAudioClient_GetMixFormat(client, &waveformat); if (FAILED(ret)) { @@ -763,7 +576,7 @@ PrepWasapiDevice(_THIS, const int iscapture, IMMDevice *device) } this->spec.samples = (Uint16) bufsize; - if (!iscapture) { + if (!this->iscapture) { this->spec.samples /= 2; /* fill half of the DMA buffer on each run. */ } @@ -772,7 +585,7 @@ PrepWasapiDevice(_THIS, const int iscapture, IMMDevice *device) this->hidden->framesize = (SDL_AUDIO_BITSIZE(this->spec.format) / 8) * this->spec.channels; - if (iscapture) { + if (this->iscapture) { this->hidden->capturestream = SDL_NewAudioStream(this->spec.format, this->spec.channels, this->spec.freq, this->spec.format, this->spec.channels, this->spec.freq); if (!this->hidden->capturestream) { return -1; /* already set SDL_Error */ @@ -805,15 +618,20 @@ PrepWasapiDevice(_THIS, const int iscapture, IMMDevice *device) } } + if (updatestream) { + if (UpdateAudioStream(this, &oldspec) == -1) { + return -1; + } + } + return 0; /* good to go. */ } + static int WASAPI_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) { - const SDL_bool is_default_device = (handle == NULL); - IMMDevice *device = NULL; - HRESULT ret = S_OK; + LPCWSTR devid = (LPCWSTR) handle; /* Initialize all variables that we clean on shutdown */ this->hidden = (struct SDL_PrivateAudioData *) @@ -823,49 +641,42 @@ WASAPI_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) } SDL_zerop(this->hidden); - if (is_default_device) { - const EDataFlow dataflow = iscapture ? eCapture : eRender; - this->hidden->default_device_generation = SDL_AtomicGet(iscapture ? &default_capture_generation : &default_playback_generation); - ret = IMMDeviceEnumerator_GetDefaultAudioEndpoint(enumerator, dataflow, SDL_WASAPI_role, &device); + WASAPI_RefDevice(this); /* so CloseDevice() will unref to zero. */ + + if (!devid) { /* is default device? */ + this->hidden->default_device_generation = SDL_AtomicGet(iscapture ? &WASAPI_DefaultCaptureGeneration : &WASAPI_DefaultPlaybackGeneration); } else { - ret = IMMDeviceEnumerator_GetDevice(enumerator, (LPCWSTR) handle, &device); + this->hidden->devid = WStrDupe(devid); + if (!this->hidden->devid) { + return SDL_OutOfMemory(); + } } - if (FAILED(ret)) { - return WIN_SetErrorFromHRESULT("WASAPI can't find requested audio endpoint", ret); + if (WASAPI_ActivateDevice(this, SDL_FALSE) == -1) { + return -1; /* already set error. */ } - SDL_assert(device != NULL); - return PrepWasapiDevice(this, iscapture, device); + /* Ready, but waiting for async device activation. + Until activation is successful, we will report silence from capture + devices and ignore data on playback devices. + Also, since we don't know the _actual_ device format until after + activation, we let the app have whatever it asks for. We set up + an SDL_AudioStream to convert, if necessary, once the activation + completes. */ + + return 0; } static void WASAPI_ThreadInit(_THIS) { - /* this thread uses COM. */ - if (SUCCEEDED(WIN_CoInitialize())) { /* can't report errors, hope it worked! */ - this->hidden->coinitialized = SDL_TRUE; - } - - /* Set this thread to very high "Pro Audio" priority. */ - if (pAvSetMmThreadCharacteristicsW) { - DWORD idx = 0; - this->hidden->task = pAvSetMmThreadCharacteristicsW(TEXT("Pro Audio"), &idx); - } + WASAPI_PlatformThreadInit(this); } static void WASAPI_ThreadDeinit(_THIS) { - /* Set this thread to very high "Pro Audio" priority. */ - if (this->hidden->task && pAvRevertMmThreadCharacteristics) { - pAvRevertMmThreadCharacteristics(this->hidden->task); - this->hidden->task = NULL; - } - - if (this->hidden->coinitialized) { - WIN_CoUninitialize(); - } + WASAPI_PlatformThreadDeinit(this); } static void @@ -874,21 +685,7 @@ WASAPI_Deinitialize(void) DevIdList *devidlist; DevIdList *next; - if (enumerator) { - IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *) ¬ification_client); - IMMDeviceEnumerator_Release(enumerator); - enumerator = NULL; - } - - #ifndef __WINRT__ - if (libavrt) { - FreeLibrary(libavrt); - libavrt = NULL; - } - #endif - - pAvSetMmThreadCharacteristicsW = NULL; - pAvRevertMmThreadCharacteristics = NULL; + WASAPI_PlatformDeinit(); for (devidlist = deviceid_list; devidlist; devidlist = next) { next = devidlist->next; @@ -896,51 +693,23 @@ WASAPI_Deinitialize(void) SDL_free(devidlist); } deviceid_list = NULL; - - WIN_CoUninitialize(); } static int WASAPI_Init(SDL_AudioDriverImpl * impl) { - HRESULT ret; + SDL_AtomicSet(&WASAPI_DefaultPlaybackGeneration, 1); + SDL_AtomicSet(&WASAPI_DefaultCaptureGeneration, 1); - /* just skip the discussion with COM here. */ - if (!WIN_IsWindowsVistaOrGreater()) { - SDL_SetError("WASAPI support requires Windows Vista or later"); + if (WASAPI_PlatformInit() == -1) { return 0; } - SDL_AtomicSet(&default_playback_generation, 1); - SDL_AtomicSet(&default_capture_generation, 1); - - if (FAILED(WIN_CoInitialize())) { - SDL_SetError("WASAPI: CoInitialize() failed"); - return 0; - } - - ret = CoCreateInstance(&SDL_CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &SDL_IID_IMMDeviceEnumerator, (LPVOID) &enumerator); - if (FAILED(ret)) { - WIN_CoUninitialize(); - WIN_SetErrorFromHRESULT("WASAPI CoCreateInstance(MMDeviceEnumerator)", ret); - return 0; /* oh well. */ - } - - #ifdef __WINRT__ - pAvSetMmThreadCharacteristicsW = AvSetMmThreadCharacteristicsW; - pAvRevertMmThreadCharacteristics = AvRevertMmThreadCharacteristics; - #else - libavrt = LoadLibraryW(L"avrt.dll"); /* this library is available in Vista and later. No WinXP, so have to LoadLibrary to use it for now! */ - if (libavrt) { - pAvSetMmThreadCharacteristicsW = (pfnAvSetMmThreadCharacteristicsW) GetProcAddress(libavrt, "AvSetMmThreadCharacteristicsW"); - pAvRevertMmThreadCharacteristics = (pfnAvRevertMmThreadCharacteristics) GetProcAddress(libavrt, "AvRevertMmThreadCharacteristics"); - } - #endif - /* Set the function pointers */ impl->DetectDevices = WASAPI_DetectDevices; impl->ThreadInit = WASAPI_ThreadInit; impl->ThreadDeinit = WASAPI_ThreadDeinit; + impl->BeginLoopIteration = WASAPI_BeginLoopIteration; impl->OpenDevice = WASAPI_OpenDevice; impl->PlayDevice = WASAPI_PlayDevice; impl->WaitDevice = WASAPI_WaitDevice; diff --git a/src/audio/wasapi/SDL_wasapi.h b/src/audio/wasapi/SDL_wasapi.h index 530c73d43..df3546e5b 100644 --- a/src/audio/wasapi/SDL_wasapi.h +++ b/src/audio/wasapi/SDL_wasapi.h @@ -23,14 +23,23 @@ #ifndef SDL_wasapi_h_ #define SDL_wasapi_h_ +#ifdef __cplusplus +extern "C" { +#endif + #include "../SDL_sysaudio.h" /* Hidden "this" pointer for the audio functions */ +#ifdef __cplusplus +#define _THIS SDL_AudioDevice *_this +#else #define _THIS SDL_AudioDevice *this +#endif struct SDL_PrivateAudioData { - IMMDevice *device; + SDL_atomic_t refcount; + WCHAR *devid; WAVEFORMATEX *waveformat; IAudioClient *client; IAudioRenderClient *render; @@ -41,8 +50,35 @@ struct SDL_PrivateAudioData int framesize; int default_device_generation; SDL_bool device_lost; + void *activation_handler; + SDL_atomic_t just_activated; }; +/* these increment as default devices change. Opened default devices pick up changes in their threads. */ +extern SDL_atomic_t WASAPI_DefaultPlaybackGeneration; +extern SDL_atomic_t WASAPI_DefaultCaptureGeneration; + +/* win32 and winrt implementations call into these. */ +int WASAPI_PrepDevice(_THIS, const SDL_bool updatestream); +void WASAPI_RefDevice(_THIS); +void WASAPI_UnrefDevice(_THIS); +void WASAPI_AddDevice(const SDL_bool iscapture, const char *devname, LPCWSTR devid); +void WASAPI_RemoveDevice(const SDL_bool iscapture, LPCWSTR devid); + +/* These are functions that are implemented differently for Windows vs WinRT. */ +int WASAPI_PlatformInit(void); +void WASAPI_PlatformDeinit(void); +void WASAPI_EnumerateEndpoints(void); +int WASAPI_ActivateDevice(_THIS, const SDL_bool isrecovery); +void WASAPI_PlatformThreadInit(_THIS); +void WASAPI_PlatformThreadDeinit(_THIS); +void WASAPI_PlatformDeleteActivationHandler(void *handler); +void WASAPI_BeginLoopIteration(_THIS); + +#ifdef __cplusplus +} +#endif + #endif /* SDL_wasapi_h_ */ /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/audio/wasapi/SDL_wasapi_win32.c b/src/audio/wasapi/SDL_wasapi_win32.c new file mode 100644 index 000000000..bb6f97c93 --- /dev/null +++ b/src/audio/wasapi/SDL_wasapi_win32.c @@ -0,0 +1,418 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2017 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +/* This is code that Windows uses to talk to WASAPI-related system APIs. + This is for non-WinRT desktop apps. The C++/CX implementation of these + functions, exclusive to WinRT, are in SDL_wasapi_winrt.cpp. + The code in SDL_wasapi.c is used by both standard Windows and WinRT builds + to deal with audio and calls into these functions. */ + +#if SDL_AUDIO_DRIVER_WASAPI && !defined(__WINRT__) + +#include "../../core/windows/SDL_windows.h" +#include "SDL_audio.h" +#include "SDL_timer.h" +#include "../SDL_audio_c.h" +#include "../SDL_sysaudio.h" +#include "SDL_assert.h" +#include "SDL_log.h" + +#define COBJMACROS +#include +#include + +#include "SDL_wasapi.h" + +static const ERole SDL_WASAPI_role = eConsole; /* !!! FIXME: should this be eMultimedia? Should be a hint? */ + +/* This is global to the WASAPI target, to handle hotplug and default device lookup. */ +static IMMDeviceEnumerator *enumerator = NULL; + +/* PropVariantInit() is an inline function/macro in PropIdl.h that calls the C runtime's memset() directly. Use ours instead, to avoid dependency. */ +#ifdef PropVariantInit +#undef PropVariantInit +#endif +#define PropVariantInit(p) SDL_zerop(p) + +/* handle to Avrt.dll--Vista and later!--for flagging the callback thread as "Pro Audio" (low latency). */ +static HMODULE libavrt = NULL; +typedef HANDLE(WINAPI *pfnAvSetMmThreadCharacteristicsW)(LPWSTR, LPDWORD); +typedef BOOL(WINAPI *pfnAvRevertMmThreadCharacteristics)(HANDLE); +static pfnAvSetMmThreadCharacteristicsW pAvSetMmThreadCharacteristicsW = NULL; +static pfnAvRevertMmThreadCharacteristics pAvRevertMmThreadCharacteristics = NULL; + +/* Some GUIDs we need to know without linking to libraries that aren't available before Vista. */ +static const CLSID SDL_CLSID_MMDeviceEnumerator = { 0xbcde0395, 0xe52f, 0x467c,{ 0x8e, 0x3d, 0xc4, 0x57, 0x92, 0x91, 0x69, 0x2e } }; +static const IID SDL_IID_IMMDeviceEnumerator = { 0xa95664d2, 0x9614, 0x4f35,{ 0xa7, 0x46, 0xde, 0x8d, 0xb6, 0x36, 0x17, 0xe6 } }; +static const IID SDL_IID_IMMNotificationClient = { 0x7991eec9, 0x7e89, 0x4d85,{ 0x83, 0x90, 0x6c, 0x70, 0x3c, 0xec, 0x60, 0xc0 } }; +static const IID SDL_IID_IMMEndpoint = { 0x1be09788, 0x6894, 0x4089,{ 0x85, 0x86, 0x9a, 0x2a, 0x6c, 0x26, 0x5a, 0xc5 } }; +static const IID SDL_IID_IAudioClient = { 0x1cb9ad4c, 0xdbfa, 0x4c32,{ 0xb1, 0x78, 0xc2, 0xf5, 0x68, 0xa7, 0x03, 0xb2 } }; +static const PROPERTYKEY SDL_PKEY_Device_FriendlyName = { { 0xa45c254e, 0xdf1c, 0x4efd,{ 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, } }, 14 }; + + +static char * +GetWasapiDeviceName(IMMDevice *device) +{ + /* PKEY_Device_FriendlyName gives you "Speakers (SoundBlaster Pro)" which drives me nuts. I'd rather it be + "SoundBlaster Pro (Speakers)" but I guess that's developers vs users. Windows uses the FriendlyName in + its own UIs, like Volume Control, etc. */ + char *utf8dev = NULL; + IPropertyStore *props = NULL; + if (SUCCEEDED(IMMDevice_OpenPropertyStore(device, STGM_READ, &props))) { + PROPVARIANT var; + PropVariantInit(&var); + if (SUCCEEDED(IPropertyStore_GetValue(props, &SDL_PKEY_Device_FriendlyName, &var))) { + utf8dev = WIN_StringToUTF8(var.pwszVal); + } + PropVariantClear(&var); + IPropertyStore_Release(props); + } + return utf8dev; +} + + +/* We need a COM subclass of IMMNotificationClient for hotplug support, which is + easy in C++, but we have to tapdance more to make work in C. + Thanks to this page for coaching on how to make this work: + https://www.codeproject.com/Articles/13601/COM-in-plain-C */ + +typedef struct SDLMMNotificationClient +{ + const IMMNotificationClientVtbl *lpVtbl; + SDL_atomic_t refcount; +} SDLMMNotificationClient; + +static HRESULT STDMETHODCALLTYPE +SDLMMNotificationClient_QueryInterface(IMMNotificationClient *this, REFIID iid, void **ppv) +{ + if ((WIN_IsEqualIID(iid, &IID_IUnknown)) || (WIN_IsEqualIID(iid, &SDL_IID_IMMNotificationClient))) + { + *ppv = this; + this->lpVtbl->AddRef(this); + return S_OK; + } + + *ppv = NULL; + return E_NOINTERFACE; +} + +static ULONG STDMETHODCALLTYPE +SDLMMNotificationClient_AddRef(IMMNotificationClient *ithis) +{ + SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis; + return (ULONG) (SDL_AtomicIncRef(&this->refcount) + 1); +} + +static ULONG STDMETHODCALLTYPE +SDLMMNotificationClient_Release(IMMNotificationClient *ithis) +{ + /* this is a static object; we don't ever free it. */ + SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis; + const ULONG retval = SDL_AtomicDecRef(&this->refcount); + if (retval == 0) { + SDL_AtomicSet(&this->refcount, 0); /* uhh... */ + return 0; + } + return retval - 1; +} + +/* These are the entry points called when WASAPI device endpoints change. */ +static HRESULT STDMETHODCALLTYPE +SDLMMNotificationClient_OnDefaultDeviceChanged(IMMNotificationClient *ithis, EDataFlow flow, ERole role, LPCWSTR pwstrDeviceId) +{ + if (role != SDL_WASAPI_role) { + return S_OK; /* ignore it. */ + } + + /* Increment the "generation," so opened devices will pick this up in their threads. */ + switch (flow) { + case eRender: + SDL_AtomicAdd(&WASAPI_DefaultPlaybackGeneration, 1); + break; + + case eCapture: + SDL_AtomicAdd(&WASAPI_DefaultCaptureGeneration, 1); + break; + + case eAll: + SDL_AtomicAdd(&WASAPI_DefaultPlaybackGeneration, 1); + SDL_AtomicAdd(&WASAPI_DefaultCaptureGeneration, 1); + break; + + default: + SDL_assert(!"uhoh, unexpected OnDefaultDeviceChange flow!"); + break; + } + + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE +SDLMMNotificationClient_OnDeviceAdded(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId) +{ + /* we ignore this; devices added here then progress to ACTIVE, if appropriate, in + OnDeviceStateChange, making that a better place to deal with device adds. More + importantly: the first time you plug in a USB audio device, this callback will + fire, but when you unplug it, it isn't removed (it's state changes to NOTPRESENT). + Plugging it back in won't fire this callback again. */ + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE +SDLMMNotificationClient_OnDeviceRemoved(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId) +{ + /* See notes in OnDeviceAdded handler about why we ignore this. */ + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE +SDLMMNotificationClient_OnDeviceStateChanged(IMMNotificationClient *ithis, LPCWSTR pwstrDeviceId, DWORD dwNewState) +{ + SDLMMNotificationClient *this = (SDLMMNotificationClient *) ithis; + IMMDevice *device = NULL; + + if (SUCCEEDED(IMMDeviceEnumerator_GetDevice(enumerator, pwstrDeviceId, &device))) { + IMMEndpoint *endpoint = NULL; + if (SUCCEEDED(IMMDevice_QueryInterface(device, &SDL_IID_IMMEndpoint, (void **) &endpoint))) { + EDataFlow flow; + if (SUCCEEDED(IMMEndpoint_GetDataFlow(endpoint, &flow))) { + const SDL_bool iscapture = (flow == eCapture); + if (dwNewState == DEVICE_STATE_ACTIVE) { + char *utf8dev = GetWasapiDeviceName(device); + if (utf8dev) { + WASAPI_AddDevice(iscapture, utf8dev, pwstrDeviceId); + SDL_free(utf8dev); + } + } else { + WASAPI_RemoveDevice(iscapture, pwstrDeviceId); + } + } + IMMEndpoint_Release(endpoint); + } + IMMDevice_Release(device); + } + + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE +SDLMMNotificationClient_OnPropertyValueChanged(IMMNotificationClient *this, LPCWSTR pwstrDeviceId, const PROPERTYKEY key) +{ + return S_OK; /* we don't care about these. */ +} + +static const IMMNotificationClientVtbl notification_client_vtbl = { + SDLMMNotificationClient_QueryInterface, + SDLMMNotificationClient_AddRef, + SDLMMNotificationClient_Release, + SDLMMNotificationClient_OnDeviceStateChanged, + SDLMMNotificationClient_OnDeviceAdded, + SDLMMNotificationClient_OnDeviceRemoved, + SDLMMNotificationClient_OnDefaultDeviceChanged, + SDLMMNotificationClient_OnPropertyValueChanged +}; + +static SDLMMNotificationClient notification_client = { ¬ification_client_vtbl, 1 }; + + +int +WASAPI_PlatformInit(void) +{ + HRESULT ret; + + /* just skip the discussion with COM here. */ + if (!WIN_IsWindowsVistaOrGreater()) { + return SDL_SetError("WASAPI support requires Windows Vista or later"); + } + + if (FAILED(WIN_CoInitialize())) { + return SDL_SetError("WASAPI: CoInitialize() failed"); + } + + ret = CoCreateInstance(&SDL_CLSID_MMDeviceEnumerator, NULL, CLSCTX_INPROC_SERVER, &SDL_IID_IMMDeviceEnumerator, (LPVOID) &enumerator); + if (FAILED(ret)) { + WIN_CoUninitialize(); + return WIN_SetErrorFromHRESULT("WASAPI CoCreateInstance(MMDeviceEnumerator)", ret); + } + + libavrt = LoadLibraryW(L"avrt.dll"); /* this library is available in Vista and later. No WinXP, so have to LoadLibrary to use it for now! */ + if (libavrt) { + pAvSetMmThreadCharacteristicsW = (pfnAvSetMmThreadCharacteristicsW) GetProcAddress(libavrt, "AvSetMmThreadCharacteristicsW"); + pAvRevertMmThreadCharacteristics = (pfnAvRevertMmThreadCharacteristics) GetProcAddress(libavrt, "AvRevertMmThreadCharacteristics"); + } + + return 0; +} + +void +WASAPI_PlatformDeinit(void) +{ + if (enumerator) { + IMMDeviceEnumerator_UnregisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *) ¬ification_client); + IMMDeviceEnumerator_Release(enumerator); + enumerator = NULL; + } + + if (libavrt) { + FreeLibrary(libavrt); + libavrt = NULL; + } + + pAvSetMmThreadCharacteristicsW = NULL; + pAvRevertMmThreadCharacteristics = NULL; + + WIN_CoUninitialize(); +} + +void +WASAPI_PlatformThreadInit(_THIS) +{ + /* this thread uses COM. */ + if (SUCCEEDED(WIN_CoInitialize())) { /* can't report errors, hope it worked! */ + this->hidden->coinitialized = SDL_TRUE; + } + + /* Set this thread to very high "Pro Audio" priority. */ + if (pAvSetMmThreadCharacteristicsW) { + DWORD idx = 0; + this->hidden->task = pAvSetMmThreadCharacteristicsW(TEXT("Pro Audio"), &idx); + } +} + +void +WASAPI_PlatformThreadDeinit(_THIS) +{ + /* Set this thread back to normal priority. */ + if (this->hidden->task && pAvRevertMmThreadCharacteristics) { + pAvRevertMmThreadCharacteristics(this->hidden->task); + this->hidden->task = NULL; + } + + if (this->hidden->coinitialized) { + WIN_CoUninitialize(); + this->hidden->coinitialized = SDL_FALSE; + } +} + +int +WASAPI_ActivateDevice(_THIS, const SDL_bool isrecovery) +{ + LPCWSTR devid = this->hidden->devid; + IMMDevice *device = NULL; + HRESULT ret; + + if (devid == NULL) { + const EDataFlow dataflow = this->iscapture ? eCapture : eRender; + ret = IMMDeviceEnumerator_GetDefaultAudioEndpoint(enumerator, dataflow, SDL_WASAPI_role, &device); + } else { + ret = IMMDeviceEnumerator_GetDevice(enumerator, devid, &device); + } + + if (FAILED(ret)) { + SDL_assert(device == NULL); + this->hidden->client = NULL; + return WIN_SetErrorFromHRESULT("WASAPI can't find requested audio endpoint", ret); + } + + /* this is not async in standard win32, yay! */ + ret = IMMDevice_Activate(device, &SDL_IID_IAudioClient, CLSCTX_ALL, NULL, (void **) &this->hidden->client); + IMMDevice_Release(device); + + if (FAILED(ret)) { + SDL_assert(this->hidden->client == NULL); + return WIN_SetErrorFromHRESULT("WASAPI can't activate audio endpoint", ret); + } + + SDL_assert(this->hidden->client != NULL); + if (WASAPI_PrepDevice(this, isrecovery) == -1) { /* not async, fire it right away. */ + return -1; + } + + return 0; /* good to go. */ +} + + +static void +WASAPI_EnumerateEndpointsForFlow(const SDL_bool iscapture) +{ + IMMDeviceCollection *collection = NULL; + UINT i, total; + + /* Note that WASAPI separates "adapter devices" from "audio endpoint devices" + ...one adapter device ("SoundBlaster Pro") might have multiple endpoint devices ("Speakers", "Line-Out"). */ + + if (FAILED(IMMDeviceEnumerator_EnumAudioEndpoints(enumerator, iscapture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &collection))) { + return; + } + + if (FAILED(IMMDeviceCollection_GetCount(collection, &total))) { + IMMDeviceCollection_Release(collection); + return; + } + + for (i = 0; i < total; i++) { + IMMDevice *device = NULL; + if (SUCCEEDED(IMMDeviceCollection_Item(collection, i, &device))) { + LPWSTR devid = NULL; + if (SUCCEEDED(IMMDevice_GetId(device, &devid))) { + char *devname = GetWasapiDeviceName(device); + if (devname) { + WASAPI_AddDevice(iscapture, devname, devid); + SDL_free(devname); + } + CoTaskMemFree(devid); + } + IMMDevice_Release(device); + } + } + + IMMDeviceCollection_Release(collection); +} + +void +WASAPI_EnumerateEndpoints(void) +{ + WASAPI_EnumerateEndpointsForFlow(SDL_FALSE); /* playback */ + WASAPI_EnumerateEndpointsForFlow(SDL_TRUE); /* capture */ + + /* if this fails, we just won't get hotplug events. Carry on anyhow. */ + IMMDeviceEnumerator_RegisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *) ¬ification_client); +} + +void +WASAPI_PlatformDeleteActivationHandler(void *handler) +{ + /* not asynchronous. */ + SDL_assert(!"This function should have only be called on WinRT."); +} + +void +WASAPI_BeginLoopIteration(_THIS) +{ + /* no-op. */ +} + +#endif /* SDL_AUDIO_DRIVER_WASAPI && !defined(__WINRT__) */ + +/* vi: set ts=4 sw=4 expandtab: */ + diff --git a/src/audio/wasapi/SDL_wasapi_winrt.cpp b/src/audio/wasapi/SDL_wasapi_winrt.cpp new file mode 100644 index 000000000..481a58bd4 --- /dev/null +++ b/src/audio/wasapi/SDL_wasapi_winrt.cpp @@ -0,0 +1,276 @@ +/* + Simple DirectMedia Layer + Copyright (C) 1997-2017 Sam Lantinga + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. +*/ + +#include "../../SDL_internal.h" + +// This is C++/CX code that the WinRT port uses to talk to WASAPI-related +// system APIs. The C implementation of these functions, for non-WinRT apps, +// is in SDL_wasapi_win32.c. The code in SDL_wasapi.c is used by both standard +// Windows and WinRT builds to deal with audio and calls into these functions. + +#if SDL_AUDIO_DRIVER_WASAPI && defined(__WINRT__) + +#include +#include +#include +#include +#include + +extern "C" { +#include "../../core/windows/SDL_windows.h" +#include "SDL_audio.h" +#include "SDL_timer.h" +#include "../SDL_audio_c.h" +#include "../SDL_sysaudio.h" +#include "SDL_assert.h" +#include "SDL_log.h" +} + +#define COBJMACROS +#include +#include + +#include "SDL_wasapi.h" + +using namespace Windows::Devices::Enumeration; +using namespace Windows::Media::Devices; +using namespace Windows::Foundation; +using namespace Microsoft::WRL; + +class SDL_WasapiDeviceEventHandler +{ +public: + SDL_WasapiDeviceEventHandler(const SDL_bool _iscapture); + ~SDL_WasapiDeviceEventHandler(); + void OnDeviceAdded(DeviceWatcher^ sender, DeviceInformation^ args); + void OnDeviceRemoved(DeviceWatcher^ sender, DeviceInformationUpdate^ args); + void OnDeviceUpdated(DeviceWatcher^ sender, DeviceInformationUpdate^ args); + void OnDefaultRenderDeviceChanged(Platform::Object^ sender, DefaultAudioRenderDeviceChangedEventArgs^ args); + void OnDefaultCaptureDeviceChanged(Platform::Object^ sender, DefaultAudioCaptureDeviceChangedEventArgs^ args); + +private: + const SDL_bool iscapture; + DeviceWatcher^ watcher; + Windows::Foundation::EventRegistrationToken added_handler; + Windows::Foundation::EventRegistrationToken removed_handler; + Windows::Foundation::EventRegistrationToken updated_handler; + Windows::Foundation::EventRegistrationToken default_changed_handler; +}; + +SDL_WasapiDeviceEventHandler::SDL_WasapiDeviceEventHandler(const SDL_bool _iscapture) + : iscapture(_iscapture) + , watcher(DeviceInformation::CreateWatcher(_iscapture ? DeviceClass::AudioCapture : DeviceClass::AudioRender)) +{ + if (!watcher) + return; // uhoh. + + // !!! FIXME: this doesn't need a lambda here, I think, if I make SDL_WasapiDeviceEventHandler a proper C++/CX class. --ryan. + added_handler = watcher->Added += ref new TypedEventHandler([this](DeviceWatcher^ sender, DeviceInformation^ args) { OnDeviceAdded(sender, args); } ); + removed_handler = watcher->Removed += ref new TypedEventHandler([this](DeviceWatcher^ sender, DeviceInformationUpdate^ args) { OnDeviceRemoved(sender, args); } ); + updated_handler = watcher->Updated += ref new TypedEventHandler([this](DeviceWatcher^ sender, DeviceInformationUpdate^ args) { OnDeviceUpdated(sender, args); } ); + if (iscapture) { + default_changed_handler = MediaDevice::DefaultAudioCaptureDeviceChanged += ref new TypedEventHandler([this](Platform::Object^ sender, DefaultAudioCaptureDeviceChangedEventArgs^ args) { OnDefaultCaptureDeviceChanged(sender, args); } ); + } else { + default_changed_handler = MediaDevice::DefaultAudioRenderDeviceChanged += ref new TypedEventHandler([this](Platform::Object^ sender, DefaultAudioRenderDeviceChangedEventArgs^ args) { OnDefaultRenderDeviceChanged(sender, args); } ); + } + watcher->Start(); +} + +SDL_WasapiDeviceEventHandler::~SDL_WasapiDeviceEventHandler() +{ + if (watcher) { + watcher->Added -= added_handler; + watcher->Removed -= removed_handler; + watcher->Updated -= updated_handler; + watcher->Stop(); + watcher = nullptr; + } + + if (iscapture) { + MediaDevice::DefaultAudioCaptureDeviceChanged -= default_changed_handler; + } else { + MediaDevice::DefaultAudioRenderDeviceChanged -= default_changed_handler; + } +} + +void +SDL_WasapiDeviceEventHandler::OnDeviceAdded(DeviceWatcher^ sender, DeviceInformation^ info) +{ + SDL_assert(sender == this->watcher); + char *utf8dev = WIN_StringToUTF8(info->Name->Data()); + if (utf8dev) { + WASAPI_AddDevice(this->iscapture, utf8dev, info->Id->Data()); + SDL_free(utf8dev); + } +} + +void +SDL_WasapiDeviceEventHandler::OnDeviceRemoved(DeviceWatcher^ sender, DeviceInformationUpdate^ info) +{ + SDL_assert(sender == this->watcher); + WASAPI_RemoveDevice(this->iscapture, info->Id->Data()); +} + +void +SDL_WasapiDeviceEventHandler::OnDeviceUpdated(DeviceWatcher^ sender, DeviceInformationUpdate^ args) +{ + SDL_assert(sender == this->watcher); +} + +void +SDL_WasapiDeviceEventHandler::OnDefaultRenderDeviceChanged(Platform::Object^ sender, DefaultAudioRenderDeviceChangedEventArgs^ args) +{ + SDL_assert(this->iscapture); + SDL_AtomicAdd(&WASAPI_DefaultPlaybackGeneration, 1); +} + +void +SDL_WasapiDeviceEventHandler::OnDefaultCaptureDeviceChanged(Platform::Object^ sender, DefaultAudioCaptureDeviceChangedEventArgs^ args) +{ + SDL_assert(!this->iscapture); + SDL_AtomicAdd(&WASAPI_DefaultCaptureGeneration, 1); +} + + +static SDL_WasapiDeviceEventHandler *playback_device_event_handler; +static SDL_WasapiDeviceEventHandler *capture_device_event_handler; + +int WASAPI_PlatformInit(void) +{ + return 0; +} + +void WASAPI_PlatformDeinit(void) +{ + delete playback_device_event_handler; + playback_device_event_handler = nullptr; + delete capture_device_event_handler; + capture_device_event_handler = nullptr; +} + +void WASAPI_EnumerateEndpoints(void) +{ + // DeviceWatchers will fire an Added event for each existing device at + // startup, so we don't need to enumerate them separately before + // listening for updates. + playback_device_event_handler = new SDL_WasapiDeviceEventHandler(SDL_FALSE); + capture_device_event_handler = new SDL_WasapiDeviceEventHandler(SDL_TRUE); +} + +struct SDL_WasapiActivationHandler : public RuntimeClass< RuntimeClassFlags< ClassicCom >, FtmBase, IActivateAudioInterfaceCompletionHandler > +{ + SDL_WasapiActivationHandler() : device(nullptr) {} + STDMETHOD(ActivateCompleted)(IActivateAudioInterfaceAsyncOperation *operation); + SDL_AudioDevice *device; +}; + +HRESULT +SDL_WasapiActivationHandler::ActivateCompleted(IActivateAudioInterfaceAsyncOperation *async) +{ + HRESULT result = S_OK; + IUnknown *iunknown = nullptr; + const HRESULT ret = async->GetActivateResult(&result, &iunknown); + + if (SUCCEEDED(ret) && SUCCEEDED(result)) { + iunknown->QueryInterface(IID_PPV_ARGS(&device->hidden->client)); + if (device->hidden->client) { + // Just set a flag, since we're probably in a different thread. We'll pick it up and init everything on our own thread to prevent races. + SDL_AtomicSet(&device->hidden->just_activated, 1); + } + } + + WASAPI_UnrefDevice(device); + + return S_OK; +} + +void +WASAPI_PlatformDeleteActivationHandler(void *handler) +{ + ((SDL_WasapiActivationHandler *) handler)->Release(); +} + +int +WASAPI_ActivateDevice(_THIS, const SDL_bool isrecovery) +{ + LPCWSTR devid = _this->hidden->devid; + Platform::String^ defdevid; + + if (devid == nullptr) { + defdevid = _this->iscapture ? MediaDevice::GetDefaultAudioCaptureId(AudioDeviceRole::Default) : MediaDevice::GetDefaultAudioRenderId(AudioDeviceRole::Default); + if (defdevid) { + devid = defdevid->Data(); + } + } + + SDL_AtomicSet(&_this->hidden->just_activated, 0); + + ComPtr handler = Make(); + if (handler == nullptr) { + return SDL_SetError("Failed to allocate WASAPI activation handler"); + } + + handler.Get()->AddRef(); // we hold a reference after ComPtr destructs on return, causing a Release, and Release ourselves in WASAPI_PlatformDeleteActivationHandler(), etc. + handler.Get()->device = _this; + _this->hidden->activation_handler = handler.Get(); + + WASAPI_RefDevice(_this); /* completion handler will unref it. */ + IActivateAudioInterfaceAsyncOperation *async = nullptr; + const HRESULT ret = ActivateAudioInterfaceAsync(devid, __uuidof(IAudioClient), nullptr, handler.Get(), &async); + + if (async != nullptr) { + async->Release(); + } + + if (FAILED(ret)) { + handler.Get()->Release(); + WASAPI_UnrefDevice(_this); + return WIN_SetErrorFromHRESULT("WASAPI can't activate requested audio endpoint", ret); + } + + return 0; +} + +void +WASAPI_BeginLoopIteration(_THIS) +{ + if (SDL_AtomicCAS(&_this->hidden->just_activated, 1, 0)) { + if (WASAPI_PrepDevice(_this, SDL_TRUE) == -1) { + SDL_OpenedAudioDeviceDisconnected(_this); + } + } +} + +void +WASAPI_PlatformThreadInit(_THIS) +{ + // !!! FIXME: set this thread to "Pro Audio" priority. +} + +void +WASAPI_PlatformThreadDeinit(_THIS) +{ + // !!! FIXME: set this thread to "Pro Audio" priority. +} + +#endif // SDL_AUDIO_DRIVER_WASAPI && defined(__WINRT__) + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/audio/xaudio2/SDL_xaudio2.c b/src/audio/xaudio2/SDL_xaudio2.c deleted file mode 100644 index 6faa17090..000000000 --- a/src/audio/xaudio2/SDL_xaudio2.c +++ /dev/null @@ -1,505 +0,0 @@ -/* - Simple DirectMedia Layer - Copyright (C) 1997-2017 Sam Lantinga - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. -*/ - -/* WinRT NOTICE: - - A few changes to SDL's XAudio2 backend were warranted by API - changes to Windows. Many, but not all of these are documented by Microsoft - at: - http://blogs.msdn.com/b/chuckw/archive/2012/04/02/xaudio2-and-windows-8-consumer-preview.aspx - - 1. Windows' thread synchronization function, CreateSemaphore, was removed - from WinRT. SDL's semaphore API was substituted instead. - 2. The method calls, IXAudio2::GetDeviceCount and IXAudio2::GetDeviceDetails - were removed from the XAudio2 API. Microsoft is telling developers to - use APIs in Windows::Foundation instead. - For SDL, the missing methods were reimplemented using the APIs Microsoft - said to use. - 3. CoInitialize and CoUninitialize are not available in WinRT. - These calls were removed, as COM will have been initialized earlier, - at least by the call to the WinRT app's main function - (aka 'int main(Platform::Array^)). (DLudwig: - This was my understanding of how WinRT: the 'main' function uses - a tag of [MTAThread], which should initialize COM. My understanding - of COM is somewhat limited, and I may be incorrect here.) - 4. IXAudio2::CreateMasteringVoice changed its integer-based 'DeviceIndex' - argument to a string-based one, 'szDeviceId'. In WinRT, the - string-based argument will be used. -*/ -#include "../../SDL_internal.h" - -#if SDL_AUDIO_DRIVER_XAUDIO2 - -#include "../../core/windows/SDL_windows.h" -#include "SDL_audio.h" -#include "../SDL_audio_c.h" -#include "../SDL_sysaudio.h" -#include "SDL_assert.h" - -#ifdef __GNUC__ -/* The configure script already did any necessary checking */ -# define SDL_XAUDIO2_HAS_SDK 1 -#elif defined(__WINRT__) -/* WinRT always has access to the XAudio 2 SDK (albeit with a header file - that doesn't compile as C code). -*/ -# define SDL_XAUDIO2_HAS_SDK -#include "SDL_xaudio2.h" /* ... compiles as C code, in contrast to XAudio2 headers - in the Windows SDK, v.10.0.10240.0 (Win 10's initial SDK) - */ -#else -/* XAudio2 exists in the last DirectX SDK as well as the latest Windows SDK. - To enable XAudio2 support, you will need to add the location of your DirectX SDK headers to - the SDL projects additional include directories and then set SDL_XAUDIO2_HAS_SDK=1 as a - preprocessor define - */ -#if 0 /* See comment above */ -#include -#if (!defined(_DXSDK_BUILD_MAJOR) || (_DXSDK_BUILD_MAJOR < 1284)) -# pragma message("Your DirectX SDK is too old. Disabling XAudio2 support.") -#else -# define SDL_XAUDIO2_HAS_SDK 1 -#endif -#endif /* 0 */ -#endif /* __GNUC__ */ - -#ifdef SDL_XAUDIO2_HAS_SDK - -/* Check to see if we're compiling for XAudio 2.8, or higher. */ -#ifdef WINVER -#if WINVER >= 0x0602 /* Windows 8 SDK or higher? */ -#define SDL_XAUDIO2_WIN8 1 -#endif -#endif - -#if !defined(SDL_XAUDIO2_H_) -#define INITGUID 1 -#include -#endif - -/* Hidden "this" pointer for the audio functions */ -#define _THIS SDL_AudioDevice *this - -#ifdef __WINRT__ -#include "SDL_xaudio2_winrthelpers.h" -#endif - -/* Fixes bug 1210 where some versions of gcc need named parameters */ -#ifdef __GNUC__ -#ifdef THIS -#undef THIS -#endif -#define THIS INTERFACE *p -#ifdef THIS_ -#undef THIS_ -#endif -#define THIS_ INTERFACE *p, -#endif - -struct SDL_PrivateAudioData -{ - IXAudio2 *ixa2; - IXAudio2SourceVoice *source; - IXAudio2MasteringVoice *mastering; - SDL_sem * semaphore; - Uint8 *mixbuf; - int mixlen; - Uint8 *nextbuf; -}; - - -static void -XAUDIO2_DetectDevices(void) -{ - IXAudio2 *ixa2 = NULL; - UINT32 devcount = 0; - UINT32 i = 0; - - if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) { - SDL_SetError("XAudio2: XAudio2Create() failed at detection."); - return; - } else if (IXAudio2_GetDeviceCount(ixa2, &devcount) != S_OK) { - SDL_SetError("XAudio2: IXAudio2::GetDeviceCount() failed."); - IXAudio2_Release(ixa2); - return; - } - - for (i = 0; i < devcount; i++) { - XAUDIO2_DEVICE_DETAILS details; - if (IXAudio2_GetDeviceDetails(ixa2, i, &details) == S_OK) { - char *str = WIN_StringToUTF8(details.DisplayName); - if (str != NULL) { - SDL_AddAudioDevice(SDL_FALSE, str, (void *) ((size_t) i+1)); - SDL_free(str); /* SDL_AddAudioDevice made a copy of the string. */ - } - } - } - - IXAudio2_Release(ixa2); -} - -static void STDMETHODCALLTYPE -VoiceCBOnBufferEnd(THIS_ void *data) -{ - /* Just signal the SDL audio thread and get out of XAudio2's way. */ - SDL_AudioDevice *this = (SDL_AudioDevice *) data; - SDL_SemPost(this->hidden->semaphore); -} - -static void STDMETHODCALLTYPE -VoiceCBOnVoiceError(THIS_ void *data, HRESULT Error) -{ - SDL_AudioDevice *this = (SDL_AudioDevice *) data; - SDL_OpenedAudioDeviceDisconnected(this); -} - -/* no-op callbacks... */ -static void STDMETHODCALLTYPE VoiceCBOnStreamEnd(THIS) {} -static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassStart(THIS_ UINT32 b) {} -static void STDMETHODCALLTYPE VoiceCBOnVoiceProcessPassEnd(THIS) {} -static void STDMETHODCALLTYPE VoiceCBOnBufferStart(THIS_ void *data) {} -static void STDMETHODCALLTYPE VoiceCBOnLoopEnd(THIS_ void *data) {} - - -static Uint8 * -XAUDIO2_GetDeviceBuf(_THIS) -{ - return this->hidden->nextbuf; -} - -static void -XAUDIO2_PlayDevice(_THIS) -{ - XAUDIO2_BUFFER buffer; - Uint8 *mixbuf = this->hidden->mixbuf; - Uint8 *nextbuf = this->hidden->nextbuf; - const int mixlen = this->hidden->mixlen; - IXAudio2SourceVoice *source = this->hidden->source; - HRESULT result = S_OK; - - if (!SDL_AtomicGet(&this->enabled)) { /* shutting down? */ - return; - } - - /* Submit the next filled buffer */ - SDL_zero(buffer); - buffer.AudioBytes = mixlen; - buffer.pAudioData = nextbuf; - buffer.pContext = this; - - if (nextbuf == mixbuf) { - nextbuf += mixlen; - } else { - nextbuf = mixbuf; - } - this->hidden->nextbuf = nextbuf; - - result = IXAudio2SourceVoice_SubmitSourceBuffer(source, &buffer, NULL); - if (result == XAUDIO2_E_DEVICE_INVALIDATED) { - /* !!! FIXME: possibly disconnected or temporary lost. Recover? */ - } - - if (result != S_OK) { /* uhoh, panic! */ - IXAudio2SourceVoice_FlushSourceBuffers(source); - SDL_OpenedAudioDeviceDisconnected(this); - } -} - -static void -XAUDIO2_WaitDevice(_THIS) -{ - if (SDL_AtomicGet(&this->enabled)) { - SDL_SemWait(this->hidden->semaphore); - } -} - -static void -XAUDIO2_PrepareToClose(_THIS) -{ - IXAudio2SourceVoice *source = this->hidden->source; - if (source) { - IXAudio2SourceVoice_Discontinuity(source); - } -} - -static void -XAUDIO2_CloseDevice(_THIS) -{ - IXAudio2 *ixa2 = this->hidden->ixa2; - IXAudio2SourceVoice *source = this->hidden->source; - IXAudio2MasteringVoice *mastering = this->hidden->mastering; - - if (source != NULL) { - IXAudio2SourceVoice_Stop(source, 0, XAUDIO2_COMMIT_NOW); - IXAudio2SourceVoice_FlushSourceBuffers(source); - IXAudio2SourceVoice_DestroyVoice(source); - } - if (ixa2 != NULL) { - IXAudio2_StopEngine(ixa2); - } - if (mastering != NULL) { - IXAudio2MasteringVoice_DestroyVoice(mastering); - } - if (ixa2 != NULL) { - IXAudio2_Release(ixa2); - } - if (this->hidden->semaphore != NULL) { - SDL_DestroySemaphore(this->hidden->semaphore); - } - - SDL_free(this->hidden->mixbuf); - SDL_free(this->hidden); -} - -static int -XAUDIO2_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) -{ - HRESULT result = S_OK; - WAVEFORMATEX waveformat; - int valid_format = 0; - SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format); - IXAudio2 *ixa2 = NULL; - IXAudio2SourceVoice *source = NULL; -#if defined(SDL_XAUDIO2_WIN8) - LPCWSTR devId = NULL; -#else - UINT32 devId = 0; /* 0 == system default device. */ -#endif - - static IXAudio2VoiceCallbackVtbl callbacks_vtable = { - VoiceCBOnVoiceProcessPassStart, - VoiceCBOnVoiceProcessPassEnd, - VoiceCBOnStreamEnd, - VoiceCBOnBufferStart, - VoiceCBOnBufferEnd, - VoiceCBOnLoopEnd, - VoiceCBOnVoiceError - }; - - static IXAudio2VoiceCallback callbacks = { &callbacks_vtable }; - -#if defined(SDL_XAUDIO2_WIN8) - /* !!! FIXME: hook up hotplugging. */ -#else - if (handle != NULL) { /* specific device requested? */ - /* -1 because we increment the original value to avoid NULL. */ - const size_t val = ((size_t) handle) - 1; - devId = (UINT32) val; - } -#endif - - if (XAudio2Create(&ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR) != S_OK) { - return SDL_SetError("XAudio2: XAudio2Create() failed at open."); - } - - /* - XAUDIO2_DEBUG_CONFIGURATION debugConfig; - debugConfig.TraceMask = XAUDIO2_LOG_ERRORS; //XAUDIO2_LOG_WARNINGS | XAUDIO2_LOG_DETAIL | XAUDIO2_LOG_FUNC_CALLS | XAUDIO2_LOG_TIMING | XAUDIO2_LOG_LOCKS | XAUDIO2_LOG_MEMORY | XAUDIO2_LOG_STREAMING; - debugConfig.BreakMask = XAUDIO2_LOG_ERRORS; //XAUDIO2_LOG_WARNINGS; - debugConfig.LogThreadID = TRUE; - debugConfig.LogFileline = TRUE; - debugConfig.LogFunctionName = TRUE; - debugConfig.LogTiming = TRUE; - ixa2->SetDebugConfiguration(&debugConfig); - */ - - /* Initialize all variables that we clean on shutdown */ - this->hidden = (struct SDL_PrivateAudioData *) - SDL_malloc((sizeof *this->hidden)); - if (this->hidden == NULL) { - IXAudio2_Release(ixa2); - return SDL_OutOfMemory(); - } - SDL_zerop(this->hidden); - - this->hidden->ixa2 = ixa2; - this->hidden->semaphore = SDL_CreateSemaphore(1); - if (this->hidden->semaphore == NULL) { - return SDL_SetError("XAudio2: CreateSemaphore() failed!"); - } - - while ((!valid_format) && (test_format)) { - switch (test_format) { - case AUDIO_U8: - case AUDIO_S16: - case AUDIO_S32: - case AUDIO_F32: - this->spec.format = test_format; - valid_format = 1; - break; - } - test_format = SDL_NextAudioFormat(); - } - - if (!valid_format) { - return SDL_SetError("XAudio2: Unsupported audio format"); - } - - /* Update the fragment size as size in bytes */ - SDL_CalculateAudioSpec(&this->spec); - - /* We feed a Source, it feeds the Mastering, which feeds the device. */ - this->hidden->mixlen = this->spec.size; - this->hidden->mixbuf = (Uint8 *) SDL_malloc(2 * this->hidden->mixlen); - if (this->hidden->mixbuf == NULL) { - return SDL_OutOfMemory(); - } - this->hidden->nextbuf = this->hidden->mixbuf; - SDL_memset(this->hidden->mixbuf, this->spec.silence, 2 * this->hidden->mixlen); - - /* We use XAUDIO2_DEFAULT_CHANNELS instead of this->spec.channels. On - Xbox360, this means 5.1 output, but on Windows, it means "figure out - what the system has." It might be preferable to let XAudio2 blast - stereo output to appropriate surround sound configurations - instead of clamping to 2 channels, even though we'll configure the - Source Voice for whatever number of channels you supply. */ -#if SDL_XAUDIO2_WIN8 - result = IXAudio2_CreateMasteringVoice(ixa2, &this->hidden->mastering, - XAUDIO2_DEFAULT_CHANNELS, - this->spec.freq, 0, devId, NULL, AudioCategory_GameEffects); -#else - result = IXAudio2_CreateMasteringVoice(ixa2, &this->hidden->mastering, - XAUDIO2_DEFAULT_CHANNELS, - this->spec.freq, 0, devId, NULL); -#endif - if (result != S_OK) { - return SDL_SetError("XAudio2: Couldn't create mastering voice"); - } - - SDL_zero(waveformat); - if (SDL_AUDIO_ISFLOAT(this->spec.format)) { - waveformat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT; - } else { - waveformat.wFormatTag = WAVE_FORMAT_PCM; - } - waveformat.wBitsPerSample = SDL_AUDIO_BITSIZE(this->spec.format); - waveformat.nChannels = this->spec.channels; - waveformat.nSamplesPerSec = this->spec.freq; - waveformat.nBlockAlign = - waveformat.nChannels * (waveformat.wBitsPerSample / 8); - waveformat.nAvgBytesPerSec = - waveformat.nSamplesPerSec * waveformat.nBlockAlign; - waveformat.cbSize = sizeof(waveformat); - -#ifdef __WINRT__ - // DLudwig: for now, make XAudio2 do sample rate conversion, just to - // get the loopwave test to work. - // - // TODO, WinRT: consider removing WinRT-specific source-voice creation code from SDL_xaudio2.c - result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat, - 0, - 1.0f, &callbacks, NULL, NULL); -#else - result = IXAudio2_CreateSourceVoice(ixa2, &source, &waveformat, - XAUDIO2_VOICE_NOSRC | - XAUDIO2_VOICE_NOPITCH, - 1.0f, &callbacks, NULL, NULL); - -#endif - if (result != S_OK) { - return SDL_SetError("XAudio2: Couldn't create source voice"); - } - this->hidden->source = source; - - /* Start everything playing! */ - result = IXAudio2_StartEngine(ixa2); - if (result != S_OK) { - return SDL_SetError("XAudio2: Couldn't start engine"); - } - - result = IXAudio2SourceVoice_Start(source, 0, XAUDIO2_COMMIT_NOW); - if (result != S_OK) { - return SDL_SetError("XAudio2: Couldn't start source voice"); - } - - return 0; /* good to go. */ -} - -static void -XAUDIO2_Deinitialize(void) -{ -#if defined(__WIN32__) - WIN_CoUninitialize(); -#endif -} - -#endif /* SDL_XAUDIO2_HAS_SDK */ - - -static int -XAUDIO2_Init(SDL_AudioDriverImpl * impl) -{ -#ifndef SDL_XAUDIO2_HAS_SDK - SDL_SetError("XAudio2: SDL was built without XAudio2 support (old DirectX SDK)."); - return 0; /* no XAudio2 support, ever. Update your SDK! */ -#else - /* XAudio2Create() is a macro that uses COM; we don't load the .dll */ - IXAudio2 *ixa2 = NULL; - HRESULT hr = S_FALSE; -#if defined(__WIN32__) - // TODO, WinRT: Investigate using CoInitializeEx here - if (FAILED(WIN_CoInitialize())) { - SDL_SetError("XAudio2: CoInitialize() failed"); - return 0; - } -#endif - - hr = XAudio2Create( &ixa2, 0, XAUDIO2_DEFAULT_PROCESSOR ); - if ( hr != S_OK) { -#if defined(__WIN32__) - WIN_CoUninitialize(); -#endif - SDL_SetError("XAudio2: XAudio2Create() failed at initialization: 0x%.8x", hr ); - return 0; /* not available. */ - } - IXAudio2_Release(ixa2); - - /* Set the function pointers */ - impl->DetectDevices = XAUDIO2_DetectDevices; - impl->OpenDevice = XAUDIO2_OpenDevice; - impl->PlayDevice = XAUDIO2_PlayDevice; - impl->WaitDevice = XAUDIO2_WaitDevice; - impl->PrepareToClose = XAUDIO2_PrepareToClose; - impl->GetDeviceBuf = XAUDIO2_GetDeviceBuf; - impl->CloseDevice = XAUDIO2_CloseDevice; - impl->Deinitialize = XAUDIO2_Deinitialize; - - /* !!! FIXME: We can apparently use a C++ interface on Windows 8 - * !!! FIXME: (Windows::Devices::Enumeration::DeviceInformation) for device - * !!! FIXME: detection, but it's not implemented here yet. - * !!! FIXME: see http://blogs.msdn.com/b/chuckw/archive/2012/04/02/xaudio2-and-windows-8-consumer-preview.aspx - * !!! FIXME: for now, force the default device. - */ -#if defined(SDL_XAUDIO2_WIN8) || defined(__WINRT__) - impl->OnlyHasDefaultOutputDevice = 1; -#endif - - return 1; /* this audio target is available. */ -#endif -} - -AudioBootStrap XAUDIO2_bootstrap = { - "xaudio2", "XAudio2", XAUDIO2_Init, 0 -}; - -#endif /* SDL_AUDIO_DRIVER_XAUDIO2 */ - -/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/audio/xaudio2/SDL_xaudio2.h b/src/audio/xaudio2/SDL_xaudio2.h deleted file mode 100644 index cbfd3cdd9..000000000 --- a/src/audio/xaudio2/SDL_xaudio2.h +++ /dev/null @@ -1,386 +0,0 @@ -/* - Simple DirectMedia Layer - Copyright (C) 1997-2017 Sam Lantinga - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. -*/ - -#ifndef SDL_XAUDIO2_H_ -#define SDL_XAUDIO2_H_ - -#include -#include -#include - -/* XAudio2 packs its structure members together as tightly as possible. - This pragma is needed to ensure compatibility with XAudio2 on 64-bit - platforms. -*/ -#pragma pack(push, 1) - -typedef interface IXAudio2 IXAudio2; -typedef interface IXAudio2SourceVoice IXAudio2SourceVoice; -typedef interface IXAudio2MasteringVoice IXAudio2MasteringVoice; -typedef interface IXAudio2EngineCallback IXAudio2EngineCallback; -typedef interface IXAudio2VoiceCallback IXAudio2VoiceCallback; -typedef interface IXAudio2Voice IXAudio2Voice; -typedef interface IXAudio2SubmixVoice IXAudio2SubmixVoice; - -typedef enum _AUDIO_STREAM_CATEGORY { - AudioCategory_Other = 0, - AudioCategory_ForegroundOnlyMedia, - AudioCategory_BackgroundCapableMedia, - AudioCategory_Communications, - AudioCategory_Alerts, - AudioCategory_SoundEffects, - AudioCategory_GameEffects, - AudioCategory_GameMedia, - AudioCategory_GameChat, - AudioCategory_Movie, - AudioCategory_Media -} AUDIO_STREAM_CATEGORY; - -typedef struct XAUDIO2_BUFFER { - UINT32 Flags; - UINT32 AudioBytes; - const BYTE *pAudioData; - UINT32 PlayBegin; - UINT32 PlayLength; - UINT32 LoopBegin; - UINT32 LoopLength; - UINT32 LoopCount; - void *pContext; -} XAUDIO2_BUFFER; - -typedef struct XAUDIO2_BUFFER_WMA { - const UINT32 *pDecodedPacketCumulativeBytes; - UINT32 PacketCount; -} XAUDIO2_BUFFER_WMA; - -typedef struct XAUDIO2_SEND_DESCRIPTOR { - UINT32 Flags; - IXAudio2Voice *pOutputVoice; -} XAUDIO2_SEND_DESCRIPTOR; - -typedef struct XAUDIO2_VOICE_SENDS { - UINT32 SendCount; - XAUDIO2_SEND_DESCRIPTOR *pSends; -} XAUDIO2_VOICE_SENDS; - -typedef struct XAUDIO2_EFFECT_DESCRIPTOR { - IUnknown *pEffect; - BOOL InitialState; - UINT32 OutputChannels; -} XAUDIO2_EFFECT_DESCRIPTOR; - -typedef struct XAUDIO2_EFFECT_CHAIN { - UINT32 EffectCount; - XAUDIO2_EFFECT_DESCRIPTOR *pEffectDescriptors; -} XAUDIO2_EFFECT_CHAIN; - -typedef struct XAUDIO2_PERFORMANCE_DATA { - UINT64 AudioCyclesSinceLastQuery; - UINT64 TotalCyclesSinceLastQuery; - UINT32 MinimumCyclesPerQuantum; - UINT32 MaximumCyclesPerQuantum; - UINT32 MemoryUsageInBytes; - UINT32 CurrentLatencyInSamples; - UINT32 GlitchesSinceEngineStarted; - UINT32 ActiveSourceVoiceCount; - UINT32 TotalSourceVoiceCount; - UINT32 ActiveSubmixVoiceCount; - UINT32 ActiveResamplerCount; - UINT32 ActiveMatrixMixCount; - UINT32 ActiveXmaSourceVoices; - UINT32 ActiveXmaStreams; -} XAUDIO2_PERFORMANCE_DATA; - -typedef struct XAUDIO2_DEBUG_CONFIGURATION { - UINT32 TraceMask; - UINT32 BreakMask; - BOOL LogThreadID; - BOOL LogFileline; - BOOL LogFunctionName; - BOOL LogTiming; -} XAUDIO2_DEBUG_CONFIGURATION; - -typedef struct XAUDIO2_VOICE_DETAILS { - UINT32 CreationFlags; - UINT32 ActiveFlags; - UINT32 InputChannels; - UINT32 InputSampleRate; -} XAUDIO2_VOICE_DETAILS; - -typedef enum XAUDIO2_FILTER_TYPE { - LowPassFilter = 0, - BandPassFilter = 1, - HighPassFilter = 2, - NotchFilter = 3, - LowPassOnePoleFilter = 4, - HighPassOnePoleFilter = 5 -} XAUDIO2_FILTER_TYPE; - -typedef struct XAUDIO2_FILTER_PARAMETERS { - XAUDIO2_FILTER_TYPE Type; - float Frequency; - float OneOverQ; -} XAUDIO2_FILTER_PARAMETERS; - -typedef struct XAUDIO2_VOICE_STATE { - void *pCurrentBufferContext; - UINT32 BuffersQueued; - UINT64 SamplesPlayed; -} XAUDIO2_VOICE_STATE; - - -typedef UINT32 XAUDIO2_PROCESSOR; -#define Processor1 0x00000001 -#define XAUDIO2_DEFAULT_PROCESSOR Processor1 - -#define XAUDIO2_E_DEVICE_INVALIDATED 0x88960004 -#define XAUDIO2_COMMIT_NOW 0 -#define XAUDIO2_VOICE_NOSAMPLESPLAYED 0x0100 -#define XAUDIO2_DEFAULT_CHANNELS 0 - -extern HRESULT __stdcall XAudio2Create( - _Out_ IXAudio2 **ppXAudio2, - _In_ UINT32 Flags, - _In_ XAUDIO2_PROCESSOR XAudio2Processor - ); - -#undef INTERFACE -#define INTERFACE IXAudio2 -typedef interface IXAudio2 { - const struct IXAudio2Vtbl FAR* lpVtbl; -} IXAudio2; -typedef const struct IXAudio2Vtbl IXAudio2Vtbl; -const struct IXAudio2Vtbl -{ - /* IUnknown */ - STDMETHOD(QueryInterface)(THIS_ REFIID iid, LPVOID *ppv) PURE; - STDMETHOD_(ULONG, AddRef)(THIS) PURE; - STDMETHOD_(ULONG, Release)(THIS) PURE; - - /* IXAudio2 */ - STDMETHOD_(HRESULT, RegisterForCallbacks)(THIS, IXAudio2EngineCallback *pCallback) PURE; - STDMETHOD_(VOID, UnregisterForCallbacks)(THIS, IXAudio2EngineCallback *pCallback) PURE; - STDMETHOD_(HRESULT, CreateSourceVoice)(THIS, IXAudio2SourceVoice **ppSourceVoice, - const WAVEFORMATEX *pSourceFormat, - UINT32 Flags, - float MaxFrequencyRatio, - IXAudio2VoiceCallback *pCallback, - const XAUDIO2_VOICE_SENDS *pSendList, - const XAUDIO2_EFFECT_CHAIN *pEffectChain) PURE; - STDMETHOD_(HRESULT, CreateSubmixVoice)(THIS, IXAudio2SubmixVoice **ppSubmixVoice, - UINT32 InputChannels, - UINT32 InputSampleRate, - UINT32 Flags, - UINT32 ProcessingStage, - const XAUDIO2_VOICE_SENDS *pSendList, - const XAUDIO2_EFFECT_CHAIN *pEffectChain) PURE; - STDMETHOD_(HRESULT, CreateMasteringVoice)(THIS, IXAudio2MasteringVoice **ppMasteringVoice, - UINT32 InputChannels, - UINT32 InputSampleRate, - UINT32 Flags, - LPCWSTR szDeviceId, - const XAUDIO2_EFFECT_CHAIN *pEffectChain, - AUDIO_STREAM_CATEGORY StreamCategory) PURE; - STDMETHOD_(HRESULT, StartEngine)(THIS) PURE; - STDMETHOD_(VOID, StopEngine)(THIS) PURE; - STDMETHOD_(HRESULT, CommitChanges)(THIS, UINT32 OperationSet) PURE; - STDMETHOD_(HRESULT, GetPerformanceData)(THIS, XAUDIO2_PERFORMANCE_DATA *pPerfData) PURE; - STDMETHOD_(HRESULT, SetDebugConfiguration)(THIS, XAUDIO2_DEBUG_CONFIGURATION *pDebugConfiguration, - VOID *pReserved) PURE; -}; - -#define IXAudio2_Release(A) ((A)->lpVtbl->Release(A)) -#define IXAudio2_CreateSourceVoice(A,B,C,D,E,F,G,H) ((A)->lpVtbl->CreateSourceVoice(A,B,C,D,E,F,G,H)) -#define IXAudio2_CreateMasteringVoice(A,B,C,D,E,F,G,H) ((A)->lpVtbl->CreateMasteringVoice(A,B,C,D,E,F,G,H)) -#define IXAudio2_StartEngine(A) ((A)->lpVtbl->StartEngine(A)) -#define IXAudio2_StopEngine(A) ((A)->lpVtbl->StopEngine(A)) - - -#undef INTERFACE -#define INTERFACE IXAudio2SourceVoice -typedef interface IXAudio2SourceVoice { - const struct IXAudio2SourceVoiceVtbl FAR* lpVtbl; -} IXAudio2SourceVoice; -typedef const struct IXAudio2SourceVoiceVtbl IXAudio2SourceVoiceVtbl; -const struct IXAudio2SourceVoiceVtbl -{ - /* MSDN says that IXAudio2Voice inherits from IXAudio2, but MSVC's debugger - * says otherwise, and that IXAudio2Voice doesn't inherit from any other - * interface! - */ - - /* IXAudio2Voice */ - STDMETHOD_(VOID, GetVoiceDetails)(THIS, XAUDIO2_VOICE_DETAILS *pVoiceDetails) PURE; - STDMETHOD_(HRESULT, SetOutputVoices)(THIS, const XAUDIO2_VOICE_SENDS *pSendList) PURE; - STDMETHOD_(HRESULT, SetEffectChain)(THIS, const XAUDIO2_EFFECT_CHAIN *pEffectChain) PURE; - STDMETHOD_(HRESULT, EnableEffect)(THIS, UINT32 EffectIndex, UINT32 OperationSet) PURE; - STDMETHOD_(HRESULT, DisableEffect)(THIS, UINT32 EffectIndex, UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetEffectState)(THIS, UINT32 EffectIndex, BOOL *pEnabled) PURE; - STDMETHOD_(HRESULT, SetEffectParameters)(THIS, UINT32 EffectIndex, - const void *pParameters, - UINT32 ParametersByteSize, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetEffectParameters)(THIS, UINT32 EffectIndex, - void *pParameters, - UINT32 ParametersByteSize) PURE; - STDMETHOD_(HRESULT, SetFilterParameters)(THIS, const XAUDIO2_FILTER_PARAMETERS *pParameters, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetFilterParameters)(THIS, XAUDIO2_FILTER_PARAMETERS *pParameters) PURE; - STDMETHOD_(HRESULT, SetOutputFilterParameters)(THIS, IXAudio2Voice *pDestinationVoice, - XAUDIO2_FILTER_PARAMETERS *pParameters, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetOutputFilterParameters)(THIS, IXAudio2Voice *pDestinationVoice, - XAUDIO2_FILTER_PARAMETERS *pParameters) PURE; - STDMETHOD_(HRESULT, SetVolume)(THIS, float Volume, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetVolume)(THIS, float *pVolume) PURE; - STDMETHOD_(HRESULT, SetChannelVolumes)(THIS, UINT32 Channels, - const float *pVolumes, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetChannelVolumes)(THIS, UINT32 Channels, - float *pVolumes) PURE; - STDMETHOD_(HRESULT, SetOutputMatrix)(THIS, IXAudio2Voice *pDestinationVoice, - UINT32 SourceChannels, - UINT32 DestinationChannels, - const float *pLevelMatrix, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetOutputMatrix)(THIS, IXAudio2Voice *pDestinationVoice, - UINT32 SourceChannels, - UINT32 DestinationChannels, - float *pLevelMatrix) PURE; - STDMETHOD_(VOID, DestroyVoice)(THIS) PURE; - - /* IXAudio2SourceVoice */ - STDMETHOD_(HRESULT, Start)(THIS, UINT32 Flags, - UINT32 OperationSet) PURE; - STDMETHOD_(HRESULT, Stop)(THIS, UINT32 Flags, - UINT32 OperationSet) PURE; - STDMETHOD_(HRESULT, SubmitSourceBuffer)(THIS, const XAUDIO2_BUFFER *pBuffer, - const XAUDIO2_BUFFER_WMA *pBufferWMA) PURE; - STDMETHOD_(HRESULT, FlushSourceBuffers)(THIS) PURE; - STDMETHOD_(HRESULT, Discontinuity)(THIS) PURE; - STDMETHOD_(HRESULT, ExitLoop)(THIS, UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetState)(THIS, XAUDIO2_VOICE_STATE *pVoiceState, - UINT32 Flags) PURE; - STDMETHOD_(HRESULT, SetFrequencyRatio)(THIS, float Ratio, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetFrequencyRatio)(THIS, float *pRatio) PURE; - STDMETHOD_(HRESULT, SetSourceSampleRate)(THIS, UINT32 NewSourceSampleRate) PURE; -}; - -#define IXAudio2SourceVoice_DestroyVoice(A) ((A)->lpVtbl->DestroyVoice(A)) -#define IXAudio2SourceVoice_Start(A,B,C) ((A)->lpVtbl->Start(A,B,C)) -#define IXAudio2SourceVoice_Stop(A,B,C) ((A)->lpVtbl->Stop(A,B,C)) -#define IXAudio2SourceVoice_SubmitSourceBuffer(A,B,C) ((A)->lpVtbl->SubmitSourceBuffer(A,B,C)) -#define IXAudio2SourceVoice_FlushSourceBuffers(A) ((A)->lpVtbl->FlushSourceBuffers(A)) -#define IXAudio2SourceVoice_Discontinuity(A) ((A)->lpVtbl->Discontinuity(A)) -#define IXAudio2SourceVoice_GetState(A,B,C) ((A)->lpVtbl->GetState(A,B,C)) - - -#undef INTERFACE -#define INTERFACE IXAudio2MasteringVoice -typedef interface IXAudio2MasteringVoice { - const struct IXAudio2MasteringVoiceVtbl FAR* lpVtbl; -} IXAudio2MasteringVoice; -typedef const struct IXAudio2MasteringVoiceVtbl IXAudio2MasteringVoiceVtbl; -const struct IXAudio2MasteringVoiceVtbl -{ - /* MSDN says that IXAudio2Voice inherits from IXAudio2, but MSVC's debugger - * says otherwise, and that IXAudio2Voice doesn't inherit from any other - * interface! - */ - - /* IXAudio2Voice */ - STDMETHOD_(VOID, GetVoiceDetails)(THIS, XAUDIO2_VOICE_DETAILS *pVoiceDetails) PURE; - STDMETHOD_(HRESULT, SetOutputVoices)(THIS, const XAUDIO2_VOICE_SENDS *pSendList) PURE; - STDMETHOD_(HRESULT, SetEffectChain)(THIS, const XAUDIO2_EFFECT_CHAIN *pEffectChain) PURE; - STDMETHOD_(HRESULT, EnableEffect)(THIS, UINT32 EffectIndex, UINT32 OperationSet) PURE; - STDMETHOD_(HRESULT, DisableEffect)(THIS, UINT32 EffectIndex, UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetEffectState)(THIS, UINT32 EffectIndex, BOOL *pEnabled) PURE; - STDMETHOD_(HRESULT, SetEffectParameters)(THIS, UINT32 EffectIndex, - const void *pParameters, - UINT32 ParametersByteSize, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetEffectParameters)(THIS, UINT32 EffectIndex, - void *pParameters, - UINT32 ParametersByteSize) PURE; - STDMETHOD_(HRESULT, SetFilterParameters)(THIS, const XAUDIO2_FILTER_PARAMETERS *pParameters, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetFilterParameters)(THIS, XAUDIO2_FILTER_PARAMETERS *pParameters) PURE; - STDMETHOD_(HRESULT, SetOutputFilterParameters)(THIS, IXAudio2Voice *pDestinationVoice, - XAUDIO2_FILTER_PARAMETERS *pParameters, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetOutputFilterParameters)(THIS, IXAudio2Voice *pDestinationVoice, - XAUDIO2_FILTER_PARAMETERS *pParameters) PURE; - STDMETHOD_(HRESULT, SetVolume)(THIS, float Volume, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetVolume)(THIS, float *pVolume) PURE; - STDMETHOD_(HRESULT, SetChannelVolumes)(THIS, UINT32 Channels, - const float *pVolumes, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetChannelVolumes)(THIS, UINT32 Channels, - float *pVolumes) PURE; - STDMETHOD_(HRESULT, SetOutputMatrix)(THIS, IXAudio2Voice *pDestinationVoice, - UINT32 SourceChannels, - UINT32 DestinationChannels, - const float *pLevelMatrix, - UINT32 OperationSet) PURE; - STDMETHOD_(VOID, GetOutputMatrix)(THIS, IXAudio2Voice *pDestinationVoice, - UINT32 SourceChannels, - UINT32 DestinationChannels, - float *pLevelMatrix) PURE; - STDMETHOD_(VOID, DestroyVoice)(THIS) PURE; - - /* IXAudio2SourceVoice */ - STDMETHOD_(VOID, GetChannelMask)(THIS, DWORD *pChannelMask) PURE; -}; - -#define IXAudio2MasteringVoice_DestroyVoice(A) ((A)->lpVtbl->DestroyVoice(A)) - - -#undef INTERFACE -#define INTERFACE IXAudio2VoiceCallback -typedef interface IXAudio2VoiceCallback { - const struct IXAudio2VoiceCallbackVtbl FAR* lpVtbl; -} IXAudio2VoiceCallback; -typedef const struct IXAudio2VoiceCallbackVtbl IXAudio2VoiceCallbackVtbl; -const struct IXAudio2VoiceCallbackVtbl -{ - /* MSDN says that IXAudio2VoiceCallback inherits from IXAudio2, but SDL's - * own code says otherwise, and that IXAudio2VoiceCallback doesn't inherit - * from any other interface! - */ - - /* IXAudio2VoiceCallback */ - STDMETHOD_(VOID, OnVoiceProcessingPassStart)(THIS, UINT32 BytesRequired) PURE; - STDMETHOD_(VOID, OnVoiceProcessingPassEnd)(THIS) PURE; - STDMETHOD_(VOID, OnStreamEnd)(THIS) PURE; - STDMETHOD_(VOID, OnBufferStart)(THIS, void *pBufferContext) PURE; - STDMETHOD_(VOID, OnBufferEnd)(THIS, void *pBufferContext) PURE; - STDMETHOD_(VOID, OnLoopEnd)(THIS, void *pBufferContext) PURE; - STDMETHOD_(VOID, OnVoiceError)(THIS, void *pBufferContext, HRESULT Error) PURE; -}; - -#pragma pack(pop) /* Undo pragma push */ - -#endif /* SDL_XAUDIO2_H_ */ - -/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/audio/xaudio2/SDL_xaudio2_winrthelpers.cpp b/src/audio/xaudio2/SDL_xaudio2_winrthelpers.cpp deleted file mode 100644 index 8915023b8..000000000 --- a/src/audio/xaudio2/SDL_xaudio2_winrthelpers.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* - Simple DirectMedia Layer - Copyright (C) 1997-2017 Sam Lantinga - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. -*/ -#include "../../SDL_internal.h" - -#include -#include "SDL_xaudio2_winrthelpers.h" - -#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP -using Windows::Devices::Enumeration::DeviceClass; -using Windows::Devices::Enumeration::DeviceInformation; -using Windows::Devices::Enumeration::DeviceInformationCollection; -#endif - -extern "C" HRESULT __cdecl IXAudio2_GetDeviceCount(IXAudio2 * ixa2, UINT32 * devcount) -{ -#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP - // There doesn't seem to be any audio device enumeration on Windows Phone. - // In lieu of this, just treat things as if there is one and only one - // audio device. - *devcount = 1; - return S_OK; -#else - // TODO, WinRT: make xaudio2 device enumeration only happen once, and in the background - auto operation = DeviceInformation::FindAllAsync(DeviceClass::AudioRender); - while (operation->Status != Windows::Foundation::AsyncStatus::Completed) - { - } - - DeviceInformationCollection^ devices = operation->GetResults(); - *devcount = devices->Size; - return S_OK; -#endif -} - -extern "C" HRESULT IXAudio2_GetDeviceDetails(IXAudio2 * unused, UINT32 index, XAUDIO2_DEVICE_DETAILS * details) -{ -#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP - // Windows Phone doesn't seem to have the same device enumeration APIs that - // Windows 8/RT has, or it doesn't have them at all. In lieu of this, - // just treat things as if there is one, and only one, default device. - if (index != 0) - { - return XAUDIO2_E_INVALID_CALL; - } - - if (details) - { - wcsncpy_s(details->DeviceID, ARRAYSIZE(details->DeviceID), L"default", _TRUNCATE); - wcsncpy_s(details->DisplayName, ARRAYSIZE(details->DisplayName), L"default", _TRUNCATE); - } - return S_OK; -#else - auto operation = DeviceInformation::FindAllAsync(DeviceClass::AudioRender); - while (operation->Status != Windows::Foundation::AsyncStatus::Completed) - { - } - - DeviceInformationCollection^ devices = operation->GetResults(); - if (index >= devices->Size) - { - return XAUDIO2_E_INVALID_CALL; - } - - DeviceInformation^ d = devices->GetAt(index); - if (details) - { - wcsncpy_s(details->DeviceID, ARRAYSIZE(details->DeviceID), d->Id->Data(), _TRUNCATE); - wcsncpy_s(details->DisplayName, ARRAYSIZE(details->DisplayName), d->Name->Data(), _TRUNCATE); - } - return S_OK; -#endif -} diff --git a/src/audio/xaudio2/SDL_xaudio2_winrthelpers.h b/src/audio/xaudio2/SDL_xaudio2_winrthelpers.h deleted file mode 100644 index ff08fafdf..000000000 --- a/src/audio/xaudio2/SDL_xaudio2_winrthelpers.h +++ /dev/null @@ -1,70 +0,0 @@ -/* - Simple DirectMedia Layer - Copyright (C) 1997-2017 Sam Lantinga - - This software is provided 'as-is', without any express or implied - warranty. In no event will the authors be held liable for any damages - arising from the use of this software. - - Permission is granted to anyone to use this software for any purpose, - including commercial applications, and to alter it and redistribute it - freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - 3. This notice may not be removed or altered from any source distribution. -*/ - -// -// Re-implementation of methods removed from XAudio2 (in WinRT): -// - -typedef struct XAUDIO2_DEVICE_DETAILS -{ - WCHAR DeviceID[256]; - WCHAR DisplayName[256]; - /* Other fields exist in the pre-Windows 8 version of this struct, however - they weren't used by SDL, so they weren't added. - */ -} XAUDIO2_DEVICE_DETAILS; - - -#ifdef __cplusplus -extern "C" { -#endif - -HRESULT IXAudio2_GetDeviceCount(IXAudio2 * unused, UINT32 * devcount); -HRESULT IXAudio2_GetDeviceDetails(IXAudio2 * unused, UINT32 index, XAUDIO2_DEVICE_DETAILS * details); - -#ifdef __cplusplus -} -#endif - - -// -// C-style macros to call XAudio2's methods in C++: -// -#ifdef __cplusplus -/* -#define IXAudio2_CreateMasteringVoice(A, B, C, D, E, F, G) (A)->CreateMasteringVoice((B), (C), (D), (E), (F), (G)) -#define IXAudio2_CreateSourceVoice(A, B, C, D, E, F, G, H) (A)->CreateSourceVoice((B), (C), (D), (E), (F), (G), (H)) -#define IXAudio2_QueryInterface(A, B, C) (A)->QueryInterface((B), (C)) -#define IXAudio2_Release(A) (A)->Release() -#define IXAudio2_StartEngine(A) (A)->StartEngine() -#define IXAudio2_StopEngine(A) (A)->StopEngine() - -#define IXAudio2MasteringVoice_DestroyVoice(A) (A)->DestroyVoice() - -#define IXAudio2SourceVoice_DestroyVoice(A) (A)->DestroyVoice() -#define IXAudio2SourceVoice_Discontinuity(A) (A)->Discontinuity() -#define IXAudio2SourceVoice_FlushSourceBuffers(A) (A)->FlushSourceBuffers() -#define IXAudio2SourceVoice_GetState(A, B) (A)->GetState((B)) -#define IXAudio2SourceVoice_Start(A, B, C) (A)->Start((B), (C)) -#define IXAudio2SourceVoice_Stop(A, B, C) (A)->Stop((B), (C)) -#define IXAudio2SourceVoice_SubmitSourceBuffer(A, B, C) (A)->SubmitSourceBuffer((B), (C)) -*/ -#endif // ifdef __cplusplus