fix(quality): scale after videoconvert at exact even WxH
Live medium-quality stream errored with "negotiation problem" on the host and rendered a squashed, garbled picture in the viewer. Two causes, both from inserting videoscale before videoconvert with PAR+range caps: - videoscale was scaling pipewiresrc's raw output directly. The portal source's format/memory (e.g. DMABuf) isn't something software videoscale negotiates — the original pipeline always fed pipewiresrc through videoconvert first. Move videoscale *after* videoconvert so it operates on system-memory NV12/I420. - `pixel-aspect-ratio=1/1` + a width range over-constrained negotiation and risked a non-square-PAR / distorted result. Instead compute an exact even WxH from the known source dimensions (Wayland: portal size; X11: root/window geometry), preserving aspect, and pin it fully in the caps. This is also downscale-only now — a source already at/below the target height is left native instead of upscaled. Unknown dims (rare X11 geometry failure) fall back to the height-only + square-pixel + even width-range negotiation. source_dims threaded through pipeline::spawn from both backends. Smoke test updated to mirror the new ordering (1920x1080 -> 852x480, videoscale after videoconvert) and still asserts an even sub-source width. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+43
-25
@@ -66,17 +66,22 @@ impl Drop for CaptureHandle {
|
||||
|
||||
/// Spawn the shared gst pipeline for a backend that supplies `source_args`
|
||||
/// (the video-source element + its properties, e.g. `["pipewiresrc", "fd=7",
|
||||
/// …]` or `["ximagesrc", "use-damage=false", …]`). `after_spawn` runs once,
|
||||
/// immediately after the gst child is launched — Wayland uses it to `close`
|
||||
/// the pipewire fd it leaked into the child; X11 passes a no-op.
|
||||
/// …]` or `["ximagesrc", "use-damage=false", …]`). `source_dims` is the source
|
||||
/// pixel size when the backend knows it (Wayland from the portal, X11 from
|
||||
/// root/window geometry); it lets a downscale preset compute an exact even
|
||||
/// target resolution and skip scaling when the source is already small enough.
|
||||
/// `after_spawn` runs once, immediately after the gst child is launched —
|
||||
/// Wayland uses it to `close` the pipewire fd it leaked into the child; X11
|
||||
/// passes a no-op.
|
||||
pub async fn spawn(
|
||||
opts: &HostOpts,
|
||||
quality: &EffectiveQuality,
|
||||
source_dims: Option<(u32, u32)>,
|
||||
source_args: Vec<String>,
|
||||
after_spawn: impl FnOnce(),
|
||||
) -> Result<CaptureHandle> {
|
||||
let (audio_routing, audio_device) = setup_audio(opts).await?;
|
||||
let args = build_args(&source_args, &audio_device, opts, quality);
|
||||
let args = build_args(&source_args, &audio_device, opts, quality, source_dims);
|
||||
|
||||
let mut gst_cmd = Command::new("gst-launch-1.0");
|
||||
gst_cmd
|
||||
@@ -148,6 +153,7 @@ fn build_args(
|
||||
audio_device: &str,
|
||||
opts: &HostOpts,
|
||||
quality: &EffectiveQuality,
|
||||
source_dims: Option<(u32, u32)>,
|
||||
) -> Vec<String> {
|
||||
let key_interval = (quality.framerate * 2).to_string();
|
||||
let bitrate = quality.bitrate.to_string();
|
||||
@@ -187,9 +193,38 @@ fn build_args(
|
||||
"fd=1".into(),
|
||||
];
|
||||
|
||||
// Downscale step for the quality presets. `None` = encode at native size
|
||||
// (the "Source" preset, or a source already at/below the target height — we
|
||||
// never upscale). When the source dimensions are known we pin an exact even
|
||||
// WxH preserving the source aspect; H.264 4:2:0 needs even dims, so width is
|
||||
// rounded to even and height is forced even (preset heights already are; a
|
||||
// raw --max-height override is rounded down). When dims are unknown (a rare
|
||||
// X11 geometry-read failure) we fall back to height-only + square pixels +
|
||||
// an even-stepped width range and let videoscale negotiate.
|
||||
let scale_caps: Option<String> = match quality.max_height {
|
||||
None => None,
|
||||
Some(max_h) => {
|
||||
let h = (max_h & !1).max(2);
|
||||
match source_dims {
|
||||
Some((sw, sh)) if sh > h => {
|
||||
let w = ((sw as u64 * h as u64 + sh as u64 / 2) / sh as u64) as u32;
|
||||
let w = (w & !1).max(2);
|
||||
Some(format!("{raw_format},width={w},height={h}"))
|
||||
}
|
||||
Some(_) => None, // source already <= target height
|
||||
None => Some(format!(
|
||||
"{raw_format},height={h},pixel-aspect-ratio=1/1,width=[2,8192,2]"
|
||||
)),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// video branch — videorate caps to the target fps so we don't ship at the
|
||||
// monitor's refresh rate (e.g. 180Hz) and pile up frames in the demuxer
|
||||
// queue faster than realtime.
|
||||
// queue faster than realtime. videoscale (when scaling) runs *after*
|
||||
// videoconvert so it operates on system-memory NV12/I420: scaling
|
||||
// pipewiresrc's raw output directly can hit a format/memory (e.g. DMABuf)
|
||||
// that software videoscale won't negotiate.
|
||||
args.extend(source.iter().cloned());
|
||||
args.extend([
|
||||
"!".into(),
|
||||
@@ -197,26 +232,6 @@ fn build_args(
|
||||
"!".into(),
|
||||
framerate_caps,
|
||||
"!".into(),
|
||||
]);
|
||||
// Optional downscale (quality presets). Omitted entirely for the native
|
||||
// "Source" preset. `pixel-aspect-ratio=1/1` forces a *proportional* scale:
|
||||
// screen capture always has square pixels, and without pinning PAR videoscale
|
||||
// keeps the full source width and just squashes PAR to preserve display
|
||||
// aspect (e.g. 1920x480 @ PAR 4/9 — no bandwidth win at all). With square
|
||||
// pixels fixed, width follows the source DAR. H.264 4:2:0 needs even
|
||||
// dimensions, so we pin height to an even value (preset heights already are;
|
||||
// a raw --max-height override is rounded down) and constrain width to a
|
||||
// stepped even range — verified 1920x1080→852x480 and 1366x768→1280x720.
|
||||
if let Some(h) = quality.max_height {
|
||||
let h = (h & !1).max(2);
|
||||
args.extend([
|
||||
"videoscale".into(),
|
||||
"!".into(),
|
||||
format!("video/x-raw,height={h},pixel-aspect-ratio=1/1,width=[2,8192,2]"),
|
||||
"!".into(),
|
||||
]);
|
||||
}
|
||||
args.extend([
|
||||
"queue".into(),
|
||||
"!".into(),
|
||||
"videoconvert".into(),
|
||||
@@ -224,6 +239,9 @@ fn build_args(
|
||||
raw_format.into(),
|
||||
"!".into(),
|
||||
]);
|
||||
if let Some(caps) = scale_caps {
|
||||
args.extend(["videoscale".into(), "!".into(), caps, "!".into()]);
|
||||
}
|
||||
args.extend(encoder_args);
|
||||
args.extend([
|
||||
"!".into(),
|
||||
|
||||
Reference in New Issue
Block a user