audio-device-enum.c 3.9 KB

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