1use super::descriptor::{Descriptor, Direction};
4use super::session::{
5 AudioInput, Delivery, Emit, ExtractSession, Need, RunContext, SessionError, Step,
6 SuppliedAudio, SuppliedFrame, VideoInput,
7};
8use alpha2_model::{DataInterval, DataSet, DataWord};
9use alpha2_time::MediaTime;
10use std::fmt;
11
12#[derive(Copy, Clone, Debug, Eq, PartialEq)]
14pub struct PassInfo {
15 pub pass: usize,
17 pub direction: Direction,
19}
20
21#[derive(Clone, Debug, Eq, PartialEq)]
23pub struct ExtractorError {
24 pub message: String,
26 pub code: Option<&'static str>,
28}
29
30impl ExtractorError {
31 pub fn new(message: impl Into<String>) -> Self {
33 Self {
34 message: message.into(),
35 code: None,
36 }
37 }
38
39 pub fn with_code(code: &'static str, message: impl Into<String>) -> Self {
41 Self {
42 message: message.into(),
43 code: Some(code),
44 }
45 }
46}
47
48impl fmt::Display for ExtractorError {
49 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
50 formatter.write_str(&self.message)
51 }
52}
53
54impl std::error::Error for ExtractorError {}
55
56#[derive(Debug, Default)]
58pub struct Emitter {
59 emits: Vec<Emit>,
60}
61
62impl Emitter {
63 pub fn sample(&mut self, output: &str, time: MediaTime, values: &[Option<f32>]) {
65 self.emits.push(Emit::Sample {
66 output: output.to_owned(),
67 time,
68 values: values.to_vec(),
69 });
70 }
71
72 pub fn word(&mut self, output: &str, word: DataWord) {
74 self.emits.push(Emit::Word {
75 output: output.to_owned(),
76 word,
77 });
78 }
79
80 pub fn interval(&mut self, output: &str, interval: DataInterval) {
82 self.emits.push(Emit::Interval {
83 output: output.to_owned(),
84 interval,
85 });
86 }
87
88 pub fn push(&mut self, emit: Emit) {
90 self.emits.push(emit);
91 }
92
93 #[must_use]
95 pub fn emits(&self) -> &[Emit] {
96 &self.emits
97 }
98
99 fn take(&mut self) -> Vec<Emit> {
100 std::mem::take(&mut self.emits)
101 }
102}
103
104pub trait Extractor {
107 fn describe(&self) -> &Descriptor;
109
110 fn begin(&mut self, context: &RunContext) -> Result<(), ExtractorError>;
115
116 fn pass(&mut self, pass: &PassInfo) -> Result<(), ExtractorError>;
121
122 fn video(&mut self, frame: &VideoInput, out: &mut Emitter) -> Result<(), ExtractorError>;
127
128 fn audio(&mut self, block: &AudioInput, out: &mut Emitter) -> Result<(), ExtractorError>;
133
134 fn end(&mut self, out: &mut Emitter) -> Result<(), ExtractorError>;
139}
140
141pub trait Supplier {
143 fn frame(&mut self, need: &Need) -> Result<Option<SuppliedFrame>, String>;
149
150 fn audio(&mut self, need: &Need) -> Result<SuppliedAudio, String>;
155}
156
157#[derive(Clone, Debug, PartialEq)]
159#[non_exhaustive]
160pub enum DriveError {
161 Extractor(ExtractorError),
163 Supplier(String),
165 Session(SessionError),
167}
168
169impl fmt::Display for DriveError {
170 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
171 match self {
172 Self::Extractor(error) => write!(formatter, "extractor failed: {error}"),
173 Self::Supplier(error) => write!(formatter, "supplier failed: {error}"),
174 Self::Session(error) => error.fmt(formatter),
175 }
176 }
177}
178
179impl std::error::Error for DriveError {}
180
181impl From<SessionError> for DriveError {
182 fn from(error: SessionError) -> Self {
183 Self::Session(error)
184 }
185}
186
187pub fn drive(
193 mut session: ExtractSession,
194 extractor: &mut dyn Extractor,
195 supplier: &mut dyn Supplier,
196) -> Result<Vec<DataSet>, DriveError> {
197 let mut out = Emitter::default();
198 extractor
199 .begin(&session.context())
200 .map_err(DriveError::Extractor)?;
201 loop {
202 match session.next() {
203 Step::Need(need) => match &need {
204 Need::SourceFrame { .. } | Need::TimelineFrame { .. } => {
205 match supplier.frame(&need).map_err(DriveError::Supplier)? {
206 Some(frame) => session.supply_video(frame)?,
207 None => session.supply_end()?,
208 }
209 }
210 Need::SourceAudio { .. } | Need::TimelineAudio { .. } => {
211 let block = supplier.audio(&need).map_err(DriveError::Supplier)?;
212 session.supply_audio(block)?;
213 }
214 },
215 Step::Deliver(delivery) => {
216 match &delivery {
217 Delivery::Video(frame) => extractor.video(frame, &mut out),
218 Delivery::Audio(block) => extractor.audio(block, &mut out),
219 }
220 .map_err(DriveError::Extractor)?;
221 session.emit(&out.take())?;
222 }
223 Step::PassBoundary { pass, direction } => {
224 extractor
225 .pass(&PassInfo { pass, direction })
226 .map_err(DriveError::Extractor)?;
227 }
228 Step::Done => break,
229 }
230 }
231 extractor.end(&mut out).map_err(DriveError::Extractor)?;
232 session.emit(&out.take())?;
233 Ok(session.finish()?)
234}