audio-device-enum.c 3.9 KB

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