Files
DomiStream/crates/domistream/src/rtsp/mod.rs
T

150 lines
4.9 KiB
Rust

//! RTSP stream negotiation (`docs/PROTOCOL.md` section 2): message codec, a framer for the TCP byte stream, SDP,
//! and the client state machine.
pub mod client;
pub mod sdp;
pub use client::{NegotiatedStream, RtspClient, RtspClientState, RtspProgress, StreamConfig, StreamPorts};
pub use sdp::{EncryptionFlags, HostCapabilities, OpusConfig, Sdp};
/// Default RTSP port.
pub const DEFAULT_RTSP_PORT: u16 = 48010;
/// Maximum size of one RTSP message (start line + headers + payload), in bytes.
pub const MAX_RTSP_MESSAGE_BYTES: usize = 64 * 1024;
/// Maximum length of one start or header line, in bytes (without CRLF).
pub const MAX_RTSP_LINE_BYTES: usize = 4096;
/// Maximum number of header lines in one message.
pub const MAX_RTSP_HEADERS: usize = 64;
/// The methods this client sends.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Method {
/// `OPTIONS`.
Options,
/// `DESCRIBE`.
Describe,
/// `SETUP`.
Setup,
/// `ANNOUNCE`.
Announce,
/// `PLAY`.
Play,
}
/// An RTSP request.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RtspRequest {
/// The method.
pub method: Method,
/// The target, e.g. `rtsp://192.0.2.10:48010` or `streamid=video/0/0`.
pub target: String,
/// Header lines in order, `CSeq` included.
pub headers: Vec<(String, String)>,
/// The payload (SDP for ANNOUNCE), possibly empty.
pub body: Vec<u8>,
}
impl RtspRequest {
/// Serializes the request (CRLF line ends, empty line, payload). Refuses CR or LF inside a target, header
/// name or value.
///
/// # Errors
/// [`crate::Error::Malformed`] for a forbidden character, [`crate::Error::TooLarge`] over
/// [`MAX_RTSP_MESSAGE_BYTES`]. Phase A: always [`crate::Error::Unimplemented`].
pub fn encode(&self) -> crate::Result<Vec<u8>> {
Err(crate::Error::Unimplemented)
}
}
/// An RTSP response.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RtspResponse {
/// Status code, e.g. 200.
pub status: u16,
/// Reason phrase.
pub reason: String,
/// Header lines in order.
pub headers: Vec<(String, String)>,
/// The payload (SDP for DESCRIBE), possibly empty.
pub body: Vec<u8>,
}
impl RtspResponse {
/// The first header with this name, compared case-insensitively.
#[must_use]
pub fn header(&self, name: &str) -> Option<&str> {
self.headers.iter().find(|(n, _)| n.eq_ignore_ascii_case(name)).map(|(_, v)| v.as_str())
}
}
/// Parses one complete response (as cut by [`RtspFramer`]).
///
/// # Errors
/// [`crate::Error::TooLarge`] over [`MAX_RTSP_MESSAGE_BYTES`], [`MAX_RTSP_LINE_BYTES`] or [`MAX_RTSP_HEADERS`],
/// [`crate::Error::Malformed`] for a bad start line, header line or `Content-Length`. Phase A: always
/// [`crate::Error::Unimplemented`].
pub fn parse_response(message: &[u8]) -> crate::Result<RtspResponse> {
let _ = message;
Err(crate::Error::Unimplemented)
}
/// Parses one complete request. Not used by the client; here for the fuzz harness and the Lumen host (Day 3).
///
/// # Errors
/// As [`parse_response`], plus [`crate::Error::UnexpectedValue`] for an unknown method.
pub fn parse_request(message: &[u8]) -> crate::Result<RtspRequest> {
let _ = message;
Err(crate::Error::Unimplemented)
}
/// Cuts the TCP byte stream from the host into whole messages.
///
/// A message ends after its headers plus `Content-Length` payload bytes; without `Content-Length` it ends when
/// the host closes the connection ([`RtspFramer::finish`]) (UNKNOWN U5). Never buffers more than
/// [`MAX_RTSP_MESSAGE_BYTES`].
#[derive(Debug, Default)]
pub struct RtspFramer {
buffer: Vec<u8>,
}
impl RtspFramer {
/// An empty framer.
#[must_use]
pub const fn new() -> Self {
Self { buffer: Vec::new() }
}
/// Appends received bytes.
///
/// # Errors
/// [`crate::Error::TooLarge`] when the unfinished message would exceed [`MAX_RTSP_MESSAGE_BYTES`]. Phase A:
/// always [`crate::Error::Unimplemented`].
pub fn push(&mut self, bytes: &[u8]) -> crate::Result<()> {
let _ = bytes;
Err(crate::Error::Unimplemented)
}
/// Takes the next complete message, if one is buffered.
///
/// # Errors
/// [`crate::Error::Malformed`] for a bad `Content-Length`. Phase A: always [`crate::Error::Unimplemented`].
pub fn next_message(&mut self) -> crate::Result<Option<Vec<u8>>> {
Err(crate::Error::Unimplemented)
}
/// The host closed the connection: returns what is buffered as the last message, if anything.
///
/// # Errors
/// [`crate::Error::Truncated`] when a `Content-Length` promised more. Phase A: always
/// [`crate::Error::Unimplemented`].
pub fn finish(&mut self) -> crate::Result<Option<Vec<u8>>> {
Err(crate::Error::Unimplemented)
}
/// Bytes buffered but not yet returned.
#[must_use]
pub fn buffered(&self) -> usize {
self.buffer.len()
}
}