Repository navigation
obs-output: Port the encoder binding checks - #2511
Merged
Merged
Conversation
Behaviour-neutral extraction before the Rust port. The decisions of obs_encoder_set_video(), encoder_set_video(), obs_encoder_set_audio(), the media getters and obs_encoder_shutdown_unstarted() move to libobs/obs-encoder-bind.c.h, as six functions on plain values declared in the new libobs/obs-encoder-bind.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - obs_encoder_set_audio() and obs_encoder_audio(): _not_audio() (the type refusal); - encoder_set_video(): _video_timebase() (fps_den over fps_num, or 0 over 0 with no video) and _divisor_override() (the frame rate divisor video is created for a divisor that is not 0); - obs_encoder_set_audio(): _audio_timebase() (1 over the sample rate, or 0 over 0 with no audio); - obs_encoder_shutdown_unstarted(): _locked() (each pthread_mutex_trylock() result) and _shutdown_unstarted() (shut down when initialized and not needed). The video type refusal of obs_encoder_set_video(), obs_encoder_video(), obs_encoder_parent_video() and obs_encoder_unbind_video() is the one obs_encoder_set_gpu_scale_type() and obs_encoder_set_frame_rate_divisor() already ask (obs-rust#2331), obs_encoder_audio_start_not_video(), so they ask it too; obs-encoder-audio-start.h notes the new callers. One function per decision, asked where the C made it, so every read stays where it was. The plan's combined gates (type, active and initialized in one call; unbind and rebind as one decision each; the getters' override choice) would read the active flag, initialized, media or fps_override where the C's && and early returns do not, so they are split as obs_encoder_set_gpu_scale_type() already is: the type refusal is a function, and the single-field tests stay C in their order (the active flag, an atomic, loaded where it always was; initialized; media; fps_override; context.data). Nothing is left to decide in obs_encoder_shutdown() (the context.data test and the resets are single fields and constants) or obs_encoder_rebind_video() (three single-field tests in an && chain), so they are unchanged. C keeps init_mutex and outputs_mutex, both pthread_mutex_trylock() calls (their results are passed) and their unlock order, the walk over videos (it computes bound and stops at the first match) and the walk over encoder->outputs (it computes needed and stops at the first output that needs the encoder), obs_encoder_shutdown() with info.destroy, maybe_clear_encoder_core_video_mix() and the paired-encoder weak releases, obs_encoder_set_last_error(), video_output_get_info(), audio_output_get_sample_rate(), the fps_override free and create, every warning and every write to the encoder. No lock section is merged and nothing divides. One order changes, inside a branch with no lock or call in it: the timebase reads the video's fps_num and fps_den (and the audio's sample rate) before it writes timebase_num, where the C wrote timebase_num between them. Those are the video's and audio's info, which nothing writes after they are created; the encoder's writes keep their order (media, timebase_num, timebase_den). Checked in a scratch harness (not committed). It compiles origin/master's ten functions (the four setters and getters of each kind, encoder_set_video(), obs_encoder_shutdown(), _shutdown_unstarted(), _unbind_video() and _rebind_video()) verbatim, and this branch's with the moved file and obs-encoder-audio-start.c.h, with every read and write of an encoder field rewritten the same way into a logged access (with the value), over a stand-in encoder, videos and audios. It logs every lock, unlock and trylock (with its result), atomic load, obs_encoder_valid(), obs_output_needs_encoders(), video_output_get_info() and the video info reads, the sample rate, the divisor free and create, info.destroy, the weak releases, the last error, the mix clear and each warning, in order. Another thread flips the active flag and the outputs' needs at every lock and call, and initialized and the divisor while init_mutex is not held. The run from the media write to the timebase_den write is compared as a sorted run (the reorder above), and must hold nothing but the info reads and the timebase_num write. - Inputs: audio, video and unknown encoder types, NULL encoders, active or not, initialized or not, with or without context.data, media (none, any of six videos or four audios), fps_override and divisors 0, 1, 2 and UINT32_MAX; frame rates of 30000/1001, 60/1, 0/1, 0 and UINT32_MAX; sample rates of 0, 44100, 48000 and UINT32_MAX; 0 to 4 outputs, 0 to 3 videos to unbind from, 0 to 2 paired encoders, busy trylocks; one to six calls in a row. - 2,900,000 runs (GCC and Clang -O2, 1,000,000 each; GCC -O0, 500,000; ASan with UBSan under GCC -O0 and Clang -O2, 200,000 each) give equal events and an equal end state (every encoder field, the outputs, the mutexes and the stubs' state): 80,633,324 events. - Every branch of origin/master's functions is reached both ways (gcov, 72 of 72). - 25 deliberately broken moves were planted and all 25 fail it: the active flag read before the type refusal (both setters), initialized read with the active flag, fps_override read before the type, the video info or sample rate read before the media write, the divisor read before the timebase write, the trylocks or unlocks swapped, initialized read before the outputs walk, the walk not stopping, the active flag read before init_mutex in unbind, the rebind && order changed, the wrong type refusal in four places, and nine changed decisions (the type test inverted, the video timebase not swapped, a timebase of 1 with no video or audio, an audio timebase of 0 over the rate, a divisor of 1 with no override, the trylock test inverted, || for && and needed ignored in the shutdown). git blame on the moved lines shows upstream commits (eb0d9dc, f07ce85, f53df7d, dfc20bb, d584aed) and two of the fork's (7041590, the unstarted shutdown; c651ea2, the unbind and rebind). OBS_ENCODER_AUDIO is copied into the header, so it needs no core header; obs-encoder.c.h checks it with _Static_assert. The new file joins libobs.core-output, just before obs-encoder.c.h, with libobs.pch.h, which includes the new header; source-sets.json lists it. docs/rust-port/naming.md maps the file to rust/obs-output. The exported symbol sets are unchanged (1766) on the C, hybrid and C reference libobs; the six functions are local. Refs obs-rust#2496
test_encoder_bind_steps compiles libobs/obs-encoder-bind.c.h and libobs/obs-encoder-audio-start.c.h (whose video type refusal the video setter, the video getters and the unbind ask) with ASan and UBSan, and calls only their functions through obs-encoder-bind.h and obs-encoder-audio-start.h. 13 cases: - the copied constant (OBS_ENCODER_AUDIO, 0); - the audio and video type refusals over edge int values (an unknown type is refused by both); - the video timebase over every pair of edge uint32_t frame rates (fps_den over fps_num; 0 over 0 with no video, whatever is passed), with 30000/1001, 60/1 and an fps_num of 0 (a timebase den of 0, stored as is: the fps-0 class, pinned and held); - the audio timebase over edge rates (1 over the rate; 0 over 0 with no audio; a rate of 0 gives 1 over 0, characterized, not endorsed); - the frame rate divisor video, created for any divisor but 0; - the trylock results (only 0 holds the mutex) and the unstarted shutdown truth table. The bodies of obs_encoder_set_video(), encoder_set_video(), obs_encoder_set_audio(), obs_encoder_video(), _parent_video() and _audio(), obs_encoder_shutdown_unstarted(), obs_encoder_unbind_video() and obs_encoder_rebind_video() are also copied onto a stand-in encoder (no lock and no video output; the trylock results and the outputs' states are given), to pin end to end: both setters for audio and video encoders, active and idle, initialized and not, and an unknown type; binding NULL; divisors 0 and 1 (the getter gives the divisor video, the parent getter the media; binding again or unbinding frees it); the getters, which have no active or initialized gate; the unstarted shutdown with each mutex busy, active, an output that needs the encoder (the walk stops there), not initialized and with no outputs; the unbind with each bound, active and initialized combination, of an audio encoder, with no media and with no videos; the rebind with each gate; and an unbind then rebind that keeps the divisor. Characterized, not endorsed: obs_encoder_set_audio() has no initialized refusal, so an initialized idle audio encoder takes the new audio, where obs_encoder_set_video() refuses an initialized video encoder (upstream). It is in the native test graph, in name order, with both headers as watch inputs, and links libm off MSVC. No assert_in_range or fail_msg, so it builds with cmocka 1.x and 2.x, and no comparison between two enum types (MSVC C5287). The real-libobs tests test_encoder_video_reset and test_output_failed_start_encoders cover the callers. Refs obs-rust#2496
RED for the port of libobs/obs-encoder-bind.c.h to a new encoder_bind module of rust/obs-output, next to encoder_size and encoder_audio_start. The safe core is a stub (todo!()): not_audio(), video_timebase(), audio_timebase(), divisor_override(), locked() and shutdown_unstarted(), with OBS_ENCODER_AUDIO (encoder_size's) and the Timebase pair. It re-exports encoder_audio_start's not_video(), the video type refusal the callers share, already ported. tests/output/encoder_bind.rs ports test_encoder_bind_steps 1:1: its 13 cases under the same names (steps_*), in the same order, with the same values and expectations, the characterized-not-endorsed case included, the stand-in encoder and its copies of the setters, getters, unstarted shutdown, unbind and rebind written in Rust over the core. Edge cases (edge_*) check the core against the origin/master expressions inline: every type and trylock result from -1,000 to 1,000 and the extremes; every pair of frame rates below 257 with and without a video, and every rate; every divisor to 10,000; the shutdown truth table; the stand-in through every caller for each type, active and initialized flag, media, divisor, trylock pair and output list (2,304 encoders); and proptests (4,096 cases each) of every decision over full-range values and of the stand-in over sequences of up to eleven calls, with another thread flipping the active flag, initialized and the divisor between them. soldr cargo test -p obs-data --test obs-tests-core -- output::encoder_bind passes 1 of 20 (the constants, already ported) and fails 19 with "not yet implemented". Refs obs-rust#2496
Ports libobs/obs-encoder-bind.c.h, the encoder media binding and shutdown checks, to the encoder_bind module of rust/obs-output. obs-encoder.c.h stays C around it: obs_encoder_valid(), every warning, the active flag (an atomic load) and the initialized, media, fps_override and context.data tests in their && order, init_mutex and outputs_mutex with both trylocks and their unlock order, the walks over the videos to unbind from and over the encoder's outputs (each stops where it did), obs_encoder_shutdown(), video_output_get_info(), audio_output_get_sample_rate(), the divisor video's free and create and every write to the encoder. The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/encoder_bind.rs): - not_audio(): the type is not OBS_ENCODER_AUDIO (encoder_size's 0); - video_timebase(): fps_den over fps_num with a video, else 0 over 0; - audio_timebase(): 1 over the sample rate with an audio, else 0 over 0; - divisor_override(): the divisor is not 0; - locked(): the trylock result is 0; - shutdown_unstarted(): initialized and not needed; - not_video() re-exported from encoder_audio_start: the video setter, the video getters and the unbind call that shim, as obs_encoder_set_gpu_scale_type() does, so they share one implementation. The module denies clippy's arithmetic, as, indexing, unwrap, expect, panic, todo, unimplemented and unreachable lints, keeps no state, takes no lock and does not divide (the timebase is two copies). Shims (src/ffi/encoder_bind.rs): the six functions keep the C signatures (int as c_int). The timebase comes back by value as obs_encoder_bind_timebase, a #[repr(C)] pair of u32 (Tier 3 checks its layout against the C). No pointer crosses. No intentional differences. C side: the file leaves libobs.core-output for its own group, libobs.ported-encoder-bind (LIBOBS_PORTED_SOURCES), whose PCH is only obs-encoder-bind.h; libobs.pch.h keeps the header for obs-encoder.c. The functions are not EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map and rust-unexports-macos.txt. libobs-rust already links the crate's ffi. ci/test_ported_sources.py checks the group on every host. The exported symbol sets are unchanged (1766) on the C, hybrid and C reference libobs, and the six functions are local in each; the hybrid libobs has no C object for the group. test_encoder_bind_steps still compiles the C file into itself, unchanged. It has no _rust variant, as for test_encoder_audio_start_steps: the shims' archive member also holds the rest of obs-output, which needs libobs's hidden seams and obs-util, so it does not link on its own. Its tests.d file says so. soldr cargo test -p obs-data --test obs-tests-core -- output::encoder_bind passes 20 of 20. Through ctest (private Xvfb and D-Bus, own HOME and runtime directory, no sound), the full suite passes against the C libobs (532 of 532), the hybrid libobs (564 of 564) and the C reference libobs (564 of 564), the same 3 skipped on each (a PulseAudio default sink and the v4l2 vivid module, absent here). Among them test_encoder_bind_steps, test_encoder_video_reset, test_output_failed_start_encoders, test_encoding_lag_rescale, test_encoder_tex_active_null, test_multitrack_encoder_override, test_texture_encoder_raw_format, test_encoder_error_stop, test_encoder_join_keyframe, test_video_reset_fps and test_raw_output_video_reset. Refs obs-rust#2496
Tier 3 for the encoder_bind port. The oracle (rust/obs-c-oracle/oracle/encoder_bind.c) compiles the unchanged libobs/obs-encoder-bind.c.h with its six functions renamed to oracle_*, plus its constant and the layout of struct obs_encoder_bind_timebase, bound in src/encoder_bind.rs. Nothing in it divides. The video type refusal's oracle is encoder_audio_start's. tests/output/encoder_bind_parity.rs compares the oracle, the shims and the core exactly, all three sides: - the constant, and the timebase struct's size, alignment and offsets (and the oracle's mirror of it); - both type refusals and the trylock result over -100,000 to 100,000, the 100,000 values at each extreme and a stride through all of i32; - the video timebase over every pair of frame rates below 512 and every pair of 24 special u32 values (the common rates, 0, 2^16, 2^31 and around u32::MAX), with and without a video; - the audio timebase and the divisor over 0 to 1,000,000, the top 100,000 and a stride through all of u32 (with and without an audio); - the shutdown truth table; - the callers on test_encoder_bind_steps' stand-in encoder (shared with Tier 1), on the oracle's gates and on the shims: every caller in turn for each of six types, the active and initialized flags, five medias, four divisors, five trylock pairs and four output lists (9,600 encoders), comparing every return and the end state; - proptests (20,000 cases each) of every decision over full-range values and of the stand-in over sequences of up to fifteen calls. No input is excluded: nothing in the C divides or overflows. Tier 1's stand-in and its gates become pub(crate), so both tiers run the same caller bodies. The proptests of both files keep no regression files (failure_persistence None), as the delay and interleave parity runners do. Mutation check (once, not committed): 22 mutants, 15 in the core and 7 in the shims. mutant Tier 1 Tier 3 not_audio: == 7 4 not_audio: the video type 6 3 OBS_ENCODER_AUDIO = 1 7 4 video_timebase: not swapped 7 4 video_timebase: ignores has_video 3 2 video_timebase: den at least 1 4 2 audio_timebase: num 0 6 3 audio_timebase: ignores has_audio 5 4 audio_timebase: den at least 1 2 1 divisor_override: > 1 5 3 divisor_override: always 5 3 locked: != 0 6 4 locked: >= 0 6 4 shutdown_unstarted: || 6 3 shutdown_unstarted: ignores needed 6 3 shim not_audio inverted 0 4 shim timebase fields swapped 0 5 shim audio_timebase: has_audio true 0 4 shim video_timebase: rates swapped 0 4 shim divisor_override: divisor - 1 0 3 shim locked: <= 0 0 4 shim shutdown: arguments swapped 0 3 (failing tests per tier.) Tier 1 kills all 15 core mutants and Tier 3 all 22. soldr cargo test -p obs-data --test obs-tests-core -- output::encoder_bind passes 29 of 29 (20 Tier 1, 9 Tier 3). Refs obs-rust#2496
rust/README.md gets the row for libobs/obs-encoder-bind.c.h (obs-output::encoder_bind), after encoder_gpu_gates': what moved, the video type refusal shared with the other video setters, what stays C and why, the one reorder inside the timebase branch, the characterized-not-endorsed behaviours (the audio setter taking an initialized encoder, the 0 frame rate and sample rate timebases) and the tests that cover it; the obs-output crate row names the file. The crate's Cargo description and lib.rs named the module in the port commit, and docs/rust-port/naming.md maps the file (the move commit). rust/obs-c-oracle/benches/port_comparison/encoder_bind.rs, in the sorted module list, runs 1024 rounds over audio and video encoders with four frame rates, two sample rates, divisors 0 to 2, a busy trylock every seventh round and mixed output states, asking every decision the setters, the getters and the unstarted shutdown ask, on the shims and on the oracle, and checks both checksums match. Release, x86_64 Linux, GCC, median (load average about 3): Rust 8.42 us, C 8.18 us (1.03x), about 8 ns a round, next to a mutex trylock or a video_output_get_info() call. The port changes nothing a user can see, so docs/features.md has no line. Fixes obs-rust#2496
Member
|
Coordinator review: a decision port in the standard shape, like #2475 and #2481. The encoder binding checks move into a steps unit and then to Rust, with a C reference copy, a parity test, a bench and the export maps. The only existing test file touched is the test-graph.json entry. Queuing; it merges when CI is green. |
zackees
enabled auto-merge
October 26, 2026 17:42
6 of 7 tasks
rdoupe
added a commit
to rdoupe/obs-studio
that referenced
this pull request
Oct 26, 2026
* libobs: Move the encoder lifecycle decisions Behaviour-neutral extraction before the Rust port. The decisions of obs_encoder_start_internal(), obs_encoder_stop(), add_connection(), remove_connection(), gpu_encode_available() (which full_stop() asks) and send_first_video_packet() move to libobs/obs-encoder-lifecycle.c.h, as ten functions on plain values declared in the new libobs/obs-encoder-lifecycle.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - obs_encoder_start_internal(), under callbacks_mutex: _first() (no callback before this one) and _found() (get_callback_idx()'s result is not DARRAY_INVALID: the callback is not added again); - obs_encoder_stop(), under callbacks_mutex: _found() (the callback is erased) and _last() (none is left); under the group mutex, _group_idle() after destroy_on_stop (the group is destroyed); - add_connection() and remove_connection(), under the group mutex: _lcm_unset() (the divisor LCM starts at 1), _started_inc() and _group_ready() (every encoder started), _started_dec() and _group_idle() (the last started encoder stopped: the group reset); - gpu_encode_available(): _pass_texture() (the texture cap); - send_first_video_packet(): _prepend_sei() (get_sei() returned true with a pointer and a size, so the SEI goes in front of the keyframe). The type tests are the ones already ported, so they share one implementation: the connection route of add_connection() and remove_connection() (the audio mix, else the GPU encode or the raw video) asks obs_encoder_bind_not_audio() (obs-rust#2511), the audio setter's refusal, negated; the keyframe request in obs_encoder_start_internal() and the first packet route in send_packet() ask obs_encoder_audio_start_not_video(), the video setters' refusal, negated. obs-encoder-bind.h and obs-encoder-audio-start.h note the new callers. One function per decision, asked where the C made it, so every read stays where it was. The plan's combined steps (skip, first, join and keyframe as one start plan; the connect route with the GPU test; the send route) would read media, info.type, request_keyframe, the mix's texture flags or sent_first_packet where the C's ||, && and early returns do not (info is rewritten when the encoder is initialized or rerouted, and the mix flags by a video reset), so they are split: the comparisons are functions, and the single-field tests stay C in their order (context.data, media, request_keyframe, destroy_on_stop, keyframe, sent_first_packet, the mix and its texture flags, the outputs' obs_output_active()). full_stop() has nothing else to decide (the active test and the texture early return are single values), so apart from gpu_encode_available() it is unchanged. C keeps init_mutex, callbacks_mutex, outputs_mutex and the group mutex with every section they guard, the darrays (the callback push and erase, the SEI copy), get_callback_idx() (a walk; its result is passed), lcm() (it divides by gcd(), the held fps-0 class; its result is stored as before), audio_output_connect() and _disconnect(), start_gpu_encode() and stop_gpu_encode(), start_raw_video() and stop_raw_video(), get_audio_info(), get_video_info(), get_mix_for_video(), add_ready_encoder_group(), the group destroy, obs_encoder_shutdown(), set_encoder_active(), pause_reset(), request_keyframe(), get_sei(), obs_output_force_stop() and the NULL packet, the callbacks and the profiler, and every write. No lock section is merged and nothing divides. Two small changes, both behaviour-neutral: - the group counters take a local: num_encoders_started is read, the stepped value written back and the local tested, where the C stepped the field and tested it (the same value, under the same mutex); - send_first_video_packet()'s sei and size start NULL and 0 and get_sei()'s result goes into a local, so _prepend_sei() never reads an unset local; the C read them only after get_sei() returned true. Checked in a scratch harness (not committed). It compiles origin/master's eleven functions (gpu_encode_available(), get_callback_idx(), add_connection(), remove_connection(), obs_encoder_start_internal(), obs_encoder_start(), obs_encoder_stop(), get_sei(), send_first_video_packet(), send_packet() and full_stop()) verbatim, and this branch's with the moved file, obs_encoder_audio_start_not_video() and obs_encoder_bind_not_audio(), with every pointer-chain field access rewritten the same way into a logged access (with the field's value at the next event; a run of accesses to one field with nothing between them counts as one, which covers the counters' local), over a stand-in encoder, group, mixes, outputs and callbacks. It logs every lock and unlock, obs_encoder_valid() and obs_ptr_valid(), get_mix_for_video(), the audio and video info, the connects, disconnects and GPU and raw starts and stops, lcm(), the ready add, the group destroy, the shutdown, set_encoder_active(), pause_reset(), each atomic store, the profiler, obs_output_active() and the force stop, the NULL packet, request_keyframe(), get_sei_data() and every callback with the packet's bytes, in order. Between calls another "thread" flips an output's active flag, a mix's NV12 flag, initialized, the group's started count or the SEI the plugin returns. - Inputs: audio, video and unknown types; caps 0, 1, the texture cap and 0xff; no media or one of four (no mix, NV12, P010, neither); with or without context.data, request_keyframe and get_sei_data; six SEI answers (false, true with no pointer, true with size 0, true with data, false with data set); divisors 0 to 3; 0 to 3 callbacks of two functions and three params, sent or not; 0 to 3 outputs, active or not; no group or one of 0 to 3 encoders with 0 to 3 or UINT32_MAX started, an LCM of 0, 1 or 6 and destroy_on_stop both ways; NULL encoders and callbacks; one to six calls of start, stop, full stop and send (keyframes or not) in a row. - 2,900,000 runs (GCC and Clang -O2, 1,000,000 each; GCC -O0, 500,000; ASan with UBSan under GCC -O0 and Clang -O2, 200,000 each) give equal events and an equal end state (every encoder, callback, group and output field and every mutex): 312,182,644 events. - Every branch of origin/master's functions is reached both ways (gcov, 90 of 90). - 27 deliberately broken moves were planted and all 27 fail it: the mix flags read before the cap, the cap read before the mix test, sent_first_packet read before the type, request_keyframe or the type read before added, first taken from the count after the add or outside callbacks_mutex, last taken for a missing callback, the destroy test's order swapped, the started count read again after the reset, ready read after the unlock, the SEI asked before the keyframe wait, the send and connection routes not negated, and thirteen changed decisions (first and the group test inverted, found inverted, last always false, the cap test always true or any cap, the LCM test at 1, the count not stepped or stepped up on stop, ready as >, and the SEI test with || or ignoring the size or get_sei()'s result). git blame on the moved lines shows upstream commits (fd37d9e, 93ba6e7, 5c91d93, caa5723, 0ed0f2c, 3990c18, 751dbda, dd1e93f, f53df7d, a1fbf10) and one of the fork's (4b811b1, the keyframe request when an output joins). The constants (OBS_ENCODER_CAP_PASS_TEXTURE and DARRAY_INVALID) are copied into the header, so it needs no core header; obs-encoder.c.h checks them with _Static_assert. The new file joins libobs.core-output, just before obs-encoder.c.h, with libobs.pch.h, which includes the new header; source-sets.json lists it. docs/rust-port/naming.md maps the file to rust/obs-output. The exported symbol set of the C libobs is unchanged (1766); the ten functions are local. Refs obs-rust#2512 * test: Characterize the encoder lifecycle decisions test_encoder_lifecycle_steps compiles libobs/obs-encoder-lifecycle.c.h, libobs/obs-encoder-bind.c.h (whose audio type test the connection route asks) and libobs/obs-encoder-audio-start.c.h (whose video type test the keyframe request and the first packet route ask) with ASan and UBSan, and calls only their functions through their headers. 12 cases: - the copied constants (the texture cap 2, DARRAY_INVALID SIZE_MAX and the type tests' OBS_ENCODER_AUDIO 0 and OBS_ENCODER_VIDEO 1); - first, found and last over edge size_t values (only DARRAY_INVALID is not found); - the audio route and the video test over edge int values (an unknown type takes the video route but asks no keyframe); - the texture cap over every low byte, with and without the high bits; - the group counters over edge uint32_t values: the LCM unset and the idle test at 0, the steps wrapping as uint32_t, ready at or past the encoder count (a group of none is ready); - the SEI truth table over edge sizes (data with size 0, a size with no data, or data from a plugin that returned false: sent as it is). The bodies of obs_encoder_start_internal(), obs_encoder_stop(), add_connection(), remove_connection(), gpu_encode_available(), full_stop(), send_packet() and send_first_video_packet() are also copied onto a stand-in encoder (no lock, no output, no darray; the mix, outputs, group and the plugin's SEI are given, every call counted), to pin end to end: the start with 0, 1 and 2 callbacks and a duplicate (not added, no keyframe request), for audio and video encoders with and without request_keyframe, and with no data or no media; the stop of a missing, a non-last and the last callback, and again after it; every connect route (audio; GPU with the cap and an NV12 or P010 mix; raw otherwise); a group of two with divisors 2 and 3 through 0, 1 and the encoder count started and back, destroy_on_stop both ways; the full stop of a raw encoder with an idle output between two active ones (only the active ones are force stopped, the fork's fix) and of a texture encoder (flagged, then the early return); the first packets for each of six SEI answers, keyframe or not, and a second output waiting for its own keyframe; an audio encoder's plain packets. Characterized, not endorsed: a stop with none started wraps the count to UINT32_MAX, so the group is never reset or destroyed again; a divisor of 0 (the setter refuses it) leaves an LCM of 0, which the next start takes as unset; the route is asked again at the stop, so a mix that lost its textures in between stops raw what started on the GPU; the stop of a missing callback still clears the packet times. It is in the native test graph, in name order, with the three headers as watch inputs, and links libm off MSVC. No assert_in_range or fail_msg, so it builds with cmocka 1.x and 2.x, and no comparison between two enum types (MSVC C5287). It passes 12 of 12 in the C and the hybrid build trees. The real-libobs tests test_encoder_join_keyframe, test_encoder_error_stop, test_encoder_group_null and test_multitrack_ladder cover the callers. Refs obs-rust#2512 * obs-output: Add Tier 1 tests for encoder lifecycle RED for the port of libobs/obs-encoder-lifecycle.c.h to a new encoder_lifecycle module of rust/obs-output, next to encoder_size and encoder_audio_start. The safe core is a stub (todo!()): first(), found(), last(), pass_texture(), lcm_unset(), started_inc(), group_ready(), started_dec(), group_idle() and prepend_sei(), with OBS_ENCODER_CAP_PASS_TEXTURE and NOT_FOUND (DARRAY_INVALID). It re-exports encoder_bind's not_audio() (the connection route) and encoder_audio_start's not_video() (the keyframe request and the first packet route), the type tests the callers share, already ported. tests/output/encoder_lifecycle.rs ports test_encoder_lifecycle_steps 1:1: its 12 cases under the same names (steps_*), in the same order, with the same values and expectations, the characterized-not-endorsed cases included, the stand-in encoder and group and their copies of the start, stop, connect, disconnect, GPU test, full stop and both sends written in Rust over the core. Edge cases (edge_*) check the core against the origin/master expressions inline: every type from -1,000 to 1,000 and the extremes; every cap word of the low 16 bits under three high words; every count and index to 10,000 and in the top 10,000 of usize, and every group count likewise in u32, with ready over every pair below 65 and of the edge values; the SEI truth table; the stand-in through a fixed run of every caller for each of four types, four caps, four mixes, both requests, six SEI answers, four groups and destroy_on_stop both ways (6,144 encoders); and proptests (4,096 cases each) of every decision over full-range values and of the stand-in over sequences of up to eleven calls, with another thread flipping an output, the mix's flags, initialized, the SEI answer and the group count between them. soldr cargo test -p obs-data --test obs-tests-core -- output::encoder_lifecycle passes 3 of 19 (the constants and both type tests, already ported: steps_constants, steps_routes and edge_routes_every_type) and fails 16 with "not yet implemented". Refs obs-rust#2512 * obs-output: Port the encoder lifecycle decisions Ports libobs/obs-encoder-lifecycle.c.h, the encoder callback and connection lifecycle decisions, to the encoder_lifecycle module of rust/obs-output. obs-encoder.c.h stays C around it: init_mutex, callbacks_mutex, outputs_mutex and the group mutex with every section they guard, the callback push and erase and get_callback_idx(), the context.data, media, request_keyframe, destroy_on_stop, keyframe, sent_first_packet, mix and texture flag tests in their order, lcm(), the connects and disconnects, the GPU and raw starts and stops, get_sei() and the SEI copy, obs_output_active() and the force stop, and every write. The hybrid libobs drops the C file and links the Rust shims in its place. Safe core (src/encoder_lifecycle.rs): - first() and last(): no callback (before the add; after the erase); - found(): the index is not NOT_FOUND (DARRAY_INVALID, usize::MAX); - pass_texture(): the caps have OBS_ENCODER_CAP_PASS_TEXTURE; - lcm_unset() and group_idle(): the LCM, or the started count, is 0; - started_inc() and started_dec(): the count plus or minus one, wrapping as the C's uint32_t does; - group_ready(): the count is at least the encoder count (a count that does not fit usize, which no host has, is past every encoder count, as the C's conversion to size_t gives); - prepend_sei(): get_sei() returned true with a pointer and a size; - not_audio() and not_video() re-exported from encoder_bind and encoder_audio_start: the connection route, the keyframe request and the first packet route call those shims, so the type tests share one implementation. The module denies clippy's arithmetic, as, indexing, unwrap, expect, panic, todo, unimplemented and unreachable lints, keeps no state, takes no lock and does not divide (lcm() stays C). Shims (src/ffi/encoder_lifecycle.rs): the ten functions keep the C signatures (size_t as usize, uint32_t as u32). Only counts, an index, the caps and three SEI values cross, by value; no pointer crosses. No intentional differences. C side: the file leaves libobs.core-output for its own group, libobs.ported-encoder-lifecycle (LIBOBS_PORTED_SOURCES), whose PCH is only obs-encoder-lifecycle.h; libobs.pch.h keeps the header for obs-encoder.c. The functions are not EXPORT, so the shims are hidden: rust-hidden.txt, the local: list of rust-exports.map and rust-unexports-macos.txt. libobs-rust already links the crate's ffi. ci/test_ported_sources.py checks the group on every host. The exported symbol sets are identical (1766) on the C, hybrid and C reference libobs, and the ten functions are local in each; the hybrid libobs has no C object for the group. test_encoder_lifecycle_steps still compiles the C file into itself, unchanged. It has no _rust variant, as for test_encoder_audio_start_steps: the shims' archive member also holds the rest of obs-output, which needs libobs's hidden seams and obs-util, so it does not link on its own. Its tests.d file says so. soldr cargo test -p obs-data --test obs-tests-core -- output::encoder_lifecycle passes 19 of 19. Through ctest (private Xvfb and D-Bus, own HOME, no sound), the full suite passes against the C libobs (538 of 538), the hybrid libobs (570 of 570) and the C reference libobs (570 of 570), the same 3 skipped on each (a PulseAudio default sink and the v4l2 vivid module, absent here). Among them test_encoder_lifecycle_steps, test_encoder_join_keyframe, test_encoder_error_stop, test_encoder_group_null, test_multitrack_ladder, test_output_interleave_e2e, test_output_interleave_restart, test_texture_encoder_raw_format, test_output_delay_restart and test_encoder_bind_steps. Refs obs-rust#2512 * obs-output: Check the encoder lifecycle against C Tier 3 for the encoder_lifecycle port. The oracle (rust/obs-c-oracle/oracle/encoder_lifecycle.c) compiles the unchanged libobs/obs-encoder-lifecycle.c.h with its ten functions renamed to oracle_*, plus its two constants, bound in src/encoder_lifecycle.rs. Nothing in it divides. The type tests' oracles are encoder_bind's and encoder_audio_start's. tests/output/encoder_lifecycle_parity.rs compares the oracle, the shims and the core exactly, all three sides: - the constants (the texture cap and DARRAY_INVALID); - first, found and last over 0 to 1,000,000, the top 1,000,000 of usize, a stride through all of it and 29 special values; - the group counts (the LCM test, the idle test and both steps) over 0 to 1,000,000, the top 1,000,000 of u32 and a stride through all of it, and ready over every pair of counts below 1,024 and of 24 special values against encoder counts below 1,024 and 29 special values; - the texture cap over every word of the low 20 bits under four high words; - the SEI test over both flags and the sizes above; - both type tests over -100,000 to 100,000, the 100,000 values at each extreme and a stride through all of i32; - the callers on test_encoder_lifecycle_steps' stand-in encoder (shared with Tier 1), on the oracle's gates and on the shims: a fixed run of every caller for each of six types, five caps, five mixes, both requests, six SEI answers, six groups (none, idle, destroy_on_stop, one started, of none, UINT32_MAX started) and three output lists (32,400 encoders), comparing the end state; - proptests (20,000 cases each) of every decision over full-range values and of the stand-in over sequences of up to fifteen calls. No input is excluded: nothing in the C divides or overflows (the steps wrap as uint32_t does). Tier 1's stand-in, its gates, the call strategy and the fixed run are pub(crate), so both tiers run the same caller bodies. The proptests of both files keep no regression files (failure_persistence None). Mutation check (once, not committed): 31 mutants, 21 in the core and 10 in the shims. mutant Tier 1 Tier 3 first: != 0 9 4 first: <= 1 5 3 found: == 10 4 found: != 0 9 3 last: != 0 7 4 last: false 6 3 pass_texture: any cap 6 4 pass_texture: bit 0 7 4 lcm_unset: == 1 5 3 started_inc: saturating 3 3 started_inc: + 0 6 4 group_ready: > 5 3 group_ready: == 4 3 started_dec: saturating 4 3 started_dec: + 1 6 4 group_idle: != 0 6 4 group_idle: <= 1 5 3 prepend_sei: || 6 4 prepend_sei: ignores size 5 3 prepend_sei: ignores got 6 4 prepend_sei: ignores has_sei 6 4 shim first inverted 0 4 shim last: index - 1 0 3 shim found: last 0 3 shim pass_texture: caps >> 1 0 4 shim lcm_unset inverted 0 4 shim started_inc: dec 0 4 shim group_ready: count - 1 0 2 shim started_dec: inc 0 4 shim group_idle inverted 0 4 shim prepend_sei: size - 1 0 3 (failing tests per tier.) Tier 1 kills all 21 core mutants and Tier 3 all 31. soldr cargo test -p obs-data --test obs-tests-core -- output::encoder_lifecycle passes 28 of 28 (19 Tier 1, 9 Tier 3). Refs obs-rust#2512 * docs: Record the encoder lifecycle port rust/README.md gets the row for libobs/obs-encoder-lifecycle.c.h (obs-output::encoder_lifecycle), after encoder_bind's: what moved, the type tests shared with encoder_bind and encoder_audio_start, what stays C and why (every mutex and section, the walk, lcm() and its division, the single-field tests), the two behaviour-neutral changes (the group counts through a local, the SEI pointer and size initialized), the characterized-not-endorsed behaviours (the count wrap, the LCM of a 0 divisor, the route asked again at the stop, the packet times cleared for a missing callback) and the tests that cover it; the obs-output crate row names the file. The crate's Cargo description and lib.rs named the module in the port commit, and docs/rust-port/naming.md maps the file (the move commit). rust/obs-c-oracle/benches/port_comparison/encoder_lifecycle.rs, in the sorted module list, runs 1024 rounds over audio and video encoders with four callback counts, four indexes (one not found), four cap words, group counts 0 to 2 and SEI answers from a fixed table, asking every decision a start, a connect, a send, a first keyframe and a stop ask, on the shims and on the oracle, and checks both checksums match. Release, x86_64 Linux, GCC, median (load average about 5): Rust 12.41 us, C 31.35 us (0.40x), about 12 ns a round, next to a mutex lock or one callback the C makes around it. The port changes nothing a user can see, so docs/features.md has no line. Fixes obs-rust#2512
This was referenced Oct 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2496
What and why
Ports the encoder media binding and shutdown checks of
libobs/obs-encoder.c.htorust/obs-output, as a new moduleencoder_bind, next toencoder_sizeandencoder_audio_start. Every recording, stream and replay buffer passes through these when an output binds its encoders to the canvas video and the audio mix, when the canvas is reset, and when an output stops before it started:obs_encoder_set_audio()andobs_encoder_audio();encoder_set_video()stores (the frame period,fps_denoverfps_num, or 0 over 0 with no video) and whether it creates the frame rate divisor video;obs_encoder_set_audio()stores (1 over the sample rate, or 0 over 0);obs_encoder_shutdown_unstarted(), eachpthread_mutex_trylock()result and the shutdown itself (initialized and not needed).The video type refusal is reused, not copied.
obs_encoder_set_video(),obs_encoder_video(),obs_encoder_parent_video()andobs_encoder_unbind_video()now askobs_encoder_audio_start_not_video(), the refusalobs_encoder_set_gpu_scale_type()andobs_encoder_set_frame_rate_divisor()already ask (#2331).encoder_bindre-exports it.One function per decision, asked where the C made it. Six functions cross the C ABI:
obs_encoder_bind_not_audio(),_video_timebase(),_audio_timebase(),_divisor_override(),_locked()and_shutdown_unstarted(). The plan's combined gates (type, active and initialized in one call; the unbind and the rebind as one decision each; the getters' override choice) would read the active flag,initialized,mediaorfps_overrideon paths where the C's&&and early returns never read them. So they are split the wayobs_encoder_set_gpu_scale_type()already is: the type refusal is a function, and the single-field tests stay C, in their order.obs_encoder_shutdown()(acontext.datatest and constant resets) andobs_encoder_rebind_video()(three single-field tests in an&&chain) have nothing left to decide, so they are unchanged.What stays C, at its point and under its condition:
obs_encoder_valid(), every warning, the active flag (an atomic, loaded where it always was), and theinitialized,media,fps_overrideandcontext.datatests;init_mutexandoutputs_mutex, both trylocks (their results are passed) and their unlock order; no lock section is merged;boundand stops at the first match) and the walk over the encoder's outputs (it computesneeded, callingobs_output_needs_encoders()for each, and stops at the first that needs the encoder);obs_encoder_shutdown()withinfo.destroy,maybe_clear_encoder_core_video_mix()and the paired-encoder weak releases;obs_encoder_set_last_error();video_output_get_info(),audio_output_get_sample_rate(), the divisor video's free and create, and every write to the encoder.Nothing divides: the timebase is two copies. The header copies
OBS_ENCODER_AUDIO, andobs-encoder.c.hchecks it with_Static_assert. The timebase comes back by value as a two-u32struct; no pointer crosses.One order changes, inside a branch with no lock or call in it. The timebase now reads the video's
fps_numandfps_den(and the audio's sample rate) before the encoder'stimebase_numwrite, where the C wrotetimebase_numbetween them. Those are the video's and audio's info, which nothing writes after they are created. The encoder's writes keep their order (media,timebase_num,timebase_den).Six commits:
Move (behaviour-neutral,
377741a75): the decisions go tolibobs/obs-encoder-bind.c.h, declared in the newobs-encoder-bind.hwithoutEXPORT, so they are hidden. The file joinslibobs.core-output.git blameon the moved lines shows upstream commits and two of the fork's (the unstarted shutdown and the unbind and rebind across a video reset).The move was checked against the original code. A scratch harness (not committed) compiles origin/master's ten functions verbatim (both setters,
encoder_set_video(), the three getters,obs_encoder_shutdown(),_shutdown_unstarted(),_unbind_video()and_rebind_video()) next to this branch's, with every read and write of an encoder field rewritten the same way into a logged access:obs_encoder_valid(),obs_output_needs_encoders(),video_output_get_info()and the video info reads, the sample rate, the divisor free and create,info.destroy, the weak releases, the last error, the mix clear and each warning are logged in order;initializedand the divisor whileinit_mutexis not held;mediawrite to thetimebase_denwrite is compared as a sorted run (the reorder above), and must hold nothing but the info reads and thetimebase_numwrite;context.data,mediaandfps_override, divisors 0, 1, 2 andUINT32_MAX, frame rates of 30000/1001, 60/1, 0/1, 0 andUINT32_MAX, sample rates of 0, 44100, 48000 andUINT32_MAX, 0 to 4 outputs, 0 to 3 videos to unbind from, 0 to 2 paired encoders, busy trylocks, and one to six calls in a row.2,900,000 runs (GCC and Clang
-O2, 1,000,000 each; GCC-O0, 500,000; ASan with UBSan under GCC-O0and Clang-O2, 200,000 each): all 80,633,324 logged events match, and so does the end state after each run. Every branch of origin/master's functions is reached both ways (gcov, 72 of 72). After the rebases ontoaee720fbc,b13780b3aand0eb612ac4(none touchesobs-encoder.c.h), another 300,000 runs match.25 deliberately broken moves were tried, and all 25 fail the harness: the active flag read before the type refusal (both setters);
initializedread with the active flag;fps_overrideread before the type; the video info or sample rate read before themediawrite; the divisor read before the timebase write; the trylocks or the unlocks swapped;initializedread before the outputs walk; the walk not stopping; the active flag read beforeinit_mutexin the unbind; the rebind&&order changed; the wrong type refusal in four places; and nine changed decisions.Characterization (
39a5c0190):test_encoder_bind_steps, 13 cases. It compiles the moved file andobs-encoder-audio-start.c.hwith ASan and UBSan, and calls only their functions. It pins every decision over edge values, and copies the setters, getters, unstarted shutdown, unbind and rebind onto a stand-in encoder, end to end:fps_num0 (a timebase den of 0: pinned, held);bound/active/initialized combination, of an audio encoder, with no media and with no videos; the rebind with each gate; an unbind then rebind that keeps the divisor.It is in the test graph, sorted, with both headers as watch inputs. No
assert_in_rangeorfail_msg, and no comparison between two enum types (MSVC C5287).RED (
598cacad4): Tier 1 againsttodo!(). It ports the 13 C cases 1:1 and adds 7 edge tests against the origin/master expressions inline: every type and trylock result from -1,000 to 1,000 and the extremes, every pair of frame rates below 257 with and without a video, every divisor to 10,000, the shutdown truth table, the stand-in through every caller for 2,304 encoder states, and proptests over full-range values and over call sequences with another thread flipping state between calls.GREEN (
7c320d9a1): the safe core (six functions) and six#[no_mangle]shims, in a newlibobs.ported-encoder-bindgroup whose PCH is only the new header.source-sets.json, the unity lists, the three export lists andci/test_ported_sources.py, each in sorted place.test_encoder_bind_stepshas no_rustvariant, as fortest_encoder_audio_start_steps: the shims' archive member also holds the rest ofobs-output, which does not link on its own. Its tests.d file says so.Tier 3 (
72ad780b0): a C oracle of the unchanged file, the parity tests and a mutation check.Docs (
2d650a15b): the README row and aport_comparisonbench file in the sorted lists.docs/rust-port/naming.mdmaps the file in the move commit. The port changes nothing a user can see, sodocs/features.mdhas no line.The core denies clippy's arithmetic, indexing,
as, unwrap, expect, panic, todo, unimplemented and unreachable lints. It keeps no state, takes no lock and does not divide.obs_encoder_set_audio()has no initialized refusal, so an initialized idle audio encoder takes a new audio, whereobs_encoder_set_video()refuses an initialized video encoder;fps_num0 gives a timebase den of 0, and a sample rate of 0 a timebase of 1 over 0 (the settings never give either).Mutation check (once, not committed), failing tests per tier:
==OBS_ENCODER_AUDIO = 1> 1!= 0>= 0||<= 0Tier 1 kills all 15 core mutants, Tier 3 all 22.
port_comparisonbench (encoder_bind/setters_and_shutdown/1024, release, x86_64 Linux, GCC, median): Rust 8.42 µs, C 8.18 µs (1.03x), about 8 ns a round, next to a trylock or avideo_output_get_info()call.RED → GREEN
598cacad4.soldr cargo test -p obs-data --test obs-tests-core -- output::encoder_bindpasses 1 of 20 (the constant, already ported) and fails 19 with "not yet implemented";local_gate.py --test-diff origin/masterfails exactly those 19 and nothing else;--lane lintpasses.7c320d9a1, 29 of 29 (20 Tier 1, 9 Tier 3) at the head. Full ctest on the GREEN commit before the rebases (a95fa15d4, on4729f1270; private Xvfb and D-Bus, own HOME and runtime directory, no sound): 532 of 532 on the C libobs, 564 of 564 on the hybrid libobs and 564 of 564 onlibobs-c-reference, the same 3 environment skips on each (a PulseAudio default sink and the v4l2 vivid module). Atf8ab40331(this head before its last rebase onto0eb612ac4, which touches only two obs-graphics Rust files), the encoder, output, video reset, multitrack, canvas and transition tests: 81 of 81 (C), 84 of 84 (hybrid), 84 of 84 (C reference), includingtest_encoder_bind_steps,test_encoder_video_reset,test_output_failed_start_encoders,test_encoding_lag_rescale,test_encoder_tex_active_null,test_multitrack_encoder_overrideandtest_texture_encoder_raw_format. Exported symbol sets identical (1766) on the C, hybrid and C reference libobs (libobs-c-referencerebuilt before each run); the six functions are local, and the hybrid libobs has no C object for the group.local_gate.py --test-diff origin/master,--lane lintand--lane cross(Windows MSVC and macOS clippy) pass atf8ab40331; after the rebase, Tier 1 and 3 (29 of 29),--test-diff origin/masterand--lane lintpass again at2d650a15b.Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): not applicable, none