Skip to content

feat(linux): pace capture at exact fractional NTSC framerates#5282

Merged
ReenigneArcher merged 4 commits into
LizardByte:masterfrom
djadjka:feat/linux-fractional-pacing
Jun 30, 2026
Merged

feat(linux): pace capture at exact fractional NTSC framerates#5282
ReenigneArcher merged 4 commits into
LizardByte:masterfrom
djadjka:feat/linux-fractional-pacing

Conversation

@djadjka

@djadjka djadjka commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Description

#4019 added support for the x-nv-video[0].clientRefreshRateX100 RTSP parameter, but
implemented the capture-side handling only for the Windows backend
(platform/windows/display_base.cpp); on Linux only the encoder time base got the exact
rational. The Linux capture loops still pace at the integer maxFPS, so for a client that
requests e.g. 11988 (Xbox, whose display pipeline runs at 120/1.001 Hz) the host delivers
120.00 fps while the client drains 119.88 fps. The 0.1% surplus accumulates one extra frame
every ~8 s, which the client-side pacer keeps correcting — visible as a periodic frame-queue
oscillation / micro-judder that no client setting can remove.

This PR mirrors the Windows handling in the four Linux capture pacers (kmsgrab, wlgrab,
x11grab, CUDA): when framerateX100 is present, the frame interval is derived from the
exact rational returned by video::framerateX100_to_rational().

Tested on an AMD RDNA4 + KDE Wayland host (KMS capture, Vulkan encoder) against a
moonlight-xbox client at 4K 119.88 Hz: the standing frame-queue oscillation on
the client disappears (queue settles flat instead of breathing by one frame every ~8 s).

Issues Fixed or Closed

Completes the Linux side of #4019.

Type of Change

  • feat: New feature (non-breaking change which adds functionality)

Checklist

  • Code follows the style guidelines of this project
  • Code has been self-reviewed
  • Code has been commented, particularly in hard-to-understand areas

AI Usage

  • Heavy: AI generated most or all of the code changes

@neatnoise

neatnoise commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Would be possible to implement it to the new pipewire based capture (xdg portal, kwin grab) too? Or is it enough there that the compositor sets the max fps?

@djadjka

djadjka commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

Would be possible to implement it to the new pipewire based capture (xdg portal, kwin grab) too? Or is it enough there that the compositor sets the max fps?

Double-checked: both PipeWire paths (xdg-portal portal_t and KWin grab kwin_t) share
pipewire::pipewire_display_t, whose init() already derives the pacing interval from
framerateX100_to_rational() — so they handle fractional rates exactly.

Compositor pacing isn't involved there: the format is deliberately negotiated as
variable rate (SPA_FORMAT_VIDEO_framerate = 0/1, "bypassing compositor pacing"), and
Sunshine paces itself. This PR just brings the legacy paths to parity with that.

@ReenigneArcher ReenigneArcher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make a reusable function instead of copying the same code to so many different places.

@djadjka djadjka requested a review from ReenigneArcher June 11, 2026 13:28

@ReenigneArcher ReenigneArcher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/platform/linux/pipewire.cpp Outdated
Comment thread src/platform/linux/pipewire.cpp Outdated
@djadjka djadjka requested a review from ReenigneArcher June 11, 2026 14:55
@andygrundman

Copy link
Copy Markdown
Contributor

Thanks, I am glad someone besides me cares about NTSC. :)

@ReenigneArcher ReenigneArcher force-pushed the feat/linux-fractional-pacing branch from 99f06f4 to dd923f4 Compare June 16, 2026 15:51
@codecov

codecov Bot commented Jun 16, 2026

Copy link
Copy Markdown

Bundle Report

Bundle size has no change ✅

@codecov

codecov Bot commented Jun 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 44.82759% with 16 lines in your changes missing coverage. Please review.
✅ Project coverage is 25.53%. Comparing base (9cb3589) to head (eabb2f2).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/platform/linux/pipewire.cpp 0.00% 3 Missing and 2 partials ⚠️
src/platform/linux/wlgrab.cpp 0.00% 3 Missing and 2 partials ⚠️
src/nvenc/nvenc_base.cpp 0.00% 3 Missing ⚠️
src/platform/linux/cuda.cpp 0.00% 1 Missing ⚠️
src/platform/linux/kmsgrab.cpp 0.00% 1 Missing ⚠️
src/platform/windows/display_base.cpp 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5282      +/-   ##
==========================================
- Coverage   27.51%   25.53%   -1.99%     
==========================================
  Files         113      112       -1     
  Lines       25593    25202     -391     
  Branches    11237    10922     -315     
==========================================
- Hits         7043     6435     -608     
- Misses      15560    16043     +483     
+ Partials     2990     2724     -266     
Flag Coverage Δ
Archlinux 0.00% <0.00%> (ø)
FreeBSD-amd64 13.36% <38.46%> (+0.05%) ⬆️
Homebrew-macos-14 ?
Homebrew-macos-15 ?
Homebrew-macos-26 21.34% <76.92%> (+0.04%) ⬆️
Homebrew-ubuntu-24.04 ?
Linux-AppImage ?
Windows-AMD64 ?
Windows-ARM64 13.35% <56.25%> (+0.06%) ⬆️
macOS-arm64 19.31% <60.00%> (+0.05%) ⬆️
macOS-x86_64 ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/platform/linux/x11grab.cpp 33.26% <100.00%> (-7.39%) ⬇️
src/video.cpp 51.59% <100.00%> (-1.09%) ⬇️
src/video.h 58.06% <100.00%> (-0.91%) ⬇️
src/platform/linux/cuda.cpp 0.00% <0.00%> (-1.68%) ⬇️
src/platform/linux/kmsgrab.cpp 0.00% <0.00%> (-3.86%) ⬇️
src/platform/windows/display_base.cpp 27.14% <0.00%> (-9.89%) ⬇️
src/nvenc/nvenc_base.cpp 30.10% <0.00%> (-0.32%) ⬇️
src/platform/linux/pipewire.cpp 0.16% <0.00%> (+<0.01%) ⬆️
src/platform/linux/wlgrab.cpp 0.00% <0.00%> (ø)

... and 71 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9cb3589...eabb2f2. Read the comment docs.

@djadjka

djadjka commented Jun 27, 2026

Copy link
Copy Markdown
Contributor Author

@ReenigneArcher, should I add anything to get this PR merged ?
Thank you!

@djadjka djadjka force-pushed the feat/linux-fractional-pacing branch from fe85434 to 00acec7 Compare June 28, 2026 08:34

@ReenigneArcher ReenigneArcher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New requirements since you created the PR: need more complete code documentation. https://app.readthedocs.org/projects/sunshinestream/builds/33342103/ (search for error:)

@andygrundman could you review since you're the only contributor I know using the NTSC framerates?

@andygrundman

Copy link
Copy Markdown
Contributor

This looks pretty good to me!

djadjka and others added 4 commits June 30, 2026 08:24
When a client requests a fractional refresh rate via
x-nv-video[0].clientRefreshRateX100 (e.g. 11988 for an Xbox whose display
pipeline runs at 120/1.001 Hz), the encoder time base is already set to the
exact rational since LizardByte#4019, but the Linux capture loops still paced at the
integer maxFPS. The resulting 0.1% delivery surplus accumulates one extra
frame every ~8 s, which the client pacer keeps correcting (periodic frame
queue oscillation / micro-judder).

Apply the same exact-rational handling to the kmsgrab, wlgrab, x11grab and
CUDA capture pacing, mirroring the Windows implementation from LizardByte#4019.

Tested on an AMD (RDNA4) + KDE Wayland host with KMS capture against a
moonlight-xbox client at 4K 119.88: the standing frame-queue oscillation on
the client disappears.
Extract video::capture_frame_interval() and use it in all Linux capture
pacers, including the PipeWire path which carried its own copy of the
same math.
Address review feedback: extract video::framerate_to_rational() as the
single place implementing the "exact rational when framerateX100 is set,
integer framerate otherwise" pattern, and rebuild capture_frame_interval()
on top of it. Migrate the call sites that re-implemented the pattern:
avcodec and NVENC encoder setup become branchless, the PipeWire and
wlroots capture pacers no longer re-derive the rational for logging, and
the Windows strict frame rate reuses the helper inside its sentinel
branch (the {0,0} sentinel must stay to keep the refresh-rate matching
heuristic). Add unit tests for the new helper and the capture frame
interval, including the integer fallback.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Doxygen runs with WARN_NO_PARAMDOC + WARN_AS_ERROR in the Read the Docs
build, which failed because framerate_to_rational and capture_frame_interval
carried a @brief-only block without documenting their config parameter or
return value. Add @param/@return to match framerateX100_to_rational.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@ReenigneArcher ReenigneArcher force-pushed the feat/linux-fractional-pacing branch from 7919ddd to eabb2f2 Compare June 30, 2026 12:24
@sonarqubecloud

Copy link
Copy Markdown

@ReenigneArcher ReenigneArcher merged commit bba6c6c into LizardByte:master Jun 30, 2026
71 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.

4 participants