BlueGit

ict rusty-teams

fix(calls): screen share sends a picture on Windows, stopping never hangs

- The capture thread pumps window messages, Windows Graphics Capture delivers its frames through them (share stayed black)
- Same for the share menu thumbnails
- Stopping a capture waits at most 2 s for its thread instead of joining forever (stop share and leave froze the app)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-11-12 · 2 weeks ago

Hash
e74d7b4fdaea62ac472a130a6ea5beb18665a6da
Parent
f441fa0c
3 files changed +60 −1
crates/calling/Cargo.toml +1 −0
@@ -41,6 +41,7 @@ windows = { version = "0.62", features = [
41 41 "Win32_System_Threading",
42 42 "Win32_System_Variant",
43 43 "Win32_UI_Shell_PropertiesSystem",
44 "Win32_UI_WindowsAndMessaging",
44 45 ] }
45 46 windows-core = "0.62"
46 47
@@ -41,6 +41,7 @@ windows = { version = "0.62", features = [
41 "Win32_System_Threading",
41 "Win32_System_Threading",
42 "Win32_System_Variant",
42 "Win32_System_Variant",
43 "Win32_UI_Shell_PropertiesSystem",
43 "Win32_UI_Shell_PropertiesSystem",
44 "Win32_UI_WindowsAndMessaging",
44] }
45] }
45windows-core = "0.62"
46windows-core = "0.62"
46
47
crates/calling/src/capture.rs +41 −1
@@ -1,9 +1,13 @@
1 1 use std::sync::Arc;
2 2 use std::sync::atomic::{AtomicBool, Ordering};
3 3 use std::thread::JoinHandle;
4 use std::time::{Duration, Instant};
4 5
5 6 use tokio::task::JoinHandle as TaskHandle;
6 7
8 const STOP_WAIT: Duration = Duration::from_secs(2);
9 const STOP_POLL: Duration = Duration::from_millis(5);
10
7 11 /// A running frame source; dropping it stops the capture.
8 12 pub enum Capture {
9 13 Thread { stop: Arc<AtomicBool>, thread: Option<JoinHandle<()>> },
@@ -24,7 +28,12 @@ impl Drop for Capture {
24 28 match self {
25 29 Capture::Thread { stop, thread } => {
26 30 stop.store(true, Ordering::SeqCst);
27 if let Some(thread) = thread.take() {
31 let Some(thread) = thread.take() else { return };
32 let started = Instant::now();
33 while !thread.is_finished() && started.elapsed() < STOP_WAIT {
34 std::thread::sleep(STOP_POLL);
35 }
36 if thread.is_finished() {
28 37 let _ = thread.join();
29 38 }
30 39 }
@@ -32,3 +41,34 @@ impl Drop for Capture {
32 41 }
33 42 }
34 43 }
44
45 #[cfg(test)]
46 mod tests {
47 use std::time::{Duration, Instant};
48
49 use super::Capture;
50
51 #[test]
52 fn a_stuck_capture_thread_does_not_hold_up_the_caller() {
53 let capture = Capture::spawn_thread("stuck", |_| loop {
54 std::thread::sleep(Duration::from_secs(60));
55 })
56 .unwrap();
57 let started = Instant::now();
58 drop(capture);
59 assert!(started.elapsed() < Duration::from_secs(5));
60 }
61
62 #[test]
63 fn a_stopped_capture_thread_is_joined() {
64 let capture = Capture::spawn_thread("quick", |stop| {
65 while !stop.load(std::sync::atomic::Ordering::SeqCst) {
66 std::thread::sleep(Duration::from_millis(5));
67 }
68 })
69 .unwrap();
70 let started = Instant::now();
71 drop(capture);
72 assert!(started.elapsed() < Duration::from_secs(1));
73 }
74 }
@@ -1,9 +1,13 @@
1use std::sync::Arc;
1use std::sync::Arc;
2use std::sync::atomic::{AtomicBool, Ordering};
2use std::sync::atomic::{AtomicBool, Ordering};
3use std::thread::JoinHandle;
3use std::thread::JoinHandle;
4use std::time::{Duration, Instant};
4
5
5use tokio::task::JoinHandle as TaskHandle;
6use tokio::task::JoinHandle as TaskHandle;
6
7
8const STOP_WAIT: Duration = Duration::from_secs(2);
9const STOP_POLL: Duration = Duration::from_millis(5);
10
7/// A running frame source; dropping it stops the capture.
11/// A running frame source; dropping it stops the capture.
8pub enum Capture {
12pub enum Capture {
9 Thread { stop: Arc<AtomicBool>, thread: Option<JoinHandle<()>> },
13 Thread { stop: Arc<AtomicBool>, thread: Option<JoinHandle<()>> },
@@ -24,7 +28,12 @@ impl Drop for Capture {
24 match self {
28 match self {
25 Capture::Thread { stop, thread } => {
29 Capture::Thread { stop, thread } => {
26 stop.store(true, Ordering::SeqCst);
30 stop.store(true, Ordering::SeqCst);
27 if let Some(thread) = thread.take() {
31 let Some(thread) = thread.take() else { return };
32 let started = Instant::now();
33 while !thread.is_finished() && started.elapsed() < STOP_WAIT {
34 std::thread::sleep(STOP_POLL);
35 }
36 if thread.is_finished() {
28 let _ = thread.join();
37 let _ = thread.join();
29 }
38 }
30 }
39 }
@@ -32,3 +41,34 @@ impl Drop for Capture {
32 }
41 }
33 }
42 }
34}
43}
44
45#[cfg(test)]
46mod tests {
47 use std::time::{Duration, Instant};
48
49 use super::Capture;
50
51 #[test]
52 fn a_stuck_capture_thread_does_not_hold_up_the_caller() {
53 let capture = Capture::spawn_thread("stuck", |_| loop {
54 std::thread::sleep(Duration::from_secs(60));
55 })
56 .unwrap();
57 let started = Instant::now();
58 drop(capture);
59 assert!(started.elapsed() < Duration::from_secs(5));
60 }
61
62 #[test]
63 fn a_stopped_capture_thread_is_joined() {
64 let capture = Capture::spawn_thread("quick", |stop| {
65 while !stop.load(std::sync::atomic::Ordering::SeqCst) {
66 std::thread::sleep(Duration::from_millis(5));
67 }
68 })
69 .unwrap();
70 let started = Instant::now();
71 drop(capture);
72 assert!(started.elapsed() < Duration::from_secs(1));
73 }
74}
crates/calling/src/screen.rs +18 −0
@@ -92,6 +92,7 @@ pub fn share_thumbnail(wanted: &ShareSource, max_width: u32, max_height: u32) ->
92 92 }
93 93 });
94 94 for _ in 0..THUMBNAIL_ATTEMPTS {
95 pump_messages();
95 96 capturer.capture_frame();
96 97 if let Some(picture) = captured.lock().ok().and_then(|mut slot| slot.take()) {
97 98 return Some(picture);
@@ -101,6 +102,22 @@ pub fn share_thumbnail(wanted: &ShareSource, max_width: u32, max_height: u32) ->
101 102 None
102 103 }
103 104
105 // Windows Graphics Capture hands frames to a DispatcherQueue on this thread; without a pump none arrive.
106 #[cfg(windows)]
107 fn pump_messages() {
108 use windows::Win32::UI::WindowsAndMessaging::{DispatchMessageW, MSG, PM_REMOVE, PeekMessageW, TranslateMessage};
109 let mut message = MSG::default();
110 unsafe {
111 while PeekMessageW(&mut message, None, 0, 0, PM_REMOVE).as_bool() {
112 let _ = TranslateMessage(&message);
113 let _ = DispatchMessageW(&message);
114 }
115 }
116 }
117
118 #[cfg(not(windows))]
119 fn pump_messages() {}
120
104 121 fn scaled_bgra(bytes: &[u8], width: u32, height: u32, stride: u32, max_width: u32, max_height: u32) -> Option<VideoPicture> {
105 122 if width == 0 || height == 0 || (stride as usize) < width as usize * 4 || bytes.len() < stride as usize * height as usize {
106 123 return None;
@@ -148,6 +165,7 @@ pub fn start_screen_capture(
148 165 }
149 166 });
150 167 while !stop.load(Ordering::SeqCst) {
168 pump_messages();
151 169 capturer.capture_frame();
152 170 std::thread::sleep(Duration::from_secs(1) / fps.load(Ordering::Relaxed).max(1));
153 171 }
@@ -92,6 +92,7 @@ pub fn share_thumbnail(wanted: &ShareSource, max_width: u32, max_height: u32) ->
92 }
92 }
93 });
93 });
94 for _ in 0..THUMBNAIL_ATTEMPTS {
94 for _ in 0..THUMBNAIL_ATTEMPTS {
95 pump_messages();
95 capturer.capture_frame();
96 capturer.capture_frame();
96 if let Some(picture) = captured.lock().ok().and_then(|mut slot| slot.take()) {
97 if let Some(picture) = captured.lock().ok().and_then(|mut slot| slot.take()) {
97 return Some(picture);
98 return Some(picture);
@@ -101,6 +102,22 @@ pub fn share_thumbnail(wanted: &ShareSource, max_width: u32, max_height: u32) ->
101 None
102 None
102}
103}
103
104
105// Windows Graphics Capture hands frames to a DispatcherQueue on this thread; without a pump none arrive.
106#[cfg(windows)]
107fn pump_messages() {
108 use windows::Win32::UI::WindowsAndMessaging::{DispatchMessageW, MSG, PM_REMOVE, PeekMessageW, TranslateMessage};
109 let mut message = MSG::default();
110 unsafe {
111 while PeekMessageW(&mut message, None, 0, 0, PM_REMOVE).as_bool() {
112 let _ = TranslateMessage(&message);
113 let _ = DispatchMessageW(&message);
114 }
115 }
116}
117
118#[cfg(not(windows))]
119fn pump_messages() {}
120
104fn scaled_bgra(bytes: &[u8], width: u32, height: u32, stride: u32, max_width: u32, max_height: u32) -> Option<VideoPicture> {
121fn scaled_bgra(bytes: &[u8], width: u32, height: u32, stride: u32, max_width: u32, max_height: u32) -> Option<VideoPicture> {
105 if width == 0 || height == 0 || (stride as usize) < width as usize * 4 || bytes.len() < stride as usize * height as usize {
122 if width == 0 || height == 0 || (stride as usize) < width as usize * 4 || bytes.len() < stride as usize * height as usize {
106 return None;
123 return None;
@@ -148,6 +165,7 @@ pub fn start_screen_capture(
148 }
165 }
149 });
166 });
150 while !stop.load(Ordering::SeqCst) {
167 while !stop.load(Ordering::SeqCst) {
168 pump_messages();
151 capturer.capture_frame();
169 capturer.capture_frame();
152 std::thread::sleep(Duration::from_secs(1) / fps.load(Ordering::Relaxed).max(1));
170 std::thread::sleep(Duration::from_secs(1) / fps.load(Ordering::Relaxed).max(1));
153 }
171 }