Skip to main content

moq_audio/decode/
consumer.rs

1//! Subscribe to an encoded audio track and emit raw PCM.
2
3use std::collections::VecDeque;
4
5use bytes::Bytes;
6
7use super::decoder::{Config, Decoder};
8use crate::resample::{Resampler, remix, validate_channels};
9use crate::{Activity, Error, Frame};
10
11/// Subscribe to a moq-mux audio track and emit decoded PCM in the layout
12/// declared by [`Config`].
13///
14/// The mirror of [`encode::Producer`](crate::encode::Producer): output format /
15/// sample rate / channel count are fixed at construction, and
16/// [`read`](Self::read) returns [`Frame`]s carrying the codec activity they
17/// were decoded from.
18pub struct Consumer {
19	decoder: Decoder,
20	track: moq_mux::container::Consumer<moq_mux::catalog::hang::Container>,
21	resampler: Option<Resampler>,
22	config: Config,
23	latency_max: std::time::Duration,
24	resolved_sample_rate: u32,
25	resolved_channels: u32,
26	/// Where the next packet's timestamp should land: the last packet's timestamp
27	/// plus the media it covered, including the codec delay the decoder trimmed off
28	/// the front. A packet that misses it is a hole nobody declared.
29	next_start: Option<moq_net::Timestamp>,
30	/// Frames decoded and not yet handed back, so a gap's tail can be returned
31	/// ahead of the packet that exposed it.
32	ready: VecDeque<Frame>,
33	/// Codec activity spans the resampler's buffered output still covers.
34	spans: VecDeque<ActivitySpan>,
35	/// Activity of the last span the output ran past, for the rounding samples the
36	/// filter leaves beyond the final input boundary.
37	trailing: Activity,
38	/// Timestamp of the first encoded packet in this decoder epoch, used to
39	/// interpret codec delay and a terminal marker.
40	epoch: Option<moq_net::Timestamp>,
41	/// Codec delay trimmed since the current decoder epoch began.
42	delay_trimmed: usize,
43	/// Codec-rate terminal frames emitted since `terminal_start`.
44	frames_decoded: usize,
45	/// Logical endpoint carried by an empty legacy frame before terminal packets.
46	end: Option<moq_net::Timestamp>,
47	/// Presentation time of the first decoded terminal frame.
48	terminal_start: Option<moq_net::Timestamp>,
49	/// Last container discontinuity applied to codec and resampler state.
50	discontinuity: u64,
51}
52
53struct ActivitySpan {
54	end: moq_net::Timestamp,
55	activity: Activity,
56}
57
58impl Consumer {
59	/// Subscribe to `name` in `broadcast`, using the catalog entry to pick the
60	/// codec.
61	pub async fn new(
62		broadcast: &moq_net::broadcast::Consumer,
63		catalog: &hang::catalog::AudioConfig,
64		name: impl Into<String>,
65		config: Config,
66	) -> Result<Self, Error> {
67		let decoder = Decoder::new(catalog)?;
68		let sample_rate = config.sample_rate.unwrap_or_else(|| decoder.sample_rate());
69		let channels = config.channels.unwrap_or_else(|| decoder.channel_count());
70		validate_channels(channels)?;
71
72		let resampler = if sample_rate == decoder.sample_rate() {
73			None
74		} else {
75			let chunk_frames = (decoder.sample_rate() as usize * 20) / 1000;
76			Some(Resampler::new(
77				decoder.sample_rate(),
78				sample_rate,
79				decoder.channel_count(),
80				chunk_frames,
81			)?)
82		};
83
84		let name = name.into();
85		let track = broadcast
86			.track(&name)?
87			.subscribe(moq_net::track::Subscription::default().with_priority(hang::catalog::PRIORITY.audio))
88			.await?;
89		let latency_max = config.latency_max.unwrap_or_default().min(track.info().latency_max);
90		// The catalog says how the track is framed, and it is not always the legacy
91		// wire: `moq import fmp4` publishes CMAF. Reading a moof+mdat fragment as a
92		// varint timestamp plus a payload decodes to garbage rather than failing.
93		let container = moq_mux::catalog::hang::Container::try_from(&catalog.container)?;
94		let mut track = moq_mux::container::Consumer::new(track, container);
95		if let Some(latency) = config.latency_max {
96			track = track.with_latency(latency);
97		}
98
99		Ok(Self {
100			decoder,
101			track,
102			resampler,
103			config,
104			latency_max,
105			resolved_sample_rate: sample_rate,
106			resolved_channels: channels,
107			next_start: None,
108			ready: VecDeque::new(),
109			spans: VecDeque::new(),
110			trailing: Activity::Active,
111			epoch: None,
112			delay_trimmed: 0,
113			frames_decoded: 0,
114			end: None,
115			terminal_start: None,
116			discontinuity: 0,
117		})
118	}
119
120	/// The config this consumer was built with.
121	pub fn config(&self) -> &Config {
122		&self.config
123	}
124
125	/// The effective latency budget after clamping to the publisher's retention window.
126	pub fn latency_max(&self) -> std::time::Duration {
127		self.latency_max
128	}
129
130	/// Sample rate samples are actually delivered at, which is
131	/// [`Config::sample_rate`] resolved against the catalog.
132	pub fn sample_rate(&self) -> u32 {
133		self.resolved_sample_rate
134	}
135
136	/// Channel count samples are actually delivered at, which is
137	/// [`Config::channels`] resolved against the catalog.
138	pub fn channels(&self) -> u32 {
139		self.resolved_channels
140	}
141
142	/// Read the next decoded PCM frame, or `None` when the track ends.
143	///
144	/// [`Frame::activity`] reports whether the packet these samples came from
145	/// coded audio. It describes where the frame begins, so a resampled
146	/// frame that straddles a change carries the activity its first sample came
147	/// from and the next frame carries the new one.
148	///
149	/// A timestamp that doesn't continue the previous packet is a hole in the
150	/// output, not a splice: nothing is carried across it, and the frames on either
151	/// side stay anchored to their own packet timeline, so the hole is there to
152	/// see. "Doesn't continue" allows for the quantization the stamps carry, which
153	/// on a millisecond-stamped ingest is most of a millisecond.
154	pub async fn read(&mut self) -> Result<Option<Frame>, Error> {
155		loop {
156			if let Some(frame) = self.ready.pop_front() {
157				return Ok(Some(frame));
158			}
159
160			let mux_frame = self.track.read().await?;
161			self.apply_discontinuity()?;
162			let Some(mux_frame) = mux_frame else {
163				return self.flush();
164			};
165
166			if let Some(end) = self.track.end()
167				&& self.end != Some(end)
168			{
169				self.end = Some(end);
170				self.frames_decoded = 0;
171				self.terminal_start = None;
172			}
173
174			// Undeclared holes are routine: a skipped stalled group, a packet the
175			// decoder refused, an ingest that resynced. Drop every stage's state at
176			// the edge, before the packet after it goes anywhere near the decoder.
177			//
178			// Skipped once an end marker arrives, because from there the terminal
179			// phase reconstructs each batch's time from the marker rather than
180			// reading it off the packet, so there is nothing left to compare.
181			if self.end.is_none()
182				&& self
183					.next_start
184					.is_some_and(|next| discontinuous(next, mux_frame.timestamp))
185				&& let Some(frame) = self.gap()?
186			{
187				self.ready.push_back(frame);
188			}
189
190			let rate = self.decoder.sample_rate();
191			let epoch = *self.epoch.get_or_insert(mux_frame.timestamp);
192			let delay = self.decoder.delay_remaining();
193			let decoded = self.decoder.decode(&mux_frame.payload)?;
194			// Codec delay trimmed off the front is media this packet covered even
195			// though no samples came out, so it still moves the packet after it along.
196			let trimmed = delay - self.decoder.delay_remaining();
197			self.delay_trimmed += trimmed;
198			let activity = decoded.activity;
199			let mut decoded = decoded.samples;
200			if let Some(end) = self.end {
201				let terminal_start = *self
202					.terminal_start
203					.get_or_insert(rewind(mux_frame.timestamp, self.delay_trimmed, rate)?.max(epoch));
204				let total = frames_between(terminal_start, end, rate)?;
205				let remaining = total.saturating_sub(self.frames_decoded);
206				decoded.truncate(remaining.saturating_mul(self.decoder.channel_count() as usize));
207			}
208
209			let frames = decoded.len() / self.decoder.channel_count().max(1) as usize;
210			let decoded_at = if let Some(terminal_start) = self.terminal_start {
211				advance(terminal_start, self.frames_decoded, rate)?
212			} else {
213				// The codec delay is padding before the epoch, not a hole after the
214				// first short frame. Keep later output contiguous by moving it back over
215				// everything trimmed since this decoder epoch began.
216				rewind(mux_frame.timestamp, self.delay_trimmed, rate)?.max(epoch)
217			};
218			if self.end.is_some() {
219				self.frames_decoded += frames;
220			}
221			// Packet continuity stays on the encoded timeline. `decoded_at` may be
222			// earlier because codec pre-skip is padding before the decoded epoch.
223			self.next_start = Some(advance(mux_frame.timestamp, frames + trimmed, rate)?);
224			if decoded.is_empty() {
225				continue;
226			}
227
228			let (pcm, timestamp) = match self.resampler.as_mut() {
229				// The resampler works in fixed chunks, so it holds back whatever didn't
230				// fill one. What comes out next starts with those held-back samples, which
231				// arrived before this packet did, so it is stamped where they arrived.
232				// Reading that off this packet instead would place the audio late by up to
233				// a chunk, sawtoothing A/V sync, and drag it the whole way whenever the
234				// source jumps forward without declaring a hole.
235				Some(r) => {
236					let held = if r.pending_frames() == 0 {
237						decoded_at
238					} else {
239						r.held_at().unwrap_or(decoded_at)
240					};
241					let skipped = r.skipped();
242					let pcm = r.process(&decoded, decoded_at)?;
243					(pcm, rewind(held, skipped, self.resolved_sample_rate)?)
244				}
245				None => (decoded, decoded_at),
246			};
247
248			let decoded_end = advance(decoded_at, frames, rate)?;
249
250			// The resampler hands back samples it was holding from earlier packets,
251			// so what comes out starts before the packet that filled its chunk. Track
252			// where each packet's activity ends so the output can be labelled by
253			// where it actually begins, not by the packet just submitted.
254			let resampled = self.resampler.is_some();
255			if resampled {
256				self.spans.push_back(ActivitySpan {
257					end: decoded_end,
258					activity,
259				});
260			}
261
262			// A packet shorter than the resampler's chunk leaves nothing to hand
263			// over yet. Read on rather than returning a frame with no samples, which
264			// a caller would otherwise see as audio arriving.
265			if pcm.is_empty() {
266				continue;
267			}
268
269			let activity = if resampled {
270				self.activity_at(timestamp)
271			} else {
272				activity
273			};
274			// Queued rather than returned, so a tail drained at a gap earlier in this
275			// same iteration still comes out first. The next turn of the loop pops it.
276			let frame = self.frame(pcm, timestamp, activity)?;
277			self.ready.push_back(frame);
278		}
279	}
280
281	/// Reset every stateful decode stage before the first packet of a new epoch.
282	fn apply_discontinuity(&mut self) -> Result<(), Error> {
283		let discontinuity = self.track.discontinuity();
284		if discontinuity == self.discontinuity {
285			return Ok(());
286		}
287
288		self.discontinuity = discontinuity;
289		self.decoder.reset()?;
290		if let Some(resampler) = self.resampler.as_mut() {
291			resampler.reset();
292		}
293		self.next_start = None;
294		self.spans.clear();
295		self.trailing = Activity::Active;
296		self.epoch = None;
297		self.delay_trimmed = 0;
298		self.frames_decoded = 0;
299		self.end = None;
300		self.terminal_start = None;
301		Ok(())
302	}
303
304	/// Reset codec prediction and resampling state at a hole, returning whatever the
305	/// resampler was still holding from before it.
306	///
307	/// Those samples arrived before the hole and belong before it, so they come
308	/// out as their own frame rather than being filtered together with the audio
309	/// on the far side. The resampler starts over from there, which is what makes
310	/// the next packet's output stamp from the packet itself: nothing is buffered
311	/// to reach back over.
312	fn gap(&mut self) -> Result<Option<Frame>, Error> {
313		self.decoder.reset_prediction()?;
314
315		let mut frame = None;
316		if let Some(resampler) = self.resampler.as_mut() {
317			let held = resampler.held_at();
318			let skipped = resampler.skipped();
319			let pcm = resampler.drain()?;
320			frame = self.tail(pcm, held, skipped)?;
321		}
322
323		self.next_start = None;
324		self.spans.clear();
325		self.trailing = Activity::Active;
326		self.epoch = None;
327		self.delay_trimmed = 0;
328		Ok(frame)
329	}
330
331	/// The tail the resampler is still holding when the track ends, once.
332	///
333	/// Without it the last partial chunk is dropped, which is up to a chunk of
334	/// audio missing from the end of every resampled track. Flushing consumes the
335	/// resampler, which is what makes calling this on every later poll return
336	/// `None` rather than more tails.
337	fn flush(&mut self) -> Result<Option<Frame>, Error> {
338		let Some(resampler) = self.resampler.take() else {
339			return Ok(None);
340		};
341
342		let held = resampler.held_at();
343		let skipped = resampler.skipped();
344		self.tail(resampler.flush()?, held, skipped)
345	}
346
347	/// Stamp and pack a tail the resampler handed back, if it handed back one.
348	///
349	/// `held` is where the samples it was holding arrived, which is where the tail
350	/// begins once the startup frames it dropped of its own are taken off. `None`
351	/// there means the resampler never ran, so there is nothing to place.
352	fn tail(
353		&mut self,
354		pcm: Vec<f32>,
355		held: Option<moq_net::Timestamp>,
356		skipped: usize,
357	) -> Result<Option<Frame>, Error> {
358		let Some(held) = held.filter(|_| !pcm.is_empty()) else {
359			return Ok(None);
360		};
361
362		let timestamp = rewind(held, skipped, self.resolved_sample_rate)?;
363		let activity = self.activity_at(timestamp);
364		Ok(Some(self.frame(pcm, timestamp, activity)?))
365	}
366
367	/// The codec activity covering `timestamp`, dropping the spans it has passed.
368	fn activity_at(&mut self, timestamp: moq_net::Timestamp) -> Activity {
369		while let Some(span) = self.spans.front().filter(|span| span.end <= timestamp) {
370			self.trailing = span.activity;
371			self.spans.pop_front();
372		}
373
374		self.spans.front().map_or(self.trailing, |span| span.activity)
375	}
376
377	/// Remix and pack decoded PCM into an output frame.
378	fn frame(&self, pcm: Vec<f32>, timestamp: moq_net::Timestamp, activity: Activity) -> Result<Frame, Error> {
379		let pcm = if self.decoder.channel_count() == self.resolved_channels {
380			pcm
381		} else {
382			remix(&pcm, self.decoder.channel_count(), self.resolved_channels)?
383		};
384
385		let bytes = self.config.format.from_interleaved_f32(&pcm, self.resolved_channels)?;
386		Ok(Frame {
387			timestamp,
388			data: Bytes::from(bytes),
389			activity,
390		})
391	}
392}
393
394/// Whether `timestamp` fails to continue `expected`, leaving a hole (or an
395/// overlap) rather than the next packet in line.
396///
397/// Exact contiguity cannot be the test. RTMP stamps in whole milliseconds while a
398/// 1024-sample AAC frame at 44.1 kHz runs 23.22 ms, so on the most common ingest
399/// path every packet lands beside where its predecessor ended.
400///
401/// The slack is the quantization the stamps carry, and nothing else. A frame
402/// duration would be far too much: a single lost packet lands exactly one frame
403/// off, and Opus packets run anywhere from 2.5 ms to 60 ms with no duration
404/// declared in the catalog, so a half-frame rule read off a 20 ms neighbour would
405/// splice straight across a lost 2.5 ms one.
406///
407/// So a packet is discontinuous when it misses `expected` by more than one unit of
408/// the coarsest timescale on the path, plus one unit of the stamp's own scale for
409/// the rounding in the arithmetic that produced `expected`. The coarsest timescale
410/// is the stamp's own scale floored at [`Timescale::default`](moq_net::Timescale):
411/// the legacy hang container re-stamps every frame in microseconds whatever the
412/// source used, and a wire that cannot carry a timescale at all (moq-lite before
413/// 05, IETF moq-transport) falls back to milliseconds, so a millisecond is the
414/// finest quantization a packet can be assumed to have kept. That floor stays under
415/// the shortest packet anything here can send, 2.5 ms of Opus, so it never
416/// swallows a lost one.
417fn discontinuous(expected: moq_net::Timestamp, timestamp: moq_net::Timestamp) -> bool {
418	let scale = expected.scale().max(timestamp.scale());
419	let quantum = scale.min(moq_net::Timescale::default());
420	let tolerance = (scale.as_u64() as u128).div_ceil(quantum.as_u64() as u128) + 1;
421	expected.as_scale(scale).abs_diff(timestamp.as_scale(scale)) > tolerance
422}
423
424/// `timestamp` moved forward by `frames` at `sample_rate`, in its own timescale.
425fn advance(timestamp: moq_net::Timestamp, frames: usize, sample_rate: u32) -> Result<moq_net::Timestamp, Error> {
426	if frames == 0 {
427		return Ok(timestamp);
428	}
429
430	let offset = moq_net::Timestamp::from_scale(frames as u64, sample_rate as u64)?.convert(timestamp.scale())?;
431	Ok(timestamp.checked_add(offset)?)
432}
433
434/// Codec-rate frames in the interval, rounding a microsecond marker to the nearest frame.
435fn frames_between(start: moq_net::Timestamp, end: moq_net::Timestamp, sample_rate: u32) -> Result<usize, Error> {
436	let duration = end.checked_sub(start)?;
437	let frames = (std::time::Duration::from(duration).as_nanos() * sample_rate as u128 + 500_000_000) / 1_000_000_000;
438	usize::try_from(frames).map_err(|_| Error::Unsupported("audio duration does not fit in memory".into()))
439}
440
441/// `timestamp` moved back by `frames` at `sample_rate`, in its own timescale.
442///
443/// Saturates at zero rather than failing: a publisher whose first timestamps
444/// don't advance is odd, but it isn't a reason to end the track.
445fn rewind(timestamp: moq_net::Timestamp, frames: usize, sample_rate: u32) -> Result<moq_net::Timestamp, Error> {
446	if frames == 0 {
447		return Ok(timestamp);
448	}
449
450	let offset = moq_net::Timestamp::from_scale(frames as u64, sample_rate as u64)?.convert(timestamp.scale())?;
451	Ok(timestamp
452		.checked_sub(offset)
453		.unwrap_or(moq_net::Timestamp::new(0, timestamp.scale())?))
454}
455
456#[cfg(test)]
457mod tests {
458	use moq_net::Timestamp;
459
460	use super::*;
461	use crate::Format;
462	use crate::encode::{Encoder, Input, Options, Producer};
463
464	#[tokio::test]
465	async fn remixes_mono_stream_to_stereo_output() {
466		let mut broadcast = moq_net::broadcast::Info::new().produce();
467		let catalog = moq_mux::catalog::Producer::new(&mut broadcast).unwrap();
468		let subscriber = broadcast.consume();
469		let input = Input {
470			format: Format::F32,
471			sample_rate: 48_000,
472			channels: 1,
473		};
474		let options = Options {
475			track: Some("audio".to_string()),
476			..Options::default()
477		};
478		let mut producer = Producer::new(&mut broadcast, catalog, input.clone(), &options).unwrap();
479		let catalog = Encoder::new(&crate::encode::Config::new(input)).unwrap().catalog();
480		let mut consumer = Consumer::new(
481			&subscriber,
482			&catalog,
483			"audio",
484			Config {
485				channels: Some(2),
486				..Config::new()
487			},
488		)
489		.await
490		.unwrap();
491
492		let samples = vec![0.1f32; 960];
493		let mut data = Vec::with_capacity(samples.len() * size_of::<f32>());
494		for sample in samples {
495			data.extend_from_slice(&sample.to_le_bytes());
496		}
497		producer.write(&Frame::new(data.into(), Timestamp::ZERO)).unwrap();
498
499		let frame = consumer.read().await.unwrap().expect("decoded frame");
500		let samples = Format::F32.as_interleaved_f32(&frame.data, 2).unwrap();
501		assert_eq!(samples.len(), (960 - 312) * 2);
502		for pair in samples.chunks_exact(2) {
503			assert_eq!(pair[0], pair[1]);
504		}
505	}
506
507	/// A packet whose sample count isn't a multiple of the resampler's chunk leaves
508	/// samples buffered, and the next output starts with those. Stamping that
509	/// output with the packet that completed the chunk puts it up to a chunk late,
510	/// which is a sawtooth in A/V sync rather than a constant offset. Any codec
511	/// whose frame is not a whole number of chunks reaches it: a 1024-sample frame
512	/// at 44.1 kHz never fills the 882-frame chunk evenly.
513	#[tokio::test]
514	async fn resampled_timestamps_follow_the_samples() {
515		let mut broadcast = moq_net::broadcast::Info::new().produce();
516		let track = broadcast.create_track("audio", hang::container::track_info()).unwrap();
517		let subscriber = broadcast.consume();
518
519		let catalog = hang::catalog::AudioConfig::new(hang::catalog::AudioCodec::Pcm, 44_100, 1);
520		let mut producer = moq_mux::container::Producer::new(track, moq_mux::catalog::hang::Container::Legacy);
521
522		let mut consumer = Consumer::new(
523			&subscriber,
524			&catalog,
525			"audio",
526			Config {
527				sample_rate: Some(48_000),
528				..Config::new()
529			},
530		)
531		.await
532		.unwrap();
533
534		// Two 1024-sample packets, back to back at the codec's own rate.
535		const FRAMES: u64 = 1024;
536		let payload: Bytes = vec![0u8; FRAMES as usize * size_of::<f32>()].into();
537		for packet in 0..2 {
538			producer
539				.write(moq_mux::container::Frame {
540					timestamp: moq_net::Timestamp::from_scale(packet * FRAMES, 44_100).unwrap(),
541					duration: None,
542					payload: payload.clone(),
543					keyframe: true,
544				})
545				.unwrap();
546		}
547
548		let first = consumer.read().await.unwrap().expect("decoded frame");
549		assert_eq!(first.timestamp.as_micros(), 0);
550
551		// Continuity, not a fixed number: the second frame starts where the first
552		// one's samples end, whatever they came to. Within a few frames rather than
553		// exactly, because the resampler emits whole frames and its count per chunk
554		// wobbles around the nominal ratio; a real hole (the samples it held back, or
555		// the startup silence it dropped) is twenty times this tolerance.
556		let second = consumer.read().await.unwrap().expect("decoded frame");
557		let first_frames = (first.data.len() / size_of::<f32>()) as u128;
558		let ends_at = first_frames * 1_000_000 / 48_000;
559		let gap = second.timestamp.as_micros().abs_diff(ends_at);
560		assert!(gap < 100, "expected the frames to meet, got a {gap} us gap");
561	}
562
563	/// The resampler only converts whole chunks, so the last partial one has to be
564	/// flushed at end of track or its audio is simply gone. A 1024-sample frame at
565	/// 44.1 kHz guarantees a remainder, never filling the 882-frame chunk evenly.
566	#[tokio::test]
567	async fn resampled_tail_survives_the_end_of_the_track() {
568		let mut broadcast = moq_net::broadcast::Info::new().produce();
569		let track = broadcast.create_track("audio", hang::container::track_info()).unwrap();
570		let subscriber = broadcast.consume();
571
572		let catalog = hang::catalog::AudioConfig::new(hang::catalog::AudioCodec::Pcm, 44_100, 1);
573		let mut producer = moq_mux::container::Producer::new(track, moq_mux::catalog::hang::Container::Legacy);
574
575		let mut consumer = Consumer::new(
576			&subscriber,
577			&catalog,
578			"audio",
579			Config {
580				sample_rate: Some(48_000),
581				..Config::new()
582			},
583		)
584		.await
585		.unwrap();
586
587		// One 1024-frame packet: 882 fill a chunk, 142 are left holding.
588		const FRAMES: usize = 1024;
589		let payload: Bytes = vec![0u8; FRAMES * size_of::<f32>()].into();
590		producer
591			.write(moq_mux::container::Frame {
592				timestamp: moq_net::Timestamp::ZERO,
593				duration: None,
594				payload,
595				keyframe: true,
596			})
597			.unwrap();
598		producer.finish().unwrap();
599
600		let first = consumer.read().await.unwrap().expect("decoded frame");
601		let first_frames = first.data.len() / size_of::<f32>();
602
603		let tail = consumer.read().await.unwrap().expect("flushed tail");
604		let tail_frames = tail.data.len() / size_of::<f32>();
605
606		// The 142 held-back frames at 44.1 kHz are ~155 at 48 kHz, plus the 69 the
607		// sinc filter still owes: it runs centred, so the end of the track only
608		// emerges once the flush has fed it silence to push it out.
609		assert!((215..=230).contains(&tail_frames), "unexpected tail: {tail_frames}");
610		// It picks up where the first frame's samples ended, within the same few
611		// frames of whole-frame rounding as above.
612		let ends_at = (first_frames as u128) * 1_000_000 / 48_000;
613		let gap = tail.timestamp.as_micros().abs_diff(ends_at);
614		assert!(gap < 100, "expected the tail to meet the body, got a {gap} us gap");
615
616		// Together they cover the packet and no more: 1024 frames at 44.1 kHz is
617		// ~1114 at 48 kHz. The filter's delay does not extend the stream, because
618		// what the drain adds here is what the start dropped off the front.
619		let total = first_frames + tail_frames;
620		assert!((1105..=1120).contains(&total), "unexpected total: {total}");
621		assert!(consumer.read().await.unwrap().is_none());
622	}
623
624	#[tokio::test]
625	async fn resampling_keeps_the_activity_boundary_on_its_source() {
626		let mut encoder = Encoder::new(&crate::encode::Config {
627			dtx: true,
628			bitrate: Some(24_000),
629			frame_duration: std::time::Duration::from_millis(10),
630			..crate::encode::Config::new(Input {
631				channels: 1,
632				..Input::default()
633			})
634		})
635		.unwrap();
636		let catalog = hang::catalog::AudioConfig::new(hang::catalog::AudioCodec::Opus, 48_000, 1);
637
638		let mut broadcast = moq_net::broadcast::Info::new().produce();
639		let track = broadcast.create_track("audio", hang::container::track_info()).unwrap();
640		let subscriber = broadcast.consume();
641		let mut producer = moq_mux::container::Producer::new(track, moq_mux::catalog::hang::Container::Legacy);
642		let mut consumer = Consumer::new(
643			&subscriber,
644			&catalog,
645			"audio",
646			Config {
647				sample_rate: Some(44_100),
648				..Config::new()
649			},
650		)
651		.await
652		.unwrap();
653
654		let active = vec![0.5; encoder.frame_size()];
655		let silence = vec![0.0; encoder.frame_size()];
656		let mut first_dtx = None;
657		for index in 0..40u64 {
658			let packet = encoder.encode(if index == 0 { &active } else { &silence }).unwrap();
659			let timestamp = Timestamp::from_scale(index * encoder.frame_size() as u64, 48_000).unwrap();
660			if first_dtx.is_none() && packet.activity.is_dtx() {
661				first_dtx = Some(timestamp);
662			}
663			producer
664				.write(moq_mux::container::Frame {
665					timestamp,
666					payload: packet.payload,
667					keyframe: true,
668					duration: None,
669				})
670				.unwrap();
671			producer.cut(None).unwrap();
672		}
673		producer.finish().unwrap();
674
675		let expected = first_dtx.expect("silence should enter Opus DTX");
676		let mut actual = None;
677		while let Some(frame) = consumer.read().await.unwrap() {
678			// 10 ms packets do not fill the 20 ms chunk, so the resampler hands back
679			// nothing every other packet. Those must not surface as frames: a frame
680			// with no samples reads as audio arriving, and carries an activity
681			// describing samples that are not there.
682			assert!(!frame.data.is_empty(), "read returned a frame with no samples");
683			if frame.activity.is_dtx() {
684				actual = Some(frame.timestamp);
685				break;
686			}
687		}
688		let actual = actual.expect("consumer should report Opus DTX");
689
690		// Each frame carries the activity its first sample came from, so the label
691		// can lag its source by up to the frame it lands in, but it must never lead
692		// it: leading means samples that are still active got labelled DTX. That is
693		// what labelling by the packet most recently submitted does, since the
694		// resampler is handing back audio from before that packet. It puts the
695		// boundary a chunk early instead of a fraction of a chunk late.
696		let delay = actual.as_micros() as i128 - expected.as_micros() as i128;
697		let chunk_us = 20_000i128;
698		assert!(
699			(0..chunk_us).contains(&delay),
700			"DTX label landed {delay} us from its source, outside [0, {chunk_us})"
701		);
702	}
703
704	/// Publish PCM packets of `frames` samples each at the given stamps, and read
705	/// back every decoded frame as `(microseconds, output frames)`.
706	async fn pcm_gaps(rate: u32, out_rate: u32, frames: usize, stamps: &[Timestamp]) -> Vec<(u128, usize)> {
707		let mut broadcast = moq_net::broadcast::Info::new().produce();
708		let track = broadcast.create_track("audio", hang::container::track_info()).unwrap();
709		let subscriber = broadcast.consume();
710
711		let catalog = hang::catalog::AudioConfig::new(hang::catalog::AudioCodec::Pcm, rate, 1);
712		let mut producer = moq_mux::container::Producer::new(track, moq_mux::catalog::hang::Container::Legacy);
713		let mut consumer = Consumer::new(
714			&subscriber,
715			&catalog,
716			"audio",
717			Config {
718				sample_rate: Some(out_rate),
719				..Config::new()
720			},
721		)
722		.await
723		.unwrap();
724
725		let payload: Bytes = vec![0u8; frames * size_of::<f32>()].into();
726		for stamp in stamps {
727			producer
728				.write(moq_mux::container::Frame {
729					timestamp: *stamp,
730					duration: None,
731					payload: payload.clone(),
732					keyframe: true,
733				})
734				.unwrap();
735		}
736		producer.finish().unwrap();
737
738		let mut read = Vec::new();
739		while let Some(frame) = consumer.read().await.unwrap() {
740			read.push((frame.timestamp.as_micros(), frame.data.len() / size_of::<f32>()));
741		}
742		read
743	}
744
745	/// A packet that doesn't continue the last one is a hole, not a splice: the
746	/// resampler hands back what it was holding from before the gap as its own
747	/// frame, and the audio after it is stamped from the packet that carried it
748	/// rather than rewound over samples that no longer exist.
749	#[tokio::test]
750	async fn a_missing_packet_leaves_a_hole() {
751		const FRAMES: usize = 1024;
752		// Packets at sample 0 and sample 2048: the one at 1024 never arrived.
753		let stamps = [
754			Timestamp::from_scale(0, 44_100).unwrap(),
755			Timestamp::from_scale(2 * FRAMES as u64, 44_100).unwrap(),
756		];
757		let read = pcm_gaps(44_100, 48_000, FRAMES, &stamps).await;
758
759		// The first packet's chunk, then the tail drained at the gap, then the
760		// second packet's chunk. The flush at end of track adds the last tail.
761		assert_eq!(read.len(), 4, "unexpected frames: {read:?}");
762
763		// Everything the first packet carried comes out before the hole: 1024 frames
764		// at 44.1 kHz is ~1114 at 48 kHz, whole-frame rounding aside.
765		let before: usize = read[..2].iter().map(|(_, frames)| frames).sum();
766		assert!((1105..=1120).contains(&before), "unexpected pre-gap audio: {before}");
767
768		// The audio after the hole is stamped by its own packet. Rewinding over the
769		// resampler's buffer instead would put it ~3 ms early, in the middle of the
770		// hole, and splice the two sides together through the filter.
771		assert_eq!(read[2].0, stamps[1].as_micros());
772
773		// And the hole is the packet that never arrived: 1024 frames at 44.1 kHz.
774		let ends_at = read[1].0 + (read[1].1 as u128) * 1_000_000 / 48_000;
775		let hole = read[2].0 - ends_at;
776		assert!((23_100..=23_350).contains(&hole), "unexpected hole: {hole} us");
777	}
778
779	/// A packet can land a hair past where the last one ended without being a hole:
780	/// the stamps are quantized, so `discontinuous` allows a millisecond of slack.
781	/// The jump is still a jump, and the resampler is holding samples from before
782	/// it. Deriving their stamp by counting back from the packet drags them forward
783	/// by the whole jump; reading it off the packet they arrived with does not.
784	#[tokio::test]
785	async fn a_jump_inside_the_slack_leaves_the_held_samples_alone() {
786		// 441 frames at 44.1 kHz is 10 ms, half of the 20 ms chunk, so the first
787		// packet is held whole and the second is what completes the chunk.
788		const FRAMES: usize = 441;
789		// A millisecond past where the first packet ended, which is the slack the
790		// legacy container's microsecond re-stamping is allowed.
791		let stamps = [
792			Timestamp::from_micros(0).unwrap(),
793			Timestamp::from_micros(11_000).unwrap(),
794		];
795		let read = pcm_gaps(44_100, 48_000, FRAMES, &stamps).await;
796
797		// The chunk, then the flush at the end of the track: no hole was declared, so
798		// nothing was drained in between.
799		assert_eq!(read.len(), 2, "unexpected frames: {read:?}");
800		// It starts with the first packet's samples, so it is stamped where that
801		// packet was. Rewinding from the second one instead puts it a millisecond late.
802		assert_eq!(read[0].0, 0, "held samples moved with the jump: {read:?}");
803	}
804
805	#[tokio::test]
806	async fn a_jump_after_a_full_chunk_uses_the_new_packet_timestamp() {
807		let stamps = [
808			Timestamp::from_micros(0).unwrap(),
809			Timestamp::from_micros(21_000).unwrap(),
810		];
811		let read = pcm_gaps(44_100, 48_000, 882, &stamps).await;
812		let mut r = crate::Resampler::new(44_100, 48_000, 1, 882).unwrap();
813		r.process(&[0.25; 882], stamps[0]).unwrap();
814		let expected = rewind(stamps[1], r.skipped(), 48_000).unwrap().as_micros();
815		assert_eq!(read[1].0, expected);
816	}
817
818	/// Once an end marker arrives the gap check stops running, because from there
819	/// each batch's time is reconstructed from the marker rather than read off the
820	/// packet. A packet that jumps forward then still moves whatever the resampler
821	/// is holding, and the activity that lands with it: those samples came from
822	/// before the jump and are labelled by the packet they came from.
823	#[tokio::test]
824	async fn a_terminal_jump_leaves_the_held_samples_alone() {
825		let mut encoder = Encoder::new(&crate::encode::Config {
826			dtx: true,
827			bitrate: Some(24_000),
828			..crate::encode::Config::new(Input {
829				channels: 1,
830				..Input::default()
831			})
832		})
833		.unwrap();
834		let catalog = encoder.catalog();
835
836		// One coded packet, then a withheld one to follow it. Taken from the same
837		// encoder rather than published in between, so nothing fills the resampler's
838		// chunk between the two.
839		let active = encoder.encode(&vec![0.5f32; encoder.frame_size()]).unwrap();
840		assert!(active.activity.is_active());
841		let silence = vec![0.0f32; encoder.frame_size()];
842		let dtx = (0..200)
843			.map(|_| encoder.encode(&silence).unwrap())
844			.find(|packet| packet.activity.is_dtx())
845			.expect("silence should enter Opus DTX");
846
847		let mut broadcast = moq_net::broadcast::Info::new().produce();
848		let track = broadcast.create_track("audio", hang::container::track_info()).unwrap();
849		let subscriber = broadcast.consume();
850		let mut producer = moq_mux::container::Producer::new(track, moq_mux::catalog::hang::Container::Legacy);
851		let mut consumer = Consumer::new(
852			&subscriber,
853			&catalog,
854			"audio",
855			Config {
856				sample_rate: Some(44_100),
857				..Config::new()
858			},
859		)
860		.await
861		.unwrap();
862
863		// A 20 ms Opus packet decodes 960 frames, less the pre-skip on the first one,
864		// so it doesn't fill the 960-frame chunk and is held whole.
865		let write = |producer: &mut moq_mux::container::Producer<_>, frames: u64, payload: Bytes| {
866			producer
867				.write(moq_mux::container::Frame {
868					timestamp: Timestamp::from_scale(frames, 48_000).unwrap(),
869					duration: None,
870					payload,
871					keyframe: true,
872				})
873				.unwrap();
874		};
875		write(&mut producer, 0, active.payload);
876		// The end marker, then the terminal packet a second past where it belongs.
877		write(&mut producer, 3 * 48_000, Bytes::new());
878		write(&mut producer, 48_000, dtx.payload);
879		producer.finish().unwrap();
880
881		let frame = consumer.read().await.unwrap().expect("decoded frame");
882		// The output begins with the first packet's samples, so it is stamped and
883		// labelled from that packet. Rewinding from the terminal one instead drops it
884		// most of a second into the future, carrying the DTX label with it.
885		assert_eq!(frame.timestamp.as_micros(), 0, "held samples moved with the jump");
886		assert!(
887			frame.activity.is_active(),
888			"held samples took the terminal packet's label"
889		);
890	}
891
892	/// Every packet on the RTMP path lands beside where the last one ended: FLV
893	/// stamps in whole milliseconds and a 1024-sample AAC frame at 44.1 kHz runs
894	/// 23.22 ms, so the stamps drift up to a millisecond either way. Reading that as
895	/// a hole would reset the codec and the resampler on nearly every packet.
896	///
897	/// PCM stands in for AAC, which needs an encoder this crate doesn't have: the
898	/// arithmetic that matters is the packet length and the millisecond stamps.
899	#[tokio::test]
900	async fn millisecond_stamps_are_not_a_gap() {
901		const FRAMES: u64 = 1024;
902		const PACKETS: u64 = 32;
903
904		// What an FLV ingest sends: each packet stamped in whole milliseconds.
905		let stamps: Vec<_> = (0..PACKETS)
906			.map(|packet| Timestamp::from_millis(packet * FRAMES * 1_000 / 44_100).unwrap())
907			.collect();
908		let read = pcm_gaps(44_100, 48_000, FRAMES as usize, &stamps).await;
909
910		// One frame per packet, since 1024 frames always fill at least one 882-frame
911		// chunk, plus the flush at the end of the track. Reading a gap would drain
912		// the resampler as well, adding a frame at every packet it fired on.
913		assert_eq!(read.len(), stamps.len() + 1, "unexpected frames: {read:?}");
914
915		// And the output stays continuous across all of them, within the millisecond
916		// the stamps themselves are quantized to.
917		for pair in read.windows(2) {
918			let ends_at = pair[0].0 + (pair[0].1 as u128) * 1_000_000 / 48_000;
919			assert!(
920				pair[1].0.abs_diff(ends_at) <= 1_100,
921				"frames at {} and {} do not meet",
922				pair[0].0,
923				pair[1].0
924			);
925		}
926	}
927
928	/// The tolerance can't come from a frame duration. Opus packets run from 2.5 ms
929	/// to 60 ms with nothing in the catalog to say which, so a rule read off the
930	/// 20 ms packet before it would splice straight across a lost 2.5 ms one.
931	#[tokio::test]
932	async fn a_lost_opus_packet_shorter_than_its_neighbour_is_a_gap() {
933		let input = Input {
934			format: Format::F32,
935			sample_rate: 48_000,
936			channels: 1,
937		};
938		let mut encoder = Encoder::new(&crate::encode::Config::new(input)).unwrap();
939		let catalog = encoder.catalog();
940
941		let mut broadcast = moq_net::broadcast::Info::new().produce();
942		let track = broadcast.create_track("audio", hang::container::track_info()).unwrap();
943		let subscriber = broadcast.consume();
944		let mut producer = moq_mux::container::Producer::new(track, moq_mux::catalog::hang::Container::Legacy);
945		let mut consumer = Consumer::new(&subscriber, &catalog, "audio", Config::new())
946			.await
947			.unwrap();
948
949		// A 20 ms packet at 0, then the next one at 22.5 ms: the 2.5 ms packet
950		// between them was lost.
951		let pcm = vec![0.25f32; encoder.frame_size()];
952		for timestamp in [
953			Timestamp::from_micros(0).unwrap(),
954			Timestamp::from_micros(22_500).unwrap(),
955			Timestamp::from_micros(42_500).unwrap(),
956		] {
957			producer
958				.write(moq_mux::container::Frame {
959					timestamp,
960					duration: None,
961					payload: encoder.encode(&pcm).unwrap().payload,
962					keyframe: true,
963				})
964				.unwrap();
965			producer.cut(None).unwrap();
966		}
967
968		// The pre-skip is trimmed off the first packet, so it decodes short. That
969		// shortfall is codec delay, not a hole: without counting it the packet after
970		// every stream start would read as a gap.
971		let first = consumer.read().await.unwrap().expect("decoded frame");
972		let frames = first.data.len() / size_of::<f32>();
973		assert!(frames < 960, "the pre-skip should be trimmed, got {frames} frames");
974
975		// The hole is real, so codec prediction starts over but stream-level pre-skip
976		// does not. The audio is stamped where the packet says rather than 2.5 ms early.
977		let second = consumer.read().await.unwrap().expect("decoded frame");
978		assert_eq!(second.timestamp.as_micros(), 22_500);
979		assert_eq!(second.data.len() / size_of::<f32>(), 960, "pre-skip was reapplied");
980
981		let third = consumer.read().await.unwrap().expect("decoded frame after gap");
982		let second_frames = second.data.len() / size_of::<f32>();
983		assert_eq!(
984			third.timestamp,
985			advance(second.timestamp, second_frames, 48_000).unwrap()
986		);
987	}
988
989	#[tokio::test]
990	async fn latency_max_is_clamped_to_publisher_retention() {
991		let mut broadcast = moq_net::broadcast::Info::new().produce();
992		let info = hang::container::track_info().with_latency_max(std::time::Duration::from_millis(100));
993		let _track = broadcast.create_track("audio", info).unwrap();
994		let subscriber = broadcast.consume();
995		let catalog = hang::catalog::AudioConfig::new(hang::catalog::AudioCodec::Pcm, 48_000, 1);
996
997		let consumer = Consumer::new(
998			&subscriber,
999			&catalog,
1000			"audio",
1001			Config {
1002				latency_max: Some(std::time::Duration::from_millis(500)),
1003				..Config::new()
1004			},
1005		)
1006		.await
1007		.unwrap();
1008
1009		assert_eq!(consumer.latency_max(), std::time::Duration::from_millis(100));
1010	}
1011
1012	/// Opus pre-skip is padding before the decoded epoch, not missing media after
1013	/// the first short frame. The second frame must meet the first or playback
1014	/// fills the codec delay with silence and creates a startup glitch.
1015	#[tokio::test]
1016	async fn opus_pre_skip_does_not_leave_a_timestamp_hole() {
1017		let input = Input {
1018			format: Format::F32,
1019			sample_rate: 48_000,
1020			channels: 1,
1021		};
1022		let mut encoder = Encoder::new(&crate::encode::Config::new(input)).unwrap();
1023		let catalog = encoder.catalog();
1024
1025		let mut broadcast = moq_net::broadcast::Info::new().produce();
1026		let track = broadcast.create_track("audio", hang::container::track_info()).unwrap();
1027		let subscriber = broadcast.consume();
1028		let mut producer = moq_mux::container::Producer::new(track, moq_mux::catalog::hang::Container::Legacy);
1029		let mut consumer = Consumer::new(&subscriber, &catalog, "audio", Config::new())
1030			.await
1031			.unwrap();
1032
1033		let pcm = vec![0.25f32; encoder.frame_size()];
1034		for packet in 0..2 {
1035			producer
1036				.write(moq_mux::container::Frame {
1037					timestamp: Timestamp::from_scale(packet * encoder.frame_size() as u64, 48_000).unwrap(),
1038					duration: None,
1039					payload: encoder.encode(&pcm).unwrap().payload,
1040					keyframe: true,
1041				})
1042				.unwrap();
1043			producer.cut(None).unwrap();
1044		}
1045
1046		let first = consumer.read().await.unwrap().expect("first decoded frame");
1047		let second = consumer.read().await.unwrap().expect("second decoded frame");
1048		let first_frames = first.data.len() / size_of::<f32>();
1049		let expected = advance(first.timestamp, first_frames, 48_000).unwrap();
1050		assert_eq!(second.timestamp, expected);
1051	}
1052
1053	#[tokio::test]
1054	async fn reads_the_container_the_catalog_declares() {
1055		let mut broadcast = moq_net::broadcast::Info::new().produce();
1056		let track = broadcast.create_track("audio", hang::container::track_info()).unwrap();
1057		let subscriber = broadcast.consume();
1058
1059		let mut catalog = hang::catalog::AudioConfig::new(hang::catalog::AudioCodec::Pcm, 48_000, 1);
1060		catalog.container = hang::catalog::Container::Loc;
1061
1062		let mut producer = moq_mux::container::Producer::new(track, moq_mux::catalog::hang::Container::Loc);
1063		let mut consumer = Consumer::new(
1064			&subscriber,
1065			&catalog,
1066			"audio",
1067			Config {
1068				format: Format::F32,
1069				..Config::new()
1070			},
1071		)
1072		.await
1073		.unwrap();
1074
1075		let samples = [0.25f32, -0.5, 0.75, -1.0];
1076		let payload: Vec<u8> = samples.iter().flat_map(|sample| sample.to_le_bytes()).collect();
1077		producer
1078			.write(moq_mux::container::Frame {
1079				timestamp: Timestamp::ZERO,
1080				duration: None,
1081				payload: payload.into(),
1082				keyframe: true,
1083			})
1084			.unwrap();
1085
1086		let frame = consumer.read().await.unwrap().expect("decoded frame");
1087		assert_eq!(
1088			Format::F32.as_interleaved_f32(&frame.data, 1).unwrap().as_ref(),
1089			samples
1090		);
1091	}
1092
1093	/// The catalog picks the framing, not this crate. Hardcoding the legacy wire
1094	/// read a CMAF fragment as a varint timestamp plus a payload, which handed the
1095	/// codec garbage instead of failing, so anything published by `moq import
1096	/// fmp4` was undecodable.
1097	#[tokio::test]
1098	async fn decodes_a_cmaf_framed_track() {
1099		let input = Input {
1100			format: Format::F32,
1101			sample_rate: 48_000,
1102			channels: 2,
1103		};
1104
1105		// One real Opus packet, so a mis-framed read can't accidentally decode.
1106		let mut encoder = Encoder::new(&crate::encode::Config::new(input.clone())).unwrap();
1107		let mut catalog = encoder.catalog();
1108		let pcm = vec![0.0f32; encoder.frame_size() * encoder.codec_channels() as usize];
1109		let packet = encoder.encode(&pcm).unwrap();
1110
1111		// Re-describe the same rendition as CMAF and publish it that way.
1112		let muxer = moq_mux::container::fmp4::Muxer::audio(&catalog).unwrap();
1113		let init = muxer.init().unwrap().expect("an out-of-band codec has an init segment");
1114		catalog.container = hang::catalog::Container::Cmaf { init };
1115
1116		let mut broadcast = moq_net::broadcast::Info::new().produce();
1117		let subscriber = broadcast.consume();
1118		let track = broadcast.create_track("audio", hang::container::track_info()).unwrap();
1119		let container = moq_mux::catalog::hang::Container::try_from(&catalog.container).unwrap();
1120		let mut producer = moq_mux::container::Producer::new(track, container);
1121
1122		let mut consumer = Consumer::new(&subscriber, &catalog, "audio", Config::new())
1123			.await
1124			.unwrap();
1125
1126		producer
1127			.write(moq_mux::container::Frame {
1128				timestamp: Timestamp::ZERO,
1129				payload: packet.payload,
1130				keyframe: true,
1131				duration: None,
1132			})
1133			.unwrap();
1134		producer.cut(None).unwrap();
1135
1136		// The whole packet decodes: one 20 ms Opus frame at 48 kHz, less the pre-skip
1137		// trimmed off the first packet. Reading the fragment as legacy hands the codec
1138		// a slice of the moof instead, which still decodes, just to a shorter buffer.
1139		let frame = consumer.read().await.unwrap().expect("decoded frame");
1140		// `as_micros`, not `==`: the CMAF path carries the fmp4 timescale and
1141		// `Timestamp`'s equality is structural, so the scales would have to match too.
1142		assert_eq!(frame.timestamp.as_micros(), 0);
1143		let samples = Format::F32.as_interleaved_f32(&frame.data, 2).unwrap();
1144		assert_eq!(samples.len(), (960 - 312) * 2);
1145	}
1146}