diff options
Diffstat (limited to 'drivers/wasapi/audio_driver_wasapi.cpp')
-rw-r--r-- | drivers/wasapi/audio_driver_wasapi.cpp | 120 |
1 files changed, 36 insertions, 84 deletions
diff --git a/drivers/wasapi/audio_driver_wasapi.cpp b/drivers/wasapi/audio_driver_wasapi.cpp index 9a05284aea..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; @@ -327,7 +323,7 @@ Error AudioDriverWASAPI::init_render_device(bool reinit) { break; default: - WARN_PRINTS("WASAPI: Unsupported number of channels: " + itos(audio_output.channels)); + WARN_PRINT("WASAPI: Unsupported number of channels: " + itos(audio_output.channels)); channels = 2; break; } @@ -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) { @@ -406,33 +397,29 @@ Error AudioDriverWASAPI::init() { exit_thread = false; thread_exited = false; - mutex = Mutex::create(true); thread = Thread::create(thread_func, this); return OK; } 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); @@ -443,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); @@ -471,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(); @@ -485,7 +470,6 @@ String AudioDriverWASAPI::get_device() { } void AudioDriverWASAPI::set_device(String device) { - lock(); audio_output.new_device = device; unlock(); @@ -553,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; @@ -586,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++) { @@ -694,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 @@ -769,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) { @@ -781,38 +759,27 @@ void AudioDriverWASAPI::start() { } void AudioDriverWASAPI::lock() { - - if (mutex) - mutex->lock(); + mutex.lock(); } void AudioDriverWASAPI::unlock() { - - if (mutex) - mutex->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(); finish_render_device(); - - if (mutex) { - memdelete(mutex); - mutex = NULL; - } } Error AudioDriverWASAPI::capture_start() { - Error err = init_capture_device(); if (err != OK) { ERR_PRINT("WASAPI: init_capture_device error"); @@ -829,7 +796,6 @@ Error AudioDriverWASAPI::capture_start() { } Error AudioDriverWASAPI::capture_stop() { - if (audio_input.active) { audio_input.audio_client->Stop(); audio_input.active = false; @@ -841,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(); @@ -862,18 +825,7 @@ String AudioDriverWASAPI::capture_get_device() { } AudioDriverWASAPI::AudioDriverWASAPI() { - - mutex = NULL; - thread = NULL; - samples_in.clear(); - - channels = 0; - mix_rate = 0; - buffer_frames = 0; - - thread_exited = false; - exit_thread = false; } -#endif +#endif // WASAPI_ENABLED |