Skip to content

Give VCP frames the bits for their content interval, not the wait before them (851-4499) - #1508

Merged
pondorasti merged 2 commits into
mainfrom
alexandru/851-4499-switch-stall
Nov 27, 2026
Merged

pondorasti merged 2 commits into
mainfrom
alexandru/851-4499-switch-stall

Conversation

@pondorasti

@pondorasti pondorasti commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Fixes 851-4499 (host drops most new frames after the rate switch to VCP). Found while measuring 851-4477.

Cause

VCP gave each frame the bits for the wall-clock time since the previous one. The transport only admits a frame while about one interval of the estimate is unacknowledged. On a link with a real round trip the two fed each other:

  1. One large frame (the switch's 1.2 MB keyframe, or a loss burst) holds admission shut.
  2. The next frame comes 100–500 ms later and gets that much time's bits: 190 KB–1.1 MB against about 55 KB.
  3. That shuts admission again, and the host settles at 2–20 frames a second.

Host counters every half second in the stall: the encoder still writes its set bitrate (1.7–2.2 MB per half second), but only 2–10 frames go out and about 28 captures are replaced before encode. Lifting VCP's QP cap changed nothing. VideoToolbox never had the problem because its sessions already stamp frames at most two intervals apart (851-4063).

Change

  • Host: each changing tile of a capture gets the time since that tile last changed, on the capture's own timestamps (contentIntervalsNs). A capture that changed nothing keeps the intervals of the one it replaces. A capture that's already been encoded carries none, so its refinement passes, refreshes and retries carry none.
  • VCP: uses VideoToolbox's presentation clock (ScreenSharingEncoderPresentationClock). Each gap is limited to the tile's content interval, kept between one and two frame intervals and never more than the real gap. With no interval, and for every keyframe or reference refresh, the limit is VideoToolbox's two intervals.
  • Refinement passes keep the wall-clock gap, as before: a still's QP-6 pass of a 4K text tile is a 0.5–0.7 MB burst, and refinements re-encode a capture that no longer changes. A refresh on a refinement plan gets the two-interval limit. A submission the session never takes (its QP cap or encode call fails) rolls the tile's clock back. A change whose timestamp didn't move forward gets one interval.
  • With Tighten the small-change metrics and tile comparer after review (851-4476) #1504: a tile the request names as repeating an encoded capture (repeatedTiles) carries no interval, and the rate switch's copy keeps repeatedTiles too.
  • Rate switch: copies the request when it forces keyframes, instead of rebuilding it field by field, so nothing the host sends is lost on the frames right after the switch.

A flat one-interval limit fixed the stall too, but it would have halved the bits of 30 fps video (captures that change every other frame) on every link. A flat two-interval limit only half fixed it.

Tests (each fails without the change)

  • ScreenSharingV2HostContentIntervalTests (new): replaced captures carry the content's interval, not the wait. Unchanged captures don't restart it. Each tile has its own interval. Only reported touches restamp a waiting tile. A replaced interval survives a capture that changed nothing. Refinements and refreshes carry none.
  • ScreenSharingEncoderPresentationClockTests: the content-interval limit and the 1/240 s floor.
  • ScreenSharingVCPEncoderStateTests: a frame that waited is stamped one content interval later. A refresh gets two intervals, on a refinement plan too. A refinement pass keeps its whole wall-clock wait. An abandoned submission leaves the clock where it was.
  • ScreenSharingRateSwitchedTileEncoderTests: the switch's forced keyframe keeps every other request field.
  • ScreenSharingVCPEngineTests (hardware, real VCP session): submissions 250 ms apart on an injected clock, with content changing every frame, come out at about 34 KB, two intervals' worth, against 80 KB on wall-clock stamping. The limit is three intervals. The test takes no real time.

Swift tests run locally (M1 Pro, with timeouts): ScreenSharingV2Tests (203 tests after the rebase onto #1504, including the serialized hardware encoder suites on VCP and VideoToolbox) and CodevisorBenchTests (58), all passing.

Measured

  • Local, diagnostic only (M1 Pro, loopback, 50 Mbit/s network, 1080p video, switch forced by a scratch 30 Mbit/s bar, 3 repetitions each): before, 22.6–38.1 updates/s with 586–934 frames dropped before encode. With this change, 55.4–59.3 updates/s with 53–189 dropped.
  • Lab: the benchmark gate on this PR, plus one same-slot pair on the 50 Mbit/s network (shipped encoder; video, scroll, typing and test cards; six repetitions; main against this branch). Results follow in a comment, with how often the switch fired.

Review findings left

  • Presentation times can now fall behind the wall clock, and VCP's 50 ms data-rate window is measured on them. The local runs showed no extra drops from it; the lab pair will show whether it matters there.
  • A retry of a change that failed to encode gets the two-interval limit, not its own interval.
  • A refinement pass that waited behind closed admission gets the whole wait's bits, as it did before this PR. The loopback test-card cells don't measure time-to-sharp, and the switch fired in only 1 of their 12 runs in the lab pair, so this PR keeps the old refinement behaviour rather than trying to prove a cap.
  • A capture without dirty rects doesn't restart the interval of a tile that's already waiting, so that tile's next interval can come out long. It is still capped at two intervals.

🤖 Generated with Claude Code

@pondorasti

Copy link
Copy Markdown
Member Author

Proof.

  • Benchmark gate: passed (run 45954352308, 20 min).
  • Lab pair, same slot: tamale, 50 Mbit/s network (25 ms ± 5, 0.1% loss), shipped encoder, six repetitions per scene. Main at this PR's base is run 45957168150; this branch is run 45959327311.
Scene Main This PR
Video, updates/s 32.2 6.8 28.6 27.3 57.3 60.0 (median 30.4) 57.1 60.0 60.0 60.0 60.0 60.0 (median 60.0)
Video, runs where the switch fired 5 of 6 5 of 6
Video, frames dropped before encode 565 1357 762 947 62 68 75 63 58 8 65 113
Video, freezes 17 9 8 5 2 0 3 0 0 0 0 0
Video, latency p50 / p95 (median of runs) 57.6 / 136.1 ms 55.9 / 64.7 ms
Scroll, updates/s (median) 56.6 56.4
Scroll, freezes 0 3 5 3 0 3 0 2 2 0 2 0
Scroll, latency p50 / p95 37.2 / 44.7 ms 37.2 / 44.9 ms
Typing, updates/s / latency p50 15.0 / 34.3 ms 15.0 / 33.8 ms
Test cards, latency p50 / p95 110.2 / 260.7 ms 103.6 / 217.5 ms

The switch fired in 5 of the 6 video runs on both sides. Scroll and typing never reached it, and test cards did once on this branch. Every cell is valid and nothing is unreadable on either side.

Not covered: Tier 2 text fidelity (T8) and the two-Mac tier, which this pair doesn't run.

pondorasti and others added 2 commits November 27, 2026 01:07
…efore them (851-4499)

VCP sized each frame by the wall-clock time since the previous one, and the
transport admits a frame only while about one interval is unacknowledged, so
after the rate switch to VCP one large frame on a link with a real round trip
held admission shut and every later frame grew with the wait. The host now
stamps each changing tile of a capture with the time since that tile last
changed, and VCP's presentation clock (the one VideoToolbox already had)
limits each gap to it, between one and two frame intervals. The rate switch
copies the request instead of rebuilding it, and re-encodes of an encoded
capture carry no interval.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pondorasti
pondorasti force-pushed the alexandru/851-4499-switch-stall branch from 02ff245 to 3b436ea Compare November 27, 2026 09:12
@pondorasti
pondorasti merged commit 15a46a3 into main Nov 27, 2026
11 checks passed
@pondorasti
pondorasti deleted the alexandru/851-4499-switch-stall branch November 27, 2026 09:49
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