Add CoInitializeEx() to mmdevice finder and refactor code

This commit is contained in:
Jonas Kvinge 2020-10-15 16:06:07 +02:00
parent 62702e4b3d
commit aeb0d05017
1 changed files with 44 additions and 63 deletions

View File

@ -36,26 +36,8 @@ MMDeviceFinder::MMDeviceFinder() : DeviceFinder("mmdevice", { "wasapisink" }) {}
QList<DeviceFinder::Device> MMDeviceFinder::ListDevices() { QList<DeviceFinder::Device> MMDeviceFinder::ListDevices() {
HRESULT hr = S_OK; HRESULT hr_coinit = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (hr_coinit != S_OK && hr_coinit != S_FALSE) {
IMMDeviceEnumerator *enumerator = nullptr;
hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&enumerator);
if (FAILED(hr)) {
return QList<Device>();
}
IMMDeviceCollection *collection = nullptr;
hr = enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &collection);
if (FAILED(hr)) {
enumerator->Release();
return QList<Device>();
}
UINT count;
hr = collection->GetCount(&count);
if (FAILED(hr)) {
collection->Release();
enumerator->Release();
return QList<Device>(); return QList<Device>();
} }
@ -65,51 +47,50 @@ QList<DeviceFinder::Device> MMDeviceFinder::ListDevices() {
default_device.iconname = GuessIconName(default_device.description); default_device.iconname = GuessIconName(default_device.description);
devices.append(default_device); devices.append(default_device);
IMMDeviceEnumerator *enumerator = nullptr;
HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&enumerator);
if (hr == S_OK) {
IMMDeviceCollection *collection = nullptr;
hr = enumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &collection);
if (hr == S_OK) {
UINT count;
hr = collection->GetCount(&count);
if (hr == S_OK) {
for (ULONG i = 0 ; i < count ; i++) { for (ULONG i = 0 ; i < count ; i++) {
IMMDevice *endpoint = nullptr; IMMDevice *endpoint = nullptr;
hr = collection->Item(i, &endpoint); hr = collection->Item(i, &endpoint);
if (FAILED(hr)) { return devices; } if (hr == S_OK) {
LPWSTR pwszid = nullptr; LPWSTR pwszid = nullptr;
hr = endpoint->GetId(&pwszid); hr = endpoint->GetId(&pwszid);
if (FAILED(hr)) { if (hr == S_OK) {
endpoint->Release();
continue;
}
IPropertyStore *props = nullptr; IPropertyStore *props = nullptr;
hr = endpoint->OpenPropertyStore(STGM_READ, &props); hr = endpoint->OpenPropertyStore(STGM_READ, &props);
if (FAILED(hr)) { if (hr == S_OK) {
CoTaskMemFree(pwszid);
endpoint->Release();
continue;
}
PROPVARIANT var_name; PROPVARIANT var_name;
PropVariantInit(&var_name); PropVariantInit(&var_name);
hr = props->GetValue(PKEY_Device_FriendlyName, &var_name); hr = props->GetValue(PKEY_Device_FriendlyName, &var_name);
if (FAILED(hr)) { if (hr == S_OK) {
props->Release();
CoTaskMemFree(pwszid);
endpoint->Release();
continue;
}
Device device; Device device;
device.description = QString::fromWCharArray(var_name.pwszVal); device.description = QString::fromWCharArray(var_name.pwszVal);
device.iconname = GuessIconName(device.description); device.iconname = GuessIconName(device.description);
device.value = QString::fromStdWString(pwszid); device.value = QString::fromStdWString(pwszid);
devices.append(device); devices.append(device);
PropVariantClear(&var_name); PropVariantClear(&var_name);
}
props->Release(); props->Release();
}
CoTaskMemFree(pwszid); CoTaskMemFree(pwszid);
}
endpoint->Release(); endpoint->Release();
}
}
} }
collection->Release(); collection->Release();
}
enumerator->Release(); enumerator->Release();
}
if (hr_coinit == S_OK || hr_coinit == S_FALSE) CoUninitialize();
return devices; return devices;