Skip to main content

moq_mux/catalog/
data.rs

1use super::hang::CatalogExt;
2use super::{Estimator, Rendition, RenditionConfig};
3
4/// The config a data producer ([`Producer::json_stream`](super::Producer::json_stream) and the
5/// like) publishes as its catalog entry.
6///
7/// Implemented for the [`json::Config`](crate::json::Config) builder, which lists the track in the
8/// `json` section. Also implemented for any [`RenditionConfig`] that embeds the data config `D`
9/// ([`JsonConfig`](hang::catalog::JsonConfig)) through [`AsMut`], which is how an application lists
10/// a data track in its own section beside its own per-track fields; see the [`RenditionConfig`]
11/// example.
12///
13/// The producer sets the embedded config's `mode` and owns the entry: it is written when the
14/// producer is created and removed when it drops.
15pub trait IntoRendition<E: CatalogExt, D> {
16	/// The catalog entry, embedding `D`.
17	type Config: RenditionConfig<E> + AsMut<D>;
18
19	/// Build the catalog entry.
20	fn into_rendition(self) -> Self::Config;
21}
22
23impl<E: CatalogExt, D, C: RenditionConfig<E> + AsMut<D>> IntoRendition<E, D> for C {
24	type Config = C;
25
26	fn into_rendition(self) -> C {
27		self
28	}
29}
30
31/// A data track's catalog entry, owned for the life of its producer and kept current with the
32/// bitrate and delay its writes measure.
33///
34/// Erases the entry's type, so a data producer's own type doesn't depend on which section lists it.
35pub(crate) struct Listing {
36	rendition: Box<dyn Owned>,
37	/// Measures `delay` against the catalog's other renditions, media included.
38	estimator: Estimator,
39	/// The catalog clock as of the last write. Read again on every write, since an importer's first
40	/// frame still places the clock if it lands while the catalog is withheld.
41	clock: crate::Clock,
42}
43
44/// The parts of a [`Rendition`] a [`Listing`] uses, without its config type.
45trait Owned: Send + Sync {
46	fn name(&self) -> &str;
47	fn clock(&self) -> crate::Clock;
48	fn estimate(&mut self, reading: super::Reading) -> crate::Result<()>;
49}
50
51impl<E: CatalogExt, C: RenditionConfig<E>> Owned for Rendition<E, C> {
52	fn name(&self) -> &str {
53		Rendition::name(self)
54	}
55	fn clock(&self) -> crate::Clock {
56		Rendition::clock(self)
57	}
58	fn estimate(&mut self, reading: super::Reading) -> crate::Result<()> {
59		Rendition::estimate(self, reading)
60	}
61}
62
63impl Listing {
64	/// Publish `config` as the entry `rendition` reserved.
65	pub(crate) fn new<E: CatalogExt, C: RenditionConfig<E>>(
66		mut rendition: Rendition<E, C>,
67		config: C,
68	) -> crate::Result<Self> {
69		rendition.set(config)?;
70		Ok(Self {
71			estimator: rendition.estimator(),
72			clock: rendition.clock(),
73			rendition: Box::new(rendition),
74		})
75	}
76
77	/// The track name, which is also the catalog key.
78	pub(crate) fn name(&self) -> &str {
79		self.rendition.name()
80	}
81
82	/// Measure a frame of `bytes` encoded bytes, just written, captured at `captured` on the
83	/// broadcast clock if known.
84	pub(crate) fn record(&mut self, bytes: usize, captured: Option<moq_net::Timestamp>) -> crate::Result<()> {
85		let clock = self.rendition.clock();
86		// A re-anchor moves this track's timeline, which measured across would read as jitter.
87		// Compare the whole clock, since an unset host wall clock has no mapping to compare.
88		if clock != self.clock {
89			self.estimator.discontinuity();
90		}
91		let now = clock.now();
92		self.clock = clock;
93		self.record_at(now, bytes, captured)
94	}
95
96	/// [`record`](Self::record) at a chosen time, which a test needs since the broadcast clock only
97	/// moves in real time.
98	pub(crate) fn record_at(
99		&mut self,
100		now: moq_net::Timestamp,
101		bytes: usize,
102		captured: Option<moq_net::Timestamp>,
103	) -> crate::Result<()> {
104		// Each write is its own span, closed by the next one.
105		self.estimator.cut(Some(now));
106		self.estimator.rate(now, bytes);
107
108		// The write time is the application's cadence, not media time, so only a capture time takes
109		// a place in the broadcast's write order. Without one no delay is advertised.
110		if let Some(captured) = captured {
111			// A source clock running fast stamps ahead of now. Ordered as given, it would put every
112			// media track behind it by however fast it runs. It was written no later than now.
113			// An unscaled capture time doesn't compare, and the estimator skips it.
114			self.estimator.order(if captured > now { now } else { captured });
115		}
116		self.rendition.estimate(self.estimator.reading())
117	}
118}