audio-device-enum.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. static void coreaudio_enum_add_device(struct device_list *list,
  22. AudioDeviceID id, bool input)
  23. {
  24. OSStatus stat;
  25. UInt32 size = 0;
  26. CFStringRef cf_name = NULL;
  27. CFStringRef cf_value = NULL;
  28. struct device_item item;
  29. AudioObjectPropertyAddress addr = {
  30. kAudioDevicePropertyStreams,
  31. kAudioDevicePropertyScopeInput,
  32. kAudioObjectPropertyElementMaster
  33. };
  34. memset(&item, 0, sizeof(item));
  35. /* check to see if it's a mac input device */
  36. AudioObjectGetPropertyDataSize(id, &addr, 0, NULL, &size);
  37. if (!size)
  38. return;
  39. size = sizeof(CFStringRef);
  40. addr.mSelector = kAudioDevicePropertyDeviceUID;
  41. stat = AudioObjectGetPropertyData(id, &addr, 0, NULL, &size, &cf_value);
  42. if (!enum_success(stat, "get audio device UID"))
  43. return;
  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. if (!cf_to_dstr(cf_name, &item.name))
  49. goto fail;
  50. if (!cf_to_dstr(cf_value, &item.value))
  51. goto fail;
  52. if (input || !device_is_input(item.value.array))
  53. device_list_add(list, &item);
  54. fail:
  55. device_item_free(&item);
  56. CFRelease(cf_name);
  57. CFRelease(cf_value);
  58. }
  59. void coreaudio_enum_devices(struct device_list *list, bool input)
  60. {
  61. AudioObjectPropertyAddress addr = {
  62. kAudioHardwarePropertyDevices,
  63. kAudioObjectPropertyScopeGlobal,
  64. kAudioObjectPropertyElementMaster
  65. };
  66. UInt32 size = 0;
  67. UInt32 count;
  68. OSStatus stat;
  69. AudioDeviceID *ids;
  70. stat = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr,
  71. 0, NULL, &size);
  72. if (!enum_success(stat, "get kAudioObjectSystemObject data size"))
  73. return;
  74. ids = bmalloc(size);
  75. count = size / sizeof(AudioDeviceID);
  76. stat = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr,
  77. 0, NULL, &size, ids);
  78. if (enum_success(stat, "get kAudioObjectSystemObject data"))
  79. for (UInt32 i = 0; i < count; i++)
  80. coreaudio_enum_add_device(list, ids[i], input);
  81. bfree(ids);
  82. }