From bd91a1007e46bbc10df873ecc29c536096f74956 Mon Sep 17 00:00:00 2001 From: mnovak Date: Wed, 25 Nov 2026 14:32:37 +0100 Subject: [PATCH] [domistream] WIP: rustfmt, doc link, clippy fixes (one collapsible-if left in rtsp/mod.rs), PROTOCOL.md status Paused at Mark's request. --- crates/domistream/src/audio/mod.rs | 18 ++++++++++--- crates/domistream/src/control/frame.rs | 2 +- crates/domistream/src/control/message.rs | 5 +++- crates/domistream/src/enet/mod.rs | 31 +++++++++++++---------- crates/domistream/src/fec.rs | 13 +++++++--- crates/domistream/src/http/launch.rs | 2 +- crates/domistream/src/http/serverinfo.rs | 8 +++--- crates/domistream/src/input/mod.rs | 7 +++-- crates/domistream/src/pairing/crypto.rs | 4 +-- crates/domistream/src/pairing/identity.rs | 6 ++--- crates/domistream/src/rtsp/client.rs | 8 +----- crates/domistream/src/util.rs | 8 ++---- crates/domistream/src/video/mod.rs | 5 +--- docs/PROTOCOL.md | 8 +++--- 14 files changed, 68 insertions(+), 57 deletions(-) diff --git a/crates/domistream/src/audio/mod.rs b/crates/domistream/src/audio/mod.rs index 1e66608..49bcbe5 100644 --- a/crates/domistream/src/audio/mod.rs +++ b/crates/domistream/src/audio/mod.rs @@ -200,7 +200,14 @@ impl AudioDepacketizer { AudioFec::Disabled => None, AudioFec::ParityMatrix(m) => Some(ReedSolomon::with_parity_matrix(DATA_SHARDS, PARITY_SHARDS, &m)?), }; - Ok(Self { config, code, next: None, blocks: HashMap::new(), events: VecDeque::new(), stats: AudioStats::default() }) + Ok(Self { + config, + code, + next: None, + blocks: HashMap::new(), + events: VecDeque::new(), + stats: AudioStats::default(), + }) } fn drop_packet(&mut self) -> Result<()> { @@ -277,7 +284,7 @@ impl AudioDepacketizer { return false; } let ahead = first.wrapping_sub(next); - if ahead < 0x8000 && ahead >= MAX_PACKETS_AHEAD { + if (MAX_PACKETS_AHEAD..0x8000).contains(&ahead) { return false; } let window = (MAX_BLOCKS_IN_FLIGHT * DATA_SHARDS) as u16; @@ -371,7 +378,9 @@ impl AudioDepacketizer { .blocks .iter() .filter(|(b, _)| b.wrapping_sub(base) != 0 && b.wrapping_sub(n) < 0x8000) - .filter_map(|(_, blk)| blk.data.iter().flatten().map(|d| d.arrived).chain(blk.parity.iter().flatten().map(|p| p.arrived)).min()) + .filter_map(|(_, blk)| { + blk.data.iter().flatten().map(|d| d.arrived).chain(blk.parity.iter().flatten().map(|p| p.arrived)).min() + }) .min(); match (own, later) { (Some(a), Some(b)) => Some(a.min(b)), @@ -411,7 +420,8 @@ impl AudioDepacketizer { .next() .or_else(|| { block.data.iter().enumerate().find_map(|(i, d)| { - d.as_ref().map(|d| d.timestamp_ms.wrapping_sub(i as u32 * u32::from(self.config.packet_duration_ms))) + d.as_ref() + .map(|d| d.timestamp_ms.wrapping_sub(i as u32 * u32::from(self.config.packet_duration_ms))) }) }) .unwrap_or(0); diff --git a/crates/domistream/src/control/frame.rs b/crates/domistream/src/control/frame.rs index 8fb43ff..60bce62 100644 --- a/crates/domistream/src/control/frame.rs +++ b/crates/domistream/src/control/frame.rs @@ -8,8 +8,8 @@ use core::fmt; -use aes_gcm::aead::{AeadInOut, KeyInit}; use aes_gcm::Aes128Gcm; +use aes_gcm::aead::{AeadInOut, KeyInit}; use crate::util::{array, cap, malformed, u16_le, u32_le}; use crate::{Error, Result}; diff --git a/crates/domistream/src/control/message.rs b/crates/domistream/src/control/message.rs index 88bc8e7..d29f6d0 100644 --- a/crates/domistream/src/control/message.rs +++ b/crates/domistream/src/control/message.rs @@ -304,7 +304,10 @@ pub fn encode_client_message(message: &ClientMessage) -> Result> { Ok(m) } ClientMessage::Input(e) => crate::input::encode_input(e), - ClientMessage::StartA | ClientMessage::StartB | ClientMessage::PeriodicPing | ClientMessage::RequestIdrFrame => { + ClientMessage::StartA + | ClientMessage::StartB + | ClientMessage::PeriodicPing + | ClientMessage::RequestIdrFrame => { Err(crate::Error::Unsupported { what: "a control message whose body is still UNKNOWN (U10)" }) } } diff --git a/crates/domistream/src/enet/mod.rs b/crates/domistream/src/enet/mod.rs index 33741d5..8448d93 100644 --- a/crates/domistream/src/enet/mod.rs +++ b/crates/domistream/src/enet/mod.rs @@ -367,14 +367,16 @@ impl EnetClient { check } CommandBody::SendUnreliable { unreliable_sequence, data } => { - if self.state == EnetState::Connected && channel < self.channel_count { - if let Some(ch) = self.channels.get_mut(usize::from(channel)) { - let newer = - ch.in_unreliable.is_none_or(|last| (1..0x8000).contains(&unreliable_sequence.wrapping_sub(last))); - if newer { - ch.in_unreliable = Some(unreliable_sequence); - self.events.push_back(EnetEvent::Received { channel, data }); - } + if self.state == EnetState::Connected + && channel < self.channel_count + && let Some(ch) = self.channels.get_mut(usize::from(channel)) + { + let newer = ch + .in_unreliable + .is_none_or(|last| (1..0x8000).contains(&unreliable_sequence.wrapping_sub(last))); + if newer { + ch.in_unreliable = Some(unreliable_sequence); + self.events.push_back(EnetEvent::Received { channel, data }); } } Ok(()) @@ -574,7 +576,10 @@ impl EnetClient { acknowledge: false, channel_id: channel, reliable_sequence: 0, - body: CommandBody::SendUnsequenced { unsequenced_group: self.unsequenced_group, data: data.to_vec() }, + body: CommandBody::SendUnsequenced { + unsequenced_group: self.unsequenced_group, + data: data.to_vec(), + }, }); } } @@ -625,11 +630,9 @@ impl EnetClient { } size += protocol::command_size(c); if let Some(c) = self.resend.pop_front() { - if let Some(f) = self - .in_flight - .iter_mut() - .find(|f| f.command.channel_id == c.channel_id && f.command.reliable_sequence == c.reliable_sequence) - { + if let Some(f) = self.in_flight.iter_mut().find(|f| { + f.command.channel_id == c.channel_id && f.command.reliable_sequence == c.reliable_sequence + }) { f.sent_at = now; } commands.push(c); diff --git a/crates/domistream/src/fec.rs b/crates/domistream/src/fec.rs index c03bead..0e59b74 100644 --- a/crates/domistream/src/fec.rs +++ b/crates/domistream/src/fec.rs @@ -157,7 +157,9 @@ fn is_mds(p: &[u8], data: usize, parity: usize) -> bool { if p.contains(&0) { return false; } - let total: u64 = (2..=data.min(parity)).map(|s| choose(data, s).saturating_mul(choose(parity, s))).fold(0u64, u64::saturating_add); + let total: u64 = (2..=data.min(parity)) + .map(|s| choose(data, s).saturating_mul(choose(parity, s))) + .fold(0u64, u64::saturating_add); let max_size = if total <= MDS_EXHAUSTIVE_BUDGET { data.min(parity) } else if choose(data, 2).saturating_mul(choose(parity, 2)) <= MDS_PAIRS_BUDGET { @@ -335,7 +337,11 @@ impl ReedSolomon { .take(missing.len()) .collect(); let e = missing.len(); - let a: Vec = rows.iter().flat_map(|&r| missing.iter().map(move |&c| (r, c))).map(|(r, c)| self.coefficient(r, c)).collect(); + let a: Vec = rows + .iter() + .flat_map(|&r| missing.iter().map(move |&c| (r, c))) + .map(|(r, c)| self.coefficient(r, c)) + .collect(); let a_inv = invert(a, e).ok_or(malformed("fec system is singular (matrix not MDS)"))?; // rhs_r = parity_r - sum over present data of P[r][i] * d_i let mut rhs: Vec> = Vec::with_capacity(e); @@ -362,7 +368,8 @@ impl ReedSolomon { (0..self.parity_shards).filter(|&j| shards.get(k + j).is_some_and(Option::is_none)).collect(); if !parity_missing.is_empty() { let data: Vec<&[u8]> = shards.iter().take(k).flatten().map(Vec::as_slice).collect(); - let rebuilt: Vec<(usize, Vec)> = parity_missing.iter().map(|&j| (j, self.parity_row(j, &data, len))).collect(); + let rebuilt: Vec<(usize, Vec)> = + parity_missing.iter().map(|&j| (j, self.parity_row(j, &data, len))).collect(); for (j, p) in rebuilt { if let Some(s) = shards.get_mut(k + j) { *s = Some(p); diff --git a/crates/domistream/src/http/launch.rs b/crates/domistream/src/http/launch.rs index 1bb91c9..0dc59ee 100644 --- a/crates/domistream/src/http/launch.rs +++ b/crates/domistream/src/http/launch.rs @@ -32,7 +32,7 @@ impl AudioLayout { } /// The speaker mask (WAVE channel-mask bits: FL FR, + FC LFE BL BR, + SL SR). Stereo's `0x3` matches the - /// documented default `surroundAudioInfo` 0x30002 [M]; the surround masks are the standard layouts (U4). + /// documented default `surroundAudioInfo` 0x30002 (source M); the surround masks are the standard layouts (U4). #[must_use] pub const fn channel_mask(self) -> u32 { match self { diff --git a/crates/domistream/src/http/serverinfo.rs b/crates/domistream/src/http/serverinfo.rs index b0b4d8d..ce1fef0 100644 --- a/crates/domistream/src/http/serverinfo.rs +++ b/crates/domistream/src/http/serverinfo.rs @@ -95,13 +95,15 @@ pub fn parse_server_info(body: &[u8]) -> Result { } let hostname = non_empty(r, "hostname")?.ok_or(malformed("serverinfo: no hostname"))?.to_owned(); let unique_id = non_empty(r, "uniqueid")?.ok_or(malformed("serverinfo: no uniqueid"))?.to_owned(); - let https_port = parse_u16("serverinfo HttpsPort", field(r, "HttpsPort")?.ok_or(malformed("serverinfo: no HttpsPort"))?)?; + let https_port = + parse_u16("serverinfo HttpsPort", field(r, "HttpsPort")?.ok_or(malformed("serverinfo: no HttpsPort"))?)?; let http_port = non_empty(r, "ExternalPort")?.map(|p| parse_u16("serverinfo ExternalPort", p)).transpose()?; let mac = non_empty(r, "mac")?.map(parse_mac).transpose()?; let max_luma_pixels_hevc = non_empty(r, "MaxLumaPixelsHEVC")?.map(|v| parse_dec("serverinfo MaxLumaPixelsHEVC", v)).transpose()?; - let codec_mode_support = - non_empty(r, "ServerCodecModeSupport")?.map(|v| parse_u32("serverinfo ServerCodecModeSupport", v)).transpose()?; + let codec_mode_support = non_empty(r, "ServerCodecModeSupport")? + .map(|v| parse_u32("serverinfo ServerCodecModeSupport", v)) + .transpose()?; let paired = match non_empty(r, "PairStatus")? { None | Some("0") => false, Some("1") => true, diff --git a/crates/domistream/src/input/mod.rs b/crates/domistream/src/input/mod.rs index b638b21..153f49b 100644 --- a/crates/domistream/src/input/mod.rs +++ b/crates/domistream/src/input/mod.rs @@ -201,10 +201,9 @@ pub fn encode_input(event: &InputEvent) -> crate::Result> { InputType::MouseMoveAbs, &[x.to_be_bytes(), y.to_be_bytes(), [0, 0], width.to_be_bytes(), height.to_be_bytes()].concat(), ), - InputEvent::MouseButton { pressed, button } => frame( - if pressed { InputType::MouseButtonDown } else { InputType::MouseButtonUp }, - &[button as u8], - ), + InputEvent::MouseButton { pressed, button } => { + frame(if pressed { InputType::MouseButtonDown } else { InputType::MouseButtonUp }, &[button as u8]) + } InputEvent::Key { pressed, flags, key_code, modifiers } => { let [lo, hi] = key_code.to_le_bytes(); frame(if pressed { InputType::KeyDown } else { InputType::KeyUp }, &[flags, lo, hi, modifiers, 0, 0]) diff --git a/crates/domistream/src/pairing/crypto.rs b/crates/domistream/src/pairing/crypto.rs index a4350a0..a47d263 100644 --- a/crates/domistream/src/pairing/crypto.rs +++ b/crates/domistream/src/pairing/crypto.rs @@ -32,8 +32,8 @@ fn ecb(key: &PairingKey, data: &[u8], encrypt: bool) -> Result> { } let cipher = Aes128::new(key.into()); let mut out = data.to_vec(); - for chunk in out.chunks_exact_mut(16) { - let block: &mut aes::Block = chunk.try_into().map_err(|_| malformed("aes block"))?; + for chunk in out.as_chunks_mut::<16>().0 { + let block: &mut aes::Block = chunk.into(); if encrypt { cipher.encrypt_block(block); } else { diff --git a/crates/domistream/src/pairing/identity.rs b/crates/domistream/src/pairing/identity.rs index 14d156e..0faca2a 100644 --- a/crates/domistream/src/pairing/identity.rs +++ b/crates/domistream/src/pairing/identity.rs @@ -271,10 +271,8 @@ impl ServerCert { const END: &[u8] = b"-----END CERTIFICATE-----"; cap("host certificate PEM", MAX_CERT_PEM_BYTES, pem.len())?; let trimmed = pem.trim_ascii(); - let body = trimmed - .strip_prefix(BEGIN) - .and_then(|b| b.strip_suffix(END)) - .ok_or(malformed("host certificate PEM"))?; + let body = + trimmed.strip_prefix(BEGIN).and_then(|b| b.strip_suffix(END)).ok_or(malformed("host certificate PEM"))?; if body.windows(5).any(|w| w == b"-----") { return Err(malformed("host certificate PEM holds more than one block")); } diff --git a/crates/domistream/src/rtsp/client.rs b/crates/domistream/src/rtsp/client.rs index ce06743..93e7570 100644 --- a/crates/domistream/src/rtsp/client.rs +++ b/crates/domistream/src/rtsp/client.rs @@ -170,13 +170,7 @@ impl RtspClient { Ok(c) } - fn queue( - &mut self, - method: Method, - target: &str, - mut extra: Vec<(String, String)>, - body: Vec, - ) -> Result<()> { + fn queue(&mut self, method: Method, target: &str, mut extra: Vec<(String, String)>, body: Vec) -> Result<()> { self.cseq = self.cseq.wrapping_add(1); let mut headers = vec![("CSeq".to_owned(), self.cseq.to_string())]; headers.push(("X-GS-ClientVersion".to_owned(), CLIENT_VERSION.to_owned())); diff --git a/crates/domistream/src/util.rs b/crates/domistream/src/util.rs index 0caf2b5..4c7e747 100644 --- a/crates/domistream/src/util.rs +++ b/crates/domistream/src/util.rs @@ -111,12 +111,8 @@ pub(crate) fn hex_decode(what: &'static str, s: &str) -> Result> { return Err(malformed(what)); } let mut out = Vec::with_capacity(b.len() / 2); - for pair in b.chunks_exact(2) { - let (hi, lo) = match pair { - [h, l] => (hex_digit(*h), hex_digit(*l)), - _ => (None, None), - }; - match (hi, lo) { + for [h, l] in b.as_chunks::<2>().0 { + match (hex_digit(*h), hex_digit(*l)) { (Some(h), Some(l)) => out.push((h << 4) | l), _ => return Err(malformed(what)), } diff --git a/crates/domistream/src/video/mod.rs b/crates/domistream/src/video/mod.rs index 4ac678c..b0f6772 100644 --- a/crates/domistream/src/video/mod.rs +++ b/crates/domistream/src/video/mod.rs @@ -286,10 +286,7 @@ impl VideoDepacketizer { } block.shards.insert(fec.shard_index, p.payload.to_vec()); let complete = (0..=usize::from(f.last_block)).all(|b| { - f.blocks - .get(b) - .and_then(Option::as_ref) - .is_some_and(|blk| blk.shards.len() >= usize::from(blk.data_shards)) + f.blocks.get(b).and_then(Option::as_ref).is_some_and(|blk| blk.shards.len() >= usize::from(blk.data_shards)) }); if complete { match assemble(f, frame, payload_len, now) { diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index ddbf895..088fab8 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -2,8 +2,9 @@ The protocol between a GameStream host (Sunshine, later Lumen) and a client (Arena Domi TV). Written from the Wolf protocol docs in our own words, completed with facts read in Moonshine (BSD-2). Every fact has a tag; every gap is -marked **UNKNOWN Un** with what would settle it. Status: 2026-11-25, Phase A (docs + API, no logic yet). U9 narrowed and U14 settled from the approved sources the same -day (sections 3.1, 5.3); the rest waits for the lab capture ([LAB_CAPTURE.md](LAB_CAPTURE.md)). +marked **UNKNOWN Un** with what would settle it. Status: 2026-11-25, implemented as stream-v0.1.0 per these docs and the approved sources; interop with a real host +is unverified (the lab capture was not run, [LAB_CAPTURE.md](LAB_CAPTURE.md)). U9 narrowed and U14 settled from the +approved sources (sections 3.1, 5.3); where an UNKNOWN remains, the code follows the Wolf-documented behaviour. **Tags** (details in the source log, [CLEAN_ROOM.md](CLEAN_ROOM.md)): @@ -510,7 +511,8 @@ Every 4 data packets are followed by 2 parity packets [W6]. A block is the 4 dat start at a multiple of 4 [M]; the base sequence and timestamp in the FEC header are the first data packet's [M]. Shards are the encrypted payloads (all 4 the same length, constant-bitrate Opus) [M]. A lost data packet is rebuilt from any 4 of the 6 packets; its RTP header comes from the base values (sequence = base + index, timestamp = base + -index × duration) [M]. +index × duration) [M]. DomiStream waits for the jitter budget (`loss_timeout`) before rebuilding a missing packet, +so a packet that arrives late is played as sent rather than as a rebuilt copy. UNKNOWN U16 (blocks audio FEC): the parity matrix. It is a fixed 2 × 4 matrix over GF(2^8), **not** the default construction of the RS library Moonshine uses [M]. The only value we have seen carries GPL provenance and is not