From 38b4cd2b4197ffa3eb48fcbe008fe2f2640bc9f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Dec 2026 04:19:17 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Windows=20share=20picker=20?= =?UTF-8?q?is=20a=20live=20thumbnail=20grid:=20xcap=20lists=20screens=20an?= =?UTF-8?q?d=20windows=20without=20cloaked/tool/own=20junk,=20rescans=20ev?= =?UTF-8?q?ery=201.5s,=20thumbnails=20refresh=20within=20a=20100ms=20budge?= =?UTF-8?q?t=20per=20tick=20and=20are=20cached=20per=20window,=20rows=20vi?= =?UTF-8?q?rtualized.=20=F0=9F=90=9B=20fix:=20window=20share=20follows=20r?= =?UTF-8?q?esizes=20(encoders=20reopen=20once=20the=20new=20size=20holds?= =?UTF-8?q?=20300ms);=20share=20button=20ignores=20clicks=20while=20a=20st?= =?UTF-8?q?art/stop=20is=20in=20flight,=20so=20Linux=20no=20longer=20stack?= =?UTF-8?q?s=20portals?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 215 ++++++++++++++++++++- crates/client/Cargo.toml | 1 + crates/client/src/gui.rs | 2 + crates/client/src/gui/share_view.rs | 198 +++++++++++++++---- crates/client/src/share/capture.rs | 4 +- crates/client/src/share/capture_windows.rs | 84 +++++--- crates/client/src/share/encode.rs | 32 ++- crates/client/src/share/mod.rs | 8 +- crates/client/src/share/session.rs | 2 +- crates/client/src/share/unsupported.rs | 4 +- 10 files changed, 478 insertions(+), 72 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9f355d7..517bdf5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -400,8 +400,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b60a01f4fbcba6a522ec4d285893bd0ce36f5987a8b3bf636e6aeb2d2acdf8ac" dependencies = [ "bitflags 2.13.2", + "futures-channel", "futures-util", "getrandom 0.4.3", + "pipewire", "serde", "serde_repr", "tokio", @@ -1314,6 +1316,7 @@ dependencies = [ "wasapi", "windows-capture 2.0.1", "winreg", + "xcap", ] [[package]] @@ -2029,6 +2032,7 @@ checksum = "1e0e367e4e7da84520dedcac1901e4da967309406d1e51017ae1abfb97adbd38" dependencies = [ "bitflags 2.13.2", "block2 0.6.2", + "libc", "objc2 0.6.5", ] @@ -2073,6 +2077,61 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8b14ccef22fc6f5a8f4d7d768562a182c04ce9a3b3157b91390b52ddfdf1a76" +[[package]] +name = "drm" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80bc8c5c6c2941f70a55c15f8d9f00f9710ebda3ffda98075f996a0e6c92756f" +dependencies = [ + "bitflags 2.13.2", + "bytemuck", + "drm-ffi", + "drm-fourcc", + "libc", + "rustix 0.38.44", +] + +[[package]] +name = "drm" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a41816e58f47f49acfd956651055ddcf137c4882c2098c30c448817af21183a" +dependencies = [ + "bitflags 2.13.2", + "bytemuck", + "bytemuck_derive", + "drm-ffi", + "drm-fourcc", + "libc", + "rustix 1.1.6", +] + +[[package]] +name = "drm-ffi" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51a91c9b32ac4e8105dec255e849e0d66e27d7c34d184364fb93e469db08f690" +dependencies = [ + "drm-sys", + "rustix 1.1.6", +] + +[[package]] +name = "drm-fourcc" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0aafbcdb8afc29c1a7ee5fbe53b5d62f4565b35a042a662ca9fecd0b54dae6f4" + +[[package]] +name = "drm-sys" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecc8e1361066d91f5ffccff060a3c3be9c3ecde15be2959c1937595f7a82a9f8" +dependencies = [ + "libc", + "linux-raw-sys 0.9.4", +] + [[package]] name = "dunce" version = "1.0.5" @@ -3006,6 +3065,30 @@ dependencies = [ "slab", ] +[[package]] +name = "gbm" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce852e998d3ca5e4a97014fb31c940dc5ef344ec7d364984525fd11e8a547e6a" +dependencies = [ + "bitflags 2.13.2", + "drm 0.14.1", + "drm-fourcc", + "gbm-sys", + "libc", + "wayland-backend", + "wayland-server", +] + +[[package]] +name = "gbm-sys" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c13a5f2acc785d8fb6bf6b7ab6bfb0ef5dad4f4d97e8e70bb8e470722312f76f" +dependencies = [ + "libc", +] + [[package]] name = "generator" version = "0.8.12" @@ -3117,6 +3200,15 @@ version = "0.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1033caf0b349c518623b5396bfb2cf0bddf44f0306d543a250e5743297aafd10" +[[package]] +name = "gl" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a94edab108827d67608095e269cf862e60d920f144a5026d3dbcfd8b877fb404" +dependencies = [ + "gl_generator", +] + [[package]] name = "gl_generator" version = "0.14.0" @@ -3343,7 +3435,7 @@ dependencies = [ "parking", "parking_lot", "pin-project", - "pollster", + "pollster 0.4.0", "profiling", "rand 0.9.5", "raw-window-handle", @@ -3699,7 +3791,7 @@ dependencies = [ "parking", "parking_lot", "pin-project", - "pollster", + "pollster 0.4.0", "profiling", "rand 0.9.5", "raw-window-handle", @@ -5229,6 +5321,27 @@ dependencies = [ "system-deps", ] +[[package]] +name = "libwayshot-xcap" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea8a46e4d016ef464e386e4970f5415fe0939c2d63abe0c2f539c05ef88c5fe5" +dependencies = [ + "drm 0.15.0", + "gbm", + "gl", + "image", + "khronos-egl", + "memmap2", + "rustix 1.1.6", + "thiserror 2.0.21", + "tracing", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-protocols-wlr", +] + [[package]] name = "linebender_resource_handle" version = "0.1.1" @@ -5273,6 +5386,12 @@ version = "0.4.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" +[[package]] +name = "linux-raw-sys" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" + [[package]] name = "linux-raw-sys" version = "0.12.1" @@ -6216,6 +6335,7 @@ checksum = "e022c9d066895efa1345f8e33e584b9f958da2fd4cd116792e15e07e4720a807" dependencies = [ "bitflags 2.13.2", "dispatch2", + "libc", "objc2 0.6.5", "objc2-core-foundation", "objc2-io-surface", @@ -6243,6 +6363,21 @@ dependencies = [ "objc2-foundation 0.3.2", ] +[[package]] +name = "objc2-core-media" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05ec576860167a15dd9fce7fbee7512beb4e31f532159d3482d1f9c6caedf31d" +dependencies = [ + "bitflags 2.13.2", + "dispatch2", + "objc2 0.6.5", + "objc2-core-audio", + "objc2-core-audio-types", + "objc2-core-foundation", + "objc2-core-video", +] + [[package]] name = "objc2-core-services" version = "0.3.2" @@ -6383,6 +6518,22 @@ dependencies = [ "objc2-metal 0.3.2", ] +[[package]] +name = "objc2-screen-capture-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74b7c5390f477482f001bc354d6571a70db7e4f8d5288e860c45521fbce11394" +dependencies = [ + "block2 0.6.2", + "dispatch2", + "libc", + "objc2 0.6.5", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-core-media", + "objc2-foundation 0.3.2", +] + [[package]] name = "objc2-security" version = "0.3.2" @@ -6897,6 +7048,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f3a9f18d041e6d0e102a0a46750538147e5e8992d3b4873aaafee2520b00ce3" +[[package]] +name = "pollster" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e9b9cf600136f5b976fbc5727d58c26e1ecab8ee23088d44ef1c7af793550ab" + [[package]] name = "polycool" version = "0.4.0" @@ -10150,6 +10307,19 @@ dependencies = [ "quote", ] +[[package]] +name = "wayland-server" +version = "0.31.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dde9c29be0f723a573977de51ee455bf3dfa03652730a74f9dd3b337e374d75" +dependencies = [ + "bitflags 2.13.2", + "downcast-rs", + "rustix 1.1.6", + "wayland-backend", + "wayland-scanner", +] + [[package]] name = "wayland-sys" version = "0.31.11" @@ -10157,7 +10327,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8eab23fefc9e41f8e841df4a9c707e8a8c4ed26e944ef69297184de2785e3be" dependencies = [ "dlib", + "libc", "log", + "memoffset", "once_cell", "pkg-config", ] @@ -10420,6 +10592,12 @@ dependencies = [ "safe_arch", ] +[[package]] +name = "widestring" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72069c3113ab32ab29e5584db3c6ec55d416895e60715417b5b883a357c3e471" + [[package]] name = "winapi" version = "0.3.9" @@ -11259,6 +11437,39 @@ dependencies = [ "time", ] +[[package]] +name = "xcap" +version = "0.9.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba87463fc0a904e360fbdc53eaa97f08d133f4ec5d04052a4ec73af0979dfae4" +dependencies = [ + "ashpd 0.14.3", + "block2 0.6.2", + "dispatch2", + "image", + "libwayshot-xcap", + "log", + "objc2 0.6.5", + "objc2-app-kit 0.3.2", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-core-media", + "objc2-core-video", + "objc2-foundation 0.3.2", + "objc2-screen-capture-kit", + "percent-encoding", + "pipewire", + "pollster 1.0.3", + "rand 0.10.4", + "scopeguard", + "serde", + "thiserror 2.0.21", + "url", + "widestring", + "windows 0.62.2", + "xcb", +] + [[package]] name = "xcb" version = "1.7.1" diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index 9292b73..d04ce24 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -54,6 +54,7 @@ ashpd = { version = "0.14.3", default-features = false, features = ["tokio", "sc [target.'cfg(target_os = "windows")'.dependencies] wasapi = "0.27.0" windows-capture = "2.0.1" +xcap = { version = "0.9.11", default-features = false } tray-icon = { version = "0.27.0", default-features = false } winreg = "0.56.0" sysinfo = { version = "0.40.4", default-features = false, features = ["system"] } diff --git a/crates/client/src/gui.rs b/crates/client/src/gui.rs index a4f2c2b..979bd74 100644 --- a/crates/client/src/gui.rs +++ b/crates/client/src/gui.rs @@ -625,6 +625,7 @@ pub struct Yumush { camera_modes: Vec, camera_devices: Vec, share_more: bool, + share_busy: bool, fullscreen: bool, pumping: bool, pumped: Instant, @@ -726,6 +727,7 @@ impl Yumush { camera_modes: Vec::new(), camera_devices: Vec::new(), share_more: false, + share_busy: false, fullscreen: false, pumping: false, pumped: Instant::now(), diff --git a/crates/client/src/gui/share_view.rs b/crates/client/src/gui/share_view.rs index a141202..ebe1a32 100644 --- a/crates/client/src/gui/share_view.rs +++ b/crates/client/src/gui/share_view.rs @@ -1,5 +1,6 @@ use common::model::Id; use std::{ + collections::HashMap, sync::Arc, time::{Duration, Instant}, }; @@ -12,6 +13,17 @@ use crate::share::Target; const FULL_VOLUME: f32 = 100.; const VOLUME_WIDTH: f32 = 80.; const APPS_HEIGHT: f32 = 160.; +const PICK_COLUMNS: usize = 3; +const PICK_REFRESH: Duration = Duration::from_millis(1500); + +const PICK_ROW: f32 = size::THUMB + size::ROW; +const PREVIEW_AGE: Duration = Duration::from_secs(3); + +#[derive(Default)] +struct Picker { + sources: Vec<(Target, String)>, + previews: HashMap)>, +} const TOP_QUALITY: Quality = Quality { resolution: Resolution::P2160, @@ -19,17 +31,17 @@ const TOP_QUALITY: Quality = Quality { }; use gpui_kit::{ Anchor, AnyElement, App, AppContext, Context, Div, Entity, InteractiveElement, IntoElement, - KeyDownEvent, Keystroke, Modifiers, MouseButton, ParentElement, PlatformInput, - StatefulInteractiveElement, Styled, Subscription, Window, + KeyDownEvent, Keystroke, Modifiers, MouseButton, ObjectFit, ParentElement, PlatformInput, + RenderImage, StatefulInteractiveElement, Styled, StyledImage, Subscription, Window, assets::IconName, component::{ WindowExt, popover::Popover, slider::{SliderEvent, SliderState}, }, - div, + div, img, prelude::FluentBuilder as _, - px, + px, uniform_list, }; use super::{ @@ -150,6 +162,65 @@ pub(super) struct Viewer { _subscriptions: Vec, } +impl Picker { + fn refresh(&mut self, sources: Vec) -> Vec> { + let mut dropped = Vec::new(); + let now = Instant::now(); + self.sources = sources + .into_iter() + .map(|source| { + if let Some(preview) = source.preview { + let image = Arc::new(RenderImage::new(vec![image::Frame::new(preview)])); + if let Some((_, old)) = self.previews.insert(source.target, (now, image)) { + dropped.push(old); + } + } + (source.target, source.name) + }) + .collect(); + let alive: Vec = self.sources.iter().map(|(target, _)| *target).collect(); + self.previews.retain(|target, (_, image)| { + let keep = alive.contains(target); + if !keep { + dropped.push(image.clone()); + } + keep + }); + dropped + } +} + +fn pick_tile( + index: usize, + target: Target, + name: String, + preview: Option>, + entity: &Entity, + palette: Palette, +) -> impl IntoElement { + let entity = entity.clone(); + clickable(("share_target", index), palette, move |window, cx| { + window.close_dialog(cx); + entity.update(cx, |yumush, cx| yumush.share_to(Some(target), window, cx)); + }) + .flex_1() + .min_w_0() + .p(px(space::XS)) + .flex() + .flex_col() + .gap(px(space::XS)) + .rounded(px(radius::MD)) + .child( + div() + .h(px(size::THUMB)) + .rounded(px(radius::SM)) + .overflow_hidden() + .bg(palette.field) + .children(preview.map(|image| img(image).size_full().object_fit(ObjectFit::Contain))), + ) + .child(div().truncate().text_size(px(text::LABEL)).child(name)) +} + impl Yumush { pub(super) fn load_shares(&self, community_id: Id, cx: &mut Context) { let network = self.network.clone(); @@ -167,55 +238,101 @@ impl Yumush { } pub(super) fn toggle_share(&mut self, window: &mut Window, cx: &mut Context) { - if !self.network.sharing() - && let Some(targets) = crate::share::capture::targets() - { - self.pick_share(targets, window, cx); + if self.share_busy { + return; + } + if self.network.sharing() || !crate::share::capture::PICKS { + self.share_to(None, window, cx); return; } - self.share_to(None, window, cx); + self.pick_share(window, cx); } - fn pick_share( - &mut self, - targets: Vec<(Target, String)>, - window: &mut Window, - cx: &mut Context, - ) { + fn pick_share(&mut self, window: &mut Window, cx: &mut Context) { let entity = cx.entity(); let palette = super::palette(cx); - window.open_dialog(cx, move |dialog, _, _| { - let rows = targets.iter().enumerate().map(|(index, (target, name))| { - let target = *target; + let picker: Entity = cx.new(|_| Picker::default()); + let weak = picker.downgrade(); + window.open_dialog(cx, move |dialog, _, cx| { + let rows = picker.read(cx).sources.len().div_ceil(PICK_COLUMNS); + let list = { + let picker = picker.clone(); let entity = entity.clone(); - clickable(("share_target", index), palette, move |window, cx| { - window.close_dialog(cx); - entity.update(cx, |yumush, cx| yumush.share_to(Some(target), window, cx)); + uniform_list("share_targets", rows, move |range, _, cx| { + let picker = picker.read(cx); + range + .map(|row| { + let start = row * PICK_COLUMNS; + let end = (start + PICK_COLUMNS).min(picker.sources.len()); + let tiles = (start..end).map(|index| { + let (target, name) = &picker.sources[index]; + let preview = + picker.previews.get(target).map(|(_, image)| image.clone()); + pick_tile(index, *target, name.clone(), preview, &entity, palette) + }); + let fillers = (end..start + PICK_COLUMNS).map(|_| div().flex_1()); + div() + .h(px(PICK_ROW)) + .flex() + .gap(px(space::SM)) + .children(tiles) + .children(fillers) + }) + .collect() }) - .h(px(size::CONTROL_SM)) - .px(px(space::SM)) - .flex() - .items_center() - .rounded(px(radius::MD)) - .child(div().flex_1().min_w_0().truncate().child(name.clone())) - }); - dialog.title(w().share_view.share).w(px(size::SHEET)).child( - div() - .id("share_targets") - .max_h(px(size::POPOVER)) - .flex() - .flex_col() - .gap(px(space::XXS)) - .overflow_y_scroll() - .children(rows), - ) + .h(px((rows as f32 * PICK_ROW).min(size::PAGE))) + }; + dialog + .title(w().share_view.share) + .w(px(size::PAGE)) + .child(list) }); + cx.spawn_in(window, async move |_, cx| { + loop { + let Ok(ages) = weak.read_with(cx, |picker, _| { + picker + .previews + .iter() + .map(|(target, (taken, _))| (*target, *taken)) + .collect::>() + }) else { + return; + }; + let sources = cx + .background_executor() + .spawn(async move { + let stale = |target: Target| { + ages.get(&target) + .is_none_or(|taken| taken.elapsed() >= PREVIEW_AGE) + }; + crate::share::capture::targets(&stale).unwrap_or_default() + }) + .await; + let Some(picker) = weak.upgrade() else { + return; + }; + let alive = cx.update(|window, cx| { + let dropped = picker.update(cx, |picker, _| picker.refresh(sources)); + for image in dropped { + cx.drop_image(image, Some(window)); + } + window.refresh(); + }); + drop(picker); + if alive.is_err() { + return; + } + cx.background_executor().timer(PICK_REFRESH).await; + } + }) + .detach(); } fn share_to(&mut self, target: Option, window: &mut Window, cx: &mut Context) { let network = self.network.clone(); let sharing = network.sharing(); let settings = self.settings.share.clone(); + self.share_busy = true; cx.spawn_in(window, async move |this, cx| { if sharing { @@ -223,7 +340,10 @@ impl Yumush { } else if let Err(error) = network.start_share(&settings, target).await { notify_error(&this, cx, error); } - let _ = this.update(cx, |_, cx| cx.notify()); + let _ = this.update(cx, |yumush, cx| { + yumush.share_busy = false; + cx.notify(); + }); }) .detach(); } diff --git a/crates/client/src/share/capture.rs b/crates/client/src/share/capture.rs index 169198f..01b974b 100644 --- a/crates/client/src/share/capture.rs +++ b/crates/client/src/share/capture.rs @@ -73,7 +73,9 @@ impl Drop for Capture { } } -pub fn targets() -> Option> { +pub const PICKS: bool = false; + +pub fn targets(_: &dyn Fn(super::Target) -> bool) -> Option> { None } diff --git a/crates/client/src/share/capture_windows.rs b/crates/client/src/share/capture_windows.rs index ca34f1c..d90c8cd 100644 --- a/crates/client/src/share/capture_windows.rs +++ b/crates/client/src/share/capture_windows.rs @@ -20,11 +20,13 @@ use windows_capture::{ }; use super::{ - Target, fake, + Source, Target, fake, frame::{BYTES_PER_PIXEL, DEFAULT_REFRESH, FRAME_QUEUE, Frame, Gpu, Release, take_gpu_failed}, }; const IN_FLIGHT: usize = 3; +const PREVIEW_WIDTH: u32 = 320; +const PREVIEW_BUDGET: std::time::Duration = std::time::Duration::from_millis(100); pub struct Capture { pub width: u32, @@ -43,33 +45,65 @@ impl Drop for Capture { } } -pub fn targets() -> Option> { - let screens = Monitor::enumerate().unwrap_or_default(); +pub const PICKS: bool = true; + +pub fn targets(stale: &dyn Fn(Target) -> bool) -> Option> { + let started = Instant::now(); + let due = |target: Target| stale(target) && started.elapsed() < PREVIEW_BUDGET; + let screens = xcap::Monitor::all().unwrap_or_default(); let count = screens.len(); - let screens = screens - .into_iter() - .enumerate() - .map(move |(index, monitor)| { - let name = if count > 1 { - w().capture_windows.screen_numbered(index + 1) - } else { - w().capture_windows.screen.into() - }; - (Target::Screen(monitor.as_raw_hmonitor() as usize), name) + let mut sources = Vec::new(); + for (index, monitor) in screens.into_iter().enumerate() { + let Ok(id) = monitor.id() else { + continue; + }; + let target = Target::Screen(raw(id)); + let name = if count > 1 { + w().capture_windows.screen_numbered(index + 1) + } else { + w().capture_windows.screen.into() + }; + let preview = due(target) + .then(|| monitor.capture_image().ok().map(shrink)) + .flatten(); + sources.push(Source { + target, + name, + preview, }); - let me = std::process::id(); - let windows = Window::enumerate() - .unwrap_or_default() - .into_iter() - .filter(|window| window.process_id().is_ok_and(|id| id != me)) - .filter_map(|window| { - let title = window - .title() - .ok() - .filter(|title| !title.trim().is_empty())?; - Some((Target::Window(window.as_raw_hwnd() as usize), title)) + } + for window in xcap::Window::all().unwrap_or_default() { + let (Ok(id), Some(name)) = ( + window.id(), + window.title().ok().filter(|title| !title.trim().is_empty()), + ) else { + continue; + }; + let target = Target::Window(raw(id)); + let shown = !window.is_minimized().unwrap_or(true); + let preview = (shown && due(target)) + .then(|| window.capture_image().ok().map(shrink)) + .flatten(); + sources.push(Source { + target, + name, + preview, }); - Some(screens.chain(windows).collect()) + } + Some(sources) +} + +fn raw(id: u32) -> usize { + id as i32 as isize as usize +} + +fn shrink(image: image::RgbaImage) -> image::RgbaImage { + let height = PREVIEW_WIDTH * image.height() / image.width().max(1); + let mut image = image::imageops::thumbnail(&image, PREVIEW_WIDTH, height.max(1)); + for pixel in image.pixels_mut() { + pixel.0.swap(0, 2); + } + image } pub async fn open(target: Option) -> Result { diff --git a/crates/client/src/share/encode.rs b/crates/client/src/share/encode.rs index c24e22c..c370ea0 100644 --- a/crates/client/src/share/encode.rs +++ b/crates/client/src/share/encode.rs @@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicU8, AtomicU32, AtomicU64, Ordering}; use std::sync::mpsc::{Receiver, Sender, SyncSender, sync_channel}; use std::sync::{Arc, OnceLock}; use std::thread; -use std::time::Instant; +use std::time::{Duration, Instant}; use common::share::{Codec, Layer}; @@ -14,6 +14,7 @@ const SOFTWARE: Codec = Codec::H264; const DEFAULT_MAX_HEIGHT: u32 = 2160; const KEYFRAME_SECONDS: u32 = 3600; const SLACK_MS: u32 = 4; +const RESIZE_SETTLE: Duration = Duration::from_millis(300); const OVERLOAD: f32 = 1.2; const GAIN: f32 = 0.8; const SETTLE_FRAMES: u32 = 4; @@ -523,6 +524,7 @@ pub struct Encoders { lanes: HashMap, scale: Arc, budget: Arc, + resized: Option<((u32, u32), Instant)>, } impl Encoders { @@ -534,6 +536,7 @@ impl Encoders { lanes: HashMap::new(), scale: Arc::new(AtomicU32::new(1f32.to_bits())), budget: Arc::new(AtomicU64::new(u64::MAX)), + resized: None, } } @@ -575,12 +578,37 @@ impl Encoders { self.scale.store(scale.to_bits(), Ordering::Relaxed); } - pub fn push(&self, frame: Arc) { + pub fn push(&mut self, frame: Arc) { + self.follow(frame.width, frame.height); for lane in self.lanes.values() { let _ = lane.input.try_send(Arc::clone(&frame)); } } + fn follow(&mut self, width: u32, height: u32) { + let size = (width, height); + if size == (self.source_width, self.source_height) { + self.resized = None; + return; + } + let since = match self.resized { + Some((pending, since)) if pending == size => since, + _ => { + self.resized = Some((size, Instant::now())); + return; + } + }; + if since.elapsed() < RESIZE_SETTLE { + return; + } + tracing::info!(width, height, "share source resized"); + self.resized = None; + (self.source_width, self.source_height) = size; + let layers: Vec = self.lanes.keys().copied().collect(); + self.lanes.clear(); + self.set_layers(&layers); + } + fn spawn(&self, layer: Layer) -> Lane { let (width, height) = layer .quality diff --git a/crates/client/src/share/mod.rs b/crates/client/src/share/mod.rs index d6eb4e8..d985b56 100644 --- a/crates/client/src/share/mod.rs +++ b/crates/client/src/share/mod.rs @@ -13,11 +13,17 @@ pub mod capture; pub mod capture; pub mod congestion; -#[derive(Clone, Copy, PartialEq, Eq, Debug)] +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] pub enum Target { Screen(usize), Window(usize), } + +pub struct Source { + pub target: Target, + pub name: String, + pub preview: Option, +} pub mod encode; #[cfg(any(target_os = "linux", target_os = "windows"))] pub mod fake; diff --git a/crates/client/src/share/session.rs b/crates/client/src/share/session.rs index 9bdf03a..3bfe2d8 100644 --- a/crates/client/src/share/session.rs +++ b/crates/client/src/share/session.rs @@ -419,7 +419,7 @@ fn feed( && last.bgra == frame.bgra && frame.timestamp_ms.saturating_sub(last.timestamp_ms) < 1000 }); - let encoders = lock(&encoders); + let mut encoders = lock(&encoders); if unchanged && !encoders.keyframe_pending() { skipped += 1; } else { diff --git a/crates/client/src/share/unsupported.rs b/crates/client/src/share/unsupported.rs index 50b8e1e..372ff53 100644 --- a/crates/client/src/share/unsupported.rs +++ b/crates/client/src/share/unsupported.rs @@ -17,7 +17,9 @@ pub mod capture { pub frames: Receiver, } - pub fn targets() -> Option> { + pub const PICKS: bool = false; + + pub fn targets(_: &dyn Fn(crate::share::Target) -> bool) -> Option> { None }