Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libobs/build/libobs.core-source+.h.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "libobs/obs-source-audio-mixers-fields.c.h"
#include "libobs/obs-source-audio-output-seam.c.h"
#include "libobs/obs-source-audio-pending-speaker-layout-fields.c.h"
#include "libobs/obs-source-audio-render-steps-fields.c.h"
#include "libobs/obs-source-audio-settings-seam.c.h"
#include "libobs/obs-source-audio-volume-apply-fields.c.h"
#include "libobs/obs-source-base-size-fields.c.h"
Expand Down
4 changes: 4 additions & 0 deletions libobs/build/libobs.ported-source-audio-render-steps+.h.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Unity group (BUILD.md): original obs_source_audio_render and its steps for C reference. */
#include "libobs/build/libobs.pch.h"

#include "libobs/obs-source-audio-render-steps.c.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"LIBOBS_PORTED_SOURCES": [
"build/libobs.ported-source-audio-render-steps+.h.c"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# obs-source-audio-render-steps.c.h (#5923): what the audio mix thread
# calls for each source on every tick, with its custom, submix and tick
# steps. Declared in obs-internal.h without EXPORT, so the shim stays hidden.
obs_source_audio_render
172 changes: 172 additions & 0 deletions libobs/obs-source-audio-render-steps-fields.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
#include "libobs/build/libobs.pch.h"

/* Private adapters of the Rust obs_source_audio_render() (obs-internal.h,
* no EXPORT; obs-source::ffi::source_audio_render_steps, #5923), each one
* access of obs-source-audio-render-steps.c.h: the route's reads, the
* submix flag, audio_buf_mutex, audio_pending, audio_ts, audio_mixers and
* the input size, the buffers the callbacks get, the memsets, the peek and
* the memcpy, the two plugin callbacks, obs_source_apply_audio_volume() and
* obs_source_output_audio(). The callers pass a live source; the buffer
* adapters are reached only with its output (or mix) buffers, a mix below
* MAX_AUDIO_MIXES and a channel below MAX_AUDIO_CHANNELS. */

/* The Rust side mirrors the callbacks' buffer structs. */
_Static_assert(MAX_AUDIO_MIXES == 6, "MAX_AUDIO_MIXES");
_Static_assert(MAX_AUDIO_CHANNELS == 8, "MAX_AUDIO_CHANNELS");
_Static_assert(sizeof(struct audio_output_data) == sizeof(float *) * MAX_AUDIO_CHANNELS, "audio_output_data");
_Static_assert(sizeof(struct obs_source_audio_mix) == sizeof(struct audio_output_data) * MAX_AUDIO_MIXES,
"obs_source_audio_mix");

bool obs_source_audio_render_steps_has_output_buffer(const obs_source_t *source);
bool obs_source_audio_render_steps_has_output_buffer(const obs_source_t *source)
{
return source->audio_output_buf[0][0] != NULL;
}

bool obs_source_audio_render_steps_has_audio_render(const obs_source_t *source);
bool obs_source_audio_render_steps_has_audio_render(const obs_source_t *source)
{
return source->info.audio_render != NULL;
}

bool obs_source_audio_render_steps_has_context_data(const obs_source_t *source);
bool obs_source_audio_render_steps_has_context_data(const obs_source_t *source)
{
return source->context.data != NULL;
}

bool obs_source_audio_render_steps_has_audio_mix(const obs_source_t *source);
bool obs_source_audio_render_steps_has_audio_mix(const obs_source_t *source)
{
return source->info.audio_mix != NULL;
}

bool obs_source_audio_render_steps_is_submix(const obs_source_t *source);
bool obs_source_audio_render_steps_is_submix(const obs_source_t *source)
{
return !!(source->info.output_flags & OBS_SOURCE_SUBMIX);
}

void obs_source_audio_render_steps_lock(obs_source_t *source);
void obs_source_audio_render_steps_lock(obs_source_t *source)
{
pthread_mutex_lock(&source->audio_buf_mutex);
}

void obs_source_audio_render_steps_unlock(obs_source_t *source);
void obs_source_audio_render_steps_unlock(obs_source_t *source)
{
pthread_mutex_unlock(&source->audio_buf_mutex);
}

void obs_source_audio_render_steps_set_audio_pending(obs_source_t *source, bool pending);
void obs_source_audio_render_steps_set_audio_pending(obs_source_t *source, bool pending)
{
source->audio_pending = pending;
}

uint64_t obs_source_audio_render_steps_audio_ts(const obs_source_t *source);
uint64_t obs_source_audio_render_steps_audio_ts(const obs_source_t *source)
{
return source->audio_ts;
}

void obs_source_audio_render_steps_set_audio_ts(obs_source_t *source, uint64_t ts);
void obs_source_audio_render_steps_set_audio_ts(obs_source_t *source, uint64_t ts)
{
source->audio_ts = ts;
}

uint32_t obs_source_audio_render_steps_audio_mixers(const obs_source_t *source);
uint32_t obs_source_audio_render_steps_audio_mixers(const obs_source_t *source)
{
return source->audio_mixers;
}

size_t obs_source_audio_render_steps_input_size(const obs_source_t *source);
size_t obs_source_audio_render_steps_input_size(const obs_source_t *source)
{
return source->audio_input_buf[0].size;
}

float *obs_source_audio_render_steps_output_buf(obs_source_t *source, size_t mix, size_t channel);
float *obs_source_audio_render_steps_output_buf(obs_source_t *source, size_t mix, size_t channel)
{
return source->audio_output_buf[mix][channel];
}

float *obs_source_audio_render_steps_mix_buf(obs_source_t *source, size_t channel);
float *obs_source_audio_render_steps_mix_buf(obs_source_t *source, size_t channel)
{
return source->audio_mix_buf[channel];
}

void obs_source_audio_render_steps_clear_output(obs_source_t *source, size_t mix, size_t bytes);
void obs_source_audio_render_steps_clear_output(obs_source_t *source, size_t mix, size_t bytes)
{
memset(source->audio_output_buf[mix][0], 0, bytes);
}

void obs_source_audio_render_steps_clear_mix_buf(obs_source_t *source, size_t bytes);
void obs_source_audio_render_steps_clear_mix_buf(obs_source_t *source, size_t bytes)
{
memset(source->audio_mix_buf[0], 0, bytes);
}

void obs_source_audio_render_steps_peek_input(obs_source_t *source, size_t channel, size_t size);
void obs_source_audio_render_steps_peek_input(obs_source_t *source, size_t channel, size_t size)
{
deque_peek_front(&source->audio_input_buf[channel], source->audio_output_buf[0][channel], size);
}

void obs_source_audio_render_steps_copy_output(obs_source_t *source, size_t mix, size_t channel, size_t size);
void obs_source_audio_render_steps_copy_output(obs_source_t *source, size_t mix, size_t channel, size_t size)
{
memcpy(source->audio_output_buf[mix][channel], source->audio_output_buf[0][channel], size);
}

bool obs_source_audio_render_steps_audio_render(obs_source_t *source, uint64_t *ts,
struct obs_source_audio_mix *audio_data, uint32_t mixers,
size_t channels, size_t sample_rate);
bool obs_source_audio_render_steps_audio_render(obs_source_t *source, uint64_t *ts,
struct obs_source_audio_mix *audio_data, uint32_t mixers,
size_t channels, size_t sample_rate)
{
return source->info.audio_render(source->context.data, ts, audio_data, mixers, channels, sample_rate);
}

bool obs_source_audio_render_steps_audio_mix(obs_source_t *source, uint64_t *ts, struct audio_output_data *audio_data,
size_t channels, size_t sample_rate);
bool obs_source_audio_render_steps_audio_mix(obs_source_t *source, uint64_t *ts, struct audio_output_data *audio_data,
size_t channels, size_t sample_rate)
{
return source->info.audio_mix(source->context.data, ts, audio_data, channels, sample_rate);
}

void obs_source_audio_render_steps_apply_volume(obs_source_t *source, uint32_t mixers, size_t channels,
size_t sample_rate);
void obs_source_audio_render_steps_apply_volume(obs_source_t *source, uint32_t mixers, size_t channels,
size_t sample_rate)
{
obs_source_apply_audio_volume(source, mixers, channels, sample_rate);
}

/* audio_submix()'s hand-off: the planes as the callback left them. */
void obs_source_audio_render_steps_output_audio(obs_source_t *source, const struct audio_output_data *audio_data,
size_t channels, size_t sample_rate, uint64_t ts);
void obs_source_audio_render_steps_output_audio(obs_source_t *source, const struct audio_output_data *audio_data,
size_t channels, size_t sample_rate, uint64_t ts)
{
struct obs_source_audio audio = {0};

for (size_t i = 0; i < channels; i++)
audio.data[i] = (const uint8_t *)audio_data->data[i];

audio.samples_per_sec = (uint32_t)sample_rate;
audio.frames = AUDIO_OUTPUT_FRAMES;
audio.format = AUDIO_FORMAT_FLOAT_PLANAR;
audio.speakers = (enum speaker_layout)channels;
audio.timestamp = ts;

obs_source_output_audio(source, &audio);
}
162 changes: 162 additions & 0 deletions libobs/obs-source-audio-render-steps.c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
#include "libobs/build/libobs.pch.h"

/* obs_source_audio_render() and its three steps, custom_audio_render(),
* audio_submix() and process_audio_source_tick(), what the audio mix thread
* calls for each source on every tick, moved verbatim out of obs-source.c.h
* (#5923). obs_source_audio_render() is declared in obs-internal.h (hidden,
* no EXPORT) for obs-audio.c and the Rust audio_callback(). */

/* Which mixes are cleared before and after the callback, and its timestamp
* and pending result, go through obs-source-audio-render.c, on values. The
* buffers, the callback, audio_buf_mutex, the memsets and
* obs_source_apply_audio_volume() stay here, in the same order; source->audio_mixers is
* read once per mix where it was before, source->audio_ts only after a
* successful callback. */
static void custom_audio_render(obs_source_t *source, uint32_t mixers, size_t channels, size_t sample_rate)
{
struct obs_source_audio_mix audio_data;
bool success;
uint64_t ts;

for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
for (size_t ch = 0; ch < channels; ch++) {
audio_data.output[mix].data[ch] = source->audio_output_buf[mix][ch];
}

if (obs_source_audio_render_custom_clear_before(source->audio_mixers, mixers, mix)) {
memset(source->audio_output_buf[mix][0], 0, sizeof(float) * AUDIO_OUTPUT_FRAMES * channels);
}
}

success = source->info.audio_render(source->context.data, &ts, &audio_data, mixers, channels, sample_rate);
/* The callback may render other sources or submit capture audio. */
pthread_mutex_lock(&source->audio_buf_mutex);
/* ts is passed but not used when the callback failed. */
source->audio_ts = obs_source_audio_render_custom_ts(success, ts);
source->audio_pending = obs_source_audio_render_custom_pending(success);

if (obs_source_audio_render_custom_pending(success) ||
obs_source_audio_render_custom_unmixed(source->audio_ts, mixers)) {
pthread_mutex_unlock(&source->audio_buf_mutex);
return;
}

for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
if (!obs_source_audio_render_custom_in_tick(mixers, mix))
continue;

if (obs_source_audio_render_custom_clear_after(source->audio_mixers, mix)) {
memset(source->audio_output_buf[mix][0], 0, sizeof(float) * AUDIO_OUTPUT_FRAMES * channels);
}
}

obs_source_apply_audio_volume(source, mixers, channels, sample_rate);
pthread_mutex_unlock(&source->audio_buf_mutex);
}

static void audio_submix(obs_source_t *source, size_t channels, size_t sample_rate)
{
struct audio_output_data audio_data;
struct obs_source_audio audio = {0};
bool success;
uint64_t ts;

for (size_t ch = 0; ch < channels; ch++) {
audio_data.data[ch] = source->audio_mix_buf[ch];
}

memset(source->audio_mix_buf[0], 0, sizeof(float) * AUDIO_OUTPUT_FRAMES * channels);

success = source->info.audio_mix(source->context.data, &ts, &audio_data, channels, sample_rate);

if (!success)
return;

for (size_t i = 0; i < channels; i++)
audio.data[i] = (const uint8_t *)audio_data.data[i];

audio.samples_per_sec = (uint32_t)sample_rate;
audio.frames = AUDIO_OUTPUT_FRAMES;
audio.format = AUDIO_FORMAT_FLOAT_PLANAR;
audio.speakers = (enum speaker_layout)channels;
audio.timestamp = ts;

obs_source_output_audio(source, &audio);
}

/* The pending check and whether each mix copies mix 0 or is cleared go
* through obs-source-audio-render.c, on values. audio_buf_mutex,
* deque_peek_front(), the memsets, the memcpys and obs_source_apply_audio_volume() stay
* here, in the same order; the input size is read only with a timestamp, and
* source->audio_mixers once per mix where it was before. */
static inline void process_audio_source_tick(obs_source_t *source, uint32_t mixers, size_t channels, size_t sample_rate,
size_t size)
{
bool audio_submix = !!(source->info.output_flags & OBS_SOURCE_SUBMIX);

pthread_mutex_lock(&source->audio_buf_mutex);

if (obs_source_audio_render_tick_unset(source->audio_ts) ||
obs_source_audio_render_tick_short(source->audio_input_buf[0].size, size)) {
source->audio_pending = true;
pthread_mutex_unlock(&source->audio_buf_mutex);
return;
}

for (size_t ch = 0; ch < channels; ch++)
deque_peek_front(&source->audio_input_buf[ch], source->audio_output_buf[0][ch], size);

for (size_t mix = 1; mix < MAX_AUDIO_MIXES; mix++) {
if (obs_source_audio_render_tick_stop(audio_submix, mix))
break;

if (!obs_source_audio_render_tick_copy(audio_submix, source->audio_mixers, mixers, mix)) {
memset(source->audio_output_buf[mix][0], 0, size * channels);
continue;
}

for (size_t ch = 0; ch < channels; ch++)
memcpy(source->audio_output_buf[mix][ch], source->audio_output_buf[0][ch], size);
}

if (!obs_source_audio_render_tick_volume(audio_submix)) {
source->audio_pending = false;
pthread_mutex_unlock(&source->audio_buf_mutex);
return;
}

if (obs_source_audio_render_tick_clear_main(source->audio_mixers, mixers))
memset(source->audio_output_buf[0][0], 0, size * channels);

obs_source_apply_audio_volume(source, mixers, channels, sample_rate);
source->audio_pending = false;
pthread_mutex_unlock(&source->audio_buf_mutex);
}

/* The route goes through obs-source-audio-render.c, on values. Each field is
* read only where the checks before reached it, as before: the audio thread
* can render a source whose context.data is not set yet (432017b2c). */
void obs_source_audio_render(obs_source_t *source, uint32_t mixers, size_t channels, size_t sample_rate, size_t size)
{
const bool has_buffer = source->audio_output_buf[0][0] != NULL;
const bool custom = has_buffer && source->info.audio_render != NULL;
const bool has_data = custom && source->context.data != NULL;

switch (obs_source_audio_render_route(has_buffer, custom, has_data)) {
case OBS_SOURCE_AUDIO_RENDER_PENDING:
source->audio_pending = true;
return;
case OBS_SOURCE_AUDIO_RENDER_CUSTOM:
custom_audio_render(source, mixers, channels, sample_rate);
return;
case OBS_SOURCE_AUDIO_RENDER_TICK:
default:
break;
}

if (source->info.audio_mix) {
audio_submix(source, channels, sample_rate);
}

process_audio_source_tick(source, mixers, channels, sample_rate, size);
}
Loading
Loading