fix(calling): end a Wayland portal share quietly after frames (#147)
Co-authored-by: Matteo Lombardi <93079933+matteolomba@users.noreply.github.com>
@@ -448,9 +448,21 @@ enum ActiveShareStop {
448
448
}
449
449
450
450
/// Classifies an error received while a portal share is active.
451
fn active_portal_error(error: CaptureError) -> Option<ActiveShareStop> {
451
///
452
/// Wayland reports a desktop stop, a closed window, and a PipeWire or compositor
453
/// failure after the first frame all as `Permanent`. Those stay quiet: the portal
454
/// does not say which one it was, and a notice would fire on every Stop sharing.
455
/// A permanent error before the first frame still shows a notice. macOS uses
456
/// `StoppedByUser` for the menu-bar stop, so a later `Permanent` there stays a
457
/// failure notice.
458
fn active_portal_error(
459
error: CaptureError,
460
delegated: bool,
461
seen_frame: bool,
462
) -> Option<ActiveShareStop> {
452
463
match error {
453
464
CaptureError::StoppedByUser => Some(ActiveShareStop::Quiet),
465
CaptureError::Permanent if delegated && seen_frame => Some(ActiveShareStop::Quiet),
454
466
CaptureError::StartFailed | CaptureError::Interrupted => {
455
467
Some(ActiveShareStop::Notice("macOS stopped the capture"))
456
468
}
@@ -492,6 +504,7 @@ pub(crate) fn start_portal_capture(
492
504
let gone_mark = gone_since.clone();
493
505
let active_end: Arc<Mutex<Option<ActiveShareStop>>> = Arc::default();
494
506
let end_mark = active_end.clone();
507
let seen_frame = Arc::new(AtomicBool::new(false));
495
508
capturer.start_capture(None, move |result| {
496
509
if !gate.load(Ordering::SeqCst) {
497
510
let Some(events) = &frame_events else { return };
@@ -506,6 +519,7 @@ pub(crate) fn start_portal_capture(
506
519
}
507
520
match result {
508
521
Ok(frame) => {
522
seen_frame.store(true, Ordering::SeqCst);
509
523
if let Ok(mut since) = gone_mark.lock() {
510
524
*since = None;
511
525
}
@@ -526,7 +540,8 @@ pub(crate) fn start_portal_capture(
526
540
}
527
541
}
528
542
Err(error) => {
529
if let Some(stop_reason) = active_portal_error(error)
543
if let Some(stop_reason) =
544
active_portal_error(error, delegated, seen_frame.load(Ordering::SeqCst))
530
545
&& let Ok(mut slot) = end_mark.lock()
531
546
{
532
547
slot.get_or_insert(stop_reason);
@@ -682,23 +697,37 @@ mod tests {
682
697
#[test]
683
698
fn active_portal_errors_separate_user_stop_from_failures() {
684
699
assert_eq!(
685
active_portal_error(CaptureError::StoppedByUser),
700
active_portal_error(CaptureError::StoppedByUser, false, false),
686
701
Some(ActiveShareStop::Quiet)
687
702
);
688
703
assert_eq!(
689
active_portal_error(CaptureError::Interrupted),
704
active_portal_error(CaptureError::Interrupted, true, true),
690
705
Some(ActiveShareStop::Notice("macOS stopped the capture"))
691
706
);
692
707
assert_eq!(
693
active_portal_error(CaptureError::StartFailed),
708
active_portal_error(CaptureError::StartFailed, true, true),
694
709
Some(ActiveShareStop::Notice("macOS stopped the capture"))
695
710
);
696
711
assert_eq!(
697
active_portal_error(CaptureError::Permanent),
712
active_portal_error(CaptureError::Permanent, true, true),
713
Some(ActiveShareStop::Quiet)
714
);
715
assert_eq!(
716
active_portal_error(CaptureError::Permanent, true, false),
717
Some(ActiveShareStop::Notice("the system stopped the capture"))
718
);
719
assert_eq!(
720
active_portal_error(CaptureError::Permanent, false, true),
698
721
Some(ActiveShareStop::Notice("the system stopped the capture"))
699
722
);
700
assert_eq!(active_portal_error(CaptureError::SourceGone), None);
701
assert_eq!(active_portal_error(CaptureError::Temporary), None);
723
assert_eq!(
724
active_portal_error(CaptureError::SourceGone, true, true),
725
None
726
);
727
assert_eq!(
728
active_portal_error(CaptureError::Temporary, true, true),
729
None
730
);
702
731
}
703
732
}
704
733
@@ -448,9 +448,21 @@ enum ActiveShareStop {448
}448
}449
449
450
/// Classifies an error received while a portal share is active.450
/// Classifies an error received while a portal share is active.451
fn active_portal_error(error: CaptureError) -> Option<ActiveShareStop> {451
///452
/// Wayland reports a desktop stop, a closed window, and a PipeWire or compositor453
/// failure after the first frame all as `Permanent`. Those stay quiet: the portal454
/// does not say which one it was, and a notice would fire on every Stop sharing.455
/// A permanent error before the first frame still shows a notice. macOS uses456
/// `StoppedByUser` for the menu-bar stop, so a later `Permanent` there stays a457
/// failure notice.458
fn active_portal_error(459
error: CaptureError,460
delegated: bool,461
seen_frame: bool,462
) -> Option<ActiveShareStop> {452
match error {463
match error {453
CaptureError::StoppedByUser => Some(ActiveShareStop::Quiet),464
CaptureError::StoppedByUser => Some(ActiveShareStop::Quiet),465
CaptureError::Permanent if delegated && seen_frame => Some(ActiveShareStop::Quiet),454
CaptureError::StartFailed | CaptureError::Interrupted => {466
CaptureError::StartFailed | CaptureError::Interrupted => {455
Some(ActiveShareStop::Notice("macOS stopped the capture"))467
Some(ActiveShareStop::Notice("macOS stopped the capture"))456
}468
}@@ -492,6 +504,7 @@ pub(crate) fn start_portal_capture(492
let gone_mark = gone_since.clone();504
let gone_mark = gone_since.clone();493
let active_end: Arc<Mutex<Option<ActiveShareStop>>> = Arc::default();505
let active_end: Arc<Mutex<Option<ActiveShareStop>>> = Arc::default();494
let end_mark = active_end.clone();506
let end_mark = active_end.clone();507
let seen_frame = Arc::new(AtomicBool::new(false));495
capturer.start_capture(None, move |result| {508
capturer.start_capture(None, move |result| {496
if !gate.load(Ordering::SeqCst) {509
if !gate.load(Ordering::SeqCst) {497
let Some(events) = &frame_events else { return };510
let Some(events) = &frame_events else { return };@@ -506,6 +519,7 @@ pub(crate) fn start_portal_capture(506
}519
}507
match result {520
match result {508
Ok(frame) => {521
Ok(frame) => {522
seen_frame.store(true, Ordering::SeqCst);509
if let Ok(mut since) = gone_mark.lock() {523
if let Ok(mut since) = gone_mark.lock() {510
*since = None;524
*since = None;511
}525
}@@ -526,7 +540,8 @@ pub(crate) fn start_portal_capture(526
}540
}527
}541
}528
Err(error) => {542
Err(error) => {529
if let Some(stop_reason) = active_portal_error(error)543
if let Some(stop_reason) =544
active_portal_error(error, delegated, seen_frame.load(Ordering::SeqCst))530
&& let Ok(mut slot) = end_mark.lock()545
&& let Ok(mut slot) = end_mark.lock()531
{546
{532
slot.get_or_insert(stop_reason);547
slot.get_or_insert(stop_reason);@@ -682,23 +697,37 @@ mod tests {682
#[test]697
#[test]683
fn active_portal_errors_separate_user_stop_from_failures() {698
fn active_portal_errors_separate_user_stop_from_failures() {684
assert_eq!(699
assert_eq!(685
active_portal_error(CaptureError::StoppedByUser),700
active_portal_error(CaptureError::StoppedByUser, false, false),686
Some(ActiveShareStop::Quiet)701
Some(ActiveShareStop::Quiet)687
);702
);688
assert_eq!(703
assert_eq!(689
active_portal_error(CaptureError::Interrupted),704
active_portal_error(CaptureError::Interrupted, true, true),690
Some(ActiveShareStop::Notice("macOS stopped the capture"))705
Some(ActiveShareStop::Notice("macOS stopped the capture"))691
);706
);692
assert_eq!(707
assert_eq!(693
active_portal_error(CaptureError::StartFailed),708
active_portal_error(CaptureError::StartFailed, true, true),694
Some(ActiveShareStop::Notice("macOS stopped the capture"))709
Some(ActiveShareStop::Notice("macOS stopped the capture"))695
);710
);696
assert_eq!(711
assert_eq!(697
active_portal_error(CaptureError::Permanent),712
active_portal_error(CaptureError::Permanent, true, true),713
Some(ActiveShareStop::Quiet)714
);715
assert_eq!(716
active_portal_error(CaptureError::Permanent, true, false),717
Some(ActiveShareStop::Notice("the system stopped the capture"))718
);719
assert_eq!(720
active_portal_error(CaptureError::Permanent, false, true),698
Some(ActiveShareStop::Notice("the system stopped the capture"))721
Some(ActiveShareStop::Notice("the system stopped the capture"))699
);722
);700
assert_eq!(active_portal_error(CaptureError::SourceGone), None);723
assert_eq!(701
assert_eq!(active_portal_error(CaptureError::Temporary), None);724
active_portal_error(CaptureError::SourceGone, true, true),725
None726
);727
assert_eq!(728
active_portal_error(CaptureError::Temporary, true, true),729
None730
);702
}731
}703
}732
}704
733