[domistream] RTSP API: message codec, stream framer, SDP, negotiation state machine

This commit is contained in:
2026-11-25 11:46:10 +01:00
parent 736a8f42b0
commit 024fe14ca6
4 changed files with 427 additions and 0 deletions
+126
View File
@@ -0,0 +1,126 @@
//! The SDP-like payloads of DESCRIBE (host → client) and ANNOUNCE (client → host) (`docs/PROTOCOL.md` 2.3, 2.4).
use super::client::StreamConfig;
/// Maximum size of an SDP payload, in bytes.
pub const MAX_SDP_BYTES: usize = 32 * 1024;
/// Maximum number of lines in an SDP payload.
pub const MAX_SDP_LINES: usize = 1024;
/// Maximum number of Opus configurations a host may advertise.
pub const MAX_OPUS_CONFIGS: usize = 16;
/// Maximum number of audio channels (7.1).
pub const MAX_AUDIO_CHANNELS: usize = 8;
/// A parsed SDP payload: `a=name:value` attributes plus every other `x=value` line, in order.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Sdp {
/// `a=` attributes: name and value (value empty for flag attributes).
pub attributes: Vec<(String, String)>,
/// Other lines: type character and value (e.g. `('v', "0")`), and bare lines such as `sprop-parameter-sets=…`
/// with type `'\0'`.
pub other: Vec<(char, String)>,
}
impl Sdp {
/// The first attribute with this exact name.
#[must_use]
pub fn attribute(&self, name: &str) -> Option<&str> {
self.attributes.iter().find(|(n, _)| n == name).map(|(_, v)| v.as_str())
}
}
/// Parses an SDP payload. Lines end in LF or CRLF.
///
/// # Errors
/// [`crate::Error::TooLarge`] over [`MAX_SDP_BYTES`] or [`MAX_SDP_LINES`], [`crate::Error::Malformed`] for a line
/// that is not UTF-8 or has no `=`. Phase A: always [`crate::Error::Unimplemented`].
pub fn parse_sdp(payload: &[u8]) -> crate::Result<Sdp> {
let _ = payload;
Err(crate::Error::Unimplemented)
}
/// Encryption flag bits of `x-ss-general.encryptionSupported` / `encryptionEnabled`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct EncryptionFlags(pub u8);
impl EncryptionFlags {
/// Control stream V2 framing with AES-GCM.
pub const CONTROL_V2: u8 = 0x01;
/// Video AES-GCM.
pub const VIDEO: u8 = 0x02;
/// Audio AES-CBC.
pub const AUDIO: u8 = 0x04;
}
/// Feature flag bits of `x-ss-general.featureFlags`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct FeatureFlags(pub u32);
impl FeatureFlags {
/// The host accepts pen and touch events.
pub const PEN_TOUCH_EVENTS: u32 = 0x01;
/// The host accepts controller touch events.
pub const CONTROLLER_TOUCH_EVENTS: u32 = 0x02;
}
/// One Opus multistream configuration (RFC 7845 §5.1.1), for the platform's Opus decoder.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OpusConfig {
/// Output channels (2, 6 or 8).
pub channels: u8,
/// Opus streams per packet.
pub streams: u8,
/// Of which coupled (stereo) streams.
pub coupled_streams: u8,
/// Channel mapping, one entry per channel.
pub mapping: Vec<u8>,
}
/// Parses one `surround-params` value (single digits: channels, streams, coupled, then one mapping digit per
/// channel), e.g. `21101`.
///
/// # Errors
/// [`crate::Error::Malformed`] for a non-digit, a wrong length, or values that break RFC 7845's rules
/// (`coupled ≤ streams`, `streams + coupled ≤ 255`, mapping entries `< streams + coupled`). Phase A: always
/// [`crate::Error::Unimplemented`].
pub fn parse_surround_params(value: &str) -> crate::Result<OpusConfig> {
let _ = value;
Err(crate::Error::Unimplemented)
}
/// What the host offers, read from the DESCRIBE payload.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct HostCapabilities {
/// `x-ss-general.encryptionSupported`.
pub encryption_supported: EncryptionFlags,
/// `x-ss-general.featureFlags`.
pub feature_flags: FeatureFlags,
/// `x-nv-video[0].refPicInvalidation:1`.
pub reference_frame_invalidation: bool,
/// `rtpmap:98 AV1/90000` present.
pub av1: bool,
/// Every `fmtp:97 surround-params=…`, in order.
pub opus_configs: Vec<OpusConfig>,
}
impl HostCapabilities {
/// Reads the capabilities from a DESCRIBE payload.
///
/// # Errors
/// [`crate::Error::Malformed`] for a present attribute with a bad value, [`crate::Error::TooLarge`] over
/// [`MAX_OPUS_CONFIGS`]. Phase A: always [`crate::Error::Unimplemented`].
pub fn from_sdp(sdp: &Sdp) -> crate::Result<Self> {
let _ = sdp;
Err(crate::Error::Unimplemented)
}
}
/// Builds the ANNOUNCE payload for `config`, turning on the encryption flags both sides support.
///
/// # Errors
/// [`crate::Error::Unsupported`] when the host lacks control V2 encryption or the requested codec. Phase A: always
/// [`crate::Error::Unimplemented`].
pub fn build_announce(config: &StreamConfig, host: &HostCapabilities) -> crate::Result<Vec<u8>> {
let _ = (config, host);
Err(crate::Error::Unimplemented)
}