diff --git a/Cargo.lock b/Cargo.lock index 5867e389..bcc29632 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,7 +86,7 @@ dependencies = [ "futures-lite", "futures-util", "serde", - "zbus 5.19.0", + "zbus", ] [[package]] @@ -349,15 +349,15 @@ dependencies = [ "objc2-foundation 0.3.2", "parking_lot", "percent-encoding", - "windows-sys 0.61.2", + "windows-sys 0.52.0", "x11rb 0.13.2", ] [[package]] name = "arc-swap" -version = "1.9.3" +version = "1.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "380962b3d93b99642e50b9febf72c071e97e8b32d86eae9cf4ef582bb960ae6e" +checksum = "db4c94f57a570fce5d381be3289b3f79aef6332d72948fe82ab5772e423ccf92" dependencies = [ "rustversion", ] @@ -399,23 +399,9 @@ dependencies = [ "futures-util", "getrandom 0.4.3", "serde", - "tokio", - "zbus 5.19.0", -] - -[[package]] -name = "ashpd" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ba377f1fb1e3972907220f1f8e7c0a45164580905c981d872c1fb90d921cbcc" -dependencies = [ - "bitflags 2.13.2", - "futures-util", - "getrandom 0.4.3", - "serde", "serde_repr", "tokio", - "zbus 6.3.0", + "zbus", ] [[package]] @@ -544,13 +530,13 @@ dependencies = [ [[package]] name = "async-recursion" -version = "1.1.1" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b43422f69d8ff38f95f1b2bb76517c91589a924d1559a0e935d7c8ce0274c11" +checksum = "4f8abc12baad266b1c8cec146854c195b5864b4221d4b2ca7296a7ae82d9e451" dependencies = [ "proc-macro2", "quote", - "syn 2.0.119", + "syn 3.0.6", ] [[package]] @@ -619,7 +605,7 @@ dependencies = [ "enumflags2", "serde", "static_assertions", - "zbus 5.19.0", + "zbus", "zbus-lockstep", "zbus-lockstep-macros", "zbus_names", @@ -634,7 +620,7 @@ checksum = "2230e48787ed3eb4088996eab66a32ca20c0b67bbd4fd6cdfe79f04f1f04c9fc" dependencies = [ "atspi-common", "serde", - "zbus 5.19.0", + "zbus", ] [[package]] @@ -2431,7 +2417,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -2560,6 +2546,17 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" +[[package]] +name = "flume" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e139bc46ca777eb5efaf62df0ab8cc5fd400866427e56c68b22e414e53bd3be" +dependencies = [ + "futures-core", + "futures-sink", + "spin", +] + [[package]] name = "fnv" version = "1.0.7" @@ -3550,6 +3547,16 @@ dependencies = [ "icu_properties", ] +[[package]] +name = "if-addrs" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0a05c691e1fae256cf7013d99dad472dc52d5543322761f83ec8d47eab40d2b" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "image" version = "0.25.10" @@ -3643,6 +3650,17 @@ dependencies = [ "rustversion", ] +[[package]] +name = "io-uring" +version = "0.7.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed3bd0ecfbb87805f538bb7b32e5239ca0763890c623e349860ecba69469f2bb" +dependencies = [ + "bitflags 2.13.2", + "cfg-if", + "libc", +] + [[package]] name = "ipnet" version = "2.12.2" @@ -4225,6 +4243,21 @@ dependencies = [ "rawpointer", ] +[[package]] +name = "mdns-sd" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6743ed743ff2bada2b8651ef298d3f98addb0b74844f19cdd57137d8bd0219e" +dependencies = [ + "fastrand", + "flume", + "if-addrs", + "log", + "mio", + "socket-pktinfo", + "socket2", +] + [[package]] name = "memchr" version = "2.8.3" @@ -4355,6 +4388,8 @@ dependencies = [ "ringbuf", "rubato", "sonora", + "symphonia-codec-aac", + "symphonia-core", "thiserror 2.0.21", "tokio", "tracing", @@ -4395,21 +4430,17 @@ dependencies = [ "kio", "rand", "rand_pcg", - "tracing", ] [[package]] name = "moq-crypto" version = "0.0.11" dependencies = [ - "bytes", "data-encoding", - "kio", "moq-net", "rind", "rind-rustls", "rustls", - "serde", "thiserror 2.0.21", "zeroize", ] @@ -4554,15 +4585,20 @@ dependencies = [ "hex", "http-body", "http-cache-reqwest", + "hyper", "hyper-util", "kio", + "libc", "moq-auth", "moq-crypto", "moq-net", "moq-quic", + "moq-sock", "moq-stats", "moq-tokio", + "moq-uring", "prometheus-client", + "qmux", "rand", "reqwest", "reqwest-middleware", @@ -4576,11 +4612,13 @@ dependencies = [ "thiserror 2.0.21", "tokio", "tokio-rustls", + "tokio-tungstenite", "toml", "tower-service", "tracing", "url", "usage-rs", + "web-transport-proto", ] [[package]] @@ -4663,7 +4701,7 @@ name = "moq-studio" version = "0.1.0" dependencies = [ "anyhow", - "ashpd 0.13.13", + "ashpd", "bytes", "eframe", "egui", @@ -4724,6 +4762,7 @@ dependencies = [ "jni", "kio", "libc", + "mdns-sd", "moq-congestion", "moq-crypto", "moq-net", @@ -4758,6 +4797,31 @@ dependencies = [ "x509-parser", ] +[[package]] +name = "moq-uring" +version = "0.0.12" +dependencies = [ + "bytes", + "futures", + "http", + "io-uring", + "kio", + "libc", + "moq-congestion", + "moq-crypto", + "moq-net", + "moq-quic", + "moq-sock", + "rustc-hash 2.1.3", + "rustls", + "send_wrapper", + "slab", + "thiserror 2.0.21", + "tracing", + "url", + "web-transport-proto", +] + [[package]] name = "moq-v4l" version = "0.1.0" @@ -4783,7 +4847,7 @@ version = "0.1.10" dependencies = [ "anyhow", "ash", - "ashpd 0.14.0", + "ashpd", "block2 0.6.2", "bytemuck", "bytes", @@ -5066,7 +5130,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.59.0", ] [[package]] @@ -6629,6 +6693,12 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "regex-lite" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab834c73d247e67f4fae452806d17d3c7501756d98c8808d7c9c7aa7d18f973" + [[package]] name = "regex-syntax" version = "0.8.11" @@ -6953,7 +7023,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -7010,7 +7080,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -7198,6 +7268,12 @@ version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" +[[package]] +name = "send_wrapper" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73" + [[package]] name = "serde" version = "1.0.229" @@ -7566,6 +7642,17 @@ dependencies = [ "serde_core", ] +[[package]] +name = "socket-pktinfo" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "612942246d0cc239cfd83af1dfd39be47f649208a3524e5e9da651910128e0ac" +dependencies = [ + "libc", + "socket2", + "windows-sys 0.61.2", +] + [[package]] name = "socket2" version = "0.6.5" @@ -7578,9 +7665,9 @@ dependencies = [ [[package]] name = "sonora" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e1ce442ab5cb392c4067b8f2cced20be4cf12e479f6dfbf44acbe9b14ac9250" +checksum = "d461e6c9ad112f8728919387a4bc5639fd28631f2070171efad38676e907edd8" dependencies = [ "sonora-aec3", "sonora-agc2", @@ -7592,9 +7679,9 @@ dependencies = [ [[package]] name = "sonora-aec3" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50280e31256d09f7838aff6715ed4b7640e514dc1092a1e14013c01e552380db" +checksum = "f2809c3fda13f71deac22d0ddb6c02c493f254d3da2afe59a7e256a8f15bcffe" dependencies = [ "sonora-common-audio", "sonora-fft", @@ -7603,9 +7690,9 @@ dependencies = [ [[package]] name = "sonora-agc2" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a93a9fa8373a33e61a7fb441d8a9e725ac6024079d2c77a845cf6abe1790b9f" +checksum = "501b5dd3f8bdc8c983dc64a131cef22a429f3ff879d6f0007b8bfe46ec7b96aa" dependencies = [ "bytemuck", "derive_more", @@ -7617,9 +7704,9 @@ dependencies = [ [[package]] name = "sonora-common-audio" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6635bc61b24807ceac93ad0807b1292b6b0620caa6cd360d3be89fc616b833cc" +checksum = "2c05dfa300c14dc8498751ce2c5b87aad2acf1892d4f9d66b92ae2f372a83410" dependencies = [ "derive_more", "sonora-simd", @@ -7627,28 +7714,40 @@ dependencies = [ [[package]] name = "sonora-fft" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "27e272c70f34363afb5382dd1818c05f8f63ef6785635736a14e9ccbbb5ba012" +checksum = "ec3529f2cf748f1acc85d9403ffa08a3fe69dec6bf22383910b3bd21aca2fd26" +dependencies = [ + "sonora-simd", +] [[package]] name = "sonora-ns" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d511feef19302c716d245c4555087d161ba91c178735ea11107dcce0d0b7a86f" +checksum = "31c0a62e78419a174dbf9c9c01943c1ecd8c298a68d15c4fb4906cb19f0428b9" dependencies = [ "sonora-fft", ] [[package]] name = "sonora-simd" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7cf0804e4752a5562262e143014f53043080ead4cd5a98f9278d3ceaf32d868" +checksum = "f939d8e63a2a0baa7555294d7b09c5e3d382f28358755b920e13ad527658c8fa" dependencies = [ "cpufeatures", ] +[[package]] +name = "spin" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3763264f6b73151db08c50ff20d7d8a0b8796e021cdea7ceedad07b80155fa0e" +dependencies = [ + "lock_api", +] + [[package]] name = "spirv" version = "0.4.0+sdk-1.4.341.0" @@ -7948,6 +8047,56 @@ dependencies = [ "siphasher", ] +[[package]] +name = "symphonia-codec-aac" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f5bf8e39552d34a3c4c98333370e62f48c92456d2e814f273b5c3ad7c4a5f45c" +dependencies = [ + "lazy_static", + "log", + "symphonia-common", + "symphonia-core", +] + +[[package]] +name = "symphonia-common" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acc3fcc18ec9b8cdd48614e259c4cf0d27b71d41e5d9b120b42c5adab12d7c4" +dependencies = [ + "log", + "symphonia-core", + "symphonia-metadata", +] + +[[package]] +name = "symphonia-core" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01c412864d599d4750d0c3d684d7e093ec05e5309681ef5252cc1096a437f6e0" +dependencies = [ + "bitflags 2.13.2", + "bytemuck", + "lazy_static", + "log", + "num-complex", + "smallvec", +] + +[[package]] +name = "symphonia-metadata" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83713a97705d77bdef7cdbc0768fd6e5a54e4cd7e48d60a806ae85639e2c87c6" +dependencies = [ + "lazy_static", + "log", + "regex-lite", + "smallvec", + "symphonia-core", +] + [[package]] name = "syn" version = "1.0.109" @@ -8083,7 +8232,7 @@ dependencies = [ "getrandom 0.4.3", "once_cell", "rustix 1.1.6", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -9623,7 +9772,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -10122,39 +10271,11 @@ dependencies = [ "uuid", "windows-sys 0.61.2", "winnow", - "zbus_macros 5.19.0", + "zbus_macros", "zbus_names", "zvariant", ] -[[package]] -name = "zbus" -version = "6.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b4aae740b23927ceabed962431abab2cdefb9f7f885e5bbab062da09bf580e4" -dependencies = [ - "async-trait", - "bitflags 2.13.2", - "endi", - "futures-core", - "hex", - "libc", - "ordered-stream", - "rustix 1.1.6", - "serde", - "serde_repr", - "socket2", - "tokio", - "uds_windows", - "uuid", - "windows-sys 0.61.2", - "winnow", - "zbus_macros 6.3.0", - "zbus_utils", - "zcheapstr", - "zruntime", -] - [[package]] name = "zbus-lockstep" version = "0.5.2" @@ -10194,42 +10315,17 @@ dependencies = [ "zvariant_utils", ] -[[package]] -name = "zbus_macros" -version = "6.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37c9dd4beaf145c16fcb2902862f50da86e54aed13dd368ddf64a1d17e6b8529" -dependencies = [ - "proc-macro2", - "quote", - "syn 3.0.6", - "zbus_utils", -] - [[package]] name = "zbus_names" -version = "4.3.4" +version = "4.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8bf88b4a3ff53e883001e0e0115b297a9d53c31b9c1edd2bfdd853e3428624e" +checksum = "812a12be4ead7a59e18ea8f047d08add0066b73bc62fc1467066a2472ae088dd" dependencies = [ "serde", "winnow", "zvariant", ] -[[package]] -name = "zbus_utils" -version = "6.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af55e52da295311b368c45bcae221f020e28e97a2420360170ce389218e76ec7" -dependencies = [ - "proc-macro2", - "quote", - "serde", - "syn 3.0.6", - "winnow", -] - [[package]] name = "zbus_xml" version = "5.2.1" @@ -10359,16 +10455,6 @@ version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" -[[package]] -name = "zruntime" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce6a55d4f521760475a30f7b96779508fe8c40dc834478709454dd1d5f0ebc72" -dependencies = [ - "futures-core", - "futures-io", -] - [[package]] name = "zune-core" version = "0.5.3" @@ -10386,9 +10472,9 @@ dependencies = [ [[package]] name = "zvariant" -version = "5.15.0" +version = "5.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1d34c27cc6cdd1f458427519dd6b8612f7b7e3f7b9a0b2355d041dda9869147" +checksum = "80bf6d6cd6a4b601acb64cfb8284cdc643fcfa75ef26ae3c46ec8aa4e8a1fbb7" dependencies = [ "endi", "enumflags2", @@ -10401,9 +10487,9 @@ dependencies = [ [[package]] name = "zvariant_derive" -version = "5.15.0" +version = "5.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "864155e69b4352db0c7f374917bf45d1e0c8d17659c8b3dbf9795f3673f8c497" +checksum = "ef2672456ba1cb54b5eaec136e63135b3c7495fb938e53b28a62e633496d486d" dependencies = [ "proc-macro-crate", "proc-macro2", diff --git a/js/join/src/renew.test.ts b/js/join/src/renew.test.ts index 4dba3255..b28024b7 100644 --- a/js/join/src/renew.test.ts +++ b/js/join/src/renew.test.ts @@ -3,7 +3,11 @@ import * as Net from "@moq/net"; import { Signal } from "@moq/signals"; import { follow, path } from "./renew.ts"; -const encode = (text: string) => new TextEncoder().encode(text); +/** A token frame starting the group at `key`, since the token track has no timescale. */ +const frame = (text: string, key: number) => ({ + payload: new TextEncoder().encode(text), + timestamp: Net.Time.Timestamp.unscaled(key), +}); /** Resolve once `tokens` holds `count` entries. */ async function received(tokens: Signal, count: number): Promise { @@ -26,17 +30,17 @@ test("each token the studio publishes reaches the page, and only for its own nam ["alice", 1], ] as const) { const other = origin.route(path(name, generation), { hidden: true }); - other.broadcast.createTrack("token").writeFrame({ payload: encode(`${name} ${generation}`) }); + other.broadcast.createTrack("token").writeFrame(frame(`${name} ${generation}`, 0)); other.announce(); } const publication = origin.route(path("alice", 0), { hidden: true }); const track = publication.broadcast.createTrack("token"); - track.writeFrame({ payload: encode("first") }); + track.writeFrame(frame("first", 0)); publication.announce(); expect(await received(tokens, 1)).toEqual(["first"]); - track.writeFrame({ payload: encode("second") }); + track.writeFrame(frame("second", 1)); expect(await received(tokens, 2)).toEqual(["first", "second"]); renewal.close(); @@ -52,16 +56,16 @@ test("a group superseded before it completes does not end the renewals", async ( const publication = origin.route(path("alice", 0), { hidden: true }); const track = publication.broadcast.createTrack("token"); - track.writeFrame({ payload: encode("first") }); + track.writeFrame(frame("first", 0)); publication.announce(); expect(await received(tokens, 1)).toEqual(["first"]); // Opened but never written, as when the studio stops partway through one, // so the page is reading it when the next arrives. const partial = track.appendGroup(); for (let turn = 0; turn < 10; turn++) await new Promise((resolve) => setTimeout(resolve)); - track.writeFrame({ payload: encode("next") }); + track.writeFrame(frame("next", 2)); expect(await received(tokens, 2)).toEqual(["first", "next"]); - track.writeFrame({ payload: encode("last") }); + track.writeFrame(frame("last", 3)); expect(await received(tokens, 3)).toEqual(["first", "next", "last"]); partial.finish(); diff --git a/moq b/moq index 51611bd8..6c726f45 160000 --- a/moq +++ b/moq @@ -1 +1 @@ -Subproject commit 51611bd83858fe78964a444b00d52cf71ea3cb5d +Subproject commit 6c726f455802564e6434078f35d807d38bdfdd4f diff --git a/rs/moq-scene/Cargo.toml b/rs/moq-scene/Cargo.toml index 2bf96aaa..0ae11ba8 100644 --- a/rs/moq-scene/Cargo.toml +++ b/rs/moq-scene/Cargo.toml @@ -58,7 +58,7 @@ humantime-serde = { workspace = true } # PNG and JPEG still images. Already in a studio's graph through eframe. image = { version = "0.25", default-features = false, features = ["png", "jpeg"] } kio = { workspace = true, features = ["runtime"] } -moq-audio = { workspace = true, features = ["bus"] } +moq-audio = { workspace = true } moq-mux = { workspace = true } moq-net = { workspace = true } # `render` is the compositor's whole input path: it turns every decoded or diff --git a/rs/moq-scene/src/layout.rs b/rs/moq-scene/src/layout.rs index e97fae78..95a0ec7f 100644 --- a/rs/moq-scene/src/layout.rs +++ b/rs/moq-scene/src/layout.rs @@ -433,15 +433,15 @@ mod tests { let mut reader = moq_json::snapshot::Consumer::::new(track, Default::default()); let read = reader.next().await.unwrap().expect("a layout"); assert_eq!( - (read.value, read.at.map(Duration::from)), - (first, Some(Duration::from_secs(1))) + (read.value, Duration::try_from(read.at).unwrap()), + (first, Duration::from_secs(1)) ); publisher.update(second.clone(), stamp(2)).unwrap(); publisher.update(second.clone(), stamp(3)).unwrap(); let read = reader.next().await.unwrap().expect("a layout"); assert_eq!( - (read.value, read.at.map(Duration::from)), - (second, Some(Duration::from_secs(2))) + (read.value, Duration::try_from(read.at).unwrap()), + (second, Duration::from_secs(2)) ); publisher.finish().unwrap(); diff --git a/rs/moq-scene/src/publish.rs b/rs/moq-scene/src/publish.rs index efaddc10..f47ea230 100644 --- a/rs/moq-scene/src/publish.rs +++ b/rs/moq-scene/src/publish.rs @@ -381,14 +381,28 @@ async fn copy( } }); let result: Result<(), moq_net::Error> = async { + // Past the last group copied, from where a skip the source declared + // is carried over. + let mut below = 0; while let Some(mut group) = track.recv_group().await? { - let mut copy = out.create_group(moq_net::group::Info { - sequence: group.sequence, - })?; + // Creating a group says nothing of the starts below it, so the + // copy repeats the source's skips: one before its first group + // marks the encoder's first, and one between groups says the + // media runs on across them. + if below < group.start && track.is_skipped(below..group.start) { + out.skip_groups(below..group.start); + } + below = below.max(group.start + 1); + let mut copy = out.create_group(moq_net::group::Info { start: group.start })?; while let Some(frame) = group.read_frame().await? { copy.write_frame(frame.timestamp, frame.payload)?; } - copy.finish()?; + // The copy ends where the source ended its group, when it said, + // so a paused source's last picture ages out of the copy too. + match group.end() { + Some(end) => drop(copy.finish(end)?), + None => drop(copy.close()?), + } } out.clone().finish().map(drop) } @@ -765,7 +779,12 @@ impl<'a> Video<'a> { /// where the first frame encodes, for a source that encodes its pictures /// ahead of when they show. pub(crate) fn anchor(&mut self, origin: kio::clock::Instant) -> Result<(), Error> { - self.epoch = Some(self.clock.capture(origin)?.as_micros()); + self.epoch = Some( + self.clock + .capture(origin)? + .as_micros() + .map_err(moq_video::Error::from)?, + ); Ok(()) } @@ -775,10 +794,14 @@ impl<'a> Video<'a> { /// the gap, and reopens when the picture's size or color space changes, /// which moves the rendition to a new track. pub(crate) fn encode(&mut self, frame: &mut moq_video::Frame) -> Result<(), Error> { - let captured = frame.timestamp.as_micros(); - let epoch = *self - .epoch - .get_or_insert_with(|| self.clock.now().as_micros().saturating_sub(captured)); + let captured = frame.timestamp.as_micros().map_err(moq_video::Error::from)?; + let epoch = match self.epoch { + Some(epoch) => epoch, + None => { + let now = self.clock.now().as_micros().map_err(moq_video::Error::from)?; + *self.epoch.insert(now.saturating_sub(captured)) + } + }; frame.timestamp = moq_net::Timestamp::from_micros((epoch + captured) as u64).map_err(moq_video::Error::from)?; if !self.used() { @@ -960,7 +983,12 @@ impl<'a> Audio<'a> { // has passed: the rest is counted on from now. let now = kio::clock::now(); let ahead = captured.saturating_duration_since(now).as_micros(); - let timestamp = self.clock.capture(captured.min(now))?.as_micros() + ahead; + let timestamp = self + .clock + .capture(captured.min(now))? + .as_micros() + .map_err(moq_audio::Error::from)? + + ahead; let timestamp = moq_net::Timestamp::from_micros(timestamp as u64).map_err(moq_audio::Error::from)?; let data = bytes::Bytes::copy_from_slice(bytemuck::cast_slice(samples)); producer.write(&moq_audio::Frame::new(data, timestamp))?; @@ -1231,6 +1259,58 @@ mod tests { .await; } + /// A forwarded track says what its source said of the starts it skipped: + /// those before its first group, which mark the encoder's first, and those + /// between its groups, but not a hole the source left. Its groups end where + /// the source said they end. + #[tokio::test(start_paused = true)] + async fn a_forwarded_track_keeps_its_skips() { + let local = moq_net::broadcast::Info::new().produce(); + let source = local + .create_track("audio", hang::container::track_info(hang::catalog::PRIORITY.audio)) + .unwrap(); + let program = moq_net::broadcast::Info::new().produce(); + let copied = copy(local.consume(), program.clone(), "audio".into(), None); + let checked = async { + // The copy's track lasts as long as the copy, so it is read while + // the source is written. + let mut forwarded = loop { + match program.consume().track(&format!("{FORWARDED}audio")) { + Ok(track) => break track.subscribe(None).await.unwrap(), + Err(_) => tokio::task::yield_now().await, + } + }; + source.skip_groups(0..1_000); + let mut groups = Vec::new(); + for (start, appended) in [(1_000, true), (3_000, true), (5_000, false)] { + // Nothing says what lies between 3 000 and 5 000. + let group = match appended { + true => source.append_group(start), + false => source.create_group(moq_net::group::Info { start }), + }; + // The first group says where it ends; the others only close. + let group = group.unwrap(); + drop( + match start { + 1_000 => group.finish(2_000), + _ => group.close(), + } + .unwrap(), + ); + let mut copied = forwarded.recv_group().await.unwrap().unwrap(); + assert!(copied.read_frame().await.unwrap().is_none()); + groups.push((copied.start, copied.end())); + } + drop(source.clone().finish().unwrap()); + assert!(forwarded.recv_group().await.unwrap().is_none()); + assert_eq!(groups, [(1_000, Some(2_000)), (3_000, None), (5_000, None)]); + assert!(forwarded.is_skipped(0..1_000), "the encoder's first group"); + assert!(forwarded.is_skipped(1_001..3_000), "the media runs on"); + assert!(!forwarded.is_skipped(3_001..5_000), "a hole stays one"); + }; + tokio::join!(copied, checked); + } + /// Stopping the sources, as quitting does, ends each source's own track /// cleanly after what it published, and takes it out of the listing. #[tokio::test] @@ -1480,7 +1560,7 @@ mod tests { assert!(video.track.is_open(), "a subscriber gets pictures"); let mut group = track.recv_group().await.unwrap().expect("a group"); assert!(group.read_frame().await.unwrap().is_some()); - let first = group.sequence; + let first = group.start; drop(group); drop(track); @@ -1496,7 +1576,7 @@ mod tests { video.encode(&mut gray(size, 100)).unwrap(); assert!(video.track.is_open(), "a new subscriber reopens the encoder"); let mut group = track.recv_group().await.unwrap().expect("a group"); - assert!(group.sequence > first, "{} after {first}", group.sequence); + assert!(group.start > first, "{} after {first}", group.start); assert!(group.read_frame().await.unwrap().is_some()); } => {} } @@ -1624,7 +1704,7 @@ mod tests { let (_, config) = take.audio.as_ref().unwrap(); let read = packets(&take.consume(), name, config).await; let near = |at: moq_net::Timestamp, to: moq_net::Timestamp| { - at.as_micros().abs_diff(to.as_micros()) <= 20_000 + at.as_micros().unwrap().abs_diff(to.as_micros().unwrap()) <= 20_000 }; let first = read.first().copied().flatten().expect("a packet first"); assert!(near(first, heard), "{first:?} for {heard:?}"); @@ -1752,7 +1832,7 @@ mod tests { if current.replace(group) != Some(group) { seen.push(Seen::Group); } - seen.push(Seen::Picture(frame.timestamp.as_micros() / 1000)); + seen.push(Seen::Picture(frame.timestamp.as_micros().unwrap() / 1000)); } moq_mux::container::Read::End { .. } => seen.push(Seen::End), moq_mux::container::Read::Gap { .. } => panic!("pictures went missing"), @@ -1957,7 +2037,7 @@ mod tests { // Stamped with when it showed, though encoded ahead of it. let shown = publish.catalog.clock().capture(first).unwrap(); assert!( - stamps[0].as_micros().abs_diff(shown.as_micros()) <= 1, + stamps[0].as_micros().unwrap().abs_diff(shown.as_micros().unwrap()) <= 1, "the first picture as it showed: {:?}, not {shown:?}", stamps[0] ); @@ -2069,7 +2149,7 @@ mod tests { let silences = until(|| { while let std::task::Poll::Ready(read) = track.poll_read(&now) { if let moq_mux::container::Read::Frame { frame, .. } = read.unwrap().expect("open") { - stamps.push(std::time::Duration::from(frame.timestamp)); + stamps.push(std::time::Duration::try_from(frame.timestamp).unwrap()); } } let gaps = stamps.windows(2).map(|pair| pair[1].saturating_sub(pair[0])); @@ -2079,7 +2159,7 @@ mod tests { .await; assert_eq!(silences, 2, "each loop's silence is kept: {stamps:?}"); // Stamped with when it sounded, though written ahead of it. - let sounded = std::time::Duration::from(publish.catalog.clock().capture(begun).unwrap()); + let sounded = std::time::Duration::try_from(publish.catalog.clock().capture(begun).unwrap()).unwrap(); assert_eq!(stamps[0], sounded, "the first sound as it sounded"); }) .await; diff --git a/rs/moq-scene/src/replay.rs b/rs/moq-scene/src/replay.rs index 055ddb09..9c580044 100644 --- a/rs/moq-scene/src/replay.rs +++ b/rs/moq-scene/src/replay.rs @@ -113,9 +113,10 @@ impl Take { )); } let end = target.end; - let start = Duration::from(end).saturating_sub(length); - let start = moq_net::Timestamp::try_from(start) - .and_then(|start| start.convert(end.scale())) + let start = Duration::try_from(end) + .map(|end| end.saturating_sub(length)) + .and_then(moq_net::Timestamp::try_from) + .and_then(|start| start.convert(end.scale().ok_or(moq_net::TimeOverflow)?)) .map_err(|err| Error::Invalid(format!("replay start: {err}")))?; let mut broadcast = moq_net::broadcast::Info::new().produce(); @@ -215,9 +216,15 @@ fn copy( cut: Cut, container: &impl moq_mux::container::Container, ) -> Result, Error> { + // The anchors are group starts, in the timescale of hang's media tracks. + let key = |at: moq_net::Timestamp| { + at.convert(hang::container::TIMESCALE) + .map(moq_net::Timestamp::value) + .map_err(|err| Error::Invalid(format!("replay span: {err}"))) + }; let range = moq_net::track::Range { - start: moq_net::track::Anchor::Time(start), - end: Some(moq_net::track::Anchor::Time(target.end)), + start: moq_net::track::Anchor::Time(key(start)?), + end: Some(moq_net::track::Anchor::Time(key(target.end)?)), }; // With the publisher's whole window, so none of the span is skipped as // stale. @@ -232,6 +239,12 @@ fn copy( Poll::Ready(source) => source?, Poll::Pending => return Ok(None), }; + // The range was keyed before the track described itself. + if source.info().timescale != Some(hang::container::TIMESCALE) { + return Err(Error::Unsupported(format!( + "replaying {name}, which is not on hang's media timescale" + ))); + } // Never stale: the take keeps its groups for as long as it lives. let info = source.info().clone().with_max_age(None); let mut track = broadcast.create_track(name, info)?; @@ -263,6 +276,8 @@ fn marker(frame: &moq_net::frame::Frame, container: &impl moq_mux::container::Co struct Held { frames: Vec, read: Result<(), moq_net::Error>, + /// Where the source ended the group, for one held whole. + end: Option, } /// Copy every group and frame `source` has ready, up to `end` as `cut` says, @@ -298,7 +313,8 @@ fn copy_held( Poll::Ready(Err(err)) => break Err(err), } }; - let inside = |frame: &moq_net::frame::Frame| frame.timestamp.is_none_or(|timestamp| timestamp <= end); + // A frame without a time cannot be past the end. + let inside = |frame: &moq_net::frame::Frame| frame.timestamp.scale().is_none() || frame.timestamp <= end; let kept = match cut { Cut::Decodable => frames.iter().rposition(inside).map_or(0, |last| last + 1), Cut::Shown => frames.iter().position(|frame| !inside(frame)).unwrap_or(frames.len()), @@ -307,22 +323,27 @@ fn copy_held( // up or that aged out before any of it was read, which replays as the // gap it is. if kept > 0 { + // A group cut short ends where its pictures do, which only a reader + // can tell. + let whole = kept == frames.len(); frames.truncate(kept); - held.insert(group.sequence, Held { frames, read }); + let end = group.end().filter(|_| whole); + held.insert(group.start, Held { frames, read, end }); } } if let Some(&first) = held.keys().next() { track.start_at(first)?; } - for (&sequence, held) in &held { - let mut into = track.create_group(moq_net::group::Info { sequence })?; + for (&start, held) in &held { + let mut into = track.create_group(moq_net::group::Info { start })?; for frame in &held.frames { into.write_frame(frame.timestamp, frame.payload.clone())?; } - match &held.read { - Ok(()) => drop(into.finish()?), - Err(err) => into.abort(err.clone())?, + match (&held.read, held.end) { + (Ok(()), Some(end)) => drop(into.finish(end)?), + (Ok(()), None) => drop(into.close()?), + (Err(err), _) => into.abort(err.clone())?, } } @@ -334,37 +355,45 @@ fn copy_held( let mut media = false; let mut covered = Duration::ZERO; let mut last: Option<(u64, Duration)> = None; - for (&sequence, held) in &held { - // Groups follow on only when no sequence is missing between them. - last = last.filter(|(previous, _)| sequence <= previous + 1); + for (&start, held) in &held { + // Groups follow on only when the publisher said nothing lies between them. + last = last.filter(|(previous, _)| source.is_skipped(previous + 1..start)); for frame in &held.frames { if marker(frame, container) { last = None; continue; } media = true; - let Some(at) = frame.timestamp.filter(|at| span.contains(at)).map(Duration::from) else { + let Some(at) = Some(frame.timestamp) + .filter(|at| span.contains(at)) + .and_then(|at| Duration::try_from(at).ok()) + else { continue; }; // Decode order can show a picture before the one decoded ahead of // it, so only time past the newest shown counts. last = match last { - Some((_, newest)) if at <= newest => Some((sequence, newest)), + Some((_, newest)) if at <= newest => Some((start, newest)), Some((_, newest)) => { covered += at - newest; - Some((sequence, at)) + Some((start, at)) } - None => Some((sequence, at)), + None => Some((start, at)), }; } } - // Groups the span covers that are not here are gaps, not groups to come. + // Carry over only the starts the source declared skipped, before its + // first group too, which marks the encoder's first. A group lost on the way + // stays a hole, which the finished take lets readers pass as the gap it + // is: a skip would tell them the media runs on across it. let copied: Vec = held.into_keys().collect(); - for pair in copied.windows(2) { - if pair[1] > pair[0] + 1 { - track.skip_groups(pair[0] + 1..pair[1]); + let mut below = 0; + for &start in &copied { + if below < start && source.is_skipped(below..start) { + track.skip_groups(below..start); } + below = start + 1; } Ok(media.then_some(covered)) } @@ -379,15 +408,36 @@ mod tests { /// Write group `sequence` of `track` with a frame at each of `at`, in /// milliseconds and decode order, the frame at `at[i]` named - /// `{sequence}.{i}`. + /// `{sequence}.{i}`. The group starts at its first frame, in microseconds + /// as hang's tracks count, and says nothing of the groups around it. fn write(track: &moq_net::track::Producer, sequence: u64, at: &[u64]) { - let mut group = track.create_group(moq_net::group::Info { sequence }).unwrap(); + fill( + track + .create_group(moq_net::group::Info { start: at[0] * 1000 }) + .unwrap(), + sequence, + at, + ); + } + + /// [`write`] as a publisher appends, saying nothing lies between this group + /// and the one before. + fn append(track: &moq_net::track::Producer, sequence: u64, at: &[u64]) { + fill(track.append_group(at[0] * 1000).unwrap(), sequence, at); + } + + fn fill(mut group: moq_net::group::Producer, sequence: u64, at: &[u64]) { for (index, at) in at.iter().enumerate() { group .write_frame(ts(*at), format!("{sequence}.{index}").into_bytes()) .unwrap(); } - group.finish().unwrap(); + group.close().unwrap(); + } + + /// A group's second, from its start in microseconds. + fn second_of(start: u64) -> u64 { + start / 1_000_000 } /// The frames of group `sequence` of [`broadcast`], three a third of a @@ -397,21 +447,32 @@ mod tests { } /// A video track with a group a second, from `0` up to but excluding - /// `groups` seconds, but for the group the publisher gave up at `lost`. + /// `groups` seconds, but for the group lost on the way at `lost`. The + /// publisher appends each group, saying nothing lies between them, up to the + /// loss; past it, nothing says what lies before a group, as a copy that lost + /// one knows. fn broadcast(groups: u64, lost: Option) -> (moq_net::broadcast::Producer, moq_net::track::Producer) { let broadcast = moq_net::broadcast::Info::new().produce(); let track = broadcast .create_track("video", hang::container::track_info(hang::catalog::PRIORITY.video)) .unwrap(); for sequence in 0..groups { - match lost == Some(sequence) { - true => track.skip_groups(sequence..sequence + 1), - false => write(&track, sequence, &second(sequence)), + match lost { + Some(lost) if sequence == lost => {} + Some(lost) if sequence > lost => write(&track, sequence, &second(sequence)), + _ => append(&track, sequence, &second(sequence)), } } (broadcast, track) } + /// Whether the take's track declares every group start in `starts`, in + /// microseconds, skipped. + async fn skipped(take: &Take, starts: std::ops::Range) -> bool { + let track = take.consume().track("video").unwrap().subscribe(None).await.unwrap(); + track.is_skipped(starts) + } + fn target(broadcast: &moq_net::broadcast::Producer, end: moq_net::Timestamp) -> Target { let config = hang::catalog::VideoConfig::new(hang::catalog::H264 { inline: true, @@ -444,7 +505,7 @@ mod tests { while let Some(frame) = group.read_frame().await.unwrap() { frames.push(String::from_utf8(frame.payload.to_vec()).unwrap()); } - groups.push((group.sequence, frames)); + groups.push((second_of(group.start), frames)); } groups } @@ -477,7 +538,7 @@ mod tests { } /// A group missing from the span stays missing, rather than one its readers - /// wait for. + /// wait for, and a gap rather than a span the media runs on across. #[tokio::test(start_paused = true)] async fn a_take_keeps_a_gap() { let (broadcast, _track) = broadcast(10, Some(5)); @@ -486,6 +547,60 @@ mod tests { read(&take).await, [(4, frames(4, 3)), (6, frames(6, 3)), (7, frames(7, 2))] ); + assert!(!skipped(&take, 4_000_001..6_000_000).await, "the lost group is a gap"); + } + + /// A group held whole ends where its source ended it, and one cut at the + /// span's end only closes. + #[tokio::test(start_paused = true)] + async fn a_take_keeps_where_its_whole_groups_end() { + let (broadcast, track) = broadcast(0, None); + for sequence in 0..3 { + let mut group = track.append_group(sequence * 1_000_000).unwrap(); + for (index, at) in second(sequence).iter().enumerate() { + group + .write_frame(ts(*at), format!("{sequence}.{index}").into_bytes()) + .unwrap(); + } + drop(group.finish((sequence + 1) * 1_000_000).unwrap()); + } + let take = Take::new(&target(&broadcast, ts(2_500)), Duration::from_secs(2)).unwrap(); + let mut copied = take + .consume() + .track("video") + .unwrap() + .subscribe( + moq_net::track::Subscription::default().with_delay(moq_net::track::Delay::default().with_max(None)), + ) + .await + .unwrap(); + let mut ends = Vec::new(); + while let Some(mut group) = copied.recv_group().await.unwrap() { + while group.read_frame().await.unwrap().is_some() {} + ends.push((second_of(group.start), group.end())); + } + assert_eq!(ends, [(0, Some(1_000_000)), (1, Some(2_000_000)), (2, None)]); + } + + /// A take says what the source said of the starts it skipped: between its + /// groups, and before the source's first group, which marks the encoder's + /// first, but not before a take that starts later. + #[tokio::test(start_paused = true)] + async fn a_take_keeps_the_skips_it_was_given() { + let (broadcast, track) = broadcast(0, None); + track.skip_groups(0..1_000_000); + for sequence in 1..10 { + append(&track, sequence, &second(sequence)); + } + let from_first = Take::new(&target(&broadcast, ts(3_500)), Duration::from_secs(3)).unwrap(); + assert_eq!(read(&from_first).await[0].0, 1); + assert!(skipped(&from_first, 0..1_000_000).await, "the encoder's first group"); + assert!(skipped(&from_first, 1_000_001..2_000_000).await); + + let later = Take::new(&target(&broadcast, ts(7_500)), Duration::from_secs(3)).unwrap(); + assert_eq!(read(&later).await[0].0, 4); + assert!(!skipped(&later, 0..4_000_000).await, "past the encoder's first"); + assert!(skipped(&later, 4_000_001..5_000_000).await); } /// Groups that reached the studio out of order are held in their own order, @@ -541,7 +656,7 @@ mod tests { #[tokio::test(start_paused = true)] async fn a_take_at_the_live_edge_holds_what_was_written() { let (broadcast, track) = broadcast(7, None); - let mut live = track.create_group(moq_net::group::Info { sequence: 7 }).unwrap(); + let mut live = track.append_group(7_000_000).unwrap(); live.write_frame(ts(7_000), b"7.0".to_vec()).unwrap(); let take = Take::new(&target(&broadcast, ts(7_500)), Duration::from_secs(3)).unwrap(); live.write_frame(ts(7_333), b"7.1".to_vec()).unwrap(); @@ -593,7 +708,7 @@ mod tests { // The marker a publisher writes as it stops encoding. let config = target(&broadcast, ts(0)).video.unwrap().1; let container = moq_mux::catalog::hang::Container::try_from(&config).unwrap(); - let mut group = track.create_group(moq_net::group::Info { sequence: 3 }).unwrap(); + let mut group = track.append_group(3_500_000).unwrap(); let gap = moq_mux::container::Frame { timestamp: ts(3_500), payload: bytes::Bytes::new(), @@ -601,7 +716,7 @@ mod tests { duration: None, }; container.write(&mut group, &[gap]).unwrap(); - group.finish().unwrap(); + group.close().unwrap(); let result = Take::new(&target(&broadcast, ts(3_900)), Duration::from_millis(300)); assert!(matches!(result, Err(Error::Unsupported(_))), "{:?}", result.err()); } @@ -620,7 +735,7 @@ mod tests { let at: Vec = (0..per_second) .map(|index| second * 1000 + index * 1000 / per_second) .collect(); - write(&track, second, &at); + append(&track, second, &at); } let take = Take::new(&target(&broadcast, ts(9_999)), Duration::from_secs(10)).unwrap(); (take.covered, broadcast, track) diff --git a/rs/moq-scene/src/source.rs b/rs/moq-scene/src/source.rs index 1aaa3e30..65c8fbbd 100644 --- a/rs/moq-scene/src/source.rs +++ b/rs/moq-scene/src/source.rs @@ -955,11 +955,11 @@ impl Driver { #[cfg(feature = "capture")] async fn device(&self, source: &Source, listing: Option<&Listing<'_>>) -> Result<(), Error> { use moq_audio::capture::Source as Audio; - use moq_video::capture::Source as Video; + use moq_video::capture::{DisplaySelection, Source as Video}; let video = match source { Source::Camera { id } => Video::Camera(id.clone()), - Source::Display { id } => Video::Display(id.clone()), + Source::Display { id } => Video::Display(id.clone().map(DisplaySelection::new).unwrap_or_default()), Source::Window { id } => Video::Window(id.clone()), Source::PickedDisplay | Source::PickedWindow => return self.picked(source, listing).await, Source::Microphone { id } => return self.listen(Audio::Microphone(id.clone()), listing).await, @@ -1785,7 +1785,7 @@ impl Driver { let stride = bus.layout.channels() as usize * size_of::(); while let Some(frame) = next_samples(consumer).await? { let length = (frame.data.len() / stride) as f64 / f64::from(bus.sample_rate); - if Duration::from(frame.timestamp) + Duration::from_secs_f64(length) > Duration::from(take.start) { + if elapsed(frame.timestamp)? + Duration::from_secs_f64(length) > elapsed(take.start)? { samples = Some(frame); break; } @@ -1802,12 +1802,13 @@ impl Driver { self.ran(play.play, |run| { run.begun.get_or_insert(begin); }); - let at = |timestamp: moq_net::Timestamp| { - let (timestamp, start) = (Duration::from(timestamp), Duration::from(take.start)); - match timestamp.checked_sub(start) { + let start = elapsed(take.start)?; + let at = |timestamp: moq_net::Timestamp| -> Result { + let timestamp = elapsed(timestamp)?; + Ok(match timestamp.checked_sub(start) { Some(into) => begin.saturating_add(into.div_f32(play.speed)), None => begin.saturating_sub((start - timestamp).div_f32(play.speed)), - } + }) }; let pictures = async { @@ -1820,7 +1821,7 @@ impl Driver { // One at the end would show for no time at all. moq_video::decode::Read::Frame(frame) if frame.timestamp >= take.end => break, moq_video::decode::Read::Frame(mut frame) => { - kio::clock::sleep_until(at(frame.timestamp)).await; + kio::clock::sleep_until(at(frame.timestamp)?).await; self.encode(&mut own, &mut frame, config, kio::clock::now()).await?; self.show_play(play.play, Picture::Frame(frame)); } @@ -1829,7 +1830,14 @@ impl Driver { // the end, the replay is over anyway. moq_video::decode::Read::Gap { timestamp, .. } | moq_video::decode::Read::End { end: timestamp } => { - kio::clock::sleep_until(at(timestamp.clamp(take.start, take.end))).await; + let timestamp = if timestamp < take.start { + take.start + } else if timestamp > take.end { + take.end + } else { + timestamp + }; + kio::clock::sleep_until(at(timestamp)?).await; self.picture.lock().unwrap().clear(); if let Some(video) = &mut own.track && let Err(err) = video.discontinuity() @@ -1851,7 +1859,7 @@ impl Driver { while let Some(frame) = samples.take() && frame.timestamp < take.end { - let due = at(frame.timestamp); + let due = at(frame.timestamp)?; ahead(due).await; let before = begin.saturating_duration_since(due).as_nanos() * u128::from(bus.sample_rate); let cut = (before / 1_000_000_000) as usize * stride; @@ -1870,7 +1878,7 @@ impl Driver { tokio::try_join!(pictures, sounds)?; std::future::pending().await }; - let end = at(take.end); + let end = at(take.end)?; // Played first, so a picture due at the very end shows before it does. tokio::select! { biased; @@ -2115,8 +2123,8 @@ impl Driver { None => None, }; let first = [ - picture.as_ref().map(|frame| Duration::from(frame.timestamp)), - samples.as_ref().map(|frame| Duration::from(frame.timestamp)), + picture.as_ref().map(|frame| elapsed(frame.timestamp)).transpose()?, + samples.as_ref().map(|frame| elapsed(frame.timestamp)).transpose()?, ]; let Some(start) = first.into_iter().flatten().min() else { // Its tracks hold no frame that decodes, which a loop would only @@ -2139,7 +2147,9 @@ impl Driver { self.ran(play, |run| { run.begun.get_or_insert(begin); }); - let at = |timestamp: moq_net::Timestamp| begin.saturating_add(Duration::from(timestamp).saturating_sub(start)); + let at = |timestamp: moq_net::Timestamp| -> Result { + Ok(begin.saturating_add(elapsed(timestamp)?.saturating_sub(start))) + }; let end = std::cell::Cell::new(begin); let last = |until: Instant| end.set(end.get().max(until)); @@ -2149,7 +2159,7 @@ impl Driver { }; let mut previous = None; while let Some(mut frame) = picture.take() { - let due = at(frame.timestamp); + let due = at(frame.timestamp)?; ahead(due).await; last(due.saturating_add(previous.map_or(Duration::ZERO, |previous| due - previous))); previous = Some(due); @@ -2172,7 +2182,7 @@ impl Driver { // after a loop's silence while its pictures play on, is a gap. let mut first = true; while let Some(frame) = samples.take() { - let due = at(frame.timestamp); + let due = at(frame.timestamp)?; ahead(due).await; let length = (frame.data.len() / bytes) as u64 * 1_000_000_000 / u64::from(bus.sample_rate); last(due.saturating_add(Duration::from_nanos(length))); @@ -2345,6 +2355,11 @@ async fn next_picture(consumer: &mut moq_video::decode::Consumer) -> Result Result { + Ok(Duration::try_from(timestamp).map_err(moq_video::Error::from)?) +} + /// The next sound `consumer` decodes, passing over gaps, or `None` at the end /// of its track. async fn next_samples(consumer: &mut moq_audio::decode::Consumer) -> Result, Error> { @@ -3245,7 +3260,7 @@ mod tests { /// The picture a feed shows next, once it shows one. async fn shown(feed: &Feed) -> Duration { until(|| match feed.next_picture() { - Some(Picture::Frame(frame)) => Some(frame.timestamp.into()), + Some(Picture::Frame(frame)) => Some(Duration::try_from(frame.timestamp).unwrap()), Some(_) => panic!("a broadcast shows only frames"), None => None, }) @@ -3274,7 +3289,7 @@ mod tests { assert_eq!(due, arrived.saturating_add(VIDEO_DELAY)); assert!(feed.next_picture().is_none(), "shown on arrival"); until_time(due).await; - assert_eq!(shown(feed).await, Duration::from(stamp)); + assert_eq!(shown(feed).await, Duration::try_from(stamp).unwrap()); assert_eq!(feed.delay(), Some(VIDEO_DELAY)); }) .await; @@ -3336,7 +3351,7 @@ mod tests { until_time(due.saturating_sub(Duration::from_millis(1))).await; assert!(layer.next_picture().is_none(), "shown before its moment"); until_time(due).await; - assert_eq!(shown(layer).await, Duration::from(stamp)); + assert_eq!(shown(layer).await, Duration::try_from(stamp).unwrap()); assert_eq!(layer.delay(), Some(VIDEO_DELAY), "the program's"); }) .await; @@ -3359,7 +3374,9 @@ mod tests { let pixel = image::RgbaImage::from_raw(1, 1, vec![255, 0, 0, 255]).unwrap(); let mut png = std::io::Cursor::new(Vec::new()); pixel.write_to(&mut png, image::ImageFormat::Png).unwrap(); - track.write_frame(None, bytes::Bytes::from(png.into_inner())).unwrap(); + track + .write_frame(moq_net::Timestamp::unscaled(0), bytes::Bytes::from(png.into_inner())) + .unwrap(); let mut still = hang::catalog::BinaryConfig::new(hang::catalog::Mode::Snapshot); still.mime = Some("image/png".into()); let mut logo = hang::Catalog::default(); @@ -3446,7 +3463,7 @@ mod tests { let first = moq_net::Timestamp::from_millis(1_000).unwrap(); let due = until(|| clock.due(first)).await; until_time(due).await; - assert_eq!(shown(feed).await, Duration::from(first)); + assert_eq!(shown(feed).await, Duration::try_from(first).unwrap()); // Its stamps sit half a second before the first track's clock reading. let timebase = live.catalog.timebase(); @@ -3481,7 +3498,7 @@ mod tests { assert!(feed.next_picture().is_none(), "shown before its delay"); until_time(arrived.saturating_add(VIDEO_DELAY)).await; assert!( - shown(feed).await < Duration::from(first), + shown(feed).await < Duration::try_from(first).unwrap(), "stamped before the first track" ); }) @@ -3519,7 +3536,7 @@ mod tests { until_time(due.saturating_sub(Duration::from_millis(1))).await; assert!(feed.next_picture().is_none(), "shown before its sound"); until_time(due).await; - assert_eq!(shown(feed).await, Duration::from(stamp(1_000))); + assert_eq!(shown(feed).await, Duration::try_from(stamp(1_000)).unwrap()); assert_eq!(feed.delay(), Some(delay)); // The sound stalls; each picture still shows on its moment. @@ -3533,7 +3550,7 @@ mod tests { until_time(due).await; assert_eq!( shown(feed).await, - Duration::from(stamp(at)), + Duration::try_from(stamp(at)).unwrap(), "held by the stalled sound" ); } @@ -3547,7 +3564,7 @@ mod tests { picture(&mut live.video, &mut live.encoder, at); let due = until(|| clock.due(stamp(at))).await; until_time(due).await; - assert_eq!(shown(feed).await, Duration::from(stamp(at))); + assert_eq!(shown(feed).await, Duration::try_from(stamp(at)).unwrap()); }) .await; } @@ -3818,7 +3835,9 @@ mod tests { let start = moq_net::Timestamp::from_micros(1_500_000).unwrap(); for index in 45..75u64 { let timestamp = moq_net::Timestamp::from_micros(index * 1_000_000 / 30).unwrap(); - let due = begin.saturating_add((Duration::from(timestamp) - Duration::from(start)).div_f32(speed)); + let due = begin.saturating_add( + (Duration::try_from(timestamp).unwrap() - Duration::try_from(start).unwrap()).div_f32(speed), + ); assert!(feed.next_picture().is_none(), "a picture before {timestamp:?} is due"); until_time(due).await; let shown = until(|| match feed.next_picture() { @@ -3993,7 +4012,10 @@ mod media_tests { assert!(feed.next_picture().is_none(), "picture {index} before its moment"); until_time(begin.saturating_add(into)).await; let (timestamp, _) = shown(feed).await; - assert_eq!(Duration::from(timestamp) - Duration::from(first), into); + assert_eq!( + Duration::try_from(timestamp).unwrap() - Duration::try_from(first).unwrap(), + into + ); } // The last picture lasts as long as the one before it, and the @@ -4176,7 +4198,10 @@ mod media_tests { let (first, begin) = shown(feed).await; until_time(begin.saturating_add(Duration::from_millis(950))).await; let (last, _) = shown(feed).await; - assert_eq!(Duration::from(last) - Duration::from(first), Duration::from_millis(900)); + assert_eq!( + Duration::try_from(last).unwrap() - Duration::try_from(first).unwrap(), + Duration::from_millis(900) + ); until_time(begin.saturating_add(Duration::from_millis(998))).await; assert!(feed.next_picture().is_none(), "the start again before the end"); until_time(begin.saturating_add(Duration::from_millis(1_100))).await; diff --git a/rs/moq-studio/Cargo.toml b/rs/moq-studio/Cargo.toml index f11020a5..f2253423 100644 --- a/rs/moq-studio/Cargo.toml +++ b/rs/moq-studio/Cargo.toml @@ -15,9 +15,7 @@ keywords = ["quic", "streaming", "studio", "live", "media"] categories = ["multimedia", "multimedia::video", "gui"] [features] -default = ["quic", "websocket", "nvidia"] -quic = ["moq-tokio/quic"] -websocket = ["moq-tokio/websocket"] +default = ["nvidia"] # Camera, display, window, microphone, and system-audio sources, and playing a # mix to the speakers. Off by default like moq-cli's `capture`: on Linux the # microphone and the speakers link libasound at build time. Enable with @@ -58,7 +56,7 @@ hang = { workspace = true } # through moq-scene's humantime-serde. humantime = { workspace = true } kio = { workspace = true, features = ["async", "runtime"] } -moq-audio = { workspace = true, features = ["bus"] } +moq-audio = { workspace = true } # Signs guest tokens with the room key. moq-auth = { workspace = true } moq-json = { workspace = true, features = ["async"] } @@ -66,7 +64,7 @@ moq-mux = { workspace = true } moq-net = { workspace = true } moq-scene = { workspace = true } moq-stats = { workspace = true, features = ["async"] } -moq-tokio = { workspace = true, default-features = false, features = ["net"] } +moq-tokio = { workspace = true } moq-video = { workspace = true } # A recording's length, from its movie header; the version moq-mux writes it # with. diff --git a/rs/moq-studio/src/engine.rs b/rs/moq-studio/src/engine.rs index 3ab8d18b..7e9bcb30 100644 --- a/rs/moq-studio/src/engine.rs +++ b/rs/moq-studio/src/engine.rs @@ -1249,7 +1249,7 @@ impl Engine { // Where the program clock starts on the catalog's, so the wall time the // catalog advertises for a frame is when the program drew it. - let epoch = Duration::from(clock.capture(start)?); + let epoch = Duration::try_from(clock.capture(start)?)?; extras.epoch = epoch; if let Some(room) = &engine.room && let Some(origin) = room.origin() @@ -3556,7 +3556,7 @@ mod tests { state.cut().unwrap(); } let cut = next(&mut layouts).await.expect("a layout"); - let at = Duration::from(cut.at.expect("stamped")); + let at = Duration::try_from(cut.at).unwrap(); assert_eq!(cut.value.start, at, "on from the frame the cut drew"); assert!(cut.value.start > first.value.start); @@ -4495,7 +4495,9 @@ mod tests { tokio::spawn(async move { while let Some(read) = pictures.read().await.expect("decode") { if let moq_video::decode::Read::Frame(frame) = read - && seen.send((Duration::from(frame.timestamp), center(&frame))).is_err() + && seen + .send((Duration::try_from(frame.timestamp).unwrap(), center(&frame))) + .is_err() { return; } @@ -4523,7 +4525,7 @@ mod tests { .chunks_exact(channels) .map(|sample| sample.iter().copied().fold(0.0, f32::max)); if heard - .send((Duration::from(frame.timestamp), loudest.collect())) + .send((Duration::try_from(frame.timestamp).unwrap(), loudest.collect())) .is_err() { return; @@ -4590,7 +4592,7 @@ mod tests { /// picture and sample it publishes. fn stamp(engine: &Engine, at: Instant) -> Duration { let now = kio::clock::now(); - let reading = Duration::from(engine.program.clock.capture(now).unwrap()); + let reading = Duration::try_from(engine.program.clock.capture(now).unwrap()).unwrap(); reading + at.saturating_duration_since(now) - now.saturating_duration_since(at) } @@ -5025,7 +5027,7 @@ mod tests { .await .expect("a second resized picture arrives"); // At least 1/15 s apart, as the old 30 fps clock would not be. - let gap = Duration::from(next) - Duration::from(first); + let gap = Duration::try_from(next).unwrap() - Duration::try_from(first).unwrap(); assert!( gap > Duration::from_millis(50), "the program clock follows the frame rate: {gap:?}" @@ -5345,9 +5347,11 @@ mod tests { for (name, track) in ["video", "audio"].into_iter().zip(&tracks) { let mut subscription = broadcast.track(name).unwrap().subscribe(None).await.unwrap(); // A head group read through the relay, so only the tail is at stake. - let mut group = track.append_group().unwrap(); - group.write_frame(moq_net::Timestamp::ZERO, vec![0; 1]).unwrap(); - group.finish().unwrap(); + let mut group = track.append_group(0).unwrap(); + group + .write_frame(moq_net::Timestamp::from_millis(0).unwrap(), vec![0; 1]) + .unwrap(); + group.close().unwrap(); let mut head = kio::clock::timeout(TIMEOUT, subscription.recv_group()) .await .expect("the head group arrives") @@ -5379,11 +5383,13 @@ mod tests { }) .collect(); for track in &tracks { - let mut group = track.append_group().unwrap(); + let mut group = track.append_group(1).unwrap(); for _ in 0..FRAMES { - group.write_frame(moq_net::Timestamp::ZERO, vec![0; PADDING]).unwrap(); + group + .write_frame(moq_net::Timestamp::from_millis(1).unwrap(), vec![0; PADDING]) + .unwrap(); } - group.finish().unwrap(); + group.close().unwrap(); // The kept producer holds the finished track listed. drop(track.clone().finish().unwrap()); } diff --git a/rs/moq-studio/src/guests.rs b/rs/moq-studio/src/guests.rs index eafbaad5..ef793d3f 100644 --- a/rs/moq-studio/src/guests.rs +++ b/rs/moq-studio/src/guests.rs @@ -808,6 +808,8 @@ struct Renewed { generation: u64, publication: moq_net::origin::Route, track: moq_net::track::Producer, + /// The key of the next token's group, since the token track has no timescale. + next: u64, } impl Drop for Renewed { @@ -856,6 +858,7 @@ impl Renewals { generation, publication, track, + next: 0, }; self.write(identity, &mut renewed)?; renewed.publication.announce()?; @@ -875,7 +878,10 @@ impl Renewals { fn write(&self, identity: &str, renewed: &mut Renewed) -> anyhow::Result<()> { let (token, _) = self.room.sign(identity, renewed.generation)?; - renewed.track.write_frame(None, token)?; + renewed + .track + .write_frame(moq_net::Timestamp::unscaled(renewed.next), token)?; + renewed.next += 1; Ok(()) } } diff --git a/rs/moq-studio/src/health.rs b/rs/moq-studio/src/health.rs index 8227dd4f..d694dfc1 100644 --- a/rs/moq-studio/src/health.rs +++ b/rs/moq-studio/src/health.rs @@ -1993,7 +1993,9 @@ mod tests { rendition.frames = 50; rendition.arrival = Some(hang::echo::Arrival::new(timestamp, wall)); report.renditions.insert(id.to_string(), rendition); - reports.update(&report).unwrap(); + reports + .update(moq_json::Timed::new(&report, moq_net::Timestamp::unscaled(0))) + .unwrap(); (viewer, reports) } @@ -2086,7 +2088,12 @@ mod tests { let route = origin.route(&path, moq_net::origin::RouteOptions::hidden()).unwrap(); let track = route.broadcast().create_track(hang::echo::TRACK, None).unwrap(); let mut reports = moq_json::snapshot::Producer::::new(track, Default::default()); - reports.update(&hang::echo::Snapshot::default()).unwrap(); + reports + .update(moq_json::Timed::new( + &hang::echo::Snapshot::default(), + moq_net::Timestamp::unscaled(0), + )) + .unwrap(); route.announce().unwrap(); (route, reports) }; @@ -2195,7 +2202,7 @@ mod tests { .unwrap() .expect("an instance"); let mut video = broadcast.track("video").unwrap().subscribe(None).await.unwrap(); - track.append_group().unwrap().finish().unwrap(); + track.append_group(0).unwrap().close().unwrap(); video.recv_group().await.unwrap(); while watching.get() != Some(1) { kio::clock::sleep(INTERVAL / 4).await; diff --git a/rs/moq-studio/src/ladder.rs b/rs/moq-studio/src/ladder.rs index 8e8acfbe..0e33279e 100644 --- a/rs/moq-studio/src/ladder.rs +++ b/rs/moq-studio/src/ladder.rs @@ -274,8 +274,10 @@ mod tests { let track = broadcast.create_track("video", info).unwrap(); let mut joins = Joins::new(track.clone()); let write = |sequence: u64, bytes: usize| { - let mut group = track.create_group(moq_net::group::Info { sequence }).unwrap(); - group.write_frame(None, vec![0u8; bytes]).unwrap(); + let mut group = track.create_group(moq_net::group::Info { start: sequence }).unwrap(); + group + .write_frame(moq_net::Timestamp::unscaled(sequence), vec![0u8; bytes]) + .unwrap(); group }; assert!(!joins.due(), "nobody watches"); @@ -286,11 +288,13 @@ mod tests { let mut group = write(0, 100); assert!(!joins.due(), "the group landed"); // Deltas outweighing twice the keyframe make the next join ask. - group.write_frame(None, vec![0u8; 300]).unwrap(); + group + .write_frame(moq_net::Timestamp::unscaled(0), vec![0u8; 300]) + .unwrap(); let _second = consumer.track("video").unwrap().subscribe(None).await.unwrap(); assert!(joins.due(), "a join past the newest group"); assert!(!joins.due(), "the keyframe is still in the encoder"); - group.finish().unwrap(); + group.close().unwrap(); let _group = write(1, 100); assert!(!joins.due(), "group 1 answered it"); } diff --git a/rs/moq-studio/src/past.rs b/rs/moq-studio/src/past.rs index 5753a688..930dffb1 100644 --- a/rs/moq-studio/src/past.rs +++ b/rs/moq-studio/src/past.rs @@ -188,7 +188,7 @@ struct Decoding { /// By the keyframe's timestamp, in microseconds on its broadcast's timeline. pictures: BTreeMap>, /// The same for the sound: what it is of, its decoder, and each group's - /// peaks by the group's sequence. + /// peaks by the group's start. sound_key: Option<(Of, String, hang::catalog::AudioConfig)>, sound_decoder: Option, peaks: BTreeMap>, @@ -209,9 +209,18 @@ impl Decoding { return view; } }; + let end = match Duration::try_from(take.end) { + Ok(end) => end, + Err(err) => { + self.reset(); + view.held = Some(Err(format!("the take's end: {err}"))); + return view; + } + }; // The moment a timestamp of the take's broadcast was shown. - let end = Duration::from(take.end); - let at = |timestamp: moq_net::Timestamp| now.saturating_sub(end.saturating_sub(Duration::from(timestamp))); + let at = |timestamp: moq_net::Timestamp| -> Result { + Ok(now.saturating_sub(end.saturating_sub(Duration::try_from(timestamp)?))) + }; let broadcast = take.consume(); let mut oldest = None; if let Some((name, config)) = &take.audio { @@ -251,7 +260,7 @@ impl Decoding { of: &Of, name: &str, config: &hang::catalog::VideoConfig, - at: &impl Fn(moq_net::Timestamp) -> Instant, + at: &impl Fn(moq_net::Timestamp) -> Result, ) -> anyhow::Result>> { let key = Some((of.clone(), name.to_string(), config.clone())); if self.key != key { @@ -261,7 +270,7 @@ impl Decoding { let container = moq_mux::catalog::hang::Container::try_from(config)?; let mut kept = BTreeMap::new(); for frame in firsts(broadcast, name, container)? { - let micros = frame.timestamp.as_micros(); + let micros = frame.timestamp.as_micros()?; let picture = match self.pictures.remove(µs) { Some(picture) => picture, None => { @@ -271,7 +280,7 @@ impl Decoding { let size = rgba.size(); Arc::new(Picture { id: LAST_ID.fetch_add(1, Ordering::Relaxed) + 1, - at: at(frame.timestamp), + at: at(frame.timestamp)?, size: [size.width as usize, size.height as usize], rgba: rgba.into_data(), }) @@ -325,7 +334,7 @@ impl Decoding { of: &Of, name: &str, config: &hang::catalog::AudioConfig, - at: &impl Fn(moq_net::Timestamp) -> Instant, + at: &impl Fn(moq_net::Timestamp) -> Result, ) -> anyhow::Result> { let key = Some((of.clone(), name.to_string(), config.clone())); if self.sound_key != key { @@ -335,11 +344,11 @@ impl Decoding { } let container = moq_mux::catalog::hang::Container::try_from(config)?; let groups = groups(broadcast, name, false)?; - let newest = groups.last().map(|(sequence, _)| *sequence); + let newest = groups.last().map(|(start, _)| *start); let mut kept = BTreeMap::new(); - for (sequence, frames) in groups { - let peaks = match self.peaks.remove(&sequence) { - Some(peaks) if Some(sequence) != newest => peaks, + for (start, frames) in groups { + let peaks = match self.peaks.remove(&start) { + Some(peaks) if Some(start) != newest => peaks, _ => { let decoder = match &mut self.sound_decoder { Some(decoder) => decoder, @@ -362,15 +371,15 @@ impl Decoding { peaks } }; - kept.insert(sequence, peaks); + kept.insert(start, peaks); } self.peaks = kept; Ok(self .peaks .values() .flatten() - .map(|(timestamp, peak)| (at(*timestamp), *peak)) - .collect()) + .map(|(timestamp, peak)| Ok((at(*timestamp)?, *peak))) + .collect::>()?) } fn reset(&mut self) { @@ -405,7 +414,7 @@ fn firsts( Ok(firsts) } -/// Each group of the track `name` by its sequence, oldest first, with its +/// Each group of the track `name` by its start, oldest first, with its /// frames, or only its first if `first`, as far as the take holds them now. fn groups( broadcast: &moq_net::broadcast::Consumer, @@ -432,7 +441,67 @@ fn groups( break; } } - groups.insert(group.sequence, frames); + groups.insert(group.start, frames); } Ok(groups.into_iter().collect()) } + +// The software encoder is the one every test machine has. +#[cfg(all(test, feature = "openh264"))] +mod tests { + use super::*; + + /// The strip holds the keyframe each group of a take opens with, at the + /// moment it was shown, and decodes each only once across samples. + #[tokio::test] + async fn the_strip_shows_each_groups_keyframe_once() { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast, Default::default()).unwrap(); + let mut config = moq_video::encode::Config::new(64, 64, moq_video::FrameRate::new(30, 1).unwrap()); + config.kind = moq_video::encode::Kind::Software; + let rendition = config.probe().await.unwrap(); + let reserved = catalog.reserve().with_interval(config.effective_join()); + let track = broadcast + .unique_track(".avc3", reserved.track_info(hang::catalog::PRIORITY.video)) + .unwrap(); + let mut producer = moq_video::encode::Producer::new(track, reserved, rendition).unwrap(); + let mut encoder = moq_video::encode::Encoder::new(&config).unwrap(); + let gray = vec![0x80; 64 * 64 * 4]; + let at = |index: u64| moq_net::Timestamp::from_micros(index * 1_000_000 / 30).unwrap(); + for index in 0..90u64 { + if index % 30 == 0 { + encoder.cut().unwrap(); + } + let surface = moq_video::Surface::rgba(&gray, moq_video::Size::new(64, 64)).unwrap(); + encoder.encode(&moq_video::Frame::new(surface, at(index))).unwrap(); + let unit = encoder.receive().unwrap().expect("an access unit per picture"); + producer.publish(&unit).unwrap(); + } + let (name, video) = catalog.snapshot().video.renditions.into_iter().next().unwrap(); + let target = moq_scene::replay::Target { + broadcast: broadcast.consume(), + video: Some((name.clone(), video.clone())), + audio: None, + end: at(89), + }; + let take = moq_scene::replay::Take::new(&target, Duration::from_secs(3)).unwrap(); + + let zero = Instant::now(); + let shown = |timestamp: moq_net::Timestamp| -> Result { + Ok(zero.saturating_add(Duration::try_from(timestamp)?)) + }; + let mut decoding = Decoding::default(); + let pictures = decoding + .pictures(&take.consume(), &Of::Program, &name, &video, &shown) + .unwrap(); + let secs = |seconds| zero.saturating_add(Duration::from_secs(seconds)); + let ats: Vec<_> = pictures.iter().map(|picture| picture.at).collect(); + assert_eq!(ats, [secs(0), secs(1), secs(2)]); + + let again = decoding + .pictures(&take.consume(), &Of::Program, &name, &video, &shown) + .unwrap(); + let ids = |pictures: &[Arc]| pictures.iter().map(|picture| picture.id).collect::>(); + assert_eq!(ids(&again), ids(&pictures), "decoded again"); + } +} diff --git a/rs/moq-studio/src/replay.rs b/rs/moq-studio/src/replay.rs index 7aa296a5..50b8388a 100644 --- a/rs/moq-studio/src/replay.rs +++ b/rs/moq-studio/src/replay.rs @@ -168,10 +168,11 @@ pub fn take(engine: &Engine, of: &Of, ago: Duration, length: Duration, play: boo /// The moment `ago` before `end`, on `end`'s timeline and in its scale. fn before(end: moq_net::Timestamp, ago: Duration) -> anyhow::Result { - let at = Duration::from(end) + let at = Duration::try_from(end)? .checked_sub(ago) .context("the broadcast is younger than the picked stretch")?; - Ok(moq_net::Timestamp::try_from(at)?.convert(end.scale())?) + let scale = end.scale().context("the broadcast's timeline has no timescale")?; + Ok(moq_net::Timestamp::try_from(at)?.convert(scale)?) } /// Play `take` at `speed` in the replay source `into`. diff --git a/rs/moq-studio/src/uplink.rs b/rs/moq-studio/src/uplink.rs index 8e0b186c..80145cb8 100644 --- a/rs/moq-studio/src/uplink.rs +++ b/rs/moq-studio/src/uplink.rs @@ -127,15 +127,17 @@ async fn fill( clock.tick().await; if frames % RATE == 0 { if let Some(done) = group.take() { - moq_net::group::Producer::finish(done)?; + moq_net::group::Producer::close(done)?; } - group = Some(track.append_group()?); + // The probe's track has no timescale, so its groups count up. + group = Some(track.append_group(u64::from(frames / RATE))?); } frames += 1; let rate = reservation.rate().unwrap_or(FLOOR).clamp(FLOOR, ceiling); let size = (rate.as_bps() / 8 / u64::from(RATE)) as usize; let group = group.as_mut().expect("opened above"); - group.write_frame(None, filler.slice(..size.clamp(1, filler.len())))?; + let key = moq_net::Timestamp::unscaled(group.start); + group.write_frame(key, filler.slice(..size.clamp(1, filler.len())))?; } } diff --git a/rs/moq-studio/src/watch.rs b/rs/moq-studio/src/watch.rs index 01488664..7a11d853 100644 --- a/rs/moq-studio/src/watch.rs +++ b/rs/moq-studio/src/watch.rs @@ -182,7 +182,12 @@ impl Read { None => self.pending.clear(), Some(at) => { let passed = self.pending.iter().take_while(|(_, stamp)| { - stamp.is_none_or(|stamp| Duration::from(stamp) + MAX_DELAY <= Duration::from(at)) + stamp.is_none_or(|stamp| { + matches!( + (Duration::try_from(stamp), Duration::try_from(at)), + (Ok(stamp), Ok(at)) if stamp + MAX_DELAY <= at + ) + }) }); self.pending.drain(..passed.count().saturating_sub(1)); } @@ -273,7 +278,7 @@ async fn instance(broadcast: moq_net::broadcast::Consumer, read: watch::Sender match next { - Ok(Some(timed)) => read.send_modify(|read| read.push(Some(timed.value), timed.at)), + Ok(Some(timed)) => read.send_modify(|read| read.push(Some(timed.value), Some(timed.at))), Ok(None) => { layouts = None; alone(); @@ -648,7 +653,9 @@ mod tests { let mut reads = read.subscribe(); let read = reads.wait_for(|read| read.pending.len() >= count).await.unwrap(); let pending = read.pending.iter().cloned(); - pending.map(|(layout, at)| (layout, at.map(Duration::from))).collect() + pending + .map(|(layout, at)| (layout, at.map(|at| Duration::try_from(at).unwrap()))) + .collect() } /// Wait until the newest thing `read` holds is the program alone.