use std::time::Duration;
use anyhow::Context;
use clap::Parser;
use futures::StreamExt;
use futures::stream::FuturesUnordered;
use moqros::ros::{spawn_ros_to_moq, spawn_ros_to_moq_data, topic_type};
use moqros_cli::{Format, PubTopic, QosArgs, RelayArgs, SharedNode, TopicSpec, init_logging, spawn_spinner};
#[derive(Debug, Parser)]
#[command(version, about)]
struct Cli {
#[arg(long, default_value = "/image_raw")]
topic: Vec<TopicSpec>,
#[arg(long)]
broadcast: Option<String>,
#[arg(long, value_enum, default_value_t = Format::Video)]
format: Format,
#[arg(id = "type", long = "type")]
ros_type: Option<String>,
#[arg(long, default_value_t = moqros::DataMode::default())]
mode: moqros::DataMode,
#[arg(long, default_value_t = moqros::Codec::default())]
codec: moqros::Codec,
#[arg(long, default_value_t = 2_000_000)]
bitrate: u32,
#[arg(long, default_value_t = 30.0)]
fps: f32,
#[arg(long, default_value_t = 2000)]
keyframe_interval_ms: u64,
#[command(flatten)]
relay: RelayArgs,
#[command(flatten)]
qos: QosArgs,
}
impl Cli {
fn start(
&self,
node: &mut moqros::ros::r2r::Node,
publisher: &moqros::Publisher,
topic: &PubTopic,
) -> anyhow::Result<Option<tokio::task::JoinHandle<moqros::Result<moqros::ros::BridgeStats>>>> {
let bridge = match topic.format.data() {
None => {
let settings = moqros::EncoderSettings {
codec: self.codec,
bitrate: self.bitrate,
max_fps: self.fps,
keyframe_interval: Duration::from_millis(self.keyframe_interval_ms),
..Default::default()
};
let images = publisher.create_image_broadcast(&topic.broadcast, settings)?;
spawn_ros_to_moq(node, &topic.topic, self.qos.profile(), images)?
}
Some(format) => {
let ros_type = match &topic.ros_type {
Some(ros_type) => ros_type.clone(),
None => match topic_type(node, &topic.topic)? {
Some(ros_type) => ros_type,
None => return Ok(None),
},
};
let settings = moqros::DataSettings {
format,
mode: topic.mode,
ros_type: Some(ros_type),
};
let data = publisher.create_data_broadcast(&topic.broadcast, settings)?;
spawn_ros_to_moq_data(node, &topic.topic, self.qos.profile(), data)?
}
};
tracing::info!(
topic = topic.topic,
format = ?topic.format,
broadcast = topic.broadcast,
"bridging ROS -> MoQ"
);
Ok(Some(bridge))
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
init_logging();
let (cli, node) = moqros_cli::init::<Cli>("moqros_pub")?;
let mut pending = moqros_cli::pub_topics(
cli.topic.clone(),
cli.format,
cli.mode,
cli.ros_type.clone(),
cli.broadcast.clone(),
)?;
let publisher = moqros::Publisher::connect(&cli.relay.config())?;
tracing::info!(url = %cli.relay.url, topics = pending.len(), "connecting to the relay");
let node = SharedNode::new(node);
let mut spinner = spawn_spinner(node.clone());
let mut bridges = FuturesUnordered::new();
let mut logged = false;
let ctrl_c = tokio::signal::ctrl_c();
tokio::pin!(ctrl_c);
loop {
let mut waiting = Vec::new();
for topic in std::mem::take(&mut pending) {
let topic_name = topic.topic.clone();
match node.with(|node| cli.start(node, &publisher, &topic))? {
Some(bridge) => bridges.push(async move { (topic_name, bridge.await) }),
None => {
waiting.push(topic.topic.clone());
pending.push(topic);
}
}
}
if !waiting.is_empty() && !logged {
tracing::info!(topics = ?waiting, "waiting for topics to appear to learn their types");
logged = true;
}
tokio::select! {
Some((topic, res)) = bridges.next() => {
let stats = res.context("bridge panicked")??;
tracing::info!(topic, ?stats, "topic stream ended");
return Ok(());
}
_ = tokio::time::sleep(Duration::from_millis(500)), if !pending.is_empty() => {}
res = publisher.closed() => return res.context("relay connection closed"),
err = spinner.failed() => return Err(err),
_ = &mut ctrl_c => {
tracing::info!("interrupted");
return Ok(());
}
}
}
}