Skip to content

Capture the shared display at its full rate, and let the viewer keep up (851-4475) - #1506

Merged
pondorasti merged 6 commits into
mainfrom
alexandru/851-4475-screen-sharing-host-captures-a-frame
Nov 27, 2026
Merged

pondorasti merged 6 commits into
mainfrom
alexandru/851-4475-screen-sharing-host-captures-a-frame

Conversation

@pondorasti

@pondorasti pondorasti commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Fixes 851-4475

The Mac host now captures the shared display at its full rate, and the viewer keeps up with a stream that brings a frame every refresh. In the four-way lab comparison this put Codevisor level with Apple High Performance on scroll, window drag and typing, at 60 updates a second, for 1.5 to 3 points of a core on the host.

Changes

  1. Viewer: catch up when every refresh brings a frame (first commit).
    • With two drawables, a full-rate stream makes every frame wait for a drawable and get committed just after a refresh, so each frame shows a refresh late.
    • Catch-up, in the render coordinator every surface draws through: when the last five frames the mailbox received (every put, timed there; puts under half a refresh apart count once) came less than 1.25 refreshes apart and a commit waited over 2 ms for its drawable, the next frame is held and replaced by the one after it. One frame is skipped, at most once a second. A held frame that nothing replaces is drawn after two refreshes plus 4 ms.
    • Newest frame after a wait, in the macOS render worker: after waiting over 1 ms for a drawable, it draws the newest frame that arrived meanwhile. A newer frame in another dynamic range is drawn next instead of being dropped.
  2. Host: capture cap from the display's refresh rate (second commit, ScreenSharingCaptureCadence). The encoder stays at 60.
    • Up to 60 Hz: 1/480 s. ScreenCaptureKit delivers about 0.8 ms after the interval it is given, so 1/60 s took 57.3 frames a second. Any cap from 64 gives every refresh on scroll. Sparse changes like typing keep their full 13.7 ms lead before the display time only far above the refresh rate (7.0 ms at 120, 10.9 at 240, 13.7 at 480 and 960), because the interval counts from the last delivery, idle ones included. A static screen costs the same at every cap: the host process used 0.05% of a core.
    • Faster displays: every k-th refresh, k = ⌈0.99 × refresh / 60⌉ (a 1% tolerance, so 120.01 Hz counts as 120), never over 60 frames a second. The cap is 16/15 of that rate so the 0.8 ms lateness never makes it skip one. 120 Hz is captured at every other refresh (cap 64; measured 59.9 a second on a 120 Hz virtual display), 90 Hz at 45, 144 Hz at 48. A lifted cap there would capture every refresh: at cap 240, 119.7 a second and the host process from 13 to 30% of a core.
    • What a 120 Hz host gains: 60 updates a second instead of 57, at a steady cadence, at no extra cost. What it doesn't: typing's late captures (a keystroke can still wait one refresh), because the cap must stay near 64 there.
    • The refresh rate comes from the screen's maximumFramesPerSecond, or else the display mode. Unknown at a capture's start counts as 120 Hz; unreadable later keeps the current cadence. The host re-reads it when the screen parameters change, in its own task that ending the session cancels, and reconfigures only when the interval changes. The capture applies reconfigurations one at a time; each setting changes only in its own turn, a turn waits for a start in progress, and each start begins a new queue.
    • A configuration asking for under 60 keeps its own rate. Known limit: such a rate still runs about 0.8 ms per frame slow, as every cap did before this change.

Proof

  • Lab, before the real change (851-4475 comments): four-way Tier 2 run 45925697606 (Apple / today / cap lifted / cap lifted + viewer catch-up, shipped encoder, display offset 10.7 ms). Image age p50 / p95 ms:
Scene Apple Today Cap lifted Cap lifted + viewer
Scroll 27.3 / 27.3 27.3 / 43.9 43.9 / 43.9 27.3 / 27.3
Window drag 27.2 / 27.2 27.2 / 43.9 27.2–43.9 / 43.9 27.2 / 27.2
Typing 27.4 / 27.4 27.4 / 44.1 27.4 / 27.4 27.4 / 27.4
  • Confirmation on this PR's code: Tier 2 run 45942849834, 31 runs, none skipped, display offset 6.3 ms. "Before" is the same build with the capture capped at 60 and the viewer's two steps off (lab branch alexandru/851-4475-cadence-confirm, this PR's first two commits plus one lab commit; the review-fix commit came later and changes only race paths this run doesn't exercise). Image age p50 / p95 ms, median of 2 repetitions:
Scene Apple Shipped encoder, before → after Updates/s, before → after Fallback encoder, before → after
Scroll 39.7 / 39.7 39.7 / 39.7 → 23.0 / 23.0 57.4 → 59.9 56.3 / 56.3 → 56.3 / 56.3
Window drag 39.6 / 39.7 39.6 / 39.7 → 23.0 / 23.0–39.6 57.4 → 59.9 56.3 / 56.3 → 56.3 / 56.3
Typing 23.1 / 23.1 23.1 / 39.7 → 23.1 / 23.1 14.1 → 14.1 23.1 / 56.4 → 23.1 / 23.1
  • At this offset the shipped encoder is now a refresh ahead of Apple on scroll and window drag, and level on typing including the slow tail.
  • The fallback encoder is no worse: scroll and window drag are unchanged (it is encode-bound at 4K, 56 a second), and typing's slow tail went from 56.4 to 23.1.
  • Host CPU p50 (% of a core), shipped encoder: scroll 46.1 → 47.6, window drag 48.8 → 51.8, typing 16.5 → 16.4. Fallback: scroll 57.8 → 58.1, window drag 56.8 → 55.7, typing 13.8 → 13.0.
  • Confirmation on the final head 645ccc8: Tier 2 run 45967941953, 31 runs, none skipped, display offset 5.0 ms (lab branch alexandru/851-4475-cadence-confirm2). Image age p50 / p95 ms, before → after:
Scene Apple Shipped encoder Updates/s Fallback encoder
Scroll 38.3 / 38.3 38.3 / 38.3 → 21.7 / 30.0 57.4 → 59.7 55.0 / 55.0 → 55.0 / 55.0
Window drag 38.3 / 38.3 38.3 / 38.3 → 38.3 / 38.3 57.3 → 59.1 55.0 / 55.0 → 55.0 / 55.0
Typing 30.0 / 30.0 21.7 / 38.3 → 21.7 / 21.7 14.1 → 14.1 21.7 / 55.0 → 21.7 / 21.7
  • Window drag gained nothing at this offset. Its frames are ready 2.5 ms after the host's refresh (scroll 1.5), and a frame must be committed about 2 ms before the viewer's refresh, which came 5.0 ms after the host's. Most drag frames therefore waited 2.3 ms for a drawable and showed a refresh later. The catch-up fired 29 times in 30 s, its once-a-second maximum, and the viewer fell behind again after each.
  • Benchmark gate: green on the final head 481b0d1 (run 45960438726), with every cell within 0.1 ms of the gate on 1527d68 (run 45946536111): scroll 8.13 / 8.44 ms e2e p50 / p95 against 8.13 / 8.43. The gate runs on one Mac with an offscreen viewer, which never waits for a drawable, so it can't show the viewer change; that was measured by Tier 2 run 45942849834.
  • iOS app: builds on 481b0d1 (bun run build:ios).
  • Tests (each fails with its half of the fix switched off):
    • Catch-up rule.
    • Coordinator: worker path, main-actor path (iPhone), fallback, slow streams, newest-frame swap and hand-back.
    • Render worker's newest-frame step on real Metal.
    • Capture cadence table, and live start, resize, refresh change, failed reconfiguration and audio restart.
    • Both encoders' capture formats.
    • The shipped encoder (ScreenSharingV2EncoderKind.configured(), VCP behind the rate switch) and the fallback, fed every refresh at 60 Hz and at most 60 a second at 120 Hz.
  • Run locally: every Swift test target this touches, in full, serial suites included: ScreenSharing, ScreenSharingV2, ScreenSharingConnection, ScreenSharingPane, SimulatorPane, CodevisorCoreMac, CodevisorBench, ScreenSharingBench. All passed (PRs don't run the Swift test lane).

Code review (medium) and what was fixed

Each fix has a test that fails without it.

  1. The render worker could take a newer frame and then drop it when its draw failed: the viewer would stay on the older image on a still screen. Now the coordinator keeps every frame the worker takes and draws it next unless it was drawn.
  2. A redraw asked for while the viewer held a frame to catch up (a resize, a failed drawable) could be lost when no new frame came. The hold's fallback now draws it.
  3. A refresh-rate follow that failed after the capture had restarted could write back the old cadence. It now restores only on the same capture.
  4. A refresh-rate follow could race a capture resize and undo it. The host now applies the follow after any resize in flight, on the same chain.
  5. Smaller: a capture start without a refresh source kept the previous display's. Every start now sets its own.
  6. Smaller: the viewer's refresh period now also follows a refresh change on the same screen.

Your review (high) of 1527d68, and what was fixed

Each item has a test that fails on the old code.

  1. A refresh rate that can't be read mid-session dropped a 60 Hz display to the 120 Hz cadence. It now keeps the current cadence; the 120 Hz rule applies only at capture start.
  2. The refresh follow was chained onto session.resizing, so ending a session cancelled only the follow. It now has its own task, which end cancels. A newer follow also cancels the one before.
  3. Refresh rates just over a multiple of 60 rounded to the next multiple (120.01 Hz gave 40 fps). The rounding now has a 1% tolerance, which also fixes 60.5 to 60.6 Hz.
  4. The catch-up counted arrivals only when the mailbox went from empty to full, timed at the main-actor hop. It now counts every put, timed in the mailbox.
  5. Cadences compare by interval, so 60 vs 59.94 Hz only relabels.
  6. framesPerSecond removed.
  7. Follow failures are logged (cancellation skipped).
  8. Capture reconfigurations are serialized.

Code review (high) of that fix, and what was fixed

  1. A reconfiguration stuck on a stopped stream would block every later one. Each start now begins a new queue.
  2. A queued turn could reconfigure a stream that was still starting. Turns now wait for the start inside the turn.
  3. The cursor, dynamic range and audio setters changed their value before queuing, so an earlier turn could apply it and the setter could then roll back. They now change only inside their turn.
  4. Counting every put made bursts (VNC updates, the tiles of one update) look full-rate. Puts under half a refresh apart now count once.

Smaller ones, also fixed: the 60.5 to 61 Hz rounding, a newer follow cancels the older, tests now assert the refresh rate rather than interval-only equality, and the resize-race test asserts the follow is queued rather than relying on timing.

Left: turning audio on mid-session still restarts the stream outside the queue, so a resize in flight can be lost. That was already the case before this PR.

Your review (medium) of 481b0d1, and what was fixed

  1. A turn could run beside the next start's queue: it checked the queue before waiting for a start. It now checks after. No test reproduces this: with the test doubles a start resets its queue before it can suspend (the real display start suspends first, while it looks up the display).
  2. Catch-up could fire long after full-rate content stopped, and on redraws. It now needs the newest arrival to be at most one refresh plus the wait old, and counts only commits of new frames.
  3. The worker path could skip two frames: a commit whose frame the worker already swapped no longer starts a hold.
  4. setDynamicRange now compares inside its turn, so a second call isn't dropped.
  5. Frames replacing the held one during a hold count as superseded.
  6. A newer frame the renderer can't make textures for is dropped (renderFramesDroppedAfterDrawableWait), not held.

Each of 2 to 6 has a test that fails on the old code.

Not checked

  • iPhone and iPad, by hand: not checked. This Mac's screen is locked. The iPhone draws through the main-actor path, which the coordinator tests cover; it gets the catch-up but not the newest-frame swap.
  • iOS compile: not built locally.
  • A real 120 Hz display: the 120 Hz numbers come from a 120 Hz virtual display on the lab mini.

🤖 Generated with Claude Code

pondorasti added a commit that referenced this pull request Nov 27, 2026
…s race (851-4475)

From the code review of #1506:
- a newer frame the render worker took is drawn next when its draw fails or
  it can't be drawn into that drawable, instead of being lost
- a redraw asked for while the viewer holds a frame to catch up is drawn
  when the hold ends
- a refresh-rate follow that fails after the capture restarted keeps the
  new capture's cadence, and every start sets its own refresh source
- the host applies a refresh-rate change after a capture resize in flight
- the viewer's refresh period follows a refresh change on the same screen

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pondorasti
pondorasti force-pushed the alexandru/851-4475-screen-sharing-host-captures-a-frame branch from ec8421d to 1527d68 Compare November 27, 2026 05:33
pondorasti and others added 4 commits November 26, 2026 23:20
A full-rate stream left the macOS viewer waiting for a drawable on every
frame, so each frame showed a refresh later. The render coordinator now
skips one frame, at most once a second, when a full-rate stream had to
wait for a drawable, and the render worker draws the newest frame that
arrived while it waited.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ScreenCaptureKit delivers about 0.8 ms later than its minimum frame
interval, so the 1/60 s cap took 57.3 frames a second on a 60 Hz display
and each frame reached the engine later than the last. The cap now
follows the display's refresh rate: 1/480 s up to 60 Hz, and at most 60
frames a second with headroom on faster displays, so a 120 Hz host is
captured at every other refresh. The host follows a refresh-rate change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s race (851-4475)

From the code review of #1506:
- a newer frame the render worker took is drawn next when its draw fails or
  it can't be drawn into that drawable, instead of being lost
- a redraw asked for while the viewer holds a frame to catch up is drawn
  when the hold ends
- a refresh-rate follow that fails after the capture restarted keeps the
  new capture's cadence, and every start sets its own refresh source
- the host applies a refresh-rate change after a capture resize in flight
- the viewer's refresh period follows a refresh change on the same screen

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rival counting (851-4475)

- a refresh rate that can't be read mid-session keeps the capture's cadence;
  only a capture start falls back to the 120 Hz rule
- the host follows the refresh rate in its own task, which ending the session
  cancels, so the session's resize stays cancellable, and logs failures
- refresh rates just over a multiple of 60 round to that multiple
- the viewer's catch-up counts every frame the mailbox receives, timed when
  it is put, including frames a newer one replaced
- cadences compare by interval, so 60 and 59.94 Hz only relabel
- capture reconfigurations run one at a time, each with the newest size and
  cadence

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pondorasti
pondorasti force-pushed the alexandru/851-4475-screen-sharing-host-captures-a-frame branch from 1527d68 to 6a94f7f Compare November 27, 2026 07:21
pondorasti and others added 2 commits November 26, 2026 23:41
…d merge bursts (851-4475)

From the code review of the previous commit:
- each capture start begins a new reconfiguration queue, so one stuck on a
  stopped stream never holds up the restarted one
- a reconfiguration waits inside its turn for a start in progress
- the cursor, dynamic range and audio settings change only in their own
  turn, so an earlier turn never applies them
- the catch-up counts puts less than half a refresh apart once, so a VNC
  burst or the tiles of one update don't look like a full-rate stream
- the refresh-rate rounding uses a 1% tolerance, so 60.6 Hz counts as 60
- a newer refresh-rate follow cancels the one before

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…l-rate frames (851-4475)

From the review of 481b0d1:
- a reconfiguration checks which queue is current after it has waited for a
  start, so it never runs beside the next start's queue
- the catch-up needs the newest arrival to be recent and counts only commits
  of new frames, so a redraw long after a scroll can't hold the next
  keystroke
- a commit whose frame the render worker already swapped for a newer one
  doesn't start a hold, so the viewer never skips two frames
- setDynamicRange compares inside its turn, so a second call isn't dropped
- frames replacing the held one during a hold count as superseded
- a newer frame the renderer can't make textures for is dropped, not held

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pondorasti
pondorasti merged commit 49aa60a into main Nov 27, 2026
11 checks passed
@pondorasti
pondorasti deleted the alexandru/851-4475-screen-sharing-host-captures-a-frame branch November 27, 2026 10:29
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