Repository navigation
Expand file tree
/
Copy pathlinux.rs
More file actions
284 lines (265 loc) · 9.89 KB
/
Copy pathlinux.rs
File metadata and controls
284 lines (265 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! 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) -> 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(super) 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();
}
}