srt-runtime 0.13.1

SRT (Secure Reliable Transport, draft-sharabayko-srt-01) packet codecs, sans-IO HSv5 handshakes, a one-connection link engine, ARQ, TSBPD, LiveCC/FileCC congestion control, optional payload crypto and an optional tokio UDP adapter; no_std core.
Documentation
//! SRT (Secure Reliable Transport) as specified by the Internet-Draft
//! `draft-sharabayko-srt-01`: byte-exact codecs for every data and control
//! packet, and sans-IO engines for the HSv5 Caller/Listener and Rendezvous
//! handshakes, the ARQ reliability loop (loss detection, NAK, ACK/ACKACK,
//! RTT), TSBPD delivery with too-late drop, and LiveCC and FileCC congestion
//! control. Behind features it adds the §6 payload encryption (AES-CTR, RFC 3394
//! key wrap, PBKDF2 key derivation, KM refresh), the one-connection
//! [`Link`] engine, and a `tokio` UDP adapter that has been checked against
//! a real libsrt 1.5.5 peer.
//!
//! The core is `no_std` + `alloc` and reads no clock: every engine takes the
//! caller's time (`tick`, `set_elapsed`, a `broadcast_core::stage::Instant`), and
//! every bound, timeout and cadence ([`arq::ArqBounds`], `io::IoConfig`,
//! `link::LinkCadence`) is the caller's. Packets the delivery engine gives up
//! on are reported as damage, coalesced per sequence run ([`damage`]); a
//! handshake that is rejected or never completes stays an error. It builds on
//! `broadcast-core`, `broadcast-common` and `broadcast-net`; `broadcast-client`
//! (the SRT caller source) and `multimux` (SRT input and push) build on it.
//!
//! # Choosing limits
//!
//! No limit struct here has a `Default`. Each one offers
//! `for_profile(broadcast_core::Profile)`, a complete preset whose every
//! number is a documented constant, and a typed `with_*` setter per field:
//!
//! | Profile | Sized for |
//! |---|---|
//! | `Embedded` | one program, a few hundred MB of memory, 32-bit or wasm32, few elementary streams (values checked against the 256 MiB budget where exercised, #4834) |
//! | `Device` | single-service playback on a phone, tablet, TV or desktop, about 30 s of read-ahead |
//! | `Server` | a live origin or HLS server carrying several programs |
//! | `Headend` | a whole multiplex with full SI/EPG, up to 2048 PIDs |
//!
//! The presets: [`arq::ArqBounds`], [`arq::ReceiverBounds`], [`arq::SenderBounds`],
//! [`io::ListenerLimits`], [`handshake_sm::HandshakeConfig`]. A preset delivers
//! a damaged unit flagged; dropping it is the caller's opt-in.
//!
//! # Quickstart
//!
//! Run a Caller/Listener handshake in memory: the bytes one side emits are
//! parsed and fed to the other, as a transport would.
//!
//! ```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));
//! ```
//!
//! # How do I...
//!
//! - Parse and re-serialize an SRT packet: [`SrtPacket`],
//!   [`packet::ControlPacket`], `examples/packet_round_trip.rs`.
//! - Run a Caller/Listener handshake without sockets: [`CallerHandshake`],
//!   [`ListenerHandshake`], [`HandshakeConfig`],
//!   `examples/handshake_loopback.rs`.
//! - Recover lost packets with NAK and retransmission: [`arq::Sender`],
//!   [`arq::Receiver`], `examples/arq_loss_recovery.rs`.
//! - Read an `srt://` URL (stream id, latency, mode, passphrase): [`SrtUrl`],
//!   `examples/parse_url.rs`.
//! - Connect over UDP with tokio: [`io::SrtListener`], [`io::SrtSocket`],
//!   [`io::SrtSocketConfig`], `examples/udp_loopback.rs`.
//! - Encrypt the payload with a passphrase: [`handshake_sm::CryptoConfig`],
//!   [`HandshakeConfig::with_crypto`], `examples/udp_loopback.rs`.
//! - Size the socket receive buffer: `broadcast_net::net::UdpOptions`,
//!   [`io::SrtSocket::recv_buffer_size`]; no example yet (the README section
//!   "Sizing the receive buffer" gives the rule).
//! - Connect two peers in Rendezvous mode: [`RendezvousHandshake`],
//!   [`RendezvousRole`]; no example yet.
//! - Drive one connection from your own socket loop: [`Link`], [`CallerLink`],
//!   [`Delivered`]; no example yet.
//! - Release data on its delivery time: [`tsbpd::TsbpdScheduler`]; no example
//!   yet.
//! - Pace a sender: [`livecc::LiveCC`], [`filecc::FileCc`]; no example yet.
//!
//! # Modules
//!
//! Wire format: [`packet`] (every data and control packet and handshake
//! extension), [`url`] (feature `std`, the `srt://` URL).
//!
//! Handshakes: [`handshake_sm`] (the shared configuration, outputs and
//! negotiated parameters), [`caller`], [`listener`], [`rendezvous`], [`cookie`]
//! (feature `link`, the Listener's SYN cookie).
//!
//! Reliability and timing: [`arq`] (sender, receiver, sequence arithmetic,
//! RTT), [`tsbpd`] (delivery scheduler), [`livecc`] and [`filecc`]
//! (congestion control), [`damage`] (drops as damage reports).
//!
//! Encryption (feature `crypto`): [`crypto`] (AES-CTR, key wrap, key
//! derivation), [`km_refresh`] (SEK rotation).
//!
//! Connections: [`link`] (feature `link`, one connection as a sans-IO engine),
//! [`io`] (feature `tokio`, the UDP adapter).
//!
//! Errors: [`error`].
//!
//! # Feature flags
//!
//! - `std` (default): the standard library and [`SrtUrl`]; without it the
//!   crate is `no_std` + `alloc`.
//! - `serde`: `Serialize` on the public wire types.
//! - `crypto`: payload encryption, the handshake key exchange and KM refresh.
//! - `link`: the [`Link`] and [`CallerLink`] engines and the SYN cookie;
//!   implies `crypto`.
//! - `tokio`: the [`io`] UDP adapter; implies `std` and `link`.
//!
//! # Spec citations
//!
//! `draft-sharabayko-srt-01` §3 (packet structure), §4.3.1 (Caller/Listener),
//! §4.3.2 (Rendezvous), §4.5 to §4.7 (TSBPD and too-late drop), §4.8 and §4.10
//! (ARQ and RTT), §5.1 (LiveCC), §5.2 (FileCC), §6 (encryption)
//! (`private/specs-verified/ietf/specs/ietf_draft_sharabayko_srt_01.txt`); RFC
//! 3394 (`private/specs-verified/ietf/srt-runtime/rfc3394-aes-key-wrap.md`);
//! RFC 8018 PBKDF2 (`private/specs-verified/ietf/srt-runtime/rfc8018-pbkdf2.md`).
#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![cfg_attr(docsrs, feature(doc_cfg))]

#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;

extern crate alloc;

/// Key lengths (bytes) AES key wrap accepts: AES-128/192/256 (FIPS 197).
pub(crate) const AES_KEY_LENS: [usize; 3] = [16, 24, 32];

pub mod arq;
pub mod caller;
#[cfg(feature = "link")]
#[cfg_attr(docsrs, doc(cfg(feature = "link")))]
pub mod cookie;
#[cfg(feature = "crypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "crypto")))]
pub mod crypto;
pub mod damage;
pub mod error;
pub mod filecc;
pub mod handshake_sm;
#[cfg(feature = "crypto")]
#[cfg_attr(docsrs, doc(cfg(feature = "crypto")))]
pub mod km_refresh;
#[cfg(feature = "link")]
#[cfg_attr(docsrs, doc(cfg(feature = "link")))]
pub mod link;
pub mod listener;
pub mod livecc;
pub mod packet;
pub mod presets;
pub mod rendezvous;
#[cfg(all(test, feature = "link"))]
mod test_support;
pub mod tsbpd;

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub mod url;

#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub mod io;

pub use caller::{CallerHandshake, CallerHandshakeState};
pub use error::{Error, Result};
pub use handshake_sm::{HandshakeConfig, HandshakeOutput, NegotiatedParams, RejectionReason};
#[cfg(feature = "link")]
#[cfg_attr(docsrs, doc(cfg(feature = "link")))]
pub use link::{
    CallerLink, CallerParams, Delivered, DeliveredItem, Link, LinkCounts, LinkEnd, LinkSetup,
    Payload,
};
pub use listener::{ListenerHandshake, ListenerHandshakeState};
pub use packet::SrtPacket;
pub use rendezvous::{RendezvousHandshake, RendezvousHandshakeState, RendezvousRole};
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub use url::SrtUrl;

/// The Internet-Draft this crate implements packet structure from.
pub const SPEC: &str = "draft-sharabayko-srt-01";