From 0b58e96d9e34dd76a853fe2dd67997c1ace378b2 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Fri, 14 Jul 2023 23:37:17 -0400 Subject: [PATCH] wasapi: Patched WinRT to compile. --- src/audio/wasapi/SDL_wasapi_winrt.cpp | 12 ++++++------ src/core/windows/SDL_immdevice.c | 23 ----------------------- src/core/windows/SDL_immdevice.h | 1 - src/core/windows/SDL_windows.c | 27 +++++++++++++++++++++++++++ src/core/windows/SDL_windows.h | 3 +++ 5 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/audio/wasapi/SDL_wasapi_winrt.cpp b/src/audio/wasapi/SDL_wasapi_winrt.cpp index 3280f13597..373244ac73 100644 --- a/src/audio/wasapi/SDL_wasapi_winrt.cpp +++ b/src/audio/wasapi/SDL_wasapi_winrt.cpp @@ -159,12 +159,12 @@ void SDL_WasapiDeviceEventHandler::OnDeviceAdded(DeviceWatcher ^ sender, DeviceI WAVEFORMATEXTENSIBLE fmt; SDL_zero(fmt); SDL_memcpy(&fmt, data->Data, SDL_min(data->Length, sizeof(WAVEFORMATEXTENSIBLE))); - spec.channels = (Uint8)fmt->Format.nChannels; - spec.freq = fmt->Format.nSamplesPerSec; - spec.format = SDL_WaveFormatExToSDLFormat((WAVEFORMATEX *)fmt); + spec.channels = (Uint8)fmt.Format.nChannels; + spec.freq = fmt.Format.nSamplesPerSec; + spec.format = SDL_WaveFormatExToSDLFormat((WAVEFORMATEX *)&fmt); } - LPWSTR devid = SDL_wcsdup(devid); + LPWSTR devid = SDL_wcsdup(info->Id->Data()); if (devid) { SDL_AddAudioDevice(this->iscapture, utf8dev, spec.channels ? &spec : NULL, devid); } @@ -244,7 +244,7 @@ void WASAPI_EnumerateEndpoints(SDL_AudioDevice **default_output, SDL_AudioDevice capture_device_event_handler = new SDL_WasapiDeviceEventHandler(SDL_TRUE); capture_device_event_handler->WaitForCompletion(); - defdevid = MediaDevice::GetDefaultAudioCaptureId(AudioDeviceRole::Default) + defdevid = MediaDevice::GetDefaultAudioCaptureId(AudioDeviceRole::Default); if (defdevid) { *default_capture = FindWinRTAudioDevice(defdevid->Data()); } @@ -307,7 +307,7 @@ int WASAPI_ActivateDevice(SDL_AudioDevice *device) } // !!! FIXME: the problems in SDL2 that needed this to be synchronous are _probably_ solved by SDL3, and this can block indefinitely if a user prompt is shown to get permission to use a microphone. - handler.WaitForCompletion(); // block here until we have an answer, so this is synchronous to us after all. + handler.Get()->WaitForCompletion(); // block here until we have an answer, so this is synchronous to us after all. HRESULT activateRes = S_OK; IUnknown *iunknown = nullptr; diff --git a/src/core/windows/SDL_immdevice.c b/src/core/windows/SDL_immdevice.c index b8c5793d15..160297e387 100644 --- a/src/core/windows/SDL_immdevice.c +++ b/src/core/windows/SDL_immdevice.c @@ -53,8 +53,6 @@ static const IID SDL_IID_IMMEndpoint = { 0x1be09788, 0x6894, 0x4089,{ 0x85, 0x86 static const PROPERTYKEY SDL_PKEY_Device_FriendlyName = { { 0xa45c254e, 0xdf1c, 0x4efd,{ 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, } }, 14 }; static const PROPERTYKEY SDL_PKEY_AudioEngine_DeviceFormat = { { 0xf19f064d, 0x82c, 0x4e27,{ 0xbc, 0x73, 0x68, 0x82, 0xa1, 0xbb, 0x8e, 0x4c, } }, 0 }; static const PROPERTYKEY SDL_PKEY_AudioEndpoint_GUID = { { 0x1da5d803, 0xd492, 0x4edd,{ 0x8c, 0x23, 0xe0, 0xc0, 0xff, 0xee, 0x7f, 0x0e, } }, 4 }; -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 } }; /* *INDENT-ON* */ /* clang-format on */ static SDL_bool FindByDevIDCallback(SDL_AudioDevice *device, void *userdata) @@ -394,25 +392,4 @@ void SDL_IMMDevice_EnumerateEndpoints(SDL_AudioDevice **default_output, SDL_Audi IMMDeviceEnumerator_RegisterEndpointNotificationCallback(enumerator, (IMMNotificationClient *)¬ification_client); } -SDL_AudioFormat SDL_WaveFormatExToSDLFormat(WAVEFORMATEX *waveformat) -{ - if ((waveformat->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) && (waveformat->wBitsPerSample == 32)) { - return SDL_AUDIO_F32SYS; - } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 16)) { - return SDL_AUDIO_S16SYS; - } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 32)) { - return SDL_AUDIO_S32SYS; - } else if (waveformat->wFormatTag == WAVE_FORMAT_EXTENSIBLE) { - const WAVEFORMATEXTENSIBLE *ext = (const WAVEFORMATEXTENSIBLE *)waveformat; - if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 32)) { - return SDL_AUDIO_F32SYS; - } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 16)) { - return SDL_AUDIO_S16SYS; - } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 32)) { - return SDL_AUDIO_S32SYS; - } - } - return 0; -} - #endif /* (defined(__WIN32__) || defined(__GDK__)) && defined(HAVE_MMDEVICEAPI_H) */ diff --git a/src/core/windows/SDL_immdevice.h b/src/core/windows/SDL_immdevice.h index d71dc053f5..a190a7cf84 100644 --- a/src/core/windows/SDL_immdevice.h +++ b/src/core/windows/SDL_immdevice.h @@ -35,6 +35,5 @@ void SDL_IMMDevice_EnumerateEndpoints(SDL_AudioDevice **default_output, SDL_Audi LPGUID SDL_IMMDevice_GetDirectSoundGUID(SDL_AudioDevice *device); LPCWSTR SDL_IMMDevice_GetDevID(SDL_AudioDevice *device); void SDL_IMMDevice_FreeDeviceHandle(SDL_AudioDevice *device); -SDL_AudioFormat SDL_WaveFormatExToSDLFormat(WAVEFORMATEX *waveformat); #endif /* SDL_IMMDEVICE_H */ diff --git a/src/core/windows/SDL_windows.c b/src/core/windows/SDL_windows.c index c18bd642cd..b842622e6b 100644 --- a/src/core/windows/SDL_windows.c +++ b/src/core/windows/SDL_windows.c @@ -335,6 +335,33 @@ BOOL WIN_IsRectEmpty(const RECT *rect) return (rect->right <= rect->left) || (rect->bottom <= rect->top); } +/* Some GUIDs we need to know without linking to libraries that aren't available before Vista. */ +/* *INDENT-OFF* */ /* clang-format off */ +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 } }; +/* *INDENT-ON* */ /* clang-format on */ + +SDL_AudioFormat SDL_WaveFormatExToSDLFormat(WAVEFORMATEX *waveformat) +{ + if ((waveformat->wFormatTag == WAVE_FORMAT_IEEE_FLOAT) && (waveformat->wBitsPerSample == 32)) { + return SDL_AUDIO_F32SYS; + } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 16)) { + return SDL_AUDIO_S16SYS; + } else if ((waveformat->wFormatTag == WAVE_FORMAT_PCM) && (waveformat->wBitsPerSample == 32)) { + return SDL_AUDIO_S32SYS; + } else if (waveformat->wFormatTag == WAVE_FORMAT_EXTENSIBLE) { + const WAVEFORMATEXTENSIBLE *ext = (const WAVEFORMATEXTENSIBLE *)waveformat; + if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_IEEE_FLOAT, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 32)) { + return SDL_AUDIO_F32SYS; + } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 16)) { + return SDL_AUDIO_S16SYS; + } else if ((SDL_memcmp(&ext->SubFormat, &SDL_KSDATAFORMAT_SUBTYPE_PCM, sizeof(GUID)) == 0) && (waveformat->wBitsPerSample == 32)) { + return SDL_AUDIO_S32SYS; + } + } + return 0; +} + /* Win32-specific SDL_RunApp(), which does most of the SDL_main work, based on SDL_windows_main.c, placed in the public domain by Sam Lantinga 4/13/98 */ #ifdef __WIN32__ diff --git a/src/core/windows/SDL_windows.h b/src/core/windows/SDL_windows.h index f6d87b1828..e9eb1f0d6e 100644 --- a/src/core/windows/SDL_windows.h +++ b/src/core/windows/SDL_windows.h @@ -78,6 +78,7 @@ #include #include /* for REFIID with broken mingw.org headers */ +#include /* Older Visual C++ headers don't have the Win64-compatible typedefs... */ #if defined(_MSC_VER) && (_MSC_VER <= 1200) @@ -154,6 +155,8 @@ extern void WIN_RectToRECT(const SDL_Rect *sdlrect, RECT *winrect); /* Returns SDL_TRUE if the rect is empty */ extern BOOL WIN_IsRectEmpty(const RECT *rect); +extern SDL_AudioFormat SDL_WaveFormatExToSDLFormat(WAVEFORMATEX *waveformat); + /* Ends C function definitions when using C++ */ #ifdef __cplusplus }