Skip to content

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

Closed

Description

@rdoupe

Unit

Port the caption text wrap and pagination of libobs/obs-output.c.h to the existing crate rust/obs-output as a new module, caption_text. These are the six static functions that turn the text given to obs_output_output_caption_text2() into caption pages:

  • the UTF-8 helpers: caption_utf8_continuation, caption_utf8_char_length (a validating length that rejects overlong forms, surrogates and values above U+10FFFF, and returns 0 so the caller drops one invalid byte), caption_is_newline and caption_utf8_whitespace (ASCII <= 0x20, DEL and U+00A0);
  • caption_text_row: skips leading whitespace and invalid bytes, wraps at the last whitespace at or before column 32, hard-wraps a longer word, ends a row at CR or LF, and writes interior tab, VT and FF (and the other 1-byte whitespace) as a space;
  • caption_text_page: up to 15 rows joined by \n.

About 130 lines. All of it is our code (the wrap that replaced the 128-byte truncation, and its follow-up). It is a pure function: the input is a const char ** cursor into a NUL-terminated string, and the output is a caller-owned buffer of CAPTION_PAGE_BYTES + 1. The bounds are CAPTION_LINE_CHARS, CAPTION_PAGE_ROWS, CAPTION_LINE_BYTES and CAPTION_PAGE_BYTES (obs-internal.h).

These stay C: caption_text_new() (the bzalloc of the flexible-array struct caption_text), obs_output_output_caption_text1/2() (the track lock and the page queue, including the rule that only the first page may be empty), and the consumer add_caption() (libcaption, the SEI/OBU build).

Phase and position

Phase 6 (#444), libobs core through opaque seams. It joins rust/obs-output, which already holds the stream delay and the interleaver. obs-output.c is one of our modified files, and this unit is the only remaining pure block of ours in it with a real-libobs test, so it is the shortest path. It is part of a large file, so it enters through function extraction (testing-policy.md, "Extracting a function before porting it"), as the delay and the interleaver did. Its Rust tests join the existing core category binary, obs-tests-core (a module of tests/output/); no new test binary.

Dependencies and existing tests

  • No struct obs_output fields, no accessors and no obs-util prerequisite: the code uses only memcpy and pointer arithmetic.
  • test_output_caption_text links OBS::libobs and drives the public obs_output_output_caption_text2(), then walks the caption queue through obs-internal.h. Its 12 cases cover rows of 127, 128 and 129 bytes, a 2-byte character at byte 128, multiple pages, word wrap, pages of 480 and 481 columns, 3- and 4-byte characters at column 32, whitespace only and a tab. Because it goes through real libobs, a Rust swap is visible to it, so in mode ON it exercises the port directly. PR 1 adds the characterization cases to it.
  • test_output_caption_queue covers the queue's lifetime, not the text.
  • The input is untrusted: caption text reaches this code from obs-websocket SendStreamCaption and from caption plugins. The C keeps three invariants, and the port must keep them: a row is at most 32 valid characters, so at most 128 bytes; a page fits CAPTION_PAGE_BYTES; and the cursor always moves forward on a non-empty page, so the do ... while (*cursor) loop of obs_output_output_caption_text2() ends. The pages go on to libcaption's caption_frame_from_text(), so the port must be byte-identical: a different wrap point is visible to viewers.

Plan

  1. Extraction and characterization PR (behaviour-neutral): the six functions move verbatim from obs-output.c.h into libobs/obs-output-caption-text.c.h, still static, in the same unity group (libobs.core-output, just before obs-output.c.h, alphabetical). They call nothing in obs-output.c.h, so the new file needs no forward declarations. New cases in test_output_caption_text (the existing 12 are unchanged): each invalid UTF-8 form dropped one byte at a time, and the valid edges of each range kept; the empty string, only spaces and only invalid bytes (one empty page); CR, LF, CRLF and blank lines; NBSP and the control bytes; words of 32 and 33 characters; whitespace at columns 32 and 33; a 2-byte character at the wrap; the 128-byte row and the CAPTION_PAGE_BYTES page; more than 15 rows; whitespace after a full page; and a check over every byte value and pseudo-random input that the call ends and every page keeps the bounds above.
  2. Port PR (RED, then GREEN):
    • obs_output::caption_text: a safe core over &[u8] that returns the bytes consumed and the page, and src/ffi/caption_text.rs with a hidden size_t obs_output_caption_text_page(const char **text, char *page). It is declared in obs-internal.h without EXPORT, and obs-output.c.h calls it in place of caption_text_page(). The C file gets that entry point too, and moves to its own ported group, libobs.ported-output-caption-text, with its own .pch.h and export fragment, as libobs.ported-output-delay has.
    • Tests: Tier 1 ports every test_output_caption_text expectation 1:1, plus the invariants. Tier 3 parity of the C file (an oracle wrapper) against the Rust core, with a proptest of arbitrary bytes (weighted toward whitespace, newlines, multibyte leads, continuation bytes and NUL) that compares the page bytes and the cursor advance, page after page, until the input runs out, and asserts the row, page and progress bounds directly. Tier 2 OFF and ON unchanged. Tier 4 does not apply (no protocol change).
    • Docs: the rust/README.md rows and the docs/rust-port/naming.md row (obs-output-delay.c.h, obs-output-interleave.c.h, obs-output-caption-text.c.h → rust/obs-output).

No intended behaviour changes. The odd behaviour that test_output_caption_text marks "characterized, not endorsed" is kept:

  • an invalid byte is dropped, not replaced with U+FFFD, so the words on either side of it join;
  • a newline is skipped as leading whitespace, so blank lines, and leading and trailing newlines, give no empty rows;
  • whitespace before a newline or the end of the text stays at the end of the row; only a wrap drops the whitespace it splits at;
  • with whitespace as both characters 32 and 33, the wrap splits at the later one, so the row ends with a space;
  • every C0 control byte and DEL counts as whitespace, so inside a row each one becomes a space.

Activity

  1. rdoupe commented on Oct 21, 2026

    @rdoupe
    CollaboratorAuthor

    Claiming: #1617

    1. Move PR: the caption text wrap moved verbatim into libobs/obs-output-caption-text.c.h (same libobs.core-output group), with test_output_caption_text extended from 12 to 32 cases. Opening it now.
    2. The port, as rust/obs-output's caption_text module, as its own PR after 1 merges.
  2. added
    claimed
    on Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    claimed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions