From e47ead793e6b8fbcb5e5f13606ce4e966bec5f1c Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 15 Oct 2026 21:35:05 -0700 Subject: [PATCH 01/19] chore: claim moq-clock migration quest Co-Authored-By: GPT-5.4 From 5a7be23125ab165e4c44ac27470fa9d1cd5de990 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Thu, 15 Oct 2026 22:20:25 -0700 Subject: [PATCH 02/19] Migrate timing onto moq-clock --- Cargo.lock | 22 +- Cargo.toml | 3 +- quest/m1/time/README.md | 2 +- rs/AGENTS.md | 2 +- rs/moq-audio/src/encode/producer.rs | 6 +- rs/moq-c/src/publish.rs | 2 +- rs/moq-c/src/video.rs | 4 +- rs/moq-cli/src/play/timeline.rs | 17 +- rs/moq-clock/src/deadline.rs | 8 + rs/moq-clock/src/lib.rs | 11 + rs/moq-clock/src/runtime.rs | 5 + rs/moq-clock/src/sim.rs | 14 + rs/moq-e2ee/tests/support/harness.rs | 2 +- rs/moq-ffi/Cargo.toml | 1 + rs/moq-ffi/clippy.toml | 9 + rs/moq-ffi/src/lib.rs | 2 + rs/moq-ffi/src/media.rs | 4 +- rs/moq-ffi/src/session.rs | 2 +- rs/moq-ffi/src/test.rs | 6 +- rs/moq-ffi/src/video.rs | 3 +- rs/moq-gst/src/sink/imp.rs | 6 +- rs/moq-gst/src/sink/pad.rs | 63 +- rs/moq-hls/src/export/mod.rs | 4 +- rs/moq-mux/Cargo.toml | 3 +- rs/moq-mux/benches/jitter.rs | 6 +- rs/moq-mux/clippy.toml | 9 + rs/moq-mux/src/binary.rs | 17 +- rs/moq-mux/src/catalog/data.rs | 4 +- rs/moq-mux/src/catalog/estimate.rs | 17 +- rs/moq-mux/src/catalog/producer.rs | 23 +- rs/moq-mux/src/clock.rs | 77 +- rs/moq-mux/src/codec/aac/import.rs | 2 +- rs/moq-mux/src/codec/av1/import.rs | 2 +- rs/moq-mux/src/codec/av1/split.rs | 4 +- rs/moq-mux/src/codec/flac/import.rs | 2 +- rs/moq-mux/src/codec/h264/import.rs | 2 +- rs/moq-mux/src/codec/h264/split.rs | 4 +- rs/moq-mux/src/codec/h265/import.rs | 2 +- rs/moq-mux/src/codec/h265/split.rs | 4 +- rs/moq-mux/src/codec/mp3.rs | 2 +- rs/moq-mux/src/codec/opus/import.rs | 2 +- rs/moq-mux/src/codec/vp8/import.rs | 2 +- rs/moq-mux/src/codec/vp9/import.rs | 2 +- rs/moq-mux/src/container/flv/export.rs | 12 +- rs/moq-mux/src/container/flv/export_test.rs | 6 +- rs/moq-mux/src/container/mkv/export.rs | 12 +- rs/moq-mux/src/container/mkv/export_test.rs | 6 +- rs/moq-mux/src/container/producer.rs | 4 +- rs/moq-mux/src/container/ts/damage.rs | 6 +- rs/moq-mux/src/container/ts/export.rs | 32 +- rs/moq-mux/src/container/ts/export_test.rs | 4 +- .../src/container/ts/export_timing_test.rs | 22 +- rs/moq-mux/src/container/ts/follower.rs | 16 +- rs/moq-mux/src/container/ts/import.rs | 2 +- rs/moq-mux/src/container/ts/release.rs | 14 +- rs/moq-mux/src/container/ts/si.rs | 4 +- rs/moq-mux/src/import/track.rs | 4 +- rs/moq-mux/src/jitter.rs | 76 +- rs/moq-mux/src/json.rs | 4 +- rs/moq-mux/src/lib.rs | 2 + rs/moq-mux/src/pace.rs | 27 +- rs/moq-mux/src/rate.rs | 28 +- rs/moq-net/AGENTS.md | 2 +- rs/moq-net/Cargo.toml | 10 +- rs/moq-net/benches/memory.rs | 2 +- rs/moq-net/benches/track.rs | 10 +- rs/moq-net/clippy.toml | 9 + rs/moq-net/sim/Cargo.toml | 16 - rs/moq-net/sim/macros/Cargo.toml | 12 - rs/moq-net/sim/macros/src/lib.rs | 46 -- rs/moq-net/sim/src/lib.rs | 708 ------------------ rs/moq-net/src/auth.rs | 60 +- rs/moq-net/src/client.rs | 60 +- rs/moq-net/src/coding/stream.rs | 4 +- rs/moq-net/src/coding/writer.rs | 2 +- rs/moq-net/src/driver.rs | 6 +- rs/moq-net/src/goaway.rs | 8 +- rs/moq-net/src/ietf/adapter.rs | 30 +- rs/moq-net/src/ietf/auth.rs | 8 +- rs/moq-net/src/ietf/control.rs | 2 +- rs/moq-net/src/ietf/peer.rs | 2 +- rs/moq-net/src/ietf/publisher.rs | 428 +++++------ rs/moq-net/src/ietf/session.rs | 156 ++-- rs/moq-net/src/ietf/subscriber.rs | 482 ++++++------ rs/moq-net/src/lib.rs | 2 + rs/moq-net/src/lite/publisher.rs | 255 ++++--- rs/moq-net/src/lite/session.rs | 54 +- rs/moq-net/src/lite/subscriber.rs | 189 ++--- rs/moq-net/src/model/bandwidth.rs | 18 +- rs/moq-net/src/model/broadcast.rs | 46 +- rs/moq-net/src/model/clock.rs | 75 +- rs/moq-net/src/model/follow.rs | 20 +- rs/moq-net/src/model/group.rs | 8 +- rs/moq-net/src/model/origin.rs | 461 ++++++------ rs/moq-net/src/model/resume.rs | 4 +- rs/moq-net/src/model/track.rs | 154 ++-- rs/moq-net/src/priority.rs | 24 +- rs/moq-net/src/server.rs | 96 +-- rs/moq-net/src/session.rs | 4 +- rs/moq-net/src/tail.rs | 12 +- rs/moq-net/src/time.rs | 407 +--------- rs/moq-net/tests/announce_covering_route.rs | 12 +- rs/moq-net/tests/announce_to_serve.rs | 22 +- rs/moq-net/tests/auth.rs | 60 +- rs/moq-net/tests/broadcast_close.rs | 10 +- rs/moq-net/tests/catalog_acquisition.rs | 4 +- rs/moq-net/tests/datagram.rs | 16 +- rs/moq-net/tests/dial_split_horizon.rs | 12 +- rs/moq-net/tests/drained_claim.rs | 14 +- rs/moq-net/tests/empty_group.rs | 12 +- rs/moq-net/tests/extensions.rs | 12 +- rs/moq-net/tests/fetch_cancel.rs | 28 +- rs/moq-net/tests/fetch_caps.rs | 14 +- rs/moq-net/tests/fetch_from_feed.rs | 32 +- rs/moq-net/tests/fetch_only.rs | 22 +- rs/moq-net/tests/finished_broadcast_mock.rs | 14 +- rs/moq-net/tests/finite_relay.rs | 33 +- rs/moq-net/tests/goaway.rs | 42 +- rs/moq-net/tests/group_order.rs | 26 +- rs/moq-net/tests/head_fetch.rs | 22 +- rs/moq-net/tests/history_groups.rs | 30 +- rs/moq-net/tests/idle_cancel_refetch.rs | 18 +- rs/moq-net/tests/idle_session.rs | 16 +- rs/moq-net/tests/late_lower_group.rs | 30 +- rs/moq-net/tests/mesh_withdraw.rs | 48 +- rs/moq-net/tests/multi_cdn.rs | 16 +- rs/moq-net/tests/quiet_catalog.rs | 22 +- rs/moq-net/tests/rejoin.rs | 61 +- rs/moq-net/tests/replacement.rs | 24 +- rs/moq-net/tests/request_caps.rs | 22 +- rs/moq-net/tests/restart.rs | 46 +- rs/moq-net/tests/route_change.rs | 72 +- rs/moq-net/tests/session_close.rs | 78 +- rs/moq-net/tests/session_panic.rs | 12 +- .../tests/settled_end_survives_abort.rs | 6 +- rs/moq-net/tests/subscribe_tracks.rs | 18 +- .../tests/subscription_end_integrity.rs | 28 +- rs/moq-net/tests/support/harness.rs | 13 +- rs/moq-net/tests/support/mock.rs | 8 +- rs/moq-net/tests/track_abort_over_session.rs | 10 +- rs/moq-net/tests/track_info_cancel.rs | 8 +- rs/moq-net/tests/track_stream_demand.rs | 44 +- rs/moq-net/tests/track_tail.rs | 60 +- rs/moq-net/tests/unannounce_release.rs | 6 +- rs/moq-net/tests/untimed.rs | 30 +- rs/moq-net/tests/wide_sequence.rs | 8 +- rs/moq-net/tests/zero_copy.rs | 8 +- rs/moq-relay/src/auth/refresh.rs | 6 +- rs/moq-relay/src/web.rs | 2 +- rs/moq-relay/tests/auth_lifetime.rs | 2 +- rs/moq-room/Cargo.toml | 2 +- rs/moq-room/clippy.toml | 9 + rs/moq-room/src/chat.rs | 14 +- rs/moq-room/src/lib.rs | 2 + rs/moq-srt/src/server.rs | 31 +- rs/moq-stats/Cargo.toml | 1 + rs/moq-stats/clippy.toml | 9 + rs/moq-stats/src/aggregate.rs | 10 +- rs/moq-stats/src/lib.rs | 2 + rs/moq-stats/src/produce.rs | 14 +- rs/moq-stats/src/produce_bench.rs | 4 +- rs/moq-tokio/src/client.rs | 4 +- rs/moq-tokio/src/connection/auth.rs | 2 +- rs/moq-tokio/src/server.rs | 14 +- rs/moq-transcode/src/catalog.rs | 4 +- rs/moq-uring/src/worker.rs | 5 +- rs/moq-uring/tests/session.rs | 8 +- rs/moq-uring/tests/web.rs | 4 +- rs/moq-video/src/encode/producer.rs | 4 +- 169 files changed, 2390 insertions(+), 3469 deletions(-) create mode 100644 rs/moq-ffi/clippy.toml create mode 100644 rs/moq-mux/clippy.toml create mode 100644 rs/moq-net/clippy.toml delete mode 100644 rs/moq-net/sim/Cargo.toml delete mode 100644 rs/moq-net/sim/macros/Cargo.toml delete mode 100644 rs/moq-net/sim/macros/src/lib.rs delete mode 100644 rs/moq-net/sim/src/lib.rs create mode 100644 rs/moq-room/clippy.toml create mode 100644 rs/moq-stats/clippy.toml diff --git a/Cargo.lock b/Cargo.lock index 265bb96f13d..d0758074386 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4483,6 +4483,7 @@ dependencies = [ "kio 0.6.2", "libc", "moq-audio", + "moq-clock", "moq-json", "moq-mux", "moq-net", @@ -4612,6 +4613,7 @@ dependencies = [ "hang", "kio 0.6.2", "memchr", + "moq-clock", "moq-flate", "moq-json", "moq-loc", @@ -4630,7 +4632,6 @@ dependencies = [ "tracing", "tracing-test", "url", - "web-async", "webm-iterable", ] @@ -4649,7 +4650,7 @@ dependencies = [ "getrandom 0.4.3", "kio 0.6.2", "loom", - "moq-net-sim", + "moq-clock", "moq-pattern", "num_enum", "rand 0.10.3", @@ -4661,7 +4662,6 @@ dependencies = [ "tracing", "uuid", "wasm-bindgen-test", - "web-async", ] [[package]] @@ -4672,19 +4672,6 @@ dependencies = [ "moq-net", ] -[[package]] -name = "moq-net-sim" -version = "0.0.0" -dependencies = [ - "futures", - "kio 0.6.2", - "moq-net-sim-macros", -] - -[[package]] -name = "moq-net-sim-macros" -version = "0.0.0" - [[package]] name = "moq-noq" version = "2.0.5" @@ -4857,12 +4844,12 @@ version = "0.2.11" dependencies = [ "kio 0.6.2", "moq-auth", + "moq-clock", "moq-json", "moq-net", "moq-tokio", "thiserror 2.0.21", "tokio", - "web-async", ] [[package]] @@ -4964,6 +4951,7 @@ version = "0.2.12" dependencies = [ "criterion", "futures", + "moq-clock", "moq-json", "moq-net", "serde", diff --git a/Cargo.toml b/Cargo.toml index 19a3faf7eed..cd0336d0705 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,8 +24,6 @@ members = [ "rs/moq-native", "rs/moq-net", "rs/moq-net/fuzz", - "rs/moq-net/sim", - "rs/moq-net/sim/macros", "rs/moq-nvenc", "rs/moq-pattern", "rs/moq-quic", @@ -150,6 +148,7 @@ moq-archive = { version = "0.0.11", path = "rs/moq-archive" } moq-audio = { version = "0.1.10", path = "rs/moq-audio", default-features = false } moq-auth = { version = "0.2.1", path = "rs/moq-auth" } moq-clock-macros = { version = "0.1.0", path = "rs/moq-clock/macros" } +moq-clock = { version = "0.11.0", path = "rs/moq-clock" } moq-flate = { version = "0.2.0", path = "rs/moq-flate" } moq-hls = { version = "0.5.11", path = "rs/moq-hls", default-features = false } moq-json = { version = "0.5.8", path = "rs/moq-json" } diff --git a/quest/m1/time/README.md b/quest/m1/time/README.md index 9b070511dd7..f132bb199f5 100644 --- a/quest/m1/time/README.md +++ b/quest/m1/time/README.md @@ -63,7 +63,7 @@ reasons, so later sessions don't reopen them: dev-dependency only, so production builds never compile it). It is one backend of the ambient clock, starts at zero, keeps deadlock detection, and seeds the later runtime trait. `#[moq_clock::test]` replaces - `#[moq_net_sim::test]`. + `#[moq_clock::test]`. - **Tests for migrated contracts use the sim wherever possible.** A test with no real I/O that cannot run on the sim yet uses paused tokio, whose clock `moq-clock` reads. A real-I/O test never uses paused tokio, which auto-advances to the diff --git a/rs/AGENTS.md b/rs/AGENTS.md index d9bd16d6b4a..ff8a2d8514b 100644 --- a/rs/AGENTS.md +++ b/rs/AGENTS.md @@ -50,7 +50,7 @@ Prefer poll. New logic is a `poll_*` with an `async` helper, not the other way a # Testing -- Tests are inline `#[cfg(test)] mod tests`, except full Client/Server session tests over a crate's shared mock harness, which go in its `tests/` directory. Time-dependent async tests call `tokio::time::pause()` first (moq-net uses `#[moq_net_sim::test]` instead), unless they cross real networking that can't be mocked (sockets, smoke tests); those run on the wall clock and assert lower bounds. +- Tests are inline `#[cfg(test)] mod tests`, except full Client/Server session tests over a crate's shared mock harness, which go in its `tests/` directory. Time-dependent async tests call `tokio::time::pause()` first (moq-net uses `#[moq_clock::test]` instead), unless they cross real networking that can't be mocked (sockets, smoke tests); those run on the wall clock and assert lower bounds. - Run tests through `just` (nextest), not `cargo test`: nextest kills a wedged test as TIMEOUT, cargo hangs forever. A test flagged SLOW is a bug to fix, not a threshold to raise. - `just check` compiles default features only, like CI. `just rs features` (nightly) covers `--all-features` / `--no-default-features`. Keep a feature gate around the dependency, not the logic, so the logic's tests stay in the merge gate. - Local checks compile only the host platform; PR CI runs `just rs windows` / `macos` on those hosts, and `just rs wasm` covers `moq-wasm`. diff --git a/rs/moq-audio/src/encode/producer.rs b/rs/moq-audio/src/encode/producer.rs index 85465c01af4..93808025d63 100644 --- a/rs/moq-audio/src/encode/producer.rs +++ b/rs/moq-audio/src/encode/producer.rs @@ -1,7 +1,5 @@ //! Encode raw PCM and publish it as a moq audio track. -use std::time::Instant; - use bytes::Bytes; use moq_mux::catalog::hang::CatalogExt; @@ -442,7 +440,7 @@ impl Producer { if self.grouped >= self.group_samples { self.track.cut(None)?; } - self.track.flush(timestamp, Instant::now())?; + self.track.flush(timestamp, moq_net::time::now())?; Ok(()) } @@ -468,7 +466,7 @@ impl Producer { keyframe: false, duration: None, })?; - track.flush(timestamp, Instant::now())?; + track.flush(timestamp, moq_net::time::now())?; } track.cut(Some(terminal.end))?; diff --git a/rs/moq-c/src/publish.rs b/rs/moq-c/src/publish.rs index 1433db00996..9db7b9e9c3d 100644 --- a/rs/moq-c/src/publish.rs +++ b/rs/moq-c/src/publish.rs @@ -246,7 +246,7 @@ impl Publish { /// unless their caller explicitly identifies the frame as encoder output. pub fn media_flush(&mut self, media: Id, timestamp: hang::container::Timestamp) -> Result<(), Error> { let track = self.media.get_mut(media).ok_or(Error::MediaNotFound)?; - track.flush(timestamp, std::time::Instant::now())?; + track.flush(timestamp, moq_net::time::now())?; Ok(()) } diff --git a/rs/moq-c/src/video.rs b/rs/moq-c/src/video.rs index bccaf89b9f1..a182f2121fe 100644 --- a/rs/moq-c/src/video.rs +++ b/rs/moq-c/src/video.rs @@ -15,7 +15,7 @@ use std::ffi::{c_char, c_void}; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, OnceLock}; -use std::time::{Duration, Instant}; +use std::time::Duration; use tokio::sync::oneshot; @@ -232,7 +232,7 @@ async fn follow_reservation( max = next; control = Control::new(Policy::new(max)); } - let Some(bitrate) = control.update(estimate, Instant::now()) else { + let Some(bitrate) = control.update(estimate, moq_net::time::now()) else { continue; }; diff --git a/rs/moq-cli/src/play/timeline.rs b/rs/moq-cli/src/play/timeline.rs index 53f5e41ece0..61ebf88f66a 100644 --- a/rs/moq-cli/src/play/timeline.rs +++ b/rs/moq-cli/src/play/timeline.rs @@ -1,6 +1,7 @@ //! Media time: the playout clock, and the audio track's own timeline. -use std::time::{Duration, Instant}; +use hang::moq_net::time::Instant; +use std::time::Duration; use hang::moq_net::Timestamp; @@ -242,7 +243,7 @@ mod tests { /// edge without giving up the configured delay. #[test] fn a_late_first_frame_catches_up_to_live() { - let start = Instant::now(); + let start = hang::moq_net::time::now(); let mut presentation = Presentation::new(DELAY); // Produced 500ms ago, though nothing here knows that yet. The window is @@ -267,7 +268,7 @@ mod tests { /// video burst must not move the anchor out from under it. #[test] fn the_speaker_owns_the_anchor_while_it_plays() { - let start = Instant::now(); + let start = hang::moq_net::time::now(); let mut presentation = Presentation::new(DELAY); presentation.audio(Duration::from_secs(1), DELAY, start); @@ -293,7 +294,7 @@ mod tests { /// behind the sound. #[test] fn the_speaker_anchors_at_the_live_edge() { - let start = Instant::now(); + let start = hang::moq_net::time::now(); let mut presentation = Presentation::new(DELAY); // The last sample written is a full delay from sounding, which is exactly @@ -313,7 +314,7 @@ mod tests { /// the sink would play now while the picture stayed on video's timeline. #[test] fn the_speaker_re_anchors_when_it_takes_over() { - let start = Instant::now(); + let start = hang::moq_net::time::now(); let mut presentation = Presentation::new(DELAY); presentation.video(ms(4_000), start); @@ -333,7 +334,7 @@ mod tests { /// would schedule video against samples the speaker never plays. #[test] fn a_restarted_speaker_re_anchors() { - let start = Instant::now(); + let start = hang::moq_net::time::now(); let mut presentation = Presentation::new(DELAY); presentation.audio(Duration::from_secs(10), DELAY, start); @@ -350,7 +351,7 @@ mod tests { /// restart left over would re-pin the frame after it onto that frame's jitter. #[test] fn a_stopped_speaker_drops_a_pending_restart() { - let start = Instant::now(); + let start = hang::moq_net::time::now(); let mut presentation = Presentation::new(DELAY); presentation.audio(Duration::from_secs(10), DELAY, start); presentation.restarted(); @@ -371,7 +372,7 @@ mod tests { /// speaker moved has to say so or the queue presents late by that much. #[test] fn a_moved_anchor_is_reported() { - let start = Instant::now(); + let start = hang::moq_net::time::now(); let mut presentation = Presentation::new(DELAY); assert!( presentation.audio(Duration::from_secs(1), DELAY, start), diff --git a/rs/moq-clock/src/deadline.rs b/rs/moq-clock/src/deadline.rs index 5fc9c676215..7641fa53d2d 100644 --- a/rs/moq-clock/src/deadline.rs +++ b/rs/moq-clock/src/deadline.rs @@ -208,6 +208,14 @@ impl Deadline { } } +impl kio::Task for Deadline { + type Output = (); + + fn poll(&mut self, waiter: &kio::Waiter) -> Poll<()> { + self.poll(waiter) + } +} + impl Drop for Deadline { fn drop(&mut self) { self.set(None); diff --git a/rs/moq-clock/src/lib.rs b/rs/moq-clock/src/lib.rs index c32128e6ccb..70b73f87d16 100644 --- a/rs/moq-clock/src/lib.rs +++ b/rs/moq-clock/src/lib.rs @@ -63,3 +63,14 @@ pub fn wall() -> Wall { } runtime::wall() } + +/// The first monotonic and wall readings observed on the ambient clock. +/// +/// Unlike [`wall`], the wall half does not change after a clock correction. +pub fn anchor() -> (Instant, Wall) { + #[cfg(feature = "sim")] + if let Some(anchor) = sim::anchor() { + return anchor; + } + runtime::anchor() +} diff --git a/rs/moq-clock/src/runtime.rs b/rs/moq-clock/src/runtime.rs index 893d59fb039..eb76abba509 100644 --- a/rs/moq-clock/src/runtime.rs +++ b/rs/moq-clock/src/runtime.rs @@ -17,6 +17,7 @@ use crate::{Instant, Wall}; /// Where every unpaused clock counts from, so instants read on any thread, in any /// runtime or none, stay comparable. static ORIGIN: OnceLock = OnceLock::new(); +static ANCHOR: OnceLock<(Instant, Wall)> = OnceLock::new(); /// The origin this context counts from, and a reading of its clock. /// @@ -49,6 +50,10 @@ pub(crate) fn wall() -> Wall { Wall::UNIX_EPOCH + since } +pub(crate) fn anchor() -> (Instant, Wall) { + *ANCHOR.get_or_init(|| (now(), wall())) +} + /// A host timer for `at`, on the origin it was created with. pub(crate) struct Timer { origin: host::Instant, diff --git a/rs/moq-clock/src/sim.rs b/rs/moq-clock/src/sim.rs index 926447a982a..eae459f48ac 100644 --- a/rs/moq-clock/src/sim.rs +++ b/rs/moq-clock/src/sim.rs @@ -49,6 +49,7 @@ struct Runtime { struct Time { clock: Manual, + anchor: Option<(Instant, Wall)>, /// Orders timers armed for the same instant by when they were armed. next: u64, timers: BTreeMap<(Instant, u64), Waker>, @@ -131,6 +132,18 @@ pub(crate) fn wall() -> Option { current().map(|runtime| runtime.time.borrow().clock.wall()) } +pub(crate) fn anchor() -> Option<(Instant, Wall)> { + current().map(|runtime| { + let mut time = runtime.time.borrow_mut(); + if let Some(anchor) = time.anchor { + return anchor; + } + let anchor = (time.clock.now(), time.clock.wall()); + time.anchor = Some(anchor); + anchor + }) +} + /// Whether the calling thread is inside [`run`]. pub fn is_running() -> bool { CURRENT.with(|current| current.borrow().is_some()) @@ -169,6 +182,7 @@ pub fn run(future: F) -> F::Output { spawned: RefCell::new(Vec::new()), time: RefCell::new(Time { clock: Manual::new(), + anchor: None, next: 0, timers: BTreeMap::new(), }), diff --git a/rs/moq-e2ee/tests/support/harness.rs b/rs/moq-e2ee/tests/support/harness.rs index aadec66bcd0..71844103988 100644 --- a/rs/moq-e2ee/tests/support/harness.rs +++ b/rs/moq-e2ee/tests/support/harness.rs @@ -14,7 +14,7 @@ use moq_net::transport::memory; pub use moq_net::time::run; pub fn now() -> moq_net::time::Instant { - tokio::time::Instant::now().into_std() + moq_net::time::now() } /// Options for [`connect_mock`]. diff --git a/rs/moq-ffi/Cargo.toml b/rs/moq-ffi/Cargo.toml index 1820ca2612c..ee2f6d5738e 100644 --- a/rs/moq-ffi/Cargo.toml +++ b/rs/moq-ffi/Cargo.toml @@ -35,6 +35,7 @@ hang = { workspace = true } # `kio::MaybeSend` is the `Send` bound that vanishes on wasm; see `ffi::Task`. kio = { workspace = true } moq-json = { workspace = true } +moq-clock = { workspace = true } moq-mux = { workspace = true } moq-net = { workspace = true } serde_json = { workspace = true } diff --git a/rs/moq-ffi/clippy.toml b/rs/moq-ffi/clippy.toml new file mode 100644 index 00000000000..892532bf144 --- /dev/null +++ b/rs/moq-ffi/clippy.toml @@ -0,0 +1,9 @@ +disallowed-methods = [ + { path = "std::time::Instant::now", reason = "read the ambient clock: moq_clock::now()" }, + { path = "std::time::SystemTime::now", reason = "read the ambient clock: moq_clock::wall()" }, + { path = "tokio::time::Instant::now", reason = "read the ambient clock: moq_clock::now()" }, + { path = "tokio::time::sleep", reason = "sleep on the ambient clock: moq_clock::sleep()" }, + { path = "tokio::time::sleep_until", reason = "sleep on the ambient clock: moq_clock::sleep_until()" }, + { path = "tokio::time::timeout", reason = "use the ambient clock: moq_clock::timeout()" }, + { path = "tokio::time::interval", reason = "use the ambient clock: moq_clock::interval()" }, +] diff --git a/rs/moq-ffi/src/lib.rs b/rs/moq-ffi/src/lib.rs index 090bd368e06..827cf6334ea 100644 --- a/rs/moq-ffi/src/lib.rs +++ b/rs/moq-ffi/src/lib.rs @@ -1,3 +1,5 @@ +#![cfg_attr(test, allow(clippy::disallowed_methods))] + //! UniFFI bindings for [`moq_net`]. //! //! Provides a Kotlin/Swift-compatible API for real-time pub/sub over QUIC. diff --git a/rs/moq-ffi/src/media.rs b/rs/moq-ffi/src/media.rs index 23c748352d7..f19120a2d39 100644 --- a/rs/moq-ffi/src/media.rs +++ b/rs/moq-ffi/src/media.rs @@ -930,7 +930,7 @@ impl MoqMediaTrackProducer { let timestamp = moq_net::Timestamp::from_micros(timestamp_us)?; let mut guard = self.inner.lock().unwrap(); let media = guard.as_mut().ok_or(MoqError::Closed)?; - media.import.flush(timestamp, std::time::Instant::now())?; + media.import.flush(timestamp, moq_clock::Instant::now())?; Ok(()) } @@ -1689,7 +1689,7 @@ mod timebase_tests { reserve(); } for touched in [1, 16, 256] { - let start = std::time::Instant::now(); + let start = moq_clock::Instant::now(); for _ in 0..1000 { for _ in 0..touched { reserve(); diff --git a/rs/moq-ffi/src/session.rs b/rs/moq-ffi/src/session.rs index a0fc995fac4..ec9f6b55266 100644 --- a/rs/moq-ffi/src/session.rs +++ b/rs/moq-ffi/src/session.rs @@ -489,7 +489,7 @@ impl Client { .with_versions(self.versions.clone()) .with_publisher(&publish) .with_subscriber(subscribe.clone()) - .connect(web_async::time::Instant::now(), transport) + .connect(moq_clock::now(), transport) .await?; crate::ffi::spawn(async move { diff --git a/rs/moq-ffi/src/test.rs b/rs/moq-ffi/src/test.rs index 633ec70bba1..3249c6139f1 100644 --- a/rs/moq-ffi/src/test.rs +++ b/rs/moq-ffi/src/test.rs @@ -5016,13 +5016,13 @@ async fn video_encoder_follows_a_shrinking_grant() { .unwrap(); assert_eq!(reservation.grant(), Some(1_000_000)); - let deadline = std::time::Instant::now() + TIMEOUT; + let deadline = moq_clock::Instant::now() + TIMEOUT; loop { if video.applied_bitrate() == 1_000_000 { break; } assert!( - std::time::Instant::now() < deadline, + moq_clock::Instant::now() < deadline, "encoder did not follow the shrinking grant, last applied {}", video.applied_bitrate() ); @@ -5798,7 +5798,7 @@ async fn shutdown_times_out_on_an_unfinished_track() { .unwrap(); shutdown_ready(&track, &reader).await; let receiving = tokio::spawn(async move { reader.read_frame().await }); - let start = std::time::Instant::now(); + let start = moq_clock::Instant::now(); let err = tokio::time::timeout(TIMEOUT, publisher.shutdown()) .await .expect("shutdown") diff --git a/rs/moq-ffi/src/video.rs b/rs/moq-ffi/src/video.rs index 94c5c86e61b..5b9234945b4 100644 --- a/rs/moq-ffi/src/video.rs +++ b/rs/moq-ffi/src/video.rs @@ -12,7 +12,6 @@ use std::sync::Arc; use std::sync::atomic::{AtomicU64, Ordering}; -use std::time::Instant; use tokio::sync::oneshot; @@ -544,7 +543,7 @@ async fn follow_reservation( max = next; control = Control::new(Policy::new(max)); } - let Some(bitrate) = control.update(estimate, Instant::now()) else { + let Some(bitrate) = control.update(estimate, moq_clock::now()) else { continue; }; diff --git a/rs/moq-gst/src/sink/imp.rs b/rs/moq-gst/src/sink/imp.rs index d697bc455c2..c10a29aaf3a 100644 --- a/rs/moq-gst/src/sink/imp.rs +++ b/rs/moq-gst/src/sink/imp.rs @@ -11,7 +11,7 @@ use std::collections::HashSet; use std::sync::{LazyLock, Mutex}; -use std::time::{Duration, Instant}; +use std::time::Duration; use anyhow::{Context, Result}; use bytes::Bytes; @@ -728,7 +728,7 @@ impl MoqSink { let outcome = lifecycle.media.place_wall(&buffer, sample).and_then(|()| { lifecycle .media - .push_buffer(data, pts, duration, current_running_time, Instant::now()) + .push_buffer(data, pts, duration, current_running_time, moq_net::time::now()) .map_err(anyhow::Error::msg) }); let changes = match &outcome { @@ -1720,7 +1720,7 @@ mod tests { let segment = gst::FormattedSegment::::new(); pad.lifecycle().media.observe_segment(segment.upcast()); sink.set_state(gst::State::Playing).unwrap(); - let anchor = Instant::now(); + let anchor = moq_net::time::now(); for (pts, arrival) in [(0, 0), (20, 120), (40, 6_000), (60, 6_020)] { if pts == 40 { sink.set_state(gst::State::Paused).unwrap(); diff --git a/rs/moq-gst/src/sink/pad.rs b/rs/moq-gst/src/sink/pad.rs index b15dff8cd2a..986327bc5f3 100644 --- a/rs/moq-gst/src/sink/pad.rs +++ b/rs/moq-gst/src/sink/pad.rs @@ -4,7 +4,8 @@ //! that pad's own streaming thread, so this type is touched from one thread and needs no generation //! tagging or cross-thread failure map. -use std::time::{Duration, Instant}; +use moq_net::time::Instant; +use std::time::Duration; use anyhow::{Context, Result, ensure}; use bytes::Bytes; @@ -1190,7 +1191,7 @@ mod tests { Some(gst::ClockTime::ZERO), None, None, - Instant::now(), + moq_net::time::now(), ) .unwrap(); @@ -1328,7 +1329,7 @@ mod tests { .subscribe(moq_net::track::Subscription::default().with_max_delay(Duration::from_secs(1))) .await .unwrap(); - let anchor = Instant::now(); + let anchor = moq_net::time::now(); for index in 0..17 { assert_eq!( pad.push_buffer( @@ -1546,7 +1547,7 @@ mod tests { Some(gst::ClockTime::from_mseconds(20 * index as u64)), None, None, - Instant::now(), + moq_net::time::now(), ) .unwrap(), PushOutcome::Published @@ -1579,7 +1580,7 @@ mod tests { Some(gst::ClockTime::ZERO), None, None, - Instant::now() + moq_net::time::now() ) .unwrap(), PushOutcome::NoSegment, @@ -1591,7 +1592,7 @@ mod tests { Some(gst::ClockTime::ZERO), None, None, - Instant::now() + moq_net::time::now() ) .unwrap(), PushOutcome::Dropped, @@ -1653,7 +1654,7 @@ mod tests { Some(gst::ClockTime::ZERO), None, None, - Instant::now(), + moq_net::time::now(), ) .unwrap(); @@ -1687,7 +1688,7 @@ mod tests { Some(gst::ClockTime::from_mseconds(40)), None, None, - Instant::now(), + moq_net::time::now(), ) .unwrap(); pad.push_buffer( @@ -1695,7 +1696,7 @@ mod tests { Some(gst::ClockTime::from_mseconds(80)), None, None, - Instant::now(), + moq_net::time::now(), ) .unwrap(); @@ -1755,7 +1756,7 @@ mod tests { Some(gst::ClockTime::ZERO), None, None, - Instant::now(), + moq_net::time::now(), ) .unwrap(); @@ -1825,7 +1826,7 @@ mod tests { None, None, Some(gst::ClockTime::from_mseconds(25)), - Instant::now(), + moq_net::time::now(), ) .unwrap(); assert!(!pad.is_failed(), "a missing PTS uses the supplied running time"); @@ -1860,8 +1861,14 @@ mod tests { pad.observe_segment(time_segment()); assert!( - pad.push_buffer(Bytes::from_static(b"no timestamp"), None, None, None, Instant::now()) - .is_err(), + pad.push_buffer( + Bytes::from_static(b"no timestamp"), + None, + None, + None, + moq_net::time::now() + ) + .is_err(), "the caller gets a hard error instead of a silent drop" ); } @@ -1884,7 +1891,7 @@ mod tests { Some(gst::ClockTime::ZERO), None, None, - Instant::now(), + moq_net::time::now(), ) .unwrap(); } @@ -1920,7 +1927,13 @@ mod tests { au.extend_from_slice(nal); } let outcome = pad - .push_buffer(Bytes::from(au), Some(gst::ClockTime::ZERO), None, None, Instant::now()) + .push_buffer( + Bytes::from(au), + Some(gst::ClockTime::ZERO), + None, + None, + moq_net::time::now(), + ) .unwrap(); assert_eq!(outcome, PushOutcome::Published); assert!(!pad.is_failed()); @@ -1941,7 +1954,7 @@ mod tests { Some(gst::ClockTime::ZERO), None, None, - Instant::now(), + moq_net::time::now(), ) .unwrap(); @@ -2144,7 +2157,7 @@ mod tests { Some(gst::ClockTime::ZERO), Some(gst::ClockTime::from_seconds(1)), None, - Instant::now(), + moq_net::time::now(), ) .unwrap(); assert!(pad.is_failed()); @@ -2171,7 +2184,7 @@ mod tests { Some(gst::ClockTime::from_mseconds(start_ms)), Some(gst::ClockTime::from_mseconds(dur_ms)), None, - Instant::now(), + moq_net::time::now(), ) .unwrap(); } @@ -2195,7 +2208,7 @@ mod tests { Some(gst::ClockTime::ZERO), None, None, - Instant::now(), + moq_net::time::now(), ) .unwrap(); assert!(!pad.is_failed(), "a durationless cue drops the buffer, not the pad"); @@ -2253,7 +2266,7 @@ mod tests { import.observe_caps(&broadcast, &catalog, producer_options(&h264_caps(), Some("import"))); // The third frame reaches the sink 100ms later than its running time says it should. - let anchor = Instant::now(); + let anchor = moq_net::time::now(); for (pts, arrival) in [(0, 0), (33, 33), (66, 166)] { for pad in [&mut encoder, &mut import] { pad.observe_segment(time_segment()); @@ -2287,7 +2300,7 @@ mod tests { &catalog, producer_options(&h264_caps(), Some("encoder")).with_encoder(true), ); - let anchor = Instant::now(); + let anchor = moq_net::time::now(); for (pts, arrival) in [(0, 0), (33, 33), (66, 166)] { pad.observe_segment(time_segment()); assert_eq!( @@ -2346,7 +2359,7 @@ mod tests { pad.observe_caps(&broadcast, &catalog, producer_options(&h264_caps(), Some("video"))); pad.observe_segment(time_segment()); let delta = Bytes::from_static(&[0, 0, 0, 1, 0x61, 0xe0, 0x12, 0x34]); - let now = Instant::now(); + let now = moq_net::time::now(); let push = |pad: &mut Pad, data: Bytes, pts: u64| { pad.push_buffer(data, Some(gst::ClockTime::from_mseconds(pts)), None, None, now) .unwrap() @@ -2367,7 +2380,7 @@ mod tests { let mut pad = Pad::new(); pad.observe_caps(&broadcast, &catalog, producer_options(&h264_caps(), Some("video"))); pad.observe_segment(time_segment()); - let now = Instant::now(); + let now = moq_net::time::now(); let push = |pad: &mut Pad, data: Bytes, pts: u64| { pad.push_buffer(data, Some(gst::ClockTime::from_mseconds(pts)), None, None, now) .unwrap() @@ -2396,7 +2409,7 @@ mod tests { pad.observe_caps(&broadcast, &catalog, producer_options(&h264_caps(), Some("video"))); pad.observe_segment(time_segment()); let delta = Bytes::from_static(&[0, 0, 0, 1, 0x61, 0xe0, 0x12, 0x34]); - let now = Instant::now(); + let now = moq_net::time::now(); let push = |pad: &mut Pad, data: Bytes, pts: u64| { pad.push_buffer(data, Some(gst::ClockTime::from_mseconds(pts)), None, None, now) .unwrap() @@ -2423,7 +2436,7 @@ mod tests { let keyframe = h264_keyframe_au(); let headers = keyframe.slice(..keyframe.len() - 9); let delta = Bytes::from_static(&[0, 0, 0, 1, 0x61, 0xe0, 0x12, 0x34]); - let now = Instant::now(); + let now = moq_net::time::now(); let push = |pad: &mut Pad, data: Bytes, pts: u64| { pad.push_buffer(data, Some(gst::ClockTime::from_mseconds(pts)), None, None, now) .unwrap() diff --git a/rs/moq-hls/src/export/mod.rs b/rs/moq-hls/src/export/mod.rs index 83196a084d6..e57010aa544 100644 --- a/rs/moq-hls/src/export/mod.rs +++ b/rs/moq-hls/src/export/mod.rs @@ -2331,7 +2331,7 @@ mod tests { // PTS zero at exactly the moq epoch, so every timestamp maps to a fixed string. let wall = UNIX_EPOCH + Duration::from_millis(hang::catalog::MOQ_EPOCH_UNIX_MILLIS); let config = moq_mux::catalog::Config::default() - .with_clock(moq_mux::Clock::at(std::time::Instant::now(), wall).expect("a representable wall")); + .with_clock(moq_mux::Clock::at(moq_net::time::now(), wall).expect("a representable wall")); let catalog = moq_mux::catalog::Producer::new(&mut broadcast, config).unwrap(); let reserved = catalog.reserve(); @@ -4067,7 +4067,7 @@ mod tests { fanout.push(at, entry(at, at), &refs[0]); } // Aligned references share record numbers, so every handoff continues seamlessly. - let started = std::time::Instant::now(); + let started = moq_net::time::now(); for at in retained..retained + 1_024 { let reference = &refs[(at % 2) as usize]; fanout.select(Some(reference.clone())); diff --git a/rs/moq-mux/Cargo.toml b/rs/moq-mux/Cargo.toml index 4d5fe054524..784684b1a17 100644 --- a/rs/moq-mux/Cargo.toml +++ b/rs/moq-mux/Cargo.toml @@ -22,6 +22,7 @@ hang = { workspace = true } kio = { workspace = true } memchr = "2" moq-flate = { workspace = true } +moq-clock = { workspace = true } moq-json = { workspace = true } moq-loc = { workspace = true } moq-msf = { workspace = true } @@ -38,12 +39,12 @@ thiserror = { workspace = true } tokio = { workspace = true, features = ["macros"] } tracing = { workspace = true } url = { workspace = true } -web-async = { workspace = true } webm-iterable = "0.7" [dev-dependencies] criterion = { workspace = true } futures = { workspace = true } +moq-clock = { workspace = true, features = ["sim"] } tokio = { workspace = true, features = ["test-util"] } tracing-test = { workspace = true, features = ["no-env-filter"] } diff --git a/rs/moq-mux/benches/jitter.rs b/rs/moq-mux/benches/jitter.rs index 66ad1492baa..b45f7d9c19e 100644 --- a/rs/moq-mux/benches/jitter.rs +++ b/rs/moq-mux/benches/jitter.rs @@ -3,8 +3,8 @@ use std::time::Duration; use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main}; +use moq_clock::Instant; use moq_net::Timestamp; -use web_async::time::Instant; #[path = "../src/jitter.rs"] #[allow(dead_code)] @@ -13,13 +13,13 @@ mod jitter; /// Every track sends a frame a second as it decodes, for as many two-second steering steps as /// fill the rate window with `floors`, each step measuring every track heard. async fn steer(tracks: u16, floors: u64) -> u64 { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = jitter::Buffer::new(Duration::from_millis(500)); buffer.expect(1..=tracks); let waiter = kio::Waiter::noop(); for second in 0..floors * 2 { let at = start + Duration::from_secs(second); - tokio::time::advance(at.saturating_duration_since(Instant::now())).await; + tokio::time::advance(at.saturating_duration_since(moq_clock::now())).await; for track in 1..=tracks { let arrival = jitter::Arrival { arrived: at, diff --git a/rs/moq-mux/clippy.toml b/rs/moq-mux/clippy.toml new file mode 100644 index 00000000000..892532bf144 --- /dev/null +++ b/rs/moq-mux/clippy.toml @@ -0,0 +1,9 @@ +disallowed-methods = [ + { path = "std::time::Instant::now", reason = "read the ambient clock: moq_clock::now()" }, + { path = "std::time::SystemTime::now", reason = "read the ambient clock: moq_clock::wall()" }, + { path = "tokio::time::Instant::now", reason = "read the ambient clock: moq_clock::now()" }, + { path = "tokio::time::sleep", reason = "sleep on the ambient clock: moq_clock::sleep()" }, + { path = "tokio::time::sleep_until", reason = "sleep on the ambient clock: moq_clock::sleep_until()" }, + { path = "tokio::time::timeout", reason = "use the ambient clock: moq_clock::timeout()" }, + { path = "tokio::time::interval", reason = "use the ambient clock: moq_clock::interval()" }, +] diff --git a/rs/moq-mux/src/binary.rs b/rs/moq-mux/src/binary.rs index 33e95470dab..0de8e72a7d9 100644 --- a/rs/moq-mux/src/binary.rs +++ b/rs/moq-mux/src/binary.rs @@ -35,7 +35,7 @@ //! //! A payload timed on the broadcast [`Clock`](crate::Clock) is written at that timestamp as given, //! and the entry advertises how late payloads reach the transport as its `jitter` and `delay`, the -//! way a media rendition does. A capture [`Instant`](std::time::Instant) converts with +//! way a media rendition does. A capture [`Instant`](moq_clock::Instant) converts with //! [`Clock::capture`](crate::Clock::capture) on the catalog's clock: //! //! ```no_run @@ -43,7 +43,7 @@ //! # catalog: &moq_mux::catalog::Producer, //! # thumbnail: &mut moq_mux::binary::Snapshot, //! # jpeg: bytes::Bytes, -//! # captured: std::time::Instant, +//! # captured: moq_clock::Instant, //! # ) -> moq_mux::Result<()> { //! let at = catalog.clock().capture(captured)?; //! thumbnail.update(moq_net::Timed::from(jpeg).at(at))?; @@ -464,7 +464,7 @@ mod test { .binary_stream(timed_track(&mut broadcast, "bare"), Config::default()) .unwrap(); - let anchor = std::time::Instant::now(); + let anchor = moq_clock::Instant::now(); for i in 0..20u64 { let now = moq_net::Timestamp::from_micros(i * 100_000).unwrap(); let at = |late: u64| anchor + std::time::Duration::from_millis(i * 100 + late); @@ -491,14 +491,15 @@ mod test { /// A captured payload measures through the real clock: written right after capture, it /// advertises no delay beyond the scheduling noise of the test itself. - #[test] - fn a_capture_measures_through_the_clock() { + #[moq_clock::test] + async fn a_capture_measures_through_the_clock() { + moq_clock::sleep(std::time::Duration::from_secs(1)).await; let (mut broadcast, catalog) = catalog(); let mut telemetry = catalog .binary_stream(timed_track(&mut broadcast, "telemetry"), Config::default()) .unwrap(); - let captured = std::time::Instant::now(); + let captured = moq_clock::Instant::now(); let at = catalog.clock().capture(captured).unwrap(); telemetry.append(Timed::from(&b"now"[..]).at(at)).unwrap(); let late = catalog @@ -544,7 +545,7 @@ mod test { .binary_stream(timed_track(&mut broadcast, "ahead"), Config::default()) .unwrap(); - let anchor = std::time::Instant::now(); + let anchor = moq_clock::Instant::now(); for i in 0..20u64 { let now = moq_net::Timestamp::from_micros(i * 100_000).unwrap(); let at = |late: u64| anchor + std::time::Duration::from_millis(i * 100 + late); @@ -948,7 +949,7 @@ mod test { .unwrap(); let listing = telemetry.listing.as_mut().unwrap(); - let anchor = std::time::Instant::now(); + let anchor = moq_clock::Instant::now(); for i in 0..60u64 { let now = moq_net::Timestamp::from_micros(i * 40_000).unwrap(); // Every other payload reaches the transport 10ms later than the rest. diff --git a/rs/moq-mux/src/catalog/data.rs b/rs/moq-mux/src/catalog/data.rs index 4a2af87a1c8..5d7cf6cc9e9 100644 --- a/rs/moq-mux/src/catalog/data.rs +++ b/rs/moq-mux/src/catalog/data.rs @@ -91,7 +91,7 @@ impl Listing { } self.clock = clock; let now = clock.now(); - let flush = captured.map(|captured| (captured, std::time::Instant::now())); + let flush = captured.map(|captured| (captured, moq_clock::Instant::now())); self.record_at(now, bytes, flush) } @@ -101,7 +101,7 @@ impl Listing { &mut self, now: moq_net::Timestamp, bytes: usize, - flush: Option<(moq_net::Timestamp, std::time::Instant)>, + flush: Option<(moq_net::Timestamp, moq_clock::Instant)>, ) -> crate::Result<()> { // Each write is its own span, closed by the next one. self.estimator.cut(Some(now)); diff --git a/rs/moq-mux/src/catalog/estimate.rs b/rs/moq-mux/src/catalog/estimate.rs index 52830505b88..56e231a3555 100644 --- a/rs/moq-mux/src/catalog/estimate.rs +++ b/rs/moq-mux/src/catalog/estimate.rs @@ -1,6 +1,7 @@ +use moq_clock::Instant; use std::collections::VecDeque; use std::sync::{Arc, Mutex, PoisonError}; -use std::time::{Duration, Instant}; +use std::time::Duration; use moq_net::Timestamp; @@ -476,7 +477,7 @@ mod tests { #[test] fn batch_flush_at_its_end_reports_its_media_span() { let mut estimator = Estimator::new(); - let anchor = Instant::now(); + let anchor = moq_clock::now(); estimator.flush(micros(0), anchor); estimator.flush(micros(0), anchor + Duration::from_millis(120)); estimator.flush(micros(40_000), anchor + Duration::from_millis(120)); @@ -486,7 +487,7 @@ mod tests { #[test] fn constant_encoder_delay_is_not_jitter() { let mut estimator = Estimator::new(); - let anchor = Instant::now(); + let anchor = moq_clock::now(); estimator.flush(micros(0), anchor + Duration::from_millis(200)); estimator.flush(micros(240_000), anchor + Duration::from_millis(440)); assert_eq!(estimator.estimate().jitter, None); @@ -495,7 +496,7 @@ mod tests { #[test] fn discontinuity_resets_the_flush_baseline() { let mut estimator = Estimator::new(); - let anchor = Instant::now(); + let anchor = moq_clock::now(); estimator.flush(micros(0), anchor); estimator.flush(micros(40_000), anchor + Duration::from_millis(50)); assert_eq!(estimator.estimate().jitter, Some(Duration::from_millis(10))); @@ -510,7 +511,7 @@ mod tests { #[test] fn baseline_expires_drift() { let mut estimator = Estimator::new(); - let anchor = Instant::now(); + let anchor = moq_clock::now(); for second in 0..100u64 { estimator.flush( micros(second * 1_000_000), @@ -524,7 +525,7 @@ mod tests { #[test] fn early_flush_keeps_lowering_the_baseline() { let mut estimator = Estimator::new(); - let anchor = Instant::now(); + let anchor = moq_clock::now(); for second in 0..100u64 { estimator.flush(micros(second * 2_000_000), anchor + Duration::from_secs(second)); } @@ -539,7 +540,7 @@ mod tests { Estimator::with_broadcast(broadcast.clone()), Estimator::with_broadcast(broadcast), ]; - let anchor = Instant::now(); + let anchor = moq_clock::now(); for frame in 0..seconds * 10 { let pts = frame * 100; for (estimator, lateness) in estimators.iter_mut().zip([fast(pts), slow(pts)]) { @@ -584,7 +585,7 @@ mod tests { let broadcast = Baseline::default(); let mut audio = Estimator::with_broadcast(broadcast.clone()); let mut video = Estimator::with_broadcast(broadcast); - let anchor = Instant::now(); + let anchor = moq_clock::now(); audio.flush(micros(0), anchor); video.flush(micros(0), anchor + Duration::from_millis(50)); assert_eq!(video.estimate().delay, Some(Duration::from_millis(50))); diff --git a/rs/moq-mux/src/catalog/producer.rs b/rs/moq-mux/src/catalog/producer.rs index 0b80c54512e..bfc49e391bf 100644 --- a/rs/moq-mux/src/catalog/producer.rs +++ b/rs/moq-mux/src/catalog/producer.rs @@ -56,7 +56,7 @@ const ESTIMATE_WINDOW: std::time::Duration = std::time::Duration::from_secs(1); #[derive(Default)] struct Throttle { /// When the last publish carrying an estimate went out. - last: Option, + last: Option, /// An estimate is in the catalog but not yet on the wire. pending: bool, } @@ -65,7 +65,7 @@ impl Throttle { /// A publish is going out: any held estimate rides it and opens a new window. fn fold(&mut self) { if std::mem::take(&mut self.pending) { - self.last = Some(web_async::time::Instant::now()); + self.last = Some(moq_clock::now()); } } } @@ -1004,7 +1004,7 @@ impl Guard<'_, E> { if !estimates.pending { return Ok(()); } - let now = web_async::time::Instant::now(); + let now = moq_clock::now(); if throttle && estimates.last.is_some_and(|last| now < last + ESTIMATE_WINDOW) { return Ok(()); } @@ -1731,9 +1731,9 @@ mod test { #[test] fn sibling_broadcasts_share_a_clock_and_capture_time() { - use std::time::{Duration, Instant, SystemTime}; + use std::time::{Duration, SystemTime}; - let captured = Instant::now(); + let captured = moq_clock::now(); let wall = SystemTime::UNIX_EPOCH + Duration::from_millis(hang::catalog::MOQ_EPOCH_UNIX_MILLIS + 60_000); let clock = crate::Clock::at(captured, wall).unwrap(); let (origin, _driver) = moq_net::origin::Producer::new(Default::default()); @@ -1787,13 +1787,14 @@ mod test { /// A `with_clock` catalog is fixed from the start: even the first importer shifts onto it, /// landing at the clock's reading on arrival, and the advertised mapping never moves. - #[test] - fn with_clock_shifts_the_first_importer() { + #[moq_clock::test] + async fn with_clock_shifts_the_first_importer() { use std::time::{Duration, SystemTime}; + moq_clock::sleep(Duration::from_secs(5)).await; let mut broadcast = moq_net::broadcast::Info::new().produce(); let wall = SystemTime::UNIX_EPOCH + Duration::from_millis(hang::catalog::MOQ_EPOCH_UNIX_MILLIS + 60_000); - let start = std::time::Instant::now() - Duration::from_secs(5); + let start = moq_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(); @@ -1979,7 +1980,7 @@ mod test { /// never replaced by a fresh clock; one that disagrees with `with_clock` is refused. #[test] fn a_seeded_root_clock_is_adopted() { - use std::time::{Duration, Instant, SystemTime}; + use std::time::{Duration, SystemTime}; let input = an_hour_ago(moq_net::Timescale::MICRO); let mut seed = Catalog::<()>::default(); @@ -1994,7 +1995,7 @@ mod test { let zero = SystemTime::UNIX_EPOCH + Duration::from_millis(hang::catalog::MOQ_EPOCH_UNIX_MILLIS) + Duration::from_micros(input.wall.as_micros() as u64); - let fixed = crate::Clock::at(Instant::now(), zero).unwrap(); + let fixed = crate::Clock::at(moq_clock::now(), zero).unwrap(); seed.clock = Some(an_hour_ago(moq_net::Timescale::MILLI)); crate::clock::fake_host_wall(SystemTime::UNIX_EPOCH + Duration::from_secs(86_400)); let config = Config::default().with_catalog(seed.clone()).with_clock(fixed); @@ -2005,7 +2006,7 @@ mod test { assert_eq!(catalog.snapshot().clock, Some(input)); // A different one is refused before any catalog track exists. - let other = crate::Clock::at(Instant::now(), zero + Duration::from_secs(1)).unwrap(); + let other = crate::Clock::at(moq_clock::now(), zero + Duration::from_secs(1)).unwrap(); let config = Config::default().with_catalog(seed).with_clock(other); let mut broadcast = moq_net::broadcast::Info::new().produce(); assert!(matches!( diff --git a/rs/moq-mux/src/clock.rs b/rs/moq-mux/src/clock.rs index dd54a9265c0..3c2dcee0853 100644 --- a/rs/moq-mux/src/clock.rs +++ b/rs/moq-mux/src/clock.rs @@ -17,21 +17,10 @@ //! anything fixes the clock places the mapping so that frame is live on arrival; any other shifts //! onto the clock (see [`catalog::Timebase`](crate::catalog::Timebase)). -use std::time::{Duration, Instant, SystemTime}; +use std::time::{Duration, SystemTime}; use hang::catalog::{MAX_SAFE_INTEGER, MOQ_EPOCH_UNIX_MILLIS}; - -/// Native capture inputs use the same epoch as the async clock. -fn monotonic(at: Instant) -> crate::Result { - #[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] - return Ok(web_async::time::Instant::from_std(at)); - - #[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] - { - let _ = at; - Err(anyhow::anyhow!("std::time::Instant inputs are unsupported in the browser").into()) - } -} +use moq_clock::Instant; /// Microseconds from the moq epoch (2020) to `wall`, negative before it. fn moq_micros(wall: SystemTime) -> i128 { @@ -47,7 +36,7 @@ fn host_wall() -> Option { #[cfg(test)] let now = HOST_WALL.get().unwrap_or_else(SystemTime::now); #[cfg(not(test))] - let now = SystemTime::now(); + let now = SystemTime::from(moq_clock::wall()); (moq_micros(now) >= 0).then_some(now) } @@ -86,7 +75,7 @@ fn wall_clock(wall: SystemTime) -> crate::Result { #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Clock { /// A monotonic instant, and what the clock read then in micros. - instant: web_async::time::Instant, + instant: Instant, reading: u64, /// `None` when the host's wall clock was unset at construction. wall: Option, @@ -112,7 +101,7 @@ impl Clock { /// name: media needs only the monotonic timeline. pub fn new() -> Self { Self { - instant: web_async::time::Instant::now(), + instant: moq_clock::now(), reading: Self::LEAD.as_micros() as u64, wall: host_wall().and_then(|now| wall_clock(now.checked_sub(Self::LEAD)?).ok()), } @@ -124,7 +113,7 @@ impl Clock { /// sampling. Refuses an unrepresentable wall or a browser target without native instants. pub fn at(epoch: Instant, wall: SystemTime) -> crate::Result { Ok(Self { - instant: monotonic(epoch)?, + instant: epoch, reading: 0, wall: Some(wall_clock(wall)?), }) @@ -135,7 +124,7 @@ impl Clock { /// Refuses a `since` so large that PTS zero lands before the moq epoch (2020), which the wall /// mapping cannot name. On a host whose wall clock is unset, the clock has no wall mapping. pub(crate) fn arrival(since: Duration) -> crate::Result { - let instant = web_async::time::Instant::now(); + let instant = moq_clock::now(); let unmappable = || crate::Error::UnmappableTimestamp(format!("{since:?} puts PTS zero before 2020")); let wall = match host_wall() { Some(now) => { @@ -197,7 +186,7 @@ impl Clock { /// /// No `Instant` arithmetic, so a PTS zero before this host booted still maps. fn mapped(zero: hang::catalog::Clock, unmappable: impl Fn(&str) -> crate::Error) -> crate::Result { - let instant = web_async::time::Instant::now(); + let instant = moq_clock::now(); let now = host_wall().ok_or_else(|| unmappable("needs the host wall clock, which is unset"))?; // Saturating would stamp everything early by the skew without saying so. let reading = u64::try_from(moq_micros(now) - zero.wall.as_micros() as i128) @@ -233,11 +222,9 @@ impl Clock { /// Map the instant a payload was captured (a datagram's arrival, a sensor read) onto this clock. /// /// Refuses an instant ahead of now, which would claim the payload reached the transport before - /// it existed, and one before PTS zero, which no timestamp can name. Native capture instants - /// are unsupported in the browser. + /// it existed, and one before PTS zero, which no timestamp can name. pub fn capture(&self, at: Instant) -> crate::Result { - let at = monotonic(at)?; - if at > web_async::time::Instant::now() { + if at > moq_clock::now() { return Err(crate::Error::InvalidCapture); } let micros = match at.checked_duration_since(self.instant) { @@ -281,7 +268,7 @@ mod tests { use super::*; fn epoch() -> Instant { - Instant::now() + moq_clock::now() } fn moq_epoch() -> SystemTime { @@ -303,14 +290,15 @@ mod tests { /// A capture maps onto the clock's own timeline, and one ahead of now or before the epoch is /// refused rather than clamped. - #[test] - fn a_capture_maps_onto_the_clock() { - let now = Instant::now(); + #[moq_clock::test] + async fn a_capture_maps_onto_the_clock() { + moq_clock::sleep(Duration::from_secs(6)).await; + let now = moq_clock::now(); let clock = Clock::at(now - Duration::from_secs(5), moq_epoch()).unwrap(); assert_eq!(clock.capture(now - Duration::from_secs(2)).unwrap(), us(3_000_000)); assert!(matches!( - clock.capture(Instant::now() + Duration::from_secs(1)), + clock.capture(moq_clock::now() + Duration::from_secs(1)), Err(crate::Error::InvalidCapture) )); assert!(matches!( @@ -319,15 +307,16 @@ mod tests { )); } - #[tokio::test(start_paused = true)] - async fn paused_clock_preserves_native_boundaries() { - let epoch = tokio::time::Instant::now(); - let clock = Clock::at(epoch.into_std(), moq_epoch()).unwrap(); + #[moq_clock::test] + async fn simulated_clock_preserves_logical_boundaries() { + moq_clock::sleep(Duration::from_secs(10)).await; + let epoch = moq_clock::now(); + let clock = Clock::at(epoch, moq_epoch()).unwrap(); let fresh = Clock::new(); let before = fresh.now(); let wall = clock.wall(); - tokio::time::advance(Duration::from_secs(3)).await; + moq_clock::sleep(Duration::from_secs(3)).await; assert_eq!(clock.now(), us(3_000_000)); assert_eq!(fresh.now().as_micros() - before.as_micros(), 3_000_000); assert_eq!(clock.wall(), wall); @@ -336,14 +325,23 @@ mod tests { moq_epoch() + Duration::from_secs(3) ); - let captured = (epoch + Duration::from_secs(2)).into_std(); + moq_clock::sim::step_wall(moq_clock::Manual::WALL_START + Duration::from_secs(3600)); + assert_eq!(clock.wall(), wall, "a wall correction does not retime archived media"); + let corrected = Clock::new(); + assert_ne!( + corrected.wall(), + fresh.wall(), + "a new mapping samples the corrected wall" + ); + + let captured = epoch + Duration::from_secs(2); assert_eq!(clock.capture(captured).unwrap(), us(2_000_000)); assert!(matches!( - clock.capture((epoch + Duration::from_secs(4)).into_std()), + clock.capture(epoch + Duration::from_secs(4)), Err(crate::Error::InvalidCapture) )); assert!(matches!( - clock.capture((epoch - Duration::from_secs(1)).into_std()), + clock.capture(epoch - Duration::from_secs(1)), Err(crate::Error::InvalidCapture) )); } @@ -387,8 +385,9 @@ mod tests { } /// A source whose first timestamp is ten hours in reads that timestamp now, mapped to now. - #[test] - fn arrival_maps_the_first_timestamp_to_now() { + #[moq_clock::test] + async fn arrival_maps_the_first_timestamp_to_now() { + moq_clock::sleep(Duration::from_secs(1)).await; let since = Duration::from_secs(10 * 3600); let clock = Clock::arrival(since).unwrap(); let now = clock.now(); @@ -401,7 +400,7 @@ mod tests { assert!(drift < Duration::from_secs(1), "the first timestamp is live on arrival"); // A capture from before the arrival still maps, down to PTS zero. - let earlier = clock.capture(Instant::now() - Duration::from_secs(1)).unwrap(); + let earlier = clock.capture(moq_clock::now() - Duration::from_secs(1)).unwrap(); assert!(earlier.as_micros() < now.as_micros()); // PTS zero before 2020 cannot be named on the wire. diff --git a/rs/moq-mux/src/codec/aac/import.rs b/rs/moq-mux/src/codec/aac/import.rs index bc5ce7f2dd3..78cfd5cffb6 100644 --- a/rs/moq-mux/src/codec/aac/import.rs +++ b/rs/moq-mux/src/codec/aac/import.rs @@ -50,7 +50,7 @@ impl Import { } /// Record a locally encoded frame's transport handoff for catalog jitter measurement. - pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: std::time::Instant) -> crate::Result<()> { + pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: moq_clock::Instant) -> crate::Result<()> { self.track.flush(timestamp, now) } diff --git a/rs/moq-mux/src/codec/av1/import.rs b/rs/moq-mux/src/codec/av1/import.rs index 017d89ff7e0..46ae5a70afa 100644 --- a/rs/moq-mux/src/codec/av1/import.rs +++ b/rs/moq-mux/src/codec/av1/import.rs @@ -200,7 +200,7 @@ impl Import { } /// Record a locally encoded frame's transport handoff for catalog jitter measurement. - pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: std::time::Instant) -> crate::Result<()> { + pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: moq_clock::Instant) -> crate::Result<()> { self.track.flush(timestamp, now) } diff --git a/rs/moq-mux/src/codec/av1/split.rs b/rs/moq-mux/src/codec/av1/split.rs index 7f1bf647c45..b41aaa51fad 100644 --- a/rs/moq-mux/src/codec/av1/split.rs +++ b/rs/moq-mux/src/codec/av1/split.rs @@ -20,7 +20,7 @@ use crate::Result; /// stdin); call [`flush`](Self::flush) to emit the final in-flight temporal unit. pub struct Split { current: Au, - zero: Option, + zero: Option, pending: Vec, // Bytes carried across calls: a partial OBU at the tail of one `decode` waits // here for the rest to arrive on the next call. @@ -169,7 +169,7 @@ impl Split { if let Some(pts) = hint { return Ok(pts); } - let zero = self.zero.get_or_insert_with(web_async::time::Instant::now); + let zero = self.zero.get_or_insert_with(moq_clock::Instant::now); Ok(moq_net::Timestamp::from_micros(zero.elapsed().as_micros() as u64)?) } } diff --git a/rs/moq-mux/src/codec/flac/import.rs b/rs/moq-mux/src/codec/flac/import.rs index a2564adc444..19fa646ebfb 100644 --- a/rs/moq-mux/src/codec/flac/import.rs +++ b/rs/moq-mux/src/codec/flac/import.rs @@ -46,7 +46,7 @@ impl Import { } /// Record a locally encoded frame's transport handoff for catalog jitter measurement. - pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: std::time::Instant) -> crate::Result<()> { + pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: moq_clock::Instant) -> crate::Result<()> { self.track.flush(timestamp, now) } diff --git a/rs/moq-mux/src/codec/h264/import.rs b/rs/moq-mux/src/codec/h264/import.rs index b2fe8a31649..f6fc67340b6 100644 --- a/rs/moq-mux/src/codec/h264/import.rs +++ b/rs/moq-mux/src/codec/h264/import.rs @@ -152,7 +152,7 @@ impl Import { } /// Record a locally encoded frame's transport handoff for catalog jitter measurement. - pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: std::time::Instant) -> crate::Result<()> { + pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: moq_clock::Instant) -> crate::Result<()> { self.track.flush(timestamp, now) } diff --git a/rs/moq-mux/src/codec/h264/split.rs b/rs/moq-mux/src/codec/h264/split.rs index b71634e3e46..79768a07cf2 100644 --- a/rs/moq-mux/src/codec/h264/split.rs +++ b/rs/moq-mux/src/codec/h264/split.rs @@ -38,7 +38,7 @@ pub struct Split { /// Retained PPS NALs. A keyframe may carry several (slices reference them by /// id); all are kept and re-injected, but a new GOP's set supersedes them. pps: Vec, - zero: Option, + zero: Option, pending: Vec, } @@ -261,7 +261,7 @@ impl Split { if let Some(pts) = hint { return Ok(pts); } - let zero = self.zero.get_or_insert_with(web_async::time::Instant::now); + let zero = self.zero.get_or_insert_with(moq_clock::Instant::now); Ok(moq_net::Timestamp::from_micros(zero.elapsed().as_micros() as u64)?) } } diff --git a/rs/moq-mux/src/codec/h265/import.rs b/rs/moq-mux/src/codec/h265/import.rs index 63aac840cc4..78e612f22a3 100644 --- a/rs/moq-mux/src/codec/h265/import.rs +++ b/rs/moq-mux/src/codec/h265/import.rs @@ -157,7 +157,7 @@ impl Import { } /// Record a locally encoded frame's transport handoff for catalog jitter measurement. - pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: std::time::Instant) -> crate::Result<()> { + pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: moq_clock::Instant) -> crate::Result<()> { self.track.flush(timestamp, now) } diff --git a/rs/moq-mux/src/codec/h265/split.rs b/rs/moq-mux/src/codec/h265/split.rs index b092e7f2556..dad657f8d2e 100644 --- a/rs/moq-mux/src/codec/h265/split.rs +++ b/rs/moq-mux/src/codec/h265/split.rs @@ -36,7 +36,7 @@ pub struct Split { /// Retained PPS NALs. A keyframe may carry several (slices reference them by /// id); all are kept and re-injected, but a new GOP's set supersedes them. pps: Vec, - zero: Option, + zero: Option, pending: Vec, } @@ -268,7 +268,7 @@ impl Split { if let Some(pts) = hint { return Ok(pts); } - let zero = self.zero.get_or_insert_with(web_async::time::Instant::now); + let zero = self.zero.get_or_insert_with(moq_clock::Instant::now); Ok(moq_net::Timestamp::from_micros(zero.elapsed().as_micros() as u64)?) } } diff --git a/rs/moq-mux/src/codec/mp3.rs b/rs/moq-mux/src/codec/mp3.rs index f1cd26c703c..660e47dcf71 100644 --- a/rs/moq-mux/src/codec/mp3.rs +++ b/rs/moq-mux/src/codec/mp3.rs @@ -134,7 +134,7 @@ impl Import { } /// Record a locally encoded frame's transport handoff for catalog jitter measurement. - pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: std::time::Instant) -> crate::Result<()> { + pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: moq_clock::Instant) -> crate::Result<()> { self.track.flush(timestamp, now) } diff --git a/rs/moq-mux/src/codec/opus/import.rs b/rs/moq-mux/src/codec/opus/import.rs index 1dbd98f120a..b00c4749a57 100644 --- a/rs/moq-mux/src/codec/opus/import.rs +++ b/rs/moq-mux/src/codec/opus/import.rs @@ -48,7 +48,7 @@ impl Import { } /// Record a locally encoded frame's transport handoff for catalog jitter measurement. - pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: std::time::Instant) -> crate::Result<()> { + pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: moq_clock::Instant) -> crate::Result<()> { self.track.flush(timestamp, now) } diff --git a/rs/moq-mux/src/codec/vp8/import.rs b/rs/moq-mux/src/codec/vp8/import.rs index 0d6321b68a4..c3ff8fbc911 100644 --- a/rs/moq-mux/src/codec/vp8/import.rs +++ b/rs/moq-mux/src/codec/vp8/import.rs @@ -98,7 +98,7 @@ impl Import { } /// Record a locally encoded frame's transport handoff for catalog jitter measurement. - pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: std::time::Instant) -> crate::Result<()> { + pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: moq_clock::Instant) -> crate::Result<()> { self.track.flush(timestamp, now) } diff --git a/rs/moq-mux/src/codec/vp9/import.rs b/rs/moq-mux/src/codec/vp9/import.rs index c8732659436..a9961b3f3ba 100644 --- a/rs/moq-mux/src/codec/vp9/import.rs +++ b/rs/moq-mux/src/codec/vp9/import.rs @@ -98,7 +98,7 @@ impl Import { } /// Record a locally encoded frame's transport handoff for catalog jitter measurement. - pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: std::time::Instant) -> crate::Result<()> { + pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: moq_clock::Instant) -> crate::Result<()> { self.track.flush(timestamp, now) } diff --git a/rs/moq-mux/src/container/flv/export.rs b/rs/moq-mux/src/container/flv/export.rs index 04542ec6249..4e75a753606 100644 --- a/rs/moq-mux/src/container/flv/export.rs +++ b/rs/moq-mux/src/container/flv/export.rs @@ -147,7 +147,7 @@ struct FlvTrack { source: ExportSource, pending: Option>, /// When the source was last empty, bounding the next frame's arrival. - empty: web_async::time::Instant, + empty: moq_clock::Instant, finished: bool, /// The FLV payload shape (legacy CodecID vs enhanced FourCC) to mux this /// track as, fixed from its catalog codec when it's bound. @@ -303,7 +303,7 @@ impl Export { } #[cfg(test)] - pub(super) fn next_deadline(&self) -> Option { + pub(super) fn next_deadline(&self) -> Option { self.jitter.next_deadline() } @@ -349,7 +349,7 @@ impl Export { break; } Poll::Pending => { - track.empty = web_async::time::Instant::now(); + track.empty = moq_clock::now(); break; } }; @@ -358,7 +358,7 @@ impl Export { } crate::jitter::Arrival { arrived: track.empty, - read: web_async::time::Instant::now(), + read: moq_clock::now(), decode: frame.timestamp, restart: track.source.markers(), skip: track.source.skips(), @@ -476,7 +476,7 @@ impl Export { track_id, source, pending: None, - empty: web_async::time::Instant::now(), + empty: moq_clock::now(), finished: false, flavor, fallback_description, @@ -522,7 +522,7 @@ impl Export { track_id, source, pending: None, - empty: web_async::time::Instant::now(), + empty: moq_clock::now(), finished: false, flavor, fallback_description: None, diff --git a/rs/moq-mux/src/container/flv/export_test.rs b/rs/moq-mux/src/container/flv/export_test.rs index ae5cfa9a4d8..8e45f1c50ff 100644 --- a/rs/moq-mux/src/container/flv/export_test.rs +++ b/rs/moq-mux/src/container/flv/export_test.rs @@ -137,7 +137,7 @@ async fn drain_export(mut exporter: Export, import Ok(Err(e)) => panic!("exporter error: {e}"), Err(_) => { if let Some(deadline) = exporter.next_deadline() { - tokio::time::advance(deadline.saturating_duration_since(web_async::time::Instant::now())).await; + tokio::time::advance(deadline.saturating_duration_since(moq_clock::now())).await; } else { importer = None; // No scheduled frames remain; close the broadcast. } @@ -1354,7 +1354,7 @@ async fn drain_until_idle( Ok(Err(e)) => return Some(Err(e)), Err(_) => { let deadline = exporter.next_deadline()?; - tokio::time::advance(deadline.saturating_duration_since(web_async::time::Instant::now())).await; + tokio::time::advance(deadline.saturating_duration_since(moq_clock::now())).await; } } } @@ -1483,7 +1483,7 @@ async fn delay_drops_late_frames_and_drains_the_final_frame() { let delay = std::time::Duration::from_millis(100); let mut export = super::Export::new(live.source().await, live.catalog_stream().await).with_delay(delay); export.next().await.unwrap().unwrap(); - let start = web_async::time::Instant::now(); + let start = moq_clock::now(); live.track.write(raw_frame(0, b"first", true)).unwrap(); let first = export.next().await.unwrap().unwrap(); assert!(first.windows(5).any(|part| part == b"first")); diff --git a/rs/moq-mux/src/container/mkv/export.rs b/rs/moq-mux/src/container/mkv/export.rs index 36176b1e8d0..a3c450cb60b 100644 --- a/rs/moq-mux/src/container/mkv/export.rs +++ b/rs/moq-mux/src/container/mkv/export.rs @@ -72,7 +72,7 @@ struct MkvTrack { reorder: Option<(u64, u64)>, source: ExportSource, pending: Option>, - empty: web_async::time::Instant, + empty: moq_clock::Instant, finished: bool, track_number: u64, kind: TrackKind, @@ -323,7 +323,7 @@ impl Export { } #[cfg(test)] - pub(super) fn next_deadline(&self) -> Option { + pub(super) fn next_deadline(&self) -> Option { self.jitter.next_deadline() } @@ -362,7 +362,7 @@ impl Export { break; } Poll::Pending => { - track.empty = web_async::time::Instant::now(); + track.empty = moq_clock::now(); break; } }; @@ -377,7 +377,7 @@ impl Export { } crate::jitter::Arrival { arrived: track.empty, - read: web_async::time::Instant::now(), + read: moq_clock::now(), decode: frame.timestamp, restart: track.source.markers(), skip: track.source.skips(), @@ -467,7 +467,7 @@ impl Export { reorder: None, source, pending: None, - empty: web_async::time::Instant::now(), + empty: moq_clock::now(), finished: false, track_number: next_track_number, kind: TrackKind::Video, @@ -493,7 +493,7 @@ impl Export { reorder: None, source, pending: None, - empty: web_async::time::Instant::now(), + empty: moq_clock::now(), finished: false, track_number: next_track_number, kind: TrackKind::Audio, diff --git a/rs/moq-mux/src/container/mkv/export_test.rs b/rs/moq-mux/src/container/mkv/export_test.rs index 01ccd31b0ea..e1d418735ac 100644 --- a/rs/moq-mux/src/container/mkv/export_test.rs +++ b/rs/moq-mux/src/container/mkv/export_test.rs @@ -438,7 +438,7 @@ async fn export_emits_blocks_for_each_frame() { Ok(Err(e)) => panic!("exporter error: {e}"), Err(_) => { if let Some(deadline) = exporter.next_deadline() { - tokio::time::advance(deadline.saturating_duration_since(web_async::time::Instant::now())).await; + tokio::time::advance(deadline.saturating_duration_since(moq_clock::now())).await; } else { importer = None; } @@ -681,7 +681,7 @@ async fn export_fragment_duration_batches_blocks() { Ok(Err(e)) => panic!("exporter error: {e}"), Err(_) => { if let Some(deadline) = exporter.next_deadline() { - tokio::time::advance(deadline.saturating_duration_since(web_async::time::Instant::now())).await; + tokio::time::advance(deadline.saturating_duration_since(moq_clock::now())).await; } else { importer = None; } @@ -925,7 +925,7 @@ async fn delay_drops_late_frames_and_drains_the_final_frame() { .with_delay(delay) .with_fragment_duration(std::time::Duration::ZERO); export.next().await.unwrap().unwrap(); - let start = web_async::time::Instant::now(); + let start = moq_clock::now(); live.track.write(raw_frame(0, b"first", true)).unwrap(); let first = export.next().await.unwrap().unwrap(); assert!(first.windows(5).any(|part| part == b"first")); diff --git a/rs/moq-mux/src/container/producer.rs b/rs/moq-mux/src/container/producer.rs index f2afb8b9991..087099101fe 100644 --- a/rs/moq-mux/src/container/producer.rs +++ b/rs/moq-mux/src/container/producer.rs @@ -239,7 +239,7 @@ where /// Record when a locally encoded frame reached the transport. Imported media must leave /// this clock observation out and rely on its container batch and reorder measurements. - pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: std::time::Instant) -> crate::Result<()> { + pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: moq_clock::Instant) -> crate::Result<()> { self.estimator.flush(timestamp, now); self.publish_estimate() } @@ -847,7 +847,7 @@ mod tests { tracks.push(track); } - let anchor = std::time::Instant::now(); + let anchor = moq_clock::Instant::now(); let ms = std::time::Duration::from_millis; let pts = |millis: u64| Timestamp::from_micros(millis * 1_000).unwrap(); tracks[0].flush(pts(0), anchor).unwrap(); diff --git a/rs/moq-mux/src/container/ts/damage.rs b/rs/moq-mux/src/container/ts/damage.rs index 30c92db4f89..c04f4c85210 100644 --- a/rs/moq-mux/src/container/ts/damage.rs +++ b/rs/moq-mux/src/container/ts/damage.rs @@ -2,7 +2,7 @@ use std::collections::BTreeMap; -use web_async::time::Instant; +use moq_clock::Instant; use super::stats; @@ -28,7 +28,7 @@ impl Burst { impl Log { pub fn record(&mut self, pid: u16, error: &anyhow::Error) { - let now = Instant::now(); + let now = moq_clock::now(); self.flush(now); let first = match self.bursts.entry(pid) { std::collections::btree_map::Entry::Occupied(mut entry) => { @@ -117,7 +117,7 @@ mod tests { tokio::time::advance(stats::Log::INTERVAL / 2).await; log.record(97, &error); tokio::time::advance(stats::Log::INTERVAL / 2).await; - log.flush(Instant::now()); + log.flush(moq_clock::now()); // This PID is quiet for a full interval before the next global summary tick. tokio::time::advance(stats::Log::INTERVAL / 2).await; log.record(97, &error); diff --git a/rs/moq-mux/src/container/ts/export.rs b/rs/moq-mux/src/container/ts/export.rs index bbd4398298f..02993676f59 100644 --- a/rs/moq-mux/src/container/ts/export.rs +++ b/rs/moq-mux/src/container/ts/export.rs @@ -166,7 +166,7 @@ where /// edge: a test exporting a broadcast it wrote whole. replay: bool, /// Wakes the export when the next grid slot is due on the jitter buffer's clock. - slot_timer: Option>>, + slot_timer: Option>>, /// Output frames ready to hand out, one per grid slot, each with what it tells /// [`Self::stats`] once returned. queue: VecDeque<(Frame, Tally)>, @@ -209,9 +209,9 @@ struct Pending { /// How many times its playhead had jumped, markers included ([`jitter::Arrival::skip`]). skip: u64, /// The earliest the frame could have arrived: when its source was last found empty. - arrived: web_async::time::Instant, + arrived: moq_clock::Instant, /// When it was read: the latest it could have arrived. - read: web_async::time::Instant, + read: moq_clock::Instant, } /// A frame waiting in the jitter buffer, with what it needs from the moment it was read. @@ -240,7 +240,7 @@ struct Track { /// When the source was last found empty. A frame read since arrived no earlier, and /// no later than it is read; the jitter buffer judges it on the earlier bound, so a caller /// that polls late (a sink sleeping to pace its writes) does not make it late. - empty: web_async::time::Instant, + empty: moq_clock::Instant, /// The marker count the decode clock runs under. restart: u64, /// The marker and skip counts of the sources the track read before this one, so the @@ -280,7 +280,7 @@ impl Track { Self { source, pending: None, - empty: web_async::time::Instant::now(), + empty: moq_clock::now(), restart: 0, base: (0, 0), finished: false, @@ -342,7 +342,7 @@ impl Track { restart: self.base.0 + self.source.markers(), skip: self.base.1 + self.source.skips(), arrived: self.empty, - read: web_async::time::Instant::now(), + read: moq_clock::now(), }; // A new timeline must reach the reset before tune-in alignment can drop it. if let Some(start) = input.video_start @@ -362,7 +362,7 @@ impl Track { break; } Poll::Pending => { - self.empty = web_async::time::Instant::now(); + self.empty = moq_clock::now(); break; } } @@ -605,6 +605,10 @@ impl Track { true => (Duration::from(decode) + jitter.delay()).saturating_sub(Duration::from(key)), false => Duration::ZERO, }; + // Logical clocks can begin at zero. Shift both readings by the same representable amount so + // their ordering survives even when the full replay lead would precede the clock's origin. + let ahead = ahead.min(arrived.duration_since(moq_clock::Instant::ZERO)); + let ahead = ahead.min(read.duration_since(moq_clock::Instant::ZERO)); let arrival = Arrival { arrived: arrived - ahead, read: read - ahead, @@ -1205,7 +1209,7 @@ where } // Hand the held frames to the jitter buffer in arrival order, so the // earliest anchors its clock, then read on. - let mut held: Vec<(web_async::time::Instant, String)> = self + let mut held: Vec<(moq_clock::Instant, String)> = self .tracks .iter() .filter_map(|(name, t)| t.pending.as_ref().map(|p| (p.arrived, name.clone()))) @@ -1645,7 +1649,7 @@ where track.base.1 += track.source.skips() + 1; track.source = source; track.finished = false; - track.empty = web_async::time::Instant::now(); + track.empty = moq_clock::now(); } Ok(()) } @@ -1696,7 +1700,7 @@ where /// When the next queued frame is due, or the next grid slot while media is queued or /// every source has finished and the tail runs out. #[cfg(test)] - pub(super) fn next_due(&self) -> Option { + pub(super) fn next_due(&self) -> Option { let finished = !self.tracks.is_empty() && self.tracks.values().all(|track| track.finished); let slot = self .slot_due() @@ -2336,7 +2340,7 @@ where /// never on when they arrived, and the output keeps the clock's pace while media is queued /// (in the schedule or still in the jitter buffer) or still to decode. `None` without a /// delay, where frames settle the slots instead. - fn slot_due(&self) -> Option<(u128, web_async::time::Instant)> { + fn slot_due(&self) -> Option<(u128, moq_clock::Instant)> { let (known, settled) = self.slot_settled()?; let settled = Timestamp::from_micros(u64::try_from(settled / 1_000).ok()?).ok()?; Some((known, self.jitter.at(settled)?)) @@ -2384,10 +2388,10 @@ where /// Lay out every grid slot whose time has come, and wake when the next one is due. fn lay_due(&mut self, waiter: &kio::Waiter) -> anyhow::Result<()> { while let Some((known, at)) = self.slot_due() { - if web_async::time::Instant::now() < at { + if moq_clock::now() < at { let timer = self .slot_timer - .get_or_insert_with(|| Box::pin(web_async::time::sleep_until(at))); + .get_or_insert_with(|| Box::pin(moq_clock::sleep_until(at))); if timer.deadline() != at { timer.as_mut().reset(at); } @@ -3379,7 +3383,7 @@ mod tests { let stream = bytes::Bytes::from(frame.repeat(3)); // In microseconds, as every hang frame is. let us = |ms: u64| Timestamp::from_micros(ms * 1_000).unwrap(); - let now = web_async::time::Instant::now(); + let now = moq_clock::now(); for (pts, payload) in [(us(0), stream.slice(..200)), (us(64), stream.slice(200..))] { let frame = super::Frame { timestamp: pts, diff --git a/rs/moq-mux/src/container/ts/export_test.rs b/rs/moq-mux/src/container/ts/export_test.rs index e77cd97475e..9850100b379 100644 --- a/rs/moq-mux/src/container/ts/export_test.rs +++ b/rs/moq-mux/src/container/ts/export_test.rs @@ -5016,7 +5016,7 @@ where // Run the paused clock only as far as the next frame due, so a test that writes // more afterwards is still on time for it. if let Some(deadline) = export.next_due() { - tokio::time::sleep_until(deadline).await; + moq_clock::sleep_until(deadline).await; continue; } match tokio::time::timeout(Duration::from_millis(10), export.next()).await { @@ -7123,6 +7123,8 @@ fn assert_decodes_after_the_clock(frames: &[Frame]) { /// the same bytes as one that sees each tick whole. #[tokio::test(start_paused = true)] async fn declared_reorder_sizes_the_decode_clock_from_the_first_frame() { + // Leave room for replay send-ahead before the logical clock's zero origin. + tokio::time::advance(Duration::from_secs(1)).await; let mut rig = Reordered::new( crate::codec::h264::fixtures::SPS_IPB, crate::codec::h264::fixtures::PPS, diff --git a/rs/moq-mux/src/container/ts/export_timing_test.rs b/rs/moq-mux/src/container/ts/export_timing_test.rs index 44b043eb723..01282108118 100644 --- a/rs/moq-mux/src/container/ts/export_timing_test.rs +++ b/rs/moq-mux/src/container/ts/export_timing_test.rs @@ -12,9 +12,9 @@ use std::time::Duration; use bytes::{Bytes, BytesMut}; use hang::catalog::{AAC, AudioConfig, Container, H264, VideoConfig}; +use moq_clock::Instant; use moq_net::Timestamp; use mpeg2ts::ts::{ReadTsPacket, TsPacketReader, TsPayload}; -use tokio::time::Instant; use crate::catalog::hang::Container as HangContainer; use crate::container::ts::Export; @@ -139,7 +139,7 @@ impl Live { scale, rate: MUX_RATE, audio_lag, - start: Instant::now(), + start: moq_clock::now(), tick: 0, audio_index: 0, } @@ -174,7 +174,7 @@ impl Live { /// same timeline from the next group, as its replacement or the other half of a 1+1 pair /// would. async fn fall_silent(&mut self, gap: Duration, legs: &mut [&mut Leg]) { - let resume = Instant::now() + gap; + let resume = moq_clock::now() + gap; advance(resume, legs).await; let media = (resume.duration_since(self.start).as_secs_f64() * self.scale * 1e6) as u64; self.tick = media.div_ceil(VIDEO_US * GOP) * GOP; @@ -191,7 +191,7 @@ impl Live { Leg { export, every, - next_poll: Instant::now(), + next_poll: moq_clock::now(), out: Vec::new(), } } @@ -321,7 +321,7 @@ impl Leg { fn poll(&mut self) { let waiter = kio::Waiter::noop(); - let now = Instant::now(); + let now = moq_clock::now(); while let std::task::Poll::Ready(frame) = self.export.poll_next(&waiter) { match frame.expect("exporter error") { Some(frame) => self.out.push((now, frame)), @@ -335,11 +335,11 @@ impl Leg { loop { self.poll(); if let Some(deadline) = self.export.next_due() { - tokio::time::sleep_until(deadline).await; + moq_clock::sleep_until(deadline).await; continue; } match tokio::time::timeout(Duration::from_millis(10), self.export.next()).await { - Ok(Ok(Some(frame))) => self.out.push((Instant::now(), frame)), + Ok(Ok(Some(frame))) => self.out.push((moq_clock::now(), frame)), Ok(Ok(None)) => break, Err(_) if self.export.next_due().is_none() => break, Err(_) => {} @@ -362,8 +362,8 @@ async fn advance(at: Instant, legs: &mut [&mut Leg]) { .filter(|wake| *wake <= at) .min() { - tokio::time::sleep_until(wake).await; - let now = Instant::now(); + moq_clock::sleep_until(wake).await; + let now = moq_clock::now(); for leg in legs.iter_mut() { if leg.wake().is_some_and(|wake| wake <= now) { leg.poll(); @@ -375,7 +375,7 @@ async fn advance(at: Instant, legs: &mut [&mut Leg]) { } } } - tokio::time::sleep_until(at).await; + moq_clock::sleep_until(at).await; // Read up to the instant the next frame arrives, so the arrival is timed exactly. for leg in legs.iter_mut() { leg.poll_now(); @@ -616,7 +616,7 @@ async fn frames_held_through_a_silence_drop_as_late() { let mut leg = live.join(Duration::ZERO).await; live.run(8 * GOP + 5, &mut [&mut leg], |_| false).await; live.fall_silent(Duration::from_secs(3), &mut [&mut leg]).await; - let resumed = Instant::now(); + let resumed = moq_clock::now(); live.run(live.tick + 8 * GOP, &mut [&mut leg], |_| false).await; let dropped = leg.export.dropped(); diff --git a/rs/moq-mux/src/container/ts/follower.rs b/rs/moq-mux/src/container/ts/follower.rs index e7df986ecb3..8bee334c7ff 100644 --- a/rs/moq-mux/src/container/ts/follower.rs +++ b/rs/moq-mux/src/container/ts/follower.rs @@ -5,7 +5,7 @@ use std::pin::Pin; use std::task::Poll; use std::time::Duration; -use web_async::time::Instant; +use moq_clock::Instant; use super::{Export, catalog}; use crate::container::Frame; @@ -100,7 +100,7 @@ pub struct Follower { closed: bool, linger: Duration, stitch: bool, - timer: Option>>, + timer: Option>>, } impl Follower { @@ -192,7 +192,7 @@ impl Follower { self.state = State::Following { resolving: self.resolve(), awaiting: Awaiting { - deadline: Instant::now() + self.linger, + deadline: moq_clock::now() + self.linger, missing: Missing::Switch, }, fallback: Box::new(State::Running { awaiting }), @@ -226,7 +226,7 @@ impl Follower { return Poll::Pending; } }; - let now = Instant::now(); + let now = moq_clock::now(); // A failure ends the broadcast only if the broadcast goes too. let grace = (end.is_err() && !self.ended && self.serving == Serving::Ours) .then(|| now + CLOSE_GRACE.min(self.linger)); @@ -429,7 +429,7 @@ impl Follower { State::Following { awaiting, .. } => Some(awaiting), State::Settling { .. } | State::Done => None, }; - if !awaiting.is_some_and(|awaiting| Instant::now() >= awaiting.deadline) { + if !awaiting.is_some_and(|awaiting| moq_clock::now() >= awaiting.deadline) { return None; } let (State::Running { @@ -464,12 +464,10 @@ impl Follower { /// Whether `at` has come, registering `waiter` to wake then if not. fn elapsed(&mut self, waiter: &kio::Waiter, at: Instant) -> bool { - if Instant::now() >= at { + if moq_clock::now() >= at { return true; } - let timer = self - .timer - .get_or_insert_with(|| Box::pin(web_async::time::sleep_until(at))); + let timer = self.timer.get_or_insert_with(|| Box::pin(moq_clock::sleep_until(at))); if timer.deadline() != at { timer.as_mut().reset(at); } diff --git a/rs/moq-mux/src/container/ts/import.rs b/rs/moq-mux/src/container/ts/import.rs index 7ee91575e51..5462e52caf9 100644 --- a/rs/moq-mux/src/container/ts/import.rs +++ b/rs/moq-mux/src/container/ts/import.rs @@ -262,7 +262,7 @@ impl Import { /// now completes. The buffer is fully consumed; a trailing partial packet /// (< 188 bytes) is retained for the next call. pub fn decode(&mut self, data: &[u8]) -> anyhow::Result<()> { - self.damage_log.flush(web_async::time::Instant::now()); + self.damage_log.flush(moq_clock::now()); self.scratch.extend_from_slice(data); // Route one whole packet at a time, so a PMT is parsed (and any PID it declares diff --git a/rs/moq-mux/src/container/ts/release.rs b/rs/moq-mux/src/container/ts/release.rs index 611b9360fdb..5c945e951e7 100644 --- a/rs/moq-mux/src/container/ts/release.rs +++ b/rs/moq-mux/src/container/ts/release.rs @@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex}; use std::task::{Context, Poll, Wake, Waker}; use std::time::Duration; -use web_async::time::Instant; +use moq_clock::Instant; use crate::catalog::{CatalogFormat, Stream}; use crate::container::{ExportSource, Frame}; @@ -28,7 +28,7 @@ impl Readable { fn new() -> Self { Self { state: Mutex::new(Availability { - empty: Instant::now(), + empty: moq_clock::now(), first: None, polling: false, during: None, @@ -51,7 +51,7 @@ impl Readable { if pending { // A notification racing the empty read belongs to the next batch. state.first = state.during.take(); - state.empty = Instant::now(); + state.empty = moq_clock::now(); } state.during = None; } @@ -70,7 +70,7 @@ impl Wake for Readable { fn wake_by_ref(self: &Arc) { let mut waiters = { let mut state = self.state.lock().unwrap(); - let now = Instant::now(); + let now = moq_clock::now(); state.first.get_or_insert(now); if state.polling { state.during.get_or_insert(now); @@ -226,7 +226,7 @@ impl Release { }; // A sink can leave arrived objects unread. The first notification after // an empty read distinguishes that backlog from a late network burst. - let read = Instant::now(); + let read = moq_clock::now(); let arrival = Arrival { arrived: self.readable.arrived().min(read), read, @@ -296,7 +296,7 @@ mod tests { let readable = Arc::new(Readable::new()); let input = kio::Waiter::new(Waker::from(readable.clone())); let caller = kio::Waiter::noop(); - let start = Instant::now(); + let start = moq_clock::now(); readable.begin(&caller); tokio::time::advance(Duration::from_millis(20)).await; input.waker().wake_by_ref(); @@ -326,7 +326,7 @@ mod tests { drop(caller); assert_eq!(Arc::strong_count(&wakes), 1); tokio::time::advance(Duration::from_millis(20)).await; - let arrived = Instant::now(); + let arrived = moq_clock::now(); input.waker().wake_by_ref(); tokio::time::advance(Duration::from_secs(1)).await; assert_eq!(readable.arrived(), arrived); diff --git a/rs/moq-mux/src/container/ts/si.rs b/rs/moq-mux/src/container/ts/si.rs index ee5b7cd7ea4..faf7e3535d0 100644 --- a/rs/moq-mux/src/container/ts/si.rs +++ b/rs/moq-mux/src/container/ts/si.rs @@ -29,7 +29,7 @@ use std::collections::{BTreeMap, HashMap}; use std::time::Duration; -use web_async::time::Instant; +use moq_clock::Instant; use bytes::Bytes; use moq_net::Timestamp; @@ -469,7 +469,7 @@ impl Capture { /// the host clock ([`DEBOUNCE`]), and a first snapshot is never delayed. `force` /// overrides the debounce, for end of stream. pub fn flush(&mut self, pts: Timestamp, force: bool) -> anyhow::Result<()> { - let now = Instant::now(); + let now = moq_clock::now(); for (key, entry) in self.entries.iter_mut() { if !entry.dirty { continue; diff --git a/rs/moq-mux/src/import/track.rs b/rs/moq-mux/src/import/track.rs index 58582eb5059..1da51bf77fc 100644 --- a/rs/moq-mux/src/import/track.rs +++ b/rs/moq-mux/src/import/track.rs @@ -405,7 +405,7 @@ impl Track { /// Record a locally encoded frame's transport handoff. Call this only for an original /// encoder; file, pipe, and network imports must leave the clock out of the estimate. - pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: std::time::Instant) -> Result<()> { + pub fn flush(&mut self, timestamp: moq_net::Timestamp, now: moq_clock::Instant) -> Result<()> { match self.kind { TrackKind::Avc3 { ref mut import, .. } | TrackKind::Avc1 { ref mut import, .. } => { import.flush(timestamp, now) @@ -1116,7 +1116,7 @@ mod tests { let (mut broadcast, catalog) = new_broadcast(); let (import, subscriber) = opus_import(&mut broadcast, &catalog); let mut import: Track = import.into(); - let anchor = std::time::Instant::now(); + let anchor = moq_clock::Instant::now(); for (pts, arrival) in [(0, 0), (20, 120)] { let timestamp = Timestamp::from_micros(pts * 1_000).unwrap(); import.decode(b"a", Some(timestamp)).unwrap(); diff --git a/rs/moq-mux/src/jitter.rs b/rs/moq-mux/src/jitter.rs index 6715d535bc7..1e22f3a3ee7 100644 --- a/rs/moq-mux/src/jitter.rs +++ b/rs/moq-mux/src/jitter.rs @@ -6,8 +6,8 @@ use std::pin::Pin; use std::task::Poll; use std::time::Duration; +use moq_clock::{Instant, Sleep}; use moq_net::Timestamp; -use web_async::time::{Instant, Sleep}; /// A frame handed to [`Buffer::push`]. pub(crate) struct Arrival { @@ -780,7 +780,7 @@ impl Buffer { /// The next frame whose deadline has come, in [`Self::front`] order. pub fn poll_next(&mut self, waiter: &kio::Waiter) -> Poll> { loop { - let now = Instant::now(); + let now = moq_clock::now(); let acquired = self.acquired_by(); if acquired.is_some_and(|end| now >= end) { self.acquired(); @@ -806,9 +806,7 @@ impl Buffer { self.timer = None; return Poll::Pending; }; - let timer = self - .timer - .get_or_insert_with(|| Box::pin(web_async::time::sleep_until(wake))); + let timer = self.timer.get_or_insert_with(|| Box::pin(moq_clock::sleep_until(wake))); if timer.deadline() != wake { timer.as_mut().reset(wake); } @@ -834,7 +832,7 @@ impl Buffer { self.tracks .get(key) .and_then(|track| track.queue.front()) - .is_some_and(|(_, _, deadline, _)| self.delay.is_zero() || *deadline <= Instant::now()) + .is_some_and(|(_, _, deadline, _)| self.delay.is_zero() || *deadline <= moq_clock::now()) } /// How many frames are waiting. @@ -960,7 +958,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn releases_at_the_delay_in_decode_order() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY).replay(); assert_eq!(buffer.push(1, arrival(start, 0, "v0")).unwrap(), Push::Queued); assert_eq!(buffer.push(1, arrival(start, 40, "v40")).unwrap(), Push::Queued); @@ -992,7 +990,7 @@ mod tests { /// its deadline, emit the same order. #[tokio::test(start_paused = true)] async fn arrival_skew_does_not_change_the_order() { - let start = Instant::now(); + let start = moq_clock::now(); let (mut early, mut late) = (Buffer::new(DELAY).replay(), Buffer::new(DELAY).replay()); let frames = [ (1, 0, "v0"), @@ -1024,7 +1022,7 @@ mod tests { /// dropped a frame waits for its next sync frame. #[tokio::test(start_paused = true)] async fn late_frames_are_dropped_until_a_sync_frame() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY).replay(); buffer.push(1, arrival(start, 0, "v0")).unwrap(); buffer.push(2, arrival(start, 0, "a0")).unwrap(); @@ -1046,7 +1044,7 @@ mod tests { /// A restart before anything went out keeps the clock, so the tracks stay on one. #[tokio::test(start_paused = true)] async fn a_skip_before_the_first_release_keeps_one_clock() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY).replay(); // The video's first group is stale and anchors the clock; the audio is live. buffer.push(1, arrival(start, 0, "v0")).unwrap(); @@ -1074,7 +1072,7 @@ mod tests { /// runs on it, whether or not that track saw the restart. #[tokio::test(start_paused = true)] async fn every_track_joins_a_new_generation_at_its_next_frame() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY).replay(); buffer.push(1, arrival(start, 0, "v0")).unwrap(); buffer.push(2, arrival(start, 0, "a0")).unwrap(); @@ -1084,7 +1082,7 @@ mod tests { // The publisher resumes a second later with its timeline five seconds on. let resume = start + Duration::from_secs(1); - tokio::time::advance(resume - Instant::now()).await; + tokio::time::advance(resume - moq_clock::now()).await; assert_eq!( buffer.push(1, after(1, arrival(resume, 5_000, "v5000"))).unwrap(), Push::Queued @@ -1110,7 +1108,7 @@ mod tests { /// clock anchors on it, the track sent latest, so it keeps the delay. #[tokio::test(start_paused = true)] async fn a_track_without_the_restart_joins_the_acquisition() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY); buffer.expect([1, 2]); buffer.push(1, arrival(start, 0, "v0")).unwrap(); @@ -1121,19 +1119,19 @@ mod tests { // The publisher resumes a second later with its timeline five seconds on, and sends // the audio further behind the video than the delay. let resume = start + Duration::from_secs(1); - tokio::time::advance(resume - Instant::now()).await; + tokio::time::advance(resume - moq_clock::now()).await; assert_eq!( buffer.push(1, after(1, arrival(resume, 5_000, "v5000"))).unwrap(), Push::Queued ); let lag = resume + DELAY + Duration::from_millis(50); - tokio::time::advance(lag - Instant::now()).await; + tokio::time::advance(lag - moq_clock::now()).await; assert_eq!(buffer.push(2, arrival(lag, 5_000, "a5000")).unwrap(), Push::Queued); tokio::time::advance(Duration::from_millis(20)).await; - let next = Instant::now(); + let next = moq_clock::now(); assert_eq!(buffer.push(2, arrival(next, 5_020, "a5020")).unwrap(), Push::Queued); - tokio::time::advance(lag + DELAY - Instant::now()).await; + tokio::time::advance(lag + DELAY - moq_clock::now()).await; assert_eq!(released(&mut buffer), [("v5000", 1), ("a5000", 1)]); tokio::time::advance(Duration::from_millis(20)).await; assert_eq!(released(&mut buffer), [("a5020", 1)]); @@ -1145,7 +1143,7 @@ mod tests { /// timeline 15 seconds on: each released frame's generation, and how many were dropped. async fn cross(steps: u64, tail: u64) -> (Vec, u64) { const LAG: u64 = 300; - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY); buffer.expect([1, 2]); @@ -1168,7 +1166,7 @@ mod tests { let mut out = Vec::new(); for (read, key, restart, decode) in frames { let at = start + Duration::from_millis(read); - tokio::time::advance(at - Instant::now()).await; + tokio::time::advance(at - moq_clock::now()).await; out.extend(released(&mut buffer).into_iter().map(|(_, generation)| generation)); buffer.push(key, after(restart, arrival(at, decode, "frame"))).unwrap(); } @@ -1206,7 +1204,7 @@ mod tests { /// neither clock, so it drops as late without steering, and the track's next frame on the /// new timeline still joins it. async fn stray(to: u64) { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY); buffer.expect([1]); buffer.push(1, arrival(start, 10_000, "v")).unwrap(); @@ -1215,12 +1213,12 @@ mod tests { assert_eq!(due(&mut buffer), ["v", "d"]); let resume = start + Duration::from_secs(1); - tokio::time::advance(resume - Instant::now()).await; + tokio::time::advance(resume - moq_clock::now()).await; buffer.push(1, after(1, arrival(resume, to, "v'"))).unwrap(); tokio::time::advance(DELAY).await; assert_eq!(released(&mut buffer), [("v'", 1)]); - let now = Instant::now(); + let now = moq_clock::now(); assert_eq!(buffer.push(2, arrival(now, 10_040, "d")).unwrap(), Push::Late); assert!(buffer.is_empty(), "the old frame was queued on the new clock"); assert!(buffer.steer.floor.is_none(), "the old frame was steered on"); @@ -1243,7 +1241,7 @@ mod tests { /// dropped, and the next on-time frame goes out on the same generation. #[tokio::test(start_paused = true)] async fn a_skip_keeps_the_clock() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY); buffer.push(1, arrival(start, 0, "a0")).unwrap(); tokio::time::advance(DELAY).await; @@ -1251,7 +1249,7 @@ mod tests { // Group 1 is skipped: frames from 100 ms on arrive after a 250 ms stall. let stalled = start + Duration::from_millis(350); - tokio::time::advance(stalled - Instant::now()).await; + tokio::time::advance(stalled - moq_clock::now()).await; let skipped = |decode, item| Arrival { skip: 1, ..arrival(stalled, decode, item) @@ -1267,13 +1265,13 @@ mod tests { /// publisher shed, so it opens a generation. #[tokio::test(start_paused = true)] async fn a_skip_that_leaps_ahead_opens_a_generation() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY); buffer.push(1, arrival(start, 0, "a0")).unwrap(); tokio::time::advance(DELAY).await; assert_eq!(due(&mut buffer), ["a0"]); - let now = Instant::now(); + let now = moq_clock::now(); let leapt = Arrival { skip: 1, ..arrival(now, 5_000, "a5000") @@ -1287,7 +1285,7 @@ mod tests { /// clock puts earlier. #[tokio::test(start_paused = true)] async fn generations_go_out_in_turn() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY).replay(); buffer.push(1, arrival(start, 0, "v0")).unwrap(); buffer.push(1, arrival(start, 40, "v40")).unwrap(); @@ -1296,7 +1294,7 @@ mod tests { assert_eq!(due(&mut buffer), ["v0"]); // A backlog across a jump in the timeline, read in one go. - let now = Instant::now(); + let now = moq_clock::now(); buffer.push(1, after(1, arrival(now, 5_000, "v5000"))).unwrap(); buffer.push(2, arrival(now, 4_900, "a4900")).unwrap(); buffer.push(2, arrival(now, 5_020, "a5020")).unwrap(); @@ -1331,7 +1329,7 @@ mod tests { /// fails. async fn drift(ppm: f64, hours: f64, settled: f64, queueing: impl Fn(f64) -> Duration) -> Run { let delay = Duration::from_millis(500); - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(delay); let mut run = Run { err: None, @@ -1356,7 +1354,7 @@ mod tests { } let source = decode.as_secs_f64() * (1.0 + ppm * 1e-6); let arrived = start + Duration::from_secs_f64(source) + queued; - tokio::time::advance(arrived.saturating_duration_since(Instant::now())).await; + tokio::time::advance(arrived.saturating_duration_since(moq_clock::now())).await; let frame = Arrival { arrived, read: arrived, @@ -1487,7 +1485,7 @@ mod tests { /// our clock reaching us at `arrive(track, sent)`, or with the frame of its track before it /// if that is later, or never if `None`, in the order they reach us, until a push fails. async fn feed(ppm: f64, secs: f64, arrive: impl Fn(u16, f64) -> Option) -> Fed { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(Duration::from_millis(500)); let mut frames = Vec::new(); for track in [1, 2] { @@ -1512,7 +1510,7 @@ mod tests { }; for (sent, track, decode) in frames { let arrived = start + Duration::from_secs_f64(sent); - tokio::time::advance(arrived.saturating_duration_since(Instant::now())).await; + tokio::time::advance(arrived.saturating_duration_since(moq_clock::now())).await; let frame = Arrival { arrived, read: arrived, @@ -1678,7 +1676,7 @@ mod tests { /// goes out a delay after it arrived. #[tokio::test(start_paused = true)] async fn a_joiner_anchors_on_its_freshest_frame() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY); let video = |arrived, decode, sync, item| Arrival { sync, @@ -1705,7 +1703,7 @@ mod tests { /// keep the delay: anchored on the video, every audio frame would be late. #[tokio::test(start_paused = true)] async fn a_joiner_anchors_on_the_track_sent_latest() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY); buffer.expect([1, 2]); // Video is sent 300 ms ahead of its decode time, audio as it decodes. @@ -1724,7 +1722,7 @@ mod tests { /// the one sent latest, but two delays at most. #[tokio::test(start_paused = true)] async fn the_acquisition_waits_two_delays_at_most_for_a_silent_track() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY); buffer.expect([1, 2]); buffer.push(1, arrival(start, 0, "v0")).unwrap(); @@ -1738,7 +1736,7 @@ mod tests { /// moment each was read. #[tokio::test(start_paused = true)] async fn a_steering_step_does_not_reorder_a_tie() { - let start = Instant::now(); + let start = moq_clock::now(); let order = |first: (u16, &'static str), second: (u16, &'static str)| { let mut buffer = Buffer::new(DELAY).replay(); buffer.push(1, arrival(start, 0, "v0")).unwrap(); @@ -1758,7 +1756,7 @@ mod tests { /// acquisition ends when the first of them falls due rather than drop it. #[tokio::test(start_paused = true)] async fn the_acquisition_ends_before_a_held_frame_is_due() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY); // Joined 60 ms into a group: its keyframe is due 40 ms from now. let key = Arrival { @@ -1784,7 +1782,7 @@ mod tests { /// frame behind the clock still goes out. #[tokio::test(start_paused = true)] async fn zero_delay_releases_on_arrival() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(Duration::ZERO); buffer.push(1, arrival(start, 0, "v0")).unwrap(); buffer.push(1, arrival(start, 40, "v40")).unwrap(); @@ -1801,7 +1799,7 @@ mod tests { /// reader leaves the rest in the source. #[tokio::test(start_paused = true)] async fn a_track_is_full_once_a_frame_is_due() { - let start = Instant::now(); + let start = moq_clock::now(); let mut buffer = Buffer::new(DELAY).replay(); assert!(!buffer.full(&1)); buffer.push(1, arrival(start, 0, "v0")).unwrap(); diff --git a/rs/moq-mux/src/json.rs b/rs/moq-mux/src/json.rs index 2e65a85154b..0a1fdb054c6 100644 --- a/rs/moq-mux/src/json.rs +++ b/rs/moq-mux/src/json.rs @@ -36,7 +36,7 @@ //! //! A value timed on the broadcast [`Clock`](crate::Clock) is written at that timestamp as given, //! and the entry advertises how late values reach the transport as its `jitter` and `delay`, the -//! way a media rendition does. A capture [`Instant`](std::time::Instant) converts with +//! way a media rendition does. A capture [`Instant`](moq_clock::Instant) converts with //! [`Clock::capture`](crate::Clock::capture) on the catalog's clock: //! //! ```no_run @@ -44,7 +44,7 @@ //! # catalog: &moq_mux::catalog::Producer, //! # gps: &mut moq_mux::json::Stream, //! # fix: serde_json::Value, -//! # received: std::time::Instant, +//! # received: moq_clock::Instant, //! # ) -> moq_mux::Result<()> { //! let at = catalog.clock().capture(received)?; //! gps.append(moq_net::Timed::from(&fix).at(at))?; diff --git a/rs/moq-mux/src/lib.rs b/rs/moq-mux/src/lib.rs index 7fb00707feb..c4bb052d27e 100644 --- a/rs/moq-mux/src/lib.rs +++ b/rs/moq-mux/src/lib.rs @@ -1,3 +1,5 @@ +#![cfg_attr(test, allow(clippy::disallowed_methods))] + //! Media muxers and demuxers for MoQ. //! //! Sits between [`moq_net`] (pub/sub transport) and [`hang`] (media diff --git a/rs/moq-mux/src/pace.rs b/rs/moq-mux/src/pace.rs index c482bfa7462..c99eea7531b 100644 --- a/rs/moq-mux/src/pace.rs +++ b/rs/moq-mux/src/pace.rs @@ -1,6 +1,7 @@ //! Pacing of media frames onto the wall clock. -use std::time::{Duration, Instant}; +use moq_clock::Instant; +use std::time::Duration; use moq_net::Timestamp; @@ -211,7 +212,7 @@ mod tests { // Tune-in burst: the live edge (4132ms of media) is produced ~8ms after the // first frame (1400ms). It must re-anchor to `now` rather than schedule // ~2.7s into the future. - let start = Instant::now(); + let start = Instant::ZERO + Duration::from_secs(1); let mut pacer = Pacer::default(); assert_eq!(pacer.pace(ms(1_400), start), start, "the first frame anchors at now"); @@ -247,7 +248,7 @@ mod tests { /// frame onto the anchor. #[test] fn paces_across_timescales() { - let start = Instant::now(); + let start = moq_clock::now(); let mut pacer = Pacer::default().with_lead(Duration::from_millis(500)); // A PCR slot at microsecond scale anchors the stream. @@ -265,7 +266,7 @@ mod tests { /// panics on a large but valid `Duration` (`--delay` is unbounded). #[test] fn huge_lead_does_not_overflow() { - let start = Instant::now(); + let start = moq_clock::now(); let mut pacer = Pacer::default().with_lead(Duration::MAX); assert_eq!(pacer.pace(ms(0), start), start); assert_eq!(pacer.pace(ms(40), start), start + Duration::from_millis(40)); @@ -278,7 +279,7 @@ mod tests { /// arrival cadence rather than the media one. #[test] fn absorbs_a_standing_delivery_lag() { - let start = Instant::now(); + let start = moq_clock::now(); let mut pacer = Pacer::default().with_lead(Duration::from_millis(500)); assert_eq!(pacer.pace(ms(0), start), start, "the first frame anchors at now"); @@ -298,7 +299,7 @@ mod tests { /// rather than growing with every outlier. #[test] fn absorbed_lag_is_capped_by_the_lead() { - let start = Instant::now(); + let start = moq_clock::now(); let mut pacer = Pacer::default().with_lead(Duration::from_millis(50)); assert_eq!(pacer.pace(ms(0), start), start); @@ -315,7 +316,7 @@ mod tests { /// Zero lead (SRT, whose receiver owns the jitter buffer) absorbs nothing. #[test] fn zero_lead_absorbs_nothing() { - let start = Instant::now(); + let start = moq_clock::now(); let mut pacer = Pacer::default(); assert_eq!(pacer.pace(ms(0), start), start); @@ -326,7 +327,7 @@ mod tests { #[test] fn hurry_makes_the_frame_the_live_edge() { - let start = Instant::now(); + let start = moq_clock::now(); let mut pacer = Pacer::default().with_lead(Duration::from_millis(500)); assert_eq!(pacer.pace(ms(0), start), start); @@ -342,7 +343,7 @@ mod tests { /// onto a re-anchor the delay itself provoked. #[test] fn the_delay_is_outside_the_lead_comparison() { - let start = Instant::now(); + let start = moq_clock::now(); let mut pacer = Pacer::default().with_delay(Duration::from_millis(100)); assert_eq!(pacer.pace(ms(0), start), start + Duration::from_millis(100)); @@ -360,7 +361,7 @@ mod tests { /// playback back toward live rather than leaving it there for the session. #[test] fn an_earlier_arrival_re_anchors_and_keeps_the_delay() { - let start = Instant::now(); + let start = moq_clock::now(); let mut pacer = Pacer::default().with_delay(Duration::from_millis(100)); // The first frame was produced 500ms ago; nothing here knows that yet. @@ -385,7 +386,7 @@ mod tests { /// present at it: the offset playback holds behind the edge has to survive. #[test] fn hurry_keeps_the_delay() { - let start = Instant::now(); + let start = moq_clock::now(); let mut pacer = Pacer::default().with_delay(Duration::from_millis(100)); assert_eq!(pacer.hurry(ms(800), start), start + Duration::from_millis(100)); @@ -398,7 +399,7 @@ mod tests { /// second arrival in. #[test] fn due_reads_the_anchor_without_moving_it() { - let start = Instant::now(); + let start = moq_clock::now(); let mut pacer = Pacer::default().with_delay(Duration::from_millis(100)); assert_eq!(pacer.due(ms(0)), None, "no anchor until the first frame"); @@ -414,7 +415,7 @@ mod tests { #[test] fn lead_schedules_bursts_into_the_future() { - let start = Instant::now(); + let start = moq_clock::now(); let mut pacer = Pacer::default().with_lead(Duration::from_millis(500)); assert_eq!(pacer.pace(ms(0), start), start); diff --git a/rs/moq-mux/src/rate.rs b/rs/moq-mux/src/rate.rs index 949a64e3e26..f5744652424 100644 --- a/rs/moq-mux/src/rate.rs +++ b/rs/moq-mux/src/rate.rs @@ -4,7 +4,7 @@ //! rate a sender should produce, so audio, video, and transcode back off the //! same way. -use std::time::Instant; +use moq_clock::Instant; use moq_net::bandwidth; @@ -71,11 +71,11 @@ impl Policy { /// ``` /// # use moq_mux::rate::{Control, Policy}; /// # use moq_net::bandwidth; -/// # use std::time::Instant; +/// # use moq_clock::Instant; /// let mut control = Control::new(Policy::new(bandwidth::Rate::from_mbps(4))); /// // A 2 Mbps estimate takes the 4 Mbps target down to what the link will carry. /// assert_eq!( -/// control.update(Some(bandwidth::Rate::from_mbps(2)), Instant::now()), +/// control.update(Some(bandwidth::Rate::from_mbps(2)), moq_clock::now()), /// Some(bandwidth::Rate::from_mbps(2)) /// ); /// ``` @@ -195,7 +195,7 @@ mod tests { let mut control = control(); // A 2 Mbps pipe: take the target down to it at once, no ramp, no waiting. assert_eq!( - control.update(Some(bps(2_000_000)), Instant::now()), + control.update(Some(bps(2_000_000)), moq_clock::now()), Some(bps(2_000_000)) ); assert_eq!(control.target(), bps(2_000_000)); @@ -204,7 +204,7 @@ mod tests { #[test] fn missing_estimate_holds_the_target() { let mut control = control(); - let now = Instant::now(); + let now = moq_clock::now(); control.update(Some(bps(2_000_000)), now).unwrap(); // Losing the estimate (disconnected) is not evidence the uplink is @@ -217,7 +217,7 @@ mod tests { fn estimate_never_raises_above_max() { let mut control = control(); // A wildly optimistic estimate is not licence to exceed what was configured. - assert_eq!(control.update(Some(bps(100_000_000)), Instant::now()), None); + assert_eq!(control.update(Some(bps(100_000_000)), moq_clock::now()), None); assert_eq!(control.target(), bps(4_000_000)); } @@ -225,14 +225,14 @@ mod tests { fn target_never_falls_below_min() { let mut control = control(); // A near-dead uplink floors at min (max/10) rather than chasing to zero. - assert_eq!(control.update(Some(bps(1)), Instant::now()), Some(bps(400_000))); + assert_eq!(control.update(Some(bps(1)), moq_clock::now()), Some(bps(400_000))); assert_eq!(control.target(), bps(400_000)); } #[test] fn raise_is_ramp_limited() { let mut control = control(); - let start = Instant::now(); + let start = moq_clock::now(); control.update(Some(bps(1_000_000)), start).unwrap(); // target 1M // The pipe reopens to 4 Mbps. One second later the default 25%/s ramp @@ -246,7 +246,7 @@ mod tests { #[test] fn raise_eventually_reaches_the_estimate() { let mut control = control(); - let start = Instant::now(); + let start = moq_clock::now(); control.update(Some(bps(1_000_000)), start).unwrap(); // target 1M // Feed a steady healthy estimate every 100ms; the ramp should walk the @@ -265,7 +265,7 @@ mod tests { #[test] fn suppressed_raises_do_not_starve_the_ramp() { let mut control = control(); - let start = Instant::now(); + let start = moq_clock::now(); control.update(Some(bps(1_000_000)), start).unwrap(); // target 1M // Tick at 100ms: each tick alone is under the 5% threshold. @@ -291,7 +291,7 @@ mod tests { #[test] fn a_raise_reaching_the_ceiling_beats_hysteresis() { let mut control = control(); - let start = Instant::now(); + let start = moq_clock::now(); control.update(Some(bps(1_000_000)), start).unwrap(); // Walk up until it settles, then confirm where it settled. @@ -317,7 +317,7 @@ mod tests { #[test] fn upward_jitter_stays_inside_the_deadband() { let mut control = control(); - let start = Instant::now(); + let start = moq_clock::now(); control.update(Some(bps(2_000_000)), start).unwrap(); // target 2M, well under the 4M ceiling // A 0.5% rise: inside the 5% deadband, and nowhere near the ceiling. @@ -349,7 +349,7 @@ mod tests { #[test] fn small_moves_are_suppressed() { let mut control = control(); - let now = Instant::now(); + let now = moq_clock::now(); control.update(Some(bps(2_000_000)), now).unwrap(); // target 2M // 2% under the current target: inside the 5% deadband, so no reconfigure. @@ -371,7 +371,7 @@ mod tests { let mut control = Control::new(policy); assert_eq!(control.target(), bps(1_000_000)); - assert_eq!(control.update(Some(bps(2_000_000)), Instant::now()), None); + assert_eq!(control.update(Some(bps(2_000_000)), moq_clock::now()), None); assert_eq!(control.target(), bps(1_000_000)); } } diff --git a/rs/moq-net/AGENTS.md b/rs/moq-net/AGENTS.md index f2b1f80a8c3..57028cec509 100644 --- a/rs/moq-net/AGENTS.md +++ b/rs/moq-net/AGENTS.md @@ -11,7 +11,7 @@ The wire layer. Generic over the transport and media-agnostic: the relay never i # Testing -- Tests never use tokio or the wall clock. Prefer a plain `#[test]` that polls with explicit instants; async bodies use `#[moq_net_sim::test]`, whose simulated time jumps only when every task is idle. tokio is for the benches. +- Tests never use tokio or the wall clock. Prefer a plain `#[test]` that polls with explicit instants; async bodies use `#[moq_clock::test]`, whose simulated time jumps only when every task is stalled. tokio is for the benches. - `just rs fuzz ` (one per file in `fuzz/fuzz_targets/`) needs nightly. The target bodies live in `src/fuzz.rs`; `fuzz/regressions//` replays under `just check`, so commit every crash input there. - `just rs loom` model-checks the concurrent handoffs. A hang is a lost wakeup, not a flake. - `just test interop --all` runs the cross-language interop matrix after a wire change. diff --git a/rs/moq-net/Cargo.toml b/rs/moq-net/Cargo.toml index ed67b1e1b6f..095aa8d4955 100644 --- a/rs/moq-net/Cargo.toml +++ b/rs/moq-net/Cargo.toml @@ -18,7 +18,7 @@ ignored = ["getrandom"] [features] default = ["async"] # Runtime-backed helpers and Lite/IETF sessions; explicit-time model drivers stay available. -async = ["dep:futures", "dep:web-async"] +async = ["dep:futures"] # Exposes the wire codecs to the `fuzz/` harness and the announce, codec, varint, and frame benches, # and session views of an origin to the viewers bench, through the hidden `fuzz` module. Off by @@ -30,6 +30,7 @@ arrayvec = "0.7" bytes = { workspace = true } futures = { workspace = true, optional = true } kio = { workspace = true } +moq-clock = { workspace = true } moq-pattern = { workspace = true, features = ["serde"] } num_enum = { workspace = true } rand = { workspace = true } @@ -38,17 +39,12 @@ slab = { workspace = true } thiserror = { workspace = true } tracing = { workspace = true } uuid = { workspace = true, features = ["js"] } -web-async = { workspace = true, optional = true } - -# The browser instant shim is needed even by explicit-time drivers. -[target.'cfg(target_family = "wasm")'.dependencies] -web-async = { workspace = true } [dev-dependencies] criterion = { workspace = true } futures = { workspace = true } # The tests' executor, with simulated time. -moq-net-sim = { path = "sim" } +moq-clock = { path = "../moq-clock", features = ["sim"] } serde_json = { workspace = true } # The benches run on tokio. test-util (tokio::time::pause/advance) is test-only and # is NOT supported on wasm, so it must not leak into the normal dependency feature set. diff --git a/rs/moq-net/benches/memory.rs b/rs/moq-net/benches/memory.rs index 790525feabd..33825a4bd06 100644 --- a/rs/moq-net/benches/memory.rs +++ b/rs/moq-net/benches/memory.rs @@ -77,7 +77,7 @@ fn runtime() -> tokio::runtime::Runtime { } fn now() -> moq_net::time::Instant { - tokio::time::Instant::now().into_std() + moq_clock::now() } /// One publisher and one subscriber session over an in-memory pair. diff --git a/rs/moq-net/benches/track.rs b/rs/moq-net/benches/track.rs index d2f4c0c1c3b..fb7f8aabdcd 100644 --- a/rs/moq-net/benches/track.rs +++ b/rs/moq-net/benches/track.rs @@ -46,7 +46,9 @@ use std::hint::black_box; use std::sync::{Arc, Barrier}; use std::task::Poll; -use std::time::{Duration, Instant}; +use std::time::{Duration, Instant as WallInstant}; + +use moq_net::time::Instant; use bytes::Bytes; use criterion::{BatchSize, BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; @@ -427,7 +429,7 @@ fn parallel_write(pool: &cache::Pool, writers: usize, iterations: u64) -> Durati // The first wait returns once every writer has built its producer, keeping setup out // of the interval. The second releases them, so no write lands before the stamp. barrier.wait(); - let start = Instant::now(); + let start = WallInstant::now(); barrier.wait(); for handle in handles { handle.join().unwrap(); @@ -626,7 +628,7 @@ fn bench_parked_read(c: &mut Criterion) { // Each append above woke the reads before it: park them again. repoll(&mut reads); - let start = Instant::now(); + let start = WallInstant::now(); for _ in 0..iterations { let mut next = track.append_group().unwrap(); next.write_frame(Timestamp::from_micros(micros).unwrap(), Bytes::from_static(b"x")) @@ -675,7 +677,7 @@ fn bench_sparse_read(c: &mut Criterion) { for gap in 0..gaps { hand_out(gap * 2); } - let start = Instant::now(); + let start = WallInstant::now(); for iteration in 0..iterations { hand_out((gaps + iteration) * 2); hand_out(iteration * 2 + 1); diff --git a/rs/moq-net/clippy.toml b/rs/moq-net/clippy.toml new file mode 100644 index 00000000000..892532bf144 --- /dev/null +++ b/rs/moq-net/clippy.toml @@ -0,0 +1,9 @@ +disallowed-methods = [ + { path = "std::time::Instant::now", reason = "read the ambient clock: moq_clock::now()" }, + { path = "std::time::SystemTime::now", reason = "read the ambient clock: moq_clock::wall()" }, + { path = "tokio::time::Instant::now", reason = "read the ambient clock: moq_clock::now()" }, + { path = "tokio::time::sleep", reason = "sleep on the ambient clock: moq_clock::sleep()" }, + { path = "tokio::time::sleep_until", reason = "sleep on the ambient clock: moq_clock::sleep_until()" }, + { path = "tokio::time::timeout", reason = "use the ambient clock: moq_clock::timeout()" }, + { path = "tokio::time::interval", reason = "use the ambient clock: moq_clock::interval()" }, +] diff --git a/rs/moq-net/sim/Cargo.toml b/rs/moq-net/sim/Cargo.toml deleted file mode 100644 index 702761fa733..00000000000 --- a/rs/moq-net/sim/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -# moq-net's test executor: single-threaded, deterministic, with simulated time. -# -# A workspace member so it shares the root lockfile. Never published: moq-net takes -# it as a path-only dev-dependency, which `cargo publish` strips. -[package] -name = "moq-net-sim" -version = "0.0.0" -license = "MIT OR Apache-2.0" -publish = false -edition = "2024" -rust-version.workspace = true - -[dependencies] -futures = { workspace = true } -kio = { workspace = true } -moq-net-sim-macros = { path = "macros" } diff --git a/rs/moq-net/sim/macros/Cargo.toml b/rs/moq-net/sim/macros/Cargo.toml deleted file mode 100644 index a4a34fb0057..00000000000 --- a/rs/moq-net/sim/macros/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -# The `#[moq_net_sim::test]` attribute. A separate crate only because a proc macro -# must be; `moq-net-sim` re-exports it. -[package] -name = "moq-net-sim-macros" -version = "0.0.0" -license = "MIT OR Apache-2.0" -publish = false -edition = "2024" -rust-version.workspace = true - -[lib] -proc-macro = true diff --git a/rs/moq-net/sim/macros/src/lib.rs b/rs/moq-net/sim/macros/src/lib.rs deleted file mode 100644 index a4131d905dc..00000000000 --- a/rs/moq-net/sim/macros/src/lib.rs +++ /dev/null @@ -1,46 +0,0 @@ -//! The `#[moq_net_sim::test]` attribute: an `async fn` test run on the simulated executor. - -use proc_macro::{Delimiter, Group, Ident, Span, TokenStream, TokenTree}; - -/// Run an `async fn` test to completion on [`moq_net_sim::run`](../moq_net_sim/fn.run.html). -#[proc_macro_attribute] -pub fn test(args: TokenStream, item: TokenStream) -> TokenStream { - if !args.is_empty() { - return error("#[moq_net_sim::test] takes no arguments"); - } - - let mut tokens: Vec = item.into_iter().collect(); - let is = |token: &TokenTree, name: &str| matches!(token, TokenTree::Ident(ident) if ident.to_string() == name); - let Some(at) = tokens - .windows(2) - .position(|pair| is(&pair[0], "async") && is(&pair[1], "fn")) - else { - return error("#[moq_net_sim::test] expects an `async fn`"); - }; - tokens.remove(at); - - let body = match tokens.pop() { - Some(TokenTree::Group(body)) if body.delimiter() == Delimiter::Brace => body, - _ => return error("#[moq_net_sim::test] expects a function body"), - }; - - // `{ ::moq_net_sim::run(async move ) }` - let mut call: TokenStream = "::moq_net_sim::run".parse().unwrap(); - let block: TokenStream = [ - TokenTree::Ident(Ident::new("async", Span::call_site())), - TokenTree::Ident(Ident::new("move", Span::call_site())), - TokenTree::Group(body), - ] - .into_iter() - .collect(); - call.extend([TokenTree::Group(Group::new(Delimiter::Parenthesis, block))]); - - let mut out: TokenStream = "#[::core::prelude::v1::test]".parse().unwrap(); - out.extend(tokens); - out.extend([TokenTree::Group(Group::new(Delimiter::Brace, call))]); - out -} - -fn error(message: &str) -> TokenStream { - format!("::core::compile_error!({message:?});").parse().unwrap() -} diff --git a/rs/moq-net/sim/src/lib.rs b/rs/moq-net/sim/src/lib.rs deleted file mode 100644 index 527316ebb91..00000000000 --- a/rs/moq-net/sim/src/lib.rs +++ /dev/null @@ -1,708 +0,0 @@ -//! A single-threaded, deterministic executor with simulated time, for moq-net's tests. -//! -//! Time never moves on its own. Once every task has stalled, [`run`] jumps the clock -//! to the earliest armed timer and wakes it, like tokio's paused clock, so a test -//! never waits on the wall clock. A run whose tasks are all parked with nothing -//! armed can never finish, so it panics rather than hang, unless a task awaits -//! [`idle`], which completes at exactly that point. -//! -//! Protocol drivers take time from their caller: [`drive`] polls one with the -//! simulated instant, and [`attach`] keeps an externally owned clock in step. - -use std::{ - cell::{Cell, RefCell}, - collections::BTreeMap, - future::{Future, IntoFuture}, - pin::{Pin, pin}, - rc::Rc, - sync::{ - Arc, - atomic::{AtomicBool, Ordering}, - }, - task::{Context, Poll, Wake, Waker}, - time::{Duration, Instant}, -}; - -use futures::{FutureExt, StreamExt, stream::FuturesUnordered}; - -pub use moq_net_sim_macros::test; - -thread_local! { - static CURRENT: RefCell>> = const { RefCell::new(None) }; -} - -/// A source of deadlines kept in step with simulated time: called with the new -/// instant whenever time moves, it returns its earliest deadline still pending. -type Source = Box Option>; - -type Task = Pin>>; - -struct Runtime { - /// Spawned since the run loop last collected them. - spawned: RefCell>, - time: RefCell