moqros-cli 0.7.0

ROS 2 <-> Media over QUIC bridge nodes for images and other messages
Documentation
//! Subscribe to MoQ broadcasts and republish each as a ROS 2 topic: video decoded into
//! images, or data tracks as the message type they carry.

use clap::Parser;
use moqros::ros::StampSource;
use moqros_cli::{Bridges, BroadcastSpec, DiagnosticsArgs, Discovery, QosArgs, Relay, RelayArgs, init_logging};

#[derive(Debug, Parser)]
#[command(version, about)]
struct Cli {
	/// Broadcast to subscribe to; repeat for several. Options after the name override
	/// the flags below for that broadcast: `chatter,topic=/chatter,type=std_msgs/msg/String,frame_id=cam,stamp=receive,max_height=480,encoding=bgr8`.
	/// Defaults to `image_raw` without `--broadcast-prefix`.
	#[arg(long)]
	broadcast: Vec<BroadcastSpec>,

	/// Also republish every broadcast announced under this prefix (whole path segments), on
	/// the topic `--topic-template` names; repeat for several. Broadcast names come from whoever
	/// announces them: ones that don't make plain topic names are skipped.
	#[arg(long)]
	broadcast_prefix: Vec<String>,

	/// Topic of a broadcast found by `--broadcast-prefix`, `{name}` standing for its name.
	#[arg(long, default_value = moqros_cli::DEFAULT_TOPIC_TEMPLATE)]
	topic_template: String,

	/// Topic to publish on: decoded images for video, or the data track's messages.
	/// Single broadcast only; defaults to `/moqros/image` for one broadcast, and to
	/// `/moqros/<broadcast>` for each of several.
	#[arg(long)]
	topic: Option<String>,

	/// `header.frame_id` of the republished images; defaults to the one the publisher announces.
	#[arg(long)]
	frame_id: Option<String>,

	/// `header.stamp` of the republished images: `source` uses the capture time the publisher
	/// announces (falling back to the receive time), `receive` the local receive time.
	#[arg(long, default_value_t = StampSource::default())]
	stamp: StampSource,

	/// Pick the largest video rendition at most this tall (the smallest if none is), instead of
	/// the largest.
	#[arg(long)]
	max_height: Option<u32>,

	/// Pixel encoding of the republished images: `rgb8`, `bgr8`, `rgba8`, `bgra8` or `mono8`.
	#[arg(long, default_value = "rgb8", value_parser = moqros_cli::parse_encoding)]
	encoding: moqros::PixelFormat,

	/// Message type to republish a data track as; defaults to the type its catalog names.
	/// Single broadcast only.
	#[arg(id = "type", long = "type")]
	ros_type: Option<String>,

	#[command(flatten)]
	relay: RelayArgs,

	#[command(flatten)]
	qos: QosArgs,

	#[command(flatten)]
	diagnostics: DiagnosticsArgs,
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
	init_logging();
	let (cli, node) = moqros_cli::init::<Cli>("moqros_sub")?;
	let discovery = match cli.broadcast_prefix.is_empty() {
		true => None,
		false => Some(Discovery::new(
			cli.broadcast_prefix,
			cli.topic_template,
			cli.frame_id.clone(),
			cli.stamp,
			cli.max_height,
			cli.encoding,
		)?),
	};
	let specs = match (cli.broadcast.is_empty(), &discovery) {
		(true, None) => vec!["image_raw".parse().expect("a valid spec")],
		_ => cli.broadcast,
	};
	let broadcasts = match specs.is_empty() {
		true => Vec::new(),
		false => moqros_cli::sub_broadcasts(
			specs,
			cli.topic,
			cli.ros_type,
			cli.frame_id.as_deref(),
			cli.stamp,
			cli.max_height,
			cli.encoding,
		)?,
	};

	cli.relay.warn_if_insecure();
	let subscriber = moqros::Subscriber::connect(&cli.relay.config())?;
	tracing::info!(url = %cli.relay.redacted_url(), broadcasts = broadcasts.len(), "connecting to the relay");
	let bridges = Bridges {
		topics: Vec::new(),
		broadcasts,
		video: Default::default(),
		qos: cli.qos.profile(),
		diagnostics_period: cli.diagnostics.period(),
		discovery,
	};
	moqros_cli::run_bridges(node, Relay::subscriber(subscriber), bridges).await
}