moqros-cli 0.8.0

ROS 2 <-> Media over QUIC bridge nodes for images and other messages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//! Running the bridges of one node over one relay connection.

use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::{Duration, Instant};

use anyhow::{Context, bail, ensure};
use futures::future::BoxFuture;
use futures::stream::FuturesUnordered;
use futures::{FutureExt, StreamExt};
use moqros::ros::{
	BitrateControl, Bridge, BridgeEnd, BridgeStats, MoqToRos, QosProfile, ROS_COMPRESSED_IMAGE_TYPE, r2r,
};
use tokio::sync::watch;

use crate::args::Format;
use crate::diagnostics::Diagnostics;
use crate::runtime::{SharedNode, Spinner, shutdown_signal, spawn_spinner};
use crate::spec::{Discovery, PubTopic, SubBroadcast, check_video_type};

/// What a bridge node does: ROS topics to publish to the relay, and broadcasts to republish
/// on ROS.
pub struct Bridges {
	pub topics: Vec<PubTopic>,
	pub broadcasts: Vec<SubBroadcast>,
	/// For video topics.
	pub video: moqros::ImageBroadcastSettings,
	pub qos: QosProfile,
	/// How often to publish the bridges' statistics on `/diagnostics`, if at all.
	pub diagnostics_period: Option<Duration>,
	/// Broadcasts to find by prefix and republish too.
	pub discovery: Option<Discovery>,
}

/// The relay connection of a bridge node: a publisher for its topics, a subscriber for its
/// broadcasts, or both over one connection.
pub struct Relay {
	publisher: Option<moqros::Publisher>,
	subscriber: Option<moqros::Subscriber>,
	session: moqros::Session,
}

impl Relay {
	/// For a node that only has topics.
	pub fn publisher(publisher: moqros::Publisher) -> Self {
		Self {
			session: publisher.session().clone(),
			publisher: Some(publisher),
			subscriber: None,
		}
	}

	/// For a node that only has broadcasts.
	pub fn subscriber(subscriber: moqros::Subscriber) -> Self {
		Self {
			session: subscriber.session().clone(),
			publisher: None,
			subscriber: Some(subscriber),
		}
	}

	/// Both directions over the one connection [`moqros::connect_both`] returns.
	pub fn both(publisher: moqros::Publisher, subscriber: moqros::Subscriber) -> Self {
		Self {
			session: publisher.session().clone(),
			publisher: Some(publisher),
			subscriber: Some(subscriber),
		}
	}
}

/// Run `bridges` until the relay connection gives up, the ROS spinner dies, or SIGINT or SIGTERM
/// arrives (which closes the connection first, so the relay ends this node's broadcasts and
/// subscriptions right away).
///
/// A topic without a type starts when it appears in the ROS graph. Broadcasts are waited for all
/// at once, each starting when it's announced; one whose topic can't be created (e.g. a message
/// type without bindings) is skipped. With `discovery`, every broadcast announced under its
/// prefixes is republished too, once.
///
/// A bridge that stops or fails is restarted on its own, after a delay that grows while it
/// keeps failing (up to 30 seconds), so one broken topic or broadcast doesn't take the others
/// down.
pub async fn run_bridges(node: r2r::Node, relay: Relay, bridges: Bridges) -> anyhow::Result<()> {
	let mut supervisor = Supervisor::new(node, relay, bridges)?;
	// Created once, so a signal arriving while the loop body runs isn't missed.
	let shutdown = shutdown_signal();
	tokio::pin!(shutdown);

	loop {
		supervisor.start_ready_topics()?;

		tokio::select! {
			Some((ready, broadcast)) = supervisor.subscribing.next() => supervisor.on_subscribed(ready, broadcast)?,
			Some(name) = next_announcement(&mut supervisor.announcements) => supervisor.on_announcement(&name),
			Some((name, value)) = next_parameter(&mut supervisor.parameters) => supervisor.on_parameter(&name, &value),
			Some(stopped) = supervisor.pub_bridges.next() => supervisor.on_topic_stopped(stopped),
			Some(stopped) = supervisor.sub_bridges.next() => supervisor.on_broadcast_stopped(stopped),
			_ = tokio::time::sleep(Duration::from_millis(500)), if !supervisor.pending.is_empty() => {}
			Some(diagnostics) = next_report(&mut supervisor.diagnostics) => {
				if let Err(err) = diagnostics.publish() {
					tracing::warn!(%err, "publishing diagnostics failed");
				}
			}
			// E.g. the relay refused the session: nothing would ever be announced.
			res = supervisor.relay.session.closed() => return res.context("relay connection closed"),
			err = supervisor.spinner.failed() => return Err(err),
			res = &mut shutdown => {
				tracing::info!(signal = res?, "shutting down");
				if let Err(err) = supervisor.relay.session.close().await {
					tracing::warn!(%err, "the relay connection didn't close cleanly");
				}
				return Ok(());
			}
		}
	}
}

/// A topic bridge that stopped.
type TopicStopped = (PubTopic, BridgeEnd);
/// A broadcast bridge that stopped.
type BroadcastStopped = (SubBroadcast, BridgeEnd);

/// The state of [`run_bridges`]: what runs, what waits to start, and what was taken.
struct Supervisor {
	node: Arc<SharedNode>,
	relay: Relay,
	/// For video topics; its bitrate follows the `bitrate` parameter.
	video: moqros::ImageBroadcastSettings,
	qos: QosProfile,
	discovery: Option<Discovery>,
	spinner: Spinner,
	/// Topics that haven't started (again) yet.
	pending: PendingTopics,
	restarts: Restarts,
	/// Broadcasts waited for, each until it's announced.
	subscribing: FuturesUnordered<BoxFuture<'static, (MoqToRos, SubBroadcast)>>,
	pub_bridges: FuturesUnordered<BoxFuture<'static, TopicStopped>>,
	sub_bridges: FuturesUnordered<BoxFuture<'static, BroadcastStopped>>,
	/// Broadcasts announced on the relay, with discovery.
	announcements: Option<moqros::Announcements>,
	/// Changes to the node's parameters, with video topics.
	parameters: Option<ParameterChanges>,
	/// The bitrate control of each running video topic.
	bitrates: HashMap<String, BitrateControl>,
	diagnostics: Option<(Diagnostics, tokio::time::Interval)>,
	/// Broadcasts and topics taken, so a found broadcast doesn't take one again.
	known_broadcasts: HashSet<String>,
	used_topics: HashSet<String>,
}

impl Supervisor {
	fn new(node: r2r::Node, relay: Relay, bridges: Bridges) -> anyhow::Result<Self> {
		let Bridges {
			topics,
			broadcasts,
			video,
			qos,
			diagnostics_period,
			discovery,
		} = bridges;
		ensure!(
			topics.is_empty() || relay.publisher.is_some(),
			"topics to publish need a publisher"
		);
		ensure!(
			(broadcasts.is_empty() && discovery.is_none()) || relay.subscriber.is_some(),
			"broadcasts to republish need a subscriber"
		);
		let known_broadcasts = broadcasts
			.iter()
			.map(|b| moqros::normalize_broadcast_name(&b.broadcast).to_string())
			.collect();
		let used_topics = broadcasts
			.iter()
			.map(|b| b.topic.clone())
			.chain(topics.iter().map(|t| t.topic.clone()))
			.collect();
		let announcements = relay
			.subscriber
			.as_ref()
			.filter(|_| discovery.is_some())
			.map(|subscriber| subscriber.announced(""));

		let node = SharedNode::new(node);
		// Only the ROS -> MoQ side subscribes to anything; a node that only publishes has
		// nothing to spin for.
		let spinner = match topics.is_empty() {
			true => Spinner::idle(),
			false => spawn_spinner(node.clone()),
		};
		// Parameter changes need the spinner, which only runs with topics; only video topics take any.
		let parameters = match topics.iter().any(|topic| topic.format == Format::Video) {
			true => Some(node.with(watch_parameters)?),
			false => None,
		};
		let diagnostics = diagnostics_period.and_then(|period| match node.with(Diagnostics::new) {
			Ok(diagnostics) => Some((diagnostics, tokio::time::interval(period))),
			Err(err) => {
				tracing::warn!(%err, "not publishing diagnostics; build with diagnostic_msgs in IDL_PACKAGE_FILTER");
				None
			}
		});

		let mut supervisor = Self {
			node,
			relay,
			video,
			qos,
			discovery,
			spinner,
			pending: PendingTopics::new(topics),
			restarts: Restarts::default(),
			subscribing: FuturesUnordered::new(),
			pub_bridges: FuturesUnordered::new(),
			sub_bridges: FuturesUnordered::new(),
			announcements,
			parameters,
			bitrates: HashMap::new(),
			diagnostics,
			known_broadcasts,
			used_topics,
		};
		// A topic's type depends on what its broadcast carries, so wait for each broadcast
		// (all at once) before creating its publisher.
		for broadcast in broadcasts {
			supervisor.subscribe(broadcast, Duration::ZERO);
		}
		Ok(supervisor)
	}

	/// Start the topics that are due and whose type is known.
	fn start_ready_topics(&mut self) -> anyhow::Result<()> {
		let Some(publisher) = &self.relay.publisher else {
			return Ok(());
		};
		let started = self
			.pending
			.start_ready(&self.node, publisher, &self.video, &self.qos, &mut self.restarts)?;
		for (topic, bridge) in started {
			let stats = bridge.watch_stats();
			self.report_running(&topic.topic, &format!("ROS -> MoQ {}", topic.format), &stats);
			if let Some(control) = bridge.bitrate_control() {
				self.bitrates.insert(topic.topic.clone(), control);
			}
			self.pub_bridges.push(async move { (topic, bridge.await) }.boxed());
		}
		Ok(())
	}

	/// Start republishing a broadcast that was announced.
	///
	/// One whose topic can't be created (e.g. a message type this build has no bindings for)
	/// is skipped, so the other broadcasts and topics can go on; the node fails only once
	/// nothing is left to bridge.
	fn on_subscribed(&mut self, ready: MoqToRos, broadcast: SubBroadcast) -> anyhow::Result<()> {
		match self
			.node
			.with(|node| spawn_sub_broadcast(ready, node, &broadcast, self.qos.clone()))
		{
			Ok(bridge) => {
				self.report_running(&broadcast.broadcast, "MoQ -> ROS", &bridge.watch_stats());
				self.sub_bridges.push(async move { (broadcast, bridge.await) }.boxed());
			}
			Err(err) => {
				tracing::error!(%err, broadcast = broadcast.broadcast, topic = broadcast.topic, "skipping broadcast");
				if self.is_idle() {
					bail!("nothing left to bridge: no broadcast could be republished");
				}
			}
		}
		Ok(())
	}

	/// Republish the announced broadcast `name` if discovery covers it and it's new.
	fn on_announcement(&mut self, name: &str) {
		let Some(discovery) = &self.discovery else {
			return;
		};
		if !discovery.covers(name)
			|| !self
				.known_broadcasts
				.insert(moqros::normalize_broadcast_name(name).to_string())
		{
			return;
		}
		match discovery.broadcast(name) {
			Ok(broadcast) if self.used_topics.insert(broadcast.topic.clone()) => {
				tracing::info!(broadcast = name, topic = broadcast.topic, "found a broadcast");
				self.subscribe(broadcast, Duration::ZERO);
			}
			Ok(broadcast) => tracing::warn!(
				broadcast = name,
				topic = broadcast.topic,
				"skipping a found broadcast: its topic is taken"
			),
			Err(err) => tracing::warn!(broadcast = name, %err, "skipping a found broadcast"),
		}
	}

	/// Apply a change to the node's parameter `name`.
	fn on_parameter(&mut self, name: &str, value: &r2r::ParameterValue) {
		match runtime_bitrate(name, value) {
			Some(Ok(bitrate)) => {
				tracing::info!(bitrate, "changing the video bitrate");
				self.video.encoder.bitrate = bitrate;
				for control in self.bitrates.values() {
					control.set(bitrate);
				}
			}
			Some(Err(err)) => tracing::warn!(%err, "ignoring the new bitrate"),
			None => tracing::warn!(
				parameter = name,
				"only `bitrate` changes at runtime; restart the node to apply this parameter"
			),
		}
	}

	fn on_topic_stopped(&mut self, (topic, end): TopicStopped) {
		let delay = self.note_stopped("topic", &topic.topic, &end);
		tracing::info!(topic = topic.topic, ?delay, "restarting the topic bridge");
		self.pending.restart(topic, delay);
	}

	fn on_broadcast_stopped(&mut self, (broadcast, end): BroadcastStopped) {
		let delay = self.note_stopped("broadcast", &broadcast.broadcast, &end);
		self.node.with(|node| end.teardown.run(node));
		tracing::info!(
			broadcast = broadcast.broadcast,
			?delay,
			"restarting the broadcast bridge"
		);
		self.subscribe(broadcast, delay);
	}

	/// Log and report that the bridge of `kind` `name` stopped, returning how long to wait
	/// before restarting it.
	fn note_stopped(&mut self, kind: &str, name: &str, end: &BridgeEnd) -> Duration {
		log_stopped(kind, name, end);
		if let Some((diagnostics, _)) = &mut self.diagnostics {
			diagnostics.restarting(name);
		}
		self.restarts.next(&format!("{kind} {name}"), end.stats.sent > 0)
	}

	/// Report the bridge `name` (`what` it bridges) as running, with its counters.
	fn report_running(&mut self, name: &str, what: &str, stats: &watch::Receiver<BridgeStats>) {
		if let Some((diagnostics, _)) = &mut self.diagnostics {
			diagnostics.running(name, what, stats.clone());
		}
	}

	/// Wait `delay`, then subscribe to `broadcast`.
	fn subscribe(&mut self, broadcast: SubBroadcast, delay: Duration) {
		if let Some(subscriber) = self.relay.subscriber.clone() {
			self.subscribing
				.push(subscribe_after(subscriber, broadcast, delay).boxed());
		}
	}

	/// Whether nothing runs or waits to run, nor ever will.
	fn is_idle(&self) -> bool {
		self.discovery.is_none()
			&& self.subscribing.is_empty()
			&& self.sub_bridges.is_empty()
			&& self.pub_bridges.is_empty()
			&& self.pending.is_empty()
	}
}

/// Changes to the node's ROS parameters, through its `set_parameters` service.
type ParameterChanges = std::pin::Pin<Box<dyn futures::Stream<Item = (String, r2r::ParameterValue)> + Send>>;

/// Serve the node's parameter services, returning the changes they make.
fn watch_parameters(node: &mut r2r::Node) -> anyhow::Result<ParameterChanges> {
	let (serve, changes) = node
		.make_parameter_handler()
		.context("failed to serve ROS parameters")?;
	tokio::spawn(serve);
	Ok(Box::pin(changes))
}

/// The next broadcast announced. Never resolves without discovery.
async fn next_announcement(announcements: &mut Option<moqros::Announcements>) -> Option<String> {
	match announcements {
		Some(announcements) => announcements.next().await,
		None => std::future::pending().await,
	}
}

/// The next parameter change. Never resolves without parameter services.
async fn next_parameter(parameters: &mut Option<ParameterChanges>) -> Option<(String, r2r::ParameterValue)> {
	match parameters {
		Some(changes) => changes.next().await,
		None => std::future::pending().await,
	}
}

/// The new bitrate if parameter `name` is the bitrate, or why `value` isn't one; `None` for other
/// parameters.
fn runtime_bitrate(name: &str, value: &r2r::ParameterValue) -> Option<Result<u32, String>> {
	(name == "bitrate").then(|| match value {
		r2r::ParameterValue::Integer(bitrate) => u32::try_from(*bitrate)
			.ok()
			.filter(|&bitrate| bitrate > 0)
			.ok_or_else(|| format!("{bitrate} is not a bitrate in bit/s")),
		other => Err(format!("{other:?} is not an integer")),
	})
}

/// The diagnostics, once their next report is due. Never resolves without diagnostics.
async fn next_report(diagnostics: &mut Option<(Diagnostics, tokio::time::Interval)>) -> Option<&mut Diagnostics> {
	match diagnostics {
		Some((diagnostics, interval)) => {
			interval.tick().await;
			Some(diagnostics)
		}
		None => std::future::pending().await,
	}
}

/// Wait `delay`, then subscribe to `broadcast` (retrying until it's announced).
async fn subscribe_after(
	subscriber: moqros::Subscriber,
	broadcast: SubBroadcast,
	delay: Duration,
) -> (MoqToRos, SubBroadcast) {
	tokio::time::sleep(delay).await;
	(
		MoqToRos::subscribe_with(subscriber, &broadcast.broadcast, broadcast.subscribe_options()).await,
		broadcast,
	)
}

/// Log why the bridge of `kind` `name` stopped.
fn log_stopped(kind: &str, name: &str, end: &BridgeEnd) {
	let stats = &end.stats;
	match &end.result {
		Ok(()) => tracing::warn!(kind, name, ?stats, "bridge stopped"),
		Err(err @ moqros::Error::BridgePanicked(_)) => tracing::error!(kind, name, ?stats, %err, "bridge panicked"),
		Err(err) => tracing::warn!(kind, name, ?stats, %err, "bridge failed"),
	}
}

/// How long to wait before restarting each bridge that stopped: one second at first, doubling
/// up to [`Restarts::MAX`] while it keeps stopping without getting a message through.
#[derive(Debug, Default)]
struct Restarts {
	delays: HashMap<String, Duration>,
}

impl Restarts {
	const MIN: Duration = Duration::from_secs(1);
	const MAX: Duration = Duration::from_secs(30);

	/// The delay before restarting `bridge`, which stopped after sending a message if
	/// `progressed`.
	fn next(&mut self, bridge: &str, progressed: bool) -> Duration {
		let delay = self.delays.entry(bridge.to_string()).or_default();
		*delay = match progressed || delay.is_zero() {
			true => Self::MIN,
			false => (*delay * 2).min(Self::MAX),
		};
		*delay
	}
}

/// A topic to publish that hasn't started (again) yet.
struct Pending {
	topic: PubTopic,
	/// Not started before this.
	due: Instant,
	/// Whether it ran before, so a failure to start is retried instead of fatal.
	restart: bool,
}

/// Topics to publish that haven't started yet, because their type isn't known or they wait
/// to be restarted.
struct PendingTopics {
	topics: Vec<Pending>,
	/// Whether the topics still waiting for their type were logged.
	logged: bool,
}

impl PendingTopics {
	fn new(topics: Vec<PubTopic>) -> Self {
		let now = Instant::now();
		let topics = topics
			.into_iter()
			.map(|topic| Pending {
				topic,
				due: now,
				restart: false,
			})
			.collect();
		Self { topics, logged: false }
	}

	fn is_empty(&self) -> bool {
		self.topics.is_empty()
	}

	/// Start `topic` again in `delay`.
	fn restart(&mut self, topic: PubTopic, delay: Duration) {
		self.topics.push(Pending {
			topic,
			due: Instant::now() + delay,
			restart: true,
		});
	}

	/// Start every topic that is due and whose type is known by now, returning each one with
	/// its bridge. Data topics without a `type` wait until something publishes them.
	///
	/// A topic that fails to start fails the node the first time (it's misconfigured), and is
	/// retried later on a restart.
	fn start_ready(
		&mut self,
		node: &SharedNode,
		publisher: &moqros::Publisher,
		video: &moqros::ImageBroadcastSettings,
		qos: &QosProfile,
		restarts: &mut Restarts,
	) -> anyhow::Result<Vec<(PubTopic, Bridge)>> {
		let now = Instant::now();
		let mut started = Vec::new();
		let mut waiting = Vec::new();
		for mut pending in std::mem::take(&mut self.topics) {
			if pending.due > now {
				self.topics.push(pending);
				continue;
			}
			match node.with(|node| start_pub_topic(node, publisher, &pending.topic, video, qos.clone())) {
				Ok(Some(bridge)) => started.push((pending.topic, bridge)),
				Ok(None) => {
					waiting.push(pending.topic.topic.clone());
					self.topics.push(pending);
				}
				Err(err) if pending.restart => {
					let delay = restarts.next(&format!("topic {}", pending.topic.topic), false);
					tracing::warn!(topic = pending.topic.topic, %err, ?delay, "restarting the topic bridge failed");
					pending.due = now + delay;
					self.topics.push(pending);
				}
				Err(err) => return Err(err),
			}
		}
		if !waiting.is_empty() && !self.logged {
			tracing::info!(topics = ?waiting, "waiting for topics to appear to learn their types");
			self.logged = true;
		}
		Ok(started)
	}
}

/// Start bridging `topic` from ROS to MoQ once its type is known: given, or looked up in the ROS
/// graph. Returns `None` while the topic hasn't appeared there yet.
fn start_pub_topic(
	node: &mut r2r::Node,
	publisher: &moqros::Publisher,
	topic: &PubTopic,
	video: &moqros::ImageBroadcastSettings,
	qos: QosProfile,
) -> anyhow::Result<Option<Bridge>> {
	let ros_type = match &topic.ros_type {
		Some(ros_type) => ros_type.clone(),
		None => match moqros::ros::topic_type(node, &topic.topic)? {
			Some(ros_type) => ros_type,
			None => return Ok(None),
		},
	};
	let source = moqros::ros::RosSource::new(topic.topic.clone(), qos);
	let bridge = match topic.format.data() {
		None => {
			check_video_type(&topic.topic, &ros_type)?;
			let images = publisher.create_image_broadcast(&topic.broadcast, video.clone())?;
			match ros_type.as_str() {
				ROS_COMPRESSED_IMAGE_TYPE => moqros::ros::spawn_ros_to_moq_compressed(node, source, images)?,
				_ => moqros::ros::spawn_ros_to_moq(node, source, images)?,
			}
		}
		Some(format) => {
			let settings = moqros::DataSettings::new(format)
				.with_mode(topic.mode)
				.with_ros_type(ros_type);
			let data = publisher.create_data_broadcast(&topic.broadcast, settings)?;
			moqros::ros::spawn_ros_to_moq_data(node, source, data)?
		}
	};
	tracing::info!(
		topic = topic.topic,
		format = ?topic.format,
		broadcast = topic.broadcast,
		"bridging ROS -> MoQ"
	);
	Ok(Some(bridge))
}

/// Create the ROS publisher for a subscribed broadcast and start republishing it.
fn spawn_sub_broadcast(
	ready: moqros::ros::MoqToRos,
	node: &mut r2r::Node,
	broadcast: &SubBroadcast,
	qos: QosProfile,
) -> moqros::Result<Bridge> {
	let bridge = ready.spawn(node, broadcast.target(qos))?;
	tracing::info!(
		broadcast = broadcast.broadcast,
		topic = broadcast.topic,
		"bridging MoQ -> ROS"
	);
	Ok(bridge)
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn reads_runtime_bitrates() {
		use r2r::ParameterValue::{Double, Integer};
		assert_eq!(runtime_bitrate("bitrate", &Integer(500_000)), Some(Ok(500_000)));
		assert!(matches!(runtime_bitrate("bitrate", &Integer(0)), Some(Err(_))));
		assert!(matches!(runtime_bitrate("bitrate", &Integer(-1)), Some(Err(_))));
		assert!(matches!(runtime_bitrate("bitrate", &Integer(1 << 40)), Some(Err(_))));
		assert!(matches!(runtime_bitrate("bitrate", &Double(5e5)), Some(Err(_))));
		assert_eq!(runtime_bitrate("fps", &Integer(10)), None);
	}

	#[test]
	fn restart_delays_back_off_and_reset() {
		let mut restarts = Restarts::default();
		let delays: Vec<u64> = (0..7).map(|_| restarts.next("topic /a", false).as_secs()).collect();
		assert_eq!(delays, [1, 2, 4, 8, 16, 30, 30]);
		// Each bridge has its own delay.
		assert_eq!(restarts.next("topic /b", false), Restarts::MIN);
		// A bridge that got messages through starts over.
		assert_eq!(restarts.next("topic /a", true), Restarts::MIN);
		assert_eq!(restarts.next("topic /a", false), Duration::from_secs(2));
	}
}