playback and import issues

This commit is contained in:
2026-05-19 14:05:47 +05:30
parent f744747167
commit 0b5510636e
7 changed files with 377 additions and 132 deletions

View File

@@ -5,14 +5,15 @@ use std::path::PathBuf;
use std::process::{Child, ChildStdout, Command, Stdio};
use std::sync::{Arc, Condvar, Mutex};
use std::thread::{self, JoinHandle};
use std::time::Duration;
use std::time::{Duration, Instant};
pub const DEFAULT_CACHE_CAPACITY: usize = 500;
pub const MIN_CACHE_CAPACITY: usize = 200;
pub const MAX_CACHE_CAPACITY: usize = 2000;
pub const DEFAULT_CACHE_CAPACITY: usize = 160;
pub const MIN_CACHE_CAPACITY: usize = 50;
pub const MAX_CACHE_CAPACITY: usize = 800;
const READ_BUF_BYTES: usize = 256 * 1024;
/// If the requested frame is this far ahead of the worker, kill + reseek.
const SEEK_AHEAD_THRESHOLD: u64 = 60;
const DECODE_TIMEOUT: Duration = Duration::from_secs(15);
struct FrameCache {
map: HashMap<u64, Vec<u8>>,
@@ -158,6 +159,7 @@ impl DecoderSession {
}
// Wait for the worker to produce idx.
let wait_started = Instant::now();
let mut st = self.shared.state.lock().unwrap();
loop {
if st.cache.contains(idx) {
@@ -175,7 +177,17 @@ impl DecoderSession {
if st.shutdown {
return Err("decoder shut down".into());
}
st = self.shared.cond.wait(st).unwrap();
let (next_st, timeout) = self
.shared
.cond
.wait_timeout(st, Duration::from_millis(250))
.unwrap();
st = next_st;
if timeout.timed_out() && wait_started.elapsed() >= DECODE_TIMEOUT {
drop(st);
self.stop_worker();
return Err(format!("timed out decoding frame {idx}"));
}
}
}
@@ -239,9 +251,7 @@ fn spawn_ffmpeg(
info: &VideoInfo,
idx: u64,
) -> Result<(Child, BufReader<ChildStdout>), String> {
let path_str = path
.to_str()
.ok_or_else(|| "non-utf8 path".to_string())?;
let path_str = path.to_str().ok_or_else(|| "non-utf8 path".to_string())?;
let t = idx as f64 / info.fps;
let mut cmd = Command::new("ffmpeg");
cmd.args(["-v", "error", "-threads", "0"]);
@@ -263,7 +273,7 @@ fn spawn_ffmpeg(
"mjpeg",
"pipe:1",
]);
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
cmd.stdout(Stdio::piped()).stderr(Stdio::null());
let mut child = cmd.spawn().map_err(|e| format!("ffmpeg spawn: {e}"))?;
let stdout = child
.stdout