[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.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
use crate::Result;
|
||||
use crate::input::InputEvent;
|
||||
use crate::util::{byte, cap, malformed, need, u16_le, u32_be};
|
||||
|
||||
/// Length of the message header (type + length).
|
||||
pub const MESSAGE_HEADER_LEN: usize = 4;
|
||||
@@ -48,14 +49,37 @@ pub enum MessageType {
|
||||
TriggerEffect = 0x5503,
|
||||
}
|
||||
|
||||
impl MessageType {
|
||||
const ALL: [Self; 16] = [
|
||||
Self::StartA,
|
||||
Self::StartB,
|
||||
Self::InvalidateRefFrames,
|
||||
Self::IdrFrame,
|
||||
Self::LossStats,
|
||||
Self::FrameStats,
|
||||
Self::InputData,
|
||||
Self::PeriodicPing,
|
||||
Self::Termination,
|
||||
Self::TerminationExtended,
|
||||
Self::HdrMode,
|
||||
Self::RumbleData,
|
||||
Self::RumbleTriggers,
|
||||
Self::MotionEvent,
|
||||
Self::RgbLed,
|
||||
Self::TriggerEffect,
|
||||
];
|
||||
}
|
||||
|
||||
impl TryFrom<u16> for MessageType {
|
||||
type Error = crate::Error;
|
||||
|
||||
/// # Errors
|
||||
/// [`crate::Error::UnexpectedValue`] for an unknown code. Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// [`crate::Error::UnexpectedValue`] for an unknown code.
|
||||
fn try_from(code: u16) -> Result<Self> {
|
||||
let _ = code;
|
||||
Err(crate::Error::Unimplemented)
|
||||
Self::ALL
|
||||
.into_iter()
|
||||
.find(|t| *t as u16 == code)
|
||||
.ok_or(crate::Error::UnexpectedValue { what: "control message type", value: code.into() })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,11 +96,16 @@ pub struct RawMessage<'a> {
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::TooLarge`] over [`MAX_MESSAGE_BYTES`], [`crate::Error::Truncated`] under
|
||||
/// [`MESSAGE_HEADER_LEN`], [`crate::Error::Malformed`] for a length that does not match. Phase A: always
|
||||
/// [`crate::Error::Unimplemented`].
|
||||
/// [`MESSAGE_HEADER_LEN`], [`crate::Error::Malformed`] for a length that does not match.
|
||||
pub fn parse_message(message: &[u8]) -> Result<RawMessage<'_>> {
|
||||
let _ = message;
|
||||
Err(crate::Error::Unimplemented)
|
||||
const W: &str = "control message";
|
||||
cap(W, MAX_MESSAGE_BYTES, message.len())?;
|
||||
need(W, MESSAGE_HEADER_LEN, message.len())?;
|
||||
let message_type = u16_le(W, message, 0)?;
|
||||
if usize::from(u16_le(W, message, 2)?) != message.len() - MESSAGE_HEADER_LEN {
|
||||
return Err(malformed("control message length"));
|
||||
}
|
||||
Ok(RawMessage { message_type, body: message.get(MESSAGE_HEADER_LEN..).unwrap_or(&[]) })
|
||||
}
|
||||
|
||||
/// Motion sensor kinds of `MOTION_EVENT`.
|
||||
@@ -169,14 +198,70 @@ pub enum HostMessage {
|
||||
},
|
||||
}
|
||||
|
||||
/// Interprets a decrypted message from the host.
|
||||
fn exact(body: &[u8], len: usize, what: &'static str) -> Result<()> {
|
||||
if body.len() == len { Ok(()) } else { Err(malformed(what)) }
|
||||
}
|
||||
|
||||
/// Interprets a decrypted message from the host. Bodies are little-endian (UNKNOWN U23), except the extended
|
||||
/// termination code (big-endian).
|
||||
///
|
||||
/// # Errors
|
||||
/// As [`parse_message`]; [`crate::Error::Malformed`] when a known type's body has the wrong length;
|
||||
/// [`crate::Error::UnexpectedValue`] for an unknown motion kind. Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// [`crate::Error::UnexpectedValue`] for an unknown motion kind.
|
||||
pub fn parse_host_message(message: &[u8]) -> Result<HostMessage> {
|
||||
let _ = message;
|
||||
Err(crate::Error::Unimplemented)
|
||||
const W: &str = "host message";
|
||||
let raw = parse_message(message)?;
|
||||
let b = raw.body;
|
||||
let u16_at = |at: usize| u16_le(W, b, at);
|
||||
Ok(match raw.message_type {
|
||||
0x010b => {
|
||||
exact(b, 10, "rumble body")?;
|
||||
HostMessage::Rumble { controller: u16_at(4)?, low_frequency: u16_at(6)?, high_frequency: u16_at(8)? }
|
||||
}
|
||||
0x5500 => {
|
||||
exact(b, 6, "trigger rumble body")?;
|
||||
HostMessage::RumbleTriggers { controller: u16_at(0)?, left: u16_at(2)?, right: u16_at(4)? }
|
||||
}
|
||||
0x5501 => {
|
||||
exact(b, 5, "motion event body")?;
|
||||
let kind = match byte(W, b, 4)? {
|
||||
1 => MotionKind::Accelerometer,
|
||||
2 => MotionKind::Gyroscope,
|
||||
other => return Err(crate::Error::UnexpectedValue { what: "motion kind", value: other.into() }),
|
||||
};
|
||||
HostMessage::MotionEvent { controller: u16_at(0)?, rate_hz: u16_at(2)?, kind }
|
||||
}
|
||||
0x5502 => {
|
||||
exact(b, 5, "rgb led body")?;
|
||||
HostMessage::RgbLed { controller: u16_at(0)?, r: byte(W, b, 2)?, g: byte(W, b, 3)?, b: byte(W, b, 4)? }
|
||||
}
|
||||
0x0109 => {
|
||||
exact(b, 4, "termination body")?;
|
||||
HostMessage::Termination { error_code: Some(u32_be(W, b, 0)?) }
|
||||
}
|
||||
0x0100 => HostMessage::Termination { error_code: None },
|
||||
0x010e => {
|
||||
let enabled = byte(W, b, 0).map_err(|_| malformed("hdr mode body"))? != 0;
|
||||
let metadata = match b.len() {
|
||||
1 => None,
|
||||
31 => {
|
||||
let v = |i: usize| u16_le(W, b, 1 + 2 * i);
|
||||
Some(HdrMetadata {
|
||||
display_primaries: [(v(0)?, v(1)?), (v(2)?, v(3)?), (v(4)?, v(5)?)],
|
||||
white_point: (v(6)?, v(7)?),
|
||||
max_display_luminance: v(8)?,
|
||||
min_display_luminance: v(9)?,
|
||||
max_content_light_level: v(10)?,
|
||||
max_frame_average_light_level: v(11)?,
|
||||
max_full_frame_luminance: v(12)?,
|
||||
})
|
||||
}
|
||||
_ => return Err(malformed("hdr mode body")),
|
||||
};
|
||||
HostMessage::HdrMode { enabled, metadata }
|
||||
}
|
||||
other => HostMessage::Other { message_type: other, body_len: b.len() },
|
||||
})
|
||||
}
|
||||
|
||||
/// A message to the host.
|
||||
@@ -204,9 +289,23 @@ pub enum ClientMessage {
|
||||
/// Serializes a message to the host (type, length, body), ready for [`super::ControlCipher::seal`].
|
||||
///
|
||||
/// # Errors
|
||||
/// [`crate::Error::Unsupported`] for a message whose body is still UNKNOWN; input errors as
|
||||
/// [`crate::input::encode_input`]. Phase A: always [`crate::Error::Unimplemented`].
|
||||
/// [`crate::Error::Unsupported`] for a message whose body is still UNKNOWN (U10: START_A, START_B,
|
||||
/// PERIODIC_PING, IDR request; refused rather than guessed); input errors as [`crate::input::encode_input`].
|
||||
pub fn encode_client_message(message: &ClientMessage) -> Result<Vec<u8>> {
|
||||
let _ = message;
|
||||
Err(crate::Error::Unimplemented)
|
||||
match message {
|
||||
ClientMessage::InvalidateRefFrames { first, last } => {
|
||||
let mut m = Vec::with_capacity(28);
|
||||
m.extend_from_slice(&(MessageType::InvalidateRefFrames as u16).to_le_bytes());
|
||||
m.extend_from_slice(&24u16.to_le_bytes());
|
||||
m.extend_from_slice(&first.to_le_bytes());
|
||||
m.extend_from_slice(&[0; 4]);
|
||||
m.extend_from_slice(&last.to_le_bytes());
|
||||
m.extend_from_slice(&[0; 12]);
|
||||
Ok(m)
|
||||
}
|
||||
ClientMessage::Input(e) => crate::input::encode_input(e),
|
||||
ClientMessage::StartA | ClientMessage::StartB | ClientMessage::PeriodicPing | ClientMessage::RequestIdrFrame => {
|
||||
Err(crate::Error::Unsupported { what: "a control message whose body is still UNKNOWN (U10)" })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user