device_audios.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "device_audios.h"
  2. #include "error_define.h"
  3. #include "log_helper.h"
  4. #include "utils_string.h"
  5. #include "headers_mmdevice.h"
  6. #include <memory>
  7. namespace am {
  8. int device_audios::get_input_devices(std::list<DEVICE_AUDIOS>& devices)
  9. {
  10. return get_devices(true, devices);
  11. }
  12. int device_audios::get_output_devices(std::list<DEVICE_AUDIOS>& devices)
  13. {
  14. return get_devices(false, devices);
  15. }
  16. int device_audios::get_default_input_device(std::string& id, std::string& name)
  17. {
  18. return get_default(true, id, name);
  19. }
  20. int device_audios::get_default_ouput_device(std::string& id, std::string& name)
  21. {
  22. return get_default(false, id, name);
  23. }
  24. int device_audios::get_devices(bool input, std::list<DEVICE_AUDIOS>& devices)
  25. {
  26. com_initialize com_obj;
  27. devices.clear();
  28. Microsoft::WRL::ComPtr<IMMDeviceEnumerator> device_enumerator = nullptr;
  29. Microsoft::WRL::ComPtr<IMMDevice> device = nullptr;
  30. Microsoft::WRL::ComPtr<IMMDeviceCollection> collection = nullptr;
  31. LPWSTR current_device_id = NULL;
  32. int ret = AE_NO;
  33. do {
  34. std::shared_ptr<void> raii_ptr(nullptr, [&](void*) {
  35. collection = nullptr;
  36. device = nullptr;
  37. device_enumerator = nullptr;
  38. });
  39. HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
  40. NULL,
  41. CLSCTX_ALL,
  42. __uuidof(IMMDeviceEnumerator),
  43. (void**) device_enumerator.GetAddressOf());
  44. if (FAILED(hr)) {
  45. ret = AE_CO_CREATE_FAILED;
  46. break;
  47. }
  48. hr = device_enumerator->GetDefaultAudioEndpoint(input == true ? eCapture : eRender,
  49. eConsole,
  50. device.GetAddressOf());
  51. if (FAILED(hr)) {
  52. ret = AE_CO_GETENDPOINT_FAILED;
  53. break;
  54. }
  55. hr = device_enumerator->EnumAudioEndpoints(input == true ? eCapture : eRender,
  56. DEVICE_STATE_ACTIVE,
  57. collection.GetAddressOf());
  58. if (FAILED(hr)) {
  59. ret = AE_CO_ENUMENDPOINT_FAILED;
  60. break;
  61. }
  62. UINT count;
  63. hr = collection->GetCount(&count);
  64. if (FAILED(hr)) {
  65. ret = AE_CO_GET_ENDPOINT_COUNT_FAILED;
  66. break;
  67. }
  68. hr = device->GetId(&current_device_id);
  69. if (FAILED(hr)) {
  70. ret = AE_CO_GET_ENDPOINT_ID_FAILED;
  71. break;
  72. }
  73. std::string default_id = utils_string::unicode_utf8(current_device_id);
  74. CoTaskMemFree(current_device_id);
  75. for (int i = 0; i < count; ++i) {
  76. IMMDevice* pEndpointDevice = NULL;
  77. IDeviceTopology* deviceTopology = NULL;
  78. IConnector* connector = NULL;
  79. IPropertyStore* pPropertyStore = NULL;
  80. PROPVARIANT pv;
  81. PropVariantInit(&pv);
  82. LPWSTR device_name = NULL;
  83. LPWSTR device_id = NULL;
  84. std::string str_name, str_id, str_friendly;
  85. hr = collection->Item(i, &pEndpointDevice);
  86. if (FAILED(hr))
  87. continue;
  88. /*hr = pEndpointDevice->Activate(__uuidof(IDeviceTopology), CLSCTX_INPROC_SERVER, NULL,
  89. (LPVOID*)&deviceTopology);
  90. hr = deviceTopology->GetConnector(0, &connector);
  91. hr = connector->GetConnectorIdConnectedTo(&device_name);
  92. str_name = utils_string::unicode_utf8(device_name);*/
  93. hr = pEndpointDevice->GetId(&device_id);
  94. if (FAILED(hr))
  95. continue;
  96. str_id = utils_string::unicode_utf8(device_id);
  97. hr = pEndpointDevice->OpenPropertyStore(STGM_READ, &pPropertyStore);
  98. if (FAILED(hr))
  99. continue;
  100. hr = pPropertyStore->GetValue(PKEY_Device_FriendlyName, &pv);
  101. if (FAILED(hr)) {
  102. PropVariantClear(&pv);
  103. continue;
  104. }
  105. if (pv.vt == VT_LPWSTR) {
  106. str_friendly = utils_string::unicode_utf8(pv.pwszVal);
  107. } else if (pv.vt == VT_LPSTR) {
  108. str_friendly = utils_string::ascii_utf8(pv.pszVal);
  109. }
  110. devices.push_back({str_id, str_friendly, str_id.compare(default_id) == 0});
  111. PropVariantClear(&pv);
  112. CoTaskMemFree(device_name);
  113. CoTaskMemFree(device_id);
  114. }
  115. } while (0);
  116. if (ret == AE_NO && devices.size()) {
  117. devices.push_front({utils_string::ascii_utf8(DEFAULT_AUDIO_INOUTPUT_ID),
  118. utils_string::ascii_utf8(DEFAULT_AUDIO_INOUTPUT_NAME),
  119. true});
  120. }
  121. if (ret != AE_NO)
  122. al_error("get_devices failed(%lu): %s", GetLastError(), err2str(ret));
  123. return ret;
  124. }
  125. int device_audios::get_default(bool input, std::string& id, std::string& name)
  126. {
  127. com_initialize com_obj;
  128. Microsoft::WRL::ComPtr<IMMDeviceEnumerator> device_enumerator = nullptr;
  129. Microsoft::WRL::ComPtr<IMMDevice> device = nullptr;
  130. Microsoft::WRL::ComPtr<IMMDeviceCollection> collection = nullptr;
  131. LPWSTR current_device_id = NULL;
  132. std::shared_ptr<void> raii_ptr(nullptr, [&](void*) {
  133. collection = nullptr;
  134. device = nullptr;
  135. device_enumerator = nullptr;
  136. });
  137. int ret = AE_NO;
  138. do {
  139. HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator),
  140. NULL,
  141. CLSCTX_ALL,
  142. __uuidof(IMMDeviceEnumerator),
  143. (void**) device_enumerator.GetAddressOf());
  144. if (FAILED(hr)) {
  145. ret = AE_CO_CREATE_FAILED;
  146. break;
  147. }
  148. hr = device_enumerator->GetDefaultAudioEndpoint(input == true ? eCapture : eRender,
  149. eConsole,
  150. device.GetAddressOf());
  151. if (FAILED(hr)) {
  152. ret = AE_CO_GETENDPOINT_FAILED;
  153. break;
  154. }
  155. hr = device_enumerator->EnumAudioEndpoints(input == true ? eCapture : eRender,
  156. DEVICE_STATE_ACTIVE,
  157. collection.GetAddressOf());
  158. if (FAILED(hr)) {
  159. ret = AE_CO_ENUMENDPOINT_FAILED;
  160. break;
  161. }
  162. UINT count;
  163. hr = collection->GetCount(&count);
  164. if (FAILED(hr)) {
  165. ret = AE_CO_GET_ENDPOINT_COUNT_FAILED;
  166. break;
  167. }
  168. hr = device->GetId(&current_device_id);
  169. if (FAILED(hr)) {
  170. ret = AE_CO_GET_ENDPOINT_ID_FAILED;
  171. break;
  172. }
  173. id = utils_string::unicode_utf8(current_device_id);
  174. CoTaskMemFree(current_device_id);
  175. IPropertyStore* pPropertyStore = NULL;
  176. PROPVARIANT pv;
  177. PropVariantInit(&pv);
  178. hr = device->OpenPropertyStore(STGM_READ, &pPropertyStore);
  179. if (FAILED(hr)) {
  180. ret = AE_CO_OPEN_PROPERTY_FAILED;
  181. break;
  182. }
  183. hr = pPropertyStore->GetValue(PKEY_Device_FriendlyName, &pv);
  184. if (FAILED(hr)) {
  185. ret = AE_CO_GET_VALUE_FAILED;
  186. break;
  187. }
  188. if (pv.vt == VT_LPWSTR) {
  189. name = utils_string::unicode_utf8(pv.pwszVal);
  190. } else if (pv.vt == VT_LPSTR) {
  191. name = utils_string::ascii_utf8(pv.pszVal);
  192. }
  193. PropVariantClear(&pv);
  194. } while (0);
  195. if (ret != AE_NO)
  196. al_debug("get_devices failed(%lu): %s", GetLastError(), err2str(ret));
  197. return ret;
  198. }
  199. } // namespace am