audio-device-enum.c 3.8 KB

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