Files
DomiStream/crates/domistream/src/audio/packet.rs
T
mnovak 30a7b90034 [domistream] Implementation: HTTP, pairing, RTSP/SDP, ENet, control, input, RTP, FEC, video and audio receivers
Per PROTOCOL.md and the Wolf docs; interop with a real host is unverified. Strict, capped parsers throughout,
shared helpers in util.rs. Where PROTOCOL.md says UNKNOWN the Wolf-documented behaviour is used. The audio
receiver waits for the jitter budget before rebuilding a missing packet, so a late packet beats a rebuilt one.
2026-11-25 14:10:24 +01:00

86 lines
3.1 KiB
Rust

//! The audio packet layout (`docs/PROTOCOL.md` 6.2): RTP header, then the Opus payload (payload type 97) or a
//! 12-byte FEC header plus parity (payload type 127). FEC header fields are big-endian.
use crate::rtp::RtpHeader;
/// Payload type of audio data packets.
pub const AUDIO_PAYLOAD_TYPE: u8 = 97;
/// Payload type of audio parity packets.
pub const AUDIO_FEC_PAYLOAD_TYPE: u8 = 127;
/// Length of the FEC header in parity packets.
pub const AUDIO_FEC_HEADER_LEN: usize = 12;
/// Maximum size of one audio packet, in bytes.
pub const MAX_AUDIO_PACKET_BYTES: usize = 2048;
/// The FEC header of a parity packet.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct AudioFecHeader {
/// Parity shard index, 0 or 1.
pub shard_index: u8,
/// Payload type of the protected packets (97).
pub payload_type: u8,
/// Sequence number of the block's first data packet.
pub base_sequence: u16,
/// Timestamp of the block's first data packet.
pub base_timestamp: u32,
/// SSRC.
pub ssrc: u32,
}
/// One audio packet.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AudioPacket<'a> {
/// Opus data (still encrypted when audio encryption is on).
Data {
/// RTP header.
rtp: RtpHeader,
/// Payload.
payload: &'a [u8],
},
/// Parity.
Parity {
/// RTP header.
rtp: RtpHeader,
/// FEC header.
fec: AudioFecHeader,
/// Parity bytes.
parity: &'a [u8],
},
}
/// Parses one audio packet.
///
/// # Errors
/// [`crate::Error::TooLarge`] over [`MAX_AUDIO_PACKET_BYTES`], [`crate::Error::Truncated`] for a cut header or an
/// empty payload, [`crate::Error::UnexpectedValue`] for another RTP version or payload type, or a parity shard
/// index above 1.
pub fn parse_audio_packet(packet: &[u8]) -> crate::Result<AudioPacket<'_>> {
use crate::util::{byte, need, u16_be, u32_be};
const W: &str = "audio packet";
crate::util::cap(W, MAX_AUDIO_PACKET_BYTES, packet.len())?;
let rtp = crate::rtp::parse_rtp_header(packet)?;
let at = crate::rtp::RTP_HEADER_LEN;
match rtp.payload_type {
AUDIO_PAYLOAD_TYPE => {
need(W, at + 1, packet.len())?;
Ok(AudioPacket::Data { rtp, payload: packet.get(at..).unwrap_or(&[]) })
}
AUDIO_FEC_PAYLOAD_TYPE => {
need(W, at + AUDIO_FEC_HEADER_LEN + 1, packet.len())?;
let shard_index = byte(W, packet, at)?;
if shard_index > 1 {
return Err(crate::Error::UnexpectedValue { what: "audio parity shard", value: shard_index.into() });
}
let fec = AudioFecHeader {
shard_index,
payload_type: byte(W, packet, at + 1)?,
base_sequence: u16_be(W, packet, at + 2)?,
base_timestamp: u32_be(W, packet, at + 4)?,
ssrc: u32_be(W, packet, at + 8)?,
};
Ok(AudioPacket::Parity { rtp, fec, parity: packet.get(at + AUDIO_FEC_HEADER_LEN..).unwrap_or(&[]) })
}
other => Err(crate::Error::UnexpectedValue { what: "audio payload type", value: other.into() }),
}
}