use std::time::Duration;
use clap::ValueEnum;
use hang::moq_net;
use tokio::io::AsyncWriteExt;
#[derive(ValueEnum, Clone, Copy)]
pub enum SubscribeFormat {
Fmp4,
}
#[derive(clap::Args, Clone)]
pub struct SubscribeArgs {
#[arg(long)]
pub format: SubscribeFormat,
#[arg(long, default_value = "500ms", value_parser = humantime::parse_duration)]
pub max_latency: Duration,
}
pub struct Subscribe {
broadcast: moq_net::BroadcastConsumer,
args: SubscribeArgs,
}
impl Subscribe {
pub fn new(broadcast: moq_net::BroadcastConsumer, args: SubscribeArgs) -> Self {
Self { broadcast, args }
}
pub async fn run(self) -> anyhow::Result<()> {
match self.args.format {
SubscribeFormat::Fmp4 => self.run_fmp4().await,
}
}
async fn run_fmp4(self) -> anyhow::Result<()> {
let mut stdout = tokio::io::stdout();
let mut fmp4 = moq_mux::export::Fmp4::new(self.broadcast)?.with_latency(self.args.max_latency);
while let Some(chunk) = fmp4.next().await? {
stdout.write_all(&chunk).await?;
stdout.flush().await?;
}
Ok(())
}
}