Skip to main content

moq_mux/import/
init.rs

1use bytes::Bytes;
2
3use super::{AudioFormat, ContainerFormat, VideoFormat};
4use crate::catalog::VideoHint;
5
6/// What an audio importer needs to start: a format, its init bytes, and an optional label.
7///
8/// `format` selects the codec parser (`"opus"`, `"aac"`, `"flac"`, `"mp3"`). `data` carries the
9/// codec init bytes (an OpusHead, an AudioSpecificConfig, ...), which audio needs up front because
10/// an audio importer cannot resolve its config from frames.
11#[derive(Clone, Debug, PartialEq)]
12#[non_exhaustive]
13pub struct AudioInit {
14	/// The audio format, e.g. `"opus"` or `"aac"`.
15	pub format: AudioFormat,
16	/// Codec init bytes. Required: audio has no in-band config.
17	pub data: Bytes,
18	/// Human-readable rendition name for a track picker.
19	pub label: Option<String>,
20	/// The container wrapping each frame on the wire.
21	///
22	/// [`Legacy`](hang::catalog::Container::Legacy) unless selected. A video rendition carries the
23	/// same knob on its [`VideoInit::hint`].
24	pub container: hang::catalog::Container,
25	/// The maximum bitrate in bits per second, for an encoder that knows it. Supplied, it replaces
26	/// any value the init bytes give and is never estimated.
27	pub bitrate: Option<u64>,
28}
29
30impl AudioInit {
31	/// An init with just a format and its codec init bytes.
32	pub fn new(format: AudioFormat, data: impl Into<Bytes>) -> Self {
33		Self {
34			format,
35			data: data.into(),
36			label: None,
37			container: hang::catalog::Container::default(),
38			bitrate: None,
39		}
40	}
41}
42
43/// What a video importer needs to start: a format, optional init bytes, a label, and hints.
44///
45/// `format` selects the codec parser (`"h264"`, `"h265"`, `"vp8"`, ...). `data` may be empty for a
46/// format that resolves in band; a [`hint`](Self::hint) can pin fields the stream never reveals
47/// (bitrate) or publish the catalog before the first keyframe. See [`VideoHint`].
48#[derive(Clone, Debug, PartialEq)]
49#[non_exhaustive]
50pub struct VideoInit {
51	/// The video format, e.g. `"h264"` or `"vp8"`.
52	pub format: VideoFormat,
53	/// Codec init bytes (an avcC, an hvcC, ...). May be empty for a format that resolves in band.
54	pub data: Bytes,
55	/// Human-readable rendition name for a track picker.
56	pub label: Option<String>,
57	/// Catalog fields the stream cannot reveal itself.
58	pub hint: VideoHint,
59	/// The track's priority, which a [`bandwidth::Allocator`](moq_net::bandwidth::Allocator)
60	/// funds in order. A rendition published above a lower one of the same source sits one
61	/// below it, so the lower one is funded first. Defaults to
62	/// [`PRIORITY.video`](hang::catalog::PRIORITY).
63	pub priority: i8,
64}
65
66impl VideoInit {
67	/// An init with just a format and its codec init bytes (which may be empty).
68	pub fn new(format: VideoFormat, data: impl Into<Bytes>) -> Self {
69		Self {
70			format,
71			data: data.into(),
72			label: None,
73			hint: VideoHint::default(),
74			priority: hang::catalog::PRIORITY.video,
75		}
76	}
77}
78
79/// What a container importer needs to start: a format, its leading bytes, and an optional shared
80/// timebase.
81///
82/// A container publishes and describes its own tracks, so there is no label or hint here: a
83/// rendition field would have no single track to land on.
84#[non_exhaustive]
85pub struct ContainerInit<E: crate::catalog::hang::CatalogExt = ()> {
86	/// The container format, e.g. `"fmp4"` or `"ts"`.
87	pub format: ContainerFormat,
88	/// The leading chunk of the container, decoded immediately. May be empty.
89	pub data: Bytes,
90	/// The timestamp base the container shifts through, shared with other importers of its source,
91	/// or `None` for a fresh one of its own.
92	pub timebase: Option<crate::catalog::Timebase<E>>,
93}
94
95impl<E: crate::catalog::hang::CatalogExt> ContainerInit<E> {
96	/// An init with just a format and its leading bytes, on a timebase of its own.
97	pub fn new(format: ContainerFormat, data: impl Into<Bytes>) -> Self {
98		Self {
99			format,
100			data: data.into(),
101			timebase: None,
102		}
103	}
104
105	/// Shift through `timebase`, shared with other importers of the same source.
106	pub fn with_timebase(mut self, timebase: impl Into<Option<crate::catalog::Timebase<E>>>) -> Self {
107		self.timebase = timebase.into();
108		self
109	}
110}
111
112// Manual so an init is clonable and printable regardless of whether `E` is.
113impl<E: crate::catalog::hang::CatalogExt> Clone for ContainerInit<E> {
114	fn clone(&self) -> Self {
115		Self {
116			format: self.format,
117			data: self.data.clone(),
118			timebase: self.timebase.clone(),
119		}
120	}
121}
122
123impl<E: crate::catalog::hang::CatalogExt> std::fmt::Debug for ContainerInit<E> {
124	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
125		f.debug_struct("ContainerInit")
126			.field("format", &self.format)
127			.field("data", &self.data)
128			.field("shared", &self.timebase.is_some())
129			.finish()
130	}
131}