[domistream] RTP and FEC API: RTP header parser, Reed-Solomon over GF(2^8)

This commit is contained in:
2026-11-25 11:47:42 +01:00
parent c01f980292
commit e7c69ac956
3 changed files with 130 additions and 0 deletions
+95
View File
@@ -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<usize> {
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<u8>,
}
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<Self> {
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<Self> {
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<u8>]) -> 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<Vec<u8>>]) -> crate::Result<()> {
let _ = shards;
Err(crate::Error::Unimplemented)
}
}
+2
View File
@@ -39,10 +39,12 @@ pub mod control;
pub mod enet; pub mod enet;
pub mod entropy; pub mod entropy;
pub mod error; pub mod error;
pub mod fec;
pub mod http; pub mod http;
pub mod input; pub mod input;
pub mod keys; pub mod keys;
pub mod pairing; pub mod pairing;
pub mod rtp;
pub mod rtsp; pub mod rtsp;
pub mod time; pub mod time;
+33
View File
@@ -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<RtpHeader> {
let _ = packet;
Err(crate::Error::Unimplemented)
}