Repository navigation
obs-output: Port the encoder pause checks - #3037
Merged
Merged
Conversation
Behaviour-neutral extraction before the Rust port. The decisions of video_pause_check() and audio_pause_check() in obs-encoder.c move to libobs/obs-encoder-pause.c.h, as nine functions on plain values declared in the new libobs/obs-encoder-pause.h without EXPORT (hidden, as -fvisibility=hidden keeps them): - video_pause_check(): obs_encoder_pause_off(ts_start), no pause on; obs_encoder_pause_video_step(ts_start, ts_end, ts), whether the frame passes, is dropped or ends the pause; - audio_pause_check(): obs_encoder_pause_off() again, obs_encoder_pause_audio_end(), the buffer's end, obs_encoder_pause_audio_start(), where the pause start falls against the buffer, and obs_encoder_pause_audio_paused(), where its end falls for a buffer after the start; for each cut, obs_encoder_pause_audio_cut_ns(), then obs_encoder_pause_audio_kept() (pause_audio()) or obs_encoder_pause_audio_left() (unpause_audio()), and obs_encoder_pause_audio_drop(), a buffer with no frame left. The arithmetic is C's, unchanged. obs-encoder.c keeps every lock and unlock of pause->mutex, the last_video_ts store, every read of the pause record and the buffer, audio_frames_to_ns() and ns_to_audio_frames() (util_mul_div64(), which traps on a zero rate and a quotient above 64 bits on MSVC x64), every write to ts_start, ts_end and the buffer's frames and timestamp, the data pointer advance of each plane, the ts_offset subtraction, pause_reset() and every caller. The one reordering: the video step reads ts_start and ts_end together under the lock, where the C read ts_start only when the frame was not at ts_end; both are plain fields of the locked record. Checked in a scratch harness (not committed): master's video_pause_check(), audio_pause_check(), pause_audio() and unpause_audio() against the moved code, over logging stand-ins for the mutex, on 300,000 random sequences of up to 40 steps (pauses, unpauses, resets, video frames, and audio buffers of 0 to 4096 frames on 0 to 8 planes at eight sample rates, with times near each other and near the top of the u64 range), logging every result, the record, the buffer's frames, timestamp and plane offsets, and every lock: gcc and clang, -O0 and -O2, with and without ASan/UBSan, 0 differences in 8 builds. 12 planted mutants of the moved file (the off test, the video end and drop compares, the buffer end, the start's two compares, the end's two compares, the cut, the kept and left frames and the empty test) are all caught. Exported symbols are unchanged and identical on the C, hybrid and C reference libobs (1767); the nine functions are local. Refs #3033.
Characterization before the Rust port, against the C moved to libobs/obs-encoder-pause.c: test_encoder_pause_steps (every platform, the moved file on its own, ASan and UBSan), 11 cases: - a pause is off only when its start is 0; - a video frame before the start passes, one at or after it is dropped, one exactly at the end ends the pause; an unpause in the pause's own frame, and a frame stamped 0 while the end is not set (characterized, not endorsed); - the buffer's end as a uint64_t sum, and its wrap; - where a pause start falls against a buffer (after it, inside it or at either end, before it), and an empty buffer; - where a set or unset end falls for a paused buffer; - the cut, the frames kept and left with their uint32_t truncation and wrap, and the empty test; - a whole pause through video frames and through audio buffers at 48 kHz: passed, cut at the start (480 frames kept), dropped before and after the end is set, cut at the end (240 frames and 960 bytes per plane off the front) and stamped with the pause start before the offset is taken off (characterized, not endorsed), then passed; - a start at either end of a buffer (nothing kept and dropped; 1023 of 1024 frames kept, as both conversions truncate), an empty buffer; - an end at either end of a buffer (nothing cut; one frame left); - the offset taken off every buffer, dropped or passed. video() and audio() in the test ask the moved functions as obs-encoder.c does, on a stand-in for the pause record. Not covered: the inputs the port changes on purpose (a video frame after a set end that never lands on it, a paused buffer that starts after a set end). Deliberate breaks of the moved C (the 12 mutants of the move's harness): all 12 caught. Refs #3033.
RED for #3033. obs_output::encoder_pause has its API and docs, with todo!() bodies: off() (no pause on), video_step() (a frame passes, is dropped or ends the pause), audio_end() (a buffer's end), audio_start() and audio_paused() (where the pause start and end fall against a buffer), audio_cut_ns(), audio_kept() and audio_left() (the cuts) and audio_drop() (an empty buffer). tests/output/encoder_pause.rs: - steps_*: test_encoder_pause_steps.c 1:1 (11 cases, same names, values and expectations), through video() and audio(), which ask the core as obs-encoder.c does; - edge_*: the two intentional differences through whole pauses (a divided encoder's unpause between its frames ends on its next frame; a pause and unpause inside one audio buffer end on the next buffer, which keeps its own timestamp), a pause end that is not set yet, and proptests of every decision against a model of the C where the port keeps it and of the differences exactly where they apply (a set ts_end before the frame or the buffer), of whole pauses on the 60 fps grid with divisors 1 to 4 and buffers at 44.1, 48 and 96 kHz that always end within a frame and a buffer of the unpause, and of any input without a panic. All 20 fail on the stubs (obs-tests-core, output::encoder_pause). Refs #3033.
GREEN for #3033. obs_output::encoder_pause implements the pause checks of obs-encoder.c: - off(): ts_start == 0; - video_step(): End for a frame at ts_end, or after a set ts_end; Drop for one at or after ts_start; Pass otherwise; - audio_end() and audio_cut_ns(): the wrapping u64 sum and difference; - audio_start(): Before, Inside (both ends included) or After; - audio_paused(): Unpause for an end inside the buffer (both ends included), Resume for a set end before it, Drop otherwise; - audio_kept() and audio_left(): the cut truncated to u32, and the wrapping u32 difference; - audio_drop(): frames == 0. Intentional differences: - the C ends a video pause only on a frame stamped exactly ts_end. The unpause time is on the frame grid of the output's first video encoder, so an encoder with a frame rate divisor of 2 or more, fed through video-io, may never get that frame: it drops every later frame and keeps its record set, so the output refuses every later pause. Here the first frame at or after a set ts_end ends it; - the C drops a paused audio buffer that starts after a set ts_end, and every later one: a pause and unpause inside one buffer (a quick double press of the pause hotkey, a script or a websocket client) cut that buffer at the start and never look at the end, so the encoder gets no more audio and keeps its record set. Here such a buffer ends the pause and passes with its own timestamp (OBS_ENCODER_PAUSE_AUDIO_PAUSED_RESUME, which audio_pause_check() handles by clearing ts_start and ts_end and dropping the buffer only if it is empty). The audio from ts_end to the end of the buffer that was cut, at most one buffer, is still lost. docs/features.md has the line. The module denies clippy::todo now too. C ABI (ffi::encoder_pause): nine shims on plain values. libobs: obs-encoder-pause.c.h moves from libobs.core-output to its own group libobs.ported-encoder-pause, whose precompiled header includes only the unit's header. The hybrid libobs leaves the group out and links the shims; the nine symbols are listed in rust-exports.map, rust-hidden.txt and rust-unexports-macos.txt and stay hidden. ci/test_ported_sources.py lists the object. Exported symbols are unchanged and identical on the C, hybrid and C reference libobs (1767). Tier 1: 20/20 (obs-tests-core, output::encoder_pause). Tier 2: the whole cmocka suite, among them test_encoder_pause_steps and test_replay_buffer_pause_stop, passes against the hybrid libobs (635 of 636) and libobs-c-reference (635), 3 skipped on each (two v4l2 vivid cases and a PulseAudio one, with no vivid module or sound server). test_source_monitor_only_load times out on both builds without a sound server, as it does on the plain C build, and is left out of the count. Refs #3033.
Tier 3 for #3033. obs-c-oracle builds the original libobs/obs-encoder-pause.c.h as oracle_obs_encoder_pause_* (oracle/encoder_pause.c). tests/output/encoder_pause_parity.rs runs every call on the shim, the C and the core and compares the results exactly: - exhaustive over a grid of times (0 to 3, the 60 fps grid at frame and half-frame steps from 1 s, the 48 kHz buffer edges, each with its neighbours, and the top of the u64 range): every video step and every audio start and end route with three times, the buffer end and the cut with two; the frames kept and left for buffers of 0, 1, 480, 1023, 1024, 4096 and u32::MAX frames against cuts of 0 to 4100 and the 32-bit edges; - whole pauses through video_pause_check() and audio_pause_check() as obs-encoder.c asks them, the C and the shims side by side: video at frame rate divisors 1 to 4 for 1,560 pauses each on the frame grid, and audio buffers of 0, 1, 480 and 1024 frames at 44.1, 48 and 96 kHz with the start and end at half-frame steps, the end set before the first buffer or after the pause began. Both outcomes occur at every divisor above 1 and every buffer length; - proptests over times near the top of the range, single buffers there, and any input on the shim against the core. Intentional differences, checked exactly where they apply: a video frame after a set ts_end ends the pause, where the C ends it only on a frame at ts_end, and a paused buffer after a set ts_end ends the pause and passes, where the C drops it. Over a whole pause the two agree up to that frame or buffer; from it on the C drops everything with its record set, and the port passes everything with the record clear. At divisor 1 the C never splits from the port. 9 tests. Deliberate breaks of the core (14: the off test, the video end with and without its two terms, the video drop, the audio start's two compares, the audio end's two compares and its set-end guard, the buffer end and cut wraps, the truncation, the left frames' wrap and the empty test): all 14 caught here and by Tier 1. port_comparison (encoder_pause/recording/10s: 600 video frames and 469 audio buffers of a recording paused from 3 s to 6 s): Rust 3.17 us, C 3.10 us on an x86_64 Linux build machine. Refs #3033.
rust/README.md: the obs-output row names libobs/obs-encoder-pause.c.h, and a row for the unit says what moved, what obs-encoder.c keeps, the C behaviour kept, the intentional differences and the tests. docs/rust-port/naming.md lists the file under rust/obs-output, and the crate's description and module docs name it. Fixes #3033.
obs_encoder_pause_video_step(ts_start, ts_end, ts) read both pause times under the lock, where video_pause_check() read ts_start only for a frame not at ts_end. It becomes two decisions, each asked where the C made it, from the field the C read there: - obs_encoder_pause_video_end(ts_end, ts): the frame ends the pause; - obs_encoder_pause_video_drop(ts_start, ts), for a frame that does not: it is dropped. The OBS_ENCODER_PAUSE_VIDEO_* results go. The Rust core (video_end() and video_drop(), the first keeping the port's end on a frame after a set ts_end), its shims, the oracle, the characterization, Tier 1, Tier 3, the bench, the three export lists and the rust/README.md row follow. The library now hides ten functions. The move harness reruns on the split C against master's functions: 0 differences in all 8 builds over the same 300,000 sequences, and the 12 mutants (the two video ones now on the new functions) are caught by it and by test_encoder_pause_steps. Exported symbols unchanged and identical on the C, hybrid and C reference libobs (1767); the ten functions are local. Tier 1 and Tier 3: 29/29. Refs #3033.
The Tier 1 proptests of encoder_pause ran a fixed 4096 cases; they now go through obs_c_oracle::parity::cases(4096), so OBS_PARITY_CASES scales them as it does every other count. The parity grids sampled at a reduced level with parity::keep() alone, so edge cases such as (0, 0, 0) could be skipped. They now run on every level, outside the sampled part: - the three-time audio grid: a time at 0 or the top of the range, or two times equal or one apart, where every compare turns; - the whole audio pauses: a pause that ends where it starts or starts at the first buffer, and the empty and one-frame buffers. The two-time grids, the cuts and the video pauses already run whole. The module docs say which part is sampled. 29/29 at reduced (0.31 s) and full (0.54 s). The 14 core mutants are all caught at reduced, by Tier 1 and Tier 3. Refs #3033.
rdoupe
force-pushed
the
fix/rust-port-encoder-pause-and-unpa-296
branch
from
October 30, 2026 12:46
0569f89 to
b55b533
Compare
rdoupe
marked this pull request as ready for review
October 30, 2026 13:33
zackees
reviewed
Oct 30, 2026
zackees
left a comment
Member
There was a problem hiding this comment.
Thanks, @rdoupe. This is another very careful port, and the evidence makes it easy to review. Coordinator review: LGTM, queuing.
- Move is behaviour-neutral. I read
obs-encoder.c.hagainst master: every lock ofpause->mutex, thelast_video_tsstore, bothutil_mul_div64()conversions (audio_frames_to_ns,ns_to_audio_frames), every record and buffer write, the plane pointer advance,ts_offsetandpause_reset()stay in C. The moved C inobs-encoder-pause.c.hmaps 1:1 onto the old branches; theSTART_AFTERpath falls through toreturn falseas before. The 300,000-sequence harness (gcc/clang, -O0/-O2, ASan/UBSan, 12 of 12 mutants caught) is strong proof. - The intentional differences are wired cleanly. The C
obs_encoder_pause_audio_paused()never returnsRESUME, so the C-reference build behaves exactly like upstream, and only the Rust shim takes the new branch. In the Rust,video_endandaudio_pausedguard both fixes withts_end != 0, so a pause with no unpause yet can't end early. Both fixes are real upstream bugs (a divisor ≥ 2 encoder, or a pause and unpause inside one audio buffer, leave the record set for the rest of the recording) and both have a features-ledger line. - Tiers. Tier 1: 20 tests including the 11 C cases. Tier 2: 637/637 on both hybrid and C-reference. Tier 3: 9 parity tests that agree with the C up to the first frame or buffer after a set end and check the split exactly where it applies; 14 planted mutants caught at the reduced level too. Exported symbols unchanged (1767 on all three builds), and the ten functions are local.
- Rules. No existing C test or public header edited (
test_encoder_pause_steps.cis new). The new unity group, PCH and the three export lists follow BUILD.md.
The bench is at parity (3.40 µs vs 3.38 µs). Nice work.
This was referenced Nov 7, 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 #3033
What and why
Ports the pause checks of
obs-encoder.c(which video frames and audio a paused recording drops, and where each encoder's pause ends) torust/obs-outputas the moduleencoder_pause, in six commits and two review follow-ups:libobs: Move encoder pause checks to own file): the decisions ofvideo_pause_check()andaudio_pause_check()move, behaviour-neutral, tolibobs/obs-encoder-pause.c.has hidden functions on plain values: whether a pause is on, whether a video frame ends the pause or is dropped, an audio buffer's end, where the pause start and end fall against a buffer, the cut, the frames kept and left, and the empty test.obs-encoder.ckeeps every lock ofpause->mutex, thelast_video_tsstore, every read of the record and the buffer, bothutil_mul_div64()conversions (audio_frames_to_ns(),ns_to_audio_frames(), so their zero-rate and MSVC x64 traps stay in C), every write to the record and the buffer, the data pointer advance of each plane, thets_offsetsubtraction,pause_reset()and every caller.test_encoder_pause_steps(11 cases, the moved C on its own, ASan/UBSan): video frames before, at the start of, inside and at the end of a pause; audio buffers before the pause (passed), holding the start (cut), wholly inside (dropped) and holding the end (cut, and stamped with the pause start before the offset is taken off: characterized, not endorsed); a start or an end at either end of a buffer, the truncating conversions (a start at a buffer's end keeps 1023 of 1024 frames), and the offset taken off every buffer.obs_output::encoder_pauseontodo!()stubs.libobs.ported-encoder-pause, hidden in all three export lists.oracle_obs_encoder_pause_*, the parity tests and aport_comparisonbench.rust/README.md,naming.md, the crate description and module docs.libobs: Split the encoder pause video decision): the video step becomesobs_encoder_pause_video_end(ts_end, ts)andobs_encoder_pause_video_drop(ts_start, ts), sots_startis read only for a frame that does not end the pause, as in the original. Ten hidden functions in all.test: Scale encoder pause cases, keep the edges): the Tier 1 proptests go throughparity::cases(), and the sampled parity grids keep their edge cases (a time at 0 or the top of the range, two times equal or one apart, a pause that ends where it starts, empty and one-frame buffers) on every level.Intentional differences (fixed in the port):
OBS_ENCODER_PAUSE_AUDIO_PAUSED_RESUME, whichaudio_pause_check()handles by clearing the record); the buffer passes with its own timestamp. The audio from the end to the end of the cut buffer (at most one buffer) is still lost.docs/features.mdhas the line under Fixed upstream bugs. No upstream issue.RED → GREEN
obs-output: Add Tier 1 tests for encoder pause): all 20 Tier 1 tests fail on the stubs with "not yet implemented" (obs-tests-core,output::encoder_pause);--test-diffon it ran 5268 tests and failed only those 20 (6 skipped), and the lint lane passed.output::encoder_pause): the 11 C cases 1:1, both differences through whole pauses, and proptests of every decision against a model of the C, whole pauses on the 60 fps grid with divisors 1 to 4 and buffers at 44.1, 48 and 96 kHz, and any input without a panic.test_encoder_pause_steps,test_output_pause_stepsandtest_replay_buffer_pause_stop(a recording paused while a replay buffer shares its encoders): 637/637 against the hybrid libobs and 637/637 againstlibobs-c-reference, 3 skipped on each (two v4l2 vivid cases and a PulseAudio one: no vivid module or sound server), under a private Xvfb.test_source_monitor_only_loadtimes out on both without a sound server, as it does on the plain C build, and is left out of both counts.output::encoder_pause_parity): exhaustive over a grid of times (the frame grid at frame and half-frame steps, buffer edges, 0 to 3 and the top of theu64range) for every decision (at a reduced level the three-time audio grid samples, keeping its edges), the cut against buffers of 0 tou32::MAXframes, whole pauses through both checks with the C and the shims side by side (video at divisors 1 to 4, 1,560 pauses each; audio at 44.1, 48 and 96 kHz with buffers of 0, 1, 480 and 1024 frames, the end set early or late), proptests near the top of the range. The differences are checked exactly where they apply: the two agree up to the first frame or buffer after a set end, and from there the C stays paused with its record set while the port passes everything; at divisor 1 they never split. 14 planted mutants of the core: all caught by Tier 3 and by Tier 1, at the reduced level too.--test-diff5310/5310, 6 skipped;./unittest's Rust lane ok (its Docker Tier 2 lane can't reach Docker on this machine; Tier 2 ran natively as above);--lane lintand--lane crossok;cargo clippy --workspace --target x86_64-pc-windows-msvc --all-targetsclean.port_comparison(encoder_pause/recording/10s: 600 video frames and 469 audio buffers of a recording paused from 3 s to 6 s): Rust 3.40 µs, C 3.38 µs.OBS_PARITY_CASES=reduced(0.45 s at full), peak RSS 23 MB over 20 runs, no own threads.Move proof: master's
video_pause_check(),audio_pause_check(),pause_audio()andunpause_audio()against the moved code over a logging stand-in for the mutex, 300,000 random sequences of up to 40 steps (pauses, unpauses, resets, video frames, audio buffers of 0 to 4096 frames on 0 to 8 planes at eight sample rates, times near each other and near the top of theu64range): gcc and clang, -O0 and -O2, with and without ASan/UBSan, 0 differences in 8 builds; 12 planted mutants all caught, by the harness and by the characterization. Rerun on the split video decision: the same 0 differences and 12 of 12 caught.Runs were on the branch rebased onto a5d5017 (after the output pause plan landed), and lint and the pause tests again on e0c5db5, the base it is pushed on.
Checklist
master(or the description starts with a Stacked: note)docs/features.mdis added or updated (not applicable: no user-visible change)-W..., ratchets): built with scripting ON (PR CI builds no OBS app: rungh workflow run ci.yml --ref <branch>for the full builds) and cross-checked with clang and MSVC