//! The video packet layout (`docs/PROTOCOL.md` 5.2): RTP header, 4 reserved bytes, the 16-byte NV video header, //! payload. NV header fields are little-endian. use crate::rtp::RtpHeader; /// RTP header plus the 4 reserved bytes. pub const VIDEO_RTP_LEN: usize = 16; /// Length of the NV video header. pub const NV_HEADER_LEN: usize = 16; /// Offset of the payload in a packet. pub const PAYLOAD_OFFSET: usize = VIDEO_RTP_LEN + NV_HEADER_LEN; /// Length of the frame header at the start of each frame's data. pub const FRAME_HEADER_LEN: usize = 8; /// Maximum size of one video packet (plaintext or encrypted), in bytes. pub const MAX_VIDEO_PACKET_BYTES: usize = 4096; /// Maximum FEC blocks per frame (two-bit block index). pub const MAX_FEC_BLOCKS: usize = 4; /// The `packetSize` used when ANNOUNCE does not set one. pub const DEFAULT_PACKET_SIZE: u16 = 1024; /// NV video flags. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] pub struct VideoFlags(pub u8); impl VideoFlags { /// The packet carries picture data (0 on parity packets). pub const PICTURE_DATA: u8 = 0x01; /// Last data packet of the frame. pub const END_OF_FRAME: u8 = 0x02; /// First data packet of the frame. pub const START_OF_FRAME: u8 = 0x04; } /// The FEC fields of one packet, decoded from `fecInfo` and `multiFecBlocks`. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] pub struct FecInfo { /// Index in the block: `0..data_shards` data, `data_shards..` parity (`fecInfo` bits 12-21). pub shard_index: u16, /// Data shards in this block (`fecInfo` bits 22-31). pub data_shards: u16, /// FEC percentage of this block (`fecInfo` bits 4-11). pub fec_percentage: u8, /// This block's index in the frame (`multiFecBlocks` bits 4-5). pub block_index: u8, /// The frame's last block index (`multiFecBlocks` bits 6-7). pub last_block_index: u8, } /// The NV video header. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] pub struct NvVideoHeader { /// Stream packet index (RTP sequence << 8). pub stream_packet_index: u32, /// Frame index. pub frame_index: u32, /// Flags. pub flags: VideoFlags, /// `multiFecFlags` byte, raw (UNKNOWN U15). pub multi_fec_flags: u8, /// FEC fields. pub fec: FecInfo, } /// One plaintext video packet. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct VideoPacket<'a> { /// RTP header. pub rtp: RtpHeader, /// NV video header. pub nv: NvVideoHeader, /// Payload (frame data for data shards, parity bytes for parity shards). pub payload: &'a [u8], } /// Parses one plaintext video packet. Checks the FEC fields are consistent (`data_shards > 0`, data + parity /// `<= 255` with parity `= ceil(data × percentage / 100)`, `shard_index < data + parity`, /// `block_index <= last_block_index`). /// /// # Errors /// [`crate::Error::TooLarge`] over [`MAX_VIDEO_PACKET_BYTES`], [`crate::Error::Truncated`] under /// [`PAYLOAD_OFFSET`], [`crate::Error::UnexpectedValue`] for an RTP version other than 2, /// [`crate::Error::Malformed`] for inconsistent FEC fields. pub fn parse_video_packet(packet: &[u8]) -> crate::Result> { use crate::util::{byte, malformed, u32_le}; const W: &str = "video packet"; crate::util::cap(W, MAX_VIDEO_PACKET_BYTES, packet.len())?; crate::util::need(W, PAYLOAD_OFFSET, packet.len())?; let rtp = crate::rtp::parse_rtp_header(packet)?; let nv = VIDEO_RTP_LEN; let fec_info = u32_le(W, packet, nv + 12)?; let blocks = byte(W, packet, nv + 11)?; let fec = FecInfo { shard_index: ((fec_info >> 12) & 0x3FF) as u16, data_shards: ((fec_info >> 22) & 0x3FF) as u16, fec_percentage: ((fec_info >> 4) & 0xFF) as u8, block_index: (blocks >> 4) & 0x3, last_block_index: (blocks >> 6) & 0x3, }; let parity = crate::fec::parity_count(usize::from(fec.data_shards), fec.fec_percentage) .map_err(|_| malformed("video fec shard counts"))?; if usize::from(fec.shard_index) >= usize::from(fec.data_shards) + parity || fec.block_index > fec.last_block_index { return Err(malformed("video fec fields")); } Ok(VideoPacket { rtp, nv: NvVideoHeader { stream_packet_index: u32_le(W, packet, nv)?, frame_index: u32_le(W, packet, nv + 4)?, flags: VideoFlags(byte(W, packet, nv + 8)?), multi_fec_flags: byte(W, packet, nv + 10)?, fec, }, payload: packet.get(PAYLOAD_OFFSET..).unwrap_or(&[]), }) } /// Frame types in the frame header. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum FrameType { /// Predicted (1). Predicted, /// Key / IDR (2). Key, /// First frame after a reference frame invalidation (5). Recovery, /// Any other value. Other(u8), } /// The 8-byte header in front of each frame's bitstream (UNKNOWN U13: Wolf shows a constant, Moonshine these /// fields). #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub struct FrameHeader { /// The 8 bytes as received. pub raw: [u8; FRAME_HEADER_LEN], /// Byte 0. pub header_type: u8, /// Bytes 1-2, LE: host processing latency (unit UNKNOWN). pub processing_latency: u16, /// Byte 3. pub frame_type: FrameType, /// Bytes 4-7, LE: payload bytes in the last data shard. pub last_payload_len: u32, } /// Parses the frame header at the start of a frame's data (the rest is ignored here). /// /// # Errors /// [`crate::Error::Truncated`] under [`FRAME_HEADER_LEN`]. pub fn parse_frame_header(frame_data: &[u8]) -> crate::Result { const W: &str = "video frame header"; let raw = crate::util::array::(W, frame_data, 0)?; let [header_type, l0, l1, kind, a, b, c, d] = raw; let frame_type = match kind { 1 => FrameType::Predicted, 2 => FrameType::Key, 5 => FrameType::Recovery, other => FrameType::Other(other), }; Ok(FrameHeader { raw, header_type, processing_latency: u16::from_le_bytes([l0, l1]), frame_type, last_payload_len: u32::from_le_bytes([a, b, c, d]), }) }