voelin-media: restart a studio input only when it has to

Two rules said when a source's input restarts: the controller's own,
and SourceKind::same_input, which nothing called and which would have
kept a text source's old picture after its text changed. Now there is
one, same_input, and it also keeps a camera running when only its
mirroring changes: that is drawn by the compositor, and restarting the
camera for it meant a second of black while PipeWire renegotiated.

A WHIP output that drops a packet because its upload stalled now asks
for a keyframe, so the service's decoder recovers when the upload does
instead of showing a broken picture until somebody else asks.

The segmenter's input_size was never called (the frame path sizes the
segmenter's copy itself) and divided the height by the clamped width;
the trait now says what the segmenter is given.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
selikhovandClaude Opus 5.5 committed 2026-10-03 02:21:36 +03:00
1 parent c6049dbd2a
commit 654ee6e771
4 files changed
+48 -33

No files matched your search

+1 -12
View File
@@ -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);
@@ -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<String>) -> 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(
+37 -7
View File
@@ -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));
+3 -10
View File
@@ -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<()>;
}