yolobs-studio/plugins/mac-capture/audio-device-enum.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

165 lines
4 KiB
C
Raw Permalink Normal View History

2016-02-23 23:16:51 +00:00
#include <CoreFoundation/CFString.h>
#include <CoreAudio/CoreAudio.h>
2019-07-27 12:47:10 +00:00
#include <util/apple/cfstring-utils.h>
2016-02-23 23:16:51 +00:00
#include "audio-device-enum.h"
/* ugh, because mac has no means of capturing output, we have to basically
* mark soundflower, wavtap and sound siphon as output devices. */
static inline bool device_is_input(char *device)
{
return astrstri(device, "soundflower") == NULL &&
2019-09-22 21:19:10 +00:00
astrstri(device, "wavtap") == NULL &&
2020-10-01 20:15:25 +00:00
astrstri(device, "soundsiphon") == NULL &&
astrstri(device, "ishowu") == NULL &&
astrstri(device, "blackhole") == NULL &&
astrstri(device, "loopback") == NULL;
2016-02-23 23:16:51 +00:00
}
static inline bool enum_success(OSStatus stat, const char *msg)
{
if (stat != noErr) {
2019-09-22 21:19:10 +00:00
blog(LOG_WARNING, "[coreaudio_enum_devices] %s failed: %d", msg,
(int)stat);
2016-02-23 23:16:51 +00:00
return false;
}
return true;
}
typedef bool (*enum_device_proc_t)(void *param, CFStringRef cf_name,
2019-09-22 21:19:10 +00:00
CFStringRef cf_uid, AudioDeviceID id);
2016-02-23 23:16:51 +00:00
static bool coreaudio_enum_device(enum_device_proc_t proc, void *param,
2019-09-22 21:19:10 +00:00
AudioDeviceID id)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
UInt32 size = 0;
CFStringRef cf_name = NULL;
CFStringRef cf_uid = NULL;
bool enum_next = true;
OSStatus stat;
AudioObjectPropertyAddress addr = {kAudioDevicePropertyStreams,
kAudioDevicePropertyScopeInput,
kAudioObjectPropertyElementMaster};
2016-02-23 23:16:51 +00:00
/* check to see if it's a mac input device */
AudioObjectGetPropertyDataSize(id, &addr, 0, NULL, &size);
if (!size)
return true;
size = sizeof(CFStringRef);
addr.mSelector = kAudioDevicePropertyDeviceUID;
stat = AudioObjectGetPropertyData(id, &addr, 0, NULL, &size, &cf_uid);
if (!enum_success(stat, "get audio device UID"))
return true;
addr.mSelector = kAudioDevicePropertyDeviceNameCFString;
stat = AudioObjectGetPropertyData(id, &addr, 0, NULL, &size, &cf_name);
if (!enum_success(stat, "get audio device name"))
goto fail;
enum_next = proc(param, cf_name, cf_uid, id);
fail:
if (cf_name)
CFRelease(cf_name);
if (cf_uid)
CFRelease(cf_uid);
return enum_next;
}
static void enum_devices(enum_device_proc_t proc, void *param)
{
2019-09-22 21:19:10 +00:00
AudioObjectPropertyAddress addr = {kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster};
UInt32 size = 0;
UInt32 count;
OSStatus stat;
2016-02-23 23:16:51 +00:00
AudioDeviceID *ids;
stat = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr,
2019-09-22 21:19:10 +00:00
0, NULL, &size);
2016-02-23 23:16:51 +00:00
if (!enum_success(stat, "get kAudioObjectSystemObject data size"))
return;
2019-09-22 21:19:10 +00:00
ids = bmalloc(size);
2016-02-23 23:16:51 +00:00
count = size / sizeof(AudioDeviceID);
2019-09-22 21:19:10 +00:00
stat = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr, 0,
NULL, &size, ids);
2016-02-23 23:16:51 +00:00
if (enum_success(stat, "get kAudioObjectSystemObject data"))
for (UInt32 i = 0; i < count; i++)
if (!coreaudio_enum_device(proc, param, ids[i]))
break;
bfree(ids);
}
struct add_data {
struct device_list *list;
2019-09-22 21:19:10 +00:00
bool input;
2016-02-23 23:16:51 +00:00
};
static bool coreaudio_enum_add_device(void *param, CFStringRef cf_name,
2019-09-22 21:19:10 +00:00
CFStringRef cf_uid, AudioDeviceID id)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
struct add_data *data = param;
2016-02-23 23:16:51 +00:00
struct device_item item;
memset(&item, 0, sizeof(item));
2019-07-27 12:47:10 +00:00
if (!cfstr_copy_dstr(cf_name, kCFStringEncodingUTF8, &item.name))
2016-02-23 23:16:51 +00:00
goto fail;
2019-09-22 21:19:10 +00:00
if (!cfstr_copy_dstr(cf_uid, kCFStringEncodingUTF8, &item.value))
2016-02-23 23:16:51 +00:00
goto fail;
if (data->input || !device_is_input(item.value.array))
device_list_add(data->list, &item);
fail:
device_item_free(&item);
UNUSED_PARAMETER(id);
return true;
}
void coreaudio_enum_devices(struct device_list *list, bool input)
{
struct add_data data = {list, input};
enum_devices(coreaudio_enum_add_device, &data);
}
struct device_id_data {
2019-09-22 21:19:10 +00:00
CFStringRef uid;
2016-02-23 23:16:51 +00:00
AudioDeviceID *id;
2019-09-22 21:19:10 +00:00
bool found;
2016-02-23 23:16:51 +00:00
};
static bool get_device_id(void *param, CFStringRef cf_name, CFStringRef cf_uid,
2019-09-22 21:19:10 +00:00
AudioDeviceID id)
2016-02-23 23:16:51 +00:00
{
struct device_id_data *data = param;
if (CFStringCompare(cf_uid, data->uid, 0) == 0) {
2019-09-22 21:19:10 +00:00
*data->id = id;
2016-02-23 23:16:51 +00:00
data->found = true;
return false;
}
UNUSED_PARAMETER(cf_name);
return true;
}
bool coreaudio_get_device_id(CFStringRef uid, AudioDeviceID *id)
{
struct device_id_data data = {uid, id, false};
enum_devices(get_device_id, &data);
return data.found;
}