[domistream] Audio API: packet parser, CBC decryption, 4+2 FEC depacketizer

This commit is contained in:
2026-11-25 11:48:04 +01:00
parent b4a21dfc10
commit 31d1fc3655
3 changed files with 233 additions and 0 deletions
+172
View File
@@ -0,0 +1,172 @@
//! The audio receive path (`docs/PROTOCOL.md` section 6): parse → rebuild lost packets from the 4 + 2 FEC block →
//! decrypt (AES-128-CBC) → one Opus packet per RTP packet, in order, for the platform's Opus decoder.
//!
//! **Opus decoding is not here.** A packet that cannot be rebuilt becomes [`AudioEvent::Lost`]; the platform
//! conceals it.
use core::fmt;
use crate::{Instant, Result, SessionKeys};
pub mod packet;
pub use packet::{AudioFecHeader, AudioPacket, parse_audio_packet};
/// Data packets per FEC block.
pub const DATA_SHARDS: usize = 4;
/// Parity packets per FEC block.
pub const PARITY_SHARDS: usize = 2;
/// Maximum blocks kept while waiting for missing packets.
pub const MAX_BLOCKS_IN_FLIGHT: usize = 8;
/// AES-128-CBC for audio payloads: IV = `u32 BE (rikeyid + sequence) ‖ 12 zero bytes`, PKCS#7 padding.
/// `Debug` never prints the key.
#[derive(Clone)]
pub struct AudioCipher {
key: [u8; 16],
rikeyid: u32,
}
impl AudioCipher {
/// A cipher for the session's keys.
#[must_use]
pub const fn new(keys: &SessionKeys) -> Self {
Self { key: keys.rikey, rikeyid: keys.rikeyid }
}
/// The IV for RTP sequence number `sequence`.
///
/// # Errors
/// Phase A: always [`crate::Error::Unimplemented`].
pub fn iv(&self, sequence: u16) -> Result<[u8; 16]> {
let _ = (sequence, self.rikeyid);
Err(crate::Error::Unimplemented)
}
/// Decrypts one payload.
///
/// # Errors
/// [`crate::Error::Malformed`] for a length that is not a positive multiple of 16, [`crate::Error::Crypto`] for
/// bad padding. Phase A: always [`crate::Error::Unimplemented`].
pub fn decrypt(&self, sequence: u16, ciphertext: &[u8]) -> Result<Vec<u8>> {
let _ = (sequence, ciphertext, &self.key);
Err(crate::Error::Unimplemented)
}
}
impl fmt::Debug for AudioCipher {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("AudioCipher(<redacted>)")
}
}
/// How lost audio packets are rebuilt.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AudioFec {
/// Parity packets are ignored; losses go to the platform's concealment.
Disabled,
/// Rebuild with this 2 × 4 parity matrix (row-major). The GameStream matrix is UNKNOWN U16 until measured.
ParityMatrix([u8; PARITY_SHARDS * DATA_SHARDS]),
}
/// Settings of the audio receiver.
#[derive(Clone, Debug)]
pub struct AudioConfig {
/// Decrypt payloads with this cipher (audio encryption negotiated, or always: UNKNOWN U17).
pub cipher: Option<AudioCipher>,
/// Packet duration in milliseconds (`x-nv-aqos.packetDuration`).
pub packet_duration_ms: u8,
/// FEC mode.
pub fec: AudioFec,
/// How long to wait for a missing packet before reporting it lost (the jitter budget).
pub loss_timeout: core::time::Duration,
}
/// What the receiver reports, in sequence order.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AudioEvent {
/// One decrypted Opus packet.
Packet {
/// RTP sequence number.
sequence: u16,
/// RTP timestamp (milliseconds).
timestamp_ms: u32,
/// The Opus packet.
opus: Vec<u8>,
/// Rebuilt by FEC.
recovered: bool,
},
/// The packet with this sequence number is gone; the decoder conceals one packet duration.
Lost {
/// RTP sequence number.
sequence: u16,
},
}
/// Counters for the stats overlay.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct AudioStats {
/// Packets accepted.
pub packets: u64,
/// Packets refused (bad format, bad padding, duplicate, too old).
pub packets_dropped: u64,
/// Packets rebuilt by FEC.
pub recovered: u64,
/// Packets reported lost.
pub lost: u64,
}
/// The audio receiver.
#[derive(Debug)]
pub struct AudioDepacketizer {
stats: AudioStats,
}
impl AudioDepacketizer {
/// A receiver for one stream.
///
/// # Errors
/// [`crate::Error::Malformed`] for a zero packet duration or a parity matrix that is not MDS. Phase A: always
/// [`crate::Error::Unimplemented`].
pub fn new(config: AudioConfig) -> Result<Self> {
let _ = config;
Err(crate::Error::Unimplemented)
}
/// Feeds one datagram from the audio port. Bad, duplicate and too-old packets are counted and dropped; only an
/// oversized datagram is an error.
///
/// # Errors
/// [`crate::Error::TooLarge`] over [`packet::MAX_AUDIO_PACKET_BYTES`]. Phase A: always
/// [`crate::Error::Unimplemented`].
pub fn push(&mut self, now: Instant, datagram: &[u8]) -> Result<()> {
let _ = (now, datagram);
Err(crate::Error::Unimplemented)
}
/// The next event.
pub fn poll_event(&mut self) -> Option<AudioEvent> {
None
}
/// When a missing packet times out.
#[must_use]
pub fn poll_timeout(&self) -> Option<Instant> {
None
}
/// Reports timed-out packets as lost.
///
/// # Errors
/// Phase A: always [`crate::Error::Unimplemented`].
pub fn handle_timeout(&mut self, now: Instant) -> Result<()> {
let _ = now;
Err(crate::Error::Unimplemented)
}
/// Counters.
#[must_use]
pub const fn stats(&self) -> AudioStats {
self.stats
}
}
+60
View File
@@ -0,0 +1,60 @@
//! 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 payload type or a parity shard index above 1.
/// Phase A: always [`crate::Error::Unimplemented`].
pub fn parse_audio_packet(packet: &[u8]) -> crate::Result<AudioPacket<'_>> {
let _ = packet;
Err(crate::Error::Unimplemented)
}
+1
View File
@@ -35,6 +35,7 @@
#![forbid(unsafe_code)]
#![deny(missing_docs)]
pub mod audio;
pub mod control;
pub mod enet;
pub mod entropy;