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 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,
}
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()
}
}
}