Rubylight Download 7.0.3
On this page
  1. 1. Wire format
  2. 2. Limits and the block plan
  3. 3. Pacing, loss recovery, timing
  4. 4. Slices and quality
  5. 5. Encoder output and threading
  6. 6. Config and measurement gate
  7. 7. Expected gain
  8. 8. Kill criteria
  9. 9. Briefs (in order, one implementer each, ~100 turns)
  10. 10. Where the code differs from this design

B2 step 2: send each AMF slice as it arrives #

Rubylight docs › Engineering notes

Design for the host half of B2 step 2 in rubylight-3.0-plan.md. Status: 2a (core) is implemented; the host half (2b to 2f) is not. Section 10 lists where the code differs from this design. Step 1 passed (rust/release/fixtures/amf-unit-probe-7900xt.json): at 4K60 on the RX 7900 XT, AMF returns 4 or 8 HEVC or H.264 slices per frame, the first 3.6 to 4.5 ms before the whole frame would arrive in frame mode.

Goal: start sending a P-frame when its first slice comes out of QueryOutput, not after the last. Each slice (or group of slices) becomes one of Moonlight's FEC blocks, with its own parity. No client change, no PROTOCOL_VERSION change; stock Moonlight and Nightjar receive a normal multi-block frame. The client still decodes the whole frame (Nightjar's partial MediaCodec input is a separate client brief), so the only gain is network time overlapped with encode time.

Recommendation: go, behind video_slice_delivery = off by default, H.264 and HEVC P-frames only. AV1 is out (tile frames were 2.6x to 4.7x the target size in step 1, and the AV1 depacketizer uses lastPayloadLen).

1. Wire format #

Every video packet today (rust/core/src/packet.rs, frame_blocks): RTP header, then the NV header with flags (byte 24: 1 PIC_DATA, 4 SOF on packet 0 of the frame, 2 EOF on the last), multiFecFlags = 0x10, multiFecBlocks (byte 27: block << 4 | (blocks - 1) << 6), fecInfo = index << 12 | dataShards << 22 | pct << 4, streamPacketIndex = seq << 8, frameIndex. Block 0's first shard starts with the 8-byte frame header: [0] = 1, [1..3] host latency in 0.1 ms, [3] frame type (1 P, 2 IDR, 5 after invalidation), [4..6] last packet payload length, [6..8] critical flags.

What each field needs, and what we know when slice 1 arrives:

Field Depends on whole frame? Streamed value
frame type [3] no, known at submit always 1: IDR and after-invalidation frames are never streamed
latency [1..3] today capture-to-packetize of the whole frame capture to first unit polled; documented, it reads ~3 ms lower
last payload length [4..6] yes write the full shard size slice. Both clients ignore it for H.264/HEVC (common-c VideoDepacketizer.c ~908 uses it only for non-H.264/HEVC; Nightjar depacketizer.rs only for Codec::Opaque)
block count (byte 27 bits 6-7) yes, must be fixed at block 0 fixed per streamed frame, chosen before slice 1 (section 2)
block number, per-block dataShards, pct per block each block's own values
SOF/EOF flags, frame index, RTP timestamp no / last packet only per block, as today: SOF on the first and EOF on the last data shard of every block (the code, not only the frame's last packet)
total size yes not on the wire

Receivers, checked:

So the only new constraint is the data layout: a block may not end on a short shard. Blocks are contiguous shard ranges of the frame's byte stream; a slice's tail that does not fill a shard is carried into the next block, never padded. The frame on the wire is then byte-identical to today's except for where the block boundaries fall, and mid-frame zero padding never happens.

2. Limits and the block plan #

Protocol limits (all enforced in frame_blocks today): at most 4 blocks; data + parity <= 255 per block when pct > 0; up to 1023 data shards per block at 0%; at most 4092 shards per frame. slice = packet_size + 16 - 32 (1376 at the default packet size).

Plan per streamed frame, B = configured blocks (2 or 4), S = slices requested from AMF:

  1. Eligible only if: Encoded::frame_cap_bytes is Some(cap) (AMF MaxAUSize, auto = 2x per-frame budget), the frame is a P-frame, and ceil((cap + 8) / slice) <= B * 1023 (the 8-byte frame header counts). Otherwise the frame goes the whole-frame way (section 5).
  2. Block k < B - 1 is emitted when unit ceil((k + 1) * S / B) has arrived (with S = B, one slice per block). It contains every complete buffered shard minus a hold-back, and never more than 255 - parity(k) shards at the frame's FEC percentage (the rest is carried).
  3. Hold-back, so the frame can always finish: keep at least B - k - 1 complete shards buffered (each later block needs at least one), and emit only if ceil((cap - emitted_bytes) / slice) <= (B - k - 1) * 1023, i.e. the worst case remainder still fits the remaining blocks at 0%. If either fails, the unit just stays buffered.
  4. On the last unit, the remainder is split over the remaining blocks with the same fitting as conventional_fec: full percentage if it fits 255, otherwise lower the percentage per block, down to 0% at 1023 data shards (fec_limited counts it, as for whole frames today).
  5. If no block has left when the last unit arrives (small frame, or every earlier block was held back), the block count is still free: the frame is framed whole with conventional_fec, byte for byte as conventional_blocks would, with the exact last payload length. Once block 0 has left, at least B - k shards exist at the end by rule 3.

At 4K60 HEVC 80 Mbps a frame is ~167 KB (~122 shards), the cap ~243 shards: all 4 blocks fit at full FEC. At 500 Mbps HEVC the cap is ~1515 shards: rule 3 holds back until the remainder fits two blocks at 0%, so at most 2 blocks go early and the tail loses FEC only when the frame really is that big, as whole frames do today.

Overflow (a unit past cap, which MaxAUSize should make impossible): the frame cannot be completed in the announced block count. StreamedFrame::push fails with SliceOverflow (blocks already built stay sent, no more are built, and the frame index stays consumed even if block 0 had not left). The caller then logs warn! once per session, bump a slice_delivery_overflow counter in the stream stats, request a recovery frame through the existing EncoderRecovery path, and disable slice delivery for the rest of the session. Loud, never a silently broken stream. Unit test it with a fake cap.

3. Pacing, loss recovery, timing #

4. Slices and quality #

Today rust/core/src/encoder_policy.rs (~476-484) sets SlicesPerFrame/HevcSlicesPerFrame to the client's RTSP request, best effort, and output mode stays frame. rust/PERFORMANCE.md (~3420-3510) shows slices cost no encode time or bytes at our settings, but there is no quality data. Slices break intra prediction and entropy context at slice edges, so expect a small SSIM loss. With slice delivery on, set slices to max(client request, B) and OutputMode/HevcOutputMode = 1. 2f measures SSIM with rust/release/amf_sweep.py (slices 1 vs B at fixed bitrate); more than 0.002 SSIM loss is a kill (section 8).

5. Encoder output and threading #

Core code (rust/core/src/packet.rs): the per-block body of frame_blocks is VideoPacketizer::fill_block(&BlockFill, &mut PacketBlock), and both conventional_blocks and the new VideoPacketizer::streamed(timestamp, latency_us, blocks, slices, cap) -> StreamedFrame are built on it, with StreamedFrame::push(&mut VideoPacketizer, unit, last, &mut PacketBlock, emit: FnMut(&mut [Vec<u8>], StreamedBlock)). emit sends each block before push returns (the shard buffers are reused). StreamedBlock { index, data, parity } feeds SendLoss::push_block (the streamed counterpart of block_layout).

6. Config and measurement gate #

Config: video_slice_delivery = off | 2 | 4 (blocks), default off, in docs/configuration.md, not in the console (since 2026-11-01 auto is the default: four blocks for 4K-size pictures, off below; PERFORMANCE.md) until it passes the gate. Off means frame output mode and the exact current code path.

Tests (each brief adds its own; every one fails on today's code or guards a fallback):

Loss: add BUTTERPOLLO_TEST_SEND_LOSS=percent:seed[:air] beside SendOutage in rust/core/src/stream_policy.rs (seeded, per datagram, applied where SendOutage is in rust/windows/src/net.rs), with parse tests.

Gate runs, release build, A8 records (BUTTERPOLLO_TIMING_RECORD, rust/release/e2e.py --record, then rust/release/timing_report.py), A/B with off vs 4, same seed, at least 3000 frames each:

  1. Loopback C receiver (rust/tests/moonlight_client.c via e2e.py): 4K60 HEVC 80 Mbps and H.264, 0% and 1% loss.
  2. Paced Wi-Fi emulation on loopback: packet_pacing=160000.
  3. NUC over Ethernet (ssh nuc), then a phone on Wi-Fi with Nightjar and stock Moonlight (user runs the phone).

Pass: median end_to_end (picture age) improves by at least 1 ms in run 2 and run 3's Wi-Fi case; at 1% loss the frame-loss rate and p99 end_to_end are no worse than off within run-to-run noise; zero slice_delivery_overflow; no decoder errors in either client.

7. Expected gain #

With W = paced send time of the whole frame, E - E1 = time from first unit to last unit, N blocks: the frame finishes min(W * (N - 1) / N, E - E1) sooner.

These are estimates from step 1 numbers, not measurements; 2f measures them.

8. Kill criteria #

Stop and revert to off-only (remove the code) if any of these hold after 2f:

9. Briefs (in order, one implementer each, ~100 turns) #

10. Where the code differs from this design #

View this page on GitHub