device_audios.cpp 6.1 KB

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