[domistream] Video API: packet parser, frame header, decryption, depacketizer with FEC
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
//! 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 (`shard_index < data + parity`,
|
||||
/// `block_index <= last_block_index`, `data_shards > 0`).
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::TooLarge`] over [`MAX_VIDEO_PACKET_BYTES`], [`crate::Error::Truncated`] under
|
||||
/// [`PAYLOAD_OFFSET`], [`crate::Error::Malformed`] for inconsistent FEC fields. Phase A: always
|
||||
/// [`crate::Error::Unimplemented`].
|
||||
pub fn parse_video_packet(packet: &[u8]) -> crate::Result<VideoPacket<'_>> {
|
||||
let _ = packet;
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
|
||||
/// 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`]. Phase A: always [`crate::Error::Unimplemented`].
|
||||
pub fn parse_frame_header(frame_data: &[u8]) -> crate::Result<FrameHeader> {
|
||||
let _ = frame_data;
|
||||
Err(crate::Error::Unimplemented)
|
||||
}
|
||||
Reference in New Issue
Block a user