forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProactiveExternalCaptureYield.swift
More file actions
84 lines (80 loc) · 3.2 KB
/
Copy pathProactiveExternalCaptureYield.swift
File metadata and controls
84 lines (80 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import Foundation
/// Decides, once per capture tick, whether Omi must skip its periodic screen capture to
/// yield to an **external capture** in progress:
///
/// - a screenshot / screen-recording app frontmost (CleanShot, Shottr, macOS screenshot, …):
/// concurrent ScreenCaptureKit use stalls the user's capture UI for 20–60s (#6819), and
/// - an active outgoing call screen share (Zoom/Teams/Meet presenting): the same WindowServer
/// capture-arbitration contention has been observed to stop the user's share outright
/// (issue #10143).
///
/// Both conditions run the same pause/backoff state machine (`ProactiveScreenshotCaptureGate`):
/// pause every tick while the condition holds, then hold a short backoff after it clears so the
/// other app's teardown/editor UI isn't disturbed either.
/// Failure class: FC-concurrent-capture-contention.
struct ProactiveExternalCaptureYield {
private(set) var screenshotGate = ProactiveScreenshotCaptureGate()
private(set) var shareGate = ProactiveScreenshotCaptureGate()
/// True when this tick must skip capture. `isScreenShareActive` is an autoclosure so the
/// share check (a CGWindowList enumeration) is not evaluated while the screenshot gate
/// already pauses the tick.
mutating func shouldYield(
isScreenshotAppFrontmost: Bool,
isScreenShareActive: @autoclosure () -> Bool,
now: Date,
screenshotBackoffDuration: TimeInterval,
shareBackoffDuration: TimeInterval
) -> Bool {
let wasScreenshotAppFrontmost = screenshotGate.wasScreenshotAppFrontmost
switch screenshotGate.nextDecision(
isScreenshotAppFrontmost: isScreenshotAppFrontmost,
now: now,
backoffDuration: screenshotBackoffDuration
) {
case .pause:
if !wasScreenshotAppFrontmost {
log("ProactiveAssistantsPlugin: Screenshot app frontmost — pausing capture to avoid WindowServer contention")
}
return true
case .resumeIntoBackoff:
log(
"ProactiveAssistantsPlugin: Screenshot app no longer frontmost, holding backoff for \(Int(max(0, screenshotGate.backoffUntil.timeIntervalSinceNow)))s"
)
return true
case .resumeAndCapture:
log("ProactiveAssistantsPlugin: Screenshot app no longer frontmost, holding backoff for 0s")
case .continueBackoff:
return true
case .capture:
break
}
let wasScreenSharing = shareGate.wasScreenshotAppFrontmost
switch shareGate.nextDecision(
isScreenshotAppFrontmost: isScreenShareActive(),
now: now,
backoffDuration: shareBackoffDuration
) {
case .pause:
if !wasScreenSharing {
log("ProactiveAssistantsPlugin: Active screen share detected — pausing capture until the share ends")
}
return true
case .resumeIntoBackoff:
log(
"ProactiveAssistantsPlugin: Screen share ended, holding backoff for \(Int(max(0, shareGate.backoffUntil.timeIntervalSinceNow)))s"
)
return true
case .resumeAndCapture:
log("ProactiveAssistantsPlugin: Screen share ended, resuming capture")
return false
case .continueBackoff:
return true
case .capture:
return false
}
}
mutating func reset() {
screenshotGate.reset()
shareGate.reset()
}
}