[domistream] RTSP API: message codec, stream framer, SDP, negotiation state machine
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
//! The RTSP client state machine: OPTIONS → DESCRIBE → SETUP audio → SETUP video → SETUP control → ANNOUNCE →
|
||||
//! PLAY (`docs/PROTOCOL.md` 2.2).
|
||||
//!
|
||||
//! The caller opens TCP to the session URL, writes each request from [`RtspClient::poll_request`], cuts the
|
||||
//! answers with [`super::RtspFramer`], and feeds each whole answer to [`RtspClient::handle_response`] until it
|
||||
//! returns [`RtspProgress::Ready`].
|
||||
|
||||
use super::sdp::{EncryptionFlags, HostCapabilities, OpusConfig};
|
||||
use crate::http::launch::AudioLayout;
|
||||
|
||||
/// Video codecs.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum VideoCodec {
|
||||
/// H.264.
|
||||
H264,
|
||||
/// HEVC (H.265).
|
||||
Hevc,
|
||||
/// AV1.
|
||||
Av1,
|
||||
}
|
||||
|
||||
/// What the client asks for in ANNOUNCE.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct StreamConfig {
|
||||
/// Width in pixels.
|
||||
pub width: u32,
|
||||
/// Height in pixels.
|
||||
pub height: u32,
|
||||
/// Frames per second.
|
||||
pub fps: u32,
|
||||
/// Video bitrate in kbit/s.
|
||||
pub bitrate_kbps: u32,
|
||||
/// Video `packetSize` (default 1024).
|
||||
pub packet_size: u16,
|
||||
/// Video codec.
|
||||
pub codec: VideoCodec,
|
||||
/// HDR (10-bit) stream.
|
||||
pub hdr: bool,
|
||||
/// Audio layout.
|
||||
pub audio: AudioLayout,
|
||||
/// Audio packet duration in milliseconds (`x-nv-aqos.packetDuration`).
|
||||
pub audio_packet_duration_ms: u8,
|
||||
/// Minimum video parity packets per FEC block.
|
||||
pub min_fec_packets: u8,
|
||||
/// The client can recover with reference frame invalidation instead of an IDR.
|
||||
pub reference_frame_invalidation: bool,
|
||||
/// Encryption the client wants (Day 1: all three); the ANNOUNCE turns on the intersection with the host.
|
||||
pub encryption: EncryptionFlags,
|
||||
}
|
||||
|
||||
/// The three UDP ports from the SETUP answers.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct StreamPorts {
|
||||
/// Audio RTP port.
|
||||
pub audio: u16,
|
||||
/// Video RTP port.
|
||||
pub video: u16,
|
||||
/// Control (ENet) port.
|
||||
pub control: u16,
|
||||
}
|
||||
|
||||
/// Everything the stream stage needs once RTSP is done.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct NegotiatedStream {
|
||||
/// UDP ports.
|
||||
pub ports: StreamPorts,
|
||||
/// What the host offered in DESCRIBE.
|
||||
pub host: HostCapabilities,
|
||||
/// Encryption turned on in ANNOUNCE.
|
||||
pub encryption: EncryptionFlags,
|
||||
/// The Opus configuration chosen for `StreamConfig::audio`, for the platform decoder.
|
||||
pub opus: OpusConfig,
|
||||
/// The `Session` id from SETUP, if the host sent one.
|
||||
pub session_id: Option<String>,
|
||||
/// The datagram to send to the video and audio ports before the stream starts (UNKNOWN U8; `PING` for
|
||||
/// Moonshine-like hosts).
|
||||
pub ping_payload: Vec<u8>,
|
||||
}
|
||||
|
||||
/// Where the negotiation is. Each state names the request just sent.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum RtspClientState {
|
||||
/// OPTIONS sent.
|
||||
Options,
|
||||
/// DESCRIBE sent.
|
||||
Describe,
|
||||
/// SETUP audio sent.
|
||||
SetupAudio,
|
||||
/// SETUP video sent.
|
||||
SetupVideo,
|
||||
/// SETUP control sent.
|
||||
SetupControl,
|
||||
/// ANNOUNCE sent.
|
||||
Announce,
|
||||
/// PLAY sent.
|
||||
Play,
|
||||
/// Done; see [`NegotiatedStream`].
|
||||
Ready,
|
||||
/// A non-200 answer or a bad payload stopped it.
|
||||
Failed,
|
||||
}
|
||||
|
||||
/// Result of feeding one answer.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum RtspProgress {
|
||||
/// The next request is ready in [`RtspClient::poll_request`].
|
||||
Continue,
|
||||
/// Negotiation finished.
|
||||
Ready(NegotiatedStream),
|
||||
}
|
||||
|
||||
/// The RTSP negotiation for one stream session.
|
||||
#[derive(Debug)]
|
||||
pub struct RtspClient {
|
||||
state: RtspClientState,
|
||||
}
|
||||
|
||||
impl RtspClient {
|
||||
/// Prepares the negotiation against `session_url` (`sessionUrl0` from launch). The first request (OPTIONS)
|
||||
/// is ready in [`Self::poll_request`].
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::Malformed`] for a URL that is not `rtsp://host:port`, or a config with zero size, frame
|
||||
/// rate, bitrate or packet size. Phase A: always [`crate::Error::Unimplemented`].
|
||||
pub fn new(session_url: &str, config: StreamConfig) -> crate::Result<Self> {
|
||||
let _ = (session_url, config);
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// The next request to write, if one is due. Each is returned once.
|
||||
pub fn poll_request(&mut self) -> Option<Vec<u8>> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Feeds one whole answer (see [`super::RtspFramer`]).
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::HostStatus`] for a non-200 answer, [`crate::Error::Malformed`] for a `CSeq` that does not
|
||||
/// match or a bad payload, [`crate::Error::Unsupported`] when the host lacks something required,
|
||||
/// [`crate::Error::State`] when no request is outstanding. Phase A: always [`crate::Error::Unimplemented`].
|
||||
pub fn handle_response(&mut self, message: &[u8]) -> crate::Result<RtspProgress> {
|
||||
let _ = message;
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// Where the negotiation is.
|
||||
#[must_use]
|
||||
pub const fn state(&self) -> RtspClientState {
|
||||
self.state
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user