Skip to content

Latest commit

 

History

History
346 lines (302 loc) · 24.4 KB

protocol.md

File metadata and controls

346 lines (302 loc) · 24.4 KB

tilt wire protocol, version 1

This is the contract between the tilt server and any client: the bundled web page, a native WebView or a test probe. The server's implementation is src/protocol.rs and src/session.rs.

In short: fetch transport next to the page, open the WebTransport session it names, send {"t":"hello","v":1,"token":"…"} on a bidirectional stream, then receive H.264 frames (one per unidirectional stream; decode them with WebCodecs) and send ACKs, a PING every second, and mouse and key events back. The rest of this page is the exact contract. A client that wants the stream to stay smooth across a long, lossy path also speaks the datagram lane.

All binary integers are little-endian. Every message is either:

  • binary: the first byte is the message type;
  • text: a JSON object with a string field t.

1. Connecting

  1. Discovery. GET transport next to the page (/transport, or /<prefix>/transport) returns {"wt":{"host":"…","port":N,"hashes":["<sha-256 hex>"],"ticket":"…"}}: where the stream's UDP goes (host only when it isn't the page's own host; port by default the HTTP port), the certificate's hash, and a ticket.

  2. Session. Open https://<host>:<port>/stream?ticket=<ticket> with WebTransport, where host is the advert's host, else the page's host (127.0.0.1 for localhost: Chrome dials localhost over QUIC on ::1 only), with serverCertificateHashes: [{algorithm: 'sha-256', value: <hash bytes>}] for each of hashes. The certificate is self-signed ECDSA P-256, valid for 14 days (from a day before it was made) and replaced every 6 days, so fetch transport before each connection. A request without the right ticket gets HTTP 403: other sites' pages cannot read transport, so a session with the ticket comes from a page this server served. Unknown paths get 404.

  3. Control stream. The client opens one bidirectional stream. Every message except VIDEO travels on it, in both directions, as u32 length (LE) | u8 kind | payload, length counting the kind byte and the payload:

    kind payload
    0 a JSON (text) message, UTF-8
    1 a binary message (type byte first)
    2 close: u16 code (LE), then the UTF-8 reason; the codes of 1.10

    The server sends a kind-2 close before it ends a session, then finishes the stream and closes the QUIC connection once the client has acknowledged it. A client that reads the end of the stream or loses the connection without a close treats it as an abnormal close (1006) and reconnects. Messages longer than 64 KiB + 1 end the session.

  4. Video. Unless recovery is negotiated (below), each access unit arrives on its own server-opened unidirectional stream: one VIDEO message (section 2), then the end of the stream. Streams may complete out of order: decode in seq order, starting at 1. If a stream fails before its header was read, skip to the oldest seq that did arrive, ACK the last skipped seq (ACKs are cumulative) and request an IDR. VIDEO may arrive before the welcome on the control stream: hold it until then. A lost packet does not block cursor updates, PONGs, input or later frames' transfer, but ordered decoding waits for that frame. The server uses BBR congestion control; the ACK credit of section 3 decides how much video is in flight.

  5. Within 5 s the client sends, on the control stream, the text message {"t":"hello","v":1,"token":"<token>","client":"tilt-web/0.1"}. The token field may be omitted only in no-auth mode. Tokens never go in a URL; the web client reads them from the page's URL fragment (#token=). A client supporting the recovery format below adds "recovery":true. Without it the server sends only ordinary VIDEO streams. A client that speaks the datagram lane (below) adds "datagrams":1, and one that knows its display's refresh rate adds "fps":N: the session's frame rate cap is then the lower of N and --max-fps. Without fps it is 60, or --max-fps if that is lower. Older servers ignore these optional fields.

  6. If v != 1, the server replies {"t":"error","code":"version","msg":...} and closes.

  7. If the token is wrong, the server sleeps 500 ms, then sends {"t":"error","code":"auth"} and closes.

  8. There is no limit on viewers: any number of sessions may watch, and control (section 4) is what only one holds. If the token file is missing or holds no token yet, the server sends {"t":"error","code":"not_ready"} and closes. (Servers before 0.4 had a viewer limit and answered {"t":"error","code":"busy"}, closing with 4002; clients should retry then.)

  9. On success the server sends, in this order:

    • {"t":"welcome","v":1,"session":"s7","role":"control"|"view","screen":{"w":W,"h":H},"server":"tilt/0.1.0","resize":bool,"fps":N}, with "datagrams":true when the datagram lane is on for the session. fps is the session's frame rate cap (servers before 0.7 do not send it)
    • {"t":"control","you":bool,"held":bool,"holder":"s3"} (see section 4)
    • CURSOR_SHAPE and CURSOR_POS, if known
    • then video, starting with a KEY frame.
  10. Close codes:

code meaning
1000 normal; also sent after 30 s without any client message (reason idle)
1001 the server is shutting down
1008 more than 100 malformed messages
1011 server-side failure (the session's encoder thread could not start or stopped)
4001 auth: wrong or missing token, or the first message was not hello
4002 busy: only from servers before 0.4, which had a viewer limit
4003 version: v is not 1
4004 not_ready: the token file is missing or holds no token yet
4005 hello timeout: no hello within 5 s

Codes 1000, 1001, 1008 and 1011 follow their WebSocket meanings; clients should treat any close as "reconnect with backoff", except 4001 and 4003, which will not succeed on retry. A 4004 means the token file has not been written yet: retrying later is expected to work.

2. Server to client, binary

type name layout after the type byte
0x01 VIDEO u8 flags, u32 seq, u64 capture_us, u16 width, u16 height, payload… (18-byte header including the type)
0x02 CURSOR_SHAPE u32 serial, u16 w, u16 h, u16 xhot, u16 yhot, w*h*4 bytes RGBA (straight alpha); w=h=0 means hidden
0x03 CURSOR_POS i16 x, i16 y (screen pixels)
0x04 PONG u32 t (echo of PING.t)

VIDEO fields:

  • flags:
    • bit0 KEY: an IDR, with SPS+PPS present in the payload.
    • bit2 PATCH: the payload is a patch (below), not H.264.
  • seq: a per-session counter, starting at 1 and incremented once per access unit. It is a u32 and wraps (skipping 0).
  • capture_us: the server's monotonic µs at capture. It is informational.
  • width, height: the coded size, which is the even-cropped screen size.
  • payload: Annex B (00 00 00 01 start codes), one access unit per seq; or, with PATCH, a patch.
  • Codec: H.264 High profile with CABAC (--profile high, the default) or Constrained Baseline (--profile baseline). The SPS signals a level that covers the screen size at --max-fps and --max-bitrate-kbps, so it holds however the bitrate adapts: with the defaults, 1920×1080 is level 4.2 and 1280×720 or 1024×768 level 3.2. A WebCodecs client builds its codec string from the three bytes after the SPS NAL header (profile_idc, constraint flags, level_idc): avc1.640C2A for 1920×1080 with High, avc1.42C02A with Baseline. The level follows the screen size, so take it from every KEY frame's SPS rather than from the first.

Patches: a small change (a typed character, a blinking caret, a hover) is sent as the changed pixels instead of an H.264 frame, which would cost the server a full-screen encode. A patch is u16 count, then per rectangle u16 x, u16 y, u16 w, u16 h, u32 len and len bytes: the rectangle's I420 pixels (Y rows, then U rows, then V rows; x, y, w and h are even), run-length coded. In the code, a byte c < 128 is followed by c + 1 literal bytes, and c ≥ 128 by one byte that repeats c − 125 times. Runs can cross rows and planes. The pixels use the stream's colours (BT.709, limited range), so draw them as an I420 VideoFrame with that colour space. The decoder never sees a patch, and the next H.264 frame codes everything since the one before, patched areas included. A patch never comes before the session's first KEY frame.

  • Order: draw updates in seq order. A patch goes over the frame before it, so a patch that arrives while an earlier frame is still in the decoder waits for that frame. Once a frame is drawn, patches with a lower seq are covered by it and can be skipped.
  • ACK: a patch is acked once it is drawn (or skipped), in seq order like frames. decode_ms is the time from receipt to drawing.

Cursor messages: CURSOR_SHAPE is sent when the cursor image changes (new serial), CURSOR_POS when the pointer moves. Captured frames never contain the cursor; draw it from these. The shape is at most 128×128.

Optional recovery streams

With hello.recovery=true, a VIDEO may instead arrive on five independent reliable unidirectional streams. Each carries u8 0x05 | u32 seq | u32 length | u8 index | shard (LE). length is the original VIDEO length including its 18-byte header. Indices 0–3 hold consecutive quarters of that message, each ceil(length/4) bytes; the last is zero-padded. Index 4 is the bytewise XOR of indices 0–3. Any four distinct shards recover the original message; strip the padding, validate its type and seq against the envelope, then deliver it in normal seq order. ACK the VIDEO once, never individual shards. Ignore late/duplicate shards for completed frames. With two missing shards, wait for reliable delivery; never decode a partial frame. A failed recovery stream reconnects the session rather than treating that shard as a missing VIDEO.

The server protects frames of 4–128 KiB only, on paths with minimum RTT ≥80 ms and recent packet loss ≥0.2%. A transport RTT more than 15 ms above its minimum suppresses recovery for five seconds. Parity costs 25% of protected payload (rounded up), plus up to three padding bytes, 50 bytes of shard headers, and QUIC overhead. Ordinary VIDEO and recovery streams may be mixed in one session. The receiver bounds incomplete recovery groups to 32 and each reconstructed VIDEO to 128 KiB; malformed shards or exceeding the group limit reconnects. The web client's tilt:stats.client includes cumulative recoveryFrames and recoveredFrames; the latter counts reconstruction using parity, not necessarily a packet loss (shards can reorder). Existing video kbps statistics exclude recovery overhead. A session with the datagram lane gets no recovery streams: the lane does their job for frames of every size.

The datagram lane

A stream resends a lost packet about one round trip later, and frames decode in order, so across 250 ms of round trip every lost packet freezes the picture for a quarter of a second, and a lost packet of input holds back all the input behind it as long. The lane carries the same messages as WebTransport datagrams, which nothing resends, with enough repetition that a lost packet seldom matters. Everything on it is also, or in the end, on a stream: a client may ignore the lane, and a datagram that does not arrive costs time, never data.

The lane is on for a session when its hello had "datagrams":1, the connection takes datagrams, and so the welcome has "datagrams":true. A client sends no datagrams before that welcome.

VIDEO as datagrams. The server may send a VIDEO message as datagrams instead of a stream:

u8 0x06 | u32 seq | u32 length | u8 k | u8 index | shard (LE)

length is the VIDEO message's length including its 18-byte header, k the number of data shards, and every shard of a frame is ceil(length / k) bytes. Shards 0..k are consecutive pieces of the message, the last zero-padded; shards from k up are parity. Any k distinct shards rebuild the message; then check its type and seq against the envelope and deliver it in seq order like one that came on a stream. Drop a shard that makes no sense, a shard of a frame already complete, and frames that never complete: nothing on the lane ends the session.

The parity is Reed-Solomon over GF(2^8) with polynomial 0x11d. Parity shard k + j is, byte by byte, the sum over the data shards i of w(j, i) · data_i, with w(j, i) = x / (x + i) for x = 255 − j (field division, + being XOR). w(j, 0) is 1, so a frame of one shard has copies for parity. To rebuild, take the data shards that arrived out of as many parity shards as data shards are missing, and solve what is left for the missing ones (fecRebuild in web/transport.js, rebuild in src/fec.rs).

The server sends one parity shard per four data shards (rounded up), and more while a frame would otherwise fail to arrive whole more than once in ten thousand at the loss rate it plans for (the path's measured one and half as much again, 0.5% at least): a frame of four shards gets two at 0.5% and three at 3%. At most 192 data shards (about 220 KiB; larger frames go on a stream), and parity of at most half the data (48 shards; three for the smallest frames). It does so for every frame, on paths with a minimum round trip of 60 ms or more. All frames go back to streams while the link cannot carry the stream (frames wait for credit with the bitrate at its floor), and for 5 to 60 seconds when more than a tenth of the frames had to be sent again: such loss comes from a full queue, which parity does not mend. The copies of a one-shard frame follow it 4 ms apart.

HAVE (client to server, datagram only): u8 0x12 | u32 base | u64 bits (LE). Every VIDEO seq before base has arrived in full, by either route, and so has seq base + 1 + i for each set bit i (bit 0 the lowest); base itself has not. Send one whenever a frame completes, and the last one again 25 ms later. A frame sent as datagrams is sent again, as an ordinary VIDEO stream and as datagrams, once a HAVE reports a seq two or more past it whole while it is not; and as a stream alone when the server has not heard of it this way, nor in an ACK, two round trips and 50 ms after sending it. So a client that ignores FEC datagrams, or cannot send HAVE, still gets every frame, late.

ACK may also be sent as a datagram, in its 7 bytes of section 3, as well as on the control stream: ACKs are cumulative, so whichever arrives first counts and the other changes nothing. An ACK held up on the stream behind a lost packet would read as a queue on the path.

INPUT (client to server, datagram only): u8 0x13 | u32 first | { u8 len | message }… (LE). Each message is an input message of section 3 (MOVE to CLIPBOARD, at most 255 bytes) that the client also sends on the control stream, and first is the number of the first one, the rest counting up from it. Messages are numbered by their place on the control stream, counting every message there except ACK and PING, the hello being 1; JSON messages count but only travel on the stream. The server acts on each numbered message once, in order, from whichever of the datagram and the stream brings it first: a datagram's messages are used from the one numbered one past the last acted on, and a datagram that starts later than that is dropped. Put the newest messages in each datagram (the web client sends up to 16 of the last 500 ms, and repeats the last datagram 15 and 45 ms later), so that what a lost datagram carried, the next carries again.

The web client's tilt:stats.client has datagrams (the lane is on), cumulative fecFrames and fecRepaired (frames that arrived whole as datagrams, and those of them rebuilt with parity, which is not always a loss: a parity shard can overtake a data shard) and displayHz.

3. Client to server, binary

type name layout after the type byte
0x10 ACK u32 seq, u16 decode_ms — sent once per VIDEO seq when the decoder outputs it (or discards it), or for a patch when it is drawn; decode_ms = ms from receipt to output, saturating. Acks are cumulative: ACK(n) acks every seq ≤ n.
0x11 PING u32 t (client ms clock, wraps) — every 1 s while connected; doubles as the heartbeat
0x20 MOVE u16 x, u16 y — normalized: 0 = left/top edge pixel, 65535 = right/bottom edge pixel
0x21 BUTTON u8 button, u8 down, u16 x, u16 y — X button numbers 1 left, 2 middle, 3 right, 8 back, 9 forward
0x22 WHEEL i16 dx, i16 dy, u16 x, u16 y — discrete notches; +dy = down (button 5), −dy = up (4), +dx = right (7), −dx = left (6)
0x30 KEY u8 down, u32 keysym — a down for an already-down keysym means autorepeat
0x31 TEXT utf8 bytes… (rest of message) — type this text: '\n' → Return, '\t' → Tab, otherwise per-codepoint keysym. At most 4096 bytes are typed: the server cuts longer text at the last character boundary before that and drops the rest (with a warning in its log). Send longer text as several messages. It is typed literally: the Control, Alt, Meta, Super or Hyper keys this session holds are released for the whole text and pressed again after it, and its own Shift or AltGr is lifted for characters that must not have it. Losing control, disconnecting or RELEASE_ALL stops the rest.
0x32 RELEASE_ALL (no payload) — release everything this session holds, and stop the TEXT it is still typing along with everything the session sent after that TEXT
0x33 CLIPBOARD utf8 bytes… (rest of message, at most 65535 bytes) — make this text the X CLIPBOARD selection's content, tilt owning it, before any input sent after it runs: send it just before a paste key (Ctrl+V), so the application pastes it. Offered as UTF8_STRING, text/plain;charset=utf-8, TEXT and STRING (Latin-1, ? for other characters).

ACK decode_ms starts when the complete VIDEO is received (or reconstructed), before it waits for an earlier seq. Include that receiver reordering wait as well as decoding time, so it is not counted as network queue delay. Do not restart the timestamp for duplicate data.

Every fixed-size message must have exactly the listed length; TEXT and CLIPBOARD must be valid UTF-8. Anything else counts as malformed (see section 5).

Coordinate mapping, done on the server against the current screen size W×H:

  • px = round(x * (W-1) / 65535), and likewise for y.
  • The client computes x = round(fx * 65535), where fx = (clientX - rect.left) / rect.width, clamped to [0,1].

Input from a session that does not hold control is silently dropped, as is input from a view-role session. ACK and PING are always processed.

Flow control depends on ACKs: the server only has a few frames in flight per session (3 to 240, two round trips' worth at the session's frame rate) and encodes nothing more until they are acknowledged. A client that stops acking stops receiving video. When nothing has moved for 5 s, neither an ACK arrived nor VIDEO was written, the server forgets the frames in flight and goes on; after each such reset with no ACK since, the next wait doubles, up to 20 s. It sends no KEY frame for this (streams lose nothing): a client that needs one asks with {"t":"idr"}. A late ACK for a forgotten frame is harmless. Never drop a VIDEO seq without acking it. An ACK for a seq the server has not sent yet counts as malformed.

4. JSON (text) messages

Client to server:

  • {"t":"control","take":true|false}: take or release control. A view-role session gets {"t":"error","code":"forbidden"} and is not closed.
  • {"t":"idr"}: request a keyframe, for example after a decoder error. The server honours it subject to the 500 ms rate limit; a request that arrives during the limit is deferred, not dropped.
  • {"t":"resize","w":W,"h":H}: with --resize MIN, the viewer's window in CSS pixels. The screen takes that shape, scaled up to at least MIN on each side and down into the largest screen the X server allows, with even sides; screen and a KEY frame follow. Allowed when the welcome says "resize":true (control role, server started with --resize) and nobody else holds control; otherwise ignored, or forbidden for a session that may not send it. The web client sends it after its welcome and whenever its window has kept a new size for 300 ms.
  • {"t":"fps","fps":N}: the viewer's display refreshes N times a second, when that is not what hello said (the web client measures it from animation frames, and its first estimate, taken while the page loads, can be low). The session's frame rate cap becomes the lower of N and --max-fps, and a KEY frame follows a change. Only for a server whose welcome has fps.
  • {"t":"video","on":false|true}: pause or resume video, for example when the page is hidden. Pausing forgets the frames in flight (ACKs for them are still accepted, and teach the server nothing). Resume forces an IDR.

Server to client:

  • {"t":"control","you":bool,"held":bool,"holder":"s3"}: sent on any change. held is true if any session holds control, and holder is that session's id as its welcome gave it; the field is absent when nobody holds control. A client that lost its connection while holding control can tell its own lost session (which keeps control until the server closes it, within 30 s) from another viewer: it may take control back at once from the former, never from the latter. A server run never reuses a session id, and each run starts numbering at a random point, so a restarted server is unlikely (one chance in a million) to give a new session the id of a lost one.
  • {"t":"screen","w":W,"h":H}: sent when the screen size changed. A KEY frame at the new size follows. VIDEO width/height are the even-cropped values of W and H.
  • {"t":"stats","fps":f,"kbps":f,"bitrate_kbps":n,"rtt_ms":f,"min_rtt_ms":f,"queue_ms":f,"enc_ms":f,"inflight":n,"skipped":n,"viewers":n,"gov_fps":n,"qp":f,"patches":n}: sent every 1 s while video is on. fps, kbps and enc_ms cover the last second; skipped counts captures this session never encoded, over the whole session. gov_fps is the frame rate cap in force (--max-fps, or the viewer's display rate if lower); qp is the mean quantizer of the last second's frames, 0 when there were none. patches is how many of the last second's fps were patches; enc_ms and qp cover the H.264 frames only.
  • {"t":"clipboard","text":"…"}: an application on the display copied this text (the CLIPBOARD selection changed hands), sent only to the session holding control, as it happens. Only text that converts to UTF8_STRING in one go, at most 65535 bytes, is sent; other content (images, larger text) is not. Text CLIPBOARD set is not sent back.
  • {"t":"error","code":"auth"|"version"|"not_ready"|"forbidden"|"hello_timeout"|"unsupported_size","msg":"…"}. unsupported_size means the encoder cannot take the screen's size (smaller than 16×16, or more than 36864 macroblocks, about 4096×2304). The session stays open without video; when the screen changes to a size that works, screen and a KEY frame follow.

Unknown fields in client JSON are ignored. A repeated hello after the handshake is ignored.

5. Liveness

  • Client:
    • Send PING every 1 s.
    • Declare the link dead after 5 s without any server message, provided a PONG was expected.
    • Reconnect with jittered backoff of 250 ms, 500 ms, 1 s, 2 s, then capped at 5 s.
    • On reconnect, keep showing the last frame until the new session's first one.
  • Server:
    • If a controlling session sends nothing for 3 s while it holds keys or buttons, release them. The session stays open.
    • If a session sends nothing for 30 s, close it (1000, idle).
    • Close a session whose write blocks for more than 10 s.
    • QUIC keep-alive every 5 s; a peer silent for 25 s (vanished without closing) is dropped.
    • More than 100 malformed messages in one session close it (1008).

6. HTTP endpoints (TCP, same port)

path
GET / the web client (index.html); /app.js, /input.js, /transport.js, /keysyms.js alongside. Strong ETag, Cache-Control: no-cache, If-None-Match answered with 304.
GET /healthz ok, no auth
GET /api/status {"version","screen":{"w","h"},"viewers","controller":bool,"sessions":[{"id","role","control","fps","kbps","rtt_ms"}]}; needs Authorization: Bearer <control token> unless --no-auth (401 wrong token, 403 view token, 503 token file missing or empty)
GET /transport {"wt":{"host":"…","port":N,"hashes":["<sha-256 hex>"],"ticket":"…"}} (1.1; host only when set by --udp-public); no auth, Cache-Control: no-store