use std::ffi::OsString;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;
use crate::net::tunnel::Provider;
use crate::types::Encoder;
use crate::util::ratio::Ratio;
pub const DEFAULT_RECONNECT: Duration = Duration::from_secs(120);
pub const SHARE_TUNNEL_TTL: Duration = Duration::from_secs(60 * 60);
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InputSpec {
Path(OsString),
Stdin,
FfmpegArgs(Vec<OsString>),
TestPattern,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Size {
pub width: u32,
pub height: u32,
}
impl FromStr for Size {
type Err = &'static str;
fn from_str(s: &str) -> Result<Size, Self::Err> {
let (w, h) = s.split_once(['x', 'X']).ok_or("expected WxH")?;
let width = w.parse().map_err(|_| "invalid width")?;
let height = h.parse().map_err(|_| "invalid height")?;
if width == 0 || height == 0 || width % 2 != 0 || height % 2 != 0 {
return Err("width and height must be positive and even");
}
Ok(Size { width, height })
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RawVideoSpec {
pub size: Size,
pub fps: Ratio,
pub pix_fmt: String,
}
impl FromStr for RawVideoSpec {
type Err = &'static str;
fn from_str(s: &str) -> Result<RawVideoSpec, Self::Err> {
let (size, rest) = s.split_once('@').ok_or("expected WxH@FPS:PIXFMT")?;
let (fps, pix_fmt) = rest.split_once(':').ok_or("expected WxH@FPS:PIXFMT")?;
if pix_fmt.is_empty()
|| !pix_fmt
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'_')
{
return Err("invalid pixel format");
}
Ok(RawVideoSpec {
size: size.parse()?,
fps: fps.parse()?,
pix_fmt: pix_fmt.to_owned(),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RawAudioSpec {
pub sample_rate: u32,
pub channels: u8,
pub sample_fmt: String,
}
impl RawAudioSpec {
pub fn session_pcm(airplay: bool) -> RawAudioSpec {
RawAudioSpec {
sample_rate: if airplay { 44_100 } else { 48_000 },
channels: 2,
sample_fmt: "s16le".into(),
}
}
}
impl FromStr for RawAudioSpec {
type Err = &'static str;
fn from_str(s: &str) -> Result<RawAudioSpec, Self::Err> {
let mut parts = s.split(':');
let (Some(rate), Some(ch), Some(fmt), None) =
(parts.next(), parts.next(), parts.next(), parts.next())
else {
return Err("expected RATE:CH:FMT");
};
let sample_rate = rate.parse().map_err(|_| "invalid sample rate")?;
let channels = ch.parse().map_err(|_| "invalid channel count")?;
if sample_rate == 0 || channels == 0 {
return Err("sample rate and channels must be positive");
}
if fmt.is_empty() || !fmt.bytes().all(|b| b.is_ascii_alphanumeric()) {
return Err("invalid sample format");
}
Ok(RawAudioSpec {
sample_rate,
channels,
sample_fmt: fmt.to_owned(),
})
}
}
#[derive(Debug, Clone)]
pub struct PlayOptions {
pub to: Vec<String>,
pub reconnect: Duration,
pub browser: BrowserOptions,
pub latency_ms: Option<u32>,
pub size: Option<Size>,
pub fps: Option<Ratio>,
pub bitrate_kbps: Option<u32>,
pub encoder: Encoder,
pub no_audio: bool,
pub looped: bool,
pub realtime: Option<bool>,
pub raw_video: Option<RawVideoSpec>,
pub raw_audio: Option<RawAudioSpec>,
pub ffmpeg: Option<PathBuf>,
pub input: InputSpec,
pub threads: usize,
}
pub const BROWSER_TARGET: &str = "browser";
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct BrowserOptions {
pub port: Option<u16>,
pub open: bool,
pub wait_for_viewer: bool,
pub new_token: bool,
pub tunnel: Option<Provider>,
pub tunnel_ttl: Option<Duration>,
}
#[derive(Debug, Clone, Default)]
pub struct ShareOptions {
pub to: Vec<String>,
pub reconnect: Duration,
pub latency_ms: Option<u32>,
pub size: Option<Size>,
pub fps: Option<Ratio>,
pub bitrate_kbps: Option<u32>,
pub no_audio: bool,
pub open: bool,
pub player: BrowserOptions,
pub tunnel: Option<Provider>,
pub tunnel_ttl: Option<Duration>,
pub yes: bool,
pub page: Option<PageOptions>,
}
#[derive(Debug, Clone, Default)]
pub struct ScreenOptions {
pub share: ShareOptions,
pub target: ScreenTarget,
pub engine: Option<ScreenEngine>,
pub browser: Option<PathBuf>,
pub ffmpeg: Option<PathBuf>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScreenTarget {
Display(u32),
DisplayNamed(String),
Window(String),
Pick,
}
impl Default for ScreenTarget {
fn default() -> Self {
ScreenTarget::Display(1)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScreenEngine {
Browser,
Ffmpeg,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct PageOptions {
pub url: String,
pub profile: Option<PathBuf>,
pub visible: bool,
pub reload: Option<Duration>,
pub browser: Option<PathBuf>,
}
#[derive(Debug, Clone)]
pub struct PipelineOptions {
pub raw: bool,
pub raw_video: Option<RawVideoSpec>,
pub raw_audio: Option<RawAudioSpec>,
pub ffmpeg: Option<PathBuf>,
pub input: InputSpec,
}
#[derive(Debug, Clone)]
pub enum Command {
Discover {
timeout: Duration,
json: bool,
interfaces: Vec<std::net::Ipv4Addr>,
},
Pair {
target: String,
pin: Option<String>,
},
Unpair {
target: String,
},
Play(PlayOptions),
TestPattern(PlayOptions),
Share(ShareOptions),
Screen(ScreenOptions),
DebugPipeline(PipelineOptions),
}
impl Command {
pub fn name(&self) -> &'static str {
match self {
Command::Discover { .. } => "discover",
Command::Pair { .. } => "pair",
Command::Unpair { .. } => "unpair",
Command::Play(_) => "play",
Command::TestPattern(_) => "test-pattern",
Command::Share(_) => "share",
Command::Screen(_) => "screen",
Command::DebugPipeline(_) => "debug-pipeline",
}
}
}