From 0dcea4f259ddabed61fbeb1a41bc5e4b36980920 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Wed, 5 Aug 2026 21:20:51 -0700 Subject: [PATCH 01/16] feat(cli): add moq play Co-authored-by: Codex --- Cargo.lock | 876 ++++++++++++++++++++++++++++++++---- doc/bin/cli.md | 45 +- rs/moq-cli/Cargo.toml | 6 + rs/moq-cli/README.md | 8 +- rs/moq-cli/src/args.rs | 28 +- rs/moq-cli/src/main.rs | 36 +- rs/moq-cli/src/play.rs | 709 +++++++++++++++++++++++++++++ rs/moq-cli/src/subscribe.rs | 48 +- 8 files changed, 1653 insertions(+), 103 deletions(-) create mode 100644 rs/moq-cli/src/play.rs diff --git a/Cargo.lock b/Cargo.lock index ba8b5aa3fc3..a2472b74926 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,22 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "ab_glyph" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01c0457472c38ea5bd1c3b5ada5e368271cb550be7a4ca4a0b4634e9913f6cc2" +dependencies = [ + "ab_glyph_rasterizer", + "owned_ttf_parser", +] + +[[package]] +name = "ab_glyph_rasterizer" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "366ffbaa4442f4684d91e2cd7c5ea7c4ed8add41959a31447066e279e432b618" + [[package]] name = "addr2line" version = "0.25.1" @@ -52,6 +68,19 @@ dependencies = [ "subtle", ] +[[package]] +name = "ahash" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a15f179cd60c4584b8a8c596927aadc462e27f2ca70c04e0071964a73ba7a75" +dependencies = [ + "cfg-if", + "getrandom 0.3.4", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "aho-corasick" version = "1.1.5" @@ -98,6 +127,31 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "android-activity" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f2a1bb052857d5dd49572219344a7332b31b76405648eabac5bc68978251bcd" +dependencies = [ + "android-properties", + "bitflags 2.13.1", + "cc", + "jni 0.22.4", + "libc", + "log", + "ndk", + "ndk-context", + "ndk-sys", + "num_enum", + "thiserror 2.0.19", +] + +[[package]] +name = "android-properties" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc7eb209b1518d6bb87b283c20095f5228ecda460da70b44f0802523dea6da04" + [[package]] name = "android_log-sys" version = "0.2.0" @@ -218,6 +272,12 @@ version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" +[[package]] +name = "as-raw-xcb-connection" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b" + [[package]] name = "ash" version = "0.38.0+1.3.281" @@ -758,13 +818,22 @@ dependencies = [ "hybrid-array", ] +[[package]] +name = "block2" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c132eebf10f5cad5289222520a4a058514204aed6d791f1cf4fe8088b82d15f" +dependencies = [ + "objc2 0.5.2", +] + [[package]] name = "block2" version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5" dependencies = [ - "objc2", + "objc2 0.6.4", ] [[package]] @@ -905,6 +974,32 @@ dependencies = [ "bytes", ] +[[package]] +name = "calloop" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b99da2f8558ca23c71f4fd15dc57c906239752dd27ff3c00a1d56b685b7cbfec" +dependencies = [ + "bitflags 2.13.1", + "log", + "polling", + "rustix 0.38.44", + "slab", + "thiserror 1.0.69", +] + +[[package]] +name = "calloop-wayland-source" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95a66a987056935f7efce4ab5668920b5d0dac4a7c99991a67395f13702ddd20" +dependencies = [ + "calloop", + "rustix 0.38.44", + "wayland-backend", + "wayland-client", +] + [[package]] name = "camino" version = "1.2.5" @@ -1244,6 +1339,15 @@ version = "0.4.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc14f565cf027a105f7a44ccf9e5b424348421a1d8952a8fc9d499d313107789" +[[package]] +name = "concurrent-queue" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "console_error_panic_hook" version = "0.1.7" @@ -1341,6 +1445,30 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "core-graphics" +version = "0.23.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c07782be35f9e1140080c6b96f0d44b739e2278479f64e02fdab4e32dfd8b081" +dependencies = [ + "bitflags 1.3.2", + "core-foundation 0.9.4", + "core-graphics-types", + "foreign-types", + "libc", +] + +[[package]] +name = "core-graphics-types" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45390e6114f68f718cc7a830514a96f903cccd70d02a8f6d9f643ac4ba45afaf" +dependencies = [ + "bitflags 1.3.2", + "core-foundation 0.9.4", + "libc", +] + [[package]] name = "coreaudio-rs" version = "0.14.2" @@ -1362,7 +1490,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5f77b11176c37874be37e8d691c946e31b2b8c357abce9526f6a99eb469e1028" dependencies = [ "alsa", - "block2", + "block2 0.6.2", "coreaudio-rs", "dasp_sample", "jni 0.22.4", @@ -1373,13 +1501,13 @@ dependencies = [ "ndk-context", "num-derive", "num-traits", - "objc2", + "objc2 0.6.4", "objc2-audio-toolbox", "objc2-avf-audio", "objc2-core-audio", "objc2-core-audio-types", "objc2-core-foundation", - "objc2-foundation", + "objc2-foundation 0.3.2", "web-sys", "windows", "windows-core", @@ -1588,6 +1716,12 @@ dependencies = [ "libloading 0.9.0", ] +[[package]] +name = "cursor-icon" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f27ae1dd37df86211c42e150270f82743308803d90a6f6e6651cd730d5e1732f" + [[package]] name = "curve25519-dalek" version = "5.0.0" @@ -1949,6 +2083,12 @@ dependencies = [ "x509-cert", ] +[[package]] +name = "dispatch" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd0c93bb4b0c6d9b77f4435b0ae98c24d17f1c45b2ff844c6151a07256ca923b" + [[package]] name = "dispatch2" version = "0.3.1" @@ -1956,9 +2096,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e0e367e4e7da84520dedcac1901e4da967309406d1e51017ae1abfb97adbd38" dependencies = [ "bitflags 2.13.1", - "block2", + "block2 0.6.2", "libc", - "objc2", + "objc2 0.6.4", ] [[package]] @@ -2001,6 +2141,18 @@ dependencies = [ "litrs", ] +[[package]] +name = "downcast-rs" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" + +[[package]] +name = "dpi" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8b14ccef22fc6f5a8f4d7d768562a182c04ce9a3b3157b91390b52ddfdf1a76" + [[package]] name = "dtoa" version = "1.0.11" @@ -2631,6 +2783,16 @@ dependencies = [ "version_check", ] +[[package]] +name = "gethostname" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bd49230192a3797a9a4d6abe9b3eed6f7fa4c8a8a4947977c6f80025f92cbd8" +dependencies = [ + "rustix 1.1.4", + "windows-link", +] + [[package]] name = "getrandom" version = "0.2.17" @@ -4107,6 +4269,18 @@ dependencies = [ "url", ] +[[package]] +name = "libredox" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2026a5056764a10b2bf5d56488cba40da507f5493a6a429340e2004d9ed085fa" +dependencies = [ + "bitflags 2.13.1", + "libc", + "plain", + "redox_syscall 0.9.1", +] + [[package]] name = "libspa" version = "0.10.0" @@ -4262,6 +4436,15 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "memmap2" +version = "0.9.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1219ed1b7f229ee7104d281dd01d6802fe28bb6e95d292942c4daacdeb798c0" +dependencies = [ + "libc", +] + [[package]] name = "memoffset" version = "0.9.1" @@ -4339,7 +4522,7 @@ dependencies = [ name = "moq-audio" version = "0.0.15" dependencies = [ - "block2", + "block2 0.6.2", "bytes", "cpal", "dispatch2", @@ -4347,11 +4530,11 @@ dependencies = [ "hang", "moq-mux", "moq-net", - "objc2", + "objc2 0.6.4", "objc2-av-foundation", "objc2-core-audio-types", "objc2-core-media", - "objc2-foundation", + "objc2-foundation 0.3.2", "objc2-screen-capture-kit", "rand 0.10.2", "rubato", @@ -4422,12 +4605,14 @@ dependencies = [ "moq-token-cli", "moq-transcode", "moq-video", + "pollster", "rustls", "sd-notify", "tokio", "tower-http 0.7.0", "tracing", "url", + "winit", ] [[package]] @@ -4836,7 +5021,7 @@ version = "0.0.13" dependencies = [ "anyhow", "ashpd", - "block2", + "block2 0.6.2", "bytemuck", "bytes", "cudarc", @@ -4850,14 +5035,14 @@ dependencies = [ "moq-net", "moq-nvenc", "moq-vaapi", - "objc2", + "objc2 0.6.4", "objc2-av-foundation", "objc2-core-foundation", "objc2-core-graphics", "objc2-core-media", "objc2-core-video", - "objc2-foundation", - "objc2-metal", + "objc2-foundation 0.3.2", + "objc2-metal 0.3.2", "objc2-screen-capture-kit", "objc2-video-toolbox", "openh264", @@ -5060,6 +5245,7 @@ dependencies = [ "log", "ndk-sys", "num_enum", + "raw-window-handle", "thiserror 1.0.69", ] @@ -5113,7 +5299,7 @@ version = "0.45.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "569dfbdd2efd771b24ec9bb57f956e04d4fbfc72f62b2f11961723f9b3f4b020" dependencies = [ - "block2", + "block2 0.6.2", "dispatch2", "dlopen2", "ipnet", @@ -5124,10 +5310,10 @@ dependencies = [ "netlink-packet-core", "netlink-packet-route", "netlink-sys", - "objc2", + "objc2 0.6.4", "objc2-core-foundation", "objc2-core-wlan", - "objc2-foundation", + "objc2-foundation 0.3.2", "objc2-system-configuration", "once_cell", "plist", @@ -5516,6 +5702,22 @@ dependencies = [ "scuffle-workspace-hack", ] +[[package]] +name = "objc-sys" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb91bdd390c7ce1a8607f35f3ca7151b65afc0ff5ff3b34fa350f7d7c7e4310" + +[[package]] +name = "objc2" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46a785d4eeff09c14c487497c162e92766fbb3e4059a71840cecc03d9a50b804" +dependencies = [ + "objc-sys", + "objc2-encode", +] + [[package]] name = "objc2" version = "0.6.4" @@ -5525,6 +5727,22 @@ dependencies = [ "objc2-encode", ] +[[package]] +name = "objc2-app-kit" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4e89ad9e3d7d297152b17d39ed92cd50ca8063a89a9fa569046d41568891eff" +dependencies = [ + "bitflags 2.13.1", + "block2 0.5.1", + "libc", + "objc2 0.5.2", + "objc2-core-data", + "objc2-core-image 0.2.2", + "objc2-foundation 0.2.2", + "objc2-quartz-core 0.2.2", +] + [[package]] name = "objc2-audio-toolbox" version = "0.3.2" @@ -5533,11 +5751,11 @@ checksum = "6948501a91121d6399b79abaa33a8aa4ea7857fe019f341b8c23ad6e81b79b08" dependencies = [ "bitflags 2.13.1", "libc", - "objc2", + "objc2 0.6.4", "objc2-core-audio", "objc2-core-audio-types", "objc2-core-foundation", - "objc2-foundation", + "objc2-foundation 0.3.2", ] [[package]] @@ -5547,19 +5765,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "478ae33fcac9df0a18db8302387c666b8ef08a3e2d62b510ca4fc278a384b6c0" dependencies = [ "bitflags 2.13.1", - "block2", + "block2 0.6.2", "dispatch2", - "objc2", + "objc2 0.6.4", "objc2-avf-audio", "objc2-core-audio-types", "objc2-core-foundation", "objc2-core-graphics", - "objc2-core-image", + "objc2-core-image 0.3.2", "objc2-core-video", - "objc2-foundation", + "objc2-foundation 0.3.2", "objc2-image-io", "objc2-media-toolbox", - "objc2-quartz-core", + "objc2-quartz-core 0.3.2", ] [[package]] @@ -5569,8 +5787,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13a380031deed8e99db00065c45937da434ca987c034e13b87e4441f9e4090be" dependencies = [ "bitflags 2.13.1", - "objc2", - "objc2-foundation", + "objc2 0.6.4", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-cloud-kit" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74dd3b56391c7a0596a295029734d3c1c5e7e510a4cb30245f8221ccea96b009" +dependencies = [ + "bitflags 2.13.1", + "block2 0.5.1", + "objc2 0.5.2", + "objc2-core-location", + "objc2-foundation 0.2.2", +] + +[[package]] +name = "objc2-contacts" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5ff520e9c33812fd374d8deecef01d4a840e7b41862d849513de77e44aa4889" +dependencies = [ + "block2 0.5.1", + "objc2 0.5.2", + "objc2-foundation 0.2.2", ] [[package]] @@ -5580,10 +5822,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1eebcea8b0dbff5f7c8504f3107c68fc061a3eb44932051c8cf8a68d969c3b2" dependencies = [ "dispatch2", - "objc2", + "objc2 0.6.4", "objc2-core-audio-types", "objc2-core-foundation", - "objc2-foundation", + "objc2-foundation 0.3.2", ] [[package]] @@ -5593,7 +5835,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a89f2ec274a0cf4a32642b2991e8b351a404d290da87bb6a9a9d8632490bd1c" dependencies = [ "bitflags 2.13.1", - "objc2", + "objc2 0.6.4", +] + +[[package]] +name = "objc2-core-data" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "617fbf49e071c178c0b24c080767db52958f716d9eabdf0890523aeae54773ef" +dependencies = [ + "bitflags 2.13.1", + "block2 0.5.1", + "objc2 0.5.2", + "objc2-foundation 0.2.2", ] [[package]] @@ -5603,10 +5857,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536" dependencies = [ "bitflags 2.13.1", - "block2", + "block2 0.6.2", "dispatch2", "libc", - "objc2", + "objc2 0.6.4", ] [[package]] @@ -5616,13 +5870,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e022c9d066895efa1345f8e33e584b9f958da2fd4cd116792e15e07e4720a807" dependencies = [ "bitflags 2.13.1", - "block2", + "block2 0.6.2", "dispatch2", "libc", - "objc2", + "objc2 0.6.4", "objc2-core-foundation", "objc2-io-surface", - "objc2-metal", + "objc2-metal 0.3.2", +] + +[[package]] +name = "objc2-core-image" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55260963a527c99f1819c4f8e3b47fe04f9650694ef348ffd2227e8196d34c80" +dependencies = [ + "block2 0.5.1", + "objc2 0.5.2", + "objc2-foundation 0.2.2", + "objc2-metal 0.2.2", ] [[package]] @@ -5631,8 +5897,20 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5d563b38d2b97209f8e861173de434bd0214cf020e3423a52624cd1d989f006" dependencies = [ - "objc2", - "objc2-foundation", + "objc2 0.6.4", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-core-location" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "000cfee34e683244f284252ee206a27953279d370e309649dc3ee317b37e5781" +dependencies = [ + "block2 0.5.1", + "objc2 0.5.2", + "objc2-contacts", + "objc2-foundation 0.2.2", ] [[package]] @@ -5642,9 +5920,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05ec576860167a15dd9fce7fbee7512beb4e31f532159d3482d1f9c6caedf31d" dependencies = [ "bitflags 2.13.1", - "block2", + "block2 0.6.2", "dispatch2", - "objc2", + "objc2 0.6.4", "objc2-core-audio", "objc2-core-audio-types", "objc2-core-foundation", @@ -5658,12 +5936,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d425caf1df73233f29fd8a5c3e5edbc30d2d4307870f802d18f00d83dc5141a6" dependencies = [ "bitflags 2.13.1", - "block2", - "objc2", + "block2 0.6.2", + "objc2 0.6.4", "objc2-core-foundation", "objc2-core-graphics", "objc2-io-surface", - "objc2-metal", + "objc2-metal 0.3.2", ] [[package]] @@ -5673,9 +5951,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c71e34919aba0d701380d911702455038a8a3587467fe0141d6a71501e7ffe48" dependencies = [ "bitflags 2.13.1", - "objc2", + "objc2 0.6.4", "objc2-core-foundation", - "objc2-foundation", + "objc2-foundation 0.3.2", "objc2-security", "objc2-security-foundation", ] @@ -5686,6 +5964,19 @@ version = "4.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33" +[[package]] +name = "objc2-foundation" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee638a5da3799329310ad4cfa62fbf045d5f56e3ef5ba4149e7452dcf89d5a8" +dependencies = [ + "bitflags 2.13.1", + "block2 0.5.1", + "dispatch", + "libc", + "objc2 0.5.2", +] + [[package]] name = "objc2-foundation" version = "0.3.2" @@ -5693,9 +5984,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3e0adef53c21f888deb4fa59fc59f7eb17404926ee8a6f59f5df0fd7f9f3272" dependencies = [ "bitflags 2.13.1", - "block2", + "block2 0.6.2", "libc", - "objc2", + "objc2 0.6.4", "objc2-core-foundation", ] @@ -5705,7 +5996,7 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32b0446e98cf4a784cc7a0177715ff317eeaa8463841c616cfc78aa4f953c4ea" dependencies = [ - "objc2", + "objc2 0.6.4", "objc2-core-foundation", "objc2-core-graphics", ] @@ -5727,22 +6018,46 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "180788110936d59bab6bd83b6060ffdfffb3b922ba1396b312ae795e1de9d81d" dependencies = [ "bitflags 2.13.1", - "objc2", + "objc2 0.6.4", "objc2-core-foundation", ] +[[package]] +name = "objc2-link-presentation" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1a1ae721c5e35be65f01a03b6d2ac13a54cb4fa70d8a5da293d7b0020261398" +dependencies = [ + "block2 0.5.1", + "objc2 0.5.2", + "objc2-app-kit", + "objc2-foundation 0.2.2", +] + [[package]] name = "objc2-media-toolbox" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edd9fdde720df3da7046bb9097811000c1e7ab5cd579fa89d96b27d56781fb30" dependencies = [ - "objc2", + "objc2 0.6.4", "objc2-core-audio-types", "objc2-core-foundation", "objc2-core-media", ] +[[package]] +name = "objc2-metal" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0cba1276f6023976a406a14ffa85e1fdd19df6b0f737b063b95f6c8c7aadd6" +dependencies = [ + "bitflags 2.13.1", + "block2 0.5.1", + "objc2 0.5.2", + "objc2-foundation 0.2.2", +] + [[package]] name = "objc2-metal" version = "0.3.2" @@ -5750,11 +6065,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0125f776a10d00af4152d74616409f0d4a2053a6f57fa5b7d6aa2854ac04794" dependencies = [ "bitflags 2.13.1", - "block2", + "block2 0.6.2", "dispatch2", - "objc2", + "objc2 0.6.4", "objc2-core-foundation", - "objc2-foundation", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-quartz-core" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e42bee7bff906b14b167da2bac5efe6b6a07e6f7c0a21a7308d40c960242dc7a" +dependencies = [ + "bitflags 2.13.1", + "block2 0.5.1", + "objc2 0.5.2", + "objc2-foundation 0.2.2", + "objc2-metal 0.2.2", ] [[package]] @@ -5764,11 +6092,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "96c1358452b371bf9f104e21ec536d37a650eb10f7ee379fff67d2e08d537f1f" dependencies = [ "bitflags 2.13.1", - "objc2", + "objc2 0.6.4", "objc2-core-foundation", "objc2-core-graphics", - "objc2-foundation", - "objc2-metal", + "objc2-foundation 0.3.2", + "objc2-metal 0.3.2", ] [[package]] @@ -5778,16 +6106,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74b7c5390f477482f001bc354d6571a70db7e4f8d5288e860c45521fbce11394" dependencies = [ "bitflags 2.13.1", - "block2", + "block2 0.6.2", "dispatch2", "libc", - "objc2", + "objc2 0.6.4", "objc2-av-foundation", "objc2-core-foundation", "objc2-core-graphics", "objc2-core-media", - "objc2-foundation", - "objc2-uniform-type-identifiers", + "objc2-foundation 0.3.2", + "objc2-uniform-type-identifiers 0.3.2", ] [[package]] @@ -5797,7 +6125,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "709fe137109bd1e8b5a99390f77a7d8b2961dafc1a1c5db8f2e60329ad6d895a" dependencies = [ "bitflags 2.13.1", - "objc2", + "objc2 0.6.4", "objc2-core-foundation", ] @@ -5807,8 +6135,18 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef76382e9cedd18123099f17638715cc3d81dba3637d4c0d39ab69df2ef345a5" dependencies = [ - "objc2", - "objc2-foundation", + "objc2 0.6.4", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-symbols" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a684efe3dec1b305badae1a28f6555f6ddd3bb2c2267896782858d5a78404dc" +dependencies = [ + "objc2 0.5.2", + "objc2-foundation 0.2.2", ] [[package]] @@ -5820,19 +6158,64 @@ dependencies = [ "bitflags 2.13.1", "dispatch2", "libc", - "objc2", + "objc2 0.6.4", "objc2-core-foundation", "objc2-security", ] +[[package]] +name = "objc2-ui-kit" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8bb46798b20cd6b91cbd113524c490f1686f4c4e8f49502431415f3512e2b6f" +dependencies = [ + "bitflags 2.13.1", + "block2 0.5.1", + "objc2 0.5.2", + "objc2-cloud-kit", + "objc2-core-data", + "objc2-core-image 0.2.2", + "objc2-core-location", + "objc2-foundation 0.2.2", + "objc2-link-presentation", + "objc2-quartz-core 0.2.2", + "objc2-symbols", + "objc2-uniform-type-identifiers 0.2.2", + "objc2-user-notifications", +] + +[[package]] +name = "objc2-uniform-type-identifiers" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44fa5f9748dbfe1ca6c0b79ad20725a11eca7c2218bceb4b005cb1be26273bfe" +dependencies = [ + "block2 0.5.1", + "objc2 0.5.2", + "objc2-foundation 0.2.2", +] + [[package]] name = "objc2-uniform-type-identifiers" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7902ac02859fc1f7045f8b598c63f1ae0cc7efeaa06a9bc9f3d9a3c955974fa4" dependencies = [ - "objc2", - "objc2-foundation", + "objc2 0.6.4", + "objc2-foundation 0.3.2", +] + +[[package]] +name = "objc2-user-notifications" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76cfcbf642358e8689af64cee815d139339f3ed8ad05103ed5eaf73db8d84cb3" +dependencies = [ + "bitflags 2.13.1", + "block2 0.5.1", + "objc2 0.5.2", + "objc2-core-location", + "objc2-foundation 0.2.2", ] [[package]] @@ -5842,14 +6225,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05bf9a3c14831a7d9641b0d81d87dd913ee238a012b2fde27db5a84b56f5df3e" dependencies = [ "bitflags 2.13.1", - "block2", - "objc2", + "block2 0.6.2", + "objc2 0.6.4", "objc2-core-foundation", "objc2-core-graphics", "objc2-core-media", "objc2-core-video", - "objc2-foundation", - "objc2-metal", + "objc2-foundation 0.3.2", + "objc2-metal 0.3.2", ] [[package]] @@ -5997,6 +6380,16 @@ dependencies = [ "paste", ] +[[package]] +name = "orbclient" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5df339f526ea9a60e371768d50efc2f2508c7203290731565d1f7a6f71d21747" +dependencies = [ + "libc", + "libredox", +] + [[package]] name = "ordered-float" version = "5.3.0" @@ -6016,6 +6409,15 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "owned_ttf_parser" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36820e9051aca1014ddc75770aab4d68bc1e9e632f0f5627c4086bc216fb583b" +dependencies = [ + "ttf-parser", +] + [[package]] name = "p256" version = "0.14.0" @@ -6089,7 +6491,7 @@ dependencies = [ "cfg-if", "libc", "petgraph", - "redox_syscall", + "redox_syscall 0.5.18", "smallvec", "windows-link", ] @@ -6316,6 +6718,20 @@ dependencies = [ "plotters-backend", ] +[[package]] +name = "polling" +version = "3.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d0e4f59085d47d8241c88ead0f274e8a0cb551f3625263c05eb8dd897c34218" +dependencies = [ + "cfg-if", + "concurrent-queue", + "hermit-abi", + "pin-project-lite", + "rustix 1.1.4", + "windows-sys 0.61.2", +] + [[package]] name = "pollster" version = "1.0.1" @@ -6916,10 +7332,10 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40d213455a5f1dc59214213c7330e074ddf8114c9a42411eb890c767357ce135" dependencies = [ - "objc2", + "objc2 0.6.4", "objc2-core-foundation", - "objc2-foundation", - "objc2-quartz-core", + "objc2-foundation 0.3.2", + "objc2-quartz-core 0.3.2", ] [[package]] @@ -6957,6 +7373,15 @@ dependencies = [ "yasna", ] +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags 1.3.2", +] + [[package]] name = "redox_syscall" version = "0.5.18" @@ -6966,6 +7391,15 @@ dependencies = [ "bitflags 2.13.1", ] +[[package]] +name = "redox_syscall" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07507be7b4a5f9f26eeb41eeaebb1f5a7ff29dfb29739facc21d35bf8b11c21e" +dependencies = [ + "bitflags 2.13.1", +] + [[package]] name = "ref-cast" version = "1.0.26" @@ -7402,6 +7836,19 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "sctk-adwaita" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6277f0217056f77f1d8f49f2950ac6c278c0d607c45f5ee99328d792ede24ec" +dependencies = [ + "ab_glyph", + "log", + "memmap2", + "smithay-client-toolkit", + "tiny-skia", +] + [[package]] name = "sctp-proto" version = "0.10.2" @@ -7968,6 +8415,40 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e8e2fb0f499abb4d162f2bedad68f5ef91a1682b5a03596ddb67efd37768d100" +[[package]] +name = "smithay-client-toolkit" +version = "0.19.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3457dea1f0eb631b4034d61d4d8c32074caa6cd1ab2d59f2327bd8461e2c0016" +dependencies = [ + "bitflags 2.13.1", + "calloop", + "calloop-wayland-source", + "cursor-icon", + "libc", + "log", + "memmap2", + "rustix 0.38.44", + "thiserror 1.0.69", + "wayland-backend", + "wayland-client", + "wayland-csd-frame", + "wayland-cursor", + "wayland-protocols", + "wayland-protocols-wlr", + "wayland-scanner", + "xkeysym", +] + +[[package]] +name = "smol_str" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd538fb6910ac1099850255cf94a94df6551fbdd602454387d0adb2d1ca6dead" +dependencies = [ + "serde", +] + [[package]] name = "socket2" version = "0.5.10" @@ -8243,6 +8724,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "strict-num" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731" + [[package]] name = "strsim" version = "0.11.1" @@ -8573,6 +9060,31 @@ dependencies = [ "time-core", ] +[[package]] +name = "tiny-skia" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83d13394d44dae3207b52a326c0c85a8bf87f1541f23b0d143811088497b09ab" +dependencies = [ + "arrayref", + "arrayvec", + "bytemuck", + "cfg-if", + "log", + "tiny-skia-path", +] + +[[package]] +name = "tiny-skia-path" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c9e7fc0c2e86a30b117d0462aa261b72b7a99b7ebd7deb3a14ceda95c5bdc93" +dependencies = [ + "arrayref", + "bytemuck", + "strict-num", +] + [[package]] name = "tinystr" version = "0.8.3" @@ -9102,6 +9614,12 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "ttf-parser" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31" + [[package]] name = "tungstenite" version = "0.29.0" @@ -9579,6 +10097,103 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "wayland-backend" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "016ccf01d1c58b6f8999612813e17c9b2390f7d70671428869913310f83f54b8" +dependencies = [ + "cc", + "downcast-rs", + "rustix 1.1.4", + "scoped-tls", + "smallvec", + "wayland-sys", +] + +[[package]] +name = "wayland-client" +version = "0.31.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3c36a0f861ad76d0901f2800b46321410d9f73f2ea88aac0650d86c32688073" +dependencies = [ + "bitflags 2.13.1", + "rustix 1.1.4", + "wayland-backend", + "wayland-scanner", +] + +[[package]] +name = "wayland-csd-frame" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "625c5029dbd43d25e6aa9615e88b829a5cad13b2819c4ae129fdbb7c31ab4c7e" +dependencies = [ + "bitflags 2.13.1", + "cursor-icon", + "wayland-backend", +] + +[[package]] +name = "wayland-cursor" +version = "0.31.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a52d18780be9b1314328a3de5f930b73d2200112e3849ca6cb11822793fb34d" +dependencies = [ + "rustix 1.1.4", + "wayland-client", + "xcursor", +] + +[[package]] +name = "wayland-protocols" +version = "0.32.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d0c813de3daa2ed6520af85a3bd49b0e722a3078506899aa9686fea58dc4b6" +dependencies = [ + "bitflags 2.13.1", + "wayland-backend", + "wayland-client", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-plasma" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b6d8cf1eb2c1c31ed1f5643c88a6e53538129d4af80030c8cabd1f9fa884d91" +dependencies = [ + "bitflags 2.13.1", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-wlr" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb04e52f7836d7c7976c78ca0250d61e33873c34156a2a1fc9474828ec268234" +dependencies = [ + "bitflags 2.13.1", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-scanner" +version = "0.31.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "338e30461b3a2b67d70eb30a6d89f8e0c93a833e07d2ae89085cd070c4a00ac0" +dependencies = [ + "proc-macro2", + "quick-xml", + "quote", +] + [[package]] name = "wayland-sys" version = "0.31.11" @@ -9891,7 +10506,7 @@ dependencies = [ "ash", "bit-set", "bitflags 2.13.1", - "block2", + "block2 0.6.2", "bytemuck", "cfg-if", "cfg_aliases", @@ -9907,12 +10522,12 @@ dependencies = [ "naga", "naga-types", "ndk-sys", - "objc2", + "objc2 0.6.4", "objc2-core-foundation", "objc2-core-graphics", - "objc2-foundation", - "objc2-metal", - "objc2-quartz-core", + "objc2-foundation 0.3.2", + "objc2-metal 0.3.2", + "objc2-quartz-core 0.3.2", "once_cell", "ordered-float", "parking_lot", @@ -10382,6 +10997,58 @@ version = "0.53.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" +[[package]] +name = "winit" +version = "0.30.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6755fa58a9f8350bd1e472d4c3fcc25f824ec358933bba33306d0b63df5978d" +dependencies = [ + "ahash", + "android-activity", + "atomic-waker", + "bitflags 2.13.1", + "block2 0.5.1", + "bytemuck", + "calloop", + "cfg_aliases", + "concurrent-queue", + "core-foundation 0.9.4", + "core-graphics", + "cursor-icon", + "dpi", + "js-sys", + "libc", + "memmap2", + "ndk", + "objc2 0.5.2", + "objc2-app-kit", + "objc2-foundation 0.2.2", + "objc2-ui-kit", + "orbclient", + "percent-encoding", + "pin-project", + "raw-window-handle", + "redox_syscall 0.4.1", + "rustix 0.38.44", + "sctk-adwaita", + "smithay-client-toolkit", + "smol_str", + "tracing", + "unicode-segmentation", + "wasm-bindgen", + "wasm-bindgen-futures", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-protocols-plasma", + "web-sys", + "web-time", + "windows-sys 0.52.0", + "x11-dl", + "x11rb", + "xkbcommon-dl", +] + [[package]] name = "winnow" version = "0.7.15" @@ -10480,6 +11147,38 @@ dependencies = [ "web-sys", ] +[[package]] +name = "x11-dl" +version = "2.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38735924fedd5314a6e548792904ed8c6de6636285cb9fec04d5b1db85c1516f" +dependencies = [ + "libc", + "once_cell", + "pkg-config", +] + +[[package]] +name = "x11rb" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9993aa5be5a26815fe2c3eacfc1fde061fc1a1f094bf1ad2a18bf9c495dd7414" +dependencies = [ + "as-raw-xcb-connection", + "gethostname", + "libc", + "libloading 0.8.9", + "once_cell", + "rustix 1.1.4", + "x11rb-protocol", +] + +[[package]] +name = "x11rb-protocol" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea6fc2961e4ef194dcbfe56bb845534d0dc8098940c7e5c012a258bfec6701bd" + [[package]] name = "x509-cert" version = "0.2.5" @@ -10510,6 +11209,31 @@ dependencies = [ "time", ] +[[package]] +name = "xcursor" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "163b33ed8786455e2fa5d72f554057ce3f3182425434f756cd39c99839d88e23" + +[[package]] +name = "xkbcommon-dl" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d039de8032a9a8856a6be89cea3e5d12fdd82306ab7c94d74e6deab2460651c5" +dependencies = [ + "bitflags 2.13.1", + "dlib", + "log", + "once_cell", + "xkeysym", +] + +[[package]] +name = "xkeysym" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56" + [[package]] name = "xml-rs" version = "0.8.28" diff --git a/doc/bin/cli.md b/doc/bin/cli.md index 36db4c40067..94ba429ce7d 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -6,8 +6,8 @@ description: Command-line tools for MoQ media # FFmpeg / moq-cli `moq-cli` is a media router: it wires one endpoint onto a shared MoQ Origin. It -moves media into MoQ from a source, or out of MoQ to a sink, bridging stdin/stdout -(via FFmpeg), HLS, RTMP, SRT, and WebRTC. +moves media into MoQ from a source, out of MoQ to a sink, or plays a broadcast +locally, bridging stdin/stdout (via FFmpeg), HLS, RTMP, SRT, and WebRTC. ## Installation @@ -74,6 +74,7 @@ Each signal writes a numbered `/tmp/moq.heap.*.heap` profile for analysis with ``` moq [endpoint options] +moq play [playback options] ``` - **MoQ side** attaches the Origin to the network, and comes before the verb. At @@ -90,6 +91,7 @@ moq [endpoint options] run); see [Redundant Publishers](#redundant-publishers-11). - **`import`** routes media INTO MoQ (a source fills the Origin); **`export`** routes it OUT (a sink drains the Origin). The verb fixes the data direction. +- **`play`** subscribes to a broadcast and plays its audio and video locally. - **endpoint** is one subcommand: a container format (`avc3`, `fmp4`, `ts`, `flv` read from stdin on import; `fmp4`, `mkv`, `ts`, `flv` written to stdout on export), or a gateway (`hls`, `rtmp`, `srt`, `rtc`). For the bidirectional @@ -109,6 +111,45 @@ side and reject one if given. `moq import ` reads a container from stdin; `moq export ` writes one to stdout. +### Play a Broadcast + +Native playback is gated behind the `play` feature, which adds the platform +window, graphics, decoder, and speaker dependencies: + +```bash +cargo install moq-cli --features play +# or build from a checkout: +cargo build --release -p moq-cli --features play +# or run straight from that checkout: +cargo run -p moq-cli --features play -- \ + --client-connect https://relay.example.com/anon --broadcast my-stream.hang play +``` + +Once built, play a broadcast with: + +```bash +moq --client-connect https://relay.example.com/anon --broadcast my-stream.hang play +``` + +`play` follows catalog updates and starts the first supported audio and video +renditions. Audio drives the video clock when both are present; audio-only and +video-only broadcasts also work. The window preserves the video's aspect ratio +and can be closed with Escape, the close button, or Ctrl-C. + +Use `--video-name`, `--audio-name`, `--video-codec`, or `--audio-codec` to select +a rendition. `--latency-max` controls how far a stalled media group may lag +before it is skipped and defaults to `500ms`: + +```bash +moq --client-connect https://relay.example.com/anon --broadcast conference.hang \ + play --video-name hd --audio-name en --latency-max 250ms +``` + +Video decoding prefers the platform hardware backend and falls back to the +built-in software H.264 decoder. Rendering uses Metal on macOS, D3D12 on +Windows, and Vulkan or OpenGL on Linux. Audio uses the system default output +device through CoreAudio, WASAPI, or ALSA. + ### Publish a Video File Remux a file to MPEG-TS and pipe it in (`-c copy` avoids re-encoding): diff --git a/rs/moq-cli/Cargo.toml b/rs/moq-cli/Cargo.toml index 889b1edafe7..e27931b736e 100644 --- a/rs/moq-cli/Cargo.toml +++ b/rs/moq-cli/Cargo.toml @@ -40,6 +40,10 @@ capture = ["dep:moq-video", "dep:moq-audio", "moq-audio/capture"] # in moq-video and its codec dependencies. Enable with # `cargo build -p moq-cli --features transcode`. transcode = ["dep:moq-transcode", "dep:moq-video"] +# Native playback (`moq ... play`): decode into a wgpu window and a cpal +# speaker. Off by default because it pulls both graphics and audio device stacks +# into a binary that may only be routing containers or serving gateways. +play = ["dep:moq-video", "moq-video/render", "dep:moq-audio", "moq-audio/playback", "dep:pollster", "dep:winit"] # NVIDIA hardware codecs for `capture` (Linux), on by default. Each just turns on # the matching moq-video feature, and only bites when `capture` pulls in moq-video. # For a `capture` build with no CUDA dependencies, drop them, e.g. @@ -73,11 +77,13 @@ moq-srt = { workspace = true } moq-token-cli = { workspace = true } moq-transcode = { workspace = true, optional = true } moq-video = { workspace = true, optional = true } +pollster = { version = "1.0", optional = true } rustls = { version = "0.23", features = ["aws-lc-rs"], default-features = false } tokio = { workspace = true, features = ["full"] } tower-http = { version = "0.7", features = ["cors"] } tracing = "0.1" url = "2" +winit = { version = "0.30.13", optional = true } [target.'cfg(unix)'.dependencies] sd-notify = "0.5" diff --git a/rs/moq-cli/README.md b/rs/moq-cli/README.md index 6e413e17267..3e63955df08 100644 --- a/rs/moq-cli/README.md +++ b/rs/moq-cli/README.md @@ -19,7 +19,7 @@ Multi-arch images (`linux/amd64` and `linux/arm64`) are published to [Docker Hub ## Usage -`moq-cli` routes one endpoint onto a shared MoQ Origin: `moq `. The MoQ side (before the verb) is either `--client-connect ` (dial a relay) or `--server-bind ` (self-host). `import` moves media into MoQ, `export` moves it out. The endpoint is a container format (`fmp4`, `ts`, `flv`, ... read from stdin / written to stdout), or a gateway (`hls`, `rtmp`, `srt`, `rtc`). +`moq-cli` routes one endpoint onto a shared MoQ Origin: `moq `. The MoQ side (before the verb) is either `--client-connect ` (dial a relay) or `--server-bind ` (self-host). `import` moves media into MoQ, `export` moves it out. The endpoint is a container format (`fmp4`, `ts`, `flv`, ... read from stdin / written to stdout), or a gateway (`hls`, `rtmp`, `srt`, `rtc`). A build with the `play` feature can also render a broadcast locally with `moq play`. ### Publish to a remote relay @@ -35,6 +35,12 @@ moq --client-connect https://relay.example.com --broadcast my-stream.hang export ffplay - ``` +Play the broadcast in a native window and speaker (requires `--features play` when building): + +```bash +moq --client-connect https://relay.example.com --broadcast my-stream.hang play +``` + ### Self-host: publish into a local relay Hosts a MoQ server and publishes a single broadcast read from stdin into it. Useful for local testing without a separate relay process. diff --git a/rs/moq-cli/src/args.rs b/rs/moq-cli/src/args.rs index 3ade0ed7d38..24b042fd487 100644 --- a/rs/moq-cli/src/args.rs +++ b/rs/moq-cli/src/args.rs @@ -1,6 +1,7 @@ //! The unified moq-cli argument surface. //! -//! Grammar: `moq [endpoint opts]`. +//! Grammar: `moq [endpoint opts]`, plus +//! `moq play` for native playback. //! //! - The MoQ side (`--client-connect` / `--server-bind`, both optional, at least //! one) attaches the shared Origin to the MoQ network, and comes before the @@ -138,6 +139,9 @@ pub enum Command { /// Route media OUT OF MoQ to one sink. #[command(alias = "subscribe")] Export(Export), + /// Play a broadcast in a native window and speaker. + #[cfg(feature = "play")] + Play(crate::play::Args), /// Re-encode `--broadcast` into a lower ladder, published next to it and /// only encoded while watched (just-in-time). #[cfg(feature = "transcode")] @@ -371,4 +375,26 @@ mod tests { assert!(err.contains(flag[0]), "{err}"); } } + + #[cfg(feature = "play")] + #[test] + fn play_verb() { + let cli = Cli::try_parse_from([ + "moq", + "--client-connect", + "https://relay.example.com/anon", + "--broadcast", + "room.hang", + "play", + "--video-name", + "hd", + ]) + .unwrap(); + let Command::Play(play) = cli.command else { + panic!("expected play") + }; + assert_eq!(play.latency_max, Duration::from_millis(500)); + assert_eq!(play.select.video_name.as_deref(), Some("hd")); + assert!(cli.moq.validate().is_ok()); + } } diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index 8d82112cc85..169d8989b57 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -1,14 +1,16 @@ //! moq-cli: a media router that wires one endpoint onto a shared MoQ Origin. //! -//! The binary is `moq`. See [`args`] for the `import`/`export` command grammar; -//! this module orchestrates the shared Origin and spawns the MoQ side plus the -//! selected endpoint. +//! The binary is `moq`. See [`args`] for the `import`/`export`/`play` command +//! grammar; this module orchestrates the shared Origin and spawns the MoQ side +//! plus the selected endpoint. mod args; #[cfg(feature = "capture")] mod devices; mod hls; mod moq; +#[cfg(feature = "play")] +mod play; mod publish; mod rtc; mod rtmp; @@ -104,6 +106,8 @@ async fn main() -> anyhow::Result<()> { match cli.command { Command::Import(import) => run_import(cli.moq, import, net).await, Command::Export(export) => run_export(cli.moq, export, net).await, + #[cfg(feature = "play")] + Command::Play(args) => run_play(cli.moq, args, net).await, #[cfg(feature = "transcode")] Command::Transcode(args) => transcode::run(cli.moq, args, net).await, Command::Token(_) => unreachable!("handled above, before the transport is bound"), @@ -118,6 +122,32 @@ async fn main() -> anyhow::Result<()> { } } +/// Fill the shared Origin from MoQ, then play one broadcast locally. +#[cfg(feature = "play")] +async fn run_play(moq: MoqSide, args: play::Args, net: Net) -> anyhow::Result<()> { + let origin = moq.origin()?; + let name = moq.broadcast.clone().unwrap_or_default(); + let mut tasks: JoinSet> = JoinSet::new(); + + if moq.client.connect.is_some() + && let Some(reconnect) = net.client(moq.client.clone())?.consume(origin.clone()) + { + moq::notify_ready(); + tasks.spawn(async move { Ok(reconnect.closed().await?) }); + } + if let Some(web_bind) = moq.server.bind.clone() { + let server = net.server(moq.server.clone())?; + let certificates = server.certificates(); + moq::notify_ready(); + let origin = origin.clone(); + tasks.spawn(async move { Ok(server.serve_consume(origin).await?) }); + tasks.spawn(async move { web::run_web(&web_bind, certificates).await }); + } + + let source = moq_mux::Source::new(origin.consume(), &name); + play::run(source, name, args, tasks) +} + /// Route one source INTO the shared Origin, exposing it to the MoQ network. async fn run_import(moq: MoqSide, import: Import, net: Net) -> anyhow::Result<()> { let origin = moq.origin()?; diff --git a/rs/moq-cli/src/play.rs b/rs/moq-cli/src/play.rs new file mode 100644 index 00000000000..424f8cbc0b8 --- /dev/null +++ b/rs/moq-cli/src/play.rs @@ -0,0 +1,709 @@ +//! Native playback for a MoQ broadcast. + +use std::collections::VecDeque; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use anyhow::Context; +use clap::Args as ClapArgs; +use moq_mux::catalog::{self, CatalogFormat, Stream}; +use moq_video::render::wgpu; +use winit::application::ApplicationHandler; +use winit::dpi::LogicalSize; +use winit::event::{ElementState, KeyEvent, WindowEvent}; +use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProxy}; +use winit::keyboard::{Key, NamedKey}; +use winit::window::{Window, WindowId}; + +use crate::subscribe::{CatalogFormatArg, SelectArgs}; + +const MAX_VIDEO_FRAMES: usize = 30; +const VIDEO_EARLY_TOLERANCE: Duration = Duration::from_millis(2); + +/// Play one MoQ broadcast through a native window and speaker. +#[derive(ClapArgs, Clone)] +pub struct Args { + /// Catalog format, detected from the broadcast suffix when omitted. + #[arg(long)] + pub catalog_format: Option, + + /// Maximum media buffering before skipping a stalled group. + #[arg(long, default_value = "500ms", value_parser = humantime::parse_duration)] + pub latency_max: Duration, + + /// Rendition selection by track name or codec. + #[command(flatten)] + pub select: SelectArgs, +} + +impl Args { + fn catalog_format(&self, broadcast: &str) -> CatalogFormat { + self.catalog_format + .map(Into::into) + .or_else(|| CatalogFormat::detect(broadcast)) + .unwrap_or_default() + } +} + +#[derive(Clone, Copy)] +struct Clock { + media: Duration, + wall: Instant, +} + +impl Clock { + fn now(self) -> Duration { + self.media.saturating_add(self.wall.elapsed()) + } +} + +enum Event { + Wake, + Finished, + Failed(String), +} + +/// Run native playback on the calling thread until the window closes. +pub fn run( + source: moq_mux::Source, + broadcast: String, + args: Args, + network: tokio::task::JoinSet>, +) -> anyhow::Result<()> { + let event_loop = EventLoop::::with_user_event() + .build() + .context("failed to create the playback event loop")?; + let proxy = event_loop.create_proxy(); + let video = Arc::new(Mutex::new(VecDeque::new())); + let audio_clock = Arc::new(Mutex::new(None)); + + let media = tokio::spawn(run_media( + source, + broadcast.clone(), + args, + video.clone(), + audio_clock.clone(), + proxy.clone(), + )); + let network = tokio::spawn(watch_network(network, proxy.clone())); + let signal = tokio::spawn({ + let proxy = proxy.clone(); + async move { + if tokio::signal::ctrl_c().await.is_ok() { + let _ = proxy.send_event(Event::Finished); + } + } + }); + + let title = if broadcast.is_empty() { + "moq play".to_string() + } else { + format!("moq play: {broadcast}") + }; + let mut app = App::new(title, video, audio_clock); + let result = event_loop.run_app(&mut app).context("playback event loop failed"); + media.abort(); + network.abort(); + signal.abort(); + result?; + + match app.error { + Some(err) => anyhow::bail!(err), + None => Ok(()), + } +} + +async fn watch_network(mut tasks: tokio::task::JoinSet>, proxy: EventLoopProxy) { + while let Some(result) = tasks.join_next().await { + let event = match result { + Ok(Ok(())) => Event::Finished, + Ok(Err(err)) => Event::Failed(format!("MoQ transport failed: {err:#}")), + Err(err) if err.is_cancelled() => continue, + Err(err) => Event::Failed(format!("MoQ transport task failed: {err}")), + }; + let _ = proxy.send_event(event); + return; + } +} + +async fn run_media( + source: moq_mux::Source, + broadcast_name: String, + args: Args, + video: Arc>>, + audio_clock: Arc>>, + proxy: EventLoopProxy, +) { + let result = media(source, broadcast_name, args, video, audio_clock, proxy.clone()).await; + let event = match result { + Ok(()) => Event::Finished, + Err(err) => Event::Failed(format!("{err:#}")), + }; + let _ = proxy.send_event(event); +} + +async fn media( + source: moq_mux::Source, + broadcast_name: String, + args: Args, + video: Arc>>, + audio_clock: Arc>>, + proxy: EventLoopProxy, +) -> anyhow::Result<()> { + let broadcast = source + .broadcast() + .await + .context("failed to subscribe to the broadcast")?; + let catalog = catalog::Consumer::<()>::new(&broadcast, args.catalog_format(&broadcast_name)) + .await + .context("failed to subscribe to the catalog")?; + let mut catalogs = catalog.select(args.select.selection(None)); + let mut tasks = tokio::task::JoinSet::new(); + let mut video_started = false; + let mut audio_started = false; + + loop { + tokio::select! { + result = tasks.join_next(), if !tasks.is_empty() => { + return joined(result.expect("guarded by is_empty")); + } + snapshot = catalogs.next(), if !video_started || !audio_started => { + let Some(snapshot) = snapshot.context("failed to read the catalog")? else { + anyhow::ensure!(video_started || audio_started, "the catalog contains no playable audio or video renditions"); + break; + }; + + if !video_started { + for (name, config) in snapshot.video.renditions { + let rendition = source.resolve(config.broadcast.as_ref()).await?; + let mut decode = moq_video::decode::Config::new(); + decode.latency_max = Some(args.latency_max); + match moq_video::decode::Consumer::new(&rendition, &config, &name, decode).await { + Ok(consumer) => { + tracing::info!(track = name, decoder = consumer.name(), "playing video rendition"); + tasks.spawn(play_video(consumer, video.clone(), proxy.clone())); + video_started = true; + break; + } + Err(err) => tracing::warn!(track = name, %err, "cannot play video rendition"), + } + } + } + + if !audio_started { + for (name, config) in snapshot.audio.renditions { + let rendition = source.resolve(config.broadcast.as_ref()).await?; + let mut decode = moq_audio::decode::Config::new(); + decode.latency_max = Some(args.latency_max); + match moq_audio::decode::Consumer::new(&rendition, &config, &name, decode).await { + Ok(consumer) => { + tracing::info!(track = name, "playing audio rendition"); + tasks.spawn(play_audio(consumer, audio_clock.clone(), proxy.clone())); + audio_started = true; + break; + } + Err(err) => tracing::warn!(track = name, %err, "cannot play audio rendition"), + } + } + } + + if video_started && audio_started { + break; + } + } + } + } + + let result = tasks.join_next().await; + joined(result.context("all playback tracks stopped")?) +} + +fn joined(result: Result, tokio::task::JoinError>) -> anyhow::Result<()> { + match result { + Ok(result) => result, + Err(err) if err.is_cancelled() => Ok(()), + Err(err) => Err(err.into()), + } +} + +async fn play_video( + mut consumer: moq_video::decode::Consumer, + video: Arc>>, + proxy: EventLoopProxy, +) -> anyhow::Result<()> { + while let Some(frame) = consumer.read().await? { + let mut video = video.lock().unwrap(); + video.push_back(frame); + while video.len() > MAX_VIDEO_FRAMES { + video.pop_front(); + } + drop(video); + let _ = proxy.send_event(Event::Wake); + } + Ok(()) +} + +async fn play_audio( + mut consumer: moq_audio::decode::Consumer, + clock: Arc>>, + proxy: EventLoopProxy, +) -> anyhow::Result<()> { + let sample_rate = consumer.sample_rate(); + let channels = consumer.channels(); + let engine = moq_audio::playback::Engine::open(Default::default()).await?; + let mut sink = engine.sink(moq_audio::playback::Input { + format: moq_audio::Format::F32, + sample_rate, + channels, + })?; + + while let Some(frame) = consumer.read().await? { + let samples = frame.data.len() / size_of::() / channels as usize; + let end = + timestamp(frame.timestamp).saturating_add(Duration::from_secs_f64(samples as f64 / sample_rate as f64)); + sink.write(&frame.data)?; + *clock.lock().unwrap() = Some(Clock { + media: end.saturating_sub(sink.buffered()), + wall: Instant::now(), + }); + let _ = proxy.send_event(Event::Wake); + } + Ok(()) +} + +fn timestamp(timestamp: hang::moq_net::Timestamp) -> Duration { + Duration::from_micros(timestamp.as_micros().min(u64::MAX as u128) as u64) +} + +struct App { + title: String, + video: Arc>>, + audio_clock: Arc>>, + video_clock: Option, + display: Option, + next_redraw: Option, + error: Option, +} + +impl App { + fn new( + title: String, + video: Arc>>, + audio_clock: Arc>>, + ) -> Self { + Self { + title, + video, + audio_clock, + video_clock: None, + display: None, + next_redraw: None, + error: None, + } + } + + fn redraw(&mut self) -> anyhow::Result<()> { + let Some(display) = self.display.as_mut() else { + return Ok(()); + }; + let mut video = self.video.lock().unwrap(); + let audio_clock = *self.audio_clock.lock().unwrap(); + + if self.video_clock.is_none() + && audio_clock.is_none() + && let Some(frame) = video.front() + { + self.video_clock = Some(Clock { + media: timestamp(frame.timestamp), + wall: Instant::now(), + }); + } + let clock = audio_clock.or(self.video_clock); + let now = clock.map(Clock::now); + let mut due = None; + while video.front().is_some_and(|frame| { + now.is_none_or(|now| timestamp(frame.timestamp) <= now.saturating_add(VIDEO_EARLY_TOLERANCE)) + }) { + due = video.pop_front(); + } + let next_timestamp = video.front().map(|frame| timestamp(frame.timestamp)); + drop(video); + + if let Some(frame) = due { + display.render(&frame)?; + } + display.present()?; + + self.next_redraw = match (clock, next_timestamp) { + (Some(clock), Some(next)) => Some(Instant::now() + next.saturating_sub(clock.now())), + _ => None, + }; + Ok(()) + } + + fn fail(&mut self, event_loop: &ActiveEventLoop, err: impl ToString) { + self.error = Some(err.to_string()); + event_loop.exit(); + } +} + +impl ApplicationHandler for App { + fn resumed(&mut self, event_loop: &ActiveEventLoop) { + if self.display.is_some() { + return; + } + match Display::new(event_loop, &self.title) { + Ok(display) => self.display = Some(display), + Err(err) => self.fail(event_loop, err), + } + } + + fn user_event(&mut self, event_loop: &ActiveEventLoop, event: Event) { + match event { + Event::Wake => { + if let Some(display) = &self.display { + display.window.request_redraw(); + } + } + Event::Finished => event_loop.exit(), + Event::Failed(err) => self.fail(event_loop, err), + } + } + + fn window_event(&mut self, event_loop: &ActiveEventLoop, window_id: WindowId, event: WindowEvent) { + if self + .display + .as_ref() + .is_none_or(|display| display.window.id() != window_id) + { + return; + } + match event { + WindowEvent::CloseRequested + | WindowEvent::KeyboardInput { + event: + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), + state: ElementState::Pressed, + .. + }, + .. + } => event_loop.exit(), + WindowEvent::Resized(size) => { + if let Some(display) = self.display.as_mut() { + display.resize(size.width, size.height); + display.window.request_redraw(); + } + } + WindowEvent::RedrawRequested => { + if let Err(err) = self.redraw() { + self.fail(event_loop, err); + } + } + _ => {} + } + } + + fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { + match self.next_redraw { + Some(deadline) if deadline <= Instant::now() => { + self.next_redraw = None; + if let Some(display) = &self.display { + display.window.request_redraw(); + } + } + Some(deadline) => event_loop.set_control_flow(ControlFlow::WaitUntil(deadline)), + None => event_loop.set_control_flow(ControlFlow::Wait), + } + } +} + +struct Display { + window: Arc, + surface: wgpu::Surface<'static>, + device: wgpu::Device, + queue: wgpu::Queue, + config: wgpu::SurfaceConfiguration, + renderer: moq_video::render::Renderer, + presenter: Presenter, + texture: Option<(wgpu::Texture, moq_video::Size)>, +} + +impl Display { + fn new(event_loop: &ActiveEventLoop, title: &str) -> anyhow::Result { + let window = Arc::new( + event_loop.create_window( + Window::default_attributes() + .with_title(title) + .with_inner_size(LogicalSize::new(960, 540)), + )?, + ); + let size = window.inner_size(); + let instance = wgpu::Instance::default(); + let surface = instance.create_surface(window.clone())?; + let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions { + power_preference: wgpu::PowerPreference::HighPerformance, + force_fallback_adapter: false, + compatible_surface: Some(&surface), + apply_limit_buckets: false, + }))?; + let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default()))?; + let mut config = surface + .get_default_config(&adapter, size.width.max(1), size.height.max(1)) + .context("the graphics adapter cannot present to this window")?; + let caps = surface.get_capabilities(&adapter); + if let Some(format) = caps.formats.iter().copied().find(|format| !format.is_srgb()) { + config.format = format; + } + config.desired_maximum_frame_latency = 1; + surface.configure(&device, &config); + let renderer = moq_video::render::Renderer::new(&device, &queue, Default::default())?; + let presenter = Presenter::new(&device, config.format); + + Ok(Self { + window, + surface, + device, + queue, + config, + renderer, + presenter, + texture: None, + }) + } + + fn resize(&mut self, width: u32, height: u32) { + if width == 0 || height == 0 { + return; + } + self.config.width = width; + self.config.height = height; + self.surface.configure(&self.device, &self.config); + } + + fn render(&mut self, frame: &moq_video::Frame) -> anyhow::Result<()> { + self.texture = Some((self.renderer.render(frame)?, frame.size())); + Ok(()) + } + + fn present(&mut self) -> anyhow::Result<()> { + use wgpu::CurrentSurfaceTexture; + + let (output, reconfigure) = match self.surface.get_current_texture() { + CurrentSurfaceTexture::Success(output) => (output, false), + CurrentSurfaceTexture::Suboptimal(output) => (output, true), + CurrentSurfaceTexture::Timeout | CurrentSurfaceTexture::Occluded => return Ok(()), + CurrentSurfaceTexture::Outdated => { + self.surface.configure(&self.device, &self.config); + return Ok(()); + } + CurrentSurfaceTexture::Lost => anyhow::bail!("the playback window's graphics surface was lost"), + CurrentSurfaceTexture::Validation => anyhow::bail!("failed to acquire the playback window's next frame"), + }; + let target = output.texture.create_view(&Default::default()); + let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor { + label: Some("moq play present"), + }); + let viewport = self + .texture + .as_ref() + .map(|(_, size)| fit((self.config.width, self.config.height), (size.width, size.height))); + self.presenter.draw( + &self.device, + &mut encoder, + self.texture.as_ref().map(|(texture, _)| texture), + &target, + viewport, + ); + self.queue.submit([encoder.finish()]); + self.queue.present(output); + if reconfigure { + self.surface.configure(&self.device, &self.config); + } + Ok(()) + } +} + +struct Presenter { + pipeline: wgpu::RenderPipeline, + layout: wgpu::BindGroupLayout, + sampler: wgpu::Sampler, +} + +impl Presenter { + fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self { + let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { + label: Some("moq play texture layout"), + entries: &[ + wgpu::BindGroupLayoutEntry { + binding: 0, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { filterable: true }, + view_dimension: wgpu::TextureViewDimension::D2, + multisampled: false, + }, + count: None, + }, + wgpu::BindGroupLayoutEntry { + binding: 1, + visibility: wgpu::ShaderStages::FRAGMENT, + ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), + count: None, + }, + ], + }); + let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { + label: Some("moq play pipeline layout"), + bind_group_layouts: &[Some(&layout)], + immediate_size: 0, + }); + let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { + label: Some("moq play shader"), + source: wgpu::ShaderSource::Wgsl( + "struct VertexOutput {\n\ + @builtin(position) position: vec4f,\n\ + @location(0) tex_coords: vec2f,\n\ + }\n\ + @group(0) @binding(0) var image: texture_2d;\n\ + @group(0) @binding(1) var image_sampler: sampler;\n\ + @vertex fn vs_main(@builtin(vertex_index) i: u32) -> VertexOutput {\n\ + var out: VertexOutput;\n\ + out.tex_coords = vec2f(f32((i << 1u) & 2u), f32(i & 2u));\n\ + out.position = vec4f(out.tex_coords * 2.0 - 1.0, 0.0, 1.0);\n\ + out.tex_coords.y = 1.0 - out.tex_coords.y;\n\ + return out;\n\ + }\n\ + @fragment fn fs_main(in: VertexOutput) -> @location(0) vec4f {\n\ + return textureSample(image, image_sampler, in.tex_coords);\n\ + }" + .into(), + ), + }); + let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { + label: Some("moq play pipeline"), + layout: Some(&pipeline_layout), + vertex: wgpu::VertexState { + module: &shader, + entry_point: Some("vs_main"), + compilation_options: Default::default(), + buffers: &[], + }, + primitive: Default::default(), + depth_stencil: None, + multisample: Default::default(), + fragment: Some(wgpu::FragmentState { + module: &shader, + entry_point: Some("fs_main"), + compilation_options: Default::default(), + targets: &[Some(wgpu::ColorTargetState { + format, + blend: None, + write_mask: wgpu::ColorWrites::ALL, + })], + }), + multiview_mask: None, + cache: None, + }); + let sampler = device.create_sampler(&wgpu::SamplerDescriptor { + label: Some("moq play sampler"), + mag_filter: wgpu::FilterMode::Linear, + min_filter: wgpu::FilterMode::Linear, + ..Default::default() + }); + Self { + pipeline, + layout, + sampler, + } + } + + fn draw( + &self, + device: &wgpu::Device, + encoder: &mut wgpu::CommandEncoder, + source: Option<&wgpu::Texture>, + target: &wgpu::TextureView, + viewport: Option<(f32, f32, f32, f32)>, + ) { + let bind = source.map(|texture| { + let view = texture.create_view(&Default::default()); + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some("moq play texture"), + layout: &self.layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: wgpu::BindingResource::TextureView(&view), + }, + wgpu::BindGroupEntry { + binding: 1, + resource: wgpu::BindingResource::Sampler(&self.sampler), + }, + ], + }) + }); + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("moq play present"), + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: target, + depth_slice: None, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(wgpu::Color::BLACK), + store: wgpu::StoreOp::Store, + }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + multiview_mask: None, + }); + if let (Some(bind), Some((x, y, width, height))) = (bind.as_ref(), viewport) { + pass.set_viewport(x, y, width, height, 0.0, 1.0); + pass.set_pipeline(&self.pipeline); + pass.set_bind_group(0, bind, &[]); + pass.draw(0..3, 0..1); + } + } +} + +fn fit(window: (u32, u32), video: (u32, u32)) -> (f32, f32, f32, f32) { + let scale = (window.0 as f32 / video.0 as f32).min(window.1 as f32 / video.1 as f32); + let width = video.0 as f32 * scale; + let height = video.1 as f32 * scale; + ( + (window.0 as f32 - width) / 2.0, + (window.1 as f32 - height) / 2.0, + width, + height, + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn letterboxes_without_changing_aspect_ratio() { + let assert_near = |actual: (f32, f32, f32, f32), expected: (f32, f32, f32, f32)| { + for (actual, expected) in [actual.0, actual.1, actual.2, actual.3] + .into_iter() + .zip([expected.0, expected.1, expected.2, expected.3]) + { + assert!((actual - expected).abs() < 0.01, "{actual} != {expected}"); + } + }; + assert_near(fit((1000, 1000), (1920, 1080)), (0.0, 218.75, 1000.0, 562.5)); + assert_near(fit((1920, 1080), (1000, 1000)), (420.0, 0.0, 1080.0, 1080.0)); + } + + #[test] + fn clock_advances_from_its_media_anchor() { + let clock = Clock { + media: Duration::from_secs(10), + wall: Instant::now() - Duration::from_millis(20), + }; + assert!(clock.now() >= Duration::from_millis(10_020)); + } +} diff --git a/rs/moq-cli/src/subscribe.rs b/rs/moq-cli/src/subscribe.rs index f23c5c1cf37..5ab084eba68 100644 --- a/rs/moq-cli/src/subscribe.rs +++ b/rs/moq-cli/src/subscribe.rs @@ -70,6 +70,7 @@ impl From for VideoCodecKind { pub enum AudioCodecArg { Aac, Opus, + Pcm, } impl From for AudioCodecKind { @@ -77,12 +78,13 @@ impl From for AudioCodecKind { match value { AudioCodecArg::Aac => Self::AAC, AudioCodecArg::Opus => Self::Opus, + AudioCodecArg::Pcm => Self::Pcm, } } } -/// Rendition selection flags for the stdout container sinks. With no flags set, -/// every rendition is kept. +/// Rendition selection flags for stdout container sinks and native playback. +/// With no flags set, every rendition is kept. #[derive(clap::Args, Clone, Default)] pub struct SelectArgs { /// Pick the video rendition with this exact name. @@ -102,6 +104,29 @@ pub struct SelectArgs { pub audio_codec: Option, } +impl SelectArgs { + /// Build the rendition selection shared by stdout exports and native playback. + pub(crate) fn selection(&self, video_codec: Option) -> select::Broadcast { + let mut video = select::Video::default(); + if let Some(name) = &self.video_name { + video = video.name(name); + } + if let Some(codec) = video_codec.or_else(|| self.video_codec.map(Into::into)) { + video = video.codec(codec); + } + + let mut audio = select::Audio::default(); + if let Some(name) = &self.audio_name { + audio = audio.name(name); + } + if let Some(codec) = self.audio_codec { + audio = audio.codec(codec.into()); + } + + select::Broadcast::default().video(video).audio(audio) + } +} + /// The resolved stdout export settings (built from the `export` flags + format). #[derive(Clone)] pub struct SubscribeArgs { @@ -159,24 +184,7 @@ impl SubscribeArgs { (None, user) => user, }; - // Both roles stay opted in; criteria-free roles keep every rendition. - let mut video = select::Video::default(); - if let Some(name) = &self.select.video_name { - video = video.name(name); - } - if let Some(codec) = codec { - video = video.codec(codec); - } - - let mut audio = select::Audio::default(); - if let Some(name) = &self.select.audio_name { - audio = audio.name(name); - } - if let Some(codec) = self.select.audio_codec { - audio = audio.codec(codec.into()); - } - - Ok(select::Broadcast::default().video(video).audio(audio)) + Ok(self.select.selection(codec)) } } From 3c9342014e380b5e4c449b935ee9d037d125188f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 6 Aug 2026 12:11:24 -0700 Subject: [PATCH 02/16] fix(cli): keep moq play alive when one track ends Address review on #2697. - Unignore CI: winit's default `wayland-csd-adwaita` pulls sctk-adwaita -> ab_glyph -> ttf-parser, which is RUSTSEC-2026-0192 (unmaintained, no upgrade). It only parses the system font for Wayland client-side decorations, so ignore the advisory rather than dropping the feature and losing a titlebar on GNOME. cargo-deny runs ahead of every compile step in `just rs ci`, so this was also what stopped CI from ever building the feature. - `media()` returned on the first task to finish, and a track ending returns `Ok(())`, so a publisher that ended video tore down the window while audio was still playing. Drain the JoinSet instead and stop once every track that started has ended; disarm the catalog branch once it ends so a `None` stream can't spin. - Ask the decoder for f32 explicitly. Both the sink layout and the sample-count behind the audio clock assumed it, via `Format::default()`. - Wake the render loop on the first audio frame only. Every 20ms frame was requesting a redraw, which repainted the same picture ~50 times a second on an audio-only broadcast. - Draw once from `resumed()` so the window is black while the broadcast resolves instead of showing undefined surface contents. - Hoist the MoQ consume-side wiring out of `run_export`/`run_play` into `spawn_moq_consume`. - Docs: `--audio-codec` gained `pcm`, the selection flags now serve `play` too, and the play section overstated how long catalog updates are followed. Co-Authored-By: Claude Opus 5 --- deny.toml | 7 +++++ doc/bin/cli.md | 17 +++++++----- rs/moq-cli/src/main.rs | 45 ++++++++++++++++--------------- rs/moq-cli/src/play.rs | 53 ++++++++++++++++++++++++++++--------- rs/moq-cli/src/subscribe.rs | 7 +++-- 5 files changed, 86 insertions(+), 43 deletions(-) diff --git a/deny.toml b/deny.toml index 378e538f0ba..64dc5edf2b0 100644 --- a/deny.toml +++ b/deny.toml @@ -26,6 +26,13 @@ ignore = [ # needs Rust 1.92, above the 1.91 workspace MSRV. Build-time only. "RUSTSEC-2024-0436", + # ttf-parser is unmaintained, with no upgrade available. Reachable only via + # winit -> sctk-adwaita -> ab_glyph, which parses the system font used to + # draw Wayland client-side decorations for `moq play`. Dropping winit's + # `wayland-csd-adwaita` feature would remove it, at the cost of a titlebar + # on GNOME. Awaits sctk-adwaita migrating to skrifa. + "RUSTSEC-2026-0192", + ] [licenses] diff --git a/doc/bin/cli.md b/doc/bin/cli.md index 94ba429ce7d..dc8b19afa98 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -131,10 +131,13 @@ Once built, play a broadcast with: moq --client-connect https://relay.example.com/anon --broadcast my-stream.hang play ``` -`play` follows catalog updates and starts the first supported audio and video -renditions. Audio drives the video clock when both are present; audio-only and -video-only broadcasts also work. The window preserves the video's aspect ratio -and can be closed with Escape, the close button, or Ctrl-C. +`play` watches the catalog until it has a playable audio and video rendition, +then stays on them. "First" means the alphabetically first track name, so pass +`--video-name` / `--audio-name` to pick a specific rung of a ladder. Audio +drives the video clock when both are present; audio-only and video-only +broadcasts also work, and playback ends once every track it started has ended. +The window preserves the video's aspect ratio and can be closed with Escape, the +close button, or Ctrl-C. Use `--video-name`, `--audio-name`, `--video-codec`, or `--audio-codec` to select a rendition. `--latency-max` controls how far a stalled media group may lag @@ -430,8 +433,8 @@ discovery. When omitted, it's auto-detected from the broadcast name suffix - `hangz` - the DEFLATE-compressed `catalog.json.z` catalog (opt-in; shares the `.hang` suffix and is never auto-detected) - `msf` - the MSF `catalog` track -Stdout exports can also select one rendition per media role before the sink -subcommand: +Stdout exports and `play` can also select one rendition per media role, before +the sink subcommand: ```bash moq --client-connect https://relay.example.com/anon --broadcast my-stream.hang \ @@ -441,7 +444,7 @@ moq --client-connect https://relay.example.com/anon --broadcast my-stream.hang \ - `--video-name ` picks the video rendition with that exact catalog name. - `--video-codec ` keeps only matching video renditions. - `--audio-name ` picks the audio rendition with that exact catalog name. -- `--audio-codec ` keeps only matching audio renditions. +- `--audio-codec ` keeps only matching audio renditions. With no selection flags, every matching rendition is kept. The `h264` and `h265` export sinks force the matching video codec, so use `export h264` / `export h265` diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index 169d8989b57..97c6787506c 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -122,13 +122,14 @@ async fn main() -> anyhow::Result<()> { } } -/// Fill the shared Origin from MoQ, then play one broadcast locally. -#[cfg(feature = "play")] -async fn run_play(moq: MoqSide, args: play::Args, net: Net) -> anyhow::Result<()> { - let origin = moq.origin()?; - let name = moq.broadcast.clone().unwrap_or_default(); - let mut tasks: JoinSet> = JoinSet::new(); - +/// Attach the MoQ side so it fills the shared Origin: dial a relay, accept +/// inbound sessions, or both. Shared by every verb that consumes. +fn spawn_moq_consume( + moq: &MoqSide, + net: &Net, + origin: &moq_net::origin::Producer, + tasks: &mut JoinSet>, +) -> anyhow::Result<()> { if moq.client.connect.is_some() && let Some(reconnect) = net.client(moq.client.clone())?.consume(origin.clone()) { @@ -143,6 +144,21 @@ async fn run_play(moq: MoqSide, args: play::Args, net: Net) -> anyhow::Result<() tasks.spawn(async move { Ok(server.serve_consume(origin).await?) }); tasks.spawn(async move { web::run_web(&web_bind, certificates).await }); } + Ok(()) +} + +/// Fill the shared Origin from MoQ, then play one broadcast locally. +/// +/// The playback event loop runs on this task's thread rather than a spawned one: +/// winit can only build an event loop on the process main thread, which is where +/// `#[tokio::main]` polls this future. +#[cfg(feature = "play")] +async fn run_play(moq: MoqSide, args: play::Args, net: Net) -> anyhow::Result<()> { + let origin = moq.origin()?; + let name = moq.broadcast.clone().unwrap_or_default(); + let mut tasks: JoinSet> = JoinSet::new(); + + spawn_moq_consume(&moq, &net, &origin, &mut tasks)?; let source = moq_mux::Source::new(origin.consume(), &name); play::run(source, name, args, tasks) @@ -291,20 +307,7 @@ async fn run_export(moq: MoqSide, export: Export, net: Net) -> anyhow::Result<() } // MoQ side: fill the Origin. - if moq.client.connect.is_some() - && let Some(reconnect) = net.client(moq.client.clone())?.consume(origin.clone()) - { - moq::notify_ready(); - tasks.spawn(async move { Ok(reconnect.closed().await?) }); - } - if let Some(web_bind) = moq.server.bind.clone() { - let server = net.server(moq.server.clone())?; - let certificates = server.certificates(); - moq::notify_ready(); - let origin = origin.clone(); - tasks.spawn(async move { Ok(server.serve_consume(origin).await?) }); - tasks.spawn(async move { web::run_web(&web_bind, certificates).await }); - } + spawn_moq_consume(&moq, &net, &origin, &mut tasks)?; // Foreign side: the single sink. if let Some((format, max_latency, fragment_duration)) = export.sink.stdout() { diff --git a/rs/moq-cli/src/play.rs b/rs/moq-cli/src/play.rs index 424f8cbc0b8..84323b9b866 100644 --- a/rs/moq-cli/src/play.rs +++ b/rs/moq-cli/src/play.rs @@ -17,7 +17,14 @@ use winit::window::{Window, WindowId}; use crate::subscribe::{CatalogFormatArg, SelectArgs}; +/// Decoded frames held for presentation. About a second at 30fps, which absorbs +/// a burst without letting a decoder that outruns the clock grow unbounded. The +/// oldest go first, since the render loop would skip past them anyway. const MAX_VIDEO_FRAMES: usize = 30; + +/// How early a frame may be shown rather than waiting another wakeup for it. +/// Under a display's frame interval, so it can't be seen, but enough that timer +/// slop doesn't push every frame a whole refresh late. const VIDEO_EARLY_TOLERANCE: Duration = Duration::from_millis(2); /// Play one MoQ broadcast through a native window and speaker. @@ -64,6 +71,10 @@ enum Event { } /// Run native playback on the calling thread until the window closes. +/// +/// Blocking is deliberate: winit only builds an event loop on the process main +/// thread, so this has to stay on the `#[tokio::main]` future rather than being +/// spawned. Media and transport run on tasks and talk to it through the proxy. pub fn run( source: moq_mux::Source, broadcast: String, @@ -161,16 +172,26 @@ async fn media( let mut tasks = tokio::task::JoinSet::new(); let mut video_started = false; let mut audio_started = false; + // Set once the catalog track ends, which disarms the branch below: a stream + // that has returned `None` returns it forever, so polling it again spins. + let mut catalog_ended = false; loop { + // Audio and video end independently, so one track finishing is not the end + // of playback. Stop once every track that started has ended. + if tasks.is_empty() && (video_started || audio_started) { + return Ok(()); + } + tokio::select! { result = tasks.join_next(), if !tasks.is_empty() => { - return joined(result.expect("guarded by is_empty")); + joined(result.expect("guarded by is_empty"))?; } - snapshot = catalogs.next(), if !video_started || !audio_started => { + snapshot = catalogs.next(), if !catalog_ended && (!video_started || !audio_started) => { let Some(snapshot) = snapshot.context("failed to read the catalog")? else { anyhow::ensure!(video_started || audio_started, "the catalog contains no playable audio or video renditions"); - break; + catalog_ended = true; + continue; }; if !video_started { @@ -195,6 +216,9 @@ async fn media( let rendition = source.resolve(config.broadcast.as_ref()).await?; let mut decode = moq_audio::decode::Config::new(); decode.latency_max = Some(args.latency_max); + // The sink and the frame-duration math below both assume f32, + // so ask for it rather than inheriting the decoder default. + decode.format = moq_audio::Format::F32; match moq_audio::decode::Consumer::new(&rendition, &config, &name, decode).await { Ok(consumer) => { tracing::info!(track = name, "playing audio rendition"); @@ -206,16 +230,9 @@ async fn media( } } } - - if video_started && audio_started { - break; - } } } } - - let result = tasks.join_next().await; - joined(result.context("all playback tracks stopped")?) } fn joined(result: Result, tokio::task::JoinError>) -> anyhow::Result<()> { @@ -262,11 +279,16 @@ async fn play_audio( let end = timestamp(frame.timestamp).saturating_add(Duration::from_secs_f64(samples as f64 / sample_rate as f64)); sink.write(&frame.data)?; - *clock.lock().unwrap() = Some(Clock { + let previous = clock.lock().unwrap().replace(Clock { media: end.saturating_sub(sink.buffered()), wall: Instant::now(), }); - let _ = proxy.send_event(Event::Wake); + // Only the very first sample needs a wake, to hand the render loop a clock + // to schedule against. After that the clock extrapolates from its wall + // anchor, so waking per 20 ms frame would just redraw the same picture. + if previous.is_none() { + let _ = proxy.send_event(Event::Wake); + } } Ok(()) } @@ -353,7 +375,12 @@ impl ApplicationHandler for App { return; } match Display::new(event_loop, &self.title) { - Ok(display) => self.display = Some(display), + // Draw once up front so the window is black while the broadcast is + // still resolving, rather than showing whatever was in the surface. + Ok(display) => { + display.window.request_redraw(); + self.display = Some(display); + } Err(err) => self.fail(event_loop, err), } } diff --git a/rs/moq-cli/src/subscribe.rs b/rs/moq-cli/src/subscribe.rs index 5ab084eba68..603bf4cf393 100644 --- a/rs/moq-cli/src/subscribe.rs +++ b/rs/moq-cli/src/subscribe.rs @@ -106,12 +106,15 @@ pub struct SelectArgs { impl SelectArgs { /// Build the rendition selection shared by stdout exports and native playback. - pub(crate) fn selection(&self, video_codec: Option) -> select::Broadcast { + /// + /// `force` takes the place of `--video-codec`, for a sink whose format implies + /// one. Pass `None` to use the flag as given. + pub(crate) fn selection(&self, force: Option) -> select::Broadcast { let mut video = select::Video::default(); if let Some(name) = &self.video_name { video = video.name(name); } - if let Some(codec) = video_codec.or_else(|| self.video_codec.map(Into::into)) { + if let Some(codec) = force.or_else(|| self.video_codec.map(Into::into)) { video = video.codec(codec); } From 854bc5a2f58f66cb7ce27797e12ef7fe7092ac2b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 6 Aug 2026 13:18:20 -0700 Subject: [PATCH 03/16] fix(cli): make moq play degrade instead of exiting Address bot review on #2697. - A rendition whose `broadcast` reference is unroutable took the whole player down, while an unsupported codec on the same rendition only warned. Warn and fall through to the next rendition either way. - Reject `--video-codec vp8|vp9` and `--audio-codec aac` before dialing. The selection flags are shared with the exports, which pass bytes through, so playback would otherwise filter the catalog down to a rendition its decoders can't open and sit on a blank window. - `Instant::now() + duration` panics rather than saturating, and the duration comes from a wire timestamp. Use `checked_add`; no deadline just means the next frame waits for a media wakeup. - Document the Linux install as `--no-default-features`: `play` enables moq-video, which the default `pipewire` feature then wires up for display capture, so `cargo install moq-cli --features play` wanted libpipewire and libclang for a backend playback never touches. - Docs: the play section claimed it waits for both roles two lines above saying single-role broadcasts work, and the export section claimed the selection flags go before the subcommand, which is only true for the stdout sinks. Co-Authored-By: Claude Opus 5 --- doc/bin/cli.md | 37 ++++++++++++++++++++++------------- rs/moq-cli/src/args.rs | 25 ++++++++++++++++++++++++ rs/moq-cli/src/main.rs | 3 +++ rs/moq-cli/src/play.rs | 44 +++++++++++++++++++++++++++++++++++++++--- 4 files changed, 93 insertions(+), 16 deletions(-) diff --git a/doc/bin/cli.md b/doc/bin/cli.md index dc8b19afa98..b58e588539b 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -117,7 +117,7 @@ Native playback is gated behind the `play` feature, which adds the platform window, graphics, decoder, and speaker dependencies: ```bash -cargo install moq-cli --features play +cargo install moq-cli --no-default-features --features "iroh,quinn,websocket,play" # or build from a checkout: cargo build --release -p moq-cli --features play # or run straight from that checkout: @@ -125,23 +125,33 @@ cargo run -p moq-cli --features play -- \ --client-connect https://relay.example.com/anon --broadcast my-stream.hang play ``` +Drop the defaults on Linux, as above. The default `pipewire` feature links +libpipewire-0.3 at build time, and its bindgen wants libclang, all to give +`import capture` a display source that playback never uses. macOS and Windows +have nothing to trim, so plain `--features play` is enough there. + Once built, play a broadcast with: ```bash moq --client-connect https://relay.example.com/anon --broadcast my-stream.hang play ``` -`play` watches the catalog until it has a playable audio and video rendition, -then stays on them. "First" means the alphabetically first track name, so pass -`--video-name` / `--audio-name` to pick a specific rung of a ladder. Audio -drives the video clock when both are present; audio-only and video-only -broadcasts also work, and playback ends once every track it started has ended. -The window preserves the video's aspect ratio and can be closed with Escape, the -close button, or Ctrl-C. +`play` starts each media role as soon as the catalog offers a rendition it can +decode, independently of the other, and keeps following catalog updates until +both have started. Audio-only and video-only broadcasts play fine; playback ends +once every track it started has ended. It doesn't switch renditions afterwards, +so a publisher that replaces the one being played ends that role. + +Within a role it takes the first rendition it can decode, in track-name order, +which is arbitrary as a quality choice. Pass `--video-name` / `--audio-name` to +pick a specific rung of a ladder. Audio drives the video clock when both are +present. The window preserves the video's aspect ratio and can be closed with +Escape, the close button, or Ctrl-C. -Use `--video-name`, `--audio-name`, `--video-codec`, or `--audio-codec` to select -a rendition. `--latency-max` controls how far a stalled media group may lag -before it is skipped and defaults to `500ms`: +Playback decodes h264, h265, and av1 video, and opus and pcm audio, so +`--video-codec` / `--audio-codec` are rejected up front for anything else. +`--latency-max` controls how far a stalled media group may lag before it is +skipped and defaults to `500ms`. These flags all follow the `play` verb: ```bash moq --client-connect https://relay.example.com/anon --broadcast conference.hang \ @@ -433,8 +443,9 @@ discovery. When omitted, it's auto-detected from the broadcast name suffix - `hangz` - the DEFLATE-compressed `catalog.json.z` catalog (opt-in; shares the `.hang` suffix and is never auto-detected) - `msf` - the MSF `catalog` track -Stdout exports and `play` can also select one rendition per media role, before -the sink subcommand: +Stdout exports can also select one rendition per media role. The flags go before +the sink subcommand here; `play` takes the same four after its own verb (see +[Play a Broadcast](#play-a-broadcast)): ```bash moq --client-connect https://relay.example.com/anon --broadcast my-stream.hang \ diff --git a/rs/moq-cli/src/args.rs b/rs/moq-cli/src/args.rs index 24b042fd487..533277b85c2 100644 --- a/rs/moq-cli/src/args.rs +++ b/rs/moq-cli/src/args.rs @@ -396,5 +396,30 @@ mod tests { assert_eq!(play.latency_max, Duration::from_millis(500)); assert_eq!(play.select.video_name.as_deref(), Some("hd")); assert!(cli.moq.validate().is_ok()); + assert!(play.validate().is_ok()); + } + + /// The selection flags are shared with the exports, which pass every codec + /// through. Playback has to decode, so it rejects the rest up front instead + /// of filtering the catalog down to a rendition that can't open. + #[cfg(feature = "play")] + #[test] + fn play_rejects_undecodable_codecs() { + for flag in [["--video-codec", "vp9"], ["--audio-codec", "aac"]] { + let cli = Cli::try_parse_from([ + "moq", + "--client-connect", + "https://relay.example.com/anon", + "play", + flag[0], + flag[1], + ]) + .unwrap(); + let Command::Play(play) = cli.command else { + panic!("expected play") + }; + let err = play.validate().unwrap_err().to_string(); + assert!(err.contains(flag[1]), "{err}"); + } } } diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index 97c6787506c..775d19b375a 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -154,6 +154,9 @@ fn spawn_moq_consume( /// `#[tokio::main]` polls this future. #[cfg(feature = "play")] async fn run_play(moq: MoqSide, args: play::Args, net: Net) -> anyhow::Result<()> { + // Before anything dials: a codec we can't decode is a blank window otherwise. + args.validate()?; + let origin = moq.origin()?; let name = moq.broadcast.clone().unwrap_or_default(); let mut tasks: JoinSet> = JoinSet::new(); diff --git a/rs/moq-cli/src/play.rs b/rs/moq-cli/src/play.rs index 84323b9b866..b254300af7e 100644 --- a/rs/moq-cli/src/play.rs +++ b/rs/moq-cli/src/play.rs @@ -50,6 +50,26 @@ impl Args { .or_else(|| CatalogFormat::detect(broadcast)) .unwrap_or_default() } + + /// Reject a codec the local decoders can't open. + /// + /// The selection flags are shared with the stdout exports, which pass bytes + /// through and so accept every codec the catalog can name. Asking for one of + /// those here would filter the catalog down to a rendition that then fails to + /// decode, leaving a blank window rather than an error. + pub fn validate(&self) -> anyhow::Result<()> { + use crate::subscribe::{AudioCodecArg, VideoCodecArg}; + + anyhow::ensure!( + !matches!(self.select.video_codec, Some(VideoCodecArg::Vp8 | VideoCodecArg::Vp9)), + "`play` cannot decode vp8 or vp9; pass --video-codec h264, h265, or av1" + ); + anyhow::ensure!( + !matches!(self.select.audio_codec, Some(AudioCodecArg::Aac)), + "`play` cannot decode aac; pass --audio-codec opus or pcm" + ); + Ok(()) + } } #[derive(Clone, Copy)] @@ -196,7 +216,16 @@ async fn media( if !video_started { for (name, config) in snapshot.video.renditions { - let rendition = source.resolve(config.broadcast.as_ref()).await?; + // A rendition pointing at a broadcast we can't reach is that + // rendition's problem, not the catalog's: fall through to the + // next one like an unsupported codec does. + let rendition = match source.resolve(config.broadcast.as_ref()).await { + Ok(rendition) => rendition, + Err(err) => { + tracing::warn!(track = name, %err, "cannot resolve video rendition"); + continue; + } + }; let mut decode = moq_video::decode::Config::new(); decode.latency_max = Some(args.latency_max); match moq_video::decode::Consumer::new(&rendition, &config, &name, decode).await { @@ -213,7 +242,13 @@ async fn media( if !audio_started { for (name, config) in snapshot.audio.renditions { - let rendition = source.resolve(config.broadcast.as_ref()).await?; + let rendition = match source.resolve(config.broadcast.as_ref()).await { + Ok(rendition) => rendition, + Err(err) => { + tracing::warn!(track = name, %err, "cannot resolve audio rendition"); + continue; + } + }; let mut decode = moq_audio::decode::Config::new(); decode.latency_max = Some(args.latency_max); // The sink and the frame-duration math below both assume f32, @@ -356,8 +391,11 @@ impl App { } display.present()?; + // `checked_add`: the wait comes from a wire timestamp, and adding a bogus + // one to an `Instant` panics rather than saturating. No deadline just means + // the next frame waits for a media wakeup instead. self.next_redraw = match (clock, next_timestamp) { - (Some(clock), Some(next)) => Some(Instant::now() + next.saturating_sub(clock.now())), + (Some(clock), Some(next)) => Instant::now().checked_add(next.saturating_sub(clock.now())), _ => None, }; Ok(()) From 07b97e04a45b9e729de5e6ae213d62fb44fdf868 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 6 Aug 2026 13:23:02 -0700 Subject: [PATCH 04/16] fix(cli): wait for the announcement before moq play resolves `media` called `Source::broadcast()` as soon as it was spawned, which lands on `origin::Consumer::request_broadcast`. That fails `Unroutable` on the spot when no session has registered a Dynamic handler yet (see `dynamic_request_unroutable_without_handler` in moq-net), and `run_play` spawns this task immediately after kicking off the reconnect loop, so it beats the handshake essentially every time rather than only under load. Wait for the broadcast to be announced first, like `run_stdout` already does. Doing it inside `media` rather than in `run_play` keeps the window up during the wait, so it reads as a black frame with a working close button instead of a process that appears hung. `run` now takes the `origin::Consumer` and builds the `Source` at the point of use, since both the wait and the source need it. Co-Authored-By: Claude Opus 5 --- rs/moq-cli/src/main.rs | 3 +-- rs/moq-cli/src/play.rs | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/rs/moq-cli/src/main.rs b/rs/moq-cli/src/main.rs index 775d19b375a..b3c84a806d6 100644 --- a/rs/moq-cli/src/main.rs +++ b/rs/moq-cli/src/main.rs @@ -163,8 +163,7 @@ async fn run_play(moq: MoqSide, args: play::Args, net: Net) -> anyhow::Result<() spawn_moq_consume(&moq, &net, &origin, &mut tasks)?; - let source = moq_mux::Source::new(origin.consume(), &name); - play::run(source, name, args, tasks) + play::run(origin.consume(), name, args, tasks) } /// Route one source INTO the shared Origin, exposing it to the MoQ network. diff --git a/rs/moq-cli/src/play.rs b/rs/moq-cli/src/play.rs index b254300af7e..a7ce16c8f82 100644 --- a/rs/moq-cli/src/play.rs +++ b/rs/moq-cli/src/play.rs @@ -6,6 +6,7 @@ use std::time::{Duration, Instant}; use anyhow::Context; use clap::Args as ClapArgs; +use hang::moq_net; use moq_mux::catalog::{self, CatalogFormat, Stream}; use moq_video::render::wgpu; use winit::application::ApplicationHandler; @@ -96,7 +97,7 @@ enum Event { /// thread, so this has to stay on the `#[tokio::main]` future rather than being /// spawned. Media and transport run on tasks and talk to it through the proxy. pub fn run( - source: moq_mux::Source, + origin: moq_net::origin::Consumer, broadcast: String, args: Args, network: tokio::task::JoinSet>, @@ -109,7 +110,7 @@ pub fn run( let audio_clock = Arc::new(Mutex::new(None)); let media = tokio::spawn(run_media( - source, + origin, broadcast.clone(), args, video.clone(), @@ -158,14 +159,14 @@ async fn watch_network(mut tasks: tokio::task::JoinSet>, prox } async fn run_media( - source: moq_mux::Source, + origin: moq_net::origin::Consumer, broadcast_name: String, args: Args, video: Arc>>, audio_clock: Arc>>, proxy: EventLoopProxy, ) { - let result = media(source, broadcast_name, args, video, audio_clock, proxy.clone()).await; + let result = media(origin, broadcast_name, args, video, audio_clock, proxy.clone()).await; let event = match result { Ok(()) => Event::Finished, Err(err) => Event::Failed(format!("{err:#}")), @@ -174,13 +175,23 @@ async fn run_media( } async fn media( - source: moq_mux::Source, + origin: moq_net::origin::Consumer, broadcast_name: String, args: Args, video: Arc>>, audio_clock: Arc>>, proxy: EventLoopProxy, ) -> anyhow::Result<()> { + // Wait for the announcement before resolving anything. A request against an + // origin no session has reached yet fails `Unroutable` on the spot, and this + // task starts well before the first handshake lands. The window is already + // up, so the wait shows as black rather than as a hang. + origin + .announced_broadcast(&broadcast_name) + .await + .with_context(|| format!("origin closed before broadcast `{broadcast_name}` was announced"))?; + + let source = moq_mux::Source::new(origin, &broadcast_name); let broadcast = source .broadcast() .await From c6b5bafac6ded57feffa9d8ecd98d7c689e6e213 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 6 Aug 2026 13:29:54 -0700 Subject: [PATCH 05/16] fix(cli): play out the tail and report an unplayable catalog - Every decoder is gated by platform and cargo feature (no AV1 without nvdec, no VideoToolbox off macOS), so the codec flags can't be validated against the build up front. Instead, a snapshot whose renditions all fail to start with nothing already playing is now an error naming each rejection, rather than a blank window that waits forever. - `Sink::write` never blocks and drops what won't fit, so a burst delivered faster than real time lost samples. Wait when the sink is more than a second ahead of the speaker, and play the buffer out at EOF instead of dropping the sink on top of it. The drain is bounded, since a device that never opens reports a full queue forever. - Reaching decoder EOF is no longer the same event as Ctrl-C: `Ended` lets the queued video present before the loop exits, while `Finished` still stops now. - `media` grew past six parameters, so it's a `Media` struct with a `run`. Docs: a Linux build needs libclang and the V4L2 headers whatever features are picked, because moq-video's camera capture isn't behind one. Putting it behind a feature is the real fix and belongs in its own change. Co-Authored-By: Claude Opus 5 --- doc/bin/cli.md | 11 +- rs/moq-cli/src/play.rs | 290 +++++++++++++++++++++++++---------------- 2 files changed, 188 insertions(+), 113 deletions(-) diff --git a/doc/bin/cli.md b/doc/bin/cli.md index b58e588539b..47a0138200b 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -125,10 +125,13 @@ cargo run -p moq-cli --features play -- \ --client-connect https://relay.example.com/anon --broadcast my-stream.hang play ``` -Drop the defaults on Linux, as above. The default `pipewire` feature links -libpipewire-0.3 at build time, and its bindgen wants libclang, all to give -`import capture` a display source that playback never uses. macOS and Windows -have nothing to trim, so plain `--features play` is enough there. +Drop the defaults on Linux, as above: the default `pipewire` feature links +libpipewire-0.3 at build time to give `import capture` a display source that +playback never uses. That still leaves one build dependency a playback-only box +would rather not have. `play` pulls in moq-video, whose V4L2 camera capture is +not behind a feature, so a Linux build needs libclang and the V4L2 headers +(`libclang-dev` and `libv4l-dev` on Debian) for bindgen either way. macOS and +Windows need nothing extra, so plain `--features play` is enough there. Once built, play a broadcast with: diff --git a/rs/moq-cli/src/play.rs b/rs/moq-cli/src/play.rs index a7ce16c8f82..fdedb14bfad 100644 --- a/rs/moq-cli/src/play.rs +++ b/rs/moq-cli/src/play.rs @@ -28,6 +28,17 @@ const MAX_VIDEO_FRAMES: usize = 30; /// slop doesn't push every frame a whole refresh late. const VIDEO_EARLY_TOLERANCE: Duration = Duration::from_millis(2); +/// How far ahead of the speaker the decoder may run before we make it wait. +/// `Sink::write` never blocks and drops whatever won't fit, so a burst (a +/// completed group arriving all at once) would otherwise lose its tail. Well +/// under the sink's own ceiling, and well over the ~50 ms it settles at. +const AUDIO_BUFFER_MAX: Duration = Duration::from_secs(1); + +/// Give up waiting on the speaker to drain this long after the last sample. +/// A device that never opens reports its queue as full forever, and a truncated +/// tail beats hanging on the way out. +const AUDIO_DRAIN_MAX: Duration = Duration::from_secs(4); + /// Play one MoQ broadcast through a native window and speaker. #[derive(ClapArgs, Clone)] pub struct Args { @@ -87,7 +98,10 @@ impl Clock { enum Event { Wake, + /// Stop now: Ctrl-C, or the transport is gone and there is nothing more coming. Finished, + /// Every track reached its end. Present what is still queued, then stop. + Ended, Failed(String), } @@ -109,14 +123,17 @@ pub fn run( let video = Arc::new(Mutex::new(VecDeque::new())); let audio_clock = Arc::new(Mutex::new(None)); - let media = tokio::spawn(run_media( - origin, - broadcast.clone(), - args, - video.clone(), - audio_clock.clone(), - proxy.clone(), - )); + let media = tokio::spawn( + Media { + origin, + broadcast: broadcast.clone(), + args, + video: video.clone(), + audio_clock: audio_clock.clone(), + proxy: proxy.clone(), + } + .run(), + ); let network = tokio::spawn(watch_network(network, proxy.clone())); let signal = tokio::spawn({ let proxy = proxy.clone(); @@ -158,123 +175,143 @@ async fn watch_network(mut tasks: tokio::task::JoinSet>, prox } } -async fn run_media( +/// Everything the media task needs to fill the window and the speaker. +struct Media { origin: moq_net::origin::Consumer, - broadcast_name: String, + broadcast: String, args: Args, video: Arc>>, audio_clock: Arc>>, proxy: EventLoopProxy, -) { - let result = media(origin, broadcast_name, args, video, audio_clock, proxy.clone()).await; - let event = match result { - Ok(()) => Event::Finished, - Err(err) => Event::Failed(format!("{err:#}")), - }; - let _ = proxy.send_event(event); } -async fn media( - origin: moq_net::origin::Consumer, - broadcast_name: String, - args: Args, - video: Arc>>, - audio_clock: Arc>>, - proxy: EventLoopProxy, -) -> anyhow::Result<()> { - // Wait for the announcement before resolving anything. A request against an - // origin no session has reached yet fails `Unroutable` on the spot, and this - // task starts well before the first handshake lands. The window is already - // up, so the wait shows as black rather than as a hang. - origin - .announced_broadcast(&broadcast_name) - .await - .with_context(|| format!("origin closed before broadcast `{broadcast_name}` was announced"))?; - - let source = moq_mux::Source::new(origin, &broadcast_name); - let broadcast = source - .broadcast() - .await - .context("failed to subscribe to the broadcast")?; - let catalog = catalog::Consumer::<()>::new(&broadcast, args.catalog_format(&broadcast_name)) - .await - .context("failed to subscribe to the catalog")?; - let mut catalogs = catalog.select(args.select.selection(None)); - let mut tasks = tokio::task::JoinSet::new(); - let mut video_started = false; - let mut audio_started = false; - // Set once the catalog track ends, which disarms the branch below: a stream - // that has returned `None` returns it forever, so polling it again spins. - let mut catalog_ended = false; - - loop { - // Audio and video end independently, so one track finishing is not the end - // of playback. Stop once every track that started has ended. - if tasks.is_empty() && (video_started || audio_started) { - return Ok(()); - } +impl Media { + async fn run(self) { + let proxy = self.proxy.clone(); + let event = match self.play().await { + Ok(()) => Event::Ended, + Err(err) => Event::Failed(format!("{err:#}")), + }; + let _ = proxy.send_event(event); + } - tokio::select! { - result = tasks.join_next(), if !tasks.is_empty() => { - joined(result.expect("guarded by is_empty"))?; + async fn play(self) -> anyhow::Result<()> { + // Wait for the announcement before resolving anything. A request against an + // origin no session has reached yet fails `Unroutable` on the spot, and this + // task starts well before the first handshake lands. The window is already + // up, so the wait shows as black rather than as a hang. + self.origin + .announced_broadcast(&self.broadcast) + .await + .with_context(|| format!("origin closed before broadcast `{}` was announced", self.broadcast))?; + + let source = moq_mux::Source::new(self.origin.clone(), &self.broadcast); + let broadcast = source + .broadcast() + .await + .context("failed to subscribe to the broadcast")?; + let catalog = catalog::Consumer::<()>::new(&broadcast, self.args.catalog_format(&self.broadcast)) + .await + .context("failed to subscribe to the catalog")?; + let mut catalogs = catalog.select(self.args.select.selection(None)); + let mut tasks = tokio::task::JoinSet::new(); + let mut video_started = false; + let mut audio_started = false; + // Set once the catalog track ends, which disarms the branch below: a stream + // that has returned `None` returns it forever, so polling it again spins. + let mut catalog_ended = false; + + loop { + // Audio and video end independently, so one track finishing is not the end + // of playback. Stop once every track that started has ended. + if tasks.is_empty() && (video_started || audio_started) { + return Ok(()); } - snapshot = catalogs.next(), if !catalog_ended && (!video_started || !audio_started) => { - let Some(snapshot) = snapshot.context("failed to read the catalog")? else { - anyhow::ensure!(video_started || audio_started, "the catalog contains no playable audio or video renditions"); - catalog_ended = true; - continue; - }; - - if !video_started { - for (name, config) in snapshot.video.renditions { - // A rendition pointing at a broadcast we can't reach is that - // rendition's problem, not the catalog's: fall through to the - // next one like an unsupported codec does. - let rendition = match source.resolve(config.broadcast.as_ref()).await { - Ok(rendition) => rendition, - Err(err) => { - tracing::warn!(track = name, %err, "cannot resolve video rendition"); - continue; - } - }; - let mut decode = moq_video::decode::Config::new(); - decode.latency_max = Some(args.latency_max); - match moq_video::decode::Consumer::new(&rendition, &config, &name, decode).await { - Ok(consumer) => { - tracing::info!(track = name, decoder = consumer.name(), "playing video rendition"); - tasks.spawn(play_video(consumer, video.clone(), proxy.clone())); - video_started = true; - break; + + tokio::select! { + result = tasks.join_next(), if !tasks.is_empty() => { + joined(result.expect("guarded by is_empty"))?; + } + snapshot = catalogs.next(), if !catalog_ended && (!video_started || !audio_started) => { + let Some(snapshot) = snapshot.context("failed to read the catalog")? else { + anyhow::ensure!(video_started || audio_started, "the catalog contains no playable audio or video renditions"); + catalog_ended = true; + continue; + }; + + // Why nothing started, so a catalog this build can't play reports the + // reason instead of leaving a blank window up forever. The decoders are + // gated by platform and cargo feature (no AV1 without nvdec, say), so + // this covers gaps the codec flags can't be validated against up front. + let mut rejected = Vec::new(); + + if !video_started { + for (name, config) in snapshot.video.renditions { + // A rendition pointing at a broadcast we can't reach is that + // rendition's problem, not the catalog's: fall through to the + // next one like an unsupported codec does. + let rendition = match source.resolve(config.broadcast.as_ref()).await { + Ok(rendition) => rendition, + Err(err) => { + tracing::warn!(track = name, %err, "cannot resolve video rendition"); + rejected.push(format!("video `{name}`: {err}")); + continue; + } + }; + let mut decode = moq_video::decode::Config::new(); + decode.latency_max = Some(self.args.latency_max); + match moq_video::decode::Consumer::new(&rendition, &config, &name, decode).await { + Ok(consumer) => { + tracing::info!(track = name, decoder = consumer.name(), "playing video rendition"); + tasks.spawn(play_video(consumer, self.video.clone(), self.proxy.clone())); + video_started = true; + break; + } + Err(err) => { + tracing::warn!(track = name, %err, "cannot play video rendition"); + rejected.push(format!("video `{name}`: {err}")); + } } - Err(err) => tracing::warn!(track = name, %err, "cannot play video rendition"), } } - } - if !audio_started { - for (name, config) in snapshot.audio.renditions { - let rendition = match source.resolve(config.broadcast.as_ref()).await { - Ok(rendition) => rendition, - Err(err) => { - tracing::warn!(track = name, %err, "cannot resolve audio rendition"); - continue; + if !audio_started { + for (name, config) in snapshot.audio.renditions { + let rendition = match source.resolve(config.broadcast.as_ref()).await { + Ok(rendition) => rendition, + Err(err) => { + tracing::warn!(track = name, %err, "cannot resolve audio rendition"); + rejected.push(format!("audio `{name}`: {err}")); + continue; + } + }; + let mut decode = moq_audio::decode::Config::new(); + decode.latency_max = Some(self.args.latency_max); + // The sink and the frame-duration math below both assume f32, + // so ask for it rather than inheriting the decoder default. + decode.format = moq_audio::Format::F32; + match moq_audio::decode::Consumer::new(&rendition, &config, &name, decode).await { + Ok(consumer) => { + tracing::info!(track = name, "playing audio rendition"); + tasks.spawn(play_audio(consumer, self.audio_clock.clone(), self.proxy.clone())); + audio_started = true; + break; + } + Err(err) => { + tracing::warn!(track = name, %err, "cannot play audio rendition"); + rejected.push(format!("audio `{name}`: {err}")); + } } - }; - let mut decode = moq_audio::decode::Config::new(); - decode.latency_max = Some(args.latency_max); - // The sink and the frame-duration math below both assume f32, - // so ask for it rather than inheriting the decoder default. - decode.format = moq_audio::Format::F32; - match moq_audio::decode::Consumer::new(&rendition, &config, &name, decode).await { - Ok(consumer) => { - tracing::info!(track = name, "playing audio rendition"); - tasks.spawn(play_audio(consumer, audio_clock.clone(), proxy.clone())); - audio_started = true; - break; - } - Err(err) => tracing::warn!(track = name, %err, "cannot play audio rendition"), } } + + // Renditions on offer and not one of them playable, with nothing + // already running to fall back on. + anyhow::ensure!( + !tasks.is_empty() || rejected.is_empty(), + "no playable rendition in the catalog: {}", + rejected.join("; ") + ); } } } @@ -321,6 +358,11 @@ async fn play_audio( })?; while let Some(frame) = consumer.read().await? { + // Let the speaker catch up before handing it more than it can hold. + if let Some(excess) = sink.buffered().checked_sub(AUDIO_BUFFER_MAX) { + tokio::time::sleep(excess).await; + } + let samples = frame.data.len() / size_of::() / channels as usize; let end = timestamp(frame.timestamp).saturating_add(Duration::from_secs_f64(samples as f64 / sample_rate as f64)); @@ -336,6 +378,18 @@ async fn play_audio( let _ = proxy.send_event(Event::Wake); } } + + // The track ended, but the speaker is still a buffer behind. Play it out + // instead of cutting the tail off by dropping the sink. + let drain = async { + // A partial period is left to the device: waiting on the last few + // milliseconds costs a wakeup per iteration and can never fully settle. + while let Some(remaining) = sink.buffered().checked_sub(Duration::from_millis(10)) { + tokio::time::sleep(remaining.max(Duration::from_millis(10))).await; + } + }; + let _ = tokio::time::timeout(AUDIO_DRAIN_MAX, drain).await; + Ok(()) } @@ -350,6 +404,9 @@ struct App { video_clock: Option, display: Option, next_redraw: Option, + /// The media tasks are done. Keep presenting whatever they left queued, then + /// stop; exiting the moment the decoder hits EOF would cut the tail off. + ending: bool, error: Option, } @@ -366,6 +423,7 @@ impl App { video_clock: None, display: None, next_redraw: None, + ending: false, error: None, } } @@ -442,6 +500,12 @@ impl ApplicationHandler for App { } } Event::Finished => event_loop.exit(), + Event::Ended => { + self.ending = true; + if let Some(display) = &self.display { + display.window.request_redraw(); + } + } Event::Failed(err) => self.fail(event_loop, err), } } @@ -481,6 +545,14 @@ impl ApplicationHandler for App { } fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { + // Nothing left to decode and nothing left to show. No window means nothing + // can drain the queue, so don't wait on it. + let drained = self.display.is_none() || self.video.lock().unwrap().is_empty(); + if self.ending && self.next_redraw.is_none() && drained { + event_loop.exit(); + return; + } + match self.next_redraw { Some(deadline) if deadline <= Instant::now() => { self.next_redraw = None; From 1f2ef05fb888db1cb7e0129a3e5f45345947f52f Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 6 Aug 2026 13:35:06 -0700 Subject: [PATCH 06/16] fix(cli): rebuild a lost play surface, and pin the announce race - `CurrentSurfaceTexture::Lost` closed the window with an error. A display change or a resume can produce it, and wgpu documents the recovery: rebuild the surface from the instance, then configure it. Keep the instance around so we can. `Validation` stays fatal. - The announce-wait from the previous commit had no regression test. Split it out as `subscribe` and cover it: without the wait, subscribing to an unannounced broadcast resolves `Unroutable` immediately (asserted directly, so the mechanism is pinned too); with it, the future parks until the broadcast is announced. Drop the wait and the test fails. Co-Authored-By: Claude Opus 5 --- rs/moq-cli/src/play.rs | 68 +++++++++++++++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 11 deletions(-) diff --git a/rs/moq-cli/src/play.rs b/rs/moq-cli/src/play.rs index fdedb14bfad..a29ad937b30 100644 --- a/rs/moq-cli/src/play.rs +++ b/rs/moq-cli/src/play.rs @@ -162,6 +162,22 @@ pub fn run( } } +/// Wait for `broadcast` to be announced on `origin`, then subscribe to it. +/// +/// The wait is the whole point. Subscribing goes through +/// `origin::Consumer::request_broadcast`, which resolves `Unroutable` on the +/// spot when no session has registered a handler yet rather than waiting for +/// one, and the media task starts well before the first handshake lands. The +/// window is already up, so this shows as a black frame rather than as a hang. +async fn subscribe(origin: moq_net::origin::Consumer, broadcast: &str) -> anyhow::Result { + origin + .announced_broadcast(broadcast) + .await + .with_context(|| format!("origin closed before broadcast `{broadcast}` was announced"))?; + + Ok(moq_mux::Source::new(origin, broadcast)) +} + async fn watch_network(mut tasks: tokio::task::JoinSet>, proxy: EventLoopProxy) { while let Some(result) = tasks.join_next().await { let event = match result { @@ -196,16 +212,7 @@ impl Media { } async fn play(self) -> anyhow::Result<()> { - // Wait for the announcement before resolving anything. A request against an - // origin no session has reached yet fails `Unroutable` on the spot, and this - // task starts well before the first handshake lands. The window is already - // up, so the wait shows as black rather than as a hang. - self.origin - .announced_broadcast(&self.broadcast) - .await - .with_context(|| format!("origin closed before broadcast `{}` was announced", self.broadcast))?; - - let source = moq_mux::Source::new(self.origin.clone(), &self.broadcast); + let source = subscribe(self.origin.clone(), &self.broadcast).await?; let broadcast = source .broadcast() .await @@ -568,6 +575,8 @@ impl ApplicationHandler for App { struct Display { window: Arc, + /// Kept past setup so a lost surface can be rebuilt from it. + instance: wgpu::Instance, surface: wgpu::Surface<'static>, device: wgpu::Device, queue: wgpu::Queue, @@ -610,6 +619,7 @@ impl Display { Ok(Self { window, + instance, surface, device, queue, @@ -645,7 +655,17 @@ impl Display { self.surface.configure(&self.device, &self.config); return Ok(()); } - CurrentSurfaceTexture::Lost => anyhow::bail!("the playback window's graphics surface was lost"), + // Not just a stale configuration like `Outdated`: the surface itself is + // gone and has to be rebuilt before it can be configured again. A + // display change or a resume can do this, so it isn't fatal. + CurrentSurfaceTexture::Lost => { + self.surface = self + .instance + .create_surface(self.window.clone()) + .context("failed to rebuild the playback window's graphics surface")?; + self.surface.configure(&self.device, &self.config); + return Ok(()); + } CurrentSurfaceTexture::Validation => anyhow::bail!("failed to acquire the playback window's next frame"), }; let target = output.texture.create_view(&Default::default()); @@ -832,6 +852,32 @@ fn fit(window: (u32, u32), video: (u32, u32)) -> (f32, f32, f32, f32) { mod tests { use super::*; + /// Subscribing before the announcement lands doesn't wait, it fails: with no + /// session yet there is no handler registered on the origin, and + /// `request_broadcast` resolves `Unroutable` immediately. The media task is + /// spawned right after the reconnect loop starts, so it gets there first. + #[tokio::test] + async fn subscribe_waits_for_the_announcement() { + tokio::time::pause(); + + let origin = moq_net::Origin::random().produce(); + let consumer = origin.consume(); + + // Resolving straight away, which is what the media task used to do. + let unannounced = moq_mux::Source::new(consumer.clone(), "room.hang").broadcast().await; + assert!(unannounced.is_err(), "expected an unroutable broadcast"); + + // Waiting first parks instead, for as long as it takes. + let mut waiting = std::pin::pin!(subscribe(consumer, "room.hang")); + let parked = tokio::time::timeout(Duration::from_secs(60), &mut waiting).await; + assert!(parked.is_err(), "expected to still be waiting on the announcement"); + + let _broadcast = origin + .create_broadcast("room.hang", moq_net::broadcast::Route::new().with_announce(true)) + .unwrap(); + waiting.await.unwrap(); + } + #[test] fn letterboxes_without_changing_aspect_ratio() { let assert_near = |actual: (f32, f32, f32, f32), expected: (f32, f32, f32, f32)| { From c224cda665acaf27527710cfbc8376e8d6b294f4 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 6 Aug 2026 13:37:13 -0700 Subject: [PATCH 07/16] fix(cli): chunk audio writes so a long PCM frame isn't truncated The pacing wait only looked at what was already buffered, so an empty sink accepted any single write whole. An Opus packet caps at 120ms, but a PCM one only has to be sample-aligned, so a frame longer than the sink's ring buffer had its tail dropped. The clock then advanced by the frame's full duration against a `buffered()` that never held those samples, putting video permanently ahead of audio. Write at most a second at a time, re-checking headroom between chunks, which keeps the sink under two seconds and inside its own ceiling. Chunks are cut on the sample stride, so alignment is unchanged. Co-Authored-By: Claude Opus 5 --- rs/moq-cli/src/play.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/rs/moq-cli/src/play.rs b/rs/moq-cli/src/play.rs index a29ad937b30..f93a33a74c3 100644 --- a/rs/moq-cli/src/play.rs +++ b/rs/moq-cli/src/play.rs @@ -364,16 +364,28 @@ async fn play_audio( channels, })?; - while let Some(frame) = consumer.read().await? { - // Let the speaker catch up before handing it more than it can hold. - if let Some(excess) = sink.buffered().checked_sub(AUDIO_BUFFER_MAX) { - tokio::time::sleep(excess).await; - } + // One sample across every channel, the unit a write has to stay aligned to. + let stride = channels as usize * size_of::(); + // A second per write, so a frame longer than the sink can hold is paced in + // rather than handed over whole and truncated. An Opus packet caps at 120 ms, + // but a PCM one is only required to be sample-aligned, so it can be any length. + // Paired with the wait below this keeps the sink under two seconds, inside its + // own ceiling. + let chunk = (sample_rate as usize * stride).max(stride); + while let Some(frame) = consumer.read().await? { let samples = frame.data.len() / size_of::() / channels as usize; let end = timestamp(frame.timestamp).saturating_add(Duration::from_secs_f64(samples as f64 / sample_rate as f64)); - sink.write(&frame.data)?; + + for part in frame.data.chunks(chunk) { + // Let the speaker catch up before handing it more than it can hold. + if let Some(excess) = sink.buffered().checked_sub(AUDIO_BUFFER_MAX) { + tokio::time::sleep(excess).await; + } + sink.write(part)?; + } + let previous = clock.lock().unwrap().replace(Clock { media: end.saturating_sub(sink.buffered()), wall: Instant::now(), From 55fafa5d5f15cfc836970867eb1272770a48dd92 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 6 Aug 2026 13:47:06 -0700 Subject: [PATCH 08/16] fix(cli): back-pressure video instead of dropping frames ahead of the clock Pacing audio to real time turned the video queue's overflow policy into a bug. Dropping the oldest frames was fine while audio was written as fast as it decoded, because the clock ran ahead and those frames were genuinely late. Now the clock advances at 1x, so a burst of more than 30 frames loses its earliest ones while their timestamps are still in the future, blanking the window until the clock reaches whatever survived. Make the decoder wait for room instead: the presenter signals each pop, and the presentation clock is anchored to the wall clock, so the queue always drains. Also request the redraw a rebuilt surface still owes. `Lost` (and `Outdated`) recover without presenting, and nothing else asks again: a stalled live stream has no next frame to trigger one, and an ending stream exits first. Bounded, since the retry is what schedules the next attempt; past the ceiling the frame is written off with a warning rather than failing playback. Co-Authored-By: Claude Opus 5 --- rs/moq-cli/src/play.rs | 94 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 77 insertions(+), 17 deletions(-) diff --git a/rs/moq-cli/src/play.rs b/rs/moq-cli/src/play.rs index f93a33a74c3..a3bfd42ed3b 100644 --- a/rs/moq-cli/src/play.rs +++ b/rs/moq-cli/src/play.rs @@ -18,9 +18,9 @@ use winit::window::{Window, WindowId}; use crate::subscribe::{CatalogFormatArg, SelectArgs}; -/// Decoded frames held for presentation. About a second at 30fps, which absorbs -/// a burst without letting a decoder that outruns the clock grow unbounded. The -/// oldest go first, since the render loop would skip past them anyway. +/// Decoded frames held for presentation, and the point at which the decoder is +/// made to wait. About a second at 30fps: enough to absorb a burst, few enough +/// that raw frames can't run away with memory. const MAX_VIDEO_FRAMES: usize = 30; /// How early a frame may be shown rather than waiting another wakeup for it. @@ -34,6 +34,11 @@ const VIDEO_EARLY_TOLERANCE: Duration = Duration::from_millis(2); /// under the sink's own ceiling, and well over the ~50 ms it settles at. const AUDIO_BUFFER_MAX: Duration = Duration::from_secs(1); +/// Consecutive surface rebuilds before a frame is written off. A display change +/// or a resume costs one; without a ceiling, a surface that can never present +/// would redraw forever, since each retry is what schedules the next. +const MAX_PRESENT_RETRIES: u32 = 8; + /// Give up waiting on the speaker to drain this long after the last sample. /// A device that never opens reports its queue as full forever, and a truncated /// tail beats hanging on the way out. @@ -122,6 +127,9 @@ pub fn run( let proxy = event_loop.create_proxy(); let video = Arc::new(Mutex::new(VecDeque::new())); let audio_clock = Arc::new(Mutex::new(None)); + // Signals the decoder that the presenter took a frame, so it can hand over + // the next one instead of dropping it. + let drained = Arc::new(tokio::sync::Notify::new()); let media = tokio::spawn( Media { @@ -130,6 +138,7 @@ pub fn run( args, video: video.clone(), audio_clock: audio_clock.clone(), + drained: drained.clone(), proxy: proxy.clone(), } .run(), @@ -149,7 +158,7 @@ pub fn run( } else { format!("moq play: {broadcast}") }; - let mut app = App::new(title, video, audio_clock); + let mut app = App::new(title, video, audio_clock, drained); let result = event_loop.run_app(&mut app).context("playback event loop failed"); media.abort(); network.abort(); @@ -198,6 +207,7 @@ struct Media { args: Args, video: Arc>>, audio_clock: Arc>>, + drained: Arc, proxy: EventLoopProxy, } @@ -270,7 +280,12 @@ impl Media { match moq_video::decode::Consumer::new(&rendition, &config, &name, decode).await { Ok(consumer) => { tracing::info!(track = name, decoder = consumer.name(), "playing video rendition"); - tasks.spawn(play_video(consumer, self.video.clone(), self.proxy.clone())); + tasks.spawn(play_video( + consumer, + self.video.clone(), + self.drained.clone(), + self.proxy.clone(), + )); video_started = true; break; } @@ -336,15 +351,20 @@ fn joined(result: Result, tokio::task::JoinError>) -> anyhow: async fn play_video( mut consumer: moq_video::decode::Consumer, video: Arc>>, + drained: Arc, proxy: EventLoopProxy, ) -> anyhow::Result<()> { while let Some(frame) = consumer.read().await? { - let mut video = video.lock().unwrap(); - video.push_back(frame); - while video.len() > MAX_VIDEO_FRAMES { - video.pop_front(); + // Wait for room rather than dropping the oldest. Audio is paced to real + // time, so during a catch-up burst the frames at the front are still ahead + // of the clock, and dropping them would blank the window until the clock + // reached whatever survived. The presentation clock is anchored to the wall + // clock, so the queue always drains and this always clears. + while video.lock().unwrap().len() >= MAX_VIDEO_FRAMES { + drained.notified().await; } - drop(video); + + video.lock().unwrap().push_back(frame); let _ = proxy.send_event(Event::Wake); } Ok(()) @@ -420,12 +440,15 @@ struct App { title: String, video: Arc>>, audio_clock: Arc>>, + drained: Arc, video_clock: Option, display: Option, next_redraw: Option, /// The media tasks are done. Keep presenting whatever they left queued, then /// stop; exiting the moment the decoder hits EOF would cut the tail off. ending: bool, + /// Consecutive presents that rebuilt the surface instead of showing a frame. + retries: u32, error: Option, } @@ -434,15 +457,18 @@ impl App { title: String, video: Arc>>, audio_clock: Arc>>, + drained: Arc, ) -> Self { Self { title, video, audio_clock, + drained, video_clock: None, display: None, next_redraw: None, ending: false, + retries: 0, error: None, } } @@ -472,12 +498,18 @@ impl App { due = video.pop_front(); } let next_timestamp = video.front().map(|frame| timestamp(frame.timestamp)); + let popped = due.is_some(); drop(video); + // Room in the queue, so the decoder can hand over whatever it held back. + if popped { + self.drained.notify_one(); + } + if let Some(frame) = due { display.render(&frame)?; } - display.present()?; + let presented = display.present()?; // `checked_add`: the wait comes from a wire timestamp, and adding a bogus // one to an `Instant` panics rather than saturating. No deadline just means @@ -486,6 +518,25 @@ impl App { (Some(clock), Some(next)) => Instant::now().checked_add(next.saturating_sub(clock.now())), _ => None, }; + + // A rebuilt surface still owes us the frame we just drew, and nothing else + // will ask for it: a stalled live stream has no next frame to trigger one, + // and an ending stream would exit first. Bounded, so a surface that can + // never present fails instead of looping. + match presented { + Presented::Shown => self.retries = 0, + // Giving up is a dropped frame, not a failure: the next one redraws. + // Retrying without a budget would spin, since each retry asks for the + // redraw that produces the next one. + Presented::Retry if self.retries >= MAX_PRESENT_RETRIES => { + tracing::warn!("gave up re-presenting after rebuilding the graphics surface"); + self.retries = 0; + } + Presented::Retry => { + self.retries += 1; + display.window.request_redraw(); + } + } Ok(()) } @@ -567,7 +618,7 @@ impl ApplicationHandler for App { // Nothing left to decode and nothing left to show. No window means nothing // can drain the queue, so don't wait on it. let drained = self.display.is_none() || self.video.lock().unwrap().is_empty(); - if self.ending && self.next_redraw.is_none() && drained { + if self.ending && self.retries == 0 && self.next_redraw.is_none() && drained { event_loop.exit(); return; } @@ -656,16 +707,17 @@ impl Display { Ok(()) } - fn present(&mut self) -> anyhow::Result<()> { + fn present(&mut self) -> anyhow::Result { use wgpu::CurrentSurfaceTexture; let (output, reconfigure) = match self.surface.get_current_texture() { CurrentSurfaceTexture::Success(output) => (output, false), CurrentSurfaceTexture::Suboptimal(output) => (output, true), - CurrentSurfaceTexture::Timeout | CurrentSurfaceTexture::Occluded => return Ok(()), + // Nothing to show it on, and nothing to fix: the next frame tries again. + CurrentSurfaceTexture::Timeout | CurrentSurfaceTexture::Occluded => return Ok(Presented::Shown), CurrentSurfaceTexture::Outdated => { self.surface.configure(&self.device, &self.config); - return Ok(()); + return Ok(Presented::Retry); } // Not just a stale configuration like `Outdated`: the surface itself is // gone and has to be rebuilt before it can be configured again. A @@ -676,7 +728,7 @@ impl Display { .create_surface(self.window.clone()) .context("failed to rebuild the playback window's graphics surface")?; self.surface.configure(&self.device, &self.config); - return Ok(()); + return Ok(Presented::Retry); } CurrentSurfaceTexture::Validation => anyhow::bail!("failed to acquire the playback window's next frame"), }; @@ -700,10 +752,18 @@ impl Display { if reconfigure { self.surface.configure(&self.device, &self.config); } - Ok(()) + Ok(Presented::Shown) } } +/// Whether a present reached the screen, or recovered the surface and still owes +/// the caller a redraw. +#[derive(Clone, Copy)] +enum Presented { + Shown, + Retry, +} + struct Presenter { pipeline: wgpu::RenderPipeline, layout: wgpu::BindGroupLayout, From 51623e7f79110f13548e50aa2e65dbbeed0f5840 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 6 Aug 2026 13:52:34 -0700 Subject: [PATCH 09/16] fix(cli): retry a present that timed out The last commit lumped `Timeout` in with `Occluded` and returned `Shown` for both, but they differ. A timeout means the swapchain was busy, not that there was nothing to draw on: the frame has already been popped and rendered, and nothing else will ask for it, so a stalled live stream stays blank and an ending one exits without its last frame. Route it through the same bounded retry as `Outdated` and `Lost`. `Occluded` still doesn't retry, since being shown again is itself a redraw. Co-Authored-By: Claude Opus 5 --- rs/moq-cli/src/play.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rs/moq-cli/src/play.rs b/rs/moq-cli/src/play.rs index a3bfd42ed3b..2e5a95eaa35 100644 --- a/rs/moq-cli/src/play.rs +++ b/rs/moq-cli/src/play.rs @@ -713,8 +713,12 @@ impl Display { let (output, reconfigure) = match self.surface.get_current_texture() { CurrentSurfaceTexture::Success(output) => (output, false), CurrentSurfaceTexture::Suboptimal(output) => (output, true), - // Nothing to show it on, and nothing to fix: the next frame tries again. - CurrentSurfaceTexture::Timeout | CurrentSurfaceTexture::Occluded => return Ok(Presented::Shown), + // The swapchain was busy, not broken. Ask again rather than waiting on a + // next frame that a stalled or ending stream may never produce. + CurrentSurfaceTexture::Timeout => return Ok(Presented::Retry), + // Nobody is looking, so there is nothing to retry for. Being shown again + // is itself a redraw. + CurrentSurfaceTexture::Occluded => return Ok(Presented::Shown), CurrentSurfaceTexture::Outdated => { self.surface.configure(&self.device, &self.config); return Ok(Presented::Retry); From 17c51d2bad67983f95dcefa0f9f38e97627d95c8 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 6 Aug 2026 15:51:47 -0700 Subject: [PATCH 10/16] fix(video, audio): decode the container the catalog declares `decode::Consumer` hardcoded `container::legacy::Wire` on both crates, so it read every track as a varint timestamp followed by a codec payload. `moq import fmp4` publishes `Container::Cmaf`, where each frame is a whole moof+mdat fragment, so the parse produced garbage rather than failing: the first frame reached the H.264 decoder as a malformed access unit and died with "annexb: truncated length-prefixed NAL unit". `catalog::hang::Container` already exists for exactly this, dispatching Legacy, Cmaf and Loc at runtime with a `TryFrom<&hang::catalog::Container>`, and the catalog entry is already a parameter here. Use it. This is why `moq play` could not play anything published through `import fmp4`, including the project's own bbb.mp4 demo. It hits moq-transcode's decode path the same way; only the legacy wire worked there too. Verified end to end rather than by a unit test, which would need a live broadcast and a platform decoder: a local relay, `import fmp4` from ffmpeg (H.264 + Opus), and `moq play` rendering through VideoToolbox with CoreAudio output for ~18s, no overflow or underflow, clean exit on window close. Co-Authored-By: Claude Opus 5 --- rs/moq-audio/src/decode/consumer.rs | 8 ++++++-- rs/moq-video/src/decode/consumer.rs | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/rs/moq-audio/src/decode/consumer.rs b/rs/moq-audio/src/decode/consumer.rs index 9191d5493ee..6c053b95f59 100644 --- a/rs/moq-audio/src/decode/consumer.rs +++ b/rs/moq-audio/src/decode/consumer.rs @@ -14,7 +14,7 @@ use crate::{Error, Frame}; /// [`read`](Self::read) returns plain [`Frame`]s. pub struct Consumer { decoder: Decoder, - track: moq_mux::container::Consumer, + track: moq_mux::container::Consumer, resampler: Option, config: Config, resolved_sample_rate: u32, @@ -52,7 +52,11 @@ impl Consumer { .track(&name)? .subscribe(moq_net::track::Subscription::default().with_priority(hang::catalog::PRIORITY.audio)) .await?; - let mut track = moq_mux::container::Consumer::new(track, moq_mux::container::legacy::Wire); + // The catalog says how the track is framed, and it is not always the legacy + // wire: `moq import fmp4` publishes CMAF. Reading a moof+mdat fragment as a + // varint timestamp plus a payload decodes to garbage rather than failing. + let container = moq_mux::catalog::hang::Container::try_from(&catalog.container)?; + let mut track = moq_mux::container::Consumer::new(track, container); if let Some(latency) = config.latency_max { track = track.with_latency(latency); } diff --git a/rs/moq-video/src/decode/consumer.rs b/rs/moq-video/src/decode/consumer.rs index 24be5d86058..322ec846c51 100644 --- a/rs/moq-video/src/decode/consumer.rs +++ b/rs/moq-video/src/decode/consumer.rs @@ -19,7 +19,7 @@ pub struct Consumer { /// so the codec would otherwise migrate between executor workers and /// unbalance the per-thread COM apartment the Windows backend opens. decoder: Sink, - track: moq_mux::container::Consumer, + track: moq_mux::container::Consumer, /// Frames a single access unit decoded to but `read` hasn't returned yet. /// One AU yields one frame in the low-delay path, but a backend may hand back /// more, so we buffer to keep `read` one-frame-per-call. @@ -42,7 +42,11 @@ impl Consumer { .track(&name)? .subscribe(moq_net::track::Subscription::default().with_priority(hang::catalog::PRIORITY.video)) .await?; - let mut track = moq_mux::container::Consumer::new(track, moq_mux::container::legacy::Wire); + // The catalog says how the track is framed, and it is not always the legacy + // wire: `moq import fmp4` publishes CMAF. Reading a moof+mdat fragment as a + // varint timestamp plus a payload decodes to garbage rather than failing. + let container = moq_mux::catalog::hang::Container::try_from(&catalog.container)?; + let mut track = moq_mux::container::Consumer::new(track, container); if let Some(latency) = config.latency_max { track = track.with_latency(latency); } From 38ae42982ba6a8297274f9db88b26b4883938814 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 6 Aug 2026 21:10:54 -0700 Subject: [PATCH 11/16] fix(rs): compile moq play in the Windows gate (#2712) Co-authored-by: Luke Curley Co-authored-by: Claude Opus 5 --- doc/bin/cli.md | 6 ++--- rs/CLAUDE.md | 2 +- rs/justfile | 6 ++++- rs/moq-audio/src/decode/consumer.rs | 40 +++++++++++++++++++++++++++++ rs/moq-audio/src/playback.rs | 5 +++- 5 files changed, 53 insertions(+), 6 deletions(-) diff --git a/doc/bin/cli.md b/doc/bin/cli.md index 47a0138200b..1cf38a11f49 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -162,9 +162,9 @@ moq --client-connect https://relay.example.com/anon --broadcast conference.hang ``` Video decoding prefers the platform hardware backend and falls back to the -built-in software H.264 decoder. Rendering uses Metal on macOS, D3D12 on -Windows, and Vulkan or OpenGL on Linux. Audio uses the system default output -device through CoreAudio, WASAPI, or ALSA. +built-in software H.264 decoder. Rendering picks the best graphics API the +machine offers: Metal on macOS, and Vulkan ahead of D3D12 or OpenGL elsewhere. +Audio uses the system default output device through CoreAudio, WASAPI, or ALSA. ### Publish a Video File diff --git a/rs/CLAUDE.md b/rs/CLAUDE.md index cc417a426d0..15c755b654c 100644 --- a/rs/CLAUDE.md +++ b/rs/CLAUDE.md @@ -169,7 +169,7 @@ Then `Config::load()?` (initializes tracing), build clients/servers via `.init() - **Local checks only compile the host's platform and target, and PR CI is Linux-only.** `#[cfg(target_os = "...")]` code for other platforms is invisible to them, and `cargo fmt` skips those modules too. Windows and Mac runners cost too much for a per-PR gate, so those platforms are manual: - - Windows (moq-video's Media Foundation and D3D11 backends): `just rs windows`, which must run ON Windows. You can't reproduce it elsewhere, since cross-compiling dies in openh264-sys2's vendored C++. + - Windows (moq-video's Media Foundation and D3D11 backends): `just rs windows`, which must run ON Windows. You can't reproduce it elsewhere, since cross-compiling dies in openh264-sys2's vendored C++. It names `moq-cli/play` explicitly, since that feature is what pulls in moq-video's wgpu renderer and moq-audio's cpal output; a default-feature build compiles neither. - macOS (moq-video's VideoToolbox and ScreenCaptureKit, moq-audio's system audio): `just rs macos`, which must run ON macOS. Scoped to moq-video + moq-audio, and needs `--all-features` because moq-audio's capture backend is off by default. - Linux: covered. `just rs ci` already runs `--all-features` in a dev shell carrying PipeWire and ALSA. VAAPI loads libva dynamically, so nvenc/nvdec/vaapi/pipewire all compile without libva installed. - wasm32 (moq-wasm): `just rs wasm`. The crate root is `#![cfg(target_arch = "wasm32")]`, so a host-target `cargo check --workspace` compiles it down to nothing and sees no errors at all. This one needs no special host (the Nix shell carries the target), so `just rs ci` and `just check-all` both run it, and `just check` adds it when the diff touches `rs/moq-wasm/`. It's a compile gate, distinct from the root `just wasm`, which builds the shippable `@moq/wasm` package. diff --git a/rs/justfile b/rs/justfile index 4c5c68b86f2..412ea00bd3a 100644 --- a/rs/justfile +++ b/rs/justfile @@ -142,10 +142,14 @@ fix-changed $FILES: # backend don't build on MSVC, while moq-video's Linux-only nvenc/nvdec defaults # are already no-ops off Linux. moq-gst is excluded because it links # GStreamer via pkg-config, which the Windows runner doesn't have. +# +# `moq-cli/play` is named explicitly because it's off by default and is what +# turns on moq-video's wgpu renderer and moq-audio's cpal output, neither of +# which any default-feature build compiles. # Compile the whole workspace on a Windows host. Must run ON Windows. windows *args: - cargo check --workspace --exclude moq-gst --all-targets {{ args }} + cargo check --workspace --exclude moq-gst --all-targets --features moq-cli/play {{ args }} # Runs the `#[cfg(target_os = "macos")]` code past the compiler: moq-video's # VideoToolbox encode/decode and its ScreenCaptureKit / AVFoundation capture, diff --git a/rs/moq-audio/src/decode/consumer.rs b/rs/moq-audio/src/decode/consumer.rs index 6c053b95f59..c85f8d2378b 100644 --- a/rs/moq-audio/src/decode/consumer.rs +++ b/rs/moq-audio/src/decode/consumer.rs @@ -168,4 +168,44 @@ mod tests { assert_eq!(pair[0], pair[1]); } } + + #[tokio::test] + async fn reads_the_container_the_catalog_declares() { + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let track = broadcast.create_track("audio", hang::container::track_info()).unwrap(); + let subscriber = broadcast.consume(); + + let mut catalog = hang::catalog::AudioConfig::new(hang::catalog::AudioCodec::Pcm, 48_000, 1); + catalog.container = hang::catalog::Container::Loc; + + let mut producer = moq_mux::container::Producer::new(track, moq_mux::catalog::hang::Container::Loc); + let mut consumer = Consumer::new( + &subscriber, + &catalog, + "audio", + Config { + format: Format::F32, + ..Config::new() + }, + ) + .await + .unwrap(); + + let samples = [0.25f32, -0.5, 0.75, -1.0]; + let payload: Vec = samples.iter().flat_map(|sample| sample.to_le_bytes()).collect(); + producer + .write(moq_mux::container::Frame { + timestamp: Timestamp::ZERO, + duration: None, + payload: payload.into(), + keyframe: true, + }) + .unwrap(); + + let frame = consumer.read().await.unwrap().expect("decoded frame"); + assert_eq!( + Format::F32.as_interleaved_f32(&frame.data, 1).unwrap().as_ref(), + samples + ); + } } diff --git a/rs/moq-audio/src/playback.rs b/rs/moq-audio/src/playback.rs index 5a3dbb9746b..4e3a45d8801 100644 --- a/rs/moq-audio/src/playback.rs +++ b/rs/moq-audio/src/playback.rs @@ -1,6 +1,9 @@ //! Play decoded PCM out a speaker, via [`cpal`] (CoreAudio / WASAPI / ALSA). //! -//! The playback counterpart to [`capture`](crate::capture), and where a +//! The playback counterpart to `capture`, and where a +// `capture` is deliberately not a doc link: the module is behind the +// off-by-default `capture` feature, so a link to it fails the `-D warnings` +// rustdoc build of a playback-only build (what `moq-cli`'s `play` enables). //! [`decode::Consumer`](crate::decode::Consumer) ends up: //! //! ```no_run From ad6643a1df5e90c43e62acfdc6816c072032653b Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 6 Aug 2026 21:22:52 -0700 Subject: [PATCH 12/16] test(moq-audio): pin CMAF framing in the decode consumer The container fix had no test of its own: I verified it by playing a live broadcast, which is not something CI can do. This covers it at the level the bug lived at, publishing one real Opus packet through the CMAF wire and decoding it back through `decode::Consumer`. The assertion is the decoded sample count, not just "some PCM came out". Reading a moof+mdat as legacy framing hands the codec a slice of the moof, which Opus still decodes, just to a shorter buffer: 336 samples instead of 1296. An emptiness check passes either way, so it would not have caught this. Complements the LOC test alongside it: that one covers a different wire, and CMAF is the one `moq import fmp4` actually publishes. No equivalent for moq-video. Its decode consumer opens a platform backend and would need a real encoded access unit to drive, where Opus is pure Rust and this crate can encode its own input. The two crates took the identical change, so this pins the behavior; the video side stays covered by the end-to-end run. Co-Authored-By: Claude Opus 5 --- rs/moq-audio/src/decode/consumer.rs | 54 +++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/rs/moq-audio/src/decode/consumer.rs b/rs/moq-audio/src/decode/consumer.rs index c85f8d2378b..75b90b925af 100644 --- a/rs/moq-audio/src/decode/consumer.rs +++ b/rs/moq-audio/src/decode/consumer.rs @@ -208,4 +208,58 @@ mod tests { samples ); } + + /// The catalog picks the framing, not this crate. Hardcoding the legacy wire + /// read a CMAF fragment as a varint timestamp plus a payload, which handed the + /// codec garbage instead of failing, so anything published by `moq import + /// fmp4` was undecodable. + #[tokio::test] + async fn decodes_a_cmaf_framed_track() { + let input = Input { + format: Format::F32, + sample_rate: 48_000, + channels: 2, + }; + + // One real Opus packet, so a mis-framed read can't accidentally decode. + let mut encoder = Encoder::new(&crate::encode::Config::new(input.clone())).unwrap(); + let mut catalog = encoder.catalog(); + let pcm = vec![0.0f32; encoder.frame_size() * encoder.codec_channels() as usize]; + let packet = encoder.encode(&pcm).unwrap(); + + // Re-describe the same rendition as CMAF and publish it that way. + let muxer = moq_mux::container::fmp4::Muxer::audio(&catalog).unwrap(); + let init = muxer.init().unwrap().expect("an out-of-band codec has an init segment"); + catalog.container = hang::catalog::Container::Cmaf { init }; + + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let subscriber = broadcast.consume(); + let track = broadcast.create_track("audio", hang::container::track_info()).unwrap(); + let container = moq_mux::catalog::hang::Container::try_from(&catalog.container).unwrap(); + let mut producer = moq_mux::container::Producer::new(track, container); + + let mut consumer = Consumer::new(&subscriber, &catalog, "audio", Config::new()) + .await + .unwrap(); + + producer + .write(moq_mux::container::Frame { + timestamp: Timestamp::ZERO, + payload: packet, + keyframe: true, + duration: None, + }) + .unwrap(); + producer.cut(None).unwrap(); + + // The whole packet decodes: one 20 ms Opus frame at 48 kHz, less the pre-skip + // trimmed off the first packet. Reading the fragment as legacy hands the codec + // a slice of the moof instead, which still decodes, just to a shorter buffer. + let frame = consumer.read().await.unwrap().expect("decoded frame"); + // `as_micros`, not `==`: the CMAF path carries the fmp4 timescale and + // `Timestamp`'s equality is structural, so the scales would have to match too. + assert_eq!(frame.timestamp.as_micros(), 0); + let samples = Format::F32.as_interleaved_f32(&frame.data, 2).unwrap(); + assert_eq!(samples.len(), (960 - 312) * 2); + } } From e03495cde60909f218006da331bf12d32d1d4359 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Thu, 6 Aug 2026 21:22:52 -0700 Subject: [PATCH 13/16] chore(py): relock after the moq-rs version bump `py/moq-rs/pyproject.toml` went to 0.4.2 in the version bump, but `uv.lock` still pinned 0.4.1, so a locked sync disagrees with the workspace. Co-Authored-By: Claude Opus 5 --- uv.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uv.lock b/uv.lock index 3c3a8dfc788..e3cc3563451 100644 --- a/uv.lock +++ b/uv.lock @@ -103,7 +103,7 @@ source = { editable = "py/moq-ffi" } [[package]] name = "moq-rs" -version = "0.4.1" +version = "0.4.2" source = { editable = "py/moq-rs" } dependencies = [ { name = "moq-ffi" }, From b9456d0f8b0d4767021ab4230baa8b6c2fb303d7 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 7 Aug 2026 08:19:43 -0700 Subject: [PATCH 14/16] docs(cli): list the ALSA headers a Linux play build needs `play` enables `moq-audio/playback`, which is cpal, which links ALSA on Linux. The dependency note listed only the libclang and V4L2 packages, so following it on a clean Debian box still failed to build. Co-Authored-By: Claude Opus 5 --- doc/bin/cli.md | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/doc/bin/cli.md b/doc/bin/cli.md index 1cf38a11f49..d141e214bf6 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -127,11 +127,19 @@ cargo run -p moq-cli --features play -- \ Drop the defaults on Linux, as above: the default `pipewire` feature links libpipewire-0.3 at build time to give `import capture` a display source that -playback never uses. That still leaves one build dependency a playback-only box -would rather not have. `play` pulls in moq-video, whose V4L2 camera capture is -not behind a feature, so a Linux build needs libclang and the V4L2 headers -(`libclang-dev` and `libv4l-dev` on Debian) for bindgen either way. macOS and -Windows need nothing extra, so plain `--features play` is enough there. +playback never uses. + +A Linux build still needs three system packages, whichever features are picked. +The speaker goes through cpal, which links ALSA, and `play` pulls in moq-video, +whose V4L2 camera capture is not behind a feature and whose bindgen wants +libclang. On Debian and Ubuntu: + +```bash +sudo apt install libasound2-dev libclang-dev libv4l-dev +``` + +macOS and Windows need nothing extra, so plain `--features play` is enough +there. Once built, play a broadcast with: From 00081e8f14cff02ccd4ebe7cd66f30ae8c43eb19 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 7 Aug 2026 09:31:09 -0700 Subject: [PATCH 15/16] docs(cli): drop defaults in the checkout play commands too Only the `cargo install` line carried `--no-default-features`, so the build and run commands right below it still pulled the default `pipewire` feature and wanted libpipewire on Linux. All three now match. Co-Authored-By: Claude Opus 5 --- doc/bin/cli.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/bin/cli.md b/doc/bin/cli.md index d141e214bf6..274d9366c85 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -119,9 +119,9 @@ window, graphics, decoder, and speaker dependencies: ```bash cargo install moq-cli --no-default-features --features "iroh,quinn,websocket,play" # or build from a checkout: -cargo build --release -p moq-cli --features play +cargo build --release -p moq-cli --no-default-features --features "iroh,quinn,websocket,play" # or run straight from that checkout: -cargo run -p moq-cli --features play -- \ +cargo run -p moq-cli --no-default-features --features "iroh,quinn,websocket,play" -- \ --client-connect https://relay.example.com/anon --broadcast my-stream.hang play ``` From bcded7211d583d4f9c1c8830631c0502edda2723 Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Fri, 7 Aug 2026 12:17:31 -0700 Subject: [PATCH 16/16] test(media): cover CMAF consumer playback --- doc/bin/cli.md | 3 +- rs/moq-audio/src/decode/consumer.rs | 71 +++++++++++++++++++++++++++++ rs/moq-video/src/decode/consumer.rs | 68 +++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 1 deletion(-) diff --git a/doc/bin/cli.md b/doc/bin/cli.md index 1cf38a11f49..6faf63a854e 100644 --- a/doc/bin/cli.md +++ b/doc/bin/cli.md @@ -130,7 +130,8 @@ libpipewire-0.3 at build time to give `import capture` a display source that playback never uses. That still leaves one build dependency a playback-only box would rather not have. `play` pulls in moq-video, whose V4L2 camera capture is not behind a feature, so a Linux build needs libclang and the V4L2 headers -(`libclang-dev` and `libv4l-dev` on Debian) for bindgen either way. macOS and +(`libclang-dev` and `libv4l-dev` on Debian) for bindgen either way. Audio +playback also needs the ALSA headers (`libasound2-dev` on Debian). macOS and Windows need nothing extra, so plain `--features play` is enough there. Once built, play a broadcast with: diff --git a/rs/moq-audio/src/decode/consumer.rs b/rs/moq-audio/src/decode/consumer.rs index c85f8d2378b..d673d645712 100644 --- a/rs/moq-audio/src/decode/consumer.rs +++ b/rs/moq-audio/src/decode/consumer.rs @@ -208,4 +208,75 @@ mod tests { samples ); } + + #[tokio::test] + async fn reads_cmaf_container_declared_by_catalog() { + let mut source_broadcast = moq_net::broadcast::Info::new().produce(); + let source_subscriber = source_broadcast.consume(); + let source_catalog = moq_mux::catalog::Producer::new(&mut source_broadcast).unwrap(); + let input = Input { + format: Format::F32, + sample_rate: 48_000, + channels: 1, + }; + let options = Options { + track: Some("audio".to_string()), + ..Options::default() + }; + let mut producer = Producer::new(&mut source_broadcast, source_catalog, input, &options).unwrap(); + + let samples = vec![0.25f32; 960]; + let mut data = Vec::with_capacity(samples.len() * size_of::()); + for sample in samples { + data.extend_from_slice(&sample.to_le_bytes()); + } + producer + .write(&Frame { + timestamp: Timestamp::ZERO, + data: data.into(), + }) + .unwrap(); + + let origin = moq_net::Origin::random().produce(); + let mut requests = origin.dynamic(); + let served = source_subscriber.clone(); + tokio::spawn(async move { + while let Ok(request) = requests.requested_broadcast().await { + request.accept(served.clone()); + } + }); + let catalog = moq_mux::catalog::Consumer::<()>::new(&source_subscriber, moq_mux::catalog::CatalogFormat::Hang) + .await + .unwrap(); + let source = moq_mux::Source::new(origin.consume(), "test"); + let mut export = moq_mux::container::fmp4::Export::new(source, catalog); + let init = export.next().await.unwrap().expect("CMAF init"); + let fragment = export.next().await.unwrap().expect("CMAF fragment"); + + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let subscriber = broadcast.consume(); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast).unwrap(); + let mut import = moq_mux::container::fmp4::Import::new(broadcast, catalog.reserve()); + import.decode(&init).unwrap(); + import.decode(&fragment).unwrap(); + + let snapshot = catalog.snapshot(); + let (name, config) = snapshot.audio.renditions.iter().next().expect("audio rendition"); + assert!(matches!(config.container, hang::catalog::Container::Cmaf { .. })); + let mut consumer = Consumer::new( + &subscriber, + config, + name, + Config { + format: Format::F32, + ..Config::new() + }, + ) + .await + .unwrap(); + + let frame = consumer.read().await.unwrap().expect("decoded frame"); + assert_eq!(frame.timestamp.as_micros(), 0); + assert!(!frame.data.is_empty()); + } } diff --git a/rs/moq-video/src/decode/consumer.rs b/rs/moq-video/src/decode/consumer.rs index 322ec846c51..e9e17174406 100644 --- a/rs/moq-video/src/decode/consumer.rs +++ b/rs/moq-video/src/decode/consumer.rs @@ -82,3 +82,71 @@ impl Consumer { } } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::decode::Kind; + use crate::encode::{Codec, Config as EncodeConfig, Encoder, Kind as EncodeKind, Producer as EncodeProducer}; + + #[tokio::test] + async fn reads_cmaf_container_declared_by_catalog() { + let mut source_broadcast = moq_net::broadcast::Info::new().produce(); + let source_subscriber = source_broadcast.consume(); + let source_catalog = moq_mux::catalog::Producer::new(&mut source_broadcast).unwrap(); + let mut producer = EncodeProducer::new(source_broadcast, source_catalog, Codec::H264).unwrap(); + let mut encoder = Encoder::new(&EncodeConfig { + kind: EncodeKind::Software, + ..EncodeConfig::new(320, 240, 30) + }) + .unwrap(); + let rgba = vec![0x80u8; 320 * 240 * 4]; + for index in 0..2 { + encoder.keyframe(); + let surface = crate::Surface::rgba(&rgba, crate::Size::new(320, 240)).unwrap(); + let frame = crate::Frame::new(surface, moq_net::Timestamp::from_micros(index * 33_333).unwrap()); + producer.publish(&encoder.encode(&frame).unwrap()).unwrap(); + } + + let origin = moq_net::Origin::random().produce(); + let mut requests = origin.dynamic(); + let served = source_subscriber.clone(); + tokio::spawn(async move { + while let Ok(request) = requests.requested_broadcast().await { + request.accept(served.clone()); + } + }); + let catalog = moq_mux::catalog::Consumer::<()>::new(&source_subscriber, moq_mux::catalog::CatalogFormat::Hang) + .await + .unwrap(); + let source = moq_mux::Source::new(origin.consume(), "test"); + let mut export = moq_mux::container::fmp4::Export::new(source, catalog); + let init = export.next().await.unwrap().expect("CMAF init"); + let fragment = export.next().await.unwrap().expect("CMAF fragment"); + + let mut broadcast = moq_net::broadcast::Info::new().produce(); + let subscriber = broadcast.consume(); + let catalog = moq_mux::catalog::Producer::new(&mut broadcast).unwrap(); + let mut import = moq_mux::container::fmp4::Import::new(broadcast, catalog.reserve()); + import.decode(&init).unwrap(); + import.decode(&fragment).unwrap(); + + let snapshot = catalog.snapshot(); + let (name, config) = snapshot.video.renditions.iter().next().expect("video rendition"); + assert!(matches!(config.container, hang::catalog::Container::Cmaf { .. })); + let mut consumer = Consumer::new( + &subscriber, + config, + name, + Config { + kind: Kind::Software, + ..Config::new() + }, + ) + .await + .unwrap(); + + let frame = consumer.read().await.unwrap().expect("decoded frame"); + assert_eq!(frame.size(), crate::Size::new(320, 240)); + } +}