diff --git a/doc/bin/cli.md b/doc/bin/cli.md index 723b80a3e46..76b01f12a76 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -66,6 +66,11 @@ discarding the old mux buffer. The first new clock packet signals the break and stdout pacing re-anchors. Every rendition joins the new program generation; no track is fenced across the marker. +MPEG-TS export frames AAC as ADTS, which labels only the AAC Main, LC, SSR, +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. + 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 diff --git a/quest/m1/audio-codecs/README.md b/quest/m1/audio-codecs/README.md index 71b47defcc5..762769dc5ae 100644 --- a/quest/m1/audio-codecs/README.md +++ b/quest/m1/audio-codecs/README.md @@ -44,9 +44,11 @@ its own decode and encode quest so verification stays per host. ## Quests - [AudioToolbox decode](/quest/m1/audio-codecs/decode-audiotoolbox.md) - macOS and iOS decode HE-AAC, multichannel AAC, and what else the framework offers -- [ADTS refusals](/quest/m1/audio-codecs/adts-refusals.md) - the ADTS writer refuses channel counts and object types it cannot label instead of mislabeling them +- [HE-AAC catalog output](/quest/m1/audio-codecs/he-aac-catalog-output.md) - HE-AAC catalog entries name the output rate and layout, not the LC core - [AudioToolbox encode](/quest/m1/audio-codecs/encode-audiotoolbox.md) - macOS and iOS encode AAC-LC - [TS surround Opus](/quest/m1/audio-codecs/ts-opus-surround.md) - a 3 to 8 channel Opus stream from MPEG-TS imports with a family 1 OpusHead that decodes +- [AAC encode refusals](/quest/m1/audio-codecs/aac-encode-refusals.md) - `Config::encode` refuses channel counts it cannot name instead of writing stereo +- [AAC parse truncated SBR](/quest/m1/audio-codecs/aac-parse-truncated-sbr.md) - `Config::parse` refuses SBR and PS configs cut off before their core ## Related diff --git a/quest/m1/audio-codecs/aac-encode-refusals.md b/quest/m1/audio-codecs/aac-encode-refusals.md new file mode 100644 index 00000000000..9a891bb69f1 --- /dev/null +++ b/quest/m1/audio-codecs/aac-encode-refusals.md @@ -0,0 +1,24 @@ +# [S] AAC config encode refuses unnameable channel counts + +## Goal + +`moq_mux::codec::aac::Config::encode` never writes an AudioSpecificConfig that +names the wrong layout. A channel count no channelConfiguration names is +refused, not logged and written as stereo, the way the ADTS writer refuses it. + +## Plan + +- `encode` returns `Bytes` today, so refusing is a published API break: this + retargets to `dev`. Decide whether it returns a `Result` or whether a + constructor validates the count up front so encoding cannot fail. +- A count a program config element could describe (7, or more than 8) is + still refused unless the caller supplies a layout; guessing speaker + positions is what this removes. +- Callers in `moq-audio` (encoder config, description synthesis) already + validate first; check they keep their error messages. + +Public API: `Config::encode` (or its constructor) changes. Wire: none. + +## Related + +- [#4283](https://github.com/moq-dev/moq/pull/4283) - the ADTS export refusals this mirrors diff --git a/quest/m1/audio-codecs/aac-parse-truncated-sbr.md b/quest/m1/audio-codecs/aac-parse-truncated-sbr.md new file mode 100644 index 00000000000..57f9bf04e69 --- /dev/null +++ b/quest/m1/audio-codecs/aac-parse-truncated-sbr.md @@ -0,0 +1,21 @@ +# [XS] AAC config parse refuses truncated SBR and PS + +## Goal + +`Config::parse` refuses an AudioSpecificConfig that signals SBR or PS +(object type 5 or 29) but ends before its extension rate and core object type, +instead of accepting it as if the core were known. + +## Plan + +- The export path already requires these fields; `parse` stays lenient only + because `Config::encode` writes a two-byte config for those object types. + Once encode refuses or completes them, the leniency has no producer. +- Check the `moq-audio` tests that feed a two-byte HE-AAC config; they should + keep asserting a refusal, just a different one. + +Public API: none beyond a new error case. Wire: none. + +## Required + +- [AAC encode refusals](/quest/m1/audio-codecs/aac-encode-refusals.md) - encode stops producing truncated SBR and PS configs diff --git a/quest/m1/audio-codecs/adts-refusals.md b/quest/m1/audio-codecs/adts-refusals.md deleted file mode 100644 index c7896e377d7..00000000000 --- a/quest/m1/audio-codecs/adts-refusals.md +++ /dev/null @@ -1,25 +0,0 @@ -# [S] ADTS export refuses what it cannot label - -## Goal - -`moq-mux`'s ADTS writer never labels an AAC track with the wrong layout or -object type. A channel count ADTS cannot name is refused, not silently written -as stereo, and an explicit SBR or PS description is written with the object -type ADTS can carry or refused, not masked to two bits into a wrong profile. - -## Plan - -- `channel_config_from_count` falls back to stereo for counts it cannot - represent; refuse instead, the same way #4178 made channelConfiguration 11 to - 14 refuse. -- The ADTS header masks the object type to two bits, so an explicit HE-AAC - (object type 5) or HE-AACv2 (29) description is mislabeled. Decide per case: - signal the backward-compatible AAC-LC core (implicit SBR) when the description - allows it, otherwise refuse. -- Tests for both with real fixtures, checked against ffprobe. - -Public API: none. Wire: none; TS output changes only for inputs it mislabeled. - -## Related - -- [#4178](https://github.com/moq-dev/moq/pull/4178) - the PCE export that found these diff --git a/quest/m1/audio-codecs/he-aac-catalog-output.md b/quest/m1/audio-codecs/he-aac-catalog-output.md new file mode 100644 index 00000000000..9032995d6e9 --- /dev/null +++ b/quest/m1/audio-codecs/he-aac-catalog-output.md @@ -0,0 +1,29 @@ +# [S] HE-AAC catalog entries describe the output + +## Goal + +A catalog entry for HE-AAC or HE-AACv2 names what the stream plays as, for +example 48 kHz stereo, not its AAC-LC core (24 kHz, and mono for v2). Players +and gateways that size buffers or pick a decoder from the catalog see the real +output. + +## Plan + +- `Config::parse` reports the core rate and channel count because those lead + an explicit SBR or PS AudioSpecificConfig. The FLV and MKV importers copy + them into the catalog; fMP4 takes the sample entry instead, so the importers + disagree for the same stream. +- Derive the output from the config: the extension rate under SBR, two + channels under PS. Implicit SBR, found only in band, cannot be known from the + config; keep the documented half-rate behavior for it. +- Consumers that rebuild a config from catalog fields (description synthesis in + `moq-audio`, the MSF path) must not then mistake the output rate for the core. +- Regression: the `fdkaacenc` HE-AAC and HE-AACv2 FLV fixtures under + `rs/moq-mux/src/container/ts/test_data` import as 48 kHz stereo, matching + ffprobe. + +Public API: possibly `Config` fields. Wire: catalog values change for HE-AAC. + +## Related + +- [#4283](https://github.com/moq-dev/moq/pull/4283) - found while labeling HE-AAC for ADTS diff --git a/rs/moq-mux/src/codec/aac/mod.rs b/rs/moq-mux/src/codec/aac/mod.rs index 5ccb24a1103..ad46c1773b9 100644 --- a/rs/moq-mux/src/codec/aac/mod.rs +++ b/rs/moq-mux/src/codec/aac/mod.rs @@ -73,7 +73,11 @@ impl Config { let mut reader = BitReader::new(buf); let (object_type, sample_rate, channel_config) = read_header(&mut reader)?; let channel_count = match channel_config { - 0 => program_config(&mut reader)?, + 0 => { + let core = read_core(&mut reader, object_type)?; + read_general_audio(&mut reader, core)?; + program_config(&mut reader)? + } _ => channel_count_from_config(channel_config)?, }; @@ -135,9 +139,8 @@ impl Config { } } -/// Read an AudioSpecificConfig up to its channels: the audioObjectType, sample rate, and -/// channelConfiguration. For a channelConfiguration of 0 the reader stops at the program config -/// element. +/// Read an AudioSpecificConfig's leading fields: the audioObjectType, sample rate, and +/// channelConfiguration. fn read_header(reader: &mut BitReader) -> Result<(u8, u32, u8)> { if reader.buf.remaining() < 2 { return Err(Error::ConfigTooShort); @@ -156,35 +159,40 @@ fn read_header(reader: &mut BitReader) -> Result<(u8, u32, u8)> { // channelConfiguration: 4 bits, immediately after the (possibly explicit) rate. let channel_config = reader.read(4, Error::IncompleteConfig)? as u8; - if channel_config != 0 { - return Ok((object_type, sample_rate, channel_config)); - } + Ok((object_type, sample_rate, channel_config)) +} - // Explicit SBR and PS name their core object type after an extension rate; the - // GASpecificConfig carrying the program config element follows that core type. - let mut core = object_type; - if matches!(object_type, 5 | 29) { - if reader.read(4, Error::IncompleteConfig)? == 15 { - reader.read(24, Error::IncompleteConfig)?; - } - core = read_object_type(reader)?; - if core == 22 { - // extensionChannelConfiguration, only for ER BSAC. - reader.read(4, Error::IncompleteConfig)?; - } +/// Read the core audioObjectType, which explicit SBR and PS name after an extension rate. Any +/// other object type is its own core, and the leading sample rate is always the core's. +fn read_core(reader: &mut BitReader, object_type: u8) -> Result { + if !matches!(object_type, 5 | 29) { + return Ok(object_type); } + if reader.read(4, Error::IncompleteConfig)? == 15 { + reader.read(24, Error::IncompleteConfig)?; + } + let core = read_object_type(reader)?; + if core == 22 { + // extensionChannelConfiguration, only for ER BSAC. + reader.read(4, Error::IncompleteConfig)?; + } + Ok(core) +} + +/// Read a GASpecificConfig up to the program config element that a channelConfiguration of 0 +/// puts next, refusing a `core` object type whose specific config is something else. +fn read_general_audio(reader: &mut BitReader, core: u8) -> Result<()> { if !GENERAL_AUDIO.contains(&core) { return Err(Error::ProgramConfigUnsupported(core)); } - // GASpecificConfig: frameLengthFlag, dependsOnCoreCoder (then a 14-bit - // coreCoderDelay), and extensionFlag precede the element. + // frameLengthFlag, dependsOnCoreCoder (then a 14-bit coreCoderDelay), and extensionFlag. reader.read(1, Error::IncompleteConfig)?; if reader.read(1, Error::IncompleteConfig)? == 1 { reader.read(14, Error::IncompleteConfig)?; } reader.read(1, Error::IncompleteConfig)?; - Ok((object_type, sample_rate, 0)) + Ok(()) } /// Build the AudioSpecificConfig for a stream that signals its fields per frame, as ADTS does. @@ -222,25 +230,46 @@ pub(crate) fn in_band_config(profile: u8, sample_rate: u32, channel_config: u8, Ok(Bytes::from(out.bytes)) } -/// Split an AudioSpecificConfig into the channel signaling of a stream that carries it per frame, -/// as ADTS does; the inverse of [`in_band_config`]. -/// -/// Returns the channelConfiguration and, when that is 0, the program config element framed to -/// lead a raw data block: its element ID, then the element aligned to the block. -pub(crate) fn in_band_channels(asc: &[u8]) -> Result<(u8, Option)> { +/// The fields a stream that signals its config per frame, as ADTS does, carries in each header. +#[derive(Clone, Debug, PartialEq)] +pub(crate) struct InBand { + /// The core audioObjectType. Explicit SBR and PS name it under their own, and a decoder finds + /// them again in band, as it would in a stream that never signaled them (implicit signaling). + pub object_type: u8, + /// The core's sample rate, which SBR doubles on output. + pub sample_rate: u32, + pub channel_config: u8, + /// When `channel_config` is 0, the program config element framed to lead a raw data block: + /// its element ID, then the element aligned to the block. + pub program_config: Option, +} + +/// Split an AudioSpecificConfig into what a stream that carries it per frame signals, as ADTS +/// does; the inverse of [`in_band_config`]. +pub(crate) fn in_band(asc: &[u8]) -> Result { let mut asc = asc; let mut reader = BitReader::new(&mut asc); - let (_, _, channel_config) = read_header(&mut reader)?; - if channel_config != 0 { - return Ok((channel_config, None)); - } + let (object_type, sample_rate, channel_config) = read_header(&mut reader)?; + let core = read_core(&mut reader, object_type)?; + + let program_config = match channel_config { + 0 => { + read_general_audio(&mut reader, core)?; + let mut out = BitWriter::default(); + out.write(3, ID_PCE); + reader.record = Some(out); + program_config(&mut reader)?; + Some(Bytes::from(reader.record.take().expect("recording set above").bytes)) + } + _ => None, + }; - let mut out = BitWriter::default(); - out.write(3, ID_PCE); - reader.record = Some(out); - program_config(&mut reader)?; - let out = reader.record.take().expect("recording set above"); - Ok((0, Some(Bytes::from(out.bytes)))) + Ok(InBand { + object_type: core, + sample_rate, + channel_config, + program_config, + }) } /// The raw data block element ID of a program config element (ISO 14496-3 Table 4.85). @@ -554,10 +583,9 @@ mod tests { assert_eq!(Config::parse(&mut asc.as_slice()).unwrap().channel_count, 7); } - #[test] - fn parses_program_config_element_behind_explicit_sbr() { - // audioObjectType 5 (SBR), 24 kHz core, channelConfiguration 0, a 48 kHz extension - // rate, then the core type (LC) whose GASpecificConfig carries the element. + /// audioObjectType 5 (SBR), 24 kHz core, channelConfiguration 0, a 48 kHz extension rate, + /// then the core type (LC) whose GASpecificConfig carries a front and back pair. + fn sbr_pce_asc() -> Vec { let mut out = BitWriter::default(); out.write(5, 5); out.write(4, 6); @@ -566,7 +594,12 @@ mod tests { out.write(5, 2); out.write(3, 0); write_pce(&mut out, &[true], &[], &[true], 0); - let cfg = Config::parse(&mut out.bytes.as_slice()).unwrap(); + out.bytes + } + + #[test] + fn parses_program_config_element_behind_explicit_sbr() { + let cfg = Config::parse(&mut sbr_pce_asc().as_slice()).unwrap(); assert_eq!(cfg.profile, 5); assert_eq!(cfg.channel_count, 4); } @@ -638,16 +671,18 @@ mod tests { assert_eq!(rest, [0xFF], "the element moves out of the block"); // And back: the element leads the block again, byte for byte. - assert_eq!(in_band_channels(&asc).unwrap(), (0, Some(Bytes::from(pce)))); + let in_band = in_band(&asc).unwrap(); + assert_eq!(in_band.channel_config, 0); + assert_eq!(in_band.program_config, Some(Bytes::from(pce))); } #[test] - fn in_band_channels_of_ffmpeg_program_config_element() { + fn in_band_of_ffmpeg_program_config_element() { // Round trip ffmpeg's own element through a raw data block and back into a config. The // trailing SBR sync extension is not part of the element, so it drops. - let (config, pce) = in_band_channels(&FFMPEG_QUAD_ASC).unwrap(); - assert_eq!(config, 0); - let pce = pce.unwrap(); + let in_band = in_band(&FFMPEG_QUAD_ASC).unwrap(); + assert_eq!(in_band.channel_config, 0); + let pce = in_band.program_config.unwrap(); let mut block = pce.as_ref(); let asc = in_band_config(2, 48_000, 0, &mut block).unwrap(); assert!(block.is_empty(), "the element is all that was framed"); @@ -655,9 +690,40 @@ mod tests { } #[test] - fn in_band_channels_without_a_program_config_element() { - assert_eq!(in_band_channels(&[0x11, 0x90]).unwrap(), (2, None)); - assert_eq!(in_band_channels(&[0x11, 0xE0]).unwrap(), (12, None)); + fn in_band_without_a_program_config_element() { + let stereo = in_band(&[0x11, 0x90]).unwrap(); + assert_eq!((stereo.channel_config, stereo.program_config), (2, None)); + assert_eq!(in_band(&[0x11, 0xE0]).unwrap().channel_config, 12); + } + + #[test] + fn in_band_of_explicit_sbr_is_its_lc_core() { + // GStreamer 1.28 `fdkaacenc` at 48 kHz stereo: HE-AAC is SBR over a 24 kHz stereo LC core, + // and HE-AACv2 is PS over a mono one. + for (asc, channel_config) in [([0x2B, 0x11, 0x88, 0x00], 2), ([0xEB, 0x09, 0x88, 0x00], 1)] { + let expected = InBand { + object_type: 2, + sample_rate: 24_000, + channel_config, + program_config: None, + }; + assert_eq!(in_band(&asc).unwrap(), expected); + } + + // A config naming SBR but stopping before its core has no core to name. + assert!(matches!(in_band(&[0x2A, 0x10]), Err(Error::IncompleteConfig))); + } + + #[test] + fn in_band_of_explicit_sbr_keeps_its_program_config_element() { + let in_band = in_band(&sbr_pce_asc()).unwrap(); + assert_eq!(in_band.object_type, 2); + assert_eq!(in_band.sample_rate, 24_000); + assert_eq!(in_band.channel_config, 0); + let pce = in_band.program_config.expect("a program config element"); + let mut block = pce.as_ref(); + let asc = in_band_config(2, 24_000, 0, &mut block).unwrap(); + assert_eq!(Config::parse(&mut asc.as_ref()).unwrap().channel_count, 4); } #[test] diff --git a/rs/moq-mux/src/container/ts/adts.rs b/rs/moq-mux/src/container/ts/adts.rs index ac1aec68232..632947bdce4 100644 --- a/rs/moq-mux/src/container/ts/adts.rs +++ b/rs/moq-mux/src/container/ts/adts.rs @@ -66,8 +66,12 @@ pub(super) fn write_header( channel_config: u8, raw_len: usize, ) -> anyhow::Result<[u8; 7]> { - // ADTS `profile` is the 2-bit audioObjectType - 1. - let profile = object_type.saturating_sub(1) & 0x03; + // ADTS `profile` is the 2-bit audioObjectType - 1, so only Main, LC, SSR, and LTP fit. + anyhow::ensure!( + (1..=4).contains(&object_type), + "audioObjectType {object_type} not representable in ADTS" + ); + let profile = object_type - 1; let freq_index = freq_index_from_rate(sample_rate)?; // ADTS has 3 bits for it; the higher configurations only fit an AudioSpecificConfig. anyhow::ensure!( @@ -101,12 +105,13 @@ fn freq_index_from_rate(sample_rate: u32) -> anyhow::Result { .with_context(|| format!("sample rate {sample_rate} not representable in ADTS")) } -/// Map a channel count to an AAC `channel_config` (ISO 14496-3 Table 1.19). -pub(super) fn channel_config_from_count(channel_count: u32) -> u8 { +/// Map a channel count to the ADTS `channel_config` (ISO 14496-3 Table 1.19) naming it, refusing a +/// count that none does rather than guess a layout. +pub(super) fn channel_config_from_count(channel_count: u32) -> anyhow::Result { match channel_count { - 1..=6 => channel_count as u8, - 8 => 7, - _ => 2, + 1..=6 => Ok(channel_count as u8), + 8 => Ok(7), + _ => anyhow::bail!("{channel_count} channels have no ADTS channelConfiguration"), } } @@ -134,6 +139,25 @@ mod tests { assert!(Header::parse(&header).is_err()); } + #[test] + fn write_refuses_object_types_outside_the_profile_field() { + for object_type in [0, 5, 29] { + assert!(write_header(object_type, 48_000, 2, 10).is_err()); + } + for object_type in 1..=4 { + let header = write_header(object_type, 48_000, 2, 10).unwrap(); + assert_eq!(Header::parse(&header).unwrap().object_type, object_type); + } + } + + #[test] + fn channel_counts_without_a_configuration_are_refused() { + assert_eq!(channel_config_from_count(8).unwrap(), 7); + for count in [0, 7, 9, 24] { + assert!(channel_config_from_count(count).is_err()); + } + } + #[test] fn frame_len_for_5_1() { let header = write_header(2, 44_100, 6, 512).unwrap(); diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index c53a7c141f5..07ac0bc49fb 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -37,7 +37,7 @@ use moq_net::Timestamp; use crate::catalog::hang::Catalog; use crate::catalog::{CatalogFormat, Stream}; -use crate::codec::annexb; +use crate::codec::{aac, annexb}; use crate::container::{ExportSource, Frame}; use super::adts; @@ -257,12 +257,7 @@ enum Kind { /// 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 { - object_type: u8, - sample_rate: u32, - channel_config: u8, - program_config: Option, - }, + Aac(aac::InBand), /// 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. @@ -1185,7 +1180,7 @@ impl Export { 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 ) }) }) @@ -1197,7 +1192,7 @@ impl Export { .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)?, @@ -1348,8 +1343,8 @@ impl Export { let track = self.tracks.get_mut(name).context("missing track")?; let pid = track.pid; let kind = track.kind.clone(); - if let Kind::Aac { program_config, .. } = &mut track.kind { - program_config.take(); + if let Kind::Aac(aac) = &mut track.kind { + aac.program_config.take(); } let is_video = matches!(kind, Kind::Video(_)); let timestamp = frame.timestamp; @@ -1360,15 +1355,10 @@ impl Export { // 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 { - object_type, - sample_rate, - channel_config, - program_config, - } => { - let pce = program_config.as_deref().unwrap_or_default(); + Kind::Aac(aac) => { + let pce = aac.program_config.as_deref().unwrap_or_default(); let raw_len = pce.len() + frame.payload.len(); - let header = adts::write_header(*object_type, *sample_rate, *channel_config, raw_len)?; + let header = adts::write_header(aac.object_type, aac.sample_rate, aac.channel_config, raw_len)?; let mut framed = Vec::with_capacity(header.len() + raw_len); framed.extend_from_slice(&header); framed.extend_from_slice(pce); @@ -2097,18 +2087,18 @@ fn video_es_payload(stream_type: StreamType, description: Option<&Bytes>, frame: fn audio_kind(config: &AudioConfig, name: &str) -> anyhow::Result { ensure_raw(&config.container, "audio", name)?; match &config.codec { - AudioCodec::AAC(aac) => { - // The description names the layout exactly; without one, the count is all there is. - let (channel_config, program_config) = match &config.description { - Some(asc) => crate::codec::aac::in_band_channels(asc)?, - None => (adts::channel_config_from_count(config.channel_count), None), - }; - Ok(Kind::Aac { - object_type: aac.profile, - sample_rate: config.sample_rate, - channel_config, - program_config, - }) + 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 { + 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, + }, + })) } AudioCodec::Mp2 => Ok(Kind::Mp2 { sample_rate: config.sample_rate, diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index 00d9294308c..f8847cffc2c 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -1484,6 +1484,91 @@ async fn aac_program_config_roundtrip() { assert_eq!(roundtripped, ingested, "the element is written once, not per frame"); } +/// GStreamer 1.28 `fdkaacenc` output, 48 kHz stereo, remuxed to FLV by ffmpeg 9.0.1. Its +/// AudioSpecificConfig signals SBR (and PS for v2) explicitly: object type 5 or 29 over an LC core +/// at 24 kHz, stereo for v1 and mono for v2. ADTS has two bits for the object type, so export +/// labels the LC core at its own rate and layout and leaves SBR and PS to implicit signaling, the +/// header ffmpeg's ADTS muxer writes too. ffprobe reads the result back as HE-AAC or HE-AACv2 at +/// 48 kHz stereo, like the source. +#[tokio::test(start_paused = true)] +async fn aac_explicit_sbr_exports_its_lc_core() { + let fixtures: [(&[u8], u8); 2] = [ + (include_bytes!("test_data/he_aac.flv"), 2), + (include_bytes!("test_data/he_aac_v2.flv"), 1), + ]; + for (data, channel_config) in fixtures { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let consumer = broadcast.consume(); + let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); + let mut import = crate::container::flv::Import::new(broadcast, catalog.reserve()); + import.decode(data).unwrap(); + import.finish().unwrap(); + + let snapshot = catalog.snapshot(); + let (name, _) = snapshot.audio.renditions.iter().next().expect("an AAC track"); + let ingested = read_frames(&consumer, name, Kind::Audio).await; + assert!(!ingested.is_empty(), "no AAC frames"); + + let ts = drain(consumer).await; + assert_packet_aligned(&ts); + + let (header, block) = first_adts_frame(&ts); + assert_eq!(header.object_type, 2, "the LC core, not a masked SBR or PS"); + assert_eq!(header.sample_rate, 24_000, "the core rate, not the output rate"); + assert_eq!(header.channel_config, channel_config); + assert_eq!(block, ingested[0], "the raw data block is untouched"); + } +} + +/// Without a description, the catalog is all ADTS has to label a track with. A channel count no +/// channelConfiguration names is refused rather than written as stereo, and HE-AAC, whose core +/// rate and layout only a description names, is refused rather than masked to AAC Main. +#[tokio::test(start_paused = true)] +async fn aac_export_refuses_what_adts_cannot_label() { + for (profile, channel_count, refusal) in [(2, 7, "7 channels"), (5, 2, "audioObjectType 5")] { + 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( + broadcast.unique_name(".aac"), + hang::container::track_info(hang::catalog::PRIORITY.audio), + ) + .unwrap(); + let mut cfg = AudioConfig::new(AAC { profile }, 48_000, channel_count); + cfg.container = Container::Legacy; + catalog + .modify() + .unwrap() + .audio + .renditions + .insert(track.name().to_string(), cfg); + + let mut producer = Producer::new(track, HangContainer::Legacy(crate::container::Kind::Data)); + producer + .write(Frame { + timestamp: Timestamp::from_millis(0).unwrap(), + duration: None, + payload: Bytes::from_static(&[0x01, 0x02]), + keyframe: true, + }) + .unwrap(); + producer.finish().unwrap(); + + let mut exporter = Export::new(crate::source::announced(&consumer)).await.unwrap(); + let err = loop { + match tokio::time::timeout(Duration::from_secs(1), exporter.next()).await { + Ok(Ok(Some(_))) => continue, + Ok(Ok(None)) => panic!("export completed; expected a refusal naming {refusal}"), + Ok(Err(e)) => break e, + Err(_) => panic!("export neither errored nor completed"), + } + }; + assert!(err.to_string().contains(refusal), "expected {refusal}, got: {err}"); + } +} + /// The ffmpeg E-AC-3 fixture must survive TS -> MoQ -> TS byte-for-byte in an /// audio-only program; the PMT re-announces ATSC 0x87 with the 'EAC3' /// registration descriptor. diff --git a/rs/moq-mux/src/container/ts/test_data/he_aac.flv b/rs/moq-mux/src/container/ts/test_data/he_aac.flv new file mode 100644 index 00000000000..5bf778ce3c0 Binary files /dev/null and b/rs/moq-mux/src/container/ts/test_data/he_aac.flv differ diff --git a/rs/moq-mux/src/container/ts/test_data/he_aac_v2.flv b/rs/moq-mux/src/container/ts/test_data/he_aac_v2.flv new file mode 100644 index 00000000000..1df313b4099 Binary files /dev/null and b/rs/moq-mux/src/container/ts/test_data/he_aac_v2.flv differ