yolobs-studio/libobs/audio-monitoring/osx/coreaudio-output.c

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

337 lines
8.2 KiB
C
Raw Permalink Normal View History

2017-04-19 19:54:15 +00:00
#include <AudioUnit/AudioUnit.h>
2018-12-16 16:14:58 +00:00
#include <AudioToolbox/AudioQueue.h>
2017-04-19 19:54:15 +00:00
#include <CoreFoundation/CFString.h>
#include <CoreAudio/CoreAudio.h>
#include "../../media-io/audio-resampler.h"
#include "../../util/circlebuf.h"
#include "../../util/threading.h"
#include "../../util/platform.h"
#include "../../obs-internal.h"
#include "../../util/darray.h"
#include "mac-helpers.h"
struct audio_monitor {
2019-09-22 21:19:10 +00:00
obs_source_t *source;
AudioQueueRef queue;
AudioQueueBufferRef buffers[3];
pthread_mutex_t mutex;
struct circlebuf empty_buffers;
struct circlebuf new_data;
audio_resampler_t *resampler;
size_t buffer_size;
size_t wait_size;
uint32_t channels;
volatile bool active;
bool paused;
bool ignore;
2017-04-19 19:54:15 +00:00
};
static inline bool fill_buffer(struct audio_monitor *monitor)
{
AudioQueueBufferRef buf;
OSStatus stat;
if (monitor->new_data.size < monitor->buffer_size) {
return false;
}
circlebuf_pop_front(&monitor->empty_buffers, &buf, sizeof(buf));
circlebuf_pop_front(&monitor->new_data, buf->mAudioData,
2019-09-22 21:19:10 +00:00
monitor->buffer_size);
2017-04-19 19:54:15 +00:00
buf->mAudioDataByteSize = monitor->buffer_size;
stat = AudioQueueEnqueueBuffer(monitor->queue, buf, 0, NULL);
if (!success(stat, "AudioQueueEnqueueBuffer")) {
blog(LOG_WARNING, "%s: %s", __FUNCTION__,
2019-09-22 21:19:10 +00:00
"Failed to enqueue buffer");
2017-04-19 19:54:15 +00:00
AudioQueueStop(monitor->queue, false);
}
return true;
}
static void on_audio_playback(void *param, obs_source_t *source,
2019-09-22 21:19:10 +00:00
const struct audio_data *audio_data, bool muted)
2017-04-19 19:54:15 +00:00
{
struct audio_monitor *monitor = param;
float vol = source->user_volume;
uint32_t bytes;
UNUSED_PARAMETER(source);
if (!os_atomic_load_bool(&monitor->active)) {
return;
}
uint8_t *resample_data[MAX_AV_PLANES];
uint32_t resample_frames;
uint64_t ts_offset;
bool success;
2019-09-22 21:19:10 +00:00
success = audio_resampler_resample(
monitor->resampler, resample_data, &resample_frames, &ts_offset,
(const uint8_t *const *)audio_data->data,
(uint32_t)audio_data->frames);
2017-04-19 19:54:15 +00:00
if (!success) {
return;
}
bytes = sizeof(float) * monitor->channels * resample_frames;
if (muted) {
memset(resample_data[0], 0, bytes);
} else {
/* apply volume */
if (!close_float(vol, 1.0f, EPSILON)) {
2019-09-22 21:19:10 +00:00
register float *cur = (float *)resample_data[0];
register float *end =
cur + resample_frames * monitor->channels;
2017-04-19 19:54:15 +00:00
while (cur < end)
*(cur++) *= vol;
}
}
pthread_mutex_lock(&monitor->mutex);
circlebuf_push_back(&monitor->new_data, resample_data[0], bytes);
if (monitor->new_data.size >= monitor->wait_size) {
monitor->wait_size = 0;
while (monitor->empty_buffers.size > 0) {
if (!fill_buffer(monitor)) {
break;
}
}
if (monitor->paused) {
AudioQueueStart(monitor->queue, NULL);
monitor->paused = false;
}
}
pthread_mutex_unlock(&monitor->mutex);
}
static void buffer_audio(void *data, AudioQueueRef aq, AudioQueueBufferRef buf)
{
struct audio_monitor *monitor = data;
pthread_mutex_lock(&monitor->mutex);
circlebuf_push_back(&monitor->empty_buffers, &buf, sizeof(buf));
while (monitor->empty_buffers.size > 0) {
if (!fill_buffer(monitor)) {
break;
}
}
if (monitor->empty_buffers.size == sizeof(buf) * 3) {
monitor->paused = true;
monitor->wait_size = monitor->buffer_size * 3;
AudioQueuePause(monitor->queue);
}
pthread_mutex_unlock(&monitor->mutex);
UNUSED_PARAMETER(aq);
}
2017-06-29 19:01:10 +00:00
extern bool devices_match(const char *id1, const char *id2);
static bool audio_monitor_init(struct audio_monitor *monitor,
2019-09-22 21:19:10 +00:00
obs_source_t *source)
2017-04-19 19:54:15 +00:00
{
2019-09-22 21:19:10 +00:00
const struct audio_output_info *info =
audio_output_get_info(obs->audio.audio);
2017-04-19 19:54:15 +00:00
uint32_t channels = get_audio_channels(info->speakers);
OSStatus stat;
AudioStreamBasicDescription desc = {
.mSampleRate = (Float64)info->samples_per_sec,
.mFormatID = kAudioFormatLinearPCM,
.mFormatFlags = kAudioFormatFlagIsFloat |
2019-09-22 21:19:10 +00:00
kAudioFormatFlagIsPacked,
2017-04-19 19:54:15 +00:00
.mBytesPerPacket = sizeof(float) * channels,
.mFramesPerPacket = 1,
.mBytesPerFrame = sizeof(float) * channels,
.mChannelsPerFrame = channels,
2019-09-22 21:19:10 +00:00
.mBitsPerChannel = sizeof(float) * 8};
2017-04-19 19:54:15 +00:00
2017-06-29 19:01:10 +00:00
monitor->source = source;
2017-04-19 19:54:15 +00:00
monitor->channels = channels;
monitor->buffer_size =
channels * sizeof(float) * info->samples_per_sec / 100 * 3;
monitor->wait_size = monitor->buffer_size * 3;
pthread_mutex_init_value(&monitor->mutex);
2017-06-29 19:01:10 +00:00
const char *uid = obs->audio.monitoring_device_id;
if (!uid || !*uid) {
2017-04-19 19:54:15 +00:00
return false;
}
2017-06-29 19:01:10 +00:00
if (source->info.output_flags & OBS_SOURCE_DO_NOT_SELF_MONITOR) {
obs_data_t *s = obs_source_get_settings(source);
const char *s_dev_id = obs_data_get_string(s, "device_id");
bool match = devices_match(s_dev_id, uid);
obs_data_release(s);
if (match) {
monitor->ignore = true;
return true;
}
}
stat = AudioQueueNewOutput(&desc, buffer_audio, monitor, NULL, NULL, 0,
2019-09-22 21:19:10 +00:00
&monitor->queue);
2017-06-29 19:01:10 +00:00
if (!success(stat, "AudioStreamBasicDescription")) {
2017-04-19 19:54:15 +00:00
return false;
}
if (strcmp(uid, "default") != 0) {
2019-09-22 21:19:10 +00:00
CFStringRef cf_uid = CFStringCreateWithBytes(
NULL, (const UInt8 *)uid, strlen(uid),
kCFStringEncodingUTF8, false);
2017-04-19 19:54:15 +00:00
stat = AudioQueueSetProperty(monitor->queue,
2019-09-22 21:19:10 +00:00
kAudioQueueProperty_CurrentDevice,
&cf_uid, sizeof(cf_uid));
2017-04-19 19:54:15 +00:00
CFRelease(cf_uid);
if (!success(stat, "set current device")) {
return false;
}
}
2019-09-22 21:19:10 +00:00
stat = AudioQueueSetParameter(monitor->queue, kAudioQueueParam_Volume,
1.0);
2017-04-19 19:54:15 +00:00
if (!success(stat, "set volume")) {
return false;
}
for (size_t i = 0; i < 3; i++) {
stat = AudioQueueAllocateBuffer(monitor->queue,
2019-09-22 21:19:10 +00:00
monitor->buffer_size,
&monitor->buffers[i]);
2017-04-19 19:54:15 +00:00
if (!success(stat, "allocation of buffer")) {
return false;
}
circlebuf_push_back(&monitor->empty_buffers,
2019-09-22 21:19:10 +00:00
&monitor->buffers[i],
sizeof(monitor->buffers[i]));
2017-04-19 19:54:15 +00:00
}
if (pthread_mutex_init(&monitor->mutex, NULL) != 0) {
blog(LOG_WARNING, "%s: %s", __FUNCTION__,
2019-09-22 21:19:10 +00:00
"Failed to init mutex");
2017-04-19 19:54:15 +00:00
return false;
}
2019-09-22 21:19:10 +00:00
struct resample_info from = {.samples_per_sec = info->samples_per_sec,
.speakers = info->speakers,
.format = AUDIO_FORMAT_FLOAT_PLANAR};
struct resample_info to = {.samples_per_sec = info->samples_per_sec,
.speakers = info->speakers,
.format = AUDIO_FORMAT_FLOAT};
2017-04-19 19:54:15 +00:00
monitor->resampler = audio_resampler_create(&to, &from);
if (!monitor->resampler) {
blog(LOG_WARNING, "%s: %s", __FUNCTION__,
2019-09-22 21:19:10 +00:00
"Failed to create resampler");
2017-04-19 19:54:15 +00:00
return false;
}
stat = AudioQueueStart(monitor->queue, NULL);
if (!success(stat, "start")) {
return false;
}
monitor->active = true;
return true;
}
static void audio_monitor_free(struct audio_monitor *monitor)
{
if (monitor->source) {
obs_source_remove_audio_capture_callback(
2019-09-22 21:19:10 +00:00
monitor->source, on_audio_playback, monitor);
2017-04-19 19:54:15 +00:00
}
if (monitor->active) {
AudioQueueStop(monitor->queue, true);
}
for (size_t i = 0; i < 3; i++) {
if (monitor->buffers[i]) {
AudioQueueFreeBuffer(monitor->queue,
2019-09-22 21:19:10 +00:00
monitor->buffers[i]);
2017-04-19 19:54:15 +00:00
}
}
if (monitor->queue) {
AudioQueueDispose(monitor->queue, true);
}
audio_resampler_destroy(monitor->resampler);
circlebuf_free(&monitor->empty_buffers);
circlebuf_free(&monitor->new_data);
pthread_mutex_destroy(&monitor->mutex);
}
2017-06-29 19:01:10 +00:00
static void audio_monitor_init_final(struct audio_monitor *monitor)
2017-04-19 19:54:15 +00:00
{
2017-06-29 19:01:10 +00:00
if (monitor->ignore)
return;
obs_source_add_audio_capture_callback(monitor->source,
2019-09-22 21:19:10 +00:00
on_audio_playback, monitor);
2017-04-19 19:54:15 +00:00
}
struct audio_monitor *audio_monitor_create(obs_source_t *source)
{
struct audio_monitor *monitor = bzalloc(sizeof(*monitor));
2017-06-29 19:01:10 +00:00
if (!audio_monitor_init(monitor, source)) {
2017-04-19 19:54:15 +00:00
goto fail;
}
pthread_mutex_lock(&obs->audio.monitoring_mutex);
da_push_back(obs->audio.monitors, &monitor);
pthread_mutex_unlock(&obs->audio.monitoring_mutex);
2017-06-29 19:01:10 +00:00
audio_monitor_init_final(monitor);
2017-04-19 19:54:15 +00:00
return monitor;
fail:
audio_monitor_free(monitor);
bfree(monitor);
return NULL;
}
void audio_monitor_reset(struct audio_monitor *monitor)
{
bool success;
obs_source_t *source = monitor->source;
audio_monitor_free(monitor);
memset(monitor, 0, sizeof(*monitor));
2017-06-29 19:01:10 +00:00
success = audio_monitor_init(monitor, source);
2017-04-19 19:54:15 +00:00
if (success)
2017-06-29 19:01:10 +00:00
audio_monitor_init_final(monitor);
2017-04-19 19:54:15 +00:00
}
void audio_monitor_destroy(struct audio_monitor *monitor)
{
if (monitor) {
audio_monitor_free(monitor);
pthread_mutex_lock(&obs->audio.monitoring_mutex);
da_erase_item(obs->audio.monitors, &monitor);
pthread_mutex_unlock(&obs->audio.monitoring_mutex);
bfree(monitor);
}
}