Merge pull request #17742 from marcelofg55/audio_device_list

Added new audio device functions to set/get the audio device
This commit is contained in:
Juan Linietsky 2018-04-07 16:40:47 -03:00 committed by GitHub
commit 1570a72eee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 780 additions and 196 deletions

View file

@ -35,6 +35,8 @@
#include "os/os.h"
#include "project_settings.h"
#include <functiondiscoverykeys.h>
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
const IID IID_IAudioClient = __uuidof(IAudioClient);
@ -121,7 +123,61 @@ Error AudioDriverWASAPI::init_device(bool reinit) {
HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
if (device_name == "Default") {
hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
} else {
IMMDeviceCollection *devices = NULL;
hr = enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &devices);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
LPWSTR strId = NULL;
bool found = false;
UINT count = 0;
hr = devices->GetCount(&count);
ERR_FAIL_COND_V(hr != S_OK, ERR_CANT_OPEN);
for (ULONG i = 0; i < count && !found; i++) {
IMMDevice *device = NULL;
hr = devices->Item(i, &device);
ERR_BREAK(hr != S_OK);
IPropertyStore *props = NULL;
hr = device->OpenPropertyStore(STGM_READ, &props);
ERR_BREAK(hr != S_OK);
PROPVARIANT propvar;
PropVariantInit(&propvar);
hr = props->GetValue(PKEY_Device_FriendlyName, &propvar);
ERR_BREAK(hr != S_OK);
if (device_name == String(propvar.pwszVal)) {
hr = device->GetId(&strId);
ERR_BREAK(hr != S_OK);
found = true;
}
PropVariantClear(&propvar);
props->Release();
device->Release();
}
if (found) {
hr = enumerator->GetDevice(strId, &device);
}
if (strId) {
CoTaskMemFree(strId);
}
if (device == NULL) {
hr = enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &device);
}
}
if (reinit) {
// In case we're trying to re-initialize the device prevent throwing this error on the console,
// otherwise if there is currently no device available this will spam the console.
@ -294,6 +350,64 @@ AudioDriver::SpeakerMode AudioDriverWASAPI::get_speaker_mode() const {
return get_speaker_mode_by_total_channels(channels);
}
Array AudioDriverWASAPI::get_device_list() {
Array list;
IMMDeviceCollection *devices = NULL;
IMMDeviceEnumerator *enumerator = NULL;
list.push_back(String("Default"));
CoInitialize(NULL);
HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void **)&enumerator);
ERR_FAIL_COND_V(hr != S_OK, Array());
hr = enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &devices);
ERR_FAIL_COND_V(hr != S_OK, Array());
UINT count = 0;
hr = devices->GetCount(&count);
ERR_FAIL_COND_V(hr != S_OK, Array());
for (ULONG i = 0; i < count; i++) {
IMMDevice *device = NULL;
hr = devices->Item(i, &device);
ERR_BREAK(hr != S_OK);
IPropertyStore *props = NULL;
hr = device->OpenPropertyStore(STGM_READ, &props);
ERR_BREAK(hr != S_OK);
PROPVARIANT propvar;
PropVariantInit(&propvar);
hr = props->GetValue(PKEY_Device_FriendlyName, &propvar);
ERR_BREAK(hr != S_OK);
list.push_back(String(propvar.pwszVal));
PropVariantClear(&propvar);
props->Release();
device->Release();
}
devices->Release();
enumerator->Release();
return list;
}
String AudioDriverWASAPI::get_device() {
return device_name;
}
void AudioDriverWASAPI::set_device(String device) {
new_device = device;
}
void AudioDriverWASAPI::write_sample(AudioDriverWASAPI *ad, BYTE *buffer, int i, int32_t sample) {
if (ad->format_tag == WAVE_FORMAT_PCM) {
switch (ad->bits_per_sample) {
@ -409,7 +523,8 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
}
}
if (default_device_changed) {
// If we're using the Default device and it changed finish it so we'll re-init the device
if (ad->device_name == "Default" && default_device_changed) {
Error err = ad->finish_device();
if (err != OK) {
ERR_PRINT("WASAPI: finish_device error");
@ -418,6 +533,15 @@ void AudioDriverWASAPI::thread_func(void *p_udata) {
default_device_changed = false;
}
// User selected a new device, finish the current one so we'll init the new device
if (ad->device_name != ad->new_device) {
ad->device_name = ad->new_device;
Error err = ad->finish_device();
if (err != OK) {
ERR_PRINT("WASAPI: finish_device error");
}
}
if (!ad->audio_client) {
Error err = ad->init_device(true);
if (err == OK) {
@ -492,6 +616,9 @@ AudioDriverWASAPI::AudioDriverWASAPI() {
thread_exited = false;
exit_thread = false;
active = false;
device_name = "Default";
new_device = "Default";
}
#endif