From 30b6d4791ff5472e7931d9a5c85fbbd03e8bc8d7 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Thu, 14 Sep 2023 14:12:31 +0200 Subject: [PATCH] Add audio cache audio clear --- transcoder/src/state.rs | 39 ++++++++++++++++++++++++++++++++----- transcoder/src/transcode.rs | 5 +++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/transcoder/src/state.rs b/transcoder/src/state.rs index e43e6e96..b3594631 100644 --- a/transcoder/src/state.rs +++ b/transcoder/src/state.rs @@ -9,14 +9,14 @@ use std::time::{Duration, SystemTime}; pub struct Transcoder { running: RwLock>, - audio_jobs: RwLock>, + audio_jobs: RwLock>, } impl Transcoder { pub fn new() -> Transcoder { Self { running: RwLock::new(HashMap::new()), - audio_jobs: RwLock::new(Vec::new()), + audio_jobs: RwLock::new(HashMap::new()), } } @@ -32,6 +32,18 @@ impl Transcoder { } } + fn clean_old_audio_transcode(&self) { + for ((path, idx), info) in self.audio_jobs.write().unwrap().iter_mut() { + if SystemTime::now() + .duration_since(*info.last_used.read().unwrap()) + .is_ok_and(|d| d > Duration::new(4 * 60 * 60, 0)) + { + _ = info.job.interrupt(); + _ = std::fs::remove_dir_all(get_audio_path(path, *idx)); + } + } + } + pub async fn build_master(&self, resource: String, slug: String) -> Option { let mut master = String::from("#EXTM3U\n"); let path = get_path(resource, slug).await.ok()?; @@ -178,13 +190,20 @@ impl Transcoder { .audio_jobs .read() .unwrap() - .contains(&(path.clone(), audio)) + .contains_key(&(path.clone(), audio)) { + self.clean_old_audio_transcode(); + // TODO: If two concurrent requests for the same audio came, the first one will // initialize the transcode and wait for the second segment while the second will use // the same transcode but not wait and retrieve a potentially invalid playlist file. - self.audio_jobs.write().unwrap().push((path.clone(), audio)); - transcode_audio(path, audio).await?; + self.audio_jobs.write().unwrap().insert( + (path.clone(), audio), + AudioInfo { + job: transcode_audio(path, audio).await?, + last_used: RwLock::new(SystemTime::now()), + }, + ); } std::fs::read_to_string(stream).map_err(|e| TranscodeError::ReadError(e)) } @@ -195,6 +214,16 @@ impl Transcoder { audio: u32, chunk: u32, ) -> Result { + if let Some(mut last) = self + .audio_jobs + .read() + .unwrap() + .get(&(path.clone(), audio)) + .and_then(|info| info.last_used.try_write().ok()) + { + *last = SystemTime::now(); + } + let mut path = PathBuf::from(get_audio_path(&path, audio)); path.push(format!("segments-{0:02}.ts", chunk)); Ok(path) diff --git a/transcoder/src/transcode.rs b/transcoder/src/transcode.rs index 6324799d..be343ab3 100644 --- a/transcoder/src/transcode.rs +++ b/transcoder/src/transcode.rs @@ -328,3 +328,8 @@ pub struct TranscodeInfo { pub uuid: String, pub last_used: RwLock, } + +pub struct AudioInfo { + pub job: Child, + pub last_used: RwLock, +}