From 948db1451f8806729084c1d209310f35bd70918f Mon Sep 17 00:00:00 2001 From: Sahil Yeole Date: Tue, 21 Nov 2023 16:59:45 +0530 Subject: [PATCH] fix stream resolution mismatch/distortion on scaling Signed-off-by: Sahil Yeole --- libs/scrap/src/wayland/pipewire.rs | 33 +++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/libs/scrap/src/wayland/pipewire.rs b/libs/scrap/src/wayland/pipewire.rs index f7eb84537..bb7eec241 100644 --- a/libs/scrap/src/wayland/pipewire.rs +++ b/libs/scrap/src/wayland/pipewire.rs @@ -67,13 +67,23 @@ pub struct PipeWireCapturable { impl PipeWireCapturable { fn new(conn: Arc, fd: OwnedFd, stream: PwStreamInfo) -> Self { + // alternative to get screen resolution as stream.size is not always correct ex: on fractional scaling + // https://github.com/rustdesk/rustdesk/issues/6116#issuecomment-1817724244 + let res = get_res(Self { + dbus_conn: conn.clone(), + fd: fd.clone(), + path: stream.path, + source_type: stream.source_type, + position: stream.position, + size: stream.size, + }); Self { dbus_conn: conn, fd, path: stream.path, source_type: stream.source_type, position: stream.position, - size: stream.size, + size: res.unwrap_or(stream.size), } } } @@ -114,6 +124,27 @@ impl Capturable for PipeWireCapturable { } } +fn get_res(capturable:PipeWireCapturable) -> Result<(usize, usize), Box> { + let rec = PipeWireRecorder::new(capturable)?; + if let Some(sample) = rec.appsink + .try_pull_sample(gst::ClockTime::from_mseconds(300)) + { + let cap = sample + .get_caps() + .ok_or("Failed get caps")? + .get_structure(0) + .ok_or("Failed to get structure")?; + let w: i32 = cap.get_value("width")?.get_some()?; + let h: i32 = cap.get_value("height")?.get_some()?; + let w = w as usize; + let h = h as usize; + Ok((w,h)) + } + else { + Err(Box::new(GStreamerError("Error getting screen resolution".into()))) + } +} + pub struct PipeWireRecorder { buffer: Option>, buffer_cropped: Vec,