Skip to content

fix(receiver): present only the newest decoded frame under backlog - #230

Merged
swellweb merged 1 commit into
swellweb:maint-3.6from
ziyadasghar-GIT:pr/receiver-newest-frame
Nov 12, 2026
Merged

swellweb merged 1 commit into
swellweb:maint-3.6from
ziyadasghar-GIT:pr/receiver-newest-frame

Conversation

@ziyadasghar-GIT

Copy link
Copy Markdown

Problem

The receiver runs decode, the 5K NV12 upload (SDL_UpdateNVTexture, ~22 MB per frame) and present on one thread, strictly in order. When one frame runs long (most often a keyframe), later frames queue up and the receiver renders that backlog one frame at a time. It never catches up, so the stall spreads and TCP back-pressure makes the sender drop frames too. At 5K 60 this shows up as uneven frame rate and blocky artifacts.

Change

  • On the VideoToolbox path the decoder hands back the decoded CVPixelBufferRef through a new optional tb_dec_set_pixbuf_cb() hook. Every frame is still decoded, because the HEVC reference chain must stay intact.
  • main.c keeps only the newest decoded frame (retained), releasing any older one, and presents it once per main-loop pass. The pending frame is dropped on disconnect and at exit.
  • Each socket drain pass is capped at 8 ms (TB_DRAIN_BUDGET_MS) so presenting is never starved.
  • Socket receive buffer 4 MB → 8 MB, falling back to 6 MB then 4 MB if the kernel refuses.
  • The FPS log reads N fps (decoded D, skipped S stale) when skipping happens.

Software/VAAPI decode and the raw NV12 passthrough (handle_raw_frame) are unchanged.

Testing

Hardware: MacBook Air M5 (macOS 27.0.1) → iMac18,3 27" 5K 2017, Radeon Pro, macOS Ventura 13.7.8, Thunderbolt Bridge (40 Gb/s link). The receiver was cross-built x86_64 against the dylibs bundled in the official 3.6.0 receiver.

A/B at the 5K 60 profile, 120 s per scenario plus 5 reconnects. Note: the patched arm also ran the sender change from the companion PR, so the frame-rate and bitrate numbers below are for both together. The main-loop numbers are receiver-only.

Baseline 3.6.0 Patched
Main loop during scroll trapped in drain_socket: one 6,371-frame FPS bucket over 131 s (0.02 loop ticks/s) 0.98 ticks/s, largest bucket ~55 frames
Delivered fps: scroll 48.8 51.1 / 47.8 (two runs)
Delivered fps: video 44.9 48.0 / 44.4
Delivered fps: window drag 38.9 38.8 (sender-bound)
Receiver RSS ~392 MB ~243 MB, flat
Decode errors / reconnects 0 / 5 of 5 clean 0 / 5 of 5 clean

iMac health while streaming the patched build: CPU 66 °C, GPU 60 °C, no throttling (CPU_Speed_Limit 100), 78% memory free, no swap.

CI-equivalent checks on maint-3.6 + this commit: make test: net parser 67, input queue 562, idle watchdog 12, receiver profile 13, status layout 43, all passing. Full receiver build clean.

It has been running as my daily receiver since 2026-10-30.

Credits

Patch prepared by Ziyad Asghar. Applied to maint-3.6, built, hardware A/B-tested and measured with Claude Opus 5.5 (Anthropic), credited via the Co-Authored-By trailer.

I know maintainer time is limited at the moment, so I kept this small and self-contained. Happy to adjust anything.

Decode every VideoToolbox frame (the HEVC reference chain must stay
intact) but upload and present only the newest one, so a backlog after a
keyframe burst or a slow present is skipped instead of being rendered one
22 MB 5K upload at a time.

- Retain the newest CVPixelBuffer, release any older pending one; present
  once per main-loop pass; drop the pending frame on disconnect and exit.
- Bound each socket drain pass to 8 ms (TB_DRAIN_BUDGET_MS) so presenting
  is never starved.
- Request an 8 MB socket receive buffer, falling back to 6 MB then 4 MB.
- FPS log reports decoded and skipped-stale counts when skipping happens.

Software/VAAPI decode and the raw NV12 passthrough path are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

2 participants