audio-device-enum.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <CoreFoundation/CFString.h>
  2. #include <CoreAudio/CoreAudio.h>
  3. #include <util/apple/cfstring-utils.h>
  4. #include "audio-device-enum.h"
  5. /* ugh, because mac has no means of capturing output, we have to basically
  6. * mark soundflower, wavtap and sound siphon as output devices. */
  7. static inline bool device_is_input(char *device)
  8. {
  9. return astrstri(device, "soundflower") == NULL &&
  10. astrstri(device, "wavtap") == NULL &&
  11. astrstri(device, "soundsiphon") == NULL &&
  12. astrstri(device, "ishowu") == NULL &&
  13. astrstri(device, "blackhole") == NULL &&
  14. astrstri(device, "loopback") == NULL;
  15. }
  16. static inline bool enum_success(OSStatus stat, const char *msg)
  17. {
  18. if (stat != noErr) {
  19. blog(LOG_WARNING, "[coreaudio_enum_devices] %s failed: %d", msg,
  20. (int)stat);
  21. return false;
  22. }
  23. return true;
  24. }
  25. typedef bool (*enum_device_proc_t)(void *param, CFStringRef cf_name,
  26. CFStringRef cf_uid, AudioDeviceID id);
  27. static bool coreaudio_enum_device(enum_device_proc_t proc, void *param,
  28. AudioDeviceID id)
  29. {
  30. UInt32 size = 0;
  31. CFStringRef cf_name = NULL;
  32. CFStringRef cf_uid = NULL;
  33. bool enum_next = true;
  34. OSStatus stat;
  35. AudioObjectPropertyAddress addr = {kAudioDevicePropertyStreams,
  36. kAudioDevicePropertyScopeInput,
  37. kAudioObjectPropertyElementMaster};
  38. /* check to see if it's a mac input device */
  39. AudioObjectGetPropertyDataSize(id, &addr, 0, NULL, &size);
  40. if (!size)
  41. return true;
  42. size = sizeof(CFStringRef);
  43. addr.mSelector = kAudioDevicePropertyDeviceUID;
  44. stat = AudioObjectGetPropertyData(id, &addr, 0, NULL, &size, &cf_uid);
  45. if (!enum_success(stat, "get audio device UID"))
  46. return true;
  47. addr.mSelector = kAudioDevicePropertyDeviceNameCFString;
  48. stat = AudioObjectGetPropertyData(id, &addr, 0, NULL, &size, &cf_name);
  49. if (!enum_success(stat, "get audio device name"))
  50. goto fail;
  51. enum_next = proc(param, cf_name, cf_uid, id);
  52. fail:
  53. if (cf_name)
  54. CFRelease(cf_name);
  55. if (cf_uid)
  56. CFRelease(cf_uid);
  57. return enum_next;
  58. }
  59. static void enum_devices(enum_device_proc_t proc, void *param)
  60. {
  61. AudioObjectPropertyAddress addr = {kAudioHardwarePropertyDevices,
  62. kAudioObjectPropertyScopeGlobal,
  63. kAudioObjectPropertyElementMaster};
  64. UInt32 size = 0;
  65. UInt32 count;
  66. OSStatus stat;
  67. AudioDeviceID *ids;
  68. stat = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr,
  69. 0, NULL, &size);
  70. if (!enum_success(stat, "get kAudioObjectSystemObject data size"))
  71. return;
  72. ids = bmalloc(size);
  73. count = size / sizeof(AudioDeviceID);
  74. stat = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0,
  75. NULL, &size, ids);
  76. if (enum_success(stat, "get kAudioObjectSystemObject data"))
  77. for (UInt32 i = 0; i < count; i++)
  78. if (!coreaudio_enum_device(proc, param, ids[i]))
  79. break;
  80. bfree(ids);
  81. }
  82. struct add_data {
  83. struct device_list *list;
  84. bool input;
  85. };
  86. static bool coreaudio_enum_add_device(void *param, CFStringRef cf_name,
  87. CFStringRef cf_uid, AudioDeviceID id)
  88. {
  89. struct add_data *data = param;
  90. struct device_item item;
  91. memset(&item, 0, sizeof(item));
  92. if (!cfstr_copy_dstr(cf_name, kCFStringEncodingUTF8, &item.name))
  93. goto fail;
  94. if (!cfstr_copy_dstr(cf_uid, kCFStringEncodingUTF8, &item.value))
  95. goto fail;
  96. if (data->input || !device_is_input(item.value.array))
  97. device_list_add(data->list, &item);
  98. fail:
  99. device_item_free(&item);
  100. UNUSED_PARAMETER(id);
  101. return true;
  102. }
  103. void coreaudio_enum_devices(struct device_list *list, bool input)
  104. {
  105. struct add_data data = {list, input};
  106. enum_devices(coreaudio_enum_add_device, &data);
  107. }
  108. struct device_id_data {
  109. CFStringRef uid;
  110. AudioDeviceID *id;
  111. bool found;
  112. };
  113. static bool get_device_id(void *param, CFStringRef cf_name, CFStringRef cf_uid,
  114. AudioDeviceID id)
  115. {
  116. struct device_id_data *data = param;
  117. if (CFStringCompare(cf_uid, data->uid, 0) == 0) {
  118. *data->id = id;
  119. data->found = true;
  120. return false;
  121. }
  122. UNUSED_PARAMETER(cf_name);
  123. return true;
  124. }
  125. bool coreaudio_get_device_id(CFStringRef uid, AudioDeviceID *id)
  126. {
  127. struct device_id_data data = {uid, id, false};
  128. enum_devices(get_device_id, &data);
  129. return data.found;
  130. }