1use std::{
7 borrow::Cow,
8 fs::{read_dir, remove_file},
9 io,
10 path::Path,
11 process::Stdio,
12 time::Duration,
13};
14
15use futures::future::try_join;
16#[cfg(unix)]
17use libc::{SIGKILL, killpg};
18use lru_cache::LruCache;
19use ruma::Mxc;
20use tokio::{
21 fs::{OpenOptions, create_dir_all},
22 io::{AsyncRead, AsyncReadExt, AsyncWriteExt, copy, sink},
23 process::Command,
24 time::{Instant, timeout_at},
25};
26use tuwunel_core::{
27 Config, Err, Result, debug, debug_warn, defer, err, implement,
28 utils::{BoolExt, math::u64_from_usize_saturating, random_string},
29};
30
31use super::{Dim, Media};
32
33const INPUT: &str = "{input}";
35const WIDTH: &str = "{width}";
36const HEIGHT: &str = "{height}";
37
38const VIDEO: &str = "video/";
40
41const STAGED: &str = "tuwunel-video-";
44
45const NAME_LENGTH: usize = 16;
46
47const DIAGNOSTIC_LEN: u64 = 4096;
48
49const FAILURE_COOLDOWN: Duration = Duration::from_mins(5);
53
54pub(super) const FAILURES: usize = 1024;
56
57const FAIR_SHARE: f64 = 4.0;
60
61pub(super) type Failures = LruCache<String, Instant>;
64
65struct Reaper {
70 group: Option<u32>,
71}
72
73impl Reaper {
74 fn disarm(&mut self) { self.group = None; }
75}
76
77impl Drop for Reaper {
78 fn drop(&mut self) {
79 if let Some(group) = self.group {
80 kill_group(group);
81 }
82 }
83}
84
85#[implement(super::Service)]
88#[tracing::instrument(
89 name = "video",
90 level = "debug",
91 skip_all,
92 fields(
93 ?dim,
94 content_type = ?media.content_type,
95 ),
96)]
97pub(super) async fn video_frame(
98 &self,
99 mxc: &Mxc<'_>,
100 dim: &Dim,
101 media: &Media,
102) -> Option<Vec<u8>> {
103 let config = &self.services.config;
104
105 let is_video = media
106 .content_type
107 .as_deref()
108 .is_some_and(|content_type| content_type.starts_with(VIDEO));
109
110 let workable = !config.media.video_thumbnail.command.is_empty()
111 && media.content.len() <= config.media.video_thumbnail.max_size
112 && !self.failed_recently(mxc);
113
114 is_video
115 .and_is(workable)
116 .then_async(|| self.extract_frame(mxc, dim, &media.content))
117 .await?
118 .inspect_err(|e| debug_warn!(%e, "Failed to extract a video frame."))
119 .ok()
120}
121
122#[implement(super::Service)]
125fn failed_recently(&self, mxc: &Mxc<'_>) -> bool {
126 let Some(cooldown) = Instant::now().checked_sub(FAILURE_COOLDOWN) else {
127 return false;
128 };
129
130 self.video_thumbnail_failures
131 .lock()
132 .ok()
133 .and_then(|mut failures| failures.get_mut(&mxc.to_string()).copied())
134 .is_some_and(|failed| failed > cooldown)
135}
136
137#[implement(super::Service)]
138pub(super) fn remember_failure(&self, mxc: &Mxc<'_>) {
139 if let Ok(mut failures) = self.video_thumbnail_failures.lock() {
140 failures.insert(mxc.to_string(), Instant::now());
141 }
142}
143
144#[implement(super::Service)]
145#[tracing::instrument(level = "debug", skip(self, content))]
146async fn extract_frame(&self, mxc: &Mxc<'_>, dim: &Dim, content: &[u8]) -> Result<Vec<u8>> {
147 let config = &self.services.config;
148 let timeout = Duration::from_secs(config.media.video_thumbnail.timeout);
149
150 let deadline = Instant::now()
153 .checked_add(timeout)
154 .ok_or_else(|| {
155 err!(Config("media.video_thumbnail.timeout", "Timeout is out of range."))
156 })?;
157
158 let Some((program, args)) = config.media.video_thumbnail.command.split_first() else {
159 return Err!(Config("media.video_thumbnail.command", "No program is configured."));
160 };
161
162 let Ok(Ok(_slot)) = timeout_at(deadline, self.video_thumbnail_slots.acquire()).await else {
163 return Err!("Timed out waiting for a video thumbnail slot.");
164 };
165
166 let path = staging_dir(config).join(format!("{STAGED}{}", random_string(NAME_LENGTH)));
167
168 defer! {{ remove_file(&path).ok(); }}
169
170 let Ok(staged) = timeout_at(deadline, stage(&path, content)).await else {
171 return Err!("Timed out staging the video.");
172 };
173
174 staged?;
175
176 if deadline.saturating_duration_since(Instant::now()) < timeout.div_f64(FAIR_SHARE) {
179 return Err!("Too little of the deadline remained to run the video thumbnail program.");
180 }
181
182 let width = dim.width.to_string();
183 let height = dim.height.to_string();
184 let args = args
185 .iter()
186 .map(|arg| substitute(arg, &path, &width, &height));
187
188 let limit = u64_from_usize_saturating(config.media.video_thumbnail.max_size);
189 let frame = run(program, args, limit, deadline).await;
190
191 if frame.is_err() {
194 self.remember_failure(mxc);
195 }
196
197 frame
198}
199
200fn staging_dir(config: &Config) -> Cow<'_, Path> {
203 config
204 .media
205 .video_thumbnail
206 .path
207 .as_deref()
208 .map_or_else(|| config.database_path.join("tmp").into(), Cow::Borrowed)
209}
210
211#[tracing::instrument(name = "sweep", level = "debug", skip_all)]
216pub(super) fn sweep_staging_dir(config: &Config) {
217 let Ok(dir) = read_dir(staging_dir(config).as_ref()) else {
218 return;
219 };
220
221 dir.filter_map(Result::ok)
222 .map(|entry| entry.path())
223 .filter(|path| {
224 path.file_name()
225 .and_then(|name| name.to_str())
226 .is_some_and(|name| name.starts_with(STAGED))
227 })
228 .for_each(|path| {
229 debug!(?path, "Removing a video staged by a previous run.");
230 remove_file(&path).ok();
231 });
232}
233
234async fn stage(path: &Path, content: &[u8]) -> Result {
237 if let Some(dir) = path.parent() {
238 create_dir_all(dir).await?;
239 }
240
241 let mut options = OpenOptions::new();
242 options.create_new(true).write(true);
243
244 #[cfg(unix)]
245 options.mode(0o600);
246
247 let mut file = options.open(path).await?;
248
249 file.write_all(content).await?;
250 file.flush().await?;
251
252 Ok(())
253}
254
255pub(super) fn substitute<'a>(
256 arg: &'a str,
257 input: &Path,
258 width: &str,
259 height: &str,
260) -> Cow<'a, str> {
261 let substituted = || {
262 arg.replace(INPUT, &input.to_string_lossy())
263 .replace(WIDTH, width)
264 .replace(HEIGHT, height)
265 .into()
266 };
267
268 arg.contains('{')
269 .then(substituted)
270 .unwrap_or(Cow::Borrowed(arg))
271}
272
273#[tracing::instrument(
278 level = "debug",
279 skip(args),
280 fields(
281 %program,
282 %limit,
283 ),
284)]
285pub(super) async fn run<Args>(
286 program: &str,
287 args: Args,
288 limit: u64,
289 deadline: Instant,
290) -> Result<Vec<u8>>
291where
292 Args: IntoIterator<Item: AsRef<str>> + Send,
293{
294 let mut command = Command::new(program);
295 command
296 .stdin(Stdio::null())
297 .stdout(Stdio::piped())
298 .stderr(Stdio::piped())
299 .kill_on_drop(true);
300
301 for arg in args {
302 command.arg(arg.as_ref());
303 }
304
305 #[cfg(unix)]
306 command.process_group(0);
307
308 let mut child = command.spawn()?;
309 let mut reaper = Reaper { group: child.id() };
310
311 let stdout = child.stdout.take().expect("stdout is piped");
312 let stderr = child.stderr.take().expect("stderr is piped");
313
314 let collect = try_join(drain(stdout, limit.saturating_add(1)), drain(stderr, DIAGNOSTIC_LEN));
316
317 let outcome = timeout_at(deadline, async {
320 let collected = collect.await?;
321 let status = child.wait().await?;
322
323 Ok::<_, io::Error>((collected, status))
324 })
325 .await;
326
327 if child.id().is_none() {
331 reaper.disarm();
332 }
333
334 let Ok(exited) = outcome else {
335 return Err!("Video thumbnail program exceeded its deadline.");
336 };
337
338 let ((frame, diagnostic), status) = exited?;
339 let diagnostic = String::from_utf8_lossy(&diagnostic);
340
341 debug!(?status, len = %frame.len(), "Video thumbnail program exited.");
342
343 if !status.success() {
344 return Err!("Video thumbnail program failed with {status}: {diagnostic}");
345 }
346
347 if frame.is_empty() {
348 return Err!("Video thumbnail program produced no frame: {diagnostic}");
349 }
350
351 if u64_from_usize_saturating(frame.len()) > limit {
352 return Err!("Video thumbnail program produced a frame past {limit} bytes.");
353 }
354
355 Ok(frame)
356}
357
358async fn drain<Pipe>(mut pipe: Pipe, limit: u64) -> Result<Vec<u8>, io::Error>
359where
360 Pipe: AsyncRead + Unpin + Send,
361{
362 let mut buf = Vec::new();
363
364 (&mut pipe)
365 .take(limit)
366 .read_to_end(&mut buf)
367 .await?;
368
369 copy(&mut pipe, &mut sink()).await?;
372
373 Ok(buf)
374}
375
376#[cfg(unix)]
377fn kill_group(group: u32) {
378 let Ok(group) = i32::try_from(group) else {
379 return;
380 };
381
382 unsafe {
386 killpg(group, SIGKILL);
387 }
388}
389
390#[cfg(not(unix))]
391fn kill_group(_group: u32) {}