Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/bin/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ and LTP profiles. HE-AAC and HE-AACv2 go out as their AAC-LC core, and decoders
find the SBR and PS in band, as ffmpeg's ADTS output does. A track whose
profile or channel layout ADTS cannot label is refused rather than mislabeled.

An AAC layout without a channelConfiguration, such as quad, rides in a program
config element. `export ts` repeats it in the first frame after each PAT/PMT, so a
receiver tuning in mid-stream can decode. ffmpeg writes it in the first frame
only, so `import ts` joining an ffmpeg source mid-stream publishes the rest of
the program without that AAC track, and adds the track if an element arrives.

A constant-rate MPEG-TS source records its multiplex rate in the catalog
(`mpegts.muxRate`, measured off the PCR clock, null stuffing included), and
`export ts` pads its output with null packets back to that rate so an IRD or
Expand Down
1 change: 0 additions & 1 deletion quest/m1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ blocks. The quests that gated m0 lines moved under them.
- [AAC encode refusals](/quest/m1/aac-encode-refusals.md) - `Config::encode` refuses channel counts it cannot name instead of writing stereo
- [AAC parse truncated SBR](/quest/m1/aac-parse-truncated-sbr.md) - `Config::parse` refuses SBR and PS configs cut off before their core
- [GStreamer surround Opus](/quest/m1/gst-opus-surround.md) - the moq-gst sink publishes 3 to 8 channel Opus with the OpusHead its caps describe
- [TS AAC PCE joins](/quest/m1/ts-aac-pce-join.md) - PCE-described AAC over TS plays after a mid-stream join or resume, and a missing PCE silences only its track
- [FFI frame duration default](/quest/m1/ffi-frame-duration-default.md) - the binding audio encoder takes the codec's own frame by default, so `aac()` needs no explicit 0
- [mp4-atom dOps mapping](/quest/m1/mp4-atom-dops-mapping.md) - a released mp4-atom reads and writes any `dOps` channel mapping family and table
- [CMAF surround Opus](/quest/m1/cmaf-opus-surround.md) - fMP4 import and export carry an Opus channel mapping table
Expand Down
36 changes: 0 additions & 36 deletions quest/m1/ts-aac-pce-join.md

This file was deleted.

49 changes: 34 additions & 15 deletions rs/moq-mux/src/container/ts/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,9 @@ enum Kind {
/// Video carries its TS stream type (H.264 = 0x1B, H.265 = 0x24).
Video(StreamType),
/// AAC, framed as ADTS. A `channel_config` of 0 defers the layout to a program config
/// element, which leads the next raw data block written and is then taken. A catalog update
/// rebuilds the kind and so repeats it once, which a decoder tuning in mid-stream welcomes.
Aac(aac::InBand),
/// element, which leads the first raw data block after each PAT/PMT, so a receiver that tunes
/// in at the tables can decode from there. `repeat` marks that the next frame carries it.
Aac { config: aac::InBand, repeat: bool },
/// Opus (private stream_type 0x06). Each frame is one Opus packet, prefixed with
/// the Opus-in-TS access-unit control header and announced with the 'Opus'
/// registration plus DVB extension descriptor.
Expand Down Expand Up @@ -1486,7 +1486,7 @@ impl<E: catalog::Catalog> Export<E> {
tracks.iter().find(|t| {
matches!(
t.kind,
Kind::Aac(_) | Kind::Opus { .. } | Kind::Mp2 { .. } | Kind::Ac3 | Kind::Eac3
Kind::Aac { .. } | Kind::Opus { .. } | Kind::Mp2 { .. } | Kind::Ac3 | Kind::Eac3
)
})
})
Expand All @@ -1498,7 +1498,7 @@ impl<E: catalog::Catalog> Export<E> {
.map(|t| {
let stream_type = match &t.kind {
Kind::Video(stream_type) => *stream_type,
Kind::Aac(_) => StreamType::AdtsAac,
Kind::Aac { .. } => StreamType::AdtsAac,
// Opus rides private-data PES; the registration + extension descriptors
// below tell the demuxer it's Opus.
Kind::Opus { .. } => StreamType::from_u8(0x06).map_err(anyhow::Error::msg)?,
Expand Down Expand Up @@ -1653,13 +1653,22 @@ impl<E: catalog::Catalog> Export<E> {
if self.span_counters.is_none() {
self.span_counters = Some(self.counters.clone());
}
let is_video = matches!(self.tracks.get(name).context("missing track")?.kind, Kind::Video(_));
// Refresh PSI at keyframes or after the interval lapses.
let psi = (is_video && frame.keyframe) || due(frame.timestamp, self.last_psi, PSI_INTERVAL);
if psi {
for track in self.tracks.values_mut() {
if let Kind::Aac { repeat, .. } = &mut track.kind {
*repeat = true;
}
}
}
let track = self.tracks.get_mut(name).context("missing track")?;
let pid = track.pid;
let kind = track.kind.clone();
if let Kind::Aac(aac) = &mut track.kind {
aac.program_config.take();
if let Kind::Aac { repeat, .. } = &mut track.kind {
*repeat = false;
}
let is_video = matches!(kind, Kind::Video(_));
let timestamp = frame.timestamp;
let keyframe = frame.keyframe;

Expand All @@ -1668,10 +1677,15 @@ impl<E: catalog::Catalog> Export<E> {
// verbatim streams carry no PES payload; the section is written separately below.
let es_payload = match &kind {
Kind::Video(stream_type) => Some(video_es_payload(*stream_type, track.source.description(), &frame)?),
Kind::Aac(aac) => {
let pce = aac.program_config.as_deref().unwrap_or_default();
Kind::Aac { config, repeat } => {
let pce = if *repeat {
config.program_config.as_deref().unwrap_or_default()
} else {
&[]
};
let raw_len = pce.len() + frame.payload.len();
let header = adts::write_header(aac.object_type, aac.sample_rate, aac.channel_config, raw_len)?;
let header =
adts::write_header(config.object_type, config.sample_rate, config.channel_config, raw_len)?;
let mut framed = Vec::with_capacity(header.len() + raw_len);
framed.extend_from_slice(&header);
framed.extend_from_slice(pce);
Expand Down Expand Up @@ -1705,8 +1719,7 @@ impl<E: catalog::Catalog> Export<E> {

let mut out = Vec::with_capacity(TsPacket::SIZE);

// Refresh PSI at keyframes or after the interval lapses.
if (is_video && frame.keyframe) || due(frame.timestamp, self.last_psi, PSI_INTERVAL) {
if psi {
let psi = self.psi.as_ref().context("PSI not built")?;
let pmt_pid = psi.pmt_pid;
let pat = TsPayload::Pat(psi.pat.clone());
Expand Down Expand Up @@ -2435,15 +2448,21 @@ fn audio_kind(config: &AudioConfig, name: &str) -> anyhow::Result<Kind> {
AudioCodec::AAC(codec) => {
// The description is exact, and names the LC core under explicit SBR or PS. Without
// one, the catalog is all there is.
Ok(Kind::Aac(match &config.description {
let in_band = match &config.description {
Some(asc) => aac::in_band(asc)?,
None => aac::InBand {
object_type: codec.profile,
sample_rate: config.sample_rate,
channel_config: adts::channel_config_from_count(config.channel_count)?,
program_config: None,
},
}))
};
// A rebuilt kind repeats the element too, so a catalog update between a PAT/PMT and
// this track's next frame cannot drop it.
Ok(Kind::Aac {
config: in_band,
repeat: true,
})
}
AudioCodec::Mp2 => Ok(Kind::Mp2 {
sample_rate: config.sample_rate,
Expand Down
116 changes: 115 additions & 1 deletion rs/moq-mux/src/container/ts/export_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,121 @@ async fn aac_program_config_roundtrip() {
assert_eq!(audio2.channel_count, 4);
assert_eq!(audio2.description, audio.description);
let roundtripped = read_frames(&consumer2, name2, Kind::Audio).await;
assert_eq!(roundtripped, ingested, "the element is written once, not per frame");
assert_eq!(roundtripped, ingested, "the element leaves the frames on import");
}

/// For each AAC PES, whether it follows a PAT and whether a program config element leads its
/// first raw data block.
fn aac_program_configs(ts: &[u8]) -> Vec<(bool, bool)> {
let mut reader = TsPacketReader::new(Cursor::new(ts));
let mut out = Vec::new();
let mut after_pat = false;
while let Some(packet) = reader.read_ts_packet().unwrap() {
match packet.payload {
Some(TsPayload::Pat(_)) => after_pat = true,
Some(TsPayload::PesStart(pes)) => {
let header = super::adts::Header::parse(&pes.data).unwrap();
assert_eq!(header.channel_config, 0);
// ID_PCE in the first three bits of the raw data block.
out.push((std::mem::take(&mut after_pat), pes.data[header.header_len] >> 5 == 5));
}
_ => {}
}
}
out
}

/// A receiver tunes in at a PAT/PMT, so the program config element follows each one rather
/// than riding the first frame only, including after a marker restarts the program clock. The
/// output cut at a later PAT imports as the same quad track.
#[tokio::test(start_paused = true)]
async fn aac_program_config_follows_each_table() {
let mut quad = vec![0x11, 0x80, 0x04, 0xC4, 0x04, 0x00, 0x21, 0x10, 0x0C];
quad.extend_from_slice(b"Lavc63.1.101");
let payload = Bytes::from_static(&[0x20; 10]);

let mut broadcast = moq_net::broadcast::Info::new().produce();
let consumer = broadcast.consume();
let mut catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap();
let track = broadcast
.create_track("a.aac", hang::container::track_info(hang::catalog::PRIORITY.audio))
.unwrap();
let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 4);
cfg.container = Container::Legacy;
cfg.description = Some(Bytes::from(quad.clone()));
catalog
.modify()
.unwrap()
.audio
.renditions
.insert("a.aac".to_string(), cfg);
let mut producer = Producer::new(track, HangContainer::Legacy(Kind::Audio));
let mut export = Export::new(crate::source::announced(&consumer))
.await
.unwrap()
.with_max_age(RECORDING_MAX_AGE);

let write = |producer: &mut Producer<HangContainer>, ms: std::ops::Range<u64>| {
for ms in ms.step_by(100) {
producer
.write(Frame {
timestamp: Timestamp::from_millis(ms).unwrap(),
duration: None,
payload: payload.clone(),
keyframe: true,
})
.unwrap();
producer.cut(None).unwrap();
}
};
write(&mut producer, 0..2_000);
producer.discontinuity().unwrap();
write(&mut producer, 2_000..3_000);
producer.finish().unwrap();
let frames = drain_frames(&mut export).await;
let ts: Vec<u8> = frames.iter().flat_map(|f| f.payload.iter().copied()).collect();
assert_packet_aligned(&ts);

let units = aac_program_configs(&ts);
assert_eq!(units.len(), 30);
for (i, (after_pat, pce)) in units.iter().enumerate() {
assert_eq!(
after_pat, pce,
"unit {i}: the element rides with the tables, and only there"
);
}
assert!(units[20].1, "the restarted clock re-sends the element");

// Join at the second PAT.
let pats: Vec<usize> = ts
.chunks(188)
.enumerate()
.filter(|(_, p)| p[1] & 0x1f == 0 && p[2] == 0)
.map(|(i, _)| i * 188)
.collect();
assert!(pats.len() > 2, "PAT on its cadence");
let mut joined = moq_net::broadcast::Info::new().produce();
let joined_consumer = joined.consume();
let joined_catalog = crate::catalog::Producer::new(&mut joined, crate::catalog::Config::default()).unwrap();
let mut import = crate::container::ts::Import::new(joined, joined_catalog.reserve());
import.decode(&BytesMut::from(&ts[pats[1]..])).unwrap();
import.finish().unwrap();

let snapshot = joined_catalog.snapshot();
let (name, audio) = snapshot
.audio
.renditions
.iter()
.next()
.expect("the late join lost the AAC track");
assert_eq!(audio.channel_count, 4);
assert_eq!(audio.description.as_deref(), Some(quad.as_slice()));
let imported = read_frames(&joined_consumer, name, Kind::Audio).await;
assert!(!imported.is_empty());
assert!(
imported.iter().all(|frame| frame[..] == payload[..]),
"the repeated element leaves every frame"
);
}

/// GStreamer 1.28 `fdkaacenc` output, 48 kHz stereo, remuxed to FLV by ffmpeg 9.0.1. Its
Expand Down
Loading
Loading