moqros-cli 0.7.0

ROS 2 <-> Media over QUIC bridge nodes for images and other messages
Documentation
//! Bridge both ways in one node: publish ROS 2 topics as MoQ broadcasts (like
//! `moqros-pub`), and republish MoQ broadcasts as ROS 2 topics (like `moqros-sub`).
//! E.g. a robot that streams its camera and takes velocity commands from a browser.

use anyhow::ensure;
use clap::Parser;
use moqros::ros::StampSource;
use moqros_cli::{
	Bridges, BroadcastSpec, DiagnosticsArgs, EncoderArgs, Format, QosArgs, Relay, RelayArgs, TopicSpec, init_logging,
};

#[derive(Debug, Parser)]
#[command(version, about)]
struct Cli {
	/// ROS topic to publish to the relay (ROS -> MoQ); repeat for several. Options as in
	/// `moqros-pub`: `/odom,format=json,type=nav_msgs/msg/Odometry,mode=stream,broadcast=robot/odom`.
	#[arg(long)]
	topic: Vec<TopicSpec>,

	/// Broadcast to republish on ROS (MoQ -> ROS); repeat for several. Options as in
	/// `moqros-sub`: `robot/cmd_vel,topic=/cmd_vel,type=geometry_msgs/msg/Twist,frame_id=cam,stamp=receive,max_height=480,encoding=bgr8`.
	/// The topic defaults to `/moqros/<broadcast>`, or `/moqros/image` for a lone broadcast.
	#[arg(long)]
	broadcast: Vec<BroadcastSpec>,

	/// How to send the topics: `video` encodes `sensor_msgs/msg/Image` or
	/// `CompressedImage`; `json` and `cdr` forward any message type as data.
	#[arg(long, value_enum, default_value_t = Format::Video)]
	format: Format,

	/// For `json` and `cdr` topics: `snapshot` sends each message on its own so subscribers
	/// skip to the newest; `stream` delivers every message in order.
	#[arg(long, default_value_t = moqros::DataMode::default())]
	mode: moqros::DataMode,

	/// `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,

	#[command(flatten)]
	encoder: EncoderArgs,

	#[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_bridge")?;
	ensure!(
		!cli.topic.is_empty() || !cli.broadcast.is_empty(),
		"nothing to bridge; give at least one --topic or --broadcast"
	);
	let topics = match cli.topic.is_empty() {
		true => Vec::new(),
		false => moqros_cli::pub_topics(cli.topic, cli.format, cli.mode, None, None)?,
	};
	let broadcasts = match cli.broadcast.is_empty() {
		true => Vec::new(),
		false => moqros_cli::sub_broadcasts(
			cli.broadcast,
			None,
			None,
			cli.frame_id.as_deref(),
			cli.stamp,
			cli.max_height,
			cli.encoding,
		)?,
	};
	moqros_cli::check_no_loop(&topics, &broadcasts, None)?;

	cli.relay.warn_if_insecure();
	// One session carries both directions.
	let (publisher, subscriber) = moqros::connect_both(&cli.relay.config())?;
	tracing::info!(
		url = %cli.relay.redacted_url(),
		topics = topics.len(),
		broadcasts = broadcasts.len(),
		"connecting to the relay"
	);
	let bridges = Bridges {
		topics,
		broadcasts,
		video: cli.encoder.broadcast_settings(),
		qos: cli.qos.profile(),
		diagnostics_period: cli.diagnostics.period(),
		discovery: None,
	};
	moqros_cli::run_bridges(node, Relay::both(publisher, subscriber), bridges).await
}