diff --git a/Cargo.lock b/Cargo.lock index 9b49bf18310..213fa6b016a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4474,6 +4474,7 @@ dependencies = [ "bytes", "crc", "criterion", + "ebml-iterable", "futures", "h264-parser", "hang", diff --git a/Cargo.toml b/Cargo.toml index 671d13b42f9..a428e1c84be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -116,6 +116,7 @@ criterion = "0.8" # (`fallback-dynamic-loading`), so nothing links against libcuda at build time. cudarc = { version = "0.19", default-features = false, features = ["driver", "fallback-dynamic-loading", "cuda-12020"] } dispatch2 = "0.3.1" +ebml-iterable = "0.7.1" flate2 = { version = "1.1", default-features = false } futures = "0.3" getrandom = { version = "0.4", features = ["wasm_js"] } diff --git a/rs/moq-mux/CHANGELOG.md b/rs/moq-mux/CHANGELOG.md index da180a9de05..0d62100afa3 100644 --- a/rs/moq-mux/CHANGELOG.md +++ b/rs/moq-mux/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Refuse malformed MKV hierarchy, incomplete elements, zero timestamp scales, duplicate or zero track numbers, and block timestamp overflow. +- MKV audio/video tracks and FLV tags with unsupported codecs now fail the import instead of being silently dropped. Transcode or remove unsupported tracks before importing. +- Preserve VP9 keyframe dimensions up to 65536 instead of wrapping them to zero. + ## [0.10.17](https://github.com/moq-dev/moq/compare/moq-mux-v0.10.16...moq-mux-v0.10.17) - 2026-10-22 ### Other diff --git a/rs/moq-mux/Cargo.toml b/rs/moq-mux/Cargo.toml index f8abd99d9db..65fbb18cf58 100644 --- a/rs/moq-mux/Cargo.toml +++ b/rs/moq-mux/Cargo.toml @@ -17,6 +17,7 @@ anyhow = { workspace = true } base64 = { workspace = true } bytes = { workspace = true } crc = "3" +ebml-iterable = { workspace = true } h264-parser = { version = "0.4.2" } hang = { workspace = true } kio = { workspace = true } diff --git a/rs/moq-mux/benches/containers.rs b/rs/moq-mux/benches/containers.rs index 5a625f68f54..8f6654410f4 100644 --- a/rs/moq-mux/benches/containers.rs +++ b/rs/moq-mux/benches/containers.rs @@ -257,5 +257,38 @@ fn containers(c: &mut Criterion) { } } -criterion_group!(benches, containers); +/// Live WebM framing across transport chunk sizes, including byte-at-a-time metadata. +fn mkv_chunks(c: &mut Criterion) { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + let _enter = runtime.enter(); + let data = include_bytes!("../src/container/mkv/test_data/opus.webm"); + let mut group = c.benchmark_group("mkv-chunks"); + group.throughput(Throughput::Bytes(data.len() as u64)); + for chunk in [1, 7, 64, data.len()] { + group.bench_function(BenchmarkId::from_parameter(chunk), |b| { + b.iter_batched( + || { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); + let import = moq_mux::container::mkv::Import::new(broadcast, catalog.reserve()); + (catalog, import) + }, + |(catalog, mut import)| { + for bytes in std::hint::black_box(data).chunks(chunk) { + import.decode(bytes).unwrap(); + } + import.finish().unwrap(); + (catalog, import) + }, + BatchSize::PerIteration, + ); + }); + } + group.finish(); +} + +criterion_group!(benches, containers, mkv_chunks); criterion_main!(benches); diff --git a/rs/moq-mux/src/codec/vp9/import.rs b/rs/moq-mux/src/codec/vp9/import.rs index 8369e14c3e7..c3012c06230 100644 --- a/rs/moq-mux/src/codec/vp9/import.rs +++ b/rs/moq-mux/src/codec/vp9/import.rs @@ -53,10 +53,10 @@ impl Import { Ok(()) } - fn init(&mut self, vp9: hang::catalog::VP9, width: u16, height: u16) -> crate::Result<()> { + fn init(&mut self, vp9: hang::catalog::VP9, width: u32, height: u32) -> crate::Result<()> { let mut config = hang::catalog::VideoConfig::new(vp9); - config.coded_width = Some(width as u32); - config.coded_height = Some(height as u32); + config.coded_width = Some(width); + config.coded_height = Some(height); self.apply_config(config)?; Ok(()) diff --git a/rs/moq-mux/src/codec/vp9/mod.rs b/rs/moq-mux/src/codec/vp9/mod.rs index d2c773dc2d6..3c0e75a5979 100644 --- a/rs/moq-mux/src/codec/vp9/mod.rs +++ b/rs/moq-mux/src/codec/vp9/mod.rs @@ -49,8 +49,8 @@ pub(crate) struct FrameHeader { /// The parts of a VP9 key-frame header we surface to the catalog. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub(crate) struct KeyFrame { - pub width: u16, - pub height: u16, + pub width: u32, + pub height: u32, pub profile: u8, pub bit_depth: u8, /// `vpcC` chroma subsampling enum (0 = 4:2:0 vertical, 1 = 4:2:0 colocated, @@ -133,8 +133,8 @@ impl FrameHeader { } // frame_size (VP9 spec §6.2.3): each field is `value - 1`. - let width = (r.read(16)? + 1) as u16; - let height = (r.read(16)? + 1) as u16; + let width = r.read(16)? + 1; + let height = r.read(16)? + 1; Ok(Self { keyframe: true, @@ -185,8 +185,8 @@ pub(crate) fn config_from_keyframe(data: &[u8]) -> Result u8 { /// The lowest VP9 level whose `MaxLumaPictureSize` fits `width * height` (VP9 /// spec Annex A). Framerate and bitrate aren't known from the header, so this /// is a picture-size lower bound rather than the exact encoded level. -fn level_for(width: u16, height: u16) -> u8 { +fn level_for(width: u32, height: u32) -> u8 { let area = width as u64 * height as u64; // (level, MaxLumaPictureSize), ascending. const LEVELS: &[(u8, u64)] = &[ @@ -309,6 +309,28 @@ mod tests { assert!(!key.full_range); } + #[test] + fn dimensions_keep_the_complete_sixteen_bit_plus_one_range() { + for (width, height) in [ + (1u32, 65_536u32), + (65_536, 1), + (65_535, 65_536), + (65_536, 65_535), + (65_536, 65_536), + ] { + let mut frame = KEYFRAME_320X240.to_vec(); + for (start, value) in [(36, width - 1), (52, height - 1)] { + for bit in 0..16 { + let position = start + bit; + let mask = 1 << (7 - position % 8); + frame[position / 8] = (frame[position / 8] & !mask) | (((value >> (15 - bit)) & 1) as u8 * mask); + } + } + let config = config_from_keyframe(&frame).unwrap().unwrap(); + assert_eq!((config.coded_width, config.coded_height), (Some(width), Some(height))); + } + } + #[test] fn keyframe_to_catalog() { let key = FrameHeader::parse(KEYFRAME_320X240).unwrap().key.unwrap(); diff --git a/rs/moq-mux/src/container/flv/import.rs b/rs/moq-mux/src/container/flv/import.rs index ff7e44555dc..a5b1c141446 100644 --- a/rs/moq-mux/src/container/flv/import.rs +++ b/rs/moq-mux/src/container/flv/import.rs @@ -18,7 +18,7 @@ //! codecs (MP3 / AC-3 / E-AC-3) carry their config in band, so they configure from //! the first frame instead. Sample bytes already match the codec payload, so no codec //! transform is needed before the selected media container wraps them. FLAC (`fLaC`) -//! enhanced audio, and any other codec, are logged and dropped. +//! enhanced audio, and any other codec, fail the import. use std::collections::BTreeMap; use std::time::{Duration, Instant}; @@ -52,8 +52,7 @@ const MAX_DATA_OFFSET: usize = 64 * 1024; /// /// Supports legacy H.264 + AAC and the enhanced-RTMP FourCC codecs (HEVC, AV1, /// VP9, Opus, AC-3, E-AC-3), the payloads produced by RTMP encoders and -/// `ffmpeg -f flv`. Unsupported codecs, plus `onMetaData` script tags, are logged -/// and dropped. +/// `ffmpeg -f flv`. Unsupported codecs fail the import; `onMetaData` script tags are ignored. /// /// Legacy and single-track enhanced tags carry one video and one audio track; /// enhanced-RTMP multitrack tags carry several of each, addressed by track id, @@ -217,8 +216,7 @@ impl Import { let frame_type = first >> 4; let codec_id = first & 0x0f; if codec_id != VIDEO_CODEC_AVC { - tracing::warn!(codec_id, "unsupported FLV video codec, dropping"); - return Ok(()); + return Err(crate::Error::UnknownFormat(format!("FLV video CodecID {codec_id}")).into()); } anyhow::ensure!(body.len() >= 5, "AVC video tag too short"); @@ -294,6 +292,9 @@ impl Import { payload: &[u8], timestamp: u64, ) -> anyhow::Result<()> { + if !matches!(fourcc, b"avc1" | b"hvc1" | b"av01" | b"vp09") { + return Err(crate::Error::UnknownFormat(format!("FLV video FourCC {fourcc:?}")).into()); + } match packet_type { VIDEO_PACKET_SEQUENCE_START => { let config = match fourcc { @@ -304,8 +305,7 @@ impl Import { // redundant with the key-frame header we configure from. b"vp09" => return Ok(()), other => { - tracing::warn!(fourcc = ?other, "unsupported enhanced FLV video codec, dropping"); - return Ok(()); + return Err(crate::Error::UnknownFormat(format!("FLV video FourCC {other:?}")).into()); } }; self.init_video(track_id, config) @@ -361,8 +361,7 @@ impl Import { return self.write_audio(DEFAULT_TRACK_ID, frame, timestamp); } if sound_format != AUDIO_FORMAT_AAC { - tracing::warn!(sound_format, "unsupported FLV audio format, dropping"); - return Ok(()); + return Err(crate::Error::UnknownFormat(format!("FLV audio SoundFormat {sound_format}")).into()); } anyhow::ensure!(body.len() >= 2, "AAC audio tag too short"); @@ -425,6 +424,9 @@ impl Import { payload: &[u8], timestamp: u64, ) -> anyhow::Result<()> { + if !matches!(fourcc, b"Opus" | b"mp4a" | b".mp3" | b"ac-3" | b"ec-3") { + return Err(crate::Error::UnknownFormat(format!("FLV audio FourCC {fourcc:?}")).into()); + } match packet_type { AUDIO_PACKET_SEQUENCE_START => { let config = match fourcc { @@ -433,8 +435,7 @@ impl Import { // MP3 / AC-3 / E-AC-3 are verbatim with no sequence header; they // configure from the first frame. Anything else is unsupported. other => { - tracing::warn!(fourcc = ?other, "unsupported enhanced FLV audio codec, dropping"); - return Ok(()); + return Err(crate::Error::UnknownFormat(format!("FLV audio FourCC {other:?}")).into()); } }; self.init_audio(track_id, config) diff --git a/rs/moq-mux/src/container/flv/import_test.rs b/rs/moq-mux/src/container/flv/import_test.rs index 2073a6886ce..26ab5f783ac 100644 --- a/rs/moq-mux/src/container/flv/import_test.rs +++ b/rs/moq-mux/src/container/flv/import_test.rs @@ -807,3 +807,49 @@ async fn live_import_restarts_forward_after_idle() { crate::container::test_util::common_offset(&verbatim.published, &second); } } + +#[test] +fn unsupported_codecs_fail_at_the_public_importer_boundary() { + let mut cases = vec![(super::TAG_VIDEO, vec![0x12, 0]), (super::TAG_AUDIO, vec![0xb0, 0])]; + for (tag, packets) in [ + (super::TAG_VIDEO, &[0, 1, 2, 3, 4][..]), + (super::TAG_AUDIO, &[0, 1, 2, 4][..]), + ] { + for packet in packets { + cases.push((tag, vec![0x90 | packet, b'n', b'o', b'p', b'e'])); + // OneTrack enhanced framing carries the same codec after its framing byte. + cases.push((tag, vec![0x95, *packet, b'n', b'o', b'p', b'e', 0])); + } + } + + for (tag, payload) in cases { + let mut data = synth_flv(); + write_tag(&mut data, tag, 0, &payload); + for initial in [false, true] { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); + let init = crate::import::ContainerInit::new( + crate::import::ContainerFormat::Flv, + if initial { + bytes::Bytes::copy_from_slice(&data) + } else { + bytes::Bytes::new() + }, + ); + let result = (|| -> crate::Result<()> { + let mut import: crate::import::Container = + crate::import::Container::new(broadcast, catalog.reserve(), &init)?; + if !initial { + for chunk in data.chunks(7) { + import.decode(chunk)?; + } + } + import.finish() + })(); + assert!( + matches!(result, Err(crate::Error::UnknownFormat(_))), + "tag {tag}, initial {initial}: {result:?}" + ); + } + } +} diff --git a/rs/moq-mux/src/container/flv/mod.rs b/rs/moq-mux/src/container/flv/mod.rs index c6c3f9463c3..1c7d1171770 100644 --- a/rs/moq-mux/src/container/flv/mod.rs +++ b/rs/moq-mux/src/container/flv/mod.rs @@ -18,7 +18,7 @@ //! from the frame), and the sample bytes already match the //! [`Legacy`](crate::catalog::hang::Container) container, so no codec transform is //! needed. Other legacy codecs (VP6, Speex, …) and the E-RTMP FLAC audio are -//! logged and dropped on import, and rejected on export. +//! rejected on import and export. mod export; mod import; diff --git a/rs/moq-mux/src/container/mkv/import.rs b/rs/moq-mux/src/container/mkv/import.rs index 4f49485c09a..cd7ff31ffda 100644 --- a/rs/moq-mux/src/container/mkv/import.rs +++ b/rs/moq-mux/src/container/mkv/import.rs @@ -1,15 +1,12 @@ -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::convert::TryFrom; use std::io::Cursor; use crate::Result; -use bytes::{Buf, Bytes, BytesMut}; +use bytes::{Bytes, BytesMut}; use hang::catalog::{AAC, AudioCodec, AudioConfig, H264, H265, VP9, VideoCodec, VideoConfig}; use moq_net::Timestamp; use mp4_atom::Atom; -use webm_iterable::WebmIterator; -use webm_iterable::errors::TagIteratorError; -use webm_iterable::iterator::AllowableErrors; use webm_iterable::matroska_spec::{Master, MatroskaSpec, SimpleBlock}; use super::Error; @@ -37,7 +34,7 @@ const DEFAULT_TIMESTAMP_SCALE_NS: u64 = 1_000_000; /// - FLAC (`A_FLAC`) /// - MP3 (`A_MPEG/L3`) /// -/// Unsupported codecs (e.g. Vorbis, AC3, subtitles) are logged and dropped. +/// Unsupported track roles are skipped; malformed tracks and unsupported codecs fail the import. pub struct Import { broadcast: moq_net::broadcast::Producer, catalog: crate::catalog::Producer, @@ -49,6 +46,10 @@ pub struct Import { /// Accumulated unparsed input. buffer: BytesMut, + /// Incremental element state preserves hierarchy across transport chunks. + decoder: ebml_iterable::TagDecoder, + /// No more input may arrive after finishing, even when finishing failed. + finished: bool, /// Whether the Tracks element has been processed. tracks_seen: bool, @@ -82,9 +83,6 @@ struct MkvTrack { kind: TrackKind, track: Media, group: Option, - /// Highest block timestamp (Matroska ticks: cluster_ts + block_relative) already emitted. - /// Used to dedup re-parsed blocks across decode() calls. - last_emitted_ticks: Option, } enum Media { @@ -138,6 +136,16 @@ impl Import { container, initial_reservation: Some(reserved), buffer: BytesMut::new(), + decoder: ebml_iterable::TagDecoder::new(&[ + MatroskaSpec::Ebml(Master::Start), + MatroskaSpec::Info(Master::Start), + MatroskaSpec::Tracks(Master::Start), + MatroskaSpec::TrackEntry(Master::Start), + MatroskaSpec::Audio(Master::Start), + MatroskaSpec::Video(Master::Start), + MatroskaSpec::BlockGroup(Master::Start), + ]), + finished: false, tracks_seen: false, timestamp_scale_ns: DEFAULT_TIMESTAMP_SCALE_NS, cluster_timestamp: 0, @@ -160,75 +168,23 @@ impl Import { /// scratch). Bytes that cannot yet form a complete top-level tag are retained /// for the next call. pub fn decode(&mut self, data: &[u8]) -> Result<()> { - // Move the input into our scratch buffer. + if self.finished && !data.is_empty() { + return Err(Error::MatroskaParse.into()); + } self.buffer.extend_from_slice(data); self.drain() } - /// Run the iterator over the buffered bytes, processing every fully-parsed top-level tag. - /// - /// On each call, the iterator restarts from the beginning of the retained buffer. Tag - /// handling is idempotent (state flags for header/tracks, per-track timestamp dedup for - /// blocks). After parsing stops (UnexpectedEOF or end of buffer), bytes up to the start - /// of the most-recently emitted top-level tag are discarded so memory does not grow - /// unboundedly. + /// Decode complete elements, leaving incomplete bytes and master state for the next chunk. fn drain(&mut self) -> Result<()> { - // Buffer master tags that are bounded and convenient to handle atomically. - let buffered = [ - MatroskaSpec::Ebml(Master::Start), - MatroskaSpec::Info(Master::Start), - MatroskaSpec::Tracks(Master::Start), - MatroskaSpec::TrackEntry(Master::Start), - MatroskaSpec::Audio(Master::Start), - MatroskaSpec::Video(Master::Start), - MatroskaSpec::BlockGroup(Master::Start), - ]; - - if self.buffer.is_empty() { - return Ok(()); - } - - let snapshot = self.buffer.clone().freeze(); - let mut cursor = Cursor::new(snapshot.as_ref()); - let mut iter = WebmIterator::new(&mut cursor, &buffered); - // We restart the iterator from the beginning of the retained buffer on every - // drain pass. Once data is replayed mid-Segment, ebml-iterable would otherwise - // reject Segment children (Cluster, Tracks, etc.) as appearing without their - // parent. Allowing hierarchy problems plus our own dedup logic on emitted - // blocks gives us idempotent streaming behavior. - iter.allow_errors(&[AllowableErrors::HierarchyProblems]); - // Don't synthesize Master::End tags when the buffer ends mid-element. - iter.emit_master_end_when_eof(false); - - let mut last_offset: usize = 0; - - loop { - match iter.next() { - Some(Ok(tag)) => { - last_offset = iter.last_emitted_tag_offset(); - self.handle_tag(tag)?; - } - Some(Err(TagIteratorError::UnexpectedEOF { .. })) => break, - Some(Err(_e)) => { - return Err(Error::MatroskaParse.into()); - } - None => { - last_offset = snapshot.len(); - break; - } - } - } - - drop(iter); - - // Retain bytes from the start of the last emitted tag (safe replay point) onward. - // At the very least, this lets us reuse partially-read tags as more data arrives. - // If we never emitted anything (very first call with too few bytes), keep everything. - if last_offset > 0 { - self.buffer.advance(last_offset); + while let Some(tag) = self + .decoder + .decode(&mut self.buffer) + .map_err(|_| Error::MatroskaParse)? + { + self.handle_tag(tag.tag)?; } - Ok(()) } @@ -244,13 +200,14 @@ impl Import { MatroskaSpec::Info(Master::Full(children)) => { for c in &children { if let MatroskaSpec::TimestampScale(v) = c { + if *v == 0 { + return Err(Error::MatroskaParse.into()); + } self.timestamp_scale_ns = *v; } } } - // Idempotency: if the parser restarts mid-stream and `last_offset` - // happens to point at Tracks (i.e. Tracks was the last fully-emitted - // tag), we'll see it again. Process once. + // A repeated Tracks element does not replace the published track set. MatroskaSpec::Tracks(Master::Full(children)) if !self.tracks_seen => { self.handle_tracks(children)?; self.tracks_seen = true; @@ -291,17 +248,16 @@ impl Import { } fn handle_tracks(&mut self, entries: Vec) -> Result<()> { + let mut numbers = HashSet::new(); for entry in entries { - if let MatroskaSpec::TrackEntry(Master::Full(children)) = entry - && let Err(e) = self.add_track(children) - { - tracing::warn!(error = ?e, "skipping MKV track"); + if let MatroskaSpec::TrackEntry(Master::Full(children)) = entry { + self.add_track(children, &mut numbers)?; } } Ok(()) } - fn add_track(&mut self, children: Vec) -> Result<()> { + fn add_track(&mut self, children: Vec, numbers: &mut HashSet) -> Result<()> { let mut track_number: Option = None; let mut track_type: Option = None; let mut codec_id: Option = None; @@ -311,7 +267,11 @@ impl Import { for c in children { match c { - MatroskaSpec::TrackNumber(v) => track_number = Some(v), + MatroskaSpec::TrackNumber(v) => { + if track_number.replace(v).is_some() { + return Err(moq_net::Error::Duplicate.into()); + } + } MatroskaSpec::TrackType(v) => track_type = Some(v), MatroskaSpec::CodecID(v) => codec_id = Some(v), MatroskaSpec::CodecPrivate(v) => codec_private = Some(Bytes::from(v)), @@ -322,6 +282,12 @@ impl Import { } let track_number = track_number.ok_or(Error::MissingTrackNumber)?; + if track_number == 0 { + return Err(Error::MatroskaParse.into()); + } + if !numbers.insert(track_number) { + return Err(moq_net::Error::Duplicate.into()); + } let track_type = track_type.ok_or(Error::MissingTrackType)?; let codec_id = codec_id.ok_or(Error::MissingCodecId)?; @@ -374,7 +340,6 @@ impl Import { kind, track: media, group: None, - last_emitted_ticks: None, }, ); @@ -414,20 +379,16 @@ impl Import { // Compute PTS in MKV's native nanosecond units and stamp it on the // timestamp at NANO scale so a passthrough re-emit preserves precision. - let block_ticks = (self.cluster_timestamp as i64) + (rel_ts as i64); - if block_ticks < 0 { - return Err(Error::NegativeBlockTimestamp.into()); - } - - // Skip blocks we've already emitted on a previous decode() pass (buffer replay). - if let Some(last) = track.last_emitted_ticks - && block_ticks <= last - { - return Ok(()); - } - track.last_emitted_ticks = Some(block_ticks); + let block_ticks = self + .cluster_timestamp + .checked_add_signed(i64::from(rel_ts)) + .ok_or(if rel_ts < 0 { + Error::NegativeBlockTimestamp + } else { + Error::TimestampOverflow + })?; - let pts_ns = (block_ticks as u64) + let pts_ns = block_ticks .checked_mul(self.timestamp_scale_ns) .ok_or(Error::TimestampOverflow)?; let timestamp = Timestamp::from_nanos(pts_ns)?; @@ -472,6 +433,16 @@ impl Import { /// Finish all tracks, flushing current groups. pub fn finish(&mut self) -> Result<()> { + self.finished = true; + while let Some(tag) = self + .decoder + .decode_eof(&mut self.buffer) + .map_err(|_| Error::MatroskaParse)? + { + self.handle_tag(tag.tag)?; + } + self.buffer = BytesMut::new(); + self.initial_reservation = None; for track in self.tracks.values_mut() { if let Some(g) = track.group.take() { g.finish()?; diff --git a/rs/moq-mux/src/container/mkv/import_test.rs b/rs/moq-mux/src/container/mkv/import_test.rs index 3eebf633ac7..7035f5607d0 100644 --- a/rs/moq-mux/src/container/mkv/import_test.rs +++ b/rs/moq-mux/src/container/mkv/import_test.rs @@ -344,8 +344,8 @@ fn test_mp3_catalog() { } #[test] -fn test_chunked_decode_dedup() { - // Build the same WebM and feed it in tiny chunks. The dedup logic should ensure +fn test_chunked_decode() { + // Build the same WebM and feed it in tiny chunks. Incremental parsing ensures // that frames aren't emitted twice across the parse restarts. let data = MkvBuilder::new() .header("webm") @@ -369,7 +369,7 @@ fn test_chunked_decode_dedup() { let catalog = crate::catalog::Producer::new(&mut broadcast, crate::catalog::Config::default()).unwrap(); let mut mkv = crate::container::mkv::Import::new(broadcast, catalog.reserve()); - // Feed in 16-byte chunks to stress the chunked-restart code path. + // Split elements across input chunks. for chunk in data.chunks(16) { let b = bytes::BytesMut::from(chunk); mkv.decode(&b).expect("decode chunk"); @@ -384,9 +384,8 @@ fn test_chunked_decode_dedup() { } #[test] -fn test_unsupported_codec_skipped() { - // Mix of supported (Opus) and unsupported (Vorbis) audio tracks. The Vorbis track - // should be dropped with a warning; Opus should make it into the catalog. +fn test_unsupported_codec_refused() { + // An unsupported audio codec fails the import instead of silently dropping a track. let data = MkvBuilder::new() .header("webm") .segment_start() @@ -404,10 +403,11 @@ fn test_unsupported_codec_skipped() { .segment_end() .build(); - let catalog = run(&data); - assert_eq!(catalog.audio.renditions.len(), 1); - let a = catalog.audio.renditions.values().next().unwrap(); - assert!(matches!(a.codec, AudioCodec::Opus)); + for initial in [false, true] { + assert!( + matches!(import_result(&data, initial), Err(crate::Error::Mkv(super::Error::UnsupportedAudioCodec(codec))) if codec == "A_VORBIS") + ); + } } #[test] @@ -459,12 +459,274 @@ fn rendition_is_not_published_when_the_media_track_fails() { let mut mkv = crate::container::mkv::Import::new(broadcast, catalog.reserve()); let buf = bytes::BytesMut::from(&data[..]); - // The importer logs and skips a track it cannot build, rather than failing the whole - // decode, so the outcome shows up in the catalog rather than in this result. - let _ = mkv.decode(&buf); + // A producer failure reaches the caller before advertising a rendition. + assert!(mkv.decode(&buf).is_err()); assert!( catalog.snapshot().video.renditions.is_empty(), "a rendition whose media producer failed must not be advertised" ); } + +/// Container entry points must preserve importer failures, both in the constructor's +/// initial data and in arbitrarily split subsequent chunks. +fn import_result(data: &[u8], initial: bool) -> crate::Result<()> { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = crate::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); + let init = crate::import::ContainerInit::new( + crate::import::ContainerFormat::Mkv, + if initial { + bytes::Bytes::copy_from_slice(data) + } else { + bytes::Bytes::new() + }, + ); + let mut import: crate::import::Container = crate::import::Container::new(broadcast, catalog.reserve(), &init)?; + if !initial { + for chunk in data.chunks(7) { + import.decode(chunk)?; + } + } + import.finish() +} + +#[test] +fn malformed_track_metadata_fails_through_the_container() { + use MatroskaSpec::*; + let cases = [ + TrackEntry(Master::Full(vec![TrackType(2), CodecID("A_OPUS".into())])), + TrackEntry(Master::Full(vec![ + TrackNumber(2), + TrackType(2), + CodecID("A_AAC".into()), + ])), + TrackEntry(Master::Full(vec![ + TrackNumber(2), + TrackType(1), + CodecID("V_MPEG4/ISO/AVC".into()), + CodecPrivate(vec![0]), + ])), + ]; + for (index, invalid) in cases.into_iter().enumerate() { + for reverse in [false, true] { + let mut tracks = vec![track_entry_audio_opus(1, 48_000.0, 2), invalid.clone()]; + if reverse { + tracks.reverse(); + } + let data = MkvBuilder::new() + .header("matroska") + .segment_start() + .info(1_000_000) + .tracks(tracks) + .segment_end() + .build(); + for initial in [false, true] { + assert!( + import_result(&data, initial).is_err(), + "case {index}, reverse {reverse}, initial {initial}" + ); + } + } + } +} + +#[test] +fn duplicate_track_numbers_fail_even_for_skipped_roles() { + use MatroskaSpec::*; + for second in [ + track_entry_audio_opus(1, 44_100.0, 1), + TrackEntry(Master::Full(vec![ + TrackNumber(1), + TrackType(18), + CodecID("B_BUTTONS".into()), + ])), + ] { + for reverse in [false, true] { + let mut tracks = vec![track_entry_audio_opus(1, 48_000.0, 2), second.clone()]; + if reverse { + tracks.reverse(); + } + let data = MkvBuilder::new() + .header("matroska") + .segment_start() + .info(1_000_000) + .tracks(tracks) + .segment_end() + .build(); + for initial in [false, true] { + let result = import_result(&data, initial); + assert!( + matches!(&result, Err(crate::Error::Moq(moq_net::Error::Duplicate))), + "reverse {reverse}, initial {initial}: {result:?}" + ); + } + } + } +} + +#[test] +fn invalid_timestamp_scales_are_refused() { + for initial in [false, true] { + let zero = MkvBuilder::new() + .header("webm") + .segment_start() + .info(0) + .tracks(vec![track_entry_audio_opus(1, 48_000.0, 2)]) + .cluster(0, || vec![simple_block(1, 0, true, b"opus")]) + .segment_end() + .build(); + assert!(matches!( + import_result(&zero, initial), + Err(crate::Error::Mkv(super::Error::MatroskaParse)) + )); + } +} + +#[test] +fn block_tick_overflow_is_refused_before_timestamp_conversion() { + for (cluster, relative, scale) in [(u64::MAX, 1, 1), (u64::MAX, 0, 2)] { + for initial in [false, true] { + let data = MkvBuilder::new() + .header("webm") + .segment_start() + .info(scale) + .tracks(vec![track_entry_audio_opus(1, 48_000.0, 2)]) + .cluster(cluster, || vec![simple_block(1, relative, true, b"opus")]) + .segment_end() + .build(); + let outcome = std::panic::catch_unwind(|| import_result(&data, initial)); + assert!(matches!( + outcome, + Ok(Err(crate::Error::Mkv(super::Error::TimestampOverflow))) + )); + } + } + let negative = MkvBuilder::new() + .header("webm") + .segment_start() + .info(1) + .tracks(vec![track_entry_audio_opus(1, 48_000.0, 2)]) + .cluster(0, || vec![simple_block(1, -1, true, b"opus")]) + .segment_end() + .build(); + for initial in [false, true] { + assert!(matches!( + import_result(&negative, initial), + Err(crate::Error::Mkv(super::Error::NegativeBlockTimestamp)) + )); + } +} + +#[test] +fn partial_track_metadata_is_refused_at_eof() { + let data = MkvBuilder::new() + .header("webm") + .segment_start() + .info(1_000_000) + .tracks(vec![track_entry_audio_opus(1, 48_000.0, 2)]) + .segment_end() + .build(); + for missing in 1..=8 { + for initial in [false, true] { + assert!( + matches!( + import_result(&data[..data.len() - missing], initial), + Err(crate::Error::Mkv(super::Error::MatroskaParse)) + ), + "missing {missing}, initial {initial}" + ); + } + } + for initial in [false, true] { + import_result(&data, initial).unwrap(); + } +} + +#[test] +fn block_ticks_above_the_signed_range_are_not_misreported_as_negative() { + for (cluster, relative) in [(i64::MAX as u64, 1i16), (u64::MAX, -1)] { + let data = MkvBuilder::new() + .header("webm") + .segment_start() + .info(1) + .tracks(vec![track_entry_audio_opus(1, 48_000.0, 2)]) + .cluster(cluster, || vec![simple_block(1, relative, true, b"frame")]) + .segment_end() + .build(); + // The addition fits u64, but exceeds MoQ's existing timestamp value range. + for initial in [false, true] { + assert!( + matches!(import_result(&data, initial), Err(crate::Error::TimestampOverflow(_))), + "{cluster}+{relative}, initial {initial}" + ); + } + } +} + +#[test] +fn segment_children_require_their_parent() { + let header = MkvBuilder::new().header("webm").build(); + let data = MkvBuilder::new() + .header("webm") + .segment_start() + .info(1_000_000) + .tracks(vec![track_entry_audio_opus(1, 48_000.0, 2)]) + .segment_end() + .build(); + let start = data + .windows(4) + .position(|bytes| bytes == [0x16, 0x54, 0xae, 0x6b]) + .unwrap(); + let mut malformed = header; + malformed.extend_from_slice(&data[start..]); + for initial in [false, true] { + assert!(matches!( + import_result(&malformed, initial), + Err(crate::Error::Mkv(super::Error::MatroskaParse)) + )); + } +} + +#[tokio::test(start_paused = true)] +async fn live_webm_and_terminal_state_survive_chunk_boundaries() { + let data = include_bytes!("test_data/opus.webm"); + for chunk in [1, 7, 64, data.len()] { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let consumer = broadcast.consume(); + let catalog = crate::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); + let mut import = super::Import::new(broadcast, catalog.reserve()); + for bytes in data.chunks(chunk) { + import.decode(bytes).unwrap(); + } + import.finish().unwrap(); + let snapshot = catalog.snapshot(); + assert_eq!(snapshot.audio.renditions.len(), 1); + let published = crate::container::test_util::published(&consumer, &snapshot).await; + let timestamps = published.into_values().next().unwrap(); + assert_eq!(timestamps.len(), 6); + assert!(timestamps.windows(2).all(|pair| pair[0] < pair[1])); + assert!(matches!( + import.decode(&[0]), + Err(crate::Error::Mkv(super::Error::MatroskaParse)) + )); + } +} + +#[test] +fn invalid_track_number_fields_are_refused() { + use MatroskaSpec::*; + for fields in [vec![TrackNumber(0)], vec![TrackNumber(1), TrackNumber(2)]] { + let mut entry = fields; + entry.extend([TrackType(2), CodecID("A_OPUS".into())]); + let data = MkvBuilder::new() + .header("webm") + .segment_start() + .info(1_000_000) + .tracks(vec![TrackEntry(Master::Full(entry))]) + .segment_end() + .build(); + for initial in [false, true] { + assert!(import_result(&data, initial).is_err()); + } + } +} diff --git a/rs/moq-mux/src/container/mkv/test_data/README.md b/rs/moq-mux/src/container/mkv/test_data/README.md new file mode 100644 index 00000000000..1523d75d769 --- /dev/null +++ b/rs/moq-mux/src/container/mkv/test_data/README.md @@ -0,0 +1,13 @@ +# WebM fixture + +`opus.webm` is 0.1 seconds of generated 440 Hz mono audio at 48 kHz, +encoded as six Opus packets in a live WebM Segment with unknown size. +It was generated with ffmpeg 9.0.2: + +```sh +ffmpeg -f lavfi -i sine=frequency=440:sample_rate=48000 -t 0.1 \ + -c:a libopus -f webm -live 1 -map_metadata -1 -fflags +bitexact opus.webm +``` + +The importer test checks constructor and streaming error propagation, +then reads all six emitted frames across one-byte and larger chunk boundaries. diff --git a/rs/moq-mux/src/container/mkv/test_data/opus.webm b/rs/moq-mux/src/container/mkv/test_data/opus.webm new file mode 100644 index 00000000000..18c3c562adf Binary files /dev/null and b/rs/moq-mux/src/container/mkv/test_data/opus.webm differ