From c5087efded8be0713044034c9466943acbbc67e0 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 09:25:50 -0800 Subject: [PATCH 01/10] chore(quest): claim quest/m2/api-shape/media.md Quest-Claim: b8382dd5eff82ef0 From 412d93b12f307a679fdc1d3fae417934e5bcd2e6 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Sun, 8 Nov 2026 09:51:01 -0800 Subject: [PATCH 02/10] refactor(mux,hang,json,stats)!: media crates' small surface fixes (wip) Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + drafts/draft-lcurley-moq-hang.md | 4 +- js/hang/src/echo.ts | 3 - js/hang/src/stats.test.ts | 5 +- rs/hang/src/catalog/clock.rs | 16 +-- rs/hang/src/catalog/root.rs | 3 +- rs/hang/src/catalog/video/color.rs | 6 +- rs/hang/src/echo.rs | 4 - rs/hang/src/snapshot.rs | 20 ++- rs/justfile | 2 +- rs/kio/src/clock/wall.rs | 3 + rs/moq-audio/src/aac.rs | 6 +- rs/moq-audio/src/encode/encoder.rs | 6 +- rs/moq-c/src/video.rs | 16 +-- rs/moq-cli/Cargo.toml | 2 + rs/moq-cli/src/play/media.rs | 2 +- rs/moq-cli/src/subscribe.rs | 13 +- rs/moq-cli/tests/integration/export.rs | 1 - rs/moq-ffi/src/test.rs | 6 +- rs/moq-ffi/src/video.rs | 16 +-- rs/moq-gst/src/sink/imp.rs | 8 +- rs/moq-hls/src/export/mod.rs | 20 ++- rs/moq-json/Cargo.toml | 4 + rs/moq-json/benches/codec.rs | 2 +- rs/moq-json/src/diff.rs | 8 +- rs/moq-json/src/lib.rs | 8 +- rs/moq-mux/benches/containers.rs | 4 +- rs/moq-mux/src/catalog/estimate.rs | 16 +-- rs/moq-mux/src/catalog/mod.rs | 3 +- rs/moq-mux/src/catalog/msf/consumer.rs | 8 +- rs/moq-mux/src/catalog/producer.rs | 11 +- rs/moq-mux/src/catalog/tracks.rs | 2 +- rs/moq-mux/src/clock.rs | 11 +- rs/moq-mux/src/codec/aac/mod.rs | 12 ++ rs/moq-mux/src/codec/annexb/export.rs | 20 +-- rs/moq-mux/src/codec/color.rs | 8 +- rs/moq-mux/src/codec/h264/export.rs | 15 +-- rs/moq-mux/src/codec/h264/import.rs | 2 +- rs/moq-mux/src/codec/h264/mod.rs | 4 +- rs/moq-mux/src/codec/h265/export.rs | 15 +-- rs/moq-mux/src/codec/h265/import.rs | 2 +- rs/moq-mux/src/codec/h265/mod.rs | 2 +- rs/moq-mux/src/container/consumer.rs | 20 ++- rs/moq-mux/src/container/flv/export.rs | 126 ++++++++++--------- rs/moq-mux/src/container/flv/export_test.rs | 22 ++-- rs/moq-mux/src/container/flv/mod.rs | 49 ++++++++ rs/moq-mux/src/container/fmp4/export.rs | 31 ++--- rs/moq-mux/src/container/fmp4/export_test.rs | 52 ++++---- rs/moq-mux/src/container/fmp4/import.rs | 34 ++--- rs/moq-mux/src/container/fmp4/import_test.rs | 2 +- rs/moq-mux/src/container/fmp4/metadata.rs | 36 +++--- rs/moq-mux/src/container/fmp4/mod.rs | 75 ++++++----- rs/moq-mux/src/container/fmp4/muxer.rs | 85 ------------- rs/moq-mux/src/container/fmp4/text.rs | 4 +- rs/moq-mux/src/container/mkv/export.rs | 26 ++-- rs/moq-mux/src/container/mkv/export_test.rs | 22 ++-- rs/moq-mux/src/container/mkv/import.rs | 4 +- rs/moq-mux/src/container/mkv/import_test.rs | 2 +- rs/moq-mux/src/container/mp4.rs | 27 ++-- rs/moq-mux/src/container/source.rs | 78 ++++++------ rs/moq-mux/src/container/ts/export.rs | 20 +-- rs/moq-mux/src/container/ts/export_test.rs | 2 +- rs/moq-mux/src/container/ts/release.rs | 2 +- rs/moq-mux/src/error.rs | 11 +- rs/moq-mux/src/rate.rs | 26 ++-- rs/moq-net/src/model/clock.rs | 5 +- rs/moq-rtmp/src/dial.rs | 2 +- rs/moq-rtmp/src/server.rs | 2 +- rs/moq-stats/benches/aggregate.rs | 3 +- rs/moq-stats/benches/decode.rs | 3 +- rs/moq-stats/src/consume.rs | 4 +- rs/moq-stats/src/lib.rs | 7 +- rs/moq-video/src/color.rs | 2 +- rs/moq-video/src/decode/consumer.rs | 2 +- rs/moq-video/src/decode/decoder.rs | 2 +- sh/gh/nightly.sh | 2 +- 76 files changed, 548 insertions(+), 562 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2bbc0785f5a..aa9b58f42ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4497,6 +4497,7 @@ dependencies = [ "moq-transcode", "moq-video", "moq-webrtc", + "mp4-atom", "mpeg2ts", "object_store", "percent-encoding", diff --git a/drafts/draft-lcurley-moq-hang.md b/drafts/draft-lcurley-moq-hang.md index fa80b68944b..e487a575501 100644 --- a/drafts/draft-lcurley-moq-hang.md +++ b/drafts/draft-lcurley-moq-hang.md @@ -875,7 +875,7 @@ type PublisherRendition = { type ViewerRendition = { frames?: number, bytes?: number, decoded?: number, late?: number, errors?: number, stalls?: number, stalled?: number, underruns?: number, - arrival?: { timestamp: number, wall: number }, latency?: number, + arrival?: { timestamp: number, wall: number }, } type Transport = { rtt?: number, rate?: number, lostBytes?: number, lostPackets?: number, age?: number, @@ -896,7 +896,7 @@ An empty rendition entry is valid and represents zero cumulative counters; its I Publisher `keyframes` counts sent keyframes; `skipped` counts frames skipped before sending. Viewer `decoded`, `late`, and `errors` count decoded frames, frames too late for playout, and decode errors. `stalls` counts playout stalls, `stalled` is cumulative stalled duration, and `underruns` counts audio buffer underruns. -`bitrate` is the latest target encoder bitrate, `latency` the latest playout latency, and `arrival` pairs the newest received media timestamp with its arrival wall time. +`bitrate` is the latest target encoder bitrate, and `arrival` pairs the newest received media timestamp with its arrival wall time. Durations and media timestamps are milliseconds; `arrival.wall` is milliseconds since the MoQ epoch (2020-01-01T00:00:00Z). Sub-millisecond durations and timestamps are floored when encoded. Rates are bits per second. diff --git a/js/hang/src/echo.ts b/js/hang/src/echo.ts index ce50ed7fff5..d92476a7619 100644 --- a/js/hang/src/echo.ts +++ b/js/hang/src/echo.ts @@ -8,7 +8,6 @@ import type { Path } from "@moq/net"; import * as z from "zod/mini"; import { u53Schema } from "./catalog/integers"; import { renditions } from "./catalog/section"; -import { measurementSchema } from "./snapshot"; import { TransportSchema } from "./stats"; /** The newest received media timestamp paired with its arrival wall time. */ @@ -46,8 +45,6 @@ export const RenditionSchema = z.object({ z.transform((value) => value ?? undefined), ), ), - // Latest playout latency in milliseconds; absent when unavailable. - latency: measurementSchema, }); /** One rendition's delivery counters and latest playout measurements. */ export type Rendition = z.infer; diff --git a/js/hang/src/stats.test.ts b/js/hang/src/stats.test.ts index c1507acb572..17157ce18e5 100644 --- a/js/hang/src/stats.test.ts +++ b/js/hang/src/stats.test.ts @@ -25,13 +25,11 @@ test("missing rendition counters mean zero while parsing preserves omission", () expect(stats.renditions?.reset.frames ?? 0).toBe(0); expect(echo.renditions?.reset.stalled ?? 0).toBe(0); expect(stats.renditions?.reset.bitrate).toBeUndefined(); - expect(echo.renditions?.reset.latency).toBeUndefined(); expect(stats.transport).toBeUndefined(); expect(Object.hasOwn(stats, "transport")).toBe(false); expect(Object.hasOwn(stats.renditions?.reset ?? {}, "frames")).toBe(false); expect(Object.hasOwn(stats.renditions?.reset ?? {}, "bitrate")).toBe(false); expect(Object.hasOwn(echo.renditions?.reset ?? {}, "arrival")).toBe(false); - expect(Object.hasOwn(echo.renditions?.reset ?? {}, "latency")).toBe(false); expect(Stats.SnapshotSchema.parse({})).toEqual({}); expect(Echo.SnapshotSchema.parse({})).toEqual({}); }); @@ -57,7 +55,7 @@ test("optional null measurements mean unavailable rather than measured zero", () ), ), ).toEqual({ transport: {}, renditions: { hd: {} } }); - expect(JSON.parse(JSON.stringify(Echo.RenditionSchema.parse({ latency: null, arrival: null })))).toEqual({}); + expect(JSON.parse(JSON.stringify(Echo.RenditionSchema.parse({ arrival: null })))).toEqual({}); }); test("unknown track fields are ignored and flattened snapshot extensions survive", () => { @@ -77,7 +75,6 @@ test("shared invalid counters refuse instead of rounding", async () => { for (const value of cases) expect(() => Stats.RenditionSchema.parse(value)).toThrow(); for (const value of [ { stalled: -1 }, - { latency: 0.5 }, { arrival: { timestamp: Number.MAX_SAFE_INTEGER + 1, wall: 0 } }, { arrival: { timestamp: 0, wall: -1 } }, { arrival: {} }, diff --git a/rs/hang/src/catalog/clock.rs b/rs/hang/src/catalog/clock.rs index 85c0fafb266..7a5df5c44ff 100644 --- a/rs/hang/src/catalog/clock.rs +++ b/rs/hang/src/catalog/clock.rs @@ -2,9 +2,6 @@ use serde::{Deserialize, Deserializer, Serialize}; use crate::Result; -/// The moq epoch (2020-01-01T00:00:00Z) in Unix-epoch milliseconds. -pub const MOQ_EPOCH_UNIX_MILLIS: u64 = 1_577_836_800_000; - /// The largest integer JSON preserves exactly (2^53 - 1). pub const MAX_SAFE_INTEGER: u64 = 9_007_199_254_740_991; @@ -38,7 +35,7 @@ struct Wire { /// The broadcast's one continuous clock, advertised at the catalog root. /// /// [`wall`](Self::wall) is the wall-clock time of PTS zero since the moq epoch -/// ([`MOQ_EPOCH_UNIX_MILLIS`], 2020-01-01). Its embedded timescale is serialized beside it as +/// ([`Wall::MOQ_EPOCH`](kio::clock::Wall::MOQ_EPOCH), 2020-01-01). Its embedded timescale is serialized beside it as /// `{ wall, timescale }`, preserving the catalog wire shape while keeping the pair inseparable in /// Rust. Every media track and every HLS record refer to this mapping after timescale conversion. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -90,9 +87,8 @@ impl Clock { let nanos = (num % den).checked_mul(1_000_000_000).ok_or_else(overflow)? / den; let since_moq_epoch = std::time::Duration::new(secs, nanos as u32); // A catalog can name a wall beyond the clock's nanosecond range. - kio::clock::Wall::UNIX_EPOCH - .checked_add(std::time::Duration::from_millis(MOQ_EPOCH_UNIX_MILLIS)) - .and_then(|moq_epoch| moq_epoch.checked_add(since_moq_epoch)) + kio::clock::Wall::MOQ_EPOCH + .checked_add(since_moq_epoch) .ok_or_else(overflow) } } @@ -187,13 +183,13 @@ mod test { #[test] fn wall_clock_converts_across_timescales() { let clock = Clock::new(moq_net::Timestamp::from_micros(1_000_000).unwrap()).unwrap(); - let epoch = kio::clock::Wall::UNIX_EPOCH + std::time::Duration::from_millis(MOQ_EPOCH_UNIX_MILLIS + 1_000); + let epoch = kio::clock::Wall::MOQ_EPOCH + std::time::Duration::from_millis(1_000); assert_eq!( clock.wall_clock(moq_net::Timestamp::from_millis(0).unwrap()).unwrap(), epoch ); - let second = kio::clock::Wall::UNIX_EPOCH + std::time::Duration::from_millis(MOQ_EPOCH_UNIX_MILLIS + 2_000); + let second = kio::clock::Wall::MOQ_EPOCH + std::time::Duration::from_millis(2_000); assert_eq!( clock .wall_clock(moq_net::Timestamp::from_millis(1000).unwrap()) @@ -217,7 +213,7 @@ mod test { /// Regression: the result was floored to the millisecond, dropping the sub-millisecond part. #[test] fn wall_clock_keeps_sub_millisecond_precision() { - let moq_epoch = kio::clock::Wall::UNIX_EPOCH + std::time::Duration::from_millis(MOQ_EPOCH_UNIX_MILLIS); + let moq_epoch = kio::clock::Wall::MOQ_EPOCH; let micros = |v| moq_net::Timestamp::from_micros(v).unwrap(); let nanos = std::time::Duration::from_nanos; diff --git a/rs/hang/src/catalog/root.rs b/rs/hang/src/catalog/root.rs index a3328a79a6a..888f554f109 100644 --- a/rs/hang/src/catalog/root.rs +++ b/rs/hang/src/catalog/root.rs @@ -1230,8 +1230,7 @@ mod test { assert_eq!(catalog.media.interval(), Some(std::time::Duration::from_secs(2))); // PTS 2000 ms lands 2s after the wall epoch, whatever the track timescale. - let expected = kio::clock::Wall::UNIX_EPOCH - + std::time::Duration::from_millis(crate::catalog::MOQ_EPOCH_UNIX_MILLIS + 1_751_846_402_000); + let expected = kio::clock::Wall::MOQ_EPOCH + std::time::Duration::from_millis(1_751_846_402_000); assert_eq!( clock .wall_clock(moq_net::Timestamp::from_millis(2000).unwrap()) diff --git a/rs/hang/src/catalog/video/color.rs b/rs/hang/src/catalog/video/color.rs index c90fe1dd365..3215072d6ac 100644 --- a/rs/hang/src/catalog/video/color.rs +++ b/rs/hang/src/catalog/video/color.rs @@ -22,7 +22,7 @@ pub struct Color { pub matrix: Option, /// The numeric range of the samples. - pub range: Option, + pub range: Option, /// The display the content was mastered on, as SMPTE ST 2086. pub mastering: Option, @@ -34,7 +34,7 @@ pub struct Color { /// The numeric range of the samples, as H.273's `VideoFullRangeFlag`. #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)] #[serde(rename_all = "lowercase")] -pub enum Range { +pub enum ColorRange { /// Studio swing: 8-bit luma spans 16..235 and chroma 16..240. Limited, /// Full swing: every 8-bit code is used. @@ -88,7 +88,7 @@ mod test { primaries: Some(9), transfer: Some(16), matrix: Some(9), - range: Some(Range::Limited), + range: Some(ColorRange::Limited), mastering: Some(Mastering { red: Chromaticity { x: 0.708, y: 0.292 }, green: Chromaticity { x: 0.17, y: 0.797 }, diff --git a/rs/hang/src/echo.rs b/rs/hang/src/echo.rs index db483066af0..adead0cdeb1 100644 --- a/rs/hang/src/echo.rs +++ b/rs/hang/src/echo.rs @@ -61,10 +61,6 @@ pub struct Rendition { /// The newest media timestamp received and its arrival wall time. #[serde(skip_serializing_if = "Option::is_none")] pub arrival: Option, - /// Latest playout latency. - #[serde_as(as = "Option")] - #[serde(skip_serializing_if = "Option::is_none")] - pub latency: Option, } /// The newest received media timestamp paired with its arrival wall time. diff --git a/rs/hang/src/snapshot.rs b/rs/hang/src/snapshot.rs index 8641a094bad..41e390ecdec 100644 --- a/rs/hang/src/snapshot.rs +++ b/rs/hang/src/snapshot.rs @@ -50,20 +50,17 @@ impl<'de> DeserializeAs<'de, moq_net::Timestamp> for Millis { } pub(crate) struct Wall; -fn epoch() -> kio::clock::Wall { - kio::clock::Wall::UNIX_EPOCH + Duration::from_millis(crate::catalog::MOQ_EPOCH_UNIX_MILLIS) -} impl SerializeAs for Wall { fn serialize_as(value: &kio::clock::Wall, serializer: S) -> Result { let duration = value - .checked_duration_since(epoch()) + .checked_duration_since(kio::clock::Wall::MOQ_EPOCH) .ok_or_else(|| serde::ser::Error::custom("snapshot wall is before the MoQ epoch"))?; Millis::serialize_as(&duration, serializer) } } impl<'de> DeserializeAs<'de, kio::clock::Wall> for Wall { fn deserialize_as>(deserializer: D) -> Result { - epoch() + kio::clock::Wall::MOQ_EPOCH .checked_add(Millis::deserialize_as(deserializer)?) .ok_or_else(|| serde::de::Error::custom("snapshot wall clock overflow")) } @@ -92,14 +89,15 @@ mod tests { #[serde_as(as = "Wall")] wall: kio::clock::Wall, } - let last_millis = (u64::MAX / 1_000_000) - crate::catalog::MOQ_EPOCH_UNIX_MILLIS; + let epoch_millis = kio::clock::Wall::MOQ_EPOCH.saturating_duration_since(kio::clock::Wall::UNIX_EPOCH).as_millis() as u64; + let last_millis = (u64::MAX / 1_000_000) - epoch_millis; let last = format!(r#"{{"wall":{last_millis}}}"#); let value: Value = serde_json::from_str(&last).unwrap(); assert_eq!(serde_json::to_string(&value).unwrap(), last); assert!(serde_json::from_str::(&format!(r#"{{"wall":{}}}"#, last_millis + 1)).is_err()); assert!( serde_json::to_string(&Value { - wall: epoch() - Duration::from_nanos(1) + wall: kio::clock::Wall::MOQ_EPOCH - Duration::from_nanos(1) }) .is_err() ); @@ -141,7 +139,7 @@ mod tests { assert_eq!(stats.transport.lost_packets, None); let echo: echo::Snapshot = serde_json::from_str(r#"{"renditions":{"reset":{}}}"#).unwrap(); assert_eq!(echo.renditions["reset"].stalled, Duration::ZERO); - assert_eq!(echo.renditions["reset"].latency, None); + assert_eq!(echo.renditions["reset"].arrival, None); } #[test] @@ -152,8 +150,7 @@ mod tests { assert_eq!(stats.transport.rtt, None); assert_eq!(stats.renditions["hd"].bitrate, None); let echo: echo::Snapshot = - serde_json::from_str(r#"{"renditions":{"hd":{"latency":null,"arrival":null}}}"#).unwrap(); - assert_eq!(echo.renditions["hd"].latency, None); + serde_json::from_str(r#"{"renditions":{"hd":{"arrival":null}}}"#).unwrap(); assert_eq!(echo.renditions["hd"].arrival, None); } @@ -182,7 +179,6 @@ mod tests { } for input in [ r#"{"stalled":-1}"#, - r#"{"latency":0.5}"#, r#"{"arrival":{"timestamp":9007199254740992,"wall":0}}"#, r#"{"arrival":{"timestamp":0,"wall":-1}}"#, r#"{"arrival":{}}"#, @@ -211,7 +207,7 @@ mod tests { let arrival = report.renditions["hd"].arrival.as_ref().unwrap(); assert_eq!(arrival.timestamp, moq_net::Timestamp::from_millis(30_000).unwrap()); assert_eq!( - arrival.wall.checked_duration_since(epoch()).unwrap(), + arrival.wall.checked_duration_since(kio::clock::Wall::MOQ_EPOCH).unwrap(), Duration::from_millis(175_184_640_000) ); assert_eq!(report.renditions["hd"].stalled, Duration::from_millis(120)); diff --git a/rs/justfile b/rs/justfile index 23a8ab90add..7c61c6240f5 100644 --- a/rs/justfile +++ b/rs/justfile @@ -567,7 +567,7 @@ gst-smoke *args: codspeed: VALGRIND_BUG_494162=1 cargo codspeed build --locked -m simulation \ -p kio -p moq-audio -p moq-crypto -p moq-json -p moq-mux -p moq-net -p moq-pattern -p moq-quic -p moq-rtmp \ - --features moq-audio/bus,moq-net/fuzz,moq-quic/fuzz \ + --features moq-audio/bus,moq-json/bench,moq-net/fuzz,moq-quic/fuzz \ --bench tasks --bench waiter --bench bus --bench protect --bench codec --bench table \ --bench catalog --bench containers --bench decode_clock --bench export --bench jitter \ --bench parameter_sets --bench timeline --bench ts_programs --bench ts_schedule --bench ts_video \ diff --git a/rs/kio/src/clock/wall.rs b/rs/kio/src/clock/wall.rs index 7988a582568..5d3c7881c72 100644 --- a/rs/kio/src/clock/wall.rs +++ b/rs/kio/src/clock/wall.rs @@ -18,6 +18,9 @@ impl Wall { /// 1970-01-01T00:00:00Z. pub const UNIX_EPOCH: Self = Self(0); + /// 2020-01-01T00:00:00Z, the origin MoQ media timestamps and wall-clock fields count from. + pub const MOQ_EPOCH: Self = Self::from_unix_secs(1_577_836_800); + /// `secs` whole seconds after the Unix epoch, for constants. pub(crate) const fn from_unix_secs(secs: u64) -> Self { Self(secs * 1_000_000_000) diff --git a/rs/moq-audio/src/aac.rs b/rs/moq-audio/src/aac.rs index b16668b9360..e01b96cc0f6 100644 --- a/rs/moq-audio/src/aac.rs +++ b/rs/moq-audio/src/aac.rs @@ -72,11 +72,7 @@ pub(crate) fn description(catalog: &hang::catalog::AudioConfig, profile: u8) -> )); } - let config = moq_mux::codec::aac::Config { - profile, - sample_rate: catalog.sample_rate, - channel_count: catalog.channel_count, - }; + let config = moq_mux::codec::aac::Config::new(profile, catalog.sample_rate, catalog.channel_count); // Synthesis still drops bits the config cannot hold: the object type is // masked to five bits, and the sample rate to 24. Encode refuses a channel diff --git a/rs/moq-audio/src/encode/encoder.rs b/rs/moq-audio/src/encode/encoder.rs index c33252db14e..edc7a6885dc 100644 --- a/rs/moq-audio/src/encode/encoder.rs +++ b/rs/moq-audio/src/encode/encoder.rs @@ -277,11 +277,7 @@ fn aac_config(config: &Config) -> Result { ))); } - Ok(moq_mux::codec::aac::Config { - profile: AAC_LC, - sample_rate, - channel_count: layout.channels(), - }) + Ok(moq_mux::codec::aac::Config::new(AAC_LC, sample_rate, layout.channels())) } /// Audio encoder over codec-sized interleaved `f32` PCM. diff --git a/rs/moq-c/src/video.rs b/rs/moq-c/src/video.rs index 6b44b18a3d1..c1007a450ec 100644 --- a/rs/moq-c/src/video.rs +++ b/rs/moq-c/src/video.rs @@ -314,7 +314,7 @@ async fn follow_reservation( ceiling: moq_net::bandwidth::Rate, ) { let noop = kio::Waiter::noop(); - let mut follower = moq_mux::rate::Follower::new(consumer, video_policy(ceiling, lower_ceiling(&lower, &noop))); + let mut follower = moq_mux::rate::Grant::new(consumer, video_policy(ceiling, lower_ceiling(&lower, &noop))); loop { // Both ceilings are read on every wake, before the follower filters a grant against // them. This one's `moq_encode_video_bitrate` moves its reservation after moving its @@ -351,7 +351,7 @@ async fn follow_reservation( /// `moq_encode_video_bitrate` already applied, instead. Returns the target applied, or /// `Err` with the name of an encoder that cannot follow at all. fn retune( - follower: &mut moq_mux::rate::Follower, + follower: &mut moq_mux::rate::Grant, policy: moq_mux::rate::Policy, target: moq_mux::rate::Target, apply: impl FnOnce(moq_mux::rate::Target) -> Result<(), moq_video::Error>, @@ -1549,7 +1549,7 @@ mod tests { mod follow_tests { use std::time::Duration; - use moq_mux::rate::{Follower, Policy, Target}; + use moq_mux::rate::{Grant, Policy, Target}; use moq_net::bandwidth::Rate; use super::*; @@ -1557,7 +1557,7 @@ mod follow_tests { /// Retune through `set`, which only ever sees an enabled target: these policies set no /// lower rendition, so nothing disables. fn rate( - follower: &mut Follower, + follower: &mut Grant, ceiling: Rate, target: Target, set: impl FnOnce(Rate) -> Result<(), moq_video::Error>, @@ -1578,7 +1578,7 @@ mod follow_tests { policy }; let grant = moq_net::bandwidth::Producer::new(); - let mut follower = Follower::new(grant.consume(), policy(Rate::from_mbps(1))); + let mut follower = Grant::new(grant.consume(), policy(Rate::from_mbps(1))); grant.set(Some(Rate::from_mbps(2))).unwrap(); let dropped = follower.next().await.unwrap(); let moved = retune(&mut follower, policy(Rate::from_kbps(900)), dropped, |_| { @@ -1600,7 +1600,7 @@ mod follow_tests { } /// Retune at every target until the follower goes a minute without one. - async fn quiet(follower: &mut Follower, ceiling: Rate, mut set: impl FnMut(Rate) -> Result<(), moq_video::Error>) { + async fn quiet(follower: &mut Grant, ceiling: Rate, mut set: impl FnMut(Rate) -> Result<(), moq_video::Error>) { while let Ok(bitrate) = kio::clock::timeout(Duration::from_secs(60), follower.next()).await { rate(follower, ceiling, bitrate.unwrap(), &mut set).unwrap(); } @@ -1612,7 +1612,7 @@ mod follow_tests { async fn a_quiet_grant_retries_a_failed_raise() { let ceiling = Rate::from_mbps(4); let grant = moq_net::bandwidth::Producer::new(); - let mut follower = Follower::new(grant.consume(), Policy::new(ceiling)); + let mut follower = Grant::new(grant.consume(), Policy::new(ceiling)); let mut applied = Vec::new(); let mut refuse = Some(ceiling); let mut set = |bitrate| { @@ -1642,7 +1642,7 @@ mod follow_tests { let old = Rate::from_mbps(4); let lowered = Rate::from_mbps(2); let grant = moq_net::bandwidth::Producer::new(); - let mut follower = Follower::new(grant.consume(), Policy::new(old)); + let mut follower = Grant::new(grant.consume(), Policy::new(old)); let applied = std::cell::RefCell::new(Vec::new()); let set = |bitrate| { applied.borrow_mut().push(bitrate); diff --git a/rs/moq-cli/Cargo.toml b/rs/moq-cli/Cargo.toml index a08da3ed7cd..701901ab48d 100644 --- a/rs/moq-cli/Cargo.toml +++ b/rs/moq-cli/Cargo.toml @@ -155,6 +155,8 @@ moq-relay = { path = "../moq-relay", default-features = false, features = ["test moq-rtsp = { path = "../moq-rtsp", features = ["test-support"] } # Applies the finite bufferbloat queue to redundant-ingest acceptance tests. moq-shaper = { path = "../moq-shaper" } +# Reads back the boxes of an exported MP4. +mp4-atom = { workspace = true } # Writes the PAT and PMTs, CRCs included, of the two-program input `import ts --program all` splits. mpeg2ts = "0.6.0" tempfile = { workspace = true } diff --git a/rs/moq-cli/src/play/media.rs b/rs/moq-cli/src/play/media.rs index 402a8bd2eca..175cc76be4e 100644 --- a/rs/moq-cli/src/play/media.rs +++ b/rs/moq-cli/src/play/media.rs @@ -285,7 +285,7 @@ impl Media { } let format = catalog::hang::Container::try_from(config)?; // The playout delay is the target latency, and the max how late a group may be. - let track = moq_mux::container::Consumer::new(subscriber, format).with_delay(delay); + let track = moq_mux::container::Playout::new(delay, Duration::ZERO).consumer(subscriber, format); Ok::<_, anyhow::Error>((track, decoder)) }; let opened = async { diff --git a/rs/moq-cli/src/subscribe.rs b/rs/moq-cli/src/subscribe.rs index ef387b7cd75..a056d603f67 100644 --- a/rs/moq-cli/src/subscribe.rs +++ b/rs/moq-cli/src/subscribe.rs @@ -2,6 +2,7 @@ use std::time::Duration; use anyhow::Context; use hang::catalog::{AudioCodecKind, VideoCodecKind}; +use hang::moq_net::track::Delay; use moq_mux::catalog::{self, CatalogFormat, Stream}; use moq_mux::select; use tokio::io::{AsyncSeekExt, AsyncWriteExt}; @@ -362,7 +363,7 @@ impl Subscribe { // yields moof+mdat fragments in timestamp order across tracks. let stream = self.stream().await?; let mut fmp4 = moq_mux::container::fmp4::Export::new(self.source, stream) - .with_max_delay(self.args.max_delay) + .with_delay(Delay::default().with_max(self.args.max_delay)) .with_fragment_duration(self.args.fragment_duration); while let Some(chunk) = fmp4.next().await? { @@ -383,7 +384,7 @@ impl Subscribe { () = finish.cancelled() => return Ok(()), }; let mut mp4 = moq_mux::container::mp4::Export::new(self.source, stream) - .with_max_delay(self.args.max_delay) + .with_delay(Delay::default().with_max(self.args.max_delay)) .with_fragment_duration(self.args.fragment_duration); let mut file = tokio::fs::File::create_new(&path) .await @@ -430,7 +431,7 @@ impl Subscribe { // shape internally (synthesizing avcC/hvcC from inline parameter sets). let stream = self.stream().await?; let mut mkv = moq_mux::container::mkv::Export::new(self.source, stream) - .with_delay(self.args.max_delay) + .with_delay(Delay::default().with_min(self.args.max_delay).with_max(self.args.max_delay)) .with_fragment_duration(self.args.fragment_duration); while let Some(chunk) = mkv.next().await? { @@ -445,7 +446,7 @@ impl Subscribe { let mut stdout = tokio::io::stdout(); let stream = self.stream().await?; - let mut h264 = moq_mux::codec::h264::Export::new(self.source, stream).with_max_delay(self.args.max_delay); + let mut h264 = moq_mux::codec::h264::Export::new(self.source, stream).with_delay(Delay::default().with_max(self.args.max_delay)); while let Some(chunk) = h264.next().await? { stdout.write_all(&chunk).await?; @@ -459,7 +460,7 @@ impl Subscribe { let mut stdout = tokio::io::stdout(); let stream = self.stream().await?; - let mut h265 = moq_mux::codec::h265::Export::new(self.source, stream).with_max_delay(self.args.max_delay); + let mut h265 = moq_mux::codec::h265::Export::new(self.source, stream).with_delay(Delay::default().with_max(self.args.max_delay)); while let Some(chunk) = h265.next().await? { stdout.write_all(&chunk).await?; @@ -587,7 +588,7 @@ impl Subscribe { // internally (synthesizing avcC from inline parameter sets). Only H.264 video // and AAC audio are supported; `fragment_duration` does not apply to FLV. let stream = self.stream().await?; - let mut flv = moq_mux::container::flv::Export::new(self.source, stream).with_delay(self.args.max_delay); + let mut flv = moq_mux::container::flv::Export::new(self.source, stream).with_delay(Delay::default().with_min(self.args.max_delay).with_max(self.args.max_delay)); while let Some(chunk) = flv.next().await? { stdout.write_all(&chunk).await?; diff --git a/rs/moq-cli/tests/integration/export.rs b/rs/moq-cli/tests/integration/export.rs index fb1cdc45559..34bfc952032 100644 --- a/rs/moq-cli/tests/integration/export.rs +++ b/rs/moq-cli/tests/integration/export.rs @@ -17,7 +17,6 @@ use std::time::Duration; use kio::clock::Instant; use hang::moq_net; -use moq_mux::container::fmp4::mp4_atom; use moq_mux::container::ts; use mpeg2ts::ts::{ReadTsPacket, TsPacketReader, TsPayload}; use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader}; diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index d2473f8919b..d4cffe75d1d 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -3244,11 +3244,7 @@ async fn media_track_activity_and_name() { #[tokio::test] async fn publish_media_aac_populates_description() { let broadcast = MoqBroadcastProducer::new().unwrap(); - let config = moq_mux::codec::aac::Config { - profile: 2, - sample_rate: 44_100, - channel_count: 2, - }; + let config = moq_mux::codec::aac::Config::new(2, 44_100, 2); let init = config.encode().unwrap(); let _media = MoqMediaTrackProducer::audio( &broadcast, diff --git a/rs/moq-ffi/src/video.rs b/rs/moq-ffi/src/video.rs index 2a6945dd17a..b2feef85da0 100644 --- a/rs/moq-ffi/src/video.rs +++ b/rs/moq-ffi/src/video.rs @@ -947,7 +947,7 @@ async fn follow_reservation(followed: Followed) { applied, } = followed; let noop = kio::Waiter::noop(); - let mut follower = moq_mux::rate::Follower::new(consumer, video_policy(ceiling, lower_ceiling(&lower, &noop))); + let mut follower = moq_mux::rate::Grant::new(consumer, video_policy(ceiling, lower_ceiling(&lower, &noop))); loop { // Both ceilings are read on every wake, before the follower filters a grant against // them. This one's `set_bitrate` moves its reservation after moving its ceiling, @@ -987,7 +987,7 @@ async fn follow_reservation(followed: Followed) { /// `MoqVideoProducer::set_bitrate` already applied, instead. Returns the target applied, /// or `Err` with the name of an encoder that cannot follow at all. fn retune( - follower: &mut moq_mux::rate::Follower, + follower: &mut moq_mux::rate::Grant, policy: moq_mux::rate::Policy, target: moq_mux::rate::Target, apply: impl FnOnce(moq_mux::rate::Target) -> Result<(), moq_video::Error>, @@ -1539,7 +1539,7 @@ mod finish_tests { mod follow_tests { use std::time::Duration; - use moq_mux::rate::{Follower, Policy, Target}; + use moq_mux::rate::{Grant, Policy, Target}; use moq_net::bandwidth::Rate; use super::*; @@ -1547,7 +1547,7 @@ mod follow_tests { /// Retune through `set`, which only ever sees an enabled target: these policies set no /// lower rendition, so nothing disables. fn rate( - follower: &mut Follower, + follower: &mut Grant, ceiling: Rate, target: Target, set: impl FnOnce(Rate) -> Result<(), moq_video::Error>, @@ -1568,7 +1568,7 @@ mod follow_tests { policy }; let grant = moq_net::bandwidth::Producer::new(); - let mut follower = Follower::new(grant.consume(), policy(Rate::from_mbps(1))); + let mut follower = Grant::new(grant.consume(), policy(Rate::from_mbps(1))); grant.set(Some(Rate::from_mbps(2))).unwrap(); let dropped = follower.next().await.unwrap(); let moved = retune(&mut follower, policy(Rate::from_kbps(900)), dropped, |_| { @@ -1590,7 +1590,7 @@ mod follow_tests { } /// Retune at every target until the follower goes a minute without one. - async fn quiet(follower: &mut Follower, ceiling: Rate, mut set: impl FnMut(Rate) -> Result<(), moq_video::Error>) { + async fn quiet(follower: &mut Grant, ceiling: Rate, mut set: impl FnMut(Rate) -> Result<(), moq_video::Error>) { while let Ok(bitrate) = kio::clock::timeout(Duration::from_secs(60), follower.next()).await { rate(follower, ceiling, bitrate.unwrap(), &mut set).unwrap(); } @@ -1602,7 +1602,7 @@ mod follow_tests { async fn a_quiet_grant_retries_a_failed_raise() { let ceiling = Rate::from_mbps(4); let grant = moq_net::bandwidth::Producer::new(); - let mut follower = Follower::new(grant.consume(), Policy::new(ceiling)); + let mut follower = Grant::new(grant.consume(), Policy::new(ceiling)); let mut applied = Vec::new(); let mut refuse = Some(ceiling); let mut set = |bitrate| { @@ -1632,7 +1632,7 @@ mod follow_tests { let old = Rate::from_mbps(4); let lowered = Rate::from_mbps(2); let grant = moq_net::bandwidth::Producer::new(); - let mut follower = Follower::new(grant.consume(), Policy::new(old)); + let mut follower = Grant::new(grant.consume(), Policy::new(old)); let applied = std::cell::RefCell::new(Vec::new()); let set = |bitrate| { applied.borrow_mut().push(bitrate); diff --git a/rs/moq-gst/src/sink/imp.rs b/rs/moq-gst/src/sink/imp.rs index f27e66b3a58..b0a287aec04 100644 --- a/rs/moq-gst/src/sink/imp.rs +++ b/rs/moq-gst/src/sink/imp.rs @@ -1492,7 +1492,9 @@ mod tests { }; assert_eq!( first.media.clock.unwrap().wall.as_micros(), - ((1_735_689_600 - hang::catalog::MOQ_EPOCH_UNIX_MILLIS / 1000) * 1_000_000) as u128, + (kio::clock::Wall::UNIX_EPOCH + std::time::Duration::from_secs(1_735_689_600)) + .saturating_duration_since(kio::clock::Wall::MOQ_EPOCH) + .as_micros(), ); } @@ -1533,7 +1535,9 @@ mod tests { }; assert_eq!( clock.wall.as_micros(), - ((zero - hang::catalog::MOQ_EPOCH_UNIX_MILLIS / 1000) * 1_000_000) as u128 + (kio::clock::Wall::UNIX_EPOCH + std::time::Duration::from_secs(zero)) + .saturating_duration_since(kio::clock::Wall::MOQ_EPOCH) + .as_micros() ); for (name, kind, expected) in [ ("video", moq_mux::container::Kind::Video, 3), diff --git a/rs/moq-hls/src/export/mod.rs b/rs/moq-hls/src/export/mod.rs index 143f1d1028a..d3747eb06bd 100644 --- a/rs/moq-hls/src/export/mod.rs +++ b/rs/moq-hls/src/export/mod.rs @@ -840,9 +840,17 @@ mod tests { /// The average bitrate encoded in a synthesized AAC init segment. fn aac_avg_bitrate(init: &bytes::Bytes) -> u32 { - let wire = moq_mux::container::fmp4::Wire::from_init(init).unwrap(); - let codec = &wire.trak().mdia.minf.stbl.stsd.codecs[0]; - let moq_mux::container::fmp4::mp4_atom::Codec::Mp4a(mp4a) = codec else { + use mp4_atom::DecodeMaybe; + + let mut cursor = std::io::Cursor::new(init.as_ref()); + let moov = std::iter::from_fn(|| mp4_atom::Any::decode_maybe(&mut cursor).unwrap()) + .find_map(|atom| match atom { + mp4_atom::Any::Moov(moov) => Some(moov), + _ => None, + }) + .expect("no moov in init"); + let codec = &moov.trak[0].mdia.minf.stbl.stsd.codecs[0]; + let mp4_atom::Codec::Mp4a(mp4a) = codec else { panic!("expected mp4a, got {codec:?}"); }; mp4a.esds.es_desc.dec_config.avg_bitrate @@ -850,7 +858,7 @@ mod tests { /// The `trun` sample durations of a fragment. fn sample_durations(fragment: &bytes::Bytes) -> Vec> { - use moq_mux::container::fmp4::mp4_atom::{self, DecodeMaybe}; + use mp4_atom::DecodeMaybe; let mut cursor = std::io::Cursor::new(fragment.as_ref()); while let Some(atom) = mp4_atom::Any::decode_maybe(&mut cursor).unwrap() { @@ -2381,7 +2389,7 @@ mod tests { settle().await; // PTS zero at exactly the moq epoch, so every timestamp maps to a fixed string. - let wall = kio::clock::Wall::UNIX_EPOCH + Duration::from_millis(hang::catalog::MOQ_EPOCH_UNIX_MILLIS); + let wall = kio::clock::Wall::MOQ_EPOCH; let config = catalog_config() .with_clock(moq_mux::Clock::at(moq_net::time::Instant::now(), wall).expect("a representable wall")); let catalog = moq_mux::catalog::Producer::new(&mut broadcast.broadcast().clone(), config).unwrap(); @@ -2553,7 +2561,7 @@ mod tests { } async fn live_fmp4_segments(version: Option<&str>, late: bool) { - use moq_mux::container::fmp4::mp4_atom::{self, DecodeMaybe, Encode}; + use mp4_atom::{DecodeMaybe, Encode}; // Reuse the A/V descriptors; opaque samples exercise segment discovery and FETCH, // without involving a codec decoder. let mut fixture = diff --git a/rs/moq-json/Cargo.toml b/rs/moq-json/Cargo.toml index cb0a9ce1465..b4118cf1a78 100644 --- a/rs/moq-json/Cargo.toml +++ b/rs/moq-json/Cargo.toml @@ -19,6 +19,9 @@ features = ["async"] default = [] # Generated async wrappers for the poll methods. Enable where you `.await` them. async = ["kio/async"] +# Exposes the merge-patch diff to the codec bench, through the hidden `bench` module. Off by +# default and hidden from the docs. Not a supported API. +bench = [] [dependencies] bytes = { workspace = true } @@ -43,6 +46,7 @@ workspace-hack = { path = "../workspace-hack" } [[bench]] name = "codec" harness = false +required-features = ["bench"] [[bench]] name = "allocations" diff --git a/rs/moq-json/benches/codec.rs b/rs/moq-json/benches/codec.rs index e405182ad4e..60701941bd4 100644 --- a/rs/moq-json/benches/codec.rs +++ b/rs/moq-json/benches/codec.rs @@ -14,7 +14,7 @@ use std::hint::black_box; use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; -use moq_json::diff; +use moq_json::bench::diff; use serde_json::{Map, Value, json}; /// One second of telemetry: a big static core plus a few moving numbers. Most fields change a little diff --git a/rs/moq-json/src/diff.rs b/rs/moq-json/src/diff.rs index 62c6d73d02d..74154bfb3c7 100644 --- a/rs/moq-json/src/diff.rs +++ b/rs/moq-json/src/diff.rs @@ -13,9 +13,10 @@ use std::cell::{Cell, RefCell}; use serde::Serialize; use serde::ser::{Impossible, SerializeMap, SerializeSeq, SerializeStruct, Serializer}; -use serde_json::{Map, Value}; +use serde_json::Value; /// The result of diffing a value into an RFC 7396 merge patch. +#[cfg(any(test, feature = "bench"))] pub struct Diff { /// A merge patch that transforms the old value into the new one. pub patch: Value, @@ -31,6 +32,7 @@ pub struct Diff { /// /// Only object roots produce a recursive patch; any other root forces a snapshot. A merge patch that /// would delete a key it shouldn't (a value genuinely set to null) also forces a snapshot. +#[cfg(any(test, feature = "bench"))] pub fn diff(old: &Value, new: &T) -> Diff { let result = bytes(old, new, &RefCell::new(Scratch::default())).unwrap_or(PatchBytes { patch: Vec::new(), @@ -38,7 +40,7 @@ pub fn diff(old: &Value, new: &T) -> Diff { }); Diff { patch: if result.patch.is_empty() { - Value::Object(Map::new()) + Value::Object(serde_json::Map::new()) } else { serde_json::from_slice(&result.patch).expect("the diff serializer emits JSON") }, @@ -1219,7 +1221,7 @@ impl Serializer for KeySer<'_> { #[cfg(test)] mod test { use super::*; - use serde_json::json; + use serde_json::{Map, json}; /// A straightforward Value-vs-Value merge-patch diff, used only as a test oracle: the production /// `diff` (the serializer) must agree with it on every case. diff --git a/rs/moq-json/src/lib.rs b/rs/moq-json/src/lib.rs index 77daab3fd41..883ab808fa2 100644 --- a/rs/moq-json/src/lib.rs +++ b/rs/moq-json/src/lib.rs @@ -31,7 +31,13 @@ pub mod stream; mod strict; pub mod window; -pub use crate::diff::{Diff, diff}; +/// The merge-patch diff, for the codec bench. Not a public API: compiled only with the `bench` +/// feature, which nothing enables by default. +#[cfg(feature = "bench")] +#[doc(hidden)] +pub mod bench { + pub use crate::diff::{Diff, diff}; +} /// Errors produced while publishing or consuming JSON. #[derive(thiserror::Error, Debug, Clone)] diff --git a/rs/moq-mux/benches/containers.rs b/rs/moq-mux/benches/containers.rs index 49e3a60da21..09e981e9d2b 100644 --- a/rs/moq-mux/benches/containers.rs +++ b/rs/moq-mux/benches/containers.rs @@ -101,7 +101,7 @@ async fn export(format: &str, source: moq_mux::Source, stream: impl Stream, coll let mut data = BytesMut::new(); if format == "cmaf" { let mut export = moq_mux::container::fmp4::Export::new(source, stream) - .with_max_delay(Duration::from_secs(30)) + .with_delay(moq_net::track::Delay::default().with_max(Duration::from_secs(30))) .with_fragment_duration(Duration::ZERO); while let Some(chunk) = export.next().await.unwrap() { if collect { @@ -111,7 +111,7 @@ async fn export(format: &str, source: moq_mux::Source, stream: impl Stream, coll } } } else { - let mut export = h264::Export::new(source, stream).with_max_delay(Duration::from_secs(30)); + let mut export = h264::Export::new(source, stream).with_delay(moq_net::track::Delay::default().with_max(Duration::from_secs(30))); while let Some(chunk) = export.next().await.unwrap() { if collect { data.extend_from_slice(&chunk); diff --git a/rs/moq-mux/src/catalog/estimate.rs b/rs/moq-mux/src/catalog/estimate.rs index b83c15073c5..43bb7f6f014 100644 --- a/rs/moq-mux/src/catalog/estimate.rs +++ b/rs/moq-mux/src/catalog/estimate.rs @@ -100,25 +100,11 @@ impl Estimate { /// # } /// ``` /// -/// Drive one directly only when you write to a raw -/// [`track::Producer`](moq_net::track::Producer) instead. The methods mirror the ones on -/// [`container::Producer`](crate::container::Producer), so call them at the same points: -/// -/// ``` -/// # use moq_mux::catalog::Estimator; -/// # use moq_net::Timestamp; -/// let mut estimator = Estimator::new(); -/// estimator.write(Timestamp::from_micros(0).unwrap(), 1200); -/// estimator.cut(Some(Timestamp::from_micros(33_000).unwrap())); -/// -/// let estimate = estimator.estimate(); -/// ``` -/// /// A standalone estimator has no other tracks to order against, so it never measures a delay. /// /// Move-only (not `Clone`): it owns the running measurement for exactly one track. #[derive(Default)] -pub struct Estimator { +pub(crate) struct Estimator { jitter: Jitter, bitrate: Bitrate, /// The framerate and delay, measured over the same windows. diff --git a/rs/moq-mux/src/catalog/mod.rs b/rs/moq-mux/src/catalog/mod.rs index 96f55ab8261..a1e3675c88c 100644 --- a/rs/moq-mux/src/catalog/mod.rs +++ b/rs/moq-mux/src/catalog/mod.rs @@ -43,7 +43,8 @@ pub use data::IntoRendition; pub(crate) use data::Listing; pub use entry::Entry; pub(crate) use estimate::Reading; -pub use estimate::{Estimate, Estimator}; +pub use estimate::Estimate; +pub(crate) use estimate::Estimator; pub use format::*; #[cfg(any(test, feature = "fuzz"))] pub(crate) use producer::to_msf; diff --git a/rs/moq-mux/src/catalog/msf/consumer.rs b/rs/moq-mux/src/catalog/msf/consumer.rs index db515153e71..fdc70ff3683 100644 --- a/rs/moq-mux/src/catalog/msf/consumer.rs +++ b/rs/moq-mux/src/catalog/msf/consumer.rs @@ -447,13 +447,13 @@ fn cmaf_video_description(track: &moq_msf::Track, init: &[u8], codec: &VideoCode ( VideoCodec::H264(codec), Codec::Avc1(mp4_atom::Avc1 { avcc, .. }) | Codec::Avc3(mp4_atom::Avc3 { avcc, .. }), - ) if codec.inline == matches!(sample, Codec::Avc3(_)) => avcc.encode_body(&mut description)?, + ) if codec.inline == matches!(sample, Codec::Avc3(_)) => avcc.encode_body(&mut description).map_err(crate::Error::mp4)?, ( VideoCodec::H265(codec), Codec::Hvc1(mp4_atom::Hvc1 { hvcc, .. }) | Codec::Hev1(mp4_atom::Hev1 { hvcc, .. }), - ) if codec.in_band == matches!(sample, Codec::Hev1(_)) => hvcc.encode_body(&mut description)?, - (VideoCodec::AV1(_), Codec::Av01(av01)) => av01.av1c.encode_body(&mut description)?, - (VideoCodec::VP9(_), Codec::Vp09(vp09)) => vp09.vpcc.encode_body(&mut description)?, + ) if codec.in_band == matches!(sample, Codec::Hev1(_)) => hvcc.encode_body(&mut description).map_err(crate::Error::mp4)?, + (VideoCodec::AV1(_), Codec::Av01(av01)) => av01.av1c.encode_body(&mut description).map_err(crate::Error::mp4)?, + (VideoCodec::VP9(_), Codec::Vp09(vp09)) => vp09.vpcc.encode_body(&mut description).map_err(crate::Error::mp4)?, (VideoCodec::VP8, Codec::Vp08(_)) => return Ok(None), _ => continue, } diff --git a/rs/moq-mux/src/catalog/producer.rs b/rs/moq-mux/src/catalog/producer.rs index c86fc6be1f7..5acba646a34 100644 --- a/rs/moq-mux/src/catalog/producer.rs +++ b/rs/moq-mux/src/catalog/producer.rs @@ -2168,7 +2168,7 @@ mod test { use std::time::Duration; let captured = kio::clock::now(); - let wall = kio::clock::Wall::UNIX_EPOCH + Duration::from_millis(hang::catalog::MOQ_EPOCH_UNIX_MILLIS + 60_000); + let wall = kio::clock::Wall::MOQ_EPOCH + Duration::from_millis(60_000); let clock = crate::Clock::at(captured, wall).unwrap(); let (origin, _driver) = moq_net::origin::Producer::new(Default::default()); let front = origin @@ -2338,7 +2338,7 @@ mod test { kio::clock::sleep(Duration::from_secs(5)).await; let mut broadcast = moq_net::broadcast::Info::new().produce(); - let wall = kio::clock::Wall::UNIX_EPOCH + Duration::from_millis(hang::catalog::MOQ_EPOCH_UNIX_MILLIS + 60_000); + let wall = kio::clock::Wall::MOQ_EPOCH + Duration::from_millis(60_000); let start = kio::clock::now() - Duration::from_secs(5); let config = Config::default().with_clock(crate::Clock::at(start, wall).expect("a representable wall")); let catalog = Producer::new(&mut broadcast, config).unwrap(); @@ -2508,7 +2508,7 @@ mod test { let now = Duration::from_secs(86_400); crate::clock::fake_host_wall( - kio::clock::Wall::UNIX_EPOCH + Duration::from_millis(hang::catalog::MOQ_EPOCH_UNIX_MILLIS) + now, + kio::clock::Wall::MOQ_EPOCH + now, ); let zero = moq_net::Timestamp::from_secs(now.as_secs() - 3600).unwrap(); hang::catalog::Clock::new(zero.convert(scale).unwrap()).unwrap() @@ -2561,8 +2561,7 @@ mod test { // The same mapping from `with_clock` agrees in any units, without reading the host's wall // clock, which may be unset. - let zero = kio::clock::Wall::UNIX_EPOCH - + Duration::from_millis(hang::catalog::MOQ_EPOCH_UNIX_MILLIS) + let zero = kio::clock::Wall::MOQ_EPOCH + Duration::from_micros(input.wall.as_micros() as u64); let fixed = crate::Clock::at(kio::clock::now(), zero).unwrap(); seed.media.clock = Some(an_hour_ago(moq_net::Timescale::MILLI)); @@ -2650,7 +2649,7 @@ mod test { let catalog = Producer::new(&mut broadcast, Config::default()).unwrap(); assert_eq!(catalog.snapshot().media.clock, None); - let wall = kio::clock::Wall::UNIX_EPOCH + Duration::from_millis(hang::catalog::MOQ_EPOCH_UNIX_MILLIS); + let wall = kio::clock::Wall::MOQ_EPOCH; let pts = moq_net::Timestamp::from_micros(1_000_000).unwrap(); assert!(matches!( catalog.timebase().place(pts, wall), diff --git a/rs/moq-mux/src/catalog/tracks.rs b/rs/moq-mux/src/catalog/tracks.rs index 08948b54c90..7c6c8fdeb7b 100644 --- a/rs/moq-mux/src/catalog/tracks.rs +++ b/rs/moq-mux/src/catalog/tracks.rs @@ -286,7 +286,7 @@ impl VideoHint { /// Build a config from these fields alone, or `None` if the codec is missing. Used to publish /// the catalog before the stream is parsed. - pub fn to_config(&self) -> Option { + pub(crate) fn to_config(&self) -> Option { let codec = self.codec.clone()?; let mut config = hang::catalog::VideoConfig::new(codec); self.apply(&mut config); diff --git a/rs/moq-mux/src/clock.rs b/rs/moq-mux/src/clock.rs index d6f4e3aa0ab..c1c9eb61d36 100644 --- a/rs/moq-mux/src/clock.rs +++ b/rs/moq-mux/src/clock.rs @@ -24,13 +24,13 @@ use kio::clock::Wall; use std::time::Duration; -use hang::catalog::{MAX_SAFE_INTEGER, MOQ_EPOCH_UNIX_MILLIS}; +use hang::catalog::MAX_SAFE_INTEGER; use kio::clock::Instant; /// Microseconds from the moq epoch (2020) to `wall`, negative before it. fn moq_micros(wall: Wall) -> i128 { - let moq_epoch = MOQ_EPOCH_UNIX_MILLIS as i128 * 1000; - wall.saturating_duration_since(Wall::UNIX_EPOCH).as_micros() as i128 - moq_epoch + let since = |from: Wall, to: Wall| to.saturating_duration_since(from).as_micros() as i128; + since(Wall::MOQ_EPOCH, wall) - since(wall, Wall::MOQ_EPOCH) } /// The host's wall clock, or `None` while it reads before the moq epoch (2020): unset. @@ -288,7 +288,7 @@ mod tests { } fn moq_epoch() -> Wall { - Wall::UNIX_EPOCH + Duration::from_millis(MOQ_EPOCH_UNIX_MILLIS) + Wall::MOQ_EPOCH } fn us(v: u64) -> moq_net::Timestamp { @@ -368,8 +368,7 @@ mod tests { #[test] fn unrepresentable_walls_are_refused() { // One micro past the JSON-safe integer range: browsers would read a different number. - let past_safe = MOQ_EPOCH_UNIX_MILLIS * 1000 + MAX_SAFE_INTEGER + 1; - let far = Wall::UNIX_EPOCH + Duration::from_micros(past_safe); + let far = Wall::MOQ_EPOCH + Duration::from_micros(MAX_SAFE_INTEGER + 1); assert!(Clock::at(epoch(), far).is_err()); // Before 2020 cannot be named on the wire; saturating to the epoch would lie. diff --git a/rs/moq-mux/src/codec/aac/mod.rs b/rs/moq-mux/src/codec/aac/mod.rs index aee9b5cb65a..f870f6ed4d9 100644 --- a/rs/moq-mux/src/codec/aac/mod.rs +++ b/rs/moq-mux/src/codec/aac/mod.rs @@ -84,6 +84,9 @@ pub type Result = std::result::Result; /// Typed AAC configuration mirroring the relevant fields of an /// AudioSpecificConfig. +/// +/// Marked `#[non_exhaustive]`: build one with [`new`](Self::new) or [`parse`](Self::parse). +#[non_exhaustive] pub struct Config { /// The leading audioObjectType: 2 for AAC-LC, 5 for HE-AAC (SBR), 29 for HE-AACv2 (PS). pub profile: u8, @@ -94,6 +97,15 @@ pub struct Config { } impl Config { + /// A config for `profile` (the audioObjectType) at the decoded `sample_rate` and `channel_count`. + pub fn new(profile: u8, sample_rate: u32, channel_count: u32) -> Self { + Self { + profile, + sample_rate, + channel_count, + } + } + /// Parse an AudioSpecificConfig buffer. /// /// Handles basic formats (object_type < 31), extended formats diff --git a/rs/moq-mux/src/codec/annexb/export.rs b/rs/moq-mux/src/codec/annexb/export.rs index cba8d472020..48775d5fe56 100644 --- a/rs/moq-mux/src/codec/annexb/export.rs +++ b/rs/moq-mux/src/codec/annexb/export.rs @@ -13,7 +13,7 @@ pub(crate) struct Export { codec: std::marker::PhantomData, source: crate::Source, catalog: Option, - max_delay: std::time::Duration, + delay: moq_net::track::Delay, track: Option, /// The latest presentation time written. A switch to another rendition resumes at its first /// keyframe past it: a new subscription starts at the rendition's latest group, which for @@ -49,19 +49,19 @@ impl Export { codec: std::marker::PhantomData, source, catalog: Some(catalog), - max_delay: std::time::Duration::ZERO, + delay: moq_net::track::Delay::default(), track: None, playhead: None, } } - /// Set the max delay for the per-track source. + /// Set how far behind the live edge the per-track source plays: [`Delay::min`](moq_net::track::Delay::min) is where it + /// starts, and [`Delay::max`](moq_net::track::Delay::max) how late a group may fall before it is skipped. /// - /// See [`Consumer`](crate::container::Consumer) for the per-track skip behavior. - /// Defaults to - /// [`std::time::Duration::ZERO`](std::time::Duration::ZERO) (skip aggressively). - pub fn with_max_delay(mut self, max_delay: std::time::Duration) -> Self { - self.max_delay = max_delay; + /// See [`Consumer`](crate::container::Consumer) for the per-track start and skip behavior. + /// Defaults to zero for both (start at the live edge, skip aggressively). + pub fn with_delay(mut self, delay: moq_net::track::Delay) -> Self { + self.delay = delay; self } @@ -153,7 +153,7 @@ impl Export { return Ok(()); } - let Some(source) = ExportSource::for_video_raw(&self.source, name, config, self.max_delay)? else { + let Some(source) = ExportSource::for_video_raw(&self.source, name, config, self.delay)? else { unreachable!("invalid broadcast references were removed above"); }; let convert = C::convert(name, config)?; @@ -214,7 +214,7 @@ mod tests { for finished in [false, true] { let mut moving = Moving::new(); let mut export = Export::new(moving.source().await, moving.catalog_stream().await) - .with_max_delay(std::time::Duration::from_secs(10)); + .with_delay(moq_net::track::Delay::default().with_max(std::time::Duration::from_secs(10))); moving.a.write(marked_frame(0, 0xa1)).unwrap(); assert_eq!(marks(&next(&mut export).await), [0xa1]); if finished { diff --git a/rs/moq-mux/src/codec/color.rs b/rs/moq-mux/src/codec/color.rs index bc01027512c..0472c2041c8 100644 --- a/rs/moq-mux/src/codec/color.rs +++ b/rs/moq-mux/src/codec/color.rs @@ -6,7 +6,7 @@ //! travels with the elementary stream, while a muxer adds the box and may get it wrong. So an //! importer resolves the bitstream's colour first and [`fill`]s only its gaps from the container. -use hang::catalog::{Chromaticity, Color, ContentLight, Mastering, Range}; +use hang::catalog::{Chromaticity, Color, ContentLight, Mastering, ColorRange}; /// The colour a set of H.273 code points signals, leaving each unspecified or reserved one absent. /// @@ -22,7 +22,7 @@ pub(crate) fn cicp(primaries: u8, transfer: u8, matrix: u8, full_range: Option Export { } } - /// Set the max delay for the per-track source. + /// Set how far behind the live edge the per-track source plays: + /// [`Delay::min`](moq_net::track::Delay::min) is where it starts, and + /// [`Delay::max`](moq_net::track::Delay::max) how late a group may fall before it is skipped. /// - /// See [`Consumer`](crate::container::Consumer) for the per-track skip behavior. - /// Defaults to - /// [`std::time::Duration::ZERO`](std::time::Duration::ZERO) (skip aggressively). - pub fn with_max_delay(mut self, max_delay: std::time::Duration) -> Self { - self.inner = self.inner.with_max_delay(max_delay); + /// See [`Consumer`](crate::container::Consumer) for the per-track start and skip behavior. + /// Defaults to zero for both (start at the live edge, skip aggressively). + pub fn with_delay(mut self, delay: moq_net::track::Delay) -> Self { + self.inner = self.inner.with_delay(delay); self } @@ -234,7 +235,7 @@ mod tests { // The whole track is written before the exporter runs, so it needs a budget // wide enough to read it: the default skips everything but the live edge. let mut export = Export::new(crate::source::announced(&consumer).await, Once(Some(catalog))) - .with_max_delay(std::time::Duration::from_secs(30)); + .with_delay(moq_net::track::Delay::default().with_max(std::time::Duration::from_secs(30))); let frame0 = export.next().await.unwrap().expect("first frame"); let frame1 = export.next().await.unwrap().expect("second frame"); diff --git a/rs/moq-mux/src/codec/h264/import.rs b/rs/moq-mux/src/codec/h264/import.rs index 120844aff48..de1418010c7 100644 --- a/rs/moq-mux/src/codec/h264/import.rs +++ b/rs/moq-mux/src/codec/h264/import.rs @@ -376,7 +376,7 @@ mod tests { .expect("colour"); assert_eq!( (color.primaries, color.transfer, color.matrix, color.range), - (Some(9), Some(16), Some(9), Some(hang::catalog::Range::Limited)) + (Some(9), Some(16), Some(9), Some(hang::catalog::ColorRange::Limited)) ); let hdr = crate::codec::color::test::hdr10(); assert_eq!( diff --git a/rs/moq-mux/src/codec/h264/mod.rs b/rs/moq-mux/src/codec/h264/mod.rs index b883a26173e..cd472fd590d 100644 --- a/rs/moq-mux/src/codec/h264/mod.rs +++ b/rs/moq-mux/src/codec/h264/mod.rs @@ -765,12 +765,12 @@ mod tests { let sdr = sps_color(fixtures::SPS_BT709).unwrap(); assert_eq!( (sdr.primaries, sdr.transfer, sdr.matrix, sdr.range), - (Some(1), Some(1), Some(1), Some(hang::catalog::Range::Limited)) + (Some(1), Some(1), Some(1), Some(hang::catalog::ColorRange::Limited)) ); let hdr = sps_color(fixtures::SPS_HDR10).unwrap(); assert_eq!( (hdr.primaries, hdr.transfer, hdr.matrix, hdr.range), - (Some(9), Some(16), Some(9), Some(hang::catalog::Range::Limited)) + (Some(9), Some(16), Some(9), Some(hang::catalog::ColorRange::Limited)) ); assert_eq!(sps_color(fixtures::SPS_IPB), None, "a VUI without a video signal type"); } diff --git a/rs/moq-mux/src/codec/h265/export.rs b/rs/moq-mux/src/codec/h265/export.rs index b4278ef48e3..5ed1b40cd34 100644 --- a/rs/moq-mux/src/codec/h265/export.rs +++ b/rs/moq-mux/src/codec/h265/export.rs @@ -30,13 +30,14 @@ impl Export { } } - /// Set the max delay for the per-track source. + /// Set how far behind the live edge the per-track source plays: + /// [`Delay::min`](moq_net::track::Delay::min) is where it starts, and + /// [`Delay::max`](moq_net::track::Delay::max) how late a group may fall before it is skipped. /// - /// See [`Consumer`](crate::container::Consumer) for the per-track skip behavior. - /// Defaults to - /// [`std::time::Duration::ZERO`](std::time::Duration::ZERO) (skip aggressively). - pub fn with_max_delay(mut self, max_delay: std::time::Duration) -> Self { - self.inner = self.inner.with_max_delay(max_delay); + /// See [`Consumer`](crate::container::Consumer) for the per-track start and skip behavior. + /// Defaults to zero for both (start at the live edge, skip aggressively). + pub fn with_delay(mut self, delay: moq_net::track::Delay) -> Self { + self.inner = self.inner.with_delay(delay); self } @@ -220,7 +221,7 @@ mod tests { // The whole track is written before the exporter runs, so it needs a budget // wide enough to read it: the default skips everything but the live edge. let mut export = Export::new(crate::source::announced(&consumer).await, Once(Some(catalog))) - .with_max_delay(std::time::Duration::from_secs(30)); + .with_delay(moq_net::track::Delay::default().with_max(std::time::Duration::from_secs(30))); let frame0 = export.next().await.unwrap().expect("first frame"); let frame1 = export.next().await.unwrap().expect("second frame"); diff --git a/rs/moq-mux/src/codec/h265/import.rs b/rs/moq-mux/src/codec/h265/import.rs index ba3b7348b0f..2db0d58542f 100644 --- a/rs/moq-mux/src/codec/h265/import.rs +++ b/rs/moq-mux/src/codec/h265/import.rs @@ -376,7 +376,7 @@ mod tests { .expect("colour"); assert_eq!( (color.primaries, color.transfer, color.matrix, color.range), - (Some(9), Some(16), Some(9), Some(hang::catalog::Range::Limited)) + (Some(9), Some(16), Some(9), Some(hang::catalog::ColorRange::Limited)) ); let hdr = crate::codec::color::test::hdr10(); assert_eq!( diff --git a/rs/moq-mux/src/codec/h265/mod.rs b/rs/moq-mux/src/codec/h265/mod.rs index c48d28cb044..001673306af 100644 --- a/rs/moq-mux/src/codec/h265/mod.rs +++ b/rs/moq-mux/src/codec/h265/mod.rs @@ -800,7 +800,7 @@ mod tests { let color = config_from_hvcc(&hvcc(fixtures::SPS_HDR10)).unwrap().color.unwrap(); assert_eq!( (color.primaries, color.transfer, color.matrix, color.range), - (Some(9), Some(16), Some(9), Some(hang::catalog::Range::Limited)) + (Some(9), Some(16), Some(9), Some(hang::catalog::ColorRange::Limited)) ); assert_eq!(config_from_hvcc(&hvcc(fixtures::SPS)).unwrap().color, None); assert_eq!(config_from_hvcc(&hvcc(fixtures::SPS_ZERO_COLOUR)).unwrap().color, None); diff --git a/rs/moq-mux/src/container/consumer.rs b/rs/moq-mux/src/container/consumer.rs index 29d0872c2a7..11888d89e15 100644 --- a/rs/moq-mux/src/container/consumer.rs +++ b/rs/moq-mux/src/container/consumer.rs @@ -98,8 +98,8 @@ pub(crate) enum Item { /// /// Put the initial [`Delay`] on the [`moq_net::track::Subscription`] before /// subscribing. [`new`](Self::new) inherits it, reading an unstated [`Delay::min`] as zero, and -/// [`with_delay`](Self::with_delay) or [`set_delay`](Self::set_delay) sets the whole [`Delay`]. -/// A rendition with a catalog `warmup` builds both with a [`Playout`] instead. +/// [`set_delay`](Self::set_delay) changes the whole [`Delay`] while reading. To build one at a +/// delay the subscription does not carry, or with a catalog `warmup`, use a [`Playout`]. /// /// ## Gaps and ends /// @@ -413,12 +413,6 @@ impl Consumer { self } - /// Set the [`Delay`] as a builder; see [`set_delay`](Self::set_delay). - pub fn with_delay(mut self, delay: Delay) -> Self { - self.set_delay(delay); - self - } - /// The group sequence of the last event read. pub(crate) fn group(&self) -> u64 { self.group @@ -3754,10 +3748,11 @@ mod tests { ) .with_range(5..); let mut consumer = - Consumer::new(consumer_track, Container::Legacy(crate::container::Kind::Data)).with_delay(Delay { + Playout::new(Delay { min: Some(Duration::from_millis(500)), max: Some(Duration::from_millis(500)), - }); + }, Duration::ZERO) + .consumer(consumer_track, Container::Legacy(crate::container::Kind::Data)); assert!( matches!(consumer.startup, Startup::Earliest { .. }), "a local range names no start, so the start still reaches back" @@ -3861,10 +3856,11 @@ mod tests { let consumer_track = track.subscribe(moq_net::track::Subscription::default().with_delay(Delay::default().with_max(None))); let mut consumer = - Consumer::new(consumer_track, Container::Legacy(crate::container::Kind::Data)).with_delay(Delay { + Playout::new(Delay { min: Some(Duration::from_millis(500)), max: None, - }); + }, Duration::ZERO) + .consumer(consumer_track, Container::Legacy(crate::container::Kind::Data)); assert_eq!(subscribed(&consumer).subscription().delay.max, None); write_group(&mut track, 5, &[ts(5_000_000)]); diff --git a/rs/moq-mux/src/container/flv/export.rs b/rs/moq-mux/src/container/flv/export.rs index 14b92d576d5..e4cd46916f3 100644 --- a/rs/moq-mux/src/container/flv/export.rs +++ b/rs/moq-mux/src/container/flv/export.rs @@ -23,7 +23,7 @@ use std::task::Poll; -use anyhow::Context; +use super::Error; use bytes::{BufMut, Bytes, BytesMut}; use hang::catalog::{AV1, AudioCodec, Catalog, Container, Reference, VideoCodec}; @@ -144,7 +144,7 @@ impl Flavor { pub struct Export { source: crate::Source, catalog: Option, - delay: std::time::Duration, + delay: moq_net::track::Delay, /// Frames as tags, encoded only once released. jitter: crate::jitter::Buffer<(u8, usize), Tag>, /// Emit every rendition as an enhanced-RTMP multitrack track, rather than only @@ -213,7 +213,7 @@ impl FlvTrack { } /// The tag timestamp of a frame presented at `pts` on the tag clock, on the inferred clock. - fn tag_timestamp(&self, pts: u32) -> anyhow::Result { + fn tag_timestamp(&self, pts: u32) -> crate::Result { if self.flavor.has_composition_time() { author_dts(pts, self.dts_reserve, self.last_dts) } else { @@ -229,7 +229,7 @@ impl FlvTrack { arrived: kio::clock::Instant, shift: u32, jitter: &mut crate::jitter::Buffer<(u8, usize), Tag>, - ) -> anyhow::Result<()> { + ) -> crate::Result<()> { self.release(key, Some(arrived), shift, true, jitter)?; let depth = match &self.config { Some(config) if self.flavor.has_composition_time() => crate::container::declared::depth(config)?, @@ -248,7 +248,7 @@ impl FlvTrack { shift: u32, flush: bool, jitter: &mut crate::jitter::Buffer<(u8, usize), Tag>, - ) -> anyhow::Result<()> { + ) -> crate::Result<()> { let Some(mut clock) = self.declared.take() else { return Ok(()); }; @@ -258,10 +258,10 @@ impl FlvTrack { arrival.arrived = arrival.arrived.max(settled); } let timestamp = u32::try_from(dts + i128::from(shift)) - .context("FLV decode time before the stream's origin or past 32 bits")?; + .map_err(|_| Error::TimestampRange)?; self.queue(key, arrival, record, timestamp, shift, jitter)?; } - anyhow::Ok(()) + Ok::<_, crate::Error>(()) })(); self.declared = Some(clock); released @@ -276,7 +276,7 @@ impl FlvTrack { timestamp: u32, shift: u32, jitter: &mut crate::jitter::Buffer<(u8, usize), Tag>, - ) -> anyhow::Result<()> { + ) -> crate::Result<()> { if self.flavor.has_composition_time() { self.last_dts = Some(timestamp); } @@ -322,7 +322,7 @@ impl FlvTrack { /// The out-of-band config record for this track's sequence-header tag, or /// `None` for codecs that carry their config in band. - fn config_record(&self) -> anyhow::Result> { + fn config_record(&self) -> crate::Result> { if !self.flavor.has_sequence_header() { return Ok(None); } @@ -330,7 +330,7 @@ impl FlvTrack { .source .description() .or(self.fallback_description.as_ref()) - .with_context(|| format!("FLV track '{}' missing its codec config record", self.name))?; + .ok_or_else(|| Error::MissingConfigRecord(self.name.clone()))?; Ok(Some(record.clone())) } } @@ -346,7 +346,7 @@ impl Export { Self { source, catalog: Some(catalog), - delay: std::time::Duration::ZERO, + delay: moq_net::track::Delay::default(), jitter: crate::jitter::Buffer::new(std::time::Duration::ZERO), multitrack: false, video: Vec::new(), @@ -356,15 +356,17 @@ impl Export { } } - /// Release frames a fixed delay after their decode time, dropping late frames. + /// Set how far behind the live edge the export plays. /// - /// A source whose clock runs further off ours than the output's may follow fails the export - /// once it has used half the delay. The same delay bounds each source's staleness. Zero (the default) releases each frame as - /// soon as it is read and drops none as late, but a stalled group is still skipped once a - /// newer one is ready. - pub fn with_delay(mut self, delay: std::time::Duration) -> Self { + /// [`Delay::min`](moq_net::track::Delay::min) releases each frame that long after its decode + /// time, dropping frames that arrive later, and is where each per-track source starts. A source + /// whose clock runs further off ours than the output's may follow fails the export once it has + /// used half of it. [`Delay::max`](moq_net::track::Delay::max) is how late a source's group may + /// fall before it is skipped. Zero for both (the default) releases each frame as soon as it is + /// read and drops none as late, but a stalled group is still skipped once a newer one is ready. + pub fn with_delay(mut self, delay: moq_net::track::Delay) -> Self { self.delay = delay; - self.jitter = crate::jitter::Buffer::new(delay); + self.jitter = crate::jitter::Buffer::new(delay.min.unwrap_or_default()); self } @@ -382,7 +384,7 @@ impl Export { /// Get the next FLV byte chunk, or `None` once the export ends. #[cfg_attr(feature = "async", kio::wrapper)] - pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll>> { + pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll>> { // 1. Drain catalog updates to discover the track layout. while let Some(catalog) = self.catalog.as_mut() { match catalog.poll_next(waiter)? { @@ -459,7 +461,7 @@ impl Export { } /// Read each track within its jitter window, preserving ingress timing before buffering. - fn fill(&mut self, waiter: &kio::Waiter) -> anyhow::Result<()> { + fn fill(&mut self, waiter: &kio::Waiter) -> crate::Result<()> { for kind in [0, 1] { let count = if kind == 0 { self.video.len() } else { self.audio.len() }; for index in 0..count { @@ -551,7 +553,7 @@ impl Export { Ok(()) } - fn update_catalog(&mut self, mut catalog: Catalog) -> anyhow::Result<()> { + fn update_catalog(&mut self, mut catalog: Catalog) -> crate::Result<()> { self.source.retain_selectable(&mut catalog); // A single-track FLV stream binds one rendition of each kind; multitrack @@ -600,7 +602,7 @@ impl Export { /// Move each bound track the catalog moved to another track or broadcast onto it. The tag /// shape is fixed by the header, so the new track must carry the same codec. - fn rebind(&mut self, catalog: &Catalog) -> anyhow::Result<()> { + fn rebind(&mut self, catalog: &Catalog) -> crate::Result<()> { for track in &mut self.video { let Some(config) = catalog.video.renditions.get(&track.name) else { continue; @@ -608,11 +610,9 @@ impl Export { if track.source.reads(&self.source, &track.name, config) { continue; } - anyhow::ensure!( - video_flavor(config)? == track.flavor, - "FLV track '{}' moved to another codec after the stream header", - track.name - ); + if video_flavor(config)? != track.flavor { + return Err(Error::CodecChanged(track.name.clone()).into()); + } ensure_legacy(&config.container, "video", &track.name)?; if let Some(source) = ExportSource::for_video(&self.source, &track.name, config, self.delay)? { track.fallback_description = fallback_description(config); @@ -627,11 +627,9 @@ impl Export { if track.source.reads(&self.source, &track.name, config) { continue; } - anyhow::ensure!( - audio_flavor(config)? == track.flavor, - "FLV track '{}' moved to another codec after the stream header", - track.name - ); + if audio_flavor(config)? != track.flavor { + return Err(Error::CodecChanged(track.name.clone()).into()); + } ensure_legacy(&config.container, "audio", &track.name)?; if let Some(source) = ExportSource::for_audio(&self.source, &track.name, config, self.delay)? { track.rebind(source); @@ -640,7 +638,7 @@ impl Export { Ok(()) } - fn bind_video(&mut self, catalog: &Catalog) -> anyhow::Result<()> { + fn bind_video(&mut self, catalog: &Catalog) -> crate::Result<()> { let renditions: Vec<_> = if self.multitrack { // Name order (the catalog is a BTreeMap), so each keeps a stable track id. catalog.video.renditions.iter().collect() @@ -669,7 +667,7 @@ impl Export { let Some(source) = ExportSource::for_video(&self.source, name, config, self.delay)? else { continue; }; - let track_id = u8::try_from(self.video.len()).context("too many FLV video tracks")?; + let track_id = u8::try_from(self.video.len()).map_err(|_| Error::TooManyTracks("video"))?; self.video.push(FlvTrack { name: name.clone(), track_id, @@ -689,7 +687,7 @@ impl Export { Ok(()) } - fn bind_audio(&mut self, catalog: &Catalog) -> anyhow::Result<()> { + fn bind_audio(&mut self, catalog: &Catalog) -> crate::Result<()> { let renditions: Vec<_> = if self.multitrack { // Name order (the catalog is a BTreeMap), so each keeps a stable track id. catalog.audio.renditions.iter().collect() @@ -718,7 +716,7 @@ impl Export { let Some(source) = ExportSource::for_audio(&self.source, name, config, self.delay)? else { continue; }; - let track_id = u8::try_from(self.audio.len()).context("too many FLV audio tracks")?; + let track_id = u8::try_from(self.audio.len()).map_err(|_| Error::TooManyTracks("audio"))?; self.audio.push(FlvTrack { name: name.clone(), track_id, @@ -746,7 +744,7 @@ impl Export { } /// Build the FLV file header plus every bound track's sequence-header tag. - fn build_header(&mut self) -> anyhow::Result { + fn build_header(&mut self) -> crate::Result { let mut out = BytesMut::new(); // FLV file header: signature, version 1, type flags, data offset = 9. @@ -788,7 +786,7 @@ impl Export { /// Encode one frame as a single FLV tag, after the sequence-header tag announcing its /// config record when the last one announced another. - fn encode_frame(&mut self, is_video: bool, index: usize, tag: Tag) -> anyhow::Result { + fn encode_frame(&mut self, is_video: bool, index: usize, tag: Tag) -> crate::Result { let Tag { frame, timestamp: timestamp_ms, @@ -938,13 +936,13 @@ impl Export { /// /// Errors if `body` exceeds FLV's 24-bit `DataSize` field (16 MiB), which would /// otherwise be silently truncated into a corrupt header. -fn write_tag(out: &mut BytesMut, tag_type: u8, timestamp_ms: u32, body: &[u8]) -> anyhow::Result<()> { +fn write_tag(out: &mut BytesMut, tag_type: u8, timestamp_ms: u32, body: &[u8]) -> crate::Result<()> { let size: u32 = body .len() .try_into() .ok() .filter(|n| *n <= 0x00FF_FFFF) - .context("FLV tag body exceeds the 24-bit DataSize limit")?; + .ok_or(Error::TagTooLarge(body.len()))?; out.put_u8(tag_type); out.put_slice(&size.to_be_bytes()[1..]); // 24-bit data size out.put_slice(×tamp_ms.to_be_bytes()[1..]); // 24-bit timestamp (low) @@ -955,14 +953,21 @@ fn write_tag(out: &mut BytesMut, tag_type: u8, timestamp_ms: u32, body: &[u8]) - Ok(()) } -fn ensure_legacy(container: &Container, kind: &str, name: &str) -> anyhow::Result<()> { +fn ensure_legacy(container: &Container, kind: &'static str, name: &str) -> crate::Result<()> { match container { Container::Legacy | Container::Loc => Ok(()), - Container::Cmaf { .. } => anyhow::bail!("FLV export does not support CMAF {kind} track '{name}'"), - Container::Unknown(unknown) => anyhow::bail!( - "FLV export does not support container '{}' on {kind} track '{name}'", - unknown.kind() - ), + Container::Cmaf { .. } => Err(Error::UnsupportedContainer { + kind, + name: name.to_string(), + container: "cmaf".to_string(), + } + .into()), + Container::Unknown(unknown) => Err(Error::UnsupportedContainer { + kind, + name: name.to_string(), + container: unknown.kind().to_string(), + } + .into()), } } @@ -975,58 +980,57 @@ fn fallback_description(config: &hang::catalog::VideoConfig) -> Option { } } -fn video_flavor(config: &hang::catalog::VideoConfig) -> anyhow::Result { +fn video_flavor(config: &hang::catalog::VideoConfig) -> crate::Result { match &config.codec { VideoCodec::H264(_) => Ok(Flavor::Avc), VideoCodec::H265(_) => Ok(Flavor::Hevc), VideoCodec::AV1(_) => Ok(Flavor::Av1), VideoCodec::VP9(_) => Ok(Flavor::Vp9), - other => anyhow::bail!("FLV export does not support video codec {other:?}"), + other => Err(Error::UnsupportedVideoCodec(other.to_string()).into()), } } -fn audio_flavor(config: &hang::catalog::AudioConfig) -> anyhow::Result { +fn audio_flavor(config: &hang::catalog::AudioConfig) -> crate::Result { match &config.codec { AudioCodec::AAC(_) => Ok(Flavor::Aac), AudioCodec::Mp3 => Ok(Flavor::Mp3), AudioCodec::Opus => Ok(Flavor::Opus), AudioCodec::Ac3 => Ok(Flavor::Ac3), AudioCodec::Ec3 => Ok(Flavor::Eac3), - other => anyhow::bail!("FLV export does not support audio codec {other:?}"), + other => Err(Error::UnsupportedAudioCodec(other.to_string()).into()), } } /// The frame's presentation time on the tag clock, `shift` milliseconds past its own. -fn frame_timestamp_ms(frame: &Frame, shift: u32) -> anyhow::Result { +fn frame_timestamp_ms(frame: &Frame, shift: u32) -> crate::Result { u32::try_from(frame.timestamp.as_millis()) .ok() .and_then(|pts| pts.checked_add(shift)) - .context("FLV timestamp exceeds 32 bits") + .ok_or_else(|| Error::TimestampRange.into()) } -fn composition_time(pts: u32, dts: u32) -> anyhow::Result { +fn composition_time(pts: u32, dts: u32) -> crate::Result { let cts = i64::from(pts) - i64::from(dts); i32::try_from(cts) .ok() .filter(|cts| (-0x80_0000..=0x7f_ffff).contains(cts)) - .context("FLV composition time exceeds signed 24 bits") + .ok_or_else(|| Error::CompositionTimeRange.into()) } -fn write_i24(out: &mut BytesMut, value: i32) -> anyhow::Result<()> { - anyhow::ensure!( - (-0x80_0000..=0x7f_ffff).contains(&value), - "FLV composition time exceeds signed 24 bits" - ); +fn write_i24(out: &mut BytesMut, value: i32) -> crate::Result<()> { + if !(-0x80_0000..=0x7f_ffff).contains(&value) { + return Err(Error::CompositionTimeRange.into()); + } out.put_slice(&(value as u32).to_be_bytes()[1..]); Ok(()) } -fn author_dts(pts: u32, reserve: u32, last: Option) -> anyhow::Result { +fn author_dts(pts: u32, reserve: u32, last: Option) -> crate::Result { let mut dts = pts.saturating_sub(reserve); if let Some(prev) = last && dts <= prev { - dts = prev.checked_add(1).context("FLV DTS exceeds 32 bits")?; + dts = prev.checked_add(1).ok_or(Error::TimestampRange)?; } Ok(dts) } diff --git a/rs/moq-mux/src/container/flv/export_test.rs b/rs/moq-mux/src/container/flv/export_test.rs index 886fc7c8c54..557fb471d5c 100644 --- a/rs/moq-mux/src/container/flv/export_test.rs +++ b/rs/moq-mux/src/container/flv/export_test.rs @@ -202,7 +202,7 @@ async fn export_emits_sequence_headers_and_frames() { let exporter = open_export(crate::source::announced(&consumer).await) .await - .with_delay(RECORDING_MAX_DELAY) + .with_delay(moq_net::track::Delay::default().with_min(RECORDING_MAX_DELAY).with_max(RECORDING_MAX_DELAY)) .with_replay(); let exported = drain_export(exporter, importer).await; @@ -1415,11 +1415,11 @@ async fn export_declared(reorder: u8, groups: &[&[u64]], open: bool) -> anyhow:: let mut exporter = open_export(crate::source::announced(&consumer).await) .await - .with_delay(RECORDING_MAX_DELAY) + .with_delay(moq_net::track::Delay::default().with_min(RECORDING_MAX_DELAY).with_max(RECORDING_MAX_DELAY)) .with_replay(); let mut exported = Vec::new(); if let Some(Err(err)) = drain_until_idle(&mut exporter, &mut exported).await { - return Err(err); + return Err(err.into()); } drop(video); let tags = parse_tags(&exported); @@ -1519,7 +1519,7 @@ async fn a_reorder_declared_after_the_header_raises_the_shift() { let mut exporter = open_export(crate::source::announced(&consumer).await) .await - .with_delay(RECORDING_MAX_DELAY) + .with_delay(moq_net::track::Delay::default().with_min(RECORDING_MAX_DELAY).with_max(RECORDING_MAX_DELAY)) .with_replay(); let mut exported = exporter.next().await.unwrap().expect("the header").to_vec(); @@ -1553,7 +1553,7 @@ async fn a_reorder_declared_after_the_header_raises_the_shift() { async fn drain_until_idle( exporter: &mut Export, out: &mut Vec, -) -> Option> { +) -> Option> { loop { match kio::clock::timeout(Duration::from_millis(100), exporter.next()).await { Ok(Ok(Some(chunk))) => out.extend_from_slice(&chunk), @@ -1672,7 +1672,7 @@ async fn a_track_leaving_the_catalog_is_read_to_its_end() { let mut exporter = open_export(crate::source::announced(&consumer).await) .await - .with_delay(RECORDING_MAX_DELAY) + .with_delay(moq_net::track::Delay::default().with_min(RECORDING_MAX_DELAY).with_max(RECORDING_MAX_DELAY)) .with_replay(); let mut exported = Vec::new(); // Consume the header and the two initial frames without advancing past @@ -1724,7 +1724,7 @@ async fn configured_delay_holds_live_frames() { config.description = Some(Bytes::from_static(&[0x12, 0x10])); let mut live = Live::audio(config); let mut export = super::Export::new(live.source().await, live.catalog_stream().await) - .with_delay(std::time::Duration::from_millis(100)); + .with_delay(moq_net::track::Delay::default().with_min(std::time::Duration::from_millis(100)).with_max(std::time::Duration::from_millis(100))); assert!(export.next().await.unwrap().is_some(), "container header"); live.track.write(raw_frame(0, &[0xde, 0xad], true)).unwrap(); assert!( @@ -1744,7 +1744,7 @@ async fn delay_drops_late_frames_and_drains_the_final_frame() { config.description = Some(Bytes::from_static(&[0x12, 0x10])); let mut live = Live::audio(config); let delay = std::time::Duration::from_millis(100); - let mut export = super::Export::new(live.source().await, live.catalog_stream().await).with_delay(delay); + let mut export = super::Export::new(live.source().await, live.catalog_stream().await).with_delay(moq_net::track::Delay::default().with_min(delay).with_max(delay)); export.next().await.unwrap().unwrap(); let start = kio::clock::now(); live.track.write(raw_frame(0, b"first", true)).unwrap(); @@ -1780,7 +1780,7 @@ async fn decode_order_is_independent_of_cross_track_arrival_order() { catalog.modify().unwrap().audio.renditions.insert(name, config); }); let mut export = super::Export::new(live.source().await, live.catalog_stream().await) - .with_delay(std::time::Duration::from_millis(100)) + .with_delay(moq_net::track::Delay::default().with_min(std::time::Duration::from_millis(100)).with_max(std::time::Duration::from_millis(100))) .with_multitrack(true); export.next().await.unwrap().unwrap(); live.track.write(raw_frame(0, b"initial-a", true)).unwrap(); @@ -1816,7 +1816,7 @@ async fn a_backlog_stays_in_the_source_until_due() { let mut config = AudioConfig::new(AAC { profile: 2 }, 44100, 2); config.description = Some(Bytes::from_static(&[0x12, 0x10])); let mut live = Live::audio(config); - let mut export = super::Export::new(live.source().await, live.catalog_stream().await).with_delay(delay); + let mut export = super::Export::new(live.source().await, live.catalog_stream().await).with_delay(moq_net::track::Delay::default().with_min(delay).with_max(delay)); export.next().await.unwrap().unwrap(); live.track.write(raw_frame(0, b"first", true)).unwrap(); export.next().await.unwrap().unwrap(); @@ -1852,7 +1852,7 @@ async fn a_retained_group_trims_to_the_delay() { tokio::time::advance(std::time::Duration::from_millis(20)).await; } let delay = std::time::Duration::from_millis(100); - let mut export = super::Export::new(live.source().await, live.catalog_stream().await).with_delay(delay); + let mut export = super::Export::new(live.source().await, live.catalog_stream().await).with_delay(moq_net::track::Delay::default().with_min(delay).with_max(delay)); export.next().await.unwrap().unwrap(); export.next().await.unwrap().unwrap(); // What is left spans no more than the delay: five 20 ms frames. diff --git a/rs/moq-mux/src/container/flv/mod.rs b/rs/moq-mux/src/container/flv/mod.rs index 1c7d1171770..b543b26637b 100644 --- a/rs/moq-mux/src/container/flv/mod.rs +++ b/rs/moq-mux/src/container/flv/mod.rs @@ -26,6 +26,55 @@ mod import; pub use export::*; pub use import::*; +/// FLV export errors. +#[derive(Debug, Clone, thiserror::Error)] +#[non_exhaustive] +pub enum Error { + /// A decode or presentation time falls before the stream's origin or past FLV's 32-bit + /// millisecond timestamp. + #[error("FLV timestamp before the stream's origin or past 32 bits")] + TimestampRange, + + /// A composition time offset does not fit FLV's signed 24-bit field. + #[error("FLV composition time exceeds signed 24 bits")] + CompositionTimeRange, + + /// A tag body exceeds FLV's 24-bit `DataSize` field (16 MiB). + #[error("FLV tag body of {0} bytes exceeds the 24-bit DataSize limit")] + TagTooLarge(usize), + + /// More renditions of one kind than FLV's 8-bit multitrack id can number. + #[error("too many FLV {0} tracks")] + TooManyTracks(&'static str), + + /// A codec that needs a sequence-header tag has no config record. + #[error("FLV track '{0}' missing its codec config record")] + MissingConfigRecord(String), + + /// A rendition moved to a track with another codec after the stream header fixed its tags. + #[error("FLV track '{0}' moved to another codec after the stream header")] + CodecChanged(String), + + /// A rendition's container is not one whose frames FLV tags carry as is. + #[error("FLV export does not support container '{container}' on {kind} track '{name}'")] + UnsupportedContainer { + /// `video` or `audio`. + kind: &'static str, + /// The rendition name. + name: String, + /// The catalog container kind. + container: String, + }, + + /// The video codec has no FLV or enhanced-RTMP mapping. + #[error("FLV export does not support video codec {0}")] + UnsupportedVideoCodec(String), + + /// The audio codec has no FLV or enhanced-RTMP mapping. + #[error("FLV export does not support audio codec {0}")] + UnsupportedAudioCodec(String), +} + #[cfg(test)] mod export_test; #[cfg(test)] diff --git a/rs/moq-mux/src/container/fmp4/export.rs b/rs/moq-mux/src/container/fmp4/export.rs index c0f69acc8b2..d6e29e1f9e6 100644 --- a/rs/moq-mux/src/container/fmp4/export.rs +++ b/rs/moq-mux/src/container/fmp4/export.rs @@ -1,6 +1,7 @@ use std::collections::{HashMap, HashSet, VecDeque}; use std::task::Poll; use std::time::Duration; +use moq_net::track::Delay; use bytes::Bytes; use hang::catalog::{AudioCodec, AudioConfig, Catalog, Container, VideoConfig}; @@ -64,7 +65,7 @@ const INIT_QUEUE: Duration = Duration::from_secs(30); pub struct Export { source: crate::Source, catalog: Option, - max_delay: Duration, + delay: Delay, fragment_duration: Option, /// The tracks being read, by rendition name. Before the init a rendition that leaves the @@ -180,10 +181,10 @@ enum Rendition { } impl Rendition { - fn subscribe(&self, source: &crate::Source, name: &str, max_delay: Duration) -> Result> { + fn subscribe(&self, source: &crate::Source, name: &str, delay: Delay) -> Result> { match self { - Rendition::Video(config) => ExportSource::for_fmp4_video(source, name, config, max_delay), - Rendition::Audio(config) => ExportSource::for_audio(source, name, config, max_delay), + Rendition::Video(config) => ExportSource::for_fmp4_video(source, name, config, delay), + Rendition::Audio(config) => ExportSource::for_audio(source, name, config, delay), } } @@ -514,7 +515,7 @@ impl Export { Self { source, catalog: Some(catalog), - max_delay: Duration::ZERO, + delay: Delay::default(), fragment_duration: None, tracks: HashMap::new(), departed: HashMap::new(), @@ -528,13 +529,13 @@ impl Export { } } - /// Set the max delay for each per-track source. + /// Set how far behind the live edge each per-track source plays: [`Delay::min`] is where it + /// starts, and [`Delay::max`] how late a group may fall before it is skipped. /// - /// See [`Consumer`](crate::container::Consumer) for the per-track skip behavior. - /// Defaults to - /// [`Duration::ZERO`] (skip aggressively). - pub fn with_max_delay(mut self, max_delay: Duration) -> Self { - self.max_delay = max_delay; + /// See [`Consumer`](crate::container::Consumer) for the per-track start and skip behavior. + /// Defaults to zero for both (start at the live edge, skip aggressively). + pub fn with_delay(mut self, delay: Delay) -> Self { + self.delay = delay; self } @@ -920,7 +921,7 @@ impl Export { match self.tracks.get_mut(&name) { Some(track) if track.source.is_some() && !moved => track.reconfigure(&name, config)?, Some(track) => { - let Some(source) = config.subscribe(&self.source, &name, self.max_delay)? else { + let Some(source) = config.subscribe(&self.source, &name, self.delay)? else { continue; }; // The publisher still caches the group the rendition left on, which a fresh @@ -941,7 +942,7 @@ impl Export { None => { // Subscribe via ExportSource, which applies any per-codec transform // (Annex-B → length-prefixed) at pull time. - let Some(source) = config.subscribe(&self.source, &name, self.max_delay)? else { + let Some(source) = config.subscribe(&self.source, &name, self.delay)? else { continue; }; if self.init_emitted && !self.late { @@ -1028,7 +1029,7 @@ pub(crate) fn extract_init( trexs: &mut Vec, ) -> Result<()> { let mut cursor = std::io::Cursor::new(init.as_ref()); - while let Some(atom) = mp4_atom::Any::decode_maybe(&mut cursor)? { + while let Some(atom) = mp4_atom::Any::decode_maybe(&mut cursor).map_err(crate::Error::mp4)? { match atom { mp4_atom::Any::Ftyp(f) if ftyp_data.is_none() => { *ftyp_data = Some(f); @@ -1398,7 +1399,7 @@ pub(crate) fn catalog_timescale_audio(config: &hang::catalog::AudioConfig) -> Re fn parse_timescale_from_init(init: &[u8]) -> Result { let mut cursor = std::io::Cursor::new(init); - while let Some(atom) = mp4_atom::Any::decode_maybe(&mut cursor)? { + while let Some(atom) = mp4_atom::Any::decode_maybe(&mut cursor).map_err(crate::Error::mp4)? { if let mp4_atom::Any::Moov(moov) = atom { let trak = moov.trak.first().ok_or(Error::NoTracks)?; return Ok(trak.mdia.mdhd.timescale as u64); diff --git a/rs/moq-mux/src/container/fmp4/export_test.rs b/rs/moq-mux/src/container/fmp4/export_test.rs index 02b42f6dae7..e364d72ff97 100644 --- a/rs/moq-mux/src/container/fmp4/export_test.rs +++ b/rs/moq-mux/src/container/fmp4/export_test.rs @@ -486,7 +486,7 @@ async fn single_track_export_init_matches_fragment_track_id() { .expect("catalog consumer"); let selected = catalog_stream.select(crate::select::Broadcast::default().audio(crate::select::Audio::default())); let mut exporter = crate::container::fmp4::Export::new(crate::source::announced(&consumer).await, selected) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); let init = kio::clock::timeout(std::time::Duration::from_secs(1), exporter.next()) .await @@ -549,7 +549,7 @@ async fn next_chunk_reports_segment_metadata() { // Sub-GOP cap so GOP 0 splits into two parts (the trailing part non-independent). let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) .with_fragment_duration(std::time::Duration::from_millis(20)) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); // First emit is the init segment, which carries no segmenting metadata to assert on. chunk_now(&mut exporter) @@ -745,7 +745,7 @@ async fn one_packet_audio_group_is_timed_by_the_catalog() { .unwrap(); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); chunk_now(&mut exporter) .await .init() @@ -777,7 +777,7 @@ async fn audio_fragment_is_the_publisher_group() { } let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); chunk_now(&mut exporter) .await .init() @@ -793,7 +793,7 @@ async fn audio_fragment_is_the_publisher_group() { ); // The one-frame-per-fragment mode is the explicit way back to per-packet output. let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY) + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)) .with_fragment_duration(std::time::Duration::ZERO); chunk_now(&mut exporter).await.init().expect("init"); let fragment = chunk_now(&mut exporter).await.fragment().expect("a media fragment"); @@ -809,7 +809,7 @@ async fn closed_audio_group_flushes_without_a_successor() { live.track.write(raw_frame(0, &[0x08, 0xaa], true)).unwrap(); live.track.write(raw_frame(20_000, &[0x08, 0xbb], false)).unwrap(); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); chunk_now(&mut exporter).await.init().expect("init"); assert!( drain_now(&mut exporter).await.is_empty(), @@ -842,7 +842,7 @@ async fn uncut_audio_waits_for_the_explicit_cap() { } let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); chunk_now(&mut exporter).await.init().expect("init"); assert!( drain_now(&mut exporter).await.is_empty(), @@ -850,7 +850,7 @@ async fn uncut_audio_waits_for_the_explicit_cap() { ); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY) + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)) .with_fragment_duration(std::time::Duration::from_millis(200)); chunk_now(&mut exporter).await.init().expect("init"); let fragments = drain_now(&mut exporter).await; @@ -875,7 +875,7 @@ async fn opus_frame_duration_from_toc() { .unwrap(); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); chunk_now(&mut exporter) .await .init() @@ -1015,7 +1015,7 @@ fn a_long_gop_does_not_cost_a_stack_frame_per_sample() { let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); assert!( chunk_now(&mut exporter).await.init().is_some(), "first chunk must be the init segment" @@ -1074,7 +1074,7 @@ async fn export_av(group: u64) -> (Vec<(u32, usize)>, (u32, u32)) { audio.finish().unwrap(); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); let init = chunk_now(&mut exporter).await.init().expect("init"); let ids = track_ids(&init); @@ -1141,7 +1141,7 @@ async fn video_that_ends_first_writes_its_tail_in_order() { } let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); let init = chunk_now(&mut exporter).await.init().expect("init"); let (video_id, audio_id) = track_ids(&init); @@ -1384,7 +1384,7 @@ async fn video_cut_preserves_the_exact_duration_without_a_successor() { live.track.write(video_frame(0, true)).unwrap(); live.track.write(video_frame(10_000, false)).unwrap(); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); chunk_now(&mut exporter).await.init().expect("init"); assert!(drain_now(&mut exporter).await.is_empty()); live.track @@ -1442,7 +1442,7 @@ async fn audio_before_the_first_keyframe_is_kept() { wait_for_sps(&mut live); // The CLI's default max delay: a parked reader falls behind it within a second. let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(std::time::Duration::from_millis(500)); + .with_delay(moq_net::track::Delay::default().with_max(std::time::Duration::from_millis(500))); // Two seconds of audio, published live with no video yet. for i in 0..20u64 { @@ -1748,7 +1748,7 @@ async fn aac_bitrate_churn_keeps_the_track() { let name = live.track.name().to_string(); live.track.write(raw_frame(0, &[0x01, 0x02, 0x03, 0x04], true)).unwrap(); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); chunk_now(&mut exporter).await.init().expect("init"); live.catalog @@ -1823,7 +1823,7 @@ async fn a_new_parameter_set_stays_in_band() { live.track.finish().unwrap(); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); let init = chunk_now(&mut exporter).await.init().expect("init"); let mp4_atom::Codec::Avc3(avc3) = video_codec(&init) else { panic!("expected an avc3 sample entry"); @@ -1877,7 +1877,7 @@ async fn split_parameter_sets_case( live.track.finish().unwrap(); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); chunk_now(&mut exporter).await.init().expect("init"); let samples: Vec<(Bytes, bool)> = drain_now(&mut exporter) .await @@ -1948,7 +1948,7 @@ async fn split_h265_parameter_sets_keep_the_sync_point() { async fn an_in_band_keyframe_without_parameter_sets_fails() { let mut live = Live::avc3(); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); chunk_now(&mut exporter).await.init().expect("init"); live.track.write(annexb_frame(0, &[IDR])).unwrap(); @@ -1998,7 +1998,7 @@ async fn a_return_resumes_after_its_last_group() { live.track.write(video_frame(0, true)).unwrap(); write_audio(&mut audio, 0, 11); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); chunk_now(&mut exporter).await.init().expect("init"); drain_now(&mut exporter).await; @@ -2034,7 +2034,7 @@ async fn a_track_that_never_describes_itself_fails() { wait_for_sps(&mut live); live.track.write(raw_frame(0, IDR_ONLY, true)).unwrap(); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); // Live audio, one second at a time, until the export gives up on the video. let mut second = 0; @@ -2065,7 +2065,7 @@ async fn an_uncut_audio_backlog_is_bounded() { wait_for_sps(&mut live); live.track.write(raw_frame(0, IDR_ONLY, true)).unwrap(); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); // One audio group that never closes, one second at a time. let mut second = 0; @@ -2219,7 +2219,7 @@ async fn cmaf_source_exports_at_the_frame_timestamp() { .await .expect("catalog consumer"); let mut exporter = crate::container::fmp4::Export::new(crate::source::announced(&consumer).await, catalog_stream) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); let init = chunk_now(&mut exporter).await.init().expect("init"); let fragments = drain_now(&mut exporter).await; @@ -2268,7 +2268,7 @@ async fn a_bare_keyframe_is_prefixed_with_cached_parameter_sets() { live.track.finish().unwrap(); let mut exporter = crate::container::fmp4::Export::new(live.source().await, live.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); chunk_now(&mut exporter).await.init().expect("init"); let fragments = drain_now(&mut exporter).await; @@ -2551,7 +2551,7 @@ async fn a_moved_rendition_continues_on_its_new_track() { moving.a.write(marked_frame(i, 0xa1)).unwrap(); } let mut exporter = crate::container::fmp4::Export::new(moving.source().await, moving.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); let init = chunk_now(&mut exporter).await.init().expect("init"); let mut fragments = drain_now(&mut exporter).await; @@ -2592,7 +2592,7 @@ async fn a_rendition_moved_to_another_sample_entry_fails() { moving.a.write(marked_frame(i, 0xa1)).unwrap(); } let mut exporter = crate::container::fmp4::Export::new(moving.source().await, moving.catalog_stream().await) - .with_max_delay(RECORDING_MAX_DELAY); + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)); chunk_now(&mut exporter).await.init().expect("init"); moving.point("b", None, Moving::MAIN_SPS); @@ -2621,7 +2621,7 @@ async fn export_reordered_clip(cap: Option) -> (Bytes, Vec< .await .expect("catalog consumer"); let mut exporter = crate::container::fmp4::Export::new(crate::source::announced(&consumer).await, catalog_stream) - .with_max_delay(RECORDING_MAX_DELAY) + .with_delay(moq_net::track::Delay::default().with_max(RECORDING_MAX_DELAY)) .with_fragment_duration(cap); let mut init = None; let mut fragments = Vec::new(); diff --git a/rs/moq-mux/src/container/fmp4/import.rs b/rs/moq-mux/src/container/fmp4/import.rs index 415cb90b3bf..c033646ccbf 100644 --- a/rs/moq-mux/src/container/fmp4/import.rs +++ b/rs/moq-mux/src/container/fmp4/import.rs @@ -392,7 +392,7 @@ impl Import { let count = (needed - self.buffer.len()).min(data.len()); self.buffer.extend_from_slice(&data[..count]); data = &data[count..]; - let Some(header) = mp4_atom::Header::decode_maybe(&mut self.buffer.as_ref())? else { + let Some(header) = mp4_atom::Header::decode_maybe(&mut self.buffer.as_ref()).map_err(crate::Error::mp4)? else { if data.is_empty() { return Ok(()); } @@ -493,9 +493,9 @@ impl Import { if kind == Moof::KIND && self.moof.is_some() { return Err(Error::DuplicateMoof.into()); } - let size = size.ok_or(mp4_atom::Error::InvalidSize)?; + let size = size.ok_or(mp4_atom::Error::InvalidSize).map_err(crate::Error::mp4)?; if size > super::MAX_METADATA_BYTES { - return Err(Error::MetadataTooLarge { kind, size }.into()); + return Err(Error::MetadataTooLarge { kind: kind.to_string(), size }.into()); } Ok(Body::Metadata { kind, @@ -508,12 +508,12 @@ impl Import { fn end_metadata(&mut self, kind: mp4_atom::FourCC, body: Bytes, header_size: usize) -> Result<()> { super::metadata::body(kind, &body)?; if kind == Moov::KIND { - return self.init(Moov::decode_body(&mut body.as_ref())?); + return self.init(Moov::decode_body(&mut body.as_ref()).map_err(crate::Error::mp4)?); } if self.moof.is_some() { return Err(Error::DuplicateMoof.into()); } - let moof = Moof::decode_body(&mut body.as_ref())?; + let moof = Moof::decode_body(&mut body.as_ref()).map_err(crate::Error::mp4)?; self.moof = Some(moof); self.moof_span = header_size.checked_add(body.len()).ok_or(Error::InvalidDataOffset)?; Ok(()) @@ -749,8 +749,8 @@ impl Import { }; let mut buf = Vec::new(); - ftyp.encode(&mut buf)?; - single_moov.encode(&mut buf)?; + ftyp.encode(&mut buf).map_err(crate::Error::mp4)?; + single_moov.encode(&mut buf).map_err(crate::Error::mp4)?; Ok(Container::Cmaf { init: buf.into() }) } @@ -850,8 +850,8 @@ impl Import { av01.spatial.as_deref(), ) } - mp4_atom::Codec::Unknown(unknown, _) => return Err(Error::UnknownCodec(*unknown).into()), - unsupported => return Err(Error::UnsupportedCodec(Box::new(unsupported.clone())).into()), + mp4_atom::Codec::Unknown(unknown, _) => return Err(Error::UnknownCodec(unknown.to_string()).into()), + unsupported => return Err(Error::unsupported_codec(unsupported).into()), }; // The codec config outranks the colr box a muxer added, which only fills its gaps. for entry in colr { @@ -886,7 +886,7 @@ impl Import { container: Container, ) -> Result { let mut description = BytesMut::new(); - avcc.encode_body(&mut description)?; + avcc.encode_body(&mut description).map_err(crate::Error::mp4)?; let description = description.freeze(); // Start from everything the avcC's SPS signals, so no field is dropped by a copy list. @@ -913,7 +913,7 @@ impl Import { container: Container, ) -> Result { let mut description = BytesMut::new(); - hvcc.encode_body(&mut description)?; + hvcc.encode_body(&mut description).map_err(crate::Error::mp4)?; let mut config = VideoConfig::new(H265 { in_band, @@ -1028,8 +1028,8 @@ impl Import { config.container = container; config } - mp4_atom::Codec::Unknown(unknown, _) => return Err(Error::UnknownCodec(*unknown).into()), - unsupported => return Err(Error::UnsupportedCodec(Box::new(unsupported.clone())).into()), + mp4_atom::Codec::Unknown(unknown, _) => return Err(Error::UnknownCodec(unknown.to_string()).into()), + unsupported => return Err(Error::unsupported_codec(unsupported).into()), }; Ok(config) @@ -1496,7 +1496,7 @@ fn fragment(traf: &mp4_atom::Traf, moof: &Moof, run: &Run, defaults: &sample::De } let mut moof_buf = Vec::new(); - adjusted_moof.encode(&mut moof_buf)?; + adjusted_moof.encode(&mut moof_buf).map_err(crate::Error::mp4)?; let new_moof_size = moof_buf.len(); // A payload past 4 GiB takes the 16-byte largesize header. @@ -1505,7 +1505,7 @@ fn fragment(traf: &mp4_atom::Traf, moof: &Moof, run: &Run, defaults: &sample::De kind: (*b"mdat").into(), size: Some(run.samples.iter().map(|sample| sample.range.len()).sum()), } - .encode(&mut mdat)?; + .encode(&mut mdat).map_err(crate::Error::mp4)?; // Re-encode moof with corrected per-trun data_offset for the per-track fragment. // Each trun's data_offset points to the start of that run's data within the new mdat. @@ -1525,7 +1525,7 @@ fn fragment(traf: &mp4_atom::Traf, moof: &Moof, run: &Run, defaults: &sample::De } moof_buf.clear(); - adjusted_moof.encode(&mut moof_buf)?; + adjusted_moof.encode(&mut moof_buf).map_err(crate::Error::mp4)?; // Each data_offset was reserved above, so restating them keeps the moof's size. debug_assert_eq!(moof_buf.len(), new_moof_size); moof_buf.extend_from_slice(&mdat); @@ -1707,7 +1707,7 @@ impl Import { _ => false, }; let mut result = match truncated { - true => Err(mp4_atom::Error::UnexpectedEof.into()), + true => Err(crate::Error::mp4(mp4_atom::Error::UnexpectedEof)), false => Ok(()), }; // End every track even on refusal, keeping the first error. A track whose finish fails diff --git a/rs/moq-mux/src/container/fmp4/import_test.rs b/rs/moq-mux/src/container/fmp4/import_test.rs index 091bafe4a63..e25b5e72860 100644 --- a/rs/moq-mux/src/container/fmp4/import_test.rs +++ b/rs/moq-mux/src/container/fmp4/import_test.rs @@ -1450,7 +1450,7 @@ fn test_av1_catalog() { let color = video.color.as_ref().expect("the range"); assert_eq!( (color.primaries, color.range), - (None, Some(hang::catalog::Range::Limited)) + (None, Some(hang::catalog::ColorRange::Limited)) ); let Container::Cmaf { init, .. } = &video.container else { panic!("expected Cmaf container"); diff --git a/rs/moq-mux/src/container/fmp4/metadata.rs b/rs/moq-mux/src/container/fmp4/metadata.rs index e8a3fe28de0..86eb545a5af 100644 --- a/rs/moq-mux/src/container/fmp4/metadata.rs +++ b/rs/moq-mux/src/container/fmp4/metadata.rs @@ -49,7 +49,7 @@ impl Scan { return Err(Error::TooManySamples(self.samples)); } if self.groups > MAX_FRAGMENT_SAMPLES { - return Err(mp4_atom::Error::TooLarge(b"sgpd".into()).into()); + return Err(Error::mp4(mp4_atom::Error::TooLarge(b"sgpd".into()))); } if self.tracks > MAX_TRACKS { return Err(Error::TooManyTracks(self.tracks)); @@ -92,7 +92,7 @@ impl Scan { fn retain(&mut self, kind: FourCC, count: u64, size: usize) -> Result<()> { self.any_bytes += count * size as u64; if self.any_bytes > MAX_METADATA_BYTES as u64 { - return Err(mp4_atom::Error::TooLarge(kind).into()); + return Err(Error::mp4(mp4_atom::Error::TooLarge(kind))); } Ok(()) } @@ -103,8 +103,8 @@ impl Scan { } let namespace = match kind.as_ref() { b"trun" => { - u32::decode(&mut data)?; // version and flags - self.samples += u64::from(u32::decode(&mut data)?); + u32::decode(&mut data).map_err(crate::container::fmp4::Error::mp4)?; // version and flags + self.samples += u64::from(u32::decode(&mut data).map_err(crate::container::fmp4::Error::mp4)?); return Ok(()); } b"sgpd" => { @@ -122,8 +122,8 @@ impl Scan { Namespace::Any } b"stsd" => { - u32::decode(&mut data)?; // full-box prefix - let count = u32::decode(&mut data)?; + u32::decode(&mut data).map_err(crate::container::fmp4::Error::mp4)?; // full-box prefix + let count = u32::decode(&mut data).map_err(crate::container::fmp4::Error::mp4)?; self.retain(kind, count.into(), std::mem::size_of::())?; Namespace::Any } @@ -134,7 +134,7 @@ impl Scan { b"mp4a" | b".mp3" | b"Opus" | b"fLaC" | b"ac-3" | b"ec-3" | b"sowt" | b"twos" | b"lpcm" | b"ipcm" | b"fpcm" | b"in24" | b"in32" | b"fl32" | b"fl64" | b"s16l" => { // Includes the variable-sized QuickTime v1/v2 fields; this decoder allocates nothing. - mp4_atom::Audio::decode(&mut data)?; + mp4_atom::Audio::decode(&mut data).map_err(crate::container::fmp4::Error::mp4)?; Namespace::Any } b"samr" => { @@ -172,7 +172,7 @@ impl Scan { _ => return Ok(()), }; if self.parents.contains(&kind) { - return Err(mp4_atom::Error::InvalidCombination("recursive metadata boxes").into()); + return Err(Error::mp4(mp4_atom::Error::InvalidCombination("recursive metadata boxes"))); } self.parents.push(kind); self.boxes(data, namespace)?; @@ -210,30 +210,30 @@ impl Scan { /// Split a complete box without copying its body or trusting a narrowed 64-bit size. fn next<'a>(data: &mut &'a [u8]) -> Result<(Header, &'a [u8])> { - let size = u32::decode(data)?; - let kind = FourCC::decode(data)?; + let size = u32::decode(data).map_err(crate::container::fmp4::Error::mp4)?; + let kind = FourCC::decode(data).map_err(crate::container::fmp4::Error::mp4)?; let size = match size { 0 => data.len(), - 1 => usize::try_from(u64::decode(data)?.checked_sub(16).ok_or(mp4_atom::Error::InvalidSize)?) - .map_err(|_| mp4_atom::Error::InvalidSize)?, - _ => size.checked_sub(8).ok_or(mp4_atom::Error::InvalidSize)? as usize, + 1 => usize::try_from(u64::decode(data).map_err(crate::container::fmp4::Error::mp4)?.checked_sub(16).ok_or(mp4_atom::Error::InvalidSize).map_err(crate::container::fmp4::Error::mp4)?) + .map_err(|_| mp4_atom::Error::InvalidSize).map_err(crate::container::fmp4::Error::mp4)?, + _ => size.checked_sub(8).ok_or(mp4_atom::Error::InvalidSize).map_err(crate::container::fmp4::Error::mp4)? as usize, }; // Media has a transport-owned payload bound and is never expanded as metadata. if kind.as_ref() != b"mdat" && size > MAX_METADATA_BYTES { - return Err(Error::MetadataTooLarge { kind, size }); + return Err(Error::MetadataTooLarge { kind: kind.to_string(), size }); } Ok((Header { kind, size: Some(size) }, take(data, size)?)) } fn take<'a>(data: &mut &'a [u8], size: usize) -> Result<&'a [u8]> { - let (body, rest) = data.split_at_checked(size).ok_or(mp4_atom::Error::OutOfBounds)?; + let (body, rest) = data.split_at_checked(size).ok_or(mp4_atom::Error::OutOfBounds).map_err(crate::container::fmp4::Error::mp4)?; *data = rest; Ok(body) } // Count descriptions across boxes before allocating the metadata tree. fn sgpd(mut data: &[u8]) -> Result { - let version = u32::decode(&mut data)? >> 24; + let version = u32::decode(&mut data).map_err(crate::container::fmp4::Error::mp4)? >> 24; take(&mut data, 4)?; // grouping_type if version >= 1 { take(&mut data, 4)?; // default_length @@ -241,7 +241,7 @@ fn sgpd(mut data: &[u8]) -> Result { if version >= 2 { take(&mut data, 4)?; // default_group_description_index } - let count = u32::decode(&mut data)?; + let count = u32::decode(&mut data).map_err(crate::container::fmp4::Error::mp4)?; Ok(count.into()) } @@ -435,7 +435,7 @@ pub(crate) mod tests { ] { let (decoded, checked) = entry(&nested); assert!(decoded); - assert!(matches!(checked, Err(Error::Mp4(err)) if matches!(*err, mp4_atom::Error::TooLarge(_)))); + assert!(matches!(checked, Err(Error::Mp4(err)) if matches!(err.downcast_ref(), Some(mp4_atom::Error::TooLarge(_))))); } // A few are accepted, as are children that do not frame: the dependency keeps those whole. let mut truncated = 100u32.to_be_bytes().to_vec(); diff --git a/rs/moq-mux/src/container/fmp4/mod.rs b/rs/moq-mux/src/container/fmp4/mod.rs index c545ea96e47..c88eba1a916 100644 --- a/rs/moq-mux/src/container/fmp4/mod.rs +++ b/rs/moq-mux/src/container/fmp4/mod.rs @@ -21,10 +21,6 @@ pub use fragmenter::*; pub use import::*; pub use muxer::*; -/// Re-export of the [`mp4_atom`] crate, whose types appear in this module's public API. -/// A major version bump of `mp4_atom` is a breaking change for moq-mux. -pub use mp4_atom; - /// The largest text sample [`Import`] holds whole. /// /// A text sample is rewritten only once its last byte arrives, so a larger one is refused before @@ -93,7 +89,7 @@ pub enum Error { /// Parsing or encoding an MP4 atom failed. #[error("mp4: {0}")] - Mp4(std::sync::Arc), + Mp4(#[source] std::sync::Arc), /// A MoQ transport operation failed. #[error("moq: {0}")] @@ -175,9 +171,9 @@ pub enum Error { #[error("multiple codecs")] MultipleCodecs, - /// The sample entry has an unrecognized codec four-character code. - #[error("unknown codec: {0:?}")] - UnknownCodec(mp4_atom::FourCC), + /// The sample entry has an unrecognized codec, named by its four-character code. + #[error("unknown codec: {0}")] + UnknownCodec(String), /// A `colr` box code point past H.273's 255. #[error("invalid colr code point: {0}")] @@ -187,9 +183,9 @@ pub enum Error { #[error("unsupported spherical video: {0}")] UnsupportedSpatial(&'static str), - /// The sample entry declares an unsupported codec. - #[error("unsupported codec: {0:?}")] - UnsupportedCodec(Box), + /// The sample entry declares a codec moq-mux does not carry, named by its four-character code. + #[error("unsupported codec: {0}")] + UnsupportedCodec(String), /// The sample entry declares unsupported MPEG-2 media. #[error("unsupported codec: MPEG2")] @@ -234,8 +230,8 @@ pub enum Error { /// A metadata body over 2 MiB is refused before parsing. #[error("{kind} declares {size} bytes, more than the {MAX_METADATA_BYTES} a metadata parser accepts")] MetadataTooLarge { - /// The box type. - kind: mp4_atom::FourCC, + /// The box type's four-character code. + kind: String, /// The body size its header declares. size: usize, }, @@ -400,10 +396,22 @@ pub enum Error { Unrepairable(&'static str), } -impl From for Error { - fn from(err: mp4_atom::Error) -> Self { +impl Error { + /// An mp4 atom error, kept opaque so an mp4-atom bump is no moq-mux break. + pub(crate) fn mp4(err: mp4_atom::Error) -> Self { Error::Mp4(std::sync::Arc::new(err)) } + + /// A sample entry this crate does not carry, named by the four-character code it encodes with. + pub(crate) fn unsupported_codec(codec: &mp4_atom::Codec) -> Self { + use mp4_atom::Encode; + let mut buf = Vec::new(); + let name = match codec.encode(&mut buf) { + Ok(()) if buf.len() >= 8 => String::from_utf8_lossy(&buf[4..8]).into_owned(), + _ => "?".to_string(), + }; + Error::UnsupportedCodec(name) + } } /// A result using this module's error type. @@ -437,11 +445,10 @@ impl Kind { /// CMAF container: encodes/decodes a single track's moof+mdat fragments. /// -/// Build from a CMAF init segment with [`Wire::from_init`], or wrap a -/// pre-extracted [`mp4_atom::Trak`] directly with [`Wire::new`]. Either refuses a track -/// that is neither audio nor video. +/// Build from a CMAF init segment with [`Wire::from_init`], which refuses a track that is +/// neither audio nor video. /// -/// The [`mp4_atom::Trak`] is heap-allocated so that embedding `Wire` +/// The parsed track is heap-allocated so that embedding `Wire` /// in other enums (e.g. [`catalog::hang::Container`](crate::catalog::hang::Container)) /// doesn't bloat unrelated variants. pub struct Wire { @@ -452,7 +459,8 @@ pub struct Wire { impl Wire { /// Wrap an already-parsed track, with no `trex` for its samples to fall back to. - pub fn new(trak: mp4_atom::Trak) -> Result { + #[cfg(test)] + pub(crate) fn new(trak: mp4_atom::Trak) -> Result { Ok(Self { kind: Kind::from_handler(trak.mdia.hdlr.handler)?, trak: Box::new(trak), @@ -466,7 +474,7 @@ impl Wire { use mp4_atom::DecodeMaybe; let mut cursor = std::io::Cursor::new(init_data); - while let Some(atom) = mp4_atom::Any::decode_maybe(&mut cursor)? { + while let Some(atom) = mp4_atom::Any::decode_maybe(&mut cursor).map_err(crate::container::fmp4::Error::mp4)? { if let mp4_atom::Any::Moov(mut moov) = atom { let trak = match moov.trak.len() { 1 => moov.trak.remove(0), @@ -485,7 +493,8 @@ impl Wire { } /// Borrow the MP4 track atom describing this wire container. - pub fn trak(&self) -> &mp4_atom::Trak { + #[cfg(test)] + pub(crate) fn trak(&self) -> &mp4_atom::Trak { &self.trak } @@ -596,7 +605,7 @@ pub(crate) fn decode(data: Bytes, timestamp: Option, track: Track) -> loop { let start = cursor.position() as usize; - let Some(atom) = mp4_atom::Any::decode_maybe(&mut cursor)? else { + let Some(atom) = mp4_atom::Any::decode_maybe(&mut cursor).map_err(crate::container::fmp4::Error::mp4)? else { break; }; match atom { @@ -805,15 +814,15 @@ pub(super) fn encode_at(info: FragmentInfo, base_dts: u64, frames: &[Frame]) -> // First pass to learn the moof size. let mut buf = Vec::new(); - build_moof(0).encode(&mut buf)?; + build_moof(0).encode(&mut buf).map_err(crate::container::fmp4::Error::mp4)?; let moof_size = buf.len(); // Second pass with data_offset = moof_size + 8 (mdat header). buf.clear(); - build_moof((moof_size + 8) as i32).encode(&mut buf)?; + build_moof((moof_size + 8) as i32).encode(&mut buf).map_err(crate::container::fmp4::Error::mp4)?; let mdat = mp4_atom::Mdat { data: mdat_data }; - mdat.encode(&mut buf)?; + mdat.encode(&mut buf).map_err(crate::container::fmp4::Error::mp4)?; Ok((Bytes::from(buf), dts)) } @@ -945,7 +954,7 @@ fn color_colr(color: Option<&hang::catalog::Color>) -> Option { colour_primaries: color.primaries.unwrap_or(2).into(), transfer_characteristics: color.transfer.unwrap_or(2).into(), matrix_coefficients: color.matrix.unwrap_or(2).into(), - full_range_flag: color.range == Some(hang::catalog::Range::Full), + full_range_flag: color.range == Some(hang::catalog::ColorRange::Full), }) } @@ -959,7 +968,7 @@ fn vpcc_color(mut vpcc: mp4_atom::VpcC, color: Option<&hang::catalog::Color>) -> vpcc.color_primaries = color.primaries.unwrap_or(2); vpcc.transfer_characteristics = color.transfer.unwrap_or(2); vpcc.matrix_coefficients = color.matrix.unwrap_or(2); - vpcc.video_full_range_flag = color.range == Some(hang::catalog::Range::Full); + vpcc.video_full_range_flag = color.range == Some(hang::catalog::ColorRange::Full); vpcc } @@ -1005,7 +1014,7 @@ pub(crate) fn synthesize_video_trak( let sample_entry = match &config.codec { VideoCodec::H264(h264) => { let mut cursor = std::io::Cursor::new(require_description()?); - let avcc = mp4_atom::Avcc::decode_body(&mut cursor).map_err(Error::from)?; + let avcc = mp4_atom::Avcc::decode_body(&mut cursor).map_err(Error::mp4)?; // Empty parameter-set lists are the in-band sample entry. A record that // still carries them, including one built from the first keyframe, stays avc1. if h264.inline && avcc.sequence_parameter_sets.is_empty() && avcc.picture_parameter_sets.is_empty() { @@ -1026,7 +1035,7 @@ pub(crate) fn synthesize_video_trak( } VideoCodec::H265(h265) => { let mut cursor = std::io::Cursor::new(require_description()?); - let hvcc = mp4_atom::Hvcc::decode_body(&mut cursor).map_err(Error::from)?; + let hvcc = mp4_atom::Hvcc::decode_body(&mut cursor).map_err(Error::mp4)?; // `in_band` (catalog) ↔ hev1 sample entry; otherwise hvc1. if h265.in_band { mp4_atom::Codec::from(mp4_atom::Hev1 { @@ -1152,7 +1161,7 @@ pub(crate) fn synthesize_audio_trak(track_id: u32, timescale: u64, config: &Audi .as_ref() .ok_or_else(|| Error::MissingAudioDescription(config.codec.to_string()))?; let mut cursor = std::io::Cursor::new(description.as_ref()); - let dec_specific = mp4_atom::esds::DecoderSpecific::decode(&mut cursor)?; + let dec_specific = mp4_atom::esds::DecoderSpecific::decode(&mut cursor).map_err(crate::container::fmp4::Error::mp4)?; // Safari and AVFoundation reject an AAC track whose DecoderConfigDescriptor is all // zeros: they endOfStream("decode") on the *init* append, which leaves the @@ -1330,8 +1339,8 @@ pub(crate) fn encode_init( }; let mut buf = Vec::new(); - ftyp.encode(&mut buf)?; - moov.encode(&mut buf)?; + ftyp.encode(&mut buf).map_err(crate::container::fmp4::Error::mp4)?; + moov.encode(&mut buf).map_err(crate::container::fmp4::Error::mp4)?; Ok(Bytes::from(buf)) } diff --git a/rs/moq-mux/src/container/fmp4/muxer.rs b/rs/moq-mux/src/container/fmp4/muxer.rs index 5a8da38e1f4..15a4b3d384d 100644 --- a/rs/moq-mux/src/container/fmp4/muxer.rs +++ b/rs/moq-mux/src/container/fmp4/muxer.rs @@ -134,36 +134,6 @@ impl Muxer { Ok(self) } - /// Synthesize missing video sample durations at `framerate`, a newer estimate than the - /// config this muxer was built from. - /// - /// Only the fallback cadence follows it: the timescale, and so the init segment, stays the - /// one the original config chose. The cadence snaps to whole ticks at that timescale (an NTSC - /// rate is fractional ticks at 90 kHz, which would otherwise fail to mux as inexact). Ignored - /// for audio, for a missing, non-finite or non-positive rate, and for one whose frame rounds - /// to zero ticks or past `trun`'s 32-bit sample duration. - pub fn with_framerate(mut self, framerate: Option) -> Self { - if !matches!(self.kind, Kind::Video(_)) { - return self; - } - let Some(fps) = framerate.filter(|fps| fps.is_finite() && *fps > 0.0) else { - return self; - }; - let timescale = self.timescale.as_u64(); - let ticks = (timescale as f64 / fps).round(); - if !(1.0..=f64::from(u32::MAX)).contains(&ticks) { - return self; - } - // Exactly `ticks / timescale`, rounded to the nanosecond, so the fallback snaps back to - // `ticks` within the muxer's one-nanosecond tolerance. - let timescale = u128::from(timescale); - let nanos = (ticks as u128 * 1_000_000_000 + timescale / 2) / timescale; - if let Ok(nanos) = u64::try_from(nanos) { - self.default_frame = Duration::from_nanos(nanos); - } - self - } - /// The rendition's catalog container, whichever kind of track this is. fn catalog_container(&self) -> &CatalogContainer { match &self.kind { @@ -824,61 +794,6 @@ mod tests { assert_eq!(decoded[0].timestamp.as_micros(), 33_333); } - // A rendition built before the publisher's framerate estimate settled keeps its 90 kHz - // timescale, but a duration-less frame follows the newer cadence. - #[test] - fn with_framerate_updates_the_fallback_without_the_timescale() { - let muxer = video_muxer_without_framerate().with_framerate(Some(60.0)); - assert_eq!(muxer.timescale().as_u64(), 90_000); - let fragment = muxer.fragment(0, &[frame(0, true)], None).unwrap(); - assert_eq!(super::super::sample_durations(&fragment), vec![Some(1_500)]); - } - - // An NTSC rate is fractional ticks at a timescale that doesn't divide it (1501.5 at 90 kHz), - // so the cadence snaps to whole ticks rather than failing the fragment as inexact. - #[test] - fn with_framerate_snaps_a_fractional_cadence_to_whole_ticks() { - for (fps, ticks) in [(60_000.0 / 1001.0, 1_502), (24_000.0 / 1001.0, 3_754)] { - let muxer = video_muxer_without_framerate().with_framerate(Some(fps)); - let fragment = muxer.fragment(0, &[frame(0, true)], None).unwrap(); - assert_eq!(super::super::sample_durations(&fragment), vec![Some(ticks)], "{fps}"); - } - - // 30 kHz doesn't divide a second's nanoseconds either: 1251.25 ticks rounds to 1251. - let mut config = VideoConfig::new(VideoCodec::VP8); - config.framerate = Some(30.0); - let muxer = Muxer::video(&config).unwrap().with_framerate(Some(24_000.0 / 1001.0)); - assert_eq!(muxer.timescale().as_u64(), 30_000); - let fragment = muxer.fragment(0, &[frame(0, true)], None).unwrap(); - assert_eq!(super::super::sample_durations(&fragment), vec![Some(1_251)]); - } - - #[test] - fn with_framerate_ignores_an_unusable_rate() { - // 90 kHz at 1e-6 fps is 9e10 ticks per frame, past trun's 32 bits; at 1e9 fps it is 0. - for fps in [ - None, - Some(0.0), - Some(-30.0), - Some(f64::NAN), - Some(f64::INFINITY), - Some(1e-6), - Some(1e9), - ] { - let muxer = video_muxer_without_framerate().with_framerate(fps); - let fragment = muxer.fragment(0, &[frame(0, true)], None).unwrap(); - // The 30 fps fallback at 90 kHz. - assert_eq!(super::super::sample_durations(&fragment), vec![Some(3_000)], "{fps:?}"); - } - } - - fn video_muxer_without_framerate() -> Muxer { - let mut config = VideoConfig::new(VideoCodec::VP8); - config.coded_width = Some(320); - config.coded_height = Some(240); - Muxer::video(&config).unwrap() - } - #[test] fn with_timescale_recomputes_the_fallback_frame_duration() { let timescale = moq_net::Timescale::new(90_000).unwrap(); diff --git a/rs/moq-mux/src/container/fmp4/text.rs b/rs/moq-mux/src/container/fmp4/text.rs index 23082860a87..5a97077cd05 100644 --- a/rs/moq-mux/src/container/fmp4/text.rs +++ b/rs/moq-mux/src/container/fmp4/text.rs @@ -127,7 +127,7 @@ fn vtt(header: &str, mut data: &[u8], start: Timestamp, end: Timestamp) -> crate let mut output = header.to_string(); while !data.is_empty() { let atom = Any::decode_maybe(&mut data) - .map_err(Error::from)? + .map_err(Error::mp4)? .ok_or(Error::InvalidSubtitle("truncated WebVTT sample box"))?; let Any::Unknown(kind, body) = atom else { return Err(Error::InvalidSubtitle("unsupported WebVTT sample box").into()); @@ -142,7 +142,7 @@ fn vtt(header: &str, mut data: &[u8], start: Timestamp, end: Timestamp) -> crate let mut origin = None; while !body.is_empty() { let atom = Any::decode_maybe(&mut body) - .map_err(Error::from)? + .map_err(Error::mp4)? .ok_or(Error::InvalidSubtitle("truncated WebVTT cue box"))?; let Any::Unknown(kind, value) = atom else { return Err(Error::InvalidSubtitle("unsupported WebVTT cue box").into()); diff --git a/rs/moq-mux/src/container/mkv/export.rs b/rs/moq-mux/src/container/mkv/export.rs index a33bf42e9c9..3a6bb7fc5dd 100644 --- a/rs/moq-mux/src/container/mkv/export.rs +++ b/rs/moq-mux/src/container/mkv/export.rs @@ -54,7 +54,7 @@ const TIMESTAMP_SCALE_NS: u64 = 1_000_000; pub struct Export { source: crate::Source, catalog: Option, - delay: Duration, + delay: moq_net::track::Delay, jitter: crate::jitter::Buffer, /// An already released frame waiting for the previous cluster to be returned. held: Option<(String, Frame)>, @@ -336,7 +336,7 @@ impl Export { Self { source, catalog: Some(catalog), - delay: Duration::ZERO, + delay: moq_net::track::Delay::default(), jitter: crate::jitter::Buffer::new(Duration::ZERO), held: None, fragment_duration: None, @@ -347,15 +347,17 @@ impl Export { } } - /// Release frames a fixed delay after their decode time, dropping late frames. + /// Set how far behind the live edge the export plays. /// - /// A source whose clock runs further off ours than the output's may follow fails the export - /// once it has used half the delay. The same delay bounds each source's staleness. Zero (the default) releases each frame as - /// soon as it is read and drops none as late, but a stalled group is still skipped once a - /// newer one is ready. - pub fn with_delay(mut self, delay: Duration) -> Self { + /// [`Delay::min`](moq_net::track::Delay::min) releases each frame that long after its decode + /// time, dropping frames that arrive later, and is where each per-track source starts. A source + /// whose clock runs further off ours than the output's may follow fails the export once it has + /// used half of it. [`Delay::max`](moq_net::track::Delay::max) is how late a source's group may + /// fall before it is skipped. Zero for both (the default) releases each frame as soon as it is + /// read and drops none as late, but a stalled group is still skipped once a newer one is ready. + pub fn with_delay(mut self, delay: moq_net::track::Delay) -> Self { self.delay = delay; - self.jitter = crate::jitter::Buffer::new(delay); + self.jitter = crate::jitter::Buffer::new(delay.min.unwrap_or_default()); self } @@ -555,7 +557,7 @@ impl Export { track.start_group(name, &mut self.jitter)?; } // A declared depth is checked even when nothing is scheduled by decode time. - if video && (track.declared.is_some() || !self.delay.is_zero()) { + if video && (track.declared.is_some() || !self.delay.min.unwrap_or_default().is_zero()) { track.push(name, arrival, &mut self.jitter)?; } else { self.jitter.push(name.clone(), arrival)?; @@ -883,8 +885,8 @@ pub(super) fn matroska_colour(color: &hang::catalog::Color) -> Option 1, - hang::catalog::Range::Full => 2, + hang::catalog::ColorRange::Limited => 1, + hang::catalog::ColorRange::Full => 2, })); } if let Some(v) = color.transfer { diff --git a/rs/moq-mux/src/container/mkv/export_test.rs b/rs/moq-mux/src/container/mkv/export_test.rs index 72db47bf4c7..0c219a4dc0d 100644 --- a/rs/moq-mux/src/container/mkv/export_test.rs +++ b/rs/moq-mux/src/container/mkv/export_test.rs @@ -113,7 +113,7 @@ async fn declared_reorder_starts_before_zero_and_resets_each_group() { audio.finish().unwrap(); live.catalog.finish().unwrap(); let mut export = super::Export::new(live.source().await, live.catalog_stream().await) - .with_delay(std::time::Duration::from_secs(5)) + .with_delay(moq_net::track::Delay::default().with_min(std::time::Duration::from_secs(5)).with_max(std::time::Duration::from_secs(5))) .with_fragment_duration(std::time::Duration::ZERO) .with_replay(); export.next().await.unwrap().unwrap(); @@ -538,7 +538,7 @@ async fn export_emits_blocks_for_each_frame() { // Use per-frame clustering so each frame is observable as its own // Cluster chunk; batching is exercised in a dedicated test below. .with_fragment_duration(std::time::Duration::ZERO) - .with_delay(RECORDING_MAX_DELAY) + .with_delay(moq_net::track::Delay::default().with_min(RECORDING_MAX_DELAY).with_max(RECORDING_MAX_DELAY)) .with_replay(); let mut exported: Vec = Vec::new(); @@ -781,7 +781,7 @@ async fn export_fragment_duration_batches_blocks() { .expect("catalog consumer"); let mut exporter = crate::container::mkv::Export::new(crate::source::announced(&consumer).await, catalog_stream) .with_fragment_duration(std::time::Duration::from_secs(2)) - .with_delay(RECORDING_MAX_DELAY) + .with_delay(moq_net::track::Delay::default().with_min(RECORDING_MAX_DELAY).with_max(RECORDING_MAX_DELAY)) .with_replay(); let mut exported: Vec = Vec::new(); @@ -1122,7 +1122,7 @@ async fn configured_delay_holds_live_frames() { config.description = Some(Bytes::from_static(&[0x12, 0x10])); let mut live = Live::audio(config); let mut export = super::Export::new(live.source().await, live.catalog_stream().await) - .with_delay(std::time::Duration::from_millis(100)) + .with_delay(moq_net::track::Delay::default().with_min(std::time::Duration::from_millis(100)).with_max(std::time::Duration::from_millis(100))) .with_fragment_duration(Some(std::time::Duration::ZERO)); assert!(export.next().await.unwrap().is_some(), "container header"); live.track.write(raw_frame(0, &[0xde, 0xad], true)).unwrap(); @@ -1144,7 +1144,7 @@ async fn delay_drops_late_frames_and_drains_the_final_frame() { let mut live = Live::audio(config); let delay = std::time::Duration::from_millis(100); let mut export = super::Export::new(live.source().await, live.catalog_stream().await) - .with_delay(delay) + .with_delay(moq_net::track::Delay::default().with_min(delay).with_max(delay)) .with_fragment_duration(std::time::Duration::ZERO); export.next().await.unwrap().unwrap(); let start = kio::clock::now(); @@ -1181,7 +1181,7 @@ async fn decode_order_is_independent_of_cross_track_arrival_order() { catalog.modify().unwrap().audio.renditions.insert(name, config); }); let mut export = super::Export::new(live.source().await, live.catalog_stream().await) - .with_delay(std::time::Duration::from_millis(100)) + .with_delay(moq_net::track::Delay::default().with_min(std::time::Duration::from_millis(100)).with_max(std::time::Duration::from_millis(100))) .with_fragment_duration(std::time::Duration::ZERO); export.next().await.unwrap().unwrap(); live.track.write(raw_frame(0, b"initial-a", true)).unwrap(); @@ -1229,7 +1229,7 @@ async fn reordered_video_uses_decode_deadlines_but_keeps_block_pts() { audio.finish().unwrap(); live.catalog.finish().unwrap(); let mut export = super::Export::new(live.source().await, live.catalog_stream().await) - .with_delay(std::time::Duration::from_secs(5)) + .with_delay(moq_net::track::Delay::default().with_min(std::time::Duration::from_secs(5)).with_max(std::time::Duration::from_secs(5))) .with_fragment_duration(std::time::Duration::ZERO) .with_replay(); export.next().await.unwrap().unwrap(); @@ -1262,7 +1262,7 @@ async fn a_backlog_stays_in_the_source_until_due() { config.description = Some(Bytes::from_static(&[0x12, 0x10])); let mut live = Live::audio(config); let mut export = super::Export::new(live.source().await, live.catalog_stream().await) - .with_delay(delay) + .with_delay(moq_net::track::Delay::default().with_min(delay).with_max(delay)) .with_fragment_duration(std::time::Duration::ZERO); export.next().await.unwrap().unwrap(); live.track.write(raw_frame(0, b"first", true)).unwrap(); @@ -1300,7 +1300,7 @@ async fn a_retained_group_trims_to_the_delay() { } let delay = std::time::Duration::from_millis(100); let mut export = super::Export::new(live.source().await, live.catalog_stream().await) - .with_delay(delay) + .with_delay(moq_net::track::Delay::default().with_min(delay).with_max(delay)) .with_fragment_duration(std::time::Duration::ZERO); export.next().await.unwrap().unwrap(); export.next().await.unwrap().unwrap(); @@ -1374,7 +1374,7 @@ async fn stalled_presentation_time_keeps_the_export_bounded() { config.jitter = Some(jitter); } let mut export = super::Export::new(live.source().await, live.catalog_stream().await) - .with_delay(delay) + .with_delay(moq_net::track::Delay::default().with_min(delay).with_max(delay)) .with_fragment_duration(Duration::ZERO); live.track.write(video_frame(pts(0), true)).unwrap(); export.next().await.unwrap().unwrap(); @@ -1404,7 +1404,7 @@ async fn live_reordered_video_keeps_every_frame() { config.jitter = Some(Duration::from_millis(120)); } let mut export = super::Export::new(live.source().await, live.catalog_stream().await) - .with_delay(Duration::from_millis(500)) + .with_delay(moq_net::track::Delay::default().with_min(Duration::from_millis(500)).with_max(Duration::from_millis(500))) .with_fragment_duration(Duration::ZERO); // A B-pyramid in decode order, a group of nine 40 ms frames at a time. let pyramid = [0, 4, 2, 1, 3, 8, 6, 5, 7]; diff --git a/rs/moq-mux/src/container/mkv/import.rs b/rs/moq-mux/src/container/mkv/import.rs index af47f50f382..091de0a9678 100644 --- a/rs/moq-mux/src/container/mkv/import.rs +++ b/rs/moq-mux/src/container/mkv/import.rs @@ -1439,7 +1439,7 @@ fn build_h265_config(codec_private: Option<&Bytes>) -> Result { let hvcc = mp4_atom::Hvcc::decode_body(&mut cursor).map_err(|_| Error::InvalidHvcc)?; let mut description = BytesMut::new(); - hvcc.encode_body(&mut description)?; + hvcc.encode_body(&mut description).map_err(crate::Error::mp4)?; let mut config = VideoConfig::new(H265 { in_band: false, @@ -1467,7 +1467,7 @@ fn build_av1_config(codec_private: Option<&Bytes>) -> Result { } let mut description = BytesMut::new(); - av1c.encode_body(&mut description)?; + av1c.encode_body(&mut description).map_err(crate::Error::mp4)?; let mut config = VideoConfig::new(crate::codec::av1::av1_from_av1c(&av1c)); config.color = crate::codec::av1::color(&av1c.config_obus); diff --git a/rs/moq-mux/src/container/mkv/import_test.rs b/rs/moq-mux/src/container/mkv/import_test.rs index 90756e1e0eb..3e15aadb366 100644 --- a/rs/moq-mux/src/container/mkv/import_test.rs +++ b/rs/moq-mux/src/container/mkv/import_test.rs @@ -1366,7 +1366,7 @@ fn sps_color_outranks_colour_which_fills_its_gaps() { let color = catalog.video.renditions.values().next().unwrap().color.clone().unwrap(); assert_eq!( (color.primaries, color.transfer, color.matrix, color.range), - (Some(1), Some(1), Some(1), Some(hang::catalog::Range::Limited)) + (Some(1), Some(1), Some(1), Some(hang::catalog::ColorRange::Limited)) ); assert_eq!( (color.mastering, color.content_light), diff --git a/rs/moq-mux/src/container/mp4.rs b/rs/moq-mux/src/container/mp4.rs index f4453203a44..44017594d9c 100644 --- a/rs/moq-mux/src/container/mp4.rs +++ b/rs/moq-mux/src/container/mp4.rs @@ -85,9 +85,9 @@ impl Export { } } - /// Set the max delay for each per-track source; see [`fmp4::Export::with_max_delay`]. - pub fn with_max_delay(mut self, max_delay: Duration) -> Self { - self.fmp4 = self.fmp4.with_max_delay(max_delay); + /// Set how far behind the live edge each per-track source plays; see [`fmp4::Export::with_delay`]. + pub fn with_delay(mut self, delay: moq_net::track::Delay) -> Self { + self.fmp4 = self.fmp4.with_delay(delay); self } @@ -132,9 +132,9 @@ impl Export { fn init(&mut self, init: &Bytes) -> Result { fmp4::metadata::check(init)?; let mut cursor = std::io::Cursor::new(init.as_ref()); - mp4_atom::Ftyp::decode(&mut cursor)?; + mp4_atom::Ftyp::decode(&mut cursor).map_err(crate::Error::mp4)?; let split = cursor.position() as usize; - let moov = mp4_atom::Moov::decode(&mut cursor)?; + let moov = mp4_atom::Moov::decode(&mut cursor).map_err(crate::Error::mp4)?; let mut data = BytesMut::with_capacity(init.len() + RESERVED as usize); data.put_slice(&init[..split]); @@ -157,7 +157,7 @@ impl Export { fn fragment(&mut self, fragment: Bytes) -> Result { fmp4::metadata::check(&fragment)?; let index = self.index.as_mut().expect("the init segment comes first"); - let moof = mp4_atom::Moof::decode(&mut std::io::Cursor::new(fragment.as_ref()))?; + let moof = mp4_atom::Moof::decode(&mut std::io::Cursor::new(fragment.as_ref())).map_err(crate::Error::mp4)?; let mut data = BytesMut::new(); for traf in &moof.traf { @@ -185,7 +185,7 @@ impl Export { kind: b"free".into(), size: Some(fragment.len()), } - .encode(&mut data)?; + .encode(&mut data).map_err(crate::Error::mp4)?; } index.fragment(self.written + data.len() as u64, &moof)?; data.put_slice(&fragment); @@ -318,7 +318,7 @@ impl Atom { let mut data = vec![0u8; (self.end - self.start) as usize]; file.seek(SeekFrom::Start(self.start))?; file.read_exact(&mut data)?; - Ok(T::decode(&mut std::io::Cursor::new(data.as_slice()))?) + Ok(T::decode(&mut std::io::Cursor::new(data.as_slice())).map_err(crate::Error::mp4)?) } } @@ -369,14 +369,14 @@ impl Description { ..Default::default() }; let mut buf = Vec::new(); - moov.encode(&mut buf)?; + moov.encode(&mut buf).map_err(crate::Error::mp4)?; mp4_atom::Header { kind: b"free".into(), size: Some(buf.len()), } - .encode(data)?; + .encode(data).map_err(crate::Error::mp4)?; data.put_slice(&buf); - Ok(mp4_atom::Moov::decode(&mut std::io::Cursor::new(buf.as_slice()))?) + Ok(mp4_atom::Moov::decode(&mut std::io::Cursor::new(buf.as_slice())).map_err(crate::Error::mp4)?) } } @@ -536,7 +536,7 @@ impl Index { ..Default::default() }; let mut buf = Vec::new(); - moov.encode(&mut buf)?; + moov.encode(&mut buf).map_err(crate::Error::mp4)?; // Everything from the reserved box to the moov, the fragmented moov and every moof // included, becomes one mdat that a reader skips through to the moov. @@ -798,7 +798,8 @@ mod tests { } async fn export(live: &Live) -> super::Export { - super::Export::new(live.source().await, live.catalog_stream().await).with_max_delay(MAX_DELAY) + super::Export::new(live.source().await, live.catalog_stream().await) + .with_delay(moq_net::track::Delay::default().with_max(MAX_DELAY)) } /// Append everything the export writes without waiting for more input. diff --git a/rs/moq-mux/src/container/source.rs b/rs/moq-mux/src/container/source.rs index 106addf2edc..c47ade4a67b 100644 --- a/rs/moq-mux/src/container/source.rs +++ b/rs/moq-mux/src/container/source.rs @@ -150,7 +150,8 @@ pub(crate) struct ExportSource { source: crate::Source, target: Target, state: SourceState, - max_delay: std::time::Duration, + /// Where the subscription starts behind the live edge, and how late a group may fall. + delay: moq_net::track::Delay, transform: Option, /// Resolved codec configuration record (avcC / hvcC / AudioSpecificConfig / /// OpusHead). Some once the codec config is available - from the catalog @@ -191,9 +192,9 @@ impl ExportSource { source: &crate::Source, name: &str, config: &VideoConfig, - max_delay: std::time::Duration, + delay: moq_net::track::Delay, ) -> Result, crate::Error> { - Self::video(source, name, config, max_delay, build_video_transform(config)) + Self::video(source, name, config, delay, build_video_transform(config)) } /// Subscribe to a video rendition for fMP4 export. @@ -205,7 +206,7 @@ impl ExportSource { source: &crate::Source, name: &str, config: &VideoConfig, - max_delay: std::time::Duration, + delay: moq_net::track::Delay, ) -> Result, crate::Error> { let record = annexb_catalog_record(config); let transform = match (&config.codec, record.is_some()) { @@ -213,7 +214,7 @@ impl ExportSource { (VideoCodec::H265(_), true) => Some(VideoTransform::InBand(InBand::new(InBandCodec::H265))), _ => build_video_transform(config), }; - let mut source = Self::video(source, name, config, max_delay, transform)?; + let mut source = Self::video(source, name, config, delay, transform)?; if let (Some(source), Some(record)) = (source.as_mut(), record) { source.description = Some(record); } @@ -230,16 +231,16 @@ impl ExportSource { source: &crate::Source, name: &str, config: &VideoConfig, - max_delay: std::time::Duration, + delay: moq_net::track::Delay, ) -> Result, crate::Error> { - Self::video(source, name, config, max_delay, None) + Self::video(source, name, config, delay, None) } fn video( source: &crate::Source, name: &str, config: &VideoConfig, - max_delay: std::time::Duration, + delay: moq_net::track::Delay, transform: Option, ) -> Result, crate::Error> { let media: HangContainer = config.try_into()?; @@ -247,7 +248,7 @@ impl ExportSource { return Ok(None); }; - let mut export = Self::new(source, name, target, media, max_delay); + let mut export = Self::new(source, name, target, media, delay); export.transform = transform; export.description = config.description.as_ref().filter(|b| !b.is_empty()).cloned(); export.video_codec = Some(config.codec.clone()); @@ -264,14 +265,14 @@ impl ExportSource { source: &crate::Source, name: &str, config: &AudioConfig, - max_delay: std::time::Duration, + delay: moq_net::track::Delay, ) -> Result, crate::Error> { let media: HangContainer = config.try_into()?; let Some(target) = Target::of(source, name, config) else { return Ok(None); }; - let mut export = Self::new(source, name, target, media, max_delay); + let mut export = Self::new(source, name, target, media, delay); export.description = config.description.as_ref().filter(|b| !b.is_empty()).cloned(); Ok(Some(export)) } @@ -285,7 +286,7 @@ impl ExportSource { pub fn for_stream( source: &crate::Source, name: &str, - max_delay: std::time::Duration, + delay: moq_net::track::Delay, ) -> Result { let target = Target { path: source.path().clone(), @@ -293,17 +294,23 @@ impl ExportSource { track: name.to_string(), }; let media = HangContainer::Legacy(crate::container::Kind::Data); - Ok(Self::new(source, name, target, media, max_delay)) + Ok(Self::new(source, name, target, media, delay)) } /// A source of rendition `name` reading `target`, with no codec configuration yet. - fn new(source: &crate::Source, name: &str, target: Target, media: HangContainer, max_delay: Duration) -> Self { + fn new( + source: &crate::Source, + name: &str, + target: Target, + media: HangContainer, + delay: moq_net::track::Delay, + ) -> Self { Self { name: name.to_string(), source: source.clone(), state: target.request(source, media), target, - max_delay, + delay, transform: None, description: None, video_codec: None, @@ -534,7 +541,7 @@ impl ExportSource { let broadcast = ready!(binding.poll_broadcast(waiter))?; let instance = binding.sibling().then(|| broadcast.clone()); let mut subscription = moq_net::track::Subscription::default() - .with_delay(moq_net::track::Delay::default().with_max(self.max_delay)); + .with_delay(self.delay); if let Some(after) = start.after { subscription = subscription.with_range((Bound::Excluded(after), Bound::Unbounded)); } @@ -890,7 +897,7 @@ mod tests { async fn escaping_reference_skips_the_rendition() { let live = Live::avc3(); let source = live.source().await; - let max_delay = std::time::Duration::ZERO; + let delay = moq_net::track::Delay::default(); let escaping = |result: Result, crate::Error>, what: &str| match result { Ok(None) => {} @@ -902,13 +909,13 @@ mod tests { // single-segment path, so its parent is the root and any `..` walks above it. for reference in ["..", "../source", "../../elsewhere"] { let config = video(Some(reference)); - escaping(ExportSource::for_video(&source, "video", &config, max_delay), reference); + escaping(ExportSource::for_video(&source, "video", &config, delay), reference); escaping( - ExportSource::for_video_raw(&source, "video", &config, max_delay), + ExportSource::for_video_raw(&source, "video", &config, delay), reference, ); escaping( - ExportSource::for_audio(&source, "audio", &audio(Some(reference)), max_delay), + ExportSource::for_audio(&source, "audio", &audio(Some(reference)), delay), reference, ); } @@ -920,13 +927,13 @@ mod tests { async fn legal_reference_keeps_the_rendition() { let live = Live::avc3(); let source = live.source().await; - let max_delay = std::time::Duration::ZERO; + let delay = moq_net::track::Delay::default(); for reference in [None, Some(""), Some("./source"), Some("sub"), Some(".")] { - ExportSource::for_video(&source, "video", &video(reference), max_delay) + ExportSource::for_video(&source, "video", &video(reference), delay) .unwrap_or_else(|err| panic!("{reference:?} should keep the rendition: {err:?}")) .unwrap_or_else(|| panic!("{reference:?} should keep the rendition")); - ExportSource::for_audio(&source, "audio", &audio(reference), max_delay) + ExportSource::for_audio(&source, "audio", &audio(reference), delay) .unwrap_or_else(|err| panic!("{reference:?} should keep the rendition: {err:?}")) .unwrap_or_else(|| panic!("{reference:?} should keep the rendition")); } @@ -946,7 +953,7 @@ mod tests { tokio::task::yield_now().await; let source = crate::source::resolved(&origin, "a/pub").await; let config = audio(Some("./source")); - let mut export = ExportSource::for_audio(&source, "audio", &config, Duration::from_secs(10)) + let mut export = ExportSource::for_audio(&source, "audio", &config, moq_net::track::Delay::default().with_max(Duration::from_secs(10))) .unwrap() .unwrap(); let mut read = async || { @@ -975,7 +982,7 @@ mod tests { assert_eq!(read().await, Some(None), "its end ends the rendition"); // A rendition still subscribing when its instance goes ends too. - let mut late = ExportSource::for_audio(&source, "late", &config, Duration::from_secs(10)) + let mut late = ExportSource::for_audio(&source, "late", &config, moq_net::track::Delay::default().with_max(Duration::from_secs(10))) .unwrap() .unwrap(); let ended = kio::clock::timeout(Duration::from_secs(1), kio::wait(|waiter| late.poll_read(waiter))).await; @@ -1000,7 +1007,7 @@ mod tests { tokio::task::yield_now().await; let source = crate::source::resolved(&origin, "a/pub").await; let config = audio(Some("./source")); - let mut export = ExportSource::for_audio(&source, "audio", &config, Duration::from_secs(10)) + let mut export = ExportSource::for_audio(&source, "audio", &config, moq_net::track::Delay::default().with_max(Duration::from_secs(10))) .unwrap() .unwrap(); @@ -1070,7 +1077,7 @@ mod tests { HangContainer::Legacy(crate::container::Kind::Audio), ); track.write(raw_frame(0, b"x", true)).unwrap(); - let mut export = ExportSource::for_audio(&source, "audio", &audio(Some("./source")), Duration::from_secs(10)) + let mut export = ExportSource::for_audio(&source, "audio", &audio(Some("./source")), moq_net::track::Delay::default().with_max(Duration::from_secs(10))) .unwrap() .unwrap(); let mut read = async || { @@ -1095,7 +1102,8 @@ mod tests { for live_start in [false, true] { let live = Live::avc3(); let max_delay = std::time::Duration::from_secs(10); - let export = ExportSource::for_video(&live.source().await, live.track.name(), &video(None), max_delay) + let delay = moq_net::track::Delay::default().with_max(max_delay); + let export = ExportSource::for_video(&live.source().await, live.track.name(), &video(None), delay) .unwrap() .expect("fixture should produce a video rendition"); let mut export = match live_start { @@ -1129,7 +1137,7 @@ mod tests { &live.source().await, live.track.name(), &audio(None), - std::time::Duration::from_secs(10), + moq_net::track::Delay::default().with_max(std::time::Duration::from_secs(10)), ) .unwrap() .unwrap() @@ -1172,7 +1180,7 @@ mod tests { .unwrap() .unwrap(); let source = crate::Source::new(consumer, remote); - let mut export = ExportSource::for_audio(&source, "audio", &audio(None), Duration::from_secs(1)) + let mut export = ExportSource::for_audio(&source, "audio", &audio(None), moq_net::track::Delay::default().with_max(Duration::from_secs(1))) .unwrap() .unwrap() .live(); @@ -1275,7 +1283,7 @@ mod tests { kio::clock::sleep(Duration::from_millis(120)).await; } - let mut export = ExportSource::for_audio(&source, "audio", &audio(None), Duration::from_millis(450)) + let mut export = ExportSource::for_audio(&source, "audio", &audio(None), moq_net::track::Delay::default().with_max(Duration::from_millis(450))) .unwrap() .unwrap() .live(); @@ -1320,15 +1328,15 @@ mod tests { for (read, first) in [(900, Some(1_000_000)), (1_500, None)] { let mut moving = Moving::new(); let source = moving.source().await; - let max_delay = Duration::from_secs(10); + let delay = moq_net::track::Delay::default().with_max(Duration::from_secs(10)); let mut previous = - ExportSource::for_video(&source, Moving::RENDITION, &Moving::config("a", None, SPS), max_delay) + ExportSource::for_video(&source, Moving::RENDITION, &Moving::config("a", None, SPS), delay) .unwrap() .unwrap(); previous.skips = 2; previous.latest = Some(Duration::from_millis(read)); let mut next = - ExportSource::for_video(&source, Moving::RENDITION, &Moving::config("b", None, SPS), max_delay) + ExportSource::for_video(&source, Moving::RENDITION, &Moving::config("b", None, SPS), delay) .unwrap() .unwrap() .continuing(&previous); @@ -1370,7 +1378,7 @@ mod tests { &live.source().await, live.track.name(), &audio(None), - Duration::from_secs(10), + moq_net::track::Delay::default().with_max(Duration::from_secs(10)), ) .unwrap() .unwrap(); diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index 91149d6ef5f..cd48c54623c 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -2497,7 +2497,7 @@ where .filter(|track| track.source.reads(&self.program.source, name, config)); let mut track = match reused { Some(track) => track, - None => match ExportSource::for_video(&self.program.source, name, config, self.budget())? + None => match ExportSource::for_video(&self.program.source, name, config, moq_net::track::Delay::default().with_max(self.budget()))? .map(|s| at_edge(s, self.program.replay)) { Some(source) => Track::new(source, kind.clone()), @@ -2518,7 +2518,7 @@ where .filter(|track| track.source.reads(&self.program.source, name, config)); let mut track = match reused { Some(track) => track, - None => match ExportSource::for_audio(&self.program.source, name, config, self.budget())? + None => match ExportSource::for_audio(&self.program.source, name, config, moq_net::track::Delay::default().with_max(self.budget()))? .map(|s| at_edge(s, self.program.replay)) { Some(source) => Track::new(source, kind.clone()), @@ -2546,7 +2546,7 @@ where Some(track) => track, None => Track::new( stream_edge( - ExportSource::for_stream(&self.program.source, name, self.budget())?, + ExportSource::for_stream(&self.program.source, name, moq_net::track::Delay::default().with_max(self.budget()))?, &kind, self.program.replay, ), @@ -2594,17 +2594,17 @@ where continue; } let source = if let Some(config) = catalog.video.renditions.get(name) { - ExportSource::for_video(&self.program.source, name, config, budget)? + ExportSource::for_video(&self.program.source, name, config, moq_net::track::Delay::default().with_max(budget))? .map(|s| at_edge(s, self.program.replay)) } else if let Some(config) = catalog.audio.renditions.get(name) { - ExportSource::for_audio(&self.program.source, name, config, budget)? + ExportSource::for_audio(&self.program.source, name, config, moq_net::track::Delay::default().with_max(budget))? .map(|s| at_edge(s, self.program.replay)) } else if mpegts .tracks .get(name) .is_some_and(|t| t.verbatim.is_some() || t.pes_packet.is_some()) { - let source = ExportSource::for_stream(&self.program.source, name, budget)?; + let source = ExportSource::for_stream(&self.program.source, name, moq_net::track::Delay::default().with_max(budget))?; Some(stream_edge(source, &track.kind, self.program.replay)) } else { None @@ -2639,13 +2639,13 @@ where if track.source.reads(&self.program.source, name, config) { continue; } - let source = ExportSource::for_video(&self.program.source, name, config, budget)?; + let source = ExportSource::for_video(&self.program.source, name, config, moq_net::track::Delay::default().with_max(budget))?; (source, video_kind(config, name)?) } else if let Some(config) = catalog.audio.renditions.get(name) { if track.source.reads(&self.program.source, name, config) { continue; } - let source = ExportSource::for_audio(&self.program.source, name, config, budget)?; + let source = ExportSource::for_audio(&self.program.source, name, config, moq_net::track::Delay::default().with_max(budget))?; (source, audio_kind(config, name)?) } else { continue; @@ -5111,7 +5111,7 @@ mod tests { let publisher = broadcast.create_track("video", hang::container::track_info(0)).unwrap(); let _publisher = Producer::new(publisher, Container::Legacy(MediaKind::Video)); let source = crate::source::announced(&broadcast.consume()).await; - let source = super::ExportSource::for_stream(&source, "video", Duration::from_secs(2)).unwrap(); + let source = super::ExportSource::for_stream(&source, "video", moq_net::track::Delay::default().with_max(Duration::from_secs(2))).unwrap(); let mut track = super::Track::new(source, Kind::Video(mpeg2ts::es::StreamType::H264)); let arrived = kio::clock::now(); for &(at, keyframe) in frames { @@ -5226,7 +5226,7 @@ mod tests { (1, "video", Kind::Video(mpeg2ts::es::StreamType::H264)), (2, "audio", Kind::Mp2 { sample_rate: 48_000 }), ] { - let source = super::ExportSource::for_stream(&source, name, Duration::from_secs(2)).unwrap(); + let source = super::ExportSource::for_stream(&source, name, moq_net::track::Delay::default().with_max(Duration::from_secs(2))).unwrap(); let mut track = super::Track::new(source, kind); track.pid = pid; tracks.push(track); diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index b6a47a44adf..98e38f1a56d 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -9157,7 +9157,7 @@ async fn the_same_instance_continues_after_a_drop() { /// A returning route serves the same publisher's catalog and monotonically numbered groups. #[tokio::test(start_paused = true)] async fn a_return_keeps_catalog_and_group_history() { - let wall = kio::clock::Wall::UNIX_EPOCH + Duration::from_millis(hang::catalog::MOQ_EPOCH_UNIX_MILLIS + 86_400_000); + let wall = kio::clock::Wall::MOQ_EPOCH + Duration::from_millis(86_400_000); crate::clock::fake_host_wall(wall); let start = kio::clock::now(); let origin = crate::source::produce_origin(); diff --git a/rs/moq-mux/src/container/ts/release.rs b/rs/moq-mux/src/container/ts/release.rs index 7b5dda0c650..d2dc85321ea 100644 --- a/rs/moq-mux/src/container/ts/release.rs +++ b/rs/moq-mux/src/container/ts/release.rs @@ -198,7 +198,7 @@ impl Release { }; match &self.track { None => { - let source = ExportSource::for_stream(&self.source, &m2ts.track, self.delay / 2)?; + let source = ExportSource::for_stream(&self.source, &m2ts.track, moq_net::track::Delay::default().with_max(self.delay / 2))?; let source = match self.replay { true => source, false => source.live(), diff --git a/rs/moq-mux/src/error.rs b/rs/moq-mux/src/error.rs index dd9fffac8d4..0d93dc43c5d 100644 --- a/rs/moq-mux/src/error.rs +++ b/rs/moq-mux/src/error.rs @@ -52,6 +52,10 @@ pub enum Error { #[error("mkv: {0}")] Mkv(#[from] crate::container::mkv::Error), + /// An FLV export error. + #[error("flv: {0}")] + Flv(#[from] crate::container::flv::Error), + /// Error decoding the MSF catalog. #[error("msf: {0}")] Msf(#[from] crate::catalog::msf::Error), @@ -110,7 +114,7 @@ pub enum Error { /// Error decoding or encoding an mp4 atom. #[error("mp4: {0}")] - Mp4(std::sync::Arc), + Mp4(#[source] std::sync::Arc), /// I/O error. #[error("io: {0}")] @@ -323,8 +327,9 @@ impl From for Error { } } -impl From for Error { - fn from(err: mp4_atom::Error) -> Self { +impl Error { + /// An mp4 atom error, kept opaque so an mp4-atom bump is no moq-mux break. + pub(crate) fn mp4(err: mp4_atom::Error) -> Self { Error::Mp4(std::sync::Arc::new(err)) } } diff --git a/rs/moq-mux/src/rate.rs b/rs/moq-mux/src/rate.rs index c61d8250dec..b922317a898 100644 --- a/rs/moq-mux/src/rate.rs +++ b/rs/moq-mux/src/rate.rs @@ -2,7 +2,7 @@ //! //! [`Control`] turns each grant from a [`bandwidth::Consumer`] into the target //! rate a sender should produce, so audio, video, and transcode back off the -//! same way. [`Follower`] drives it from a live grant. A rendition encoded beside +//! same way. [`Grant`] drives it from a live grant. A rendition encoded beside //! lower ones of the same source sets [`Policy::lower`], and stops once its grant //! can no longer fund it, so the lower renditions keep their share. @@ -138,7 +138,7 @@ pub enum Target { /// /// A raise climbs over several updates, and a grant that has settled sends no /// further estimate to climb on, so a sender following a live grant uses -/// [`Follower`], which also wakes when the ramp allows the next step. +/// [`Grant`], which also wakes when the ramp allows the next step. /// /// The time source is a parameter rather than an [`Instant::now`] call so the /// policy stays pure and testable. Pass the time the estimate was observed. @@ -207,7 +207,7 @@ impl Control { } /// Follow `policy` from the next [`update`](Self::update), returning whether it changed; - /// see [`Follower::set_policy`]. + /// see [`Grant::set_policy`]. fn set_policy(&mut self, policy: Policy) -> bool { if policy == self.policy { return false; @@ -424,7 +424,7 @@ impl Control { /// Follow a [`Reservation`](bandwidth::Reservation)'s consumer: it reports whether a /// session probes for it, which a rendition needs before it disables, and the slice it /// would get back while unwatched, which it needs to resume after its viewers left. -pub struct Follower { +pub struct Grant { grant: bandwidth::Consumer, control: Control, /// Armed at the control's deadline, created on first use. @@ -434,7 +434,7 @@ pub struct Follower { refeed: bool, } -impl Follower { +impl Grant { /// Follow `grant` under `policy`, starting at [`Policy::max`]. pub fn new(grant: bandwidth::Consumer, policy: Policy) -> Self { Self { @@ -452,7 +452,7 @@ impl Follower { /// Follow under `policy` from now on, feeding it the current grant on the next poll. /// - /// A new ceiling or floor starts over at its [`Policy::max`], as a fresh [`Follower`] + /// A new ceiling or floor starts over at its [`Policy::max`], as a fresh [`Grant`] /// would, since the sender applied that ceiling itself. A new /// [`lower`](Policy::lower) alone keeps the target and ramp, since nothing retuned /// the sender. Either way a disabled sender stays disabled until that grant resumes @@ -916,7 +916,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn a_follower_recovers_a_quiet_grant() { let grant = bandwidth::Producer::new(); - let mut follower = Follower::new(grant.consume(), Policy::new(bps(4_000_000))); + let mut follower = Grant::new(grant.consume(), Policy::new(bps(4_000_000))); grant.set(Some(bps(1_000_000))).unwrap(); assert_eq!(follower.next().await.unwrap(), on(bps(1_000_000))); @@ -948,7 +948,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn a_new_policy_feeds_the_current_grant() { let grant = bandwidth::Producer::new(); - let mut follower = Follower::new(grant.consume(), Policy::new(bps(4_000_000))); + let mut follower = Grant::new(grant.consume(), Policy::new(bps(4_000_000))); grant.set(Some(bps(3_000_000))).unwrap(); assert_eq!(follower.next().await.unwrap(), on(bps(3_000_000))); @@ -978,7 +978,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn a_raised_policy_follows_the_next_grant() { let grant = bandwidth::Producer::new(); - let mut follower = Follower::new(grant.consume(), Policy::new(bps(2_000_000))); + let mut follower = Grant::new(grant.consume(), Policy::new(bps(2_000_000))); grant.set(Some(bps(2_000_000))).unwrap(); assert!( kio::clock::timeout(Duration::from_secs(60), follower.next()) @@ -997,7 +997,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn a_new_lower_keeps_the_applied_target() { let grant = bandwidth::Producer::new(); - let mut follower = Follower::new(grant.consume(), Policy::new(bps(4_000_000))); + let mut follower = Grant::new(grant.consume(), Policy::new(bps(4_000_000))); grant.set(Some(bps(2_000_000))).unwrap(); assert_eq!(follower.next().await.unwrap(), on(bps(2_000_000))); @@ -1021,7 +1021,7 @@ mod tests { let grant = bandwidth::Producer::new(); let mut policy = Policy::new(bps(4_000_000)); policy.lower = Some(bps(2_000_000)); - let mut follower = Follower::new(grant.consume(), policy); + let mut follower = Grant::new(grant.consume(), policy); grant.set(Some(bps(1_000_000))).unwrap(); assert_eq!(follower.next().await.unwrap(), on(bps(1_000_000))); } @@ -1030,7 +1030,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn a_closed_grant_ends_the_follower() { let grant = bandwidth::Producer::new(); - let mut follower = Follower::new(grant.consume(), Policy::new(bps(4_000_000))); + let mut follower = Grant::new(grant.consume(), Policy::new(bps(4_000_000))); drop(grant); assert!(follower.next().await.is_err()); } @@ -1339,7 +1339,7 @@ mod tests { } /// Random walks of grants, probing, policy changes, refused changes, and time, - /// replayed through [`Control`] the way [`Follower`] drives it, checking the rung's + /// replayed through [`Control`] the way [`Grant`] drives it, checking the rung's /// invariants after every wake. mod model { use proptest::prelude::*; diff --git a/rs/moq-net/src/model/clock.rs b/rs/moq-net/src/model/clock.rs index f45f0b447eb..0884cdfd680 100644 --- a/rs/moq-net/src/model/clock.rs +++ b/rs/moq-net/src/model/clock.rs @@ -63,10 +63,7 @@ pub(super) fn anchor() -> (crate::time::Instant, Duration) { /// trimming a byte or two off the first frame's varint. Saturates to zero on a wall /// clock set before 2020 (an unsynced clock), since only a non-negative start matters. fn offset(wall: kio::clock::Wall, jitter: Duration) -> Duration { - const EPOCH: Duration = Duration::from_secs(1_577_836_800); - wall.saturating_duration_since(kio::clock::Wall::UNIX_EPOCH) - .saturating_sub(EPOCH) - .saturating_sub(jitter) + wall.saturating_duration_since(kio::clock::Wall::MOQ_EPOCH).saturating_sub(jitter) } #[cfg(test)] diff --git a/rs/moq-rtmp/src/dial.rs b/rs/moq-rtmp/src/dial.rs index 395f215bfa1..4489304e1f4 100644 --- a/rs/moq-rtmp/src/dial.rs +++ b/rs/moq-rtmp/src/dial.rs @@ -261,7 +261,7 @@ impl Client { .catalog::<()>(moq_mux::catalog::CatalogFormat::default()) .await .map_err(|e| anyhow::anyhow!("init FLV export: {e}"))?; - let mut export = FlvExport::new(source, catalog).with_delay(self.delay); + let mut export = FlvExport::new(source, catalog).with_delay(moq_net::track::Delay::default().with_min(self.delay).with_max(self.delay)); let mut tags = flv::TagReader::new(); let mut buffer = [0u8; READ_BUFFER]; diff --git a/rs/moq-rtmp/src/server.rs b/rs/moq-rtmp/src/server.rs index ffc4990937d..4b5ce04ddbe 100644 --- a/rs/moq-rtmp/src/server.rs +++ b/rs/moq-rtmp/src/server.rs @@ -890,7 +890,7 @@ impl Play { .map_err(|e| anyhow::anyhow!("init FLV export: {e}"))? .select(select); let mut export = FlvExport::new(source, stream) - .with_delay(self.delay) + .with_delay(moq_net::track::Delay::default().with_min(self.delay).with_max(self.delay)) .with_multitrack(self.capabilities.multitrack); // Resolve the catalog and codec headers before Play.Start, too. Otherwise a diff --git a/rs/moq-stats/benches/aggregate.rs b/rs/moq-stats/benches/aggregate.rs index 2dc0e54c795..47f71ffbe58 100644 --- a/rs/moq-stats/benches/aggregate.rs +++ b/rs/moq-stats/benches/aggregate.rs @@ -18,7 +18,8 @@ use std::collections::BTreeMap; use std::time::{Duration, Instant}; use moq_net::origin; -use moq_stats::{Group, Traffic, aggregate, group_track}; +use moq_net::stats::Traffic; +use moq_stats::{Group, aggregate, group_track}; /// Broadcast paths each node reports. const KEYS: usize = 16; diff --git a/rs/moq-stats/benches/decode.rs b/rs/moq-stats/benches/decode.rs index 329ce8465b6..2bceb8217d1 100644 --- a/rs/moq-stats/benches/decode.rs +++ b/rs/moq-stats/benches/decode.rs @@ -21,7 +21,8 @@ use std::time::{Duration, Instant}; use std::collections::BTreeMap; use moq_json::snapshot::{self, Decoder, Encoder}; -use moq_stats::{Group, GroupFrame, Traffic}; +use moq_net::stats::Traffic; +use moq_stats::{Group, GroupFrame}; /// A group frame as the producer writes it. #[derive(serde::Serialize)] diff --git a/rs/moq-stats/src/consume.rs b/rs/moq-stats/src/consume.rs index c599914e491..fb936a43e3b 100644 --- a/rs/moq-stats/src/consume.rs +++ b/rs/moq-stats/src/consume.rs @@ -186,7 +186,9 @@ mod tests { use moq_net::{Consume, Path, Timestamp, origin, track}; - use crate::{Producer, Tier, produce}; + use moq_net::stats::Tier; + + use crate::{Producer, produce}; use super::*; diff --git a/rs/moq-stats/src/lib.rs b/rs/moq-stats/src/lib.rs index c20ad0c6c68..da92086466a 100644 --- a/rs/moq-stats/src/lib.rs +++ b/rs/moq-stats/src/lib.rs @@ -21,7 +21,7 @@ //! [`parse_node_path`]. Each announcement carries a fresh [`Instance`](moq_net::Instance) on its //! route, so a restarted node is a new broadcast at the same path; a reader //! tells them apart by the announced route's instance. The broadcast carries a -//! `totals.json` track of each [`Tier`]'s [`Totals`] for the instance, and a +//! `totals.json` track of each [`Tier`](moq_net::stats::Tier)'s [`Totals`] for the instance, and a //! reader may request any stats label's `