Timing record (3.0 A8, measure only) #
Rubylight docs › For developers
Design and implementer briefs for plan item A8. Nothing here changes how a frame is captured, encoded, sent, decoded or shown. No PROTOCOL_VERSION change; stock Moonlight sees no difference.
Section 2 is the reference for the record's format, the join and the report (rust/release/timing_report.py and code comments cite it). Sections 1, 3 and 4 record the design as decided on 2026-10-22: what was missing then, the implementer briefs, and the decisions. Performance has the measured baselines.
1. What exists and what is missing #
| Point | Where it is stamped today | Reaches the other side? | Gap |
|---|---|---|---|
| Game present | RTP timestamp from present_timing::Stamper (WGC + DXGI ETW refine), else the capture's present stamp (video_send.rs / pyrowave_send.rs, stamp) |
Yes: every packet; common-c presentationTimeUs = rtp × 1000 / 90 on the media clock |
None. The host record needs its µs value. |
| Capture present | Stamps::presented (DDA LastPresentTime, WGC SystemRelativeTime, or arrival) |
No | Absolute value not on the wire |
| Capture | Stamps::acquired |
No (folded into 0x5536 queue_us) |
Not split out |
| Hold/claim, convert | published, claimed |
No (in queue_us) |
Convert not split out |
| Encode submit | Stamps::submitted |
As present + queue_us |
Only relative to present |
| Encode complete | Stamps::encoded |
As + encode_us |
Only relative |
| First / last packet sent | first_sent, last_sent |
As host_us, send_us |
Only relative |
| PyroWave, all host points | pyrowave_send.rs carries no Claim |
No 0x5536 records and no stage samples | Missing |
| Client first packet | common-c / nightjar-core receiveTimeUs |
Nightjar CSV receive_ns |
None |
| Frame assembled | enqueueTimeUs |
Nightjar CSV enqueue_ns |
None |
| Decoder input / output | MediaCodecDecoderRenderer |
CSV decoder_input_ns, decoder_output_ns |
None |
| Present | OnFrameRenderedListener / release |
CSV render_ns, release_ns |
None |
| Clock offset | present_clock::ClockOffset fed by 0x5537 |
Overlay "host to present" only | Probes run only with host frame times on; offset never exported |
So the client stages already exist (FrameLatencyStats.CSV_HEADER). The missing parts are: absolute host times per frame, capture and convert split out, PyroWave host stamps, clock sync independent of VRR presentation, the offset in the record, a host-side record for receivers that cannot speak 0x5536/0x5537, and the report script. 0x5536 and its extension are in control-messages.md.
2. Record design #
Clock domains #
- Host media clock: µs since
media_epoch(set when video starts,stream.rs). 0x5537 replies and RTP timestamps use it. All host columns are on this clock. - Client clock: Nightjar uses
System.nanoTime()(CLOCK_MONOTONIC). The C probe already stamps CLOCK_MONOTONIC (android_control.c). Brief B checks that the Rust engine's probe uses the same base. - Join:
client = host + offset, so host times map tohost_us + offset. The report converts client times to the host clock.
0x5536 extension (rubylight-protocol, appended fields) #
The v1 report has a header (version, count, reserved, reserved) followed by count × 20 bytes. The current Rust and Java decoders check only len ≥ 4 + count × 20 and ignore the reserved bytes. Extension:
- Header byte 2 is
extra, the bytes per record in a trailing block. It is0in v1 and16now. The version stays1. - After the v1 records,
count × extrabytes follow. For each record:presented_at_usu64 (capture present on the media clock),capture_usu32 (presented to acquired),convert_usu32 (claimed to submitted). A decoder reads the first 16 bytes of each entry and skips the rest, so later fields can be appended. - It is valid only if
capture_us + convert_us ≤ queue_us. If not, the client drops the extension and keeps the base records. - An extended report carries at most 17 records (616 bytes,
HOST_TIMING_EXTENDED_MAX_RECORDSin the protocol crate). The Rust and Java decoders accept counts up to 32 and ignore trailing bytes, but older Nightjar builds' patched common-c (host-timing.patch) hands 0x5536 to Java only if the message is at most 644 bytes, and4 + 36 × Nexceeds that from N = 18. The current patch accepts up to 1156 bytes (4 + 36 × 32), as brief B step 1a asked. - The API is additive:
HostTimingExtra,HostTiming::encode_report_with(&[(HostTiming, HostTimingExtra)])anddecode_report_with(&[u8]) -> Option<Vec<(HostTiming, Option<HostTimingExtra>)>>.encode_report/decode_reportare unchanged, so existing callers keep compiling.
The client derives these from the extension: capture present = P, acquired = P + capture_us, submit = P + queue_us, encoded = submit + encode_us, last sent = P + host_us, first sent = last sent − send_us. Hold + claim = queue − capture − convert, and packetize + pacing = residual_us(). Game present comes from the frame's RTP µs, which the client already has.
Host-side record (opt-in) #
Set BUTTERPOLLO_TIMING_RECORD=<dir> on the host process. It is unset by default and has no UI. While unset, no thread and no file exist. When set, each video stream writes <dir>\host-<stream_id>.csv:
# rubylight timing record 1
# media_epoch_qpc_ms=<QPC ms at media_epoch, as clock_ms() in moonlight_client.c>
# codec=<h264|hevc|av1|pyrowave> capture=<wgc|ddx|dxgi|idd-push> present_source=<etw|capture>
wire_frame,rtp_us,present_us,acquired_us,published_us,claimed_us,submitted_us,encoded_us,packetized_us,prepared_us,first_sent_us,last_sent_us,first_unit_us,early_blocks
...
# dropped=<rows dropped because the writer fell behind>
capture is the stream's capture method (auto is recorded as wgc); present_source is etw when the ETW present tracker stamped the game's presents (WGC only), else capture. All values are integer µs on the media clock. first_unit_us and early_blocks are empty unless the frame was streamed (below); records written before they existed end at last_sent_us, and the report reads both. The send thread does a non-blocking try_send into a bounded channel (4096 rows) at the point where host_timings.record is called today. A writer thread does the file IO. A full channel drops rows and counts them, and never blocks the send thread.
Streamed frames (video_slice_delivery) #
A P-frame the host sends block by block while the encoder still produces it has its first blocks on the wire before its last slice exists. Its points are the same as for any frame, but first_sent_us can precede encoded_us:
encoded_usis when the encoder's last slice was in the host's hands, as for a whole frame, soencodeis the encoder's whole time.first_unit_usis when its first slice was, andearly_blocksis how many FEC blocks were built before the last slice arrived.packetized_usis when packetizing began on the first slice,prepared_usequalsfirst_sent_us(the first packet of block 0), andlast_sent_usis the last packet of the last block.- The host's own stages (
Stamps::stages, the 0x5536encode_usandsend_us, thestagesfigures in the status JSON) count a point that falls before the one preceding it as reached with it, so no stage is negative and they still add up to present to last packet. For a streamed frame the encoder's time ends atencoded_us,packetize,send_queueand the start ofsendare zero, andsendis the tail after the encoder:last_sent_usminusencoded_us.Stamps::overlap()isencoded_us - first_sent_us, the time the first block was on the wire before the encoder finished. timing_report.pyreports, for a frame withfirst_unit_us:encodeas above,packetize_pacefromfirst_unit_ustofirst_sent_us(the first block's packetizing and pacing, insideencode),overlap=encoded_us - first_sent_us(floored at zero; zero when nothing left early), andsendfrom the later offirst_sent_usandencoded_ustolast_sent_us.packetize_paceandoverlaplie insideencode, so a streamed frame's stages are not a partition ofhost_total. The overlap row appears only when a streamed frame is in the record.- Frames the host does not stream (keyframes, recovery frames, frames the plan cannot split) are joined and stamped as before.
Client record #
- Nightjar latency CSV (
FrameLatencyStats.CSV_HEADERon Android; Nightjar desktop writes the same file): nine columns followcallback_delay_ms, the existing ones keeping their order:rtp_host_us(game present),host_present_us,host_acquired_us,host_submitted_us,host_encoded_us,host_first_sent_us,host_last_sent_us,clock_offset_us,clock_rtt_us. Later columns, such as the present scheduler'ssched_vsync_nstodrop_reason, come after them, so read columns by name. Host columns are µs on the host clock. A column is empty when its source is missing (no extension, report lost, or no 0x5537). The offset and RTT are read once per CSV flush, outside the stats lock, for the newest exchange. Drift within one flush is about 50 µs/s at most. - Test receivers (
BUTTERPOLLO_TEST_TIMING_CSV,frames.csv):moonlight_client.cstays as it is. nightjar-cli appends the same nine columns after its existing ones.csv.DictReaderusers are unaffected.
Join and error bound #
The report script picks the join mode per file and prints it next to every result.
- clock_sync (Nightjar, nightjar-cli): per row,
offset = clock_offset_us.ClockOffsetuses the shortest-RTT exchange of the last ~8 s and carries it forward by the measured drift. Asymmetry error is at mostrtt_best / 2. The host's receive and send stamps and any socket delay lie inside the round trip, so the bound holds. Granularity is 1 µs. The report prints± rtt/2(the session's medianclock_rtt_us / 2) beside every cross-clock stage. Stages within one clock are exact. - shared_clock (loopback on this PC, C receiver): host and receiver both read QPC. The receiver's
first_packet_usandassembled_uscount from its own start (common-cPltGetMicroseconds), butarrival_msis absolute QPC: exactly the assembly time with a hardware decoder, the start of decoding (assembly plus the software queue) otherwise. The report rebases withbase_us = min over rows of (arrival_ms × 1000 − assembled_us)(the minimum strips the queue) and usesoffset = media_epoch_qpc_ms × 1000 − base_us, withclient = host + offsetas in mode 1. The bound is 0 up to the clock's granularity. Rows are joined bywire_frame. This mode is also the reference for checking that mode 1 stays within its bound. - min_owd (C receiver on another machine, for example the NUC under Linux):
offset = min over frames (first_packet − first_sent). Cross-clock stages then show the one-way delay above the minimum, and end-to-end is understated by the minimum one-way delay. The report labels these as lower bounds.
Stages reported (median, p95, p99, n, in ms) #
Host: game_to_present (RTP to capture present, ETW only), capture, hold_claim, convert, encode, packetize_pace (encoded to first sent), send (first to last packet), host_total (present to last sent). Cross-clock (±bound): network_first (first sent to first packet; the host stamps "first sent" when the first send call returns, so on loopback, where the packet arrives during that call, it is a few tens of microseconds negative), network_last (last sent to assembled), end_to_end (game present to client present). Client: assemble (first packet to assembled), client_queue (assembled to decoder input), decode, display (decoder output to present), client_total. The report also counts host rows without a client row (lost or never shown) and client rows without host data (report lost).
Cadence (B1; each is one row of n, median, p95, p99, and is left out of the table when it has no pairs): the series are taken in frame-number order and each frame is paired with the previous one that has the stamp. present_interval is the gap between the game's present times of adjacent frames, the content's own cadence (the host record's rows, so it includes frames the client lost; consecutive rows are paired, so a static repeat, which has no row but uses up a frame number, shows as one long interval). host_cadence_error is |(last_sent_i - last_sent_{i-1}) - (present_i - present_{i-1})|, the cadence the host adds; it is judged against the present stamp, or the RTP stamp when a row has none, and uses the host record's rows when there is one, otherwise the client file's host columns. client_present_interval is the gap between render_ns of adjacent shown frames and client_cadence_error is |render_ns gap - rtp_host_us gap| over consecutive shown frames. arrival_interval and arrival_cadence_error are the same two against enqueue_ns (assembled, before the decoder); they need no display, so they also work for a frames.csv. A frame that never showed (lost, or dropped by the client) is skipped: the error of a pair that spans it is kept, since the shown gap should still equal the content's gap, but the client's interval is reported for adjacent frame numbers only. A pair whose stamps are on different clocks (a present time against an RTP stamp) has no error. RTP stamps are differenced the short way round the 32-bit wrap. No clock offset is involved, so these rows have no bound. --skip-first SECONDS leaves the warm-up out of every row (the first frame whose time is that late is the cutoff on both sides, read from one clock column per file). rust/release/cadence_matrix.py runs the host's cadence across stream rates, codecs and the VRR hint with these rows. The percentiles are nearest-rank like the other rows, so p99 is meaningful from a few hundred pairs.
The 5 s stream timings line carries the capture's age when the stream takes it, from its acquire: capture_age_at_claim_ms (acquire to the pacer's claim) and capture_age_at_submit_ms (to the encoder's submit), each p50/p95/p99. They show whether the claim tick follows the content's frame phase: a fixed tick unrelated to it spreads the age across up to one content frame interval. The same two are acquire_to_claim and acquire_to_submit in the stage snapshot and the stages=trace picture line. cadence_matrix.py groups: core and codecs (a 90 Hz game family at 90/120/144 fps), sixty (60 Hz game, 60 fps) and surplus (120 and 240 Hz game, 60 fps).
What a moonlight-common-c receiver provides #
Stock common-c has no API to send 0x5536 or 0x5537, so rust/tests/moonlight_client.c gets no host stamps over the wire. Its frames.csv already has wire_frame, first_packet_us and assembled_us (its own clock), arrival_ms (absolute QPC or CLOCK_MONOTONIC ms, about assembled), decode_ms (decode starts in the callback, so there is no separate queue), render_qpc (test renderer only) and host_ms (the frame-header latency). Combined with the host-side record it gives every host stage, plus assemble and decode. With the shared clock it also gives exact cross-clock stages, and with min_owd lower bounds. It does not give MediaCodec queueing or display. Nightjar's patched common-c engine and its Rust engine both do speak 0x5536/0x5537.
3. Briefs #
Each brief runs in its own worktree. No git stash. LF endings. Commit messages go to a file (git commit -F). Push when the acceptance check passes and the reviewer has seen the diff.
Brief A: protocol extension and host stamps (implementer, ~90 turns) #
Owned: Nightjar protocol/core/src/control.rs (worktree from Nightjar origin/main). Rubylight rust/core/src/host_timing.rs, a new rust/core/src/timing_record.rs (pure row/format logic), rust/core/Cargo.toml (rev bump), rust/host/src/stream.rs (epoch QPC, sink creation), rust/host/src/video_send/transport.rs, the stamp site in video_send.rs, rust/host/src/pyrowave_send.rs and its transport.rs, docs/control-messages.md.
- In Nightjar protocol, add
HostTimingExtraand the additive encode/decode from §2, with the 17-record cap. Tests: round trip; the unchangeddecode_reportaccepts an extended report with the same base records; an extension withcapture+convert > queueis dropped and the base kept; a 616-byte cap (under the 644 of a full v1 report); trailing bytes beyondextra=16are skipped. Runcargo test -p rubylight-protocolinprotocol/. Push to Nightjar main. - In Rubylight, bump the
rubylight-protocolrev andcargo update -p rubylight-protocol. Readgit log <old>..<new> -- protocol/corefor anything else that changed. host_timing::timingalso returns the extra fromStampsplus the epoch.HostTimingsstores pairs andtake_reportusesencode_report_with.- PyroWave: carry the
ClaimthroughFrame/Preparedto the PyroWave transport, and at the last packet callrecord_stages,host_timings.recordand the record sink, asvideo_send/transport.rsdoes. First confirm from a host log that PyroWavestage_framesis 0 today. - Add the host-side record from §2. Keep the formatting in core (
timing_record.rs), and put the channel and writer thread in host. Take the epoch QPC withQueryPerformanceCounternext toInstant::now()atmedia_epoch. Recordrtp_usfrom the packetizer'sstamp. - Document 0x5536 in
docs/control-messages.md: a table row, a section with the request, v1 records and the extension, and the "17 records" rule.
Acceptance: pwsh -File C:\src\rl-quick.ps1 -Repo <wt> -Crates butterpollo-core,butterpollo -TargetDir C:\src\rl-target-a8a passes. New tests: extra values from fixed Stamps; a report with 24 extended records decodes in the old decoder; a PyroWave frame yields a 0x5536 record (pattern: video_send/tests.rs:1171); with the record sink unset no file is created; with it set the rows equal the Stamps points; with a stalled writer the send path returns at once and dropped counts. Report: commits on both repos, new rev, test names.
Brief B: Nightjar client record (implementer, ~100 turns; after A step 1) #
Owned (Nightjar worktree from origin/main): HostTimingReport.java, FrameLatencyStats.java, HostFrameClock.java, MediaCodecDecoderRenderer.java (CSV flush, probe start), MoonBridge.java/RustBridge.java (an offset getter), the native offset getter for both engines (android_present_clock.c, protocol/android), protocol/android/src/listener.rs, protocol/cli/src/main.rs, and their tests.
HostTimingReport.parsereads the extension, with the same validation as Rust and empty fields without it. 1a. Raise the 644-byte limit inapp/src/main/jni/moonlight-core/patches/host-timing.patch(length <= 644for 0x5536). The host keeps extended reports at 17 records (616 bytes) so that shipped Nightjar builds still receive them, but the patch should accept the full extended size,4 + 36 × 32, so the cap can be raised later.- The Rust engine path must keep the extension.
listener.rsre-encodes records withencode_reporttoday, which would drop it. Forward the raw payload or re-encode withencode_report_with, and add a test. - Clock sync probing is gated only on the host announcing 0x5537 (
hostSupportsControlMessage), not onprefs.hostFrameTimes. Scheduling (presentNs) stays behind host frame times. - Add
MoonBridge.presentClockOffset(long[] out)returning{offset_us, rtt_us}(false before an exchange) for both engines. Check the client base is CLOCK_MONOTONIC on both. - Append the nine columns to
CSV_HEADERandwriteCsv. Read the offset once per flush, before the stats lock is taken. - nightjar-cli: when 0x5536/0x5537 are announced, request host timing, probe 4 times a second, and append the same nine columns to the timing CSV.
Acceptance: .\gradlew.bat --no-daemon --max-workers=2 :app:testNonRootDebugUnitTest --tests '*HostTimingReportTest' --tests '*FrameLatencyStatsTest' --tests '*HostFrameClockTest' passes while iterating, and the full :app:testNonRootDebugUnitTest plus cargo test for protocol/android and protocol/cli pass at the end. New tests:
- the header keeps its old prefix, byte for byte;
- the new columns are filled from an extended report plus an offset;
- they are empty for a v1 report or a host without 0x5537;
- probes start with host frame times off while
presentNsreturnsreadyNsunchanged; - the Rust listener keeps the extension.
Host-neutral: gate only on HostCapabilities and the negotiated x-rl-control ids. Report: commit, test names, any base-clock finding.
Brief C: report script and harness glue (implementer, ~60 turns; can run alongside A and B) #
Owned: new rust/release/timing_report.py and rust/release/test_timing_report.py with small fixture CSVs under rust/release/fixtures/timing/, a --record flag in rust/release/e2e.py (sets BUTTERPOLLO_TIMING_RECORD=<case>/record for the isolated host and writes timing into result.json), and the CI step in .github/workflows/rust-windows.yml next to test_release.py.
- Input adapters:
- a Nightjar latency CSV;
frames.csv(C receiver or nightjar-cli) plus an optional host record. The join mode is chosen as in §2: clock columns present means clock_sync; a host record with QPC arrival means shared_clock; otherwise min_owd, or--joinforces one.
- Output: a stage table (median, p95, p99, n, ms; the bound beside cross-clock stages; join mode; unmatched counts) and
--json PATH. Usesoak_result.percentile(nearest-rank, floor) so the numbers match the other release reports. - With both a clock_sync file and the shared QPC available (loopback nightjar-cli),
--check-boundfails if|offset − shared offset| > rtt/2on more than 1% of rows.
Acceptance: python -m unittest discover -s rust/release -p test_timing_report.py -v and the existing test_release.py pass. The fixtures cover all three join modes, missing columns, wrapped RTP, and unmatched rows. Report: files, sample table from a fixture.
Brief D: baselines (runner, ~60 turns; after A–C are on main) #
Exit code 3 means an AMD encoder hang: stop and report, never retry. Use a release package from build.ps1 -SkipTests -Package.
- Loopback on this PC:
e2e.py --recordwith the C receiver for H.264, HEVC and AV1, and with the PyroWave receiver. Use 1920x1080x60, the e2e default bitrate, 60 s, and 3 runs each. Repeat H.264 with nightjar-cli plus--check-bound. - NUC (
ssh nuc, Dockerbutterpollo-test-client:20261005, sources mounted as in~/butterpollo-tests-20261015-pacing): run the same matrix against an isolated host (--config-dir C:\tests\a8 --port 48123 --bind <LAN IP>,virtual_display_layout = extended). Use nightjar-cli where it builds there and the C receiver with min_owd otherwise. - Overhead A/B: H.264 loopback with and without
--record.present_to_sent_p99_msfrom the 5 sstream timingslines must not get worse beyond run-to-run noise.
Report: one stage table per codec and client, with the join mode and bound, plus the A/B numbers, raw file paths and anything that failed. Mark the phone runs (Nightjar MediaCodec stages) as pending for the user. Main session writes them into rust/PERFORMANCE.md and the plan status table.
4. Decisions #
Decided 2026-10-22. Revisit if the user says so.
- Nightjar always sends clock sync probes when the host announces 0x5537 (4 packets of 16 bytes a second). No setting.
- The host-side record is an env var only (
BUTTERPOLLO_TIMING_RECORD). It gets a config key only if a user-facing need comes up. - On the NUC, if nightjar-cli doesn't build in the test image, those baselines report the network and end-to-end stages as lower bounds and say so.