Skip to content

fix(mac): 1080p30 GPU-direct streams ran video at twice real time (release blocker) - #1265

Merged
iamfatness merged 3 commits into
mainfrom
fix/mac-gpu-stream-cadence
Nov 27, 2026
Merged

iamfatness merged 3 commits into
mainfrom
fix/mac-gpu-stream-cadence

Conversation

@iamfatness

Copy link
Copy Markdown
Owner

macOS 1080p30 GPU-direct streaming: video ran at twice real time

Release blocker from the macOS 1.0.0 qualification (installer 14). It hit the shipping default for RTMP, SRT and HLS.

Root cause

The render loop is a fixed 60 Hz, but the VideoToolbox encoder made PTS as frameNumber / destinationFps (30). ProgramStreamClock anchored audio the same way.

  • Each encoded picture advanced two 30 fps periods, so video ran at about 2x audio.
  • VideoToolbox budgets bits by PTS, so it spent about 2x the target per picture. A hardware test on the old code produced 10.69 MB for 10.7 s of Program at 4 Mbps. The qualification log shows 30.07 pictures/s at 12.04 Mbps.
  • The sender's wall-clock pacer also picked uneven frames (1 or 3 Program frames apart).
  • Separately, on macOS a skipped render slot was not in the timestamps (frame numbers count renders, and there is no Program buffer to account for skips). That left 16.7 ms of A/V offset per skip.

Fix

  • ProgramFrame::programTick: the render slot on Metal. 0 elsewhere, which means the frame number.
  • GpuVideoEncoder::programTickRate: 60 for VideoToolbox. The default is the destination fps, so Windows is unchanged. The sender uses the same rate for audio PTS.
  • A slower destination picks pictures from the tick grid (ProgramCadenceDecimator: every even tick for 30). The shared encoder pool takes one picture per destination period.
  • A destination at 60 and the raw path keep the wall-clock pacer. At 60, PTS differ from before only when render slots are skipped.

Windows (from code only; not run on Windows hardware)

Windows has the same bug at 30 fps on the GPU-direct path.

  • MediaFoundationGpuVideoEncoder::processInput stamps frameNumber / config.fps.
  • D3D11 exports the encoder texture on every canvas frame.
  • The encode loop consumes each keyed-mutex publication (convertToNv12 replaces the frame number with the published one). So the sender's 30 fps pacer does not limit what gets encoded: about 60 pictures/s, each stamped 1/30 apart. That means 2x video time, with the rate controller and H264CbrFiller budgeting 1/30 s per picture.

This PR does not change Windows behaviour. The Media Foundation encoder is untouched and keeps the default tick rate. A fix there needs it to encode only the selected pictures and report 60.

The default gate now includes a 30 fps leg, so expect it to fail on Windows until that is fixed.

Gate (scripts/validate-gpu-encode.mjs)

  • Without --fps it runs 30 and then 60 on the 60 Hz canvas, with sources pinned at 60.
  • New --sink-protocol hls (local PUT origin). The SRT receiver falls back to the bundled ffmpeg when the system ffmpeg has no libsrt.
  • A new unit-tested judge fails the run on A/V drift or offset, wrong cadence, too many pictures per audio second, an early stop, or any audio gap. It prints missing pictures.
  • It fails when the sender's per-wall-second encoder output is over target by more than 10% (with --check-bitrate).
  • SRT only: an end-of-capture audio tail of up to 400 ms is allowed. Silent AAC is packed 17 frames (363 ms) per MPEG-TS PES, and the unfinished PES is lost at Stop.
  • --slow-sink runs that discard audio will now fail. They were not run here.
  • Run against unfixed origin/main at 30, the gate fails: 66.6 ms cadence, 8.2 s drift (capture cut by -t), 15.0 fps.

Real gate results (M3 Max, local sinks, one 1080p60 fake participant, 60 s unless noted)

This was not an idle machine. Parsec, ChatGPT and Chrome were running. Most runs started once the 1-minute load was below 2.45. Runs before about 01:05 were on battery and later runs on AC. This is not a performance qualification.

30 fps (the blocker): A/V clock, cadence and bitrate are correct on all three protocols

Run Received Video gaps (missing) Audio gaps A/V offset start / end Encoder, per wall second Load before Gate
RTMP 30.0 fps 0 (0) 0 -10 / +39 ms 6.00 Mbps per audio second (1) 1.94 PASS
RTMP r2 30.0 1 (1) 0 -20 / +21 ms 6.00 Mbps, 30.01 pics/s 2.42 PASS
SRT 30.0 0 (0) 0 -3 / +241 ms (2) 6.03 Mbps per audio second (1) 2.44 PASS
SRT r2 30.0 0 (0) 0 -2 / +242 ms (2) 5.99 Mbps, 29.97 1.95 PASS
HLS 30.0 0 (0) 0 -8 / -33 ms 6.00 Mbps per audio second (1) 2.25 PASS
HLS r2 30.0 0 (0) 0 -19 / +39 ms 6.00 Mbps, 30.01 1.60 PASS
RTMP, 30 s default two-leg run 30.0 0 (0) 0 -7 / +52 ms 6.02 Mbps, 30.10 2.13 PASS
RTMP raw fallback (GPU_ENCODE=0) 29.97 0 (0) 0 +21 / -56 ms n/a 1.48 PASS

(1) These runs predate the encoder output line. (2) This is the unfinished MPEG-TS audio PES at Stop. In the first capture, the video-to-audio mux position stayed at 0.345 to 0.347 s from start to end.

An earlier raw-fallback run failed only because of a judge bug with B-frame order, fixed in 2c1df92.

60 fps: timestamps and audio are correct, but frame delivery fails intermittently on both this branch and unfixed origin/main. This is the separate frame-loss problem and was not investigated. Frames are already missing at the sender's mux input.

Protocol This branch, fps (missing pictures where recorded) origin/main 40a00d6, fps
RTMP 59.9 (4); 56.6 (101, 30 s); 49.9; 48.2 53.6; 47.6
SRT 51.2 (534) not re-run; the qualification had 52.3 and 54.8
HLS 47.4; 46.8; 58.4; 52.5; 49.9 49.9; 49.6; 48.2; 54.6; 50.5

No 60 fps run had zero missing pictures. Two early SRT/HLS 60 runs overlapped each other by mistake and are excluded.

An unverified lead from writing the tests: the unchanged 60-on-60 wall-clock pacer rejects a tick more than 1 ms early. A test fixture with +-1.5 ms of jitter lost 143 of 600 frames.

Every counted 60 fps run, on both builds, had A/V offsets within 250 ms and 0 audio gaps.

Tests

  • Full native ctest on the final C++: 6/6 entries pass (main suite 299 s, realtime 61 s).
  • Judge: node --test scripts/qa/stream-timing-judge.test.mjs, 21/21.
  • Red first: on the old code, the VideoToolbox, sender-cadence and mux A/V tests all failed.
  • Coverage: real VideoToolbox 30-on-60 PTS, duration and bitrate with a Program skip; the real sender through the TS mux with the real ProgramStreamClock (30 and 60, skips, audio phase); a destination-clock (Windows-like) encoder keeping its pacing and units; Metal ticks; the shared pool; the composite anchor.
  • Mutation checks: 18 C++ and 8 judge mutations, all caught.
  • Independent review: no serious defect in the production path. Fixed from it: two destinations sharing an encoder could submit two pictures in one 30 fps period; the judge would have failed healthy 24/25/50 fps; and three weak checks.

Not fixed, noted by review: on macOS the audio anchor pairs the frame's tick with the time the video-output tick picked it up, so a late pickup at stream start becomes a constant offset of that size. This predates the PR.

Not covered: Windows hardware, HEVC, --slow-sink, multiple simultaneous destinations on hardware, audio content (the test audio is digital silence, so timestamps only), lip sync, RTMPS, and real platforms.

🤖 Generated with Claude Code

John Wallace and others added 3 commits November 27, 2026 00:53
… destination fps

macOS 1080p30 GPU-direct RTMP/SRT/HLS (the shipping default) stamped pictures
as frameNumber / 30 while frameNumber counted 60 Hz Program frames: video ran
at twice real time (A/V drift about 1 s per second), and VideoToolbox, which
budgets bits by PTS, spent about twice the target bitrate.

- ProgramFrame::programTick (Metal: the render slot, so skipped slots stay
  timestamp holes) and GpuVideoEncoder::programTickRate (VideoToolbox: 60).
- The sender converts the shared-AAC clock with that rate, and a slower
  destination picks pictures from the tick grid (every even tick for 30)
  instead of the wall-clock pacer, which picked frames 1,3,1,3 apart.
- The shared encoder pool takes one picture per destination period.
- Windows behaviour is unchanged (tick = frame number, rate = destination fps);
  the Media Foundation encoder has the same defect at 30 fps and is not fixed.
- The streaming gate runs 30 and 60 on the 60 Hz canvas, gains an HLS sink, and
  fails on A/V timestamp drift, video cadence and any audio gap.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…udio PES at Stop

Homebrew ffmpeg has no SRT, so the SRT leg could not listen on a Mac; it now
falls back to the bundled build. An SRT capture of silent audio carries 17 AAC
frames (363 ms) per PES and loses the unfinished one at Stop: 241 ms measured on
a healthy run, with a constant video/audio mux position for the whole capture.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…er's wall-second output

The raw fallback encodes B-frames, so packet order is decode order: monotonicity
is checked on DTS and cadence on sorted PTS. The sender's mux-input Mbps per wall
second now gates --check-bitrate: a capture cut at a media-time limit hid the
doubled bitrate (6.0 per media second against 12 emitted).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@iamfatness
iamfatness marked this pull request as ready for review November 27, 2026 12:59
@iamfatness
iamfatness merged commit 1890468 into main Nov 27, 2026
30 of 31 checks passed
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