Skip to main content

yscv_video/
source.rs

1use crate::{Frame, VideoError};
2
3/// Generic frame source abstraction.
4pub trait FrameSource {
5    fn next_frame(&mut self) -> Result<Option<Frame>, VideoError>;
6}
7
8impl<S: FrameSource + ?Sized> FrameSource for Box<S> {
9    fn next_frame(&mut self) -> Result<Option<Frame>, VideoError> {
10        (**self).next_frame()
11    }
12}
13
14/// Deterministic in-memory source for tests and reproducible local runs.
15#[derive(Debug, Clone)]
16pub struct InMemoryFrameSource {
17    frames: Vec<Frame>,
18    cursor: usize,
19}
20
21impl InMemoryFrameSource {
22    pub fn new(frames: Vec<Frame>) -> Self {
23        Self { frames, cursor: 0 }
24    }
25}
26
27impl FrameSource for InMemoryFrameSource {
28    fn next_frame(&mut self) -> Result<Option<Frame>, VideoError> {
29        if self.cursor >= self.frames.len() {
30            return Ok(None);
31        }
32        let frame = self.frames[self.cursor].clone();
33        self.cursor += 1;
34        Ok(Some(frame))
35    }
36}