From 5352f669338d8ae9b307de1cd0e0d1ac2f0c01ff Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 16 Nov 2026 08:05:17 -0800 Subject: [PATCH 1/7] chore(quest): claim quest/m0/program-encoder-priority.md Quest-Claim: dcd1f5ac0a302577 From 471374e7e3cf8acdd5fa8d31a45d94df477474fd Mon Sep 17 00:00:00 2001 From: Luke Curley Date: Mon, 16 Nov 2026 08:38:44 -0800 Subject: [PATCH 2/7] feat: the program reclaims the sources' replay encoders' sessions When the GPU refuses the program, a published scene, or the recording an encoder while sources on program hold sessions only so a replay has their recent past, those replay encoders close and the program's opens in their sessions. Each such source reopens once a session is given back, its replay starting over; a source whose track is read keeps its encoder. Sessions::capped stands in for a session-capped GPU in tests. Co-Authored-By: Claude Opus 5.5 --- README.md | 16 +- quest/m0/program-encoder-priority.md | 29 --- rs/moq-scene/src/lib.rs | 5 + rs/moq-scene/src/publish.rs | 280 ++++++++++++++++++++++++--- rs/moq-studio/src/engine.rs | 223 ++++++++++++++++++--- rs/moq-studio/src/engine/air.rs | 2 + rs/moq-studio/src/shots.rs | 1 + rs/moq-studio/src/show.rs | 1 + 8 files changed, 468 insertions(+), 89 deletions(-) delete mode 100644 quest/m0/program-encoder-priority.md diff --git a/README.md b/README.md index 4a415841..5a6ee595 100644 --- a/README.md +++ b/README.md @@ -1226,12 +1226,16 @@ plus 1 for each captured source it shows that program does not. When the program 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 -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 -where it can and software otherwise. +drawing in the studio's own scenes. The program comes first: when the GPU +refuses the program or a published scene an encoder, such as going live while +the sources on program hold every session, the studio closes the encoders those +sources keep only for a replay of their recent past and opens the program's in +their sessions. Each such source reopens once a session is given back, such as +when the stream ends, its replay starting over; one whose track something reads, +such as a recording, keeps its encoder. A scene published past the cap with no +replay left to close publishes without video and says why in Settings' Show +tab. When the program encodes in software, a source takes hardware where it can +and software otherwise. ### Published independently diff --git a/quest/m0/program-encoder-priority.md b/quest/m0/program-encoder-priority.md deleted file mode 100644 index af6f51d7..00000000 --- a/quest/m0/program-encoder-priority.md +++ /dev/null @@ -1,29 +0,0 @@ -# [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/rs/moq-scene/src/lib.rs b/rs/moq-scene/src/lib.rs index a2482534..10b0b030 100644 --- a/rs/moq-scene/src/lib.rs +++ b/rs/moq-scene/src/lib.rs @@ -134,6 +134,11 @@ pub enum Error { #[error(transparent)] Net(#[from] moq_net::Error), + /// Every encode session [`Sessions::capped`](publish::Sessions::capped) + /// allows is open. + #[error("all {0} encode sessions are open")] + Sessions(usize), + /// A browser source's page failed, or its helper process stopped. #[error("browser: {0}")] Browser(String), diff --git a/rs/moq-scene/src/publish.rs b/rs/moq-scene/src/publish.rs index 59df52f3..9ee457e9 100644 --- a/rs/moq-scene/src/publish.rs +++ b/rs/moq-scene/src/publish.rs @@ -12,7 +12,6 @@ //! something in the studio subscribes to it, such as a recording. use std::collections::{BTreeMap, BTreeSet}; -use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; use serde::{Deserialize, Serialize}; @@ -64,29 +63,131 @@ impl Section { /// The catalog of the program's broadcast, or of the sources' local one. pub type Catalog = moq_mux::catalog::Producer; -/// How many video encoders are open. Each holds a session on a hardware -/// encoder, which a GPU caps, such as at 8 on a GeForce card. -#[derive(Clone, Debug, Default)] -pub struct Sessions(Arc); +/// The video encoders open, each holding a session on a hardware encoder, +/// which a GPU caps, such as at 8 on a GeForce card, refusing an open past it. +/// +/// The program's encoders come first: one the GPU refuses +/// [reclaims](Self::reclaim) the sessions of the sources' replay encoders, +/// those open only so a replay of a source on program has its recent past. +#[derive(Clone, Debug)] +pub struct Sessions(Arc>); + +#[derive(Clone, Copy, Debug, Default)] +struct State { + open: usize, + /// Those of `open` held by a replay encoder, which the program may take. + spare: usize, + /// The program's reclaims under way: while any is, a replay encoder + /// closes and none opens. + reclaims: usize, + /// Refuse an open past this many, as a capped GPU does. + cap: Option, +} + +impl Default for Sessions { + fn default() -> Self { + Self(Arc::new(tokio::sync::watch::Sender::new(State::default()))) + } +} impl Sessions { + /// Sessions that refuse an encoder past `cap` open at once, as a GPU with + /// that cap does, to stand in for one on a machine without it. + pub fn capped(cap: usize) -> Self { + let state = State { + cap: Some(cap), + ..Default::default() + }; + Self(Arc::new(tokio::sync::watch::Sender::new(state))) + } + /// How many encoders are open now. pub fn open(&self) -> usize { - self.0.load(Ordering::Relaxed) + self.0.borrow().open } - fn hold(&self) -> Session { - self.0.fetch_add(1, Ordering::Relaxed); - Session(self.clone()) + /// How many of those are the sources' replay encoders, which a + /// [reclaim](Self::reclaim) would close. + pub fn spare(&self) -> usize { + self.0.borrow().spare + } + + /// Close every replay encoder and open none until the returned guard + /// drops, so the program's encoder can open in their sessions. A replay + /// starts over once its encoder reopens, which waits for a session given + /// back if the GPU refuses it. + pub fn reclaim(&self) -> Reclaim { + self.0.send_modify(|state| state.reclaims += 1); + Reclaim(self.clone()) + } + + fn hold(&self) -> Result { + let mut refused = None; + self.0.send_if_modified(|state| match state.cap { + Some(cap) if state.open >= cap => { + refused = Some(cap); + false + } + _ => { + state.open += 1; + true + } + }); + match refused { + Some(cap) => Err(Error::Sessions(cap)), + None => Ok(Session { + sessions: self.clone(), + spare: false, + }), + } + } +} + +/// The program's claim on the replay encoders' sessions, from +/// [`Sessions::reclaim`] until dropped. +pub struct Reclaim(Sessions); + +impl Reclaim { + /// Wait until every replay encoder has closed. + pub async fn closed(&self) { + let mut state = (self.0).0.subscribe(); + // Fails only once the sender drops, which this holds. + let _ = state.wait_for(|state| state.spare == 0).await; + } +} + +impl Drop for Reclaim { + fn drop(&mut self) { + (self.0).0.send_modify(|state| state.reclaims -= 1); } } /// One open encoder's place in its [`Sessions`], given back when dropped. -struct Session(Sessions); +struct Session { + sessions: Sessions, + /// Held by a replay encoder. + spare: bool, +} + +impl Session { + fn set_spare(&mut self, spare: bool) { + if self.spare != spare { + self.spare = spare; + self.sessions.0.send_modify(|state| match spare { + true => state.spare += 1, + false => state.spare -= 1, + }); + } + } +} impl Drop for Session { fn drop(&mut self) { - (self.0).0.fetch_sub(1, Ordering::Relaxed); + let spare = self.spare; + self.sessions.0.send_modify(|state| { + state.open -= 1; + state.spare -= usize::from(spare); + }); } } @@ -665,12 +766,25 @@ impl Encoding { } /// Encode with `encoder` from here on, after draining the one it replaces. + /// Fails when [capped](Sessions::capped) sessions are all open. pub fn open(&mut self, encoder: E) -> Result<(), Error> { self.drain()?; - self.encoder = Some((encoder, self.sessions.hold())); + self.encoder = Some((encoder, self.sessions.hold()?)); Ok(()) } + /// Whether the open encoder is a replay's, which the program may reclaim. + fn spare(&self) -> bool { + self.encoder.as_ref().is_some_and(|(_, session)| session.spare) + } + + /// Mark the open encoder as a replay's, or not. + fn set_spare(&mut self, spare: bool) { + if let Some((_, session)) = &mut self.encoder { + session.set_spare(spare); + } + } + /// Encode `frame` and publish what the encoder hands back. /// /// # Panics @@ -793,6 +907,12 @@ pub(crate) struct Video<'a> { /// Where the capture's private timeline starts on the studio clock, in /// microseconds, taken at the first frame unless [anchored](Self::anchor). epoch: Option, + /// The encoder closed for the program's [reclaim](Sessions::reclaim), + /// until it reopens. + reclaimed: bool, + /// How many sessions were open when the GPU refused a reclaimed encoder + /// reopening, which waits until fewer are. + refused: Option, } impl<'a> Video<'a> { @@ -825,6 +945,8 @@ impl<'a> Video<'a> { config, clock: listing.studio.clock(), epoch: None, + reclaimed: false, + refused: None, }) } @@ -857,7 +979,7 @@ impl<'a> Video<'a> { }; frame.timestamp = moq_net::Timestamp::from_micros((epoch + captured) as u64).map_err(moq_video::Error::from)?; - if !self.used() { + if !self.wanted() { return self.release(); } @@ -871,9 +993,26 @@ impl<'a> Video<'a> { self.track.drain()?; } if !self.track.is_open() { - self.track.open(moq_video::encode::Encoder::new(&self.config)?)?; - self.granted = None; + let opened = moq_video::encode::Encoder::new(&self.config) + .map_err(Error::from) + .and_then(|encoder| self.track.open(encoder)); + match opened { + Ok(()) => { + self.reclaimed = false; + self.refused = None; + self.granted = None; + } + // Its session went to the program, so it waits for one given + // back rather than failing. + Err(err) if self.reclaimed => { + tracing::warn!(source = %self.claim.name, %err, "waiting for an encode session"); + self.refused = Some(self.track.sessions.open()); + return Ok(()); + } + Err(err) => return Err(err), + } } + self.track.set_spare(!self.track.watched()); // Published independently, it encodes at what the connection grants. let grant = self .grants @@ -907,23 +1046,44 @@ impl<'a> Video<'a> { self.on_program.borrow().shown.contains(&self.claim.name) || self.track.watched() } - /// Wait until whether the source is used disagrees with whether its - /// encoder is open, so a source that delivers nothing new, such as a - /// still screen or a media file that ended, can act without waiting on a - /// picture: [`release`] the encoder when it is open and nothing uses the - /// source, or bring a picture when it is closed and the source goes into - /// use. + /// Whether the encoder is to be open: while the source is used, but not + /// for a replay alone while the program reclaims its session, nor while + /// waiting for a session given back since the GPU refused one. + fn wanted(&self) -> bool { + let watched = self.track.watched(); + if !watched && !self.on_program.borrow().shown.contains(&self.claim.name) { + return false; + } + let sessions = self.track.sessions.0.borrow(); + (watched || sessions.reclaims == 0) && self.refused.is_none_or(|open| sessions.open < open) + } + + /// Whether the encoder is not as [`wanted`](Self::wanted): open or closed + /// when it should not be, or open and marked a replay's while read, or + /// the other way around, which a reclaim would misjudge. + fn unsettled(&self) -> bool { + let wanted = self.wanted(); + wanted != self.track.is_open() || (wanted && self.track.spare() == self.track.watched()) + } + + /// Wait until the encoder is not as wanted, so a source that delivers + /// nothing new, such as a still screen or a media file that ended, can + /// act without waiting on a picture: [`release`] the encoder when it is + /// open, or bring a picture when it is closed and wanted. /// /// [`release`]: Self::release pub(crate) async fn settle(&self) { let mut demand = self.track.demand(); let mut on_program = self.on_program.clone(); on_program.mark_unchanged(); - while self.used() == self.track.is_open() { + let mut sessions = self.track.sessions.0.subscribe(); + while !self.unsettled() { // The first look at the demand reports it as it is now. tokio::select! { changed = on_program.changed() => if changed.is_err() { std::future::pending::<()>().await }, wanted = demand.wanted_changed() => if wanted.is_err() { std::future::pending::<()>().await }, + // Never fails: the track holds the sender. + _ = sessions.changed() => {} } } } @@ -943,13 +1103,20 @@ impl<'a> Video<'a> { self.epoch = None; } - /// Close the encoder while nothing uses the source, marking the gap, which - /// gives the encode session back. + /// Close the encoder while it is not [wanted](Self::wanted), marking the + /// gap, which gives the encode session back, or else mark it a replay's + /// or not, as whether its track is read says. pub(crate) fn release(&mut self) -> Result<(), Error> { - if self.track.is_open() && !self.used() { - self.track.discontinuity()?; + if !self.track.is_open() { + return Ok(()); } - Ok(()) + if self.wanted() { + self.track.set_spare(!self.track.watched()); + return Ok(()); + } + // Still used, so it closed for the program. + self.reclaimed = self.used(); + self.track.discontinuity() } /// Start a group with the next picture, as a keyframe, such as where a @@ -1600,6 +1767,65 @@ mod tests { .await; } + /// On a GPU at its session cap, the program reclaims a replay encoder's + /// session, even from a still screen that delivers no picture, and the + /// replay waits for a session given back rather than failing, then starts + /// over. One whose track is read keeps its encoder. + #[tokio::test(start_paused = true)] + async fn the_program_reclaims_a_replays_session() { + local(async { + let clock = moq_mux::Clock::new(); + let sessions = Sessions::capped(1); + let publish = Publish::new(clock, moq_video::encode::Kind::Auto, sessions.clone()).unwrap(); + let claim = publish.claim("display"); + let listing = Listing::new(&claim, &publish).unwrap(); + let framerate = moq_video::FrameRate::new(30, 1).unwrap(); + let size = moq_video::Size::new(64, 48); + let mut video = Video::new(&listing, size, framerate, None).await.unwrap(); + let wait = std::time::Duration::from_secs(1); + let settled = async |video: &Video<'_>| kio::clock::timeout(wait, video.settle()).await; + + publish.set_on_program(shown("display")); + video.encode(&mut gray(size, 0)).unwrap(); + assert_eq!((sessions.open(), sessions.spare()), (1, 1), "a replay's"); + assert!(sessions.hold().is_err(), "at the cap"); + + let reclaim = sessions.reclaim(); + settled(&video).await.expect("asked to close without a picture"); + video.release().unwrap(); + kio::clock::timeout(wait, reclaim.closed()) + .await + .expect("the replay closed"); + assert_eq!(sessions.open(), 0); + video.encode(&mut gray(size, 33)).unwrap(); + assert_eq!(sessions.open(), 0, "none opens while the program reclaims"); + + let program = sessions.hold().unwrap(); + drop(reclaim); + settled(&video).await.expect("wanted again"); + video.encode(&mut gray(size, 66)).unwrap(); + assert!(!video.is_open(), "refused, so it waits"); + assert!(settled(&video).await.is_err(), "until a session is given back"); + drop(program); + settled(&video).await.expect("a session given back"); + video.encode(&mut gray(size, 100)).unwrap(); + assert_eq!((sessions.open(), sessions.spare()), (1, 1), "the replay starts over"); + + // Read, such as by a recording, it is no replay's alone. + let snapshot = listing.catalog.snapshot(); + let name = snapshot.video.renditions.keys().next().unwrap(); + let _track = publish.consume().track(name).unwrap().subscribe(None).await.unwrap(); + let reclaim = sessions.reclaim(); + settled(&video).await.expect("marked read without a picture"); + video.release().unwrap(); + kio::clock::timeout(wait, reclaim.closed()) + .await + .expect("nothing to close"); + assert_eq!((sessions.open(), sessions.spare()), (1, 0), "kept"); + }) + .await; + } + /// A source off program encodes only while its track is subscribed, such /// as by a recording, holding no encoder between subscribers. #[tokio::test] diff --git a/rs/moq-studio/src/engine.rs b/rs/moq-studio/src/engine.rs index 7b5f0b09..e3e7df59 100644 --- a/rs/moq-studio/src/engine.rs +++ b/rs/moq-studio/src/engine.rs @@ -39,7 +39,7 @@ use anyhow::Context; use bytes::Bytes; use kio::clock::Instant; use moq_net::bandwidth; -use moq_scene::publish::{Encoding, Sessions}; +use moq_scene::publish::{Encoding, Reclaim, Sessions}; use moq_scene::source::{self, Driver, Sources, Status}; use moq_scene::{Canvas, Collection, Compositor, Draft, Edit, History, Scaler}; use moq_video::render::export::{Exporter, Pending, Route}; @@ -860,6 +860,8 @@ pub struct Program { epoch: Duration, /// Where the local sources keep their own tracks, on the same clock. sources: moq_scene::publish::Publish, + /// [`Renditions::reclaim`]. + reclaim: Option, /// The sources published independently, whose tracks the broadcast on air /// carries beside the program's. published: tokio::sync::watch::Sender>, @@ -1001,6 +1003,8 @@ pub struct Launch { pub recordings: PathBuf, /// The room guests join, if any. pub room: Option>, + /// Where the video encoders count the encode sessions they hold. + pub sessions: Sessions, } impl Engine { @@ -1012,7 +1016,12 @@ impl Engine { /// /// 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, recordings, room } = launch; + let Launch { + gpu, + recordings, + room, + sessions, + } = 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. @@ -1032,7 +1041,10 @@ impl Engine { dropped, } = loaded; let canvas = collection.canvas; - let stats = Arc::new(Stats::default()); + let stats = Arc::new(Stats { + sessions, + ..Default::default() + }); // The program clock's start, from which the audio clock advances the // bus too, so the sources place their sound on it. @@ -1865,22 +1877,22 @@ async fn publish(gpu: &Gpu, stats: &Arc, canvas: Canvas, start: Instant) // 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, - }, + let kind = match Probe::open(gpu, canvas, false).await { + Ok(probe) if !probe.hardware() => moq_video::encode::Kind::Auto, + Ok(_) => moq_video::encode::Kind::Hardware, Err(err) => { stats.video.fail(format!("{err:#}")); moq_video::encode::Kind::Hardware } }; + let reclaim = matches!(kind, moq_video::encode::Kind::Hardware).then(|| stats.sessions.clone()); let sources = moq_scene::publish::Publish::new(clock.clone(), kind, stats.sessions.clone())?; let recorded_video = Renditions { gpu: gpu.clone(), broadcast: recorder.clone(), timebase: recorded.timebase(), record: true, + reclaim: reclaim.clone(), bandwidth: moq_net::bandwidth::Allocator::unlimited(), on_demand: true, live: false, @@ -1897,6 +1909,7 @@ async fn publish(gpu: &Gpu, stats: &Arc, canvas: Canvas, start: Instant) clock, epoch, sources: sources.clone(), + reclaim, published, recording, recorded, @@ -1960,6 +1973,10 @@ struct Renditions { /// full quality whatever the grant, rather than the published full and /// half sizes. record: bool, + /// Where the program reclaims the sources' replay encoders' sessions when + /// the GPU refuses it the hardware encoder it found at launch, or `None` + /// when it found none, so the sources' encoders hold none either. + reclaim: Option, /// The connection's bandwidth, which each published rendition follows its /// share of. bandwidth: bandwidth::Allocator, @@ -1978,10 +1995,10 @@ struct Renditions { impl Renditions { /// Probe for `canvas`'s renditions and [`start`](Self::start) them, waiting - /// for the full size's encoder to open, as at startup, so it holds its - /// encode session before any source opens one. + /// for the full size's encoder to open, so a canvas the GPU cannot encode + /// fails here, even after reclaiming the sources' replay encoders. async fn open(&self, canvas: Canvas) -> anyhow::Result { - let probe = Probe::new(&self.gpu, canvas, self.record).await?; + let probe = Probe::new(self, canvas).await?; let (ladder, opened) = self.start(probe)?; opened.await.context("the encode thread stopped")??; Ok(ladder) @@ -2013,6 +2030,7 @@ impl Renditions { .collect(); let (opened, open) = tokio::sync::oneshot::channel(); let mut opened = Some(opened); + let mut reclaim = probe.reclaim; let number = self.video.rendition(); let mut rungs = Vec::with_capacity(probe.rungs.len()); for probed in probe.rungs { @@ -2062,8 +2080,18 @@ impl Renditions { // One more than the exporter's pool, for a skip behind a full pool. let (frames, pending) = mpsc::sync_channel(Exporter::DEPTH + 1); + // Live, the canvas size holds its session from the start, so a + // source past the GPU's session cap is the one that fails. Without + // a relay nobody can watch it, so it opens nothing. + let opened = match role { + Role::Canvas if self.live => opened.take(), + _ => None, + }; let job = Job { sessions: self.stats.sessions.clone(), + // Kept from the probe until the canvas size opens, so no source + // takes back a session meanwhile. + reclaim: opened.is_some().then(|| reclaim.take()).flatten(), video: self.video.clone(), role, rendition: (role == Role::Canvas).then_some(number), @@ -2074,13 +2102,6 @@ impl Renditions { // The canvas size holds its session while it skips frames. hold: role == Role::Canvas, }; - // Live, the canvas size holds its session from the start, so a - // source past the GPU's session cap is the one that fails. Without - // a relay nobody can watch it, so it opens nothing. - let opened = match role { - Role::Canvas if self.live => opened.take(), - _ => None, - }; let thread = std::thread::Builder::new() .name("encode".into()) .spawn(move || encode(job, opened, pending))?; @@ -2110,6 +2131,8 @@ struct Probe { rungs: Vec, /// Why the encoder refused a rendition the program does without. refused: Vec<(Role, String)>, + /// The sources' replay encoders' sessions, reclaimed for these encoders. + reclaim: Option, } /// One rendition of a [`Probe`]. @@ -2121,11 +2144,41 @@ struct Probed { } impl Probe { + /// [`open`](Self::open) `renditions` of `canvas`. When the GPU refuses + /// them the hardware encoder found at launch, as one at its session cap + /// does, the program comes first: this closes the sources' replay + /// encoders, which replay a source's recent past, and probes again in + /// their sessions, holding them until the full size's encoder opens. + async fn new(renditions: &Renditions, canvas: Canvas) -> anyhow::Result { + let probe = Self::open(&renditions.gpu, canvas, renditions.record).await; + let Some(sessions) = renditions.reclaim.as_ref().filter(|sessions| sessions.spare() > 0) else { + return probe; + }; + if probe.as_ref().is_ok_and(Self::hardware) { + return probe; + } + match &probe { + Ok(_) => tracing::warn!("closing the sources' replay encoders for the program's hardware encoder"), + Err(err) => tracing::warn!(%err, "closing the sources' replay encoders for the program's encoder"), + } + drop(probe); + let reclaim = sessions.reclaim(); + reclaim.closed().await; + let mut probe = Self::open(&renditions.gpu, canvas, renditions.record).await?; + probe.reclaim = Some(reclaim); + Ok(probe) + } + + /// Whether the canvas size took a hardware encoder. + fn hardware(&self) -> bool { + !matches!(&self.rungs[0].config.kind, moq_video::encode::Kind::Named(name) if name == "openh264") + } + /// Open a throwaway encoder of each of `canvas`'s renditions on what `gpu` /// exports, the published sizes or, if `record`, the recording's alone, /// which also settles whether the encoder reads the program on the GPU. /// The first picks the encoder, which the other uses too. - async fn new(gpu: &Gpu, canvas: Canvas, record: bool) -> anyhow::Result { + async fn open(gpu: &Gpu, canvas: Canvas, record: bool) -> anyhow::Result { let framerate = canvas.framerate(); let size = moq_video::Size::new(canvas.width, canvas.height); let roles: Vec<(Role, moq_video::Size)> = match record { @@ -2162,15 +2215,18 @@ impl Probe { video, }); } - Ok(Self { rungs, refused }) + Ok(Self { + rungs, + refused, + reclaim: None, + }) } /// [`new`](Self::new) on a thread of its own, off the program clock, once /// the encode threads `ending` have closed their sessions. fn spawn( - gpu: Gpu, + renditions: Renditions, canvas: Canvas, - record: bool, ending: Vec>, ) -> std::io::Result>> { std::thread::Builder::new().name("probe".into()).spawn(move || { @@ -2179,7 +2235,7 @@ impl Probe { let _ = thread.join(); } let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build()?; - runtime.block_on(Self::new(&gpu, canvas, record)) + runtime.block_on(Self::new(&renditions, canvas)) }) } } @@ -2303,7 +2359,7 @@ impl Line { } if self.probing.is_none() && !self.settled { let ending = std::mem::take(&mut self.ending); - match Probe::spawn(renditions.gpu.clone(), self.canvas, renditions.record, ending) { + match Probe::spawn(renditions.clone(), self.canvas, ending) { Ok(probe) => self.probing = Some((self.canvas, probe)), Err(err) => { self.settled = true; @@ -2438,6 +2494,8 @@ struct Extras { on_demand: bool, live: bool, stats: Arc, + /// [`Renditions::reclaim`]. + reclaim: Option, /// Where each published scene's broadcast is graded, while live. graded: Option, } @@ -2527,6 +2585,7 @@ impl Extras { broadcast, timebase: catalog.timebase(), record: false, + reclaim: self.reclaim.clone(), bandwidth: self.bandwidth.clone(), on_demand: self.on_demand, live: self.live, @@ -2685,6 +2744,8 @@ enum Video { /// What an encode thread runs. struct Job { sessions: Sessions, + /// The sources' replay encoders' sessions, held until the encoder opens. + reclaim: Option, /// How its canvas's video is doing. video: Arc, /// Which of the program's renditions it is. @@ -2705,6 +2766,7 @@ struct Job { /// One rendition's encoder and track, doing what its gate decided. struct Encoder { track: Encoding, + sessions: Sessions, config: moq_video::encode::Config, joins: Joins, /// Where to report each encoder opened as its canvas's, under the @@ -2723,8 +2785,24 @@ struct Encoder { } impl Encoder { - /// Open an encoder, returning its backend's name. + /// Open an encoder. When the GPU refuses it one, the program comes first: + /// this closes the sources' replay encoders and opens it in their + /// sessions. fn open(&mut self) -> anyhow::Result<()> { + match self.try_open() { + Err(err) if self.sessions.spare() > 0 => { + tracing::warn!(err = %format!("{err:#}"), "closing the sources' replay encoders for the program's"); + let reclaim = self.sessions.reclaim(); + // Each source closes its own at its next picture, or at once + // when it delivers none. + pollster::block_on(reclaim.closed()); + self.try_open() + } + result => result, + } + } + + fn try_open(&mut self) -> anyhow::Result<()> { let mut encoder = moq_video::encode::Encoder::new(&self.config)?; // Asked of a fresh encoder, whose first picture is a keyframe anyway, so // the cut it queues costs nothing. @@ -2813,6 +2891,7 @@ impl Encoder { fn encode(job: Job, opened: Option>>, video: mpsc::Receiver