Skip to content

input: rate-limit gamepad state sends and remove SDL timers from the input path - #2061

Closed
robinmaomao wants to merge 3 commits into
moonlight-stream:masterfrom
robinmaomao:pr/gamepad-input-throttle
Closed

robinmaomao wants to merge 3 commits into
moonlight-stream:masterfrom
robinmaomao:pr/gamepad-input-throttle

Conversation

@robinmaomao

@robinmaomao robinmaomao commented Oct 24, 2026 •

Copy link
Copy Markdown

Problem

Controllers that poll much faster than the stream frame rate can cause periodic stutter and frame-pacing hitches on the host.

With a joystick present, Moonlight batches axis events but does not limit how often they are sent. A 1000 Hz controller can therefore produce roughly 1000 LiSendMultiControllerEvent() calls per second, and SDL_WaitEventTimeout() degrades to a 1 ms poll on the thread that also presents video frames. On the host, Sunshine handles the Xbox 360 virtual pad path with one synchronous ViGEm IOCTL per packet (unlike the DS4 path), so this packet rate turns into frame-pacing spikes (see #1156).

Reported against the built-in controller of Windows handhelds such as the ROG Xbox Ally X.

Changes

  • Add a client-side send throttle for gamepad state, exposed as a new Controller update rate setting (62.5 / 125 / 250 / 500 / 1000 Hz / Unlimited). Default is 125 Hz.
  • Button and trigger edges are still sent immediately, and the trailing state is always flushed once the throttle window elapses, so the host can never be left with a stale stick position.
  • Remove SDL timers from the input path. Windows builds use SDL2.dll as sdl2-compat over SDL3.dll, where SDL_RemoveTimer() destroys the timer's properties before cancelling it. A callback racing with removal dereferences freed memory and crashes in SDL3's SRW lock helpers (observed at SDL3.dll+0x1B8444, SDL_UnlockRWLock_srw). Pending state and deadlines are now serviced from the SDL event loop, and SDL_WaitEventTimeout() is capped by the nearest deadline. This also migrates the touch long-press / drag / tap-release timers and the gamepad mouse-emulation tick off SDL timers.

Testing

  • Built with MSVC 19.44 and Qt 6.10.3 (local toolchain; upstream CI uses newer Qt).
  • App starts and streams normally from the packaged build.
  • Validated on a ROG Xbox Ally X: with the built-in controller, the periodic stutter/frame-pacing hitches previously seen while moving the sticks are gone.
  • Mouse, keyboard, touch, rumble, adaptive trigger, and external gamepad paths are functionally unchanged; only the timer implementation behind touch gestures and gamepad mouse emulation moved to the event loop.
  • No new compiler warnings; the only link warning is the pre-existing LNK4291 from Qt6EntryPoint.lib.

Notes / limitations

  • Sub-8 ms opposite-direction flicks are coalesced by the 125 Hz default, like any 125 Hz pipeline. The setting can be raised to 250/500/1000 Hz if that is noticeable.
  • With a DS4 virtual pad, Sunshine already coalesces input, so the benefit is smaller than with the Xbox 360 path.
  • Unlimited restores the previous upstream behavior exactly, which is useful for A/B testing.

robinmaomao and others added 3 commits October 24, 2026 11:15
Cap how often gamepad state packets are sent to the host. Controllers that
poll much faster than the stream frame rate (such as handheld built-ins that
report at ~1000 Hz) currently generate roughly one LiSendMultiControllerEvent()
per millisecond. Sunshine processes every packet as a synchronous ViGEm IOCTL
on its single input thread, which surfaces as periodic frame-pacing hitches
while the stick is moving.

Axis and trigger updates are now coalesced into a configurable window
(default 8 ms = 125 Hz). Button edges are still sent immediately, and a
one-shot SDL timer pushes a user event so the trailing state is always
delivered - dropping it would leave the host with a stale stick position.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The gamepad send throttle used 1-8 ms SDL timers. With sdl2-compat,
SDL_RemoveTimer() destroys the timer's properties before cancelling the
timer, so a callback racing with removal dereferences freed memory and
crashes inside SDL3's SRW lock helpers (observed at SDL3.dll+0x1B8444).

Replace every input-path SDL timer with main-loop deadlines:
- gamepad send throttle and mouse-emulation polling
- touch long-press, drag, and tap-release timers

Pending state and deadlines are serviced from the SDL event loop, and
SDL_WaitEventTimeout() is capped by the nearest deadline.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@cgutman

cgutman commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

I think this should be handled on the host side, not the client. If the host's input driver can't handle 1000 Hz input, that should be its problem to handle batching, not Moonlight's.

@cgutman cgutman closed this Nov 27, 2026
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