audio-device-enum.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <CoreFoundation/CFString.h>
  2. #include <CoreAudio/CoreAudio.h>
  3. #include "mac-helpers.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. CFRelease(cf_name);
  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 = {
  59. kAudioHardwarePropertyDevices,
  60. kAudioObjectPropertyScopeGlobal,
  61. kAudioObjectPropertyElementMaster
  62. };
  63. UInt32 size = 0;
  64. UInt32 count;
  65. OSStatus stat;
  66. AudioDeviceID *ids;
  67. stat = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr,
  68. 0, NULL, &size);
  69. if (!enum_success(stat, "get kAudioObjectSystemObject data size"))
  70. return;
  71. ids = bmalloc(size);
  72. count = size / sizeof(AudioDeviceID);
  73. stat = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr,
  74. 0, NULL, &size, ids);
  75. if (enum_success(stat, "get kAudioObjectSystemObject data"))
  76. for (UInt32 i = 0; i < count; i++)
  77. if (!coreaudio_enum_device(proc, param, ids[i]))
  78. break;
  79. bfree(ids);
  80. }
  81. struct add_data {
  82. struct device_list *list;
  83. bool input;
  84. };
  85. static bool coreaudio_enum_add_device(void *param, CFStringRef cf_name,
  86. CFStringRef cf_uid, AudioDeviceID id)
  87. {
  88. struct add_data *data = param;
  89. struct device_item item;
  90. memset(&item, 0, sizeof(item));
  91. if (!cf_to_dstr(cf_name, &item.name))
  92. goto fail;
  93. if (!cf_to_dstr(cf_uid, &item.value))
  94. goto fail;
  95. if (data->input || !device_is_input(item.value.array))
  96. device_list_add(data->list, &item);
  97. fail:
  98. device_item_free(&item);
  99. UNUSED_PARAMETER(id);
  100. return true;
  101. }
  102. void coreaudio_enum_devices(struct device_list *list, bool input)
  103. {
  104. struct add_data data = {list, input};
  105. enum_devices(coreaudio_enum_add_device, &data);
  106. }
  107. struct device_id_data {
  108. CFStringRef uid;
  109. AudioDeviceID *id;
  110. bool found;
  111. };
  112. static bool get_device_id(void *param, CFStringRef cf_name, CFStringRef cf_uid,
  113. AudioDeviceID id)
  114. {
  115. struct device_id_data *data = param;
  116. if (CFStringCompare(cf_uid, data->uid, 0) == 0) {
  117. *data->id = id;
  118. data->found = true;
  119. return false;
  120. }
  121. UNUSED_PARAMETER(cf_name);
  122. return true;
  123. }
  124. bool coreaudio_get_device_id(CFStringRef uid, AudioDeviceID *id)
  125. {
  126. struct device_id_data data = {uid, id, false};
  127. enum_devices(get_device_id, &data);
  128. return data.found;
  129. }