diff --git a/Cargo.lock b/Cargo.lock
index b93e8cea..3df86821 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -123,6 +123,17 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
+[[package]]
+name = "aes"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fc0fc36d7440d015b45468ba08a21e80e056ebbd014c611c6298bb7503e04bd1"
+dependencies = [
+ "cipher",
+ "cpubits",
+ "cpufeatures",
+]
+
[[package]]
name = "ahash"
version = "0.8.12"
@@ -333,6 +344,17 @@ dependencies = [
"serde",
]
+[[package]]
+name = "apple-native-keyring-store"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2b350bfd03649e07aa05c0a81b3e15934374e585c98204a57e20b9d49f49bb9a"
+dependencies = [
+ "keyring-core",
+ "log",
+ "security-framework",
+]
+
[[package]]
name = "arboard"
version = "3.7.0"
@@ -349,7 +371,7 @@ dependencies = [
"objc2-foundation 0.3.2",
"parking_lot",
"percent-encoding",
- "windows-sys 0.52.0",
+ "windows-sys 0.61.2",
"x11rb 0.13.2",
]
@@ -941,6 +963,15 @@ dependencies = [
"hybrid-array",
]
+[[package]]
+name = "block-padding"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "710f1dd022ef4e93f8a438b4ba958de7f64308434fa6a87104481645cc30068b"
+dependencies = [
+ "hybrid-array",
+]
+
[[package]]
name = "block2"
version = "0.5.1"
@@ -1135,6 +1166,15 @@ dependencies = [
"wayland-client",
]
+[[package]]
+name = "cbc"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ce2dc9ee5f88d11e0beb842c88b33c8a5cf0d1329c4b19494af42b07dbfe8896"
+dependencies = [
+ "cipher",
+]
+
[[package]]
name = "cc"
version = "1.11.1"
@@ -1244,6 +1284,16 @@ dependencies = [
"windows-link",
]
+[[package]]
+name = "cipher"
+version = "0.5.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c"
+dependencies = [
+ "crypto-common",
+ "inout",
+]
+
[[package]]
name = "clang-sys"
version = "1.9.1"
@@ -1313,6 +1363,12 @@ dependencies = [
"cc",
]
+[[package]]
+name = "cmov"
+version = "0.5.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a"
+
[[package]]
name = "cobs"
version = "0.3.0"
@@ -1567,6 +1623,12 @@ dependencies = [
"windows-core",
]
+[[package]]
+name = "cpubits"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "15b85f9c39137c3a891689859392b1bd49812121d0d61c9caf00d46ed5ce06ae"
+
[[package]]
name = "cpufeatures"
version = "0.3.1"
@@ -1685,6 +1747,15 @@ dependencies = [
"syn 3.0.6",
]
+[[package]]
+name = "ctutils"
+version = "0.4.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f"
+dependencies = [
+ "cmov",
+]
+
[[package]]
name = "cudarc"
version = "0.19.10"
@@ -1937,27 +2008,7 @@ dependencies = [
"block-buffer",
"const-oid 0.10.2",
"crypto-common",
-]
-
-[[package]]
-name = "dirs"
-version = "7.0.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8d57d423b3c82e89b9a24ca3091fee61f456a26edbd28d26c65906f4bc1dcd8f"
-dependencies = [
- "dirs-sys",
-]
-
-[[package]]
-name = "dirs-sys"
-version = "0.5.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab"
-dependencies = [
- "libc",
- "option-ext",
- "redox_users",
- "windows-sys 0.61.2",
+ "ctutils",
]
[[package]]
@@ -2438,7 +2489,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
- "windows-sys 0.52.0",
+ "windows-sys 0.61.2",
]
[[package]]
@@ -3180,6 +3231,24 @@ version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5491a308e0214554f07a81d8944abe45f552871c12e3c3c6e7e5d354039a6c4c"
+[[package]]
+name = "hkdf"
+version = "0.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018"
+dependencies = [
+ "hmac",
+]
+
+[[package]]
+name = "hmac"
+version = "0.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f"
+dependencies = [
+ "digest",
+]
+
[[package]]
name = "html-escape"
version = "0.2.15"
@@ -3662,6 +3731,16 @@ dependencies = [
"libc",
]
+[[package]]
+name = "inout"
+version = "0.2.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3a580c13f0ffc335ef42dd3f6b1a41b726051c5e1656d9a3aa6fddb3c5dfbbf9"
+dependencies = [
+ "block-padding",
+ "hybrid-array",
+]
+
[[package]]
name = "inventory"
version = "0.3.25"
@@ -3887,6 +3966,15 @@ dependencies = [
"serde",
]
+[[package]]
+name = "keyring-core"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "29fbf4b551298c67925f38cf341a1ada797a75f1837bdf8ae1fc8e2ebc6efb59"
+dependencies = [
+ "log",
+]
+
[[package]]
name = "khronos-egl"
version = "6.0.0"
@@ -4722,9 +4810,9 @@ name = "moq-studio"
version = "0.1.0"
dependencies = [
"anyhow",
+ "apple-native-keyring-store",
"ashpd",
"bytes",
- "dirs",
"eframe",
"egui",
"egui_extras",
@@ -4733,7 +4821,9 @@ dependencies = [
"global-hotkey",
"hang",
"humantime",
+ "humantime-serde",
"image",
+ "keyring-core",
"kio",
"moq-audio",
"moq-auth",
@@ -4749,10 +4839,12 @@ dependencies = [
"mp4-atom",
"objc2-app-kit 0.3.2",
"pipewire",
+ "png 0.18.1",
"pollster 1.0.1",
"qrcodegen",
"raw-window-handle",
"resvg 0.50.0",
+ "rfd",
"rune-fx",
"rustix 1.1.6",
"same-file",
@@ -4764,9 +4856,11 @@ dependencies = [
"url",
"usage-rs",
"wgpu",
+ "windows-native-keyring-store",
"windows-sys 0.61.2",
"winit",
"x11rb 0.13.2",
+ "zbus-secret-service-keyring-store",
]
[[package]]
@@ -5152,7 +5246,7 @@ version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [
- "windows-sys 0.59.0",
+ "windows-sys 0.61.2",
]
[[package]]
@@ -5325,10 +5419,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d49e936b501e5c5bf01fda3a9452ff86dc3ea98ad5f283e1455153142d97518c"
dependencies = [
"bitflags 2.13.2",
+ "block2 0.6.2",
"objc2 0.6.5",
"objc2-core-foundation",
"objc2-core-graphics",
"objc2-foundation 0.3.2",
+ "objc2-uniform-type-identifiers 0.3.2",
]
[[package]]
@@ -5847,12 +5943,6 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
-[[package]]
-name = "option-ext"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
-
[[package]]
name = "orbclient"
version = "0.3.55"
@@ -6678,16 +6768,6 @@ dependencies = [
"bitflags 2.13.2",
]
-[[package]]
-name = "redox_users"
-version = "0.5.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "60dc65c0ff1a7ae1294b0c67b9f14baf70b644404010370171787bfac1038fc0"
-dependencies = [
- "libredox",
- "thiserror 2.0.21",
-]
-
[[package]]
name = "ref-cast"
version = "1.0.27"
@@ -6841,6 +6921,34 @@ dependencies = [
"mpeg4-audio-const",
]
+[[package]]
+name = "rfd"
+version = "0.18.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c4b9d4857b10297ffb0f64cae711e983a7aeaee561848d56d429d3858bd8ed93"
+dependencies = [
+ "block2 0.6.2",
+ "dispatch2",
+ "js-sys",
+ "libc",
+ "log",
+ "objc2 0.6.5",
+ "objc2-app-kit 0.3.2",
+ "objc2-core-foundation",
+ "objc2-foundation 0.3.2",
+ "objc2-uniform-type-identifiers 0.3.2",
+ "percent-encoding",
+ "pollster 1.0.1",
+ "raw-window-handle",
+ "wasm-bindgen",
+ "wasm-bindgen-futures",
+ "wayland-backend",
+ "wayland-client",
+ "wayland-protocols",
+ "web-sys",
+ "windows-sys 0.61.2",
+]
+
[[package]]
name = "rgb"
version = "0.8.53"
@@ -7061,7 +7169,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.12.1",
- "windows-sys 0.52.0",
+ "windows-sys 0.61.2",
]
[[package]]
@@ -7118,7 +7226,7 @@ dependencies = [
"security-framework",
"security-framework-sys",
"webpki-root-certs",
- "windows-sys 0.52.0",
+ "windows-sys 0.61.2",
]
[[package]]
@@ -7252,6 +7360,25 @@ dependencies = [
"libc",
]
+[[package]]
+name = "secret-service"
+version = "5.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5107b24b91445dd2aa449a258a1807b63240942157292354dc5bfdbeb8bc6db8"
+dependencies = [
+ "aes",
+ "cbc",
+ "futures-util",
+ "getrandom 0.4.3",
+ "hkdf",
+ "hybrid-array",
+ "num",
+ "once_cell",
+ "serde",
+ "sha2",
+ "zbus",
+]
+
[[package]]
name = "security-framework"
version = "3.7.0"
@@ -7469,6 +7596,17 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d"
+[[package]]
+name = "sha2"
+version = "0.11.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19"
+dependencies = [
+ "cfg-if",
+ "cpufeatures",
+ "digest",
+]
+
[[package]]
name = "sharded-slab"
version = "0.1.7"
@@ -8270,7 +8408,7 @@ dependencies = [
"getrandom 0.4.3",
"once_cell",
"rustix 1.1.6",
- "windows-sys 0.52.0",
+ "windows-sys 0.61.2",
]
[[package]]
@@ -9810,7 +9948,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
- "windows-sys 0.52.0",
+ "windows-sys 0.61.2",
]
[[package]]
@@ -9901,6 +10039,18 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
+[[package]]
+name = "windows-native-keyring-store"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "063426e76fdec7438d56bb777f67e318a84a25c707b07e575cb8b78e10c028f8"
+dependencies = [
+ "byteorder",
+ "keyring-core",
+ "windows-sys 0.61.2",
+ "zeroize",
+]
+
[[package]]
name = "windows-numerics"
version = "0.3.1"
@@ -10338,6 +10488,17 @@ dependencies = [
"zvariant",
]
+[[package]]
+name = "zbus-secret-service-keyring-store"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "74801d001b9e7729adb4f1825b67b398185fed424749aa3d8bacf70417137d9a"
+dependencies = [
+ "keyring-core",
+ "secret-service",
+ "zbus",
+]
+
[[package]]
name = "zbus_macros"
version = "5.19.0"
diff --git a/README.md b/README.md
index 493257a7..4a415841 100644
--- a/README.md
+++ b/README.md
@@ -10,11 +10,11 @@ Flatpak bundle for x86_64 and aarch64:
```bash
flatpak install --user moq-studio-x86_64.flatpak
-flatpak run live.kixel.studio show.toml --connect https://cdn.moq.dev/anon --broadcast my-show
+flatpak run live.kixel.studio
```
-Its menu entry opens the scene collection kept in the app's own data
-directory and saves recordings to your Videos folder. The sandbox reaches
+Its menu entry opens [your shows](#your-shows) and saves recordings to your
+Videos folder. The sandbox reaches
cameras through the camera portal, which asks once, and files under your home
directory. The virtual camera is its PipeWire node only, since the sandbox
has no `/dev/video*`.
@@ -22,9 +22,8 @@ has no `/dev/video*`.
On macOS 13 or later, each release ships the app for Apple silicon
(`moq-studio-macos-arm64.dmg`) and Intel (`moq-studio-macos-x86_64.dmg`),
signed and notarized. Open the disk image and drag **kixel.live Studio** to
-Applications. Opened from Finder or the Dock, it opens the scene collection
-in `~/Library/Application Support/moq-studio` and saves recordings to your
-Movies folder. macOS asks you to grant it the camera, microphone, and Screen &
+Applications. Opened from Finder or the Dock, it opens
+[your shows](#your-shows). macOS asks you to grant it the camera, microphone, and Screen &
System Audio Recording permissions, the last in System Settings > Privacy &
Security. Run it from a terminal with the flags below as
`"/Applications/kixel.live Studio.app/Contents/MacOS/moq-studio"`.
@@ -35,32 +34,38 @@ submodule:
```bash
git clone --recurse-submodules https://github.com/moq-dev/studio
cd studio
-cargo run --release -p moq-studio --features capture,openh264 -- \
- show.toml --connect https://cdn.moq.dev/anon --broadcast my-show
+cargo run --release -p moq-studio --features capture,openh264
```
-- `show.toml` is the scene collection. If it does not exist yet,
- [guided setup](#first-launch) proposes one and creates it. Changes are saved
- to it a second after you stop editing, with no save step. Without one, the
- studio opens `scenes.toml` in its data directory, as the macOS app does.
-- `--recordings
` is where recordings and clips are saved, by default
- beside `show.toml`, or in your videos folder when no scene file is named.
-- Without `--connect`, the studio composes, previews, and records but
- publishes nothing, and broadcast sources stay offline.
-- `--failover ` publishes the same broadcast to a second relay under the
+- With no arguments, the window opens on [your shows](#your-shows). Name a
+ show to open it at once: `moq-studio show.toml`. A show is its scene file;
+ if it does not exist yet, [guided setup](#first-launch) proposes one and
+ creates it. Changes are saved to it a second after you stop editing, with
+ no save step.
+- Nothing is published until you [go live](#going-live) from the window, and
+ you end the stream from it too. Until then the studio composes, previews,
+ and records, and broadcast sources and guests play through the show's
+ relays.
+- Where a show streams is in Settings, Stream (see
+ [Stream settings](#stream-settings)), kept beside the show. A flag sets one
+ for a single run, over the show's own, and is never saved: `--connect` (the
+ relay) with `--connect-token`, `--failover`, `--broadcast`, `--on-demand`,
+ `--recordings `, where recordings and clips go (by default beside the
+ show), and the guest flags.
+- A failover relay publishes the same broadcast to a second relay under the
same broadcast instance, so a viewer linked to both resumes mid-group from
it when the first fails. Both relays must speak the one `--connect-version`,
which must carry instances (`moq-lite-07-wip` today, which a relay
- accepts only when configured to), or the studio refuses to start; it also
+ accepts only when configured to), or going live with both refuses; it also
refuses a fixed `--connect-bind` port, since each relay's connection binds
- its own. Its credential rides its URL's `?jwt=`; `--connect-token` goes to
- the first relay only. The stream's tracks claim their bitrate on the first
+ its own. Each relay's token is its own: `--connect-token` goes to the
+ `--connect` relay only. The stream's tracks claim their bitrate on the first
relay's link alone, so only that link is probed toward the stream's rate,
and the video follows that link's estimate alone.
-- Quitting while live ends every track after its last pictures and sound,
- then closes each relay session once the relay has them, waiting up to about
- a second, so viewers and recordings on the relay see the broadcast end
- rather than cut off.
+- Quitting while live ends the stream as **END STREAM** does: every track
+ ends after its last pictures and sound, then each relay session closes once
+ the relay has them, waiting up to about a second, so viewers and recordings
+ on the relay see the broadcast end rather than cut off.
- `capture` adds camera, display, window, microphone, and system-audio
sources, and listening to an audio scene. On Linux it links ALSA; add `pipewire` for
PipeWire cameras and the [virtual camera](#virtual-camera)'s PipeWire node.
@@ -85,10 +90,21 @@ cargo run --release -p moq-studio --features capture,openh264 -- \
- `moq-studio --watch ` opens no scene file and plays a studio broadcast
instead, as a viewer sees it; see [Watching](#watching).
+## Your shows
+
+Opened with no show, the window lists the shows you have opened, the latest
+first, each with the last picture of its program, its shape, and when it was
+last live and for how long. Click one to pick up where it ended; nothing goes
+live until you say so. **New show** asks where to keep a new show's file and
+starts [guided setup](#first-launch) there, and **Open a show file** opens
+any other. Right-click a show to forget it; one whose file has moved says so.
+The list and the pictures are yours, kept with the app's settings rather than
+beside any show, and a show's picture is saved as the window closes.
+
## First launch
-Opening a scene file that does not exist yet starts guided setup instead of
-the studio. It runs three checks, and a check that fails shows its reason:
+Opening a show whose scene file does not exist yet starts guided setup instead
+of the studio. It runs three checks, and a check that fails shows its reason:
- **Devices:** the cameras, displays, and audio inputs this machine has.
- **Encoders:** for each canvas setup can propose (1080p and 720p at 60 or 30
@@ -97,12 +113,13 @@ the studio. It runs three checks, and a check that fails shows its reason:
short burst of moving frames. An encoder keeps up when a frame takes at most
three quarters of the frame period, leaving the rest for sources and the
compositor.
-- **Uplink:** with `--connect`, setup publishes a probe at the `--broadcast`
- path for about six seconds and subscribes to it through the relay, so the
- relay pulls it. The congestion controller's send-rate estimate is the
- measurement. With `--failover`, only the `--connect` relay is measured,
- since only its link is probed toward the stream's rate. A connection that
- reports no estimate, such as WebSocket, fails the check.
+- **Uplink:** with a relay, from `--connect` or the show's stream settings,
+ setup publishes a probe at the broadcast's path for about six seconds and
+ subscribes to it through the relay, so the relay pulls it. The congestion
+ controller's send-rate estimate is the measurement. With a failover relay,
+ only the first is measured, since only its link is probed toward the
+ stream's rate. A connection that reports no estimate, such as WebSocket,
+ fails the check.
Setup then proposes a scene named Main and a canvas, explaining each choice.
The scene has the first display filling the canvas, the first camera in the
@@ -120,17 +137,18 @@ canvas and no sources. The proposed scene is the first edit in the history, so
undoing it leaves that empty scene.
Setup writes an ordinary scene file. Change its sources and scenes on the
-BUILD page, and its canvas in Settings. The health dot shows how the uplink
-holds up once you are live.
+BUILD page, its canvas in Settings, and where it streams in Settings, Stream.
+The health dot shows how the uplink holds up once you are live.
-Setup sits in the desktop's own window frame, and the studio then draws its
-own (see [The window](#the-window)). On macOS the system's title bar stays
+Your shows and setup sit in the desktop's own window frame, and the studio
+then draws its own (see [The window](#the-window)). On macOS the system's title bar stays
until the next launch.
## The window
-The studio opens to its **LIVE** page: the program in a blue tally frame, as
-large as the window allows. The title bar and the strip along the bottom stay
+The studio opens to its **LIVE** page: the program as large as the window
+allows, in a neutral frame tagged **OFF AIR** until you go live, then a blue
+one tagged **ON AIR**. The title bar and the strip along the bottom stay
on every page, so the scenes, the sound, and the panic key never leave the
window while you are on air.
@@ -149,23 +167,25 @@ macOS the system's buttons and edges stay. It holds, from the left:
the history (the clock), which lists every edit, newest first: pick one to
undo or redo back to just after it.
- The pages: **LIVE**, **BUILD**, **MIX**, and **REVIEW**.
-- The health dot, the stream's worst grade at a glance. Click it for each
- relay, who is watching, the encoder, and each problem, and **Details** for
- the [Health](#health) window. It reads **Not live** without `--connect`.
+- The health dot, the stream's worst grade at a glance. Click it for the
+ worst problem first, then each destination, who is watching, and the
+ encoder, and **Details** for the [Health](#health) window. Off air it reads
+ **Ready** once the show has a relay, and **No relay** until then.
- Record, which starts or stops a [recording](#recording) from any page, with
the picks in Settings' Recording tab. Its dot pulses while recording, with
how long it has been recording beside it.
-- The live control: **LIVE** and how long the studio has been live, with
- `--connect`, or **NOT LIVE** without it. The studio goes live at launch,
- so it starts and ends nothing yet.
+- The live control: **GO LIVE** off air, which opens the
+ [go-live sheet](#going-live); on air, **LIVE** and how long the stream has
+ run, which reads **END STREAM** under the pointer.
- The gear, which opens [Settings](#settings).
- Minimize, maximize, and close, except on macOS.
The **strip** holds:
- A **pad** for each scene, its picture redrawn a few times a second, with its
- name, its hotkey, an ON AIR tag on the scene on program, and a NEXT tag on
- the scene cued or open off air. Click a pad to take its scene on air by the
+ name, its hotkey, an ON AIR tag on the scene on program (before going live,
+ a **FIRST** tag in amber, as the stream opens on it), and a NEXT tag on the
+ scene cued or open off air. Click a pad to take its scene on air by the
show's transition (see [Transitions](#transitions)), or to cue it in
[Studio mode](#off-air-and-studio-mode); clicking the scene already on air
leaves nothing cued. A scene that cannot go on air, of another size or
@@ -303,13 +323,63 @@ corner while the selected layer is under it, so it hides neither the layer
nor its toolbar, even mid-drag. Studio mode is kept per user, with
the [hotkeys](#hotkeys), not in the scene file.
+### Going live
+
+**GO LIVE** opens a sheet that says which scene the stream opens on, then:
+
+- **Where it goes:** the show's relay and its failover relay, each with a
+ switch and its state, such as *Connected, 18 ms away*. A relay switched off
+ is left out of this stream.
+- **Checks**, filled in as they finish: whether each camera has a picture,
+ whether a microphone's level moves, whether the encoder keeps up with the
+ canvas (timed as [setup](#first-launch) times it), and whether the
+ connection to the first relay switched on carries the program (measured as
+ setup measures it; published scenes and sources share what is left). A failed check is one sentence and its fix,
+ such as **Send 720p instead**, which changes the canvas, or **Check again**.
+- **GO LIVE TO** the number of destinations switched on. A failed check never
+ stops you; only having none switched on does.
+
+Going live dials each relay, failing with why if none answers within 15
+seconds, then opens the program's encoders and announces its broadcast. Each
+time on air is a broadcast of its own. On air, the live control is the clock;
+under the pointer it reads **END STREAM**, and holding it down for a moment
+ends the stream, handing the relays its tail first, as quitting does. The
+**Go live** and **End stream** hotkeys do the same at once, going live to the
+relays switched on without the sheet.
+
+Off air, the program has no past to replay, and a replay of it says so.
+
+### Stream settings
+
+Settings, Stream holds where the show streams, saved in `show.stream.toml`
+beside `show.toml` rather than in it, since a scene file is meant to be
+shared:
+
+- **Relay**, the one the stream goes to, and its **Token**.
+- **Failover relay** and its token: a second relay the same broadcast goes
+ to, which needs one `--connect-version` with broadcast instances.
+- **Broadcast name**, the path on the relays, `studio` until set.
+- **On demand**, which encodes the canvas size only while someone watches
+ (see [Output](#output)).
+
+Tokens are kept in the system's credential store (the Secret Service on Linux,
+the login keychain on macOS, the Credential Manager on Windows), one per relay
+address, never in a file: a `?jwt=` typed into an address moves there when
+saved. Without a credential store, a relay that needs no token still
+connects, and saving a token says why it cannot. A setting a flag overrides for this run says so. A change applies the
+next time you go live; a change to the room the guests join applies once the
+show is opened again. The recordings folder and the guests' room are kept in
+the same file, as `recordings`, `room`, `room_key`, `join_page`, and
+`guest_expiry`, set there by hand until Settings has panes for them.
+
### Settings
| Tab | Use it to |
| --- | --- |
| Show | Set the canvas size and frame rate and each scene's size, see the broadcast path, whether the program is encoding, the encoder sessions open, and the encoder work of the program, the recording, and each published scene, and turn the virtual camera of the program or of each published scene on. |
+| Stream | Set the relay, its token, the failover relay, the broadcast's name, and on demand (see [Stream settings](#stream-settings)). |
| Recording | See and open the recordings folder, and pick the picture and the audio scene a recording holds, and whether each source records a file of its own too (see [Recording](#recording)). |
-| Hotkeys | Bind keys to the panic key, TAKE, AUTO, Studio mode, replay, save a clip, start or stop recording, and take or cue each scene, and clear them (see [Hotkeys](#hotkeys)). |
+| Hotkeys | Bind keys to the panic key, TAKE, AUTO, Studio mode, replay, save a clip, start or stop recording, go live, end the stream, and take or cue each scene, and clear them (see [Hotkeys](#hotkeys)). |
| Effects | Pick how the window's [effects](#effects) move. |
| Advanced | See the sources, scenes, and audio scenes as nodes, wired left to right to what draws or holds them, and wire them there (see [Scene graph](#scene-graph)). |
@@ -343,9 +413,9 @@ why, and an edit resizing the scene on air is refused.
Switch on **Publish** for a scene, in BUILD's inspector with no layer selected
or in the scene graph, to put it live as a broadcast
-of its own beneath the program's, at `<--broadcast>/`, at its own
-size, with renditions and encoders of its own (see [Output](#output)). A
-published scene is always live and never switched: it shows what its layers
+of its own beneath the program's, at `/`, at its own
+size, with renditions and encoders of its own (see [Output](#output)), while
+the program is on air. A published scene is never switched: it shows what its layers
show, whatever is on program. For a portrait stream that follows the program,
publish a portrait scene drawing the program through a program source (see
[Scene sources](#scene-sources)), with any overlay of its own placed beside
@@ -359,7 +429,7 @@ does not publish while live.
A source captures or decodes once however many scenes show it. A published
scene is drawn only while one of its encoders takes the frame, so with
-`--on-demand`, or without a relay, one nobody watches costs no drawing. A
+on demand, or off air, one nobody watches costs no drawing. A
layer's keyframes play in a published scene from when it was published, picked
for recording, or the studio started, whichever came first: a recording and a
broadcast of one scene share one picture.
@@ -475,8 +545,9 @@ the source comes along, renamed if its name is taken. Copied filters paste
onto the selected layer, after its own. The clipboard holds text in the scene
file's format.
-A broadcast source subscribes through the same relay connections. It plays its
-broadcast through the first relay while that relay has a route to it, and
+A broadcast source subscribes through the show's relays, on air or off,
+over sessions of their own that only subscribe. It plays its broadcast
+through the first relay while that relay has a route to it, and
through the failover otherwise, so a publisher restarted behind only the
failover replaces the first relay's copy once that copy ends. It plays the
best video and audio its catalog lists, switches when the catalog changes, and
@@ -722,9 +793,12 @@ moq-studio show.toml --connect 'https://relay.example.com/show?jwt=...' \
--room guests --room-key room.jwk --join-page https://join.example.com/
```
-- `--room` is a path under the `--connect` URL's, here `show/guests` on the
+The guest flags set the room for one run; `room`, `room_key`, `join_page`, and
+`guest_expiry` in the show's [stream settings](#stream-settings) file keep it.
+
+- `--room` is a path under the relay's URL, here `show/guests` on the
relay. The studio's own token must let it subscribe there. A guest's browser
- dials the same relay, so `--connect` must be `https://`, or `http://` only to
+ dials the same relay, so it must be `https://`, or `http://` only to
this machine or the local network.
- `--room-key` signs each guest's token. The studio refuses a key whose scope
reaches outside the room, so a stolen key cannot sign for the rest of the
@@ -749,13 +823,13 @@ moq-studio show.toml --connect 'https://relay.example.com/show?jwt=...' \
Links from before let their holder subscribe to the whole room, renewals
included, so they must die with the old key, and a relay keeps a session
it already admitted until its token expires.
-- With `--failover`, a link still names only the `--connect` relay: the join
+- With a failover relay, a link still names only the first relay: the join
page dials one relay, and a guest's token is rooted at the room there, so
guests drop while that relay is down. The studio follows the room through
both relays, like any broadcast source, so a guest the failover routes at
the same path, as a relay in the same cluster does, keeps reaching it.
Anyone the failover lets publish in the room joins as a guest, so guard the
- room there as on the `--connect` relay.
+ room there as on the first relay.
**Invite**, under Guests in BUILD's Add, makes a link for one guest name and shows it as
a QR code; **Copy link** puts it on the clipboard instead. The guest's token is
@@ -914,7 +988,7 @@ items = [
[[scenes]]
name = "Portrait"
size = { width = 1080, height = 1920 } # its own size; the program's without one
-published = true # live at <--broadcast>/Portrait
+published = true # live at /Portrait
recorded = true # recordings hold this scene rather than the program
items = [
{ source = "on-air", rect = { x = 0, y = 0, width = 1080, height = 1920 }, crop = { left = 0.34, right = 0.34 } },
@@ -1063,8 +1137,9 @@ before sharing.
The program publishes as two H.264 renditions, at the canvas size and at half
of it, at the canvas frame rate, with the audio scene on program as Opus stereo audio,
-under the `--broadcast` path. A canvas under 360 lines has no half size. Each
-published scene publishes the same way at `<--broadcast>/`, at
+under the broadcast's path, while on air. A canvas under 360 lines has no
+half size. Each published scene publishes the same way at
+`/`, at
its own size and half of it, its tracks sharing the connection's bandwidth
with the program's, and its own encode of the audio scene it carries.
Timestamps come from the studio's own clock, so a source that stalls repeats
@@ -1085,12 +1160,11 @@ renditions' tracks end and new ones are listed in their place in a catalog
update, on the same timeline, and viewers switch to them; the video pauses
while the new encoders open. Items keep their place in canvas pixels. A canvas
the encoder cannot take stops the video, saying why, until the canvas changes
-again. At startup, it stops the studio when live; without `--connect`, the
-studio starts without video.
+again. Going live with one fails, saying why.
The canvas size encodes whether or not anyone watches, so a viewer who arrives
late can fetch the recent past. The half size encodes only while someone
-watches it. `--on-demand` copies and encodes the canvas size only while someone
+watches it. On demand copies and encodes the canvas size only while someone
watches it too, which saves that work but leaves nothing from before the first
viewer; audio encodes either way. A recording has an encoder of its own (see
Recording). A hardware encoder that reads
@@ -1139,7 +1213,7 @@ encode engine for them all. Settings' Show tab counts the sessions open:
| Encoder | Sessions |
|---|---|
-| The program's canvas size, live | 1, or none without `--connect` |
+| The program's canvas size | 1 while on air |
| Its half size | 1 while someone watches it |
| Each published scene's own size and half size | as the program's |
| The recording's own size | 1 while recording |
@@ -1153,7 +1227,7 @@ encodes on hardware, so do the sources, and a
session the GPU refuses fails that source's track with the encoder's reason on
its layer rather than falling back to software. The source keeps
drawing in the studio's own scenes. The program opens its session before any
-source, even with `--on-demand`, so the sources past the cap fail rather than
+source, even with on demand, so the sources past the cap fail rather than
the program; so does each scene published in the scene file at startup. A
scene published later, past the cap, publishes without video and says why in
Settings' Show tab. When the program encodes in software, a source takes hardware
@@ -1290,7 +1364,7 @@ the relay serves, so a recording starts and stops without touching the live
stream. The audio scene it holds is
encoded a second time for it while recording: the one on program, so the file
holds what viewers hear, or the one Settings' Recording tab picks, which viewers never
-can. Without `--connect`, only the
+can. Off air, only the
recording's encoder encodes the program.
Each file plays while it grows, so a crash leaves a playable fragmented MP4,
@@ -1329,9 +1403,10 @@ a WebGL page runs slower.
The Hotkeys tab in Settings binds a key to the **Panic** key, TAKE and AUTO
for the cued scene, turning [Studio mode](#off-air-and-studio-mode) on or off,
-**Replay**, **Save clip**, starting or stopping a recording, and each scene,
-which its key takes, or cues in Studio mode, as its pad does; the tab says
-which. Press **Bind**, then the key with any modifiers; Escape, or leaving
+**Replay**, **Save clip**, starting or stopping a recording, **Go live**,
+**End stream**, and each scene, which its key takes, or cues in Studio mode,
+as its pad does; the tab says which. Press **Bind**, then the key with any
+modifiers; Escape, or leaving
the window or the tab, cancels. Every action starts unbound, so no key a game
uses is taken unasked, and a key bound to one action is refused for another
until it is cleared there. Replay and Save clip use REVIEW's picks, but take
@@ -1410,7 +1485,7 @@ Sound replays only at 100%, through the replay source's strip on MIX, so a
replay of a microphone or system audio, which has only sound, plays only at
100%. A new replay cuts off the one playing, and a stretch the source was
offline or paused replays as the gap it was. When Replay or Save clip cannot
-act, such as off air with no relay or for a source with no past, it says why
+act, such as on the program off air or for a source with no past, it says why
in place of what it would do.
**Save clip** writes the same stretch, in real time, into the recordings
@@ -1427,8 +1502,9 @@ Nothing records in the background for this. The program's tracks keep their
last 30 seconds for late viewers anyway, and each source's track while it is on
program (a picture shown in a scene on air, or a sound played by the audio
scene on program) or recorded, and a replay copies from those, so a replay
-goes back at most that far. A replay of the program needs `--connect`; a local
-source keeps its track without a relay. With `--on-demand`, the program has
+goes back at most that far. A replay of the program needs it on air, and
+reaches back no further than going live; a local source keeps its track off
+air. With on demand, the program has
no past from before someone watched it, and a source has none from before it
was last on program or recorded; a replay of nothing is refused, saying why. A
broadcast source replays from what the studio received of it, and only over a
diff --git a/packaging/flatpak/live.kixel.studio.yml b/packaging/flatpak/live.kixel.studio.yml
index d69bcfba..7f448c2f 100644
--- a/packaging/flatpak/live.kixel.studio.yml
+++ b/packaging/flatpak/live.kixel.studio.yml
@@ -23,6 +23,8 @@ finish-args:
- --filesystem=xdg-run/pipewire-0
# Scene files, image sources, and recordings wherever the creator keeps them.
- --filesystem=home
+ # Relay tokens, kept in the Secret Service (GNOME Keyring, KWallet).
+ - --talk-name=org.freedesktop.secrets
modules:
- name: moq-studio
buildsystem: simple
diff --git a/packaging/flatpak/moq-studio-desktop.sh b/packaging/flatpak/moq-studio-desktop.sh
index a265fd81..b6af47d0 100755
--- a/packaging/flatpak/moq-studio-desktop.sh
+++ b/packaging/flatpak/moq-studio-desktop.sh
@@ -1,7 +1,5 @@
#!/bin/sh
-# The menu entry's launch, which passes no arguments: the studio's default
-# scene collection in its data directory, the one `flatpak run` opens too, so
-# the first launch runs guided setup, with recordings in the Videos folder the
-# desktop names.
+# The menu entry's launch, which passes no arguments: the library of recent
+# shows, and recordings in the Videos folder rather than beside each show.
set -eu
exec moq-studio --recordings "$(xdg-user-dir VIDEOS)" "$@"
diff --git a/packaging/flatpak/smoke.sh b/packaging/flatpak/smoke.sh
index f4d41fb1..8db56105 100755
--- a/packaging/flatpak/smoke.sh
+++ b/packaging/flatpak/smoke.sh
@@ -64,7 +64,10 @@ SWAYSOCK=$(compgen -G "$runtime/sway-ipc.*.sock")
WAYLAND_DISPLAY=$(basename "$(compgen -G "$runtime/wayland-*[0-9]")")
export SWAYSOCK WAYLAND_DISPLAY
-flatpak run --env=NO_COLOR=1 --command=moq-studio-desktop live.kixel.studio 2>"$log" &
+# A show that does not exist yet, so the launch runs guided setup rather than
+# opening the library.
+# shellcheck disable=SC2016 # Expanded inside the sandbox.
+flatpak run --env=NO_COLOR=1 --command=sh live.kixel.studio -c 'exec moq-studio-desktop "$XDG_DATA_HOME/smoke.toml"' 2>"$log" &
studio=$!
pids+=("$studio")
diff --git a/packaging/macos/smoke.sh b/packaging/macos/smoke.sh
index f0c00724..f25ca443 100755
--- a/packaging/macos/smoke.sh
+++ b/packaging/macos/smoke.sh
@@ -1,8 +1,9 @@
#!/usr/bin/env bash
-# Launch the app bundle given as Finder does, through Launch Services with no
-# arguments, and check that it lists cameras and audio, opens its window, and
-# quits when asked without crashing. A fresh home directory makes it open a
-# new scene collection, so guided setup lists the devices.
+# Launch the app bundle given through Launch Services, as Finder does, and
+# check that it lists cameras and audio, opens its window, and quits when
+# asked without crashing. Without arguments it opens the library of recent
+# shows, so it is handed a show not yet made, whose guided setup lists the
+# devices.
# Given a disk image instead, it mounts it as a download is and launches the
# app inside, after checking the image offers the drag to Applications.
set -euo pipefail
@@ -63,9 +64,10 @@ reports=~/Library/Logs/DiagnosticReports
marker=$home/started
touch "$marker"
-open -n "$app" --stdout "$log" --stderr "$log" --env HOME="$home" --env NO_COLOR=1
+show=$home/show.toml
+open -n "$app" --stdout "$log" --stderr "$log" --env HOME="$home" --env NO_COLOR=1 --args "$show"
deadline=$((SECONDS + 30))
-until pid=$(pgrep -nfx "$executable"); do
+until pid=$(pgrep -nfx "$executable $show"); do
((SECONDS < deadline)) || fail "the studio never started"
sleep 0.1
done
diff --git a/quest/m0/destinations.md b/quest/m0/destinations.md
index 02b63aff..2176be9f 100644
--- a/quest/m0/destinations.md
+++ b/quest/m0/destinations.md
@@ -74,13 +74,12 @@ Decided 2026-11-09 (maintainer, /quest-audit):
Changed 2026-11-09 (maintainer, /quest-plan): going live and stopping, as
actions and as hotkeys, are
-[Going live from inside the app](/quest/m0/ux/go-live.md), which lands them
-for MoQ relays. This quest adds its destinations to that sheet rather than
+the go-live sheet and live control the studio has, which land them for MoQ
+relays. This quest adds its destinations to that sheet rather than
building a second way to start a stream.
## Required
-- [Going live from inside the app](/quest/m0/ux/go-live.md) - the go-live sheet, the live control, and the health list its destinations join
- [moq's egress dial changes are pinned](/quest/m0/pin-egress-dial.md) - RTMPS that keeps `tcUrl`, and a dial policy the caller supplies
## Related
diff --git a/quest/m0/program-encoder-priority.md b/quest/m0/program-encoder-priority.md
new file mode 100644
index 00000000..af6f51d7
--- /dev/null
+++ b/quest/m0/program-encoder-priority.md
@@ -0,0 +1,29 @@
+# [S] The program's encoder before the sources'
+
+## Goal
+
+Going live gets the program its hardware encode sessions even when the
+sources on program took every one the GPU allows while off air. Today the
+program opens its encoders at GO LIVE, after the sources' replay encoders
+(`rs/moq-scene/src/publish.rs`, which encode a source shown on program so a
+replay has its recent past) already hold theirs, so on a GPU with a session
+cap, such as a GeForce card's, a show with many sources on program goes live
+on the CPU encoder or not at all. Before going live from the window, the
+program opened first, at launch.
+
+## Plan
+
+Raised by the review of
+[moq-dev/studio#175](https://github.com/moq-dev/studio/pull/175). The go-live
+sheet's Encoder check already opens the same encoder with the sources'
+sessions held, so the failure shows before the confirm; this makes it not
+happen. `Sessions` counts encoders but cannot know a cap, which a driver
+reports only by refusing an open.
+
+Decided by the maintainer: when the program's encoder cannot open, release
+the replay encoders of the sources on program, open the program's, then let
+each source reopen as sessions allow, its replay starting over (rejected:
+opening the program's encoders when the show opens and holding them idle off
+air, at one or two sessions and their memory for the whole session; leaving
+it to the go-live sheet's Encoder check alone). A replay is a convenience and
+the program is what viewers see; the cost falls only on a machine at its cap.
diff --git a/quest/m0/ux/close-show.md b/quest/m0/ux/close-show.md
new file mode 100644
index 00000000..d2c1a812
--- /dev/null
+++ b/quest/m0/ux/close-show.md
@@ -0,0 +1,25 @@
+# [S] Close the show and return to the library
+
+## Goal
+
+A broadcaster switches shows without quitting: the show's menu in the title
+bar has Close show, which returns the window to YOUR SHOWS, the library of
+recent shows. Today the library appears only when the studio starts with no
+scene file, so changing shows means quitting and starting again.
+
+## Plan
+
+Raised by [moq-dev/studio#175](https://github.com/moq-dev/studio/pull/175),
+which added the library (`rs/moq-studio/src/library.rs`) and the
+`App::Library` state in `rs/moq-studio/src/main.rs`.
+
+- Closing does what quitting does for the show: it flushes the autosave,
+ saves the show's picture for its library card, records how long it last
+ went live, finishes a recording, and closes the watch sessions, encoders,
+ and capture. Then the window shows the library, its cards refreshed.
+- While on air, Close show is disabled, its tooltip saying to end the stream
+ first. Recommended over ending the stream as a side effect of closing (a
+ menu click should not end a broadcast that END STREAM makes the
+ broadcaster hold for) and over keeping the stream up across the switch (the
+ stream belongs to the show being closed).
+- Its README entry and a `just shots` view of the menu with the row.
diff --git a/quest/m0/ux/first-run.md b/quest/m0/ux/first-run.md
index f6535e5f..1983f146 100644
--- a/quest/m0/ux/first-run.md
+++ b/quest/m0/ux/first-run.md
@@ -44,4 +44,3 @@ machine with a real camera and microphone, watched without helping.
## Required
- [Adding a source by picking a picture](/quest/m0/ux/add.md) - the cards with live pictures and meters
-- [Going live from inside the app](/quest/m0/ux/go-live.md) - the show library it starts from and the relay it measures
diff --git a/quest/m0/ux/go-live.md b/quest/m0/ux/go-live.md
deleted file mode 100644
index 3de8b878..00000000
--- a/quest/m0/ux/go-live.md
+++ /dev/null
@@ -1,74 +0,0 @@
-# [L] Going live from inside the app
-
-## Goal
-
-`moq-studio` starts with no arguments and no terminal, and a broadcaster goes
-live and stops from the window, as in
-[`mock/golive.html`](/quest/m0/ux/mock/golive.html):
-
-- **Opened with no show**, it lists recent shows with a picture of each, a
- New show card, and Open a show file.
-- **Off air**, the frame around the picture is neutral, the tag reads OFF
- AIR, and the live control is GO LIVE.
-- **GO LIVE** opens a short sheet: where the stream is going, each
- destination with a toggle and its state, and a checklist that fills in
- (camera, a microphone level that moves, an encoder that keeps up, a
- connection that carries the bitrate), then one confirm. A failed line is
- one plain sentence and its fix.
-- **On air**, the control is the clock, and END STREAM under the pointer,
- held to confirm. Ending delivers the broadcast's tail before the session
- closes, as quitting does today.
-- **The health dot** opens every destination, the viewers, and the encoder,
- and its details: the worst problem first, in plain words with its cause,
- which is today's Health panel.
-
-Where the stream goes is set in Settings: the relay's address, its token, a
-failover relay, the broadcast's name, and whether to encode only while
-someone watches. Go live and End stream are hotkey actions.
-
-## Plan
-
-Today the studio is live from launch when `--connect` is given and never
-otherwise, and the relay, token, broadcast name, failover, on-demand, and
-recordings folder are flags (`rs/moq-studio/src/main.rs`). A packaged app is
-opened by a double click, so none of that can stay a flag only.
-
-- ✅ **Connecting is a runtime action.** The engine opens and closes its
- relay sessions on Go live and End stream. Composing, previewing, and
- recording never needed a relay and still do not.
-- **Flags stay** as overrides for scripts and tests: a flag wins over the
- saved setting for that run and is never written back.
-- **Where it is saved:** the relay address, broadcast name, and options are
- per show, beside the scene file, not in it, since the scene file is meant
- to be shared and names no relay. Tokens go in the operating system's
- credential store, as [destinations](/quest/m0/destinations.md) decided for
- stream keys, and never in a file.
-- **The checklist is guided setup's probes** (`rs/moq-studio/src/setup.rs`):
- devices, the encoder's pace, and the uplink. The uplink probe runs against
- the relay about to be used and is skipped with its reason when a
- connection reports no estimate.
-- Recommended, not decided: a failed check never blocks going live. Its
- sentence and its fix are advice, since a streamer on a bad connection may
- still choose to go. Only having nothing to send, no destination switched
- on, disables the confirm.
-- GO LIVE is not in the accent, which stays the on-air state, nor red, which
- is errors alone.
-- **A replay of the program** needs the relay's cache, so off air it says so
- rather than failing.
-- **Recent shows** are kept per user. A show's picture is the last frame of
- its program, saved on quit.
-- Guests' room flags move with the rest; the room key stays a file the
- setting points at.
-
-[Destinations](/quest/m0/destinations.md) adds RTMP and WHIP rows to the same
-sheet and the same health popover; this quest lands them for MoQ relays
-alone.
-
-Human check: a real stream to a real relay with the maintainer's token,
-ended and started again; the credential store's own prompt on each desktop;
-and opening the app from a file manager with no terminal.
-
-## Related
-
-- [Destinations](/quest/m0/destinations.md) - RTMP and WHIP rows in this sheet
-- [Packaging](/quest/m0/packaging/README.md) - an app opened by a double click has no flags
diff --git a/quest/m0/ux/settings.md b/quest/m0/ux/settings.md
index 6b56dda0..40266682 100644
--- a/quest/m0/ux/settings.md
+++ b/quest/m0/ux/settings.md
@@ -9,8 +9,8 @@ title bar's gear, as in
- **Show:** landscape or portrait, the canvas size and frame rate as a few
named choices with the numbers behind Custom, and per-scene sizes behind a
disclosure.
-- **Stream:** the relay, failover, and options from
- [go live](/quest/m0/ux/go-live.md).
+- **Stream:** the relay, its token, the failover, the broadcast's name, and
+ on demand, as today's Stream tab holds them in `.stream.toml`.
- **Recording:** the folder, what a recording holds, and each source too.
- **Hotkeys:** today's Hotkeys panel, on keycaps.
- **Guests:** the room, its key, the join page, and how long a link lasts.
@@ -40,10 +40,16 @@ bar.
says so before it applies while on air.
- The scene graph (`rs/moq-studio/src/ui/graph.rs`) opens from Advanced in a
window of its own.
+- The Recording pane's folder and the Guests pane's room, key, join page, and
+ link lifetime are already fields of `.stream.toml` (`recordings`,
+ `room`, `room_key`, `join_page`, `guest_expiry`), which go live
+ ([moq-dev/studio#175](https://github.com/moq-dev/studio/pull/175)) saves
+ but gives no UI; the Recording tab shows and opens the folder but cannot
+ change it. These panes edit them there, and a flag that overrides one
+ shows it as the flag's, as the Stream pane does.
+- A room or relay change reaches the open show's guests at once: the guest
+ room renews its links against the new room and relay, instead of only after
+ the show is reopened, as go live left it.
Human check: whether each setting is where a streamer would look for it, and
the words on each pane.
-
-## Required
-
-- [Going live from inside the app](/quest/m0/ux/go-live.md) - the Stream pane's settings and where they are saved
diff --git a/quest/m0/ux/try-live.md b/quest/m0/ux/try-live.md
index 1c5f96a0..711382a1 100644
--- a/quest/m0/ux/try-live.md
+++ b/quest/m0/ux/try-live.md
@@ -25,6 +25,12 @@ says where to find it). What to try:
edge, snapping to a screen edge, and full screen.
- **macOS:** the system title bar stays after first-run setup until the next
launch, then the studio's own frame replaces it.
+- **Going live** ([moq-dev/studio#175](https://github.com/moq-dev/studio/pull/175)):
+ opening a show from the library, a real stream to a relay with your token
+ set in Settings, Stream (kept in the OS credential store; only Linux's
+ Secret Service has been exercised, so macOS's keychain matters most), the
+ GO LIVE sheet's checks, ending by holding END STREAM for 1.2 s, and the Go
+ live and End stream hotkeys while another app is full screen.
An agent then turns each finding into a quest, or an edit to the quest that
owns that page, and deletes this one.
diff --git a/quest/m1/dual-format.md b/quest/m1/dual-format.md
index 1e44a4d7..1da4b73a 100644
--- a/quest/m1/dual-format.md
+++ b/quest/m1/dual-format.md
@@ -33,7 +33,3 @@ program source, cropped. This quest makes that one switch.
beside it, and Studio mode cues both.
Human check: a phone and a desktop viewer watching the same show.
-
-## Required
-
-- [Going live from inside the app](/quest/m0/ux/go-live.md) - a format per destination
diff --git a/quest/m2/phone-remote.md b/quest/m2/phone-remote.md
index ec0c4360..a8cd578f 100644
--- a/quest/m2/phone-remote.md
+++ b/quest/m2/phone-remote.md
@@ -21,7 +21,3 @@ Planned 2026-11-09 (maintainer, /quest-plan), and held for later: ✅ m2
- Removing a remote stops renewing its token, as for a guest.
- The page follows the look of the studio's LIVE page
([mock-up](/quest/m0/ux/mock/shell-live.webp)) at a phone's width.
-
-## Required
-
-- [Going live from inside the app](/quest/m0/ux/go-live.md) - the relay and room it is reached through
diff --git a/rs/moq-scene/src/publish.rs b/rs/moq-scene/src/publish.rs
index f47ea230..59df52f3 100644
--- a/rs/moq-scene/src/publish.rs
+++ b/rs/moq-scene/src/publish.rs
@@ -183,24 +183,32 @@ impl Publish {
});
}
- /// Publish each source `published` names in `broadcast`, the program's,
- /// as long as it stays named, until `published` closes: its tracks are
- /// copied in under [`FORWARDED`] and its catalog listed under `catalog`'s
- /// [`SECTION`] section, renamed to match. Reading its tracks keeps each
- /// source encoding while it is published. Each copied audio or video
- /// track claims its bitrate from `bandwidth` while subscribed, as the
- /// program's tracks do.
- pub async fn forward(
- &self,
- mut published: tokio::sync::watch::Receiver>,
- broadcast: moq_net::broadcast::Producer,
- mut catalog: Catalog,
- bandwidth: moq_net::bandwidth::Allocator,
- ) -> Result<(), Error> {
+ /// Publish each source `forward.published` names in the program's
+ /// broadcast, as long as it stays named, until `published` closes or
+ /// `stop` is dropped: its tracks are copied in under [`FORWARDED`] and its
+ /// catalog listed under the catalog's [`SECTION`] section, renamed to
+ /// match. Reading its tracks keeps each source encoding while it is
+ /// published. Each copied audio or video track claims its bitrate from
+ /// `bandwidth` while subscribed, as the program's tracks do. A copied
+ /// track that ends, once its source is no longer named or forwarding
+ /// stops, is finished after what it copied, so its viewers see it end
+ /// rather than cut off; this returns once every one has.
+ pub async fn forward(&self, forward: Forward) -> Result<(), Error> {
+ let Forward {
+ mut published,
+ broadcast,
+ mut catalog,
+ bandwidth,
+ mut stop,
+ } = forward;
let mut updates = self.catalog.consume()?;
let mut local = self.catalog.snapshot();
- // Each copied track's copy, by its local name, aborted once dropped.
- let mut copies: BTreeMap> = BTreeMap::new();
+ // Each copied track's copy, by its local name, and what stops it,
+ // finishing its track, once dropped.
+ let mut copies: BTreeMap, tokio::sync::watch::Sender<()>)> =
+ BTreeMap::new();
+ // The copies of tracks no longer listed, still finishing them.
+ let mut retiring: Vec> = Vec::new();
loop {
let listed: BTreeMap = {
let published = published.borrow_and_update();
@@ -212,13 +220,19 @@ impl Publish {
.iter()
.flat_map(|(source, catalog)| tracks(catalog).map(move |(name, rate)| (name, (source.clone(), rate))))
.collect();
- copies.retain(|name, copy| {
- let keep = tracks.contains_key(name);
- if !keep {
- copy.abort();
+ // Its stopper dropped, a copy finishes its track on its own, and is
+ // still waited on below should forwarding stop first.
+ retiring.retain(|copy| !copy.is_finished());
+ let gone: Vec = copies
+ .keys()
+ .filter(|name| !tracks.contains_key(*name))
+ .cloned()
+ .collect();
+ for name in gone {
+ if let Some((copy, _stopper)) = copies.remove(&name) {
+ retiring.push(copy);
}
- keep
- });
+ }
for (name, (source, rate)) in tracks {
if copies.contains_key(&name) {
continue;
@@ -230,8 +244,9 @@ impl Publish {
grants: self.grants.clone(),
source,
});
- let copy = copy(self.consume(), broadcast.clone(), name.clone(), bandwidth);
- copies.insert(name, tokio::spawn(copy));
+ let (stopper, stopped) = tokio::sync::watch::channel(());
+ let copy = copy(self.consume(), broadcast.clone(), name.clone(), bandwidth, stopped);
+ copies.insert(name, (tokio::spawn(copy), stopper));
}
let listed: BTreeMap = listed
.into_iter()
@@ -249,10 +264,14 @@ impl Publish {
None => break,
},
changed = published.changed() => if changed.is_err() { break },
+ _ = stop.changed() => break,
}
}
- for copy in copies.into_values() {
- copy.abort();
+ // Each stopped as its stopper drops, and waited on, so the broadcast
+ // holds every copied track's end before the listing goes.
+ let copies: Vec<_> = copies.into_values().map(|(copy, _stopper)| copy).collect();
+ for copy in copies.into_iter().chain(retiring) {
+ let _ = copy.await;
}
catalog.mutate(|root| root.ext.studio.sources.clear())?;
Ok(())
@@ -284,6 +303,20 @@ impl Publish {
}
}
+/// What [`Publish::forward`] copies into, and how long.
+pub struct Forward {
+ /// The names of the sources to publish.
+ pub published: tokio::sync::watch::Receiver>,
+ /// The program's broadcast, which each source's tracks are copied into.
+ pub broadcast: moq_net::broadcast::Producer,
+ /// The program's catalog, which lists each source.
+ pub catalog: Catalog,
+ /// What each copied audio or video track claims its bitrate from.
+ pub bandwidth: moq_net::bandwidth::Allocator,
+ /// Forwarding stops once its sender is dropped.
+ pub stop: tokio::sync::watch::Receiver<()>,
+}
+
/// The prefix of each source track's name in the program's broadcast, ahead
/// of its name in the local one, so it never meets one of the program's own.
pub const FORWARDED: &str = "source/";
@@ -323,8 +356,6 @@ fn renamed(mut source: hang::Catalog) -> hang::Catalog {
source
}
-/// Copy the local track `name` into `broadcast` under [`FORWARDED`], group by
-/// group, until it ends. A failure ends the copy, failed, with the reason.
/// How a copied track claims its bitrate of the connection, and where its
/// source's video finds what it was granted.
struct Grant {
@@ -355,16 +386,23 @@ impl Drop for Granted {
}
}
+/// Copy the local track `name` into `broadcast` under [`FORWARDED`], group by
+/// group, until it ends or `stop`'s sender is dropped, when the copy finishes
+/// the group it is on and then its track. A failure ends the copy, failed,
+/// with the reason.
async fn copy(
local: moq_net::broadcast::Consumer,
broadcast: moq_net::broadcast::Producer,
name: String,
grant: Option,
+ mut stop: tokio::sync::watch::Receiver<()>,
) {
let copied = async {
- let mut track = local.track(&name)?.subscribe(None).await?;
+ let mut track = tokio::select! {
+ track = async { local.track(&name)?.subscribe(None).await } => track?,
+ _ = stop.changed() => return Ok(()),
+ };
let out = broadcast.create_track(format!("{FORWARDED}{name}"), track.info().clone())?;
- // Held for as long as the copy runs.
// Held for as long as the copy runs. A video's grant is what its source
// encodes at, so the source follows the connection as the program does.
let _grant = grant.map(|grant| {
@@ -384,7 +422,12 @@ async fn copy(
// Past the last group copied, from where a skip the source declared
// is carried over.
let mut below = 0;
- while let Some(mut group) = track.recv_group().await? {
+ 'groups: loop {
+ let group = tokio::select! {
+ group = track.recv_group() => group?,
+ _ = stop.changed() => break,
+ };
+ let Some(mut group) = group else { break };
// Creating a group says nothing of the starts below it, so the
// copy repeats the source's skips: one before its first group
// marks the encoder's first, and one between groups says the
@@ -394,7 +437,17 @@ async fn copy(
}
below = below.max(group.start + 1);
let mut copy = out.create_group(moq_net::group::Info { start: group.start })?;
- while let Some(frame) = group.read_frame().await? {
+ loop {
+ let frame = tokio::select! {
+ frame = group.read_frame() => frame?,
+ // The group ends at what it holds, where the source
+ // has not said.
+ _ = stop.changed() => {
+ drop(copy.close()?);
+ break 'groups;
+ }
+ };
+ let Some(frame) = frame else { break };
copy.write_frame(frame.timestamp, frame.payload)?;
}
// The copy ends where the source ended its group, when it said,
@@ -1232,14 +1285,18 @@ mod tests {
let config = moq_mux::catalog::Config::default().with_catalog(Default::default());
let catalog = Catalog::new(&mut program, config).unwrap();
let (published, follow) = tokio::sync::watch::channel(collection.published.clone());
- let forward = publish.clone();
- let (broadcast, listing) = (program.clone(), catalog.clone());
- tokio::spawn(async move {
- forward
- .forward(follow, broadcast, listing, moq_net::bandwidth::Allocator::unlimited())
- .await
- .unwrap()
- });
+ let (stop, stopped) = tokio::sync::watch::channel(());
+ let forward = Forward {
+ published: follow,
+ broadcast: program.clone(),
+ catalog: catalog.clone(),
+ bandwidth: moq_net::bandwidth::Allocator::unlimited(),
+ stop: stopped,
+ };
+ let forwarding = {
+ let publish = publish.clone();
+ tokio::spawn(async move { publish.forward(forward).await.unwrap() })
+ };
let snapshot = listed(&catalog, |sources| sources.contains_key("red")).await;
let red = &snapshot.ext.studio.sources["red"];
@@ -1252,9 +1309,19 @@ mod tests {
assert_eq!(mime, "image/png");
let red = image::load_from_memory(&payload).unwrap().into_rgba8();
assert_eq!(red.into_raw(), [255, 0, 0, 255]);
+ let name = snapshot.ext.studio.sources["red"].binary.tracks.keys().next().unwrap();
+ let mut track = program.consume().track(name).unwrap().subscribe(None).await.unwrap();
published.send(BTreeSet::new()).unwrap();
listed(&catalog, |sources| sources.is_empty()).await;
+ // No longer published, its copy ends after what it copied rather
+ // than being cut off.
+ let mut group = track.recv_group().await.unwrap().expect("the still");
+ while group.read_frame().await.unwrap().is_some() {}
+ assert!(track.recv_group().await.unwrap().is_none(), "the copy finished");
+
+ drop(stop);
+ forwarding.await.expect("forwarding stops once told to");
})
.await;
}
@@ -1270,7 +1337,9 @@ mod tests {
.create_track("audio", hang::container::track_info(hang::catalog::PRIORITY.audio))
.unwrap();
let program = moq_net::broadcast::Info::new().produce();
- let copied = copy(local.consume(), program.clone(), "audio".into(), None);
+ // Held to the end, so the copy runs until its source finishes.
+ let (_stop, stopped) = tokio::sync::watch::channel(());
+ let copied = copy(local.consume(), program.clone(), "audio".into(), None, stopped);
let checked = async {
// The copy's track lasts as long as the copy, so it is read while
// the source is written.
diff --git a/rs/moq-studio/Cargo.toml b/rs/moq-studio/Cargo.toml
index 200657b9..f3bb42ef 100644
--- a/rs/moq-studio/Cargo.toml
+++ b/rs/moq-studio/Cargo.toml
@@ -43,9 +43,6 @@ cef = ["moq-scene/cef"]
[dependencies]
anyhow = { workspace = true, features = ["backtrace"] }
bytes = { workspace = true }
-# The user's videos folder, where recordings go when no scene file is named:
-# Linux keeps it in user-dirs.dirs, and Windows lets it be moved.
-dirs = "7"
# The window, its event loop, and the egui integration. `wgpu` is the renderer
# the compositor shares a device with; no glow, no system font discovery.
# `persistence` keeps per-user picks, such as the output Listen plays on, out
@@ -58,6 +55,11 @@ hang = { workspace = true }
# Stamps a recording's file name with when it started. Already in the graph
# through moq-scene's humantime-serde.
humantime = { workspace = true }
+# How long a guest's link lasts, as the stream settings file spells it.
+humantime-serde = { workspace = true }
+# The relay tokens, in the operating system's credential store rather than a
+# file; each platform's store is its own crate, below.
+keyring-core = "1.0"
kio = { workspace = true, features = ["async", "runtime"] }
moq-audio = { workspace = true }
# Signs guest tokens with the room key.
@@ -72,9 +74,14 @@ moq-video = { workspace = true }
# A recording's length, from its movie header; the version moq-mux writes it
# with.
mp4-atom = "0.17.0"
+# A show's picture in the library, the last frame of its program.
+png = "0.18"
pollster = { workspace = true }
# The join link's QR code. No dependencies of its own.
qrcodegen = "1.8"
+# The system's own dialog for opening a show file or saving a new one: the
+# desktop portal on Linux, so it works in the Flatpak.
+rfd = "0.18"
# Whether a recording's marker is still the file its holder locked, which std
# cannot tell on Windows.
same-file = "1.0.6"
@@ -113,6 +120,9 @@ raw-window-handle = "0.6"
# Hotkeys on X11, grabbed by keycode so any key binds on any layout. `xkb`
# tells a held key's repeats from new presses.
x11rb = { version = "0.13", features = ["xkb"] }
+# Relay tokens in the Secret Service (GNOME Keyring, KWallet), over the D-Bus
+# stack ashpd uses, on tokio as ashpd has it, with no system library.
+zbus-secret-service-keyring-store = { version = "1.0", features = ["rt-tokio-crypto-rust"] }
[target.'cfg(unix)'.dependencies]
# The room left on the recordings folder's disk. moq-scene's release.
@@ -127,6 +137,8 @@ global-hotkey = "0.9"
# Whether the system asks apps to reduce motion, for the effects. eframe's
# version, already in the graph through winit.
objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSAccessibility", "NSWorkspace"] }
+# Relay tokens in the login keychain.
+apple-native-keyring-store = { version = "1.0", features = ["keychain"] }
[target.'cfg(windows)'.dependencies]
# Which virtual key the foreground window's layout puts on each key, so a
@@ -134,6 +146,9 @@ objc2-app-kit = { version = "0.3", default-features = false, features = ["std",
# reduce motion, for the effects, and the room left on the recordings folder's
# disk.
windows-sys = { version = "0.61", features = ["Win32_Storage_FileSystem", "Win32_UI_Input_KeyboardAndMouse", "Win32_UI_WindowsAndMessaging"] }
+# Relay tokens in the Windows Credential Manager. Without `search`, which the
+# studio never does, so no regex.
+windows-native-keyring-store = { version = "1.1", default-features = false }
[build-dependencies]
# Rasterizes the app icon's SVG for the window. No text or embedded images, so
diff --git a/rs/moq-studio/assets/icons/check.svg b/rs/moq-studio/assets/icons/check.svg
new file mode 100644
index 00000000..6252adfd
--- /dev/null
+++ b/rs/moq-studio/assets/icons/check.svg
@@ -0,0 +1,14 @@
+
+
+
+
diff --git a/rs/moq-studio/assets/icons/cpu.svg b/rs/moq-studio/assets/icons/cpu.svg
new file mode 100644
index 00000000..f17efb08
--- /dev/null
+++ b/rs/moq-studio/assets/icons/cpu.svg
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/rs/moq-studio/assets/icons/folder-open.svg b/rs/moq-studio/assets/icons/folder-open.svg
new file mode 100644
index 00000000..96c91c2d
--- /dev/null
+++ b/rs/moq-studio/assets/icons/folder-open.svg
@@ -0,0 +1,14 @@
+
+
+
+
diff --git a/rs/moq-studio/assets/icons/rectangle-horizontal.svg b/rs/moq-studio/assets/icons/rectangle-horizontal.svg
new file mode 100644
index 00000000..a6f23595
--- /dev/null
+++ b/rs/moq-studio/assets/icons/rectangle-horizontal.svg
@@ -0,0 +1,14 @@
+
+
+
+
diff --git a/rs/moq-studio/assets/icons/rectangle-vertical.svg b/rs/moq-studio/assets/icons/rectangle-vertical.svg
new file mode 100644
index 00000000..375b91b9
--- /dev/null
+++ b/rs/moq-studio/assets/icons/rectangle-vertical.svg
@@ -0,0 +1,14 @@
+
+
+
+
diff --git a/rs/moq-studio/assets/icons/triangle-alert.svg b/rs/moq-studio/assets/icons/triangle-alert.svg
new file mode 100644
index 00000000..ddfe1ab7
--- /dev/null
+++ b/rs/moq-studio/assets/icons/triangle-alert.svg
@@ -0,0 +1,16 @@
+
+
+
+
+
+
diff --git a/rs/moq-studio/assets/icons/upload.svg b/rs/moq-studio/assets/icons/upload.svg
new file mode 100644
index 00000000..270074cb
--- /dev/null
+++ b/rs/moq-studio/assets/icons/upload.svg
@@ -0,0 +1,16 @@
+
+
+
+
+
+
diff --git a/rs/moq-studio/src/controls.rs b/rs/moq-studio/src/controls.rs
index bee7e2a5..dde1c1fe 100644
--- a/rs/moq-studio/src/controls.rs
+++ b/rs/moq-studio/src/controls.rs
@@ -1,7 +1,7 @@
//! What the operator does from a button or a hotkey: take a scene, panic,
-//! replay, save a clip, and record. Hotkeys fire from outside the window's
-//! frames, so a key pressed while a game has focus acts at once, even while
-//! the window is hidden and draws nothing.
+//! replay, save a clip, record, and go live and end the stream. Hotkeys fire
+//! from outside the window's frames, so a key pressed while a game has focus
+//! acts at once, even while the window is hidden and draws nothing.
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
@@ -9,8 +9,9 @@ use std::sync::{Arc, Mutex};
use anyhow::Context;
use moq_scene::Source;
-use crate::engine::{Engine, State};
+use crate::engine::{Engine, OnAir, State};
use crate::replay;
+use crate::stream::{Stream, Switched};
/// Something a hotkey can be bound to.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
@@ -32,11 +33,15 @@ pub enum Action {
Record,
/// Cut to the safe scene and mute every microphone and guest.
Panic,
+ /// Go live to the relays switched on, skipping the go-live sheet's checks.
+ GoLive,
+ /// End the stream, handing the relays its tail first.
+ EndStream,
}
impl Action {
/// The actions every scene file has, as the Hotkeys tab lists them.
- pub const FIXED: [Self; 7] = [
+ pub const FIXED: [Self; 9] = [
Self::Panic,
Self::Cut,
Self::Transition,
@@ -44,6 +49,8 @@ impl Action {
Self::Replay,
Self::SaveClip,
Self::Record,
+ Self::GoLive,
+ Self::EndStream,
];
/// What it does with Studio mode on or off, as the Hotkeys tab and a
@@ -69,14 +76,60 @@ impl std::fmt::Display for Action {
Self::SaveClip => write!(f, "Save clip"),
Self::Record => write!(f, "Start or stop recording"),
Self::Panic => write!(f, "Panic"),
+ Self::GoLive => write!(f, "Go live"),
+ Self::EndStream => write!(f, "End stream"),
}
}
}
+/// How long a go-live waits for encoder checks still running.
+const PROBED: std::time::Duration = std::time::Duration::from_secs(10);
+
+/// The encoder checks, each holding a hardware encode session the
+/// program's would count against, and whether a go-live is opening the
+/// program's, which admits no new check.
+#[derive(Default)]
+struct Probes {
+ running: usize,
+ starting: bool,
+}
+
+/// An encoder check running, until dropped: see [`Controls::probing`].
+pub struct Probing(Arc>);
+
+impl Drop for Probing {
+ fn drop(&mut self) {
+ self.0.send_modify(|probes| probes.running -= 1);
+ }
+}
+
/// The engine as panels and hotkeys drive it, with the picks both share.
pub struct Controls {
pub engine: Arc,
runtime: tokio::runtime::Handle,
+ /// Where the show streams, as Settings edits it.
+ pub stream: Mutex,
+ /// Which relays going live sends to, as the go-live sheet switches them.
+ pub switched: Mutex,
+ /// Why the last go-live failed, until the go-live sheet or the status bar
+ /// shows it.
+ pub refused: Mutex>,
+ /// Whether a go-live is under way, from the press until it is live or
+ /// refused, which covers reading the tokens before the engine says
+ /// Starting, so a second press is refused at once.
+ going: AtomicBool,
+ /// Whether the relays wait to be followed: the show's, until its first
+ /// follow, and those saved on air until the stream ends, since following
+ /// them then would cut the broadcast sources and guests on program.
+ rewatch: AtomicBool,
+ /// Held while the relays are followed, and by a go-live while it follows
+ /// any still waiting, so each follow reads the settings after the last
+ /// one landed, and none is followed once it is on air.
+ following: Mutex<()>,
+ /// The encoder checks running, which a go-live waits out, a closed
+ /// go-live sheet's included, whose check runs to its end, and admits no
+ /// more of until it is live or refused.
+ probes: Arc>,
/// REVIEW's replay picks, which a replay or clip from a hotkey takes too.
pub replay: Mutex,
/// Whether the next recording gives each source a file of its own too.
@@ -112,10 +165,17 @@ impl Default for Replay {
}
impl Controls {
- pub fn new(engine: Arc, runtime: tokio::runtime::Handle) -> Self {
+ pub fn new(engine: Arc, stream: Stream, runtime: tokio::runtime::Handle) -> Self {
Self {
engine,
runtime,
+ stream: Mutex::new(stream),
+ switched: Mutex::default(),
+ refused: Mutex::default(),
+ going: AtomicBool::new(false),
+ rewatch: AtomicBool::new(false),
+ following: Mutex::new(()),
+ probes: Arc::new(tokio::sync::watch::Sender::new(Probes::default())),
replay: Mutex::default(),
record_sources: AtomicBool::new(false),
studio: AtomicBool::new(false),
@@ -143,7 +203,7 @@ impl Controls {
}
/// Do `action`, saying what it did.
- pub fn fire(&self, action: &Action) -> anyhow::Result {
+ pub fn fire(self: &Arc, action: &Action) -> anyhow::Result {
match action {
Action::Cut => {
let scene = self.engine.state.lock().unwrap().take_cued(None)?;
@@ -180,11 +240,184 @@ impl Controls {
}
}
}
+ Action::GoLive => self.go_live(),
+ Action::EndStream => self.end_stream(),
+ }
+ }
+
+ /// Go live to the relays switched on, in the background: reading their
+ /// tokens blocks on the credential store, and dialing them takes a while.
+ /// How it went shows in the status bar, and a refusal in the go-live
+ /// sheet too.
+ pub fn go_live(self: &Arc) -> anyhow::Result {
+ anyhow::ensure!(
+ self.engine.on_air() == OnAir::Off,
+ "the program is already on air, or on its way on or off"
+ );
+ anyhow::ensure!(!self.going.swap(true, Ordering::AcqRel), "already going live");
+ *self.refused.lock().unwrap() = None;
+ let controls = self.clone();
+ let spawned = std::thread::Builder::new().name("go live".into()).spawn(move || {
+ // Each relay's client binds its socket on the runtime.
+ let _runtime = controls.runtime.enter();
+ // One snapshot of the settings, both followed, if their relays wait
+ // to be, and gone live with, after any follow already under way.
+ // A save after it waits for this stream to end.
+ let stream = {
+ let _following = controls.following.lock().unwrap();
+ let (stream, waiting) = controls.snapshot();
+ if waiting {
+ controls.follow(&stream);
+ }
+ stream
+ };
+ // Wait out the encoder checks still running before opening anything.
+ controls.runtime.block_on(controls.probed());
+ let switched = *controls.switched.lock().unwrap();
+ let destination = stream.destination(switched);
+ let result = destination.and_then(|destination| {
+ let to = destination.broadcast.clone();
+ controls.runtime.block_on(controls.engine.go_live(destination))?;
+ Ok(format!("live as {to}"))
+ });
+ let result = result.map_err(|err| format!("{err:#}"));
+ if let Err(err) = &result {
+ *controls.refused.lock().unwrap() = Some(err.clone());
+ }
+ // Before `going`, which admits the next go-live, whose own gate this
+ // would otherwise lift.
+ controls.probes.send_modify(|probes| probes.starting = false);
+ controls.going.store(false, Ordering::Release);
+ // Relays saved while this started, followed now if it failed;
+ // once live, when it ends. Whichever of a save and this runs
+ // last follows them.
+ drop(controls.follow_saved_relays());
+ controls.report(&Action::GoLive, result);
+ });
+ if let Err(err) = spawned {
+ self.going.store(false, Ordering::Release);
+ return Err(err).context("cannot go live");
+ }
+ Ok("going live".into())
+ }
+
+ /// Note an encoder check running until the returned guard drops, which
+ /// a go-live waits for before the program's encoders open, or `None`
+ /// while a go-live is opening them, whose sessions it would take.
+ pub fn probing(&self) -> Option {
+ let mut admitted = false;
+ self.probes.send_if_modified(|probes| {
+ admitted = !probes.starting;
+ probes.running += usize::from(admitted);
+ admitted
+ });
+ admitted.then(|| Probing(self.probes.clone()))
+ }
+
+ /// Admit no more encoder checks until this go-live ends, and wait out
+ /// those running, bounded at [`PROBED`], past which the program's
+ /// encoders open anyway and a refused one says why.
+ async fn probed(&self) {
+ self.probes.send_modify(|probes| probes.starting = true);
+ let mut probes = self.probes.subscribe();
+ let waited = kio::clock::timeout(PROBED, probes.wait_for(|probes| probes.running == 0)).await;
+ if waited.is_err() {
+ tracing::warn!("going live beside an encoder check still running");
+ }
+ }
+
+ /// Whether a go-live is under way, from the press until it is live or
+ /// refused.
+ pub fn going(&self) -> bool {
+ self.going.load(Ordering::Acquire)
+ }
+
+ /// Take `stream` as the settings, as Settings saved them. When
+ /// `relays_changed`, follow the new relays now if off air, or once the
+ /// stream ends; says whether they wait for it.
+ pub fn set_stream(self: &Arc, stream: Stream, relays_changed: bool) -> bool {
+ {
+ // Marked with the settings it belongs to, so a snapshot never
+ // holds the new relays without knowing they wait to be followed.
+ let mut current = self.stream.lock().unwrap();
+ *current = stream;
+ if relays_changed {
+ self.rewatch.store(true, Ordering::Release);
+ }
}
+ if !relays_changed {
+ return false;
+ }
+ let waits = self.going() || self.engine.on_air() != OnAir::Off;
+ drop(self.follow_saved_relays());
+ waits
+ }
+
+ /// Follow `stream`'s relays, which wait to be followed again if any could
+ /// not be set up, such as on a token the credential store would not read,
+ /// so the next go-live, end of stream, or save tries them again. Call it
+ /// holding [`Self::following`].
+ fn follow(&self, stream: &Stream) {
+ if !crate::show::follow(&self.engine, stream) {
+ self.rewatch.store(true, Ordering::Release);
+ }
+ }
+
+ /// The settings, and whether their relays wait to be followed, which
+ /// this takes on, as one, so a save never falls between the two.
+ fn snapshot(&self) -> (Stream, bool) {
+ let stream = self.stream.lock().unwrap();
+ (stream.clone(), self.rewatch.swap(false, Ordering::AcqRel))
+ }
+
+ /// Follow broadcast sources, guests, and viewers' reports through the
+ /// show's relays, as the settings hold them once the follow runs, in the
+ /// background. Ordered with every save and go-live after it, as a save's
+ /// follow is, so it never lands after a later one with relays since
+ /// replaced, nor once a stream is on air.
+ pub fn follow_relays(self: &Arc) -> Option> {
+ self.rewatch.store(true, Ordering::Release);
+ self.follow_saved_relays()
+ }
+
+ /// Follow the saved relays, if any wait to be, on a thread of its own,
+ /// since the credential store blocks, unless a stream is on air or on its
+ /// way, which follows them itself.
+ fn follow_saved_relays(self: &Arc) -> Option> {
+ let controls = self.clone();
+ let spawned = std::thread::Builder::new().name("follow".into()).spawn(move || {
+ let _following = controls.following.lock().unwrap();
+ if controls.going() || controls.engine.on_air() != OnAir::Off {
+ return;
+ }
+ let (stream, waiting) = controls.snapshot();
+ if !waiting {
+ return;
+ }
+ // Each relay's client binds its socket on the runtime.
+ let _runtime = controls.runtime.enter();
+ controls.follow(&stream);
+ });
+ spawned
+ .inspect_err(|err| tracing::warn!(error = format!("{err:#}"), "cannot follow the relays"))
+ .ok()
+ }
+
+ /// End the stream in the background, handing the relays its tail first.
+ pub fn end_stream(self: &Arc) -> anyhow::Result {
+ anyhow::ensure!(self.engine.on_air().live().is_some(), "the program is not on air");
+ let controls = self.clone();
+ self.runtime.spawn(async move {
+ let result = controls.engine.end_stream().await;
+ drop(controls.follow_saved_relays());
+ let result = result.map(|()| "the stream ended".to_string());
+ controls.report(&Action::EndStream, result.map_err(|err| format!("{err:#}")));
+ });
+ Ok("ending the stream".into())
}
/// Do `action` for a hotkey, keeping how it went for the status bar.
- pub fn press(&self, action: &Action) {
+ pub fn press(self: &Arc, action: &Action) {
match action {
Action::Replay => drop(self.replay(true, true)),
Action::SaveClip => drop(self.replay(false, true)),
diff --git a/rs/moq-studio/src/engine.rs b/rs/moq-studio/src/engine.rs
index 7e9bcb30..7b5f0b09 100644
--- a/rs/moq-studio/src/engine.rs
+++ b/rs/moq-studio/src/engine.rs
@@ -1,10 +1,11 @@
//! Everything behind the window: the GPU, the sources, the program clock that
-//! draws the canvas and mixes the audio, and the broadcast it encodes into:
-//! the program, at the canvas size and at half of it ([`crate::ladder`]), and
-//! each published scene's beneath it, at its own size and half. The relays
-//! serve those broadcasts, and recordings read the program or a scene. Each local source's own
-//! track stays in a broadcast of the studio's own, which recordings read too,
-//! as they read each broadcast source from the relays.
+//! draws the canvas and mixes the audio, and, while on air ([`air`]), the
+//! broadcast it encodes into: the program, at the canvas size and at half of
+//! it ([`crate::ladder`]), and each published scene's beneath it, at its own
+//! size and half. The relays serve those broadcasts, and recordings read the
+//! program or a scene, on air or not. Each local source's own track stays in
+//! a broadcast of the studio's own, which recordings read too, as they read
+//! each broadcast source from the relays.
//!
//! Threads:
//! - **program** draws preview, program, and each published scene once per
@@ -12,11 +13,10 @@
//! encode, scaled to the rendition and stamped with the program clock.
//! - **audio** advances the audio bus, and with it each audio scene's mix, on
//! the same clock.
-//! - **audio-encode** encodes the audio scene on program into the program
-//! audio, and **audio-record** the one recordings hold into the recording
-//! broadcast's own audio while a recording reads it.
-//! - Each published scene's audio, and each guest's return feed, a
-//! mix-minus, encodes on the tokio runtime.
+//! - **audio-record** encodes the audio scene recordings hold into the
+//! recording broadcast's own audio while a recording reads it.
+//! - While on air, the program audio, each published scene's, and each
+//! guest's return feed, a mix-minus, encode on the tokio runtime.
//! - **encode**: one per program rendition, which waits for each program copy
//! and encodes it. A new canvas size or frame rate starts new renditions.
//! - Recordings write their files on the tokio runtime.
@@ -49,6 +49,10 @@ use crate::autosave::{self, Autosave};
use crate::ladder::{self, Gate, Joins, Step};
use crate::{health, mix, record};
+pub mod air;
+
+pub use air::{Aired, Destination, OnAir};
+
/// The GPU everything shares: sources upload to it, the compositor draws on
/// it, the window presents from it, and the encoder reads the program from it.
#[derive(Clone)]
@@ -838,41 +842,6 @@ impl Stats {
}
}
-/// The relays the program goes live on.
-pub struct Output {
- /// The broadcast path on the relays.
- pub path: String,
- /// Whether the program video's canvas size is copied and encoded only
- /// while someone watches it, rather than all the time. The half size
- /// always is.
- pub on_demand: bool,
- /// A connection to each relay, which reconnects on its own and dials only
- /// while held. The Health panel reads their states.
- connections: Vec,
- /// The client that dialed each, whose endpoint sends the last close.
- clients: Vec,
-}
-
-impl Output {
- /// Close each relay session once the relay has received what it was sent
- /// of every finished track, which the session bounds at about a second,
- /// then each client's endpoint, so the relay hears the close rather than
- /// waiting out an idle timeout. A track still live is cut.
- async fn close(&self) {
- let mut closing = tokio::task::JoinSet::new();
- for (connection, client) in self.connections.iter().zip(&self.clients) {
- let (connection, client) = (connection.clone(), client.clone());
- closing.spawn(async move {
- if let Err(err) = connection.close().await {
- tracing::warn!(%err, "the relay session closed without draining");
- }
- client.close().await;
- });
- }
- closing.join_all().await;
- }
-}
-
/// A relay the program is published to.
#[derive(Clone)]
pub struct Relay {
@@ -880,23 +849,20 @@ pub struct Relay {
pub url: url::Url,
}
-/// The program's broadcast, open for as long as the engine runs.
+/// What the program's broadcasts share for as long as the engine runs, on air
+/// or not: the clock, the local sources' tracks, and the recording's own
+/// broadcast.
pub struct Program {
- /// Where the broadcast is announced, which the relay connections publish.
- _origin: moq_net::origin::Producer,
- publication: moq_net::origin::Route,
- /// What the broadcast lists and the clock its timestamps are on, for a
- /// replay of the program or a source.
- catalog: moq_scene::publish::Catalog,
+ /// What every catalog's timestamps are on: the recording's, the program's
+ /// each time it goes live, and each published scene's.
clock: moq_mux::Clock,
+ /// Where the program clock started on [`clock`](Self::clock).
+ epoch: Duration,
/// Where the local sources keep their own tracks, on the same clock.
sources: moq_scene::publish::Publish,
- /// The sources published independently, whose tracks the broadcast
+ /// The sources published independently, whose tracks the broadcast on air
/// carries beside the program's.
published: tokio::sync::watch::Sender>,
- /// Where and when the broadcast's viewers show those sources, while any
- /// is published.
- layout: Mutex,
/// What a recording reads the program from, on the same clock.
recording: moq_net::origin::Route,
/// The recording broadcast's catalog, held for as long as the engine runs,
@@ -905,48 +871,14 @@ pub struct Program {
/// The origin the studio's own broadcasts are read through, which no relay
/// connection carries.
local: moq_net::origin::Producer,
- /// The audio's claim on the connection's bandwidth, which it does not
- /// follow: Opus at this rate is never what congests the uplink.
- _audio: bandwidth::Reservation,
-}
-
-impl Drop for Program {
- fn drop(&mut self) {
- self.publication.broadcast().close();
- }
}
impl Program {
- /// End the layer layout, then finish the program's catalog and the
- /// recording's, once every track they list has ended, so a viewer or a
- /// recording reading either reaches its end rather than a cut.
+ /// Finish the recording's catalog, once every track it lists has ended, so
+ /// a recording reading it reaches its end rather than a cut.
fn finish(&self) {
- if let Err(err) = self.layout.lock().unwrap().finish() {
- tracing::warn!(%err, "cannot finish the layer layout");
- }
- for catalog in [&self.catalog, &self.recorded] {
- // Fails only once the catalog failed, which ended its tracks already.
- let _ = catalog.clone().finish();
- }
- }
-
- /// What a replay of the program could copy: its video at each size,
- /// largest first, with its audio, up to now.
- pub fn program(&self) -> Vec {
- let catalog = self.catalog.snapshot();
- let audio = catalog.audio.renditions.into_iter().next();
- let mut video: Vec<_> = catalog.video.renditions.into_iter().collect();
- video.sort_by_key(|(_, config)| {
- let pixels = u64::from(config.coded_width.unwrap_or(0)) * u64::from(config.coded_height.unwrap_or(0));
- std::cmp::Reverse(pixels)
- });
- match video.is_empty() {
- true => vec![self.target(None, audio)],
- false => video
- .into_iter()
- .map(|video| self.target(Some(video), audio.clone()))
- .collect(),
- }
+ // Fails only once the catalog failed, which ended its tracks already.
+ let _ = self.recorded.clone().finish();
}
/// What a replay of the local source `name` copies: the video or sound of
@@ -967,19 +899,6 @@ impl Program {
})
}
- fn target(
- &self,
- video: Option<(String, hang::catalog::VideoConfig)>,
- audio: Option<(String, hang::catalog::AudioConfig)>,
- ) -> moq_scene::replay::Target {
- moq_scene::replay::Target {
- broadcast: self.publication.broadcast().consume(),
- video,
- audio,
- end: self.clock.now(),
- }
- }
-
/// Encode the local sources' tracks on program, whether or not anything
/// reads them, so a replay of one has its past: the pictures `shown` in a
/// scene on air or a published or recorded scene drawn, and the sound the
@@ -1007,31 +926,6 @@ impl Program {
self.sources
.set_on_program(moq_scene::publish::OnProgram { shown, heard });
}
-
- /// Publish the layer layout of what `state` drew on program at `at` on
- /// the media clock, while any source is published independently.
- fn lay_out(&self, state: &State, at: Duration) {
- // Past what a timestamp holds, the frame goes unstamped too.
- let Ok(stamp) = moq_net::Timestamp::try_from(at) else {
- return;
- };
- let collection = &state.collection;
- let mut publisher = self.layout.lock().unwrap();
- let published = match collection.published.is_empty() {
- true => publisher.finish(),
- false => {
- // To the whole microsecond as the frame's stamp is, rather than
- // rounded as the wire would, so a cut's layout starts on its frame.
- let start = at.saturating_sub(state.compositor.program_since()).as_micros();
- let start = Duration::from_micros(start as u64);
- let layout = moq_scene::layout::Layout::new(collection, state.compositor.program_scene(), start);
- publisher.update(layout, stamp)
- }
- };
- if let Err(err) = published {
- tracing::warn!(%err, "cannot publish the layer layout");
- }
- }
}
/// The running studio.
@@ -1043,24 +937,40 @@ pub struct Engine {
pub stats: Arc,
/// How the stream is delivered, graded every [`health::INTERVAL`].
pub health: Mutex,
+ /// What grades it: the stream on air's delivery, or only the studio's
+ /// own work while off air.
+ sampler: Mutex,
/// The audio scene the program audio carries, as the collection picks it.
program_audio: mix::Pick,
/// The audio scene recordings hold, as the collection picks it.
recorded_audio: mix::Pick,
/// Plays an audio scene to the speakers while the creator listens.
monitor: Mutex>,
- /// `None` when running without a relay.
- pub output: Option,
+ /// The stream, while on air, going live, or ending.
+ air: Mutex,
+ /// The last stream to go off air.
+ aired: Mutex>,
+ /// Whether the program is on air, for the window and for quitting, which
+ /// waits out a stream going live or ending.
+ on_air: tokio::sync::watch::Sender,
+ /// What the program clock takes from going live and gives back on ending.
+ handoff: Mutex,
+ /// The program's broadcast on air, which guests' return feeds show.
+ on_air_program: tokio::sync::watch::Sender>,
/// Records the program, and each source too if asked.
pub recorder: record::Recorder,
/// The room guests join, when configured.
pub room: Option>,
/// Each guest's return feed, once the studio's session in the room dials.
feeds: Mutex>,
- /// The program's broadcast.
+ /// What the program's broadcasts share, on air or not.
pub program: Program,
- /// Keeps broadcast sources' origin open when no connection holds it.
- _origin: Option,
+ /// Where broadcast sources, guests, and viewers' reports arrive from the
+ /// relays the studio [watches](Self::watch), open even while none is.
+ subscribe: moq_net::origin::Producer,
+ /// A session to each relay set for the show, which only subscribes, so
+ /// broadcast sources and guests are seen off air too.
+ watching: Mutex>,
/// Set by [`stop`](Self::stop), which ends the program clock.
stopped: AtomicBool,
/// The program clock's and the audio's threads, which [`stop`](Self::stop)
@@ -1087,12 +997,6 @@ const CLOSE: Duration = Duration::from_secs(3);
#[derive(Clone)]
pub struct Launch {
pub gpu: Gpu,
- /// The relays to publish to, the preferred first; none publishes nothing.
- pub relays: Vec,
- /// The broadcast path the program is published at.
- pub broadcast: String,
- /// Whether the program video encodes only while someone watches.
- pub on_demand: bool,
/// The directory recordings are saved in.
pub recordings: PathBuf,
/// The room guests join, if any.
@@ -1102,23 +1006,13 @@ pub struct Launch {
impl Engine {
/// Open the `loaded` collection and history on `launch.gpu`, start the
/// program clock, and save both to `autosave`'s files as they change,
- /// starting at once.
- /// The program is encoded into a broadcast at `launch.broadcast` while
- /// anyone wants it: a recording into `launch.recordings`, or every one of
- /// `launch.relays`, preferred first, which want it all the time unless
- /// `launch.on_demand`. The guests who join `launch.room` are added as
- /// sources.
+ /// starting at once. Recordings go into `launch.recordings`, and the
+ /// guests who join `launch.room` are added as sources. Nothing is
+ /// published until the studio [goes live](Self::go_live).
///
/// Call inside the tokio runtime that serves the relay connections.
pub async fn start(launch: Launch, autosave: Autosave, loaded: autosave::Loaded) -> anyhow::Result> {
- let Launch {
- gpu,
- relays,
- broadcast,
- on_demand,
- recordings,
- room,
- } = launch;
+ let Launch { gpu, recordings, room } = launch;
let (bus, driver) = moq_audio::bus::Bus::new(Default::default())?;
let subscribe = moq_tokio::origin::spawn();
// Every relay feeds `subscribe`, so a guest either one routes is seen.
@@ -1140,26 +1034,10 @@ impl Engine {
let canvas = collection.canvas;
let stats = Arc::new(Stats::default());
- let path =
- moq_net::Path::new(broadcast.as_str()).with_context(|| format!("cannot publish at `{broadcast}`"))?;
- let origin = moq_tokio::origin::spawn();
- let (live, subscribe) = if relays.is_empty() {
- (None, Some(subscribe))
- } else {
- tracing::info!(broadcast = %path, "publishing");
- let reports = subscribe.consume();
- let clients = relays.iter().map(|relay| relay.client.clone()).collect();
- let (connections, links) = connect(relays, &origin, &subscribe);
- let live = Live {
- connections,
- clients,
- links,
- reports,
- on_demand,
- };
- (Some(live), None)
- };
- let (program, publish) = publish(&gpu, &stats, origin, &path, canvas, live.as_ref()).await?;
+ // The program clock's start, from which the audio clock advances the
+ // bus too, so the sources place their sound on it.
+ let start = Instant::now();
+ let (program, publish) = publish(&gpu, &stats, canvas, start).await?;
// Nothing in either local broadcast refers to another.
let local = |broadcast: moq_net::broadcast::Consumer| moq_mux::Source::new(program.local.consume(), broadcast);
let inputs = record::Inputs {
@@ -1168,19 +1046,10 @@ impl Engine {
origin: sources.clone(),
};
let recorder = record::Recorder::new(inputs, &recordings, autosave.path())?;
- let output = live.map(|live| Output {
- path: path.to_string(),
- on_demand,
- connections: live.connections,
- clients: live.clients,
- });
// Absolute, so a copied image names the same file in another collection.
let dir = autosave.path().parent().filter(|dir| !dir.as_os_str().is_empty());
let dir = std::path::absolute(dir.unwrap_or(Path::new(".")))?;
- // The program clock's start, from which the audio clock advances the
- // bus too, so the sources place their sound on it.
- let start = Instant::now();
let (mixer, mixed) = source::Mixer::new(driver, start);
let sources = Sources::new(source::Context {
bus: bus.clone(),
@@ -1204,6 +1073,7 @@ impl Engine {
// Nothing is cued, so nothing shows the preview.
compositor.set_previewing(false);
+ let epoch = program.epoch;
let engine = Arc::new(Self {
gpu,
state: Mutex::new(State {
@@ -1222,65 +1092,44 @@ impl Engine {
autosave,
stats,
health: Mutex::default(),
+ sampler: Mutex::new(health::Sampler::new(None)),
program_audio: mix::Pick::default(),
recorded_audio: mix::Pick::default(),
monitor: Mutex::default(),
- output,
+ air: Mutex::new(air::Slot::Off),
+ aired: Mutex::default(),
+ on_air: tokio::sync::watch::Sender::new(OnAir::Off),
+ handoff: Mutex::default(),
+ on_air_program: tokio::sync::watch::Sender::new(None),
recorder,
room,
feeds: Mutex::default(),
program,
- _origin: subscribe,
+ subscribe,
+ watching: Mutex::default(),
stopped: AtomicBool::new(false),
threads: Mutex::default(),
running: Mutex::default(),
});
let Publish {
- renditions,
- ladder,
recording,
- mut extras,
- audio,
recorded_audio,
- clock,
- health,
sources: _,
} = publish;
- // Where the program clock starts on the catalog's, so the wall time the
- // catalog advertises for a frame is when the program drew it.
- let epoch = Duration::try_from(clock.capture(start)?)?;
- extras.epoch = epoch;
if let Some(room) = &engine.room
&& let Some(origin) = room.origin()
{
- let program = engine.program.publication.broadcast().consume();
- let feeds = crate::feed::Feeds::new(room.clone(), origin.clone(), program, clock.clone(), epoch)?;
+ let program = engine.on_air_program.subscribe();
+ let clock = engine.program.clock.clone();
+ let feeds = crate::feed::Feeds::new(room.clone(), origin.clone(), program, clock, epoch)?;
*engine.feeds.lock().unwrap() = Some(feeds);
}
- let main = Line::new(renditions, canvas, ladder, true);
- // Opened before the program clock starts the sources, as the
- // program's encoder is, so each holds its encode session first.
- let mut lines = Lines::new();
- let (published, recorded): (Vec<(String, Canvas)>, Canvas) = {
- let state = engine.state.lock().unwrap();
- (published(state.collection()), recorded(state.collection()).1)
- };
- // Probed now too, so a scene a recording cannot encode says so before
- // anyone records it. It opens its session only once recorded.
+ // Probed now, so a scene a recording cannot encode says so before
+ // anyone records it, and opened before the program clock starts the
+ // sources. It opens its session only once recorded.
+ let (_, recorded) = recorded(engine.state.lock().unwrap().collection());
let recording = Line::open(recording, recorded).await;
- for (name, canvas) in published {
- let video = Arc::new(VideoStats::default());
- engine
- .stats
- .published
- .lock()
- .unwrap()
- .insert(name.clone(), video.clone());
- let opened = extras.open(&name, canvas, video.clone()).await;
- let opened = opened.inspect_err(|err| video.fail(format!("{err:#}"))).ok();
- lines.insert(name, opened);
- }
// Each worker holds the engine, so one that cannot start stops those
// already running, which would otherwise keep this engine and its
// publication alive beside a retry's.
@@ -1294,37 +1143,22 @@ impl Engine {
let thread = clocked("audio".into(), async move || mix.run_audio(start, config, mixer).await)?;
engine.threads.lock().unwrap().push(thread);
let program = engine.clone();
- let encoders = Encoders {
- main,
- recording,
- extras,
- lines,
- };
let thread = clocked("program".into(), async move || {
- program.run_program(start, epoch, encoders).await
+ program.run_program(start, epoch, recording).await
+ })?;
+ engine.threads.lock().unwrap().push(thread);
+ // On its own thread, so an encoder that falls behind skips its own
+ // audio rather than holding up the bus.
+ let encode = engine.clone();
+ let follow = engine.recorded_audio.follow();
+ let thread = clocked("audio-record".into(), async move || {
+ encode.encode_audio(epoch, rate, follow, recorded_audio).await
})?;
engine.threads.lock().unwrap().push(thread);
- // Each on its own thread, so an encoder that falls behind skips its own
- // audio rather than holding up the bus or the other.
- let audio = [
- ("audio-encode", Audio::Program(audio), engine.program_audio.follow()),
- (
- "audio-record",
- Audio::Recording(recorded_audio),
- engine.recorded_audio.follow(),
- ),
- ];
- for (name, audio, follow) in audio {
- let encode = engine.clone();
- let thread = clocked(name.into(), async move || {
- encode.encode_audio(epoch, rate, follow, audio).await
- })?;
- engine.threads.lock().unwrap().push(thread);
- }
let save = engine.clone();
clocked("autosave".into(), async move || save.autosave.run(&save.state).await)?;
starting.started();
- tokio::spawn(engine.clone().grade(health::Sampler::new(health)));
+ tokio::spawn(engine.clone().grade());
// Last, once nothing can fail: a start that fails is retried from setup,
// and a follower already running would keep this engine and its
// publication alive beside the retry's.
@@ -1351,12 +1185,12 @@ impl Engine {
/// every picture, to publish the copies on their way to it and what it
/// still holds, and to end its video track, so quitting loses none of the
/// program's last pictures; a published scene's catalog then finishes too.
- /// The program audio ends its track too, and then the program's catalog
- /// and the recording's finish, so a viewer or a recording reading the
- /// program reaches the end of each. Then stop every source, which
- /// finishes its own track on its thread, and the browser sources' helper,
- /// waiting for it to exit, so their profile is saved and free for the next
- /// launch.
+ /// The audio ends its tracks too, and then the recording's catalog
+ /// finishes, so a recording reading the program reaches its end. Then stop
+ /// every source, which finishes its own track on its thread, and the
+ /// browser sources' helper, waiting for it to exit, so their profile is
+ /// saved and free for the next launch. A stream on air ends with
+ /// [`close`](Self::close).
pub fn stop(&self) {
self.stopped.store(true, Ordering::Relaxed);
let threads = std::mem::take(&mut *self.threads.lock().unwrap());
@@ -1378,11 +1212,13 @@ impl Engine {
}
}
- /// Once [`stop`](Self::stop) has ended the tracks of every picture, close
+ /// Once [`stop`](Self::stop) has ended the tracks of every picture, end
+ /// the stream on air as [`end_stream`](Self::end_stream) does, closing
/// each relay session once the relay has their tails, so a viewer or a
- /// recording on the relay receives them rather than a cut. Meanwhile wait
- /// for each source's thread to end, having finished its own track, and then
- /// finish the sources' local catalog. Bounded at [`CLOSE`].
+ /// recording on the relay receives them rather than a cut; one going live
+ /// or ending is waited out first. Meanwhile wait for each source's thread
+ /// to end, having finished its own track, and then finish the sources'
+ /// local catalog. Bounded at [`CLOSE`].
pub async fn close(&self) {
let running = std::mem::take(&mut *self.running.lock().unwrap());
let sources = async {
@@ -1395,9 +1231,15 @@ impl Engine {
let _ = self.program.sources.finish();
};
let relays = async {
- if let Some(output) = &self.output {
- output.close().await;
+ let mut on_air = self.on_air.subscribe();
+ // Fails only once the engine is gone, which holds the sender.
+ let _ = on_air
+ .wait_for(|on_air| !matches!(on_air, OnAir::Starting | OnAir::Ending))
+ .await;
+ if let Err(err) = self.end_stream().await {
+ tracing::debug!(error = format!("{err:#}"), "nothing on air to end");
}
+ self.unwatch().await;
};
// A source's track is in the studio's own local broadcast, which no
// relay session carries, so neither waits on the other.
@@ -1486,12 +1328,13 @@ impl Engine {
}
/// Grade the stream's health every interval, for the Health panel.
- async fn grade(self: Arc, mut sampler: health::Sampler) {
+ async fn grade(self: Arc) {
let mut interval = kio::clock::interval(health::INTERVAL);
interval.set_missed_tick_behavior(kio::clock::MissedTickBehavior::Skip);
loop {
let now = interval.tick().await;
- let report = sampler.sample(now, self.local(now));
+ let local = self.local(now);
+ let report = self.sampler.lock().unwrap().sample(now, local);
*self.health.lock().unwrap() = report;
}
}
@@ -1648,34 +1491,30 @@ impl Engine {
/// Draw the program and each published scene once per tick of the program
/// clock, and hand each to its renditions' encoders, stamped `epoch` past
- /// `start`, while their gates let them encode: the full size every frame
- /// while live and not on demand, otherwise only while anyone watches it,
- /// and the half size only while anyone watches it. The program, or the
- /// scene recordings hold, goes to the recording's own encoder too, which
- /// encodes only while recording.
+ /// `start`, while their gates let them encode. On air, that is the full
+ /// size every frame unless on demand, otherwise only while anyone watches
+ /// it, and the half size only while anyone watches it; off air, the
+ /// program and the published scenes encode nothing. The program, or the
+ /// scene recordings hold, goes to `recording`, the recording's own
+ /// encoder, which encodes only while recording.
///
/// A published or recorded scene is drawn only while one of its encoders
/// or virtual camera takes the frame, so one nobody reads costs no
- /// drawing. See [`Encoders`] for what encodes each. Drawing outlives the
- /// encoders: a failed output stops copying its picture, and the scenes
- /// keep moving. Each virtual camera on takes every frame of what it shows
- /// while an app reads it, and one whose scene is no longer published
- /// turns off.
+ /// drawing. Drawing outlives the encoders: a failed output stops copying
+ /// its picture, and the scenes keep moving. Each virtual camera on takes
+ /// every frame of what it shows while an app reads it, and one whose
+ /// scene is no longer published turns off.
///
/// Runs until the studio [stops](Self::stop), and returns once every
/// rendition's encode thread has finished its track.
- async fn run_program(&self, start: Instant, epoch: Duration, encoders: Encoders) {
- let Encoders {
- mut main,
- mut recording,
- extras,
- mut lines,
- } = encoders;
+ async fn run_program(&self, start: Instant, epoch: Duration, mut recording: Line) {
let mut framerate = self.state.lock().unwrap().compositor.canvas().framerate;
let mut clock = program_clock(start, framerate);
// The encoders of lines since ended, waited on as the studio stops so
// each finishes its track.
let mut retired: Vec = Vec::new();
+ // The encoders of the stream on air.
+ let mut air: Option = None;
loop {
let tick = clock.tick().await;
@@ -1683,22 +1522,50 @@ impl Engine {
// is let go, so the UI does not wait on their backends.
let mut closed: Vec = Vec::new();
if self.stopped.load(Ordering::Relaxed) {
+ // A stream handed over since is ended too, and one asked to end
+ // finds nothing left to wait for.
+ let (start, _end) = self.handoff.lock().unwrap().close();
// Without a sender, each encoder publishes what it was sent and
// still holds, then finishes its track.
- retired.push(main.end(None));
retired.push(recording.end(None));
- retired.extend(
- lines
- .into_values()
- .flatten()
- .map(|(line, broadcast)| line.end(Some(broadcast))),
- );
+ let mut layouts = Vec::new();
+ for lines in air.take().into_iter().chain(start) {
+ let ending = lines.end();
+ retired.extend(ending.ended);
+ layouts.extend(ending.layout);
+ }
for ended in retired {
ended.join();
}
+ // Once the video it lays out has ended.
+ for mut layout in layouts {
+ air::finish_layout(&mut layout);
+ }
return;
}
retired.retain(|ended| !ended.is_finished());
+ // Ended before another starts, so the two never hold encode sessions
+ // at once.
+ let (mut starting, end) = self.handoff.lock().unwrap().take();
+ if let Some(reply) = end {
+ // The stream on air, or one handed over since that never drew a
+ // frame, such as one ended at once at a low frame rate.
+ let mut ending = air
+ .take()
+ .or_else(|| starting.take())
+ .map(air::Lines::end)
+ .unwrap_or_default();
+ // The scenes unpublished on air too, so each finishes its
+ // track before the stream's sessions close.
+ ending.ended.append(&mut retired);
+ if let Err(ending) = reply.send(ending) {
+ retired.extend(ending.ended);
+ }
+ self.stats.published.lock().unwrap().clear();
+ }
+ if let Some(starting) = starting {
+ air = Some(starting);
+ }
// Where this frame lands on the media clock.
let at = epoch + tick.saturating_duration_since(start);
let drawn = {
@@ -1717,37 +1584,42 @@ impl Engine {
self.pick_audio(state);
}
- // Each published scene's line starts once it is published, in a
- // broadcast of its own, and ends once it no longer is.
+ // On air, each published scene's line starts once it is
+ // published, in a broadcast of its own, and ends once it no
+ // longer is.
let published = published(&state.collection);
- let gone: Vec = lines
- .keys()
- .filter(|line| !published.iter().any(|(name, _)| name == *line))
- .cloned()
- .collect();
- for name in gone {
- if let Some(Some((line, broadcast))) = lines.remove(&name) {
- retired.push(line.end(Some(broadcast)));
+ if let Some(air) = &mut air {
+ let gone: Vec = air
+ .lines
+ .keys()
+ .filter(|line| !published.iter().any(|(name, _)| name == *line))
+ .cloned()
+ .collect();
+ for name in gone {
+ if let Some(Some((line, broadcast))) = air.lines.remove(&name) {
+ retired.push(line.end(Some(broadcast)));
+ }
+ self.stats.published.lock().unwrap().remove(&name);
}
- self.stats.published.lock().unwrap().remove(&name);
- }
- for (name, canvas) in &published {
- let started = !lines.contains_key(name);
- let line = lines.entry(name.clone()).or_insert_with(|| {
- let video = Arc::new(VideoStats::default());
- self.stats.published.lock().unwrap().insert(name.clone(), video.clone());
- extras
- .start(name, *canvas, video.clone())
- .inspect_err(|err| video.fail(format!("{err:#}")))
- .ok()
- });
- // Its audio follows the audio scene it carries from now on.
- if (synced || started)
- && let Some((_, broadcast)) = line
- && let Some(audio) = state.collection.published_audio(name)
- && let Some(mix) = state.sources.mix(audio)
- {
- broadcast.audio.set(mix.clone());
+ for (name, canvas) in &published {
+ let started = !air.lines.contains_key(name);
+ let extras = &air.extras;
+ let line = air.lines.entry(name.clone()).or_insert_with(|| {
+ let video = Arc::new(VideoStats::default());
+ self.stats.published.lock().unwrap().insert(name.clone(), video.clone());
+ extras
+ .start(name, *canvas, video.clone())
+ .inspect_err(|err| video.fail(format!("{err:#}")))
+ .ok()
+ });
+ // Its audio follows the audio scene it carries from now on.
+ if (synced || started)
+ && let Some((_, broadcast)) = line
+ && let Some(audio) = state.collection.published_audio(name)
+ && let Some(mix) = state.sources.mix(audio)
+ {
+ broadcast.audio.set(mix.clone());
+ }
}
}
closed.extend(
@@ -1762,8 +1634,9 @@ impl Engine {
);
let (recorded, recorded_canvas) = recorded(&state.collection);
let cameras = &state.cameras;
+ let lines = air.as_ref().map(|air| &air.lines);
let wants = |name: &str| {
- matches!(lines.get(name), Some(Some((line, _))) if line.wants())
+ matches!(lines.and_then(|lines| lines.get(name)), Some(Some((line, _))) if line.wants())
|| cameras
.get(&Some(name.to_string()))
.is_some_and(crate::vcam::Camera::wanted)
@@ -1786,7 +1659,9 @@ impl Engine {
continue;
}
self.program.on_program(state, shown);
- self.program.lay_out(state, at);
+ if let Some(air) = &mut air {
+ air::lay_out(&mut air.layout, state, at);
+ }
let compositor = &state.compositor;
for (scene, camera) in &mut state.cameras {
match scene {
@@ -1828,17 +1703,18 @@ impl Engine {
framerate = canvas.framerate;
clock = program_clock(tick.saturating_add(period(framerate)), framerate);
}
- main.feed(Some(&program), canvas, timestamp);
- let (texture, canvas) = drawn.recorded;
- recording.feed(texture.as_ref(), canvas, timestamp);
- for (name, texture, canvas) in drawn.published {
- if let Some(Some((line, _))) = lines.get_mut(&name) {
- line.feed(texture.as_ref(), canvas, timestamp);
+ let (texture, recorded) = drawn.recorded;
+ recording.feed(texture.as_ref(), recorded, timestamp);
+ if let Some(air) = &mut air {
+ air.main.feed(Some(&program), canvas, timestamp);
+ for (name, texture, canvas) in drawn.published {
+ if let Some(Some((line, _))) = air.lines.get_mut(&name) {
+ line.feed(texture.as_ref(), canvas, timestamp);
+ }
}
}
}
}
-
/// Advance the bus as the program clock advances, from `start`.
///
/// Runs without a relay too, so the meters move and sources never back up,
@@ -1870,32 +1746,31 @@ impl Engine {
}
}
- /// Encode the mix `follow` reads into `audio`, stamped `epoch` past the
- /// bus's start at `rate`, until the bus stops, and finish the track.
+ /// Encode the mix `follow` reads into the recording's own `audio`, stamped
+ /// `epoch` past the bus's start at `rate`, until the bus stops, and finish
+ /// the track.
///
- /// The recording's audio encodes only while a recording reads it, and marks
- /// the gap as the last one stops, so the next starts on the program clock
- /// rather than where the samples left off.
- async fn encode_audio(&self, epoch: Duration, rate: u32, mut follow: mix::Follow, audio: Audio) {
- let (mut producer, recording) = match audio {
- Audio::Program(producer) => (producer, false),
- Audio::Recording(producer) => (producer, true),
- };
- // The recording's audio failing leaves the published audio running, so
- // it is the recording's to report.
- let fail = |err: moq_audio::Error| match recording {
- false => self.stats.fail_audio(err),
- true => self.stats.fail_recording_audio(err),
- };
+ /// It encodes only while a recording reads it, and marks the gap as the
+ /// last one stops, so the next starts on the program clock rather than
+ /// where the samples left off. Its failing leaves the published audio
+ /// running, so it is the recording's to report.
+ async fn encode_audio(
+ &self,
+ epoch: Duration,
+ rate: u32,
+ mut follow: mix::Follow,
+ mut audio: moq_audio::encode::Producer,
+ ) {
+ let fail = |err: moq_audio::Error| self.stats.fail_recording_audio(err);
let rate = u128::from(rate);
let mut next = None;
let mut wanted = false;
while let Some(block) = follow.read().await {
- let was = std::mem::replace(&mut wanted, !recording || producer.demand().wanted().is_some());
+ let was = std::mem::replace(&mut wanted, audio.demand().wanted().is_some());
if !wanted {
- if was && let Err(err) = producer.discontinuity() {
+ if was && let Err(err) = audio.discontinuity() {
fail(err);
- producer.abort(moq_net::StreamError::Internal.into());
+ audio.abort(moq_net::StreamError::Internal.into());
return;
}
next = None;
@@ -1904,7 +1779,7 @@ impl Engine {
// Audio this encoder fell too far behind to read leaves a gap in
// the track rather than pulling what follows early.
if next.is_some_and(|next| next != block.position) {
- producer.reanchor();
+ audio.reanchor();
}
next = Some(block.position + block.frames() as u64);
let position = u128::from(block.position);
@@ -1913,14 +1788,14 @@ impl Engine {
continue;
};
let data: Vec = block.data.iter().flat_map(|sample| sample.to_le_bytes()).collect();
- if let Err(err) = producer.write(&moq_audio::Frame::new(Bytes::from(data), timestamp)) {
+ if let Err(err) = audio.write(&moq_audio::Frame::new(Bytes::from(data), timestamp)) {
fail(err);
// Readers see the audio fail rather than go away unfinished.
- producer.abort(moq_net::StreamError::Internal.into());
+ audio.abort(moq_net::StreamError::Internal.into());
return;
}
}
- if let Err(err) = producer.finish() {
+ if let Err(err) = audio.finish() {
fail(err);
}
}
@@ -1945,104 +1820,23 @@ impl Drop for Starting {
}
}
-/// One encoder of a mix: the published program audio, or the recording
-/// broadcast's own.
-enum Audio {
- Program(moq_audio::encode::Producer),
- Recording(moq_audio::encode::Producer),
-}
-
-/// The relay connections the program goes live on, the preferred first, and
-/// the origin their viewers' reports and the relays' stats arrive on.
-struct Live {
- connections: Vec,
- /// The client that dialed each connection.
- clients: Vec,
- links: Vec,
- reports: moq_net::origin::Consumer,
- /// The canvas size encodes only while someone watches it.
- on_demand: bool,
-}
-
-/// The encoders behind the program's broadcast, handed to their threads.
+/// What starting the engine sets up for the program's broadcasts beside
+/// [`Program`], handed to the threads that encode it.
struct Publish {
- /// Where the program video is published, a canvas at a time.
- renditions: Renditions,
- /// The renditions of the canvas the studio started with, unless the canvas
- /// could not be encoded without a relay.
- ladder: Option,
/// Where the recording's own video goes, of the program or the scene it
/// holds.
recording: Renditions,
- /// What publishes each published scene.
- extras: Extras,
- /// The program audio's encoder.
- audio: moq_audio::encode::Producer,
/// The recording broadcast's own audio encoder.
recorded_audio: moq_audio::encode::Producer,
- /// The catalog's clock, which the codec producers write timestamps
- /// against verbatim.
- clock: moq_mux::Clock,
- /// What the health sampler grades, while live on a relay.
- health: Option,
/// Where each local source keeps its own track.
sources: moq_scene::publish::Publish,
}
-/// Announce the program broadcast at `path` on `origin` under one instance,
-/// which `live`'s relays publish when there are any.
-async fn publish(
- gpu: &Gpu,
- stats: &Arc,
- origin: moq_net::origin::Producer,
- path: &moq_net::Path,
- canvas: Canvas,
- live: Option<&Live>,
-) -> anyhow::Result<(Program, Publish)> {
- let publication = origin
- .route(path, moq_net::origin::RouteOptions::hidden())
- .context("cannot create the broadcast")?;
- let mut broadcast = publication.broadcast().clone();
- // Each track reserves its ceiling on the first relay's estimate, which
- // that connection then probes to fund: the bandwidth the stream is granted.
- // An allocator spans one connection, so the failover's link is never
- // probed. The catalog claims nothing itself, which would count each track
- // twice. Without a relay nothing divides any bandwidth.
- let bandwidth = match live {
- Some(live) => live.connections[0].allocator(),
- None => moq_net::bandwidth::Allocator::unlimited(),
- };
- let catalog = moq_mux::catalog::Config::default().with_catalog(Default::default());
- let catalog = moq_scene::publish::Catalog::new(&mut broadcast, catalog)?;
- let clock = catalog.clock();
-
- // Probed now, so a machine that cannot encode the canvas fails at startup
- // rather than once the program clock starts. Without a relay the studio
- // still composes, previews, and records audio, and a new canvas tries again.
- let probe = match Probe::new(gpu, canvas, false).await {
- Ok(probe) => Some(probe),
- Err(err) if live.is_none() => {
- stats.video.fail(format!("{err:#}"));
- None
- }
- Err(err) => return Err(err),
- };
- // Sources take the program's tier: hardware when the program found it, so a
- // session the GPU refuses fails that source loudly instead of falling to
- // software. When the program could not use hardware, such as NVENC beside an
- // integrated GPU it cannot read, a source may still reach it from the CPU.
- let kind = match probe.as_ref().map(|probe| &probe.rungs[0].config.kind) {
- Some(moq_video::encode::Kind::Named(name)) if name == "openh264" => moq_video::encode::Kind::Auto,
- _ => moq_video::encode::Kind::Hardware,
- };
- let sources = moq_scene::publish::Publish::new(clock.clone(), kind, stats.sessions.clone())?;
- // Held until the video and audio are listed, so the first catalog carries
- // both and any solicitation.
- let reserved = catalog.reserve();
- // Viewers report back beneath the broadcast, which only a relay carries.
- if live.is_some() {
- health::solicit(&mut catalog.clone(), path)?;
- }
+/// Set up what the program's broadcasts share for as long as the engine runs:
+/// the clock, with the program clock's `start` on it, the local sources'
+/// tracks, and the recording's own broadcast, which reads the program at
+/// `canvas` until a recording asks for another.
+async fn publish(gpu: &Gpu, stats: &Arc, canvas: Canvas, start: Instant) -> anyhow::Result<(Program, Publish)> {
// What a recording reads the program from: its own full-size encoder, of
// the program or the scene it holds, at full quality whatever the grant,
// and the mix it holds, in a broadcast no relay sees: announced on an
@@ -2056,24 +1850,32 @@ async fn publish(
.announce()
.context("cannot announce the recording broadcast")?;
let mut recorder = recording.broadcast().clone();
- let config = moq_mux::catalog::Config::default()
- .with_catalog(Default::default())
- .with_clock(clock.clone());
+ let config = moq_mux::catalog::Config::default().with_catalog(Default::default());
let recorded = moq_scene::publish::Catalog::new(&mut recorder, config)?;
- let renditions = Renditions {
- gpu: gpu.clone(),
- broadcast: broadcast.clone(),
- timebase: catalog.timebase(),
- record: false,
- bandwidth: bandwidth.clone(),
- // Live on a relay and not on demand; otherwise only a viewer asks for
- // the video.
- on_demand: live.is_none_or(|live| live.on_demand),
- live: live.is_some(),
- stats: stats.clone(),
- video: stats.video.clone(),
- grants: String::new(),
+ // Every other catalog shares the recording's clock, so a recording, the
+ // program on air, and a source's track agree on when a frame was drawn.
+ let clock = recorded.clock();
+ // Where the program clock starts on the catalog's, so the wall time the
+ // catalog advertises for a frame is when the program drew it.
+ let epoch = Duration::try_from(clock.capture(start)?)?;
+
+ // Probed now, so a machine that cannot encode the canvas says so before
+ // going live, which tries it again. Sources take the program's tier:
+ // hardware when the program found it, so a session the GPU refuses fails
+ // that source loudly instead of falling to software. When the program could
+ // not use hardware, such as NVENC beside an integrated GPU it cannot read, a
+ // source may still reach it from the CPU.
+ let kind = match Probe::new(gpu, canvas, false).await {
+ Ok(probe) => match &probe.rungs[0].config.kind {
+ moq_video::encode::Kind::Named(name) if name == "openh264" => moq_video::encode::Kind::Auto,
+ _ => moq_video::encode::Kind::Hardware,
+ },
+ Err(err) => {
+ stats.video.fail(format!("{err:#}"));
+ moq_video::encode::Kind::Hardware
+ }
};
+ let sources = moq_scene::publish::Publish::new(clock.clone(), kind, stats.sessions.clone())?;
let recorded_video = Renditions {
gpu: gpu.clone(),
broadcast: recorder.clone(),
@@ -2086,107 +1888,27 @@ async fn publish(
video: stats.recording.clone(),
grants: String::new(),
};
- let ladder = match probe {
- Some(probe) => {
- let (ladder, opened) = renditions.start(probe)?;
- // Before the program clock starts the sources, so the program holds
- // its encode session even while unwatched.
- match opened.await.context("the encode thread stopped")? {
- Ok(()) => Some(ladder),
- // Its encode thread reported why.
- Err(_) if live.is_none() => None,
- Err(err) => return Err(err),
- }
- }
- None => None,
- };
-
let (input, options) = audio_options();
- let graded = catalog.clone();
- let listed = catalog.clone();
- let recorded_audio = moq_audio::encode::Producer::new(&mut recorder, recorded.clone(), input.clone(), &options)?;
- let audio = moq_audio::encode::Producer::new(&mut broadcast, catalog, input, &options)?;
- let audio_reservation = bandwidth.reserve(&audio.demand(), AUDIO_BITRATE);
- stats
- .grants
- .lock()
- .unwrap()
- .insert(audio.track_name().to_string(), audio_reservation.consume());
- drop(reserved);
-
- // One instance on every relay, so a viewer moving between them resumes
- // mid-group rather than restarting.
- let instance = publication.instance();
- // Set once the program clock starts.
- let epoch = Duration::ZERO;
- publication.announce().context("cannot announce the broadcast")?;
-
- let (published, follow) = tokio::sync::watch::channel(BTreeSet::new());
- let forward = (sources.clone(), broadcast.clone(), listed.clone(), bandwidth.clone());
- tokio::spawn(async move {
- let (sources, broadcast, catalog, bandwidth) = forward;
- if let Err(err) = sources.forward(follow, broadcast, catalog, bandwidth).await {
- tracing::warn!(%err, "stopped publishing sources independently");
- }
- });
- let layout = Mutex::new(moq_scene::layout::Publisher::new(broadcast.clone(), listed.clone()));
-
- let health = match live {
- Some(live) => {
- let observer = hang::health::Observer::new(path.clone(), Some(instance.clone()));
- Some(health::Publisher {
- main: health::Broadcast::follow(observer, graded, &live.reports)?,
- scenes: health::Scenes::new(live.reports.clone()),
- clock: clock.clone(),
- relays: live.links.clone(),
- sources: sources.clone(),
- })
- }
- None => None,
- };
- // Each published scene is graded beside the program, with its own viewers.
- let graded = health.as_ref().map(|health| health.scenes.clone());
+ let recorded_audio = moq_audio::encode::Producer::new(&mut recorder, recorded.clone(), input, &options)?;
+ let (published, _) = tokio::sync::watch::channel(BTreeSet::new());
Ok((
Program {
- _origin: origin.clone(),
- publication,
- catalog: listed,
- clock: clock.clone(),
+ clock,
+ epoch,
sources: sources.clone(),
published,
- layout,
recording,
recorded,
local,
- _audio: audio_reservation,
},
Publish {
- extras: Extras {
- origin: origin.clone(),
- path: path.clone(),
- clock: clock.clone(),
- epoch,
- runtime: tokio::runtime::Handle::current(),
- gpu: gpu.clone(),
- bandwidth,
- on_demand: renditions.on_demand,
- live: renditions.live,
- stats: stats.clone(),
- graded,
- },
- renditions,
- ladder,
recording: recorded_video,
- audio,
recorded_audio,
- clock,
- health,
sources,
},
))
}
-
/// What the program audio, the recording's, and each published scene's encode:
/// the bus's mix, as Opus at [`AUDIO_BITRATE`] on a track called `audio`.
fn audio_options() -> (moq_audio::encode::Input, moq_audio::encode::Options) {
@@ -2658,19 +2380,6 @@ struct Drawn {
recorded: (Option, Canvas),
}
-/// What the program clock hands each picture to.
-struct Encoders {
- /// The program's.
- main: Line,
- /// The recording's own, fed the program or the scene recordings hold,
- /// which a change of size probes for again as any line does.
- recording: Line,
- /// What starts the line of a scene published while the studio runs.
- extras: Extras,
- /// The published scenes' lines, those the studio started with first.
- lines: Lines,
-}
-
/// Each published scene in `collection`, with its canvas.
fn published(collection: &Collection) -> Vec<(String, Canvas)> {
let published = collection.scenes.iter().filter(|scene| scene.published);
@@ -2743,16 +2452,6 @@ impl Extras {
Ok((Line::new(renditions, canvas, None, false), broadcast))
}
- /// [`start`](Self::start), opening the full size's encoder before
- /// returning, as the program does at startup, so a scene published in the
- /// scene file holds its encode session before any source opens one. A
- /// scene whose encoder cannot open draws on without video, saying why in
- /// `video`.
- async fn open(&self, name: &str, canvas: Canvas, video: Arc) -> anyhow::Result<(Line, Broadcast)> {
- let (renditions, broadcast) = self.publish(name, video)?;
- Ok((Line::open(renditions, canvas).await, broadcast))
- }
-
/// Publish the scene `name` at its own path beneath the program's
/// broadcast, with audio of its own, which encodes the audio scene it is
/// [picked](Broadcast::audio) on the runtime, and where its video goes,
@@ -2971,44 +2670,6 @@ fn period(framerate: u32) -> Duration {
Duration::from_secs(1) / framerate
}
-/// Connect to every relay, publishing `origin` to each and feeding `subscribe`
-/// the broadcast sources of each, and watch each connection for the Health
-/// panel.
-fn connect(
- relays: Vec,
- origin: &moq_net::origin::Producer,
- subscribe: &moq_net::origin::Producer,
-) -> (Vec, Vec) {
- relays
- .into_iter()
- .enumerate()
- .map(|(rank, Relay { client, url })| {
- tracing::info!(url = %moq_tokio::RedactedUrl::new(&url), "connecting");
- // Only the scheme, host, and port: a path can carry a credential, and
- // the panel may be on a shared screen. A URL without a host, such as
- // a unix socket's, keeps its path, which is the address.
- let endpoint = match url.host_str() {
- Some(host) => match url.port() {
- Some(port) => format!("{}://{host}:{port}", url.scheme()),
- None => format!("{}://{host}", url.scheme()),
- },
- None => format!("{}:{}", url.scheme(), url.path()),
- };
- // The first relay's route to a broadcast source wins while it has
- // one, whether it carries the same instance as the failover's or
- // another.
- let preference = match rank {
- 0 => moq_net::origin::Preference::default(),
- _ => moq_net::origin::Preference::new(-1),
- };
- let sources = subscribe.clone().with_preference(preference);
- let connection = client.with_publisher(origin).with_subscriber(sources).connect(url);
- let monitor = connection.monitor();
- (connection, health::Link { endpoint, monitor })
- })
- .unzip()
-}
-
/// What the program clock hands a rendition's encoder.
enum Video {
/// A copy of the program on its way to the encoder, to encode at this
@@ -3402,16 +3063,50 @@ mod tests {
assert!(unwritten.iter().all(|key| *key == scene), "{unwritten:?}");
}
- /// How the tests start a studio, live on `relays`.
- fn launch(gpu: Gpu, relays: Vec, recordings: &Path) -> Launch {
- Launch {
+ /// Start a studio recording into `recordings`, and go live on `relays`
+ /// as `studio` unless there are none.
+ async fn start(
+ gpu: Gpu,
+ relays: Vec,
+ recordings: &Path,
+ autosave: Autosave,
+ loaded: autosave::Loaded,
+ ) -> anyhow::Result> {
+ let launch = Launch {
gpu,
- relays,
- broadcast: "studio".into(),
- on_demand: false,
recordings: recordings.to_path_buf(),
room: None,
+ };
+ let engine = Engine::start(launch, autosave, loaded).await?;
+ if !relays.is_empty() {
+ let destination = Destination {
+ relays,
+ broadcast: "studio".into(),
+ on_demand: false,
+ };
+ engine.go_live(destination).await?;
}
+ Ok(engine)
+ }
+
+ /// A relay of its own, running, with a client that trusts it, what it
+ /// routes, and the config that makes another client for it.
+ async fn test_relay() -> (Relay, moq_net::origin::Consumer, moq_tokio::connect::Config) {
+ let _ = moq_tokio::crypto::install_default();
+ let fixture = moq_relay::test_relay().await.expect("test relay");
+ let ready = fixture.relay.ready();
+ let routes = fixture.relay.cluster().origin.consume();
+ tokio::spawn(fixture.relay.run());
+ ready.wait().await.expect("relay ready");
+ let mut client = moq_tokio::connect::Config::default();
+ client.tls.fingerprint = vec![fixture.fingerprint.clone()];
+ let config = client;
+ let client = config.clone().init(Default::default()).expect("client");
+ let relay = Relay {
+ client,
+ url: fixture.url,
+ };
+ (relay, routes, config)
}
/// A small canvas filled red, at two sizes.
@@ -3519,11 +3214,12 @@ mod tests {
let mut two = loaded.collection.scenes[0].clone();
two.name = "Two".into();
loaded.collection.scenes.push(two);
- let engine = Engine::start(launch(gpu, Vec::new(), dir.path()), autosave, loaded)
+ let (relay, _routes, _) = test_relay().await;
+ let engine = start(gpu, vec![relay], dir.path(), autosave, loaded)
.await
- .expect("the studio starts");
+ .expect("the studio goes live");
- let broadcast = engine.program.publication.broadcast().consume();
+ let broadcast = engine.on_air_program.borrow().clone().expect("on air");
let mut catalogs = moq_mux::catalog::Consumer::<()>::new(&broadcast, Default::default())
.await
.expect("the program's catalog");
@@ -3602,10 +3298,10 @@ mod tests {
let dir = tempfile::tempdir().unwrap();
let gpu = Gpu::new(wgpu::Backends::all()).await.expect("a GPU");
let (autosave, _) = Autosave::open(dir.path().join("show.toml")).unwrap();
- let engine = Engine::start(launch(gpu, Vec::new(), dir.path()), autosave, red_canvas())
+ let engine = start(gpu, Vec::new(), dir.path(), autosave, red_canvas())
.await
.expect("the studio starts");
- assert!(engine.output.is_none());
+ assert_eq!(engine.on_air(), OnAir::Off);
assert_eq!(recorded(&engine), 0);
assert_eq!(
engine.stats.sessions.open(),
@@ -3727,7 +3423,7 @@ mod tests {
loaded.collection.video.insert("sting".into(), video);
let gpu = Gpu::new(wgpu::Backends::all()).await.expect("a GPU");
let (autosave, _) = Autosave::open(dir.path().join("show.toml")).unwrap();
- let engine = Engine::start(launch(gpu, Vec::new(), dir.path()), autosave, loaded)
+ let engine = start(gpu, Vec::new(), dir.path(), autosave, loaded)
.await
.expect("the studio starts");
let status = |name: &str| engine.state.lock().unwrap().sources.get(name).map(source::Feed::status);
@@ -3817,7 +3513,7 @@ mod tests {
.extend([moq_scene::Scene::new("Guest"), brb, portrait]);
let gpu = Gpu::new(wgpu::Backends::all()).await.expect("a GPU");
let (autosave, _) = Autosave::open(dir.path().join("show.toml")).unwrap();
- let engine = Engine::start(launch(gpu, Vec::new(), dir.path()), autosave, loaded)
+ let engine = start(gpu, Vec::new(), dir.path(), autosave, loaded)
.await
.expect("the studio starts");
{
@@ -3893,7 +3589,7 @@ mod tests {
collection.scenes.extend([moq_scene::Scene::new("Guest"), portrait]);
let gpu = Gpu::new(wgpu::Backends::all()).await.expect("a GPU");
let (autosave, _) = Autosave::open(dir.path().join("show.toml")).unwrap();
- let engine = Engine::start(launch(gpu, Vec::new(), dir.path()), autosave, loaded)
+ let engine = start(gpu, Vec::new(), dir.path(), autosave, loaded)
.await
.expect("the studio starts");
{
@@ -3975,7 +3671,19 @@ mod tests {
// A scene's key, from the hotkey thread or a button, cues in Studio
// mode and takes otherwise.
- let controls = crate::controls::Controls::new(engine.clone(), tokio::runtime::Handle::current());
+ let stream = crate::stream::Stream::load(
+ &dir.path().join("show.toml"),
+ Default::default(),
+ Default::default(),
+ Default::default(),
+ crate::stream::Credentials::memory(),
+ )
+ .unwrap();
+ let controls = Arc::new(crate::controls::Controls::new(
+ engine.clone(),
+ stream,
+ tokio::runtime::Handle::current(),
+ ));
controls.set_studio(true);
controls.scene(&mut engine.state.lock().unwrap(), "Guest").unwrap();
{
@@ -4344,7 +4052,7 @@ mod tests {
],
..moq_scene::Scene::new("tall")
});
- let engine = Engine::start(launch(gpu, relays, dir.path()), autosave, loaded)
+ let engine = start(gpu, relays, dir.path(), autosave, loaded)
.await
.expect("the studio starts");
@@ -4681,7 +4389,7 @@ mod tests {
sounds: ["bed".to_string()].into(),
..moq_scene::AudioScene::new("Portrait")
});
- let engine = Engine::start(launch(gpu, relays, dir.path()), autosave, loaded)
+ let engine = start(gpu, relays, dir.path(), autosave, loaded)
.await
.expect("the studio starts");
@@ -4805,7 +4513,7 @@ mod tests {
let camera = moq_net::Path::new("alice/camera.hang").unwrap();
room.observe(&camera, true, &Collection::new(), &History::new());
let origin = moq_tokio::origin::spawn();
- let program = engine.program.publication.broadcast().consume();
+ let program = engine.on_air_program.subscribe();
let clock = engine.program.clock.clone();
let mut feeds = crate::feed::Feeds::new(room, origin.clone(), program, clock, Duration::ZERO).unwrap();
{
@@ -4873,7 +4581,7 @@ mod tests {
client,
url: fixture.url.clone(),
}];
- let engine = Engine::start(launch(gpu, relays, dir.path()), autosave, red_canvas())
+ let engine = start(gpu, relays, dir.path(), autosave, red_canvas())
.await
.expect("the studio starts");
assert!(
@@ -5196,7 +4904,9 @@ mod tests {
let origin = moq_tokio::origin::spawn();
let subscribe = moq_tokio::origin::spawn();
- let (_connections, links) = connect(relays, &origin, &subscribe);
+ let studio = moq_net::Path::new("studio").unwrap();
+ let (_connections, links) = air::publish(relays.clone(), &origin, &studio);
+ let _watching = air::subscribe(&relays, &subscribe);
let publication = origin.route("studio", Default::default()).unwrap();
let instance = publication.instance();
@@ -5282,7 +4992,305 @@ mod tests {
.expect("both relays route the program, the failover the sources");
}
- /// Closing the output as quitting does delivers the tail of every finished
+ /// A stream ended before the program drew a frame of it, as at a low
+ /// frame rate, ends with its encoders rather than running on off air.
+ #[tokio::test(flavor = "multi_thread")]
+ #[ignore = "needs a Vulkan device and an H.264 encoder; run by just render-sw"]
+ async fn ending_before_the_first_frame_ends_the_stream() {
+ let (relay, _routes, _config) = test_relay().await;
+ let dir = tempfile::tempdir().unwrap();
+ let gpu = Gpu::new(wgpu::Backends::all()).await.expect("a GPU");
+ let (autosave, _) = Autosave::open(dir.path().join("show.toml")).unwrap();
+ let mut canvas = red_canvas();
+ canvas.collection.canvas.framerate = 1;
+ let engine = start(gpu, vec![relay], dir.path(), autosave, canvas)
+ .await
+ .expect("the studio goes live");
+ kio::clock::timeout(TIMEOUT, engine.end_stream())
+ .await
+ .expect("the stream ends in time")
+ .expect("each relay takes the stream's end");
+
+ let rendered = || engine.stats.rendered.load(Ordering::Relaxed);
+ let since = rendered();
+ kio::clock::timeout(TIMEOUT, async {
+ while rendered() < since + 2 {
+ kio::clock::sleep(Duration::from_millis(100)).await;
+ }
+ })
+ .await
+ .expect("the program draws on off air");
+ assert_eq!(engine.on_air(), OnAir::Off);
+ assert_eq!(engine.stats.sessions.open(), 0, "no encoder runs off air");
+ engine.stop();
+ kio::clock::timeout(TIMEOUT, engine.close())
+ .await
+ .expect("the studio closes");
+ }
+
+ /// Ending the stream ends the program's video and then its catalog cleanly
+ /// for a viewer at the relay, rather than cutting them, and the studio
+ /// runs on off air, where a replay of the program has nothing to copy and
+ /// says so. Going live again announces the program anew.
+ #[tokio::test(flavor = "multi_thread")]
+ #[ignore = "needs a Vulkan device and an H.264 encoder; run by just render-sw"]
+ async fn ending_the_stream_delivers_its_end_and_goes_live_again() {
+ let (relay, routes, _config) = test_relay().await;
+ let dir = tempfile::tempdir().unwrap();
+ let gpu = Gpu::new(wgpu::Backends::all()).await.expect("a GPU");
+ let (autosave, _) = Autosave::open(dir.path().join("show.toml")).unwrap();
+ let show = relay.clone();
+ let engine = start(gpu, vec![relay], dir.path(), autosave, red_canvas())
+ .await
+ .expect("the studio goes live");
+ let since = engine.on_air().live().expect("on air").since;
+ assert_eq!(engine.aired(), None, "nothing has gone off air yet");
+ assert!(engine.program_targets().is_ok(), "the program has a past on air");
+ let (broadcast, catalogs, catalog) = routed(&routes, "studio").await;
+ let video = catalog.video.renditions.keys().next().expect("its video").clone();
+ let viewer = served(&broadcast, &video).await;
+
+ kio::clock::timeout(TIMEOUT, engine.end_stream())
+ .await
+ .expect("the stream ends in time")
+ .expect("each relay takes the stream's end");
+ assert_eq!(engine.on_air(), OnAir::Off);
+ // Kept by the engine as the stream ends, whether or not a window saw it.
+ let aired = engine.aired().expect("the stream is noted as it ends");
+ assert_eq!(aired.since, since);
+ assert!(aired.lasted <= since.elapsed(), "{aired:?}");
+ ended(catalogs, viewer).await;
+ let refused = engine
+ .program_targets()
+ .err()
+ .expect("off air, the program has no past");
+ assert!(format!("{refused:#}").contains("on air"), "{refused:#}");
+ assert!(engine.end_stream().await.is_err(), "nothing is on air to end");
+
+ // The show's client outlives the stream: the next one dials from it.
+ let destination = Destination {
+ relays: vec![show],
+ broadcast: "studio".into(),
+ on_demand: false,
+ };
+ engine.go_live(destination).await.expect("the studio goes live again");
+ let (_, _, again) = routed(&routes, "studio").await;
+ assert!(!again.video.renditions.is_empty(), "the program is announced anew");
+ engine.stop();
+ kio::clock::timeout(TIMEOUT, engine.close())
+ .await
+ .expect("the studio closes");
+ let failed = engine.stats.error();
+ assert!(failed.is_none(), "{failed:?}");
+ }
+
+ /// Going live to a relay that never answers fails within the dial's
+ /// bound, naming the relay, and leaves the studio off air to try again.
+ #[tokio::test(flavor = "multi_thread")]
+ #[ignore = "needs a Vulkan device; run by just render-sw"]
+ async fn going_live_to_an_unreachable_relay_fails() {
+ let _ = moq_tokio::crypto::install_default();
+ let dir = tempfile::tempdir().unwrap();
+ let gpu = Gpu::new(wgpu::Backends::all()).await.expect("a GPU");
+ let (autosave, _) = Autosave::open(dir.path().join("show.toml")).unwrap();
+ let engine = start(gpu, Vec::new(), dir.path(), autosave, red_canvas())
+ .await
+ .expect("the studio starts");
+ assert_eq!(engine.on_air(), OnAir::Off);
+ // Nothing listens on the discard port.
+ let client = moq_tokio::connect::Config::default()
+ .init(Default::default())
+ .expect("client");
+ let destination = Destination {
+ relays: vec![Relay {
+ client,
+ url: "https://127.0.0.1:9/".parse().unwrap(),
+ }],
+ broadcast: "studio".into(),
+ on_demand: false,
+ };
+ let refused = kio::clock::timeout(TIMEOUT, engine.go_live(destination))
+ .await
+ .expect("going live gives up in time")
+ .expect_err("nothing answers");
+ assert!(format!("{refused:#}").contains("127.0.0.1:9"), "{refused:#}");
+ assert_eq!(engine.on_air(), OnAir::Off);
+ engine.stop();
+ kio::clock::timeout(TIMEOUT, engine.close())
+ .await
+ .expect("the studio closes");
+ }
+
+ /// A go-live, from a hotkey as from the sheet, waits out an encoder check
+ /// still running, such as a closed sheet's, whose hardware session the
+ /// program's encoders would count against, before it opens anything, and
+ /// admits no new one, such as a canvas change's, until it ends.
+ #[tokio::test(flavor = "multi_thread")]
+ #[ignore = "needs a Vulkan device; run by just render-sw"]
+ async fn going_live_waits_out_an_encoder_check() {
+ let dir = tempfile::tempdir().unwrap();
+ let gpu = Gpu::new(wgpu::Backends::all()).await.expect("a GPU");
+ let (autosave, _) = Autosave::open(dir.path().join("show.toml")).unwrap();
+ let engine = start(gpu, Vec::new(), dir.path(), autosave, red_canvas())
+ .await
+ .expect("the studio starts");
+ // No relay, so the go-live is refused at once once it reads where to go.
+ let stream = crate::stream::Stream::load(
+ &dir.path().join("show.toml"),
+ Default::default(),
+ Default::default(),
+ Default::default(),
+ crate::stream::Credentials::memory(),
+ )
+ .unwrap();
+ let controls = Arc::new(crate::controls::Controls::new(
+ engine.clone(),
+ stream,
+ tokio::runtime::Handle::current(),
+ ));
+ let probing = controls.probing().expect("a check is admitted off air");
+ controls.go_live().expect("the go-live starts");
+ kio::clock::sleep(Duration::from_millis(300)).await;
+ assert!(controls.going(), "the go-live waits for the check");
+ assert_eq!(*controls.refused.lock().unwrap(), None);
+ assert!(controls.probing().is_none(), "no new check while going live");
+ drop(probing);
+ kio::clock::timeout(TIMEOUT, async {
+ while controls.going() {
+ kio::clock::sleep(Duration::from_millis(10)).await;
+ }
+ })
+ .await
+ .expect("the go-live goes on once the check ends");
+ assert!(controls.refused.lock().unwrap().is_some(), "and reads where to go");
+ assert!(controls.probing().is_some(), "checks are admitted once it ends");
+ engine.stop();
+ kio::clock::timeout(TIMEOUT, engine.close())
+ .await
+ .expect("the studio closes");
+ }
+
+ /// A follow that can set up none of the show's relays, such as a relay
+ /// and a failover on one fixed `--connect-bind` port, keeps the sessions
+ /// followed before, and with them the broadcast sources and guests.
+ #[tokio::test(flavor = "multi_thread")]
+ #[ignore = "needs a Vulkan device; run by just render-sw"]
+ async fn a_follow_that_sets_up_no_relay_keeps_the_last() {
+ let _ = moq_tokio::crypto::install_default();
+ let dir = tempfile::tempdir().unwrap();
+ let gpu = Gpu::new(wgpu::Backends::all()).await.expect("a GPU");
+ let (autosave, _) = Autosave::open(dir.path().join("show.toml")).unwrap();
+ let engine = start(gpu, Vec::new(), dir.path(), autosave, red_canvas())
+ .await
+ .expect("the studio starts");
+ let scene = dir.path().join("show.toml");
+ let follow = |client: moq_tokio::connect::Config, saved: crate::stream::Saved| {
+ let (engine, scene) = (engine.clone(), scene.clone());
+ let runtime = tokio::runtime::Handle::current();
+ tokio::task::spawn_blocking(move || {
+ let _runtime = runtime.enter();
+ let mut stream = crate::stream::Stream::load(
+ &scene,
+ Default::default(),
+ client,
+ Default::default(),
+ crate::stream::Credentials::memory(),
+ )
+ .unwrap();
+ stream.save(saved).unwrap();
+ crate::show::follow(&engine, &stream)
+ })
+ };
+ let watched = || -> Vec { engine.watched().into_iter().map(|(relay, _)| relay.endpoint).collect() };
+ let relay = |port: u16| Some(format!("https://127.0.0.1:{port}/").parse().unwrap());
+
+ let first = crate::stream::Saved {
+ relay: relay(9),
+ ..Default::default()
+ };
+ assert!(
+ follow(Default::default(), first).await.unwrap(),
+ "every relay is set up"
+ );
+ assert_eq!(watched(), ["https://127.0.0.1:9"]);
+
+ let mut fixed = moq_tokio::connect::Config::default();
+ fixed.bind = Some("127.0.0.1:4999".parse().unwrap());
+ let unset = crate::stream::Saved {
+ relay: relay(10),
+ failover: relay(11),
+ ..Default::default()
+ };
+ assert!(!follow(fixed, unset).await.unwrap(), "the follow says it failed");
+ assert_eq!(watched(), ["https://127.0.0.1:9"], "the last relay is kept");
+
+ engine.stop();
+ kio::clock::timeout(TIMEOUT, engine.close())
+ .await
+ .expect("the studio closes");
+ }
+
+ /// The show's first follow of its relays is ordered with a save's, so
+ /// relays saved while it may still be reading its tokens are the ones
+ /// followed, never replaced by the opening settings landing late.
+ #[tokio::test(flavor = "multi_thread")]
+ #[ignore = "needs a Vulkan device; run by just render-sw"]
+ async fn the_first_follow_never_lands_after_a_save() {
+ let _ = moq_tokio::crypto::install_default();
+ let dir = tempfile::tempdir().unwrap();
+ let gpu = Gpu::new(wgpu::Backends::all()).await.expect("a GPU");
+ let (autosave, _) = Autosave::open(dir.path().join("show.toml")).unwrap();
+ let engine = start(gpu, Vec::new(), dir.path(), autosave, red_canvas())
+ .await
+ .expect("the studio starts");
+ let mut stream = crate::stream::Stream::load(
+ &dir.path().join("show.toml"),
+ Default::default(),
+ Default::default(),
+ Default::default(),
+ crate::stream::Credentials::memory(),
+ )
+ .unwrap();
+ let relay = |port: u16| crate::stream::Saved {
+ relay: Some(format!("https://127.0.0.1:{port}/").parse().unwrap()),
+ ..Default::default()
+ };
+ let saving = tokio::task::spawn_blocking(move || {
+ stream.save(relay(9)).unwrap();
+ stream
+ });
+ let mut stream = saving.await.unwrap();
+ let controls = Arc::new(crate::controls::Controls::new(
+ engine.clone(),
+ stream.clone(),
+ tokio::runtime::Handle::current(),
+ ));
+ let first = controls.follow_relays().expect("the follow starts");
+ let saved = tokio::task::spawn_blocking(move || {
+ stream.save(relay(10)).unwrap();
+ controls.set_stream(stream, true);
+ first.join().expect("the first follow ran");
+ });
+ saved.await.unwrap();
+ // The first follow is done, so only the save's can land after it.
+ let followed = kio::clock::timeout(TIMEOUT, async {
+ loop {
+ let watched: Vec = engine.watched().into_iter().map(|(relay, _)| relay.endpoint).collect();
+ if watched == ["https://127.0.0.1:10"] {
+ return;
+ }
+ kio::clock::sleep(Duration::from_millis(10)).await;
+ }
+ })
+ .await;
+ assert!(followed.is_ok(), "watched {:?}", engine.watched());
+ engine.stop();
+ kio::clock::timeout(TIMEOUT, engine.close())
+ .await
+ .expect("the studio closes");
+ }
+
+ /// Closing the sessions as ending the stream does delivers the tail of every finished
/// track before the relay session goes: a subscriber at the relay reads
/// each track's last group whole and then its end, and the broadcast is
/// withdrawn. Each last group is still on its way as the close starts, so
@@ -5306,20 +5314,12 @@ mod tests {
let client = client.init(Default::default()).expect("client");
let origin = moq_tokio::origin::spawn();
- let (connections, _links) = connect(
- vec![Relay {
- client: client.clone(),
- url: fixture.url.clone(),
- }],
- &origin,
- &moq_tokio::origin::spawn(),
- );
- let output = Output {
- path: "studio".into(),
- on_demand: false,
- connections,
- clients: vec![client],
- };
+ let studio = moq_net::Path::new("studio").unwrap();
+ let relays = vec![Relay {
+ client: client.clone(),
+ url: fixture.url.clone(),
+ }];
+ let (connections, _links) = air::publish(relays, &origin, &studio);
let publication = origin.route("studio", Default::default()).unwrap();
let info = || moq_net::track::Info::default().with_timescale(moq_net::Timescale::MILLI);
let tracks = ["video", "audio"].map(|name| publication.broadcast().create_track(name, info()).unwrap());
@@ -5393,9 +5393,9 @@ mod tests {
// The kept producer holds the finished track listed.
drop(track.clone().finish().unwrap());
}
- kio::clock::timeout(TIMEOUT, output.close())
+ air::close(&connections)
.await
- .expect("the output closes");
+ .expect("each relay takes the tail before its session closes");
for reader in readers {
kio::clock::timeout(TIMEOUT, reader)
.await
@@ -5526,7 +5526,7 @@ mod tests {
items: vec![moq_scene::Item::new("blue", fill)],
..moq_scene::Scene::new("tall")
});
- let engine = Engine::start(launch(gpu, Vec::new(), dir.path()), autosave, loaded)
+ let engine = start(gpu, Vec::new(), dir.path(), autosave, loaded)
.await
.expect("the studio starts");
engine.set_camera(None, true).expect("the camera turns on");
diff --git a/rs/moq-studio/src/engine/air.rs b/rs/moq-studio/src/engine/air.rs
new file mode 100644
index 00000000..25e6348e
--- /dev/null
+++ b/rs/moq-studio/src/engine/air.rs
@@ -0,0 +1,798 @@
+//! Going live and ending the stream.
+//!
+//! Off air the studio composes, previews, and records with no relay session
+//! that publishes. Going live dials a session to each relay, announces the
+//! program's broadcast on them, and opens its encoders; ending the stream
+//! ends every track it lists, so each encoder publishes what it holds, and
+//! closes the sessions once the relays have the broadcast's tail, as quitting
+//! does. Each time on air is a broadcast of its own, with its own instance.
+//!
+//! Broadcast sources, guests, and viewers' reports arrive through sessions
+//! of their own, which only subscribe, so they are seen off air too.
+
+use super::*;
+
+/// The longest going live waits for a relay to take the session.
+const DIAL: Duration = Duration::from_secs(15);
+
+/// Where the program goes live.
+#[derive(Clone)]
+pub struct Destination {
+ /// The relays to publish to, the preferred first. Their clients are the
+ /// show's, which outlive the stream, so going live again dials from them.
+ pub relays: Vec,
+ /// The broadcast path on them.
+ pub broadcast: String,
+ /// Whether the program video's canvas size is copied and encoded only
+ /// while someone watches it, rather than all the time. The half size
+ /// always is.
+ pub on_demand: bool,
+}
+
+/// Whether the program is on air.
+#[derive(Clone, Debug, PartialEq)]
+pub enum OnAir {
+ Off,
+ /// Dialing the relays and opening the encoders.
+ Starting,
+ Live(Live),
+ /// Handing the relays the broadcast's tail.
+ Ending,
+}
+
+impl OnAir {
+ /// The stream on air, if it is.
+ pub fn live(&self) -> Option<&Live> {
+ match self {
+ Self::Live(live) => Some(live),
+ _ => None,
+ }
+ }
+}
+
+/// The stream on air.
+#[derive(Clone, Debug, PartialEq)]
+pub struct Live {
+ /// The broadcast path on the relays.
+ pub path: String,
+ /// Whether the canvas size encodes only while someone watches.
+ pub on_demand: bool,
+ /// When it went live.
+ pub since: Instant,
+ /// Each relay's scheme, host, and port, the preferred first.
+ pub relays: Vec,
+}
+
+/// A stream that has gone off air.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct Aired {
+ /// When it went live.
+ pub since: Instant,
+ /// How long it was on air, until END STREAM.
+ pub lasted: Duration,
+}
+
+/// The engine's stream.
+pub(super) enum Slot {
+ Off,
+ Starting,
+ Live(Box),
+ Ending,
+}
+
+/// A stream on air: the sessions publishing it and what its broadcast lists
+/// beside the video, which the program clock encodes ([`Lines`]).
+pub(super) struct Air {
+ live: Live,
+ /// A session to each relay, which reconnects on its own. Their clients
+ /// are the show's, which its watching sessions and the next stream share.
+ connections: Vec,
+ /// The program's broadcast, which closes once dropped.
+ publication: moq_net::origin::Route,
+ catalog: moq_scene::publish::Catalog,
+ /// Ends the program audio, which then finishes its track.
+ audio_stop: tokio::sync::oneshot::Sender<()>,
+ audio: tokio::task::JoinHandle<()>,
+ /// Its audio's claim on the connection's bandwidth, which it does not
+ /// follow: Opus at this rate is never what congests the uplink. Listed
+ /// under its track's name in [`Stats::grants`].
+ _audio_grant: bandwidth::Reservation,
+ audio_track: String,
+ /// Copies the sources published independently into the broadcast.
+ forward: tokio::task::JoinHandle<()>,
+ /// Dropped, the copies finish their tracks and the forwarding ends.
+ forward_stop: tokio::sync::watch::Sender<()>,
+ /// Where the broadcasts are announced, which the sessions publish.
+ _origin: moq_net::origin::Producer,
+}
+
+impl Drop for Air {
+ fn drop(&mut self) {
+ self.forward.abort();
+ self.publication.broadcast().close();
+ }
+}
+
+/// What the program clock feeds while on air.
+pub(super) struct Lines {
+ /// The program's.
+ pub main: Line,
+ /// What starts the line of each published scene.
+ pub extras: Extras,
+ /// The published scenes' lines.
+ pub lines: super::Lines,
+ /// Where and when the broadcast's viewers show the sources published
+ /// independently, while any is.
+ pub layout: moq_scene::layout::Publisher,
+}
+
+impl Lines {
+ /// Stop handing every line frames, so each encode thread publishes what
+ /// it holds and finishes its track.
+ pub fn end(self) -> Ending {
+ let mut ended = vec![self.main.end(None)];
+ ended.extend(
+ self.lines
+ .into_values()
+ .flatten()
+ .map(|(line, broadcast)| line.end(Some(broadcast))),
+ );
+ Ending {
+ ended,
+ layout: Some(self.layout),
+ }
+ }
+}
+
+/// What ending a stream leaves running, to wait on before the sessions close.
+#[derive(Default)]
+pub(super) struct Ending {
+ pub ended: Vec,
+ /// Finished once the video it lays out has ended.
+ pub layout: Option,
+}
+
+/// What the program clock takes from going live and gives back on ending,
+/// at its next tick.
+#[derive(Default)]
+pub(super) struct Handoff {
+ start: Option,
+ end: Option>,
+ /// Set once the program clock stops, after which nothing is taken.
+ closed: bool,
+}
+
+impl Handoff {
+ /// What to start and whether to end, this tick.
+ pub fn take(&mut self) -> (Option, Option>) {
+ (self.start.take(), self.end.take())
+ }
+
+ /// Take nothing more, handing back what was waiting.
+ pub fn close(&mut self) -> (Option, Option>) {
+ self.closed = true;
+ self.take()
+ }
+}
+
+/// A session to a relay that only subscribes.
+pub(super) struct Watch {
+ pub(super) connection: moq_tokio::Connection,
+ pub(super) client: moq_tokio::Client,
+ pub(super) link: health::Link,
+}
+
+/// Dial a session to each of `relays` that publishes what `origin` announces,
+/// such as the broadcast at `path`, with each one's link.
+pub(super) fn publish(
+ relays: Vec,
+ origin: &moq_net::origin::Producer,
+ path: &moq_net::Path,
+) -> (Vec, Vec) {
+ relays
+ .into_iter()
+ .map(|Relay { client, url }| {
+ tracing::info!(url = %moq_tokio::RedactedUrl::new(&url), broadcast = %path, "going live");
+ let endpoint = endpoint(&url);
+ let connection = client.with_publisher(origin).connect(url);
+ let monitor = connection.monitor();
+ (connection, health::Link { endpoint, monitor })
+ })
+ .unzip()
+}
+
+/// Dial a session to each of `relays` that only subscribes, into
+/// `subscribe`, the first relay's routes preferred.
+pub(super) fn subscribe(relays: &[Relay], subscribe: &moq_net::origin::Producer) -> Vec {
+ relays
+ .iter()
+ .enumerate()
+ .map(|(rank, relay)| {
+ // The first relay's route to a broadcast wins while it has one,
+ // whether it carries the same instance as the failover's or another.
+ let preference = match rank {
+ 0 => moq_net::origin::Preference::default(),
+ _ => moq_net::origin::Preference::new(-1),
+ };
+ let subscribe = subscribe.clone().with_preference(preference);
+ let connection = relay
+ .client
+ .clone()
+ .with_subscriber(subscribe)
+ .connect(relay.url.clone());
+ let link = health::Link {
+ endpoint: endpoint(&relay.url),
+ monitor: connection.monitor(),
+ };
+ Watch {
+ connection,
+ client: relay.client.clone(),
+ link,
+ }
+ })
+ .collect()
+}
+
+/// Close each of `connections` once its relay has what it was sent, bounded
+/// at [`CLOSE`]. Their clients stay open: the show's watching sessions share
+/// them, and its endpoint, still running, sends each close to the relay.
+pub(super) async fn close(connections: &[moq_tokio::Connection]) -> anyhow::Result<()> {
+ let mut closing = tokio::task::JoinSet::new();
+ for connection in connections {
+ closing.spawn(connection.clone().close());
+ }
+ let drained = kio::clock::timeout(CLOSE, closing.join_all())
+ .await
+ .map_err(|_| anyhow::anyhow!("the relays did not take the stream's end within {CLOSE:?}"))?;
+ for drained in drained {
+ drained.context("a relay session closed without taking the stream's end")?;
+ }
+ Ok(())
+}
+
+impl Engine {
+ /// Whether the program is on air.
+ pub fn on_air(&self) -> OnAir {
+ self.on_air.borrow().clone()
+ }
+
+ /// The last stream to go off air, kept as it ends rather than as the
+ /// window sees it, since hotkeys go live and end while it draws nothing.
+ pub fn aired(&self) -> Option {
+ *self.aired.lock().unwrap()
+ }
+
+ /// Whether the program is on air, as it changes.
+ pub fn on_air_changes(&self) -> tokio::sync::watch::Receiver {
+ self.on_air.subscribe()
+ }
+
+ /// The GPU the program is drawn on, which going live's encoder check
+ /// times encoders on.
+ pub fn gpu(&self) -> &Gpu {
+ &self.gpu
+ }
+
+ /// Move the stream to `slot`, and say so.
+ fn set_air(&self, slot: &mut Slot, to: Slot) {
+ let on_air = match &to {
+ Slot::Off => OnAir::Off,
+ Slot::Starting => OnAir::Starting,
+ Slot::Live(air) => OnAir::Live(air.live.clone()),
+ Slot::Ending => OnAir::Ending,
+ };
+ *slot = to;
+ self.on_air.send_replace(on_air);
+ }
+
+ /// Go live to `destination`: dial each relay, failing with why when none
+ /// takes the session, then open the program's encoders and announce its
+ /// broadcast, and each published scene's beneath it. The program audio
+ /// follows the audio scene on program, and guests' return feeds show the
+ /// program from now on.
+ ///
+ /// Call inside the tokio runtime that serves the relay connections.
+ pub async fn go_live(&self, destination: Destination) -> anyhow::Result<()> {
+ {
+ let mut slot = self.air.lock().unwrap();
+ match &*slot {
+ Slot::Off => {}
+ Slot::Starting => anyhow::bail!("already going live"),
+ Slot::Live(_) => anyhow::bail!("already live"),
+ Slot::Ending => anyhow::bail!("the last stream is still ending"),
+ }
+ anyhow::ensure!(!self.stopped.load(Ordering::Relaxed), "the studio is quitting");
+ self.set_air(&mut slot, Slot::Starting);
+ }
+ let lifted = self.lift(destination).await;
+ let mut slot = self.air.lock().unwrap();
+ match lifted {
+ Ok(air) => {
+ self.set_air(&mut slot, Slot::Live(Box::new(air)));
+ Ok(())
+ }
+ Err(err) => {
+ self.set_air(&mut slot, Slot::Off);
+ Err(err)
+ }
+ }
+ }
+
+ /// Dial `destination`'s relays and start the stream on them.
+ async fn lift(&self, destination: Destination) -> anyhow::Result {
+ let Destination {
+ relays,
+ broadcast,
+ on_demand,
+ } = destination;
+ anyhow::ensure!(!relays.is_empty(), "no relay to go live on");
+ let path =
+ moq_net::Path::new(broadcast.as_str()).with_context(|| format!("cannot publish at `{broadcast}`"))?;
+ let origin = moq_tokio::origin::spawn();
+ let (connections, links) = publish(relays, &origin, &path);
+ dial(&connections, &links).await?;
+
+ // Each track reserves its ceiling on the first relay's estimate, which
+ // that connection then probes to fund: the bandwidth the stream is
+ // granted. An allocator spans one connection, so the failover's link is
+ // never probed. The catalog claims nothing itself, which would count
+ // each track twice.
+ let bandwidth = connections[0].allocator();
+ let publication = origin
+ .route(&path, moq_net::origin::RouteOptions::hidden())
+ .context("cannot create the broadcast")?;
+ let mut broadcast = publication.broadcast().clone();
+ let clock = self.program.clock.clone();
+ let config = moq_mux::catalog::Config::default()
+ .with_catalog(Default::default())
+ .with_clock(clock.clone());
+ let catalog = moq_scene::publish::Catalog::new(&mut broadcast, config)?;
+ // Held until the video and audio are listed, so the first catalog
+ // carries both and the solicitation.
+ let reserved = catalog.reserve();
+ // Viewers report back beneath the broadcast.
+ health::solicit(&mut catalog.clone(), &path)?;
+ let renditions = Renditions {
+ gpu: self.gpu.clone(),
+ broadcast: broadcast.clone(),
+ timebase: catalog.timebase(),
+ record: false,
+ bandwidth: bandwidth.clone(),
+ on_demand,
+ live: true,
+ stats: self.stats.clone(),
+ video: self.stats.video.clone(),
+ grants: String::new(),
+ };
+ // Opened before announcing, so a canvas this machine cannot encode
+ // fails going live rather than going live without a picture. The
+ // program clock probes again if the canvas changes meanwhile.
+ let canvas = self.state.lock().unwrap().compositor.canvas();
+ let ladder = renditions.open(canvas).await;
+ let ladder = ladder.inspect_err(|err| self.stats.video.fail(format!("{err:#}")))?;
+ let main = Line::new(renditions, canvas, Some(ladder), true);
+
+ let (input, options) = audio_options();
+ let rate = input.sample_rate;
+ let mut audio = moq_audio::encode::Producer::new(&mut broadcast, catalog.clone(), input, &options)?;
+ let audio_grant = bandwidth.reserve(&audio.demand(), AUDIO_BITRATE);
+ let audio_track = audio.track_name().to_string();
+ self.stats
+ .grants
+ .lock()
+ .unwrap()
+ .insert(audio_track.clone(), audio_grant.consume());
+ drop(reserved);
+
+ // One instance on every relay, so a viewer moving between them resumes
+ // mid-group rather than restarting.
+ let instance = publication.instance();
+ if let Err(err) = publication.announce() {
+ // Taken back, as ending does, before saying why.
+ self.stats.grants.lock().unwrap().remove(&audio_track);
+ drop(main.end(None));
+ return Err(err).context("cannot announce the broadcast");
+ }
+
+ let (forward_stop, stop) = tokio::sync::watch::channel(());
+ let forward = {
+ let sources = self.program.sources.clone();
+ let forward = moq_scene::publish::Forward {
+ published: self.program.published.subscribe(),
+ broadcast: broadcast.clone(),
+ catalog: catalog.clone(),
+ bandwidth: bandwidth.clone(),
+ stop,
+ };
+ tokio::spawn(async move {
+ if let Err(err) = sources.forward(forward).await {
+ tracing::warn!(%err, "stopped publishing sources independently");
+ }
+ })
+ };
+ let reports = self.subscribe.consume();
+ let observer = hang::health::Observer::new(path.clone(), Some(instance));
+ let publisher = health::Publisher {
+ main: health::Broadcast::follow(observer, catalog.clone(), &reports)?,
+ scenes: health::Scenes::new(reports),
+ clock: clock.clone(),
+ relays: links.clone(),
+ sources: self.program.sources.clone(),
+ };
+ let extras = Extras {
+ origin: origin.clone(),
+ path: path.clone(),
+ clock,
+ epoch: self.program.epoch,
+ runtime: tokio::runtime::Handle::current(),
+ gpu: self.gpu.clone(),
+ bandwidth,
+ on_demand,
+ live: true,
+ stats: self.stats.clone(),
+ graded: Some(publisher.scenes.clone()),
+ };
+ let layout = moq_scene::layout::Publisher::new(broadcast.clone(), catalog.clone());
+
+ // The audio scene on program, encoded on the runtime, never the
+ // program clock's thread.
+ let mut follow = self.program_audio.follow();
+ // A stream before this one whose audio failed is not this one's.
+ *self.stats.audio_error.lock().unwrap() = None;
+ let (audio_stop, stopped) = tokio::sync::oneshot::channel::<()>();
+ let (stats, epoch) = (self.stats.clone(), self.program.epoch);
+ let audio = tokio::spawn(async move {
+ let encoded = tokio::select! {
+ encoded = mix::encode(&mut follow, &mut audio, epoch, rate) => encoded,
+ _ = stopped => Ok(()),
+ };
+ match encoded {
+ // Fails only once the broadcast closed, which ended the track.
+ Ok(()) => drop(audio.finish()),
+ Err(err) => {
+ stats.fail_audio(format!("{err:#}"));
+ // Readers see the audio fail rather than go away unfinished.
+ audio.abort(moq_net::StreamError::Internal.into());
+ }
+ }
+ });
+
+ let lines = Lines {
+ main,
+ extras,
+ lines: super::Lines::new(),
+ layout,
+ };
+ {
+ let mut handoff = self.handoff.lock().unwrap();
+ if handoff.closed {
+ drop(handoff);
+ // The program clock stopped meanwhile, so nothing will feed them.
+ let ending = lines.end();
+ for ended in ending.ended {
+ ended.join();
+ }
+ anyhow::bail!("the studio is quitting");
+ }
+ handoff.start = Some(lines);
+ }
+ self.on_air_program
+ .send_replace(Some(publication.broadcast().consume()));
+ *self.sampler.lock().unwrap() = health::Sampler::new(Some(publisher));
+
+ Ok(Air {
+ live: Live {
+ path: path.to_string(),
+ on_demand,
+ since: kio::clock::now(),
+ relays: links.into_iter().map(|link| link.endpoint).collect(),
+ },
+ connections,
+ publication,
+ catalog,
+ audio_stop,
+ audio,
+ _audio_grant: audio_grant,
+ audio_track,
+ forward,
+ forward_stop,
+ _origin: origin,
+ })
+ }
+
+ /// End the stream: each encoder publishes what it holds and finishes its
+ /// track, the broadcast's catalog finishes, and each relay session closes
+ /// once the relay has received what it was sent, so a viewer or a
+ /// recording on the relay reaches the end rather than a cut. Bounded at
+ /// [`CLOSE`] for the sessions. Fails when nothing is on air.
+ pub async fn end_stream(&self) -> anyhow::Result<()> {
+ let air = {
+ let mut slot = self.air.lock().unwrap();
+ let air = match std::mem::replace(&mut *slot, Slot::Off) {
+ Slot::Live(air) => air,
+ other => {
+ let why = match &other {
+ Slot::Off => "not live",
+ Slot::Starting => "still going live",
+ Slot::Live(_) => unreachable!("matched above"),
+ Slot::Ending => "already ending",
+ };
+ *slot = other;
+ anyhow::bail!(why);
+ }
+ };
+ self.set_air(&mut slot, Slot::Ending);
+ *self.aired.lock().unwrap() = Some(Aired {
+ since: air.live.since,
+ lasted: air.live.since.elapsed(),
+ });
+ air
+ };
+ let ending = {
+ let mut handoff = self.handoff.lock().unwrap();
+ match handoff.closed {
+ // The program clock ended the encoders as it stopped.
+ true => None,
+ false => {
+ let (reply, ending) = tokio::sync::oneshot::channel();
+ handoff.end = Some(reply);
+ Some(ending)
+ }
+ }
+ };
+ let ending = match ending {
+ // The program clock stopping meanwhile drops the reply, having
+ // ended the encoders itself.
+ Some(ending) => ending.await.unwrap_or_default(),
+ None => Ending::default(),
+ };
+ let landed = self.land(*air, ending).await;
+ let mut slot = self.air.lock().unwrap();
+ self.set_air(&mut slot, Slot::Off);
+ landed
+ }
+
+ /// Finish `air` once its encoders, `ending`, have, then close its
+ /// sessions.
+ async fn land(&self, mut air: Air, ending: Ending) -> anyhow::Result<()> {
+ let Ending { ended, layout } = ending;
+ let joined = tokio::task::spawn_blocking(move || {
+ for ended in ended {
+ ended.join();
+ }
+ });
+ if joined.await.is_err() {
+ tracing::error!("waiting on the encoders panicked");
+ }
+ if let Some(mut layout) = layout {
+ finish_layout(&mut layout);
+ }
+ // Dropped, so the audio finishes its track.
+ let (stop, _) = tokio::sync::oneshot::channel();
+ drop(std::mem::replace(&mut air.audio_stop, stop));
+ if (&mut air.audio).await.is_err() {
+ tracing::error!("the program audio panicked");
+ }
+ // Dropped, so each source's copied track finishes after what it has.
+ drop(std::mem::replace(
+ &mut air.forward_stop,
+ tokio::sync::watch::channel(()).0,
+ ));
+ if (&mut air.forward).await.is_err() {
+ tracing::error!("publishing the sources independently panicked");
+ }
+ self.stats.grants.lock().unwrap().remove(&air.audio_track);
+ // Fails only once the catalog failed, which ended its tracks already.
+ let _ = air.catalog.clone().finish();
+ self.on_air_program.send_replace(None);
+ *self.sampler.lock().unwrap() = health::Sampler::new(None);
+
+ close(&air.connections).await
+ }
+
+ /// What a replay of the program could copy: its video at each size,
+ /// largest first, with its audio, up to now. It has a past only on air,
+ /// as the relay's copy of it.
+ pub fn program_targets(&self) -> anyhow::Result> {
+ let slot = self.air.lock().unwrap();
+ let Slot::Live(air) = &*slot else {
+ anyhow::bail!("the program has a past to replay only while on air");
+ };
+ let catalog = air.catalog.snapshot();
+ let broadcast = air.publication.broadcast().consume();
+ let end = self.program.clock.now();
+ let target = |video, audio| moq_scene::replay::Target {
+ broadcast: broadcast.clone(),
+ video,
+ audio,
+ end,
+ };
+ let audio = catalog.audio.renditions.into_iter().next();
+ let mut video: Vec<_> = catalog.video.renditions.into_iter().collect();
+ video.sort_by_key(|(_, config)| {
+ let pixels = u64::from(config.coded_width.unwrap_or(0)) * u64::from(config.coded_height.unwrap_or(0));
+ std::cmp::Reverse(pixels)
+ });
+ Ok(match video.is_empty() {
+ true => vec![target(None, audio)],
+ false => video
+ .into_iter()
+ .map(|video| target(Some(video), audio.clone()))
+ .collect(),
+ })
+ }
+
+ /// Subscribe to broadcast sources, guests, and viewers' reports through
+ /// `relays`, the preferred first, instead of those followed before.
+ pub fn watch(&self, relays: &[Relay]) {
+ let watching = subscribe(relays, &self.subscribe);
+ // Each dropped stops dialing, and its session goes.
+ drop(std::mem::replace(&mut *self.watching.lock().unwrap(), watching));
+ }
+
+ /// Each relay [watched](Self::watch), with its state and round trip.
+ pub fn watched(&self) -> Vec<(health::Relay, Option)> {
+ let watching = self.watching.lock().unwrap();
+ watching
+ .iter()
+ .map(|watch| {
+ let rtt = watch.link.monitor.stats().and_then(|stats| stats.rtt);
+ (health::relay(&watch.link), rtt)
+ })
+ .collect()
+ }
+
+ /// Close each watching session, so its relay hears the close.
+ pub(super) async fn unwatch(&self) {
+ let watching = std::mem::take(&mut *self.watching.lock().unwrap());
+ let mut closing = tokio::task::JoinSet::new();
+ for Watch { connection, client, .. } in watching {
+ closing.spawn(async move {
+ // A session that only subscribes has nothing to drain.
+ let _ = connection.close().await;
+ client.close().await;
+ });
+ }
+ closing.join_all().await;
+ }
+}
+
+/// Wait until any of `connections` has a session, or fail with why the first
+/// one could not, within [`DIAL`]. A connection keeps redialing until its
+/// loop gives up, so a relay whose first dial fails may still answer in time.
+async fn dial(connections: &[moq_tokio::Connection], links: &[health::Link]) -> anyhow::Result<()> {
+ let mut dialing: tokio::task::JoinSet<(usize, anyhow::Result<()>)> = tokio::task::JoinSet::new();
+ for (index, connection) in connections.iter().enumerate() {
+ let connection = connection.clone();
+ dialing.spawn(async move {
+ let established = connection.established().await.map(drop).map_err(anyhow::Error::from);
+ (index, established)
+ });
+ }
+ let mut failures: Vec<(usize, anyhow::Error)> = Vec::new();
+ let first = async {
+ while let Some(joined) = dialing.join_next().await {
+ match joined {
+ Ok((_, Ok(()))) => return true,
+ Ok((index, Err(err))) => failures.push((index, err)),
+ Err(err) => failures.push((usize::MAX, anyhow::anyhow!("the dial panicked: {err}"))),
+ }
+ }
+ false
+ };
+ let connected = kio::clock::timeout(DIAL, first).await;
+ match connected {
+ Ok(true) => Ok(()),
+ Ok(false) => {
+ failures.sort_by_key(|(index, _)| *index);
+ let (index, err) = failures.into_iter().next().expect("every dial failed");
+ let endpoint = links.get(index).map_or("the relay", |link| link.endpoint.as_str());
+ Err(err.context(format!("cannot reach {endpoint}")))
+ }
+ Err(_) => {
+ // Each relay, and its latest failed dial where one failed rather
+ // than hung. A relay whose loop gave up, such as on an auth
+ // rejection, is closed, so its monitor no longer says why; its
+ // dial did.
+ let relays: Vec = connections
+ .iter()
+ .zip(links)
+ .enumerate()
+ .map(|(index, (connection, link))| {
+ let gave_up = failures.iter().find(|(failed, _)| *failed == index);
+ match (gave_up, connection.monitor().failure()) {
+ (Some((_, err)), _) => format!("{}: {err:#}", link.endpoint),
+ (None, Some(err)) => format!("{}: {err}", link.endpoint),
+ (None, None) => format!("{}: no answer", link.endpoint),
+ }
+ })
+ .collect();
+ anyhow::bail!(
+ "no relay took the session within {} s ({})",
+ DIAL.as_secs(),
+ relays.join("; ")
+ )
+ }
+ }
+}
+
+/// The relay at `url` as the window names it: only the scheme, host, and
+/// port, since a path can carry a credential, and the window may be on a
+/// shared screen. A URL without a host, such as a unix socket's, keeps its
+/// path, which is the address.
+pub fn endpoint(url: &url::Url) -> String {
+ match url.host_str() {
+ Some(host) => match url.port() {
+ Some(port) => format!("{}://{host}:{port}", url.scheme()),
+ None => format!("{}://{host}", url.scheme()),
+ },
+ None => format!("{}:{}", url.scheme(), url.path()),
+ }
+}
+
+/// Publish the layer layout of what `state` drew on program at `at` on the
+/// media clock, while any source is published independently.
+pub(super) fn lay_out(publisher: &mut moq_scene::layout::Publisher, state: &State, at: Duration) {
+ // Past what a timestamp holds, the frame goes unstamped too.
+ let Ok(stamp) = moq_net::Timestamp::try_from(at) else {
+ return;
+ };
+ let collection = &state.collection;
+ let published = match collection.published.is_empty() {
+ true => publisher.finish(),
+ false => {
+ // To the whole microsecond as the frame's stamp is, rather than
+ // rounded as the wire would, so a cut's layout starts on its frame.
+ let start = at.saturating_sub(state.compositor.program_since()).as_micros();
+ let start = Duration::from_micros(start as u64);
+ let layout = moq_scene::layout::Layout::new(collection, state.compositor.program_scene(), start);
+ publisher.update(layout, stamp)
+ }
+ };
+ if let Err(err) = published {
+ tracing::warn!(%err, "cannot publish the layer layout");
+ }
+}
+
+/// End the layer layout, once the video it lays out has ended.
+pub(super) fn finish_layout(publisher: &mut moq_scene::layout::Publisher) {
+ if let Err(err) = publisher.finish() {
+ tracing::warn!(%err, "cannot finish the layer layout");
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// When no relay takes the session in time, the error names why each
+ /// one did not: a relay whose loop already gave up keeps the reason it
+ /// gave up with, beside the one that never answered, rather than reading
+ /// as unanswered because its closed monitor no longer holds it.
+ #[tokio::test(start_paused = true)]
+ async fn a_relay_that_gave_up_keeps_its_reason_past_the_timeout() {
+ let _ = moq_tokio::crypto::install_default();
+ let client = moq_tokio::connect::Config::default()
+ .init(Default::default())
+ .expect("client");
+ // No backend dials this scheme, so its loop gives up at once.
+ let primary: url::Url = "ftp://127.0.0.1:1/".parse().unwrap();
+ // Bound but never read, so the failover never answers.
+ let silent = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
+ let failover: url::Url = format!("https://{}/", silent.local_addr().unwrap()).parse().unwrap();
+ let relays = vec![
+ Relay {
+ client: client.clone(),
+ url: primary,
+ },
+ Relay { client, url: failover },
+ ];
+ let origin = moq_tokio::origin::spawn();
+ let (connections, links) = publish(relays, &origin, &moq_net::Path::new("studio").unwrap());
+ let err = dial(&connections, &links).await.expect_err("no relay answers");
+ let err = format!("{err:#}");
+ assert!(err.contains("no relay took the session"), "{err}");
+ assert!(err.contains("ftp://127.0.0.1:1: url scheme must be"), "{err}");
+ assert!(err.contains("no answer"), "the failover hung: {err}");
+ }
+}
diff --git a/rs/moq-studio/src/feed.rs b/rs/moq-studio/src/feed.rs
index ea03ed8b..a8eaaa35 100644
--- a/rs/moq-studio/src/feed.rs
+++ b/rs/moq-studio/src/feed.rs
@@ -2,11 +2,12 @@
//!
//! Every guest the studio renews gets a broadcast at
//! `.return//` in the room, which only their token may
-//! read. Its video is the program's: the studio's session in the room serves the
-//! program broadcast once at `.program`, and each feed's catalog lists its video
-//! renditions by reference, so a guest costs no video encode. Its audio is its
-//! own: a mix-minus of the audio scene on program without the guest's microphone, so
-//! they follow the show without hearing themselves back.
+//! read. Its video is the program's while on air: the studio's session in the
+//! room serves the program broadcast once at `.program`, and each feed's catalog
+//! lists its video renditions by reference, so a guest costs no video encode.
+//! Off air it lists none. Its audio is its own, on air or not: a mix-minus of
+//! the audio scene on program without the guest's microphone, so they follow
+//! the show without hearing themselves back.
//!
//! The studio's session in the room publishes them, holding only what the room
//! key signs. A guest costs one subtraction per audio block and an Opus encoder
@@ -30,13 +31,16 @@ use crate::mix;
/// not for broadcast.
const BITRATE: moq_net::bandwidth::Rate = moq_net::bandwidth::Rate::from_kbps(64);
+/// The program's broadcast while on air, and `None` off air.
+pub type Program = tokio::sync::watch::Receiver>;
+
/// The return feeds of every renewed guest, and the program they show.
pub struct Feeds {
room: Arc,
/// What the studio's session in the room publishes.
origin: moq_net::origin::Producer,
- /// The program broadcast, which each feed's video names.
- program: moq_net::broadcast::Consumer,
+ /// The program broadcast while on air, which each feed's video names.
+ program: Program,
/// The program catalog's clock, which each feed's catalog shares so its
/// audio lines up with the program's video.
clock: moq_mux::Clock,
@@ -52,14 +56,14 @@ pub struct Feeds {
}
impl Feeds {
- /// Serve `program` at [`PROGRAM`] in `room`, through `origin`, the studio's
- /// session there, and publish a feed for each guest [`sync`](Self::sync)
- /// finds renewed. `clock` and `epoch` are the program audio's, which the
- /// feeds' audio is stamped against.
+ /// Serve the broadcast `program` holds while on air at [`PROGRAM`] in
+ /// `room`, through `origin`, the studio's session there, and publish a feed
+ /// for each guest [`sync`](Self::sync) finds renewed. `clock` and `epoch`
+ /// are the program audio's, which the feeds' audio is stamped against.
pub fn new(
room: Arc,
origin: moq_net::origin::Producer,
- program: moq_net::broadcast::Consumer,
+ program: Program,
clock: moq_mux::Clock,
epoch: Duration,
) -> anyhow::Result {
@@ -69,10 +73,12 @@ impl Feeds {
let served = program.clone();
let serving = tokio::spawn(async move {
while let Some(request) = route.requested().await {
- // The route claims everything beneath it too, which holds nothing.
- match request.path().as_str() == PROGRAM {
- true => request.serve(served.clone()),
- false => request.reject(moq_net::Error::NotFound),
+ let on_air = served.borrow().clone();
+ // The route claims everything beneath it too, which holds nothing,
+ // and off air the program is nowhere.
+ match on_air {
+ Some(program) if request.path().as_str() == PROGRAM => request.serve(program),
+ _ => request.reject(moq_net::Error::NotFound),
}
}
});
@@ -203,8 +209,9 @@ struct Running {
catalog: moq_mux::catalog::Producer,
audio: moq_audio::encode::Producer,
follow: mix::Follow,
- /// The program broadcast, whose video renditions the catalog lists.
- program: moq_net::broadcast::Consumer,
+ /// The program broadcast while on air, whose video renditions the catalog
+ /// lists.
+ program: Program,
/// The program, relative to the feed.
reference: moq_net::path::Relative,
epoch: Duration,
@@ -212,8 +219,9 @@ struct Running {
}
impl Running {
- /// List the program's video and encode the picked audio until `stopped`
- /// or the program ends, then finish the audio track and the catalog.
+ /// List the program's video while on air and encode the picked audio
+ /// until `stopped` or the studio closes, then finish the audio track and
+ /// the catalog.
async fn run(mut self, stopped: tokio::sync::oneshot::Receiver<()>) -> anyhow::Result<()> {
let Self {
catalog,
@@ -243,9 +251,47 @@ impl Running {
}
}
+/// List in `catalog` the video renditions of the program on air, read from
+/// the broadcast at `reference`, and none while it is off air, until the
+/// studio closes.
+async fn list(
+ program: &mut Program,
+ catalog: &mut moq_mux::catalog::Producer,
+ reference: &moq_net::path::Relative,
+) -> anyhow::Result<()> {
+ loop {
+ let on_air = program.borrow_and_update().clone();
+ // Whether the program changed before this one ended.
+ let changed = match &on_air {
+ Some(on_air) => tokio::select! {
+ changed = program.changed() => Some(changed.is_ok()),
+ listed = list_one(on_air, catalog, reference) => {
+ // The stream ending can cut its catalog, which says only that.
+ if let Err(err) = listed {
+ tracing::debug!(error = format!("{err:#}"), "the program's catalog ended");
+ }
+ None
+ }
+ },
+ None => None,
+ };
+ let open = match changed {
+ Some(open) => open,
+ None => {
+ catalog.mutate(|catalog| catalog.video = Default::default())?;
+ program.changed().await.is_ok()
+ }
+ };
+ // The studio closed.
+ if !open {
+ return Ok(());
+ }
+ }
+}
+
/// List the video renditions of each catalog `program` publishes in
/// `catalog`, read from the broadcast at `reference`, until the program ends.
-async fn list(
+async fn list_one(
program: &moq_net::broadcast::Consumer,
catalog: &mut moq_mux::catalog::Producer,
reference: &moq_net::path::Relative,
@@ -295,21 +341,21 @@ mod tests {
/// Return feeds on `origin` for the guests in `room`, of a program that
/// lives as long as the returned broadcast and catalog.
- fn feeds(
- room: &Arc,
- origin: &moq_net::origin::Producer,
- ) -> (Feeds, (moq_net::broadcast::Producer, moq_mux::catalog::Producer)) {
+ fn feeds(room: &Arc, origin: &moq_net::origin::Producer) -> (Feeds, OnAir) {
let (broadcast, catalog) = program();
- let feeds = Feeds::new(
- room.clone(),
- origin.clone(),
- broadcast.consume(),
- catalog.clock(),
- Duration::ZERO,
- );
- (feeds.unwrap(), (broadcast, catalog))
+ let (on_air, program) = tokio::sync::watch::channel(Some(broadcast.consume()));
+ let feeds = Feeds::new(room.clone(), origin.clone(), program, catalog.clock(), Duration::ZERO);
+ (feeds.unwrap(), (on_air, broadcast, catalog))
}
+ /// The program on air, as the engine holds it, with its broadcast and
+ /// catalog.
+ type OnAir = (
+ tokio::sync::watch::Sender>,
+ moq_net::broadcast::Producer,
+ moq_mux::catalog::Producer,
+ );
+
/// Alice and bob in `room` with their cameras on, and a collection playing
/// each camera, whose sources run on `bus`, which `mixed` follows.
fn guests(room: &Room, bus: &Bus, mixed: Mixed) -> (Collection, Sources) {
diff --git a/rs/moq-studio/src/guests.rs b/rs/moq-studio/src/guests.rs
index ef793d3f..6d2f71ac 100644
--- a/rs/moq-studio/src/guests.rs
+++ b/rs/moq-studio/src/guests.rs
@@ -92,13 +92,12 @@ fn guest(path: &Path) -> Option<(Path, Kind)> {
Some((identity, kind))
}
-/// The guest flags.
-#[derive(usage_rs::Args, Clone, Debug)]
+/// The guest flags, which win over the show's own guest settings for a run.
+#[derive(usage_rs::Args, Clone, Debug, Default)]
#[usage(unknown_flags = "error", args_override_self = false)]
pub struct Config {
- /// The room guests join: a path under the `--connect` URL's, which the
- /// studio's own connection must be allowed to subscribe to. Needs
- /// `--room-key`.
+ /// The room guests join: a path under the relay's URL, which the studio's
+ /// own connection must be allowed to subscribe to. Needs `--room-key`.
#[usage(long)]
pub room: Option,
@@ -116,28 +115,33 @@ pub struct Config {
/// How long a join link, and each token the studio renews it with, works. A
/// guest in the room is renewed every half of this, so a guest removed from
- /// scenes is disconnected within it. At least a minute.
- #[usage(long, default = "4h")]
- pub guest_expiry: moq_tokio::cli::Duration,
+ /// scenes is disconnected within it. At least a minute. Defaults to 4h.
+ #[usage(long)]
+ pub guest_expiry: Option,
}
+/// How long a join link works unless the show or a flag says otherwise.
+pub const DEFAULT_EXPIRY: Duration = Duration::from_secs(4 * 3600);
+
impl Config {
- /// The room these flags describe on the relay at `connect`, or `None` without
- /// `--room`, remembering who was removed from scenes beside the room key.
+ /// The room these settings describe on the relay at `connect`, or `None`
+ /// without a room, remembering who was removed from scenes beside the room
+ /// key.
pub fn load(&self, connect: Option<&Url>) -> anyhow::Result> {
let (room, key) = match (&self.room, &self.room_key, &self.join_page) {
(None, None, None) => return Ok(None),
(Some(room), Some(key), _) => (room, key),
- _ => anyhow::bail!("guests need both --room and --room-key"),
+ _ => anyhow::bail!("guests need both a room and a room key"),
};
let page = self
.join_page
.clone()
.unwrap_or_else(|| Url::parse(JOIN_PAGE).expect("the default join page is a URL"));
- let connect = connect.context("guests join through the relay; add --connect")?;
+ let connect = connect.context("guests join through the relay; set one in Settings, Stream")?;
let removals = removals_path(key);
let key = moq_auth::Key::from_file(key).with_context(|| format!("reading the room key {}", key.display()))?;
- let room = Room::new(connect, room, key, page, self.guest_expiry.into())?;
+ let expiry = self.guest_expiry.map_or(DEFAULT_EXPIRY, Duration::from);
+ let room = Room::new(connect, room, key, page, expiry)?;
Ok(Some(room.remember(removals)?))
}
}
@@ -925,7 +929,7 @@ pub(crate) mod tests {
room: room.then(|| "guests".into()),
room_key: key.then(|| path.clone()),
join_page: page.then(|| Url::parse("https://studio.example/join/").unwrap()),
- guest_expiry: EXPIRY.into(),
+ guest_expiry: Some(EXPIRY.into()),
};
assert!(config(false, false, false).load(Some(&connect)).unwrap().is_none());
@@ -938,7 +942,7 @@ pub(crate) mod tests {
assert!(err.to_string().contains("need both"), "{err}");
}
let err = config(true, true, true).load(None).err().unwrap();
- assert!(err.to_string().contains("--connect"), "{err}");
+ assert!(err.to_string().contains("relay"), "{err}");
let room = config(true, true, true).load(Some(&connect)).unwrap().unwrap();
assert_eq!(room.path().as_str(), "guests");
let link = room.invite("alice").unwrap().link;
@@ -1443,7 +1447,7 @@ pub(crate) mod tests {
room: Some("guests".into()),
room_key: Some(path),
join_page: None,
- guest_expiry: EXPIRY.into(),
+ guest_expiry: Some(EXPIRY.into()),
};
// Nothing the studio loads names the scene file, so another one sees the same state.
let start = || config.load(Some(&connect)).unwrap().unwrap();
diff --git a/rs/moq-studio/src/health.rs b/rs/moq-studio/src/health.rs
index d694dfc1..9370eede 100644
--- a/rs/moq-studio/src/health.rs
+++ b/rs/moq-studio/src/health.rs
@@ -634,7 +634,8 @@ impl Sampler {
}
}
-fn relay(link: &Link) -> Relay {
+/// How `link` is doing now.
+pub(crate) fn relay(link: &Link) -> Relay {
use moq_tokio::connection::Phase;
let (state, version) = match link.monitor.state().phase {
Phase::Connecting { .. } => (State::Connecting, None),
diff --git a/rs/moq-studio/src/hotkeys/portal.rs b/rs/moq-studio/src/hotkeys/portal.rs
index dd314968..acce067f 100644
--- a/rs/moq-studio/src/hotkeys/portal.rs
+++ b/rs/moq-studio/src/hotkeys/portal.rs
@@ -145,6 +145,8 @@ fn id(action: &Action, binding: &Binding) -> String {
Action::Record => "record".into(),
Action::Panic => "panic".into(),
Action::StudioMode => "studio-mode".into(),
+ Action::GoLive => "go-live".into(),
+ Action::EndStream => "end-stream".into(),
}
}
diff --git a/rs/moq-studio/src/kit/icon.rs b/rs/moq-studio/src/kit/icon.rs
index 8074423f..ae709e73 100644
--- a/rs/moq-studio/src/kit/icon.rs
+++ b/rs/moq-studio/src/kit/icon.rs
@@ -64,12 +64,19 @@ pub enum Icon {
Folder,
Save,
Repaired,
+ Open,
+ Check,
+ Encoder,
+ Warning,
+ Landscape,
+ Portrait,
+ Uplink,
}
impl Icon {
/// Every icon, for the gallery.
#[cfg(test)]
- pub const ALL: [Self; 53] = [
+ pub const ALL: [Self; 60] = [
Self::Undo,
Self::Redo,
Self::Settings,
@@ -123,6 +130,13 @@ impl Icon {
Self::Folder,
Self::Save,
Self::Repaired,
+ Self::Open,
+ Self::Check,
+ Self::Encoder,
+ Self::Warning,
+ Self::Landscape,
+ Self::Portrait,
+ Self::Uplink,
];
fn source(self) -> ImageSource<'static> {
@@ -180,6 +194,13 @@ impl Icon {
Self::Folder => include_image!("../../assets/icons/folder.svg"),
Self::Save => include_image!("../../assets/icons/download.svg"),
Self::Repaired => include_image!("../../assets/icons/shield.svg"),
+ Self::Open => include_image!("../../assets/icons/folder-open.svg"),
+ Self::Check => include_image!("../../assets/icons/check.svg"),
+ Self::Encoder => include_image!("../../assets/icons/cpu.svg"),
+ Self::Warning => include_image!("../../assets/icons/triangle-alert.svg"),
+ Self::Landscape => include_image!("../../assets/icons/rectangle-horizontal.svg"),
+ Self::Portrait => include_image!("../../assets/icons/rectangle-vertical.svg"),
+ Self::Uplink => include_image!("../../assets/icons/upload.svg"),
}
}
diff --git a/rs/moq-studio/src/kit/widgets.rs b/rs/moq-studio/src/kit/widgets.rs
index 4ccd5b2a..dda16f98 100644
--- a/rs/moq-studio/src/kit/widgets.rs
+++ b/rs/moq-studio/src/kit/widgets.rs
@@ -310,10 +310,7 @@ impl<'a> Pill<'a> {
#[cfg_attr(
not(test),
- expect(
- dead_code,
- reason = "the pages draw with it: the rest of quest/m0/ux, such as go-live.md"
- )
+ expect(dead_code, reason = "the pages draw with it: the rest of quest/m0/ux")
)]
pub fn value(mut self, value: &'a str) -> Self {
self.value = Some(value);
@@ -1021,6 +1018,9 @@ pub struct Pad<'a> {
picture: Option,
key: Option<&'a str>,
tally: Tally,
+ /// The tag over the tally's own, such as FIRST for the scene a stream
+ /// opens on.
+ tag: Option<&'a str>,
/// Why the scene cannot be taken, if it cannot.
refused: Option<&'a str>,
/// What just failed on it, such as a rename.
@@ -1038,6 +1038,7 @@ impl<'a> Pad<'a> {
picture: None,
key: None,
tally: Tally::Off,
+ tag: None,
refused: None,
fault: None,
width: 124.0,
@@ -1063,6 +1064,12 @@ impl<'a> Pad<'a> {
self
}
+ /// Tagged `tag` in its tally's color, rather than the tally's own word.
+ pub fn tag(mut self, tag: Option<&'a str>) -> Self {
+ self.tag = tag;
+ self
+ }
+
/// Shown, but not takeable, for `why`. It still hears a click, such as
/// for its menu, which its caller does not take it on.
pub fn refused(mut self, why: Option<&'a str>) -> Self {
@@ -1135,8 +1142,8 @@ impl Widget for Pad<'_> {
}
let tag_text = match self.tally {
Tally::Off => None,
- Tally::Air => Some("On air"),
- Tally::Cue => Some("Next"),
+ Tally::Air => Some(self.tag.unwrap_or("On air")),
+ Tally::Cue => Some(self.tag.unwrap_or("Next")),
};
if let Some(text) = tag_text {
tag(painter, thumb.min + vec2(4.0, 4.0), text, self.tally, 11.0);
@@ -1183,12 +1190,15 @@ fn shadow(painter: &egui::Painter, rect: Rect, corner: u8, color: Color32, blur:
}
/// The frame around the live picture: the accent while on air, amber and striped
-/// while it shows a scene that is not, with a tag naming which.
+/// while it shows a scene that is not, neutral off air, with a tag naming which.
pub struct TallyFrame<'a> {
tally: Tally,
- tag: Option<&'a str>,
+ tag: Option<(&'a str, Tally)>,
}
+/// The neutral frame's outline, off air.
+const NEUTRAL: Color32 = Color32::from_rgb(0x3a, 0x3a, 0x46);
+
impl<'a> TallyFrame<'a> {
pub fn new(tally: Tally) -> Self {
Self { tally, tag: None }
@@ -1196,7 +1206,14 @@ impl<'a> TallyFrame<'a> {
/// The tag in the picture's corner, such as ON AIR.
pub fn tag(mut self, tag: &'a str) -> Self {
- self.tag = Some(tag);
+ self.tag = Some((tag, self.tally));
+ self
+ }
+
+ /// The tag in `tally`'s color rather than the frame's, such as OFF AIR
+ /// in amber on the neutral frame.
+ pub fn tag_as(mut self, tag: &'a str, tally: Tally) -> Self {
+ self.tag = Some((tag, tally));
self
}
@@ -1231,9 +1248,11 @@ impl<'a> TallyFrame<'a> {
painter.with_clip_rect(clip).set(band, Shape::Vec(stripes));
}
painter.rect_stroke(outline, 12, Stroke::new(3.0, color), StrokeKind::Middle);
+ } else {
+ painter.rect_stroke(rect.expand(4.5), 12, Stroke::new(3.0, NEUTRAL), StrokeKind::Middle);
}
- if let Some(text) = self.tag {
- tag(painter, rect.min + vec2(12.0, 12.0), text, self.tally, 15.0);
+ if let Some((text, tally)) = self.tag {
+ tag(painter, rect.min + vec2(12.0, 12.0), text, tally, 15.0);
}
if let (Some(fx), Some(mark)) = (effects, mark) {
fx.program(&mark.around(rect));
diff --git a/rs/moq-studio/src/library.rs b/rs/moq-studio/src/library.rs
new file mode 100644
index 00000000..e5792906
--- /dev/null
+++ b/rs/moq-studio/src/library.rs
@@ -0,0 +1,691 @@
+//! The library: the shows this user has opened, newest first, each with the
+//! last picture of its program, kept per user in the app's storage rather than
+//! beside any show. The window opens on it when no show is named.
+
+use std::collections::HashMap;
+use std::path::{Path, PathBuf};
+use std::time::Duration;
+
+use anyhow::Context;
+use egui::{Align, Color32, Rect, Sense, Stroke, Ui, UiBuilder, Vec2, pos2, vec2};
+
+use crate::engine::{Engine, Gpu};
+use crate::kit::{self, Icon};
+use crate::setup::Task;
+
+/// Where the list is kept in the app's storage.
+const KEY: &str = "recent";
+
+/// How many shows the list keeps.
+const KEPT: usize = 30;
+
+/// How wide a saved picture is at most, enough for a card on a large screen.
+const PICTURE: u32 = 640;
+
+/// The shows this user has opened, the latest first.
+#[derive(Clone, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
+#[serde(default)]
+pub struct Recent {
+ pub shows: Vec,
+}
+
+/// A show in the list.
+#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
+pub struct Show {
+ /// Its scene file.
+ pub path: PathBuf,
+ /// When it was last opened, in seconds since the Unix epoch.
+ pub opened: u64,
+ /// Its last time on air, if it ever went live.
+ #[serde(default)]
+ pub live: Option,
+ /// Whether its canvas is taller than wide, as of its last picture.
+ #[serde(default)]
+ pub portrait: bool,
+}
+
+/// A time on air.
+#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
+pub struct Aired {
+ /// When it went live, in seconds since the Unix epoch.
+ pub at: u64,
+ /// How long it stayed live, in seconds.
+ pub lasted: u64,
+}
+
+/// Now, in seconds since the Unix epoch, on the ambient clock.
+pub fn now() -> u64 {
+ kio::clock::wall()
+ .saturating_duration_since(kio::clock::Wall::UNIX_EPOCH)
+ .as_secs()
+}
+
+impl Recent {
+ /// The list as the app's storage holds it, or none yet.
+ pub fn load(storage: Option<&dyn eframe::Storage>) -> Self {
+ storage
+ .and_then(|storage| eframe::get_value(storage, KEY))
+ .unwrap_or_default()
+ }
+
+ pub fn save(&self, storage: &mut dyn eframe::Storage) {
+ eframe::set_value(storage, KEY, self);
+ }
+
+ /// The show at `path` was opened at `at`: it moves to the front.
+ pub fn opened(&mut self, path: &Path, at: u64) {
+ let path = absolute(path);
+ let index = self.shows.iter().position(|show| show.path == path);
+ let mut show = match index {
+ Some(index) => self.shows.remove(index),
+ None => Show {
+ path,
+ opened: at,
+ live: None,
+ portrait: false,
+ },
+ };
+ show.opened = at;
+ self.shows.insert(0, show);
+ self.shows.truncate(KEPT);
+ }
+
+ /// The show at `path` was on air as `aired`.
+ pub fn aired(&mut self, path: &Path, aired: Aired) {
+ if let Some(show) = self.find(path) {
+ show.live = Some(aired);
+ }
+ }
+
+ /// The show at `path` was last drawn `portrait` or not.
+ pub fn shaped(&mut self, path: &Path, portrait: bool) {
+ if let Some(show) = self.find(path) {
+ show.portrait = portrait;
+ }
+ }
+
+ /// Take the show at `path` off the list, and its picture in `pictures`
+ /// with it.
+ pub fn forget(&mut self, path: &Path, pictures: Option<&Path>) {
+ self.shows.retain(|show| show.path != path);
+ if let Some(pictures) = pictures {
+ let _ = std::fs::remove_file(picture_path(pictures, path));
+ }
+ }
+
+ fn find(&mut self, path: &Path) -> Option<&mut Show> {
+ let path = absolute(path);
+ self.shows.iter_mut().find(|show| show.path == path)
+ }
+}
+
+/// `path` from the root, so the list names a show once however it was
+/// opened.
+fn absolute(path: &Path) -> PathBuf {
+ std::path::absolute(path).unwrap_or_else(|_| path.to_path_buf())
+}
+
+/// Where the shows' pictures are kept: the app's storage.
+pub fn pictures() -> Option {
+ eframe::storage_dir("moq-studio").map(|dir| dir.join("shows"))
+}
+
+/// Where the picture of the show at `path` is kept in `pictures`, by a hash of
+/// the show's path, so moving the show starts it without one.
+pub fn picture_path(pictures: &Path, path: &Path) -> PathBuf {
+ let path = absolute(path);
+ // FNV-1a, which unlike std's hasher stays the same across releases.
+ let hash = path
+ .as_os_str()
+ .as_encoded_bytes()
+ .iter()
+ .fold(0xcbf2_9ce4_8422_2325_u64, |hash, byte| {
+ (hash ^ u64::from(*byte)).wrapping_mul(0x0100_0000_01b3)
+ });
+ pictures.join(format!("{hash:016x}.png"))
+}
+
+/// A picture in memory, as RGBA rows.
+pub struct Picture {
+ pub width: u32,
+ pub height: u32,
+ pub rgba: Vec,
+}
+
+impl Picture {
+ /// Whether it is taller than wide.
+ pub fn portrait(&self) -> bool {
+ self.height > self.width
+ }
+
+ /// This picture, shrunk to `width` at most, by averaging each block of
+ /// pixels that becomes one.
+ fn shrink(self, width: u32) -> Self {
+ let step = self.width.div_ceil(width.max(1)).max(1);
+ if step == 1 {
+ return self;
+ }
+ let (out_width, out_height) = (self.width / step, self.height / step);
+ let mut rgba = Vec::with_capacity((out_width * out_height * 4) as usize);
+ for y in 0..out_height {
+ for x in 0..out_width {
+ let mut sum = [0u32; 4];
+ for dy in 0..step {
+ for dx in 0..step {
+ let at = (((y * step + dy) * self.width + x * step + dx) * 4) as usize;
+ for (channel, total) in sum.iter_mut().enumerate() {
+ *total += u32::from(self.rgba[at + channel]);
+ }
+ }
+ }
+ rgba.extend(sum.map(|total| (total / (step * step)) as u8));
+ }
+ }
+ Self {
+ width: out_width,
+ height: out_height,
+ rgba,
+ }
+ }
+}
+
+/// Read the program's last frame back from the GPU as viewers see it, with
+/// its layer layout's pictures, shrunk for a card.
+pub fn capture(gpu: &Gpu, engine: &Engine) -> anyhow::Result {
+ let (buffer, width, height, row) = {
+ let state = engine.state.lock().unwrap();
+ let texture = state.compositor.layered();
+ let (width, height) = (texture.width(), texture.height());
+ let row = (width * 4).next_multiple_of(wgpu::COPY_BYTES_PER_ROW_ALIGNMENT);
+ let buffer = gpu.device.create_buffer(&wgpu::BufferDescriptor {
+ label: Some("show picture"),
+ size: u64::from(row) * u64::from(height),
+ usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
+ mapped_at_creation: false,
+ });
+ let mut encoder = gpu.device.create_command_encoder(&Default::default());
+ encoder.copy_texture_to_buffer(
+ texture.as_image_copy(),
+ wgpu::TexelCopyBufferInfo {
+ buffer: &buffer,
+ layout: wgpu::TexelCopyBufferLayout {
+ offset: 0,
+ bytes_per_row: Some(row),
+ rows_per_image: None,
+ },
+ },
+ texture.size(),
+ );
+ gpu.queue.submit([encoder.finish()]);
+ (buffer, width, height, row)
+ };
+ let (send, recv) = std::sync::mpsc::channel();
+ buffer.map_async(wgpu::MapMode::Read, .., move |result| {
+ let _ = send.send(result);
+ });
+ gpu.device
+ .poll(wgpu::PollType::wait_indefinitely())
+ .context("cannot wait for the GPU")?;
+ recv.recv()
+ .context("the GPU dropped the read")?
+ .context("cannot read the program back")?;
+ let mapped = buffer
+ .slice(..)
+ .get_mapped_range()
+ .map_err(|err| anyhow::anyhow!("cannot read the program back: {err:?}"))?;
+ let mut rgba = Vec::with_capacity((width * height * 4) as usize);
+ for line in mapped.chunks(row as usize).take(height as usize) {
+ rgba.extend_from_slice(&line[..(width * 4) as usize]);
+ }
+ drop(mapped);
+ buffer.unmap();
+ Ok(Picture { width, height, rgba }.shrink(PICTURE))
+}
+
+/// Keep `picture` as the show at `path`'s, in `pictures`.
+pub fn save_picture(pictures: &Path, path: &Path, picture: &Picture) -> anyhow::Result<()> {
+ let file = picture_path(pictures, path);
+ let dir = pictures;
+ std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
+ let mut out = tempfile::NamedTempFile::new_in(dir).with_context(|| format!("writing {}", file.display()))?;
+ {
+ let mut encoder = png::Encoder::new(std::io::BufWriter::new(&mut out), picture.width, picture.height);
+ encoder.set_color(png::ColorType::Rgba);
+ encoder.set_depth(png::BitDepth::Eight);
+ let mut writer = encoder.write_header().context("cannot encode the picture")?;
+ writer
+ .write_image_data(&picture.rgba)
+ .context("cannot encode the picture")?;
+ }
+ out.persist(&file)
+ .with_context(|| format!("writing {}", file.display()))?;
+ Ok(())
+}
+
+/// The picture kept for the show at `path` in `pictures`, if there is one.
+fn load_picture(pictures: &Path, path: &Path) -> Option {
+ let file = std::fs::File::open(picture_path(pictures, path)).ok()?;
+ let mut decoder = png::Decoder::new(std::io::BufReader::new(file));
+ decoder.set_transformations(png::Transformations::normalize_to_color8());
+ let mut reader = decoder.read_info().ok()?;
+ let mut buffer = vec![0; reader.output_buffer_size()?];
+ let info = reader.next_frame(&mut buffer).ok()?;
+ let size = [info.width as usize, info.height as usize];
+ let pixels = &buffer[..info.buffer_size()];
+ match info.color_type {
+ png::ColorType::Rgba => Some(egui::ColorImage::from_rgba_unmultiplied(size, pixels)),
+ png::ColorType::Rgb => Some(egui::ColorImage::from_rgb(size, pixels)),
+ _ => None,
+ }
+}
+
+/// What the library asks the window for.
+pub enum Pick {
+ /// Open the show at this path, or set it up if there is no file there.
+ Open(PathBuf),
+}
+
+/// The library page.
+pub struct Library {
+ /// Where the shows' pictures are kept, if anywhere.
+ dir: Option,
+ /// Each show's picture, loaded the first time it is drawn; `None` for one
+ /// without.
+ pictures: HashMap>,
+ /// Whether each show's file is still there, checked once.
+ found: HashMap,
+ /// The system's file dialog, while open.
+ dialog: Option>>,
+ /// Why the last show failed to open.
+ pub error: Option,
+ /// The show being opened, by name, while it opens.
+ pub opening: Option,
+}
+
+impl Library {
+ /// The library, with the shows' pictures from `dir`.
+ pub fn new(dir: Option) -> Self {
+ Self {
+ dir,
+ pictures: HashMap::new(),
+ found: HashMap::new(),
+ dialog: None,
+ error: None,
+ opening: None,
+ }
+ }
+}
+
+/// Room between cards.
+const GAP: f32 = 22.0;
+/// The height of a card's name and line under its picture.
+const LABEL: f32 = 70.0;
+
+impl Library {
+ /// Draw the page; a show to open once one is picked.
+ pub fn ui(&mut self, ui: &mut Ui, recent: &mut Recent, runtime: &tokio::runtime::Handle) -> Option {
+ let mut pick = None;
+ if let Some(dialog) = &mut self.dialog {
+ ui.ctx().request_repaint_after(Duration::from_millis(100));
+ if let Some(picked) = dialog.poll() {
+ pick = picked.clone().map(Pick::Open);
+ self.dialog = None;
+ }
+ }
+ let window = ui.max_rect();
+ kit::wash(ui.painter(), window);
+ let width = (window.width() - 2.0 * 48.0).min(1220.0);
+ let column = Rect::from_center_size(pos2(window.center().x, window.center().y), vec2(width, window.height()))
+ .with_min_y(window.top() + 56.0);
+ let builder = UiBuilder::new()
+ .max_rect(column)
+ .layout(egui::Layout::top_down(Align::Min));
+ ui.scope_builder(builder, |ui| {
+ egui::ScrollArea::vertical().show(ui, |ui| {
+ ui.set_width(width);
+ if self.heading(ui, runtime) {
+ self.dialog(runtime, false);
+ }
+ ui.add_space(20.0);
+ if let Some(error) = &self.error {
+ ui.colored_label(kit::ERR, error);
+ ui.add_space(12.0);
+ }
+ if let Some(name) = &self.opening {
+ ui.horizontal(|ui| {
+ ui.spinner();
+ ui.label(format!("Opening {name}…"));
+ });
+ ui.add_space(12.0);
+ }
+ let busy = self.opening.is_some() || self.dialog.is_some();
+ ui.add_enabled_ui(!busy, |ui| {
+ if let Some(picked) = self.cards(ui, recent, width, runtime) {
+ pick = Some(picked);
+ }
+ });
+ ui.add_space(40.0);
+ });
+ });
+ pick
+ }
+
+ /// YOUR SHOWS, and Open a show file beside it; whether that was clicked.
+ fn heading(&mut self, ui: &mut Ui, _runtime: &tokio::runtime::Handle) -> bool {
+ ui.horizontal(|ui| {
+ ui.vertical(|ui| {
+ ui.label(kit::caps("Your shows", kit::display(44.0)).color(kit::TEXT));
+ ui.label(
+ egui::RichText::new("Open one to pick up where it ended. Nothing goes live until you say so.")
+ .font(kit::ui(14.0))
+ .color(kit::DIM),
+ );
+ });
+ ui.with_layout(egui::Layout::right_to_left(Align::Max), |ui| {
+ let open = kit::button(Icon::Open, "Open a show file").min_size(vec2(0.0, 38.0));
+ ui.add_enabled(self.dialog.is_none() && self.opening.is_none(), open)
+ .clicked()
+ })
+ .inner
+ })
+ .inner
+ }
+
+ /// Open the system's dialog: for a new show's file if `new`, or an
+ /// existing one's.
+ fn dialog(&mut self, runtime: &tokio::runtime::Handle, new: bool) {
+ let dialog = Task::spawn(runtime, move || async move {
+ let dialog = rfd::AsyncFileDialog::new().add_filter("Show", &["toml"]);
+ let picked = match new {
+ true => {
+ dialog
+ .set_title("Where to keep the new show")
+ .set_file_name("show.toml")
+ .save_file()
+ .await
+ }
+ false => dialog.set_title("Open a show").pick_file().await,
+ };
+ picked.map(|file| file.path().to_path_buf())
+ });
+ match dialog {
+ Ok(dialog) => self.dialog = Some(dialog),
+ Err(err) => self.error = Some(format!("cannot open the file dialog: {err}")),
+ }
+ }
+
+ /// The New show card and a card for each show, three to a row.
+ fn cards(
+ &mut self,
+ ui: &mut Ui,
+ recent: &mut Recent,
+ width: f32,
+ runtime: &tokio::runtime::Handle,
+ ) -> Option {
+ let columns = if width > 900.0 { 3 } else { 2 };
+ let card = (width - GAP * (columns - 1) as f32) / columns as f32;
+ let size = vec2(card, card * 9.0 / 16.0 + LABEL);
+ let mut pick = None;
+ let mut forget = None;
+ let count = recent.shows.len() + 1;
+ for row in 0..count.div_ceil(columns) {
+ ui.horizontal(|ui| {
+ ui.spacing_mut().item_spacing.x = GAP;
+ for column in 0..columns {
+ let index = row * columns + column;
+ if index == 0 {
+ if self.new_card(ui, size).clicked() {
+ self.dialog(runtime, true);
+ }
+ continue;
+ }
+ let Some(show) = recent.shows.get(index - 1) else { break };
+ let response = self.show_card(ui, show, size);
+ if response.clicked() && self.found(&show.path) {
+ pick = Some(Pick::Open(show.path.clone()));
+ }
+ response.context_menu(|ui| {
+ if ui.add(kit::button(Icon::Remove, "Forget this show")).clicked() {
+ forget = Some(show.path.clone());
+ }
+ });
+ }
+ });
+ ui.add_space(GAP);
+ }
+ if let Some(path) = forget {
+ recent.forget(&path, self.dir.as_deref());
+ self.pictures.remove(&path);
+ }
+ pick
+ }
+
+ /// Whether the show at `path` is still there, checked the first time.
+ fn found(&mut self, path: &Path) -> bool {
+ *self
+ .found
+ .entry(path.to_path_buf())
+ .or_insert_with(|| path.try_exists().unwrap_or(false))
+ }
+
+ /// The card that makes a new show.
+ fn new_card(&mut self, ui: &mut Ui, size: Vec2) -> egui::Response {
+ let (rect, response) = ui.allocate_exact_size(size, Sense::click());
+ response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, ui.is_enabled(), "New show"));
+ let hovered = response.hovered();
+ let painter = ui.painter();
+ let stroke = Stroke::new(1.5, if hovered { kit::DIM } else { kit::LINE });
+ crate::ui::dashed(painter, rect.shrink(1.0), stroke);
+ let center = rect.center() - vec2(0.0, 26.0);
+ painter.circle_filled(center, 30.0, if hovered { Color32::WHITE } else { kit::TEXT });
+ Icon::Add.paint(ui, Rect::from_center_size(center, Vec2::splat(22.0)), 22.0, kit::BG);
+ let name = kit::caps_galley(painter, "New show", kit::display(24.0), kit::TEXT, None);
+ let at = pos2(rect.center().x - name.size().x / 2.0, center.y + 44.0);
+ painter.galley(at, name, kit::TEXT);
+ painter.text(
+ pos2(rect.center().x, at.y + 38.0),
+ egui::Align2::CENTER_TOP,
+ "Pick a shape, a camera, and a layout",
+ kit::ui(13.0),
+ kit::DIM,
+ );
+ response
+ }
+
+ /// A show's card: its picture, its name, and when it was last live.
+ fn show_card(&mut self, ui: &mut Ui, show: &Show, size: Vec2) -> egui::Response {
+ let found = self.found(&show.path);
+ let picture = self
+ .pictures
+ .entry(show.path.clone())
+ .or_insert_with(|| {
+ let image = load_picture(self.dir.as_deref()?, &show.path)?;
+ let name = format!("show picture {}", show.path.display());
+ Some(ui.ctx().load_texture(name, image, egui::TextureOptions::LINEAR))
+ })
+ .clone();
+ let name = show
+ .path
+ .file_stem()
+ .unwrap_or(show.path.as_os_str())
+ .to_string_lossy()
+ .into_owned();
+ let (rect, response) = ui.allocate_exact_size(size, Sense::click());
+ response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, ui.is_enabled(), &name));
+ let response = response.on_hover_text(show.path.display().to_string());
+ let painter = ui.painter();
+ let hovered = response.hovered() && found;
+ painter.rect_filled(rect, 12, kit::P1);
+ let top = rect.with_max_y(rect.bottom() - LABEL);
+ let clip = painter.with_clip_rect(top.intersect(painter.clip_rect()));
+ clip.rect_filled(
+ top,
+ egui::CornerRadius {
+ nw: 12,
+ ne: 12,
+ sw: 0,
+ se: 0,
+ },
+ Color32::BLACK,
+ );
+ match &picture {
+ Some(texture) => {
+ let shape = texture.aspect_ratio();
+ let fit = crate::ui::fit(top, shape);
+ let image = egui::Image::new(texture).corner_radius(0).tint(match found {
+ true => Color32::WHITE,
+ false => Color32::from_gray(90),
+ });
+ image.paint_at(&ui.new_child(UiBuilder::new().max_rect(top)), fit);
+ }
+ None => {
+ let icon = if show.portrait { Icon::Portrait } else { Icon::Landscape };
+ icon.paint(
+ ui,
+ Rect::from_center_size(top.center(), Vec2::splat(36.0)),
+ 36.0,
+ kit::FAINT,
+ );
+ }
+ }
+ let painter = ui.painter();
+ let label = rect.with_min_y(top.bottom());
+ let ink = if found { kit::TEXT } else { kit::DIM };
+ let title = kit::caps_galley(painter, &name, kit::display_bold(19.0), ink, Some(label.width() - 60.0));
+ painter.galley(pos2(label.left() + 14.0, label.top() + 14.0), title, ink);
+ let line = match found {
+ true => said(show, crate::library::now()),
+ false => "Not where it was; right-click to forget it".to_string(),
+ };
+ painter.text(
+ pos2(label.left() + 14.0, label.top() + 44.0),
+ egui::Align2::LEFT_TOP,
+ line,
+ kit::ui(12.5),
+ kit::DIM,
+ );
+ let icon = if show.portrait { Icon::Portrait } else { Icon::Landscape };
+ let at = Rect::from_center_size(pos2(label.right() - 26.0, label.center().y), Vec2::splat(18.0));
+ icon.paint(ui, at, 18.0, kit::DIM);
+ let stroke = match hovered {
+ true => Stroke::new(1.5, kit::DIM),
+ false => Stroke::new(1.0, kit::LINE),
+ };
+ ui.painter().rect_stroke(rect, 12, stroke, egui::StrokeKind::Inside);
+ response
+ }
+}
+
+/// What a show's card says of it: when it was last live and for how long, or
+/// when it was opened if it never went live.
+pub fn said(show: &Show, now: u64) -> String {
+ match show.live {
+ Some(aired) => format!(
+ "Live {}, for {}",
+ ago(now.saturating_sub(aired.at)),
+ lasted(aired.lasted)
+ ),
+ None => format!("Opened {}, never live", ago(now.saturating_sub(show.opened))),
+ }
+}
+
+/// `seconds` ago, as a person would say it.
+fn ago(seconds: u64) -> String {
+ const MINUTE: u64 = 60;
+ const HOUR: u64 = 60 * MINUTE;
+ const DAY: u64 = 24 * HOUR;
+ const WEEK: u64 = 7 * DAY;
+ const MONTH: u64 = 30 * DAY;
+ const YEAR: u64 = 365 * DAY;
+ if seconds < MINUTE {
+ return "just now".into();
+ }
+ // Each unit, below which the next takes over, and what one of it ago is.
+ let units = [
+ (MINUTE, HOUR, "minute", None),
+ (HOUR, DAY, "hour", None),
+ (DAY, WEEK, "day", Some("yesterday")),
+ (WEEK, MONTH, "week", Some("last week")),
+ (MONTH, YEAR, "month", Some("last month")),
+ (YEAR, u64::MAX, "year", Some("last year")),
+ ];
+ let (unit, _, name, last) = units
+ .into_iter()
+ .find(|(_, below, ..)| seconds < *below)
+ .unwrap_or(units[units.len() - 1]);
+ match (seconds / unit, last) {
+ (1, Some(last)) => last.into(),
+ (1, None) => format!("1 {name} ago"),
+ (count, _) => format!("{count} {name}s ago"),
+ }
+}
+
+/// `seconds` on air, to the minute past the first.
+fn lasted(seconds: u64) -> String {
+ let (hours, minutes) = (seconds / 3600, seconds / 60 % 60);
+ match (hours, minutes) {
+ (0, 0) => format!("{seconds} s"),
+ (0, minutes) => format!("{minutes} min"),
+ (hours, 0) => format!("{hours} h"),
+ (hours, minutes) => format!("{hours} h {minutes} min"),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ /// A show opened again moves to the front rather than appearing twice, and
+ /// keeps when it was last live.
+ #[test]
+ fn reopening_moves_a_show_to_the_front() {
+ let mut recent = Recent::default();
+ recent.opened(Path::new("/shows/a.toml"), 10);
+ recent.opened(Path::new("/shows/b.toml"), 20);
+ recent.aired(Path::new("/shows/a.toml"), Aired { at: 12, lasted: 60 });
+ recent.opened(Path::new("/shows/a.toml"), 30);
+ let paths: Vec<_> = recent.shows.iter().map(|show| show.path.clone()).collect();
+ assert_eq!(paths, [PathBuf::from("/shows/a.toml"), PathBuf::from("/shows/b.toml")]);
+ assert_eq!(recent.shows[0].opened, 30);
+ assert_eq!(recent.shows[0].live, Some(Aired { at: 12, lasted: 60 }));
+ }
+
+ /// A card says when the show was last live and for how long, as a person
+ /// would.
+ #[test]
+ fn says_when_a_show_was_live() {
+ let day = 24 * 3600;
+ let show = |live| Show {
+ path: "/shows/a.toml".into(),
+ opened: 0,
+ live,
+ portrait: false,
+ };
+ let now = 400 * day;
+ let aired = |ago: u64, lasted: u64| Some(Aired { at: now - ago, lasted });
+ assert_eq!(
+ said(&show(aired(3 * day, 3 * 3600 + 120)), now),
+ "Live 3 days ago, for 3 h 2 min"
+ );
+ assert_eq!(said(&show(aired(8 * day, 6000)), now), "Live last week, for 1 h 40 min");
+ assert_eq!(
+ said(&show(aired(14 * day, 55 * 60)), now),
+ "Live 2 weeks ago, for 55 min"
+ );
+ assert_eq!(said(&show(aired(40 * day, 7200)), now), "Live last month, for 2 h");
+ assert_eq!(said(&show(None), now), "Opened last year, never live");
+ }
+
+ /// A picture shrinks by averaging, to the width asked for at most.
+ #[test]
+ fn shrinks_a_picture() {
+ let picture = Picture {
+ width: 4,
+ height: 2,
+ rgba: [[0, 0, 0, 255], [200, 100, 50, 255]].repeat(4).concat(),
+ };
+ let small = picture.shrink(2);
+ assert_eq!((small.width, small.height), (2, 1));
+ assert_eq!(&small.rgba[..4], &[100, 50, 25, 255]);
+ }
+}
diff --git a/rs/moq-studio/src/main.rs b/rs/moq-studio/src/main.rs
index 6deb6293..496d3e65 100644
--- a/rs/moq-studio/src/main.rs
+++ b/rs/moq-studio/src/main.rs
@@ -26,6 +26,7 @@ mod hotkeys;
mod interact;
mod kit;
mod ladder;
+mod library;
mod mix;
mod past;
mod record;
@@ -34,7 +35,9 @@ mod setup;
mod shelf;
#[cfg(test)]
mod shots;
+mod show;
mod snap;
+mod stream;
mod ui;
mod uplink;
mod vcam;
@@ -45,9 +48,9 @@ mod watch;
#[usage(name = "moq-studio")]
#[usage(completion, settings, unknown_env(prefix = "MOQ_"))]
pub struct Config {
- /// The scene collection to open, saved as it changes. If it does not exist,
- /// guided setup proposes one and creates it. Defaults to `scenes.toml` in
- /// the studio's data directory, which a launch from the desktop opens.
+ /// The show to open: its scene file, saved as it changes. If it does not
+ /// exist, guided setup proposes one and creates it. Without one, the
+ /// window opens on your recent shows.
#[usage(value_hint = usage_rs::ValueHint::FilePath, extensions("toml"))]
pub scenes: Option,
@@ -59,19 +62,21 @@ pub struct Config {
#[usage(long)]
pub watch: Option,
- /// The broadcast path the program is published at.
- #[usage(long, default = "studio")]
- pub broadcast: String,
+ /// The broadcast path the program goes live at, for this run, over the
+ /// show's own (Settings, Stream), which is `studio` until set.
+ #[usage(long)]
+ pub broadcast: Option,
/// Copy and encode the program video at the canvas size only while someone
- /// watches it, as the half size always is. This saves the work while
- /// nobody does, but leaves a late viewer nothing from before they arrived.
- /// Audio is always encoded, and a recording has an encoder of its own.
+ /// watches it, as the half size always is, for this run. This saves the
+ /// work while nobody does, but leaves a late viewer nothing from before
+ /// they arrived. Audio is always encoded, and a recording has an encoder
+ /// of its own.
#[usage(long)]
pub on_demand: bool,
- /// The directory recordings and clips are saved in. Defaults to a named
- /// scene file's, or else to the user's videos folder.
+ /// The directory recordings and clips are saved in, for this run.
+ /// Defaults to the scene file's.
#[usage(long, value_hint = usage_rs::ValueHint::DirPath)]
pub recordings: Option,
@@ -79,16 +84,16 @@ pub struct Config {
#[usage(flatten)]
pub guests: guests::Config,
- /// The MoQ client configuration. Without `--connect`, nothing is published
- /// and broadcast sources stay offline.
+ /// The MoQ client configuration. `--connect` names the relay to go live
+ /// on for this run, over the show's own, and `--connect-token` goes to it
+ /// alone. Nothing is published until you go live.
#[usage(flatten)]
pub client: moq_tokio::connect::Config,
- /// A second relay to publish the same broadcast to, so a viewer linked to
- /// both resumes from it when the `--connect` relay fails. Both must speak
- /// the one `--connect-version`, which must carry broadcast instances. Its
- /// credential rides its URL's `?jwt=`: `--connect-token` goes to the
- /// `--connect` relay only.
+ /// A second relay to publish the same broadcast to, for this run, so a
+ /// viewer linked to both resumes from it when the first fails. Both must
+ /// speak the one `--connect-version`, which must carry broadcast instances.
+ /// Its credential is its own, from Settings or its URL's `?jwt=`.
#[usage(long)]
pub failover: Option,
@@ -102,11 +107,10 @@ pub struct Config {
}
impl Config {
- /// The scene file to open and the directory recordings go to, or `None` to
- /// `--watch` a broadcast, refusing the studio's own flags beside it.
- fn files(&self) -> anyhow::Result> {
+ /// Refuse `--watch` beside the studio's own flags.
+ fn check_watch(&self) -> anyhow::Result<()> {
if self.watch.is_none() {
- return Files::new(self.scenes.clone(), self.recordings.clone()).map(Some);
+ return Ok(());
}
anyhow::ensure!(
self.scenes.is_none(),
@@ -120,7 +124,7 @@ impl Config {
self.guests.room.is_none(),
"--watch publishes nothing, so it has no --room"
);
- Ok(None)
+ Ok(())
}
/// Refuse a config parsed from released spellings, naming what replaced each.
@@ -131,91 +135,34 @@ impl Config {
Ok(())
}
- /// The relays to publish to, the `--connect` one first; none without it.
- fn relays(&self) -> anyhow::Result> {
- let Some(url) = self.client.url.clone() else {
- anyhow::ensure!(self.failover.is_none(), "--failover needs --connect");
- return Ok(Vec::new());
- };
- if self.failover.is_some() {
- self.check_failover()?;
- }
- let client = self.client.clone().init(self.quic.clone())?;
- let mut relays = vec![engine::Relay { client, url }];
- if let Some(url) = self.failover.clone() {
- let mut client = self.client.clone();
- // Its own credentials ride its URL; another operator's never reach it.
- client.tokens.clear();
- let client = client
- .init(self.quic.clone())
- .context("cannot set up the failover relay")?;
- relays.push(engine::Relay { client, url });
+ /// What the flags set for every show this run opens.
+ fn flags(&self) -> stream::Flags {
+ stream::Flags {
+ relay: self.client.url.clone(),
+ tokens: self.client.tokens.clone(),
+ failover: self.failover.clone(),
+ broadcast: self.broadcast.clone(),
+ on_demand: self.on_demand,
+ recordings: self.recordings.clone(),
+ guests: self.guests.clone(),
}
- Ok(relays)
}
- /// Refuse relays a viewer could not move between without a restart: a copy
- /// resumes mid-group only under the same instance, which needs both links on
- /// one protocol version that carries it.
- fn check_failover(&self) -> anyhow::Result<()> {
- let [version] = self.client.version.as_slice() else {
- anyhow::bail!("--failover needs exactly one --connect-version, so both relays negotiate the same protocol");
- };
- anyhow::ensure!(
- matches!(version, moq_net::Version::Lite(lite) if lite.has_instance()),
- "--failover needs a --connect-version with broadcast instances (moq-lite-07-wip today), not {version}, \
- or a viewer moving between relays restarts the stream rather than resuming it"
- );
- // Each relay's client binds its own socket, so a fixed port would be
- // bound twice.
- anyhow::ensure!(
- self.client.bind.is_none_or(|bind| bind.port() == 0),
- "--failover cannot share a fixed --connect-bind port between the two relays; leave the port 0"
- );
- Ok(())
+ /// Every other client flag, which each relay is dialed with: the relay and
+ /// its tokens come from the show, or the flags over it.
+ fn client(&self) -> moq_tokio::connect::Config {
+ let mut client = self.client.clone();
+ client.url = None;
+ client.tokens.clear();
+ client
}
}
-/// Where the studio keeps its own files: the window's settings, and the scene
-/// collection when none is named.
+/// Where the studio keeps its own files, such as the window's settings.
fn data_dir() -> Option {
eframe::storage_dir("moq-studio")
}
-/// The files the studio opens and writes.
-#[derive(Debug, PartialEq)]
-struct Files {
- scenes: PathBuf,
- recordings: PathBuf,
-}
-
-impl Files {
- /// A named scene file keeps its recordings beside it. With none named, as
- /// from the desktop, which passes no arguments, the collection is the one
- /// in the data directory, where nobody looks for a recording, so they go to
- /// the videos folder instead.
- fn new(scenes: Option, recordings: Option) -> anyhow::Result {
- if let Some(scenes) = scenes {
- let recordings = recordings.unwrap_or_else(|| match scenes.parent() {
- Some(dir) if !dir.as_os_str().is_empty() => dir.to_path_buf(),
- _ => PathBuf::from("."),
- });
- return Ok(Self { scenes, recordings });
- }
- let data = data_dir().context("this system has no data directory to keep scenes in; name a scene file")?;
- let recordings = match recordings {
- Some(dir) => dir,
- None => dirs::video_dir()
- .or_else(std::env::home_dir)
- .context("this system has no videos folder; pass --recordings")?,
- };
- Ok(Self {
- scenes: data.join("scenes.toml"),
- recordings,
- })
- }
-}
-
fn main() -> anyhow::Result {
// A browser source's helper, and CEF's subprocesses, are this executable
// started again.
@@ -226,53 +173,35 @@ fn main() -> anyhow::Result {
let config = Config::parse();
config.log.init()?;
config.check_deprecated()?;
- let Some(Files { scenes, recordings }) = config.files()? else {
- let url = config.watch.clone().expect("checked by files");
+ config.check_watch()?;
+ if let Some(url) = config.watch.clone() {
watch::run(config.client.clone(), config.quic.clone(), url)?;
return Ok(ExitCode::SUCCESS);
- };
+ }
let runtime = tokio::runtime::Runtime::new()?;
// The QUIC client binds its socket on the current runtime.
let _runtime = runtime.enter();
- let relays = config.relays()?;
let gpu = pollster::block_on(engine::Gpu::new(wgpu::Backends::all()))?;
- let room = config.guests.load(config.client.url.as_ref())?.map(Arc::new);
- if let Some(room) = &room {
- let renewal = guests::Renewal::connect(room.clone(), &config.client, config.quic.clone())?;
- runtime.spawn(renewal.run());
- }
- let launch = engine::Launch {
+ let opener = show::Opener {
gpu: gpu.clone(),
- relays,
- broadcast: config.broadcast.clone(),
- on_demand: config.on_demand,
- recordings,
- room,
+ runtime: runtime.handle().clone(),
+ flags: config.flags(),
+ client: config.client(),
+ quic: config.quic.clone(),
+ credentials: stream::Credentials::system(),
};
- let exists = scenes
- .try_exists()
- .with_context(|| format!("reading {}", scenes.display()))?;
- if !exists && let Some(dir) = scenes.parent().filter(|dir| !dir.as_os_str().is_empty()) {
- // Setup creates the file, but not the data directory a first launch
- // from the desktop names.
- std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
- }
- let app = match exists {
- true => {
- let (autosave, loaded) = autosave::Autosave::open(scenes.clone())?;
- App::Starting(runtime.block_on(engine::Engine::start(launch, autosave, loaded))?)
- }
- // Nothing is saved until setup starts the studio, so quitting before then
- // leaves no file and the next launch runs setup again.
- false => App::Setup(Box::new(setup::Setup::new(
- launch,
- scenes.clone(),
- runtime.handle().clone(),
- )?)),
+ let app = match &config.scenes {
+ // Nothing is saved until setup starts the studio, so quitting before
+ // then leaves no file and the next launch runs setup again.
+ Some(scenes) => match runtime.block_on(opener.open(scenes.clone()))? {
+ show::Opened::Studio(engine, stream) => App::Starting(engine, stream),
+ show::Opened::Setup(setup, stream) => App::Setup(setup, stream),
+ },
+ None => App::Library(Box::new(library::Library::new(library::pictures())), None),
};
- run(app, gpu, runtime.handle().clone())?;
+ run(app, opener)?;
Ok(ExitCode::SUCCESS)
}
@@ -291,27 +220,39 @@ fn shutdown(engine: &engine::Engine, runtime: &tokio::runtime::Handle) -> anyhow
saved
}
+/// A show being opened from the library.
+type Opening = setup::Task>;
+
/// What the window shows.
enum App {
- /// First launch: guided setup, until it starts the studio.
- Setup(Box),
+ /// No show open: the library, and the show it is opening.
+ Library(Box, Option),
+ /// A new show: guided setup, until it starts the studio.
+ Setup(Box, stream::Stream),
/// An engine whose studio is built once the window can draw it.
- Starting(Arc),
+ Starting(Arc, stream::Stream),
Studio(Box),
+ /// Between two of the above, within the frame that moves from one to the
+ /// next.
+ Moving,
}
/// The window's contents, and what building the studio takes.
struct Window {
app: App,
render: eframe::egui_wgpu::RenderState,
- runtime: tokio::runtime::Handle,
+ opener: show::Opener,
/// The engine once the studio is built, for ending it on quit.
started: Arc>>,
+ /// The shows this user has opened.
+ recent: library::Recent,
+ /// The open show's last stream off air that [`Self::recent`] has noted.
+ aired: Option,
keyboard: hotkeys::Keyboard,
/// The window's own effects, on the setup screen and in the studio.
fx: fx::Fx,
/// Whether the studio's title bar is the window's frame, rather than the
- /// desktop's, which guided setup keeps.
+ /// desktop's, which the library and guided setup keep.
framed: bool,
/// Set once the window is asked to close, as [`Events::closed`] is.
closed: Arc,
@@ -324,6 +265,7 @@ impl eframe::App for Window {
if ctx.input(|input| input.viewport().close_requested()) {
self.closed.store(true, Ordering::Relaxed);
}
+ self.follow_air();
if let App::Studio(studio) = &mut self.app {
studio.logic(ctx, frame);
}
@@ -340,28 +282,112 @@ impl eframe::App for Window {
if let App::Studio(studio) = &mut self.app {
studio.save(storage);
}
+ self.follow_air();
+ if let Some(engine) = self.started.get() {
+ let path = engine.autosave.path();
+ // Its time on air so far, in case the window closes while live.
+ if let Some(live) = engine.on_air().live() {
+ let lasted = live.since.elapsed().as_secs();
+ let at = library::now().saturating_sub(lasted);
+ self.recent.aired(path, library::Aired { at, lasted });
+ }
+ // The show's picture is the program as it was when the window
+ // closed.
+ if self.closed.load(Ordering::Relaxed) {
+ let pictures = library::pictures().context("this system has no app storage");
+ let picture = pictures.and_then(|pictures| Ok((pictures, library::capture(&self.opener.gpu, engine)?)));
+ let saved = picture.and_then(|(pictures, picture)| {
+ self.recent.shaped(path, picture.portrait());
+ library::save_picture(&pictures, path, &picture)
+ });
+ if let Err(err) = saved {
+ tracing::warn!(error = format!("{err:#}"), "cannot keep the show's picture");
+ }
+ }
+ }
+ self.recent.save(storage);
}
}
impl Window {
- /// Draw setup or the studio, building the studio once its engine starts.
+ /// Draw the library, setup, or the studio, building the studio once its
+ /// engine starts.
fn show(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
- let engine = match &mut self.app {
- App::Studio(studio) => return studio.ui(ui, frame, &mut self.fx),
- App::Setup(setup) => match setup.ui(ui, &mut self.fx) {
- Some(engine) => engine,
- None => return,
+ match std::mem::replace(&mut self.app, App::Moving) {
+ App::Studio(mut studio) => {
+ studio.ui(ui, frame, &mut self.fx);
+ self.app = App::Studio(studio);
+ }
+ App::Library(library, opening) => self.library(ui, library, opening),
+ App::Setup(mut setup, stream) => match setup.ui(ui, &mut self.fx) {
+ Some(engine) => {
+ self.app = App::Starting(engine, stream);
+ ui.ctx().request_repaint();
+ }
+ None => self.app = App::Setup(setup, stream),
},
- App::Starting(engine) => engine.clone(),
- };
+ App::Starting(engine, stream) => self.start(ui, frame, engine, stream),
+ App::Moving => unreachable!("the window moves between pages within a frame"),
+ }
+ }
+
+ /// Draw the library, and open the show picked there, off this thread.
+ fn library(&mut self, ui: &mut egui::Ui, mut library: Box, mut opening: Option) {
+ if let Some(task) = &mut opening {
+ ui.ctx().request_repaint_after(std::time::Duration::from_millis(50));
+ if task.poll().is_some() {
+ let Some(setup::Task::Done(opened)) = opening.take() else {
+ unreachable!("polled done")
+ };
+ library.opening = None;
+ match opened {
+ Ok(show::Opened::Studio(engine, stream)) => {
+ self.app = App::Starting(engine, stream);
+ return ui.ctx().request_repaint();
+ }
+ Ok(show::Opened::Setup(setup, stream)) => {
+ self.app = App::Setup(setup, stream);
+ return ui.ctx().request_repaint();
+ }
+ Err(err) => library.error = Some(format!("{err:#}")),
+ }
+ }
+ }
+ let runtime = self.opener.runtime.clone();
+ if let Some(library::Pick::Open(path)) = library.ui(ui, &mut self.recent, &runtime) {
+ let name = path.file_stem().unwrap_or(path.as_os_str()).to_string_lossy();
+ library.opening = Some(name.into_owned());
+ library.error = None;
+ let opener = self.opener.clone();
+ match setup::Task::spawn(&runtime, move || async move { opener.open(path).await }) {
+ Ok(task) => opening = Some(task),
+ Err(err) => {
+ library.opening = None;
+ library.error = Some(format!("cannot open the show: {err}"));
+ }
+ }
+ }
+ self.app = App::Library(library, opening);
+ }
+
+ /// Build the studio around `engine`, now that the window can draw it.
+ fn start(
+ &mut self,
+ ui: &mut egui::Ui,
+ frame: &mut eframe::Frame,
+ engine: Arc,
+ stream: stream::Stream,
+ ) {
let _ = self.started.set(engine.clone());
- // After setup, the desktop's frame goes. macOS cannot draw under its
- // title bar once the window is open, so it keeps it until a restart.
+ self.recent.opened(engine.autosave.path(), library::now());
+ // Once a show is open, the desktop's frame goes. macOS cannot draw under
+ // its title bar once the window is open, so it keeps it until a restart.
if !self.framed && !cfg!(target_os = "macos") {
ui.ctx().send_viewport_cmd(egui::ViewportCommand::Decorations(false));
self.framed = true;
}
- let mut studio = ui::Studio::new(&self.render, engine, self.runtime.clone(), self.framed);
+ let runtime = self.opener.runtime.clone();
+ let mut studio = ui::Studio::new(&self.render, engine, stream, runtime, self.framed);
studio.load(hotkeys::Window {
frame,
ctx: ui.ctx(),
@@ -370,6 +396,20 @@ impl Window {
self.app = App::Studio(Box::new(studio));
ui.ctx().request_repaint();
}
+
+ /// Note when the open show's last stream went live, and how long it
+ /// stayed, once it ends.
+ fn follow_air(&mut self) {
+ let Some(engine) = self.started.get() else { return };
+ let Some(aired) = engine.aired() else { return };
+ if self.aired == Some(aired) {
+ return;
+ }
+ self.aired = Some(aired);
+ let at = library::now().saturating_sub(aired.since.elapsed().as_secs());
+ let lasted = aired.lasted.as_secs();
+ self.recent.aired(engine.autosave.path(), library::Aired { at, lasted });
+ }
}
/// eframe's handler for the window's events, which hears keys before egui,
@@ -466,12 +506,12 @@ fn icon() -> egui::IconData {
}
/// Show the window until it is closed, then end the studio.
-fn run(app: App, gpu: engine::Gpu, runtime: tokio::runtime::Handle) -> anyhow::Result<()> {
+fn run(app: App, opener: show::Opener) -> anyhow::Result<()> {
let started = Arc::new(OnceLock::new());
// The studio's title bar is the window's: the desktop draws no frame, and
- // macOS draws its buttons over the studio's bar. Guided setup keeps the
- // desktop's until it starts the studio.
- let framed = matches!(app, App::Starting(_));
+ // macOS draws its buttons over the studio's bar. The library and guided
+ // setup keep the desktop's until a show opens.
+ let framed = matches!(app, App::Starting(..));
let mut viewport = egui::ViewportBuilder::default()
.with_title("kixel.live Studio")
// The Wayland app id and X11 class, which the desktop finds the
@@ -489,6 +529,7 @@ fn run(app: App, gpu: engine::Gpu, runtime: tokio::runtime::Handle) -> anyhow::R
false => viewport.with_decorations(false),
};
}
+ let gpu = opener.gpu.clone();
let options = eframe::NativeOptions {
viewport,
wgpu_options: eframe::egui_wgpu::WgpuConfiguration {
@@ -513,7 +554,7 @@ fn run(app: App, gpu: engine::Gpu, runtime: tokio::runtime::Handle) -> anyhow::R
let keyboard = hotkeys::Keyboard::default();
let window_keyboard = keyboard.clone();
let window_started = started.clone();
- let window_runtime = runtime.clone();
+ let runtime = opener.runtime.clone();
let closed = Arc::new(AtomicBool::new(false));
let window_closed = closed.clone();
let eframe = eframe::create_native(
@@ -525,12 +566,15 @@ fn run(app: App, gpu: engine::Gpu, runtime: tokio::runtime::Handle) -> anyhow::R
.clone()
.context("the window is not drawn with wgpu")?;
kit::install(&cc.egui_ctx);
- let fx = fx::Fx::new(&render, cc.storage, &cc.egui_ctx, &window_runtime);
+ let fx = fx::Fx::new(&render, cc.storage, &cc.egui_ctx, &opener.runtime);
+ let recent = library::Recent::load(cc.storage);
Ok(Box::new(Window {
app,
render,
- runtime: window_runtime,
+ opener,
started: window_started,
+ recent,
+ aired: None,
keyboard: window_keyboard,
fx,
framed,
@@ -558,90 +602,58 @@ fn run(app: App, gpu: engine::Gpu, runtime: tokio::runtime::Handle) -> anyhow::R
#[cfg(test)]
mod tests {
- use std::path::Path;
- use std::time::Duration;
-
use super::*;
+ fn parse(args: &[&str]) -> Config {
+ let args: Vec<_> = args.iter().map(std::ffi::OsStr::new).collect();
+ Config::parse_from(&args).unwrap()
+ }
+
+ /// A scene file and a relay parse as flags over the show's own settings,
+ /// with the relay and its tokens taken out of the client every relay is
+ /// dialed with.
#[test]
fn parses_a_scene_file_and_a_relay() {
- let config = Config::parse_from(&[
- std::ffi::OsStr::new("show.toml"),
- std::ffi::OsStr::new("--connect"),
- std::ffi::OsStr::new("http://localhost:4443"),
- ])
- .expect("a scene file and a relay should parse");
+ let config = parse(&[
+ "show.toml",
+ "--connect",
+ "http://localhost:4443",
+ "--connect-token",
+ "secret",
+ ]);
config.check_deprecated().unwrap();
- let files = config.files().unwrap().unwrap();
- assert_eq!(files.scenes, Path::new("show.toml"));
- assert_eq!(files.recordings, Path::new("."), "recordings go beside the scene file");
- assert_eq!(config.broadcast, "studio");
- assert!(!config.on_demand, "the program encodes whatever the demand by default");
- assert!(config.guests.room.is_none(), "guests are opt-in");
- assert_eq!(
- Duration::from(config.guests.guest_expiry),
- Duration::from_secs(4 * 3600)
- );
- assert_eq!(config.client.url.unwrap().port(), Some(4443));
+ config.check_watch().unwrap();
+ assert_eq!(config.scenes.as_deref(), Some(std::path::Path::new("show.toml")));
+ let flags = config.flags();
+ assert_eq!(flags.broadcast, None, "the show's broadcast name stands");
+ assert!(!flags.on_demand, "the program encodes whatever the demand by default");
+ assert!(flags.guests.room.is_none(), "guests are opt-in");
+ assert_eq!(flags.relay.unwrap().port(), Some(4443));
+ assert_eq!(flags.tokens.len(), 1);
+ let client = config.client();
+ assert!(client.url.is_none() && client.tokens.is_empty());
}
- /// A failover relay is accepted only on one protocol version that carries
- /// broadcast instances, so a viewer moving between the relays resumes rather
- /// than restarts.
+ /// With no arguments the studio opens on the library.
#[test]
- fn failover_needs_one_version_with_instances() {
- let check = |extra: &[&str]| {
- let mut args = vec!["show.toml", "--connect", "https://a/", "--failover", "https://b/"];
- args.extend(extra);
- let args: Vec<_> = args.into_iter().map(std::ffi::OsStr::new).collect();
- Config::parse_from(&args).unwrap().check_failover()
- };
- let lite07 = ["--connect-version", "moq-lite-07-wip"];
- check(&lite07).expect("one version with instances");
- check(&[&lite07[..], &["--connect-bind", "127.0.0.1:0"]].concat()).expect("an ephemeral port");
- for refused in [
- &[][..],
- &["--connect-version", "moq-lite-06"],
- &["--connect-version", "moq-transport-22"],
- &[&lite07[..], &["--connect-version", "moq-lite-06"]].concat(),
- // Each relay binds its own socket.
- &[&lite07[..], &["--connect-bind", "127.0.0.1:4444"]].concat(),
- ] {
- assert!(check(refused).is_err(), "{refused:?} was accepted");
- }
+ fn opens_with_no_arguments() {
+ let config = parse(&[]);
+ config.check_watch().unwrap();
+ assert!(config.scenes.is_none() && config.watch.is_none());
}
/// `--watch` opens no scene file and names its own relay.
#[test]
fn watch_takes_no_scene_file_or_relay() {
- let parse = |args: &[&str]| {
- let args: Vec<_> = args.iter().map(std::ffi::OsStr::new).collect();
- Config::parse_from(&args).unwrap()
- };
- let watch = parse(&["--watch", "https://relay.example/anon/studio"]);
- assert_eq!(watch.files().unwrap(), None);
+ parse(&["--watch", "https://relay.example/anon/studio"])
+ .check_watch()
+ .unwrap();
for refused in [
&["show.toml", "--watch", "https://a/b"][..],
&["--watch", "https://a/b", "--connect", "https://a/"],
&["--watch", "https://a/b", "--room", "guests"],
] {
- assert!(parse(refused).files().is_err(), "{refused:?} was accepted");
+ assert!(parse(refused).check_watch().is_err(), "{refused:?} was accepted");
}
}
-
- /// With no scene file named, as from the desktop, the studio opens the one
- /// in its data directory and records to the videos folder, unless told
- /// where.
- #[test]
- fn defaults_to_the_collection_in_the_data_directory() {
- let files = Files::new(None, None).unwrap();
- assert!(files.scenes.is_absolute());
- assert!(files.recordings.is_absolute());
- assert_ne!(Some(files.recordings.as_path()), files.scenes.parent());
-
- let files = Files::new(None, Some("clips".into())).unwrap();
- assert_eq!(files.recordings, Path::new("clips"));
- let files = Files::new(Some("shows/main.toml".into()), None).unwrap();
- assert_eq!(files.recordings, Path::new("shows"));
- }
}
diff --git a/rs/moq-studio/src/replay.rs b/rs/moq-studio/src/replay.rs
index 50b8388a..c0830e09 100644
--- a/rs/moq-studio/src/replay.rs
+++ b/rs/moq-studio/src/replay.rs
@@ -89,17 +89,12 @@ impl Engine {
/// What a replay of `of` could copy, ending now, the most wanted first:
/// the program at each of its sizes, largest first, or a source's one.
///
- /// The program has a past only with a relay, from while it was encoded, a
- /// local source, relay or not, only from while it was on program, shown or
+ /// The program has a past only on air, from while it was encoded, a local
+ /// source, on air or not, only from while it was on program, shown or
/// heard, or recorded, and a broadcast source once it shows a picture.
pub fn targets(&self, of: &Of) -> anyhow::Result> {
let name = match of {
- Of::Program => {
- self.output
- .as_ref()
- .context("without --connect, the program holds no past to replay")?;
- return Ok(self.program.program());
- }
+ Of::Program => return self.program_targets(),
Of::Source(name) => name,
};
let state = self.state.lock().unwrap();
diff --git a/rs/moq-studio/src/setup.rs b/rs/moq-studio/src/setup.rs
index ff3cc03a..ec7ba09b 100644
--- a/rs/moq-studio/src/setup.rs
+++ b/rs/moq-studio/src/setup.rs
@@ -17,7 +17,7 @@ use moq_scene::{Canvas, Collection, Draft, Edit, History, Item, Rect, Scene};
use crate::autosave::{self, Autosave};
use crate::devices::{self, Listing, Pick};
-use crate::engine::{self, Engine, Launch};
+use crate::engine::{self, Engine, Launch, Relay};
use crate::fx::{Fx, Mark};
use crate::health::rate;
use crate::uplink;
@@ -44,7 +44,7 @@ const fn canvas(width: u32, height: u32, framerate: u32) -> Canvas {
/// Health panel's headroom grade healthy, so a proposal is not flagged the
/// moment it goes live. The rest absorbs the estimate's error and whatever else
/// shares the connection.
-fn headroom() -> f64 {
+pub(crate) fn headroom() -> f64 {
1.0 / hang::health::Thresholds::default().transport.headroom.degraded
}
@@ -79,12 +79,12 @@ pub struct Encoding {
impl Encoding {
/// What the program sends: this video plus the audio.
- fn total(&self) -> Rate {
+ pub(crate) fn total(&self) -> Rate {
Rate::from_bps(self.bitrate.as_bps() + engine::AUDIO_BITRATE.as_bps())
}
/// Whether the encoder keeps up with `canvas` in real time, within [`PACE`].
- fn keeps_up(&self, canvas: Canvas) -> bool {
+ pub(crate) fn keeps_up(&self, canvas: Canvas) -> bool {
self.frame <= budget(canvas)
}
@@ -125,7 +125,7 @@ fn candidates(encoders: &Encoders) -> Vec<(Canvas, &Encoding)> {
/// Open the exporter the program would for each canvas, the same probe that
/// picks its encoder at startup, and time a burst through what it settled on.
async fn probe_encoders(gpu: engine::Gpu) -> Encoders {
- let picture = Picture::new(&gpu);
+ let picture = Picture::new(&gpu, &CANVASES);
let mut probed = Vec::new();
for canvas in CANVASES {
let result = probe_encoder(&gpu, &picture, canvas)
@@ -136,6 +136,15 @@ async fn probe_encoders(gpu: engine::Gpu) -> Encoders {
probed
}
+/// How `canvas` encodes on this machine, as going live checks it: the probe
+/// setup runs for each canvas it could propose.
+pub(crate) async fn probe(gpu: engine::Gpu, canvas: Canvas) -> Result {
+ let picture = Picture::new(&gpu, &[canvas]);
+ probe_encoder(&gpu, &picture, canvas)
+ .await
+ .map_err(|err| format!("{err:#}"))
+}
+
async fn probe_encoder(gpu: &engine::Gpu, picture: &Picture, canvas: Canvas) -> anyhow::Result {
use moq_video::render::export::{Exporter, Route};
@@ -220,14 +229,14 @@ async fn time(budget: Duration, mut encode: impl AsyncFnMut(u32) -> anyhow::Resu
Ok(started.elapsed() / BURST)
}
-/// A picture as large as the largest canvas plus a burst's panning, uploaded
-/// once and copied from for every canvas.
+/// A picture as large as the largest canvas it serves plus a burst's panning,
+/// uploaded once and copied from for each of them.
struct Picture(wgpu::Texture);
impl Picture {
- fn new(gpu: &engine::Gpu) -> Self {
- let width = CANVASES.iter().map(|canvas| canvas.width).max().unwrap_or_default() + PAN * BURST;
- let height = CANVASES.iter().map(|canvas| canvas.height).max().unwrap_or_default();
+ fn new(gpu: &engine::Gpu, canvases: &[Canvas]) -> Self {
+ let width = canvases.iter().map(|canvas| canvas.width).max().unwrap_or_default() + PAN * BURST;
+ let height = canvases.iter().map(|canvas| canvas.height).max().unwrap_or_default();
let texture = gpu.device.create_texture(&wgpu::TextureDescriptor {
label: Some("setup picture"),
size: extent(width, height),
@@ -465,7 +474,7 @@ fn corner(canvas: &Canvas) -> Rect {
}
/// A result being worked out in the background.
-enum Task {
+pub(crate) enum Task {
Running(mpsc::Receiver),
Done(T),
}
@@ -476,7 +485,7 @@ impl Task {
/// blocking pool, which dropping the runtime waits out, so a probe that never
/// ends, such as one waiting on a camera permission prompt, cannot hold the
/// studio open once its window closes.
- fn spawn(runtime: &tokio::runtime::Handle, task: F) -> std::io::Result
+ pub(crate) fn spawn(runtime: &tokio::runtime::Handle, task: F) -> std::io::Result
where
F: FnOnce() -> Fut + Send + 'static,
Fut: Future,
@@ -493,7 +502,7 @@ impl Task {
///
/// Panics if the task did, rather than waiting forever on a result that
/// will never come.
- fn poll(&mut self) -> Option<&T> {
+ pub(crate) fn poll(&mut self) -> Option<&T> {
if let Self::Running(recv) = self {
match recv.try_recv() {
Ok(done) => *self = Self::Done(done),
@@ -512,6 +521,8 @@ impl Task {
pub struct Setup {
runtime: tokio::runtime::Handle,
launch: Launch,
+ /// The relay the uplink is measured to, and the broadcast measured as.
+ relay: Option<(Relay, String)>,
/// The scene file, which does not exist until setup starts the studio.
path: PathBuf,
devices: Task,
@@ -526,14 +537,21 @@ pub struct Setup {
}
impl Setup {
- /// Start probing for `launch`, to create the scene file at `path`.
- pub fn new(launch: Launch, path: PathBuf, runtime: tokio::runtime::Handle) -> std::io::Result {
+ /// Start probing for `launch`, to create the scene file at `path`, with
+ /// the uplink measured to `relay` as the broadcast it names.
+ pub fn new(
+ launch: Launch,
+ relay: Option<(Relay, String)>,
+ path: PathBuf,
+ runtime: tokio::runtime::Handle,
+ ) -> std::io::Result {
let devices = Task::spawn(&runtime, devices::list)?;
let gpu = launch.gpu.clone();
let encoders = Task::spawn(&runtime, move || probe_encoders(gpu))?;
Ok(Self {
runtime,
launch,
+ relay,
path,
devices,
encoders,
@@ -598,15 +616,14 @@ impl Setup {
.map(|(_, encoding)| encoding.total())
.max()
.map(|top| top.scaled(1.0 / headroom()));
- // The first relay's: the stream's tracks claim their bitrate on its
+ // The show's relay: the stream's tracks claim their bitrate on its
// link alone, so it is the one probed toward the stream's rate.
let connect = self
- .launch
- .relays
- .first()
- .map(|relay| (relay.client.clone(), relay.url.clone(), self.launch.broadcast.clone()));
+ .relay
+ .as_ref()
+ .map(|(relay, path)| (relay.client.clone(), relay.url.clone(), path.clone()));
self.uplink = Some(match (connect, ceiling) {
- (None, _) => Task::Done(Err("no relay is configured; start with --connect to measure it".into())),
+ (None, _) => Task::Done(Err("no relay is set yet, so it was not measured".into())),
(Some(_), None) => Task::Done(Err("nothing encodes on this machine, so nothing would be sent".into())),
(Some((client, url, path)), Some(ceiling)) => {
let measured = Task::spawn(&self.runtime, move || async move {
@@ -960,15 +977,19 @@ mod tests {
}
/// The burst runs on a real device and encoder, through the program's own
- /// copy path.
+ /// copy path, for each canvas setup proposes and for a portrait show's,
+ /// taller than any of them, as going live checks it.
#[tokio::test(flavor = "multi_thread")]
#[ignore = "needs a Vulkan device and an H.264 encoder; run by just render-sw"]
async fn times_every_canvas() {
let gpu = engine::Gpu::new(wgpu::Backends::all()).await.expect("a GPU");
- for (canvas, result) in probe_encoders(gpu).await {
+ for (canvas, result) in probe_encoders(gpu.clone()).await {
let encoding = result.unwrap_or_else(|err| panic!("{}: {err}", describe(canvas)));
assert!(encoding.frame > Duration::ZERO, "{}", describe(canvas));
}
+ let portrait = canvas(720, 1280, 30);
+ let encoding = probe(gpu, portrait).await.expect("a portrait canvas encodes");
+ assert!(encoding.frame > Duration::ZERO);
}
#[test]
diff --git a/rs/moq-studio/src/shots.rs b/rs/moq-studio/src/shots.rs
index f38e1b2c..1733daf7 100644
--- a/rs/moq-studio/src/shots.rs
+++ b/rs/moq-studio/src/shots.rs
@@ -21,7 +21,7 @@ use moq_scene::source::Status;
use crate::fx::Effects;
use crate::ui::{Inspect, MixView, Page, Setting, Side};
-use crate::{App, Window, autosave, engine, fx, hotkeys, setup};
+use crate::{App, Window, autosave, engine, fx, hotkeys, library, setup, show, stream};
/// The window's size, the mock-ups'.
const SIZE: egui::Vec2 = egui::vec2(1440.0, 900.0);
@@ -36,12 +36,15 @@ const SETTLE: Duration = Duration::from_secs(20);
enum Open {
/// Guided setup, as the first launch shows it.
Setup,
+ /// The library, as the app opens with no show, with a few recent shows.
+ Library,
/// The studio on the show in `shots/`, in a window `size` large,
- /// turned to `view`.
+ /// turned to `view`, with a relay of its own to go live on if `relay`.
Studio {
view: View,
size: egui::Vec2,
show: &'static str,
+ relay: bool,
},
/// The kit's gallery, with a sheet open over it or not.
Kit { sheet: bool },
@@ -72,6 +75,14 @@ enum View {
/// The LIVE page after a mute, with the health dot's popover open and the
/// pointer on undo, which names the mute.
Hover,
+ /// The go-live sheet, once its checks finish.
+ Going,
+ /// On air, at the LIVE page.
+ OnAir,
+ /// On air, with the pointer on the clock.
+ EndStream,
+ /// On air, with the health dot's popover open.
+ OnAirHealth,
/// REVIEW, with this show's recordings and clips, and if `picked`, the
/// last 30 seconds held and a stretch picked. With `replays`, the show
/// has two replay sources to pick between.
@@ -99,6 +110,17 @@ fn studio(view: View) -> Open {
view,
size: SIZE,
show: "show.toml",
+ relay: false,
+ }
+}
+
+/// [`studio`], with a relay to go live on.
+fn relayed(view: View) -> Open {
+ Open::Studio {
+ view,
+ size: SIZE,
+ show: "show.toml",
+ relay: true,
}
}
@@ -109,6 +131,7 @@ fn mixing(view: MixView, opened: Option<&'static str>, size: egui::Vec2) -> Open
view: View::Mix(view, opened),
size,
show: "mix.toml",
+ relay: false,
}
}
@@ -149,10 +172,10 @@ views! {
live_hover: Still, studio(View::Hover);
/// A portrait show: the pages in a sidebar, and the scenes and the sound
/// in columns beside the picture.
- live_portrait: Still, Open::Studio { view: View::Page(Page::Live), size: SIZE, show: "portrait.toml" };
+ live_portrait: Still, Open::Studio { view: View::Page(Page::Live), size: SIZE, show: "portrait.toml", relay: false };
/// A window snapped beside a game stacks the picture, the scenes, and the
/// sound.
- live_compact: Still, Open::Studio { view: View::Page(Page::Live), size: HALF, show: "show.toml" };
+ live_compact: Still, Open::Studio { view: View::Page(Page::Live), size: HALF, show: "show.toml", relay: false };
/// Studio mode: the cued scene's pad turns amber, and NEXT docks on the
/// live picture with TAKE and AUTO.
live_studio: Still, studio(View::Cued(Page::Live));
@@ -170,7 +193,7 @@ views! {
build_menu: Still, studio(View::Menu);
/// BUILD in a window snapped beside a game: the inspector is a tab beside
/// Layers and Add.
- build_compact: Still, Open::Studio { view: BUILD, size: HALF, show: "show.toml" };
+ build_compact: Still, Open::Studio { view: BUILD, size: HALF, show: "show.toml", relay: false };
/// BUILD in Studio mode edits the scene on air, with NEXT docked on it.
build_studio: Still, studio(View::Cued(Page::Build));
/// A scene open off air: the frame turns amber, and the live picture
@@ -181,7 +204,7 @@ views! {
/// dimmed, saying why under the pointer.
build_off_air_refused: Still, studio(View::OffAir("Vertical"));
/// Too narrow for the banner beside the live picture, it goes under it.
- build_off_air_compact: Still, Open::Studio { view: View::OffAir("Break"), size: HALF, show: "show.toml" };
+ build_off_air_compact: Still, Open::Studio { view: View::OffAir("Break"), size: HALF, show: "show.toml", relay: false };
/// MIX: a strip per sound and per audio scene, with their sends, and a
/// small live picture in the sound's place.
mix: Still, mixing(MixView::Strips, None, SIZE);
@@ -189,13 +212,13 @@ views! {
/// its neighbours narrowed.
mix_strip: Still, mixing(MixView::Strips, Some("mic"), SIZE);
/// An audio scene's menu: rename, move, or remove it.
- mix_menu: Still, Open::Studio { view: View::MixMenu, size: SIZE, show: "mix.toml" };
+ mix_menu: Still, Open::Studio { view: View::MixMenu, size: SIZE, show: "mix.toml", relay: false };
/// The same sends as a matrix.
mix_routes: Still, mixing(MixView::Routes, None, SIZE);
/// MIX in a stacked window, its strips scrolling, its live picture as
/// tall as the sound's slot allows.
mix_compact: Still, mixing(MixView::Strips, None, HALF);
- /// REVIEW off air: without a relay the program keeps no past, which the
+ /// REVIEW off air: the program keeps no past until it goes live, which the
/// strip and both actions say, beside this show's recordings and clips.
review: Still, studio(View::Review { picked: false, replays: false });
/// REVIEW with the last 30 seconds held: a stretch picked between the
@@ -215,6 +238,22 @@ views! {
/// Settings' Show tab, with the encoders' timings.
output: Live, studio(View::Setting(Setting::Show));
hotkeys: Still, studio(View::Setting(Setting::Hotkeys));
+ /// With no show open: recent shows, each with its picture, beside New
+ /// show and Open a show file.
+ library: Still, Open::Library;
+ /// GO LIVE's sheet: the relay with its switch and its state, and the
+ /// checks once they finish.
+ going: Live, relayed(View::Going);
+ /// On air: the accent frame, ON AIR, and the clock.
+ on_air: Live, relayed(View::OnAir);
+ /// On air, the pointer on the clock, which reads END STREAM.
+ end_stream: Live, relayed(View::EndStream);
+ /// On air, the health dot's popover: the worst problem, every
+ /// destination, the viewers, and the encoder.
+ on_air_health: Live, relayed(View::OnAirHealth);
+ /// Settings' Stream tab: the relay, its token, the failover, the
+ /// broadcast's name, and on demand.
+ settings_stream: Still, studio(View::Setting(Setting::Stream));
}
/// Draw the view `name` opens into `$SHOTS_DIR/.png`, twice if it is
@@ -281,27 +320,60 @@ fn lavapipe() -> (tempfile::TempDir, tokio::runtime::Runtime, engine::Gpu) {
(show, runtime, gpu)
}
-/// How a view starts the studio: with no relay, recording beside the show.
+/// How a view starts the studio: recording beside the show.
fn launch(gpu: &engine::Gpu) -> engine::Launch {
engine::Launch {
gpu: gpu.clone(),
- relays: Vec::new(),
- broadcast: "studio".into(),
- on_demand: false,
recordings: ".".into(),
room: None,
}
}
+/// `show.toml`'s stream settings, on `relay` if given, with tokens in a store
+/// of the test's own.
+fn stream(relay: Option<&Relay>) -> stream::Stream {
+ let mut flags = stream::Flags::default();
+ let mut client = moq_tokio::connect::Config::default();
+ if let Some(relay) = relay {
+ flags.relay = Some(relay.url.clone());
+ client.tls.fingerprint = vec![relay.fingerprint.clone()];
+ }
+ let credentials = stream::Credentials::memory();
+ stream::Stream::load(Path::new("show.toml"), flags, client, Default::default(), credentials).unwrap()
+}
+
+/// A relay of the view's own, running on `runtime`.
+struct Relay {
+ url: url::Url,
+ fingerprint: String,
+}
+
+impl Relay {
+ fn start(runtime: &tokio::runtime::Runtime) -> Self {
+ runtime.block_on(async {
+ let _ = moq_tokio::crypto::install_default();
+ let fixture = moq_relay::test_relay().await.expect("test relay");
+ let ready = fixture.relay.ready();
+ tokio::spawn(fixture.relay.run());
+ ready.wait().await.expect("relay ready");
+ Self {
+ url: fixture.url,
+ fingerprint: fixture.fingerprint,
+ }
+ })
+ }
+}
+
/// Start the studio by `launch` on `show.toml`, without the history an
-/// earlier draw's edits left.
-fn start(launch: engine::Launch, runtime: &tokio::runtime::Runtime) -> App {
+/// earlier draw's edits left, streaming as `stream` sets.
+fn start(launch: engine::Launch, stream: stream::Stream, runtime: &tokio::runtime::Runtime) -> App {
let _ = std::fs::remove_file("show.history.toml");
let (autosave, loaded) = autosave::Autosave::open("show.toml".into()).unwrap();
App::Starting(
runtime
.block_on(engine::Engine::start(launch, autosave, loaded))
.unwrap(),
+ stream,
)
}
@@ -331,7 +403,8 @@ fn a_click_takes_and_the_panic_key_cuts() {
room: Some(Arc::new(room)),
..launch(&gpu)
};
- let (mut harness, started, closed) = window(start(launch, &runtime), &gpu, &runtime, SIZE);
+ let app = start(launch, stream(None), &runtime);
+ let (mut harness, started, closed) = window(app, &gpu, &runtime, SIZE, library::Recent::default());
harness.step();
let engine = started.get().expect("the studio started").clone();
runtime
@@ -398,7 +471,13 @@ fn a_layer_menu_edits_its_layer() {
let _entered = runtime.enter();
let fixture = Path::new(env!("CARGO_MANIFEST_DIR"));
std::fs::copy(fixture.join("shots/show.toml"), "show.toml").unwrap();
- let (mut harness, started, _) = window(start(launch(&gpu), &runtime), &gpu, &runtime, SIZE);
+ let (mut harness, started, _) = window(
+ start(launch(&gpu), stream(None), &runtime),
+ &gpu,
+ &runtime,
+ SIZE,
+ Default::default(),
+ );
harness.step();
let App::Studio(studio) = &mut harness.state_mut().app else {
panic!("the studio starts on its first frame")
@@ -498,7 +577,13 @@ fn studio_mode_cues_and_takes() {
let _entered = runtime.enter();
let fixture = Path::new(env!("CARGO_MANIFEST_DIR"));
std::fs::copy(fixture.join("shots/show.toml"), "show.toml").unwrap();
- let (mut harness, started, _) = window(start(launch(&gpu), &runtime), &gpu, &runtime, SIZE);
+ let (mut harness, started, _) = window(
+ start(launch(&gpu), stream(None), &runtime),
+ &gpu,
+ &runtime,
+ SIZE,
+ Default::default(),
+ );
harness.step();
let engine = started.get().expect("the studio started").clone();
runtime
@@ -571,7 +656,13 @@ fn mix_sends_and_mutes() {
let _entered = runtime.enter();
let fixture = Path::new(env!("CARGO_MANIFEST_DIR"));
std::fs::copy(fixture.join("shots/mix.toml"), "show.toml").unwrap();
- let (mut harness, started, _) = window(start(launch(&gpu), &runtime), &gpu, &runtime, SIZE);
+ let (mut harness, started, _) = window(
+ start(launch(&gpu), stream(None), &runtime),
+ &gpu,
+ &runtime,
+ SIZE,
+ Default::default(),
+ );
harness.step();
let App::Studio(studio) = &mut harness.state_mut().app else {
panic!("the studio starts on its first frame")
@@ -691,25 +782,34 @@ fn shoot(open: &Open, gpu: &engine::Gpu, runtime: &tokio::runtime::Runtime, show
if let &Open::Kit { sheet } = open {
return gallery(gpu, runtime, sheet);
}
+ if let Open::Library = open {
+ let (app, recent) = library_shelf();
+ let (mut harness, ..) = window(app, gpu, runtime, SIZE, recent);
+ // Enough for the pictures to load and the cards to settle.
+ harness.run_steps(4);
+ return harness.render().unwrap();
+ }
let app = match open {
- Open::Setup => App::Setup(Box::new(
- setup::Setup::new(launch(gpu), "show.toml".into(), runtime.handle().clone()).unwrap(),
- )),
- Open::Kit { .. } => unreachable!("drawn without an engine"),
- Open::Studio { show, view, .. } => {
+ Open::Setup => {
+ let setup = setup::Setup::new(launch(gpu), None, "show.toml".into(), runtime.handle().clone());
+ App::Setup(Box::new(setup.unwrap()), stream(None))
+ }
+ Open::Kit { .. } | Open::Library => unreachable!("drawn without an engine"),
+ Open::Studio { show, view, relay, .. } => {
let mut text = std::fs::read_to_string(shows.join(show)).unwrap();
if let View::Review { replays: true, .. } = view {
text.push_str("\n[sources.replay]\nkind = \"replay\"\n\n[sources.slowmo]\nkind = \"replay\"\n");
}
std::fs::write("show.toml", text).unwrap();
- start(launch(gpu), runtime)
+ let relay = relay.then(|| Relay::start(runtime));
+ start(launch(gpu), stream(relay.as_ref()), runtime)
}
};
let size = match open {
Open::Studio { size, .. } => *size,
_ => SIZE,
};
- let (mut harness, started, _) = window(app, gpu, runtime, size);
+ let (mut harness, started, _) = window(app, gpu, runtime, size, library::Recent::default());
let Open::Studio { view, .. } = open else {
runtime
@@ -721,6 +821,7 @@ fn shoot(open: &Open, gpu: &engine::Gpu, runtime: &tokio::runtime::Runtime, show
let App::Studio(studio) = &mut harness.state_mut().app else {
panic!("the studio starts on its first frame")
};
+ let controls = studio.controls();
match view {
View::Page(page) => studio.turn(*page),
View::Mix(view, opened) => studio.mix(*view, *opened),
@@ -729,7 +830,8 @@ fn shoot(open: &Open, gpu: &engine::Gpu, runtime: &tokio::runtime::Runtime, show
View::Menu => studio.build(Side::Layers, Inspect::Layer, Some(1)),
View::Setting(setting) => studio.open(*setting),
View::Health => studio.details(),
- View::Hover | View::History => {}
+ View::Going => studio.going(),
+ View::Hover | View::History | View::OnAir | View::EndStream | View::OnAirHealth => {}
View::Cued(page) => {
studio.studio_mode(true);
studio.cue("Interview");
@@ -747,11 +849,36 @@ fn shoot(open: &Open, gpu: &engine::Gpu, runtime: &tokio::runtime::Runtime, show
}
}
let engine = started.get().expect("the studio started").clone();
+ if let View::OnAir | View::EndStream | View::OnAirHealth = view {
+ controls.go_live().expect("going live starts");
+ runtime
+ .block_on(kio::clock::timeout(SETTLE, async {
+ while engine.on_air().live().is_none() {
+ kio::clock::sleep(Duration::from_millis(20)).await;
+ }
+ }))
+ .unwrap_or_else(|_| panic!("the studio goes live: {:?}", controls.refused.lock().unwrap()));
+ }
runtime
.block_on(kio::clock::timeout(SETTLE, settle(&mut harness, &engine)))
.expect("the sources and pictures settle");
match view {
View::Hover => hover(&mut harness),
+ View::Going => runtime
+ .block_on(kio::clock::timeout(SETTLE, checks(&mut harness)))
+ .expect("the go-live checks finish"),
+ View::EndStream => {
+ harness.get_by_label("End stream").hover();
+ harness.run_steps(2);
+ }
+ View::OnAirHealth => {
+ let pill = ["Healthy", "Starting", "Degraded", "Unhealthy"]
+ .into_iter()
+ .find_map(|name| harness.query_by_label(name))
+ .expect("the health dot");
+ pill.click();
+ harness.run_steps(2);
+ }
View::MixMenu => {
harness.get_by_label("Audio scene Headphones").click_secondary();
harness.run_steps(4);
@@ -805,7 +932,16 @@ fn window<'a>(
gpu: &engine::Gpu,
runtime: &'a tokio::runtime::Runtime,
size: egui::Vec2,
+ recent: library::Recent,
) -> (Harness<'a, Window>, Arc>>, Arc) {
+ let opener = show::Opener {
+ gpu: gpu.clone(),
+ runtime: runtime.handle().clone(),
+ flags: Default::default(),
+ client: Default::default(),
+ quic: Default::default(),
+ credentials: stream::Credentials::memory(),
+ };
let render = render_state(gpu);
let started = Arc::new(OnceLock::new());
let kept = started.clone();
@@ -822,8 +958,10 @@ fn window<'a>(
Window {
app,
render: render.clone(),
- runtime: runtime.handle().clone(),
+ opener,
started: kept,
+ recent,
+ aired: None,
keyboard: hotkeys::Keyboard::default(),
fx,
framed: true,
@@ -870,13 +1008,67 @@ fn hover(harness: &mut Harness<'_, Window>) {
.expect("a sound to mute")
.click();
harness.run_steps(2);
- harness.get_by_label("Not live").click();
+ harness.get_by_label("No relay").click();
harness.run_steps(2);
harness.get_by_label_contains("Undo: Mute").hover();
// egui shows a tooltip once the pointer rests half a second.
harness.run_steps(4);
}
+/// The library's shows: a few recent ones, beside the view's directory, most
+/// with the person's picture as their last program.
+fn library_shelf() -> (App, library::Recent) {
+ let pictures = std::path::absolute("pictures").unwrap();
+ let person = image::open("person.jpg").unwrap().to_rgba8();
+ let day = 24 * 3600;
+ let now = library::now();
+ let shows = [
+ ("Friday Night Ranked", Some((3 * day, 3 * 3600 + 120)), Some(false)),
+ ("Sunday Chill", Some((6 * day, 6000)), Some(false)),
+ ("Garage Sale Live", Some((14 * day, 55 * 60)), Some(true)),
+ ("Podcast with Sam", None, None),
+ ("Workbench Close-ups", Some((40 * day, 2 * 3600 + 900)), Some(false)),
+ ];
+ let mut recent = library::Recent::default();
+ // Oldest first, so the first is the latest opened.
+ for (index, (name, live, picture)) in shows.into_iter().enumerate().rev() {
+ let path = std::path::absolute(format!("{name}.toml")).unwrap();
+ std::fs::write(&path, "").unwrap();
+ recent.opened(&path, now - 21 * day + (5 - index as u64) * 3600);
+ if let Some((ago, lasted)) = live {
+ recent.aired(&path, library::Aired { at: now - ago, lasted });
+ }
+ if let Some(portrait) = picture {
+ let (width, height) = if portrait { (360, 640) } else { (640, 360) };
+ let picture = image::imageops::resize(&person, width, height, image::imageops::FilterType::Triangle);
+ let picture = library::Picture {
+ width,
+ height,
+ rgba: picture.into_raw(),
+ };
+ recent.shaped(&path, portrait);
+ library::save_picture(&pictures, &path, &picture).unwrap();
+ }
+ }
+ let app = App::Library(Box::new(library::Library::new(Some(pictures))), None);
+ (app, recent)
+}
+
+/// Draw until the go-live sheet's checks finish.
+async fn checks(harness: &mut Harness<'_, Window>) {
+ loop {
+ harness.step();
+ let App::Studio(studio) = &harness.state().app else {
+ panic!("the studio stays open")
+ };
+ if studio.checked() {
+ break;
+ }
+ kio::clock::sleep(Duration::from_millis(50)).await;
+ }
+ harness.run_steps(2);
+}
+
/// The last 30 seconds as of `now`: a keyframe every 2.5 s, a sun setting
/// across a gradient, drawn rather than decoded so the view is the same on
/// every machine.
@@ -972,7 +1164,7 @@ fn shelf() -> crate::shelf::Listing {
async fn checked(harness: &mut Harness<'_, Window>) {
loop {
harness.step();
- let App::Setup(setup) = &harness.state().app else {
+ let App::Setup(setup, _) = &harness.state().app else {
panic!("setup starts nothing on its own")
};
if setup.proposing() {
diff --git a/rs/moq-studio/src/show.rs b/rs/moq-studio/src/show.rs
new file mode 100644
index 00000000..67979dfe
--- /dev/null
+++ b/rs/moq-studio/src/show.rs
@@ -0,0 +1,136 @@
+//! Opening a show: its stream settings, its guests' room, and its engine, or
+//! guided setup when its scene file does not exist yet.
+
+use std::path::PathBuf;
+use std::sync::Arc;
+
+use anyhow::Context;
+
+use crate::autosave::Autosave;
+use crate::engine::{self, Engine, Launch};
+use crate::stream::{self, Stream};
+use crate::{guests, setup};
+
+/// What opening a show takes besides its path.
+#[derive(Clone)]
+pub struct Opener {
+ pub gpu: engine::Gpu,
+ pub runtime: tokio::runtime::Handle,
+ /// What the command line sets for every show this run opens.
+ pub flags: stream::Flags,
+ /// Every other client flag a relay is dialed with.
+ pub client: moq_tokio::connect::Config,
+ pub quic: moq_tokio::quic::Config,
+ pub credentials: stream::Credentials,
+}
+
+/// A show, opened.
+pub enum Opened {
+ /// Its engine, started.
+ Studio(Arc, Stream),
+ /// Guided setup, for a scene file that does not exist yet.
+ Setup(Box, Stream),
+}
+
+impl Opener {
+ /// Open the show at `path`: start its engine, with its room's session for
+ /// guests when it has one, or guided setup when there is no scene file
+ /// there yet. Nothing goes live.
+ pub async fn open(&self, path: PathBuf) -> anyhow::Result {
+ let stream = Stream::load(
+ &path,
+ self.flags.clone(),
+ self.client.clone(),
+ self.quic.clone(),
+ self.credentials.clone(),
+ )?;
+ let room = stream.guests().load(stream.relay())?.map(Arc::new);
+ // Set up now, so a room that cannot be fails the open, and run only
+ // once the show opened, so a show that fails to open, which the
+ // library lets the operator try again, leaves none renewing.
+ let renewal = match &room {
+ Some(room) => Some(guests::Renewal::connect(
+ room.clone(),
+ &stream.client(),
+ stream.quic().clone(),
+ )?),
+ None => None,
+ };
+ let launch = Launch {
+ gpu: self.gpu.clone(),
+ recordings: stream.recordings(&path),
+ room,
+ };
+ let exists = path
+ .try_exists()
+ .with_context(|| format!("reading {}", path.display()))?;
+ if exists {
+ let (autosave, loaded) = Autosave::open(path)?;
+ let engine = Engine::start(launch, autosave, loaded).await?;
+ if let Some(renewal) = renewal {
+ self.runtime.spawn(renewal.run());
+ }
+ return Ok(Opened::Studio(engine, stream));
+ }
+ // The relay setup measures the uplink to, with its token, read off
+ // this task since the credential store blocks.
+ let measured = stream.clone();
+ let relay = tokio::task::spawn_blocking(move || {
+ let only = stream::Switched {
+ relay: true,
+ failover: false,
+ };
+ measured.relays(only)
+ })
+ .await
+ .context("reading the relay's token panicked")?;
+ let uplink = match relay {
+ Ok(relays) => relays
+ .into_iter()
+ .next()
+ .map(|relay| (relay, stream.broadcast().to_string())),
+ Err(err) => {
+ tracing::warn!(error = format!("{err:#}"), "cannot measure the uplink");
+ None
+ }
+ };
+ let setup = setup::Setup::new(launch, uplink, path, self.runtime.clone())?;
+ if let Some(renewal) = renewal {
+ self.runtime.spawn(renewal.run());
+ }
+ Ok(Opened::Setup(Box::new(setup), stream))
+ }
+}
+
+/// Follow broadcast sources, guests, and viewers' reports through `stream`'s
+/// relays, read with their tokens. Each relay is set up on its own, so one
+/// that cannot be is logged and the other still followed; going live says
+/// why. Blocks on the credential store: call it off the window's thread,
+/// outside an async task, inside the tokio runtime, and through
+/// [`Controls`](crate::controls::Controls), which orders the follows.
+/// Says whether every relay was set up, so a follow that was not can be
+/// tried again.
+#[must_use]
+pub fn follow(engine: &Engine, stream: &Stream) -> bool {
+ let each = [(true, false), (false, true)].map(|(relay, failover)| stream::Switched { relay, failover });
+ let mut relays = Vec::new();
+ let mut failed = false;
+ for switched in each {
+ match stream.relays(switched) {
+ Ok(set) => relays.extend(set),
+ Err(err) => {
+ tracing::warn!(error = format!("{err:#}"), "cannot follow a relay");
+ failed = true;
+ }
+ }
+ }
+ // None could be set up, such as a new relay on a fixed --connect-bind
+ // port the sessions followed now still hold: those keep the broadcast
+ // sources and guests rather than none.
+ if relays.is_empty() && failed {
+ tracing::warn!("keeping the relays followed before");
+ return false;
+ }
+ engine.watch(&relays);
+ !failed
+}
diff --git a/rs/moq-studio/src/stream.rs b/rs/moq-studio/src/stream.rs
new file mode 100644
index 00000000..1c70d06f
--- /dev/null
+++ b/rs/moq-studio/src/stream.rs
@@ -0,0 +1,776 @@
+//! Where a show streams: its relay, a failover relay, the broadcast's name, and
+//! the options, kept per show in `.stream.toml` beside its scene file
+//! rather than in it, since the scene file is meant to be shared and names no
+//! relay. A relay's token goes in the operating system's credential store,
+//! never in a file. Each command-line flag wins over its setting for the run it
+//! is given to, and is never written back.
+
+use std::collections::HashMap;
+use std::path::{Path, PathBuf};
+use std::sync::{Arc, Mutex};
+use std::time::Duration;
+
+use anyhow::Context;
+use url::Url;
+
+use crate::engine::{Destination, Relay};
+use crate::guests;
+
+/// The broadcast path a show without one goes live at.
+pub const BROADCAST: &str = "studio";
+
+/// What `.stream.toml` holds.
+#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
+#[serde(default, deny_unknown_fields)]
+pub struct Saved {
+ /// The relay the program goes live on.
+ pub relay: Option,
+ /// A second relay to publish the same broadcast to, so a viewer linked to
+ /// both resumes from it when the first fails.
+ pub failover: Option,
+ /// The broadcast path on the relays, [`BROADCAST`] when unset.
+ pub broadcast: Option,
+ /// Whether the program video's canvas size encodes only while someone
+ /// watches it.
+ #[serde(skip_serializing_if = "std::ops::Not::not")]
+ pub on_demand: bool,
+ /// Where recordings are saved, the scene file's directory when unset.
+ pub recordings: Option,
+ /// The room guests join, a path beneath the relay's.
+ pub room: Option,
+ /// The key that signs guest tokens, a file `moq auth generate` writes.
+ pub room_key: Option,
+ /// The join page a guest opens.
+ pub join_page: Option,
+ /// How long a join link works.
+ #[serde(with = "humantime_serde")]
+ pub guest_expiry: Option,
+}
+
+/// What the command line sets for one run, over the show's saved settings.
+#[derive(Clone, Debug, Default)]
+pub struct Flags {
+ pub relay: Option,
+ /// Credentials for the relay, over the one in the credential store.
+ pub tokens: Vec,
+ pub failover: Option,
+ pub broadcast: Option,
+ /// Set only to turn it on; off leaves the show's setting.
+ pub on_demand: bool,
+ pub recordings: Option,
+ pub guests: guests::Config,
+}
+
+/// Which settings a flag sets for this run, so the window shows them as the
+/// flags' and does not offer to change them.
+#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
+pub struct Overridden {
+ pub relay: bool,
+ pub token: bool,
+ pub failover: bool,
+ pub broadcast: bool,
+ pub on_demand: bool,
+}
+
+/// Which relays a stream goes to, as the go-live sheet switches them.
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub struct Switched {
+ pub relay: bool,
+ pub failover: bool,
+}
+
+impl Default for Switched {
+ fn default() -> Self {
+ Self {
+ relay: true,
+ failover: true,
+ }
+ }
+}
+
+/// A show's stream settings, as saved and as this run's flags override them.
+#[derive(Clone)]
+pub struct Stream {
+ /// `.stream.toml`.
+ path: PathBuf,
+ saved: Saved,
+ flags: Flags,
+ /// Every other client flag a relay is dialed with.
+ client: moq_tokio::connect::Config,
+ quic: moq_tokio::quic::Config,
+ credentials: Credentials,
+ /// Each relay's client, shared by every clone, so watching, measuring,
+ /// and going live dial from one socket per relay, which a fixed
+ /// `--connect-bind` port can be bound for only once.
+ clients: Arc>>,
+}
+
+/// A relay's client, by the address and tokens it was set up with.
+struct Cached {
+ url: Url,
+ tokens: Vec,
+ client: moq_tokio::Client,
+}
+
+impl Stream {
+ /// The settings of the show at `scene`, as its stream file holds them, or
+ /// none yet, under `flags`. Each relay is dialed as `client` and `quic`
+ /// configure, with its token from `credentials`.
+ pub fn load(
+ scene: &Path,
+ flags: Flags,
+ client: moq_tokio::connect::Config,
+ quic: moq_tokio::quic::Config,
+ credentials: Credentials,
+ ) -> anyhow::Result {
+ let path = path(scene);
+ let saved = match std::fs::read_to_string(&path) {
+ Ok(text) => toml::from_str(&text).with_context(|| format!("reading {}", path.display()))?,
+ Err(err) if err.kind() == std::io::ErrorKind::NotFound => Saved::default(),
+ Err(err) => return Err(err).with_context(|| format!("reading {}", path.display())),
+ };
+ Ok(Self {
+ path,
+ saved,
+ flags,
+ client,
+ quic,
+ credentials,
+ clients: Default::default(),
+ })
+ }
+
+ /// The stream file.
+ pub fn path(&self) -> &Path {
+ &self.path
+ }
+
+ /// What the stream file holds.
+ pub fn saved(&self) -> &Saved {
+ &self.saved
+ }
+
+ /// Which settings this run's flags set.
+ pub fn overridden(&self) -> Overridden {
+ let flags = &self.flags;
+ Overridden {
+ relay: flags.relay.is_some(),
+ token: !flags.tokens.is_empty(),
+ failover: flags.failover.is_some(),
+ broadcast: flags.broadcast.is_some(),
+ on_demand: flags.on_demand,
+ }
+ }
+
+ /// The credential store the tokens are kept in.
+ pub fn credentials(&self) -> &Credentials {
+ &self.credentials
+ }
+
+ /// Write `saved` to the stream file, all at once, so a crash leaves the
+ /// old file or the new one. A token in either relay's address moves to
+ /// the credential store first, so the file never holds one.
+ ///
+ /// Blocks on the credential store: call it off the window's thread, and
+ /// outside an async task.
+ pub fn save(&mut self, mut saved: Saved) -> anyhow::Result<()> {
+ for url in [&mut saved.relay, &mut saved.failover].into_iter().flatten() {
+ if let Some(token) = take_token(url) {
+ self.credentials.set(url, Some(&token))?;
+ }
+ }
+ let text = toml::to_string(&saved).context("cannot write the stream settings")?;
+ let dir = self
+ .path
+ .parent()
+ .filter(|dir| !dir.as_os_str().is_empty())
+ .unwrap_or(Path::new("."));
+ let mut file =
+ tempfile::NamedTempFile::new_in(dir).with_context(|| format!("writing {}", self.path.display()))?;
+ std::io::Write::write_all(&mut file, text.as_bytes())
+ .with_context(|| format!("writing {}", self.path.display()))?;
+ file.persist(&self.path)
+ .with_context(|| format!("writing {}", self.path.display()))?;
+ self.saved = saved;
+ Ok(())
+ }
+
+ /// The relay the program goes live on.
+ pub fn relay(&self) -> Option<&Url> {
+ self.flags.relay.as_ref().or(self.saved.relay.as_ref())
+ }
+
+ /// The failover relay.
+ pub fn failover(&self) -> Option<&Url> {
+ self.flags.failover.as_ref().or(self.saved.failover.as_ref())
+ }
+
+ /// The broadcast path on the relays.
+ pub fn broadcast(&self) -> &str {
+ let set = self.flags.broadcast.as_deref().or(self.saved.broadcast.as_deref());
+ set.unwrap_or(BROADCAST)
+ }
+
+ /// Whether the canvas size encodes only while someone watches.
+ pub fn on_demand(&self) -> bool {
+ self.flags.on_demand || self.saved.on_demand
+ }
+
+ /// Where recordings are saved for the show at `scene`.
+ pub fn recordings(&self, scene: &Path) -> PathBuf {
+ let set = self.flags.recordings.as_ref().or(self.saved.recordings.as_ref());
+ match set {
+ Some(dir) => dir.clone(),
+ None => scene
+ .parent()
+ .filter(|dir| !dir.as_os_str().is_empty())
+ .unwrap_or(Path::new("."))
+ .to_path_buf(),
+ }
+ }
+
+ /// The guests' room, as the flags and the show set it.
+ pub fn guests(&self) -> guests::Config {
+ let (flags, saved) = (&self.flags.guests, &self.saved);
+ guests::Config {
+ room: flags.room.clone().or(saved.room.clone()),
+ room_key: flags.room_key.clone().or(saved.room_key.clone()),
+ join_page: flags.join_page.clone().or(saved.join_page.clone()),
+ guest_expiry: flags.guest_expiry.or(saved.guest_expiry.map(Into::into)),
+ }
+ }
+
+ /// The client flags with the relay's address, for the guests' room, which
+ /// takes none of its credentials.
+ pub fn client(&self) -> moq_tokio::connect::Config {
+ let mut client = self.client.clone();
+ client.url = self.relay().cloned();
+ client
+ }
+
+ /// The QUIC tuning every relay is dialed with.
+ pub fn quic(&self) -> &moq_tokio::quic::Config {
+ &self.quic
+ }
+
+ /// Each relay `switched` on, the preferred first, with its token, as a
+ /// client ready to dial it: the one set up before for the same address
+ /// and tokens, if any.
+ ///
+ /// Blocks on the credential store: call it off the window's thread, and
+ /// outside an async task. Call it inside the tokio runtime, where each
+ /// client binds its socket.
+ pub fn relays(&self, switched: Switched) -> anyhow::Result> {
+ // Each relay's client binds its own socket, so a fixed port would be
+ // bound twice, by the one watched and the one gone live to.
+ anyhow::ensure!(
+ self.client.bind.is_none_or(|bind| bind.port() == 0) || self.relay().is_none() || self.failover().is_none(),
+ "a failover relay cannot share a fixed --connect-bind port with the relay; leave the port 0, or remove the failover"
+ );
+ let dials = self.dials(switched)?;
+ let mut clients = self.clients.lock().unwrap();
+ // A relay no longer set, or set with other tokens, lets go of its
+ // socket before another binds one.
+ let current = [self.relay(), self.failover()];
+ clients.retain(|cached| current.contains(&Some(&cached.url)));
+ dials
+ .into_iter()
+ .map(|(url, tokens)| {
+ if let Some(cached) = clients
+ .iter()
+ .find(|cached| cached.url == url && cached.tokens == tokens)
+ {
+ return Ok(Relay {
+ client: cached.client.clone(),
+ url,
+ });
+ }
+ clients.retain(|cached| cached.url != url);
+ let mut config = self.client.clone();
+ config.url = Some(url.clone());
+ config.tokens = tokens.clone();
+ let client = config
+ .init(self.quic.clone())
+ .with_context(|| format!("cannot set up the relay {}", crate::engine::air::endpoint(&url)))?;
+ clients.push(Cached {
+ url: url.clone(),
+ tokens,
+ client: client.clone(),
+ });
+ Ok(Relay { client, url })
+ })
+ .collect()
+ }
+
+ /// Each relay `switched` on, the preferred first, with the tokens it is
+ /// dialed with: the flags' for the relay they name, none for an address
+ /// that carries its own `?jwt=`, or its own from the credential store, so
+ /// one relay's never reach another.
+ fn dials(&self, switched: Switched) -> anyhow::Result)>> {
+ let picked = [
+ (switched.relay, self.relay(), !self.flags.tokens.is_empty()),
+ (switched.failover, self.failover(), false),
+ ];
+ let picked: Vec<(&Url, bool)> = picked
+ .into_iter()
+ .filter_map(|(on, url, flagged)| Some((on.then_some(url?)?, flagged)))
+ .collect();
+ picked
+ .into_iter()
+ .map(|(url, flagged)| {
+ let carried = url.query_pairs().any(|(key, _)| key == "jwt");
+ let tokens = match (flagged, carried) {
+ (true, _) => self.flags.tokens.clone(),
+ (false, true) => Vec::new(),
+ (false, false) => match self.credentials.get(url)? {
+ Some(token) => vec![token.parse().map_err(|err| anyhow::anyhow!("{err}"))?],
+ None => Vec::new(),
+ },
+ };
+ Ok((url.clone(), tokens))
+ })
+ .collect()
+ }
+
+ /// Where going live with the relays `switched` on sends the program.
+ /// Refuses a failover beside a relay a viewer could not move between
+ /// without a restart.
+ ///
+ /// Blocks as [`relays`](Self::relays) does.
+ pub fn destination(&self, switched: Switched) -> anyhow::Result {
+ if switched.relay && switched.failover && self.relay().is_some() && self.failover().is_some() {
+ self.check_failover()?;
+ }
+ let relays = self.relays(switched)?;
+ anyhow::ensure!(
+ !relays.is_empty(),
+ "no relay is switched on; set one in Settings, Stream"
+ );
+ Ok(Destination {
+ relays,
+ broadcast: self.broadcast().to_string(),
+ on_demand: self.on_demand(),
+ })
+ }
+
+ /// Refuse relays a viewer could not move between without a restart: a
+ /// copy resumes mid-group only under the same instance, which needs both
+ /// links on one protocol version that carries it.
+ fn check_failover(&self) -> anyhow::Result<()> {
+ let [version] = self.client.version.as_slice() else {
+ anyhow::bail!(
+ "a failover relay needs exactly one --connect-version, so both relays negotiate the same protocol"
+ );
+ };
+ anyhow::ensure!(
+ matches!(version, moq_net::Version::Lite(lite) if lite.has_instance()),
+ "a failover relay needs a --connect-version with broadcast instances (moq-lite-07-wip today), not {version}, \
+ or a viewer moving between relays restarts the stream rather than resuming it"
+ );
+ Ok(())
+ }
+}
+
+/// `show.stream.toml` for `show.toml`.
+fn path(scene: &Path) -> PathBuf {
+ let mut name = scene.file_stem().unwrap_or(scene.as_os_str()).to_os_string();
+ name.push(".stream.toml");
+ scene.with_file_name(name)
+}
+
+/// Whether the address `url` carries a token that [saving](Stream::save)
+/// takes out of it, its query's `jwt` as decoded, however it is written.
+pub fn carries_token(url: &str) -> bool {
+ Url::parse(url.trim()).is_ok_and(|url| url.query_pairs().any(|(key, _)| key == "jwt"))
+}
+
+/// Take the token out of `url`'s `?jwt=`, if it has one, leaving the rest of
+/// its query.
+fn take_token(url: &mut Url) -> Option {
+ let mut token = None;
+ let kept: Vec<(String, String)> = url
+ .query_pairs()
+ .filter_map(|(key, value)| match key == "jwt" {
+ true => {
+ token = Some(value.into_owned());
+ None
+ }
+ false => Some((key.into_owned(), value.into_owned())),
+ })
+ .collect();
+ token.as_ref()?;
+ match kept.is_empty() {
+ true => url.set_query(None),
+ false => {
+ url.query_pairs_mut().clear().extend_pairs(kept);
+ }
+ }
+ token
+}
+
+/// What the credential store keeps tokens under.
+const SERVICE: &str = "live.kixel.studio";
+
+/// The operating system's credential store, where relay tokens are kept, one
+/// per relay address.
+#[derive(Clone)]
+pub struct Credentials(Result, String>);
+
+impl Credentials {
+ /// This system's store: the Secret Service on Linux, the login keychain on
+ /// macOS, the Credential Manager on Windows. One that cannot open says why
+ /// whenever it is used.
+ pub fn system() -> Self {
+ Self(open().map_err(|err| format!("{err:#}")))
+ }
+
+ /// A store in memory, for tests.
+ #[cfg(test)]
+ pub fn memory() -> Self {
+ let store: Arc = keyring_core::mock::Store::new().expect("a mock store");
+ Self(Ok(store))
+ }
+
+ /// The token kept for the relay at `url`, if any. Without a store, none:
+ /// a relay that needs no token still connects, one that does refuses the
+ /// connection, and saving a token says why it cannot.
+ ///
+ /// Blocks on the store, which may ask the user to unlock it: call it off
+ /// the window's thread, and outside an async task.
+ pub fn get(&self, url: &Url) -> anyhow::Result> {
+ if let Err(err) = &self.0 {
+ tracing::warn!(%err, "no credential store, so the relay is dialed without a token");
+ return Ok(None);
+ }
+ match self.entry(url)?.get_password() {
+ Ok(token) => Ok(Some(token)),
+ Err(keyring_core::Error::NoEntry) => Ok(None),
+ Err(err) => Err(err).context("cannot read the relay's token from the credential store"),
+ }
+ }
+
+ /// Keep `token` for the relay at `url`, or forget its token with `None`.
+ ///
+ /// Blocks as [`get`](Self::get) does.
+ pub fn set(&self, url: &Url, token: Option<&str>) -> anyhow::Result<()> {
+ let entry = self.entry(url)?;
+ match token {
+ Some(token) => entry
+ .set_password(token)
+ .context("cannot save the relay's token in the credential store"),
+ None => match entry.delete_credential() {
+ Ok(()) | Err(keyring_core::Error::NoEntry) => Ok(()),
+ Err(err) => Err(err).context("cannot forget the relay's token in the credential store"),
+ },
+ }
+ }
+
+ /// The entry for the relay at `url`: its address without a query or a
+ /// fragment, which a token could ride.
+ fn entry(&self, url: &Url) -> anyhow::Result {
+ let store = self
+ .0
+ .as_ref()
+ .map_err(|err| anyhow::anyhow!("no credential store: {err}"))?;
+ let mut user = url.clone();
+ user.set_query(None);
+ user.set_fragment(None);
+ let modifiers: Option<&HashMap<&str, &str>> = None;
+ store
+ .build(SERVICE, user.as_str(), modifiers)
+ .context("cannot use the credential store")
+ }
+}
+
+/// Open this system's credential store.
+fn open() -> anyhow::Result> {
+ #[cfg(target_os = "linux")]
+ let store: Arc = zbus_secret_service_keyring_store::Store::new()?;
+ #[cfg(target_os = "macos")]
+ let store: Arc = apple_native_keyring_store::keychain::Store::new()?;
+ #[cfg(windows)]
+ let store: Arc = windows_native_keyring_store::Store::new()?;
+ #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
+ anyhow::bail!("this system has no credential store the studio knows");
+ #[cfg(any(target_os = "linux", target_os = "macos", windows))]
+ Ok(store)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn stream(dir: &Path, flags: Flags) -> Stream {
+ Stream::load(
+ &dir.join("show.toml"),
+ flags,
+ Default::default(),
+ Default::default(),
+ Credentials::memory(),
+ )
+ .unwrap()
+ }
+
+ /// The settings round-trip through the file beside the scene file, which
+ /// a token in a relay's address never reaches: the credential store takes
+ /// it, and the address keeps the rest of its query.
+ #[test]
+ fn saves_beside_the_scene_file_without_a_token() {
+ let dir = tempfile::tempdir().unwrap();
+ let mut first = stream(dir.path(), Flags::default());
+ assert_eq!(first.path(), dir.path().join("show.stream.toml"));
+ assert_eq!(first.broadcast(), BROADCAST);
+ let saved = Saved {
+ relay: Some("https://relay.example/anon?jwt=secret&keep=1".parse().unwrap()),
+ broadcast: Some("friday".into()),
+ on_demand: true,
+ guest_expiry: Some(Duration::from_secs(3600)),
+ ..Default::default()
+ };
+ first.save(saved).unwrap();
+ let text = std::fs::read_to_string(first.path()).unwrap();
+ assert!(!text.contains("secret"), "the token reached the file: {text}");
+
+ let second = Stream::load(
+ &dir.path().join("show.toml"),
+ Flags::default(),
+ Default::default(),
+ Default::default(),
+ first.credentials().clone(),
+ )
+ .unwrap();
+ let relay = second.relay().unwrap().clone();
+ assert_eq!(relay.as_str(), "https://relay.example/anon?keep=1");
+ assert_eq!(second.broadcast(), "friday");
+ assert!(second.on_demand());
+ assert_eq!(
+ second.guests().guest_expiry.map(Duration::from),
+ Some(Duration::from_secs(3600))
+ );
+ assert_eq!(second.credentials().get(&relay).unwrap().as_deref(), Some("secret"));
+ }
+
+ /// A flag wins over the saved setting for its run, and saving another
+ /// setting never writes the flag's value back.
+ #[test]
+ fn flags_win_and_are_never_saved() {
+ let dir = tempfile::tempdir().unwrap();
+ let saved = Saved {
+ relay: Some("https://saved.example/".parse().unwrap()),
+ broadcast: Some("saved".into()),
+ ..Default::default()
+ };
+ stream(dir.path(), Flags::default()).save(saved.clone()).unwrap();
+ let flags = Flags {
+ relay: Some("https://flag.example/".parse().unwrap()),
+ on_demand: true,
+ ..Default::default()
+ };
+ let mut flagged = stream(dir.path(), flags);
+ assert_eq!(flagged.relay().unwrap().as_str(), "https://flag.example/");
+ assert_eq!(flagged.broadcast(), "saved");
+ assert!(flagged.on_demand());
+ let overridden = flagged.overridden();
+ assert!(overridden.relay && overridden.on_demand && !overridden.broadcast);
+
+ flagged
+ .save(Saved {
+ broadcast: Some("renamed".into()),
+ ..flagged.saved().clone()
+ })
+ .unwrap();
+ let reread = stream(dir.path(), Flags::default());
+ assert_eq!(reread.relay().unwrap().as_str(), "https://saved.example/");
+ assert_eq!(reread.broadcast(), "renamed");
+ assert!(!reread.on_demand());
+ }
+
+ /// An unknown key is refused rather than dropped on the next save.
+ #[test]
+ fn refuses_an_unknown_setting() {
+ let dir = tempfile::tempdir().unwrap();
+ std::fs::write(dir.path().join("show.stream.toml"), "relays = []\n").unwrap();
+ let loaded = Stream::load(
+ &dir.path().join("show.toml"),
+ Flags::default(),
+ Default::default(),
+ Default::default(),
+ Credentials::memory(),
+ );
+ assert!(loaded.is_err());
+ }
+
+ /// Each relay switched on is dialed with its own token: the flags' for the
+ /// relay they name, the store's for the failover, and a failover is
+ /// refused unless both relays resume the same broadcast.
+ #[test]
+ fn each_relay_gets_its_own_token() {
+ let dir = tempfile::tempdir().unwrap();
+ let relay: Url = "https://a.example/".parse().unwrap();
+ let failover: Url = "https://b.example/".parse().unwrap();
+ let mut client = moq_tokio::connect::Config::default();
+ let credentials = Credentials::memory();
+ credentials.set(&failover, Some("b-token")).unwrap();
+ let flags = Flags {
+ relay: Some(relay.clone()),
+ tokens: vec!["a-token".parse().unwrap()],
+ failover: Some(failover.clone()),
+ ..Default::default()
+ };
+ let load = |client: moq_tokio::connect::Config| {
+ Stream::load(
+ &dir.path().join("show.toml"),
+ flags.clone(),
+ client,
+ Default::default(),
+ credentials.clone(),
+ )
+ .unwrap()
+ };
+ let unversioned = load(client.clone());
+ assert!(
+ unversioned.destination(Switched::default()).is_err(),
+ "going live to both needs a single version"
+ );
+ assert_eq!(
+ unversioned.dials(Switched::default()).unwrap().len(),
+ 2,
+ "watching both does not"
+ );
+ client.version = vec!["moq-lite-07-wip".parse().unwrap()];
+ let stream = load(client);
+ let token = |token: &str| -> Vec { vec![token.parse().unwrap()] };
+ let dials = stream.dials(Switched::default()).unwrap();
+ assert_eq!(dials, [(relay, token("a-token")), (failover.clone(), token("b-token"))]);
+ let alone = Switched {
+ relay: false,
+ failover: true,
+ };
+ assert_eq!(stream.dials(alone).unwrap(), [(failover, token("b-token"))]);
+ let none = Switched {
+ relay: false,
+ failover: false,
+ };
+ assert!(stream.destination(none).is_err(), "nothing to send");
+ }
+
+ /// An address carries a token wherever saving would take one out of it,
+ /// its key percent-encoded included, and only there.
+ #[test]
+ fn a_token_is_found_as_saving_finds_it() {
+ for url in [
+ "https://relay.example/anon?jwt=abc",
+ " https://relay.example/anon?%6awt=abc ",
+ "https://relay.example/anon?x=1&jwt=abc",
+ ] {
+ assert!(carries_token(url), "{url}");
+ let mut parsed: Url = url.trim().parse().unwrap();
+ assert_eq!(take_token(&mut parsed).as_deref(), Some("abc"), "{url}");
+ }
+ for url in [
+ "https://relay.example/anon",
+ "https://relay.example/anon?notjwt=abc",
+ "https://relay.example/jwt=abc",
+ "not a url ?jwt=abc",
+ ] {
+ assert!(!carries_token(url), "{url}");
+ }
+ }
+
+ /// An address carrying its own `?jwt=` dials with it, without the
+ /// credential store, which may not open at all.
+ #[test]
+ fn a_token_in_the_address_needs_no_store() {
+ let dir = tempfile::tempdir().unwrap();
+ let relay: Url = "https://a.example/?jwt=x".parse().unwrap();
+ let flags = Flags {
+ relay: Some(relay.clone()),
+ ..Default::default()
+ };
+ let broken = Credentials(Err("no Secret Service".into()));
+ let stream = Stream::load(
+ &dir.path().join("show.toml"),
+ flags,
+ Default::default(),
+ Default::default(),
+ broken,
+ )
+ .unwrap();
+ assert_eq!(stream.dials(Switched::default()).unwrap(), [(relay, Vec::new())]);
+ }
+
+ /// Without a credential store, a relay that needs no token still dials,
+ /// tokenless, while saving a token says why it cannot.
+ #[test]
+ fn a_relay_without_a_token_needs_no_store() {
+ let dir = tempfile::tempdir().unwrap();
+ let relay: Url = "https://a.example/".parse().unwrap();
+ let flags = Flags {
+ relay: Some(relay.clone()),
+ ..Default::default()
+ };
+ let broken = Credentials(Err("no Secret Service".into()));
+ let stream = Stream::load(
+ &dir.path().join("show.toml"),
+ flags,
+ Default::default(),
+ Default::default(),
+ broken,
+ )
+ .unwrap();
+ assert_eq!(
+ stream.dials(Switched::default()).unwrap(),
+ [(relay.clone(), Vec::new())]
+ );
+ let refused = stream.credentials().set(&relay, Some("secret")).unwrap_err();
+ assert!(format!("{refused:#}").contains("no Secret Service"), "{refused:#}");
+ }
+
+ /// Watching, measuring, and going live share one client per relay, so a
+ /// fixed `--connect-bind` port is bound once, and a failover, which
+ /// would bind it again, is refused.
+ #[tokio::test]
+ async fn a_relay_is_set_up_once() {
+ let dir = tempfile::tempdir().unwrap();
+ let relay: Url = "https://a.example/".parse().unwrap();
+ let free = std::net::UdpSocket::bind("127.0.0.1:0").unwrap();
+ let mut client = moq_tokio::connect::Config::default();
+ client.bind = Some(free.local_addr().unwrap());
+ drop(free);
+ let mut stream = Stream::load(
+ &dir.path().join("show.toml"),
+ Flags::default(),
+ client,
+ Default::default(),
+ Credentials::memory(),
+ )
+ .unwrap();
+ stream
+ .save(Saved {
+ relay: Some(relay.clone()),
+ ..Default::default()
+ })
+ .unwrap();
+ let only = Switched {
+ relay: true,
+ failover: false,
+ };
+ let _watching = stream.relays(Switched::default()).unwrap();
+ stream.relays(only).expect("the bound port is reused");
+ stream.clone().destination(only).expect("so is going live's");
+ assert_eq!(stream.clients.lock().unwrap().len(), 1);
+
+ // A failover beside it would bind the port a second time, so the
+ // pair is refused, whichever is switched on.
+ let mut paired = stream.clone();
+ paired
+ .save(Saved {
+ relay: Some(relay),
+ failover: Some("https://b.example/".parse().unwrap()),
+ ..Default::default()
+ })
+ .unwrap();
+ let refused = paired.relays(only).err().expect("a fixed port with a failover");
+ assert!(format!("{refused:#}").contains("--connect-bind"), "{refused:#}");
+ }
+}
diff --git a/rs/moq-studio/src/ui.rs b/rs/moq-studio/src/ui.rs
index 4f4ba85c..a15fa95b 100644
--- a/rs/moq-studio/src/ui.rs
+++ b/rs/moq-studio/src/ui.rs
@@ -36,14 +36,17 @@ mod inspector;
mod layers;
mod layout;
mod levels;
+mod live;
mod mixer;
mod review;
mod settings;
mod shell;
+pub(crate) use layout::fit;
use levels::Levels;
#[cfg(test)]
pub(crate) use mixer::View as MixView;
+pub(crate) use shell::dashed;
/// The smallest the window gets, at which every part still has room.
pub const MIN_SIZE: egui::Vec2 = layout::MIN;
@@ -98,11 +101,14 @@ pub(crate) enum Setting {
Effects,
/// The scene graph: every source, scene, and audio scene, wired.
Advanced,
+ /// Where the show streams: its relays, their tokens, and the broadcast.
+ Stream,
}
impl Setting {
- const ALL: [Self; 5] = [
+ const ALL: [Self; 6] = [
Self::Show,
+ Self::Stream,
Self::Recording,
Self::Hotkeys,
Self::Effects,
@@ -112,6 +118,7 @@ impl Setting {
fn name(self) -> &'static str {
match self {
Self::Show => "Show",
+ Self::Stream => "Stream",
Self::Recording => "Recording",
Self::Hotkeys => "Hotkeys",
Self::Effects => "Effects",
@@ -159,10 +166,12 @@ pub struct Studio {
}
impl Studio {
- /// The studio on `engine`, `framed` if its title bar is the window's frame.
+ /// The studio on `engine`, streaming as `stream` sets, `framed` if its
+ /// title bar is the window's frame.
pub fn new(
render: &eframe::egui_wgpu::RenderState,
engine: Arc,
+ stream: crate::stream::Stream,
runtime: tokio::runtime::Handle,
framed: bool,
) -> Self {
@@ -179,7 +188,8 @@ impl Studio {
state.dropped,
)
};
- let controls = Arc::new(Controls::new(engine.clone(), runtime.clone()));
+ let controls = Arc::new(Controls::new(engine.clone(), stream, runtime.clone()));
+ drop(controls.follow_relays());
let review = review::Review::new(&engine, &runtime);
let mut panels = Panels {
engine,
@@ -199,7 +209,9 @@ impl Studio {
details: false,
side: Side::default(),
inspect: Inspect::default(),
- launched: kio::clock::now(),
+ going: None,
+ ending: None,
+ stream_form: None,
selection: None,
cropping: false,
gestures: Gestures::default(),
@@ -266,6 +278,21 @@ impl Studio {
self.panels.details = true;
}
+ /// Open the go-live sheet, as GO LIVE would.
+ pub(crate) fn going(&mut self) {
+ self.panels.open_going();
+ }
+
+ /// Whether the go-live sheet's checks have finished, or it is closed.
+ pub(crate) fn checked(&self) -> bool {
+ self.panels.going.as_ref().is_none_or(live::Going::done)
+ }
+
+ /// What the buttons and hotkeys do, such as going live.
+ pub(crate) fn controls(&self) -> Arc {
+ self.panels.controls.clone()
+ }
+
/// Show `listing` as REVIEW's cards, as if the folder held them.
pub(crate) fn seed_shelf(&mut self, listing: crate::shelf::Listing) {
self.panels.review.seed_shelf(listing);
@@ -380,6 +407,7 @@ impl Studio {
if let Some(hotkeys) = &mut self.panels.hotkeys {
hotkeys.hear();
}
+ self.panels.follow_stream_save();
}
/// Draw the window, with `fx` drawing its effects.
@@ -405,7 +433,7 @@ impl Studio {
.first()
.map_or(Verdict::Healthy, |problem| problem.verdict)
};
- fx.follow(panels.engine.output.is_some(), snapshot.takes, verdict);
+ fx.follow(panels.engine.on_air().live().is_some(), snapshot.takes, verdict);
panels.window(ui, fx, &snapshot);
// A stage not shown hears no input, so it lets go of the pages, as
// turning Interact off does.
@@ -489,9 +517,12 @@ struct Panels {
/// The BUILD page's left column and inspector tabs.
side: Side,
inspect: Inspect,
- /// When the studio started, which it is live from with `--connect`, until
- /// going live is its own step (quest/m0/ux/go-live.md).
- launched: kio::clock::Instant,
+ /// The go-live sheet, while open.
+ going: Option,
+ /// Since when END STREAM has been held down.
+ ending: Option,
+ /// The Stream tab's form, while it has edits not yet saved.
+ stream_form: Option,
/// The layer the inspector edits, the picture's handles hold, and a copy
/// takes.
selection: Option,
diff --git a/rs/moq-studio/src/ui/build.rs b/rs/moq-studio/src/ui/build.rs
index 4039f17b..d6689db3 100644
--- a/rs/moq-studio/src/ui/build.rs
+++ b/rs/moq-studio/src/ui/build.rs
@@ -99,12 +99,8 @@ impl Panels {
});
let (canvas, id, frame) = match off_air {
Some((canvas, tag)) => (canvas, self.preview.id, TallyFrame::new(Tally::Cue).tag(tag)),
- // It is the live picture, so the accent frame stays while editing it.
- None => (
- self.program_drawn,
- self.program.id,
- TallyFrame::new(Tally::Air).tag("On air"),
- ),
+ // It is the live picture, so its frame says whether it is on air.
+ None => (self.program_drawn, self.program.id, self.program_frame()),
};
let builder = egui::UiBuilder::new().max_rect(at.expand(10.0));
if self.interacting {
diff --git a/rs/moq-studio/src/ui/live.rs b/rs/moq-studio/src/ui/live.rs
new file mode 100644
index 00000000..43873f31
--- /dev/null
+++ b/rs/moq-studio/src/ui/live.rs
@@ -0,0 +1,1291 @@
+//! Going live from the window: the live control on the title bar, the go-live
+//! sheet with where the stream goes and the checks before it does, the health
+//! dot and what it opens, and the Stream settings.
+
+use std::collections::BTreeMap;
+
+use egui::{Align, Rect, Sense, Stroke, Vec2, pos2, vec2};
+use moq_net::bandwidth::Rate;
+
+use super::levels::reading;
+use super::*;
+use crate::engine::OnAir;
+use crate::setup::{self, Encoding, Task};
+use crate::stream::{Saved, Stream};
+use crate::uplink;
+
+/// How long END STREAM is held down to end the stream.
+const HOLD: Duration = Duration::from_millis(1200);
+
+/// How loud a microphone gets, on the meter's scale, to count as moving.
+const MOVING: f32 = 0.2;
+
+/// How long the microphone check listens before saying it heard nothing.
+const LISTEN: Duration = Duration::from_secs(4);
+
+/// The go-live sheet's checks, run while it is open.
+pub(super) struct Going {
+ /// The canvas the encoder was timed at; another starts the checks over.
+ canvas: Canvas,
+ encoder: Task>,
+ /// Started once the encoder is known, since what it sends is what the
+ /// connection is measured toward.
+ uplink: Option,
+ /// The loudest each microphone was since the sheet opened, on the meter's
+ /// scale.
+ heard: BTreeMap,
+ opened: kio::clock::Instant,
+ /// Whether the confirm was pressed, so the sheet waits on going live.
+ confirmed: bool,
+}
+
+/// How a check came out.
+#[derive(Clone, Copy, PartialEq, Eq)]
+enum Outcome {
+ /// Still checking.
+ Waiting,
+ Passed,
+ /// Failed: going live still works, but viewers would notice.
+ Failed,
+ /// Nothing to check, such as a show without a camera.
+ Skipped,
+}
+
+/// What a failed check offers to do about it.
+#[derive(Clone, Copy)]
+enum Fix {
+ /// Send the program at this canvas instead.
+ Send(Canvas),
+ /// Measure the connection again.
+ Again,
+ /// Open BUILD, where the sources are.
+ Build,
+}
+
+/// A check's line in the sheet.
+struct Check {
+ mark: Outcome,
+ icon: Icon,
+ name: &'static str,
+ /// What it found, or what is wrong and why, as one sentence.
+ said: String,
+ /// The microphone's level, for its meter.
+ level: Option<(f32, f32)>,
+ fixes: Vec<(String, Fix)>,
+}
+
+impl Going {
+ /// Start the checks for the program at `canvas`, the encoder's noted as
+ /// `probing` until it ends, even once the sheet closes, and not run
+ /// without it, while a go-live opens the program's encoders.
+ fn new(
+ gpu: crate::engine::Gpu,
+ runtime: &tokio::runtime::Handle,
+ canvas: Canvas,
+ probing: Option,
+ ) -> Self {
+ let encoder = match probing {
+ Some(probing) => Task::spawn(runtime, move || async move {
+ let _probing = probing;
+ setup::probe(gpu, canvas).await
+ })
+ .unwrap_or_else(|err| Task::Done(Err(format!("cannot start the check: {err}")))),
+ None => Task::Done(Err("not checked while a stream goes live".into())),
+ };
+ Self {
+ canvas,
+ encoder,
+ uplink: None,
+ heard: BTreeMap::new(),
+ opened: kio::clock::now(),
+ confirmed: false,
+ }
+ }
+}
+
+impl Going {
+ /// Whether every check that runs in the background has finished.
+ #[cfg(test)]
+ pub(super) fn done(&self) -> bool {
+ matches!(self.encoder, Task::Done(_))
+ && matches!(
+ self.uplink,
+ Some(Measuring {
+ task: Task::Done(_),
+ ..
+ })
+ )
+ }
+}
+
+/// The connection being measured, which stops, withdrawing its probe, when
+/// this is dropped, and on its own once the stream starts going live, from
+/// the sheet or a hotkey, window shown or not, so it never competes with the
+/// stream for the uplink.
+struct Measuring {
+ task: Task>,
+ /// Dropping it ends the measurement.
+ _stop: Option>,
+}
+
+/// `canvas` as a broadcaster names it, such as `1080p at 60`.
+fn named(canvas: Canvas) -> String {
+ format!("{}p at {}", canvas.width.min(canvas.height), canvas.framerate)
+}
+
+/// A smaller canvas to send, if there is one worth offering: 720p from
+/// anything larger, then 30 frames a second rather than more.
+fn smaller(canvas: Canvas) -> Option {
+ let short = canvas.width.min(canvas.height);
+ if short > 720 {
+ let scale = |side: u32| (u64::from(side) * 720 / u64::from(short)) as u32 & !1;
+ return Some(Canvas {
+ width: scale(canvas.width),
+ height: scale(canvas.height),
+ framerate: canvas.framerate,
+ });
+ }
+ (canvas.framerate > 30).then_some(Canvas {
+ framerate: 30,
+ ..canvas
+ })
+}
+
+impl Panels {
+ /// The live frame's tally and tag: ON AIR in the accent while live, OFF
+ /// AIR in amber on a neutral frame otherwise.
+ pub(super) fn program_frame(&self) -> TallyFrame<'static> {
+ match self.engine.on_air() {
+ OnAir::Live(_) => TallyFrame::new(Tally::Air).tag("On air"),
+ OnAir::Ending => TallyFrame::new(Tally::Air).tag("Ending"),
+ OnAir::Starting => TallyFrame::new(Tally::Off).tag_as("Going live", Tally::Cue),
+ OnAir::Off => TallyFrame::new(Tally::Off).tag_as("Off air", Tally::Cue),
+ }
+ }
+
+ /// The live control: GO LIVE off air, which opens the go-live sheet; the
+ /// clock on air, which reads END STREAM under the pointer and ends the
+ /// stream once held down.
+ pub(super) fn live_control(&mut self, ui: &mut Ui) {
+ let on_air = self.engine.on_air();
+ let font = kit::display_bold(17.0);
+ let clock_font = kit::mono(14.0);
+ let painter = ui.painter().clone();
+ let end = kit::caps_galley(&painter, "End stream", font.clone(), Color32::WHITE, None);
+ let live_word = kit::caps_galley(&painter, "Live", font.clone(), Color32::WHITE, None);
+ let clock_width = painter
+ .layout_no_wrap("00:00:00".into(), clock_font.clone(), Color32::WHITE)
+ .size()
+ .x;
+ match on_air {
+ OnAir::Off => {
+ let key = self.key(&Action::GoLive, ui.ctx());
+ // Neither the accent nor red: those say the stream is on air.
+ let word = kit::caps_galley(&painter, "Go live", font, kit::BG, None);
+ let size = vec2(word.size().x + 36.0, 30.0);
+ let (rect, response) = ui.allocate_exact_size(size, Sense::click());
+ response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, "Go live"));
+ let response = kit::tip(response, "Go live", key.as_deref());
+ let fill = match response.hovered() {
+ true => Color32::WHITE,
+ false => Color32::from_gray(0xe8),
+ };
+ painter.rect_filled(rect, 6, fill);
+ painter.galley(rect.center() - word.size() / 2.0, word, kit::BG);
+ if response.clicked() {
+ self.open_going();
+ }
+ }
+ OnAir::Starting | OnAir::Ending => {
+ let text = match on_air {
+ OnAir::Starting => "Going live",
+ _ => "Ending",
+ };
+ let word = kit::caps_galley(&painter, text, font, kit::DIM, None);
+ let size = vec2(word.size().x + 24.0 + 20.0, 30.0);
+ let (rect, response) = ui.allocate_exact_size(size, Sense::hover());
+ let tip = match on_air {
+ OnAir::Starting => "Dialing the relays and opening the encoders",
+ _ => "Handing the relays the stream's tail before the sessions close",
+ };
+ kit::tip(response, tip, None);
+ painter.rect(rect, 6, kit::P2, Stroke::new(1.0, kit::LINE), egui::StrokeKind::Inside);
+ let spinner = Rect::from_center_size(pos2(rect.left() + 18.0, rect.center().y), Vec2::splat(12.0));
+ egui::Spinner::new().size(12.0).color(kit::DIM).paint_at(ui, spinner);
+ painter.galley(
+ pos2(rect.left() + 30.0, rect.center().y - word.size().y / 2.0),
+ word,
+ kit::DIM,
+ );
+ }
+ OnAir::Live(live) => {
+ let key = self.key(&Action::EndStream, ui.ctx());
+ let width = (12.0 + live_word.size().x + 10.0 + clock_width + 12.0).max(end.size().x + 24.0);
+ let (rect, response) = ui.allocate_exact_size(vec2(width, 30.0), Sense::click_and_drag());
+ response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, "End stream"));
+ let relays = live.relays.join(" and ");
+ let note = format!("Live at {} on {relays}. Hold to end the stream.", live.path);
+ let held = response.is_pointer_button_down_on();
+ let response = kit::tip(response, ¬e, key.as_deref());
+ let ending = match (held, self.ending) {
+ (true, Some(since)) => Some(since),
+ (true, None) => Some(kio::clock::now()),
+ (false, _) => None,
+ };
+ self.ending = ending;
+ let progress = ending.map_or(0.0, |since| since.elapsed().as_secs_f32() / HOLD.as_secs_f32());
+ if progress >= 1.0 {
+ self.ending = None;
+ if let Err(err) = self.controls.end_stream() {
+ self.report(Err(err));
+ }
+ } else if ending.is_some() {
+ ui.ctx().request_repaint();
+ }
+ let glow = egui::Shadow {
+ offset: [0, 0],
+ blur: 22,
+ spread: 0,
+ color: kit::ACCENT.gamma_multiply(0.4),
+ };
+ painter.add(glow.as_shape(rect, egui::CornerRadius::same(6)));
+ if response.hovered() || held {
+ painter.rect_filled(rect, 6, kit::ERR.gamma_multiply(0.35));
+ let filled = rect.with_max_x(rect.left() + rect.width() * progress.min(1.0));
+ painter.rect_filled(filled, 6, kit::ERR);
+ painter.rect_stroke(rect, 6, Stroke::new(1.0, kit::ERR), egui::StrokeKind::Inside);
+ painter.galley(rect.center() - end.size() / 2.0, end, Color32::WHITE);
+ } else {
+ painter.rect_filled(rect, 6, kit::ACCENT);
+ let elapsed = live.since.elapsed();
+ let time = painter.layout_no_wrap(super::shell::clock(elapsed), clock_font, Color32::WHITE);
+ let x = rect.left() + 12.0;
+ let after = x + live_word.size().x + 10.0;
+ painter.galley(
+ pos2(x, rect.center().y - live_word.size().y / 2.0),
+ live_word,
+ Color32::WHITE,
+ );
+ let x = after;
+ painter.galley(pos2(x, rect.center().y - time.size().y / 2.0), time, Color32::WHITE);
+ }
+ }
+ }
+ }
+
+ /// Open the go-live sheet, starting its checks.
+ pub(super) fn open_going(&mut self) {
+ let canvas = self.engine.state.lock().unwrap().collection().canvas;
+ *self.controls.refused.lock().unwrap() = None;
+ let probing = self.controls.probing();
+ self.going = Some(Going::new(self.engine.gpu().clone(), &self.runtime, canvas, probing));
+ }
+
+ /// The go-live sheet, while open: where the stream goes, each with a
+ /// switch and its state, the checks as they come in, and one confirm.
+ pub(super) fn going_sheet(&mut self, ctx: &egui::Context) {
+ if self.going.is_none() {
+ return;
+ }
+ let on_air = self.engine.on_air();
+ // Live: the sheet has done its job.
+ if on_air.live().is_some() {
+ self.going = None;
+ return;
+ }
+ ctx.request_repaint_after(Duration::from_millis(100));
+ let (show, program, canvas) = {
+ let state = self.engine.state.lock().unwrap();
+ let path = self.engine.autosave.path();
+ let show = path
+ .file_stem()
+ .unwrap_or(path.as_os_str())
+ .to_string_lossy()
+ .into_owned();
+ let program = state.compositor.program_scene().map(str::to_string);
+ (show, program, state.collection().canvas)
+ };
+ // Not while going live, which keeps the sheet waiting on it, and
+ // whose encoders a new check would compete with: once it ends.
+ if self.going.as_ref().is_some_and(|going| going.canvas != canvas) && !self.controls.going() {
+ self.open_going();
+ }
+ self.advance_going();
+ let checks = self.checks();
+ let mut close = false;
+ let mut fixes = Vec::new();
+ let mut confirm = false;
+ let mut settings = false;
+ let refused = self.controls.refused.lock().unwrap().clone();
+ let confirmed = self.going.as_ref().is_some_and(|going| going.confirmed);
+ // The encoder check holds a hardware encode session until it finishes,
+ // which would count against the program's.
+ let probing = self
+ .going
+ .as_ref()
+ .is_some_and(|going| matches!(going.encoder, Task::Running(_)));
+ let id = egui::Id::new("go live");
+ let asked = kit::Sheet::new(id, "Go live").show(ctx, |ui| {
+ let opens = match &program {
+ Some(scene) => format!("{show} opens on {scene}"),
+ None => format!("{show} opens on an empty picture"),
+ };
+ ui.label(egui::RichText::new(opens).color(kit::DIM));
+ ui.add_space(10.0);
+ heading(ui, "Where it goes");
+ let count = self.destinations(ui, &mut settings);
+ ui.add_space(10.0);
+ heading(ui, "Checks");
+ for (index, check) in checks.iter().enumerate() {
+ kit::popover_row(ui, index + 1 == checks.len(), |ui| {
+ if let Some(fix) = check_row(ui, check) {
+ fixes.push(fix);
+ }
+ });
+ }
+ ui.add_space(12.0);
+ if let Some(refused) = &refused {
+ ui.horizontal(|ui| {
+ ui.add(Icon::Warning.image(16.0, kit::ERR));
+ ui.add(egui::Label::new(egui::RichText::new(refused).color(kit::TEXT)).wrap());
+ });
+ ui.add_space(8.0);
+ }
+ let going = self.controls.going();
+ let waiting = confirmed && (going || matches!(on_air, OnAir::Starting));
+ let text = match (waiting, count) {
+ (true, _) => "Going live".to_string(),
+ (false, 0) => "Switch a destination on".to_string(),
+ (false, _) if probing => "Checking the encoder".to_string(),
+ (false, count) => format!("Go live to {count}"),
+ };
+ let enabled = count > 0 && on_air == OnAir::Off && !going && !probing;
+ confirm = confirm_button(ui, &text, enabled, waiting).clicked();
+ });
+ close |= asked;
+ if settings {
+ self.setting = Some(Setting::Stream);
+ close = true;
+ }
+ for fix in fixes {
+ match fix {
+ Fix::Send(canvas) => {
+ let label = format!("Send {} instead", named(canvas));
+ let mut state = self.engine.state.lock().unwrap();
+ let result = super::edit(&mut state, label, None, |draft| draft.set_canvas(canvas));
+ drop(state);
+ self.check(result);
+ }
+ Fix::Again => {
+ if let Some(going) = &mut self.going {
+ going.uplink = None;
+ }
+ }
+ Fix::Build => {
+ self.page = Page::Build;
+ close = true;
+ }
+ }
+ }
+ if confirm {
+ match self.controls.go_live() {
+ Ok(_) => {
+ if let Some(going) = &mut self.going {
+ going.confirmed = true;
+ }
+ }
+ Err(err) => *self.controls.refused.lock().unwrap() = Some(format!("{err:#}")),
+ }
+ }
+ if close {
+ self.going = None;
+ }
+ }
+
+ /// Collect the checks that finished, and start measuring the connection
+ /// once the encoder says what it sends.
+ fn advance_going(&mut self) {
+ let Some(going) = &mut self.going else { return };
+ going.encoder.poll();
+ if going.uplink.is_none()
+ && let Task::Done(encoder) = &going.encoder
+ {
+ going.uplink = Some(match encoder {
+ Ok(encoding) => {
+ // Up to past what it sends, so a connection that carries
+ // it says so rather than stopping at its rate.
+ let ceiling = encoding.total().scaled(1.0 / setup::headroom());
+ measure(&self.controls, &self.runtime, ceiling)
+ }
+ Err(_) => Measuring {
+ task: Task::Done(Err("nothing encodes, so nothing would be sent".into())),
+ _stop: None,
+ },
+ });
+ }
+ if let Some(uplink) = &mut going.uplink {
+ uplink.task.poll();
+ }
+ }
+
+ /// Each destination, with its switch and its state; how many are on.
+ fn destinations(&mut self, ui: &mut Ui, settings: &mut bool) -> usize {
+ let (relay, failover) = {
+ let stream = self.controls.stream.lock().unwrap();
+ (stream.relay().cloned(), stream.failover().cloned())
+ };
+ if relay.is_none() && failover.is_none() {
+ kit::popover_row(ui, true, |ui| {
+ ui.label("No relay yet. Add the one the stream goes to.");
+ ui.with_layout(egui::Layout::right_to_left(Align::Center), |ui| {
+ *settings = ui.button("Set one up").clicked();
+ });
+ });
+ return 0;
+ }
+ let watched = self.engine.watched();
+ let mut switched = *self.controls.switched.lock().unwrap();
+ let rows = [
+ ("Relay", relay, &mut switched.relay),
+ ("Failover relay", failover, &mut switched.failover),
+ ];
+ let rows: Vec<_> = rows
+ .into_iter()
+ .filter_map(|(name, url, on)| Some((name, url?, on)))
+ .collect();
+ let count = rows.len();
+ let mut on_count = 0;
+ for (index, (name, url, on)) in rows.into_iter().enumerate() {
+ let endpoint = crate::engine::air::endpoint(&url);
+ let state = watched.iter().find(|(relay, _)| relay.endpoint == endpoint);
+ let (dot, said) = match (*on, state) {
+ (false, _) => (None, "Off".to_string()),
+ (true, None) => (Some(kit::DIM), "Not dialed yet".to_string()),
+ (true, Some((relay, rtt))) => match &relay.state {
+ health::State::Connected => (
+ Some(kit::OK),
+ match rtt {
+ Some(rtt) => format!("Connected, {} ms away", rtt.as_millis()),
+ None => "Connected".into(),
+ },
+ ),
+ health::State::Connecting => (Some(kit::DIM), "Connecting".into()),
+ health::State::Disconnected(why) => (Some(kit::ERR), format!("Cannot reach it: {why}")),
+ },
+ };
+ if *on {
+ on_count += 1;
+ }
+ kit::popover_row(ui, index + 1 == count, |ui| {
+ ui.add(kit::Toggle::new(on));
+ let (at, _) = ui.allocate_exact_size(vec2(8.0, 8.0), Sense::hover());
+ match dot {
+ Some(dot) => ui.painter().circle_filled(at.center(), 4.0, dot),
+ None => ui
+ .painter()
+ .circle_stroke(at.center(), 3.5, Stroke::new(1.0, kit::FAINT)),
+ };
+ ui.label(egui::RichText::new(name).font(kit::ui_strong(13.0)).color(kit::TEXT))
+ // The endpoint, never the address, whose query can carry a token
+ // on a shared screen.
+ .on_hover_text(endpoint.as_str());
+ ui.with_layout(egui::Layout::right_to_left(Align::Center), |ui| {
+ ui.add(egui::Label::new(egui::RichText::new(said).color(kit::DIM)).truncate());
+ });
+ });
+ }
+ let changed = switched != *self.controls.switched.lock().unwrap();
+ *self.controls.switched.lock().unwrap() = switched;
+ // The connection is measured to the first relay switched on.
+ if changed && let Some(going) = &mut self.going {
+ going.uplink = None;
+ }
+ on_count
+ }
+
+ /// The checks, as they stand.
+ fn checks(&mut self) -> Vec {
+ let engine = self.engine.clone();
+ let state = engine.state.lock().unwrap();
+ let collection = state.collection();
+ let now = kio::clock::now();
+ let Some(going) = &mut self.going else {
+ return Vec::new();
+ };
+ let mut checks = Vec::new();
+
+ // Each camera in the show has a picture.
+ let cameras: Vec<_> = collection
+ .sources
+ .iter()
+ .filter(|(_, source)| matches!(source, Source::Camera { .. }))
+ .map(|(name, _)| {
+ let failed = state.compositor.error(name).map(|err| err.to_string());
+ let status = state.sources.get(name).map(|feed| feed.status());
+ (name.clone(), failed, status)
+ })
+ .collect();
+ let blank = cameras
+ .iter()
+ .find(|(_, failed, status)| failed.is_some() || !matches!(status, Some(Status::Live)));
+ checks.push(match (cameras.first(), blank) {
+ (None, _) => Check {
+ mark: Outcome::Skipped,
+ icon: Icon::Camera,
+ name: "Camera",
+ said: "This show has no camera".into(),
+ level: None,
+ fixes: Vec::new(),
+ },
+ (Some((name, ..)), None) => Check {
+ mark: Outcome::Passed,
+ icon: Icon::Camera,
+ name: "Camera",
+ said: format!("{name} has a picture"),
+ level: None,
+ fixes: Vec::new(),
+ },
+ (Some(_), Some((name, failed, status))) => {
+ let why = match (failed, status) {
+ (Some(failed), _) => format!("{name} has no picture: {failed}."),
+ (None, Some(Status::Failed(why))) => format!("{name} has no picture: {why}."),
+ (None, Some(Status::Starting)) | (None, None) => format!("{name} is still starting."),
+ (None, _) => format!("{name} has no picture. Check it is plugged in and not in use."),
+ };
+ Check {
+ mark: Outcome::Failed,
+ icon: Icon::Camera,
+ name: "Camera",
+ said: why,
+ level: None,
+ fixes: vec![("Open BUILD".into(), Fix::Build)],
+ }
+ }
+ });
+
+ // Each microphone's level moves.
+ let microphones: Vec = collection
+ .sources
+ .iter()
+ .filter(|(_, source)| matches!(source, Source::Microphone { .. }))
+ .map(|(name, _)| name.clone())
+ .collect();
+ for name in µphones {
+ let (level, _) = reading(self.levels.sound(name));
+ let heard = going.heard.entry(name.clone()).or_default();
+ *heard = heard.max(level);
+ }
+ // The first that has not moved yet, so one silent beside another that
+ // moves is still named.
+ let silent = microphones
+ .iter()
+ .find(|name| going.heard.get(*name).is_none_or(|heard| *heard < MOVING));
+ let listened = now.saturating_duration_since(going.opened) >= LISTEN;
+ let shown = silent.or(microphones.first());
+ let level = shown.map(|name| reading(self.levels.sound(name)));
+ checks.push(match (shown, silent, listened) {
+ (None, ..) => Check {
+ mark: Outcome::Skipped,
+ icon: Icon::Mic,
+ name: "Microphone",
+ said: "This show has no microphone".into(),
+ level: None,
+ fixes: Vec::new(),
+ },
+ (Some(name), None, _) => Check {
+ mark: Outcome::Passed,
+ icon: Icon::Mic,
+ name: "Microphone",
+ said: match microphones.len() {
+ 1 => format!("{name} is moving"),
+ count => format!("All {count} microphones are moving"),
+ },
+ level,
+ fixes: Vec::new(),
+ },
+ (Some(_), Some(name), false) => Check {
+ mark: Outcome::Waiting,
+ icon: Icon::Mic,
+ name: "Microphone",
+ said: format!("Listening to {name}; say something"),
+ level,
+ fixes: Vec::new(),
+ },
+ (Some(_), Some(name), true) => Check {
+ mark: Outcome::Failed,
+ icon: Icon::Mic,
+ name: "Microphone",
+ said: format!("{name} has been silent. Speak, or check it is not muted in the sound panel."),
+ level,
+ fixes: Vec::new(),
+ },
+ });
+
+ // The encoder keeps up with the canvas.
+ let canvas = going.canvas;
+ let send = smaller(canvas).map(|smaller| (format!("Send {} instead", named(smaller)), Fix::Send(smaller)));
+ let encoding = match &going.encoder {
+ Task::Done(Ok(encoding)) => Some(encoding.clone()),
+ _ => None,
+ };
+ checks.push(match &going.encoder {
+ Task::Running(_) => Check {
+ mark: Outcome::Waiting,
+ icon: Icon::Encoder,
+ name: "Encoder",
+ said: format!("Timing {}", named(canvas)),
+ level: None,
+ fixes: Vec::new(),
+ },
+ Task::Done(Ok(encoding)) if encoding.keeps_up(canvas) => Check {
+ mark: Outcome::Passed,
+ icon: Icon::Encoder,
+ name: "Encoder",
+ said: format!("Keeps up with {}", named(canvas)),
+ level: None,
+ fixes: Vec::new(),
+ },
+ Task::Done(Ok(encoding)) => Check {
+ mark: Outcome::Failed,
+ icon: Icon::Encoder,
+ name: "Encoder",
+ said: format!(
+ "The encoder takes {} ms a frame, too slow for {}. Viewers would see it stutter.",
+ encoding.frame.as_millis(),
+ named(canvas)
+ ),
+ level: None,
+ fixes: send.clone().into_iter().collect(),
+ },
+ Task::Done(Err(err)) => Check {
+ mark: Outcome::Failed,
+ icon: Icon::Encoder,
+ name: "Encoder",
+ said: format!("Nothing encodes {} here: {err}.", named(canvas)),
+ level: None,
+ fixes: send.clone().into_iter().collect(),
+ },
+ });
+
+ // The connection carries the program. Published scenes and sources
+ // share what is left, following the connection as it changes.
+ let again = ("Check again".to_string(), Fix::Again);
+ checks.push(match (going.uplink.as_ref().map(|uplink| &uplink.task), &encoding) {
+ (None, _) | (Some(Task::Running(_)), _) => Check {
+ mark: Outcome::Waiting,
+ icon: Icon::Uplink,
+ name: "Connection",
+ said: "Measuring the connection to the relay".into(),
+ level: None,
+ fixes: Vec::new(),
+ },
+ (Some(Task::Done(Err(err))), _) => Check {
+ mark: Outcome::Failed,
+ icon: Icon::Uplink,
+ name: "Connection",
+ said: format!("The connection was not measured: {err}."),
+ level: None,
+ fixes: vec![again],
+ },
+ (Some(Task::Done(Ok(_))), None) => Check {
+ mark: Outcome::Skipped,
+ icon: Icon::Uplink,
+ name: "Connection",
+ said: "Measured, but nothing encodes to compare it with".into(),
+ level: None,
+ fixes: Vec::new(),
+ },
+ (Some(Task::Done(Ok(carries))), Some(encoding)) => {
+ let needs = encoding.total();
+ match carries.scaled(setup::headroom()) >= needs {
+ true => Check {
+ mark: Outcome::Passed,
+ icon: Icon::Uplink,
+ name: "Connection",
+ said: format!(
+ "Carries {}, and the program needs {}",
+ health::rate(*carries),
+ health::rate(needs)
+ ),
+ level: None,
+ fixes: Vec::new(),
+ },
+ false => Check {
+ mark: Outcome::Failed,
+ icon: Icon::Uplink,
+ name: "Connection",
+ said: format!(
+ "The connection carries {} and the program needs {}. Viewers would see it stall.",
+ health::rate(*carries),
+ health::rate(needs)
+ ),
+ level: None,
+ fixes: send.into_iter().chain([again]).collect(),
+ },
+ }
+ }
+ });
+ checks
+ }
+
+ /// The stream's health as one dot, its name beside it unless `compact`,
+ /// opening the worst problem, every destination, the viewers, and the
+ /// encoder, and the details from there.
+ pub(super) fn health_dot(&mut self, ui: &mut Ui, fx: &mut Fx, compact: bool) {
+ let on_air = self.engine.on_air();
+ let watched = self.engine.watched();
+ let report = self.engine.health.lock().unwrap();
+ let worst = report
+ .problems
+ .first()
+ .map_or(Verdict::Healthy, |problem| problem.verdict);
+ // A problem shows even off air, such as a failed source.
+ let (name, dot) = match (worst, on_air.live().is_some(), watched.is_empty()) {
+ (Verdict::Unhealthy, ..) => ("Unhealthy", kit::ERR),
+ (Verdict::Degraded, ..) => ("Degraded", kit::CUE),
+ (_, false, true) => ("No relay", kit::DIM),
+ (_, false, false) => ("Ready", kit::OK),
+ (Verdict::Healthy, true, _) => ("Healthy", kit::OK),
+ (Verdict::Unknown, true, _) => ("Starting", kit::DIM),
+ };
+ let mark = fx.on().then(|| crate::fx::Mark::new(ui.painter()));
+ let text = if compact { "" } else { name };
+ let pill = ui.add(kit::Pill::new(text).dot(dot).clickable());
+ let pill = kit::tip(pill, &format!("Health: {name}"), None);
+ if let Some(mark) = mark {
+ fx.health(&mark.around(pill.rect).inset(2.0));
+ }
+ let mut details = false;
+ let mut settings = false;
+ kit::popover(&pill).show(|ui| {
+ ui.set_width(400.0);
+ let row = |ui: &mut Ui, dot: Option, name: &str, note: &str, value: &str| {
+ kit::popover_row(ui, false, |ui| {
+ let (at, _) = ui.allocate_exact_size(vec2(8.0, 8.0), Sense::hover());
+ match dot {
+ Some(dot) => ui.painter().circle_filled(at.center(), 4.0, dot),
+ None => ui
+ .painter()
+ .circle_stroke(at.center(), 3.5, Stroke::new(1.0, kit::FAINT)),
+ };
+ ui.label(egui::RichText::new(name).color(kit::TEXT));
+ ui.add(egui::Label::new(egui::RichText::new(note).color(kit::DIM)).truncate());
+ ui.with_layout(egui::Layout::right_to_left(Align::Center), |ui| {
+ ui.label(egui::RichText::new(value).font(kit::mono(12.0)).color(kit::TEXT))
+ });
+ });
+ };
+ // The worst problem first, on a card of its own.
+ if let Some(problem) = report.problems.first() {
+ let color = super::color(problem.verdict);
+ egui::Frame::new()
+ .fill(color.gamma_multiply(0.08))
+ .stroke(Stroke::new(1.0, color))
+ .corner_radius(10)
+ .inner_margin(12)
+ .show(ui, |ui| {
+ ui.set_width(ui.available_width());
+ ui.horizontal_top(|ui| {
+ ui.add(Icon::Warning.image(18.0, color));
+ ui.add(
+ egui::Label::new(
+ egui::RichText::new(&problem.cause)
+ .font(kit::ui_strong(14.0))
+ .color(kit::TEXT),
+ )
+ .wrap(),
+ );
+ });
+ });
+ for problem in report.problems.iter().skip(1) {
+ row(ui, Some(super::color(problem.verdict)), &problem.cause, "", "");
+ }
+ }
+ heading(ui, "Destinations");
+ match on_air.live() {
+ Some(_) => {
+ for relay in &report.relays {
+ let (dot, state) = match &relay.state {
+ health::State::Connecting => (kit::DIM, "connecting"),
+ health::State::Connected => (kit::OK, "connected"),
+ health::State::Disconnected(_) => (kit::ERR, "unreachable"),
+ };
+ let rtt = match &report.uplink.relay {
+ Some(endpoint) if *endpoint == relay.endpoint => report.uplink.rtt,
+ _ => None,
+ };
+ let rtt = rtt.map(|rtt| format!("{} ms", rtt.as_millis())).unwrap_or_default();
+ row(ui, Some(dot), &relay.endpoint, state, &rtt);
+ }
+ }
+ None if watched.is_empty() => {
+ kit::popover_row(ui, false, |ui| {
+ ui.label("No relay yet.");
+ ui.with_layout(egui::Layout::right_to_left(Align::Center), |ui| {
+ settings = ui.button("Set one up").clicked();
+ });
+ });
+ }
+ None => {
+ for (relay, rtt) in &watched {
+ let (dot, state) = match &relay.state {
+ health::State::Connected => (Some(kit::OK), "standing by"),
+ health::State::Connecting => (None, "connecting"),
+ health::State::Disconnected(_) => (Some(kit::ERR), "unreachable"),
+ };
+ let rtt = rtt.map(|rtt| format!("{} ms", rtt.as_millis())).unwrap_or_default();
+ row(ui, dot, &relay.endpoint, state, &rtt);
+ }
+ }
+ }
+ heading(ui, "Tracks and viewers");
+ let uplink = &report.uplink;
+ let media = uplink.media.map(health::rate).unwrap_or_default();
+ row(ui, None, "Sent", "every track, to the relay", &media);
+ let watching = report
+ .broadcasts
+ .first()
+ .and_then(|program| program.viewers.watching)
+ .map(|watching| watching.to_string());
+ let watching_note = match on_air.live() {
+ Some(_) => "",
+ None => "off air",
+ };
+ row(ui, None, "Watching", watching_note, watching.as_deref().unwrap_or("-"));
+ let encoder = self.engine.stats.video.encoder();
+ let encoder_note = match &encoder {
+ Some(_) => "",
+ None => "opens on air",
+ };
+ row(ui, None, "Encoder", encoder_note, encoder.as_deref().unwrap_or(""));
+ ui.add_space(4.0);
+ details = ui.button("Details").clicked();
+ });
+ drop(report);
+ if details {
+ self.details = true;
+ }
+ if settings {
+ self.setting = Some(Setting::Stream);
+ }
+ }
+
+ /// Apply a Stream settings save once it finished, before every frame and
+ /// while the window is hidden, so one still running when Settings closes,
+ /// the tab changes, or the window hides applies before a hotkey goes live.
+ pub(super) fn follow_stream_save(&mut self) {
+ let Some(form) = &mut self.stream_form else { return };
+ let Some(saving) = &mut form.saving else { return };
+ // The save asks for a frame once it finishes, which polls it again.
+ let Some(saved) = saving.poll() else { return };
+ match saved.clone() {
+ Ok(saved) => {
+ let before = self.controls.stream.lock().unwrap().clone();
+ // Only a new address or token follows the relays anew, which
+ // cuts the broadcast sources and guests they carry, so not on
+ // air: then once the stream ends.
+ let moved = before.relay() != saved.relay() || before.failover() != saved.failover();
+ let retokened = !form.tokens().is_empty()
+ || [&form.relay, &form.failover]
+ .iter()
+ .any(|url| crate::stream::carries_token(url));
+ let deferred = self.controls.set_stream(saved.clone(), moved || retokened);
+ // A measurement under way is of the relay as it was.
+ if (moved || retokened)
+ && let Some(going) = &mut self.going
+ {
+ going.uplink = None;
+ }
+ *form = Form::new(&saved);
+ form.note = Some(match deferred {
+ true => "Saved. It applies the next time you go live; broadcast sources and guests follow the new relays once this stream ends.".into(),
+ false => "Saved. It applies the next time you go live.".into(),
+ });
+ }
+ Err(err) => {
+ form.saving = None;
+ form.note = Some(err);
+ }
+ }
+ }
+
+ /// Settings, Stream: the relay and its token, a failover relay, the
+ /// broadcast's name, and on-demand encoding, kept beside the show. A
+ /// setting a flag overrides for this run says so.
+ pub(super) fn stream_settings(&mut self, ui: &mut Ui) {
+ let stream = self.controls.stream.lock().unwrap().clone();
+ let overridden = stream.overridden();
+ let form = self.stream_form.get_or_insert_with(|| Form::new(&stream));
+ let busy = form.saving.is_some();
+ if busy {
+ ui.ctx().request_repaint_after(Duration::from_millis(100));
+ }
+ ui.label(
+ egui::RichText::new(format!(
+ "Kept beside the show, in {}. Tokens go in the system's credential store, never in a file.",
+ stream.path().display()
+ ))
+ .color(kit::DIM),
+ );
+ ui.add_space(8.0);
+ let flagged = |ui: &mut Ui, flag: &str| {
+ ui.label(egui::RichText::new(format!("{flag} overrides this for this run")).color(kit::CUE));
+ };
+ ui.add_enabled_ui(!busy, |ui| {
+ egui::Grid::new("stream settings")
+ .num_columns(2)
+ .spacing(vec2(14.0, 10.0))
+ .show(ui, |ui| {
+ ui.label("Relay");
+ ui.vertical(|ui| {
+ ui.add(
+ egui::TextEdit::singleline(&mut form.relay)
+ .hint_text("https://relay.example/anon")
+ .desired_width(360.0),
+ );
+ if overridden.relay {
+ flagged(ui, "--connect");
+ }
+ });
+ ui.end_row();
+ ui.label("Token");
+ ui.vertical(|ui| {
+ ui.add(
+ egui::TextEdit::singleline(&mut form.relay_token)
+ .password(true)
+ .hint_text("Leave empty to keep the saved one")
+ .desired_width(360.0),
+ );
+ ui.checkbox(&mut form.forget_relay_token, "Forget the saved token");
+ if overridden.token {
+ flagged(ui, "--connect-token");
+ }
+ });
+ ui.end_row();
+ ui.label("Failover relay");
+ ui.vertical(|ui| {
+ ui.add(
+ egui::TextEdit::singleline(&mut form.failover)
+ .hint_text("A second relay viewers resume from")
+ .desired_width(360.0),
+ );
+ if overridden.failover {
+ flagged(ui, "--failover");
+ }
+ });
+ ui.end_row();
+ ui.label("Its token");
+ ui.vertical(|ui| {
+ ui.add(
+ egui::TextEdit::singleline(&mut form.failover_token)
+ .password(true)
+ .hint_text("Leave empty to keep the saved one")
+ .desired_width(360.0),
+ );
+ ui.checkbox(&mut form.forget_failover_token, "Forget the saved token");
+ });
+ ui.end_row();
+ ui.label("Broadcast name");
+ ui.vertical(|ui| {
+ ui.add(
+ egui::TextEdit::singleline(&mut form.broadcast)
+ .hint_text(crate::stream::BROADCAST)
+ .desired_width(360.0),
+ );
+ if overridden.broadcast {
+ flagged(ui, "--broadcast");
+ }
+ });
+ ui.end_row();
+ ui.label("On demand");
+ ui.vertical(|ui| {
+ ui.add(kit::Toggle::new(&mut form.on_demand).label("Encode the full size only while watched"));
+ ui.add(
+ egui::Label::new(
+ egui::RichText::new(
+ "Saves the work, but a late viewer gets nothing from before they arrived.",
+ )
+ .color(kit::DIM),
+ )
+ .wrap(),
+ );
+ if overridden.on_demand {
+ flagged(ui, "--on-demand");
+ }
+ });
+ ui.end_row();
+ });
+ });
+ ui.add_space(10.0);
+ ui.horizontal(|ui| {
+ let edited = form.edited(stream.saved());
+ if ui.add_enabled(edited && !busy, egui::Button::new("Save")).clicked() {
+ match form.saved(stream.saved()) {
+ Ok(saved) => {
+ let tokens = form.tokens();
+ form.saving = Some(save(stream, saved, tokens, ui.ctx()));
+ form.note = None;
+ }
+ Err(err) => form.note = Some(err),
+ }
+ }
+ if ui.add_enabled(edited && !busy, egui::Button::new("Revert")).clicked() {
+ let stream = self.controls.stream.lock().unwrap().clone();
+ *form = Form::new(&stream);
+ }
+ if busy {
+ ui.spinner();
+ }
+ if let Some(note) = &form.note {
+ ui.label(egui::RichText::new(note).color(kit::DIM));
+ }
+ });
+ }
+}
+
+/// Measure the connection to the first relay switched on toward `ceiling`,
+/// reading its token on the task's own thread, since the credential store
+/// blocks.
+fn measure(controls: &Controls, runtime: &tokio::runtime::Handle, ceiling: Rate) -> Measuring {
+ let stream = controls.stream.lock().unwrap().clone();
+ let switched = *controls.switched.lock().unwrap();
+ let handle = runtime.clone();
+ let (stop, stopped) = tokio::sync::oneshot::channel::<()>();
+ let mut on_air = controls.engine.on_air_changes();
+ let task = Task::spawn(runtime, move || {
+ let relays = {
+ // Each client binds its socket on the runtime.
+ let _runtime = handle.enter();
+ stream.relays(switched)
+ };
+ let path = stream.broadcast().to_string();
+ async move {
+ let relays = relays.map_err(|err| format!("{err:#}"))?;
+ let relay = relays.into_iter().next().ok_or("no relay is switched on")?;
+ let measured = uplink::measure(&relay.client, relay.url, &path, ceiling, uplink::WINDOW);
+ tokio::select! {
+ biased;
+ // Fails only once the engine is gone, which ends this anyway.
+ _ = on_air.wait_for(|on_air| *on_air != OnAir::Off) => Err("stopped as the stream went live".into()),
+ // Sent never: the sender is dropped to stop.
+ _ = stopped => Err("stopped".into()),
+ result = measured => result.map_err(|err| format!("{err:#}")),
+ }
+ }
+ });
+ Measuring {
+ task: task.unwrap_or_else(|err| Task::Done(Err(format!("cannot start the measurement: {err}")))),
+ _stop: Some(stop),
+ }
+}
+
+/// A section's heading in a sheet or popover.
+fn heading(ui: &mut Ui, text: &str) {
+ ui.add_space(6.0);
+ ui.label(kit::caps(text, kit::display_bold(11.0)).color(kit::FAINT));
+ ui.add_space(2.0);
+}
+
+/// A check's line: its mark, its icon and name, what it found, and a button
+/// for each fix; the fix pressed, if any.
+fn check_row(ui: &mut Ui, check: &Check) -> Option {
+ let mut pressed = None;
+ ui.vertical(|ui| {
+ ui.horizontal(|ui| {
+ let (at, _) = ui.allocate_exact_size(Vec2::splat(20.0), Sense::hover());
+ let painter = ui.painter();
+ match check.mark {
+ Outcome::Waiting => {
+ egui::Spinner::new()
+ .size(14.0)
+ .color(kit::DIM)
+ .paint_at(ui, at.shrink(3.0));
+ }
+ Outcome::Passed => {
+ painter.circle_filled(at.center(), 10.0, kit::OK.gamma_multiply(0.2));
+ Icon::Check.paint(ui, at.shrink(4.0), 12.0, kit::OK);
+ }
+ Outcome::Failed => {
+ painter.circle_filled(at.center(), 10.0, kit::CUE);
+ Icon::Close.paint(ui, at.shrink(4.0), 12.0, kit::ON_CUE);
+ }
+ Outcome::Skipped => {
+ painter.circle_stroke(at.center(), 9.0, Stroke::new(1.0, kit::FAINT));
+ }
+ }
+ if check.mark == Outcome::Failed {
+ ui.add(
+ egui::Label::new(
+ egui::RichText::new(&check.said)
+ .font(kit::ui_strong(13.0))
+ .color(kit::TEXT),
+ )
+ .wrap(),
+ );
+ return;
+ }
+ ui.add(check.icon.image(14.0, kit::DIM));
+ ui.label(
+ egui::RichText::new(check.name)
+ .font(kit::ui_strong(13.0))
+ .color(kit::TEXT),
+ );
+ ui.with_layout(egui::Layout::right_to_left(Align::Center), |ui| {
+ ui.add(egui::Label::new(egui::RichText::new(&check.said).color(kit::DIM)).truncate());
+ if let Some((level, peak)) = check.level {
+ ui.add(kit::Meter::new(level, peak).run(kit::Run::Across, 80.0, 8.0));
+ }
+ });
+ });
+ if !check.fixes.is_empty() {
+ ui.horizontal(|ui| {
+ ui.add_space(28.0);
+ for (index, (label, fix)) in check.fixes.iter().enumerate() {
+ let button = match index {
+ 0 => egui::Button::new(egui::RichText::new(label).color(kit::ON_CUE)).fill(kit::CUE),
+ _ => egui::Button::new(label),
+ };
+ if ui.add(button).clicked() {
+ pressed = Some(*fix);
+ }
+ }
+ });
+ }
+ });
+ pressed
+}
+
+/// The sheet's confirm: one wide light button, neither the accent nor red.
+fn confirm_button(ui: &mut Ui, text: &str, enabled: bool, waiting: bool) -> egui::Response {
+ let size = vec2(ui.available_width(), 54.0);
+ let sense = if enabled { Sense::click() } else { Sense::hover() };
+ let (rect, response) = ui.allocate_exact_size(size, sense);
+ response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, enabled, text));
+ let (fill, ink) = match (enabled, response.hovered()) {
+ (true, true) => (Color32::WHITE, kit::BG),
+ (true, false) => (Color32::from_gray(0xe8), kit::BG),
+ (false, _) => (kit::P3, kit::DIM),
+ };
+ let painter = ui.painter();
+ painter.rect_filled(rect, 8, fill);
+ let galley = kit::caps_galley(painter, text, kit::display(22.0), ink, None);
+ let mut at = rect.center() - galley.size() / 2.0;
+ if waiting {
+ let spinner = Rect::from_center_size(pos2(at.x - 14.0, rect.center().y), Vec2::splat(16.0));
+ egui::Spinner::new().size(16.0).color(kit::DIM).paint_at(ui, spinner);
+ at.x += 6.0;
+ }
+ ui.painter().galley(at, galley, ink);
+ response
+}
+
+/// The Stream tab's form.
+pub(super) struct Form {
+ relay: String,
+ relay_token: String,
+ forget_relay_token: bool,
+ failover: String,
+ failover_token: String,
+ forget_failover_token: bool,
+ broadcast: String,
+ on_demand: bool,
+ saving: Option>>,
+ /// How the last save went, or why it was refused.
+ note: Option,
+}
+
+/// A token to keep for a relay, or `None` to forget its saved one.
+type Token = (url::Url, Option);
+
+impl Form {
+ /// The form holding what `stream` saved, not what the flags set.
+ fn new(stream: &Stream) -> Self {
+ let saved = stream.saved();
+ let text = |url: &Option| url.as_ref().map(url::Url::to_string).unwrap_or_default();
+ Self {
+ relay: text(&saved.relay),
+ relay_token: String::new(),
+ forget_relay_token: false,
+ failover: text(&saved.failover),
+ failover_token: String::new(),
+ forget_failover_token: false,
+ broadcast: saved.broadcast.clone().unwrap_or_default(),
+ on_demand: saved.on_demand,
+ saving: None,
+ note: None,
+ }
+ }
+
+ /// What the form would save over `saved`, or why it cannot.
+ fn saved(&self, saved: &Saved) -> Result {
+ let url = |text: &str, name: &str| -> Result, String> {
+ match text.trim() {
+ "" => Ok(None),
+ text => text
+ .parse()
+ .map(Some)
+ .map_err(|err| format!("The {name} address is not a URL: {err}.")),
+ }
+ };
+ let broadcast = self.broadcast.trim();
+ Ok(Saved {
+ relay: url(&self.relay, "relay")?,
+ failover: url(&self.failover, "failover relay")?,
+ broadcast: (!broadcast.is_empty()).then(|| broadcast.to_string()),
+ on_demand: self.on_demand,
+ ..saved.clone()
+ })
+ }
+
+ /// Whether the form holds anything to save over `saved`.
+ fn edited(&self, saved: &Saved) -> bool {
+ let tokens = !self.relay_token.is_empty()
+ || !self.failover_token.is_empty()
+ || self.forget_relay_token
+ || self.forget_failover_token;
+ tokens || self.saved(saved).is_ok_and(|form| form != *saved) || self.saved(saved).is_err()
+ }
+
+ /// The tokens typed or forgotten, by the relay they go with.
+ fn tokens(&self) -> Vec {
+ let mut tokens = Vec::new();
+ for (url, token, forget) in [
+ (&self.relay, &self.relay_token, self.forget_relay_token),
+ (&self.failover, &self.failover_token, self.forget_failover_token),
+ ] {
+ let Ok(url) = url.trim().parse::() else {
+ continue;
+ };
+ match (token.trim(), forget) {
+ ("", true) => tokens.push((url, None)),
+ ("", false) => {}
+ (token, _) => tokens.push((url, Some(token.to_string()))),
+ }
+ }
+ tokens
+ }
+}
+
+/// Keep `tokens` and save `saved` as `stream`'s, on a thread of their own,
+/// since the credential store blocks, asking `ctx` for a frame once the
+/// result is in, which applies it even while the window is hidden.
+fn save(mut stream: Stream, saved: Saved, tokens: Vec, ctx: &egui::Context) -> Task