moqros 0.7.0

Stream ROS 2 topics (images as video, any message as data) over Media over QUIC
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
//! Subscribe to moqros broadcasts: hang VP8/H.264/AV1 video decoded back into images, or data tracks.

use std::sync::{Arc, Mutex};
use std::time::Duration;

use bytes::Bytes;
use hang::moq_net;

use crate::catalog::{self, Kind, MoqrosCatalog};
use crate::codec::{Codec, Decoder};
use crate::data::{DataMode, DataSubscriber};
use crate::track::{CatalogWatch, GroupReader, RouteWatch};
use crate::{ClientConfig, Error, Image, PixelFormat, Result, Session};

/// A connection to a relay used to consume image and data broadcasts.
///
/// Clones share the connection, which stays up while any clone is alive.
#[derive(Clone)]
pub struct Subscriber {
	origin: moq_net::origin::Producer,
	session: Session,
}

impl Subscriber {
	/// Start connecting to the relay. Must be called inside a tokio runtime.
	pub fn connect(config: &ClientConfig) -> Result<Self> {
		let origin = moq_tokio::origin::spawn();
		let connection = config
			.client()?
			.with_subscriber(origin.clone())
			.connect(config.url.clone());
		Ok(Self {
			origin,
			session: Session::new(connection),
		})
	}

	pub(crate) fn from_parts(origin: moq_net::origin::Producer, session: Session) -> Self {
		Self { origin, session }
	}

	/// The relay connection, shared with every clone.
	pub fn session(&self) -> &Session {
		&self.session
	}

	/// See [`Session::established`].
	pub async fn established(&self) -> Result<()> {
		self.session.established().await
	}

	/// See [`Session::closed`].
	pub async fn closed(&self) -> Result<()> {
		self.session.closed().await
	}

	/// See [`Session::close`].
	pub async fn close(&self) -> Result<()> {
		self.session.close().await
	}

	/// Wait for `name` to be announced, then subscribe to its best video rendition
	/// whose codec this build can decode.
	pub async fn subscribe_images(&self, name: &str) -> Result<ImageSubscriber> {
		self.subscribe_images_with(name, &ImageOptions::default()).await
	}

	/// Like [`Subscriber::subscribe_images`], picking the rendition by `options`.
	pub async fn subscribe_images_with(&self, name: &str, options: &ImageOptions) -> Result<ImageSubscriber> {
		let (broadcast, catalog, route) = self.catalog(name).await?;
		images(name, broadcast, catalog, route, options).await
	}

	/// Wait for `name` to be announced, then subscribe to its data track.
	pub async fn subscribe_data(&self, name: &str) -> Result<DataSubscriber> {
		let (broadcast, catalog, route) = self.catalog(name).await?;
		data(name, broadcast, catalog, route).await
	}

	/// Wait for `name` to be announced, then subscribe to whatever it carries: its video
	/// if this build can decode it, otherwise its data track.
	pub async fn subscribe(&self, name: &str) -> Result<Subscription> {
		self.subscribe_with(name, &ImageOptions::default()).await
	}

	/// Like [`Subscriber::subscribe`], picking a video rendition by `options`.
	pub async fn subscribe_with(&self, name: &str, options: &ImageOptions) -> Result<Subscription> {
		let (broadcast, catalog, route) = self.catalog(name).await?;
		match Kind::of(catalog.catalog()) {
			Kind::Images => Ok(Subscription::Images(
				images(name, broadcast, catalog, route, options).await?,
			)),
			Kind::Data => Ok(Subscription::Data(data(name, broadcast, catalog, route).await?)),
			Kind::None => Err(Error::NoTracks(name.to_string())),
		}
	}

	/// Follow the broadcasts announced under `prefix` (whole path segments: `robots` covers
	/// `robots/a/camera` but not `robots2`; an empty prefix covers every broadcast), including the
	/// ones announced already.
	pub fn announced(&self, prefix: &str) -> Announcements {
		Announcements {
			announced: self.origin.consume().announced(),
			prefix: prefix.trim_matches('/').to_string(),
		}
	}

	/// Wait for `name` to be announced and read its first catalog. Also returns a watch on
	/// the announcement, which the subscription ends with.
	async fn catalog(&self, name: &str) -> Result<(moq_net::broadcast::Consumer, CatalogWatch, RouteWatch)> {
		let consumer = self.origin.consume();
		let mut announced = consumer.announced();
		loop {
			let update = announced
				.next()
				.await
				.ok_or_else(|| Error::NotAnnounced(name.to_string()))?;
			if update.kind.is_active() && update.prefix.as_str() == name {
				break;
			}
		}

		let broadcast = consumer.request_broadcast(name).await?;
		let mut updates = MoqrosCatalog::subscribe(&broadcast).await?;
		let first = updates.next().await?.ok_or_else(|| Error::NoTracks(name.to_string()))?;
		let route = RouteWatch::new(announced, name);
		Ok((broadcast, CatalogWatch::new(updates, first), route))
	}
}

async fn images(
	name: &str,
	broadcast: moq_net::broadcast::Consumer,
	catalog: CatalogWatch,
	route: RouteWatch,
	options: &ImageOptions,
) -> Result<ImageSubscriber> {
	let (track, config, codec) = catalog::select_video(catalog.catalog(), name, options.max_height)?;
	tracing::info!(
		broadcast = name,
		// The track name comes from the remote catalog: record it as a str (quoted
		// and escaped), not with `%`, so control characters can't forge log lines.
		track,
		codec = %config.codec,
		width = ?config.coded_width,
		height = ?config.coded_height,
		"subscribing"
	);

	let video = GroupReader::subscribe(&broadcast, track, true, route).await?;
	Ok(ImageSubscriber {
		_broadcast: broadcast,
		video,
		catalog,
		codec,
		decoder: Arc::new(Mutex::new(Decoder::new(codec)?)),
	})
}

async fn data(
	name: &str,
	broadcast: moq_net::broadcast::Consumer,
	catalog: CatalogWatch,
	route: RouteWatch,
) -> Result<DataSubscriber> {
	let (track, settings) = catalog::find_data(catalog.catalog()).ok_or_else(|| Error::NoData(name.to_string()))?;
	tracing::info!(
		broadcast = name,
		track,
		format = %settings.format,
		mode = %settings.mode,
		ros_type = settings.ros_type.as_deref().unwrap_or_default(),
		"subscribing"
	);
	let reader = GroupReader::subscribe(&broadcast, &track, settings.mode == DataMode::Snapshot, route).await?;
	Ok(DataSubscriber::new(broadcast, reader, settings, catalog))
}

/// Broadcasts announced under a prefix, from [`Subscriber::announced`].
pub struct Announcements {
	announced: moq_net::announce::Consumer,
	prefix: String,
}

impl Announcements {
	/// The name of the next broadcast announced (or announced again) under the prefix, or `None`
	/// once the connection is gone for good. Names come from whoever announces them.
	pub async fn next(&mut self) -> Option<String> {
		while let Some(update) = self.announced.next().await {
			let name = update.prefix.as_str();
			if update.kind.is_active() && is_under(name, &self.prefix) {
				return Some(name.to_string());
			}
		}
		None
	}
}

/// Whether the broadcast `name` is under `prefix`, by whole path segments.
fn is_under(name: &str, prefix: &str) -> bool {
	prefix.is_empty() || name == prefix || name.strip_prefix(prefix).is_some_and(|rest| rest.starts_with('/'))
}

/// Which video rendition [`Subscriber::subscribe_images_with`] picks, among those whose codec
/// this build can decode. The rendition is picked once, from the catalog at subscription time.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct ImageOptions {
	/// The tallest picture wanted: the largest rendition at most this tall, or the smallest if
	/// none is. `None` picks the largest.
	pub max_height: Option<u32>,
}

impl ImageOptions {
	/// Set [`ImageOptions::max_height`].
	pub fn with_max_height(mut self, max_height: u32) -> Self {
		self.max_height = Some(max_height);
		self
	}
}

/// What [`Subscriber::subscribe`] found in a broadcast.
#[non_exhaustive]
pub enum Subscription {
	/// The broadcast carries decodable video.
	Images(ImageSubscriber),
	/// The broadcast carries a data track.
	Data(DataSubscriber),
}

/// One encoded video frame received from a broadcast.
#[derive(Debug, Clone)]
pub struct VideoFrame {
	/// Presentation time relative to the start of the broadcast.
	pub timestamp: Duration,
	/// When the frame was captured, in the publisher's clock (e.g. the ROS `header.stamp` of
	/// the image), if the broadcast's catalog says.
	pub capture_time: Option<Duration>,
	/// Whether the frame decodes on its own.
	pub keyframe: bool,
	/// Codec payload (VP8 frame, Annex-B H.264 access unit, or AV1 temporal unit).
	pub data: Bytes,
}

/// Reads frames of one video track, always jumping to the newest group so a slow
/// reader falls back to the live edge instead of accumulating latency.
pub struct ImageSubscriber {
	_broadcast: moq_net::broadcast::Consumer,
	video: GroupReader,
	catalog: CatalogWatch,
	codec: Codec,
	/// Shared with the blocking task decoding the current frame, which keeps running (and then
	/// lets go) when [`ImageSubscriber::next_image`] is cancelled, so the decoder stays in step
	/// with the frames it was fed.
	decoder: Arc<Mutex<Decoder>>,
}

impl ImageSubscriber {
	/// Codec of the subscribed track.
	pub fn codec(&self) -> Codec {
		self.codec
	}

	/// The frame id the publisher announces (e.g. the ROS `header.frame_id` of its images), if any.
	pub fn frame_id(&self) -> Option<&str> {
		self.catalog.section().frame_id.as_deref()
	}

	/// Whether the broadcast announces capture times, so that [`VideoFrame::capture_time`] is set
	/// and [`ImageSubscriber::next_image`] stamps images with them.
	pub fn has_capture_times(&self) -> bool {
		self.catalog.has_capture_times()
	}

	/// Accept decoded pictures of up to `max_pixels`; see [`Decoder::set_max_pixels`].
	pub fn set_max_pixels(&mut self, max_pixels: u64) {
		lock(&self.decoder).set_max_pixels(max_pixels);
	}

	/// Decode into `format` images; see [`Decoder::set_output_format`].
	pub fn set_output_format(&mut self, format: PixelFormat) -> Result<()> {
		lock(&self.decoder).set_output_format(format)
	}

	/// Next encoded frame, or `None` once the broadcast ends.
	pub async fn next_frame(&mut self) -> Result<Option<VideoFrame>> {
		let frame = loop {
			tokio::select! {
				// A new capture origin comes before the frames stamped against it.
				biased;
				() = self.catalog.changed() => {}
				frame = self.video.next_frame() => break frame?,
			}
		};
		let Some(frame) = frame else {
			return Ok(None);
		};
		let frame = hang::container::Frame::decode(frame.payload)?;
		let timestamp = Duration::from_micros(u64::try_from(frame.timestamp.as_micros()).unwrap_or(u64::MAX));
		Ok(Some(VideoFrame {
			timestamp,
			capture_time: self.catalog.capture_time(timestamp),
			keyframe: self.codec.is_keyframe(&frame.payload),
			data: frame.payload,
		}))
	}

	/// Next decoded image (`rgb8` unless [set otherwise](ImageSubscriber::set_output_format)), or
	/// `None` once the broadcast ends.
	///
	/// Its timestamp is the [capture time](VideoFrame::capture_time) when the broadcast announces
	/// capture times, and otherwise the presentation time relative to the start of the broadcast.
	///
	/// Decoding and color conversion run on tokio's blocking pool, so a large picture doesn't
	/// stall the other tasks of the runtime.
	///
	/// Fails with [`Error::TooLarge`] for a picture above the decoder's
	/// [`max_pixels`](Decoder::max_pixels).
	pub async fn next_image(&mut self) -> Result<Option<Image>> {
		while let Some(frame) = self.next_frame().await? {
			let decoder = self.decoder.clone();
			let timestamp = frame.capture_time.unwrap_or(frame.timestamp);
			let decoded = tokio::task::spawn_blocking(move || lock(&decoder).decode(&frame.data, timestamp))
				.await
				.map_err(|err| Error::Codec(format!("decoder task failed: {err}")))??;
			if let Some(image) = decoded {
				return Ok(Some(image));
			}
		}
		Ok(None)
	}
}

/// Lock `decoder`, recovering it from a panicked decode: the codec state may be off, but the
/// next keyframe resets it.
fn lock(decoder: &Mutex<Decoder>) -> std::sync::MutexGuard<'_, Decoder> {
	decoder.lock().unwrap_or_else(|err| err.into_inner())
}

#[cfg(test)]
mod tests {
	use std::sync::atomic::{AtomicBool, Ordering};

	use super::*;
	use crate::codec::tests::test_pattern;
	use crate::{EncoderSettings, ImagePublisher};

	/// Subscribe to `name` on `origin` the way [`Subscriber::subscribe_images`] does.
	async fn subscribe_images(origin: &moq_net::origin::Producer, name: &str) -> ImageSubscriber {
		let consumer = origin.consume();
		let announced = consumer.announced();
		let broadcast = consumer.request_broadcast(name).await.unwrap();
		let mut updates = MoqrosCatalog::subscribe(&broadcast).await.unwrap();
		let first = updates.next().await.unwrap().unwrap();
		images(
			name,
			broadcast,
			CatalogWatch::new(updates, first),
			RouteWatch::new(announced, name),
			&ImageOptions::default(),
		)
		.await
		.unwrap()
	}

	#[test]
	fn matches_prefixes_by_segment() {
		assert!(is_under("robots/a/camera", "robots"));
		assert!(is_under("robots/a/camera", "robots/a"));
		assert!(is_under("robots", "robots"));
		assert!(is_under("anything", ""));
		assert!(!is_under("robots2/camera", "robots"));
		assert!(!is_under("robot", "robots"));
	}

	/// Broadcasts already announced, and later ones, come back when under the prefix.
	#[tokio::test]
	async fn follows_announcements_under_a_prefix() {
		let origin = moq_tokio::origin::spawn();
		let first = origin.create_broadcast("robots/a/camera").unwrap();
		first.announce(Default::default()).unwrap();
		// What `Subscriber::announced("robots/")` returns.
		let mut announced = Announcements {
			announced: origin.consume().announced(),
			prefix: "robots".into(),
		};
		let other = origin.create_broadcast("robots2/camera").unwrap();
		other.announce(Default::default()).unwrap();
		let second = origin.create_broadcast("robots/b/camera").unwrap();
		second.announce(Default::default()).unwrap();
		assert_eq!(announced.next().await.as_deref(), Some("robots/a/camera"));
		assert_eq!(announced.next().await.as_deref(), Some("robots/b/camera"));
	}

	/// Other tasks of a single-threaded runtime run while a picture decodes.
	#[tokio::test(flavor = "current_thread")]
	async fn decoding_yields_to_the_runtime() {
		if Codec::available().is_empty() {
			return;
		}
		let origin = moq_tokio::origin::spawn();
		let mut publisher = ImagePublisher::new(&origin, "camera", EncoderSettings::default()).unwrap();
		// The catalog only exists once the first keyframe is out.
		assert!(publisher.publish(&test_pattern(64, 48, 0)).unwrap());
		let mut images = subscribe_images(&origin, "camera").await;
		assert!(publisher.publish(&test_pattern(64, 48, 1)).unwrap());

		let ran = Arc::new(AtomicBool::new(false));
		tokio::spawn({
			let ran = ran.clone();
			async move { ran.store(true, Ordering::Relaxed) }
		});
		let image = images.next_image().await.unwrap().expect("a decoded image");
		assert_eq!((image.width, image.height), (64, 48));
		assert!(ran.load(Ordering::Relaxed), "the other task never ran while decoding");
	}

	/// Images come back stamped with their capture time and the publisher's frame id, which
	/// follow the catalog when they change.
	#[tokio::test]
	async fn images_carry_capture_time_and_frame_id() {
		if Codec::available().is_empty() {
			return;
		}
		let origin = moq_tokio::origin::spawn();
		let mut publisher = ImagePublisher::new(&origin, "camera", EncoderSettings::default()).unwrap();
		publisher.set_frame_id("camera_optical").unwrap();
		let at = |t: u32, millis: u64| {
			let mut image = test_pattern(64, 48, t);
			image.timestamp = Duration::from_millis(millis);
			image
		};
		assert!(publisher.publish(&at(0, 100_000)).unwrap());
		let mut images = subscribe_images(&origin, "camera").await;
		assert_eq!(images.frame_id(), Some("camera_optical"));
		assert!(images.has_capture_times());

		publisher.publish(&at(1, 100_033)).unwrap();
		// The subscription starts at the keyframe opening the current group.
		for millis in [100_000, 100_033] {
			let image = images.next_image().await.unwrap().expect("a decoded image");
			assert_eq!(image.timestamp, Duration::from_millis(millis));
		}

		// A restarted capture clock and a new frame id reach the subscriber with the next frame.
		publisher.set_frame_id("left").unwrap();
		publisher.publish(&at(2, 5_000)).unwrap();
		let image = images.next_image().await.unwrap().expect("a decoded image");
		assert_eq!(image.timestamp, Duration::from_millis(5_000));
		assert_eq!(images.frame_id(), Some("left"));
	}
}