mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-28 13:02:21 +01:00
* windows: hear only the call app's outputs during meetings A virtual mixer (SteelSeries Sonar, VB-Cable, Voicemeeter) has a playback endpoint that carries the user's processed microphone, and replays the call on the physical output. Looping back every render endpoint therefore recorded the user's own voice, and the remote voices twice. A meeting started for a call app now loops back the endpoints where the app's microphone holder also plays, else any endpoint one of its processes plays on, and falls back to every output only when the app has no session or its outputs would not open. A manual recording still hears every output. The selection is a pure function in meeting/loopback_route.rs, tested on every platform; the Windows adapter maps cpal devices to WASAPI endpoint ids. The route check recomputes every 2 s, compares endpoint ids, and keeps the current outputs while the app's sessions are temporarily absent or the enumeration fails. * windows meetings: keep the loopback outputs a blip would flip-flop The 2 s route check reopened whenever the computed app set differed from the open one, so the seconds the call app's microphone-holder session was missing moved it to the fallback set, and the holder's return moved it back, reopening the system track each way. should_reopen in meeting/loopback_route.rs keeps the current outputs when the new app set still covers them (equal or a superset, as the fallback of a second process is), and still reopens on a changed microphone-holder set or app outputs that leave out a current endpoint. A detected call app that has no playback session yet, or whose sessions can't be read, is heard through every output until the 2 s check finds the app's outputs; when none of the app's outputs open, Hex hears every output and stops following the app for that source. docs/features/meetings.md says both, with the current test names. The CoInitializeEx SAFETY comment now covers a thread already in another apartment (S_FALSE or RPC_E_CHANGED_MODE, both fine since the calls need no message pump), the GetId comment covers the CoTaskMemAlloc string read and freed once, and process_path is private again. * windows meetings: reopen when an app's own outputs grow
287 lines
9.9 KiB
Rust
287 lines
9.9 KiB
Rust
//! The Linux side of meetings: system audio from a PipeWire monitor of the
|
|
//! default output, and the file manager and Trash.
|
|
//!
|
|
//! `pw-record` captures the monitor (`stream.capture.sink`), which the
|
|
//! session manager links to whatever the default output is, following it
|
|
//! when it changes. It runs as a child process so Hex links no PipeWire
|
|
//! library and a desktop without PipeWire fails only this source.
|
|
|
|
use std::io::Read;
|
|
use std::os::unix::process::CommandExt;
|
|
use std::path::{Path, PathBuf};
|
|
use std::process::{Child, Command, Stdio};
|
|
use std::sync::mpsc::{self, Receiver};
|
|
use std::thread;
|
|
use std::time::{Instant, SystemTime};
|
|
|
|
use color_eyre::eyre::{Result, WrapErr, eyre};
|
|
|
|
use super::SAMPLE_RATE;
|
|
use super::capture::{LiveSource, POLL_INTERVAL, PacketSink, STALL_TIMEOUT};
|
|
|
|
const PW_RECORD: &str = "pw-record";
|
|
/// 100 ms of 16 kHz mono `f32`.
|
|
const READ_BYTES: usize = SAMPLE_RATE as usize / 10 * 4;
|
|
const QUEUE_CAPACITY: usize = 128;
|
|
|
|
/// Fails until `pw-record`, which hears the other participants, is installed.
|
|
pub(super) fn ensure_system_audio() -> Result<()> {
|
|
if find_executable(PW_RECORD).is_some() {
|
|
Ok(())
|
|
} else {
|
|
Err(eyre!(
|
|
"Install PipeWire's pw-record (the pipewire-audio package) so Hex can hear the other participants, then try again"
|
|
))
|
|
}
|
|
}
|
|
|
|
/// Ends `command`'s process with the thread that starts it, so a helper
|
|
/// never outlives a Hex that stopped without dropping it.
|
|
fn tied_to_thread(command: &mut Command) -> &mut Command {
|
|
// SAFETY: `prctl` is async-signal-safe and touches only the child.
|
|
unsafe {
|
|
command.pre_exec(|| {
|
|
libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM);
|
|
Ok(())
|
|
})
|
|
}
|
|
}
|
|
|
|
fn find_executable(name: &str) -> Option<PathBuf> {
|
|
std::env::var_os("PATH")
|
|
.into_iter()
|
|
.flat_map(|path| std::env::split_paths(&path).collect::<Vec<_>>())
|
|
.chain(["/usr/bin".into(), "/bin".into()])
|
|
.map(|directory| directory.join(name))
|
|
.find(|candidate| candidate.is_file())
|
|
}
|
|
|
|
pub(super) fn open_system_audio(
|
|
_sink: &PacketSink,
|
|
_application: Option<&str>,
|
|
) -> Result<Box<dyn LiveSource>> {
|
|
MonitorSource::start().map(|source| Box::new(source) as Box<dyn LiveSource>)
|
|
}
|
|
|
|
/// The default output's monitor through `pw-record`, as 16 kHz mono `f32`.
|
|
struct MonitorSource {
|
|
child: Child,
|
|
buffers: Receiver<Vec<f32>>,
|
|
last_audio: Instant,
|
|
}
|
|
|
|
impl MonitorSource {
|
|
fn start() -> Result<Self> {
|
|
let executable =
|
|
find_executable(PW_RECORD).ok_or_else(|| eyre!("{PW_RECORD} is not installed"))?;
|
|
let mut child = tied_to_thread(&mut Command::new(executable))
|
|
.args([
|
|
"--raw",
|
|
"--rate",
|
|
&SAMPLE_RATE.to_string(),
|
|
"--channels",
|
|
"1",
|
|
"--format",
|
|
"f32",
|
|
"--media-category",
|
|
"Capture",
|
|
"-P",
|
|
"{ stream.capture.sink = true node.name = hex-meeting-system-audio \
|
|
node.description = \"Hex meeting system audio\" }",
|
|
"-",
|
|
])
|
|
.stdin(Stdio::null())
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::null())
|
|
.spawn()
|
|
.wrap_err("could not start pw-record")?;
|
|
let mut stdout = child
|
|
.stdout
|
|
.take()
|
|
.ok_or_else(|| eyre!("pw-record has no output"))?;
|
|
let (sender, buffers) = mpsc::sync_channel::<Vec<f32>>(QUEUE_CAPACITY);
|
|
thread::Builder::new()
|
|
.name("meeting-pw-record".into())
|
|
.spawn(move || {
|
|
let mut bytes = vec![0_u8; READ_BYTES];
|
|
let mut carry = Vec::<u8>::new();
|
|
loop {
|
|
let read = match stdout.read(&mut bytes) {
|
|
Ok(0) | Err(_) => return,
|
|
Ok(read) => read,
|
|
};
|
|
carry.extend_from_slice(&bytes[..read]);
|
|
let whole = carry.len() / 4 * 4;
|
|
let samples = carry[..whole]
|
|
.as_chunks::<4>()
|
|
.0
|
|
.iter()
|
|
.map(|sample| f32::from_le_bytes(*sample))
|
|
.collect::<Vec<_>>();
|
|
carry.drain(..whole);
|
|
if sender.send(samples).is_err() {
|
|
return;
|
|
}
|
|
}
|
|
})
|
|
.wrap_err("could not start the pw-record reader")?;
|
|
tracing::info!("system audio monitor started");
|
|
Ok(Self {
|
|
child,
|
|
buffers,
|
|
last_audio: Instant::now(),
|
|
})
|
|
}
|
|
}
|
|
|
|
impl LiveSource for MonitorSource {
|
|
fn poll(&mut self, sink: &PacketSink) -> std::result::Result<(), String> {
|
|
match self.buffers.recv_timeout(POLL_INTERVAL) {
|
|
Ok(mut samples) => {
|
|
while let Ok(next) = self.buffers.try_recv() {
|
|
samples.extend(next);
|
|
}
|
|
self.last_audio = Instant::now();
|
|
sink.send(SAMPLE_RATE, samples, None);
|
|
Ok(())
|
|
}
|
|
Err(mpsc::RecvTimeoutError::Timeout) if self.last_audio.elapsed() > STALL_TIMEOUT => {
|
|
Err("the system audio monitor stopped delivering audio".into())
|
|
}
|
|
Err(mpsc::RecvTimeoutError::Timeout) => Ok(()),
|
|
Err(mpsc::RecvTimeoutError::Disconnected) => {
|
|
let status = self.child.try_wait().ok().flatten();
|
|
Err(match status {
|
|
Some(status) => format!("pw-record exited ({status})"),
|
|
None => "pw-record closed its output".into(),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
fn describe(&self) -> &'static str {
|
|
"pipewire-monitor"
|
|
}
|
|
}
|
|
|
|
impl Drop for MonitorSource {
|
|
fn drop(&mut self) {
|
|
let _ = self.child.kill();
|
|
let _ = self.child.wait();
|
|
}
|
|
}
|
|
|
|
/// Opens the folder holding `path` (or `path` itself, when a folder).
|
|
pub(crate) fn reveal(path: &Path) {
|
|
let folder = if path.is_dir() {
|
|
path
|
|
} else {
|
|
path.parent().unwrap_or(path)
|
|
};
|
|
if let Err(error) = Command::new("xdg-open")
|
|
.arg(folder)
|
|
.stdin(Stdio::null())
|
|
.stdout(Stdio::null())
|
|
.stderr(Stdio::null())
|
|
.spawn()
|
|
{
|
|
tracing::error!(%error, "could not reveal meeting file");
|
|
}
|
|
}
|
|
|
|
/// Moves `path` to the home Trash as the freedesktop.org Trash
|
|
/// specification lays it out, so file managers can restore it.
|
|
pub(crate) fn move_to_trash(path: &Path) -> Result<()> {
|
|
let data = dirs::data_dir().ok_or_else(|| eyre!("no data directory for the Trash"))?;
|
|
move_to_trash_in(&data.join("Trash"), path, SystemTime::now())
|
|
}
|
|
|
|
fn move_to_trash_in(trash: &Path, path: &Path, now: SystemTime) -> Result<()> {
|
|
let path = std::path::absolute(path)?;
|
|
let files = trash.join("files");
|
|
let info = trash.join("info");
|
|
std::fs::create_dir_all(&files)?;
|
|
std::fs::create_dir_all(&info)?;
|
|
let name = path
|
|
.file_name()
|
|
.ok_or_else(|| eyre!("{} has no name", path.display()))?
|
|
.to_string_lossy()
|
|
.into_owned();
|
|
let deleted_ms = now
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
.unwrap_or_default()
|
|
.as_millis() as u64;
|
|
let deleted = super::transcript::LocalTime::from_ms(deleted_ms).iso();
|
|
for attempt in 0.. {
|
|
let candidate = if attempt == 0 {
|
|
name.clone()
|
|
} else {
|
|
format!("{name}.{attempt}")
|
|
};
|
|
let info_path = info.join(format!("{candidate}.trashinfo"));
|
|
let claimed = std::fs::OpenOptions::new()
|
|
.write(true)
|
|
.create_new(true)
|
|
.open(&info_path);
|
|
let mut file = match claimed {
|
|
Ok(file) => file,
|
|
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,
|
|
Err(error) => return Err(error.into()),
|
|
};
|
|
std::io::Write::write_all(
|
|
&mut file,
|
|
format!(
|
|
"[Trash Info]\nPath={}\nDeletionDate={deleted}\n",
|
|
percent_encode(&path.to_string_lossy())
|
|
)
|
|
.as_bytes(),
|
|
)?;
|
|
if let Err(error) = std::fs::rename(&path, files.join(&candidate)) {
|
|
let _ = std::fs::remove_file(&info_path);
|
|
return Err(eyre!(
|
|
"could not move {} to the Trash: {error}",
|
|
path.display()
|
|
));
|
|
}
|
|
return Ok(());
|
|
}
|
|
unreachable!("the loop returns")
|
|
}
|
|
|
|
/// Percent-encodes a path for a `.trashinfo` file, keeping `/`.
|
|
fn percent_encode(path: &str) -> String {
|
|
let mut encoded = String::with_capacity(path.len());
|
|
for byte in path.bytes() {
|
|
if byte.is_ascii_alphanumeric() || b"/-_.~".contains(&byte) {
|
|
encoded.push(byte as char);
|
|
} else {
|
|
encoded.push_str(&format!("%{byte:02X}"));
|
|
}
|
|
}
|
|
encoded
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn trashing_writes_restorable_info_and_never_overwrites() {
|
|
let root = std::env::temp_dir().join(format!("hex-trash-test-{}", std::process::id()));
|
|
let _ = std::fs::remove_dir_all(&root);
|
|
let trash = root.join("Trash");
|
|
let meeting = root.join("my meeting");
|
|
for _ in 0..2 {
|
|
std::fs::create_dir_all(&meeting).unwrap();
|
|
std::fs::write(meeting.join("manifest.json"), "{}").unwrap();
|
|
move_to_trash_in(&trash, &meeting, SystemTime::UNIX_EPOCH).unwrap();
|
|
}
|
|
assert!(!meeting.exists());
|
|
assert!(trash.join("files/my meeting/manifest.json").exists());
|
|
assert!(trash.join("files/my meeting.1/manifest.json").exists());
|
|
let info = std::fs::read_to_string(trash.join("info/my meeting.trashinfo")).unwrap();
|
|
assert!(info.contains("/my%20meeting\n"), "{info}");
|
|
assert!(info.contains("DeletionDate="), "{info}");
|
|
std::fs::remove_dir_all(&root).unwrap();
|
|
}
|
|
}
|