Skip to content

fix(windows,webrtc): RemoteIdd capture crash and pacing, stream fitted to the TV's link - #12

Merged
mlopezsegura merged 8 commits into
masterfrom
perf/webrtc-tv-link
Nov 30, 2026
Merged

mlopezsegura merged 8 commits into
masterfrom
perf/webrtc-tv-link

Conversation

@mlopezsegura

@mlopezsegura mlopezsegura commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Seat capture fixes for OpenStreamMS and a WebRTC stream that fits the TV's network link.

Divide-by-zero on the first capture of a seat (refs OpenstreamCo/OpenStreamMultiSeat#106)

  • capture() lowered the client rate to a fractional display rate with client_frame_rate % display_refresh_rate_rounded. Only Desktop Duplication set those fields, and display_ridd_vram_t has its own constructor, so with RemoteIdd they held heap garbage: a denominator above 1 with a rounded rate of 0 crashed Sunshine with STATUS_INTEGER_DIVIDE_BY_ZERO (the shipped 2.2.0/2.3.0 build, at RVA 0x1d422f).
  • The refresh-rate and client-rate fields start at zero, and the adjustment (now adjust_capture_rate()) skips a zero rate.

RemoteIdd capture

  • The driver's shared texture is held only while it is copied, not while waiting for a free image, so late encodes no longer make the driver drop frames.
  • qpc_time_difference multiplied the tick difference by the counter frequency instead of dividing by it, so every difference came out ten thousand times too small and frame, RTP and host-latency timestamps were the capture time rather than the present.
  • RemoteIdd reads its display's refresh rate, so a seat faster than the client is paced by frame arrival, capped at the client rate (steady 1-1-2 at 90 fps on 120 Hz, every other frame at 60 fps).

WebRTC: stream fitted to the TV's link

  • One keyframe on its way at a time; PLIs while one is encoded or sent, and for a grace period after it, count as answered. The grace starts at 250 ms and doubles up to 2 s while the TV keeps asking.
  • Retransmission history of 4096 packets (a whole 4K keyframe). Retransmissions go through the pacer, at most 1 MiB queued, instead of bursting from the network thread.
  • TVs may report their link (network.type, network.linkKbps): video is then paced at 90 % of it and the bitrate kept to 60 %. RTP payloads are 1380 bytes on Ethernet or Wi-Fi.

Tests: RemoteIdd*, Webrtc*, ConstructorContracts*, WindowsQpcTest*, FrameRateLimiterTests*, D3dkmtAbiTests* and Framerate*: 138 pass, 4 new for the capture rate. The WebRTC changes were tried on a Samsung TV over 4K at 80 Mbps; the first round was worse; the second (retransmission pacing and keyframe back-off) is in the OpenStreamMS preview and not yet confirmed on the TV.

🤖 Generated with Claude Code

mlopezsegura and others added 8 commits November 26, 2026 22:53
The RemoteIdd snapshot took the driver's shared texture first and then
waited, with it held, for a free image and for the encoder to release that
image's capture texture. When the encoder ran late, every frame the driver
presented in that window found the texture busy and was dropped, which showed
up as an occasional skip of one or two frames, more often while the image
pool was still being filled at the start of a stream.

Get the destination image ready and locked before taking the driver's
texture, so it is held only for the copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
qpc_time_difference multiplied the tick difference by the counter frequency
instead of dividing by it, so with the usual 10 MHz counter every difference
came out ten thousand times too small. Frame timestamps were therefore the
moment of capture rather than the present, and so were the RTP timestamps
and the host latency reported to clients. Convert in floating point: ticks
times 1e9 overflows int64 after about 15 minutes at 10 MHz.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the client

Only Desktop Duplication set display_refresh_rate, and display_base_t left it
uninitialized, so with RemoteIdd capture() never knew the display rate and
sampled a client-rate grid. A seat at 120 Hz streamed at 60 fps then lined
the grid up with the display ticks: whether a slot took the odd or the even
frame depended on timer and delivery jitter, so the stream mixed one- and
three-frame steps (measured: 30-50% of frames) and the capture grabbed the
driver's texture just as the driver published the next frame.

RemoteIdd now reads the refresh rate of its display, so capture() sends
frames as they arrive, capped at the client rate. With timestamps on the
display ticks the limiter tolerates half a tick instead of a whole one: a
whole tick puts every other frame exactly on its slot boundary and the same
flip comes back. The rate fields now start at zero for backends that do not
set them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Keep the copy of SharedFrame.h in step with the driver, which now waits up
to half a millisecond for the consumer to release the texture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The limiter only took over at 1.5 times the client rate, so a seat at
120 Hz streamed at 90 fps stayed on the grid. There it sent runs of
consecutive frames, fell behind and skipped, instead of three frames out of
four in a steady 1-1-2. With timestamps on the display ticks the limiter
keeps that pattern at any rate clearly above the client's (1.1 times);
composed timestamps keep the old 1.5 threshold.

The capture tests now stream a 120 Hz seat at 60 and at 90 fps and check
the content steps of the frames sent. They tolerate 2% of stray steps for a
busy machine; the grid broke the pattern far more often.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
At 4K and 80 Mbps a Samsung TV on its 100 Mbps Ethernet port lost parts
of large frames, asked for a keyframe, and every PLI was answered with a
new 1-3 MB IDR: up to 26 a second, which overflowed the link and the PC's
UDP send buffer (25,000 packets dropped in a second) and left the picture
black for seconds.

- One keyframe on its way at a time: requests while one is being encoded
  or sent, and for 250 ms after it has gone out, are taken as having
  crossed it.
- Retransmission history of 4096 packets instead of 512, enough for a
  whole 4K keyframe, so a lost packet is resent instead of a new IDR.
- The TV may report its link (network.type, network.linkKbps). Video is
  then paced at 90 % of the link instead of 800 Mbps, the bitrate is kept
  to 60 % of it, and retransmissions are charged to the same budget.
  TVs that report nothing are paced as before.
- RTP payloads of 1380 bytes on Ethernet or Wi-Fi instead of 1220.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The first TV test, with a TV that did not report its link, was worse than
before: 109,000 packets dropped in six minutes and IDRs of up to 5 MB.
The larger NACK history let a single request resend megabytes at once
from the network thread, outside the pacer, overflowing the 1 MiB UDP
send buffer; and 250 ms between keyframes was not enough while the TV
kept losing them.

- Retransmissions are queued (once per packet, at most 1 MiB) and sent
  by the pacer at the paced rate, ahead of the frame being sent and,
  between frames, from the video sender thread.
- The wait after a keyframe doubles, up to 2 s, each time the TV asks
  again soon after one; five quiet seconds start over at 250 ms.
- Tests use tests_common.h.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
capture() lowered the client rate to a fractional display rate with
client_frame_rate % display_refresh_rate_rounded whenever the refresh-rate
denominator was above 1. Only Desktop Duplication set those fields, and
display_ridd_vram_t has its own constructor, so with RemoteIdd capture they
held whatever was on the heap. A denominator above 1 with a rounded rate of
0 crashed Sunshine with STATUS_INTEGER_DIVIDE_BY_ZERO on the first capture
after a seat started (OpenstreamCo/OpenStreamMultiSeat#106).

The fields already start at zero; the adjustment now also skips a rounded
rate or client rate of zero. It moves to adjust_capture_rate() so it can be
tested, and the client rate fields start at zero too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant