From bc5798530663a9b3fec8df60feba510bd681c5ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20M=C3=BCller?= Date: Thu, 11 Mar 2021 19:22:56 +0100 Subject: Audaspace: add support for WASAPI on Windows This adds WASAPI as audio backend on Windows. WASAPI is the modern standard audio API on Windows introduced with Windows Vista. Ref T86590 --- extern/audaspace/plugins/wasapi/WASAPIDevice.cpp | 401 +++++++++++++++++++++++ extern/audaspace/plugins/wasapi/WASAPIDevice.h | 99 ++++++ 2 files changed, 500 insertions(+) create mode 100644 extern/audaspace/plugins/wasapi/WASAPIDevice.cpp create mode 100644 extern/audaspace/plugins/wasapi/WASAPIDevice.h (limited to 'extern/audaspace/plugins') diff --git a/extern/audaspace/plugins/wasapi/WASAPIDevice.cpp b/extern/audaspace/plugins/wasapi/WASAPIDevice.cpp new file mode 100644 index 00000000000..241f694feb5 --- /dev/null +++ b/extern/audaspace/plugins/wasapi/WASAPIDevice.cpp @@ -0,0 +1,401 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +#include "WASAPIDevice.h" +#include "devices/DeviceManager.h" +#include "devices/IDeviceFactory.h" +#include "Exception.h" +#include "IReader.h" + +AUD_NAMESPACE_BEGIN + +template void SafeRelease(T **ppT) +{ + if(*ppT) + { + (*ppT)->Release(); + *ppT = NULL; + } +} + +void WASAPIDevice::start() +{ + lock(); + + if(!m_playing) + { + if(m_thread.joinable()) + m_thread.join(); + + m_playing = true; + + m_thread = std::thread(&WASAPIDevice::updateStream, this); + } + + unlock(); +} + +void WASAPIDevice::updateStream() +{ + UINT32 buffer_size; + data_t* buffer; + + if(FAILED(m_audio_client->GetBufferSize(&buffer_size))) + return; + + IAudioRenderClient* render_client = nullptr; + const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient); + + if(FAILED(m_audio_client->GetService(IID_IAudioRenderClient, reinterpret_cast(&render_client)))) + return; + + UINT32 padding; + + if(FAILED(m_audio_client->GetCurrentPadding(&padding))) + { + SafeRelease(&render_client); + return; + } + + UINT32 length = buffer_size - padding; + + if(FAILED(render_client->GetBuffer(length, &buffer))) + { + SafeRelease(&render_client); + return; + } + + lock(); + + mix((data_t*)buffer, length); + + unlock(); + + if(FAILED(render_client->ReleaseBuffer(length, 0))) + { + SafeRelease(&render_client); + return; + } + + m_audio_client->Start(); + + auto sleepDuration = std::chrono::milliseconds(buffer_size * 1000 / int(m_specs.rate) / 2); + + for(;;) + { + if(FAILED(m_audio_client->GetCurrentPadding(&padding))) + { + m_audio_client->Stop(); + SafeRelease(&render_client); + return; + } + + length = buffer_size - padding; + + if(FAILED(render_client->GetBuffer(length, &buffer))) + { + m_audio_client->Stop(); + SafeRelease(&render_client); + return; + } + + lock(); + + mix((data_t*)buffer, length); + + unlock(); + + if(FAILED(render_client->ReleaseBuffer(length, 0))) + { + m_audio_client->Stop(); + SafeRelease(&render_client); + return; + } + + // stop thread + if(!m_playing) + { + m_audio_client->Stop(); + SafeRelease(&render_client); + return; + } + + std::this_thread::sleep_for(sleepDuration); + } +} + +void WASAPIDevice::playing(bool playing) +{ + if(!m_playing && playing) + start(); + else + m_playing = playing; +} + +WASAPIDevice::WASAPIDevice(DeviceSpecs specs, int buffersize) : + m_playing(false), + + m_imm_device_enumerator(nullptr), + m_imm_device(nullptr), + m_audio_client(nullptr), + + m_wave_format_extensible({}) +{ + // initialize COM if it hasn't happened yet + CoInitializeEx(nullptr, COINIT_MULTITHREADED); + + const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator); + const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator); + const IID IID_IAudioClient = __uuidof(IAudioClient); + + WAVEFORMATEXTENSIBLE wave_format_extensible_closest_match; + WAVEFORMATEXTENSIBLE* closest_match_pointer = &wave_format_extensible_closest_match; + + HRESULT result; + + REFERENCE_TIME minimum_time = 0; + REFERENCE_TIME buffer_duration; + + if(FAILED(CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, reinterpret_cast(&m_imm_device_enumerator)))) + goto error; + + if(FAILED(m_imm_device_enumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &m_imm_device))) + goto error; + + if(FAILED(m_imm_device->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, reinterpret_cast(&m_audio_client)))) + goto error; + + if(specs.channels == CHANNELS_INVALID) + specs.channels = CHANNELS_STEREO; + if(specs.format == FORMAT_INVALID) + specs.format = FORMAT_FLOAT32; + if(specs.rate == RATE_INVALID) + specs.rate = RATE_48000; + + switch(specs.format) + { + case FORMAT_U8: + case FORMAT_S16: + case FORMAT_S24: + case FORMAT_S32: + m_wave_format_extensible.SubFormat = KSDATAFORMAT_SUBTYPE_PCM; + break; + case FORMAT_FLOAT32: + m_wave_format_extensible.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT; + break; + default: + m_wave_format_extensible.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT; + specs.format = FORMAT_FLOAT32; + break; + } + + switch(specs.channels) + { + case CHANNELS_MONO: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_CENTER; + break; + case CHANNELS_STEREO: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT; + break; + case CHANNELS_STEREO_LFE: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY; + break; + case CHANNELS_SURROUND4: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; + break; + case CHANNELS_SURROUND5: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; + break; + case CHANNELS_SURROUND51: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT; + break; + case CHANNELS_SURROUND61: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT; + break; + case CHANNELS_SURROUND71: + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT | SPEAKER_FRONT_CENTER | SPEAKER_LOW_FREQUENCY | SPEAKER_BACK_LEFT | SPEAKER_BACK_RIGHT | SPEAKER_SIDE_LEFT | SPEAKER_SIDE_RIGHT; + break; + default: + specs.channels = CHANNELS_STEREO; + m_wave_format_extensible.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT; + break; + } + + m_wave_format_extensible.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE; + m_wave_format_extensible.Format.nChannels = specs.channels; + m_wave_format_extensible.Format.nSamplesPerSec = specs.rate; + m_wave_format_extensible.Format.nAvgBytesPerSec = specs.rate * AUD_DEVICE_SAMPLE_SIZE(specs); + m_wave_format_extensible.Format.nBlockAlign = AUD_DEVICE_SAMPLE_SIZE(specs); + m_wave_format_extensible.Format.wBitsPerSample = AUD_FORMAT_SIZE(specs.format) * 8; + m_wave_format_extensible.Format.cbSize = 22; + m_wave_format_extensible.Samples.wValidBitsPerSample = m_wave_format_extensible.Format.wBitsPerSample; + + result = m_audio_client->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, reinterpret_cast(&m_wave_format_extensible), reinterpret_cast(&closest_match_pointer)); + + if(result == S_FALSE) + { + if(closest_match_pointer->Format.wFormatTag != WAVE_FORMAT_EXTENSIBLE) + goto error; + + specs.channels = Channels(closest_match_pointer->Format.nChannels); + specs.rate = closest_match_pointer->Format.nSamplesPerSec; + + if(closest_match_pointer->SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) + { + if(closest_match_pointer->Format.wBitsPerSample == 32) + specs.format = FORMAT_FLOAT32; + else if(closest_match_pointer->Format.wBitsPerSample == 64) + specs.format = FORMAT_FLOAT64; + else + goto error; + } + else if(closest_match_pointer->SubFormat == KSDATAFORMAT_SUBTYPE_PCM) + { + switch(closest_match_pointer->Format.wBitsPerSample) + { + case 8: + specs.format = FORMAT_U8; + break; + case 16: + specs.format = FORMAT_S16; + break; + case 24: + specs.format = FORMAT_S24; + break; + case 32: + specs.format = FORMAT_S32; + break; + default: + goto error; + break; + } + } + else + goto error; + + m_wave_format_extensible = *closest_match_pointer; + + if(closest_match_pointer != &wave_format_extensible_closest_match) + { + CoTaskMemFree(closest_match_pointer); + closest_match_pointer = &wave_format_extensible_closest_match; + } + } + else if(FAILED(result)) + goto error; + + if(FAILED(m_audio_client->GetDevicePeriod(nullptr, &minimum_time))) + goto error; + + buffer_duration = REFERENCE_TIME(buffersize) * REFERENCE_TIME(10000000) / REFERENCE_TIME(specs.rate); + + if(minimum_time > buffer_duration) + buffer_duration = minimum_time; + + m_specs = specs; + + if(FAILED(m_audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, buffer_duration, 0, reinterpret_cast(&m_wave_format_extensible), nullptr))) + goto error; + + create(); + + return; + + error: + if(closest_match_pointer != &wave_format_extensible_closest_match) + CoTaskMemFree(closest_match_pointer); + SafeRelease(&m_imm_device); + SafeRelease(&m_imm_device_enumerator); + SafeRelease(&m_audio_client); + AUD_THROW(DeviceException, "The audio device couldn't be opened with WASAPI."); +} + +WASAPIDevice::~WASAPIDevice() +{ + lock(); + + stopAll(); + + unlock(); + + if(m_thread.joinable()) + m_thread.join(); + + SafeRelease(&m_audio_client); + SafeRelease(&m_imm_device); + SafeRelease(&m_imm_device_enumerator); + + destroy(); +} + +class WASAPIDeviceFactory : public IDeviceFactory +{ +private: + DeviceSpecs m_specs; + int m_buffersize; + +public: + WASAPIDeviceFactory() : + m_buffersize(AUD_DEFAULT_BUFFER_SIZE) + { + m_specs.format = FORMAT_S16; + m_specs.channels = CHANNELS_STEREO; + m_specs.rate = RATE_48000; + } + + virtual std::shared_ptr openDevice() + { + return std::shared_ptr(new WASAPIDevice(m_specs, m_buffersize)); + } + + virtual int getPriority() + { + return 1 << 15; + } + + virtual void setSpecs(DeviceSpecs specs) + { + m_specs = specs; + } + + virtual void setBufferSize(int buffersize) + { + m_buffersize = buffersize; + } + + virtual void setName(std::string name) + { + } +}; + +void WASAPIDevice::registerPlugin() +{ + DeviceManager::registerDevice("WASAPI", std::shared_ptr(new WASAPIDeviceFactory)); +} + +#ifdef WASAPI_PLUGIN +extern "C" AUD_PLUGIN_API void registerPlugin() +{ + WASAPIDevice::registerPlugin(); +} + +extern "C" AUD_PLUGIN_API const char* getName() +{ + return "WASAPI"; +} +#endif + +AUD_NAMESPACE_END diff --git a/extern/audaspace/plugins/wasapi/WASAPIDevice.h b/extern/audaspace/plugins/wasapi/WASAPIDevice.h new file mode 100644 index 00000000000..b7b520e802c --- /dev/null +++ b/extern/audaspace/plugins/wasapi/WASAPIDevice.h @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright 2009-2016 Jörg Müller + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ + +#pragma once + +#ifdef WASAPI_PLUGIN +#define AUD_BUILD_PLUGIN +#endif + +/** + * @file WASAPIDevice.h + * @ingroup plugin + * The WASAPIDevice class. + */ + +#include "devices/SoftwareDevice.h" + +#include + +#include +#include +#include +#include + +AUD_NAMESPACE_BEGIN + +/** + * This device plays back through WASAPI, the Windows audio API. + */ +class AUD_PLUGIN_API WASAPIDevice : public SoftwareDevice +{ +private: + /** + * Whether there is currently playback. + */ + bool m_playing; + + IMMDeviceEnumerator* m_imm_device_enumerator; + IMMDevice* m_imm_device; + IAudioClient* m_audio_client; + WAVEFORMATEXTENSIBLE m_wave_format_extensible; + + /** + * The streaming thread. + */ + std::thread m_thread; + + /** + * Starts the streaming thread. + */ + AUD_LOCAL void start(); + + /** + * Streaming thread main function. + */ + AUD_LOCAL void updateStream(); + + // delete copy constructor and operator= + WASAPIDevice(const WASAPIDevice&) = delete; + WASAPIDevice& operator=(const WASAPIDevice&) = delete; + +protected: + virtual void playing(bool playing); + +public: + /** + * Opens the WASAPI audio device for playback. + * \param specs The wanted audio specification. + * \param buffersize The size of the internal buffer. + * \note The specification really used for opening the device may differ. + * \exception Exception Thrown if the audio device cannot be opened. + */ + WASAPIDevice(DeviceSpecs specs, int buffersize = AUD_DEFAULT_BUFFER_SIZE); + + /** + * Closes the WASAPI audio device. + */ + virtual ~WASAPIDevice(); + + /** + * Registers this plugin. + */ + static void registerPlugin(); +}; + +AUD_NAMESPACE_END -- cgit v1.2.3