//! `GET /launch` and `GET /resume` (`docs/PROTOCOL.md` 1.4). use super::{HttpRequest, Scheme, build, check_unique_id}; use crate::util::{cap, check_rtsp_url, hex_encode, malformed}; use crate::{Result, SessionKeys}; /// Maximum size of a launch or resume answer, in bytes. pub const MAX_LAUNCH_RESPONSE_BYTES: usize = 16 * 1024; /// Maximum length of `sessionUrl0`, in bytes. pub const MAX_SESSION_URL_BYTES: usize = 256; /// Audio channel layouts the client can ask for. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum AudioLayout { /// 2.0. Stereo, /// 5.1. Surround51, /// 7.1. Surround71, } impl AudioLayout { /// Channel count. #[must_use] pub const fn channels(self) -> u8 { match self { Self::Stereo => 2, Self::Surround51 => 6, Self::Surround71 => 8, } } /// The speaker mask (WAVE channel-mask bits: FL FR, + FC LFE BL BR, + SL SR). Stereo's `0x3` matches the /// 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 { Self::Stereo => 0x0003, Self::Surround51 => 0x003F, Self::Surround71 => 0x063F, } } /// `surroundAudioInfo`: `(channel_mask << 16) | channel_count`. #[must_use] pub const fn surround_audio_info(self) -> u32 { (self.channel_mask() << 16) | self.channels() as u32 } } /// What to launch and how. #[derive(Clone, Debug, PartialEq, Eq)] pub struct LaunchParams { /// App `ID` from [`super::parse_app_list`]. pub app_id: u32, /// Stream width in pixels. pub width: u32, /// Stream height in pixels. pub height: u32, /// Frames per second. pub fps: u32, /// Fresh keys for this session (never reuse; see [`SessionKeys`]). pub keys: SessionKeys, /// Ask for HDR (`hdrMode=1`). pub hdr: bool, /// Audio layout (`surroundAudioInfo`). pub audio: AudioLayout, } /// What the host answers to launch or resume. #[derive(Clone, Debug, PartialEq, Eq)] pub struct LaunchResponse { /// `gamesession == 1`. pub game_session: bool, /// `sessionUrl0`, e.g. `rtsp://192.0.2.10:48010`; `None` when missing (UNKNOWN U4). pub session_url: Option, } /// Builds `GET /launch` (HTTPS). /// /// # Errors /// [`crate::Error::Malformed`] for a zero size or frame rate, or a bad `unique_id`. pub fn launch_request(params: &LaunchParams, unique_id: &str) -> Result { check_unique_id(unique_id)?; if params.width == 0 || params.height == 0 || params.fps == 0 { return Err(malformed("launch mode")); } let app = params.app_id.to_string(); let mode = format!("{}x{}x{}", params.width, params.height, params.fps); let rikey = hex_encode(¶ms.keys.rikey); let rikeyid = params.keys.rikeyid.to_string(); let hdr = if params.hdr { "1" } else { "0" }; let audio = params.audio.surround_audio_info().to_string(); Ok(build( Scheme::Https, "/launch", &[ ("uniqueid", unique_id), ("appid", &app), ("mode", &mode), ("rikey", &rikey), ("rikeyid", &rikeyid), ("hdrMode", hdr), ("surroundAudioInfo", &audio), ], None, )) } /// Builds `GET /resume` (HTTPS) with fresh keys. /// /// # Errors /// As [`launch_request`]. pub fn resume_request(keys: &SessionKeys, unique_id: &str) -> Result { check_unique_id(unique_id)?; let rikey = hex_encode(&keys.rikey); let rikeyid = keys.rikeyid.to_string(); Ok(build(Scheme::Https, "/resume", &[("uniqueid", unique_id), ("rikey", &rikey), ("rikeyid", &rikeyid)], None)) } /// Parses a launch or resume answer. /// /// # Errors /// [`crate::Error::TooLarge`] over [`MAX_LAUNCH_RESPONSE_BYTES`] or [`MAX_SESSION_URL_BYTES`], /// [`crate::Error::HostStatus`] for a non-200 answer, [`crate::Error::Malformed`] for a `sessionUrl0` that is not an /// `rtsp://host:port` URL. pub fn parse_launch_response(body: &[u8]) -> Result { let doc = super::document(body, MAX_LAUNCH_RESPONSE_BYTES, "launch answer")?; let game_session = doc.root.child("gamesession").is_some_and(|g| g.text == "1"); let session_url = match doc.root.child("sessionUrl0") { None => None, Some(e) => { cap("sessionUrl0", MAX_SESSION_URL_BYTES, e.text.len())?; check_rtsp_url("sessionUrl0", &e.text)?; Some(e.text.clone()) } }; Ok(LaunchResponse { game_session, session_url }) }