feat #14740: automatic call detection on windows

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-05 15:38:55 +01:00
co-authored by Claude Opus 5.5
parent 61bf18e47f
commit d631252600
15 changed files with 1255 additions and 71 deletions
Generated
+1
View File
@@ -3225,6 +3225,7 @@ dependencies = [
"log", "log",
"recorder-core", "recorder-core",
"wasapi", "wasapi",
"windows",
] ]
[[package]] [[package]]
+6
View File
@@ -17,3 +17,9 @@ libpulse-simple-binding = "2.29"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
wasapi = "0.26" wasapi = "0.26"
# Same version as wasapi's, for the process APIs behind mic_sessions.
windows = { version = "0.62.2", features = [
"Win32_Foundation",
"Win32_System_Diagnostics_ToolHelp",
"Win32_System_Threading",
] }
+215
View File
@@ -0,0 +1,215 @@
//! Output capture that falls back from per-process to device loopback.
//!
//! Application loopback (`OutputTarget::Process`) needs Windows 10 2004+ and
//! a live process; if it cannot be opened, the call is still worth recording
//! from the default output. [`crate::Recorder`] keeps a failed track running
//! as silence instead of retrying, so the retry happens here, inside
//! `start`.
use std::sync::{Arc, Mutex, PoisonError};
use recorder_core::capture::{
CaptureError, CaptureSource, DataCallback, OutputTarget, StreamFormat,
};
/// Creates the platform source for one output target.
pub(crate) type Factory = fn(OutputTarget) -> Box<dyn CaptureSource>;
/// Tries `target`, and if that is a `Process` target that fails to start,
/// `DefaultRender`. `stop` and `failure` act on whichever source started.
pub(crate) struct FallbackSource {
target: OutputTarget,
factory: Factory,
active: Option<Box<dyn CaptureSource>>,
}
impl FallbackSource {
pub(crate) fn new(target: OutputTarget, factory: Factory) -> Self {
Self {
target,
factory,
active: None,
}
}
}
impl CaptureSource for FallbackSource {
fn start(&mut self, on_data: DataCallback) -> Result<StreamFormat, CaptureError> {
self.stop();
// A failed start consumes the callback it was given, so each attempt
// gets a forwarder to the one shared callback.
let on_data = Arc::new(Mutex::new(on_data));
let forward = |on_data: &Arc<Mutex<DataCallback>>| -> DataCallback {
let on_data = Arc::clone(on_data);
Box::new(move |samples| {
(on_data.lock().unwrap_or_else(PoisonError::into_inner))(samples)
})
};
let mut source = (self.factory)(self.target.clone());
let format = match source.start(forward(&on_data)) {
Ok(format) => format,
Err(err) => {
let OutputTarget::Process { root_pid } = self.target else {
return Err(err);
};
log::warn!(
"process loopback for pid {root_pid} failed ({err}), \
recording the default output instead"
);
source = (self.factory)(OutputTarget::DefaultRender);
source.start(forward(&on_data))?
}
};
self.active = Some(source);
Ok(format)
}
fn stop(&mut self) {
if let Some(mut source) = self.active.take() {
source.stop();
}
}
fn failure(&self) -> Option<String> {
self.active.as_ref().and_then(|source| source.failure())
}
}
#[cfg(test)]
mod tests {
use super::*;
const FORMAT: StreamFormat = StreamFormat {
rate: 48_000,
channels: 2,
};
/// Per-test record of what the fake sources saw. Tests run in parallel,
/// so each test gets its own log via a thread-local.
#[derive(Default)]
struct Log {
started: Vec<OutputTarget>,
stopped: Vec<OutputTarget>,
delivered: Vec<f32>,
}
thread_local! {
static LOG: Arc<Mutex<Log>> = Arc::new(Mutex::new(Log::default()));
}
fn log() -> Arc<Mutex<Log>> {
LOG.with(Arc::clone)
}
/// Fails to start for `Process` targets whose pid is 0 and for
/// `Device("broken")`; otherwise delivers one sample (the call count)
/// synchronously from `start` and reports a failure for
/// `Device("dying")`.
struct FakeOutput {
target: OutputTarget,
log: Arc<Mutex<Log>>,
started: bool,
}
fn fake(target: OutputTarget) -> Box<dyn CaptureSource> {
Box::new(FakeOutput {
target,
log: log(),
started: false,
})
}
impl CaptureSource for FakeOutput {
fn start(&mut self, mut on_data: DataCallback) -> Result<StreamFormat, CaptureError> {
self.log.lock().unwrap().started.push(self.target.clone());
match &self.target {
OutputTarget::Process { root_pid: 0 } => {
return Err(CaptureError::Backend("no such process".into()))
}
OutputTarget::Device(id) if id == "broken" => {
return Err(CaptureError::DeviceNotFound(id.clone()))
}
_ => {}
}
self.started = true;
on_data(&[1.0]);
Ok(FORMAT)
}
fn stop(&mut self) {
if std::mem::take(&mut self.started) {
self.log.lock().unwrap().stopped.push(self.target.clone());
}
}
fn failure(&self) -> Option<String> {
(self.started && self.target == OutputTarget::Device("dying".into()))
.then(|| "device lost".to_string())
}
}
fn collecting_callback() -> DataCallback {
let log = log();
Box::new(move |samples| log.lock().unwrap().delivered.extend_from_slice(samples))
}
#[test]
fn a_working_process_target_is_used_as_is() {
let mut source = FallbackSource::new(OutputTarget::Process { root_pid: 42 }, fake);
assert_eq!(source.start(collecting_callback()).unwrap(), FORMAT);
source.stop();
let log = log();
let log = log.lock().unwrap();
assert_eq!(log.started, vec![OutputTarget::Process { root_pid: 42 }]);
assert_eq!(log.stopped, vec![OutputTarget::Process { root_pid: 42 }]);
assert_eq!(log.delivered, vec![1.0]);
}
#[test]
fn a_failing_process_target_falls_back_to_default_render() {
let mut source = FallbackSource::new(OutputTarget::Process { root_pid: 0 }, fake);
assert_eq!(source.start(collecting_callback()).unwrap(), FORMAT);
assert_eq!(source.failure(), None);
source.stop();
let log = log();
let log = log.lock().unwrap();
assert_eq!(
log.started,
vec![
OutputTarget::Process { root_pid: 0 },
OutputTarget::DefaultRender
]
);
assert_eq!(log.stopped, vec![OutputTarget::DefaultRender]);
// The callback survives the failed first attempt.
assert_eq!(log.delivered, vec![1.0]);
}
#[test]
fn other_targets_do_not_fall_back() {
let mut source = FallbackSource::new(OutputTarget::Device("broken".into()), fake);
assert!(matches!(
source.start(collecting_callback()),
Err(CaptureError::DeviceNotFound(_))
));
assert_eq!(
log().lock().unwrap().started,
vec![OutputTarget::Device("broken".into())]
);
}
#[test]
fn failure_and_stop_act_on_the_started_source() {
let mut source = FallbackSource::new(OutputTarget::Device("dying".into()), fake);
assert_eq!(source.failure(), None);
source.start(collecting_callback()).unwrap();
assert_eq!(source.failure().as_deref(), Some("device lost"));
source.stop();
assert_eq!(source.failure(), None);
source.stop();
assert_eq!(log().lock().unwrap().stopped.len(), 1);
}
}
+23 -2
View File
@@ -2,10 +2,15 @@
//! //!
//! Implements [`recorder_core::capture::CaptureSource`] for the microphone and //! Implements [`recorder_core::capture::CaptureSource`] for the microphone and
//! for the call app's audio output: WASAPI on Windows, PulseAudio on Linux. //! for the call app's audio output: WASAPI on Windows, PulseAudio on Linux.
//! The platform-neutral trait, types and the two-track recorder live in //! Also lists the processes holding the microphone ([`mic_sessions`]), which
//! `recorder-core`. //! drives call detection on Windows. The platform-neutral trait, types, the
//! two-track recorder and the call detector live in `recorder-core`.
mod capture_thread; mod capture_thread;
mod fallback;
mod process_tree;
use recorder_core::capture::{CaptureSource, OutputTarget};
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
mod linux { mod linux {
@@ -17,6 +22,22 @@ pub use linux::capture::{list_devices, mic_source, output_source};
#[cfg(windows)] #[cfg(windows)]
mod windows { mod windows {
pub mod capture; pub mod capture;
pub mod sessions;
} }
#[cfg(windows)] #[cfg(windows)]
pub use windows::capture::{list_devices, mic_source, output_source}; pub use windows::capture::{list_devices, mic_source, output_source};
#[cfg(windows)]
pub use windows::sessions::mic_sessions;
/// Processes currently holding a microphone. Call detection is Windows-only;
/// Linux reports none.
#[cfg(target_os = "linux")]
pub fn mic_sessions() -> Vec<recorder_core::detect::MicSession> {
Vec::new()
}
/// Output capture for `target`; if `target` is a process whose loopback
/// cannot be opened, records the default output device instead.
pub fn output_source_with_fallback(target: OutputTarget) -> Box<dyn CaptureSource> {
Box::new(fallback::FallbackSource::new(target, output_source))
}
+118
View File
@@ -0,0 +1,118 @@
//! Finding the root of a multi-process app (Chrome, Edge, Teams).
//!
//! Browsers and Teams open the microphone from a child process, but the
//! call's audio is played by a sibling. Application loopback records a
//! process *tree*, so the recorder needs the top-most process of the app:
//! walk up the parent chain while the parent runs the same executable.
//!
//! The walk is a pure function over a process table so it can be tested
//! without a live system; Windows builds the table from a ToolHelp snapshot.
// Only the Windows session enumeration calls into this module.
#![cfg_attr(not(windows), allow(dead_code))]
use std::collections::{HashMap, HashSet};
/// Upper bound on parent steps, so a corrupt table (or pid reuse forming a
/// long chain) can never make the walk run away.
const MAX_STEPS: usize = 64;
/// pid -> (parent pid, executable file name).
pub(crate) type ProcessTable = HashMap<u32, (u32, String)>;
/// The top-most ancestor of `pid` reachable through parents that run the
/// same executable (compared case-insensitively). Returns `pid` itself if it
/// is unknown or its parent runs something else. Cycles (possible after pid
/// reuse) end the walk, and so does [`MAX_STEPS`].
pub(crate) fn root_pid(table: &ProcessTable, pid: u32) -> u32 {
let Some((_, exe)) = table.get(&pid) else {
return pid;
};
let mut root = pid;
let mut seen = HashSet::from([pid]);
for _ in 0..MAX_STEPS {
let parent = table[&root].0;
match table.get(&parent) {
Some((_, parent_exe))
if parent_exe.eq_ignore_ascii_case(exe) && seen.insert(parent) =>
{
root = parent;
}
_ => break,
}
}
root
}
#[cfg(test)]
mod tests {
use super::*;
fn table(entries: &[(u32, u32, &str)]) -> ProcessTable {
entries
.iter()
.map(|&(pid, parent, exe)| (pid, (parent, exe.to_string())))
.collect()
}
#[test]
fn unknown_pid_is_its_own_root() {
assert_eq!(root_pid(&ProcessTable::new(), 42), 42);
}
#[test]
fn process_started_by_another_exe_is_its_own_root() {
let t = table(&[(4, 0, "explorer.exe"), (10, 4, "MicroSIP.exe")]);
assert_eq!(root_pid(&t, 10), 10);
}
#[test]
fn walks_up_a_same_exe_chain_to_the_top() {
let t = table(&[
(4, 0, "explorer.exe"),
(100, 4, "chrome.exe"),
(200, 100, "chrome.exe"),
(300, 200, "chrome.exe"),
]);
assert_eq!(root_pid(&t, 300), 100);
assert_eq!(root_pid(&t, 200), 100);
assert_eq!(root_pid(&t, 100), 100);
}
#[test]
fn exe_comparison_ignores_case() {
let t = table(&[
(4, 0, "explorer.exe"),
(100, 4, "ms-teams.exe"),
(200, 100, "MS-Teams.EXE"),
]);
assert_eq!(root_pid(&t, 200), 100);
}
#[test]
fn stops_at_a_parent_missing_from_the_table() {
// The parent exited; its pid is no longer in the snapshot.
let t = table(&[(200, 100, "chrome.exe"), (300, 200, "chrome.exe")]);
assert_eq!(root_pid(&t, 300), 200);
}
#[test]
fn a_cycle_ends_the_walk() {
// After pid reuse two processes can name each other as parent.
let t = table(&[(100, 200, "chrome.exe"), (200, 100, "chrome.exe")]);
let root = root_pid(&t, 100);
assert!(root == 100 || root == 200, "root {root}");
let own_parent = table(&[(7, 7, "chrome.exe")]);
assert_eq!(root_pid(&own_parent, 7), 7);
}
#[test]
fn the_walk_is_capped() {
// pid n's parent is n + 1, all chrome: 1000 steps to the top.
let entries: Vec<(u32, u32, &str)> =
(1..=1000).map(|pid| (pid, pid + 1, "chrome.exe")).collect();
let t = table(&entries);
assert_eq!(root_pid(&t, 1), 1 + MAX_STEPS as u32);
}
}
+169
View File
@@ -0,0 +1,169 @@
//! Which processes hold a microphone right now (drives call detection).
//!
//! Every active capture endpoint is asked for its audio sessions
//! (`IAudioSessionManager2::GetSessionEnumerator`). A session belongs to one
//! process (`IAudioSessionControl2::GetProcessId`) and is active while one
//! of its streams runs, i.e. while the app actually records the mic. The
//! process's exe comes from `QueryFullProcessImageNameW`; its `root_pid` is
//! the top of its same-exe parent chain in a ToolHelp snapshot (see
//! [`crate::process_tree`]), which is the tree application loopback records.
use std::path::Path;
use recorder_core::detect::MicSession;
use wasapi::{deinitialize, initialize_mta, DeviceEnumerator, Direction, SessionState};
use windows::core::PWSTR;
use windows::Win32::Foundation::{CloseHandle, HANDLE};
use windows::Win32::System::Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS,
};
use windows::Win32::System::Threading::{
OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32, PROCESS_QUERY_LIMITED_INFORMATION,
};
use crate::process_tree::{root_pid, ProcessTable};
/// Sessions on every active capture endpoint, one entry per session.
///
/// Initialises COM (MTA) on the calling thread for the duration of the
/// call, so call it from a thread that is not an STA (the detection loop
/// has its own thread). Sessions of the system (pid 0) and of processes
/// that cannot be opened are skipped; enumeration errors yield fewer or no
/// sessions, never an error, because detection simply polls again.
pub fn mic_sessions() -> Vec<MicSession> {
if let Err(err) = initialize_mta().ok() {
log::debug!("mic sessions: COM initialisation failed: {err}");
return Vec::new();
}
let sessions = enumerate();
// Every COM object is dropped by now.
deinitialize();
sessions
}
/// (pid, active) of every non-system session on every active capture
/// endpoint, then resolved to exe and root pid.
fn enumerate() -> Vec<MicSession> {
let raw = capture_sessions();
if raw.is_empty() {
return Vec::new();
}
let table = process_table();
raw.into_iter()
.filter_map(|(pid, active)| {
let exe = image_name(pid)?;
Some(MicSession {
exe,
pid,
root_pid: root_pid(&table, pid),
active,
})
})
.collect()
}
fn capture_sessions() -> Vec<(u32, bool)> {
let collection = match DeviceEnumerator::new()
.and_then(|enumerator| enumerator.get_device_collection(&Direction::Capture))
{
Ok(collection) => collection,
Err(err) => {
log::debug!("mic sessions: cannot enumerate capture endpoints: {err}");
return Vec::new();
}
};
let mut sessions = Vec::new();
for device in &collection {
let Ok(device) = device else { continue };
let Ok(enumerator) = device
.get_iaudiosessionmanager()
.and_then(|manager| manager.get_audiosessionenumerator())
else {
continue;
};
let count = enumerator.get_count().unwrap_or(0);
for index in 0..count {
let Ok(control) = enumerator.get_session(index) else {
continue;
};
let (Ok(pid), Ok(state)) = (control.get_process_id(), control.get_state()) else {
continue;
};
if pid != 0 {
sessions.push((pid, state == SessionState::Active));
}
}
}
sessions
}
/// The file name of `pid`'s executable, or `None` if the process cannot be
/// opened (exited, or protected).
fn image_name(pid: u32) -> Option<String> {
// SAFETY: plain Win32 calls; the handle is closed below and the buffer
// outlives the call that fills it.
unsafe {
let handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid).ok()?;
let mut buf = [0u16; 1024];
let mut len = buf.len() as u32;
let result = QueryFullProcessImageNameW(
handle,
PROCESS_NAME_WIN32,
PWSTR(buf.as_mut_ptr()),
&mut len,
);
close(handle);
result.ok()?;
let path = String::from_utf16_lossy(&buf[..len as usize]);
Path::new(&path)
.file_name()
.map(|name| name.to_string_lossy().into_owned())
}
}
/// pid -> (parent pid, exe) of every process, from one ToolHelp snapshot.
/// Empty if the snapshot fails, which makes every session its own root.
fn process_table() -> ProcessTable {
let mut table = ProcessTable::new();
// SAFETY: plain Win32 calls; `entry.dwSize` is set as the API requires
// and the snapshot handle is closed below.
unsafe {
let snapshot = match CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) {
Ok(snapshot) => snapshot,
Err(err) => {
log::debug!("mic sessions: process snapshot failed: {err}");
return table;
}
};
let mut entry = PROCESSENTRY32W {
dwSize: std::mem::size_of::<PROCESSENTRY32W>() as u32,
..Default::default()
};
let mut next = Process32FirstW(snapshot, &mut entry);
while next.is_ok() {
let len = entry
.szExeFile
.iter()
.position(|&c| c == 0)
.unwrap_or(entry.szExeFile.len());
table.insert(
entry.th32ProcessID,
(
entry.th32ParentProcessID,
String::from_utf16_lossy(&entry.szExeFile[..len]),
),
);
next = Process32NextW(snapshot, &mut entry);
}
close(snapshot);
}
table
}
fn close(handle: HANDLE) {
// SAFETY: `handle` was returned by a successful Win32 call and is closed
// exactly once.
if let Err(err) = unsafe { CloseHandle(handle) } {
log::debug!("mic sessions: CloseHandle failed: {err}");
}
}
+129 -21
View File
@@ -1,4 +1,5 @@
//! Tauri commands and the UI side effects of recording (windows, events). //! Tauri commands and the UI side effects of recording (windows, events),
//! shared by the UI, the tray and call detection.
//! //!
//! The handlers stay thin: the work happens in [`AppState`]. Anything that //! The handlers stay thin: the work happens in [`AppState`]. Anything that
//! blocks (audio devices, stopping a recorder, the keyring) runs on the //! blocks (audio devices, stopping a recorder, the keyring) runs on the
@@ -11,15 +12,22 @@ use recorder_core::capture::Devices;
use recorder_core::model::CallId; use recorder_core::model::CallId;
use recorder_core::secrets::Service; use recorder_core::secrets::Service;
use recorder_core::settings::Settings; use recorder_core::settings::Settings;
use serde::Serialize;
use tauri::{AppHandle, Emitter, Manager, State}; use tauri::{AppHandle, Emitter, Manager, State};
use tauri_plugin_autostart::ManagerExt; use tauri_plugin_autostart::ManagerExt;
use crate::state::{AppState, DeviceLevels, RecordingState}; use crate::state::{AppState, CallBrief, DeviceLevels, Ended, RecordingState};
const LEVELS_INTERVAL: Duration = Duration::from_millis(100); const LEVELS_INTERVAL: Duration = Duration::from_millis(100);
type CmdResult<T> = Result<T, String>; type CmdResult<T> = Result<T, String>;
/// Payload of the `call-ended` event: a call awaits the prompt's decision.
#[derive(Serialize, Clone)]
struct CallEnded {
id: CallId,
}
#[tauri::command] #[tauri::command]
pub fn get_settings(state: State<'_, AppState>) -> Settings { pub fn get_settings(state: State<'_, AppState>) -> Settings {
state.settings() state.settings()
@@ -95,32 +103,122 @@ pub async fn discard_current_recording(app: AppHandle) -> CmdResult<()> {
blocking(app, discard).await blocking(app, discard).await
} }
/// The call the end-of-call prompt asks about, so the prompt can render even
/// if it missed the `call-ended` event.
#[tauri::command]
pub fn get_pending_prompt(state: State<'_, AppState>) -> Option<CallBrief> {
state.pending_prompt()
}
#[tauri::command]
pub fn get_call_brief(state: State<'_, AppState>, id: CallId) -> CmdResult<CallBrief> {
state.call_brief(id)
}
/// Deletes a finished call's audio and row; closes the prompt if it asked
/// about this call.
#[tauri::command]
pub async fn discard_call(app: AppHandle, id: CallId) -> CmdResult<()> {
blocking(app, move |app| {
if app.state::<AppState>().discard_call(id)? {
set_window_visible(app, "prompt", false);
}
Ok(())
})
.await
}
/// Placeholder until transcription exists; the prompt shows the error.
#[tauri::command]
pub fn transcribe_call(id: CallId) -> CmdResult<()> {
let _ = id;
Err("Transkription ist noch nicht verfügbar".to_string())
}
/// Starts a manual recording, shows the floating window and streams levels. /// Starts a manual recording, shows the floating window and streams levels.
pub fn start_manual(app: &AppHandle) -> CmdResult<CallId> { pub fn start_manual(app: &AppHandle) -> CmdResult<CallId> {
let state = app.state::<AppState>().start_manual_recording()?; let call_id = app.state::<AppState>().start_manual_recording()?;
let RecordingState::Recording { call_id, .. } = state else { on_started(app, call_id);
return Err("Aufnahme wurde nicht gestartet".to_string());
};
spawn_level_emitter(app.clone(), call_id);
set_window_visible(app, "recorder", true);
emit_state(app, &state);
Ok(call_id) Ok(call_id)
} }
/// Stops the current recording and hides the floating window. /// Starts recording a call that detection found (`app` is its display
pub fn stop(app: &AppHandle) -> CmdResult<()> { /// name, `root_pid` the process tree whose audio is the far end).
let result = app.state::<AppState>().stop_recording(); pub fn start_detected(app: &AppHandle, name: &str, root_pid: u32) -> CmdResult<CallId> {
set_window_visible(app, "recorder", false); let call_id = app
emit_state(app, &RecordingState::Idle); .state::<AppState>()
result.map(|_| ()) .start_detected_recording(name, root_pid)?;
on_started(app, call_id);
Ok(call_id)
} }
/// Throws the current recording away and hides the floating window. /// Stops the current recording at the user's request (■ Stopp). Stopping
/// a detected call also keeps detection from restarting it until the call
/// app releases the mic.
pub fn stop(app: &AppHandle) -> CmdResult<()> {
let state = app.state::<AppState>();
let ended = state.stop_recording();
if ended.as_ref().is_some_and(|ended| ended.detected) {
state.suppress_detected_call();
}
after_stop(app, ended)
}
/// Stops the current recording because detection saw the call end. A
/// manual recording is left running.
pub fn end_detected_call(app: &AppHandle) {
let ended = app.state::<AppState>().stop_detected_recording();
if let Err(e) = after_stop(app, ended) {
log::error!("finishing the detected call failed: {e}");
}
}
/// Throws the current recording away. Never shows the prompt; a discarded
/// detected call is not detected again until the call app releases the mic.
pub fn discard(app: &AppHandle) -> CmdResult<()> { pub fn discard(app: &AppHandle) -> CmdResult<()> {
let result = app.state::<AppState>().discard_current_recording(); let state = app.state::<AppState>();
set_window_visible(app, "recorder", false); let ended = state.discard_current_recording();
emit_state(app, &RecordingState::Idle); if ended.as_ref().is_some_and(|ended| ended.detected) {
result state.suppress_detected_call();
}
sync_recorder_window(app);
ended.map_or(Ok(()), |ended| ended.result)
}
/// Streams levels, shows the floating window and announces a recording
/// that just started.
fn on_started(app: &AppHandle, call_id: CallId) {
spawn_level_emitter(app.clone(), call_id);
set_window_visible(app, "recorder", true);
emit_state(app, &app.state::<AppState>().recording_state());
}
/// After a recording stopped: updates the floating window and, if the call
/// was saved, asks what to do with it in the prompt window.
fn after_stop(app: &AppHandle, ended: Option<Ended>) -> CmdResult<()> {
sync_recorder_window(app);
let Some(ended) = ended else {
return Ok(());
};
ended.result?;
let id = ended.call_id;
app.state::<AppState>().set_pending_prompt(id);
show_and_focus(app, "prompt");
if let Err(e) = app.emit("call-ended", CallEnded { id }) {
log::error!("could not emit call-ended: {e}");
}
Ok(())
}
/// Announces the actual recording state and hides the floating window only
/// if nothing records any more: detection may already have started the
/// next recording.
fn sync_recorder_window(app: &AppHandle) {
let state = app.state::<AppState>().recording_state();
if matches!(state, RecordingState::Idle) {
set_window_visible(app, "recorder", false);
}
emit_state(app, &state);
} }
/// Enables or disables launching at login. Debug builds leave the login /// Enables or disables launching at login. Debug builds leave the login
@@ -142,7 +240,17 @@ pub fn apply_autostart(app: &AppHandle, enabled: bool) {
} }
pub fn show_main(app: &AppHandle) { pub fn show_main(app: &AppHandle) {
if let Some(window) = app.get_webview_window("main") { show_and_focus(app, "main");
}
/// Brings the floating window to the front (tray "Aufnahme manuell
/// starten" while a recording runs).
pub fn show_recorder(app: &AppHandle) {
show_and_focus(app, "recorder");
}
fn show_and_focus(app: &AppHandle, label: &str) {
if let Some(window) = app.get_webview_window(label) {
let _ = window.unminimize(); let _ = window.unminimize();
let _ = window.show(); let _ = window.show();
let _ = window.set_focus(); let _ = window.set_focus();
+97
View File
@@ -0,0 +1,97 @@
//! Automatic call detection: once a second, asks which processes hold a
//! microphone and drives the [`CallDetector`](recorder_core::detect::CallDetector)
//! kept in [`AppState`]. A detected call is recorded until the detector
//! sees it end; then the end-of-call prompt asks what to do with it.
//!
//! Only Windows reports microphone sessions; elsewhere the loop runs but
//! never detects anything.
use std::thread;
use std::time::{Duration, Instant};
use recorder_core::detect::{DetectorConfig, DetectorEvent, MicSession};
use recorder_core::settings::Settings;
use tauri::{AppHandle, Manager};
use crate::commands;
use crate::state::AppState;
const POLL_INTERVAL: Duration = Duration::from_secs(1);
/// The detector configuration the settings ask for. The app's own process
/// is always ignored.
pub fn detector_config(settings: &Settings) -> DetectorConfig {
DetectorConfig {
allowlist: settings.allowlist.clone(),
own_pid: std::process::id(),
start_after: Duration::from_secs(settings.start_after_secs),
end_after: Duration::from_secs(settings.end_after_secs),
}
}
/// Starts the detection loop on its own thread. Session enumeration
/// initialises COM on that thread, so it must not be a UI thread.
pub fn spawn(app: AppHandle) {
let spawned = thread::Builder::new()
.name("call-detection".into())
.spawn(move || {
let mut last_sessions = String::new();
loop {
thread::sleep(POLL_INTERVAL);
poll(&app, &mut last_sessions);
}
});
if let Err(e) = spawned {
log::error!("could not start call detection: {e}");
}
}
/// One detection step. `last_sessions` is the previous [`describe`] output,
/// so the log shows each change of who holds the mic exactly once.
fn poll(app: &AppHandle, last_sessions: &mut String) {
let state = app.state::<AppState>();
// A recording started by hand owns the devices; detection waits.
if state.manual_recording_running() {
return;
}
let sessions = recorder_audio::mic_sessions();
let described = describe(&sessions);
if described != *last_sessions {
log::info!("mic sessions: [{described}]");
*last_sessions = described;
}
match state.detector_tick(Instant::now(), &sessions) {
Some(DetectorEvent::CallStarted {
exe,
app: name,
root_pid,
}) => {
log::info!("call detected: {exe} (process tree {root_pid})");
if let Err(e) = commands::start_detected(app, &name, root_pid) {
log::error!("recording the detected call failed: {e}");
// Otherwise the detector would stay in the call and never
// retry; suppressing waits for the app to release the mic.
state.suppress_detected_call();
}
}
Some(DetectorEvent::CallEnded) => {
log::info!("call ended");
commands::end_detected_call(app);
}
None => {}
}
}
/// "exe pid N root N active" per session, sorted; for diagnosing detection
/// on a user's machine from the log.
fn describe(sessions: &[MicSession]) -> String {
let mut parts: Vec<String> = sessions
.iter()
.map(|s| {
let state = if s.active { "active" } else { "inactive" };
format!("{} pid {} root {} {state}", s.exe, s.pid, s.root_pid)
})
.collect();
parts.sort();
parts.join(", ")
}
+23 -5
View File
@@ -1,4 +1,5 @@
mod commands; mod commands;
mod detection;
mod state; mod state;
mod tray; mod tray;
@@ -6,12 +7,15 @@ use recorder_core::secrets::KeyringSecrets;
use recorder_core::store::Store; use recorder_core::store::Store;
use tauri::{Manager, WindowEvent}; use tauri::{Manager, WindowEvent};
use tauri_plugin_autostart::MacosLauncher; use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_log::{RotationStrategy, Target, TargetKind, TimezoneStrategy};
use crate::state::AppState; use crate::state::AppState;
/// Command-line flag the login item starts the app with; the main window /// Command-line flag the login item starts the app with; the main window
/// then stays hidden in the tray. /// then stays hidden in the tray.
const AUTOSTART_ARG: &str = "--autostart"; const AUTOSTART_ARG: &str = "--autostart";
/// Size at which the log file is rotated; two old files are kept.
const LOG_FILE_SIZE: u128 = 1_000_000;
#[cfg_attr(mobile, tauri::mobile_entry_point)] #[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() { pub fn run() {
@@ -25,13 +29,22 @@ pub fn run() {
Some(vec![AUTOSTART_ARG]), Some(vec![AUTOSTART_ARG]),
)) ))
.setup(|app| { .setup(|app| {
// Release builds log to a file in the app log directory (on
// Windows %LOCALAPPDATA%\eu.euvino.callrecorder\logs), debug
// builds to stdout as well. Never log API keys or transcripts.
let mut targets = vec![Target::new(TargetKind::LogDir { file_name: None })];
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
app.handle().plugin( targets.push(Target::new(TargetKind::Stdout));
tauri_plugin_log::Builder::default()
.level(log::LevelFilter::Info)
.build(),
)?;
} }
app.handle().plugin(
tauri_plugin_log::Builder::new()
.targets(targets)
.level(log::LevelFilter::Info)
.max_file_size(LOG_FILE_SIZE)
.rotation_strategy(RotationStrategy::KeepSome(2))
.timezone_strategy(TimezoneStrategy::UseLocal)
.build(),
)?;
let data_dir = app.path().app_data_dir()?; let data_dir = app.path().app_data_dir()?;
let config_dir = app.path().app_config_dir()?; let config_dir = app.path().app_config_dir()?;
@@ -51,6 +64,7 @@ pub fn run() {
commands::apply_autostart(app.handle(), autostart); commands::apply_autostart(app.handle(), autostart);
tray::create(app.handle())?; tray::create(app.handle())?;
detection::spawn(app.handle().clone());
if !std::env::args().any(|arg| arg == AUTOSTART_ARG) { if !std::env::args().any(|arg| arg == AUTOSTART_ARG) {
commands::show_main(app.handle()); commands::show_main(app.handle());
} }
@@ -75,6 +89,10 @@ pub fn run() {
commands::start_manual_recording, commands::start_manual_recording,
commands::stop_recording, commands::stop_recording,
commands::discard_current_recording, commands::discard_current_recording,
commands::get_pending_prompt,
commands::get_call_brief,
commands::discard_call,
commands::transcribe_call,
]) ])
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while building tauri application"); .expect("error while building tauri application");
+320 -38
View File
@@ -1,10 +1,12 @@
//! Application state shared by commands, the tray and background threads. //! Application state shared by commands, the tray and background threads.
//! //!
//! Holds the store, the settings, the secret store and the recording that is //! Holds the store, the settings, the secret store, the recording that is
//! currently running. Everything here is independent of windows and events; //! currently running, the call detector and the call awaiting a decision in
//! the command layer turns results into UI updates. //! 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::fs;
use std::io::ErrorKind;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Mutex, MutexGuard}; use std::sync::{Mutex, MutexGuard};
@@ -12,14 +14,17 @@ use std::thread;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};
use recorder_core::capture::OutputTarget; use recorder_core::capture::{CaptureSource, OutputTarget};
use recorder_core::model::{CallId, CallStatus}; use recorder_core::detect::{CallDetector, DetectorEvent, MicSession};
use recorder_core::model::{Call, CallId, CallStatus};
use recorder_core::recorder::Recorder; use recorder_core::recorder::Recorder;
use recorder_core::secrets::SecretStore; use recorder_core::secrets::SecretStore;
use recorder_core::settings::Settings; use recorder_core::settings::Settings;
use recorder_core::store::Store; use recorder_core::store::Store;
use serde::Serialize; use serde::Serialize;
use crate::detection::detector_config;
/// `app` value of calls started from the tray or UI instead of detection. /// `app` value of calls started from the tray or UI instead of detection.
const MANUAL_APP: &str = "Manuell"; const MANUAL_APP: &str = "Manuell";
/// How long [`AppState::test_devices`] records. /// How long [`AppState::test_devices`] records.
@@ -53,10 +58,46 @@ pub struct DeviceLevels {
pub out: f32, 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 { struct CurrentRecording {
call_id: CallId, call_id: CallId,
app: String, app: String,
started_at: DateTime<Local>, started_at: DateTime<Local>,
detected: bool,
recorder: Recorder, recorder: Recorder,
} }
@@ -67,6 +108,10 @@ pub struct AppState {
settings_path: PathBuf, settings_path: PathBuf,
recordings_dir: PathBuf, recordings_dir: PathBuf,
current: Mutex<Option<CurrentRecording>>, 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 /// Set while a recording starts or the devices are tested, so neither
/// runs twice and `current` is never locked for longer than a moment. /// runs twice and `current` is never locked for longer than a moment.
devices_busy: AtomicBool, devices_busy: AtomicBool,
@@ -89,6 +134,7 @@ impl AppState {
recordings_dir: PathBuf, recordings_dir: PathBuf,
) -> Self { ) -> Self {
let settings = Settings::load(&settings_path); let settings = Settings::load(&settings_path);
let detector = CallDetector::new(detector_config(&settings));
Self { Self {
store, store,
secrets, secrets,
@@ -96,6 +142,8 @@ impl AppState {
settings_path, settings_path,
recordings_dir, recordings_dir,
current: Mutex::new(None), current: Mutex::new(None),
detector: Mutex::new(detector),
pending_prompt: Mutex::new(None),
devices_busy: AtomicBool::new(false), devices_busy: AtomicBool::new(false),
} }
} }
@@ -108,10 +156,29 @@ impl AppState {
settings settings
.save(&self.settings_path) .save(&self.settings_path)
.map_err(|e| format!("Einstellungen konnten nicht gespeichert werden: {e}"))?; .map_err(|e| format!("Einstellungen konnten nicht gespeichert werden: {e}"))?;
lock(&self.detector).set_config(detector_config(&settings));
*lock(&self.settings) = settings; *lock(&self.settings) = settings;
Ok(()) 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 { pub fn recording_state(&self) -> RecordingState {
match lock(&self.current).as_ref() { match lock(&self.current).as_ref() {
Some(cur) => RecordingState::Recording { Some(cur) => RecordingState::Recording {
@@ -136,9 +203,28 @@ impl AppState {
}) })
} }
/// Creates a call row and starts recording the configured mic and /// Starts recording the configured mic and output device.
/// output device into `recordings/<id>-mic.wav` / `<id>-out.wav`. pub fn start_manual_recording(&self) -> Result<CallId, String> {
pub fn start_manual_recording(&self) -> Result<RecordingState, 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 _claim = self.claim_devices("Es läuft bereits eine Aufnahme")?;
let settings = self.settings(); let settings = self.settings();
fs::create_dir_all(&self.recordings_dir) fs::create_dir_all(&self.recordings_dir)
@@ -147,7 +233,7 @@ impl AppState {
let started_at = Local::now(); let started_at = Local::now();
let call_id = self let call_id = self
.store .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}"))?; .map_err(|e| format!("Gespräch konnte nicht angelegt werden: {e}"))?;
let (mic_path, out_path) = self.track_paths(call_id); let (mic_path, out_path) = self.track_paths(call_id);
@@ -157,7 +243,6 @@ impl AppState {
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
.and_then(|()| { .and_then(|()| {
let mic = recorder_audio::mic_source(settings.mic_device.as_deref()); 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()) Recorder::start(mic, out, &mic_path, &out_path).map_err(|e| e.to_string())
}); });
let recorder = match started { let recorder = match started {
@@ -172,44 +257,140 @@ impl AppState {
*lock(&self.current) = Some(CurrentRecording { *lock(&self.current) = Some(CurrentRecording {
call_id, call_id,
app: MANUAL_APP.to_string(), app: app.to_string(),
started_at, started_at,
detected,
recorder, recorder,
}); });
Ok(self.recording_state()) Ok(call_id)
} }
/// Stops the current recording and finalizes its row. Returns `Ok(false)` /// Stops the current recording, if any, and finalizes its row. A
/// if nothing was recording. A recorder error still finishes the row and /// recorder error still finishes the row and marks the call as failed.
/// marks the call as failed. pub fn stop_recording(&self) -> Option<Ended> {
pub fn stop_recording(&self) -> Result<bool, String> { self.stop_where(|_| true)
let Some(cur) = lock(&self.current).take() else { }
return Ok(false);
}; /// Like [`AppState::stop_recording`], but only stops a recording that
let result = cur.recorder.stop(); /// call detection started; a manual recording keeps running.
let id = cur.call_id; pub fn stop_detected_recording(&self) -> Option<Ended> {
self.store self.stop_where(|cur| cur.detected)
.finish_recording(id, Local::now(), false) }
.map_err(|e| format!("Gespräch konnte nicht gespeichert werden: {e}"))?;
if let Err(err) = result { fn stop_where(&self, matches: impl Fn(&CurrentRecording) -> bool) -> Option<Ended> {
let msg = format!("Aufnahme fehlerhaft: {err}"); let cur = {
if let Err(e) = self.store.set_status(id, CallStatus::Failed, Some(&msg)) { let mut current = lock(&self.current);
log::error!("could not mark call {id} as failed: {e}"); if !current.as_ref().is_some_and(matches) {
return None;
} }
return Err(msg); current.take()?
} };
Ok(true) 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. /// Discards the current recording, if any: deletes both WAVs and the
pub fn discard_current_recording(&self) -> Result<(), String> { /// call row.
let Some(cur) = lock(&self.current).take() else { pub fn discard_current_recording(&self) -> Option<Ended> {
return Ok(()); let cur = lock(&self.current).take()?;
}; let id = cur.call_id;
cur.recorder.discard(); 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 self.store
.delete_call(cur.call_id) .delete_call(id)
.map_err(|e| format!("Gespräch konnte nicht gelöscht werden: {e}")) .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 /// Records both configured devices into a temporary directory for five
@@ -277,3 +458,104 @@ fn lock<T>(mutex: &Mutex<T>) -> MutexGuard<'_, T> {
.lock() .lock()
.unwrap_or_else(|poisoned| poisoned.into_inner()) .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));
}
}
+13 -2
View File
@@ -7,7 +7,7 @@ use tauri::tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent}
use tauri::{AppHandle, Manager}; use tauri::{AppHandle, Manager};
use crate::commands; use crate::commands;
use crate::state::AppState; use crate::state::{AppState, RecordingState};
const OPEN: &str = "open"; const OPEN: &str = "open";
const RECORD: &str = "record"; const RECORD: &str = "record";
@@ -34,6 +34,13 @@ pub fn create(app: &AppHandle) -> tauri::Result<()> {
commands::show_recorder_if_recording(app); commands::show_recorder_if_recording(app);
} }
RECORD => { RECORD => {
if matches!(
app.state::<AppState>().recording_state(),
RecordingState::Recording { .. }
) {
commands::show_recorder(app);
return;
}
// Opening the audio devices can take a moment; keep the // Opening the audio devices can take a moment; keep the
// event loop free. // event loop free.
let app = app.clone(); let app = app.clone();
@@ -67,7 +74,11 @@ pub fn create(app: &AppHandle) -> tauri::Result<()> {
fn quit(app: &AppHandle) { fn quit(app: &AppHandle) {
let app = app.clone(); let app = app.clone();
thread::spawn(move || { thread::spawn(move || {
if let Err(e) = app.state::<AppState>().stop_recording() { if let Some(Err(e)) = app
.state::<AppState>()
.stop_recording()
.map(|ended| ended.result)
{
log::error!("stopping the recording on quit failed: {e}"); log::error!("stopping the recording on quit failed: {e}");
} }
app.exit(0); app.exit(0);
+2 -3
View File
@@ -1,5 +1,6 @@
<script lang="ts"> <script lang="ts">
import { onMount } from 'svelte' import { onMount } from 'svelte'
import Prompt from './routes/Prompt.svelte'
import Recorder from './routes/Recorder.svelte' import Recorder from './routes/Recorder.svelte'
import Settings from './routes/Settings.svelte' import Settings from './routes/Settings.svelte'
@@ -21,9 +22,7 @@
{:else if route === '/settings'} {:else if route === '/settings'}
<Settings /> <Settings />
{:else if route === '/prompt'} {:else if route === '/prompt'}
<main class="page"> <Prompt />
<p class="muted">Gespräch beendet.</p>
</main>
{:else} {:else}
<main class="page"> <main class="page">
<header class="page-header"> <header class="page-header">
+15
View File
@@ -40,6 +40,15 @@ export type RecordingState =
| { state: 'idle' } | { state: 'idle' }
| { state: 'recording'; call_id: CallId; app: string; started_at: string } | { state: 'recording'; call_id: CallId; app: string; started_at: string }
/** What the end-of-call prompt shows about a call. Times are RFC 3339. */
export interface CallBrief {
id: CallId
app: string
started_at: string
ended_at: string | null
duration_secs: number
}
export interface Levels { export interface Levels {
mic: number mic: number
out: number out: number
@@ -57,9 +66,15 @@ export const getRecordingState = () => invoke<RecordingState>('get_recording_sta
export const startManualRecording = () => invoke<CallId>('start_manual_recording') export const startManualRecording = () => invoke<CallId>('start_manual_recording')
export const stopRecording = () => invoke<void>('stop_recording') export const stopRecording = () => invoke<void>('stop_recording')
export const discardCurrentRecording = () => invoke<void>('discard_current_recording') export const discardCurrentRecording = () => invoke<void>('discard_current_recording')
export const getPendingPrompt = () => invoke<CallBrief | null>('get_pending_prompt')
export const getCallBrief = (id: CallId) => invoke<CallBrief>('get_call_brief', { id })
export const transcribeCall = (id: CallId) => invoke<void>('transcribe_call', { id })
export const discardCall = (id: CallId) => invoke<void>('discard_call', { id })
export const onRecordingState = (handler: (state: RecordingState) => void): Promise<UnlistenFn> => export const onRecordingState = (handler: (state: RecordingState) => void): Promise<UnlistenFn> =>
listen<RecordingState>('recording-state', (event) => handler(event.payload)) listen<RecordingState>('recording-state', (event) => handler(event.payload))
export const onCallEnded = (handler: (id: CallId) => void): Promise<UnlistenFn> =>
listen<{ id: CallId }>('call-ended', (event) => handler(event.payload.id))
export const onLevels = (handler: (levels: Levels) => void): Promise<UnlistenFn> => export const onLevels = (handler: (levels: Levels) => void): Promise<UnlistenFn> =>
listen<Levels>('levels', (event) => handler(event.payload)) listen<Levels>('levels', (event) => handler(event.payload))
+22
View File
@@ -0,0 +1,22 @@
// German display formats for calls ("Microsoft Teams · 32 min",
// "heute, 10:14 – 10:46").
const pad = (n: number) => String(n).padStart(2, '0')
const clock = (d: Date) => `${pad(d.getHours())}:${pad(d.getMinutes())}`
const sameDay = (a: Date, b: Date) =>
a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate()
/** Whole minutes, at least 1: "32 min". */
export function formatMinutes(secs: number): string {
return `${Math.max(1, Math.round(secs / 60))} min`
}
/** "heute, 10:14 – 10:46", or "04.11., 10:14 – 10:46" for another day. */
export function formatTimeRange(startedAt: string, endedAt: string | null, now = new Date()): string {
const start = new Date(startedAt)
const day = sameDay(start, now) ? 'heute' : `${pad(start.getDate())}.${pad(start.getMonth() + 1)}.`
const range = endedAt ? `${clock(start)} – ${clock(new Date(endedAt))}` : clock(start)
return `${day}, ${range}`
}
+102
View File
@@ -0,0 +1,102 @@
<script lang="ts">
import { onMount } from 'svelte'
import {
discardCall,
errorText,
getCallBrief,
getPendingPrompt,
onCallEnded,
transcribeCall,
type CallBrief,
type CallId,
} from '../lib/api'
import { formatMinutes, formatTimeRange } from '../lib/format'
let brief = $state<CallBrief | null>(null)
let busy = $state(false)
let error = $state('')
function show(next: CallBrief | null) {
if (next?.id !== brief?.id) error = ''
brief = next
}
// The window stays loaded while hidden, so it may have missed the event:
// ask the backend whenever it is shown again.
const refresh = () =>
getPendingPrompt()
.then(show)
.catch((err) => (error = errorText(err)))
const load = (id: CallId) =>
getCallBrief(id)
.then(show)
.catch((err) => (error = errorText(err)))
onMount(() => {
const unlisten = onCallEnded(load)
refresh()
window.addEventListener('focus', refresh)
return () => {
window.removeEventListener('focus', refresh)
unlisten.then((off) => off())
}
})
async function run(action: (id: CallId) => Promise<void>) {
if (!brief) return
busy = true
error = ''
try {
await action(brief.id)
} catch (err) {
error = errorText(err)
} finally {
busy = false
}
}
</script>
<main class="prompt">
{#if brief}
<div class="title">{brief.app} · {formatMinutes(brief.duration_secs)}</div>
<div class="muted">{formatTimeRange(brief.started_at, brief.ended_at)}</div>
<div class="actions">
<button class="btn primary" disabled={busy} onclick={() => run(transcribeCall)}>
Transkribieren
</button>
<button class="btn danger" disabled={busy} onclick={() => run(discardCall)}>Verwerfen</button>
</div>
{:else}
<p class="muted">Kein Gespräch wartet auf eine Entscheidung.</p>
{/if}
{#if error}
<div class="error">{error}</div>
{/if}
</main>
<style>
:global(body:has(.prompt)) {
overflow: hidden;
}
.prompt {
min-height: 100vh;
background: var(--surface);
padding: 14px;
}
.title {
font-weight: 600;
}
.actions {
display: flex;
gap: 8px;
margin-top: 14px;
}
.error {
margin-top: 8px;
}
</style>