This commit is contained in:
Alex Chronopoulos 2016-07-04 15:54:59 +03:00
Родитель fd8a322aab
Коммит 0ddcc6c0b2
3 изменённых файлов: 63 добавлений и 54 удалений

Просмотреть файл

@ -65,10 +65,10 @@ typedef UInt32 AudioFormatFlags;
#define LOG(...)
#endif
static struct cubeb_ops const audiounit_ops;
extern cubeb_ops const audiounit_ops;
struct cubeb {
struct cubeb_ops const * ops;
cubeb_ops const * ops;
pthread_mutex_t mutex;
int active_streams;
int limit_streams;
@ -165,7 +165,7 @@ audiounit_input_callback(void * user_ptr,
UInt32 input_frames,
AudioBufferList * bufs)
{
cubeb_stream * stm = user_ptr;
cubeb_stream * stm = static_cast<cubeb_stream *>(user_ptr);
long outframes, frames;
pthread_mutex_lock(&stm->mutex);
@ -253,7 +253,7 @@ audiounit_output_callback(void * user_ptr,
assert(AU_OUT_BUS == bus);
assert(outBufferList->mNumberBuffers == 1);
cubeb_stream * stm = user_ptr;
cubeb_stream * stm = static_cast<cubeb_stream *>(user_ptr);
LOG("- output(%p): buffers %d, size %d, channels %d, frames %d\n", stm,
outBufferList->mNumberBuffers, outBufferList->mBuffers[0].mDataByteSize,
@ -358,7 +358,8 @@ audiounit_output_callback(void * user_ptr,
return noErr;
}
/*static*/ int
extern "C" {
int
audiounit_init(cubeb ** context, char const * context_name)
{
cubeb * ctx;
@ -366,8 +367,9 @@ audiounit_init(cubeb ** context, char const * context_name)
*context = NULL;
ctx = calloc(1, sizeof(*ctx));
ctx = new cubeb;
assert(ctx);
memset(ctx, 0, sizeof(cubeb));
ctx->ops = &audiounit_ops;
@ -385,6 +387,7 @@ audiounit_init(cubeb ** context, char const * context_name)
return CUBEB_OK;
}
}
static char const *
audiounit_get_backend_id(cubeb * ctx)
@ -773,7 +776,7 @@ audiounit_destroy(cubeb * ctx)
int r = pthread_mutex_destroy(&ctx->mutex);
assert(r == 0);
free(ctx);
delete ctx;
}
static void audiounit_stream_destroy(cubeb_stream * stm);
@ -862,7 +865,7 @@ audiounit_create_unit(AudioUnit * unit,
devid = audiounit_get_default_device_id(is_input ? CUBEB_DEVICE_TYPE_INPUT
: CUBEB_DEVICE_TYPE_OUTPUT);
} else {
devid = (AudioDeviceID)device;
devid = *(static_cast<AudioDeviceID*>(device));
}
int err = AudioUnitSetProperty(*unit, kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global,
@ -945,8 +948,9 @@ audiounit_stream_init(cubeb * context,
}
}
stm = calloc(1, sizeof(cubeb_stream));
stm = new cubeb_stream;
assert(stm);
memset(stm, 0, sizeof(cubeb_stream));
/* These could be different in the future if we have both
* full-duplex stream and different devices for input vs output. */
@ -1225,7 +1229,7 @@ audiounit_stream_destroy(cubeb_stream * stm)
stm->context->active_streams -= 1;
pthread_mutex_unlock(&stm->context->mutex);
free(stm);
delete stm;
}
static int
@ -1419,10 +1423,11 @@ int audiounit_stream_get_current_device(cubeb_stream * stm,
return CUBEB_ERROR;
}
*device = malloc(sizeof(cubeb_device));
*device = new cubeb_device;
if (!*device) {
return CUBEB_ERROR;
}
memset(*device, 0, sizeof(cubeb_device));
size = sizeof(UInt32);
/* This fails with some USB headset, so simply return an empty string. */
@ -1434,12 +1439,7 @@ int audiounit_stream_get_current_device(cubeb_stream * stm,
data = 0;
}
(*device)->output_name = malloc(size + 1);
if (!(*device)->output_name) {
return CUBEB_ERROR;
}
(*device)->output_name = malloc(size + 1);
(*device)->output_name = static_cast<char*>(malloc(size + 1));
if (!(*device)->output_name) {
return CUBEB_ERROR;
}
@ -1465,7 +1465,7 @@ int audiounit_stream_get_current_device(cubeb_stream * stm,
data = 0;
}
(*device)->input_name = malloc(size + 1);
(*device)->input_name = static_cast<char*>(malloc(size + 1));
if (!(*device)->input_name) {
return CUBEB_ERROR;
}
@ -1488,7 +1488,7 @@ int audiounit_stream_device_destroy(cubeb_stream * stream,
{
free(device->output_name);
free(device->input_name);
free(device);
delete device;
return CUBEB_OK;
}
@ -1533,7 +1533,7 @@ audiounit_get_devices(AudioObjectID ** devices, uint32_t * count)
if (*devices != NULL) {
free(*devices);
}
*devices = malloc(size);
*devices = static_cast<AudioObjectID *>(malloc(size));
memset(*devices, 0, size);
ret = AudioObjectGetPropertyData(kAudioObjectSystemObject, &adr, 0, NULL, &size, (void *)*devices);
@ -1560,7 +1560,7 @@ audiounit_strref_to_cstr_utf8(CFStringRef strref)
len = CFStringGetLength(strref);
size = CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8);
ret = malloc(size);
ret = static_cast<char*>(malloc(size));
if (!CFStringGetCString(strref, ret, size, kCFStringEncodingUTF8)) {
free(ret);
@ -1580,7 +1580,7 @@ audiounit_get_channel_count(AudioObjectID devid, AudioObjectPropertyScope scope)
adr.mSelector = kAudioDevicePropertyStreamConfiguration;
if (AudioObjectGetPropertyDataSize(devid, &adr, 0, NULL, &size) == noErr && size > 0) {
AudioBufferList * list = alloca(size);
AudioBufferList * list = static_cast<AudioBufferList *>(alloca(size));
if (AudioObjectGetPropertyData(devid, &adr, 0, NULL, &size, list) == noErr) {
for (i = 0; i < list->mNumberBuffers; i++)
ret += list->mBuffers[i].mNumberChannels;
@ -1611,7 +1611,7 @@ audiounit_get_available_samplerate(AudioObjectID devid, AudioObjectPropertyScope
if (AudioObjectHasProperty(devid, &adr) &&
AudioObjectGetPropertyDataSize(devid, &adr, 0, NULL, &size) == noErr) {
uint32_t i, count = size / sizeof(AudioValueRange);
AudioValueRange * ranges = malloc(size);
AudioValueRange * ranges = static_cast<AudioValueRange *>(malloc(size));
range.mMinimum = 9999999999.0;
range.mMaximum = 0.0;
if (AudioObjectGetPropertyData(devid, &adr, 0, NULL, &size, ranges) == noErr) {
@ -1683,7 +1683,8 @@ audiounit_create_device_from_hwdev(AudioObjectID devid, cubeb_device_type type)
return NULL;
}
ret = calloc(1, sizeof(cubeb_device_info));
ret = new cubeb_device_info;
memset(ret, 0, sizeof(cubeb_device_info));
size = sizeof(CFStringRef);
adr.mSelector = kAudioDevicePropertyDeviceUID;
@ -1733,7 +1734,7 @@ audiounit_create_device_from_hwdev(AudioObjectID devid, cubeb_device_type type)
CUBEB_DEVICE_PREF_ALL : CUBEB_DEVICE_PREF_NONE;
ret->max_channels = ch;
ret->format = CUBEB_DEVICE_FMT_ALL; /* CoreAudio supports All! */
ret->format = (cubeb_device_fmt)CUBEB_DEVICE_FMT_ALL; /* CoreAudio supports All! */
/* kAudioFormatFlagsAudioUnitCanonical is deprecated, prefer floating point */
ret->default_format = CUBEB_DEVICE_FMT_F32NE;
audiounit_get_available_samplerate(devid, adr.mScope,
@ -1766,8 +1767,8 @@ audiounit_enumerate_devices(cubeb * context, cubeb_device_type type,
return CUBEB_ERROR;
}
*collection = malloc(sizeof(cubeb_device_collection) +
sizeof(cubeb_device_info*) * (hwdevcount > 0 ? hwdevcount - 1 : 0));
*collection = static_cast<cubeb_device_collection *>(malloc(sizeof(cubeb_device_collection) +
sizeof(cubeb_device_info*) * (hwdevcount > 0 ? hwdevcount - 1 : 0)));
(*collection)->count = 0;
if (hwdevcount > 0) {
@ -1825,7 +1826,7 @@ audiounit_get_devices_of_type(cubeb_device_type devtype, AudioObjectID ** devid_
if (devtype == (CUBEB_DEVICE_TYPE_INPUT | CUBEB_DEVICE_TYPE_OUTPUT)) {
if (devid_array) {
*devid_array = calloc(count, sizeof(AudioObjectID));
*devid_array = static_cast<AudioObjectID *>(calloc(count, sizeof(AudioObjectID)));
assert(*devid_array);
memcpy(*devid_array, &devices, count * sizeof(AudioObjectID));
}
@ -1847,7 +1848,7 @@ audiounit_get_devices_of_type(cubeb_device_type devtype, AudioObjectID ** devid_
}
if (devid_array && dev_count > 0) {
*devid_array = calloc(dev_count, sizeof(AudioObjectID));
*devid_array = static_cast<AudioObjectID *>(calloc(dev_count, sizeof(AudioObjectID)));
assert(*devid_array);
memcpy(*devid_array, &devices_in_scope, dev_count * sizeof(AudioObjectID));
}
@ -1872,7 +1873,7 @@ audiounit_collection_changed_callback(AudioObjectID inObjectID,
const AudioObjectPropertyAddress * inAddresses,
void * inClientData)
{
cubeb * context = inClientData;
cubeb * context = static_cast<cubeb *>(inClientData);
pthread_mutex_lock(&context->mutex);
if (context->collection_changed_callback == NULL) {
/* Listener removed while waiting in mutex, abort. */
@ -1957,7 +1958,7 @@ audiounit_remove_device_listener(cubeb * context)
context);
if (ret == noErr) {
/* Reset all values. */
context->collection_changed_devtype = 0;
context->collection_changed_devtype = CUBEB_DEVICE_TYPE_UNKNOWN;
context->collection_changed_callback = NULL;
context->collection_changed_user_ptr = NULL;
context->devtype_device_count = 0;
@ -1987,24 +1988,24 @@ int audiounit_register_device_collection_changed(cubeb * context,
return (ret == noErr) ? CUBEB_OK : CUBEB_ERROR;
}
static struct cubeb_ops const audiounit_ops = {
.init = audiounit_init,
.get_backend_id = audiounit_get_backend_id,
.get_max_channel_count = audiounit_get_max_channel_count,
.get_min_latency = audiounit_get_min_latency,
.get_preferred_sample_rate = audiounit_get_preferred_sample_rate,
.enumerate_devices = audiounit_enumerate_devices,
.destroy = audiounit_destroy,
.stream_init = audiounit_stream_init,
.stream_destroy = audiounit_stream_destroy,
.stream_start = audiounit_stream_start,
.stream_stop = audiounit_stream_stop,
.stream_get_position = audiounit_stream_get_position,
.stream_get_latency = audiounit_stream_get_latency,
.stream_set_volume = audiounit_stream_set_volume,
.stream_set_panning = audiounit_stream_set_panning,
.stream_get_current_device = audiounit_stream_get_current_device,
.stream_device_destroy = audiounit_stream_device_destroy,
.stream_register_device_changed_callback = audiounit_stream_register_device_changed_callback,
.register_device_collection_changed = audiounit_register_device_collection_changed
cubeb_ops const audiounit_ops = {
/*.init =*/ audiounit_init,
/*.get_backend_id =*/ audiounit_get_backend_id,
/*.get_max_channel_count =*/ audiounit_get_max_channel_count,
/*.get_min_latency =*/ audiounit_get_min_latency,
/*.get_preferred_sample_rate =*/ audiounit_get_preferred_sample_rate,
/*.enumerate_devices =*/ audiounit_enumerate_devices,
/*.destroy =*/ audiounit_destroy,
/*.stream_init =*/ audiounit_stream_init,
/*.stream_destroy =*/ audiounit_stream_destroy,
/*.stream_start =*/ audiounit_stream_start,
/*.stream_stop =*/ audiounit_stream_stop,
/*.stream_get_position =*/ audiounit_stream_get_position,
/*.stream_get_latency =*/ audiounit_stream_get_latency,
/*.stream_set_volume =*/ audiounit_stream_set_volume,
/*.stream_set_panning =*/ audiounit_stream_set_panning,
/*.stream_get_current_device =*/ audiounit_stream_get_current_device,
/*.stream_device_destroy =*/ audiounit_stream_device_destroy,
/*.stream_register_device_changed_callback =*/ audiounit_stream_register_device_changed_callback,
/*.register_device_collection_changed =*/ audiounit_register_device_collection_changed
};

Просмотреть файл

@ -11,4 +11,12 @@
*
* This has to be called only once per process, so it is in a separate header
* for easy integration in other code bases. */
#if defined(__cplusplus)
extern "C" {
#endif
void cubeb_set_coreaudio_notification_runloop();
#if defined(__cplusplus)
}
#endif

Просмотреть файл

@ -64,7 +64,7 @@ ring_array_init(ring_array * ra,
ra->tail = 0;
ra->count = 0;
ra->buffer_array = calloc(ra->capacity, sizeof(AudioBuffer));
ra->buffer_array = (AudioBuffer *)calloc(ra->capacity, sizeof(AudioBuffer));
if (ra->buffer_array == NULL) {
return CUBEB_ERROR;
}
@ -111,7 +111,7 @@ ring_array_get_free_buffer(ring_array * ra)
}
assert(ra->count == 0 || (ra->tail + ra->count) % ra->capacity != ra->tail);
void * ret = &ra->buffer_array[(ra->tail + ra->count) % ra->capacity];
AudioBuffer * ret = &ra->buffer_array[(ra->tail + ra->count) % ra->capacity];
++ra->count;
assert(ra->count <= ra->capacity);
@ -131,7 +131,7 @@ ring_array_get_data_buffer(ring_array * ra)
if (ra->count == 0) {
return NULL;
}
void * ret = &ra->buffer_array[ra->tail];
AudioBuffer * ret = &ra->buffer_array[ra->tail];
ra->tail = (ra->tail + 1) % ra->capacity;
assert(ra->tail < ra->capacity);