Add audio cache audio clear

This commit is contained in:
Zoe Roux 2023-09-14 14:12:31 +02:00
parent e5c185627d
commit 30b6d4791f
2 changed files with 39 additions and 5 deletions

View File

@ -9,14 +9,14 @@ use std::time::{Duration, SystemTime};
pub struct Transcoder { pub struct Transcoder {
running: RwLock<HashMap<String, TranscodeInfo>>, running: RwLock<HashMap<String, TranscodeInfo>>,
audio_jobs: RwLock<Vec<(String, u32)>>, audio_jobs: RwLock<HashMap<(String, u32), AudioInfo>>,
} }
impl Transcoder { impl Transcoder {
pub fn new() -> Transcoder { pub fn new() -> Transcoder {
Self { Self {
running: RwLock::new(HashMap::new()), 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<String> { pub async fn build_master(&self, resource: String, slug: String) -> Option<String> {
let mut master = String::from("#EXTM3U\n"); let mut master = String::from("#EXTM3U\n");
let path = get_path(resource, slug).await.ok()?; let path = get_path(resource, slug).await.ok()?;
@ -178,13 +190,20 @@ impl Transcoder {
.audio_jobs .audio_jobs
.read() .read()
.unwrap() .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 // 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 // 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. // the same transcode but not wait and retrieve a potentially invalid playlist file.
self.audio_jobs.write().unwrap().push((path.clone(), audio)); self.audio_jobs.write().unwrap().insert(
transcode_audio(path, audio).await?; (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)) std::fs::read_to_string(stream).map_err(|e| TranscodeError::ReadError(e))
} }
@ -195,6 +214,16 @@ impl Transcoder {
audio: u32, audio: u32,
chunk: u32, chunk: u32,
) -> Result<PathBuf, std::io::Error> { ) -> Result<PathBuf, std::io::Error> {
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)); let mut path = PathBuf::from(get_audio_path(&path, audio));
path.push(format!("segments-{0:02}.ts", chunk)); path.push(format!("segments-{0:02}.ts", chunk));
Ok(path) Ok(path)

View File

@ -328,3 +328,8 @@ pub struct TranscodeInfo {
pub uuid: String, pub uuid: String,
pub last_used: RwLock<SystemTime>, pub last_used: RwLock<SystemTime>,
} }
pub struct AudioInfo {
pub job: Child,
pub last_used: RwLock<SystemTime>,
}