Files
DomiStream/crates/domistream/src/audio/mod.rs
T

173 lines
5.2 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 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
}
}