CoreAudio: Separate enumeration code
The enumeration code being up at the top was making things quite messy, so I split that code out to a separate set of files.
This commit is contained in:
@@ -7,10 +7,9 @@
|
||||
#include <obs.h>
|
||||
#include <util/threading.h>
|
||||
#include <util/c99defs.h>
|
||||
#include <util/darray.h>
|
||||
#include <util/dstr.h>
|
||||
|
||||
#include "mac-helpers.h"
|
||||
#include "audio-device-enum.h"
|
||||
|
||||
#define PROPERTY_DEFAULT_DEVICE kAudioHardwarePropertyDefaultInputDevice
|
||||
#define PROPERTY_FORMATS kAudioStreamPropertyAvailablePhysicalFormats
|
||||
@@ -51,133 +50,6 @@ struct coreaudio_data {
|
||||
obs_source_t source;
|
||||
};
|
||||
|
||||
struct device_item {
|
||||
struct dstr name, value;
|
||||
};
|
||||
|
||||
static inline void device_item_free(struct device_item *item)
|
||||
{
|
||||
dstr_free(&item->name);
|
||||
dstr_free(&item->value);
|
||||
}
|
||||
|
||||
struct device_list {
|
||||
DARRAY(struct device_item) items;
|
||||
};
|
||||
|
||||
static inline void device_list_add(struct device_list *list,
|
||||
struct device_item *item)
|
||||
{
|
||||
da_push_back(list->items, item);
|
||||
memset(item, 0, sizeof(struct device_item));
|
||||
}
|
||||
|
||||
static inline void device_list_free(struct device_list *list)
|
||||
{
|
||||
for (size_t i = 0; i < list->items.num; i++)
|
||||
device_item_free(list->items.array+i);
|
||||
|
||||
da_free(list->items);
|
||||
}
|
||||
|
||||
/* ugh, because mac has no means of capturing output, we have to basically
|
||||
* mark soundflower and wavtap as output devices. */
|
||||
static inline bool device_is_input(char *device)
|
||||
{
|
||||
return astrstri(device, "soundflower") == NULL &&
|
||||
astrstri(device, "wavtap") == NULL;
|
||||
}
|
||||
|
||||
static inline bool enum_success(OSStatus stat, const char *msg)
|
||||
{
|
||||
if (stat != noErr) {
|
||||
blog(LOG_WARNING, "[coreaudio_enum_devices] %s failed: %d",
|
||||
msg, (int)stat);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void coreaudio_enum_add_device(struct device_list *list,
|
||||
AudioDeviceID id, bool input)
|
||||
{
|
||||
OSStatus stat;
|
||||
UInt32 size = 0;
|
||||
CFStringRef cf_name = NULL;
|
||||
CFStringRef cf_value = NULL;
|
||||
struct device_item item;
|
||||
|
||||
AudioObjectPropertyAddress addr = {
|
||||
kAudioDevicePropertyStreams,
|
||||
kAudioDevicePropertyScopeInput,
|
||||
kAudioObjectPropertyElementMaster
|
||||
};
|
||||
|
||||
memset(&item, 0, sizeof(item));
|
||||
|
||||
/* check to see if it's a mac input device */
|
||||
AudioObjectGetPropertyDataSize(id, &addr, 0, NULL, &size);
|
||||
if (!size)
|
||||
return;
|
||||
|
||||
size = sizeof(CFStringRef);
|
||||
|
||||
addr.mSelector = kAudioDevicePropertyDeviceUID;
|
||||
stat = AudioObjectGetPropertyData(id, &addr, 0, NULL, &size, &cf_value);
|
||||
if (!enum_success(stat, "get audio device UID"))
|
||||
return;
|
||||
|
||||
addr.mSelector = kAudioDevicePropertyDeviceNameCFString;
|
||||
stat = AudioObjectGetPropertyData(id, &addr, 0, NULL, &size, &cf_name);
|
||||
if (!enum_success(stat, "get audio device name"))
|
||||
goto fail;
|
||||
|
||||
if (!cf_to_dstr(cf_name, &item.name))
|
||||
goto fail;
|
||||
if (!cf_to_dstr(cf_value, &item.value))
|
||||
goto fail;
|
||||
|
||||
if (input || !device_is_input(item.value.array))
|
||||
device_list_add(list, &item);
|
||||
|
||||
fail:
|
||||
device_item_free(&item);
|
||||
CFRelease(cf_name);
|
||||
CFRelease(cf_value);
|
||||
}
|
||||
|
||||
static void coreaudio_enum_devices(struct device_list *list, bool input)
|
||||
{
|
||||
AudioObjectPropertyAddress addr = {
|
||||
kAudioHardwarePropertyDevices,
|
||||
kAudioObjectPropertyScopeGlobal,
|
||||
kAudioObjectPropertyElementMaster
|
||||
};
|
||||
|
||||
UInt32 size = 0;
|
||||
UInt32 count;
|
||||
OSStatus stat;
|
||||
AudioDeviceID *ids;
|
||||
|
||||
stat = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr,
|
||||
0, NULL, &size);
|
||||
if (!enum_success(stat, "get kAudioObjectSystemObject data size"))
|
||||
return;
|
||||
|
||||
ids = bmalloc(size);
|
||||
count = size / sizeof(AudioDeviceID);
|
||||
|
||||
stat = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr,
|
||||
0, NULL, &size, ids);
|
||||
|
||||
if (enum_success(stat, "get kAudioObjectSystemObject data"))
|
||||
for (UInt32 i = 0; i < count; i++)
|
||||
coreaudio_enum_add_device(list, ids[i], input);
|
||||
|
||||
bfree(ids);
|
||||
}
|
||||
|
||||
static bool get_default_output_device(struct coreaudio_data *ca)
|
||||
{
|
||||
struct device_list list;
|
||||
|
Reference in New Issue
Block a user