From f241fc3820ab1d4872df12b41801b577e66d68e9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 23:33:57 -0700 Subject: [PATCH 1/7] chore(quest): claim quest/m1/cut-audit/aac-description-less.md Quest-Claim: 71a5401c7b6d1f4c From 40b83099a9317cdce0a2b50a2d460f05ffeb735e Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Tue, 27 Oct 2026 23:58:09 -0700 Subject: [PATCH 2/7] fix(aac): honor catalog-selected ADTS framing Co-Authored-By: Codex --- drafts/draft-lcurley-moq-hang.md | 4 + js/hang/src/util/libav.test.ts | 82 ++++++------- js/watch/src/audio/decoder.test.ts | 32 ++++- quest/m1/cut-audit/aac-description-less.md | 45 +++---- rs/moq-audio/src/aac.rs | 6 +- rs/moq-audio/src/decode/backend/symphonia.rs | 24 +++- rs/moq-audio/src/decode/decoder.rs | 63 +++++++++- rs/moq-gst/src/source/imp.rs | 18 ++- .../src/{container/ts => codec/aac}/adts.rs | 112 +++++++++++++----- rs/moq-mux/src/codec/aac/import.rs | 28 +++++ rs/moq-mux/src/codec/aac/mod.rs | 7 ++ rs/moq-mux/src/container/ts/export.rs | 60 +++++++--- rs/moq-mux/src/container/ts/export_test.rs | 33 +++++- .../src/container/ts/export_timing_test.rs | 1 + rs/moq-mux/src/container/ts/import.rs | 6 +- rs/moq-mux/src/container/ts/mod.rs | 2 +- rs/moq-mux/src/container/ts/revision_test.rs | 1 + 17 files changed, 398 insertions(+), 126 deletions(-) rename rs/moq-mux/src/{container/ts => codec/aac}/adts.rs (64%) diff --git a/drafts/draft-lcurley-moq-hang.md b/drafts/draft-lcurley-moq-hang.md index d8992f76fab..ebe6fb6b0be 100644 --- a/drafts/draft-lcurley-moq-hang.md +++ b/drafts/draft-lcurley-moq-hang.md @@ -923,6 +923,7 @@ Audio publishers do not append per-group duration markers because the codec defi Data tracks retain empty payloads as data, without endpoint semantics. For example, h.264 with no `description` field would be annex.b encoded, while h.264 with a `description` field would be AVCC encoded. +For AAC (`mp4a.40.*`), each nonempty codec payload MUST be one complete ADTS frame when `description` is absent; with an AudioSpecificConfig in `description`, it MUST be one raw AAC access unit without an ADTS header. For a text track, the remainder is the cue in the track's declared `format` (for example a `WEBVTT` segment). ## cmaf @@ -940,6 +941,7 @@ A consumer MUST feed `init` to the decoder before the first frame. ## loc Each frame is a Low Overhead Container frame {{!I-D.ietf-moq-loc}}: a property block, carrying the timestamp among other properties, followed by the codec payload. +The codec payload uses the same framing as {{legacy}}. For audio and video, consumers accept empty codec payload metadata with the same video-duration and audio-terminal-trimming meanings as the legacy container. Data tracks retain empty payloads as data. @@ -1144,6 +1146,8 @@ This document has no IANA actions. ## moq-hang-04 {:numbered="false"} +- Defined AAC payloads without a `description` as ADTS, and payloads with an AudioSpecificConfig as raw AAC. + - Replaced the root `archive` section and its per-track timelines with an optional, per-rendition `hls` field on video and audio ({{hls}}). Each record is a segment's first `group` and `pts` in milliseconds, with an optional `duration` where zero marks a stop; frame positions, `end`, `keyframe`, `timescale`, `replay`, `replayEpoch`, `store`, and `version` are gone. Each rendition numbers its own segments, and an edge maps a rendition's record `n` to its segment `n`. The record track is named `.hls` and is not compressed. - A reader refuses a JSON frame in which an object repeats a member name. - Added the root `version` ({{catalog-version}}): the oldest draft that reads the catalog, which a consumer refuses when above its own. A `track` other than the entry's ID or a video `warmup` needs 4. Consumers MUST ignore unknown fields. diff --git a/js/hang/src/util/libav.test.ts b/js/hang/src/util/libav.test.ts index ac6cc86b3fc..8b1706ed343 100644 --- a/js/hang/src/util/libav.test.ts +++ b/js/hang/src/util/libav.test.ts @@ -10,13 +10,13 @@ const SAMPLE_RATE = 48000; // -filter_complex amerge -c:a aac -b:a 64k -f adts libav.test.aac const FIXTURE = new URL("./libav.test.aac", import.meta.url); -// Strip the ADTS headers: a hang catalog carries the AudioSpecificConfig and frames are raw. -function rawFrames(adts: Uint8Array): Uint8Array[] { +// A description selects raw blocks; without one, each packet keeps its ADTS header. +function aacFrames(adts: Uint8Array, raw: boolean): Uint8Array[] { const frames: Uint8Array[] = []; for (let i = 0; i < adts.length; ) { const length = ((adts[i + 3] & 0x03) << 11) | (adts[i + 4] << 3) | (adts[i + 5] >> 5); const header = adts[i + 1] & 0x01 ? 7 : 9; - frames.push(adts.subarray(i + header, i + length)); + frames.push(adts.subarray(i + (raw ? header : 0), i + length)); i += length; } return frames; @@ -51,45 +51,47 @@ test("the fallback decodes AAC without a native AudioDecoder", async () => { expect((await AudioDecoder.isConfigSupported({ ...config, codec })).supported).toBe(true); } - const planes: Float32Array[][] = [[], []]; - let failure: Error | undefined; - const decoder = new AudioDecoder({ - output: (data) => { - for (const [index, plane] of planes.entries()) { - const samples = new Float32Array(data.numberOfFrames); - data.copyTo(samples, { planeIndex: index, format: "f32-planar" }); - plane.push(samples); - } - data.close(); - }, - error: (error) => { - failure = error; - }, - }); - decoder.configure(config); - - const frames = rawFrames(new Uint8Array(await Bun.file(FIXTURE).arrayBuffer())); - for (const [index, data] of frames.entries()) { - decoder.decode( - new EncodedAudioChunk({ type: "key", timestamp: (index * 1024 * 1_000_000) / SAMPLE_RATE, data }), - ); - } - await decoder.flush(); - decoder.close(); - expect(failure).toBeUndefined(); + for (const raw of [true, false]) { + const planes: Float32Array[][] = [[], []]; + let failure: Error | undefined; + const decoder = new AudioDecoder({ + output: (data) => { + for (const [index, plane] of planes.entries()) { + const samples = new Float32Array(data.numberOfFrames); + data.copyTo(samples, { planeIndex: index, format: "f32-planar" }); + plane.push(samples); + } + data.close(); + }, + error: (error) => { + failure = error; + }, + }); + decoder.configure({ ...config, description: raw ? config.description : undefined }); - const [left, right] = planes.map((chunks) => { - const out = new Float32Array(chunks.reduce((sum, chunk) => sum + chunk.length, 0)); - let offset = 0; - for (const chunk of chunks) { - out.set(chunk, offset); - offset += chunk.length; + const frames = aacFrames(new Uint8Array(await Bun.file(FIXTURE).arrayBuffer()), raw); + for (const [index, data] of frames.entries()) { + decoder.decode( + new EncodedAudioChunk({ type: "key", timestamp: (index * 1024 * 1_000_000) / SAMPLE_RATE, data }), + ); } - return out; - }); - expect(left.length).toBe(frames.length * 1024); - expect(power(left, 440)).toBeGreaterThan(100 * power(left, 880)); - expect(power(right, 880)).toBeGreaterThan(100 * power(right, 440)); + await decoder.flush(); + decoder.close(); + expect(failure).toBeUndefined(); + + const [left, right] = planes.map((chunks) => { + const out = new Float32Array(chunks.reduce((sum, chunk) => sum + chunk.length, 0)); + let offset = 0; + for (const chunk of chunks) { + out.set(chunk, offset); + offset += chunk.length; + } + return out; + }); + expect(left.length).toBe(frames.length * 1024); + expect(power(left, 440)).toBeGreaterThan(100 * power(left, 880)); + expect(power(right, 880)).toBeGreaterThan(100 * power(right, 440)); + } }); // Runs on the polyfill's AudioData, which libopus decodes into. After the AAC test, which needs Bun's diff --git a/js/watch/src/audio/decoder.test.ts b/js/watch/src/audio/decoder.test.ts index f1450bbd4e3..2012df93fc9 100644 --- a/js/watch/src/audio/decoder.test.ts +++ b/js/watch/src/audio/decoder.test.ts @@ -157,6 +157,8 @@ let decodeThrows: Error | undefined; let deferAudioOutput = false; let pendingAudio: (() => void)[] = []; let audioInputs: number[] = []; +let audioConfigs: AudioDecoderConfig[] = []; +let audioChunks: EncodedAudioChunkInit[] = []; beforeEach(() => { frameTimestamp = 0; @@ -166,6 +168,8 @@ beforeEach(() => { deferAudioOutput = false; pendingAudio = []; audioInputs = []; + audioConfigs = []; + audioChunks = []; contexts = []; worklets = []; moduleLoaded = Promise.resolve(); @@ -180,7 +184,8 @@ beforeEach(() => { codecErrors.push(init.error); codecs++; } - configure() { + configure(config: AudioDecoderConfig) { + audioConfigs.push(config); this.needsKey = true; } reset() { @@ -204,6 +209,7 @@ beforeEach(() => { readonly timestamp: number; readonly type: EncodedAudioChunkType; constructor(init: EncodedAudioChunkInit) { + audioChunks.push(init); this.timestamp = init.timestamp; this.type = init.type; } @@ -392,6 +398,30 @@ async function play(initial: Delay, buffered = false, cmaf = false) { }; } +for (const description of [undefined, "1190"]) { + it(`passes AAC to WebCodecs as ${description ? "raw" : "ADTS"}`, async () => { + const playback = await play(Milli(100)); + try { + playback.restore({ codec: "mp4a.40.2", sampleRate: 48_000, numberOfChannels: 2, description }); + await microtasks(); + const config = audioConfigs.at(-1); + expect(config?.codec).toBe("mp4a.40.2"); + expect(config?.description).toEqual(description ? new Uint8Array([0x11, 0x90]) : undefined); + const packet = new Uint8Array( + description ? [0x01, 0x52, 0xf2] : [0xff, 0xf1, 0x4c, 0x80, 0x01, 0x5f, 0xfc, 0x01, 0x52, 0xf2], + ); + const read = frame(); + read.frame.payload = packet; + playback.push(read); + await microtasks(); + expect(audioChunks.at(-1)?.data).toEqual(packet); + } finally { + playback.close(); + await microtasks(); + } + }); +} + for (const cmaf of [false, true]) { it(`a ${cmaf ? "CMAF" : "legacy"} gap releases accounting for codec outputs it discarded`, async () => { const clock = new Manual(); diff --git a/quest/m1/cut-audit/aac-description-less.md b/quest/m1/cut-audit/aac-description-less.md index 8f0bc3161c6..57b31da00aa 100644 --- a/quest/m1/cut-audit/aac-description-less.md +++ b/quest/m1/cut-audit/aac-description-less.md @@ -8,23 +8,28 @@ moqsrc, moq-audio, TS export) decodes it the same way. ## Plan -Found by the 2026-10-24 pre-cut audit at 151a529670. Source-inferred and -spot-verified, inherited. - -Today `rs/moq-gst/src/source/imp.rs:1769` advertises `stream-format=adts`, -`js/watch/src/audio/decoder.ts:488` configures WebCodecs with no description -(which WebCodecs reads as ADTS), `rs/moq-audio/src/aac.rs:68` synthesizes an -AudioSpecificConfig and decodes the frames as raw, and -`rs/moq-mux/src/container/ts/export.rs:3823` treats them as raw and adds -ADTS headers. Whichever form a publisher sends, half the consumers break. - -Decided 2026-10-25 (maintainer): ADTS, matching WebCodecs, the browser -path most viewers use. moq-audio parses ADTS headers and TS export passes -the frames through. - -State the choice in `drafts/draft-lcurley-moq-hang.md` and add a test per -consumer with a description-less fixture. Check which first-party -publishers emit description-less AAC today, so none starts producing the -other form. - -Public API: none. Wire: a format clarification in the hang draft. +Decided 2026-10-25 (maintainer): ADTS, matching WebCodecs. For legacy and +LOC payloads, an absent `description` means one complete ADTS frame; an +AudioSpecificConfig means raw AAC. CMAF samples remain raw. + +The draft implements ADTS stripping in moq-audio and pass-through in TS +export, fixes GStreamer's raw-AAC caps spelling, and states the format in +the hang draft. Both native mismatches reproduced before the fixes: ADTS +produced different PCM from its raw block, and TS prepended a second header. +Tests cover both description modes in all four consumers, CRC framing, +truncated packets, publication metadata, and CMAF's raw samples. + +First-party AAC publishers retain an AudioSpecificConfig: the codec +importer, TS/FLV/fMP4/MKV importers, moq-audio's encoder, moqsink, and the JS +publisher. The raw codec importer now refuses to publish without one. + +Remaining maintainer choice: the shared parser moves from TS's private +module to the additive `moq_mux::codec::aac::adts::{Header, Error}` API, +used by both moq-audio and TS. Recommend this existing parser over a second +copy or a new internal crate. Approve that public shape, or choose a +sharing boundary, before treating the quest as complete. Finish the local +CI and all-language interop evidence before readiness. + +Public API: additive ADTS parser and a typed missing-description refusal +for the raw AAC importer. Wire: the hang format clarification; no MoQ +protocol framing changes. diff --git a/rs/moq-audio/src/aac.rs b/rs/moq-audio/src/aac.rs index de7ff717dbe..1dab4c9efa5 100644 --- a/rs/moq-audio/src/aac.rs +++ b/rs/moq-audio/src/aac.rs @@ -45,9 +45,9 @@ fn validate(config: &moq_mux::codec::aac::Config) -> Result<(), Error> { /// The AudioSpecificConfig to open a decoder with, validated as AAC-LC. /// /// Prefers the catalog `description` verbatim, since re-encoding the parsed -/// fields would drop any extension it carries. A catalog without one (an MSF -/// track, or a producer that only filled in the plain fields) gets one -/// synthesized from the declared profile, rate, and channel count. +/// fields would drop any extension it carries. Without one, the decoder's config is +/// synthesized from the declared profile, rate, and channels. Legacy and LOC packets +/// then carry ADTS headers that must agree with it; CMAF samples remain raw. /// /// The profile is read back out of the resulting config rather than taken from /// the catalog codec string: the description is what the decoder acts on, and diff --git a/rs/moq-audio/src/decode/backend/symphonia.rs b/rs/moq-audio/src/decode/backend/symphonia.rs index e2a87c6253d..7bcc8ec448e 100644 --- a/rs/moq-audio/src/decode/backend/symphonia.rs +++ b/rs/moq-audio/src/decode/backend/symphonia.rs @@ -13,6 +13,7 @@ pub(super) struct Symphonia { inner: symphonia_codec_aac::AacDecoder, sample_rate: u32, layout: Layout, + adts: bool, } impl Symphonia { @@ -58,15 +59,32 @@ impl Symphonia { inner, sample_rate, layout: Layout::from_channels(channel_count as u32)?, + adts: catalog.description.is_none() && !matches!(catalog.container, hang::catalog::Container::Cmaf { .. }), })) } } impl Backend for Symphonia { fn decode(&mut self, packet: &[u8]) -> Result { - // The packet is a raw AAC frame, not ADTS, so there is nothing to timestamp - // it with here: the container carries the timestamp and the decoder only - // reads the payload. + let packet = if self.adts { + let header = + moq_mux::codec::aac::adts::Header::parse(packet).map_err(|err| Error::Decode(format!("aac: {err}")))?; + if header.object_type != 2 + || header.sample_rate != self.sample_rate + || u32::from(header.channel_config) != self.layout.channels() + || header.blocks != 1 + { + return Err(Error::Decode( + "ADTS header disagrees with the AAC-LC catalog or carries multiple blocks".into(), + )); + } + header + .payload(packet) + .map_err(|err| Error::Decode(format!("aac: {err}")))? + } else { + packet + }; + // The container carries the timestamp; Symphonia reads only the raw AAC block. let packet = symphonia_core::packet::PacketRef::new( 0, symphonia_core::units::Timestamp::ZERO, diff --git a/rs/moq-audio/src/decode/decoder.rs b/rs/moq-audio/src/decode/decoder.rs index 5ed9969e4d5..462981480fa 100644 --- a/rs/moq-audio/src/decode/decoder.rs +++ b/rs/moq-audio/src/decode/decoder.rs @@ -52,7 +52,8 @@ impl Decoder { /// `description` declares, refusing a malformed head; without a description /// it takes the catalog's channel count and applies neither. PCM uses the /// catalog fields directly and requires an absent `description`. AAC reads - /// its AudioSpecificConfig, synthesizing one from the catalog when absent. + /// its AudioSpecificConfig; without one, legacy and LOC packets carry ADTS headers, + /// while demuxed CMAF samples remain raw. pub fn new(catalog: &hang::catalog::AudioConfig, config: &Config) -> Result { let backend = backend::open(catalog, config)?; let delay = backend.delay(); @@ -198,13 +199,69 @@ pub(crate) mod tests { #[cfg(feature = "aac")] #[test] - fn aac_synthesizes_a_missing_description() { - // An MSF catalog carries the shape in its own fields instead. + fn aac_without_a_description_refuses_raw_packets() { let mut catalog = aac_catalog(); catalog.description = None; let mut decoder = Decoder::new(&catalog, &Config::default()).unwrap(); assert_eq!(decoder.sample_rate(), 44_100); + assert!(matches!(decoder.decode(AAC_FRAMES[0]), Err(Error::Decode(_)))); + } + + #[cfg(feature = "aac")] + #[test] + fn aac_without_a_description_decodes_adts() { + let mut catalog = aac_catalog(); + catalog.description = None; + for crc in [false, true] { + let mut adts = Decoder::new(&catalog, &Config::default()).unwrap(); + let mut raw = Decoder::new(&aac_catalog(), &Config::default()).unwrap(); + for frame in AAC_FRAMES { + let len = frame.len() + if crc { 9 } else { 7 }; + let mut packet = vec![ + 0xff, + if crc { 0xf0 } else { 0xf1 }, + 0x50, + 0x40 | (len >> 11) as u8, + (len >> 3) as u8, + ((len & 7) << 5) as u8 | 0x1f, + 0xfc, + ]; + if crc { + packet.extend_from_slice(&[0, 0]); + } + packet.extend_from_slice(frame); + let got = adts.decode(&packet).unwrap().samples; + let expected = raw.decode(frame).unwrap().samples; + assert!(got == expected, "ADTS framing changed decoded PCM"); + for altered in [2, 3, 6] { + let mut invalid = packet.clone(); + invalid[altered] ^= if altered == 2 { + 4 + } else if altered == 3 { + 0xc0 + } else { + 1 + }; + assert!(matches!(adts.decode(&invalid), Err(Error::Decode(_)))); + } + assert!(matches!( + adts.decode(&packet[..packet.len() - 1]), + Err(Error::Decode(_)) + )); + } + } + } + + #[cfg(feature = "aac")] + #[test] + fn cmaf_aac_samples_remain_raw_without_a_catalog_description() { + let mut catalog = aac_catalog(); + catalog.description = None; + catalog.container = hang::catalog::Container::Cmaf { + init: bytes::Bytes::new(), + }; + let mut decoder = Decoder::new(&catalog, &Config::default()).unwrap(); assert_eq!(decoder.decode(AAC_FRAMES[0]).unwrap().samples.len(), 1024); } diff --git a/rs/moq-gst/src/source/imp.rs b/rs/moq-gst/src/source/imp.rs index 075ff496262..be85a94ec71 100644 --- a/rs/moq-gst/src/source/imp.rs +++ b/rs/moq-gst/src/source/imp.rs @@ -1764,7 +1764,7 @@ fn audio_caps(config: &hang::catalog::AudioConfig) -> Result { if let Some(description) = &config.description { builder = builder .field("codec_data", gst::Buffer::from_slice(description.clone())) - .field("stream-format", "aac"); + .field("stream-format", "raw"); } else { builder = builder.field("stream-format", "adts"); } @@ -1890,6 +1890,22 @@ mod tests { } } + #[test] + fn aac_caps_follow_the_description_framing() { + gst::init().unwrap(); + for description in [None, Some(bytes::Bytes::from_static(&[0x12, 0x08]))] { + let mut config = hang::catalog::AudioConfig::new(hang::catalog::AAC { profile: 2 }, 44_100, 1); + config.description = description.clone(); + let caps = super::audio_caps(&config).unwrap(); + let structure = caps.structure(0).unwrap(); + assert_eq!( + structure.get::("stream-format").unwrap(), + if description.is_some() { "raw" } else { "adts" } + ); + assert_eq!(structure.has_field("codec_data"), description.is_some()); + } + } + /// opusdec's templates require the mapping fields as ints, so decodebin finds no decoder without them. #[test] fn opus_caps_carry_the_stream_layout() { diff --git a/rs/moq-mux/src/container/ts/adts.rs b/rs/moq-mux/src/codec/aac/adts.rs similarity index 64% rename from rs/moq-mux/src/container/ts/adts.rs rename to rs/moq-mux/src/codec/aac/adts.rs index 632947bdce4..4fa0b9de953 100644 --- a/rs/moq-mux/src/container/ts/adts.rs +++ b/rs/moq-mux/src/codec/aac/adts.rs @@ -7,33 +7,58 @@ use anyhow::Context; -/// ISO 14496-3 sampling-frequency-index table. -const SAMPLE_RATES: [u32; 13] = [ - 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, -]; +use super::SAMPLE_RATES; + +/// An invalid ADTS header or frame. +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +#[non_exhaustive] +pub enum Error { + /// The fixed header is incomplete. + #[error("ADTS header truncated")] + Truncated, + /// The header does not start with an ADTS syncword. + #[error("missing ADTS syncword")] + Syncword, + /// The header names a reserved sample rate. + #[error("reserved ADTS sample rate index")] + SampleRate, + /// The declared frame cannot contain its header. + #[error("ADTS frame length smaller than its header")] + FrameLength, + /// A packet does not contain exactly one complete ADTS frame. + #[error("ADTS packet must contain exactly one complete frame")] + PacketLength, +} /// Bytes needed to attempt a header parse: the fixed header without a CRC. -pub(super) const MIN_HEADER_LEN: usize = 7; +pub(crate) const MIN_HEADER_LEN: usize = 7; /// Fields decoded from an ADTS fixed+variable header. #[derive(Clone, Copy, Debug)] -pub(super) struct Header { +pub struct Header { /// audioObjectType (ADTS `profile` + 1). AAC-LC is 2. pub object_type: u8, + /// The sampling rate signaled by the ADTS core. pub sample_rate: u32, /// channelConfiguration; 0 means a program config element in the raw data block describes the channels. pub channel_config: u8, /// Total access-unit length, header included. - pub frame_len: usize, + pub(crate) frame_len: usize, + /// The number of raw AAC blocks carried by this frame. + pub blocks: u8, /// Header length: 7 without CRC, 9 with. - pub header_len: usize, + pub(crate) header_len: usize, } impl Header { /// Parse the ADTS header at the start of `data`. - pub fn parse(data: &[u8]) -> anyhow::Result { - anyhow::ensure!(data.len() >= MIN_HEADER_LEN, "ADTS header truncated"); - anyhow::ensure!(data[0] == 0xFF && (data[1] & 0xF0) == 0xF0, "missing ADTS syncword"); + pub fn parse(data: &[u8]) -> Result { + if data.len() < MIN_HEADER_LEN { + return Err(Error::Truncated); + } + if data[0] != 0xFF || data[1] & 0xF6 != 0xF0 { + return Err(Error::Syncword); + } let protection_absent = data[1] & 0x01; let profile = (data[2] >> 6) & 0x03; @@ -45,22 +70,32 @@ impl Header { let sample_rate = SAMPLE_RATES .get(freq_index as usize) .copied() - .context("reserved ADTS sample rate index")?; + .ok_or(Error::SampleRate)?; let header_len = if protection_absent == 1 { 7 } else { 9 }; - anyhow::ensure!(frame_len >= header_len, "ADTS frame length smaller than its header"); + if frame_len < header_len { + return Err(Error::FrameLength); + } Ok(Self { object_type: profile + 1, sample_rate, channel_config, frame_len, + blocks: 1 + (data[6] & 3), header_len, }) } + /// Borrow the raw data of exactly one complete ADTS frame. + pub fn payload<'a>(&self, packet: &'a [u8]) -> Result<&'a [u8], Error> { + if packet.len() != self.frame_len || self.header_len > packet.len() { + return Err(Error::PacketLength); + } + Ok(&packet[self.header_len..]) + } } /// Build a 7-byte ADTS header (no CRC) for a raw AAC frame of `raw_len` bytes. -pub(super) fn write_header( +pub(crate) fn write_header( object_type: u8, sample_rate: u32, channel_config: u8, @@ -105,16 +140,6 @@ 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 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 => Ok(channel_count as u8), - 8 => Ok(7), - _ => anyhow::bail!("{channel_count} channels have no ADTS channelConfiguration"), - } -} - #[cfg(test)] mod tests { use super::*; @@ -150,14 +175,6 @@ mod tests { } } - #[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(); @@ -166,4 +183,35 @@ mod tests { assert_eq!(parsed.sample_rate, 44_100); assert_eq!(parsed.frame_len, 519); } + + #[test] + fn a_packet_holds_one_whole_frame_including_its_crc() { + for crc in [false, true] { + let mut packet = write_header(2, 44_100, 1, 3 + usize::from(crc) * 2).unwrap().to_vec(); + if crc { + packet[1] &= !1; + packet.extend_from_slice(&[0, 0]); + } + packet.extend_from_slice(&[1, 2, 3]); + let header = Header::parse(&packet).unwrap(); + assert_eq!(header.payload(&packet).unwrap(), [1, 2, 3]); + for end in 0..packet.len() { + assert!(header.payload(&packet[..end]).is_err()); + } + packet.push(0); + assert!( + header.payload(&packet).is_err(), + "trailing bytes are not part of the frame" + ); + } + } + + #[test] + fn an_adts_syncword_has_zero_layer_bits() { + let mut packet = write_header(2, 44_100, 1, 1).unwrap(); + for layer in 1..=3 { + packet[1] = 0xf1 | layer << 1; + assert!(matches!(Header::parse(&packet), Err(Error::Syncword))); + } + } } diff --git a/rs/moq-mux/src/codec/aac/import.rs b/rs/moq-mux/src/codec/aac/import.rs index 4fc34784137..9ff8cfe426d 100644 --- a/rs/moq-mux/src/codec/aac/import.rs +++ b/rs/moq-mux/src/codec/aac/import.rs @@ -28,6 +28,11 @@ impl Import { reserved: crate::catalog::Reserved, config: hang::catalog::AudioConfig, ) -> crate::Result { + if config.description.is_none() { + let err = crate::Error::from(super::Error::MissingDescription); + track.abort(err.reason())?; + return Err(err); + } tracing::debug!(name = ?track.name(), ?config, "starting track"); // The caller's config names the container; the writer is built from that same value so the // wire cannot disagree with what the rendition advertises. @@ -145,3 +150,26 @@ fn catalog_config(config: &Config) -> hang::catalog::AudioConfig { audio.container = hang::catalog::Container::Legacy; audio } + +#[cfg(test)] +mod tests { + #[tokio::test(start_paused = true)] + async fn raw_publication_needs_a_description_before_advertising() { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); + let track = broadcast + .create_track("audio", hang::container::track_info(hang::catalog::PRIORITY.audio)) + .unwrap(); + let mut subscriber = track.subscribe(None); + let config = hang::catalog::AudioConfig::new(hang::catalog::AAC { profile: 2 }, 48_000, 2); + assert!(matches!( + super::Import::new(track, catalog.reserve(), config), + Err(crate::Error::Aac(super::super::Error::MissingDescription)) + )); + assert!(catalog.snapshot().audio.renditions.is_empty()); + assert!(matches!( + subscriber.poll_finished(&kio::Waiter::noop()), + std::task::Poll::Ready(Err(_)) + )); + } +} diff --git a/rs/moq-mux/src/codec/aac/mod.rs b/rs/moq-mux/src/codec/aac/mod.rs index f01b4036b4c..b11cb599fdc 100644 --- a/rs/moq-mux/src/codec/aac/mod.rs +++ b/rs/moq-mux/src/codec/aac/mod.rs @@ -3,6 +3,9 @@ //! ISO 14496-3 AudioSpecificConfig parse and encode lives in [`Config`]. //! [`Import`] publishes raw AAC frames (not ADTS) to a moq broadcast. +/// ADTS framing for AAC packets without an AudioSpecificConfig. +pub mod adts; + mod import; pub use import::*; @@ -61,6 +64,10 @@ pub enum Error { /// The program config element declares zero channels. #[error("program config element declares no channels")] ProgramConfigEmpty, + + /// Raw AAC publication needs an AudioSpecificConfig in the catalog. + #[error("raw AAC publication requires an AudioSpecificConfig description")] + MissingDescription, } /// A result using this module's error type. diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index 7d7360e73d7..870a329a7e4 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -549,6 +549,10 @@ impl Track { fn samples(&self, payload: &[u8]) -> Option<(u64, u32)> { let parse = match &self.kind { Kind::Aac { config, .. } => return (config.sample_rate > 0).then_some((1024, config.sample_rate)), + Kind::Adts { .. } => { + let header = adts::Header::parse(payload).ok()?; + return Some((1024 * u64::from(header.blocks), header.sample_rate)); + } Kind::Mp2 { .. } => crate::codec::mp2::parse_header, Kind::Ac3 => crate::codec::ac3::parse_header, Kind::Eac3 => crate::codec::eac3::parse_header, @@ -882,6 +886,12 @@ enum Kind { /// 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 }, + /// Description-less AAC carries its ADTS framing in each frame. + Adts { + profile: u8, + sample_rate: u32, + channels: u32, + }, /// 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. `channel_config_code` is a plain @@ -907,7 +917,7 @@ impl Kind { fn stream_type(&self) -> u8 { match self { Kind::Video(stream_type) => *stream_type as u8, - Kind::Aac { .. } => StreamType::AdtsAac as u8, + Kind::Aac { .. } | Kind::Adts { .. } => StreamType::AdtsAac as u8, // Opus rides private-data PES; the registration + extension descriptors tell the // demuxer it's Opus. Kind::Opus { .. } => 0x06, @@ -945,7 +955,7 @@ impl Kind { match self { Kind::Video(StreamType::H265) => ".hev1", Kind::Video(_) => ".avc3", - Kind::Aac { .. } => ".aac", + Kind::Aac { .. } | Kind::Adts { .. } => ".aac", Kind::Opus { .. } => ".opus", Kind::Mp2 { .. } => ".mp2", Kind::Ac3 => ".ac3", @@ -963,7 +973,7 @@ impl Kind { | Kind::PesPacket { stream_type: 0x01 | 0x02, } => super::stats::Class::Video, - Kind::Aac { .. } | Kind::Opus { .. } | Kind::Mp2 { .. } | Kind::Ac3 | Kind::Eac3 => { + Kind::Aac { .. } | Kind::Adts { .. } | Kind::Opus { .. } | Kind::Mp2 { .. } | Kind::Ac3 | Kind::Eac3 => { super::stats::Class::Audio } Kind::Verbatim { .. } | Kind::PesPacket { .. } => super::stats::Class::Data, @@ -2791,7 +2801,10 @@ where tracks.iter().find(|t| { matches!( t.kind, - Kind::Aac { .. } | Kind::Opus { .. } | Kind::Mp2 { .. } | Kind::Ac3 | Kind::Eac3 + Kind::Aac { .. } + | Kind::Adts { .. } | Kind::Opus { .. } + | Kind::Mp2 { .. } | Kind::Ac3 + | Kind::Eac3 ) }) }) @@ -4097,7 +4110,7 @@ fn audio_buffer(config: &AudioConfig, kind: &Kind) -> Buffer { _ => (33_177_600, 51_216), }; let (rate, size) = match kind { - Kind::Aac { .. } | Kind::Opus { .. } => by_channels(), + Kind::Aac { .. } | Kind::Adts { .. } | Kind::Opus { .. } => by_channels(), Kind::Mp2 { .. } => (2_000_000, 3_584), // ATSC A/53 Part 5 5.7, and A/52 Annex G 3.6.1 for E-AC-3. Kind::Ac3 => (2_000_000, 2_592), @@ -4246,6 +4259,12 @@ fn es_payload(kind: &Kind, description: Option<&Bytes>, frame: &Frame, pce: bool framed.extend_from_slice(&frame.payload); Some(framed) } + Kind::Adts { .. } => { + let header = adts::Header::parse(&frame.payload)?; + header.payload(&frame.payload)?; + // ADTS names the core; the catalog may name SBR's output rate and channels. + Some(frame.payload.to_vec()) + } // Each moq Opus frame is one packet; prefix the Opus-in-TS control header. Kind::Opus { .. } => Some(opus_es_payload(&frame.payload)), // Legacy audio frames were ingested whole (framing header included), so @@ -4308,17 +4327,15 @@ fn audio_kind(config: &AudioConfig, name: &str) -> anyhow::Result { ensure_raw(&config.container, "audio", name)?; match &config.codec { 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. - let in_band = match &config.description { - Some(asc) => aac::in_band(asc)?, - None => aac::InBand { - object_type: codec.profile, + let Some(asc) = &config.description else { + return Ok(Kind::Adts { + profile: codec.profile, sample_rate: config.sample_rate, - channel_config: adts::channel_config_from_count(config.channel_count)?, - program_config: None, - }, + channels: config.channel_count, + }); }; + // The description names the LC core under explicit SBR or PS. + let in_band = aac::in_band(asc)?; // 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 { @@ -4629,6 +4646,21 @@ mod tests { Timestamp::from_millis(value).unwrap() } + #[test] + fn aac_without_a_description_preserves_adts() { + let config = hang::catalog::AudioConfig::new(hang::catalog::AAC { profile: 2 }, 44_100, 1); + let kind = super::audio_kind(&config, "audio").unwrap(); + let mut packet = super::adts::write_header(2, 44_100, 1, 3).unwrap().to_vec(); + packet.extend_from_slice(&[0x01, 0x52, 0xf2]); + let frame = super::Frame { + timestamp: ms(0), + payload: packet.clone().into(), + keyframe: true, + duration: None, + }; + assert_eq!(super::es_payload(&kind, None, &frame, true).unwrap().unwrap(), packet); + } + #[test] fn video_es_payload_keeps_aud_first() { for (stream_type, params, aud, slice) in [ diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index 63a4e9c03e6..937ce5452d9 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -175,6 +175,7 @@ async fn custom_selected_catalog_drives_export_and_follow() { .create_track(name, hang::container::track_info(hang::catalog::PRIORITY.audio)) .unwrap(); let mut config = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + config.description = Some(Bytes::from_static(&[0x11, 0x90])); config.container = Container::Legacy; snapshot.audio.renditions.insert(name.into(), config); let mut producer = Producer::new(track, HangContainer::Legacy(Kind::Data)); @@ -302,6 +303,7 @@ async fn export_aac_roundtrip() { let name = track.name().to_string(); { let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; catalog.modify().unwrap().audio.renditions.insert(name.clone(), cfg); } @@ -383,6 +385,7 @@ async fn export_skips_a_disabled_rendition() { ) .unwrap(); let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; cfg.enabled = enabled; catalog @@ -446,6 +449,7 @@ async fn export_packs_aac_frames_up_to_their_buffer() { let name = track.name().to_string(); { let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; catalog.modify().unwrap().audio.renditions.insert(name.clone(), cfg); } @@ -569,6 +573,7 @@ async fn export_lead_audio() -> BytesMut { .unwrap(); { let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; catalog .modify() @@ -1091,6 +1096,7 @@ async fn export_resumes_whole_mpeg2_video_at_a_sequence_header() { ) .unwrap(); let mut aac = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + aac.description = Some(Bytes::from_static(&[0x11, 0x90])); aac.container = Container::Legacy; let track = broadcast .create_track("audio", hang::container::track_info(hang::catalog::PRIORITY.audio)) @@ -1467,6 +1473,7 @@ async fn export_pcr_wraps_below_zero_at_start() { .unwrap(); { let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; catalog .modify() @@ -2812,12 +2819,10 @@ async fn aac_explicit_sbr_exports_its_lc_core() { } } -/// 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. +/// A description-less catalog selects ADTS, so raw AAC is refused before it can be mislabeled. #[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")] { +async fn description_less_aac_refuses_raw_payloads() { + for (profile, channel_count, refusal) in [(2, 7, "ADTS header truncated"), (5, 2, "ADTS header truncated")] { 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(); @@ -3724,6 +3729,7 @@ async fn export_rejects_si_pid_on_the_pmt() { let name = track.name().to_string(); { let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; let mut guard = catalog.modify().unwrap(); guard.audio.renditions.insert(name, cfg); @@ -3781,6 +3787,7 @@ async fn export_rejects_si_pid_on_an_elementary_stream() { let name = track.name().to_string(); { let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; let mut guard = catalog.modify().unwrap(); guard.audio.renditions.insert(name.clone(), cfg); @@ -3878,6 +3885,7 @@ async fn discontinuity_re_emits_tables_and_resumes_the_clock() { { let mut guard = catalog.modify().unwrap(); let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; guard.audio.renditions.insert(name, cfg); } @@ -4075,6 +4083,8 @@ async fn discontinuity_flags_the_break_once_across_tracks() { guard.video.renditions.insert(video_track.name().to_string(), video); let mut audio = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + + audio.description = Some(Bytes::from_static(&[0x11, 0x90])); audio.container = Container::Legacy; guard.audio.renditions.insert(audio_track.name().to_string(), audio); } @@ -5403,6 +5413,7 @@ async fn export_twice(with_video: bool) -> (Vec, Vec) { ) .unwrap(); let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; catalog .modify() @@ -5613,6 +5624,7 @@ impl Interleave { ) .unwrap(); let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; catalog .modify() @@ -7455,6 +7467,7 @@ async fn export_refuses_an_absurd_catalog_mux_rate() { let name = track.name().to_string(); catalog.modify().unwrap().audio.renditions.insert(name.clone(), { let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; cfg }); @@ -7510,6 +7523,7 @@ async fn export_pcr_is_its_byte_position_at_the_rate() { let name = track.name().to_string(); { let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; catalog.modify().unwrap().audio.renditions.insert(name.clone(), cfg); } @@ -7615,6 +7629,7 @@ impl Reordered { ) .unwrap(); let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; catalog .modify() @@ -8023,6 +8038,7 @@ async fn a_skipped_group_keeps_the_clock() { .unwrap(); { let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; catalog .modify() @@ -8087,6 +8103,7 @@ async fn a_timeline_restarting_at_zero_fails_the_export() { .unwrap(); { let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; catalog .modify() @@ -8188,6 +8205,7 @@ fn aac_rendition( .create_track(name, hang::container::track_info(hang::catalog::PRIORITY.audio)) .unwrap(); let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; catalog.modify().unwrap().audio.renditions.insert(name.to_string(), cfg); Producer::new(track, HangContainer::Legacy(crate::container::Kind::Data)) @@ -8371,6 +8389,7 @@ async fn a_rendition_moved_to_another_aac_configuration_fails() { let config = guard.audio.renditions.get_mut("main").unwrap(); config.track = Some("next".to_string()); config.sample_rate = 44_100; + config.description = Some(Bytes::from_static(&[0x12, 0x10])); } write_aac(&mut next, 200); let err = tokio::time::timeout(DRAIN, async { @@ -9134,7 +9153,7 @@ async fn large_pmt(streams: u16, mux_rate: Option) -> usize { .unwrap() .audio .renditions - .insert(name.clone(), AudioConfig::new(AAC { profile: 2 }, 48_000, 2)); + .insert(name.clone(), crate::codec::aac::config(&[0x11, 0x90]).unwrap()); let mut entry = tscat::Track::new(0x1001 + index); entry.descriptors = vec![tscat::Descriptor { tag: 0x0a, @@ -10368,6 +10387,8 @@ async fn export_placed(placed: Placed) -> crate::Result> { } let mut aac = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + + aac.description = Some(Bytes::from_static(&[0x11, 0x90])); aac.container = Container::Legacy; let track = broadcast .create_track( diff --git a/rs/moq-mux/src/container/ts/export_timing_test.rs b/rs/moq-mux/src/container/ts/export_timing_test.rs index 0fe2c2de406..d29fc98654a 100644 --- a/rs/moq-mux/src/container/ts/export_timing_test.rs +++ b/rs/moq-mux/src/container/ts/export_timing_test.rs @@ -127,6 +127,7 @@ impl Live { ) .unwrap(); let mut cfg = AudioConfig::new(AAC { profile: 2 }, 48_000, 2); + cfg.description = Some(Bytes::from_static(&[0x11, 0x90])); cfg.container = Container::Legacy; catalog .modify() diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index 4b48737537c..b50759e4a94 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -3738,10 +3738,12 @@ impl AacStream { // the next PES extends the buffer, rather than trusting it now. break; } else { - adts::Header::parse(&data[end..]).map(|_| (header, end)).map_err(Some) + adts::Header::parse(&data[end..]) + .map(|_| (header, end)) + .map_err(|err| Some(err.into())) } } - Err(err) => Err(Some(err)), + Err(err) => Err(Some(err.into())), }; let (header, end) = match parsed { diff --git a/rs/moq-mux/src/container/ts/mod.rs b/rs/moq-mux/src/container/ts/mod.rs index ac1956e9509..947e1d3a2cf 100644 --- a/rs/moq-mux/src/container/ts/mod.rs +++ b/rs/moq-mux/src/container/ts/mod.rs @@ -23,7 +23,7 @@ //! demultiplexing cannot carry (TS-level scrambling, PSI and SI as authored), and //! [`Release`] writes that track back out on a fixed delay. -mod adts; +use crate::codec::aac::adts; mod damage; mod demux; mod export; diff --git a/rs/moq-mux/src/container/ts/revision_test.rs b/rs/moq-mux/src/container/ts/revision_test.rs index dbaadccf4b1..39dfde9845d 100644 --- a/rs/moq-mux/src/container/ts/revision_test.rs +++ b/rs/moq-mux/src/container/ts/revision_test.rs @@ -601,6 +601,7 @@ fn aac_track( .unwrap(); let name = track.name().to_string(); let mut config = hang::catalog::AudioConfig::new(hang::catalog::AAC { profile: 2 }, 48_000, 2); + config.description = Some(bytes::Bytes::from_static(&[0x11, 0x90])); config.container = hang::catalog::Container::Legacy; let mut guard = catalog.modify().unwrap(); guard.audio.renditions.insert(name.clone(), config); From b033ac9f34509ba3f7a2f89a74038ddeb801157a Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 00:03:41 -0700 Subject: [PATCH 3/7] test(aac): describe raw SRT fixtures and retain ADTS block headers Co-Authored-By: Codex --- rs/moq-mux/src/codec/aac/adts.rs | 9 +++++---- rs/moq-srt/src/server.rs | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/rs/moq-mux/src/codec/aac/adts.rs b/rs/moq-mux/src/codec/aac/adts.rs index 4fa0b9de953..c1320979d58 100644 --- a/rs/moq-mux/src/codec/aac/adts.rs +++ b/rs/moq-mux/src/codec/aac/adts.rs @@ -46,7 +46,7 @@ pub struct Header { pub(crate) frame_len: usize, /// The number of raw AAC blocks carried by this frame. pub blocks: u8, - /// Header length: 7 without CRC, 9 with. + /// Header length, including CRC and raw-block position fields when present. pub(crate) header_len: usize, } @@ -71,7 +71,8 @@ impl Header { .get(freq_index as usize) .copied() .ok_or(Error::SampleRate)?; - let header_len = if protection_absent == 1 { 7 } else { 9 }; + let blocks = 1 + (data[6] & 3); + let header_len = if protection_absent == 1 { 7 } else { 7 + 2 * usize::from(blocks) }; if frame_len < header_len { return Err(Error::FrameLength); } @@ -81,11 +82,11 @@ impl Header { sample_rate, channel_config, frame_len, - blocks: 1 + (data[6] & 3), + blocks, header_len, }) } - /// Borrow the raw data of exactly one complete ADTS frame. + /// Borrow the payload after the header of one complete ADTS frame. pub fn payload<'a>(&self, packet: &'a [u8]) -> Result<&'a [u8], Error> { if packet.len() != self.frame_len || self.header_len > packet.len() { return Err(Error::PacketLength); diff --git a/rs/moq-srt/src/server.rs b/rs/moq-srt/src/server.rs index cff326f0cd3..40b606a532e 100644 --- a/rs/moq-srt/src/server.rs +++ b/rs/moq-srt/src/server.rs @@ -929,6 +929,7 @@ mod tests { { let mut guard = catalog.modify().unwrap(); let mut config = hang::catalog::AudioConfig::new(hang::catalog::AAC { profile: 2 }, 48_000, 2); + config.description = Some(Bytes::from_static(&[0x11, 0x90])); config.container = hang::catalog::Container::Legacy; guard.audio.renditions.insert(track.name().to_string(), config); } @@ -1035,6 +1036,7 @@ mod tests { .create_track(track, hang::container::track_info(hang::catalog::PRIORITY.audio)) .unwrap(); let mut config = hang::catalog::AudioConfig::new(hang::catalog::AAC { profile: 2 }, 48_000, 2); + config.description = Some(Bytes::from_static(&[0x11, 0x90])); config.container = hang::catalog::Container::Legacy; catalog .modify() From d470d0368150f0155b28d04688e9407d52227935 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 00:08:09 -0700 Subject: [PATCH 4/7] test(aac): cover protected multi-block ADTS headers Co-Authored-By: Codex --- rs/moq-mux/src/codec/aac/adts.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/rs/moq-mux/src/codec/aac/adts.rs b/rs/moq-mux/src/codec/aac/adts.rs index c1320979d58..57e11967d89 100644 --- a/rs/moq-mux/src/codec/aac/adts.rs +++ b/rs/moq-mux/src/codec/aac/adts.rs @@ -72,7 +72,11 @@ impl Header { .copied() .ok_or(Error::SampleRate)?; let blocks = 1 + (data[6] & 3); - let header_len = if protection_absent == 1 { 7 } else { 7 + 2 * usize::from(blocks) }; + let header_len = if protection_absent == 1 { + 7 + } else { + 7 + 2 * usize::from(blocks) + }; if frame_len < header_len { return Err(Error::FrameLength); } @@ -215,4 +219,16 @@ mod tests { assert!(matches!(Header::parse(&packet), Err(Error::Syncword))); } } + + #[test] + fn protected_multi_block_frames_keep_their_position_table_in_the_header() { + let mut packet = write_header(2, 44_100, 1, 11).unwrap().to_vec(); + packet[1] &= !1; + packet[6] |= 3; + packet.extend_from_slice(&[0; 8]); + packet.extend_from_slice(&[1, 2, 3]); + let header = Header::parse(&packet).unwrap(); + assert_eq!(header.blocks, 4); + assert_eq!(header.payload(&packet).unwrap(), [1, 2, 3]); + } } From 57b2bf251698f93f83ed4d00d273bcd067f7f2a9 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 28 Oct 2026 03:54:45 -0700 Subject: [PATCH 5/7] test(net): observe SETUP refusals before the peer's write can yield The shared stream write budget can yield a host task right after a SETUP write. Both moql SETUP refusal tests held the handshake promise unobserved across that write, so the refusal could land first and Bun reported it as an unhandled rejection. Observe each promise when it is created; the refusal assertions are unchanged. Co-Authored-By: Claude Opus 5.5 (cherry picked from commit f336e625eaad93a8d0af559a364ab75fd2f86eb7) --- js/net/src/connection/connect.test.ts | 4 +++ quest/m1/README.md | 1 - quest/m1/js-setup-rejection.md | 38 --------------------------- 3 files changed, 4 insertions(+), 39 deletions(-) delete mode 100644 quest/m1/js-setup-rejection.md diff --git a/js/net/src/connection/connect.test.ts b/js/net/src/connection/connect.test.ts index aeef7bb808a..19db002e18b 100644 --- a/js/net/src/connection/connect.test.ts +++ b/js/net/src/connection/connect.test.ts @@ -455,6 +455,8 @@ test("a client refuses an IETF version selected in the moql SETUP", async () => const restore = stubWebTransport(pair.client); try { const connecting = connect(url, { websocket: { enabled: false } }); + // The SETUP write may yield a task, during which the refusal can already land. + void connecting.catch(() => {}); const stream = await Stream.accept(pair.server, Lite.DRAFT_02); if (!stream) throw new Error("no SETUP stream"); await ClientSetup.decode(stream.reader); @@ -468,6 +470,8 @@ test("a client refuses an IETF version selected in the moql SETUP", async () => test("a server refuses a client offering only IETF versions in the moql SETUP", async () => { const pair = createMockTransportPair(""); const accepting = accept({ url, transport: pair.server }); + // The SETUP write may yield a task, during which the refusal can already land. + void accepting.catch(() => {}); const stream = await Stream.open(pair.client, { version: Lite.DRAFT_02 }); await new ClientSetup([DRAFT_14]).encode(stream.writer); await expect(accepting).rejects.toThrow("no common version found; client offered: ff00000e"); diff --git a/quest/m1/README.md b/quest/m1/README.md index e3489cf3128..64d28f322fb 100644 --- a/quest/m1/README.md +++ b/quest/m1/README.md @@ -176,7 +176,6 @@ removed). - [An HLS viewer starts capture](/quest/m1/hls-record-demand.md) - a record-track subscriber counts as demand for its rendition's media - [One moq-ffi build definition](/quest/m1/ffi-build-flags.md) - every binding takes moq-ffi's default features from one place - [moqsrc max-delay](/quest/m1/gst-max-delay.md) - GStreamer's `moqsrc` takes a `max-delay` property instead of a hard-coded 1 s budget -- [JS SETUP rejection ownership](/quest/m1/js-setup-rejection.md) - reproduce an unhandled version-refusal rejection without weakening the refusal - [moq-clock moves into kio::clock](/quest/m1/kio-clock.md) - moq-clock and its test macro become `kio::clock` and `#[kio::test]` before the cut publishes them - [Poll-first async](/quest/m1/poll-first/README.md) - library crates compile no coroutines by default: async wrappers are generated from `poll_*` and gated behind an `async` feature - [Every package's release backport ships](/quest/m1/release-versions.md) - a bot bumps every hand-versioned package changed on `release`, so backports publish without a manual bump diff --git a/quest/m1/js-setup-rejection.md b/quest/m1/js-setup-rejection.md deleted file mode 100644 index 0481ea17ccb..00000000000 --- a/quest/m1/js-setup-rejection.md +++ /dev/null @@ -1,38 +0,0 @@ -# [M] A JS SETUP refusal has one rejection owner - -## Goal - -An IETF-only client offering its versions in a moql SETUP is refused with the -existing no-common-version error and close behavior. The expected refusal -never escapes as an unhandled promise rejection. - -## Plan - -Found during local full CI for -[moq-dev/moq#6983](https://github.com/moq-dev/moq/pull/6983), at -[moq-dev/moq@58027559e9](https://github.com/moq-dev/moq/commit/58027559e98fc806a0136680c6b2bbe5b1b9bd75), with Bun 1.4.2. The test -`a server refuses a client offering only IETF versions in the moql SETUP` -reported an unhandled `no common version found; client offered: ff00000e` -from `js/net/src/connection/accept.ts:187`. - -The failure is not reproduced: clean current main passed the focused test, -all 44 connection-file tests, and all 2,507 net-package tests. No matching -open quest or PR was found, including deleted quest history. -A [later report](https://github.com/moq-dev/moq/pull/6997#issuecomment-6459139941) -saw the same test fail twice, intermittently, while iterating on -[moq-dev/moq#6906](https://github.com/moq-dev/moq/pull/6906). - -Reproduce and trace rejection ownership before changing code. The test -creates `accepting = accept(...)`, then awaits `Stream.open` and -`ClientSetup.encode` before attaching `expect(accepting).rejects`. A late -handler is a hypothesis, not an established cause. Determine whether the -test or production owns the missing handler, and fix it at that boundary. -Preserve the existing refusal assertion, error, and close behavior; do not -add retries, sleeps, or an arbitrary run-count gate. - -Public API: none expected. Wire: none expected. Update existing documentation -inline only if the eventual fix changes what a caller must do. - -## Related - -- [Nonfinal JS drops](https://github.com/moq-dev/moq/pull/6983) - the full check that exposed the failure; its drop behavior is independent From 162feb4c2fad7e480408777296ecce564e3ba8ab Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 14:59:49 -0700 Subject: [PATCH 6/7] quest: finish aac-description-less Co-Authored-By: Claude Opus 5.5 --- quest/m1/cut-audit/README.md | 1 - quest/m1/cut-audit/aac-description-less.md | 35 ---------------------- 2 files changed, 36 deletions(-) delete mode 100644 quest/m1/cut-audit/aac-description-less.md diff --git a/quest/m1/cut-audit/README.md b/quest/m1/cut-audit/README.md index c5316b207a3..4d39c941aff 100644 --- a/quest/m1/cut-audit/README.md +++ b/quest/m1/cut-audit/README.md @@ -55,7 +55,6 @@ leaving JS-only redesigns of `js/net` to [rs2ts](/quest/m2/rs2ts/README.md). ## Required - [A reserved group survives a route returning](/quest/m1/cut-audit/reserved-route-return.md) - a route flip A to B to A no longer panics a peeked reader -- [A description-less AAC rendition means one thing](/quest/m1/cut-audit/aac-description-less.md) - every player and exporter agrees on the frame format - [The export clock keeps only the rules admission still needs](/quest/m1/cut-audit/jitter-admission.md) - the clock rules the time and byte admission bound made redundant are measured and removed - [Stream-accept loops yield on a per-poll budget](/quest/m1/cut-audit/accept-budget.md) - a peer opening many streams cannot monopolize a session driver - [Rust and JS accept the same catalogs](/quest/m1/cut-audit/catalog-parity.md) - one strictness, per rendition, with a shared fixture corpus diff --git a/quest/m1/cut-audit/aac-description-less.md b/quest/m1/cut-audit/aac-description-less.md deleted file mode 100644 index 57b31da00aa..00000000000 --- a/quest/m1/cut-audit/aac-description-less.md +++ /dev/null @@ -1,35 +0,0 @@ -# [S] A description-less AAC rendition means one thing everywhere - -## Goal - -An `mp4a.40.*` catalog entry with no `description` has one defined frame -format, written in the hang draft, and every player and exporter (JS watch, -moqsrc, moq-audio, TS export) decodes it the same way. - -## Plan - -Decided 2026-10-25 (maintainer): ADTS, matching WebCodecs. For legacy and -LOC payloads, an absent `description` means one complete ADTS frame; an -AudioSpecificConfig means raw AAC. CMAF samples remain raw. - -The draft implements ADTS stripping in moq-audio and pass-through in TS -export, fixes GStreamer's raw-AAC caps spelling, and states the format in -the hang draft. Both native mismatches reproduced before the fixes: ADTS -produced different PCM from its raw block, and TS prepended a second header. -Tests cover both description modes in all four consumers, CRC framing, -truncated packets, publication metadata, and CMAF's raw samples. - -First-party AAC publishers retain an AudioSpecificConfig: the codec -importer, TS/FLV/fMP4/MKV importers, moq-audio's encoder, moqsink, and the JS -publisher. The raw codec importer now refuses to publish without one. - -Remaining maintainer choice: the shared parser moves from TS's private -module to the additive `moq_mux::codec::aac::adts::{Header, Error}` API, -used by both moq-audio and TS. Recommend this existing parser over a second -copy or a new internal crate. Approve that public shape, or choose a -sharing boundary, before treating the quest as complete. Finish the local -CI and all-language interop evidence before readiness. - -Public API: additive ADTS parser and a typed missing-description refusal -for the raw AAC importer. Wire: the hang format clarification; no MoQ -protocol framing changes. From 806b7a54eaa86d5098512d11c667934942163ac4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sat, 31 Oct 2026 15:58:59 -0700 Subject: [PATCH 7/7] docs(ts): explain Kind::Adts fields and fix a stale error comment Co-Authored-By: Claude Opus 5.5 --- rs/moq-mux/src/container/ts/export.rs | 4 +++- rs/moq-mux/src/container/ts/import.rs | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index 5b189dad80f..23e0923398a 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -943,7 +943,9 @@ enum Kind { /// 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 }, - /// Description-less AAC carries its ADTS framing in each frame. + /// Description-less AAC carries its ADTS framing in each frame, so frames pass through as + /// they are. The catalog's profile, rate, and channels are kept only so [`Export::rebind`] + /// refuses a rendition announced differently, as it does for [`Kind::Aac`]'s config. Adts { profile: u8, sample_rate: u32, diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index d07fc23d9c4..bd05446e88f 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -7278,8 +7278,7 @@ pub(super) mod test { // The AAC counterpart of the above. Worth its own test rather than trusting the shared // `Resync`: this path scans for the ADTS sync byte rather than a descriptor's, and - // builds its give-up error by adding context to the `anyhow` one `adts::Header::parse` - // returns, where the legacy path wraps a `thiserror` value. + // builds its give-up error from the one `adts::Header::parse` returns. #[test] fn aac_gives_up_when_nothing_ever_parses() { const AAC_PID: u16 = 0x0060;