BlueGit

ict rusty-teams

fix(calls): keep the computer awake in a call, silence the far end at hang up

- a power request (Windows) or systemd-inhibit (Linux) holds system and display awake from dial to end
- hang up closes the peer and audio before the leave request, so no voices play after Call ended

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

metis Daniel Däschle · pushed by metis

Authored 2026-11-10 · 3 weeks ago

Hash
a9c981432e02130eae9b0e6accd6a9f97a654a17
Parent
a89e2fff
3 files changed +90 −8
crates/calling/src/awake.rs added +82 −0
@@ -0,0 +1,82 @@
1 /// Keeps the computer and its display awake while it lives; a call holds one from dial to hang up.
2 pub struct StayAwake {
3 #[cfg(windows)]
4 request: Option<windows::Win32::Foundation::HANDLE>,
5 #[cfg(not(windows))]
6 inhibitor: Option<std::process::Child>,
7 }
8
9 const REASON: &str = "Teams call in progress";
10
11 #[cfg(windows)]
12 mod platform {
13 use windows::Win32::Foundation::{CloseHandle, HANDLE};
14 use windows::Win32::System::Power::{POWER_REQUEST_TYPE, PowerClearRequest, PowerCreateRequest, PowerRequestDisplayRequired, PowerRequestSystemRequired, PowerSetRequest};
15 use windows::Win32::System::Threading::{POWER_REQUEST_CONTEXT_SIMPLE_STRING, REASON_CONTEXT, REASON_CONTEXT_0};
16 use windows_core::PWSTR;
17
18 pub const REQUESTS: [POWER_REQUEST_TYPE; 2] = [PowerRequestSystemRequired, PowerRequestDisplayRequired];
19
20 pub fn acquire(reason: &str) -> Option<HANDLE> {
21 let mut wide: Vec<u16> = reason.encode_utf16().chain(Some(0)).collect();
22 let context = REASON_CONTEXT { Version: 0, Flags: POWER_REQUEST_CONTEXT_SIMPLE_STRING, Reason: REASON_CONTEXT_0 { SimpleReasonString: PWSTR(wide.as_mut_ptr()) } };
23 let request = unsafe { PowerCreateRequest(&context) }.ok()?;
24 for kind in REQUESTS {
25 let _ = unsafe { PowerSetRequest(request, kind) };
26 }
27 Some(request)
28 }
29
30 pub fn release(request: HANDLE) {
31 for kind in REQUESTS {
32 let _ = unsafe { PowerClearRequest(request, kind) };
33 }
34 let _ = unsafe { CloseHandle(request) };
35 }
36 }
37
38 impl StayAwake {
39 #[cfg(windows)]
40 pub fn new() -> Self {
41 StayAwake { request: platform::acquire(REASON) }
42 }
43
44 #[cfg(not(windows))]
45 pub fn new() -> Self {
46 let inhibitor = std::process::Command::new("systemd-inhibit")
47 .args(["--what=idle:sleep", "--who=Rusty Teams", &format!("--why={REASON}"), "--mode=block", "sleep", "infinity"])
48 .stdin(std::process::Stdio::null())
49 .stdout(std::process::Stdio::null())
50 .stderr(std::process::Stdio::null())
51 .spawn()
52 .ok();
53 StayAwake { inhibitor }
54 }
55
56 pub fn holding(&self) -> bool {
57 #[cfg(windows)]
58 return self.request.is_some();
59 #[cfg(not(windows))]
60 return self.inhibitor.is_some();
61 }
62 }
63
64 impl Drop for StayAwake {
65 fn drop(&mut self) {
66 #[cfg(windows)]
67 if let Some(request) = self.request.take() {
68 platform::release(request);
69 }
70 #[cfg(not(windows))]
71 if let Some(mut inhibitor) = self.inhibitor.take() {
72 let _ = inhibitor.kill();
73 let _ = inhibitor.wait();
74 }
75 }
76 }
77
78 // SAFETY: a power request handle is a process-wide kernel object, usable and closable from any thread.
79 #[cfg(windows)]
80 unsafe impl Send for StayAwake {}
81 #[cfg(windows)]
82 unsafe impl Sync for StayAwake {}
@@ -0,0 +1,82 @@
1/// Keeps the computer and its display awake while it lives; a call holds one from dial to hang up.
2pub struct StayAwake {
3 #[cfg(windows)]
4 request: Option<windows::Win32::Foundation::HANDLE>,
5 #[cfg(not(windows))]
6 inhibitor: Option<std::process::Child>,
7}
8
9const REASON: &str = "Teams call in progress";
10
11#[cfg(windows)]
12mod platform {
13 use windows::Win32::Foundation::{CloseHandle, HANDLE};
14 use windows::Win32::System::Power::{POWER_REQUEST_TYPE, PowerClearRequest, PowerCreateRequest, PowerRequestDisplayRequired, PowerRequestSystemRequired, PowerSetRequest};
15 use windows::Win32::System::Threading::{POWER_REQUEST_CONTEXT_SIMPLE_STRING, REASON_CONTEXT, REASON_CONTEXT_0};
16 use windows_core::PWSTR;
17
18 pub const REQUESTS: [POWER_REQUEST_TYPE; 2] = [PowerRequestSystemRequired, PowerRequestDisplayRequired];
19
20 pub fn acquire(reason: &str) -> Option<HANDLE> {
21 let mut wide: Vec<u16> = reason.encode_utf16().chain(Some(0)).collect();
22 let context = REASON_CONTEXT { Version: 0, Flags: POWER_REQUEST_CONTEXT_SIMPLE_STRING, Reason: REASON_CONTEXT_0 { SimpleReasonString: PWSTR(wide.as_mut_ptr()) } };
23 let request = unsafe { PowerCreateRequest(&context) }.ok()?;
24 for kind in REQUESTS {
25 let _ = unsafe { PowerSetRequest(request, kind) };
26 }
27 Some(request)
28 }
29
30 pub fn release(request: HANDLE) {
31 for kind in REQUESTS {
32 let _ = unsafe { PowerClearRequest(request, kind) };
33 }
34 let _ = unsafe { CloseHandle(request) };
35 }
36}
37
38impl StayAwake {
39 #[cfg(windows)]
40 pub fn new() -> Self {
41 StayAwake { request: platform::acquire(REASON) }
42 }
43
44 #[cfg(not(windows))]
45 pub fn new() -> Self {
46 let inhibitor = std::process::Command::new("systemd-inhibit")
47 .args(["--what=idle:sleep", "--who=Rusty Teams", &format!("--why={REASON}"), "--mode=block", "sleep", "infinity"])
48 .stdin(std::process::Stdio::null())
49 .stdout(std::process::Stdio::null())
50 .stderr(std::process::Stdio::null())
51 .spawn()
52 .ok();
53 StayAwake { inhibitor }
54 }
55
56 pub fn holding(&self) -> bool {
57 #[cfg(windows)]
58 return self.request.is_some();
59 #[cfg(not(windows))]
60 return self.inhibitor.is_some();
61 }
62}
63
64impl Drop for StayAwake {
65 fn drop(&mut self) {
66 #[cfg(windows)]
67 if let Some(request) = self.request.take() {
68 platform::release(request);
69 }
70 #[cfg(not(windows))]
71 if let Some(mut inhibitor) = self.inhibitor.take() {
72 let _ = inhibitor.kill();
73 let _ = inhibitor.wait();
74 }
75 }
76}
77
78// SAFETY: a power request handle is a process-wide kernel object, usable and closable from any thread.
79#[cfg(windows)]
80unsafe impl Send for StayAwake {}
81#[cfg(windows)]
82unsafe impl Sync for StayAwake {}
crates/calling/src/call.rs +7 −8
@@ -456,6 +456,8 @@ impl CallEngine {
456 456 };
457 457 let _trouter_tab_stays = inner.session.hold_tab(session::App::Teams);
458 458 let timeline = Timeline::new(options.trace);
459 let stay_awake = crate::awake::StayAwake::new();
460 timeline.record("stay awake", if stay_awake.holding() { "system and display kept awake" } else { "not available" });
459 461 let mut report = CallReport::default();
460 462 let mut state = CallState::Idle;
461 463 advance(&mut state, CallSignal::Dial, &control);
@@ -3340,8 +3342,11 @@ fn sync_software_audio(
3340 3342 }
3341 3343
3342 3344 async fn tear_down(mut live: Live, inner: &Inner, timeline: &Timeline, report: &mut CallReport, cancelled: bool) {
3343 if let Some(audio) = &live.audio {
3344 audio.track.set_enabled(false);
3345 for peer in &live.peers {
3346 peer.close();
3347 }
3348 if let Some(audio) = live.audio.take() {
3349 audio.stop().await;
3345 3350 }
3346 3351 live.broker_running.store(false, Ordering::SeqCst);
3347 3352 if !live.skip_leave
@@ -3369,12 +3374,6 @@ async fn tear_down(mut live: Live, inner: &Inner, timeline: &Timeline, report: &
3369 3374 .await;
3370 3375 report.end = waited.ok().flatten();
3371 3376 }
3372 for peer in &live.peers {
3373 peer.close();
3374 }
3375 if let Some(audio) = live.audio.take() {
3376 audio.stop().await;
3377 }
3378 3377 if let Some(broker) = live.broker.take() {
3379 3378 broker.abort();
3380 3379 }
@@ -456,6 +456,8 @@ impl CallEngine {
456 };
456 };
457 let _trouter_tab_stays = inner.session.hold_tab(session::App::Teams);
457 let _trouter_tab_stays = inner.session.hold_tab(session::App::Teams);
458 let timeline = Timeline::new(options.trace);
458 let timeline = Timeline::new(options.trace);
459 let stay_awake = crate::awake::StayAwake::new();
460 timeline.record("stay awake", if stay_awake.holding() { "system and display kept awake" } else { "not available" });
459 let mut report = CallReport::default();
461 let mut report = CallReport::default();
460 let mut state = CallState::Idle;
462 let mut state = CallState::Idle;
461 advance(&mut state, CallSignal::Dial, &control);
463 advance(&mut state, CallSignal::Dial, &control);
@@ -3340,8 +3342,11 @@ fn sync_software_audio(
3340}
3342}
3341
3343
3342async fn tear_down(mut live: Live, inner: &Inner, timeline: &Timeline, report: &mut CallReport, cancelled: bool) {
3344async fn tear_down(mut live: Live, inner: &Inner, timeline: &Timeline, report: &mut CallReport, cancelled: bool) {
3343 if let Some(audio) = &live.audio {
3345 for peer in &live.peers {
3344 audio.track.set_enabled(false);
3346 peer.close();
3347 }
3348 if let Some(audio) = live.audio.take() {
3349 audio.stop().await;
3345 }
3350 }
3346 live.broker_running.store(false, Ordering::SeqCst);
3351 live.broker_running.store(false, Ordering::SeqCst);
3347 if !live.skip_leave
3352 if !live.skip_leave
@@ -3369,12 +3374,6 @@ async fn tear_down(mut live: Live, inner: &Inner, timeline: &Timeline, report: &
3369 .await;
3374 .await;
3370 report.end = waited.ok().flatten();
3375 report.end = waited.ok().flatten();
3371 }
3376 }
3372 for peer in &live.peers {
3373 peer.close();
3374 }
3375 if let Some(audio) = live.audio.take() {
3376 audio.stop().await;
3377 }
3378 if let Some(broker) = live.broker.take() {
3377 if let Some(broker) = live.broker.take() {
3379 broker.abort();
3378 broker.abort();
3380 }
3379 }
crates/calling/src/lib.rs +1 −0
@@ -1,5 +1,6 @@
1 1 pub mod audio;
2 2 pub mod audio_io;
3 pub mod awake;
3 4 pub mod background;
4 5 pub mod call;
5 6 pub mod candidates;
@@ -1,5 +1,6 @@
1pub mod audio;
1pub mod audio;
2pub mod audio_io;
2pub mod audio_io;
3pub mod awake;
3pub mod background;
4pub mod background;
4pub mod call;
5pub mod call;
5pub mod candidates;
6pub mod candidates;