Skip to content

obs-outputs: Run the new socket loop over RTMPS (#2220) - #2778

Merged
zackees merged 2 commits into
masterfrom
fix/2220-rtmps-socket-loop
Oct 28, 2026
Merged

zackees merged 2 commits into
masterfrom
fix/2220-rtmps-socket-loop

Conversation

@zackees

@zackees zackees commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Makes "Enable network optimizations" (the new socket loop) and low-latency mode work over RTMPS on Windows instead of being turned off, and stops the loop from spinning or losing bytes when the TLS layer would block. Refs #2220.

RED

addd46be4 adds plugins/obs-outputs/rtmp-send-result.h, the platform-neutral
half of the loop's decision (rtmp_send_result_of(), rtmp_send_action_of(),
rtmp_send_retry_length()), and pins it from Linux Tier 2 in the crate's
existing test_obs_outputs_rtmp_host binary (no new test binary). The helper
in that commit reproduces today's rule — plain socket only, no retry-length
memory — so three cases fail:

[  ERROR   ] --- 3 != 1     test_send_result_tls_would_block   (WANT_WRITE -> WAIT_WRITE)
[  ERROR   ] --- 3 != 2     test_send_result_tls_wants_read    (WANT_READ -> WAIT_READ)
[  ERROR   ] --- 4000 != 1000  test_send_retry_repeats_first_length
[==========] tests: 11 test(s) run.
[  PASSED  ] 8 test(s).

Local command: OBS_CTEST_JOBS=16 uv run ci/local_gate.py --lane tier2-linux
(test/cmocka/test_obs_outputs_rtmp_host.c:112, :123, :161).

Why these are the bug: RTMPSockBuf_Send() is mbedtls_ssl_write() when
sb_ssl is set (plugins/obs-outputs/librtmp/rtmp.h:104), and the old
write_data() only understood ret == -1 + WSAEWOULDBLOCK and ret == 0.
MBEDTLS_ERR_SSL_WANT_WRITE (-0x6880) therefore fell through both branches
with can_write still true — the loop spins — and WSAGetLastError() was
queried for a code that is not a Winsock error. The send length was also
recomputed from write_buf_len on every pass, which mbedtls_ssl_write()
forbids after WANT_WRITE (it keeps the unflushed record in out_left and
returns the new call's length once it flushes), so a retry with a grown
length made the memmove drop bytes the record never carried.

GREEN

  • rtmp_send_result_of() classifies > 0 as success, WANT_WRITE as
    wait-for-FD_WRITE, WANT_READ as wait-for-FD_READ, and everything else
    (a closed connection, a plain -1 that is not WSAEWOULDBLOCK, any other
    Mbed TLS error) as fatal. rtmp_send_action_of() says what the loop does
    with each: clear can_write and wait, or shut the stream down.
  • write_data() sends through the helper, remembers the in-flight length in
    struct rtmp_stream::send_retry and repeats it on a WANT_WRITE retry, and
    logs a TLS failure with mbedtls_strerror() instead of GetLastError().
  • The FD_READ discard reads through TLS_read() when sb_ssl is set, so a
    handshake or renegotiation can make progress; it used a raw recv(), which
    throws the TLS records away.
  • The rtmps:// check in rtmp-stream.c.h is gone, so new_socket_loop and
    low_latency_mode stay on for an RTMPS URL.
  • A fatal TLS send logs rtmp_send_tls_error_text() (declared in the helper,
    which is why it includes mbedtls/error.h: mbedtls/ssl.h alone does not
    declare mbedtls_strerror()), so the Windows loop does not have to reach
    for a header its unity PCH does not carry.

GREEN: test_obs_outputs_rtmp_host now runs 12 cases and all pass
(OBS_CTEST_JOBS=16 OBS_TIER2_MODES=ON uv run ci/local_gate.py --lane tier2-linux, [ PASSED ] 12 test(s); the case that pins the error text
also proves the Mbed TLS link the Windows lane uses). ./lint clean.

What this does not do

Refs #2220, not Fixes: acceptance item 3 is a real RTMPS run on Windows
with both settings on, compared with plain RTMP at the same settings, with
the log in the PR. No PR lane builds the Windows app (ci/ci_scope.py, "full:
never from a PR path"), and this checkout has no Windows host, so the Windows
Tier 2 lane can only show that the code compiles and the Linux-side rules
hold. The Windows app lane on release-rust.yaml (-f verify_only=true) can
build it once someone dispatches it.

@zackees
zackees force-pushed the fix/2220-rtmps-socket-loop branch from 8822cfd to be5421d Compare October 28, 2026 11:23
@zackees

zackees commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

Status from the worker that opened this: RED and GREEN are both in, the Linux
side is verified, and the PR stays a draft for the one thing this checkout
cannot produce.

Verified here

$ LD_LIBRARY_PATH=/build/tree/libobs ./test_obs_outputs_rtmp_host
[==========] tests: Running 12 test(s).
...
[  PASSED  ] 12 test(s).

./lint clean (== lane lint: ok (40.5s)), and
uv run ci/local_gate.py --lane tier2-linux (mode ON, OBS_CTEST_JOBS=16)
builds and runs the whole cmocka suite from this tree with no failure.

Still needed, and it needs a maintainer decision or hardware

Acceptance item 3 in #2220: one real RTMPS stream on Windows with
"Enable network optimizations" and low-latency mode both on, compared with
plain RTMP at the same settings (dropped frames, CPU), with the log in the
PR. That cannot come from PR CI — no PR lane builds the Windows app
(ci/ci_scope.py, "full: never from a PR path") — and this checkout has no
Windows host. The nearest machine-run path is
release-rust.yaml -f verify_only=true on this branch, which builds the
Windows app without streaming.

So this says Refs #2220, not Fixes. Two things I would ask a reviewer
to weigh before it leaves draft:

  1. The FD_READ change reads through TLS_read() when sb_ssl is set. The
    loop discards what it reads, so a handshake or renegotiation can now make
    progress; the risk is that a server message consumed here is one the
    librtmp path would otherwise have buffered. The plain-socket path is
    unchanged.
  2. struct rtmp_stream grew one size_t (send_retry.len). Nothing sizes
    that struct across an ABI boundary (it is the plugin's own), but it is the
    only layout change in the PR.

@zackees

zackees commented Oct 28, 2026

Copy link
Copy Markdown
Member Author

Windows evidence is in. Verify-only run 40684548610 (release-rust.yaml -f verify_only=true, ref fix/2220-rtmps-socket-loop) built the Windows app for both architectures:

  • Build windows x64 🧱 — success
  • Build windows arm64 🧱 — success
  • Release gate 🚦 / Windows 🪟 (x64) and (arm64) — success
  • Linux x86_64, AppImage, Flatpak, Ubuntu and the Rust Tier 2 Linux build — success

So plugins/obs-outputs/rtmp-send-result.h and the write_data()/FD_READ changes compile and link in the real Windows app, which is what the draft was waiting on for acceptance item 3's compile half (the "no PR lane builds the Windows app" note in the body is now out of date).

The macOS jobs in the same run failed, but not from this branch: both died on find-font.c.h:58 / :218 with -Wsign-conversion, i.e. the text-freetype2 ratchet #2755 merged into master at 09:30Z. PR #2789 casts those two arguments; once it lands I will re-run verify_only on this branch so the run is green end to end.

Still missing for Fixes: a real RTMPS session on Windows with "Enable network optimizations" and low-latency mode on, compared against plain RTMP, with the log in the PR. This host cannot produce it, so the PR stays in draft meanwhile.

The new socket loop (`write_data()` in `rtmp-windows.c.h`) treats anything
but `-1`/WSAEWOULDBLOCK and `0` as "no error". That holds for a plain
socket; an RTMPS stream sends through `mbedtls_ssl_write()`, which answers a
full transport with `MBEDTLS_ERR_SSL_WANT_WRITE` and a pending handshake with
`MBEDTLS_ERR_SSL_WANT_READ`, so the loop spins with `can_write` still set
and a real TLS error is not fatal either.

This pulls the decision out of the `#ifdef _WIN32` loop into
`plugins/obs-outputs/rtmp-send-result.h` and pins it from Linux Tier 2, in
the crate's existing `test_obs_outputs_rtmp_host` binary: the two Mbed TLS
error codes, the classification, the loop action, and the retry length
`mbedtls_ssl_write()` requires after WANT_WRITE.

RED: the helper reproduces today's rule (plain socket only, no retry length
memory), so the new cases fail:

  test_send_result_tls_would_block: -0x6880 classifies as FAILED, not WAIT_WRITE
  test_send_result_tls_wants_read:  -0x6900 classifies as FAILED, not WAIT_READ
  test_send_retry_repeats_first_length: 4000, not 1000 after a WANT_WRITE

Refs #2220.
`RTMPSockBuf_Send()` is `mbedtls_ssl_write()` when `sb_ssl` is set, but
`write_data()` only understood a plain socket's `-1`/WSAEWOULDBLOCK and
`0`. An RTMPS stream therefore spun on `MBEDTLS_ERR_SSL_WANT_WRITE` with
`can_write` still true, queried `WSAGetLastError()` for a code that is not a
Winsock error, and could drop queued bytes when it retried a write the TLS
layer had not accepted with a length the buffer had grown past.

- `write_data()` classifies through `rtmp_send_result_of()`: WANT_WRITE and
  WANT_READ clear `can_write` and return RET_BREAK (the loop waits for
  FD_WRITE/FD_READ), any other negative is fatal, and a fatal TLS send logs
  `mbedtls_strerror()` instead of `GetLastError()`.
- The in-flight length lives in `struct rtmp_stream::send_retry` and is
  repeated on a WANT_WRITE retry, which is what `mbedtls_ssl_write()`
  requires (it reports the new call's length once it flushes `out_left`).
- The `FD_READ` discard reads through `TLS_read()` when a session is set, so
  a handshake or renegotiation can complete; a raw `recv()` threw those
  records away.
- The `rtmps://` check in `rtmp-stream.c.h` is deleted, so network
  optimizations and low-latency mode stay on for RTMPS.

GREEN: `OBS_CTEST_JOBS=16 uv run ci/local_gate.py --lane tier2-linux` runs
11 cases in test_obs_outputs_rtmp_host, all passing; `./lint --fix` clean.
docs/features.md has the ledger line.

Refs #2220: the Windows RTMPS run with both settings on is still needed.
@zackees
zackees force-pushed the fix/2220-rtmps-socket-loop branch from 331e6f6 to d3a9713 Compare October 28, 2026 14:33
@zackees
zackees marked this pull request as ready for review October 28, 2026 15:00
@zackees
zackees added this pull request to the merge queue Oct 28, 2026
Merged via the queue into master with commit 4ed3172 Oct 28, 2026
38 checks passed
@zackees
zackees deleted the fix/2220-rtmps-socket-loop branch October 28, 2026 15:35
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.

1 participant