Skip to content

Don't give a slow link's first keyframe up while it can still arrive (851-4477) - #1510

Merged
pondorasti merged 14 commits into
mainfrom
alexandru/851-4477-keyframe-give-up
Nov 27, 2026
Merged

pondorasti merged 14 commits into
mainfrom
alexandru/851-4477-keyframe-give-up

Conversation

@pondorasti

@pondorasti pondorasti commented Nov 27, 2026 •

Copy link
Copy Markdown
Member

Part of 851-4477 (screen sharing freezes briefly while scrolling on a lossy network). This PR is the first keyframe on a slow link. The clumped-delivery PR comes later; the estimate collapse on the 50 Mbit/s network is 851-4501.

What was wrong

On a 1 Mbit/s link a tile's first keyframe (about 190 KB) was declared lost about when its last packets arrived, and the host answered with a second, larger keyframe. Three things added up:

  1. QUIC discards packets early in a connection. The viewer's QUIC library drops some packets while new paths are opened and on the first key update, so the first loss is real.
  2. Both ends gave the frame up too early. The viewer gave up one round trip after the last packet. The host's probes ran on a round-trip figure that doesn't include the link's queue.
  3. The host resent repeats every round trip. Each repeated request was answered while the first resend was still seconds deep in the link's queue. In the sim, 130 lost packets were resent 1,268 times.

What changes

All of it is in the Rust transport (codevisor-media), so the shipped encoder (VCP behind the rate switch) and the fallback get the same code.

  • The viewer waits for what can still arrive. It waits out the missing packets at the pace that frame's own packets came at. While packets keep arriving back to back (a queue that could be ahead of the resend), the wait doesn't run out either. Traffic with gaps between frames doesn't hold a frame: with four tiles of video on 50 Mbit/s, a packet that never arrives is given up 66 ms after the request in the sim (give-up time 53 ms), not 218 ms. Bound: 5 s in all.
  • The host doesn't give up a frame the viewer is still answering. A request naming a new packet restarts the probe count. So does a repeat the host sends again, or one whose resend it is holding. After the sixth unanswered probe, it waits as long as what was in flight needs to cross at the acknowledged rate, at most 5 s.
  • The host holds a repeat while its resend is behind a deep queue. It tracks how far into what it sent the viewer has got. A resend that left with more than four round trips of queue ahead isn't sent again until that point passes it. Everything else keeps the old half-round-trip rule. The count starts again after an idle spell only once the measured reach has passed everything sent, so fresh resends or a given-up frame's queued packets don't reset it.
  • Benchmark: both tiers now time the first picture, and the two-Mac tier gets a 1 Mbit/s network (N7: 50 ms each way, 5% loss). Its plan check budgets the 90 s ready wait for runs on that network.

The crate README has the full description and the numbers behind each choice.

The trade on a 1 Mbit/s link

On a 1 Mbit/s link this PR trades long stalls for short hitches:

  • stalls over half a second drop from 2.5–3.3 s to 0.6–0.8 s per run (median);
  • one keyframe instead of two or three, and almost no frames given up;
  • the first picture arrives in about 3 s, against up to 14 s on main;
  • but there are about twice as many gaps between 250 and 500 ms, for about the same total time in gaps over 250 ms.

The likely cause, not proven: this PR waits for a slow frame to finish crossing the link, where main gives it up and refreshes. Each slow frame then shows as one 250–500 ms gap, instead of fewer, longer stalls while a refresh is requested and sent.

The tables are below: "Fixed-threshold gaps" has the 250/500 ms split, and the first 1 Mbit/s table has keyframes, give-ups, first picture and freezes.

Lab (tamale, same-slot pairs)

1 Mbit/s network, scroll, six repetitions, main at 49aa60a against 74f444a. Runs 46033888710/46034691778 (fallback) and 46035491044/46036614459 (shipped).

Fallback main → this PR Shipped main → this PR
Keyframes per run 2 in 6 of 6 → 1 in 6 of 6 1–3 (2 in 4 of 6) → 1 in 6 of 6
Frames given up per run 3–7 → 0–1 3–8 → 0–2
First picture not recorded on main → 2.6–2.9 s not recorded on main → 2.8–3.2 s
Updates/s (median) 10.8 → 12.2 8.1 → 18.0
Longest gap between updates 0.8–1.9 s → 0.5–0.7 s 0.5–2.9 s → 0.5–1.1 s
Freezes (six runs) 65 → 96 45 → 109
Latency p95 (median) 225 → 274 ms 307 → 234 ms

First-picture timing is new in this PR, so main's build doesn't record it; main's earlier runs on this network showed 2.7–14.3 s. The freeze counter flags a gap three times the average of the last 30 gaps, so a faster stream with shorter stalls gets more of them flagged. The earlier pair below shows the same rise.

Fixed-threshold gaps, same network and scene, six repetitions (runs 46046692081/46047309465 fallback, 46048049753/46048777518 shipped). Both sides carry #1512's gap count: #1512's head (cbdcb49) against this PR's commits on top of it (lab branch 16e7219, the same tree as this PR's head after the rebase onto main). Median per run, range in brackets:

Fallback main → this PR Shipped main → this PR
Gaps over 250 ms 12 (11–15) → 22 (5–27) 11.5 (7–19) → 21 (16–27)
Time in them 5.7 s (4.3–8.3) → 7.1 s (2.0–10.1) 7.6 s (3.1–8.9) → 7.2 s (5.0–8.8)
Gaps over 500 ms 3 (0–5) → 1.5 (0–4) 3.5 (0–5) → 1 (0–2)
Time in them 2.5 s (0–6.1) → 0.8 s (0–2.4) 3.3 s (0–5.8) → 0.6 s (0–1.3)

This PR has about twice as many gaps between 250 and 500 ms, and fewer and shorter gaps over 500 ms.

Earlier pair, before #1508 and before your review's fixes, scroll and video: runs 45917034388 and 45917909821 (fallback), 45924480340 and 45925299171 (shipped).

Fallback before → after Shipped before → after
Scroll, updates/s (median) 5.3 → 11.9 9.7 → 11.1
Scroll, keyframes per run 2 in 6 of 6 → 1 in 5 of 6 2 in 6 of 6 → 1 in 5 of 6
Scroll, frames given up per run 4–9 → 0–1 4–13 → 0–1
Scroll, freezes (six runs) 48 → 67 50 → 73
Video, updates/s (median) 24.8 → 38.7 24.8 → 37.7
Video, frames given up per run 1–38 → 0 10–43 → 0
Video, freezes (six runs) 72 → 50 87 → 54

All seven simulated networks, both encoders, one repetition each. Runs 45907401490/45910867266, 45914887801/45915977188, 45918777916/45920462091 and 45922155291/45923418670. On the good, lossy, burst-loss and 30 Mbit/s networks, latency, updates/s and freezes are unchanged within noise, and first picture stays at 43–132 ms.

50 Mbit/s jittery network (25 ms ± 5, 0.1% loss), six repetitions, main at 49aa60a against 74f444a (after the fixes from your review). Runs 46028957018/46030099696 (fallback) and 46031274586/46032439045 (shipped).

Main This PR
Fallback video 60.0 updates/s, 0 freezes 60.0, 0 freezes
Fallback scroll median 58.0, 4 freezes 57.7, 6 freezes
Shipped video median 60.0, 3 freezes 59.9, 5 freezes
Shipped scroll median 55.8, 18 freezes 58.0, 1 freeze
Runs with the estimate collapsed (scroll) 4 + 4 4 + 4

Latency is unchanged except shipped video, p95 59.3 → 64.9 ms. There the rate switch to the fallback encoder fired in 2 of 6 runs on main and 5 of 6 here; in the pair before the fixes it was 5 on main and 3 here, so that count is noise between runs.

The pair before your review's fixes (main at 15a46a3, runs 46001609754/46003118246 and 46004287387/46005429801) had fallback scroll at 9 → 14 freezes and shipped video at 3 → 14. That shipped-video gap is gone; the remaining differences are one or two runs either way.

Why the freezes on the 50 Mbit/s network are not this PR (851-4501). They all come from a rate-estimate collapse that main has too:

  • A pause in arrivals during a large frame makes the viewer ask for 30–170 packets that are still on their way.
  • The host counts those requests as losses, so its one-second loss window reads 13–32% on a link that loses 0.1%.
  • The heavy-loss cut then drops the estimate to about 3 Mbit/s, and it stays there.

In 16 interleaved local traced runs (fallback scroll on that network), the collapse started the same way every time:

Main This PR
Runs collapsed 5 of 8 5 of 8
Freezes (all inside collapsed runs) 23 21
Heavy-loss cuts 6 6

The real network lost 13–14 packets per 30 s run in those traces. So the lab gap is which runs hit the trigger, not this change. 851-4501 takes it.

Tests

New sim tests in tests/core.rs (each fails without the change), covering:

  • a frame still crossing a slow link isn't declared lost;
  • neither end gives up a frame the viewer is still asking for;
  • resends waiting behind another tile, on 1 Mbit/s and on 50 Mbit/s (behind a 1.5 MB keyframe);
  • a held resend keeping its frame alive while the viewer asks for it;
  • repeats aren't resent every round trip (130 lost packets: ≤195 resends, against 1,268);
  • a resend lost twice on a fast link is repaired at the next request;
  • a resend lost behind a slow queue is sent again once the queue has passed it;
  • four tiles of video on 50 Mbit/s don't hold a lost frame past one give-up time plus a frame interval;
  • bounds: a frame that never arrives, or a packet that never gets through, still ends lost.

Unit tests: the arrival tracker (arrived.rs), a repeat the host sends again counting as an answer, and a whole-frame request not marking the frame as guessed. A script test (tier2-plan.test.mjs) covers the plan budget.

One assertion changed on purpose: the_viewer_waits_a_little_longer_while_other_tiles_arrive… required the viewer to hold a lost frame for over two give-up times while another tile's video arrived with gaps. Your review found that hold behind the 50 Mbit/s freezes, so it's replaced by the four-tile test above, which requires the opposite.

Run locally (M1 Pro, with timeouts):

  • Rust: cargo test -p codevisor-media (50 unit, 57 sim, 6 session), plus clippy.
  • After the rebase onto main (with Count the gaps over 250 ms and 500 ms in every bench run (851-4503) #1512): the same Rust tests; ScreenSharingV2Tests 206, ScreenSharingBenchTests 98 and CodevisorBenchTests 61; swift:format:check and swift:lint.
  • Swift (the bench files this touches): ScreenSharingV2Tests 206 (including the hardware encoder suites on VCP and VideoToolbox), ScreenSharingBenchTests 96, CodevisorBenchTests 59. One run of four timed out in ScreenSharingV2HostContentIntervalTests.eachTileCarriesTheTimeSinceItsOwnContentChanged, a Give VCP frames the bits for their content interval, not the wait before them (851-4499) #1508 test on main that this PR doesn't touch; three more full runs and five runs of that suite passed.
  • node --test on the bench scripts.

Code review

Your review at a1c566d. All five are fixed:

  1. The viewer counted the link busy whenever any packet arrived, so on 50 Mbit/s with several tiles it held a lost frame for about four give-up times. It now counts only back-to-back arrivals, which mean a queue could be ahead of the resend. Sim: 218 → 66 ms.
  2. A repeat the host sends again now restarts the probe count.
  3. The arrival tracker no longer restarts just because no frame is in flight. It restarts only once the measured reach has passed everything sent, and otherwise keeps its sightings and known losses.
  4. Whole-frame ranges no longer set the frame's guess.
  5. The two-Mac plan check adds the 90 s ready wait to each run on the 1 Mbit/s network.

Code-review skill on the fixes. Five fixed:

  • The back-to-back gap had a fixed 1 ms floor, too tight for bursty delivery. It now uses the viewer's tail wait, which follows the longest gap seen inside a frame.
  • Before the link is measured, sightings across app-limited gaps understate the rate. The tracker skipping its restart there would have kept the idle time, so it now always restarts until a rate is measured.
  • A real restart clears the loss total again, so one over-count can't stick for the session.
  • Nothing tested that the hold still works on a fast link; the 1.5 MB keyframe test does now.
  • The four-tile test stepped 17 ms rounds; it now steps whole frame intervals.

Three left:

  • The host keeps resending a repeat while the viewer asks. It no longer has its own deadline then, which is item 2 as asked. The viewer's bounded give-up ends it; if that report is lost, the probe cycle and expiry follow.
  • The 90 s ready wait applies on every network. It did before this PR; only N7 runs are known to need it, so only they are budgeted for it.
  • A whole-frame request counts the frame's start as reached. The viewer only sends one after a later frame's packet arrived, so the start was passed.

Still left from the round before your review:

  • A lost request can make the host take one packet as arrived. The worst case is main's resend behavior for that packet; real evidence would need a wire field.
  • The viewer marks packets past a request's 64-range cap as requested. This predates this PR.
  • The viewer can wait up to 5 s for a frame the host already dropped. The host can't tell the viewer without a wire change.

Not measured

  • The two-Mac pair on the 1 Mbit/s network. Main's own run there (45777237923) showed first picture at 3.6–17.3 s.
  • Text fidelity (T8).
  • Video on the 1 Mbit/s network on this head (scroll was rerun above).

🤖 Generated with Claude Code

pondorasti and others added 14 commits November 27, 2026 10:28
…-Mac network (851-4477)

Reports gain first_picture_ms: in Tier 1 from the first version produced to
the first frame the viewer commits, in Tier 2 from the viewer's connect to
its first presented frame. The two-Mac benchmark can shape N7 (50 ms, 5%
loss, 1 Mbit/s), the only case slow enough for a first keyframe to overlap
the connection's start-up.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
The host counted a frame lost after six probes spaced by a round trip that
was measured before the link's queue built up, and the viewer gave a frame
up one round trip after its last packet although its resends were still
queued. On a 1 Mbit/s link that turned a first keyframe that lost some
packets into a second, larger keyframe. The viewer now keeps waiting while
the link is still delivering, and the host treats a NACK as an answer and
waits at least as long as the bytes in flight need to cross.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…4477)

Review fixes. The viewer waits at most 5 s more while the link delivers,
and a complete keyframe or refresh releases the frames of its stream that
are still being repaired. The host's count starts again only at a NACK
that names a packet it wasn't asked for before, its last wait is at most
5 s, and it reads the acknowledged rate at the present time.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…(851-4477)

Second review round. Releasing a tile at its recovery frame made the host
answer the released frames with another recovery frame, so it is gone. The
viewer's wait while the link delivers is now at most four times the time
the frame's own missing packets need, counted from its last packet, and the
host fixes its last wait when its sixth probe goes out.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…n to have got (851-4477)

Co-Authored-By: Claude Fable 5.2 <[email protected]>
… first request as an answer (851-4477)

Co-Authored-By: Claude Fable 5.2 <[email protected]>
… the byte count (851-4477)

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…nce the reach has passed it (851-4477)

Co-Authored-By: Claude Fable 5.2 <[email protected]>
…every sighting (851-4477)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…he host's answer, restart and guess rules (851-4477)

- The viewer holds a lost frame for other traffic only while that traffic arrives back to back, which means a queue
  could be ahead of the resend. With gaps between frames nothing is queued: four tiles of video on 50 Mbit/s held a
  packet that never arrived 218 ms after its request in the sim; now 66 ms.
- A repeat the host sends again counts as the viewer answering, so the host doesn't run out its probes on a frame
  the viewer is still asking for.
- The arrival tracker starts its count again only once its reach has passed everything sent, and keeps what is known
  lost, instead of restarting whenever no frame is in flight.
- A whole-frame request no longer marks the frame as guessed past what was sent.
- The two-Mac plan check budgets the 90 s ready wait for runs on the 1 Mbit/s network.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…cker early only on a measured rate (851-4477)

What the code review of the last commit found:
- The back-to-back gap floor is the viewer's tail wait instead of a fixed 1 ms, so bursty delivery (Wi-Fi
  aggregation) still counts as a queue.
- The arrival tracker skips a restart only when the link has been measured; before that, sightings across
  app-limited gaps understate the rate. A real restart clears the loss total again, so an over-count can't stick.
- A fast-link test shows the hold still works: a resend queued behind another tile's 1.5 MB keyframe is waited for.
- The gap test steps whole frame intervals and names its lost frame directly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pondorasti
pondorasti force-pushed the alexandru/851-4477-keyframe-give-up branch from 74f444a to 1679f46 Compare November 27, 2026 18:32
@pondorasti
pondorasti merged commit 1a74aba into main Nov 27, 2026
11 checks passed
@pondorasti
pondorasti deleted the alexandru/851-4477-keyframe-give-up branch November 27, 2026 19:00
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