enum-wasapi.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "enum-wasapi.hpp"
  2. #include <util/base.h>
  3. #include <util/platform.h>
  4. #include <util/windows/HRError.hpp>
  5. #include <util/windows/ComPtr.hpp>
  6. #include <util/windows/CoTaskMemPtr.hpp>
  7. using namespace std;
  8. string GetDeviceName(IMMDevice *device)
  9. {
  10. string device_name;
  11. ComPtr<IPropertyStore> store;
  12. HRESULT res;
  13. if (SUCCEEDED(device->OpenPropertyStore(STGM_READ, store.Assign()))) {
  14. PROPVARIANT nameVar;
  15. PropVariantInit(&nameVar);
  16. res = store->GetValue(PKEY_Device_FriendlyName, &nameVar);
  17. if (SUCCEEDED(res)) {
  18. size_t len = wcslen(nameVar.pwszVal);
  19. size_t size;
  20. size = os_wcs_to_utf8(nameVar.pwszVal, len,
  21. nullptr, 0) + 1;
  22. device_name.resize(size);
  23. os_wcs_to_utf8(nameVar.pwszVal, len,
  24. &device_name[0], size);
  25. }
  26. }
  27. return device_name;
  28. }
  29. void GetWASAPIAudioDevices_(vector<AudioDeviceInfo> &devices, bool input)
  30. {
  31. ComPtr<IMMDeviceEnumerator> enumerator;
  32. ComPtr<IMMDeviceCollection> collection;
  33. UINT count;
  34. HRESULT res;
  35. res = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL,
  36. __uuidof(IMMDeviceEnumerator),
  37. (void**)enumerator.Assign());
  38. if (FAILED(res))
  39. throw HRError("Failed to create enumerator", res);
  40. res = enumerator->EnumAudioEndpoints(input ? eCapture : eRender,
  41. DEVICE_STATE_ACTIVE, collection.Assign());
  42. if (FAILED(res))
  43. throw HRError("Failed to enumerate devices", res);
  44. res = collection->GetCount(&count);
  45. if (FAILED(res))
  46. throw HRError("Failed to get device count", res);
  47. for (UINT i = 0; i < count; i++) {
  48. ComPtr<IMMDevice> device;
  49. CoTaskMemPtr<WCHAR> w_id;
  50. AudioDeviceInfo info;
  51. size_t len, size;
  52. res = collection->Item(i, device.Assign());
  53. if (FAILED(res))
  54. continue;
  55. res = device->GetId(&w_id);
  56. if (FAILED(res) || !w_id || !*w_id)
  57. continue;
  58. info.name = GetDeviceName(device);
  59. len = wcslen(w_id);
  60. size = os_wcs_to_utf8(w_id, len, nullptr, 0) + 1;
  61. info.id.resize(size);
  62. os_wcs_to_utf8(w_id, len, &info.id[0], size);
  63. devices.push_back(info);
  64. }
  65. }
  66. void GetWASAPIAudioDevices(vector<AudioDeviceInfo> &devices, bool input)
  67. {
  68. devices.clear();
  69. try {
  70. GetWASAPIAudioDevices_(devices, input);
  71. } catch (HRError error) {
  72. blog(LOG_WARNING, "[GetWASAPIAudioDevices] %s: %lX",
  73. error.str, error.hr);
  74. }
  75. }