diff --git a/crates/voelin-media/src/studio/mod.rs b/crates/voelin-media/src/studio/mod.rs index ade7f4e..a20cd1e 100644 --- a/crates/voelin-media/src/studio/mod.rs +++ b/crates/voelin-media/src/studio/mod.rs @@ -782,7 +782,7 @@ impl Studio { graph.inputs.retain(|(id, input)| { wanted.iter().any(|(w, kind, background)| { w == id - && !restart(input.kind(), kind) + && input.kind().same_input(kind) // A background filter is set up when the input starts. && input.background().needs_mask() == background.needs_mask() }) @@ -857,17 +857,6 @@ fn no_scene(scene: u64) -> Error { Error::InvalidFrame(format!("no studio scene {scene}")) } -/// Whether a kind change means the input must be started again. A text or -/// colour source is simply drawn again, but everything else opens a device. -fn restart(old: &SourceKind, new: &SourceKind) -> bool { - match (old, new) { - // The portal hands back a token we store in the scene; that alone is - // not a reason to ask the user again. - (SourceKind::Portal { cursor: a, .. }, SourceKind::Portal { cursor: b, .. }) => a != b, - _ => old != new, - } -} - impl Shared { fn status(&self) -> Status { let live = lock(&self.live); diff --git a/crates/voelin-media/src/studio/output/whip.rs b/crates/voelin-media/src/studio/output/whip.rs index 588f766..13e5355 100644 --- a/crates/voelin-media/src/studio/output/whip.rs +++ b/crates/voelin-media/src/studio/output/whip.rs @@ -34,8 +34,9 @@ use crate::codec::Codec; use crate::studio::output::{OutputSink, Packet, Track}; use crate::{Error, Result}; -/// Packets waiting for the session thread; a full queue drops the oldest, -/// because a stalled upload must not stall an encoder. +/// Packets waiting for the session thread. A full queue drops the packet +/// (a stalled upload must not stall an encoder) and asks for a keyframe, so +/// the service's decoder recovers once the upload does. const QUEUE: usize = 256; fn error(message: impl Into) -> Error { @@ -81,8 +82,9 @@ pub struct Whip { #[derive(Default)] struct Shared { connected: AtomicBool, - /// A keyframe is wanted: at the start, when the session comes up, and - /// when the service asks (PLI/FIR). Taken by [`OutputSink::needs_keyframe`]. + /// A keyframe is wanted: at the start, when the session comes up, when + /// the service asks (PLI/FIR), and after a packet was dropped. Taken by + /// [`OutputSink::needs_keyframe`]. keyframe: AtomicBool, packets: AtomicU64, bytes: AtomicU64, @@ -273,6 +275,7 @@ impl OutputSink for Whip { Ok(()) => Ok(()), Err(TrySendError::Full(_)) => { self.shared.dropped.fetch_add(1, Ordering::Relaxed); + self.shared.keyframe.store(true, Ordering::Relaxed); Ok(()) } Err(TrySendError::Disconnected(_)) => Err(error( diff --git a/crates/voelin-media/src/studio/scene.rs b/crates/voelin-media/src/studio/scene.rs index 237fd22..78c3ee2 100644 --- a/crates/voelin-media/src/studio/scene.rs +++ b/crates/voelin-media/src/studio/scene.rs @@ -305,15 +305,18 @@ impl SourceKind { } } - /// Whether two kinds describe the same live input, so a source that - /// changes from one to the other need not restart (only its drawing - /// changes). + /// Whether a source that changes from one kind to the other keeps its + /// running input. Only what is drawn differs for a camera's mirroring, + /// and a portal's restore token is what the portal wrote back, not a + /// reason to ask the user again; any other change starts the input anew + /// (for text and colour that is just drawing them again). pub fn same_input(&self, other: &Self) -> bool { match (self, other) { - // Text and colour are redrawn, not recaptured. - (Self::Text { .. }, Self::Text { .. }) | (Self::Colour { .. }, Self::Colour { .. }) => { - true - } + (Self::Portal { cursor: a, .. }, Self::Portal { cursor: b, .. }) => a == b, + ( + Self::Camera { device: a, size: sa, fps: fa, .. }, + Self::Camera { device: b, size: sb, fps: fb, .. }, + ) => (a, sa, fa) == (b, sb, fb), _ => self == other, } } @@ -612,6 +615,33 @@ mod tests { assert!(scenes.check().unwrap_err().contains("width and height")); } + #[test] + fn which_changes_keep_the_input() { + let camera = |mirror, fps| SourceKind::Camera { + device: "/dev/video0".into(), + size: None, + fps: Some(fps), + mirror, + }; + assert!(camera(false, 30).same_input(&camera(true, 30))); + assert!(!camera(false, 30).same_input(&camera(false, 60))); + let portal = |token: Option<&str>| SourceKind::Portal { + restore_token: token.map(str::to_owned), + cursor: true, + }; + assert!(portal(None).same_input(&portal(Some("t")))); + let text = |text: &str| SourceKind::Text { + text: text.into(), + font: None, + size_px: 48.0, + colour: Colour::WHITE, + backdrop: Colour::CLEAR, + align: Align::Left, + padding: 0, + }; + assert!(!text("a").same_input(&text("b")), "new text is drawn anew"); + } + #[test] fn crop_and_labels() { assert_eq!(Crop::default().apply(100, 50), (0, 0, 100, 50)); diff --git a/crates/voelin-media/src/studio/segment.rs b/crates/voelin-media/src/studio/segment.rs index 7c996a8..59c836e 100644 --- a/crates/voelin-media/src/studio/segment.rs +++ b/crates/voelin-media/src/studio/segment.rs @@ -89,16 +89,9 @@ pub trait Segmenter: Send { /// For logs and the UI. fn name(&self) -> &'static str; - /// The size the frame should be reduced to before [`Segmenter::mask`]. - fn input_size(&self, width: u32, height: u32) -> (u32, u32) { - let width = width.clamp(1, SEGMENT_WIDTH); - let height = - (u64::from(width) * u64::from(height.max(1)) / u64::from(width.max(1)).max(1)) as u32; - (width, height.max(1)) - } - - /// Fill `mask` from `frame` (packed RGBA at the size - /// [`Segmenter::input_size`] asked for). + /// Fill `mask` from `frame`: packed RGBA, the source's picture reduced + /// to at most 256 pixels wide with its aspect ratio kept. A model with a + /// fixed input size scales it to that itself; the mask may have any size. fn mask(&mut self, frame: &VideoFrame, mask: &mut Mask) -> Result<()>; }