BlueGit

ict rusty-teams

fix(macos): screen share picker no longer closes right away

On macOS the share picker opened and closed within milliseconds, then
showed "Screen sharing needs a desktop portal on this system". The share
path expected a delegated picker like the Wayland portal, but libwebrtc's
ScreenCaptureKit capturer has none: it opens the system picker in Start
and reports the outcome through frames.

On macOS we now keep the capturer alive and poll frames. The first frame
means a source was picked; a permanent error before that means the
picker was cancelled.

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

metis Michael Wallner · pushed by metis

Authored 2026-11-15 · 2 weeks ago

Hash
bd941974e4a4eb8a4930f971ce7062eef154d9bd
Parent
0e3132f8
1 file changed +56 −9
crates/calling/src/screen.rs +56 −9
@@ -309,6 +309,19 @@ pub fn portal_follow_up(event: PortalPickerEvent, source: Option<ShareSource>) -
309 309 }
310 310 }
311 311
312 /// What a frame result says about a system picker without a delegated
313 /// controller (macOS). Frames start once a source is picked. A cancel, or a
314 /// picker that fails to start, turns into a permanent error.
315 fn picker_event_from_frame<T>(
316 result: &std::result::Result<T, CaptureError>,
317 ) -> Option<PortalPickerEvent> {
318 match result {
319 Ok(_) => Some(PortalPickerEvent::Selected),
320 Err(CaptureError::Permanent) => Some(PortalPickerEvent::Cancelled),
321 Err(CaptureError::Temporary) => None,
322 }
323 }
324
312 325 pub(crate) fn start_portal_capture(
313 326 kind: ShareKind,
314 327 fps: Arc<AtomicU32>,
@@ -321,11 +334,29 @@ pub(crate) fn start_portal_capture(
321 334 outcome(PortalFollowUp::Notice(PORTAL_UNAVAILABLE_NOTICE));
322 335 return;
323 336 };
337 // The Wayland portal reports the pick through a delegated picker. The
338 // macOS system picker has none: it opens in `start_capture` and the
339 // outcome shows in the frame results.
340 let delegated = capturer.has_delegated_picker();
341 if !delegated && !cfg!(target_os = "macos") {
342 outcome(PortalFollowUp::Notice(PORTAL_UNAVAILABLE_NOTICE));
343 return;
344 }
345 let (sender, receiver) = std::sync::mpsc::channel();
346 let frame_events = (!delegated).then(|| sender.clone());
324 347 let selected = Arc::new(AtomicBool::new(false));
325 348 let gate = selected.clone();
326 349 capturer.start_capture(None, move |result| {
327 350 if !gate.load(Ordering::SeqCst) {
328 return;
351 let Some(events) = &frame_events else { return };
352 let Some(event) = picker_event_from_frame(&result) else {
353 return;
354 };
355 let _ = events.send(event);
356 if event != PortalPickerEvent::Selected {
357 return;
358 }
359 gate.store(true, Ordering::SeqCst);
329 360 }
330 361 let Ok(frame) = result else { return };
331 362 let (width, height) = (frame.width() as u32 & !1, frame.height() as u32 & !1);
@@ -333,19 +364,19 @@ pub(crate) fn start_portal_capture(
333 364 deliver(i420_from_bgra(frame.data(), width, height, frame.stride()));
334 365 }
335 366 });
336 if !capturer.has_delegated_picker() {
337 outcome(PortalFollowUp::Notice(PORTAL_UNAVAILABLE_NOTICE));
338 return;
367 if delegated {
368 capturer.watch_picker(move |event| {
369 let _ = sender.send(event);
370 });
371 capturer.ensure_picker_visible();
339 372 }
340 let (sender, receiver) = std::sync::mpsc::channel();
341 capturer.watch_picker(move |event| {
342 let _ = sender.send(event);
343 });
344 capturer.ensure_picker_visible();
345 373 let event = loop {
346 374 if stop.load(Ordering::SeqCst) {
347 375 return;
348 376 }
377 if !delegated {
378 capturer.capture_frame();
379 }
349 380 match receiver.recv_timeout(Duration::from_millis(40)) {
350 381 Ok(event) => break event,
351 382 Err(std::sync::mpsc::RecvTimeoutError::Timeout) => continue,
@@ -405,6 +436,22 @@ mod tests {
405 436 };
406 437 assert_eq!(window.label(), "Notes");
407 438 }
439
440 #[test]
441 fn frames_report_the_macos_picker_outcome() {
442 assert_eq!(
443 picker_event_from_frame(&Ok(())),
444 Some(PortalPickerEvent::Selected)
445 );
446 assert_eq!(
447 picker_event_from_frame::<()>(&Err(CaptureError::Permanent)),
448 Some(PortalPickerEvent::Cancelled)
449 );
450 assert_eq!(
451 picker_event_from_frame::<()>(&Err(CaptureError::Temporary)),
452 None
453 );
454 }
408 455 }
409 456
410 457 #[cfg(test)]
@@ -309,6 +309,19 @@ pub fn portal_follow_up(event: PortalPickerEvent, source: Option<ShareSource>) -
309 }
309 }
310}
310}
311
311
312/// What a frame result says about a system picker without a delegated
313/// controller (macOS). Frames start once a source is picked. A cancel, or a
314/// picker that fails to start, turns into a permanent error.
315fn picker_event_from_frame<T>(
316 result: &std::result::Result<T, CaptureError>,
317) -> Option<PortalPickerEvent> {
318 match result {
319 Ok(_) => Some(PortalPickerEvent::Selected),
320 Err(CaptureError::Permanent) => Some(PortalPickerEvent::Cancelled),
321 Err(CaptureError::Temporary) => None,
322 }
323}
324
312pub(crate) fn start_portal_capture(
325pub(crate) fn start_portal_capture(
313 kind: ShareKind,
326 kind: ShareKind,
314 fps: Arc<AtomicU32>,
327 fps: Arc<AtomicU32>,
@@ -321,11 +334,29 @@ pub(crate) fn start_portal_capture(
321 outcome(PortalFollowUp::Notice(PORTAL_UNAVAILABLE_NOTICE));
334 outcome(PortalFollowUp::Notice(PORTAL_UNAVAILABLE_NOTICE));
322 return;
335 return;
323 };
336 };
337 // The Wayland portal reports the pick through a delegated picker. The
338 // macOS system picker has none: it opens in `start_capture` and the
339 // outcome shows in the frame results.
340 let delegated = capturer.has_delegated_picker();
341 if !delegated && !cfg!(target_os = "macos") {
342 outcome(PortalFollowUp::Notice(PORTAL_UNAVAILABLE_NOTICE));
343 return;
344 }
345 let (sender, receiver) = std::sync::mpsc::channel();
346 let frame_events = (!delegated).then(|| sender.clone());
324 let selected = Arc::new(AtomicBool::new(false));
347 let selected = Arc::new(AtomicBool::new(false));
325 let gate = selected.clone();
348 let gate = selected.clone();
326 capturer.start_capture(None, move |result| {
349 capturer.start_capture(None, move |result| {
327 if !gate.load(Ordering::SeqCst) {
350 if !gate.load(Ordering::SeqCst) {
328 return;
351 let Some(events) = &frame_events else { return };
352 let Some(event) = picker_event_from_frame(&result) else {
353 return;
354 };
355 let _ = events.send(event);
356 if event != PortalPickerEvent::Selected {
357 return;
358 }
359 gate.store(true, Ordering::SeqCst);
329 }
360 }
330 let Ok(frame) = result else { return };
361 let Ok(frame) = result else { return };
331 let (width, height) = (frame.width() as u32 & !1, frame.height() as u32 & !1);
362 let (width, height) = (frame.width() as u32 & !1, frame.height() as u32 & !1);
@@ -333,19 +364,19 @@ pub(crate) fn start_portal_capture(
333 deliver(i420_from_bgra(frame.data(), width, height, frame.stride()));
364 deliver(i420_from_bgra(frame.data(), width, height, frame.stride()));
334 }
365 }
335 });
366 });
336 if !capturer.has_delegated_picker() {
367 if delegated {
337 outcome(PortalFollowUp::Notice(PORTAL_UNAVAILABLE_NOTICE));
368 capturer.watch_picker(move |event| {
338 return;
369 let _ = sender.send(event);
370 });
371 capturer.ensure_picker_visible();
339 }
372 }
340 let (sender, receiver) = std::sync::mpsc::channel();
341 capturer.watch_picker(move |event| {
342 let _ = sender.send(event);
343 });
344 capturer.ensure_picker_visible();
345 let event = loop {
373 let event = loop {
346 if stop.load(Ordering::SeqCst) {
374 if stop.load(Ordering::SeqCst) {
347 return;
375 return;
348 }
376 }
377 if !delegated {
378 capturer.capture_frame();
379 }
349 match receiver.recv_timeout(Duration::from_millis(40)) {
380 match receiver.recv_timeout(Duration::from_millis(40)) {
350 Ok(event) => break event,
381 Ok(event) => break event,
351 Err(std::sync::mpsc::RecvTimeoutError::Timeout) => continue,
382 Err(std::sync::mpsc::RecvTimeoutError::Timeout) => continue,
@@ -405,6 +436,22 @@ mod tests {
405 };
436 };
406 assert_eq!(window.label(), "Notes");
437 assert_eq!(window.label(), "Notes");
407 }
438 }
439
440 #[test]
441 fn frames_report_the_macos_picker_outcome() {
442 assert_eq!(
443 picker_event_from_frame(&Ok(())),
444 Some(PortalPickerEvent::Selected)
445 );
446 assert_eq!(
447 picker_event_from_frame::<()>(&Err(CaptureError::Permanent)),
448 Some(PortalPickerEvent::Cancelled)
449 );
450 assert_eq!(
451 picker_event_from_frame::<()>(&Err(CaptureError::Temporary)),
452 None
453 );
454 }
408}
455}
409
456
410#[cfg(test)]
457#[cfg(test)]