moq_mux/codec/aac/
import.rs1use super::Config;
2use crate::catalog::hang::CatalogExt;
3use crate::container::Frame;
4use crate::import::track::Written;
5
6pub struct Import {
15 track: crate::container::Producer<crate::catalog::hang::Wire, hang::catalog::AudioConfig>,
16 frame: Option<moq_net::Timestamp>,
19}
20
21impl Import {
22 pub fn new<E: CatalogExt>(
29 track: moq_net::track::Producer,
30 reserved: crate::catalog::Reserved<E>,
31 config: hang::catalog::AudioConfig,
32 ) -> crate::Result<Self> {
33 if config.description.is_none() {
34 let err = crate::Error::from(super::Error::MissingDescription);
35 track.abort(err.reason())?;
36 return Err(err);
37 }
38 tracing::debug!(name = ?track.name(), ?config, "starting track");
39 let wire = crate::catalog::hang::Wire::try_from(&config)?;
42 let frame = config.description.as_deref().and_then(super::frame_duration);
43 let track = reserved.audio(track, wire, config)?;
47 Ok(Self { track, frame })
48 }
49
50 pub fn name(&self) -> &str {
52 self.track.name()
53 }
54
55 pub fn demand(&self) -> moq_net::track::Demand {
57 self.track.track().demand()
58 }
59
60 pub fn finish(
65 self,
66 ) -> crate::Result<crate::container::Finished<crate::catalog::hang::Wire, hang::catalog::AudioConfig>> {
67 self.track.finish()
68 }
69
70 pub(crate) fn finish_in_place(&mut self) -> crate::Result<()> {
72 self.track.end_with(None)
73 }
74
75 pub fn abort(self, err: moq_net::Error) {
78 self.track.abort(err);
79 }
80
81 pub(crate) fn burst(&mut self, duration: std::time::Duration) -> crate::Result<()> {
83 self.track.burst(duration)
84 }
85
86 pub(crate) fn close(&mut self, end: Option<moq_net::Timestamp>) -> crate::Result<()> {
89 match end {
90 Some(end) => self.track.cut(Some(end)),
91 None => self.track.cut_unended(),
92 }
93 }
94
95 pub fn cut(&mut self, end: Option<moq_net::Timestamp>) -> crate::Result<()> {
97 self.track.cut(end)?;
98 Ok(())
99 }
100
101 pub fn discontinuity(&mut self) -> crate::Result<()> {
105 self.track.discontinuity()?;
106 Ok(())
107 }
108
109 pub fn decode<B: moq_net::IntoBytes>(&mut self, frame: B, pts: Option<moq_net::Timestamp>) -> crate::Result<()> {
115 self.write(frame.into_bytes(), pts).map(|_| ())
116 }
117
118 pub(crate) fn write(&mut self, payload: bytes::Bytes, pts: Option<moq_net::Timestamp>) -> crate::Result<Written> {
120 let timestamp = self.track.timestamp(pts)?;
121 let keyframe = self.track.needs_keyframe();
124 self.track.write(Frame {
125 timestamp,
126 decode: None,
127 payload,
128 keyframe,
129 duration: None,
130 })?;
131 Ok(Written {
132 timestamp,
133 duration: self.frame,
134 })
135 }
136}
137
138pub fn config(init: &[u8]) -> crate::Result<hang::catalog::AudioConfig> {
142 let mut buf = init;
143 let parsed = Config::parse(&mut buf)?;
144 let mut audio = catalog_config(&parsed);
147 audio.description = Some(bytes::Bytes::copy_from_slice(init));
148 Ok(audio)
149}
150
151impl TryFrom<Config> for hang::catalog::AudioConfig {
152 type Error = super::Error;
153
154 fn try_from(config: Config) -> Result<Self, Self::Error> {
160 let mut audio = catalog_config(&config);
161 audio.description = Some(config.encode()?);
162 Ok(audio)
163 }
164}
165
166fn catalog_config(config: &Config) -> hang::catalog::AudioConfig {
167 let mut audio = hang::catalog::AudioConfig::new(
168 hang::catalog::AAC {
169 profile: config.profile,
170 },
171 config.sample_rate,
172 config.channel_count,
173 );
174 audio.container = hang::catalog::Container::Legacy;
175 audio
176}
177
178#[cfg(test)]
179mod tests {
180 #[tokio::test(start_paused = true)]
181 async fn raw_publication_needs_a_description_before_advertising() {
182 let mut broadcast = moq_net::broadcast::Info::new().produce();
183 let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap();
184 let track = broadcast
185 .create_track("audio", hang::container::track_info(hang::catalog::PRIORITY.audio))
186 .unwrap();
187 let mut subscriber = track.subscribe(None);
188 let config = hang::catalog::AudioConfig::new(hang::catalog::AAC { profile: 2 }, 48_000, 2);
189 assert!(matches!(
190 super::Import::new(track, catalog.reserve(), config),
191 Err(crate::Error::Aac(super::super::Error::MissingDescription))
192 ));
193 assert!(catalog.snapshot().audio.renditions.is_empty());
194 assert!(matches!(
195 subscriber.poll_finished(&kio::Waiter::noop()),
196 std::task::Poll::Ready(Err(_))
197 ));
198 }
199}