Skip to content

libobs: Port the caption text wrap to obs-output - #1620

Merged
zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-caption-text
Oct 21, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
rdoupe:rust/port-caption-text

Conversation

@rdoupe

@rdoupe rdoupe commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1617 (PR 2 of 2; PR 1, #1618, moved the wrap into libobs/obs-output-caption-text.c.h and extended test_output_caption_text to 32 cases)

What and why

This PR ports libobs/obs-output-caption-text.c.h, the caption text wrap and pagination, to the caption_text module of rust/obs-output (#1567, #1603). obs_output_output_caption_text2() turns the text it is given into caption pages, one call per page until the text runs out. A page has up to 15 rows of up to 32 characters. The pages go on to libcaption, so the port has to be byte-identical: a different wrap point would be visible to viewers. libobs-with-rust drops the C file and links the Rust shim in its place.

  • Safe core (src/caption_text.rs):
    • The four helpers (utf8_continuation, utf8_char_length, is_newline, utf8_whitespace), plus text_row() and text_page(). Each takes a &[u8] that ends at its first NUL or at the end of the slice.
    • Row and Page hold the text in fixed buffers of CAPTION_LINE_BYTES and CAPTION_PAGE_BYTES. They also record how many bytes the cursor consumed, and whether the call read the end of the slice.
    • Byte for byte the C. That includes the five behaviours test_output_caption_text marks "characterized, not endorsed":
      • an invalid byte is dropped, so the words on either side join;
      • a newline is skipped as leading whitespace, so blank lines give no empty rows;
      • whitespace before a newline or the end of the text is kept;
      • with whitespace as characters 32 and 33, the row ends with a space;
      • C0 control bytes and DEL become spaces.
    • It keeps the 128-byte row bound and the cursor that always moves forward on a non-empty page.
    • The text is untrusted (obs-websocket SendStreamCaption, caption plugins), so the module denies clippy's indexing_slicing, arithmetic_side_effects, unwrap_used, expect_used, panic, todo, unimplemented and unreachable. Every read is a checked get, every write a bounded copy, and every addition saturating. I checked that the lints fire: a probe indexing and adding inside the module fails clippy.
    • No host cfg, no unsafe.
  • Shim (src/ffi/output_caption_text.rs):
    • obs_output_caption_text_page has the C signature. It writes the page and its NUL (bytes 0 to the length, the same bytes the C writes) and moves the cursor.
    • The core takes a slice. Measuring the whole rest of the string on every call would read the text once per page, where the C reads only what one page needs. That would be quadratic on a long caption. So the shim gives the core a window of the string: up to the NUL, or 2048 bytes. While the page reads the end of a window that is not the end of the string, the shim doubles the window and asks again. A call reads about what the C reads, and never past the NUL.
    • text_page_with_window is Rust-mangled (hidden by _ZN*). The parity test drives it with windows of 1 to 7 bytes so that most calls retry.
  • C side, the smallest change that lets the file live in its own translation unit:
    • caption_text_page() becomes obs_output_caption_text_page() with external linkage. It is declared in obs-internal.h without EXPORT, and obs-output.c.h calls it by that name. The other five functions stay static.
    • obs-output-caption-text.c.h leaves libobs.core-output for its own group, libobs.ported-output-caption-text, listed in source-sets.json (LIBOBS_PORTED_SOURCES). Its PCH, libobs-ported-output-caption-text.pch.h, is obs-internal.h plus string.h.
  • Symbol: in rust-hidden.txt, rust-exports.map and rust-unexports-macos.txt. It is not EXPORT, so there is no rust-exports.d fragment. nm shows t obs_output_caption_text_page in both libobs.so builds. The exported symbol sets are unchanged.
  • Oracle (oracle/output_caption_text.c): the original file, unmodified, with the page function renamed. It adds oracle_output_caption_text_* wrappers around its static functions, and the CAPTION_* values.
  • Intentional differences: none. One that nothing can see: a null text, *text or page returns 0 and writes nothing, where the C would dereference it. libobs never passes one.

test_output_caption_text (32 cases) links OBS::libobs and drives the public obs_output_output_caption_text2(). That makes it the end-to-end proof: in mode ON it runs the Rust port through real libobs.

RED → GREEN

  • RED commit: 2788b6f2e libobs: Add Tier 1 tests for the caption text wrap port. The core is todo!(). soldr cargo test -p obs-data --test obs-tests-core output::caption_text:: fails 48 of 49 with "not yet implemented"; only the constants test passes.
    • Tier 1 (tests/output/caption_text.rs) ports test_output_caption_text 1:1: the 32 cases under the same names, in the same order, with the same assertions.
      • obs_output_output_caption_text2() stays C. The tests repeat its loop over text_page(): one page per call until the text runs out, and only a sole page may be empty.
      • The C test's helpers are ported too: assert_valid_utf8, assert_pages_preserve, assert_single_page and assert_page_invariants.
    • 14 new edge cases:
      • the constants;
      • the length of every lead byte, and the edges of each range;
      • continuation bytes, whitespace and newlines over every byte value;
      • the cursor of a row: leading whitespace consumed with no row, a wrap, a newline, a hard wrap, the end of the text;
      • a NUL inside the slice;
      • invalid bytes after column 32 and before a newline;
      • the wrap at the last whitespace and at a tab;
      • the 128-byte row and the CAPTION_PAGE_BYTES page;
      • the page cursor after 15 rows;
      • whitespace between a full page and more text;
      • runs of invalid bytes and spaces far longer than a page.
    • 3 proptests:
      • on weighted random text the queue always ends and every page keeps the bounds;
      • a page that did not read the end of a prefix equals the page of the whole text;
      • fuzz-style: raw bytes of every value, up to 20000 bytes, panic nowhere, including the helpers and text_row() on suffixes.
    • The tests are a module of the existing core category binary, obs-tests-core. No new test binary: ci/test_binary_cap.py passes.
  • GREEN: 9585ad379 libobs: Port the caption text wrap to obs-output. output::caption_text passes 57/57 (49 Tier 1, 8 Tier 3). The whole obs-tests-core binary passes 397/397.
    • Tier 3 (tests/output/caption_text_parity.rs), comparing against the C oracle:
      • the four CAPTION_* bounds;
      • the helpers over every byte value and every first two bytes (with the third and fourth from the values the C tests against);
      • caption_text_row() against text_row() from the start of each row: the return value, every byte of the row buffer (both start filled with the same pattern, so a stray write shows) and the cursor;
      • the page against the shim, page after page, as obs_output_output_caption_text2() calls it: the return value, the whole page buffer, the cursor after each call, and the queued pages. Each input runs with the shim's own window and with windows of 1 to 7 bytes.
    • Tier 3 inputs:
      • the characterization inputs and the inputs of caption_any_input_ends;
      • runs of every lead byte with every continuation length;
      • skips of invalid bytes and spaces far past the window;
      • proptests over weighted text (up to 800 words; whitespace, newlines, leads, continuations, valid characters of every length, NBSP, long words, rare NULs), raw bytes of every value (up to 4000), and long text (up to 12000 words).
    • Every comparison also checks the bounds directly: row ≤ CAPTION_LINE_BYTES, page ≤ CAPTION_PAGE_BYTES, and a cursor that moves forward and stays inside the text.
    • Deliberate-break check: in the Rust core I made the row wrap at 33 columns instead of 32 (both chars == LINE_CHARS comparisons), then restored it.
      • Tier 1 and 3: 20 of 57 tests failed. 11 of them are ported C cases (caption_word_wrap, caption_page_edge_481, caption_utf8_2byte_at_wrap, caption_utf8_3byte_at_edge, caption_utf8_4byte_at_edge, caption_invalid_bytes_take_no_column, caption_word_of_33, caption_space_at_column_32, caption_row_and_page_byte_bound, caption_full_page_then_whitespace, caption_any_input_ends). 4 are edge cases or the bounds proptest, and 5 are Tier 3 parity tests. Every failure was a test assertion; nothing panicked in the core, even with rows over 128 bytes.
      • With libobs-with-rust rebuilt: test_output_caption_text failed the same 11 cases in mode ON (21/32 passed) and passed 32/32 in mode OFF.
      • After the restore, all of these pass again (57/57, and 32/32 in mode ON).
    • ASan: output::caption_text (Tier 1 and 3, every proptest) passes 57/57 under AddressSanitizer, with no reports. The Rust side was built with -Zsanitizer=address, and the C oracle with -fsanitize=address. The page and row buffers and the input string are heap buffers of exactly their C sizes, so a write past the page or a read past the NUL would show.
  • Local, two build trees (ENABLE_RUST_LIBOBS=OFF and ON). Each tree runs the full cmocka suite against libobs (OFF) and against libobs-with-rust (ON):
Lane libobs libobs-with-rust Exported symbols
C (ENABLE_RUST_LIBOBS=OFF) 348/350, then 2/2 on 3 reruns (see below) 350/350 IDENTICAL (1766, same as master)
Rust (ENABLE_RUST_LIBOBS=ON) 350/350 350/350 IDENTICAL (1766)
  • The two failures in the C lane against libobs were test_audio_controls (in meter_callback_order_test) and test_deinterlace_2x_field_order. Neither uses captions. The run overlapped the Rust lane's build, and both passed three of three reruns once nothing else was running. No test was skipped or not run.
  • test_output_caption_text passes 32/32 in all four runs, and again three times each in a focused run. ldd shows each run loading the intended libobs.so.
  • libobs.ported-output-caption-text+.h.c is linked into libobs and not into libobs-with-rust: the C statics are in libobs only. The export lists are also identical across the two lanes.
  • The suites ran with an extracted cmocka 2.0.2 (the build host has no system cmocka package): CMake found it through CMocka_DIR, and ctest ran with LD_LIBRARY_PATH pointing at its lib.
  • Environment: headless (private Xvfb, private D-Bus, fresh HOME).
  • ci/local_gate.py:
    • --lane lint: passed.
    • --lane cross: passed.
    • --lane cross-release: passed.
    • --lane rust-test: passed (obs-tests-core 397/397, the obs-util harness 740/740, including export_list).
  • ci/test_build_groups.py, ci/test_naming.py, ci/test_ported_sources.py, ci/test_rust_scope.py, ci/test_binary_cap.py, ci/test_cmocka_includes.py: OK.
  • Real app: not applicable (no behaviour change).

Checklist

  • Base is master (or the description starts with a Stacked: note)
  • I claimed the issue, or no one else has an open PR for it
  • Characterization test (if the unit had none), then RED, then GREEN commits (the characterization of test_output_caption_text landed in libobs: Move the caption text wrap into obs-output-caption-text.c #1618)
  • User-visible change: its line in docs/features.md is added or updated (not applicable: no user-visible change)
  • (Optional) Local gate passed and the pushed head is stamped; otherwise PR CI runs the full suite
  • Warning or flag changes (-W..., ratchets): not applicable
  • Intentional differences from C listed in the parity test header and rust/README.md (ports): none

RED for the port of libobs/obs-output-caption-text.c.h to the
caption_text module of rust/obs-output. The safe core is a stub
(todo!()): the CAPTION_* bounds, the four UTF-8 and whitespace helpers,
text_row() and text_page(), each over a &[u8] that ends at its first NUL
or at the end of the slice. A Row and a Page carry their text, how far
the cursor moved, and whether they read the end of the slice.

tests/output/caption_text.rs ports test_output_caption_text 1:1: its 32
cases under the same names, in the same order, with the same
assertions. The C test drives obs_output_output_caption_text2(), which
stays C; the port repeats its loop over text_page() (one page per call
until the text runs out, and only a sole page may be empty) and ports
its helpers (assert_valid_utf8, assert_pages_preserve,
assert_single_page, assert_page_invariants). The "characterized, not
endorsed" cases are kept as such.

Edge cases: the constants; the length of every lead byte and the edges
of each range; continuation bytes; whitespace and newlines over every
byte value; the cursor of a row (leading whitespace consumed with no
row, a wrap, a newline, a hard wrap, the end of the text); a NUL inside
the slice; invalid bytes after column 32 and before a newline; the wrap
at the last whitespace and at a tab; the 128-byte row and the
CAPTION_PAGE_BYTES page; the cursor of a page after 15 rows; whitespace
between a full page and more text; and runs of invalid bytes and spaces
far longer than a page. Three proptests: the queue ends and every page
keeps the bounds on weighted random bytes; a page that did not read the
end of a prefix is the page of the whole text; and raw bytes of every
value, up to 20000, panic nowhere.

The tests are a module of the existing core category binary,
obs-tests-core; no new test binary.

soldr cargo test -p obs-data --test obs-tests-core output::caption_text::
fails 48 of 49 with "not yet implemented"; only the constants pass.

Refs obs-rust#1617
Ports libobs/obs-output-caption-text.c.h, the caption text wrap and
pagination, to the caption_text module of rust/obs-output.
obs_output_output_caption_text2() (still C) turns its text into pages,
one call per page until the text runs out; libobs-with-rust drops the C
file and links the Rust shim in its place.

Safe core (src/caption_text.rs): the four helpers
(caption_utf8_continuation, caption_utf8_char_length, caption_is_newline
and caption_utf8_whitespace), text_row() and text_page() over a &[u8]
that ends at its first NUL or at the end of the slice. Row and Page
carry the text (in fixed buffers of CAPTION_LINE_BYTES and
CAPTION_PAGE_BYTES), the bytes consumed, and whether they read the end
of the slice. Byte for byte the C, including the five behaviours
test_output_caption_text marks "characterized, not endorsed" (an
invalid byte dropped so words join, no empty rows for blank lines,
trailing whitespace kept, a row ending in a space with whitespace at
columns 32 and 33, C0 and DEL as spaces), the 128-byte row bound and the
cursor that always moves forward on a non-empty page. The text is
untrusted (obs-websocket SendStreamCaption), so the module denies
clippy's indexing_slicing, arithmetic_side_effects, unwrap_used,
expect_used, panic, todo, unimplemented and unreachable: every read is a
checked get, every write a bounded copy, every addition saturating.

Shim (src/ffi/output_caption_text.rs): obs_output_caption_text_page,
with the C signature, writes the page and its NUL (bytes 0 to the
length, the bytes the C writes) and moves the cursor. The core takes a
slice, and measuring the whole rest of the string on every call would
read the text once per page where the C reads only what a page needs.
So the shim gives the core a window, up to the NUL or 2048 bytes, and
doubles it while the page reads the end of a window that is not the end
of the string. A call reads about what the C reads and never past the
NUL. A null text, *text or page returns 0 (the C would crash; libobs
never passes one).

C side: caption_text_page() becomes obs_output_caption_text_page(), with
external linkage, declared in obs-internal.h without EXPORT, and
obs-output.c.h calls it by that name. The file leaves libobs.core-output
for its own group, libobs.ported-output-caption-text (LIBOBS_PORTED_
SOURCES), whose PCH is obs-internal.h and string.h. The symbol is in
rust-hidden.txt, rust-exports.map and rust-unexports-macos.txt; it is
hidden in both libobs builds and there is no rust-exports.d fragment.
The exported symbol sets are unchanged.

Oracle (oracle/output_caption_text.c): the original file with the page
function renamed, plus wrappers around its static functions and the
CAPTION_* values.

Tier 3 (tests/output/caption_text_parity.rs): the bounds; the helpers
over every byte value and every first two bytes; the row against
caption_text_row() (return value, every byte of the row buffer, cursor)
from the start of each row; and the page against the oracle page after
page as obs_output_output_caption_text2() calls it (return value, the
whole page buffer, the cursor, the queued pages), with the shim's own
window and with windows of 1 to 7 bytes that make it retry. Inputs:
the characterization inputs, runs of every lead byte, skips far past
the window, and proptests over weighted text up to 800 words, raw bytes
of every value up to 4000, and long text up to 12000 words. Intentional
differences: none.

Docs: rust/README.md rows and the naming.md row.

Fixes obs-rust#1617
@rdoupe
rdoupe requested a review from zackees as a code owner October 21, 2026 13:49
@zackees
zackees added this pull request to the merge queue Oct 21, 2026
Merged via the queue into obs-rust:master with commit a114b2f Oct 21, 2026
27 checks passed
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.

rust: Port the caption text wrap in obs-output.c to obs-output

2 participants