moqros 0.8.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
453
454
455
456
//! MoQ plumbing shared by the image and data broadcasts: announcing a broadcast with its catalog,
//! writing groups within moq-net's limits, and reading a track group after group.

use std::time::Duration;

use bytes::Bytes;
use hang::catalog::Catalog;
use hang::moq_net;
use serde::Serialize;

use crate::catalog::{MoqrosCatalog, MoqrosSection};
use crate::error::Wrap;
use crate::{Error, Result};

/// One announced broadcast and its catalog track. Closes the broadcast when dropped.
pub(crate) struct BroadcastWriter {
	name: String,
	broadcast: moq_net::broadcast::Producer,
	catalog: moq_net::track::Producer,
}

impl BroadcastWriter {
	/// Create and announce the broadcast `name`, with an empty catalog track.
	pub(crate) fn create(origin: &moq_net::origin::Producer, name: &str) -> Result<Self> {
		let broadcast = origin.create_broadcast(name).moq()?;
		broadcast.announce(Default::default()).moq()?;
		let catalog = broadcast
			.create_track(Catalog::DEFAULT_NAME, Catalog::default_track_info())
			.moq()?;
		Ok(Self {
			name: name.to_string(),
			broadcast,
			catalog,
		})
	}

	/// Broadcast name, relative to the relay URL.
	pub(crate) fn name(&self) -> &str {
		&self.name
	}

	pub(crate) fn create_track(&self, name: &str, info: moq_net::track::Info) -> Result<moq_net::track::Producer> {
		self.broadcast.create_track(name, info).moq()
	}

	/// Replace the catalog subscribers see.
	pub(crate) fn publish_catalog<E: Serialize>(&mut self, catalog: &Catalog<E>) -> Result<()> {
		let mut group = self.catalog.append_group().moq()?;
		group
			.write_frame(moq_net::Timestamp::now(), catalog.to_json().hang()?)
			.moq()?;
		group.finish().moq()?;
		Ok(())
	}
}

impl Drop for BroadcastWriter {
	fn drop(&mut self) {
		self.broadcast.close();
	}
}

/// Writes frames into the groups of one track, keeping each group within moq-net's limits.
/// Finishes the open group when dropped.
pub(crate) struct GroupWriter {
	track: moq_net::track::Producer,
	group: Option<moq_net::group::Producer>,
	/// What [`Self::group`] has used of moq-net's per-group limits.
	budget: GroupBudget,
}

impl GroupWriter {
	pub(crate) fn new(track: moq_net::track::Producer) -> Self {
		Self {
			track,
			group: None,
			budget: GroupBudget::default(),
		}
	}

	/// Finish the open group, if any, and start a new one.
	pub(crate) fn start_group(&mut self) -> Result<()> {
		self.finish_group()?;
		self.group = Some(self.track.append_group().moq()?);
		self.budget = GroupBudget::default();
		Ok(())
	}

	/// Finish the open group, if any.
	pub(crate) fn finish_group(&mut self) -> Result<()> {
		if let Some(group) = self.group.take() {
			group.finish().moq()?;
		}
		Ok(())
	}

	pub(crate) fn is_open(&self) -> bool {
		self.group.is_some()
	}

	/// Whether a frame with `payload` bytes still fits into the open group.
	pub(crate) fn fits(&self, payload: usize) -> bool {
		self.budget.fits(payload)
	}

	/// Whether the open group has used three quarters of either limit.
	pub(crate) fn nearly_full(&self) -> bool {
		self.budget.nearly_full()
	}

	/// Write one frame into the open group, starting one if there is none.
	pub(crate) fn write_frame(&mut self, timestamp: moq_net::Timestamp, payload: Bytes) -> Result<()> {
		let size = payload.len();
		self.group()?.write_frame(timestamp, payload).moq()?;
		self.budget.add(size);
		Ok(())
	}

	/// Write one frame in the hang container into the open group, starting one if there is none.
	pub(crate) fn write_container(&mut self, frame: &hang::container::Frame) -> Result<()> {
		frame.write_to(self.group()?).hang()?;
		self.budget.add(frame.payload.len());
		Ok(())
	}

	fn group(&mut self) -> Result<&mut moq_net::group::Producer> {
		if self.group.is_none() {
			self.start_group()?;
		}
		Ok(self.group.as_mut().expect("group started above"))
	}

	/// Frames written to the open group.
	#[cfg(test)]
	pub(crate) fn frames(&self) -> usize {
		self.budget.frames
	}
}

impl Drop for GroupWriter {
	fn drop(&mut self) {
		let _ = self.finish_group();
	}
}

/// How much of moq-net's per-group limits a group has used, so writers can start a new
/// group before a write would abort the current one.
#[derive(Debug, Default)]
struct GroupBudget {
	frames: usize,
	bytes: u64,
}

impl GroupBudget {
	/// Bytes a frame may add on top of its payload (hang's timestamp varint).
	const OVERHEAD: u64 = 8;

	fn fits(&self, payload: usize) -> bool {
		self.frames < moq_net::group::MAX_GROUP_FRAMES
			&& self.bytes + payload as u64 + Self::OVERHEAD <= moq_net::group::MAX_CACHE_BYTES
	}

	fn add(&mut self, payload: usize) {
		self.frames += 1;
		self.bytes += payload as u64 + Self::OVERHEAD;
	}

	fn nearly_full(&self) -> bool {
		self.frames >= moq_net::group::MAX_GROUP_FRAMES / 4 * 3 || self.bytes >= moq_net::group::MAX_CACHE_BYTES / 4 * 3
	}
}

/// The catalog of a subscribed broadcast, following its updates.
pub(crate) struct CatalogWatch {
	/// `None` once the catalog track ended or failed: the last catalog stays.
	updates: Option<Box<hang::catalog::Consumer<serde_json::Map<String, serde_json::Value>>>>,
	catalog: Box<MoqrosCatalog>,
	section: MoqrosSection,
}

impl CatalogWatch {
	/// Follow `updates`, whose first catalog was `first`.
	pub(crate) fn new(
		updates: hang::catalog::Consumer<serde_json::Map<String, serde_json::Value>>,
		first: MoqrosCatalog,
	) -> Self {
		Self {
			updates: Some(Box::new(updates)),
			section: MoqrosSection::read(&first),
			catalog: Box::new(first),
		}
	}

	/// The current catalog.
	pub(crate) fn catalog(&self) -> &MoqrosCatalog {
		&self.catalog
	}

	/// The `moqros` section of the current catalog.
	pub(crate) fn section(&self) -> &MoqrosSection {
		&self.section
	}

	/// Whether the current catalog maps media timestamps to capture times.
	pub(crate) fn has_capture_times(&self) -> bool {
		self.section.capture_origin.is_some() || self.catalog.clock.is_some()
	}

	/// The capture time of a frame with media timestamp `pts`, by the current catalog.
	pub(crate) fn capture_time(&self, pts: Duration) -> Option<Duration> {
		self.section.capture_time(&self.catalog, pts)
	}

	/// Resolves once the next catalog has replaced the current one. Never resolves after the
	/// catalog track ended; the media tracks end with it anyway.
	pub(crate) async fn changed(&mut self) {
		let Some(updates) = self.updates.as_mut() else {
			return std::future::pending().await;
		};
		match updates.next().await {
			Ok(Some(catalog)) => {
				self.section = MoqrosSection::read(&catalog);
				*self.catalog = catalog;
			}
			Ok(None) => self.updates = None,
			Err(err) => {
				tracing::debug!(%err, "catalog update failed, keeping the last catalog");
				self.updates = None;
			}
		}
	}
}

/// Watches the announcement of one subscribed broadcast.
///
/// A relay doesn't end a subscription when the broadcast's publisher retracts it: the track
/// just goes quiet. Nor does it move the subscription when another publisher of the same name
/// takes over. So a subscription ends on any change to the broadcast's route, and the caller
/// resubscribes to whatever serves the name next.
///
/// That includes [`Updated`](moq_net::announce::Kind::Updated), though moq-net delivers a
/// re-priced route in place: a takeover by another publisher also arrives as `Updated` (the path
/// stays covered throughout), and it can't be told apart from a mere re-pricing. Route changes
/// are rare, so an occasional needless resubscribe is the cheaper mistake.
pub(crate) struct RouteWatch {
	announced: moq_net::announce::Consumer,
	name: String,
}

impl RouteWatch {
	/// Watch `name` on `announced`, which has already delivered its current announcement.
	pub(crate) fn new(announced: moq_net::announce::Consumer, name: &str) -> Self {
		Self {
			announced,
			name: name.to_string(),
		}
	}

	/// Resolves on the next update to this broadcast's announcement: retracted, re-announced,
	/// or re-routed. Never resolves once the origin closes; the track ends then anyway.
	async fn changed(&mut self) -> moq_net::announce::Kind {
		while let Some(update) = self.announced.next().await {
			if update.prefix.as_str() == self.name {
				return update.kind;
			}
		}
		std::future::pending().await
	}
}

/// Reads the frames of one track, group after group, until the track ends or its broadcast's
/// route changes.
pub(crate) struct GroupReader {
	frames: Frames,
	route: RouteWatch,
	ended: bool,
}

impl GroupReader {
	/// Subscribe to `track`. With `skip`, the reader jumps to a newer group as soon as one
	/// arrives, dropping the rest of the current one.
	async fn subscribe(
		broadcast: &moq_net::broadcast::Consumer,
		track: &str,
		skip: bool,
		route: RouteWatch,
	) -> Result<Self> {
		let track = broadcast
			.track(track)
			.moq()?
			.subscribe(moq_net::track::Subscription::default())
			.await
			.moq()?
			.ordered();
		Ok(Self {
			frames: Frames {
				track,
				group: None,
				skip,
			},
			route,
			ended: false,
		})
	}

	/// Next frame, or `None` once the track ends or the broadcast's route changes.
	async fn next_frame(&mut self) -> Result<Option<moq_net::frame::Frame>> {
		if self.ended {
			return Ok(None);
		}
		tokio::select! {
			// Frames first: whatever arrived before the route changed (e.g. a last stop command
			// sent just before the retraction) is still delivered.
			biased;
			frame = self.frames.next() => {
				let frame = frame?;
				self.ended = frame.is_none();
				Ok(frame)
			}
			kind = self.route.changed() => {
				tracing::info!(broadcast = self.route.name, ?kind, "broadcast announcement changed, ending the subscription");
				self.ended = true;
				Ok(None)
			}
		}
	}
}

/// A subscribed broadcast whose first catalog has arrived, before one of its tracks is read.
pub(crate) struct OpenBroadcast {
	broadcast: moq_net::broadcast::Consumer,
	catalog: CatalogWatch,
	route: RouteWatch,
}

impl OpenBroadcast {
	/// Wait for `name` to be announced on `origin` and read its first catalog. Also watches the
	/// announcement, which the subscription ends with.
	pub(crate) async fn open(origin: &moq_net::origin::Producer, name: &str) -> Result<Self> {
		let consumer = 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.moq()?;
		let mut updates = MoqrosCatalog::subscribe(&broadcast).await.hang()?;
		let first = updates
			.next()
			.await
			.hang()?
			.ok_or_else(|| Error::NoTracks(name.to_string()))?;
		Ok(Self {
			broadcast,
			catalog: CatalogWatch::new(updates, first),
			route: RouteWatch::new(announced, name),
		})
	}

	/// The first catalog.
	pub(crate) fn catalog(&self) -> &MoqrosCatalog {
		self.catalog.catalog()
	}

	/// Subscribe to `track`; `skip` as for [`GroupReader::subscribe`].
	pub(crate) async fn subscribe(self, track: &str, skip: bool) -> Result<TrackReader> {
		Ok(TrackReader {
			frames: GroupReader::subscribe(&self.broadcast, track, skip, self.route).await?,
			catalog: self.catalog,
			_broadcast: self.broadcast,
		})
	}
}

/// Reads one track of a broadcast while following the broadcast's catalog.
pub(crate) struct TrackReader {
	_broadcast: moq_net::broadcast::Consumer,
	frames: GroupReader,
	catalog: CatalogWatch,
}

impl TrackReader {
	/// The current catalog of the broadcast.
	pub(crate) fn catalog(&self) -> &CatalogWatch {
		&self.catalog
	}

	/// Next frame, or `None` once the track ends or the broadcast's route changes.
	///
	/// A catalog update that arrived first is applied first, so a new capture origin comes
	/// before the frames stamped against it.
	pub(crate) async fn next_frame(&mut self) -> Result<Option<moq_net::frame::Frame>> {
		loop {
			tokio::select! {
				biased;
				() = self.catalog.changed() => {}
				frame = self.frames.next_frame() => return frame,
			}
		}
	}
}

/// The frames of one track, group after group.
struct Frames {
	track: moq_net::track::Ordered,
	group: Option<moq_net::group::Consumer>,
	/// Jump to a newer group as soon as one arrives, dropping the rest of the current one.
	skip: bool,
}

impl Frames {
	/// Next frame, or `None` once the track ends.
	async fn next(&mut self) -> Result<Option<moq_net::frame::Frame>> {
		loop {
			let Some(group) = self.group.as_mut() else {
				match self.track.next_group().await.moq()? {
					Some(group) => self.group = Some(group),
					None => return Ok(None),
				}
				continue;
			};

			let frame = if self.skip {
				tokio::select! {
					biased;
					// A newer group starts with a keyframe (or a complete message), so the rest of
					// this one can go.
					next = self.track.next_group() => {
						match next.moq()? {
							Some(group) => self.group = Some(group),
							None => return Ok(None),
						}
						continue;
					}
					frame = group.read_frame() => frame,
				}
			} else {
				group.read_frame().await
			};
			match frame {
				Ok(Some(frame)) => return Ok(Some(frame)),
				Ok(None) => self.group = None,
				Err(err) => {
					tracing::debug!(%err, "group ended early, waiting for the next one");
					self.group = None;
				}
			}
		}
	}
}