BlueGit

ict rusty-teams

fix(calling): arm noise hook only from outgoing-track sync

SetMicSettings no longer calls apply() before sync_software_audio, so
Auto cannot briefly double-run DeepFilterNet while MicChain is active.
clear() also resets the learned gate state for the next call.

metis Joachim Mild · pushed by metis

Authored 2026-11-15 · 2 weeks ago

Hash
8a38c07e4d959ee9a3386e8ee60c9fec232d1b02
Parent
17354a99
2 files changed +25 −4
crates/calling/src/call.rs +4 −3
@@ -1361,9 +1361,9 @@ async fn drive(
1361 1361 let mut software_audio: Option<SoftwareAudio> = None;
1362 1362 let mut share_sound_wanted = false;
1363 1363 let mut mic_settings = MicSettings::default();
1364 crate::noise_stage::apply(mic_settings);
1365 1364 let mut audio_disabled = false;
1366 1365 let mut remote_audio: Option<RtcAudioTrack> = None;
1366 // sync_software_audio arms/disarms the capture hook from the outgoing track state.
1367 1367 sync_software_audio(
1368 1368 &factory,
1369 1369 options,
@@ -2233,12 +2233,13 @@ async fn drive(
2233 2233 }
2234 2234 Some(CallCommand::SetMicSettings(settings)) if !audio_disabled => {
2235 2235 mic_settings = settings;
2236 crate::noise_stage::apply(mic_settings);
2236 // Let sync_software_audio be the single authority for hook arming.
2237 2237 sync_software_audio(&factory, options, &input, muted, remote_audio.clone(), &current.peer, &track, software_wanted(mic_settings, share_sound_wanted, &local_video, live.audio.as_ref()), false, &mut software_audio, mic_settings, control, timeline);
2238 2238 }
2239 2239 Some(CallCommand::SetMicSettings(settings)) => {
2240 2240 mic_settings = settings;
2241 crate::noise_stage::apply(mic_settings);
2241 // Audio is off both ways: prepare Auto if needed, keep the capture hook disarmed.
2242 crate::noise_stage::apply_for_outgoing(mic_settings, false);
2242 2243 }
2243 2244 Some(CallCommand::SelectOutput(choice)) => {
2244 2245 if live.audio.as_ref().is_some_and(AudioSetup::uses_devices) {
@@ -1361,9 +1361,9 @@ async fn drive(
1361 let mut software_audio: Option<SoftwareAudio> = None;
1361 let mut software_audio: Option<SoftwareAudio> = None;
1362 let mut share_sound_wanted = false;
1362 let mut share_sound_wanted = false;
1363 let mut mic_settings = MicSettings::default();
1363 let mut mic_settings = MicSettings::default();
1364 crate::noise_stage::apply(mic_settings);
1365 let mut audio_disabled = false;
1364 let mut audio_disabled = false;
1366 let mut remote_audio: Option<RtcAudioTrack> = None;
1365 let mut remote_audio: Option<RtcAudioTrack> = None;
1366 // sync_software_audio arms/disarms the capture hook from the outgoing track state.
1367 sync_software_audio(
1367 sync_software_audio(
1368 &factory,
1368 &factory,
1369 options,
1369 options,
@@ -2233,12 +2233,13 @@ async fn drive(
2233 }
2233 }
2234 Some(CallCommand::SetMicSettings(settings)) if !audio_disabled => {
2234 Some(CallCommand::SetMicSettings(settings)) if !audio_disabled => {
2235 mic_settings = settings;
2235 mic_settings = settings;
2236 crate::noise_stage::apply(mic_settings);
2236 // Let sync_software_audio be the single authority for hook arming.
2237 sync_software_audio(&factory, options, &input, muted, remote_audio.clone(), &current.peer, &track, software_wanted(mic_settings, share_sound_wanted, &local_video, live.audio.as_ref()), false, &mut software_audio, mic_settings, control, timeline);
2237 sync_software_audio(&factory, options, &input, muted, remote_audio.clone(), &current.peer, &track, software_wanted(mic_settings, share_sound_wanted, &local_video, live.audio.as_ref()), false, &mut software_audio, mic_settings, control, timeline);
2238 }
2238 }
2239 Some(CallCommand::SetMicSettings(settings)) => {
2239 Some(CallCommand::SetMicSettings(settings)) => {
2240 mic_settings = settings;
2240 mic_settings = settings;
2241 crate::noise_stage::apply(mic_settings);
2241 // Audio is off both ways: prepare Auto if needed, keep the capture hook disarmed.
2242 crate::noise_stage::apply_for_outgoing(mic_settings, false);
2242 }
2243 }
2243 Some(CallCommand::SelectOutput(choice)) => {
2244 Some(CallCommand::SelectOutput(choice)) => {
2244 if live.audio.as_ref().is_some_and(AudioSetup::uses_devices) {
2245 if live.audio.as_ref().is_some_and(AudioSetup::uses_devices) {
crates/calling/src/noise_stage.rs +21 −1
@@ -152,9 +152,12 @@ pub fn apply_for_outgoing(settings: MicSettings, device_track_outgoing: bool) {
152 152 install_hook(should_arm_hook(settings, device_track_outgoing));
153 153 }
154 154
155 /// Drop the capture hook on hangup so the next call starts clean.
155 /// Drop the capture hook on hangup and reset learned gate state so the next call starts clean.
156 156 pub fn clear() {
157 157 install_hook(false);
158 if let Ok(mut stage) = HOOK_STAGE.lock() {
159 *stage = NoiseStage::new();
160 }
158 161 }
159 162
160 163 pub struct NoiseStage {
@@ -473,6 +476,23 @@ mod tests {
473 476 assert!(!hook_armed());
474 477 }
475 478
479 #[test]
480 fn clear_resets_learned_gate_state() {
481 apply_for_outgoing(MicSettings::default(), true);
482 {
483 let mut stage = HOOK_STAGE.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
484 stage.noise_floor = vec![999_999.0];
485 stage.gate_hangover = vec![GATE_HANGOVER_FRAMES];
486 stage.noise_frames = vec![GATE_NOISE_LEARN_FRAMES];
487 }
488 clear();
489 assert!(!hook_armed());
490 let stage = HOOK_STAGE.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
491 assert!(stage.noise_floor.is_empty());
492 assert!(stage.gate_hangover.is_empty());
493 assert!(stage.noise_frames.is_empty());
494 }
495
476 496 #[test]
477 497 fn hook_and_mic_chain_never_arm_together() {
478 498 let auto = MicSettings::default();
@@ -152,9 +152,12 @@ pub fn apply_for_outgoing(settings: MicSettings, device_track_outgoing: bool) {
152 install_hook(should_arm_hook(settings, device_track_outgoing));
152 install_hook(should_arm_hook(settings, device_track_outgoing));
153}
153}
154
154
155/// Drop the capture hook on hangup so the next call starts clean.
155/// Drop the capture hook on hangup and reset learned gate state so the next call starts clean.
156pub fn clear() {
156pub fn clear() {
157 install_hook(false);
157 install_hook(false);
158 if let Ok(mut stage) = HOOK_STAGE.lock() {
159 *stage = NoiseStage::new();
160 }
158}
161}
159
162
160pub struct NoiseStage {
163pub struct NoiseStage {
@@ -473,6 +476,23 @@ mod tests {
473 assert!(!hook_armed());
476 assert!(!hook_armed());
474 }
477 }
475
478
479 #[test]
480 fn clear_resets_learned_gate_state() {
481 apply_for_outgoing(MicSettings::default(), true);
482 {
483 let mut stage = HOOK_STAGE.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
484 stage.noise_floor = vec![999_999.0];
485 stage.gate_hangover = vec![GATE_HANGOVER_FRAMES];
486 stage.noise_frames = vec![GATE_NOISE_LEARN_FRAMES];
487 }
488 clear();
489 assert!(!hook_armed());
490 let stage = HOOK_STAGE.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
491 assert!(stage.noise_floor.is_empty());
492 assert!(stage.gate_hangover.is_empty());
493 assert!(stage.noise_frames.is_empty());
494 }
495
476 #[test]
496 #[test]
477 fn hook_and_mic_chain_never_arm_together() {
497 fn hook_and_mic_chain_never_arm_together() {
478 let auto = MicSettings::default();
498 let auto = MicSettings::default();