feat #14740: automatic call detection on windows
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+320
-38
@@ -1,10 +1,12 @@
|
||||
//! Application state shared by commands, the tray and background threads.
|
||||
//!
|
||||
//! Holds the store, the settings, the secret store and the recording that is
|
||||
//! currently running. Everything here is independent of windows and events;
|
||||
//! the command layer turns results into UI updates.
|
||||
//! Holds the store, the settings, the secret store, the recording that is
|
||||
//! currently running, the call detector and the call awaiting a decision in
|
||||
//! the end-of-call prompt. Everything here is independent of windows and
|
||||
//! events; the command layer turns results into UI updates.
|
||||
|
||||
use std::fs;
|
||||
use std::io::ErrorKind;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Mutex, MutexGuard};
|
||||
@@ -12,14 +14,17 @@ use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use chrono::{DateTime, Local};
|
||||
use recorder_core::capture::OutputTarget;
|
||||
use recorder_core::model::{CallId, CallStatus};
|
||||
use recorder_core::capture::{CaptureSource, OutputTarget};
|
||||
use recorder_core::detect::{CallDetector, DetectorEvent, MicSession};
|
||||
use recorder_core::model::{Call, CallId, CallStatus};
|
||||
use recorder_core::recorder::Recorder;
|
||||
use recorder_core::secrets::SecretStore;
|
||||
use recorder_core::settings::Settings;
|
||||
use recorder_core::store::Store;
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::detection::detector_config;
|
||||
|
||||
/// `app` value of calls started from the tray or UI instead of detection.
|
||||
const MANUAL_APP: &str = "Manuell";
|
||||
/// How long [`AppState::test_devices`] records.
|
||||
@@ -53,10 +58,46 @@ pub struct DeviceLevels {
|
||||
pub out: f32,
|
||||
}
|
||||
|
||||
/// What the end-of-call prompt shows about a call.
|
||||
#[derive(Serialize, Clone, Debug, PartialEq)]
|
||||
pub struct CallBrief {
|
||||
pub id: CallId,
|
||||
pub app: String,
|
||||
pub started_at: DateTime<Local>,
|
||||
pub ended_at: Option<DateTime<Local>>,
|
||||
pub duration_secs: i64,
|
||||
}
|
||||
|
||||
impl CallBrief {
|
||||
fn of(call: &Call) -> Self {
|
||||
Self {
|
||||
id: call.id,
|
||||
app: call.app.clone(),
|
||||
started_at: call.started_at,
|
||||
ended_at: call.ended_at,
|
||||
duration_secs: call
|
||||
.ended_at
|
||||
.map_or(0, |end| (end - call.started_at).num_seconds().max(0)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A recording that was just stopped or discarded.
|
||||
#[derive(Debug)]
|
||||
pub struct Ended {
|
||||
pub call_id: CallId,
|
||||
/// Started by call detection rather than by hand.
|
||||
pub detected: bool,
|
||||
/// Whether stopping (finalizing the files and the row) or discarding
|
||||
/// worked.
|
||||
pub result: Result<(), String>,
|
||||
}
|
||||
|
||||
struct CurrentRecording {
|
||||
call_id: CallId,
|
||||
app: String,
|
||||
started_at: DateTime<Local>,
|
||||
detected: bool,
|
||||
recorder: Recorder,
|
||||
}
|
||||
|
||||
@@ -67,6 +108,10 @@ pub struct AppState {
|
||||
settings_path: PathBuf,
|
||||
recordings_dir: PathBuf,
|
||||
current: Mutex<Option<CurrentRecording>>,
|
||||
detector: Mutex<CallDetector>,
|
||||
/// The last stopped call the end-of-call prompt asks about, until it is
|
||||
/// decided (discarded or sent to transcription).
|
||||
pending_prompt: Mutex<Option<CallId>>,
|
||||
/// Set while a recording starts or the devices are tested, so neither
|
||||
/// runs twice and `current` is never locked for longer than a moment.
|
||||
devices_busy: AtomicBool,
|
||||
@@ -89,6 +134,7 @@ impl AppState {
|
||||
recordings_dir: PathBuf,
|
||||
) -> Self {
|
||||
let settings = Settings::load(&settings_path);
|
||||
let detector = CallDetector::new(detector_config(&settings));
|
||||
Self {
|
||||
store,
|
||||
secrets,
|
||||
@@ -96,6 +142,8 @@ impl AppState {
|
||||
settings_path,
|
||||
recordings_dir,
|
||||
current: Mutex::new(None),
|
||||
detector: Mutex::new(detector),
|
||||
pending_prompt: Mutex::new(None),
|
||||
devices_busy: AtomicBool::new(false),
|
||||
}
|
||||
}
|
||||
@@ -108,10 +156,29 @@ impl AppState {
|
||||
settings
|
||||
.save(&self.settings_path)
|
||||
.map_err(|e| format!("Einstellungen konnten nicht gespeichert werden: {e}"))?;
|
||||
lock(&self.detector).set_config(detector_config(&settings));
|
||||
*lock(&self.settings) = settings;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Advances the call detector by one poll.
|
||||
pub fn detector_tick(&self, now: Instant, sessions: &[MicSession]) -> Option<DetectorEvent> {
|
||||
lock(&self.detector).tick(now, sessions)
|
||||
}
|
||||
|
||||
/// Ends the detector's current call without an event and keeps it from
|
||||
/// detecting the same app again until that app releases the mic.
|
||||
pub fn suppress_detected_call(&self) {
|
||||
lock(&self.detector).manual_stop();
|
||||
}
|
||||
|
||||
/// Whether a recording started by hand (not by detection) is running.
|
||||
pub fn manual_recording_running(&self) -> bool {
|
||||
lock(&self.current)
|
||||
.as_ref()
|
||||
.is_some_and(|cur| !cur.detected)
|
||||
}
|
||||
|
||||
pub fn recording_state(&self) -> RecordingState {
|
||||
match lock(&self.current).as_ref() {
|
||||
Some(cur) => RecordingState::Recording {
|
||||
@@ -136,9 +203,28 @@ impl AppState {
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates a call row and starts recording the configured mic and
|
||||
/// output device into `recordings/<id>-mic.wav` / `<id>-out.wav`.
|
||||
pub fn start_manual_recording(&self) -> Result<RecordingState, String> {
|
||||
/// Starts recording the configured mic and output device.
|
||||
pub fn start_manual_recording(&self) -> Result<CallId, String> {
|
||||
let out = recorder_audio::output_source(output_target(&self.settings()));
|
||||
self.start_recording(MANUAL_APP, false, out)
|
||||
}
|
||||
|
||||
/// Starts recording a detected call: the configured mic and the audio
|
||||
/// of the call app's process tree (or, if that cannot be captured, the
|
||||
/// default output).
|
||||
pub fn start_detected_recording(&self, app: &str, root_pid: u32) -> Result<CallId, String> {
|
||||
let out = recorder_audio::output_source_with_fallback(OutputTarget::Process { root_pid });
|
||||
self.start_recording(app, true, out)
|
||||
}
|
||||
|
||||
/// Creates a call row and records the configured mic and `out` into
|
||||
/// `recordings/<id>-mic.wav` / `<id>-out.wav`.
|
||||
fn start_recording(
|
||||
&self,
|
||||
app: &str,
|
||||
detected: bool,
|
||||
out: Box<dyn CaptureSource>,
|
||||
) -> Result<CallId, String> {
|
||||
let _claim = self.claim_devices("Es läuft bereits eine Aufnahme")?;
|
||||
let settings = self.settings();
|
||||
fs::create_dir_all(&self.recordings_dir)
|
||||
@@ -147,7 +233,7 @@ impl AppState {
|
||||
let started_at = Local::now();
|
||||
let call_id = self
|
||||
.store
|
||||
.create_call(MANUAL_APP, started_at, Path::new(""), Path::new(""))
|
||||
.create_call(app, started_at, Path::new(""), Path::new(""))
|
||||
.map_err(|e| format!("Gespräch konnte nicht angelegt werden: {e}"))?;
|
||||
let (mic_path, out_path) = self.track_paths(call_id);
|
||||
|
||||
@@ -157,7 +243,6 @@ impl AppState {
|
||||
.map_err(|e| e.to_string())
|
||||
.and_then(|()| {
|
||||
let mic = recorder_audio::mic_source(settings.mic_device.as_deref());
|
||||
let out = recorder_audio::output_source(output_target(&settings));
|
||||
Recorder::start(mic, out, &mic_path, &out_path).map_err(|e| e.to_string())
|
||||
});
|
||||
let recorder = match started {
|
||||
@@ -172,44 +257,140 @@ impl AppState {
|
||||
|
||||
*lock(&self.current) = Some(CurrentRecording {
|
||||
call_id,
|
||||
app: MANUAL_APP.to_string(),
|
||||
app: app.to_string(),
|
||||
started_at,
|
||||
detected,
|
||||
recorder,
|
||||
});
|
||||
Ok(self.recording_state())
|
||||
Ok(call_id)
|
||||
}
|
||||
|
||||
/// Stops the current recording and finalizes its row. Returns `Ok(false)`
|
||||
/// if nothing was recording. A recorder error still finishes the row and
|
||||
/// marks the call as failed.
|
||||
pub fn stop_recording(&self) -> Result<bool, String> {
|
||||
let Some(cur) = lock(&self.current).take() else {
|
||||
return Ok(false);
|
||||
};
|
||||
let result = cur.recorder.stop();
|
||||
let id = cur.call_id;
|
||||
self.store
|
||||
.finish_recording(id, Local::now(), false)
|
||||
.map_err(|e| format!("Gespräch konnte nicht gespeichert werden: {e}"))?;
|
||||
if let Err(err) = result {
|
||||
let msg = format!("Aufnahme fehlerhaft: {err}");
|
||||
if let Err(e) = self.store.set_status(id, CallStatus::Failed, Some(&msg)) {
|
||||
log::error!("could not mark call {id} as failed: {e}");
|
||||
/// Stops the current recording, if any, and finalizes its row. A
|
||||
/// recorder error still finishes the row and marks the call as failed.
|
||||
pub fn stop_recording(&self) -> Option<Ended> {
|
||||
self.stop_where(|_| true)
|
||||
}
|
||||
|
||||
/// Like [`AppState::stop_recording`], but only stops a recording that
|
||||
/// call detection started; a manual recording keeps running.
|
||||
pub fn stop_detected_recording(&self) -> Option<Ended> {
|
||||
self.stop_where(|cur| cur.detected)
|
||||
}
|
||||
|
||||
fn stop_where(&self, matches: impl Fn(&CurrentRecording) -> bool) -> Option<Ended> {
|
||||
let cur = {
|
||||
let mut current = lock(&self.current);
|
||||
if !current.as_ref().is_some_and(matches) {
|
||||
return None;
|
||||
}
|
||||
return Err(msg);
|
||||
}
|
||||
Ok(true)
|
||||
current.take()?
|
||||
};
|
||||
let id = cur.call_id;
|
||||
let recorded = cur.recorder.stop();
|
||||
let result = self
|
||||
.store
|
||||
.finish_recording(id, Local::now(), false)
|
||||
.map_err(|e| format!("Gespräch konnte nicht gespeichert werden: {e}"))
|
||||
.and_then(|()| {
|
||||
let Err(err) = recorded else {
|
||||
return Ok(());
|
||||
};
|
||||
let msg = format!("Aufnahme fehlerhaft: {err}");
|
||||
if let Err(e) = self.store.set_status(id, CallStatus::Failed, Some(&msg)) {
|
||||
log::error!("could not mark call {id} as failed: {e}");
|
||||
}
|
||||
Err(msg)
|
||||
});
|
||||
Some(Ended {
|
||||
call_id: id,
|
||||
detected: cur.detected,
|
||||
result,
|
||||
})
|
||||
}
|
||||
|
||||
/// Discards the current recording: deletes both WAVs and the call row.
|
||||
pub fn discard_current_recording(&self) -> Result<(), String> {
|
||||
let Some(cur) = lock(&self.current).take() else {
|
||||
return Ok(());
|
||||
};
|
||||
/// Discards the current recording, if any: deletes both WAVs and the
|
||||
/// call row.
|
||||
pub fn discard_current_recording(&self) -> Option<Ended> {
|
||||
let cur = lock(&self.current).take()?;
|
||||
let id = cur.call_id;
|
||||
cur.recorder.discard();
|
||||
let result = self
|
||||
.store
|
||||
.delete_call(id)
|
||||
.map_err(|e| format!("Gespräch konnte nicht gelöscht werden: {e}"));
|
||||
Some(Ended {
|
||||
call_id: id,
|
||||
detected: cur.detected,
|
||||
result,
|
||||
})
|
||||
}
|
||||
|
||||
/// Remembers `id` as the call the end-of-call prompt asks about.
|
||||
pub fn set_pending_prompt(&self, id: CallId) {
|
||||
*lock(&self.pending_prompt) = Some(id);
|
||||
}
|
||||
|
||||
/// The call the end-of-call prompt asks about, if it still exists.
|
||||
pub fn pending_prompt(&self) -> Option<CallBrief> {
|
||||
let mut pending = lock(&self.pending_prompt);
|
||||
let id = (*pending)?;
|
||||
match self.store.get_call(id) {
|
||||
Ok(Some(call)) => Some(CallBrief::of(&call)),
|
||||
Ok(None) => {
|
||||
*pending = None;
|
||||
None
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("could not load call {id} for the prompt: {e}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn call_brief(&self, id: CallId) -> Result<CallBrief, String> {
|
||||
let call = self
|
||||
.store
|
||||
.get_call(id)
|
||||
.map_err(|e| e.to_string())?
|
||||
.ok_or_else(|| "Gespräch nicht gefunden".to_string())?;
|
||||
Ok(CallBrief::of(&call))
|
||||
}
|
||||
|
||||
/// Deletes a finished call: both audio files and the row. Returns
|
||||
/// whether it was the call the end-of-call prompt asked about.
|
||||
pub fn discard_call(&self, id: CallId) -> Result<bool, String> {
|
||||
if lock(&self.current)
|
||||
.as_ref()
|
||||
.is_some_and(|cur| cur.call_id == id)
|
||||
{
|
||||
return Err("Das Gespräch wird gerade aufgenommen".to_string());
|
||||
}
|
||||
let call = self
|
||||
.store
|
||||
.get_call(id)
|
||||
.map_err(|e| e.to_string())?
|
||||
.ok_or_else(|| "Gespräch nicht gefunden".to_string())?;
|
||||
// Files first: if one cannot be deleted, the row stays so the user
|
||||
// can try again instead of leaving orphaned audio behind.
|
||||
for path in [&call.mic_path, &call.out_path] {
|
||||
if path.as_os_str().is_empty() {
|
||||
continue;
|
||||
}
|
||||
match fs::remove_file(path) {
|
||||
Ok(()) => {}
|
||||
Err(e) if e.kind() == ErrorKind::NotFound => {}
|
||||
Err(e) => return Err(format!("Audiodatei konnte nicht gelöscht werden: {e}")),
|
||||
}
|
||||
}
|
||||
self.store
|
||||
.delete_call(cur.call_id)
|
||||
.map_err(|e| format!("Gespräch konnte nicht gelöscht werden: {e}"))
|
||||
.delete_call(id)
|
||||
.map_err(|e| format!("Gespräch konnte nicht gelöscht werden: {e}"))?;
|
||||
let mut pending = lock(&self.pending_prompt);
|
||||
let was_pending = *pending == Some(id);
|
||||
if was_pending {
|
||||
*pending = None;
|
||||
}
|
||||
Ok(was_pending)
|
||||
}
|
||||
|
||||
/// Records both configured devices into a temporary directory for five
|
||||
@@ -277,3 +458,104 @@ fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
|
||||
.lock()
|
||||
.unwrap_or_else(|poisoned| poisoned.into_inner())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::TimeZone;
|
||||
use recorder_core::secrets::MemorySecrets;
|
||||
|
||||
use super::*;
|
||||
|
||||
struct Fixture {
|
||||
state: AppState,
|
||||
dir: tempfile::TempDir,
|
||||
}
|
||||
|
||||
fn fixture() -> Fixture {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let state = AppState::new(
|
||||
Store::open_in_memory().unwrap(),
|
||||
Box::new(MemorySecrets::default()),
|
||||
dir.path().join("settings.json"),
|
||||
dir.path().join("recordings"),
|
||||
);
|
||||
Fixture { state, dir }
|
||||
}
|
||||
|
||||
fn at(h: u32, m: u32, s: u32) -> DateTime<Local> {
|
||||
Local.with_ymd_and_hms(2026, 11, 5, h, m, s).unwrap()
|
||||
}
|
||||
|
||||
/// A finished call from 10:14:00 to 10:46:30 with both track files.
|
||||
fn finished_call(f: &Fixture) -> (CallId, PathBuf, PathBuf) {
|
||||
let mic = f.dir.path().join("mic.wav");
|
||||
let out = f.dir.path().join("out.wav");
|
||||
fs::write(&mic, b"mic").unwrap();
|
||||
fs::write(&out, b"out").unwrap();
|
||||
let store = &f.state.store;
|
||||
let id = store
|
||||
.create_call("Microsoft Teams", at(10, 14, 0), &mic, &out)
|
||||
.unwrap();
|
||||
store.finish_recording(id, at(10, 46, 30), false).unwrap();
|
||||
(id, mic, out)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn call_brief_has_app_times_and_duration() {
|
||||
let f = fixture();
|
||||
let (id, _, _) = finished_call(&f);
|
||||
assert_eq!(
|
||||
f.state.call_brief(id).unwrap(),
|
||||
CallBrief {
|
||||
id,
|
||||
app: "Microsoft Teams".into(),
|
||||
started_at: at(10, 14, 0),
|
||||
ended_at: Some(at(10, 46, 30)),
|
||||
duration_secs: 32 * 60 + 30,
|
||||
}
|
||||
);
|
||||
assert!(f.state.call_brief(id + 1).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_prompt_shows_the_call_until_it_is_gone() {
|
||||
let f = fixture();
|
||||
assert_eq!(f.state.pending_prompt(), None);
|
||||
let (id, _, _) = finished_call(&f);
|
||||
f.state.set_pending_prompt(id);
|
||||
assert_eq!(f.state.pending_prompt().map(|b| b.id), Some(id));
|
||||
|
||||
f.state.store.delete_call(id).unwrap();
|
||||
assert_eq!(f.state.pending_prompt(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discard_call_deletes_audio_and_row_and_clears_the_prompt() {
|
||||
let f = fixture();
|
||||
let (id, mic, out) = finished_call(&f);
|
||||
f.state.set_pending_prompt(id);
|
||||
|
||||
assert_eq!(f.state.discard_call(id), Ok(true));
|
||||
assert!(!mic.exists());
|
||||
assert!(!out.exists());
|
||||
assert_eq!(f.state.store.get_call(id).unwrap(), None);
|
||||
assert_eq!(f.state.pending_prompt(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discard_call_of_another_call_keeps_the_prompt() {
|
||||
let f = fixture();
|
||||
let (pending, _, _) = finished_call(&f);
|
||||
f.state.set_pending_prompt(pending);
|
||||
let other = f
|
||||
.state
|
||||
.store
|
||||
.create_call("MicroSIP", at(9, 0, 0), Path::new(""), Path::new(""))
|
||||
.unwrap();
|
||||
|
||||
// Missing track files are not an error.
|
||||
assert_eq!(f.state.discard_call(other), Ok(false));
|
||||
assert_eq!(f.state.store.get_call(other).unwrap(), None);
|
||||
assert_eq!(f.state.pending_prompt().map(|b| b.id), Some(pending));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user