Files
VoidLink/ideas/pts-display-link.txt
T
Travis Thieman df2b67ac99 Fix thread sync
Reformat Metal files

Rewrite Metal render loop to not use DisplayLink so we can use the amazing afterMinimumDuration API call

Improve frame drop algorithm

Misc fixes to HDR <-> SDR switching, deprecation warning cleanup

Fix AVSB mode

Fix renderingBackend selected value

Add Rendering Backend setting to iPhone

Let users choose which renderer to use. Refactor metrics into a singleton

Fix tvOS build

Port moonlight-qt's Metal renderer

Bump imgui to latest

Misc tweaks and refactoring

Switch iOS to use Opus float32 audio, in line with other clients.  It was already being sent from the server as float32, so this is simply decoding it with less loss of dynamic range.

Give FrameQueue a faster data structure and full test coverage. Remove pts frame pacing, it's not a fit for iOS.

Don't allow very transparent opacity for middle stats overlay box

Change wording to select button instead of remote button

Add tvOS gesture for double-clicking remote button to show/hide stats

Remove kVTDecodeFrame_1xRealTimePlayback, this was found to seriously impact decode times

Move decode time stat out of async block

Enabe FQ logging

Misc cleanup/fixes

iPad: 26 will require UIRequiresFullScreen to be false, and for all orientations to be supported

Updated iPhone and iPad settings panels

Add setting for graph opacity. Enable stats and graphs to be toggled via two finger swipe

Fix wait time in displaylink, fix settings spacing, stats fixes

Settings for frame queue size, graphs. Misc refactoring and cleanup

Bump moonlight-common-c

Improve logging readability

Fix host frametime and frame queue stats to be recorded during input not output. Show min/max/avg decode time

Add bytes per frame graph

Started on settings changes, both modes working very well

Misc refactoring, move graphs around, both pacing functions working well

Set frameDropTarget to 2 always

Use raw RTP timestamps without converting to floating point for even more accurate playback

Standard frame pacing: remove +3 logic

Switch to NSRunLoopCommonModes for displayLink

Add frame pacing algorithm based on moonlight-qt's Pacer class as a simpler baseline

Switch to os_unfair_lock, misc tweaks

Large refactor that now uses VTDecompressionSession based on PR535 from felipejfc. This now allows more control over queue size and allows dropping arbitrary frames, but introduces a lot of new problems

bump moonlight-common-c to fix 32-bit rollover issue

LiWait timing graph

Add displaylink interval graph

Graphs checkpoint

ImGui graph working checkpoint

ImGui working checkpoint

Add pending frames to stats, add a define for verbose logging

DisplayLink WIP

Add imgui & implot modules

Frame pacing: alternate method based on thieman's pullFrames thread

Frame pacing: simplify displayLink, support passing targetTimestamp down to the sample buffer

Point to my common-c with some timing changes.

Add stats gestures: 2-finger swipe down to show stats, and 2-finger swipe up to hide

Limit refresh rate to 60 when in low power mode

Display screen refresh rate in stats overlay, remove decode time and explain why it can't be measured

Adjust the refresh rate of the display to match the stream framerate and improve battery life

Don't use presentationTimeMs when creating CMSampleTimingInfo

Fix overlayView build for iOS

Refactor CADisplayLink callback for improved frame delivery. Support a user-configurable buffered frame count.

[opt] Use UILabel for stats overlay
2025-07-11 21:02:13 +08:00

89 lines
3.7 KiB
Plaintext

#pragma mark DisplayLink - RTP PTS timestamp-based frame pacing (experimental)
// TODO: handle presentationTimeUs rollover every 13 hours
// This frame pacing method attempts to use timestamps from Sunshine to determine when
// frames should be displayed. Sunshine v2025.600+ required.
- (void)framePacingUsingTimestamps:(CADisplayLink *)link {
// All times are in seconds
static CFTimeInterval anchorLocal = 0.0f;
static CMTime anchorHost;
static CFTimeInterval lastTargetLocal = 0.0f;
CFTimeInterval start = link.timestamp;
CFTimeInterval deadline = link.targetTimestamp;
_displayRefreshRate = 1.0f / (deadline - start);
[self checkDisplayLayer];
Frame *frame = nil;
CFTimeInterval dl0 = CACurrentMediaTime();
static CFTimeInterval avgOverhead = 0.004f; // averaged each callback
CFTimeInterval waitFor = deadline - dl0 - avgOverhead;
if (waitFor < 0.001f) {
waitFor = 0.001f;
}
// Process the next frame for display
frame = [_frameQueue dequeueWithTimeout:waitFor];
if (frame) {
CFTimeInterval now = CACurrentMediaTime();
// during stream startup, wait and let the first frames through untouched
if (!_seenFrameOne) {
Log(LOG_I, @"Frame pacing (pts mode): target %f Hz with %d FPS stream", _displayRefreshRate, self->frameRate);
_seenFrameOne = YES;
// Just let frame 1 through, we don't want to anchor to it
Log(LOG_I, @"[%.3f] rendering frame %d / %.3f, queue size %d",
deadline, frame.frameNumber, frame.pts, [_frameQueue count]);
[self renderFrame:frame atTime:frame.pts90];
return;
}
if (!CMTIME_IS_VALID(anchorHost)) {
// Use this frame as the anchor point. This is normally frame 2.
anchorHost = frame.pts90;
anchorLocal = now;
Log(LOG_I, @"Setting anchor point: anchorHost=%.3f == anchorLocal=%.3f",
CMTimeGetSeconds(anchorHost), anchorLocal);
}
//CMTime targetTime = CMTimeSubtract(frame.pts90, frameQueue.ptsCorrection); // total of all dropped frames
CMTime targetTime = frame.pts90;
CFTimeInterval targetLocal = CMTimeGetSeconds(targetTime);
#ifdef DISPLAYLINK_VERBOSE
// iOS double-buffering renders the frame we queue here during the interval +2 from now
CMTime hostDelta = CMTimeSubtract(frame.pts90, anchorHost);
CFTimeInterval hostDeltaF = CMTimeGetSeconds(hostDelta);
CMTime drift = CMTimeSubtract(hostDelta, CMTimeMakeWithSeconds(now - anchorLocal, NSEC_PER_SEC));
Log(LOG_I, @"[%.3f] rendering frame %d / %.3f @ %.3f, drift %.3f, queue size %d",
deadline - anchorLocal, frame.frameNumber, hostDeltaF, targetLocal,
CMTimeGetSeconds(drift), [_frameQueue count]);
#endif
// Schedule the frame at its original timestamp, adjusted by a correction factor (total duration of all dropped frames)
[self renderFrame:frame atTime:targetTime];
// Update metrics
if (lastTargetLocal != 0) {
[self->_callbacks observeFloat:PLOT_FRAMETIME value:(targetLocal - lastTargetLocal) * 1000.0];
}
lastTargetLocal = targetLocal;
// weighted moving average of how much time displayLink needs after dequeuing a frame.
// This is used to avoid overshooting a vsync by waiting too long.
const double alpha = 0.1f;
avgOverhead = ((CACurrentMediaTime() - dl0) * alpha) + (avgOverhead * (1.0 - alpha));
}
// are we returning too late?
CFTimeInterval dl2 = CACurrentMediaTime();
if (dl2 > deadline) {
Log(LOG_W, @"*** returned past deadline by %.3f ms (%f > %f)", (dl2 - deadline) * 1000.0, dl2, deadline);
}
}