Skip to content

rust: Port media-io audio-io.h inlines + structs to obs-media-io - #129

Merged
zackees merged 3 commits into
obs-rust:masterfrom
swilk19:port/audio-io-inlines
Oct 11, 2026
Merged

zackees merged 3 commits into
obs-rust:masterfrom
swilk19:port/audio-io-inlines

Conversation

@swilk19

@swilk19 swilk19 commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Description

Ports the static inline helpers in libobs/media-io/audio-io.h to obs-media-io (obs_media_io::audio_io), as item 3 of the media-io part 2 order on #45: get_audio_channels, get_audio_bytes_per_channel, is_audio_planar, get_audio_planes, get_audio_size, get_total_audio_size, audio_frames_to_ns and ns_to_audio_frames. No intentional differences.

Header-inline exemption. The helpers are static inline, so nothing is swapped or exported. There is no ENABLE_RUST_LIBOBS guard and no rust-exports.txt entry, and the header is unmodified. Tier 2 has two parts:

  • A layout test of every struct the header defines, checking size, alignment and every field offset against the oracle (no hardcoded offsets):
    • struct audio_data
    • struct audio_output_data
    • struct audio_output_info, whose audio_input_callback_t is an Option<unsafe extern "C" fn> so NULL is None
    • struct audio_convert_info, which ends in a bool
  • The unchanged cmocka test passing OFF and ON.

The layout test also checks MAX_AV_PLANES and MAX_AUDIO_CHANNELS against the array lengths, and the size macros (MAX_AUDIO_MIXES, MAX_DEVICE_INPUT_CHANNELS, AUDIO_OUTPUT_FRAMES, TOTAL_AUDIO_SIZE). Tier 1 and Tier 3 run against non-inline oracle_* wrappers.

Enums. enum audio_format and enum speaker_layout become AudioFormat and SpeakerLayout, with *_to_c / *_from_c like the other media-io enums, and get the same size, alignment and enumerator checks that #106 did for colorspace and range. speaker_layout skips 7 (SPEAKERS_7POINT1 = 8), so speaker_layout_from_c(7) is None. ffi::audio_io has the helpers for C enum values. No C case matches a value outside the enums, so they map it to the enum's Unknown, which gives the C's results: 0 channels, 0 bytes per channel, and not planar (one plane).

Conversions. As agreed on #45, audio_frames_to_ns / ns_to_audio_frames are thin wrappers over obs_util::util_uint64::mul_div64 (#122), so obs-media-io now depends on obs-util. They inherit its per-platform math and its exclusions. The port panics instead of guessing on:

  • audio_frames_to_ns with sample_rate == 0: division by zero, UB in C. ns_to_audio_frames takes 0 as the multiplier and returns 0.
  • On MSVC x64, results that do not fit in 64 bits: _udiv128 faults. Off MSVC they wrap, and are compared.

These are listed in tests/audio_io_inlines_parity.rs, in the README row and here for #45.

Oracle. oracle/audio_io_inlines.c with its own binding module (src/audio_io_inlines.rs). Its build.rs entries are inserted in the media-io group.

Relation to #120. #120 characterizes audio-io.c in test_audio_io.c. This PR's test is test_audio_io_inlines.c, following test_video_io_inlines.c, and its CMake block sits after test_video_io_inlines rather than at the end of the file, so the two don't conflict. The header is unchanged, so the struct fields #120 fills in (audio_output_info, audio_convert_info) are exactly what the layout test checks. #120's getter values (float stereo: 1 plane, 2 channels, 8-byte block; planar 16-bit 5.1: 6/6/2) are among the cmocka cases.

audio-math.h (item 4) is not folded in; it will be a separate PR.

Motivation and Context

Item 3 of the order approved on #45 (#45 (comment)). Later media-io ports (audio-io.c) will use these types and helpers.

Refs #45

How Has This Been Tested?

Linux x86_64 (WSL2), Rust via soldr.

  • Commits follow the policy: test/cmocka/test_audio_io_inlines.c (C characterization, passes OFF and ON before any Rust), then RED (todo!() cores), then GREEN.

  • cmocka test. It pins:

    • every layout's channel count and every format's bytes per channel and planarity
    • planes for every format × layout pair
    • get_audio_size / get_total_audio_size, including UINT32_MAX frames and TOTAL_AUDIO_SIZE
    • the conversions at 48 kHz and 44.1 kHz, with truncation and a lossy round trip
    • large inputs where both util_mul_div64 paths agree

    Values outside the enums (-1, the gap at 7, last + 1, 1000) are characterized, not endorsed. Per-platform differences and division by zero stay in test_util_uint64.c.

  • soldr cargo test -p obs-media-io:

    • Tier 1 ports the cmocka cases 1:1, plus the enum values, the sample_rate == 0 panic, and whole-second round trips at common rates.
    • Tier 3 sweeps every enumerator plus values just outside each enum (7 included) over edge frame counts, and compares the conversions over edge rates × edge values.
    • Proptest covers arbitrary c_ints and u32 frame counts, and runs 20,000 cases each over edge-biased and arbitrary rates and u64s.
  • RED→GREEN, checked with soldr's cache disabled and a fresh target dir: at the RED commit audio_io_inlines fails 12/13 (the passing test is the enum values) and audio_io_inlines_parity fails 10/10. The layout test passes there, since the structs and enums are declarations. At head everything passes.

  • Mutation check: each of these fails Tier 1 and/or parity, and all were reverted:

    • giving SPEAKERS_7POINT1 7 channels
    • reporting AUDIO_FORMAT_32BIT_PLANAR as interleaved
    • computing ns_to_audio_frames exactly in u128
  • Local checks: ./lint and ./unittest pass. tier2-linux ran test_audio_io_inlines with ENABLE_RUST_LIBOBS OFF and ON (48/48 each). The libobs exported symbols are identical OFF vs ON (1752). HEAD carries the local-gate stamp.

  • Not run locally: macOS and Windows; the Rust Tests and rust-tier2 CI jobs cover them (Windows covers the MSVC mul_div64 path).

Types of changes

  • Code cleanup (non-breaking change which makes code smaller or more readable)

Checklist:

  • My code has been run through clang-format.
  • I have read the contributing document.
  • My code is not on the master branch.
  • The code has been tested.
  • All commit messages are properly formatted and commits squashed where appropriate.

🤖 Generated with Claude Code

@swilk19
swilk19 force-pushed the port/audio-io-inlines branch from f45274b to 13a0c19 Compare October 10, 2026 23:14
swilk19 and others added 3 commits October 10, 2026 17:09
Pins the header-inline helpers before the Rust port: get_audio_channels
for every enum speaker_layout value, get_audio_bytes_per_channel and
is_audio_planar for every enum audio_format value, get_audio_planes for
every format and layout pair, get_audio_size and get_total_audio_size
(including TOTAL_AUDIO_SIZE), and the audio_frames_to_ns and
ns_to_audio_frames conversions.

Values outside the enums are characterized: an unknown layout,
including 7 (the gap before SPEAKERS_7POINT1 = 8), has 0 channels, and
an unknown format has 0 bytes per channel and is interleaved, so it has
one plane. Planar sizes ignore the layout, even an unknown one.

The conversions truncate, so a frames -> ns -> frames round trip can
lose a frame. ns_to_audio_frames takes a sample rate of 0 (it is the
multiplier) and returns 0. The large-value cases are inputs where both
util_mul_div64 paths agree; the per-platform differences and division
by zero stay with test_util_uint64.c.

Nothing is swapped, so the test passes with ENABLE_RUST_LIBOBS OFF and
ON alike. It is separate from the audio-io.c characterization in obs-rust#120
(test_audio_io.c), which needs a running obs core.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Tier 1, Tier 2 layout and Tier 3 parity tests for the static inline
helpers in media-io/audio-io.h (get_audio_channels,
get_audio_bytes_per_channel, is_audio_planar, get_audio_planes,
get_audio_size, get_total_audio_size, audio_frames_to_ns and
ns_to_audio_frames), against todo!() cores.

The helpers are header-inline, so nothing is swapped or exported and
the header-inline exemption applies: Tier 2 is a layout test of every
struct the header defines plus the unchanged cmocka test passing OFF
and ON. ffi::audio_io mirrors struct audio_data, struct
audio_output_data, struct audio_output_info (with
audio_input_callback_t as an Option of an extern "C" fn, so NULL is
None) and struct audio_convert_info, with the enums as c_ints. The
layout test checks size, alignment and every field offset against the
oracle, MAX_AV_PLANES and MAX_AUDIO_CHANNELS against the array lengths,
and the size macros (MAX_AUDIO_MIXES, MAX_AUDIO_CHANNELS,
MAX_DEVICE_INPUT_CHANNELS, AUDIO_OUTPUT_FRAMES, TOTAL_AUDIO_SIZE).

audio_io.rs adds enum audio_format and enum speaker_layout
(AudioFormat, SpeakerLayout) with *_to_c / *_from_c like the other
media-io enums. speaker_layout skips 7 (SPEAKERS_7POINT1 is 8), so
speaker_layout_from_c(7) is None; the layout test checks every
enumerator's value against the oracle.

ffi::audio_io also has the helpers for C enum values, for later shims
that receive them as c_ints. No C case matches a value outside the
enums, so they map it to the enum's Unknown: 0 channels, 0 bytes per
channel, not planar.

audio_frames_to_ns and ns_to_audio_frames will call
obs_util::util_uint64::mul_div64, so obs-media-io now depends on
obs-util. Parity runs against each platform's own C and excludes what
mul_div64 excludes: a sample rate of 0 to audio_frames_to_ns, and on
MSVC x64 results past 64 bits.

The oracle wraps the header in non-inline oracle_* functions
(oracle/audio_io_inlines.c) with its own binding module. Parity sweeps
every enumerator plus values just outside each enum (7 included) over
edge frame counts, compares the conversions over edge rates and values,
and adds proptest over arbitrary c_ints, u32 frame counts, sample rates
and u64s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
get_audio_channels, get_audio_bytes_per_channel, is_audio_planar,
get_audio_planes, get_audio_size, get_total_audio_size,
audio_frames_to_ns and ns_to_audio_frames, as safe Rust that later
media-io ports (audio-io.c, audio-resampler) use when they handle these
enums and structs. The C helpers are static inline and stay compiled
into every C caller; nothing is swapped or exported, so there is no
ENABLE_RUST_LIBOBS guard. No intentional differences.

The switches become exhaustive matches, so a new enum value fails to
compile instead of falling through to the C's default. The size
helpers wrap like the C's size_t arithmetic. The conversions are thin
wrappers over obs_util::util_uint64::mul_div64, so they inherit its
per-platform math and panic where it does: a sample rate of 0 to
audio_frames_to_ns, and on MSVC x64 a result past 64 bits.

Mutation check: giving SPEAKERS_7POINT1 7 channels, and reporting
AUDIO_FORMAT_32BIT_PLANAR as interleaved, each fail Tier 1 and the
enumerator sweep; computing ns_to_audio_frames exactly in u128 fails
the edge-value and proptest parity tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Local-Gate: v1 tree=a0486750dadbb37edf7b72870f6581b958dee202 secs=114 lanes=lint:run,rust-test:run,tier2-linux:run
Ci-Attestation: {"at":1791677497,"gate":"rust/all/fmt","host":"linux-x86_64","key":"5c3da106582c7fcd2acde8a9d734fd10f8b8a2db933a20d165dac70b82607097","lane":"lint","parents":["edaa87b96a97e7d8733e289e5739cf19376857b6"],"secs":18,"stamp":"98934ae8867a27f3651dd31b9ab5897e","tree":"a0486750dadbb37edf7b72870f6581b958dee202","v":1,"via":"run"}
Ci-Attestation: {"at":1791677497,"gate":"rust/x86_64-unknown-linux-gnu/clippy","host":"linux-x86_64","key":"5c3da106582c7fcd2acde8a9d734fd10f8b8a2db933a20d165dac70b82607097","lane":"lint","parents":["edaa87b96a97e7d8733e289e5739cf19376857b6"],"secs":18,"stamp":"649000bcd6f4dbdef27cdc720777302c","tree":"a0486750dadbb37edf7b72870f6581b958dee202","v":1,"via":"run"}
Ci-Attestation: {"at":1791677497,"gate":"rust/x86_64-unknown-linux-gnu/test","host":"linux-x86_64","key":"73c295560e121eb8e88805ceb02d9fceda38638d050e83c6442c175c697570fd","lane":"rust-test","parents":["edaa87b96a97e7d8733e289e5739cf19376857b6"],"secs":35,"stamp":"ca64531306b48721cd9434eb0f0fa3ef","tree":"a0486750dadbb37edf7b72870f6581b958dee202","v":1,"via":"run"}
Ci-Attestation: {"at":1791677497,"gate":"cpp/linux-x64/tier2","host":"linux-x86_64","key":"3f34096abb565fbfbe68190c6a79f014ed2728242c3d107a3cf624792fcdb5a1","lane":"tier2-linux","parents":["edaa87b96a97e7d8733e289e5739cf19376857b6"],"secs":61,"stamp":"81a9038c3c9dadd90a15282038e93d4c","tree":"a0486750dadbb37edf7b72870f6581b958dee202","v":1,"via":"run"}
@zackees

zackees commented Oct 11, 2026

Copy link
Copy Markdown
Member

Rebased onto master (old head 13a0c19, replaced with --force-with-lease). #168 just split the shared registration lists into per-unit files, so this PR follows the new layout:

  • The oracle build.rs now compiles oracle/*.c automatically, so the .file("oracle/audio_io_inlines.c") line is gone. lib.rs gets pub mod audio_io_inlines; in sorted order.
  • The cmocka test moved to test/cmocka/tests.d/test_audio_io_inlines.cmake (folded into your characterization commit) instead of being appended to CMakeLists.txt.

RED→GREEN, checked with soldr's cache disabled: audio_io_inlines fails 12/13 at the RED commit and the full suite passes at head. Tier 2 49/49 with the Rust port OFF and ON, exports IDENTICAL (1752). The local gate passes and is stamped.

@zackees
zackees force-pushed the port/audio-io-inlines branch from 13a0c19 to 9354e55 Compare October 11, 2026 00:11
@zackees
zackees self-requested a review as a code owner October 11, 2026 00:11
@zackees
zackees merged commit 817d5bc into obs-rust:master Oct 11, 2026
20 checks passed
@swilk19
swilk19 deleted the port/audio-io-inlines branch October 11, 2026 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants