# srt-runtime
[](https://crates.io/crates/srt-runtime)
[](https://docs.rs/srt-runtime)
SRT (Secure Reliable Transport): packet codecs plus sans-IO handshake, ARQ, delivery-timing and congestion-control engines, with optional payload encryption and an optional `tokio` UDP adapter. Grounded in `draft-sharabayko-srt-01`.
The [crate-root guide](https://docs.rs/srt-runtime) has the quickstart, the `How do I...` task index with an example for each task, the module map and the feature flags.
```toml
[dependencies]
srt-runtime = "0.13"
# srt-runtime = { version = "0.13", features = ["tokio", "crypto"] }
```
## What it does / what it does not
**Does:** parse and serialize every SRT packet type byte-exactly; run the HSv5 Caller/Listener and Rendezvous handshakes as state machines; run the ARQ reliability engine (loss detection, NAK, ACK/ACKACK, RTT), the TSBPD delivery scheduler (timestamp-based ordered release and too-late drop, with `SocketStats::too_late_dropped` counting the sequence numbers it gave up on), LiveCC pacing and FileCC window congestion control; and, behind features, do the AES-CTR payload crypto with key wrap and KM refresh, and drive all of it over real UDP sockets. The core is `no_std` + `alloc` and reads no clock: you call `tick()` and pass time in.
**Does not:** wrap an MPEG-TS stream or choose a payload for you, implement the legacy Version-4 Rendezvous path (permanently out of scope), or implement CUBIC/BBR file-transfer congestion control. The sans-IO congestion controllers are not wired into `arq::Sender` (it has no CC hook), and the tokio adapter now encrypts and decrypts in its data path (AES-CTR per draft section 6, with KM refresh), accepting a `HandshakeConfig` with `crypto` set for AES-128/192/256 on the Caller, Listener and Rendezvous paths. Encryption is negotiated end-to-end and verified against a real libsrt peer, including in Rendezvous mode.
Typical users: an SRT ingest or contribution endpoint (`multimux` builds its SRT input and push on this crate), test peers, and tooling that needs to inspect SRT packets.
## Standards supported
| Standard | Edition | Scope | Status | Notes |
|---|---|---|---|---|
| SRT protocol, IETF Internet-Draft `draft-sharabayko-srt` | -01 (private repo: `private/specs-verified/ietf/specs/ietf_draft_sharabayko_srt_01.txt`; rules in `private/specs-verified/rules/srt-*.md`) | §3 packet structure (data, every control type, handshake extensions, Key Material, Stream ID, Group Membership); §4.3.1 Caller/Listener; §4.3.2 Rendezvous (HSv5); §4.5-§4.7 TSBPD and too-late drop; §4.8, §4.10 ARQ and RTT; §5.1 LiveCC; §5.2 FileCC; §6 encryption | Partial | A draft, not a ratified RFC. Omissions: V4 rendezvous, CUBIC/BBR, CC wired into the sender |
| RFC 3394 AES Key Wrap | RFC 3394 (private/specs-verified/ietf/srt-runtime/rfc3394-aes-key-wrap.md) | Wrapping the SEK | Full, behind `crypto` | Verified against RFC 3394 §4 vectors (tests/it/rfc_vectors.rs) |
| RFC 8018 PBKDF2 (HMAC-SHA1) | private/specs-verified/ietf/srt-runtime/rfc8018-pbkdf2.md (PBKDF2) + rfc6070-pbkdf2-hmac-sha1-vectors.md (vectors) | KEK derivation from a passphrase | Full, behind `crypto` | Verified against RFC 6070 vectors (tests/it/rfc_vectors.rs); SRT uses LSB(64, Salt), 2048 iterations |
| libsrt 1.5.5 (reference implementation) | Used as an interop oracle, not a standard | Handshake, key exchange, loss recovery, rendezvous, MPEG-TS media | Verified | `tests/libsrt_*.rs`, see below |
## Features
| Feature | Default | Description |
|---|---|---|
| `std` | yes | Links the standard library. Without it the crate is `#![no_std]` and needs only `alloc`; the packet codecs and sans-IO engines are fully `no_std`. Also adds `url::SrtUrl`, the typed `srt://` URL (host/port, `streamid`, `latency`, `mode`, `passphrase`), which parses with the `url` crate. |
| `serde` | no | `Serialize` on the public wire types. |
| `crypto` | no | `crypto` (AES-CTR, RFC 3394 key wrap, PBKDF2/HMAC-SHA1), `handshake_sm::CryptoConfig` key exchange in the handshake, `km_refresh::KmRefreshDriver` (§6.1.6). Std-only dependencies; secrets are wiped on drop and compared in constant time. |
| `tokio` | no | `io::SrtSocket` and `io::SrtListener` over UDP (implies `std` and `link`): each connection sleeps until its own next deadline, datagrams are `bytes::Bytes`, `io::IoConfig` sets timeouts and `max_datagram`, rendezvous via `SrtSocket::rendezvous`, socket options (`SO_RCVBUF`, reuse) from the config's `UdpOptions` and the granted receive buffer from `recv_buffer_size`. Randomised ISN and socket id. |
| `link` | no | The sans-IO one-connection engine: `Link`, `CallerLink`, `LinkSetup`, `Delivered`, `LinkCounts`, `LinkEnd` and `cookie`. Implies `crypto`; `tokio` implies `link`. |
## Quick start
Parse a real `libsrt` KEEPALIVE (captured from `srt-live-transmit` 1.5.5) and write it back byte-for-byte:
```rust
use srt_runtime::packet::{ControlPacket, SrtPacket};
let bytes = std::fs::read("../fixtures/srt/libsrt-keepalive.bin").unwrap();
let packet = SrtPacket::parse(&bytes).unwrap();
assert!(matches!(packet, SrtPacket::Control(ControlPacket::KeepAlive(_))));
let mut out = vec![0u8; packet.serialized_len()];
packet.serialize_into(&mut out).unwrap();
assert_eq!(out, bytes);
```
Run a Caller/Listener handshake in memory (no sockets; the bytes one side emits are parsed and fed to the other):
```rust
use core::time::Duration;
use srt_runtime::HandshakeConfig;
use srt_runtime::caller::{CallerHandshake, CallerHandshakeState};
use srt_runtime::handshake_sm::HandshakeOutput;
use srt_runtime::listener::{ListenerHandshake, ListenerHandshakeState};
use srt_runtime::packet::ControlPacket;
fn first_send(outputs: Vec<HandshakeOutput>) -> Option<Vec<u8>> {
outputs.into_iter().find_map(|o| match o {
HandshakeOutput::Send(bytes) => Some(bytes),
_ => None,
})
}
let mut caller = CallerHandshake::new(
0x1111_1111,
HandshakeConfig::new(
Duration::from_millis(120),
1500,
8192,
Duration::from_millis(250),
12,
)
.with_latency(Duration::from_millis(180))
.with_stream_id(Some("live/camera-1".into())),
);
// A real listener derives the SYN cookie from the peer address and a secret.
let mut listener = ListenerHandshake::new(
0x2222_2222,
0xC0FF_EE42,
HandshakeConfig::new(
Duration::from_millis(120),
1500,
8192,
Duration::from_millis(250),
12,
).with_latency(Duration::from_millis(250)),
);
let mut to_listener = caller.start().unwrap();
for _ in 0..10 {
let pkt = ControlPacket::parse(&to_listener).unwrap();
let Some(reply) = first_send(listener.feed(&pkt).unwrap()) else { break };
let pkt = ControlPacket::parse(&reply).unwrap();
match first_send(caller.feed(&pkt).unwrap()) {
Some(next) => to_listener = next,
None => break,
}
}
assert_eq!(caller.state(), CallerHandshakeState::Connected);
assert_eq!(listener.state(), ListenerHandshakeState::Connected);
// Both sides agree on the greater of the two latencies.
assert_eq!(caller.negotiated().unwrap().latency, Duration::from_millis(250));
```
Runnable programs: `packet_round_trip`, `handshake_loopback`, `arq_loss_recovery`, `parse_url` and, with `--features tokio`, `udp_loopback` (`cargo run -p srt-runtime --example <name>`).
## Sizing the receive buffer
With the `tokio` feature every `io::SrtSocketConfig` carries a required `broadcast_net::net::UdpOptions` (`SrtSocketConfig::new(handshake, io, udp)`), applied to every socket the adapter binds (`SrtListener::bind`, `SrtSocket::connect`, `connect_from`, `rendezvous`). The library chooses no size: `UdpOptions::new()` says, explicitly, "leave every option at the system's value". A socket you built yourself goes in through `SrtSocket::connect_from_socket` or `rendezvous_from_socket` and keeps the options you gave it.
The kernel holds datagrams in the socket's receive buffer until the adapter reads them, and a datagram that arrives to a full buffer is dropped before this crate sees it. A drop at the tail of a burst cannot even be reported as a gap until a later packet arrives. The buffer therefore has to hold everything that can arrive while the application is not reading:
```text
receive buffer >= bitrate (bytes per second) x longest time the reader can be late (seconds)
```
then add headroom for the per-datagram overhead the kernel accounts against the buffer (a small datagram is charged for more than its payload), and for retransmissions arriving on top of the live stream. The longest lateness is the application's own figure: a scheduler pause, a slow consumer or a garbage-collection style stall. Measure it rather than assume it.
The kernel may grant less than was asked (it clamps to a system maximum) or more (Linux doubles the request for its own bookkeeping), so read back the result with `SrtListener::recv_buffer_size` or `SrtSocket::recv_buffer_size` and log it at start-up. If the granted size is below the figure you computed, raise the system maximum (`net.core.rmem_max` on Linux, `kern.ipc.maxsockbuf` on macOS) rather than lowering the bitrate.
## What is implemented
- **`packet`:** `SrtPacket::{Data, Control}`; `DataPacket` (sequence, message number, position, order, retransmit and encryption flags); every `ControlPacket` (Handshake with Handshake Extension, Key Material, Stream ID and Group Membership extensions; KeepAlive; ACK full, small and light; NAK with loss-list coding; Congestion Warning; Shutdown; ACKACK; Message Drop Request; Peer Error). Reserved and fixed-value fields are validated on parse and rebuilt on serialize.
- **Handshakes:** `CallerHandshake`, `ListenerHandshake`, `RendezvousHandshake` (cookie contest, Initiator or Responder at runtime), shared `HandshakeConfig`, `NegotiatedParams`, `HandshakeOutput`, `RejectionReason`; each gains `set_elapsed(Duration)` so the handshake machines read no clock, and the driver stamps the elapsed time before each start/feed/tick.
- **`arq`:** `Sender`, `Receiver`, wrap-safe `seq` arithmetic (over `broadcast_common::modular::Modulus`), `rtt::RttEstimator`.
- **`tsbpd`:** `TsbpdScheduler`. **`livecc`:** `LiveCC` and `MaxBwConfig` (MAXBW_SET, INPUTBW_SET, INPUTBW_ESTIMATED). **`filecc`:** `FileCc` (Slow Start then Congestion Avoidance).
- **`crypto`, `km_refresh`** (feature `crypto`), **`link` and `cookie`** (feature `link`) and **`io`** (feature `tokio`) as in the features table. `Link` and `CallerLink` drive one established connection from caller datagrams and `broadcast_core::stage::Instant`, returning `Delivered` payloads and damage and the datagrams to send; `LinkCounts` totals what was unparseable, refused, undecryptable, late, duplicate or lost. The tokio adapter is verified against a real libsrt peer including rendezvous.
## Damage reports
The delivery engine reports every packet it gave up on as `SrtEvent::Damage`,
coalesced per contiguous sequence run, beside the `SocketStats` counters it
already kept.
Reported: a packet TSBPD delivered too late (or the `TLPKTDROP` sequence it
walks past), a sender `DROPREQ` range, a peer `PeerError`, a packet outside the
flow window, an encrypted packet with no key installed (or an AES-CTR failure),
and a negotiated latency raised to the protocol floor, and packets still
unacknowledged when the caller's close-linger bound expires.
A handshake rejection or a handshake that never completes stays an error: the
peer cannot be connected to.
## Design notes
Sans-IO and clock-free (`tick()` and caller-supplied time), `#![forbid(unsafe_code)]`, `no_std` core, errors as structured `Error` (never a panic on hostile input; a `no_panic` test sweeps the parsers), `#[non_exhaustive]` enums, labelled spec enums. The tokio adapter bounds every IO and cancels its background tasks when the listener and all accepted connections drop.
## Testing against libsrt
The `tests/libsrt_*.rs` files use a real `srt-live-transmit` as an independent oracle for handshake, key exchange, loss recovery, rendezvous, shutdown and MPEG-TS media checked with `ffprobe`. A missing tool makes a test skip loudly (a no-op pass, not coverage). Set `SRT_REQUIRE_LIBSRT=1` to make every skip a failure, for example in CI:
```sh
SRT_REQUIRE_LIBSRT=1 cargo test -p srt-runtime --all-features --locked
```
## Related crates
Depends on `broadcast-common`, `broadcast-core` and `broadcast-net`. `multimux` uses it for SRT input and push. `rist-runtime` is the architectural sibling (the same ARQ-over-UDP idea for RIST). `media-plane` carries the received bytes onward.
## MSRV and licence
MSRV 1.95.0. Licensed under MIT OR Apache-2.0.