Skip to main content

moq_mux/container/loc/
mod.rs

1//! Low Overhead Container.
2//!
3//! The IETF draft replacement for hang's Legacy format. Each moq frame
4//! holds a small property block (timestamp, optional per-frame
5//! timescale) followed by the codec bitstream. Defaults to microsecond
6//! timestamps. See [draft-ietf-moq-loc-04](https://www.ietf.org/archive/id/draft-ietf-moq-loc-04.html).
7
8use moq_net::{Timescale, Timestamp};
9
10use crate::container::{Container, Frame, Kind};
11
12/// LOC's catalog convention: timestamps are in microseconds when no per-frame
13/// 0x08 timescale property is present.
14const DEFAULT_TIMESCALE: Timescale = Timescale::MICRO;
15
16/// LOC wire format configured for the track's media role.
17pub struct Wire(
18	/// The kind of content carried by the track.
19	pub Kind,
20);
21
22impl Wire {
23	/// The bytes preceding a frame's payload: its property block.
24	pub(crate) fn prefix(&self, timestamp: Timestamp) -> crate::Result<bytes::Bytes> {
25		let timestamp = timestamp.convert(DEFAULT_TIMESCALE).map_err(hang::Error::from)?;
26		Ok(moq_loc::encode(&moq_loc::Frame {
27			timestamp: timestamp.value(),
28			timescale: None,
29			payload: bytes::Bytes::new(),
30		})?)
31	}
32}
33
34impl Container for Wire {
35	type Error = crate::Error;
36
37	fn write(&self, group: &mut crate::container::Writer<'_>, frames: &[Frame]) -> Result<(), Self::Error> {
38		for frame in frames {
39			// LOC's wire format omits per-frame timescale by convention; the catalog
40			// default is microseconds, so convert at the boundary.
41			let timestamp = frame.timestamp.convert(DEFAULT_TIMESCALE).map_err(hang::Error::from)?;
42			let data = moq_loc::encode(&moq_loc::Frame {
43				timestamp: timestamp.value(),
44				timescale: None,
45				payload: frame.payload.clone(),
46			})?;
47
48			// The property carries the presentation time, and the transport the newest so far
49			// (converted to the track's timescale), which never decreases within a group.
50			let transport = match group.latest() {
51				Some(latest) if frame.timestamp < latest => latest,
52				_ => frame.timestamp,
53			};
54			group.write_frame(transport, data)?;
55		}
56		Ok(())
57	}
58
59	fn decode(&self, frame: moq_net::frame::Frame) -> Result<Vec<Frame>, Self::Error> {
60		// On a data track an object with no bytes is its discontinuity marker, not a frame.
61		// Elsewhere it is malformed, so it fails to decode below.
62		if self.0 == Kind::Data && frame.payload.is_empty() {
63			return Ok(Vec::new());
64		}
65
66		let loc = moq_loc::decode(frame.payload)?;
67		let scale = loc.timescale.unwrap_or(DEFAULT_TIMESCALE);
68		let timestamp = Timestamp::new(loc.timestamp, scale).map_err(hang::Error::from)?;
69
70		Ok(vec![Frame {
71			timestamp,
72			decode: None,
73			payload: loc.payload,
74			// LOC doesn't carry the keyframe bit on the wire; the
75			// wrapping Consumer fills it in from group position.
76			keyframe: false,
77			// LOC carries no per-frame duration.
78			duration: None,
79		}])
80	}
81
82	fn kind(&self) -> Kind {
83		self.0
84	}
85
86	fn end(&self, frame: &Frame) -> Option<moq_net::Timestamp> {
87		(self.0 != Kind::Data && frame.payload.is_empty()).then_some(frame.timestamp)
88	}
89
90	fn finish_group(
91		&self,
92		group: &mut crate::container::Writer<'_>,
93		end: Option<moq_net::Timestamp>,
94	) -> Result<(), Self::Error> {
95		if self.0 == Kind::Video
96			&& let Some(timestamp) = end
97		{
98			self.write(
99				group,
100				&[Frame {
101					timestamp,
102					decode: None,
103					payload: bytes::Bytes::new(),
104					keyframe: false,
105					duration: None,
106				}],
107			)?;
108		}
109		Ok(())
110	}
111}