use std::path::PathBuf;
use std::time::Duration;
use clap::builder::BoolishValueParser;
use clap::{ArgAction, Args, ValueEnum};
use moqros::ros::QosProfile;
use crate::url::{insecurity, redact_url};
#[derive(Debug, Clone, Args)]
pub struct RelayArgs {
#[arg(long, env = "MOQROS_URL", default_value = "http://localhost:4443/anon")]
pub url: moqros::Url,
#[arg(long, env = "MOQROS_INSECURE", action = ArgAction::Set, value_parser = BoolishValueParser::new(),
num_args = 0..=1, require_equals = true, default_value_t = false, default_missing_value = "true")]
pub insecure: bool,
#[arg(long, env = "MOQROS_TLS_CERT", requires = "tls_key")]
pub tls_cert: Option<PathBuf>,
#[arg(long, env = "MOQROS_TLS_KEY", requires = "tls_cert")]
pub tls_key: Option<PathBuf>,
}
impl RelayArgs {
pub fn config(&self) -> moqros::ClientConfig {
moqros::ClientConfig {
url: self.url.clone(),
insecure: self.insecure,
tls_cert: self.tls_cert.clone(),
tls_key: self.tls_key.clone(),
}
}
pub fn redacted_url(&self) -> moqros::Url {
redact_url(&self.url)
}
pub fn warn_if_insecure(&self) {
if let Some(reason) = insecurity(&self.url, self.insecure) {
tracing::warn!("{reason}; anyone on the network path can read or alter the stream");
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum Format {
Video,
Json,
Cdr,
}
impl Format {
pub fn name(self) -> &'static str {
match self {
Self::Video => "video",
Self::Json => "json",
Self::Cdr => "cdr",
}
}
pub fn data(self) -> Option<moqros::DataFormat> {
match self {
Self::Video => None,
Self::Json => Some(moqros::DataFormat::Json),
Self::Cdr => Some(moqros::DataFormat::Cdr),
}
}
}
#[derive(Debug, Clone, Args)]
pub struct QosArgs {
#[arg(long, action = ArgAction::Set, value_parser = BoolishValueParser::new(),
num_args = 0..=1, require_equals = true, default_value_t = false, default_missing_value = "true")]
pub reliable: bool,
}
impl QosArgs {
pub fn profile(&self) -> QosProfile {
if self.reliable {
QosProfile::default().keep_last(5)
} else {
QosProfile::sensor_data()
}
}
}
#[derive(Debug, Clone, Args)]
pub struct EncoderArgs {
#[arg(long, default_value_t = moqros::Codec::default())]
pub codec: moqros::Codec,
#[arg(long, default_value_t = moqros::Hardware::default())]
pub hw: moqros::Hardware,
#[arg(long, default_value_t = 2_000_000)]
pub bitrate: u32,
#[arg(long, default_value_t = 30.0)]
pub fps: f32,
#[arg(long, default_value_t = 2000)]
pub keyframe_interval_ms: u64,
#[arg(id = "rendition", long = "rendition", value_parser = parse_rendition)]
pub renditions: Vec<moqros::RenditionSettings>,
}
fn parse_rendition(spec: &str) -> Result<moqros::RenditionSettings, String> {
let invalid = || format!("expected HEIGHTp@BITRATE (e.g. 360p@500000), got {spec:?}");
let (height, bitrate) = spec.split_once('@').ok_or_else(invalid)?;
let height: u32 = height.trim().trim_end_matches('p').parse().map_err(|_| invalid())?;
let bitrate: u32 = bitrate.trim().parse().map_err(|_| invalid())?;
if height < 2 || bitrate == 0 {
return Err(invalid());
}
Ok(moqros::RenditionSettings { height, bitrate })
}
impl EncoderArgs {
pub fn settings(&self) -> moqros::EncoderSettings {
moqros::EncoderSettings {
codec: self.codec,
bitrate: self.bitrate,
max_fps: self.fps,
keyframe_interval: Duration::from_millis(self.keyframe_interval_ms),
hardware: self.hw,
..Default::default()
}
}
pub fn broadcast_settings(&self) -> moqros::ImageBroadcastSettings {
moqros::ImageBroadcastSettings {
encoder: self.settings(),
renditions: self.renditions.clone(),
}
}
}
#[derive(Debug, Clone, Args)]
pub struct DiagnosticsArgs {
#[arg(long, default_value_t = 1000)]
pub diagnostics_period_ms: u64,
}
impl DiagnosticsArgs {
pub fn period(&self) -> Option<Duration> {
(self.diagnostics_period_ms > 0).then(|| Duration::from_millis(self.diagnostics_period_ms))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_renditions() {
let rendition = |height, bitrate| moqros::RenditionSettings { height, bitrate };
assert_eq!(parse_rendition("360p@500000"), Ok(rendition(360, 500_000)));
assert_eq!(parse_rendition("720@2000000"), Ok(rendition(720, 2_000_000)));
for bad in [
"360p",
"@500000",
"360p@",
"p@1",
"1p@100",
"360p@0",
"360p@1.5M",
"-360p@1",
] {
assert!(parse_rendition(bad).is_err(), "{bad}");
}
}
}