diff options
Diffstat (limited to 'drivers/wasapi')
-rw-r--r-- | drivers/wasapi/audio_driver_wasapi.cpp | 105 | ||||
-rw-r--r-- | drivers/wasapi/audio_driver_wasapi.h | 52 |
2 files changed, 53 insertions, 104 deletions
diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index fa78771993..cd1c08b717 100644 --- a/drivers/wasapi/audio_driver_wasapi.cpp +++ b/drivers/wasapi/audio_driver_wasapi.cpp @@ -54,10 +54,10 @@ const IID IID_IAudioClient = __uuidof(IAudioClient); const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient); const IID IID_IAudioCaptureClient = __uuidof(IAudioCaptureClient); -#define SAFE_RELEASE(memory) \ - if ((memory) != NULL) { \ - (memory)->Release(); \ - (memory) = NULL; \ +#define SAFE_RELEASE(memory) \ + if ((memory) != nullptr) { \ + (memory)->Release(); \ + (memory) = nullptr; \ } #define REFTIMES_PER_SEC 10000000 @@ -69,17 +69,15 @@ static bool default_render_device_changed = false; static bool default_capture_device_changed = false; class CMMNotificationClient : public IMMNotificationClient { - LONG _cRef; - IMMDeviceEnumerator *_pEnumerator; + LONG _cRef = 1; + IMMDeviceEnumerator *_pEnumerator = nullptr; public: - CMMNotificationClient() : - _cRef(1), - _pEnumerator(NULL) {} + CMMNotificationClient() {} virtual ~CMMNotificationClient() { - if ((_pEnumerator) != NULL) { + if ((_pEnumerator) != nullptr) { (_pEnumerator)->Release(); - (_pEnumerator) = NULL; + (_pEnumerator) = nullptr; } } @@ -103,7 +101,7 @@ public: AddRef(); *ppvInterface = (IMMNotificationClient *)this; } else { - *ppvInterface = NULL; + *ppvInterface = nullptr; return E_NOINTERFACE; } return S_OK; @@ -141,25 +139,24 @@ public: static CMMNotificationClient notif_client; Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_capture, bool reinit) { - WAVEFORMATEX *pwfex; - IMMDeviceEnumerator *enumerator = NULL; - IMMDevice *device = NULL; + IMMDeviceEnumerator *enumerator = nullptr; + IMMDevice *device = nullptr; - CoInitialize(NULL); + CoInitialize(nullptr); - HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator); + HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); if (p_device->device_name == "Default") { hr = enumerator->GetDefaultAudioEndpoint(p_capture ? eCapture : eRender, eConsole, &device); } else { - IMMDeviceCollection *devices = NULL; + IMMDeviceCollection *devices = nullptr; hr = enumerator->EnumAudioEndpoints(p_capture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices); ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); - LPWSTR strId = NULL; + LPWSTR strId = nullptr; bool found = false; UINT count = 0; @@ -167,12 +164,12 @@ Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_c ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN); for (ULONG i = 0; i < count && !found; i++) { - IMMDevice *tmp_device = NULL; + IMMDevice *tmp_device = nullptr; hr = devices->Item(i, &tmp_device); ERR_BREAK(hr != S_OK); - IPropertyStore *props = NULL; + IPropertyStore *props = nullptr; hr = tmp_device->OpenPropertyStore(STGM_READ, &props); ERR_BREAK(hr != S_OK); @@ -202,7 +199,7 @@ Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_c CoTaskMemFree(strId); } - if (device == NULL) { + if (device == nullptr) { hr = enumerator->GetDefaultAudioEndpoint(p_capture ? eCapture : eRender, eConsole, &device); } } @@ -224,7 +221,7 @@ Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_c ERR_PRINT("WASAPI: RegisterEndpointNotificationCallback error"); } - hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)&p_device->audio_client); + hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, (void **)&p_device->audio_client); SAFE_RELEASE(device) if (reinit) { @@ -246,7 +243,7 @@ Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_c print_verbose("WASAPI: wBitsPerSample = " + itos(pwfex->wBitsPerSample)); print_verbose("WASAPI: cbSize = " + itos(pwfex->cbSize)); - WAVEFORMATEX *closest = NULL; + WAVEFORMATEX *closest = nullptr; hr = p_device->audio_client->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, pwfex, &closest); if (hr == S_FALSE) { WARN_PRINT("WASAPI: Mix format is not supported by the Device"); @@ -295,7 +292,7 @@ Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_c pwfex->nAvgBytesPerSec = pwfex->nSamplesPerSec * pwfex->nChannels * (pwfex->wBitsPerSample / 8); } - hr = p_device->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, p_capture ? REFTIMES_PER_SEC : 0, 0, pwfex, NULL); + hr = p_device->audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED, streamflags, p_capture ? REFTIMES_PER_SEC : 0, 0, pwfex, nullptr); ERR_FAIL_COND_V_MSG(hr != S_OK, ERR_CANT_OPEN, "WASAPI: Initialize failed with error 0x" + String::num_uint64(hr, 16) + "."); if (p_capture) { @@ -313,7 +310,6 @@ Error AudioDriverWASAPI::audio_device_init(AudioDeviceWASAPI *p_device, bool p_c } Error AudioDriverWASAPI::init_render_device(bool reinit) { - Error err = audio_device_init(&audio_output, false, reinit); if (err != OK) return err; @@ -352,7 +348,6 @@ Error AudioDriverWASAPI::init_render_device(bool reinit) { } Error AudioDriverWASAPI::init_capture_device(bool reinit) { - Error err = audio_device_init(&audio_input, true, reinit); if (err != OK) return err; @@ -368,7 +363,6 @@ Error AudioDriverWASAPI::init_capture_device(bool reinit) { } Error AudioDriverWASAPI::audio_device_finish(AudioDeviceWASAPI *p_device) { - if (p_device->active) { if (p_device->audio_client) { p_device->audio_client->Stop(); @@ -385,18 +379,15 @@ Error AudioDriverWASAPI::audio_device_finish(AudioDeviceWASAPI *p_device) { } Error AudioDriverWASAPI::finish_render_device() { - return audio_device_finish(&audio_output); } Error AudioDriverWASAPI::finish_capture_device() { - return audio_device_finish(&audio_input); } Error AudioDriverWASAPI::init() { - - mix_rate = GLOBAL_DEF_RST("audio/mix_rate", DEFAULT_MIX_RATE); + mix_rate = GLOBAL_GET("audio/mix_rate"); Error err = init_render_device(); if (err != OK) { @@ -412,26 +403,23 @@ Error AudioDriverWASAPI::init() { } int AudioDriverWASAPI::get_mix_rate() const { - return mix_rate; } AudioDriver::SpeakerMode AudioDriverWASAPI::get_speaker_mode() const { - return get_speaker_mode_by_total_channels(channels); } Array AudioDriverWASAPI::audio_device_get_list(bool p_capture) { - Array list; - IMMDeviceCollection *devices = NULL; - IMMDeviceEnumerator *enumerator = NULL; + IMMDeviceCollection *devices = nullptr; + IMMDeviceEnumerator *enumerator = nullptr; list.push_back(String("Default")); - CoInitialize(NULL); + CoInitialize(nullptr); - HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator); + HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator); ERR_FAIL_COND_V(hr != S_OK, Array()); hr = enumerator->EnumAudioEndpoints(p_capture ? eCapture : eRender, DEVICE_STATE_ACTIVE, &devices); @@ -442,12 +430,12 @@ Array AudioDriverWASAPI::audio_device_get_list(bool p_capture) { ERR_FAIL_COND_V(hr != S_OK, Array()); for (ULONG i = 0; i < count; i++) { - IMMDevice *device = NULL; + IMMDevice *device = nullptr; hr = devices->Item(i, &device); ERR_BREAK(hr != S_OK); - IPropertyStore *props = NULL; + IPropertyStore *props = nullptr; hr = device->OpenPropertyStore(STGM_READ, &props); ERR_BREAK(hr != S_OK); @@ -470,12 +458,10 @@ Array AudioDriverWASAPI::audio_device_get_list(bool p_capture) { } Array AudioDriverWASAPI::get_device_list() { - return audio_device_get_list(false); } String AudioDriverWASAPI::get_device() { - lock(); String name = audio_output.device_name; unlock(); @@ -484,7 +470,6 @@ String AudioDriverWASAPI::get_device() { } void AudioDriverWASAPI::set_device(String device) { - lock(); audio_output.new_device = device; unlock(); @@ -552,13 +537,11 @@ void AudioDriverWASAPI::write_sample(WORD format_tag, int bits_per_sample, BYTE } void AudioDriverWASAPI::thread_func(void *p_udata) { - AudioDriverWASAPI *ad = (AudioDriverWASAPI *)p_udata; uint32_t avail_frames = 0; uint32_t write_ofs = 0; while (!ad->exit_thread) { - uint32_t read_frames = 0; uint32_t written_frames = 0; @@ -585,19 +568,16 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { ad->start_counting_ticks(); if (avail_frames > 0 && ad->audio_output.audio_client) { - UINT32 cur_frames; bool invalidated = false; HRESULT hr = ad->audio_output.audio_client->GetCurrentPadding(&cur_frames); if (hr == S_OK) { - // Check how much frames are available on the WASAPI buffer UINT32 write_frames = MIN(ad->buffer_frames - cur_frames, avail_frames); if (write_frames > 0) { - BYTE *buffer = NULL; + BYTE *buffer = nullptr; hr = ad->audio_output.render_client->GetBuffer(write_frames, &buffer); if (hr == S_OK) { - // We're using WASAPI Shared Mode so we must convert the buffer if (ad->channels == ad->audio_output.channels) { for (unsigned int i = 0; i < write_frames * ad->channels; i++) { @@ -693,7 +673,7 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { HRESULT hr = ad->audio_input.capture_client->GetNextPacketSize(&packet_length); if (hr == S_OK) { while (packet_length != 0) { - hr = ad->audio_input.capture_client->GetBuffer(&data, &num_frames_available, &flags, NULL, NULL); + hr = ad->audio_input.capture_client->GetBuffer(&data, &num_frames_available, &flags, nullptr, nullptr); ERR_BREAK(hr != S_OK); // fixme: Only works for floating point atm @@ -768,7 +748,6 @@ void AudioDriverWASAPI::thread_func(void *p_udata) { } void AudioDriverWASAPI::start() { - if (audio_output.audio_client) { HRESULT hr = audio_output.audio_client->Start(); if (hr != S_OK) { @@ -780,23 +759,20 @@ void AudioDriverWASAPI::start() { } void AudioDriverWASAPI::lock() { - mutex.lock(); } void AudioDriverWASAPI::unlock() { - mutex.unlock(); } void AudioDriverWASAPI::finish() { - if (thread) { exit_thread = true; Thread::wait_to_finish(thread); memdelete(thread); - thread = NULL; + thread = nullptr; } finish_capture_device(); @@ -804,7 +780,6 @@ void AudioDriverWASAPI::finish() { } Error AudioDriverWASAPI::capture_start() { - Error err = init_capture_device(); if (err != OK) { ERR_PRINT("WASAPI: init_capture_device error"); @@ -821,7 +796,6 @@ Error AudioDriverWASAPI::capture_start() { } Error AudioDriverWASAPI::capture_stop() { - if (audio_input.active) { audio_input.audio_client->Stop(); audio_input.active = false; @@ -833,19 +807,16 @@ Error AudioDriverWASAPI::capture_stop() { } void AudioDriverWASAPI::capture_set_device(const String &p_name) { - lock(); audio_input.new_device = p_name; unlock(); } Array AudioDriverWASAPI::capture_get_device_list() { - return audio_device_get_list(true); } String AudioDriverWASAPI::capture_get_device() { - lock(); String name = audio_input.device_name; unlock(); @@ -854,17 +825,7 @@ String AudioDriverWASAPI::capture_get_device() { } AudioDriverWASAPI::AudioDriverWASAPI() { - - thread = NULL; - samples_in.clear(); - - channels = 0; - mix_rate = 0; - buffer_frames = 0; - - thread_exited = false; - exit_thread = false; } -#endif +#endif // WASAPI_ENABLED diff --git a/drivers/wasapi/audio_driver_wasapi.h b/drivers/wasapi/audio_driver_wasapi.h index 3ea61c6010..41ff7c9895 100644 --- a/drivers/wasapi/audio_driver_wasapi.h +++ b/drivers/wasapi/audio_driver_wasapi.h @@ -42,50 +42,38 @@ #include <windows.h> class AudioDriverWASAPI : public AudioDriver { - class AudioDeviceWASAPI { public: - IAudioClient *audio_client; - IAudioRenderClient *render_client; - IAudioCaptureClient *capture_client; - bool active; - - WORD format_tag; - WORD bits_per_sample; - unsigned int channels; - unsigned int frame_size; - - String device_name; - String new_device; - - AudioDeviceWASAPI() : - audio_client(NULL), - render_client(NULL), - capture_client(NULL), - active(false), - format_tag(0), - bits_per_sample(0), - channels(0), - frame_size(0), - device_name("Default"), - new_device("Default") { - } + IAudioClient *audio_client = nullptr; + IAudioRenderClient *render_client = nullptr; + IAudioCaptureClient *capture_client = nullptr; + bool active = false; + + WORD format_tag = 0; + WORD bits_per_sample = 0; + unsigned int channels = 0; + unsigned int frame_size = 0; + + String device_name = "Default"; + String new_device = "Default"; + + AudioDeviceWASAPI() {} }; AudioDeviceWASAPI audio_input; AudioDeviceWASAPI audio_output; Mutex mutex; - Thread *thread; + Thread *thread = nullptr; Vector<int32_t> samples_in; - unsigned int channels; - int mix_rate; - int buffer_frames; + unsigned int channels = 0; + int mix_rate = 0; + int buffer_frames = 0; - bool thread_exited; - mutable bool exit_thread; + bool thread_exited = false; + mutable bool exit_thread = false; static _FORCE_INLINE_ void write_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i, int32_t sample); static _FORCE_INLINE_ int32_t read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i); |