From e7c69ac95637005ea45efd6d00b100c1ebee8a93 Mon Sep 17 00:00:00 2001 From: mnovak Date: Wed, 25 Nov 2026 11:47:42 +0100 Subject: [PATCH] [domistream] RTP and FEC API: RTP header parser, Reed-Solomon over GF(2^8) --- crates/domistream/src/fec.rs | 95 ++++++++++++++++++++++++++++++++++++ crates/domistream/src/lib.rs | 2 + crates/domistream/src/rtp.rs | 33 +++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 crates/domistream/src/fec.rs create mode 100644 crates/domistream/src/rtp.rs diff --git a/crates/domistream/src/fec.rs b/crates/domistream/src/fec.rs new file mode 100644 index 0000000..9049eec --- /dev/null +++ b/crates/domistream/src/fec.rs @@ -0,0 +1,95 @@ +//! Reed-Solomon erasure coding over GF(2^8), polynomial `0x11D` (`docs/PROTOCOL.md` 5.3, 6.4). +//! +//! Systematic code: `data` shards pass through unchanged, `parity` shards are linear combinations of them, and any +//! `data` of the `data + parity` shards rebuild the rest. Every byte position is coded on its own, so all shards of +//! a block have the same length. +//! +//! Two matrices are needed: video (`ReedSolomon::new`, construction UNKNOWN U14) and audio (a fixed 2 × 4 parity +//! matrix, UNKNOWN U16, passed in with [`ReedSolomon::with_parity_matrix`] until it is measured). + +/// Maximum shards in one block (data + parity). +pub const MAX_SHARDS: usize = 255; +/// Maximum length of one shard, in bytes. +pub const MAX_SHARD_BYTES: usize = 4096; + +/// Parity shards for `data_shards` at `fec_percentage`: `ceil(data × percentage / 100)`. +/// +/// # Errors +/// [`crate::Error::UnexpectedValue`] when `data_shards` is 0 or the result would exceed [`MAX_SHARDS`]. Phase A: +/// always [`crate::Error::Unimplemented`]. +pub fn parity_count(data_shards: usize, fec_percentage: u8) -> crate::Result { + let _ = (data_shards, fec_percentage); + Err(crate::Error::Unimplemented) +} + +/// One code: shard counts and its parity matrix. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ReedSolomon { + data_shards: usize, + parity_shards: usize, + /// `parity_shards × data_shards` coefficients, row-major. + parity_matrix: Vec, +} + +impl ReedSolomon { + /// The code GameStream video uses for `data_shards` + `parity_shards` (UNKNOWN U14: construction). + /// + /// # Errors + /// [`crate::Error::UnexpectedValue`] when either count is 0 or the sum exceeds [`MAX_SHARDS`]. Phase A: always + /// [`crate::Error::Unimplemented`]. + pub fn new(data_shards: usize, parity_shards: usize) -> crate::Result { + let _ = (data_shards, parity_shards); + Err(crate::Error::Unimplemented) + } + + /// A code with an explicit parity matrix (`parity_shards × data_shards`, row-major). The matrix must make every + /// square submatrix of `[I; P]` invertible, or some loss patterns cannot be rebuilt; this is checked. + /// + /// # Errors + /// As [`Self::new`]; [`crate::Error::Malformed`] for a matrix of the wrong size or one that is not MDS. + pub fn with_parity_matrix(data_shards: usize, parity_shards: usize, parity_matrix: &[u8]) -> crate::Result { + let _ = (data_shards, parity_shards, parity_matrix); + Err(crate::Error::Unimplemented) + } + + /// Number of data shards. + #[must_use] + pub const fn data_shards(&self) -> usize { + self.data_shards + } + + /// Number of parity shards. + #[must_use] + pub const fn parity_shards(&self) -> usize { + self.parity_shards + } + + /// The parity matrix, row-major. + #[must_use] + pub fn parity_matrix(&self) -> &[u8] { + &self.parity_matrix + } + + /// Computes the parity shards from the data shards. `shards` holds `data + parity` buffers of equal length; + /// the parity ones are overwritten. + /// + /// # Errors + /// [`crate::Error::Malformed`] for the wrong shard count or unequal lengths, [`crate::Error::TooLarge`] over + /// [`MAX_SHARD_BYTES`]. Phase A: always [`crate::Error::Unimplemented`]. + pub fn encode(&self, shards: &mut [Vec]) -> crate::Result<()> { + let _ = shards; + Err(crate::Error::Unimplemented) + } + + /// Rebuilds missing shards in place. `shards` holds `data + parity` entries, `None` for lost ones; on success + /// every entry is `Some`. + /// + /// # Errors + /// [`crate::Error::Malformed`] for the wrong count or unequal lengths, [`crate::Error::TooLarge`] over + /// [`MAX_SHARD_BYTES`], [`crate::Error::Truncated`] when fewer than `data` shards are present. Phase A: always + /// [`crate::Error::Unimplemented`]. + pub fn reconstruct(&self, shards: &mut [Option>]) -> crate::Result<()> { + let _ = shards; + Err(crate::Error::Unimplemented) + } +} diff --git a/crates/domistream/src/lib.rs b/crates/domistream/src/lib.rs index bdee7f1..a37c452 100644 --- a/crates/domistream/src/lib.rs +++ b/crates/domistream/src/lib.rs @@ -39,10 +39,12 @@ pub mod control; pub mod enet; pub mod entropy; pub mod error; +pub mod fec; pub mod http; pub mod input; pub mod keys; pub mod pairing; +pub mod rtp; pub mod rtsp; pub mod time; diff --git a/crates/domistream/src/rtp.rs b/crates/domistream/src/rtp.rs new file mode 100644 index 0000000..bf16a00 --- /dev/null +++ b/crates/domistream/src/rtp.rs @@ -0,0 +1,33 @@ +//! The 12-byte RTP header shared by video and audio ([RFC 3550] §5.1, `docs/PROTOCOL.md` 5.2, 6.2). +//! +//! [RFC 3550]: https://www.rfc-editor.org/rfc/rfc3550 + +/// Length of the fixed RTP header. +pub const RTP_HEADER_LEN: usize = 12; + +/// The fixed RTP header. Fields are big-endian on the wire. The first byte is kept raw (version 2 plus flags: +/// `0x90` for video, `0x80` for audio). +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub struct RtpHeader { + /// Byte 0: version, padding, extension, CSRC count. + pub flags: u8, + /// Byte 1: payload type (video 0, audio 97, audio parity 127). + pub payload_type: u8, + /// Sequence number. + pub sequence: u16, + /// Timestamp. + pub timestamp: u32, + /// SSRC. + pub ssrc: u32, +} + +/// Parses the first [`RTP_HEADER_LEN`] bytes of `packet` (the rest is ignored here). Refuses a version other +/// than 2. +/// +/// # Errors +/// [`crate::Error::Truncated`] for fewer than 12 bytes, [`crate::Error::UnexpectedValue`] for another RTP version. +/// Phase A: always [`crate::Error::Unimplemented`]. +pub fn parse_rtp_header(packet: &[u8]) -> crate::Result { + let _ = packet; + Err(crate::Error::Unimplemented) +}