diff --git a/ci/test_ported_sources.py b/ci/test_ported_sources.py index 489d3c49da..5c4f51446a 100644 --- a/ci/test_ported_sources.py +++ b/ci/test_ported_sources.py @@ -39,6 +39,7 @@ "obs-service-getters.c.h.o", "obs-source-async-pacing.c.h.o", "obs-source-audio-input.c.h.o", + "obs-source-audio-render.c.h.o", "obs-source-audio-timing.c.h.o", "obs-source-color-space.c.h.o", "obs-source-conversion-plan.c.h.o", diff --git a/libobs/build/libobs-ported-source-audio-render.pch.h b/libobs/build/libobs-ported-source-audio-render.pch.h new file mode 100644 index 0000000000..3f27521af1 --- /dev/null +++ b/libobs/build/libobs-ported-source-audio-render.pch.h @@ -0,0 +1,9 @@ +/* Precompiled header (BUILD.md) for the libobs unity group + * libobs.ported-source-audio-render. obs-source-audio-render.c needs only + * its declarations (obs-source-audio-render.h, with stdbool.h, stddef.h and + * stdint.h), not the core, so the C oracle and the C test can compile it on + * its own. The implementation file includes this header and nothing else. */ + +#pragma once + +#include "libobs/obs-source-audio-render.h" diff --git a/libobs/build/libobs.pch.h b/libobs/build/libobs.pch.h index 3840cf5b2d..925837df88 100644 --- a/libobs/build/libobs.pch.h +++ b/libobs/build/libobs.pch.h @@ -144,6 +144,7 @@ #include "obs-source-async-pacing.h" #include "obs-source-audio-input.h" +#include "obs-source-audio-render.h" #include "obs-source-audio-timing.h" #include "obs-source-color-space.h" #include "obs-source-conversion-plan.h" diff --git a/libobs/build/libobs.ported-source-audio-render+.h.c b/libobs/build/libobs.ported-source-audio-render+.h.c new file mode 100644 index 0000000000..d2918457e0 --- /dev/null +++ b/libobs/build/libobs.ported-source-audio-render+.h.c @@ -0,0 +1,5 @@ +/* Unity group (BUILD.md): ported source audio render mix plan. */ + +#include "libobs/build/libobs-ported-source-audio-render.pch.h" + +#include "libobs/obs-source-audio-render.c.h" diff --git a/libobs/build/source-sets.json b/libobs/build/source-sets.json index 3ffd9f4453..4dc5fbf3e3 100644 --- a/libobs/build/source-sets.json +++ b/libobs/build/source-sets.json @@ -47,6 +47,7 @@ "obs-service.h", "obs-source-async-pacing.h", "obs-source-audio-input.h", + "obs-source-audio-render.h", "obs-source-audio-timing.h", "obs-source-color-space.h", "obs-source-conversion-plan.h", @@ -189,6 +190,7 @@ "build/libobs.ported-service-getters+.h.c", "build/libobs.ported-source-async-pacing+.h.c", "build/libobs.ported-source-audio-input+.h.c", + "build/libobs.ported-source-audio-render+.h.c", "build/libobs.ported-source-audio-timing+.h.c", "build/libobs.ported-source-color-space+.h.c", "build/libobs.ported-source-conversion-plan+.h.c", diff --git a/libobs/cmake/rust-exports.map b/libobs/cmake/rust-exports.map index 79d7edcda4..d375ef4ebb 100644 --- a/libobs/cmake/rust-exports.map +++ b/libobs/cmake/rust-exports.map @@ -153,6 +153,21 @@ obs_source_audio_input_mono_needed; obs_source_audio_input_balance; obs_source_audio_input_downmix; + /* obs-source-audio-render.c. obs-source.c calls them inside libobs. Not + * EXPORT. */ + obs_source_audio_render_route; + obs_source_audio_render_custom_clear_before; + obs_source_audio_render_custom_ts; + obs_source_audio_render_custom_pending; + obs_source_audio_render_custom_unmixed; + obs_source_audio_render_custom_in_tick; + obs_source_audio_render_custom_clear_after; + obs_source_audio_render_tick_unset; + obs_source_audio_render_tick_short; + obs_source_audio_render_tick_stop; + obs_source_audio_render_tick_copy; + obs_source_audio_render_tick_volume; + obs_source_audio_render_tick_clear_main; /* obs-source-audio-timing.c. obs-source.c calls them inside libobs. Not * EXPORT. */ obs_source_audio_timing_pre_lock; diff --git a/libobs/cmake/rust-hidden.txt b/libobs/cmake/rust-hidden.txt index ed50537c67..f0fc7115eb 100644 --- a/libobs/cmake/rust-hidden.txt +++ b/libobs/cmake/rust-hidden.txt @@ -153,6 +153,23 @@ obs_source_audio_input_balance_needed obs_source_audio_input_mono_needed obs_source_audio_input_balance obs_source_audio_input_downmix +# obs-source-audio-render.c: obs_source_audio_render(), custom_audio_render() +# and process_audio_source_tick() in obs-source.c call them inside libobs. +# Declared in obs-source-audio-render.h without EXPORT, so the shims stay +# hidden the same way. +obs_source_audio_render_route +obs_source_audio_render_custom_clear_before +obs_source_audio_render_custom_ts +obs_source_audio_render_custom_pending +obs_source_audio_render_custom_unmixed +obs_source_audio_render_custom_in_tick +obs_source_audio_render_custom_clear_after +obs_source_audio_render_tick_unset +obs_source_audio_render_tick_short +obs_source_audio_render_tick_stop +obs_source_audio_render_tick_copy +obs_source_audio_render_tick_volume +obs_source_audio_render_tick_clear_main # obs-source-audio-timing.c: source_output_audio_data() and # reset_audio_data_at_sys_time() in obs-source.c call them inside libobs. # Declared in obs-source-audio-timing.h without EXPORT, so the shims stay diff --git a/libobs/cmake/rust-unexports-macos.txt b/libobs/cmake/rust-unexports-macos.txt index dd80a7ad9c..11dac28483 100644 --- a/libobs/cmake/rust-unexports-macos.txt +++ b/libobs/cmake/rust-unexports-macos.txt @@ -130,6 +130,21 @@ _obs_source_audio_input_balance_needed _obs_source_audio_input_mono_needed _obs_source_audio_input_balance _obs_source_audio_input_downmix +# obs-source-audio-render.c: obs-source.c calls them inside libobs. Not +# EXPORT. +_obs_source_audio_render_route +_obs_source_audio_render_custom_clear_before +_obs_source_audio_render_custom_ts +_obs_source_audio_render_custom_pending +_obs_source_audio_render_custom_unmixed +_obs_source_audio_render_custom_in_tick +_obs_source_audio_render_custom_clear_after +_obs_source_audio_render_tick_unset +_obs_source_audio_render_tick_short +_obs_source_audio_render_tick_stop +_obs_source_audio_render_tick_copy +_obs_source_audio_render_tick_volume +_obs_source_audio_render_tick_clear_main # obs-source-audio-timing.c: obs-source.c calls them inside libobs. Not # EXPORT. _obs_source_audio_timing_pre_lock diff --git a/libobs/obs-source-audio-render.c.h b/libobs/obs-source-audio-render.c.h new file mode 100644 index 0000000000..bc07009dc3 --- /dev/null +++ b/libobs/obs-source-audio-render.c.h @@ -0,0 +1,115 @@ +/****************************************************************************** + Copyright (C) 2023 by Lain Bailey + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#include "libobs/build/libobs-ported-source-audio-render.pch.h" + +/* The audio render mix plan of obs-source.c, moved from + * obs_source_audio_render() (the route), custom_audio_render() (the clears + * around the callback and its result) and process_audio_source_tick() (the + * pending check, the copy or clear of each mix): the decisions, on plain + * values. obs-source.c keeps every field read and write in the same order + * and passes the values it read. Declared in obs-source-audio-render.h + * without EXPORT. In hybrid libobs the Rust port (rust/obs-source, + * audio_render_plan) replaces this file. */ + +enum obs_source_audio_render_route obs_source_audio_render_route(bool has_buffer, bool custom, bool has_data) +{ + if (!has_buffer) + return OBS_SOURCE_AUDIO_RENDER_PENDING; + + if (custom) { + if (!has_data) + return OBS_SOURCE_AUDIO_RENDER_PENDING; + return OBS_SOURCE_AUDIO_RENDER_CUSTOM; + } + + return OBS_SOURCE_AUDIO_RENDER_TICK; +} + +bool obs_source_audio_render_custom_clear_before(uint32_t audio_mixers, uint32_t mixers, size_t mix) +{ + return (audio_mixers & mixers & (1 << mix)) != 0; +} + +uint64_t obs_source_audio_render_custom_ts(bool success, uint64_t ts) +{ + return success ? ts : 0; +} + +bool obs_source_audio_render_custom_pending(bool success) +{ + return !success; +} + +bool obs_source_audio_render_custom_unmixed(uint64_t audio_ts, uint32_t mixers) +{ + return !audio_ts || !mixers; +} + +bool obs_source_audio_render_custom_in_tick(uint32_t mixers, size_t mix) +{ + uint32_t mix_bit = 1 << mix; + + return (mixers & mix_bit) != 0; +} + +bool obs_source_audio_render_custom_clear_after(uint32_t audio_mixers, size_t mix) +{ + uint32_t mix_bit = 1 << mix; + + return (audio_mixers & mix_bit) == 0; +} + +bool obs_source_audio_render_tick_unset(uint64_t audio_ts) +{ + return !audio_ts; +} + +bool obs_source_audio_render_tick_short(size_t input_size, size_t size) +{ + return input_size < size; +} + +bool obs_source_audio_render_tick_stop(bool submix, size_t mix) +{ + return submix && mix > 1; +} + +bool obs_source_audio_render_tick_copy(bool submix, uint32_t audio_mixers, uint32_t mixers, size_t mix) +{ + uint32_t mix_and_val; + + /* A submix source fills mix 1 from bit 0 of its mixers. */ + if (submix) { + mixers = 1; + mix_and_val = 1; + } else { + mix_and_val = (1 << mix); + } + + return !((audio_mixers & mix_and_val) == 0 || (mixers & mix_and_val) == 0); +} + +bool obs_source_audio_render_tick_volume(bool submix) +{ + return !submix; +} + +bool obs_source_audio_render_tick_clear_main(uint32_t audio_mixers, uint32_t mixers) +{ + return (audio_mixers & 1) == 0 || (mixers & 1) == 0; +} diff --git a/libobs/obs-source-audio-render.h b/libobs/obs-source-audio-render.h new file mode 100644 index 0000000000..14fce0267b --- /dev/null +++ b/libobs/obs-source-audio-render.h @@ -0,0 +1,124 @@ +/****************************************************************************** + Copyright (C) 2023 by Lain Bailey + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#pragma once + +#include +#include +#include + +/* The audio render mix plan of obs-source.c, on plain values + * (obs-source-audio-render.c): which way obs_source_audio_render() takes, + * and which mixes custom_audio_render() and process_audio_source_tick() + * clear, copy and scale. + * + * - obs_source_audio_render_route(): pending without an output buffer, or + * with an audio_render callback but no data yet (a source still being + * created); the callback with data; else the tick; + * - custom_audio_render(), around the plugin's audio_render callback: + * - obs_source_audio_render_custom_clear_before(): a mix in both the + * source's mixers and the tick's is cleared before the callback; + * - obs_source_audio_render_custom_ts() and _custom_pending(): the + * callback's timestamp when it succeeds, else 0 and pending; + * - obs_source_audio_render_custom_unmixed(): with no timestamp or no + * tick mixers, nothing is cleared or scaled after the callback; + * - obs_source_audio_render_custom_in_tick() and _custom_clear_after(): + * a mix in the tick's mixers but not the source's is cleared after it; + * - process_audio_source_tick(), on the buffered input: + * - obs_source_audio_render_tick_unset() and _tick_short(): pending with + * no timestamp or less input than the tick needs; + * - obs_source_audio_render_tick_stop() and _tick_copy(): mixes 1 to 5 + * copy mix 0 when in both masks, else are cleared; a submix source + * (OBS_SOURCE_SUBMIX) does mix 1 only, as if both masks were 1; + * - obs_source_audio_render_tick_volume() and _tick_clear_main(): a + * submix source stops there; any other clears mix 0 unless it is in + * both masks, then scales by its volume. + * + * obs-source.c keeps every read and write of the source's fields, in the + * same order and under the same conditions, and passes the values it read: + * the output buffer, the callbacks and context.data only where the checks + * before reached them (the audio thread can render a source still being + * created), audio_ts and the input size under audio_buf_mutex, and + * audio_mixers (set by other threads, without a lock) once per mix where it + * was read. It keeps audio_buf_mutex, deque_peek_front(), every memset and + * memcpy, the plugin callbacks, audio_submix() and apply_audio_volume(). + * None of the functions takes a lock, calls into libobs, touches memory or + * keeps state. + * + * Hidden in libobs: not EXPORT. */ + +/* Which way obs_source_audio_render() takes. */ +enum obs_source_audio_render_route { + /* no output buffer, or a custom render without data: pending */ + OBS_SOURCE_AUDIO_RENDER_PENDING = 0, + /* the source's audio_render callback (custom_audio_render()) */ + OBS_SOURCE_AUDIO_RENDER_CUSTOM = 1, + /* the buffered input (audio_submix() first when the source has an + * audio_mix callback, then process_audio_source_tick()) */ + OBS_SOURCE_AUDIO_RENDER_TICK = 2, +}; + +/* PENDING without has_buffer; with custom, CUSTOM when has_data, else + * PENDING; else TICK. The caller reads custom only with has_buffer and + * has_data only with custom. */ +enum obs_source_audio_render_route obs_source_audio_render_route(bool has_buffer, bool custom, bool has_data); + +/* (audio_mixers & mixers & (1 << mix)) != 0: custom_audio_render() clears + * the mix before the callback. mix is below MAX_AUDIO_MIXES (the C shifts + * an int by it). */ +bool obs_source_audio_render_custom_clear_before(uint32_t audio_mixers, uint32_t mixers, size_t mix); + +/* success ? ts : 0, the audio_ts the callback leaves. ts is ignored when + * the callback failed (it may not have written it). */ +uint64_t obs_source_audio_render_custom_ts(bool success, uint64_t ts); + +/* !success: the source is pending after the callback. */ +bool obs_source_audio_render_custom_pending(bool success); + +/* !audio_ts || !mixers: after a successful callback, nothing is cleared or + * scaled. */ +bool obs_source_audio_render_custom_unmixed(uint64_t audio_ts, uint32_t mixers); + +/* (mixers & (1 << mix)) != 0: the clear after the callback looks at the + * mix. */ +bool obs_source_audio_render_custom_in_tick(uint32_t mixers, size_t mix); + +/* (audio_mixers & (1 << mix)) == 0: a mix of the tick that the source is + * not on is cleared after the callback. */ +bool obs_source_audio_render_custom_clear_after(uint32_t audio_mixers, size_t mix); + +/* !audio_ts: no input timestamp yet, the tick is pending. */ +bool obs_source_audio_render_tick_unset(uint64_t audio_ts); + +/* input_size < size: not enough buffered input (in bytes of channel 0), + * the tick is pending. */ +bool obs_source_audio_render_tick_short(size_t input_size, size_t size); + +/* submix && mix > 1: a submix source fills mix 1 only. */ +bool obs_source_audio_render_tick_stop(bool submix, size_t mix); + +/* Whether mix copies mix 0 (else it is cleared): in both audio_mixers and + * mixers; for a submix source, whether audio_mixers has bit 0 (the C sets + * both masks to 1 there). */ +bool obs_source_audio_render_tick_copy(bool submix, uint32_t audio_mixers, uint32_t mixers, size_t mix); + +/* !submix: the tick goes on to mix 0 and the volume. */ +bool obs_source_audio_render_tick_volume(bool submix); + +/* (audio_mixers & 1) == 0 || (mixers & 1) == 0: mix 0 is cleared before + * the volume. */ +bool obs_source_audio_render_tick_clear_main(uint32_t audio_mixers, uint32_t mixers); diff --git a/libobs/obs-source.c.h b/libobs/obs-source.c.h index 88a6af02e4..5437dbc4bd 100644 --- a/libobs/obs-source.c.h +++ b/libobs/obs-source.c.h @@ -4862,6 +4862,12 @@ static void apply_audio_volume(obs_source_t *source, uint32_t mixers, size_t cha } } +/* 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 + * 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; @@ -4873,7 +4879,7 @@ static void custom_audio_render(obs_source_t *source, uint32_t mixers, size_t ch audio_data.output[mix].data[ch] = source->audio_output_buf[mix][ch]; } - if ((source->audio_mixers & mixers & (1 << mix)) != 0) { + 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); } } @@ -4881,21 +4887,21 @@ static void custom_audio_render(obs_source_t *source, uint32_t mixers, size_t ch 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); - source->audio_ts = success ? ts : 0; - source->audio_pending = !success; + /* 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 (!success || !source->audio_ts || !mixers) { + 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++) { - uint32_t mix_bit = 1 << mix; - - if ((mixers & mix_bit) == 0) + if (!obs_source_audio_render_custom_in_tick(mixers, mix)) continue; - if ((source->audio_mixers & mix_bit) == 0) { + 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); } } @@ -4934,6 +4940,11 @@ static void audio_submix(obs_source_t *source, size_t channels, size_t sample_ra 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 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) { @@ -4941,7 +4952,8 @@ static inline void process_audio_source_tick(obs_source_t *source, uint32_t mixe pthread_mutex_lock(&source->audio_buf_mutex); - if (!source->audio_ts || source->audio_input_buf[0].size < size) { + 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; @@ -4951,17 +4963,10 @@ static inline void process_audio_source_tick(obs_source_t *source, uint32_t mixe 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++) { - uint32_t mix_and_val = (1 << mix); - - if (audio_submix) { - if (mix > 1) - break; - - mixers = 1; - mix_and_val = 1; - } + if (obs_source_audio_render_tick_stop(audio_submix, mix)) + break; - if ((source->audio_mixers & mix_and_val) == 0 || (mixers & mix_and_val) == 0) { + 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; } @@ -4970,13 +4975,13 @@ static inline void process_audio_source_tick(obs_source_t *source, uint32_t mixe memcpy(source->audio_output_buf[mix][ch], source->audio_output_buf[0][ch], size); } - if (audio_submix) { + if (!obs_source_audio_render_tick_volume(audio_submix)) { source->audio_pending = false; pthread_mutex_unlock(&source->audio_buf_mutex); return; } - if ((source->audio_mixers & 1) == 0 || (mixers & 1) == 0) + if (obs_source_audio_render_tick_clear_main(source->audio_mixers, mixers)) memset(source->audio_output_buf[0][0], 0, size * channels); apply_audio_volume(source, mixers, channels, sample_rate); @@ -4984,20 +4989,25 @@ static inline void process_audio_source_tick(obs_source_t *source, uint32_t mixe 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) { - if (!source->audio_output_buf[0][0]) { + 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; - } - - if (source->info.audio_render) { - if (!source->context.data) { - 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) { diff --git a/rust/README.md b/rust/README.md index 8a408f0429..f22219ed27 100644 --- a/rust/README.md +++ b/rust/README.md @@ -22,7 +22,7 @@ are in [docs/rust-port/naming.md](../docs/rust-port/naming.md#repository-layout- | `obs-missing-files/` | Port of `libobs/obs-missing-files.c.h` | | `obs-output/` | Ports of `libobs/obs-output-delay.c.h` (the stream delay), `libobs/obs-output-interleave.c.h` (the encoded packet interleaver), `libobs/obs-output-caption-text.c.h` (the caption text wrap) , `libobs/obs-output-protocols.c.h` (the output protocol-list match), `libobs/obs-encoder-roi.c.h` (the encoder region of interest checks and scaling), `libobs/obs-output-video-checks.c.h` (the output video size, conversion and capture checks), `libobs/obs-encoder-size.c.h` (the encoder size and info choices) and `libobs/obs-service-getters.c.h` (the service capability, connect-info, settings, properties and identity functions, and its lifecycle, references and activation) | | `obs-scene/` | Ports of the units extracted from `libobs/obs-scene.c`: `libobs/obs-scene-bounds.c.h`, `libobs/obs-scene-blend.c.h`, `libobs/obs-scene-coords.c.h`, `libobs/obs-scene-group-transform.c.h`, `libobs/obs-scene-audio-actions.c.h` (the core of the scene item visibility audio ramp), `libobs/obs-scene-extent.c.h` (the content extent of a scene or group), `libobs/obs-scene-crop.c.h` (the scene item crop arithmetic), `libobs/obs-scene-render-plan.c.h` (the per-item render decisions of `render_item` and `render_item_texture`), and `libobs/obs-scene-item-names.c.h` (the scene item save/load names and coordinate keys), and `libobs/obs-scene-audio-window.c.h` (the audio window decisions of `scene_audio_render`) | -| `obs-source/` | Ports of the units extracted from `libobs/obs-source.c`, `libobs/obs-source-deinterlace.c` and `libobs/obs-source-transition.c`: so far `libobs/obs-source-async-pacing.c.h` (the async video frame pacing of `get_closest_frame()`), `libobs/obs-source-audio-input.c.h` (the audio input steps of `process_audio()` and `reset_resampler()`), `libobs/obs-source-audio-timing.c.h` (the audio timestamp placement of `source_output_audio_data()`), `libobs/obs-source-color-space.c.h` (the colour space query routing of `obs_source_get_color_space()`), `libobs/obs-source-conversion-plan.c.h` (the async video GPU conversion plan), `libobs/obs-source-deinterlace-draw.c.h` (the deinterlace draw decisions), `libobs/obs-source-deinterlace-pacing.c.h` (the 2x deinterlace frame pacing), `libobs/obs-source-draw-plan.c.h` (the source draw decisions of `rotate_async_video(), `libobs/obs-source-frame-copy.c.h` (the async frame copy plan and cache decisions)`, `obs_source_render_async_video()` and `source_render()`), `libobs/obs-source-ptm-gate.c.h` (the push-to-mute and push-to-talk gate of `source_muted()`), `libobs/obs-source-render-route.c.h` (the source and filter render routing), `libobs/obs-source-size.c.h` (the size query routing of `obs_source_get_width()` and `obs_source_get_height()`), `libobs/obs-source-transition-clock.c.h` (the transition clock and fit of `libobs/obs-source-transition.c.h`), `libobs/obs-source-transition-render.c.h` (the transition render and audio steps of `libobs/obs-source-transition.c.h`) and `libobs/obs-source-volume.c.h` (the volume snap of `get_source_volume()` and the apply decisions of `apply_audio_volume()`) | +| `obs-source/` | Ports of the units extracted from `libobs/obs-source.c`, `libobs/obs-source-deinterlace.c` and `libobs/obs-source-transition.c`: so far `libobs/obs-source-async-pacing.c.h` (the async video frame pacing of `get_closest_frame()`), `libobs/obs-source-audio-input.c.h` (the audio input steps of `process_audio()` and `reset_resampler()`), `libobs/obs-source-audio-render.c.h` (the audio render mix plan of `obs_source_audio_render()`, `custom_audio_render()` and `process_audio_source_tick()`), `libobs/obs-source-audio-timing.c.h` (the audio timestamp placement of `source_output_audio_data()`), `libobs/obs-source-color-space.c.h` (the colour space query routing of `obs_source_get_color_space()`), `libobs/obs-source-conversion-plan.c.h` (the async video GPU conversion plan), `libobs/obs-source-deinterlace-draw.c.h` (the deinterlace draw decisions), `libobs/obs-source-deinterlace-pacing.c.h` (the 2x deinterlace frame pacing), `libobs/obs-source-draw-plan.c.h` (the source draw decisions of `rotate_async_video()`, `obs_source_render_async_video()` and `source_render()`), `libobs/obs-source-frame-copy.c.h` (the async frame copy plan and cache decisions), `libobs/obs-source-ptm-gate.c.h` (the push-to-mute and push-to-talk gate of `source_muted()`), `libobs/obs-source-render-route.c.h` (the source and filter render routing), `libobs/obs-source-size.c.h` (the size query routing of `obs_source_get_width()` and `obs_source_get_height()`), `libobs/obs-source-transition-clock.c.h` (the transition clock and fit of `libobs/obs-source-transition.c.h`), `libobs/obs-source-transition-render.c.h` (the transition render and audio steps of `libobs/obs-source-transition.c.h`) and `libobs/obs-source-volume.c.h` (the volume snap of `get_source_volume()` and the apply decisions of `apply_audio_volume()`) | | `obs-audio-monitoring/` | Ports of `libobs/audio-monitoring/*` (so far `pulse/pulseaudio-stream-flags.c.h` and `pulse/pulseaudio-monitor-format.c.h`) | | `obs-audio-controls/` | Port of `libobs/obs-audio-controls.c.h` | | `obs-core/` | Ports of the units extracted from `libobs/obs.c.h`, `libobs/obs-module.c.h` and `libobs/obs-audio.c.h`: so far `libobs/obs-video-plan.c.h` (the video output plan), `libobs/obs-context-names.c.h` (the context names), `libobs/obs-source-load.c.h` (the source load migration and value rules), `libobs/obs-module-paths.c.h` (the module search paths), `libobs/obs-module-names.c.h` (module names and files), `libobs/obs-module-locale.c.h` (the module locale files), `libobs/obs-module-source-rules.c.h` (the source registration rules), `libobs/obs-module-type-rules.c.h` (the output, encoder and service registration rules), `libobs/obs-audio-mix.c.h` (the audio tick arithmetic); of `libobs/util/source-profiler.c.h` (per-source tick and render timing), which reads core state; of `libobs/obs-view-api.c.h` (views); and of `libobs/obs-video-copy-plan.c.h` (the output frame copy plan and scale effect choice, from `libobs/obs-video.c.h`); and of `libobs/obs-video-output-step.c.h` (the per-frame video output step decisions, from `libobs/obs-video.c.h`) | @@ -161,6 +161,7 @@ and compares their exported symbols. `OBS_LIBOBS_BACKEND=C` is temporary rollbac | `libobs/obs-scene-item-names.c.h` | `obs-scene::item_names` | Swapped in hybrid `libobs` (group `libobs.ported-scene-item-names`). The name tables and the coordinate-key choice of `scene_load_item()` and `scene_save_item()`, one hidden function each: the value a saved scale filter, blend method or blend type name loads as (`obs_scene_item_scale_filter_from_name` and the two others: the first known name, in the C's order, that the compare finds equal, else Disable, Default or Normal; a `NULL` name is the default without a compare); the name each value is saved under (a `&'static CStr`, with the fallbacks `disable`, `default` and `normal` for any other value); which keys the position and scale, and the bounds, load from (relative, absolute converted, or absolute as they are); and whether the relative keys are saved. `obs-scene.c.h` keeps every `obs_data_*` call in its order, the coordinate conversions (`obs-scene-coords.c.h`), the source lookup, the transitions, the private settings and the crop clamp of #1840, and stores the results; it now asks `obs_data_has_user_value()` of `pos_rel`, `scale_rel` and `scale_ref` all three before the choice (a plain lookup; the old `&&` chain stopped early). The loaders take the compare as a parameter and `obs-scene.c.h` passes libobs's own `astrcmpi`, so the case rules stay one implementation (in the hybrid, `obs-util`'s), called the same number of times in the same order, and `obs-scene` still links no `obs-util` object (calling `obs-util` from the crate broke every `obs-scene` `_rust` test link on `libuuid`). The enum values cross as `int`; `obs-scene.c.h` checks the mirrored constants at compile time. Kept, characterized, not endorsed: each table knows only its own names; numbers are names, not values; the bounds load from `bounds_rel` without the position's relative keys. `test_scene_item_names_steps` compiles the C into itself, and `test_scene_item_names_steps_rust` runs the same test source against `libobs_rust-static` (after `OBS::libobs`, for `astrcmpi`); `test_scene_blend`, `test_scene_item_crop_load`, `test_scene_duplicate_scale_ref` and `test_saved_transition` see the swap through the hybrid. Tier 3 compares the values with each library's `astrcmpi` and the compares asked of one recording compare, exhaustively over the enums, the flags and every string of up to two bytes, and with proptests over arbitrary byte strings. No intentional differences. | | `libobs/obs-source-async-pacing.c.h` | `obs-source::async_pacing` | Swapped in hybrid `libobs` (group `libobs.ported-source-async-pacing`). The async video frame pacing of `get_closest_frame()` and `ready_async_frame()`, which picks the frame `async_tick()` shows for every async source without 2x deinterlacing (media, cameras, capture cards), as one function on the queue's timestamps: `obs_source_async_pacing()` returns how many frames to drop from the front, whether to take the front frame, and the new `last_frame_ts`. The rules: with `last_frame_ts` 0 the oldest frame (buffered or not); unbuffered the newest; buffered, the oldest after a timing jump (more than `MAX_TS_VAR` either way, Rust's own copy of `frame_out_of_bounds()`), else the catch-up walk: the clock advances by the canvas time since the last tick, drops the frames it passed, keeps a passed frame while the next is due within 2 ms (only once a frame is passed), never drops the last frame and re-anchors when the next frame jumps more than `MAX_TS_VAR`; then a frame taken with `last_frame_ts` 0 seeds it. It is declared in `obs-source-async-pacing.h` without `EXPORT`, so the shim stays hidden. `obs-source.c.h` keeps the `async_frames` queue, `da_erase()`, `remove_async_frame()`, the async cache, `async_mutex` and the frame pointers: it copies the timestamps into a stack array of `MAX_ASYNC_FRAMES` (a heap buffer past it, which producers on several threads can reach), erases the dropped frames at index 0 with `remove_async_frame()` after each, as the C always did, stores `last_frame_ts` and takes the front frame. Every sum and difference wraps as the C's `uint64_t` does. Kept, characterized, not endorsed: a frame at timestamp 0 seeds `last_frame_ts` 0; the first-frame rule shows the oldest frame of an unbuffered source; a timestamp before the passed frame's re-anchors; a canvas clock going backwards moves the source clock back (below 0, to near `UINT64_MAX`); a re-anchoring tick loses its canvas advance; a re-anchor onto 0 re-seeds; at equal rates on exact clocks frames come a tick late. Intentional difference: a null `timestamps` with a count above 0 is an empty queue (libobs never passes one). `test_async_frame_pacing_steps` compiles the C into itself against a model queue, and `test_async_frame_pacing_steps_rust` runs the same test source against `libobs_rust-static`; `test_media_restart_stale_frame`, `test_media_restart_first_frame`, `test_async_delay_audio` and the v4l2 tests see the swap through the real libobs. Tier 3 compares the result bytes over the special values and full-range proptests of 1 to 30 frames, and sequences of 23.976, 25, 29.97, 30, 50 and 60 frames a second into 30 and 60 fps, buffered and unbuffered, with jitter, jumps, resets, stalls and a full queue, through a model of the caller (the queue, `cache_video()`'s limit, the erases and the stores), every call and the state after every tick. | | `libobs/obs-source-audio-input.c.h` | `obs-source::audio_input` | Swapped in hybrid `libobs` (group `libobs.ported-source-audio-input`). The audio input steps of `process_audio()` and `reset_resampler()`, as seven functions: `obs_source_audio_input_discontinuous()` (with a resampler and `next_resample_in_ts` set, a packet 70 ms or more from it either way restarts the resampler, #546), `obs_source_audio_input_reset_needed()` (the packet's rate, format or speakers differ, the clock drift compensation flag changed, or the discontinuity), `obs_source_audio_input_resampler_needed()` (none only for the output's format with the compensation off: the clock drift fix), the balance gate `obs_source_audio_input_balance_needed()` (an output that is not mono, a stereo source, a balance above `0.51f` or below `0.49f`) and the Force Mono gate `obs_source_audio_input_mono_needed()`, and the two loops on a plain `float **`: `obs_source_audio_input_balance()` (the sine, square and linear laws; any other law reads nothing) and `obs_source_audio_input_downmix()` (each plane added into the first in turn, times the `f32` reciprocal of the channel count, copied back). They are declared in `obs-source-audio-input.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps the field reads and stores, `next_resample_in_ts` (it computes it and passes it in), the resampler calls, `copy_audio_data()` and its allocation, the clock drift filter and `audio_resampler_set_compensation_error()`; it loads the atomic compensation flag and passes a `bool`. The formats cross as `struct obs_source_audio_input_format` by value (mirrored); `obs-source.c.h` checks the threshold, `SPEAKERS_STEREO`, `OBS_SOURCE_FLAG_FORCE_MONO`, the law values and the enum sizes, and the C file checks that `M_PI` is a double. The sine law's gains keep the C's casts: `(1.0f32 - balance)` widened to `f64`, times the `f64` `PI / 2.0`, rounded to `f32`, then `f32::sin`, which is the platform's `sinf`; Tier 3 matches the C bit for bit for all 2^32 balances on x86_64 Linux. The shims read what the C reads: nothing for zero frames or an unknown law, planes 0 and 1 for a law, planes 0 to `channels - 1` for the downmix (plane 0 alone for 0 channels), two at a time, with no allocation. Kept, characterized, not endorsed: the distance is not modular; a packet of all zeros against a zeroed `sample_info` resets nothing; a NaN balance is not balanced, and balances outside 0 to 1 (only the API sets them) give negative gains, gains above 1 and NaN; the downmix of one channel scales by 1 and of none scales the first plane by +inf. Intentional difference: a null `data`, or a null plane the C would read, does nothing (the C dereferences it; libobs passes `source->audio_data.data`). Where one multiply or add has two NaN operands, which NaN comes out depends on the compiler's operand order, in C builds too (-O0 and -O2 differ), so Tier 3 asks for a NaN there and the exact bits everywhere else. `test_source_audio_input_steps` compiles the C into itself, and `test_source_audio_input_steps_rust` runs the same test source against `libobs_rust-static`; `test_source_audio_balance_mono` (the gates through an audio capture callback), `test_audio_restart_resampler_tail` and `test_audio_clock_drift` see the swap through the real libobs. Tier 3 compares the gates over every combination of their flags and special values, proptests over full-range `u64` timestamps, any formats and balances drawn from bit patterns, and every sample's bits of the laws and the downmix over random samples, frame counts and 0 to 8 channels. | +| `libobs/obs-source-audio-render.c.h` | `obs-source::audio_render_plan` | Swapped in hybrid `libobs` (group `libobs.ported-source-audio-render`). The audio render mix plan of `obs_source_audio_render()`, `custom_audio_render()` and `process_audio_source_tick()`, which decide for every audio source on every tick of the mix which mixes are cleared, copied and scaled, as thirteen functions on plain values: `obs_source_audio_render_route()` (pending without an output buffer, or for a custom render without data, 432017b2c and cf0a5e5c9; the custom render; else the tick), `_custom_clear_before()` (a mix in both masks is cleared before the plugin's `audio_render` callback), `_custom_ts()` and `_custom_pending()` (its timestamp when it succeeds, else 0 and pending), `_custom_unmixed()` (no timestamp or no tick mixers: nothing after it), `_custom_in_tick()` and `_custom_clear_after()` (a mix of the tick the source is not on is cleared after it, d6ac45702), `_tick_unset()` and `_tick_short()` (pending with no timestamp or less input than the tick, #130), `_tick_stop()` and `_tick_copy()` (mixes 1 to 5 copy mix 0 when in both masks, else are cleared; a submix source, `OBS_SOURCE_SUBMIX`, does mix 1 only, from bit 0 of its mixers), and `_tick_volume()` and `_tick_clear_main()` (a submix source stops there; any other clears mix 0 unless in both masks, then scales). They are declared in `obs-source-audio-render.h` without `EXPORT`, so the shims stay hidden. The plan is one function per decision, not one call per tick, because `obs-source.c.h` keeps each read where it was: `audio_mixers` (set by other threads without a lock) once per mix before and after the callback, the input size only with a timestamp, `audio_ts` only after a successful callback, and the output buffer, `audio_render` and `context.data` only where the checks before reached them. It keeps `audio_buf_mutex`, `deque_peek_front()`, every memset and memcpy, the plugin callbacks, `audio_submix()` and `apply_audio_volume()`. No pointer crosses; the route goes back as the C enum's value; the callback's `ts` crosses by value also when the callback failed and is not looked at. Kept, characterized, not endorsed: tick mixers past the six mixes (0x40 and up) are not "no mixers", so a custom render's clears look at each mix and its volume runs; a submix source keeps the peeked input in mix 0 even when it is on no mix, and fills mix 1 whatever the tick's mixers. Intentional difference: a mix of 31 or more for the shifting decisions, where the C's `int` shift is undefined, names bit 31 and then no mix (libobs passes a mix below 6). `test_source_audio_render_steps` compiles the C into itself, and `test_source_audio_render_steps_rust` runs the same test source against `libobs_rust-static`; `test_source_volume_mix`, `test_source_audio_ranges`, `test_audio_capture_mix_race`, `test_group_hide_transition_audio` and the transition audio tests see the swap through the real libobs. Tier 3 compares the oracle, the shims and the core: every mask decision over every pair of the low seven mask bits with and without the high ones and every mix up to 30, the submix copy and the stop over any index, the timestamp, pending and size decisions over every pair of 30 special values, the three callers copied onto a stand-in source over every combination of seven flags with every pair of the low seven mask bits and edge timestamps, masks, sizes and channels (the recorded lock, peeks, memsets, memcpys, callbacks and volume, `audio_ts` and `audio_pending`), and full-range proptests. | | `libobs/obs-source-audio-timing.c.h` | `obs-source::audio_timing` | Swapped in hybrid `libobs` (group `libobs.ported-source-audio-timing`). The audio timestamp placement of `source_output_audio_data()`, in two pure steps around `audio_buf_mutex`: `obs_source_audio_timing_pre_lock()` (the direct timestamp check, then reset, jump, smooth with the drift error and the async decoupled re-anchor, or the log, and `next_audio_ts_min`) and `obs_source_audio_timing_under_lock()` (append or place, the reset when the system time jumped and the audio was not continuous, #522, the sync and resampler offsets, `next_audio_sys_ts_min`, and "the sync offset changed, so place", #333), plus `obs_source_audio_timing_buffer_start()` (where `reset_audio_data_at_sys_time()` starts an emptied buffer, #286). They are declared in `obs-source-audio-timing.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps the field reads and the stores, in their order (the volatile `timing_set` and `timing_adjust` included), the lock, `handle_ts_jump()`, `audio_resampler_set_compensation_error()`, `conv_frames_to_time()` (it passes the packet's duration, so nothing in the port divides), the deque, the signal, the muting and the log lines. The shims read the struct's input fields and write its output fields one at a time, so the inputs and the padding keep their bytes. Every sum and difference wraps as the C's `uint64_t` does; the drift error is `cast_signed()` and the sync offset `cast_unsigned()`, the C's conversions. The crate depends on no other crate. Kept, characterized, not endorsed: a timestamp ahead of the clock gives a `timing_adjust` near `u64::MAX`; `timestamp + timing_adjust`, the packet's end, a negative sync offset larger than the packet's system time and a resampler offset larger than it wrap; the distance is not modular; a direct timestamp more than 2 s from the expected one is logged, not a jump. Intentional difference: a null struct pointer does nothing (the C dereferences it; libobs passes one on its stack). `test_source_audio_timing_steps` compiles the C into itself, and `test_source_audio_timing_steps_rust` runs the same test source against `libobs_rust-static` (the first `_rust` variant since `test_scene_bounds_place_rust`: the `obs-source` member links on its own); `test_async_delay_audio`, `test_audio_sync_offset_change`, `test_media_sync_offset_restart`, `test_audio_restart_resampler_tail` and `test_audio_clock_drift` see the swap through the real libobs. Tier 3 compares every byte of the step structs (padding included, from a random fill) over the special values with every flag, proptests of each step, and 2-50-packet sequences through a model of the caller, with the written-back state. | | `libobs/obs-source-color-space.c.h` | `obs-source::color_space` | Swapped in hybrid `libobs` (group `libobs.ported-source-color-space`). The colour space query routing of `obs_source_get_color_space()`, as two functions: `obs_source_color_space_route()` (which source answers: a non-filter without video asks its filter parent; a filter without video asks its target, #1493; a source without data, or disabled, asks its target; an async source picks; anything else asks its callback, in that order) and `obs_source_color_space_pick()` (the async pick: the first preferred space equal to the frame's space, else the last one, else the frame's space). They are declared in `obs-source-color-space.h` without `EXPORT`, so the shims stay hidden. `obs-source.c.h` keeps `data_valid()`, reads `info.type`, `info.output_flags`, `context.data != NULL`, `enabled`, `filter_parent != NULL` and `filter_target != NULL` into `struct obs_source_color_space_node` (passed by value, mirrored), and keeps the recursion into the parent or target, `convert_video_space()` of the async frame (it passes the frame's space) and the `video_get_color_space` callback. No source pointer crosses. Colour spaces cross as `int`; `obs-source.c.h` checks that `enum gs_color_space` and `enum obs_source_type` are `int`-sized and that the header's filter type and flags match `obs-source.h`. Only `OBS_SOURCE_TYPE_FILTER` is a filter and only the VIDEO and ASYNC bits count. Kept, characterized, not endorsed: a filter flagged async picks unless the earlier rules send it to its target; with no data and nowhere to go the route is the callback or the pick (`data_valid()` keeps libobs from getting there); a value outside the enum in the preferred list is compared and returned as it is. Intentional difference: a null preferred list with a count above 0 is read as empty (the C reads through the null pointer; libobs passes `NULL` only with a count of 0). `test_source_color_space_steps` compiles the C into itself, and `test_source_color_space_steps_rust` runs the same test source against `libobs_rust-static`; `test_filter_color_space` (the HDR Tone Mapping chains of #1493) sees the swap through the real libobs. Tier 3 compares the route over every row of the characterization with eight types and sixteen sets of other flags, a proptest over any node, the pick over every list of 0 to 5 values from -1..=4 and a proptest over lists of 0 to 8 arbitrary `i32` values. | | `libobs/obs-source-conversion-plan.c.h` | `obs-source::conversion_plan` | Swapped in hybrid `libobs` (group `libobs.ported-source-conversion-plan`). The async video GPU conversion plan of `obs-source.c`, as one function: `obs_source_conversion_plan_fill()` takes the frame's format, range, transfer and size and fills `struct obs_source_conversion_plan` (mirrored): the convert type (`get_convert_type()`), the channel count and each plane's width, height and texture format (the nineteen `set_*_sizes()` of `init_gpu_conversion()`; the RGB limited plane takes `convert_video_format()`'s format, RGBA16F for PQ and HLG), the technique of `format_conversion.effect` (`select_conversion_technique()`) and the linear flag (`need_linear_output()`). It is declared in `obs-source-conversion-plan.h` without `EXPORT`, so the shim stays hidden. `obs-source.c.h` keeps `set_async_texture_size()` (the change check, the textures and texrenders, the graphics context), `upload_raw_frame()`, `update_async_texrender()` and its shader parameters, the `convert_video_format()` inline of `obs-internal.h` for its other callers, and the two asserts for a frame that needs no conversion; `get_convert_type()`, `select_conversion_technique()` and `need_linear_output()` keep their signatures and read the plan, and `init_gpu_conversion()` copies it into `async_convert_width`, `async_convert_height` and `async_texture_formats` for the plan's planes, then `async_channel_count`. The format and transfer cross as `int`, so values outside the enums take the C's `default:` paths (no conversion, no technique); the transfer is the frame's `uint8_t`, so it counts modulo 256. The technique crosses as a pointer to a `c"..."` literal of the core (a `&'static CStr`), or null: nothing is allocated. Kept, characterized, not endorsed: the sizes wrap in `u32` as the C's do near `UINT32_MAX` (`(width + 1) / 2` is 0 at `UINT32_MAX`, `width * 3` wraps from 0x55555556, `(width + 5) / 6 * 4` is 0 from `UINT32_MAX - 4`); the port uses `wrapping_*` for each. Intentional difference: a null plan is left alone (the C writes through it; libobs passes a plan on its stack). `test_async_conversion_plan` compiles the C into itself and pins the whole table (every format and two values outside the enum, both ranges, every transfer, 4 and 258, at 14 sizes up to `UINT32_MAX`, from tables generated from the moved C), and `test_async_conversion_plan_rust` runs the same test source against `libobs_rust-static`; `test_media_restart_first_frame`, `test_deinterlace_2x_field_order` and `test_pipewire_camera_nv12` see the swap through the real libobs. Tier 3 compares every field, the technique as a string, over format x range x transfer exhaustively (47 formats, 270 transfers, 22 widths and heights: 1.1 million plans), every pair of 22 sizes for the in-enum formats, and a proptest over any `u32` size and any `i32` format and transfer. | diff --git a/rust/obs-c-oracle/benches/port_comparison.rs b/rust/obs-c-oracle/benches/port_comparison.rs index 17982c1bcb..7c9c822846 100644 --- a/rust/obs-c-oracle/benches/port_comparison.rs +++ b/rust/obs-c-oracle/benches/port_comparison.rs @@ -46,6 +46,8 @@ mod service; mod source_async_pacing; #[path = "port_comparison/source_audio_input.rs"] mod source_audio_input; +#[path = "port_comparison/source_audio_render.rs"] +mod source_audio_render; #[path = "port_comparison/source_audio_timing.rs"] mod source_audio_timing; #[path = "port_comparison/source_color_space.rs"] @@ -160,6 +162,7 @@ fn comparisons(criterion: &mut Criterion) { service, source_async_pacing, source_audio_input, + source_audio_render, source_audio_timing, source_color_space, source_conversion_plan, diff --git a/rust/obs-c-oracle/benches/port_comparison/source_audio_render.rs b/rust/obs-c-oracle/benches/port_comparison/source_audio_render.rs new file mode 100644 index 0000000000..9c5ee2560d --- /dev/null +++ b/rust/obs-c-oracle/benches/port_comparison/source_audio_render.rs @@ -0,0 +1,201 @@ +//! obs-source: the audio render mix plan +//! (`libobs/obs-source-audio-render.c.h`), which +//! `obs_source_audio_render()` runs for every audio source on every tick of +//! the mix. The measurement folds its results into a checksum that both +//! sides must match: +//! - `render`: 1024 sources through `obs_source_audio_render()`, +//! `custom_audio_render()` and `process_audio_source_tick()` of +//! `obs-source.c` on a stand-in source (the route, the clears around a +//! custom render's callback and its result, the tick's pending check and +//! the copy or clear of each mix), with every combination of the six +//! source flags, mixer masks from none to all and input sizes below, at +//! and above the tick; the buffers, the callbacks and the volume are not +//! run. +//! +//! Measured when it was added (x86_64 Linux, GCC, not pinned to a core; +//! median per 1024 sources): render 12.2 us Rust, 30.2 us C (0.40x). Under +//! 30 ns a source a tick either way, of a tick of 21.3 ms. + +use std::hint::black_box; + +use criterion::{Criterion, Throughput}; +use obs_c_oracle::source_audio_render as c; +use obs_source::ffi::source_audio_render as rs; + +const COUNT: usize = 1024; +const MIXES: usize = 6; +const TICK_BYTES: usize = 4096; + +struct Side { + route: fn(bool, bool, bool) -> u32, + custom_clear_before: fn(u32, u32, usize) -> bool, + custom_ts: fn(bool, u64) -> u64, + custom_pending: fn(bool) -> bool, + custom_unmixed: fn(u64, u32) -> bool, + custom_in_tick: fn(u32, usize) -> bool, + custom_clear_after: fn(u32, usize) -> bool, + tick_unset: fn(u64) -> bool, + tick_short: fn(usize, usize) -> bool, + tick_stop: fn(bool, usize) -> bool, + tick_copy: fn(bool, u32, u32, usize) -> bool, + tick_volume: fn(bool) -> bool, + tick_clear_main: fn(u32, u32) -> bool, +} + +const RUST: Side = Side { + route: |a, b, c| rs::obs_source_audio_render_route(a, b, c), + custom_clear_before: |a, b, c| rs::obs_source_audio_render_custom_clear_before(a, b, c), + custom_ts: |a, b| rs::obs_source_audio_render_custom_ts(a, b), + custom_pending: |a| rs::obs_source_audio_render_custom_pending(a), + custom_unmixed: |a, b| rs::obs_source_audio_render_custom_unmixed(a, b), + custom_in_tick: |a, b| rs::obs_source_audio_render_custom_in_tick(a, b), + custom_clear_after: |a, b| rs::obs_source_audio_render_custom_clear_after(a, b), + tick_unset: |a| rs::obs_source_audio_render_tick_unset(a), + tick_short: |a, b| rs::obs_source_audio_render_tick_short(a, b), + tick_stop: |a, b| rs::obs_source_audio_render_tick_stop(a, b), + tick_copy: |a, b, c, d| rs::obs_source_audio_render_tick_copy(a, b, c, d), + tick_volume: |a| rs::obs_source_audio_render_tick_volume(a), + tick_clear_main: |a, b| rs::obs_source_audio_render_tick_clear_main(a, b), +}; + +const C: Side = Side { + // SAFETY: plain values; the C touches no memory. + route: |b, cu, d| unsafe { c::oracle_obs_source_audio_render_route(b, cu, d) }, + // SAFETY: as above; mix < 6. + custom_clear_before: |a, m, x| unsafe { + c::oracle_obs_source_audio_render_custom_clear_before(a, m, x) != 0 + }, + // SAFETY: as above. + custom_ts: |s, t| unsafe { c::oracle_obs_source_audio_render_custom_ts(s, t) }, + // SAFETY: as above. + custom_pending: |s| unsafe { c::oracle_obs_source_audio_render_custom_pending(s) != 0 }, + // SAFETY: as above. + custom_unmixed: |t, m| unsafe { c::oracle_obs_source_audio_render_custom_unmixed(t, m) != 0 }, + // SAFETY: as above; mix < 6. + custom_in_tick: |m, x| unsafe { c::oracle_obs_source_audio_render_custom_in_tick(m, x) != 0 }, + // SAFETY: as above; mix < 6. + custom_clear_after: |a, x| unsafe { + c::oracle_obs_source_audio_render_custom_clear_after(a, x) != 0 + }, + // SAFETY: as above. + tick_unset: |t| unsafe { c::oracle_obs_source_audio_render_tick_unset(t) != 0 }, + // SAFETY: as above. + tick_short: |i, s| unsafe { c::oracle_obs_source_audio_render_tick_short(i, s) != 0 }, + // SAFETY: as above. + tick_stop: |s, x| unsafe { c::oracle_obs_source_audio_render_tick_stop(s, x) != 0 }, + // SAFETY: as above; mix < 6. + tick_copy: |s, a, m, x| unsafe { c::oracle_obs_source_audio_render_tick_copy(s, a, m, x) != 0 }, + // SAFETY: as above. + tick_volume: |s| unsafe { c::oracle_obs_source_audio_render_tick_volume(s) != 0 }, + // SAFETY: as above. + tick_clear_main: |a, m| unsafe { c::oracle_obs_source_audio_render_tick_clear_main(a, m) != 0 }, +}; + +#[derive(Clone, Copy)] +struct Source { + bits: u8, + audio_ts: u64, + audio_mixers: u32, + input_size: usize, + cb_ts: u64, + mixers: u32, +} + +/// What the three callers would do, folded into a number. +fn render(side: &Side, s: &Source) -> u64 { + let has_buffer = s.bits & 1 != 0; + let custom = has_buffer && s.bits & 2 != 0; + let has_data = custom && s.bits & 4 != 0; + let submix = s.bits & 16 != 0; + let mut sum = 0_u64; + match (side.route)(has_buffer, custom, has_data) { + 0 => return 1, + 1 => { + for mix in 0..MIXES { + if (side.custom_clear_before)(s.audio_mixers, s.mixers, mix) { + sum = sum.wrapping_mul(7).wrapping_add(mix as u64 + 1); + } + } + let success = s.bits & 32 != 0; + let ts = (side.custom_ts)(success, s.cb_ts); + sum = sum.wrapping_add(ts); + if (side.custom_pending)(success) || (side.custom_unmixed)(ts, s.mixers) { + return sum.wrapping_add(2); + } + for mix in 0..MIXES { + if (side.custom_in_tick)(s.mixers, mix) + && (side.custom_clear_after)(s.audio_mixers, mix) + { + sum = sum.wrapping_mul(11).wrapping_add(mix as u64 + 1); + } + } + return sum.wrapping_add(3); + } + _ => {} + } + if (side.tick_unset)(s.audio_ts) || (side.tick_short)(s.input_size, TICK_BYTES) { + return 4; + } + for mix in 1..MIXES { + if (side.tick_stop)(submix, mix) { + break; + } + let copy = (side.tick_copy)(submix, s.audio_mixers, s.mixers, mix); + sum = sum + .wrapping_mul(13) + .wrapping_add(u64::from(copy) + mix as u64); + } + if !(side.tick_volume)(submix) { + return sum.wrapping_add(5); + } + if (side.tick_clear_main)(s.audio_mixers, s.mixers) { + sum = sum.wrapping_add(6); + } + sum.wrapping_add(7) +} + +const MASKS: [u32; 8] = [0x3f, 0x01, 0x03, 0x00, 0x40, 0x3e, 0xff, u32::MAX]; + +fn sources() -> Vec { + (0..COUNT) + .map(|i| { + let n = i as u64; + Source { + bits: (i % 64) as u8 | 1, + audio_ts: if i % 11 == 0 { + 0 + } else { + 1_000_000_000 + n * 21_333_333 + }, + audio_mixers: MASKS[(i / 3) % MASKS.len()], + input_size: TICK_BYTES - 1 + i % 3, + cb_ts: if i % 13 == 0 { 0 } else { 2_000_000_000 + n }, + mixers: MASKS[(i / 7) % MASKS.len()], + } + }) + .collect() +} + +fn run(side: &Side, sources: &[Source]) -> u64 { + let mut sum = 0_u64; + for s in sources { + let r = render(side, black_box(s)); + sum = sum.wrapping_mul(31).wrapping_add(r); + } + sum +} + +pub(crate) fn benchmarks(criterion: &mut Criterion) { + let sources = sources(); + assert_eq!( + run(&RUST, &sources), + run(&C, &sources), + "benchmark fixture differs: render" + ); + + let mut group = criterion.benchmark_group("source_audio_render/render"); + group.throughput(Throughput::Elements(COUNT as u64)); + group.bench_function("rust", |b| b.iter(|| black_box(run(&RUST, &sources)))); + group.bench_function("c", |b| b.iter(|| black_box(run(&C, &sources)))); + group.finish(); +} diff --git a/rust/obs-c-oracle/oracle/source_audio_render.c b/rust/obs-c-oracle/oracle/source_audio_render.c new file mode 100644 index 0000000000..f77683a2f5 --- /dev/null +++ b/rust/obs-c-oracle/oracle/source_audio_render.c @@ -0,0 +1,55 @@ +/* Test-only oracle: the original libobs/obs-source-audio-render.c.h, + * unmodified, with its functions renamed to oracle_* so they can link next + * to the Rust shims. */ +#define obs_source_audio_render_route oracle_obs_source_audio_render_route +#define obs_source_audio_render_custom_clear_before oracle_obs_source_audio_render_custom_clear_before +#define obs_source_audio_render_custom_ts oracle_obs_source_audio_render_custom_ts +#define obs_source_audio_render_custom_pending oracle_obs_source_audio_render_custom_pending +#define obs_source_audio_render_custom_unmixed oracle_obs_source_audio_render_custom_unmixed +#define obs_source_audio_render_custom_in_tick oracle_obs_source_audio_render_custom_in_tick +#define obs_source_audio_render_custom_clear_after oracle_obs_source_audio_render_custom_clear_after +#define obs_source_audio_render_tick_unset oracle_obs_source_audio_render_tick_unset +#define obs_source_audio_render_tick_short oracle_obs_source_audio_render_tick_short +#define obs_source_audio_render_tick_stop oracle_obs_source_audio_render_tick_stop +#define obs_source_audio_render_tick_copy oracle_obs_source_audio_render_tick_copy +#define obs_source_audio_render_tick_volume oracle_obs_source_audio_render_tick_volume +#define obs_source_audio_render_tick_clear_main oracle_obs_source_audio_render_tick_clear_main + +#include "obs-source-audio-render.c.h" + +#undef obs_source_audio_render_route +#undef obs_source_audio_render_custom_clear_before +#undef obs_source_audio_render_custom_ts +#undef obs_source_audio_render_custom_pending +#undef obs_source_audio_render_custom_unmixed +#undef obs_source_audio_render_custom_in_tick +#undef obs_source_audio_render_custom_clear_after +#undef obs_source_audio_render_tick_unset +#undef obs_source_audio_render_tick_short +#undef obs_source_audio_render_tick_stop +#undef obs_source_audio_render_tick_copy +#undef obs_source_audio_render_tick_volume +#undef obs_source_audio_render_tick_clear_main + +/* The header's enum values and size, for the Rust side. */ +unsigned oracle_source_audio_render_route_value(int which); +unsigned oracle_source_audio_render_route_size(void); + +unsigned oracle_source_audio_render_route_value(int which) +{ + switch (which) { + case 0: + return OBS_SOURCE_AUDIO_RENDER_PENDING; + case 1: + return OBS_SOURCE_AUDIO_RENDER_CUSTOM; + default: + return OBS_SOURCE_AUDIO_RENDER_TICK; + } +} + +unsigned oracle_source_audio_render_route_size(void) +{ + /* The enum shares its tag with the route function, which the rename above + * also renamed, so take the size of what the function returns. */ + return (unsigned)sizeof(oracle_obs_source_audio_render_route(false, false, false)); +} diff --git a/rust/obs-c-oracle/src/lib.rs b/rust/obs-c-oracle/src/lib.rs index b22abd2b91..27bfeb3a61 100644 --- a/rust/obs-c-oracle/src/lib.rs +++ b/rust/obs-c-oracle/src/lib.rs @@ -129,6 +129,7 @@ pub mod shader_sampler_convert; pub mod signal; pub mod source_async_pacing; pub mod source_audio_input; +pub mod source_audio_render; pub mod source_audio_timing; pub mod source_color_space; pub mod source_conversion_plan; diff --git a/rust/obs-c-oracle/src/source_audio_render.rs b/rust/obs-c-oracle/src/source_audio_render.rs new file mode 100644 index 0000000000..79308fc8aa --- /dev/null +++ b/rust/obs-c-oracle/src/source_audio_render.rs @@ -0,0 +1,51 @@ +//! `oracle_*` build of `libobs/obs-source-audio-render.c.h` +//! (oracle/source_audio_render.c): the source audio render mix plan, +//! renamed. The C's `bool` results are declared as `u8`, the byte the C +//! returns, so a parity test compares them without assuming they are 0 or +//! 1; the enum is declared as `u32`, the width the C returns it in. + +unsafe extern "C" { + /// `enum obs_source_audio_render_route obs_source_audio_render_route(bool has_buffer, bool custom, bool has_data)`. + pub fn oracle_obs_source_audio_render_route( + has_buffer: bool, + custom: bool, + has_data: bool, + ) -> u32; + /// `bool obs_source_audio_render_custom_clear_before(uint32_t audio_mixers, uint32_t mixers, size_t mix)`. + pub fn oracle_obs_source_audio_render_custom_clear_before( + audio_mixers: u32, + mixers: u32, + mix: usize, + ) -> u8; + /// `uint64_t obs_source_audio_render_custom_ts(bool success, uint64_t ts)`. + pub fn oracle_obs_source_audio_render_custom_ts(success: bool, ts: u64) -> u64; + /// `bool obs_source_audio_render_custom_pending(bool success)`. + pub fn oracle_obs_source_audio_render_custom_pending(success: bool) -> u8; + /// `bool obs_source_audio_render_custom_unmixed(uint64_t audio_ts, uint32_t mixers)`. + pub fn oracle_obs_source_audio_render_custom_unmixed(audio_ts: u64, mixers: u32) -> u8; + /// `bool obs_source_audio_render_custom_in_tick(uint32_t mixers, size_t mix)`. + pub fn oracle_obs_source_audio_render_custom_in_tick(mixers: u32, mix: usize) -> u8; + /// `bool obs_source_audio_render_custom_clear_after(uint32_t audio_mixers, size_t mix)`. + pub fn oracle_obs_source_audio_render_custom_clear_after(audio_mixers: u32, mix: usize) -> u8; + /// `bool obs_source_audio_render_tick_unset(uint64_t audio_ts)`. + pub fn oracle_obs_source_audio_render_tick_unset(audio_ts: u64) -> u8; + /// `bool obs_source_audio_render_tick_short(size_t input_size, size_t size)`. + pub fn oracle_obs_source_audio_render_tick_short(input_size: usize, size: usize) -> u8; + /// `bool obs_source_audio_render_tick_stop(bool submix, size_t mix)`. + pub fn oracle_obs_source_audio_render_tick_stop(submix: bool, mix: usize) -> u8; + /// `bool obs_source_audio_render_tick_copy(bool submix, uint32_t audio_mixers, uint32_t mixers, size_t mix)`. + pub fn oracle_obs_source_audio_render_tick_copy( + submix: bool, + audio_mixers: u32, + mixers: u32, + mix: usize, + ) -> u8; + /// `bool obs_source_audio_render_tick_volume(bool submix)`. + pub fn oracle_obs_source_audio_render_tick_volume(submix: bool) -> u8; + /// `bool obs_source_audio_render_tick_clear_main(uint32_t audio_mixers, uint32_t mixers)`. + pub fn oracle_obs_source_audio_render_tick_clear_main(audio_mixers: u32, mixers: u32) -> u8; + /// `OBS_SOURCE_AUDIO_RENDER_PENDING` (0), `_CUSTOM` (1) or `_TICK` (2). + pub fn oracle_source_audio_render_route_value(which: i32) -> u32; + /// `sizeof(enum obs_source_audio_render_route)`. + pub fn oracle_source_audio_render_route_size() -> u32; +} diff --git a/rust/obs-source/Cargo.toml b/rust/obs-source/Cargo.toml index 5bf1fac9d0..8ba8a586af 100644 --- a/rust/obs-source/Cargo.toml +++ b/rust/obs-source/Cargo.toml @@ -2,7 +2,7 @@ autotests = false name = "obs-source" version = "0.1.0" -description = "Rust ports of libobs/obs-source-async-pacing.c.h (the async video frame pacing), libobs/obs-source-audio-input.c.h (the source audio input steps), libobs/obs-source-audio-timing.c.h (the source audio timestamp placement), libobs/obs-source-color-space.c.h (the colour space query routing), libobs/obs-source-conversion-plan.c.h (the async video GPU conversion plan), libobs/obs-source-deinterlace-draw.c.h (the deinterlace draw decisions), libobs/obs-source-deinterlace-pacing.c.h (the 2x deinterlace frame pacing), libobs/obs-source-draw-plan.c.h (the source draw decisions), libobs/obs-source-frame-copy.c.h (the async frame copy plan and cache decisions), libobs/obs-source-ptm-gate.c.h (the push-to-mute and push-to-talk gate), libobs/obs-source-render-route.c.h (the source and filter render routing), libobs/obs-source-size.c.h (the size query routing), libobs/obs-source-transition-clock.c.h (the transition clock and fit), libobs/obs-source-transition-render.c.h (the transition render and audio steps) and libobs/obs-source-volume.c.h (the source volume snap and apply decisions)" +description = "Rust ports of libobs/obs-source-async-pacing.c.h (the async video frame pacing), libobs/obs-source-audio-input.c.h (the source audio input steps), libobs/obs-source-audio-render.c.h (the source audio render mix plan), libobs/obs-source-audio-timing.c.h (the source audio timestamp placement), libobs/obs-source-color-space.c.h (the colour space query routing), libobs/obs-source-conversion-plan.c.h (the async video GPU conversion plan), libobs/obs-source-deinterlace-draw.c.h (the deinterlace draw decisions), libobs/obs-source-deinterlace-pacing.c.h (the 2x deinterlace frame pacing), libobs/obs-source-draw-plan.c.h (the source draw decisions), libobs/obs-source-frame-copy.c.h (the async frame copy plan and cache decisions), libobs/obs-source-ptm-gate.c.h (the push-to-mute and push-to-talk gate), libobs/obs-source-render-route.c.h (the source and filter render routing), libobs/obs-source-size.c.h (the size query routing), libobs/obs-source-transition-clock.c.h (the transition clock and fit), libobs/obs-source-transition-render.c.h (the transition render and audio steps) and libobs/obs-source-volume.c.h (the source volume snap and apply decisions)" edition.workspace = true rust-version.workspace = true license.workspace = true diff --git a/rust/obs-source/src/audio_render_plan.rs b/rust/obs-source/src/audio_render_plan.rs new file mode 100644 index 0000000000..7a3108ea1e --- /dev/null +++ b/rust/obs-source/src/audio_render_plan.rs @@ -0,0 +1,183 @@ +//! Safe core of `libobs/obs-source-audio-render.c.h`: the audio render mix +//! plan of `obs_source_audio_render()`, `custom_audio_render()` and +//! `process_audio_source_tick()`. +//! +//! Each audio tick, the mix asks every audio source for its six mixes +//! through `obs_source_audio_render()` in `obs-source.c.h`, and these are +//! the decisions it takes on the way: +//! - [`route`]: pending without an output buffer, or with an `audio_render` +//! callback but no data yet (the audio thread can reach a source still +//! being created, 432017b2c); the callback with data; else the tick; +//! - around the callback (`custom_audio_render()`): +//! [`custom_clear_before`] (a mix in both masks is cleared before it), +//! [`custom_ts`] and [`custom_pending`] (its timestamp when it succeeds, +//! else 0 and pending), [`custom_unmixed`] (no timestamp or no tick +//! mixers: nothing after it), [`custom_in_tick`] and +//! [`custom_clear_after`] (a mix of the tick the source is not on is +//! cleared after it); +//! - on the buffered input (`process_audio_source_tick()`): [`tick_unset`] +//! and [`tick_short`] (pending with no timestamp or less input than the +//! tick), [`tick_stop`] and [`tick_copy`] (mixes 1 to 5 copy mix 0 when +//! in both masks, else are cleared; a submix source does mix 1 only, from +//! bit 0 of its mixers), [`tick_volume`] and [`tick_clear_main`] (a +//! submix source stops there; any other clears mix 0 unless it is in both +//! masks, then scales by its volume). +//! +//! The plan is one function per decision, not one call per tick, because +//! `obs-source.c.h` must read each field where it did: `audio_mixers` is set +//! by other threads without a lock and read once per mix, before and after +//! the plugin's callback; `context.data` only behind the callback check; the +//! input size only with a timestamp; `audio_ts` only after a successful +//! callback. `obs-source.c.h` keeps those reads, `audio_buf_mutex`, +//! `deque_peek_front()`, every memset and memcpy, the plugin callbacks, +//! `audio_submix()` and `apply_audio_volume()`. Nothing here keeps state, +//! takes a lock or calls libobs. +//! +//! Characterized, not endorsed (`test_source_audio_render_steps`), and +//! kept: tick mixers with no bit below [`MAX_AUDIO_MIXES`] (0x40 and up) +//! are not "no mixers", so a custom render's clears look at every mix and +//! its volume runs; a submix source keeps the peeked input in mix 0 even +//! when it is on no mix, and fills mix 1 whatever the tick's mixers. +//! +//! Intentional difference: a `mix` of 32 or more names no mix (the C's +//! `1 << mix` on an `int` is undefined from 31 on; libobs passes a mix +//! below [`MAX_AUDIO_MIXES`]). + +#![deny( + clippy::arithmetic_side_effects, + clippy::indexing_slicing, + clippy::as_conversions, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::todo, + clippy::unimplemented, + clippy::unreachable +)] + +/// `MAX_AUDIO_MIXES`: the mixes a tick fills. +pub const MAX_AUDIO_MIXES: usize = 6; + +/// `enum obs_source_audio_render_route`: which way +/// `obs_source_audio_render()` takes. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u32)] +pub enum Route { + /// `OBS_SOURCE_AUDIO_RENDER_PENDING`: no output buffer, or a custom + /// render without data. + Pending = 0, + /// `OBS_SOURCE_AUDIO_RENDER_CUSTOM`: the source's `audio_render` + /// callback. + Custom = 1, + /// `OBS_SOURCE_AUDIO_RENDER_TICK`: the buffered input (`audio_submix()` + /// first when there is an `audio_mix` callback). + Tick = 2, +} + +impl From for u32 { + /// The C enum's value. + fn from(route: Route) -> Self { + match route { + Route::Pending => 0, + Route::Custom => 1, + Route::Tick => 2, + } + } +} + +/// The mix's bit, the C's `1 << mix`; 0 (no mix) from 32 on. +fn bit(mix: usize) -> u32 { + u32::try_from(mix) + .ok() + .and_then(|shift| 1_u32.checked_shl(shift)) + .unwrap_or(0) +} + +/// `obs_source_audio_render_route()`: [`Route::Pending`] without +/// `has_buffer`; with `custom`, [`Route::Custom`] when `has_data`, else +/// [`Route::Pending`]; else [`Route::Tick`]. +pub const fn route(has_buffer: bool, custom: bool, has_data: bool) -> Route { + if !has_buffer { + return Route::Pending; + } + if custom { + if !has_data { + return Route::Pending; + } + return Route::Custom; + } + Route::Tick +} + +/// `obs_source_audio_render_custom_clear_before()`: the mix is in both +/// `audio_mixers` and `mixers`. +pub fn custom_clear_before(audio_mixers: u32, mixers: u32, mix: usize) -> bool { + audio_mixers & mixers & bit(mix) != 0 +} + +/// `obs_source_audio_render_custom_ts()`: `ts` when the callback +/// succeeded, else 0 (`ts` is not looked at). +pub const fn custom_ts(success: bool, ts: u64) -> u64 { + if success { ts } else { 0 } +} + +/// `obs_source_audio_render_custom_pending()`: pending when the callback +/// failed. +pub const fn custom_pending(success: bool) -> bool { + !success +} + +/// `obs_source_audio_render_custom_unmixed()`: no timestamp or no tick +/// mixers, so nothing is cleared or scaled after the callback. +pub const fn custom_unmixed(audio_ts: u64, mixers: u32) -> bool { + audio_ts == 0 || mixers == 0 +} + +/// `obs_source_audio_render_custom_in_tick()`: the mix is in the tick's +/// `mixers`. +pub fn custom_in_tick(mixers: u32, mix: usize) -> bool { + mixers & bit(mix) != 0 +} + +/// `obs_source_audio_render_custom_clear_after()`: the source is not on the +/// mix. +pub fn custom_clear_after(audio_mixers: u32, mix: usize) -> bool { + audio_mixers & bit(mix) == 0 +} + +/// `obs_source_audio_render_tick_unset()`: no input timestamp yet. +pub const fn tick_unset(audio_ts: u64) -> bool { + audio_ts == 0 +} + +/// `obs_source_audio_render_tick_short()`: less buffered input than the +/// tick reads. +pub const fn tick_short(input_size: usize, size: usize) -> bool { + input_size < size +} + +/// `obs_source_audio_render_tick_stop()`: a submix source stops after +/// mix 1. +pub const fn tick_stop(submix: bool, mix: usize) -> bool { + submix && mix > 1 +} + +/// `obs_source_audio_render_tick_copy()`: the mix copies mix 0 when it is +/// in both masks (else it is cleared); for a submix source, when +/// `audio_mixers` has bit 0, whatever `mixers` and `mix`. +pub fn tick_copy(submix: bool, audio_mixers: u32, mixers: u32, mix: usize) -> bool { + let (mixers, mix_bit) = if submix { (1, 1) } else { (mixers, bit(mix)) }; + audio_mixers & mix_bit != 0 && mixers & mix_bit != 0 +} + +/// `obs_source_audio_render_tick_volume()`: anything but a submix source +/// goes on to mix 0 and the volume. +pub const fn tick_volume(submix: bool) -> bool { + !submix +} + +/// `obs_source_audio_render_tick_clear_main()`: mix 0 is cleared unless it +/// is in both masks. +pub const fn tick_clear_main(audio_mixers: u32, mixers: u32) -> bool { + audio_mixers & 1 == 0 || mixers & 1 == 0 +} diff --git a/rust/obs-source/src/ffi/mod.rs b/rust/obs-source/src/ffi/mod.rs index 29f4e3ee79..7e242e003b 100644 --- a/rust/obs-source/src/ffi/mod.rs +++ b/rust/obs-source/src/ffi/mod.rs @@ -2,6 +2,7 @@ pub mod source_async_pacing; pub mod source_audio_input; +pub mod source_audio_render; pub mod source_audio_timing; pub mod source_color_space; pub mod source_conversion_plan; diff --git a/rust/obs-source/src/ffi/source_audio_render.rs b/rust/obs-source/src/ffi/source_audio_render.rs new file mode 100644 index 0000000000..cb1c5aa8ca --- /dev/null +++ b/rust/obs-source/src/ffi/source_audio_render.rs @@ -0,0 +1,142 @@ +//! C ABI for `libobs/obs-source-audio-render.c.h`, the audio render mix plan +//! of `obs_source_audio_render()`, `custom_audio_render()` and +//! `process_audio_source_tick()`, declared in +//! `libobs/obs-source-audio-render.h` without `EXPORT`. +//! +//! The three callers in `obs-source.c.h` (still C) read the source's fields +//! themselves, in the C's order and under its conditions, pass the values +//! to these shims and act on the answers: `audio_buf_mutex`, +//! `deque_peek_front()`, every memset and memcpy, the plugin callbacks, +//! `audio_submix()` and `apply_audio_volume()` stay C. So `struct +//! obs_source` is never mirrored, no pointer crosses, and the shims take no +//! lock, call nothing in libobs and touch no memory. They stay hidden +//! (`rust-hidden.txt`). +//! +//! `obs_source_audio_render_custom_ts()` receives the callback's `ts` by +//! value also when the callback failed and may not have written it (an +//! indeterminate `uint64_t` whose address was taken, so not undefined in +//! C); any `u64` is valid here, and it is not looked at. +//! +//! Intentional difference: a `mix` of 32 or more names no mix, where the C's +//! `1 << mix` on an `int` is undefined from 31 on (libobs passes +//! `mix < MAX_AUDIO_MIXES`). + +use crate::audio_render_plan::{self as core, Route}; + +/// `enum obs_source_audio_render_route`, as the C's enum is returned (an +/// `int` or `unsigned int`; every value fits both). +#[allow(non_camel_case_types)] +pub type obs_source_audio_render_route_t = u32; + +/// `OBS_SOURCE_AUDIO_RENDER_PENDING`. +pub const OBS_SOURCE_AUDIO_RENDER_PENDING: obs_source_audio_render_route_t = 0; +/// `OBS_SOURCE_AUDIO_RENDER_CUSTOM`. +pub const OBS_SOURCE_AUDIO_RENDER_CUSTOM: obs_source_audio_render_route_t = 1; +/// `OBS_SOURCE_AUDIO_RENDER_TICK`. +pub const OBS_SOURCE_AUDIO_RENDER_TICK: obs_source_audio_render_route_t = 2; + +/// `enum obs_source_audio_render_route obs_source_audio_render_route(bool +/// has_buffer, bool custom, bool has_data)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_route( + has_buffer: bool, + custom: bool, + has_data: bool, +) -> obs_source_audio_render_route_t { + match core::route(has_buffer, custom, has_data) { + Route::Pending => OBS_SOURCE_AUDIO_RENDER_PENDING, + Route::Custom => OBS_SOURCE_AUDIO_RENDER_CUSTOM, + Route::Tick => OBS_SOURCE_AUDIO_RENDER_TICK, + } +} + +/// `bool obs_source_audio_render_custom_clear_before(uint32_t audio_mixers, +/// uint32_t mixers, size_t mix)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_custom_clear_before( + audio_mixers: u32, + mixers: u32, + mix: usize, +) -> bool { + core::custom_clear_before(audio_mixers, mixers, mix) +} + +/// `uint64_t obs_source_audio_render_custom_ts(bool success, uint64_t ts)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_custom_ts(success: bool, ts: u64) -> u64 { + core::custom_ts(success, ts) +} + +/// `bool obs_source_audio_render_custom_pending(bool success)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_custom_pending(success: bool) -> bool { + core::custom_pending(success) +} + +/// `bool obs_source_audio_render_custom_unmixed(uint64_t audio_ts, uint32_t +/// mixers)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_custom_unmixed(audio_ts: u64, mixers: u32) -> bool { + core::custom_unmixed(audio_ts, mixers) +} + +/// `bool obs_source_audio_render_custom_in_tick(uint32_t mixers, size_t +/// mix)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_custom_in_tick(mixers: u32, mix: usize) -> bool { + core::custom_in_tick(mixers, mix) +} + +/// `bool obs_source_audio_render_custom_clear_after(uint32_t audio_mixers, +/// size_t mix)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_custom_clear_after( + audio_mixers: u32, + mix: usize, +) -> bool { + core::custom_clear_after(audio_mixers, mix) +} + +/// `bool obs_source_audio_render_tick_unset(uint64_t audio_ts)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_tick_unset(audio_ts: u64) -> bool { + core::tick_unset(audio_ts) +} + +/// `bool obs_source_audio_render_tick_short(size_t input_size, size_t +/// size)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_tick_short(input_size: usize, size: usize) -> bool { + core::tick_short(input_size, size) +} + +/// `bool obs_source_audio_render_tick_stop(bool submix, size_t mix)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_tick_stop(submix: bool, mix: usize) -> bool { + core::tick_stop(submix, mix) +} + +/// `bool obs_source_audio_render_tick_copy(bool submix, uint32_t +/// audio_mixers, uint32_t mixers, size_t mix)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_tick_copy( + submix: bool, + audio_mixers: u32, + mixers: u32, + mix: usize, +) -> bool { + core::tick_copy(submix, audio_mixers, mixers, mix) +} + +/// `bool obs_source_audio_render_tick_volume(bool submix)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_tick_volume(submix: bool) -> bool { + core::tick_volume(submix) +} + +/// `bool obs_source_audio_render_tick_clear_main(uint32_t audio_mixers, +/// uint32_t mixers)`. +#[unsafe(no_mangle)] +pub extern "C" fn obs_source_audio_render_tick_clear_main(audio_mixers: u32, mixers: u32) -> bool { + core::tick_clear_main(audio_mixers, mixers) +} diff --git a/rust/obs-source/src/lib.rs b/rust/obs-source/src/lib.rs index c0f6e5f5bf..6cb0fc35ec 100644 --- a/rust/obs-source/src/lib.rs +++ b/rust/obs-source/src/lib.rs @@ -4,6 +4,9 @@ //! `libobs/obs-source-async-pacing.c.h`, the async video frame pacing of //! `get_closest_frame()`; `libobs/obs-source-audio-input.c.h`, the audio //! input steps of `process_audio()` and `reset_resampler()`; +//! `libobs/obs-source-audio-render.c.h`, the audio render mix plan of +//! `obs_source_audio_render()`, `custom_audio_render()` and +//! `process_audio_source_tick()`; //! `libobs/obs-source-audio-timing.c.h`, the audio timestamp placement of //! `source_output_audio_data()`; `libobs/obs-source-color-space.c.h`, the //! colour space query routing of `obs_source_get_color_space()`; @@ -34,7 +37,8 @@ //! `libobs/obs-source-volume.c.h`, the volume snap of //! `get_source_volume()` and the apply decisions of `apply_audio_volume()`. //! -//! The safe cores are [`async_pacing`], [`audio_input`], [`audio_timing`], +//! The safe cores are [`async_pacing`], [`audio_input`], +//! [`audio_render_plan`], [`audio_timing`], //! [`color_space`], [`conversion_plan`], [`deinterlace_draw`], //! [`deinterlace_pacing`], [`draw_plan`], [`frame_copy`], [`ptm_gate`], [`render_route`], [`size_route`], //! [`transition_clock`], [`transition_render`] and [`volume_apply`]; [`draw_technique`] holds the @@ -46,6 +50,7 @@ pub mod async_pacing; pub mod audio_input; +pub mod audio_render_plan; pub mod audio_timing; pub mod color_space; pub mod conversion_plan; diff --git a/rust/obs-source/tests/proptest-regressions/audio_render_plan.txt b/rust/obs-source/tests/proptest-regressions/audio_render_plan.txt new file mode 100644 index 0000000000..1fae13697f --- /dev/null +++ b/rust/obs-source/tests/proptest-regressions/audio_render_plan.txt @@ -0,0 +1,18 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 970ebc5182180a729e90fe69fc1c281b841b14aa8a7f043e50ec43f317ac8b92 # shrinks to bits = 0, audio_ts = 0, audio_mixers = 0, mixers = 0, input_size = 0, tick_size = 0, channels = 0, cb_ts = 0 +cc 32e25fff3b0710c9a8f1aa294fbbabb5e91e63333b27fdd0cff1167c286cf1a6 # shrinks to a = 0, m = 0, mix = 0, ts = 0, input = 0, want = 0, success = false, submix = false +cc 6f072a5cb2b06caf9e50aee54afd81350dcf3276d940225e930b98515ab79959 # shrinks to bits = 55, audio_ts = 0, audio_mixers = 0, mixers = 1, input_size = 0, tick_size = 0, channels = 0, cb_ts = 0 +cc 3311652f70ae61e285d492b587c2b4dc1eb5a60cf4be498909b98b38c5770654 # shrinks to a = 285212672, m = 3372220416, mix = 23, ts = 0, input = 0, want = 0, success = false, submix = false +cc 6fe2ee617f44c7571f06f2d64a8a24313ca3ee6769722c8cfd1851b43aacc652 # shrinks to bits = 7, audio_ts = 0, audio_mixers = 0, mixers = 0, input_size = 0, tick_size = 0, channels = 0, cb_ts = 1 +cc f1dd88724b1dfff2141adfb40ba1c8045e2ec236968f7b29f0547c06b1d70775 # shrinks to bits = 47, audio_ts = 0, audio_mixers = 0, mixers = 1, input_size = 0, tick_size = 0, channels = 0, cb_ts = 0 +cc abc333658f38ba65bd1b67f518f085bfa24471945806b3089f0f13c7cf9c5f2e # shrinks to bits = 41, audio_ts = 1, audio_mixers = 0, mixers = 0, input_size = 4096, tick_size = 0, channels = 0, cb_ts = 0 +cc 5638351be6b724f3bfa627d4b3d1aefbe0ced89e00c2862924f8396ad16f48db # shrinks to bits = 9, audio_ts = 1, audio_mixers = 0, mixers = 0, input_size = 0, tick_size = 0, channels = 0, cb_ts = 0 +cc 467e0b259a75c8deac5c0f165f93f9da4d67efdc7fb8111ae7fa74d98edba4e0 # shrinks to bits = 29, audio_ts = 1, audio_mixers = 0, mixers = 0, input_size = 0, tick_size = 0, channels = 0, cb_ts = 0 +cc 03f9eed11fdd255230f868b638e2c0e5c19f6e0c8e7a8a95d7cdf04e3c4b22fa # shrinks to a = 0, m = 0, mix = 1, ts = 0, input = 0, want = 0, success = false, submix = true +cc e4172a56e179009f2bca993204b2a931fa7dfae4852aae9eaeea14644bb4d4f3 # shrinks to bits = 25, audio_ts = 1, audio_mixers = 65, mixers = 2344418720, input_size = 18446744073709551615, tick_size = 0, channels = 0, cb_ts = 0 +cc 4090bd418ac49c221f939825faa41038dc40c596e4624032385d0124e1468327 # shrinks to a = 0, m = 2147483648, mix = 31, ts = 0, input = 0, want = 0, success = false, submix = false diff --git a/rust/obs-source/tests/proptest-regressions/audio_render_plan_parity.txt b/rust/obs-source/tests/proptest-regressions/audio_render_plan_parity.txt new file mode 100644 index 0000000000..2b923738f9 --- /dev/null +++ b/rust/obs-source/tests/proptest-regressions/audio_render_plan_parity.txt @@ -0,0 +1,16 @@ +# Seeds for failure cases proptest has generated in the past. It is +# automatically read and these particular cases re-run before any +# novel cases are generated. +# +# It is recommended to check this file in to source control so that +# everyone who runs the test benefits from these saved cases. +cc 6023426853ca31469b16899defa366e7787498f08dd2a7bd6089e41d95a5258a # shrinks to a = 0, m = 0, mix = 0, any_mix = 0, ts = 0, input = 0, want = 0, success = false, submix = false, bits = 3 +cc 4e2de601f62ee4f1b414b27de077020bcea3b4c614a524ae1ddd301d059744a2 # shrinks to bits = 43, ts = 0, cb_ts = 0, a = 0, m = 0, input = 0, want = 0, channels = 0 +cc 46958aefb4a2bf86479a148969f61e74c87341513b4df2a80405fdf422d3af96 # shrinks to bits = 7, ts = 0, cb_ts = 0, a = 0, m = 1, input = 0, want = 0, channels = 0 +cc 1746be6ea8c727b8fceafba66c4c41091f4b2f79e7269533403673baa2af0a7f # shrinks to a = 0, m = 0, mix = 0, any_mix = 0, ts = 18446744073709551615, input = 0, want = 0, success = false, submix = false, bits = 0 +cc a08e747b22331ef3da6f23c9d75822d5ac07f2638ea473859f2381f087be9bc6 # shrinks to bits = 7, ts = 0, cb_ts = 18446744073709551615, a = 0, m = 0, input = 0, want = 0, channels = 0 +cc 23bb9032bcaf863633ad8e7af37f4c85ded3c878ec25343cb226de1879f52d49 # shrinks to bits = 55, ts = 0, cb_ts = 18446744073709551615, a = 0, m = 1, input = 0, want = 0, channels = 0 +cc f5758ca202c8c718cd9b1a9eece407021b496f3c39d171310fbea8c91ba60876 # shrinks to bits = 57, ts = 18446744073709551615, cb_ts = 0, a = 0, m = 0, input = 4096, want = 4096, channels = 0 +cc 5ae6b06426425731d21716f8803671db8c66d7bb84fc23f210a078eb57832f13 # shrinks to bits = 49, ts = 18446744073709551615, cb_ts = 0, a = 0, m = 0, input = 0, want = 0, channels = 0 +cc 1c2f461394cb0d1436c758524526db03f2d11a3de922abea31c19cddc38a00ca # shrinks to a = 0, m = 0, mix = 0, any_mix = 0, ts = 18446744073709551615, input = 0, want = 0, success = false, submix = false, bits = 0 +cc de2fef4d5b4c144cb15a2ce10f137e7685d9b73854a138b64c5c10fc1d128ce8 # shrinks to bits = 55, ts = 0, cb_ts = 18446744073709551615, a = 0, m = 0, input = 0, want = 0, channels = 0 diff --git a/rust/obs-source/tests/source/audio_render_plan.rs b/rust/obs-source/tests/source/audio_render_plan.rs new file mode 100644 index 0000000000..25d630adc0 --- /dev/null +++ b/rust/obs-source/tests/source/audio_render_plan.rs @@ -0,0 +1,821 @@ +//! Tier 1: `obs_source::audio_render_plan`, the audio render mix plan of +//! `obs_source_audio_render()`, `custom_audio_render()` and +//! `process_audio_source_tick()`. +//! +//! The `steps_*` cases port `test/cmocka/test_source_audio_render_steps.c` +//! 1:1, under the same case names, in the same order, with the same inputs +//! and expectations, the stand-in source and the copies of the three +//! callers (the lock, the peeks, every memset and memcpy, the callbacks and +//! the volume recorded) included. +//! +//! The `edge_*` cases are new: every decision against its rule written +//! another way, over every mix index up to 64 and the mask edges, and +//! proptests over any masks, any `u64` timestamps and any `usize` sizes; +//! the callers over any inputs against the rules of the C written out; and +//! the intentional difference, a mix of 32 or more naming no mix. + +use obs_source::audio_render_plan::{ + MAX_AUDIO_MIXES, Route, custom_clear_after, custom_clear_before, custom_in_tick, + custom_pending, custom_ts, custom_unmixed, route, tick_clear_main, tick_copy, tick_short, + tick_stop, tick_unset, tick_volume, +}; +use proptest::prelude::*; + +const MIXES: usize = 6; +const CHANNELS: usize = 8; +const FRAMES: usize = 1024; +const TICK_BYTES: usize = FRAMES * 4; + +const MASKS: [u32; 18] = [ + 0, + 1, + 2, + 0x20, + 0x3e, + 0x3f, + 0x40, + 0x41, + 0x7f, + 0x80, + 0xff, + 0x100, + 0xfc0, + 0x10000, + 0x7fff_ffff, + 0x8000_0000, + 0xffff_fffe, + u32::MAX, +]; + +fn has_mix(mask: u32, mix: usize) -> bool { + mix < 32 && (mask >> mix) & 1 != 0 +} + +// --------------------------------------------------------------------------- +// The callers, copied from obs-source.c onto a stand-in source + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Call { + Lock, + Unlock, + Peek(usize, usize), + Memset(usize, usize), + Memcpy(usize, usize), + Render(u32, usize), + Submix(usize), + Volume(u32, usize), +} + +#[derive(Debug, Clone)] +struct StandIn { + has_buffer: bool, + custom: bool, + has_data: bool, + has_mix: bool, + submix: bool, + audio_ts: u64, + audio_pending: bool, + audio_mixers: u32, + input_size: usize, + cb_success: bool, + cb_ts: u64, + calls: Vec, +} + +fn source() -> StandIn { + StandIn { + has_buffer: true, + custom: false, + has_data: true, + has_mix: false, + submix: false, + audio_ts: 1000, + audio_pending: false, + audio_mixers: 0x3f, + input_size: TICK_BYTES, + cb_success: true, + cb_ts: 2000, + calls: Vec::new(), + } +} + +fn custom_audio_render(s: &mut StandIn, mixers: u32, channels: usize) { + for mix in 0..MIXES { + if custom_clear_before(s.audio_mixers, mixers, mix) { + s.calls.push(Call::Memset(mix, TICK_BYTES * channels)); + } + } + s.calls.push(Call::Render(mixers, channels)); + let success = s.cb_success; + let ts = s.cb_ts; + s.calls.push(Call::Lock); + s.audio_ts = custom_ts(success, ts); + s.audio_pending = custom_pending(success); + if custom_pending(success) || custom_unmixed(s.audio_ts, mixers) { + s.calls.push(Call::Unlock); + return; + } + for mix in 0..MIXES { + if !custom_in_tick(mixers, mix) { + continue; + } + if custom_clear_after(s.audio_mixers, mix) { + s.calls.push(Call::Memset(mix, TICK_BYTES * channels)); + } + } + s.calls.push(Call::Volume(mixers, channels)); + s.calls.push(Call::Unlock); +} + +fn process_audio_source_tick(s: &mut StandIn, mixers: u32, channels: usize, size: usize) { + let audio_submix = s.submix; + s.calls.push(Call::Lock); + if tick_unset(s.audio_ts) || tick_short(s.input_size, size) { + s.audio_pending = true; + s.calls.push(Call::Unlock); + return; + } + for ch in 0..channels { + s.calls.push(Call::Peek(ch, size)); + } + for mix in 1..MIXES { + if tick_stop(audio_submix, mix) { + break; + } + if !tick_copy(audio_submix, s.audio_mixers, mixers, mix) { + s.calls.push(Call::Memset(mix, size.wrapping_mul(channels))); + continue; + } + for ch in 0..channels { + s.calls.push(Call::Memcpy(mix * CHANNELS + ch, size)); + } + } + if !tick_volume(audio_submix) { + s.audio_pending = false; + s.calls.push(Call::Unlock); + return; + } + if tick_clear_main(s.audio_mixers, mixers) { + s.calls.push(Call::Memset(0, size.wrapping_mul(channels))); + } + s.calls.push(Call::Volume(mixers, channels)); + s.audio_pending = false; + s.calls.push(Call::Unlock); +} + +fn audio_render(s: &mut StandIn, mixers: u32, channels: usize, size: usize) { + let has_buffer = s.has_buffer; + let custom = has_buffer && s.custom; + let has_data = custom && s.has_data; + match route(has_buffer, custom, has_data) { + Route::Pending => { + s.audio_pending = true; + return; + } + Route::Custom => { + custom_audio_render(s, mixers, channels); + return; + } + Route::Tick => {} + } + if s.has_mix { + s.calls.push(Call::Submix(channels)); + } + process_audio_source_tick(s, mixers, channels, size); +} + +// --------------------------------------------------------------------------- +// steps_*: test_source_audio_render_steps.c, 1:1 + +#[test] +fn steps_route_values() { + assert_eq!(u32::from(Route::Pending), 0); + assert_eq!(u32::from(Route::Custom), 1); + assert_eq!(u32::from(Route::Tick), 2); + assert_eq!(Route::Pending as u32, 0); + assert_eq!(Route::Custom as u32, 1); + assert_eq!(Route::Tick as u32, 2); + assert_eq!(MAX_AUDIO_MIXES, MIXES); +} + +#[test] +fn steps_route_every_input() { + for bits in 0..8_u8 { + let (has_buffer, custom, has_data) = (bits & 1 != 0, bits & 2 != 0, bits & 4 != 0); + let expected = if !has_buffer { + Route::Pending + } else if !custom { + Route::Tick + } else if has_data { + Route::Custom + } else { + Route::Pending + }; + assert_eq!(route(has_buffer, custom, has_data), expected, "bits {bits}"); + } + assert_eq!(route(false, false, false), Route::Pending); + assert_eq!(route(false, true, true), Route::Pending); + assert_eq!(route(true, true, false), Route::Pending); + assert_eq!(route(true, true, true), Route::Custom); + // data does not matter without a custom render + assert_eq!(route(true, false, false), Route::Tick); + assert_eq!(route(true, false, true), Route::Tick); +} + +#[test] +fn steps_custom_clear_before_masks() { + assert!(custom_clear_before(0x3f, 0x3f, 0)); + assert!(custom_clear_before(0x3f, 0x3f, 5)); + assert!(!custom_clear_before(0x01, 0x3f, 1)); + assert!(!custom_clear_before(0x3f, 0x01, 1)); + assert!(!custom_clear_before(0, u32::MAX, 0)); + assert!(!custom_clear_before(u32::MAX, 0, 0)); + assert!(custom_clear_before(u32::MAX, u32::MAX, 3)); + // bits past the six mixes are never asked for + assert!(!custom_clear_before(0x40, 0x40, 0)); + + for a in MASKS { + for m in MASKS { + for mix in 0..MIXES { + assert_eq!( + custom_clear_before(a, m, mix), + has_mix(a, mix) && has_mix(m, mix), + "{a:#x} {m:#x} {mix}" + ); + } + } + } + for a in 0..64_u32 { + for m in 0..64_u32 { + for mix in 0..MIXES { + assert_eq!(custom_clear_before(a, m, mix), has_mix(a & m, mix)); + } + } + } +} + +#[test] +fn steps_custom_result() { + for ts in [0, 1, 1_000_000_000, 0x8000_0000_0000_0000, u64::MAX] { + assert_eq!(custom_ts(true, ts), ts); + assert_eq!(custom_ts(false, ts), 0); + } + assert!(!custom_pending(true)); + assert!(custom_pending(false)); +} + +#[test] +fn steps_custom_unmixed() { + for ts in [0, 1, u64::MAX] { + for m in MASKS { + assert_eq!(custom_unmixed(ts, m), ts == 0 || m == 0); + } + } + assert!(custom_unmixed(0, u32::MAX)); + assert!(custom_unmixed(1, 0)); + assert!(!custom_unmixed(1, 1)); + assert!(!custom_unmixed(1, 0x40)); + assert!(!custom_unmixed(u64::MAX, 0x8000_0000)); +} + +#[test] +fn steps_custom_clear_after_masks() { + for m in MASKS { + for mix in 0..MIXES { + assert_eq!(custom_in_tick(m, mix), has_mix(m, mix)); + assert_eq!(custom_clear_after(m, mix), !has_mix(m, mix)); + } + } + assert!(!custom_in_tick(0, 0)); + assert!(custom_in_tick(1, 0)); + assert!(custom_in_tick(0x20, 5)); + assert!(!custom_in_tick(0x40, 5)); + assert!(custom_clear_after(0, 0)); + assert!(!custom_clear_after(0x3f, 5)); + assert!(custom_clear_after(0x1f, 5)); + assert!(!custom_clear_after(u32::MAX, 0)); +} + +#[test] +fn steps_tick_pending_edges() { + assert!(tick_unset(0)); + assert!(!tick_unset(1)); + assert!(!tick_unset(u64::MAX)); + + let size = TICK_BYTES; + assert!(tick_short(size - 1, size)); + assert!(!tick_short(size, size)); + assert!(!tick_short(size + 1, size)); + assert!(tick_short(0, size)); + assert!(!tick_short(0, 0)); + assert!(!tick_short(usize::MAX, size)); + assert!(tick_short(usize::MAX - 1, usize::MAX)); + assert!(!tick_short(usize::MAX, usize::MAX)); + assert!(tick_short(0, 1)); +} + +#[test] +fn steps_tick_stop_mixes() { + for mix in 0..=64 { + assert!(!tick_stop(false, mix)); + assert_eq!(tick_stop(true, mix), mix > 1); + } + assert!(tick_stop(true, usize::MAX)); + assert!(tick_volume(false)); + assert!(!tick_volume(true)); +} + +#[test] +fn steps_tick_copy_masks() { + for a in 0..64_u32 { + for m in 0..64_u32 { + for mix in 1..MIXES { + assert_eq!( + tick_copy(false, a, m, mix), + has_mix(a, mix) && has_mix(m, mix) + ); + assert_eq!(tick_copy(true, a, m, mix), has_mix(a, 0)); + } + } + } + for a in MASKS { + for m in MASKS { + for mix in 0..MIXES { + assert_eq!( + tick_copy(false, a, m, mix), + has_mix(a, mix) && has_mix(m, mix) + ); + assert_eq!(tick_copy(true, a, m, mix), has_mix(a, 0)); + } + } + } + // submix: mix 1 from bit 0 even with no tick mixers, not from bit 1 + assert!(tick_copy(true, 1, 0, 1)); + assert!(!tick_copy(true, 2, u32::MAX, 1)); + assert!(tick_copy(false, 2, 2, 1)); + assert!(!tick_copy(false, 1, 1, 1)); + // a submix source's mix index is not shifted, so any index answers + assert!(tick_copy(true, 1, 0, 64)); +} + +#[test] +fn steps_tick_clear_main_masks() { + for a in MASKS { + for m in MASKS { + assert_eq!(tick_clear_main(a, m), !(has_mix(a, 0) && has_mix(m, 0))); + } + } + assert!(!tick_clear_main(1, 1)); + assert!(tick_clear_main(0x3e, 0x3f)); + assert!(tick_clear_main(0x3f, 0x3e)); + assert!(!tick_clear_main(u32::MAX, u32::MAX)); + assert!(tick_clear_main(0, 0)); +} + +#[test] +fn steps_render_no_buffer() { + for bits in 0..8_u8 { + let mut s = source(); + s.has_buffer = false; + s.custom = bits & 1 != 0; + s.has_data = bits & 2 != 0; + s.has_mix = bits & 4 != 0; + audio_render(&mut s, 0x3f, 2, TICK_BYTES); + assert!(s.audio_pending); + assert!(s.calls.is_empty()); + assert_eq!(s.audio_ts, 1000); + } +} + +#[test] +fn steps_render_custom_no_data() { + let mut s = source(); + s.custom = true; + s.has_data = false; + s.has_mix = true; + audio_render(&mut s, 0x3f, 2, TICK_BYTES); + assert!(s.audio_pending); + assert!(s.calls.is_empty()); + assert_eq!(s.audio_ts, 1000); +} + +#[test] +fn steps_render_custom_success() { + use Call::*; + let mut s = source(); + s.custom = true; + s.has_mix = true; + s.audio_pending = true; + s.audio_mixers = 0x05; + audio_render(&mut s, 0x03, 2, TICK_BYTES); + assert_eq!( + s.calls, + [ + Memset(0, 2 * TICK_BYTES), + Render(0x03, 2), + Lock, + Memset(1, 2 * TICK_BYTES), + Volume(0x03, 2), + Unlock + ] + ); + assert_eq!(s.audio_ts, 2000); + assert!(!s.audio_pending); + + // every mix in both masks, so nothing cleared after + let mut s = source(); + s.custom = true; + audio_render(&mut s, 0x3f, 8, TICK_BYTES); + let mut expected: Vec = (0..MIXES).map(|m| Memset(m, 8 * TICK_BYTES)).collect(); + expected.extend([Render(0x3f, 8), Lock, Volume(0x3f, 8), Unlock]); + assert_eq!(s.calls, expected); + + // the source on no mix: nothing before, every tick mix after + let mut s = source(); + s.custom = true; + s.audio_mixers = 0; + audio_render(&mut s, 0x21, 1, TICK_BYTES); + assert_eq!( + s.calls, + [ + Render(0x21, 1), + Lock, + Memset(0, TICK_BYTES), + Memset(5, TICK_BYTES), + Volume(0x21, 1), + Unlock + ] + ); + + // characterized, not endorsed: tick mixers past the six mixes clear + // nothing and still reach the volume + let mut s = source(); + s.custom = true; + audio_render(&mut s, 0x40, 2, TICK_BYTES); + assert_eq!(s.calls, [Render(0x40, 2), Lock, Volume(0x40, 2), Unlock]); + let mut s = source(); + s.custom = true; + s.audio_mixers = u32::MAX; + audio_render(&mut s, u32::MAX, 2, TICK_BYTES); + assert_eq!(s.calls.len(), 6 + 4); +} + +#[test] +fn steps_render_custom_failure() { + use Call::*; + let mut s = source(); + s.custom = true; + s.cb_success = false; + s.cb_ts = 5000; + s.audio_mixers = 0x01; + audio_render(&mut s, 0x3f, 2, TICK_BYTES); + assert_eq!( + s.calls, + [Memset(0, 2 * TICK_BYTES), Render(0x3f, 2), Lock, Unlock] + ); + assert_eq!(s.audio_ts, 0); + assert!(s.audio_pending); +} + +#[test] +fn steps_render_custom_unmixed() { + use Call::*; + let mut s = source(); + s.custom = true; + s.cb_ts = 0; + s.audio_pending = true; + audio_render(&mut s, 0x01, 2, TICK_BYTES); + assert_eq!( + s.calls, + [Memset(0, 2 * TICK_BYTES), Render(0x01, 2), Lock, Unlock] + ); + assert_eq!(s.audio_ts, 0); + assert!(!s.audio_pending); + + let mut s = source(); + s.custom = true; + audio_render(&mut s, 0, 2, TICK_BYTES); + assert_eq!(s.calls, [Render(0, 2), Lock, Unlock]); + assert_eq!(s.audio_ts, 2000); + assert!(!s.audio_pending); +} + +#[test] +fn steps_render_tick_pending() { + use Call::*; + let mut s = source(); + s.audio_ts = 0; + s.has_mix = true; + audio_render(&mut s, 0x3f, 2, TICK_BYTES); + assert_eq!(s.calls, [Submix(2), Lock, Unlock]); + assert!(s.audio_pending); + + let mut s = source(); + s.input_size = TICK_BYTES - 1; + audio_render(&mut s, 0x3f, 2, TICK_BYTES); + assert_eq!(s.calls, [Lock, Unlock]); + assert!(s.audio_pending); + + let mut s = source(); + s.input_size = 0; + s.audio_ts = u64::MAX; + audio_render(&mut s, 0x3f, 2, 1); + assert!(s.audio_pending); + + // exactly enough + let mut s = source(); + s.audio_pending = true; + audio_render(&mut s, 0x01, 1, TICK_BYTES); + assert_eq!( + s.calls, + [ + Lock, + Peek(0, TICK_BYTES), + Memset(1, TICK_BYTES), + Memset(2, TICK_BYTES), + Memset(3, TICK_BYTES), + Memset(4, TICK_BYTES), + Memset(5, TICK_BYTES), + Volume(0x01, 1), + Unlock + ] + ); + assert!(!s.audio_pending); + + // more than enough, and a size of 0 with no input + let mut s = source(); + s.input_size = TICK_BYTES + 1; + audio_render(&mut s, 0x01, 1, TICK_BYTES); + assert!(!s.audio_pending); + let mut s = source(); + s.input_size = 0; + audio_render(&mut s, 0x01, 1, 0); + assert!(!s.audio_pending); + assert_eq!(s.calls[1], Peek(0, 0)); +} + +/// The tick's calls written out from the C's rules. +fn expected_tick(audio_mixers: u32, mixers: u32, channels: usize, size: usize) -> Vec { + let mut expected = vec![Call::Lock]; + expected.extend((0..channels).map(|ch| Call::Peek(ch, size))); + for mix in 1..MIXES { + if has_mix(audio_mixers, mix) && has_mix(mixers, mix) { + expected.extend((0..channels).map(|ch| Call::Memcpy(mix * CHANNELS + ch, size))); + } else { + expected.push(Call::Memset(mix, size.wrapping_mul(channels))); + } + } + if !(has_mix(audio_mixers, 0) && has_mix(mixers, 0)) { + expected.push(Call::Memset(0, size.wrapping_mul(channels))); + } + expected.extend([Call::Volume(mixers, channels), Call::Unlock]); + expected +} + +#[test] +fn steps_render_tick_every_mask() { + let masks: Vec = (0..64).chain(MASKS).collect(); + for &audio_mixers in &masks { + for &mixers in &masks { + let mut s = source(); + s.audio_mixers = audio_mixers; + audio_render(&mut s, mixers, 2, TICK_BYTES); + assert_eq!( + s.calls, + expected_tick(audio_mixers, mixers, 2, TICK_BYTES), + "{audio_mixers:#x} {mixers:#x}" + ); + assert!(!s.audio_pending); + } + } +} + +#[test] +fn steps_render_tick_submix() { + use Call::*; + for a in MASKS { + for m in MASKS { + let mut s = source(); + s.submix = true; + s.has_mix = true; + s.audio_pending = true; + s.audio_mixers = a; + audio_render(&mut s, m, 2, TICK_BYTES); + if has_mix(a, 0) { + assert_eq!( + s.calls, + [ + Submix(2), + Lock, + Peek(0, TICK_BYTES), + Peek(1, TICK_BYTES), + Memcpy(CHANNELS, TICK_BYTES), + Memcpy(CHANNELS + 1, TICK_BYTES), + Unlock + ] + ); + } else { + assert_eq!( + s.calls, + [ + Submix(2), + Lock, + Peek(0, TICK_BYTES), + Peek(1, TICK_BYTES), + Memset(1, 2 * TICK_BYTES), + Unlock + ] + ); + } + assert!(!s.audio_pending); + } + } + + // without a submix callback, and with no channels + let mut s = source(); + s.submix = true; + s.audio_mixers = 1; + audio_render(&mut s, 0, 0, TICK_BYTES); + assert_eq!(s.calls, [Lock, Unlock]); +} + +#[test] +fn steps_render_mix_callback_without_flag() { + let mut s = source(); + s.has_mix = true; + audio_render(&mut s, 0x01, 1, TICK_BYTES); + assert_eq!(s.calls[0], Call::Submix(1)); + assert_eq!(s.calls[s.calls.len() - 2], Call::Volume(0x01, 1)); +} + +// --------------------------------------------------------------------------- +// edge_*: new + +/// Every mix index up to 64: the bit tests against `u64` shifts, and a mix +/// of 32 or more names no mix (an intentional difference: the C's `int` +/// shift is undefined from 31 on; libobs passes a mix below 6). +#[test] +fn edge_mix_index_past_the_mask() { + for mix in 0..=64_usize { + for a in MASKS { + for m in MASKS { + let in_a = mix < 32 && (u64::from(a) >> mix) & 1 == 1; + let in_m = mix < 32 && (u64::from(m) >> mix) & 1 == 1; + assert_eq!( + custom_clear_before(a, m, mix), + in_a && in_m, + "{a:#x} {m:#x} {mix}" + ); + assert_eq!(custom_in_tick(m, mix), in_m); + assert_eq!(custom_clear_after(a, mix), !in_a); + assert_eq!(tick_copy(false, a, m, mix), in_a && in_m); + assert_eq!(tick_copy(true, a, m, mix), a & 1 == 1); + } + } + } + assert!(custom_clear_before(0x8000_0000, 0x8000_0000, 31)); + assert!(!custom_clear_before(u32::MAX, u32::MAX, 32)); + assert!(custom_clear_after(u32::MAX, usize::MAX)); + assert!(!custom_in_tick(u32::MAX, usize::MAX)); + assert!(!tick_copy(false, u32::MAX, u32::MAX, usize::MAX)); +} + +/// The custom render with any inputs, against the C's rules written out. +fn expected_custom( + audio_mixers: u32, + mixers: u32, + channels: usize, + success: bool, + ts: u64, +) -> Vec { + let mut expected = Vec::new(); + for mix in 0..MIXES { + if has_mix(audio_mixers & mixers, mix) { + expected.push(Call::Memset(mix, TICK_BYTES * channels)); + } + } + expected.extend([Call::Render(mixers, channels), Call::Lock]); + if !success || ts == 0 || mixers == 0 { + expected.push(Call::Unlock); + return expected; + } + for mix in 0..MIXES { + if has_mix(mixers, mix) && !has_mix(audio_mixers, mix) { + expected.push(Call::Memset(mix, TICK_BYTES * channels)); + } + } + expected.extend([Call::Volume(mixers, channels), Call::Unlock]); + expected +} + +fn mask() -> impl Strategy { + prop_oneof![ + prop::sample::select(MASKS.to_vec()), + 0..64_u32, + any::() + ] +} + +fn timestamp() -> impl Strategy { + prop_oneof![Just(0_u64), Just(1), Just(u64::MAX), any::()] +} + +fn size() -> impl Strategy { + prop_oneof![ + Just(0_usize), + Just(TICK_BYTES), + Just(usize::MAX), + any::() + ] +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(20_000))] + + #[test] + fn edge_decisions_any_values( + a in mask(), + m in mask(), + mix in 0..80_usize, + ts in timestamp(), + input in size(), + want in size(), + success: bool, + submix: bool, + ) { + let in_a = mix < 32 && (a >> mix) & 1 == 1; + let in_m = mix < 32 && (m >> mix) & 1 == 1; + prop_assert_eq!(custom_clear_before(a, m, mix), in_a && in_m); + prop_assert_eq!(custom_in_tick(m, mix), in_m); + prop_assert_eq!(custom_clear_after(a, mix), !in_a); + prop_assert_eq!(custom_ts(success, ts), if success { ts } else { 0 }); + prop_assert_eq!(custom_pending(success), !success); + prop_assert_eq!(custom_unmixed(ts, m), ts == 0 || m == 0); + prop_assert_eq!(tick_unset(ts), ts == 0); + prop_assert_eq!(tick_short(input, want), u128::try_from(input).unwrap() < u128::try_from(want).unwrap()); + prop_assert_eq!(tick_stop(submix, mix), submix && mix >= 2); + prop_assert_eq!(tick_copy(submix, a, m, mix), if submix { a & 1 == 1 } else { in_a && in_m }); + prop_assert_eq!(tick_volume(submix), !submix); + prop_assert_eq!(tick_clear_main(a, m), a & m & 1 == 0); + } + + #[test] + fn edge_render_any_inputs( + bits in 0..64_u8, + audio_ts in timestamp(), + audio_mixers in mask(), + mixers in mask(), + input_size in size(), + tick_size in size(), + channels in 0..=CHANNELS, + cb_ts in timestamp(), + ) { + let mut s = source(); + s.has_buffer = bits & 1 != 0; + s.custom = bits & 2 != 0; + s.has_data = bits & 4 != 0; + s.has_mix = bits & 8 != 0; + s.submix = bits & 16 != 0; + s.cb_success = bits & 32 != 0; + s.audio_ts = audio_ts; + s.audio_mixers = audio_mixers; + s.input_size = input_size; + s.cb_ts = cb_ts; + let before = s.clone(); + audio_render(&mut s, mixers, channels, tick_size); + + if !before.has_buffer || (before.custom && !before.has_data) { + prop_assert!(s.calls.is_empty()); + prop_assert!(s.audio_pending); + prop_assert_eq!(s.audio_ts, audio_ts); + } else if before.custom { + prop_assert_eq!(&s.calls, &expected_custom(audio_mixers, mixers, channels, before.cb_success, cb_ts)); + prop_assert_eq!(s.audio_ts, if before.cb_success { cb_ts } else { 0 }); + prop_assert_eq!(s.audio_pending, !before.cb_success); + } else { + let calls = if before.has_mix { &s.calls[1..] } else { &s.calls[..] }; + if before.has_mix { + prop_assert_eq!(s.calls[0], Call::Submix(channels)); + } + if audio_ts == 0 || input_size < tick_size { + prop_assert_eq!(calls, &[Call::Lock, Call::Unlock][..]); + prop_assert!(s.audio_pending); + } else if before.submix { + let mut expected = vec![Call::Lock]; + expected.extend((0..channels).map(|ch| Call::Peek(ch, tick_size))); + if audio_mixers & 1 == 1 { + expected.extend((0..channels).map(|ch| Call::Memcpy(CHANNELS + ch, tick_size))); + } else { + expected.push(Call::Memset(1, tick_size.wrapping_mul(channels))); + } + expected.push(Call::Unlock); + prop_assert_eq!(calls, &expected[..]); + prop_assert!(!s.audio_pending); + } else { + prop_assert_eq!(calls, &expected_tick(audio_mixers, mixers, channels, tick_size)[..]); + prop_assert!(!s.audio_pending); + } + prop_assert_eq!(s.audio_ts, audio_ts); + } + } +} diff --git a/rust/obs-source/tests/source/audio_render_plan_parity.rs b/rust/obs-source/tests/source/audio_render_plan_parity.rs new file mode 100644 index 0000000000..203df3b1b0 --- /dev/null +++ b/rust/obs-source/tests/source/audio_render_plan_parity.rs @@ -0,0 +1,636 @@ +//! Tier 3: the `obs_source_audio_render_*` shims and the safe core against +//! the original C (`obs-c-oracle`, `oracle/source_audio_render.c`: the +//! unmodified `libobs/obs-source-audio-render.c.h` with its functions +//! renamed). +//! +//! The thirteen functions take and return plain values, so a run compares +//! the route as the `u32` the C returns, the timestamp as a `u64`, and each +//! `bool` as the byte the C returns (it must be 0 or 1, and the same on +//! both sides). Each input is given to the oracle, to the shim (the +//! `extern "C"` function the hybrid links) and to the core. +//! +//! Runs: +//! - the enum's values and size against the C header; +//! - the route over all eight inputs; +//! - every mask decision exhaustively over every pair of the low seven mask +//! bits (2^14 pairs), with no high bits and with all of them, for every +//! mix index of 0 to 30, submix on and off (the submix copy and the stop +//! also for any index up to 2^16 and `usize::MAX`, where the C does not +//! shift); +//! - the timestamp, pending and size decisions over every pair of 30 +//! special `u64` and `usize` values, at and around the tick sizes and the +//! ends; +//! - the three callers of `obs-source.c`, copied onto a stand-in source and +//! run once over the oracle and once over the shims, with the lock, the +//! peeks, every memset (with its length) and memcpy, the callbacks and the +//! volume recorded: every combination of the six source flags with every +//! pair of the low seven mask bits and edge sizes, and a proptest over +//! full-range masks, timestamps and sizes; the recorded calls, `audio_ts` +//! and `audio_pending` must match; +//! - proptests over any masks, any `u64` timestamps and any `usize` sizes +//! for each decision. +//! +//! MSVC x64: nothing here divides or reaches `util_mul_div64()`, so neither +//! `_udiv128` nor `INT_MIN / -1` can trap; no input is guarded. +//! +//! Intentional difference (tested in Tier 1 only): a mix of 31 or more for +//! the shifting decisions, where the C's `int` shift is undefined. + +use obs_c_oracle::source_audio_render as oracle; +use obs_source::audio_render_plan::{self as core, Route}; +use obs_source::ffi::source_audio_render as shim; +use proptest::prelude::*; + +const MIXES: usize = 6; +const CHANNELS: usize = 8; +const TICK_BYTES: usize = 4096; +/// The C shifts an `int` by the mix: defined up to 30. +const DEFINED_MIXES: usize = 31; + +#[track_caller] +fn c_bool(byte: u8, what: &str) -> bool { + assert!(byte <= 1, "{what}: the C returned the byte {byte}"); + byte == 1 +} + +fn route_u32(route: Route) -> u32 { + u32::from(route) +} + +/// One side of the comparison: the C oracle or the Rust shims. +struct Side { + route: fn(bool, bool, bool) -> u32, + custom_clear_before: fn(u32, u32, usize) -> bool, + custom_ts: fn(bool, u64) -> u64, + custom_pending: fn(bool) -> bool, + custom_unmixed: fn(u64, u32) -> bool, + custom_in_tick: fn(u32, usize) -> bool, + custom_clear_after: fn(u32, usize) -> bool, + tick_unset: fn(u64) -> bool, + tick_short: fn(usize, usize) -> bool, + tick_stop: fn(bool, usize) -> bool, + tick_copy: fn(bool, u32, u32, usize) -> bool, + tick_volume: fn(bool) -> bool, + tick_clear_main: fn(u32, u32) -> bool, +} + +// SAFETY (every oracle call below): plain values in and out; the C touches +// no memory. +const ORACLE: Side = Side { + route: |b, c, d| unsafe { oracle::oracle_obs_source_audio_render_route(b, c, d) }, + custom_clear_before: |a, m, x| { + c_bool( + unsafe { oracle::oracle_obs_source_audio_render_custom_clear_before(a, m, x) }, + "custom_clear_before", + ) + }, + custom_ts: |s, t| unsafe { oracle::oracle_obs_source_audio_render_custom_ts(s, t) }, + custom_pending: |s| { + c_bool( + unsafe { oracle::oracle_obs_source_audio_render_custom_pending(s) }, + "custom_pending", + ) + }, + custom_unmixed: |t, m| { + c_bool( + unsafe { oracle::oracle_obs_source_audio_render_custom_unmixed(t, m) }, + "custom_unmixed", + ) + }, + custom_in_tick: |m, x| { + c_bool( + unsafe { oracle::oracle_obs_source_audio_render_custom_in_tick(m, x) }, + "custom_in_tick", + ) + }, + custom_clear_after: |a, x| { + c_bool( + unsafe { oracle::oracle_obs_source_audio_render_custom_clear_after(a, x) }, + "custom_clear_after", + ) + }, + tick_unset: |t| { + c_bool( + unsafe { oracle::oracle_obs_source_audio_render_tick_unset(t) }, + "tick_unset", + ) + }, + tick_short: |i, s| { + c_bool( + unsafe { oracle::oracle_obs_source_audio_render_tick_short(i, s) }, + "tick_short", + ) + }, + tick_stop: |s, x| { + c_bool( + unsafe { oracle::oracle_obs_source_audio_render_tick_stop(s, x) }, + "tick_stop", + ) + }, + tick_copy: |s, a, m, x| { + c_bool( + unsafe { oracle::oracle_obs_source_audio_render_tick_copy(s, a, m, x) }, + "tick_copy", + ) + }, + tick_volume: |s| { + c_bool( + unsafe { oracle::oracle_obs_source_audio_render_tick_volume(s) }, + "tick_volume", + ) + }, + tick_clear_main: |a, m| { + c_bool( + unsafe { oracle::oracle_obs_source_audio_render_tick_clear_main(a, m) }, + "tick_clear_main", + ) + }, +}; + +const SHIM: Side = Side { + route: |a, b, c| shim::obs_source_audio_render_route(a, b, c), + custom_clear_before: |a, b, c| shim::obs_source_audio_render_custom_clear_before(a, b, c), + custom_ts: |a, b| shim::obs_source_audio_render_custom_ts(a, b), + custom_pending: |a| shim::obs_source_audio_render_custom_pending(a), + custom_unmixed: |a, b| shim::obs_source_audio_render_custom_unmixed(a, b), + custom_in_tick: |a, b| shim::obs_source_audio_render_custom_in_tick(a, b), + custom_clear_after: |a, b| shim::obs_source_audio_render_custom_clear_after(a, b), + tick_unset: |a| shim::obs_source_audio_render_tick_unset(a), + tick_short: |a, b| shim::obs_source_audio_render_tick_short(a, b), + tick_stop: |a, b| shim::obs_source_audio_render_tick_stop(a, b), + tick_copy: |a, b, c, d| shim::obs_source_audio_render_tick_copy(a, b, c, d), + tick_volume: |a| shim::obs_source_audio_render_tick_volume(a), + tick_clear_main: |a, b| shim::obs_source_audio_render_tick_clear_main(a, b), +}; + +const CORE: Side = Side { + route: |b, c, d| route_u32(core::route(b, c, d)), + custom_clear_before: core::custom_clear_before, + custom_ts: core::custom_ts, + custom_pending: core::custom_pending, + custom_unmixed: core::custom_unmixed, + custom_in_tick: core::custom_in_tick, + custom_clear_after: core::custom_clear_after, + tick_unset: core::tick_unset, + tick_short: core::tick_short, + tick_stop: core::tick_stop, + tick_copy: core::tick_copy, + tick_volume: core::tick_volume, + tick_clear_main: core::tick_clear_main, +}; + +/// Every 7-bit mask, alone and with every high bit set. +fn masks7() -> Vec { + (0..128_u32) + .chain((0..128_u32).map(|m| m | 0xffff_ff80)) + .collect() +} + +const U64S: [u64; 30] = [ + 0, + 1, + 2, + 3, + 0x3f, + 0x40, + 1000, + 1 << 31, + (1 << 32) - 1, + 1 << 32, + (1 << 32) + 1, + 21_333_333, + 1_000_000_000, + 1 << 53, + (1 << 63) - 1, + 1 << 63, + (1 << 63) + 1, + u64::MAX - 2, + u64::MAX - 1, + u64::MAX, + 4095, + 4096, + 4097, + 8191, + 8192, + 32768, + 32769, + 0x5555_5555_5555_5555, + 0xaaaa_aaaa_aaaa_aaaa, + 0xdead_beef_cafe_f00d, +]; + +fn usizes() -> Vec { + U64S.iter().map(|&v| v as usize).collect() +} + +#[test] +fn parity_enum_values() { + // SAFETY: plain values. + unsafe { + assert_eq!(oracle::oracle_source_audio_render_route_value(0), 0); + assert_eq!(oracle::oracle_source_audio_render_route_value(1), 1); + assert_eq!(oracle::oracle_source_audio_render_route_value(2), 2); + assert_eq!(oracle::oracle_source_audio_render_route_size(), 4); + assert_eq!( + oracle::oracle_source_audio_render_route_value(0), + shim::OBS_SOURCE_AUDIO_RENDER_PENDING + ); + assert_eq!( + oracle::oracle_source_audio_render_route_value(1), + shim::OBS_SOURCE_AUDIO_RENDER_CUSTOM + ); + assert_eq!( + oracle::oracle_source_audio_render_route_value(2), + shim::OBS_SOURCE_AUDIO_RENDER_TICK + ); + } + assert_eq!(route_u32(Route::Pending), 0); + assert_eq!(route_u32(Route::Custom), 1); + assert_eq!(route_u32(Route::Tick), 2); +} + +#[test] +fn parity_route_every_input() { + for bits in 0..8_u8 { + let (b, c, d) = (bits & 1 != 0, bits & 2 != 0, bits & 4 != 0); + let o = (ORACLE.route)(b, c, d); + assert_eq!((SHIM.route)(b, c, d), o, "{bits}"); + assert_eq!((CORE.route)(b, c, d), o, "{bits}"); + } +} + +#[test] +fn parity_mask_decisions_exhaustive() { + let masks = masks7(); + let mut calls = 0_u64; + for &a in &masks { + for mix in 0..DEFINED_MIXES { + let o = (ORACLE.custom_in_tick)(a, mix); + assert_eq!((SHIM.custom_in_tick)(a, mix), o); + assert_eq!((CORE.custom_in_tick)(a, mix), o); + let o = (ORACLE.custom_clear_after)(a, mix); + assert_eq!((SHIM.custom_clear_after)(a, mix), o); + assert_eq!((CORE.custom_clear_after)(a, mix), o); + } + for &m in &masks { + let o = (ORACLE.tick_clear_main)(a, m); + assert_eq!((SHIM.tick_clear_main)(a, m), o); + assert_eq!((CORE.tick_clear_main)(a, m), o); + for mix in 0..DEFINED_MIXES { + let o = (ORACLE.custom_clear_before)(a, m, mix); + assert_eq!( + (SHIM.custom_clear_before)(a, m, mix), + o, + "{a:#x} {m:#x} {mix}" + ); + assert_eq!( + (CORE.custom_clear_before)(a, m, mix), + o, + "{a:#x} {m:#x} {mix}" + ); + for submix in [false, true] { + let o = (ORACLE.tick_copy)(submix, a, m, mix); + assert_eq!((SHIM.tick_copy)(submix, a, m, mix), o); + assert_eq!((CORE.tick_copy)(submix, a, m, mix), o); + } + calls += 4; + } + } + } + assert_eq!(calls, 256 * 256 * 31 * 4); +} + +#[test] +fn parity_submix_any_index() { + // A submix source's copy does not shift and the stop only compares, so + // the C is defined for every index. + let indexes: Vec = (0..=(1 << 16)) + .chain([usize::MAX - 1, usize::MAX, 1 << 31, 1 << 32]) + .collect(); + for &mix in &indexes { + for submix in [false, true] { + let o = (ORACLE.tick_stop)(submix, mix); + assert_eq!((SHIM.tick_stop)(submix, mix), o, "{submix} {mix}"); + assert_eq!((CORE.tick_stop)(submix, mix), o, "{submix} {mix}"); + } + for a in [0, 1, 2, 3, 0xfe, u32::MAX] { + for m in [0, 1, 2, u32::MAX] { + let o = (ORACLE.tick_copy)(true, a, m, mix); + assert_eq!((SHIM.tick_copy)(true, a, m, mix), o); + assert_eq!((CORE.tick_copy)(true, a, m, mix), o); + } + } + } + for submix in [false, true] { + let o = (ORACLE.tick_volume)(submix); + assert_eq!((SHIM.tick_volume)(submix), o); + assert_eq!((CORE.tick_volume)(submix), o); + } +} + +#[test] +fn parity_value_decisions_special() { + let sizes = usizes(); + for &ts in &U64S { + let o = (ORACLE.tick_unset)(ts); + assert_eq!((SHIM.tick_unset)(ts), o); + assert_eq!((CORE.tick_unset)(ts), o); + for success in [false, true] { + let o = (ORACLE.custom_ts)(success, ts); + assert_eq!((SHIM.custom_ts)(success, ts), o); + assert_eq!((CORE.custom_ts)(success, ts), o); + } + for m in masks7() { + let o = (ORACLE.custom_unmixed)(ts, m); + assert_eq!((SHIM.custom_unmixed)(ts, m), o); + assert_eq!((CORE.custom_unmixed)(ts, m), o); + } + } + for success in [false, true] { + let o = (ORACLE.custom_pending)(success); + assert_eq!((SHIM.custom_pending)(success), o); + assert_eq!((CORE.custom_pending)(success), o); + } + for &input in &sizes { + for &size in &sizes { + let o = (ORACLE.tick_short)(input, size); + assert_eq!((SHIM.tick_short)(input, size), o, "{input} {size}"); + assert_eq!((CORE.tick_short)(input, size), o, "{input} {size}"); + } + } +} + +// --------------------------------------------------------------------------- +// The callers, copied from obs-source.c onto a stand-in source + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Call { + Lock, + Unlock, + Peek(usize, usize), + Memset(usize, usize), + Memcpy(usize, usize), + Render(u32, usize), + Submix(usize), + Volume(u32, usize), +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct StandIn { + has_buffer: bool, + custom: bool, + has_data: bool, + has_mix: bool, + submix: bool, + cb_success: bool, + audio_ts: u64, + audio_pending: bool, + audio_mixers: u32, + input_size: usize, + cb_ts: u64, + calls: Vec, +} + +fn custom_audio_render(side: &Side, s: &mut StandIn, mixers: u32, channels: usize) { + for mix in 0..MIXES { + if (side.custom_clear_before)(s.audio_mixers, mixers, mix) { + s.calls + .push(Call::Memset(mix, TICK_BYTES.wrapping_mul(channels))); + } + } + s.calls.push(Call::Render(mixers, channels)); + let success = s.cb_success; + let ts = s.cb_ts; + s.calls.push(Call::Lock); + s.audio_ts = (side.custom_ts)(success, ts); + s.audio_pending = (side.custom_pending)(success); + if (side.custom_pending)(success) || (side.custom_unmixed)(s.audio_ts, mixers) { + s.calls.push(Call::Unlock); + return; + } + for mix in 0..MIXES { + if !(side.custom_in_tick)(mixers, mix) { + continue; + } + if (side.custom_clear_after)(s.audio_mixers, mix) { + s.calls + .push(Call::Memset(mix, TICK_BYTES.wrapping_mul(channels))); + } + } + s.calls.push(Call::Volume(mixers, channels)); + s.calls.push(Call::Unlock); +} + +fn process_audio_source_tick( + side: &Side, + s: &mut StandIn, + mixers: u32, + channels: usize, + size: usize, +) { + let audio_submix = s.submix; + s.calls.push(Call::Lock); + if (side.tick_unset)(s.audio_ts) || (side.tick_short)(s.input_size, size) { + s.audio_pending = true; + s.calls.push(Call::Unlock); + return; + } + for ch in 0..channels { + s.calls.push(Call::Peek(ch, size)); + } + for mix in 1..MIXES { + if (side.tick_stop)(audio_submix, mix) { + break; + } + if !(side.tick_copy)(audio_submix, s.audio_mixers, mixers, mix) { + s.calls.push(Call::Memset(mix, size.wrapping_mul(channels))); + continue; + } + for ch in 0..channels { + s.calls.push(Call::Memcpy(mix * CHANNELS + ch, size)); + } + } + if !(side.tick_volume)(audio_submix) { + s.audio_pending = false; + s.calls.push(Call::Unlock); + return; + } + if (side.tick_clear_main)(s.audio_mixers, mixers) { + s.calls.push(Call::Memset(0, size.wrapping_mul(channels))); + } + s.calls.push(Call::Volume(mixers, channels)); + s.audio_pending = false; + s.calls.push(Call::Unlock); +} + +fn audio_render(side: &Side, s: &mut StandIn, mixers: u32, channels: usize, size: usize) { + let has_buffer = s.has_buffer; + let custom = has_buffer && s.custom; + let has_data = custom && s.has_data; + match (side.route)(has_buffer, custom, has_data) { + 0 => { + s.audio_pending = true; + return; + } + 1 => { + custom_audio_render(side, s, mixers, channels); + return; + } + _ => {} + } + if s.has_mix { + s.calls.push(Call::Submix(channels)); + } + process_audio_source_tick(side, s, mixers, channels, size); +} + +#[allow(clippy::too_many_arguments)] +fn stand_in(bits: u8, audio_ts: u64, audio_mixers: u32, input_size: usize, cb_ts: u64) -> StandIn { + StandIn { + has_buffer: bits & 1 != 0, + custom: bits & 2 != 0, + has_data: bits & 4 != 0, + has_mix: bits & 8 != 0, + submix: bits & 16 != 0, + cb_success: bits & 32 != 0, + audio_ts, + audio_pending: bits & 64 != 0, + audio_mixers, + input_size, + cb_ts, + calls: Vec::new(), + } +} + +#[track_caller] +fn compare_render(s: &StandIn, mixers: u32, channels: usize, size: usize) { + let mut o = s.clone(); + audio_render(&ORACLE, &mut o, mixers, channels, size); + let mut r = s.clone(); + audio_render(&SHIM, &mut r, mixers, channels, size); + assert_eq!( + r, o, + "shim, mixers {mixers:#x} channels {channels} size {size}" + ); + let mut c = s.clone(); + audio_render(&CORE, &mut c, mixers, channels, size); + assert_eq!( + c, o, + "core, mixers {mixers:#x} channels {channels} size {size}" + ); +} + +#[test] +fn parity_callers_every_flag_and_mask() { + let mut runs = 0_u64; + for bits in 0..128_u8 { + for a in 0..128_u32 { + for m in 0..128_u32 { + let s = stand_in(bits, 1000, a, TICK_BYTES, 2000); + compare_render(&s, m, 2, TICK_BYTES); + runs += 1; + } + } + } + assert_eq!(runs, 128 * 128 * 128); +} + +#[test] +fn parity_callers_edges() { + let edges_ts = [0_u64, 1, u64::MAX]; + let edge_masks = [0_u32, 1, 0x3f, 0x40, 0x41, 0x8000_0000, u32::MAX]; + let edge_sizes = [ + (0_usize, 0_usize), + (TICK_BYTES - 1, TICK_BYTES), + (TICK_BYTES, TICK_BYTES), + (TICK_BYTES + 1, TICK_BYTES), + (usize::MAX, usize::MAX), + (usize::MAX - 1, usize::MAX), + (0, 1), + ]; + for bits in 0..128_u8 { + for ts in edges_ts { + for cb_ts in edges_ts { + for a in edge_masks { + for m in edge_masks { + for (input, size) in edge_sizes { + for channels in [0, 1, 2, CHANNELS] { + let s = stand_in(bits, ts, a, input, cb_ts); + compare_render(&s, m, channels, size); + } + } + } + } + } + } + } +} + +fn mask() -> impl Strategy { + prop_oneof![0..128_u32, any::(), Just(u32::MAX)] +} + +fn timestamp() -> impl Strategy { + prop_oneof![Just(0_u64), Just(u64::MAX), any::()] +} + +fn size() -> impl Strategy { + prop_oneof![ + Just(0_usize), + Just(TICK_BYTES), + Just(usize::MAX), + any::() + ] +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(20_000))] + + #[test] + fn parity_decisions_any_values( + a in mask(), + m in mask(), + mix in 0..DEFINED_MIXES, + any_mix: usize, + ts in timestamp(), + input in size(), + want in size(), + success: bool, + submix: bool, + bits in 0..8_u8, + ) { + let sides = [&SHIM, &CORE]; + let (b, c, d) = (bits & 1 != 0, bits & 2 != 0, bits & 4 != 0); + for side in sides { + prop_assert_eq!((side.route)(b, c, d), (ORACLE.route)(b, c, d)); + prop_assert_eq!((side.custom_clear_before)(a, m, mix), (ORACLE.custom_clear_before)(a, m, mix)); + prop_assert_eq!((side.custom_ts)(success, ts), (ORACLE.custom_ts)(success, ts)); + prop_assert_eq!((side.custom_pending)(success), (ORACLE.custom_pending)(success)); + prop_assert_eq!((side.custom_unmixed)(ts, m), (ORACLE.custom_unmixed)(ts, m)); + prop_assert_eq!((side.custom_in_tick)(m, mix), (ORACLE.custom_in_tick)(m, mix)); + prop_assert_eq!((side.custom_clear_after)(a, mix), (ORACLE.custom_clear_after)(a, mix)); + prop_assert_eq!((side.tick_unset)(ts), (ORACLE.tick_unset)(ts)); + prop_assert_eq!((side.tick_short)(input, want), (ORACLE.tick_short)(input, want)); + prop_assert_eq!((side.tick_stop)(submix, any_mix), (ORACLE.tick_stop)(submix, any_mix)); + prop_assert_eq!((side.tick_copy)(submix, a, m, mix), (ORACLE.tick_copy)(submix, a, m, mix)); + prop_assert_eq!((side.tick_copy)(true, a, m, any_mix), (ORACLE.tick_copy)(true, a, m, any_mix)); + prop_assert_eq!((side.tick_volume)(submix), (ORACLE.tick_volume)(submix)); + prop_assert_eq!((side.tick_clear_main)(a, m), (ORACLE.tick_clear_main)(a, m)); + } + } + + #[test] + fn parity_callers_any_inputs( + bits in 0..128_u8, + ts in timestamp(), + cb_ts in timestamp(), + a in mask(), + m in mask(), + input in size(), + want in size(), + channels in 0..=CHANNELS, + ) { + let s = stand_in(bits, ts, a, input, cb_ts); + compare_render(&s, m, channels, want); + } +} diff --git a/rust/obs-source/tests/source/main.rs b/rust/obs-source/tests/source/main.rs index 0742349596..dc220d3f28 100644 --- a/rust/obs-source/tests/source/main.rs +++ b/rust/obs-source/tests/source/main.rs @@ -4,6 +4,8 @@ mod async_pacing; mod async_pacing_parity; mod audio_input; mod audio_input_parity; +mod audio_render_plan; +mod audio_render_plan_parity; mod audio_timing; mod audio_timing_parity; mod color_space; diff --git a/test/cmocka/build/test-graph.json b/test/cmocka/build/test-graph.json index 8c055aafd2..aafa9578d1 100644 --- a/test/cmocka/build/test-graph.json +++ b/test/cmocka/build/test-graph.json @@ -2740,6 +2740,29 @@ ], "requires": "OS_LINUX" }, + "test_source_audio_render_steps": { + "sources": [ + "test/cmocka/test_source_audio_render_steps.c", + "libobs/obs-source-audio-render.c.h" + ], + "includes": [ + "libobs", + "deps/libcaption" + ], + "libraries": [ + "OBS::libobs", + "OBS::caption", + "Uthash::Uthash", + "OBS::native-test-cmocka", + { + "name": "m", + "requires": { + "not": "MSVC" + } + } + ], + "sanitizers": "address,undefined" + }, "test_source_audio_timing_steps": { "sources": [ "test/cmocka/test_source_audio_timing_steps.c", @@ -4713,6 +4736,12 @@ "${target}" ] }, + "test_source_audio_render_steps": { + "target": "test_source_audio_render_steps", + "command": [ + "${target}" + ] + }, "test_source_audio_timing_steps": { "target": "test_source_audio_timing_steps", "command": [ diff --git a/test/cmocka/test_source_audio_render_steps.c b/test/cmocka/test_source_audio_render_steps.c new file mode 100644 index 0000000000..9a86503971 --- /dev/null +++ b/test/cmocka/test_source_audio_render_steps.c @@ -0,0 +1,765 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "obs-source-audio-render.h" + +/* + * Characterization of the audio render mix plan of obs_source_audio_render(), + * custom_audio_render() and process_audio_source_tick() in + * libobs/obs-source.c, moved to libobs/obs-source-audio-render.c before its + * Rust port: + * - obs_source_audio_render_route(): pending without an output buffer or + * with a custom render but no data, the custom render, else the tick; + * - custom_audio_render(): the mixes cleared before the callback (in both + * masks), the timestamp and pending result, nothing more with no timestamp + * or no tick mixers, the mixes cleared after it (in the tick's mixers but + * not the source's); + * - process_audio_source_tick(): pending with no timestamp or less input + * than the tick needs; mixes 1 to 5 copy mix 0 when in both masks, else + * are cleared; a submix source does mix 1 only, from bit 0 of its mixers, + * and stops there; any other clears mix 0 unless it is in both masks and + * scales by its volume. + * Cases marked "characterized, not endorsed" are odd but real. + * + * The functions are hidden in libobs, so this test compiles the C source + * into itself and calls only them. A stand-in source holds the fields, and + * the three callers are copied from obs-source.c onto it, with the lock, + * deque_peek_front(), every memset and memcpy, the callbacks and + * apply_audio_volume() recorded instead of run, so the compositions are + * pinned too. test_source_volume_mix, test_source_audio_ranges, + * test_audio_capture_mix_race and test_group_hide_transition_audio cover the + * callers through the real libobs. + */ + +/* MAX_AUDIO_MIXES, MAX_AUDIO_CHANNELS and AUDIO_OUTPUT_FRAMES of + * media-io/audio-io.h */ +#define MIXES 6 +#define CHANNELS 8 +#define FRAMES 1024 +/* the bytes of one channel of a tick, AUDIO_OUTPUT_FRAMES floats */ +#define TICK_BYTES (FRAMES * sizeof(float)) + +static void check_u64(uint64_t actual, uint64_t expected, const char *what, int line) +{ + if (actual != expected) { + fprintf(stderr, "line %d: %s is %" PRIu64 " (0x%" PRIx64 "), expected %" PRIu64 " (0x%" PRIx64 ")\n", + line, what, actual, actual, expected, expected); + fail(); + } +} + +static void check_bool(bool actual, bool expected, const char *what, int line) +{ + if (actual != expected) { + fprintf(stderr, "line %d: %s is %s, expected %s\n", line, what, actual ? "true" : "false", + expected ? "true" : "false"); + fail(); + } +} + +#define CHECK_U64(actual, expected) check_u64((uint64_t)(actual), (uint64_t)(expected), #actual, __LINE__) +#define CHECK_BOOL(actual, expected) check_bool((actual), (expected), #actual, __LINE__) + +/* Mixer masks: none, each edge of the six mixes, past them, the top bit + * and all bits. */ +static const uint32_t MASKS[] = {0, 1, 2, 0x20, 0x3e, 0x3f, 0x40, 0x41, 0x7f, + 0x80, 0xff, 0x100, 0xfc0, 0x10000, 0x7fffffffu, 0x80000000u, 0xfffffffeu, UINT32_MAX}; +#define NUM_MASKS (sizeof(MASKS) / sizeof(MASKS[0])) + +static bool has_mix(uint32_t mask, size_t mix) +{ + return (mask >> mix) & 1u; +} + +/* ------------------------------------------------------------------------- + * obs_source_audio_render_route() */ + +static void route_values(void **state) +{ + (void)state; + + CHECK_U64(OBS_SOURCE_AUDIO_RENDER_PENDING, 0); + CHECK_U64(OBS_SOURCE_AUDIO_RENDER_CUSTOM, 1); + CHECK_U64(OBS_SOURCE_AUDIO_RENDER_TICK, 2); + CHECK_U64(sizeof(enum obs_source_audio_render_route), sizeof(int)); +} + +/* Every combination of the three inputs, including the ones the caller + * never passes (it reads custom only with a buffer and data only with + * custom): no buffer is pending whatever the rest, a custom render needs + * data, anything else ticks. */ +static void route_every_input(void **state) +{ + (void)state; + + for (int bits = 0; bits < 8; bits++) { + bool has_buffer = bits & 1, custom = bits & 2, has_data = bits & 4; + enum obs_source_audio_render_route expected = !has_buffer ? OBS_SOURCE_AUDIO_RENDER_PENDING + : !custom ? OBS_SOURCE_AUDIO_RENDER_TICK + : has_data ? OBS_SOURCE_AUDIO_RENDER_CUSTOM + : OBS_SOURCE_AUDIO_RENDER_PENDING; + CHECK_U64(obs_source_audio_render_route(has_buffer, custom, has_data), expected); + } + + CHECK_U64(obs_source_audio_render_route(false, false, false), OBS_SOURCE_AUDIO_RENDER_PENDING); + CHECK_U64(obs_source_audio_render_route(false, true, true), OBS_SOURCE_AUDIO_RENDER_PENDING); + CHECK_U64(obs_source_audio_render_route(true, true, false), OBS_SOURCE_AUDIO_RENDER_PENDING); + CHECK_U64(obs_source_audio_render_route(true, true, true), OBS_SOURCE_AUDIO_RENDER_CUSTOM); + /* data does not matter without a custom render */ + CHECK_U64(obs_source_audio_render_route(true, false, false), OBS_SOURCE_AUDIO_RENDER_TICK); + CHECK_U64(obs_source_audio_render_route(true, false, true), OBS_SOURCE_AUDIO_RENDER_TICK); +} + +/* ------------------------------------------------------------------------- + * custom_audio_render()'s decisions */ + +/* Cleared before the callback: the mixes in both masks. */ +static void custom_clear_before_masks(void **state) +{ + (void)state; + + CHECK_BOOL(obs_source_audio_render_custom_clear_before(0x3f, 0x3f, 0), true); + CHECK_BOOL(obs_source_audio_render_custom_clear_before(0x3f, 0x3f, 5), true); + CHECK_BOOL(obs_source_audio_render_custom_clear_before(0x01, 0x3f, 1), false); + CHECK_BOOL(obs_source_audio_render_custom_clear_before(0x3f, 0x01, 1), false); + CHECK_BOOL(obs_source_audio_render_custom_clear_before(0, UINT32_MAX, 0), false); + CHECK_BOOL(obs_source_audio_render_custom_clear_before(UINT32_MAX, 0, 0), false); + CHECK_BOOL(obs_source_audio_render_custom_clear_before(UINT32_MAX, UINT32_MAX, 3), true); + /* bits past the six mixes are never asked for */ + CHECK_BOOL(obs_source_audio_render_custom_clear_before(0x40, 0x40, 0), false); + + for (size_t a = 0; a < NUM_MASKS; a++) + for (size_t m = 0; m < NUM_MASKS; m++) + for (size_t mix = 0; mix < MIXES; mix++) + CHECK_BOOL(obs_source_audio_render_custom_clear_before(MASKS[a], MASKS[m], mix), + has_mix(MASKS[a], mix) && has_mix(MASKS[m], mix)); + /* every pair of the six mix bits */ + for (uint32_t a = 0; a < 64; a++) + for (uint32_t m = 0; m < 64; m++) + for (size_t mix = 0; mix < MIXES; mix++) + CHECK_BOOL(obs_source_audio_render_custom_clear_before(a, m, mix), has_mix(a & m, mix)); +} + +/* The callback's timestamp when it succeeds, else 0 and pending; the + * timestamp is not looked at when it failed. */ +static void custom_result(void **state) +{ + (void)state; + + static const uint64_t TS[] = {0, 1, 1000000000ull, 0x8000000000000000ull, UINT64_MAX}; + for (size_t i = 0; i < sizeof(TS) / sizeof(TS[0]); i++) { + CHECK_U64(obs_source_audio_render_custom_ts(true, TS[i]), TS[i]); + CHECK_U64(obs_source_audio_render_custom_ts(false, TS[i]), 0); + } + CHECK_BOOL(obs_source_audio_render_custom_pending(true), false); + CHECK_BOOL(obs_source_audio_render_custom_pending(false), true); +} + +/* After a successful callback: nothing is cleared or scaled with no + * timestamp (the callback said 0) or no tick mixers. Characterized, not + * endorsed: tick mixers past the six mixes (0x40 and up) are not "no + * mixers", so the clears look at each mix (none is in the tick) and the + * volume runs. */ +static void custom_unmixed(void **state) +{ + (void)state; + + static const uint64_t TS[] = {0, 1, UINT64_MAX}; + for (size_t t = 0; t < 3; t++) + for (size_t m = 0; m < NUM_MASKS; m++) + CHECK_BOOL(obs_source_audio_render_custom_unmixed(TS[t], MASKS[m]), + TS[t] == 0 || MASKS[m] == 0); + + CHECK_BOOL(obs_source_audio_render_custom_unmixed(0, UINT32_MAX), true); + CHECK_BOOL(obs_source_audio_render_custom_unmixed(1, 0), true); + CHECK_BOOL(obs_source_audio_render_custom_unmixed(1, 1), false); + CHECK_BOOL(obs_source_audio_render_custom_unmixed(1, 0x40), false); + CHECK_BOOL(obs_source_audio_render_custom_unmixed(UINT64_MAX, 0x80000000u), false); +} + +/* Cleared after the callback: a mix of the tick that the source is not + * on. */ +static void custom_clear_after_masks(void **state) +{ + (void)state; + + for (size_t m = 0; m < NUM_MASKS; m++) + for (size_t mix = 0; mix < MIXES; mix++) { + CHECK_BOOL(obs_source_audio_render_custom_in_tick(MASKS[m], mix), has_mix(MASKS[m], mix)); + CHECK_BOOL(obs_source_audio_render_custom_clear_after(MASKS[m], mix), !has_mix(MASKS[m], mix)); + } + + CHECK_BOOL(obs_source_audio_render_custom_in_tick(0, 0), false); + CHECK_BOOL(obs_source_audio_render_custom_in_tick(1, 0), true); + CHECK_BOOL(obs_source_audio_render_custom_in_tick(0x20, 5), true); + CHECK_BOOL(obs_source_audio_render_custom_in_tick(0x40, 5), false); + CHECK_BOOL(obs_source_audio_render_custom_clear_after(0, 0), true); + CHECK_BOOL(obs_source_audio_render_custom_clear_after(0x3f, 5), false); + CHECK_BOOL(obs_source_audio_render_custom_clear_after(0x1f, 5), true); + CHECK_BOOL(obs_source_audio_render_custom_clear_after(UINT32_MAX, 0), false); +} + +/* ------------------------------------------------------------------------- + * process_audio_source_tick()'s decisions */ + +/* Pending with no timestamp, or less buffered input than the tick reads: + * at the size edges. */ +static void tick_pending_edges(void **state) +{ + (void)state; + + CHECK_BOOL(obs_source_audio_render_tick_unset(0), true); + CHECK_BOOL(obs_source_audio_render_tick_unset(1), false); + CHECK_BOOL(obs_source_audio_render_tick_unset(UINT64_MAX), false); + + const size_t size = TICK_BYTES; + CHECK_BOOL(obs_source_audio_render_tick_short(size - 1, size), true); + CHECK_BOOL(obs_source_audio_render_tick_short(size, size), false); + CHECK_BOOL(obs_source_audio_render_tick_short(size + 1, size), false); + CHECK_BOOL(obs_source_audio_render_tick_short(0, size), true); + CHECK_BOOL(obs_source_audio_render_tick_short(0, 0), false); + CHECK_BOOL(obs_source_audio_render_tick_short(SIZE_MAX, size), false); + CHECK_BOOL(obs_source_audio_render_tick_short(SIZE_MAX - 1, SIZE_MAX), true); + CHECK_BOOL(obs_source_audio_render_tick_short(SIZE_MAX, SIZE_MAX), false); + CHECK_BOOL(obs_source_audio_render_tick_short(0, 1), true); +} + +/* A submix source stops after mix 1; any other walks mixes 1 to 5. */ +static void tick_stop_mixes(void **state) +{ + (void)state; + + for (size_t mix = 0; mix <= 64; mix++) { + CHECK_BOOL(obs_source_audio_render_tick_stop(false, mix), false); + CHECK_BOOL(obs_source_audio_render_tick_stop(true, mix), mix > 1); + } + CHECK_BOOL(obs_source_audio_render_tick_stop(true, SIZE_MAX), true); + CHECK_BOOL(obs_source_audio_render_tick_volume(false), true); + CHECK_BOOL(obs_source_audio_render_tick_volume(true), false); +} + +/* Copy mix 0 into the mix when it is in both masks, else clear it; a + * submix source copies into mix 1 when bit 0 of its mixers is set, + * whatever the tick's mixers (the C sets both masks to 1 there). */ +static void tick_copy_masks(void **state) +{ + (void)state; + + for (uint32_t a = 0; a < 64; a++) + for (uint32_t m = 0; m < 64; m++) + for (size_t mix = 1; mix < MIXES; mix++) { + CHECK_BOOL(obs_source_audio_render_tick_copy(false, a, m, mix), + has_mix(a, mix) && has_mix(m, mix)); + CHECK_BOOL(obs_source_audio_render_tick_copy(true, a, m, mix), has_mix(a, 0)); + } + for (size_t a = 0; a < NUM_MASKS; a++) + for (size_t m = 0; m < NUM_MASKS; m++) + for (size_t mix = 0; mix < MIXES; mix++) { + CHECK_BOOL(obs_source_audio_render_tick_copy(false, MASKS[a], MASKS[m], mix), + has_mix(MASKS[a], mix) && has_mix(MASKS[m], mix)); + CHECK_BOOL(obs_source_audio_render_tick_copy(true, MASKS[a], MASKS[m], mix), + has_mix(MASKS[a], 0)); + } + + /* submix: mix 1 from bit 0 even with no tick mixers, not from bit 1 */ + CHECK_BOOL(obs_source_audio_render_tick_copy(true, 1, 0, 1), true); + CHECK_BOOL(obs_source_audio_render_tick_copy(true, 2, UINT32_MAX, 1), false); + CHECK_BOOL(obs_source_audio_render_tick_copy(false, 2, 2, 1), true); + CHECK_BOOL(obs_source_audio_render_tick_copy(false, 1, 1, 1), false); + /* a submix source's mix index is not shifted, so any index answers */ + CHECK_BOOL(obs_source_audio_render_tick_copy(true, 1, 0, 64), true); +} + +/* Mix 0 is cleared before the volume unless it is in both masks. */ +static void tick_clear_main_masks(void **state) +{ + (void)state; + + for (size_t a = 0; a < NUM_MASKS; a++) + for (size_t m = 0; m < NUM_MASKS; m++) + CHECK_BOOL(obs_source_audio_render_tick_clear_main(MASKS[a], MASKS[m]), + !(has_mix(MASKS[a], 0) && has_mix(MASKS[m], 0))); + CHECK_BOOL(obs_source_audio_render_tick_clear_main(1, 1), false); + CHECK_BOOL(obs_source_audio_render_tick_clear_main(0x3e, 0x3f), true); + CHECK_BOOL(obs_source_audio_render_tick_clear_main(0x3f, 0x3e), true); + CHECK_BOOL(obs_source_audio_render_tick_clear_main(UINT32_MAX, UINT32_MAX), false); + CHECK_BOOL(obs_source_audio_render_tick_clear_main(0, 0), true); +} + +/* ------------------------------------------------------------------------- + * The callers, copied from obs-source.c onto a stand-in source */ + +enum call_kind { + CALL_LOCK = 1, + CALL_UNLOCK, + CALL_PEEK, + CALL_MEMSET, + CALL_MEMCPY, + CALL_RENDER, + CALL_SUBMIX, + CALL_VOLUME, +}; + +struct call { + int kind; + uint64_t a, b; +}; + +#define MAX_CALLS 128 + +struct stand_in { + /* fields the callers read */ + bool has_buffer; + bool custom; + bool has_data; + bool has_mix; + bool submix; + uint64_t audio_ts; + bool audio_pending; + uint32_t audio_mixers; + size_t input_size; + /* the audio_render callback's answer */ + bool cb_success; + uint64_t cb_ts; + /* recorded calls */ + struct call calls[MAX_CALLS]; + size_t num_calls; +}; + +static void record(struct stand_in *s, int kind, uint64_t a, uint64_t b) +{ + if (s->num_calls == MAX_CALLS) { + fprintf(stderr, "too many calls\n"); + fail(); + } + s->calls[s->num_calls++] = (struct call){kind, a, b}; +} + +static void custom_audio_render(struct stand_in *s, uint32_t mixers, size_t channels) +{ + bool success; + uint64_t ts; + + for (size_t mix = 0; mix < MIXES; mix++) { + if (obs_source_audio_render_custom_clear_before(s->audio_mixers, mixers, mix)) + record(s, CALL_MEMSET, mix, TICK_BYTES * channels); + } + + record(s, CALL_RENDER, mixers, channels); + success = s->cb_success; + ts = s->cb_ts; + record(s, CALL_LOCK, 0, 0); + s->audio_ts = obs_source_audio_render_custom_ts(success, ts); + s->audio_pending = obs_source_audio_render_custom_pending(success); + + if (obs_source_audio_render_custom_pending(success) || + obs_source_audio_render_custom_unmixed(s->audio_ts, mixers)) { + record(s, CALL_UNLOCK, 0, 0); + return; + } + + for (size_t mix = 0; mix < MIXES; mix++) { + if (!obs_source_audio_render_custom_in_tick(mixers, mix)) + continue; + if (obs_source_audio_render_custom_clear_after(s->audio_mixers, mix)) + record(s, CALL_MEMSET, mix, TICK_BYTES * channels); + } + + record(s, CALL_VOLUME, mixers, channels); + record(s, CALL_UNLOCK, 0, 0); +} + +static void process_audio_source_tick(struct stand_in *s, uint32_t mixers, size_t channels, size_t size) +{ + bool audio_submix = s->submix; + + record(s, CALL_LOCK, 0, 0); + + if (obs_source_audio_render_tick_unset(s->audio_ts) || + obs_source_audio_render_tick_short(s->input_size, size)) { + s->audio_pending = true; + record(s, CALL_UNLOCK, 0, 0); + return; + } + + for (size_t ch = 0; ch < channels; ch++) + record(s, CALL_PEEK, ch, size); + + for (size_t mix = 1; mix < MIXES; mix++) { + if (obs_source_audio_render_tick_stop(audio_submix, mix)) + break; + + if (!obs_source_audio_render_tick_copy(audio_submix, s->audio_mixers, mixers, mix)) { + record(s, CALL_MEMSET, mix, size * channels); + continue; + } + + for (size_t ch = 0; ch < channels; ch++) + record(s, CALL_MEMCPY, mix * CHANNELS + ch, size); + } + + if (!obs_source_audio_render_tick_volume(audio_submix)) { + s->audio_pending = false; + record(s, CALL_UNLOCK, 0, 0); + return; + } + + if (obs_source_audio_render_tick_clear_main(s->audio_mixers, mixers)) + record(s, CALL_MEMSET, 0, size * channels); + + record(s, CALL_VOLUME, mixers, channels); + s->audio_pending = false; + record(s, CALL_UNLOCK, 0, 0); +} + +static void audio_render(struct stand_in *s, uint32_t mixers, size_t channels, size_t size) +{ + const bool has_buffer = s->has_buffer; + const bool custom = has_buffer && s->custom; + const bool has_data = custom && s->has_data; + + switch (obs_source_audio_render_route(has_buffer, custom, has_data)) { + case OBS_SOURCE_AUDIO_RENDER_PENDING: + s->audio_pending = true; + return; + case OBS_SOURCE_AUDIO_RENDER_CUSTOM: + custom_audio_render(s, mixers, channels); + return; + case OBS_SOURCE_AUDIO_RENDER_TICK: + default: + break; + } + + if (s->has_mix) + record(s, CALL_SUBMIX, channels, 0); + + process_audio_source_tick(s, mixers, channels, size); +} + +static void check_calls(const struct stand_in *s, const struct call *expected, size_t n, int line) +{ + bool same = s->num_calls == n; + for (size_t i = 0; same && i < n; i++) + same = s->calls[i].kind == expected[i].kind && s->calls[i].a == expected[i].a && + s->calls[i].b == expected[i].b; + if (!same) { + fprintf(stderr, "line %d: calls differ\n", line); + for (size_t i = 0; i < s->num_calls || i < n; i++) + fprintf(stderr, " %2zu: got %d %" PRIu64 " %" PRIu64 ", expected %d %" PRIu64 " %" PRIu64 "\n", + i, i < s->num_calls ? s->calls[i].kind : 0, i < s->num_calls ? s->calls[i].a : 0, + i < s->num_calls ? s->calls[i].b : 0, i < n ? expected[i].kind : 0, + i < n ? expected[i].a : 0, i < n ? expected[i].b : 0); + fail(); + } +} + +#define CALLS(s, ...) \ + do { \ + const struct call expected_[] = {__VA_ARGS__}; \ + check_calls((s), expected_, sizeof(expected_) / sizeof(expected_[0]), __LINE__); \ + } while (0) + +static struct stand_in source(void) +{ + struct stand_in s; + memset(&s, 0, sizeof(s)); + s.has_buffer = true; + s.has_data = true; + s.audio_ts = 1000; + s.audio_mixers = 0x3f; + s.input_size = TICK_BYTES; + s.cb_success = true; + s.cb_ts = 2000; + return s; +} + +/* No output buffer: pending, nothing else, whatever the callbacks. */ +static void render_no_buffer(void **state) +{ + (void)state; + + for (int bits = 0; bits < 8; bits++) { + struct stand_in s = source(); + s.has_buffer = false; + s.custom = bits & 1; + s.has_data = bits & 2; + s.has_mix = bits & 4; + audio_render(&s, 0x3f, 2, TICK_BYTES); + CHECK_BOOL(s.audio_pending, true); + CHECK_U64(s.num_calls, 0); + CHECK_U64(s.audio_ts, 1000); + } +} + +/* A custom render without data (a source still being created): pending, + * the callback is not called (432017b2c, cf0a5e5c9). */ +static void render_custom_no_data(void **state) +{ + (void)state; + + struct stand_in s = source(); + s.custom = true; + s.has_data = false; + s.has_mix = true; + audio_render(&s, 0x3f, 2, TICK_BYTES); + CHECK_BOOL(s.audio_pending, true); + CHECK_U64(s.num_calls, 0); + CHECK_U64(s.audio_ts, 1000); +} + +/* A custom render that succeeds: the mixes in both masks cleared, the + * callback, then under the lock the timestamp, the mixes of the tick the + * source is not on cleared, and the volume. A submix callback is not + * called. */ +static void render_custom_success(void **state) +{ + (void)state; + + struct stand_in s = source(); + s.custom = true; + s.has_mix = true; + s.audio_pending = true; + s.audio_mixers = 0x05; + audio_render(&s, 0x03, 2, TICK_BYTES); + CALLS(&s, {CALL_MEMSET, 0, 2 * TICK_BYTES}, {CALL_RENDER, 0x03, 2}, {CALL_LOCK, 0, 0}, + {CALL_MEMSET, 1, 2 * TICK_BYTES}, {CALL_VOLUME, 0x03, 2}, {CALL_UNLOCK, 0, 0}); + CHECK_U64(s.audio_ts, 2000); + CHECK_BOOL(s.audio_pending, false); + + /* every mix in both masks, so nothing cleared after */ + s = source(); + s.custom = true; + audio_render(&s, 0x3f, 8, TICK_BYTES); + CALLS(&s, {CALL_MEMSET, 0, 8 * TICK_BYTES}, {CALL_MEMSET, 1, 8 * TICK_BYTES}, {CALL_MEMSET, 2, 8 * TICK_BYTES}, + {CALL_MEMSET, 3, 8 * TICK_BYTES}, {CALL_MEMSET, 4, 8 * TICK_BYTES}, {CALL_MEMSET, 5, 8 * TICK_BYTES}, + {CALL_RENDER, 0x3f, 8}, {CALL_LOCK, 0, 0}, {CALL_VOLUME, 0x3f, 8}, {CALL_UNLOCK, 0, 0}); + + /* the source on no mix: nothing before, every tick mix after */ + s = source(); + s.custom = true; + s.audio_mixers = 0; + audio_render(&s, 0x21, 1, TICK_BYTES); + CALLS(&s, {CALL_RENDER, 0x21, 1}, {CALL_LOCK, 0, 0}, {CALL_MEMSET, 0, TICK_BYTES}, {CALL_MEMSET, 5, TICK_BYTES}, + {CALL_VOLUME, 0x21, 1}, {CALL_UNLOCK, 0, 0}); + + /* characterized, not endorsed: tick mixers past the six mixes clear + * nothing and still reach the volume */ + s = source(); + s.custom = true; + audio_render(&s, 0x40, 2, TICK_BYTES); + CALLS(&s, {CALL_RENDER, 0x40, 2}, {CALL_LOCK, 0, 0}, {CALL_VOLUME, 0x40, 2}, {CALL_UNLOCK, 0, 0}); + s = source(); + s.custom = true; + s.audio_mixers = UINT32_MAX; + audio_render(&s, UINT32_MAX, 2, TICK_BYTES); + CHECK_U64(s.num_calls, 6 + 4); +} + +/* A custom render that fails: timestamp 0 and pending, unlocked at once; + * the mixes in both masks were cleared before it. */ +static void render_custom_failure(void **state) +{ + (void)state; + + struct stand_in s = source(); + s.custom = true; + s.cb_success = false; + s.cb_ts = 5000; + s.audio_mixers = 0x01; + audio_render(&s, 0x3f, 2, TICK_BYTES); + CALLS(&s, {CALL_MEMSET, 0, 2 * TICK_BYTES}, {CALL_RENDER, 0x3f, 2}, {CALL_LOCK, 0, 0}, {CALL_UNLOCK, 0, 0}); + CHECK_U64(s.audio_ts, 0); + CHECK_BOOL(s.audio_pending, true); +} + +/* A custom render that succeeds with timestamp 0, or with no tick mixers: + * not pending, but nothing after the callback. */ +static void render_custom_unmixed(void **state) +{ + (void)state; + + struct stand_in s = source(); + s.custom = true; + s.cb_ts = 0; + s.audio_pending = true; + audio_render(&s, 0x01, 2, TICK_BYTES); + CALLS(&s, {CALL_MEMSET, 0, 2 * TICK_BYTES}, {CALL_RENDER, 0x01, 2}, {CALL_LOCK, 0, 0}, {CALL_UNLOCK, 0, 0}); + CHECK_U64(s.audio_ts, 0); + CHECK_BOOL(s.audio_pending, false); + + s = source(); + s.custom = true; + audio_render(&s, 0, 2, TICK_BYTES); + CALLS(&s, {CALL_RENDER, 0, 2}, {CALL_LOCK, 0, 0}, {CALL_UNLOCK, 0, 0}); + CHECK_U64(s.audio_ts, 2000); + CHECK_BOOL(s.audio_pending, false); +} + +/* The tick pending: no timestamp, or less input than size; the submix + * callback still runs first. At, above and one below the size. */ +static void render_tick_pending(void **state) +{ + (void)state; + + struct stand_in s = source(); + s.audio_ts = 0; + s.has_mix = true; + audio_render(&s, 0x3f, 2, TICK_BYTES); + CALLS(&s, {CALL_SUBMIX, 2, 0}, {CALL_LOCK, 0, 0}, {CALL_UNLOCK, 0, 0}); + CHECK_BOOL(s.audio_pending, true); + + s = source(); + s.input_size = TICK_BYTES - 1; + audio_render(&s, 0x3f, 2, TICK_BYTES); + CALLS(&s, {CALL_LOCK, 0, 0}, {CALL_UNLOCK, 0, 0}); + CHECK_BOOL(s.audio_pending, true); + + s = source(); + s.input_size = 0; + s.audio_ts = UINT64_MAX; + audio_render(&s, 0x3f, 2, 1); + CHECK_BOOL(s.audio_pending, true); + + /* exactly enough */ + s = source(); + s.audio_pending = true; + audio_render(&s, 0x01, 1, TICK_BYTES); + CALLS(&s, {CALL_LOCK, 0, 0}, {CALL_PEEK, 0, TICK_BYTES}, {CALL_MEMSET, 1, TICK_BYTES}, + {CALL_MEMSET, 2, TICK_BYTES}, {CALL_MEMSET, 3, TICK_BYTES}, {CALL_MEMSET, 4, TICK_BYTES}, + {CALL_MEMSET, 5, TICK_BYTES}, {CALL_VOLUME, 0x01, 1}, {CALL_UNLOCK, 0, 0}); + CHECK_BOOL(s.audio_pending, false); + + /* more than enough, and a size of 0 with no input */ + s = source(); + s.input_size = TICK_BYTES + 1; + audio_render(&s, 0x01, 1, TICK_BYTES); + CHECK_BOOL(s.audio_pending, false); + s = source(); + s.input_size = 0; + audio_render(&s, 0x01, 1, 0); + CHECK_BOOL(s.audio_pending, false); + CHECK_U64(s.calls[1].kind, CALL_PEEK); + CHECK_U64(s.calls[1].b, 0); +} + +/* The tick: peek every channel into mix 0, copy it into mixes 1 to 5 in + * both masks (each channel), clear the others (all channels at once, size + * * channels bytes from channel 0), clear mix 0 unless in both masks, then + * the volume. Every pair of the six mix bits, and the mask edges. */ +static void render_tick_every_mask(void **state) +{ + (void)state; + + for (size_t a = 0; a < 64 + NUM_MASKS; a++) + for (size_t m = 0; m < 64 + NUM_MASKS; m++) { + uint32_t audio_mixers = a < 64 ? (uint32_t)a : MASKS[a - 64]; + uint32_t mixers = m < 64 ? (uint32_t)m : MASKS[m - 64]; + struct stand_in s = source(); + s.audio_mixers = audio_mixers; + audio_render(&s, mixers, 2, TICK_BYTES); + + struct call expected[MAX_CALLS]; + size_t n = 0; + expected[n++] = (struct call){CALL_LOCK, 0, 0}; + expected[n++] = (struct call){CALL_PEEK, 0, TICK_BYTES}; + expected[n++] = (struct call){CALL_PEEK, 1, TICK_BYTES}; + for (size_t mix = 1; mix < MIXES; mix++) { + if (has_mix(audio_mixers, mix) && has_mix(mixers, mix)) { + expected[n++] = (struct call){CALL_MEMCPY, mix * CHANNELS, TICK_BYTES}; + expected[n++] = (struct call){CALL_MEMCPY, mix * CHANNELS + 1, TICK_BYTES}; + } else { + expected[n++] = (struct call){CALL_MEMSET, mix, 2 * TICK_BYTES}; + } + } + if (!(has_mix(audio_mixers, 0) && has_mix(mixers, 0))) + expected[n++] = (struct call){CALL_MEMSET, 0, 2 * TICK_BYTES}; + expected[n++] = (struct call){CALL_VOLUME, mixers, 2}; + expected[n++] = (struct call){CALL_UNLOCK, 0, 0}; + check_calls(&s, expected, n, __LINE__); + CHECK_BOOL(s.audio_pending, false); + } +} + +/* A submix source (OBS_SOURCE_SUBMIX, audio lines): mix 1 copies mix 0 + * when bit 0 of the source's mixers is set, else is cleared; mixes 2 to 5 + * and mix 0 are left as they are, the volume is not applied, and the tick's + * mixers do not matter. The submix callback runs first when there is one. + * Characterized, not endorsed: mix 0 keeps the peeked input even when the + * source is on no mix. */ +static void render_tick_submix(void **state) +{ + (void)state; + + for (size_t a = 0; a < NUM_MASKS; a++) + for (size_t m = 0; m < NUM_MASKS; m++) { + struct stand_in s = source(); + s.submix = true; + s.has_mix = true; + s.audio_pending = true; + s.audio_mixers = MASKS[a]; + audio_render(&s, MASKS[m], 2, TICK_BYTES); + if (has_mix(MASKS[a], 0)) + CALLS(&s, {CALL_SUBMIX, 2, 0}, {CALL_LOCK, 0, 0}, {CALL_PEEK, 0, TICK_BYTES}, + {CALL_PEEK, 1, TICK_BYTES}, {CALL_MEMCPY, CHANNELS, TICK_BYTES}, + {CALL_MEMCPY, CHANNELS + 1, TICK_BYTES}, {CALL_UNLOCK, 0, 0}); + else + CALLS(&s, {CALL_SUBMIX, 2, 0}, {CALL_LOCK, 0, 0}, {CALL_PEEK, 0, TICK_BYTES}, + {CALL_PEEK, 1, TICK_BYTES}, {CALL_MEMSET, 1, 2 * TICK_BYTES}, + {CALL_UNLOCK, 0, 0}); + CHECK_BOOL(s.audio_pending, false); + } + + /* without a submix callback, and with no channels */ + struct stand_in s = source(); + s.submix = true; + s.audio_mixers = 1; + audio_render(&s, 0, 0, TICK_BYTES); + CALLS(&s, {CALL_LOCK, 0, 0}, {CALL_UNLOCK, 0, 0}); +} + +/* A plain source with an audio_mix callback but not the SUBMIX flag: the + * callback runs, then the ordinary tick. */ +static void render_mix_callback_without_flag(void **state) +{ + (void)state; + + struct stand_in s = source(); + s.has_mix = true; + audio_render(&s, 0x01, 1, TICK_BYTES); + CHECK_U64(s.calls[0].kind, CALL_SUBMIX); + CHECK_U64(s.calls[s.num_calls - 2].kind, CALL_VOLUME); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(route_values), + cmocka_unit_test(route_every_input), + cmocka_unit_test(custom_clear_before_masks), + cmocka_unit_test(custom_result), + cmocka_unit_test(custom_unmixed), + cmocka_unit_test(custom_clear_after_masks), + cmocka_unit_test(tick_pending_edges), + cmocka_unit_test(tick_stop_mixes), + cmocka_unit_test(tick_copy_masks), + cmocka_unit_test(tick_clear_main_masks), + cmocka_unit_test(render_no_buffer), + cmocka_unit_test(render_custom_no_data), + cmocka_unit_test(render_custom_success), + cmocka_unit_test(render_custom_failure), + cmocka_unit_test(render_custom_unmixed), + cmocka_unit_test(render_tick_pending), + cmocka_unit_test(render_tick_every_mask), + cmocka_unit_test(render_tick_submix), + cmocka_unit_test(render_mix_callback_without_flag), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/test/cmocka/tests.d/test_source_audio_render_steps.cmake b/test/cmocka/tests.d/test_source_audio_render_steps.cmake new file mode 100644 index 0000000000..c12e00bb19 --- /dev/null +++ b/test/cmocka/tests.d/test_source_audio_render_steps.cmake @@ -0,0 +1,36 @@ +# Characterization of the audio render mix plan of obs_source_audio_render(), +# custom_audio_render() and process_audio_source_tick() in libobs/obs-source.c, +# moved to libobs/obs-source-audio-render.c and ported to rust/obs-source +# (audio_render_plan): every route (no buffer, a custom render with and without +# data, the tick, the submix tick), the clears before and after a custom +# render's callback for its success and failure, the tick's pending check at +# the size edges and with no timestamp, and the copy or clear of each mix over +# every pair of the six mix bits and the mask edges (0, 1, 0x3f, 0x40 and +# above, UINT32_MAX), the submix case included. The three callers are copied +# onto a stand-in source, with the lock, the peeks, every memset and memcpy, +# the callbacks and the volume recorded. Builds the moved file on its own: no +# display, so it runs on every platform, and links libm except on MSVC. The +# functions are hidden in libobs, so this characterizes that C source; the test +# calls only them. test_source_volume_mix, test_source_audio_ranges, +# test_audio_capture_mix_race and test_group_hide_transition_audio cover the +# callers through the real libobs. +# Target and test are declared in ../build/test-graph.json. + +# Exercise the unchanged characterization against the Rust shims. The test +# calls only the thirteen hidden functions of obs-source-audio-render.c and +# includes only their header, so it links the archive alone: its obs-source +# member needs nothing from libobs or the other crates. Linking the archive +# directly avoids exporting private symbols just for a test. +if(TARGET libobs_rust-static) + add_executable(test_source_audio_render_steps_rust test_source_audio_render_steps.c) + target_include_directories( + test_source_audio_render_steps_rust + PRIVATE "${CMAKE_SOURCE_DIR}/libobs" ${CMOCKA_INCLUDE_DIR} + ) + target_link_libraries(test_source_audio_render_steps_rust PRIVATE libobs_rust-static ${CMOCKA_LIBRARIES}) + if(NOT MSVC) + target_link_libraries(test_source_audio_render_steps_rust PRIVATE m) + endif() + add_dependencies(cmocka-tests test_source_audio_render_steps_rust) + add_test(test_source_audio_render_steps_rust ${CMAKE_CURRENT_BINARY_DIR}/test_source_audio_render_steps_rust) +endif()