diff --git a/src/api/context.rs b/src/api/context.rs index e607bbc22e..ecb21cb8c7 100644 --- a/src/api/context.rs +++ b/src/api/context.rs @@ -122,12 +122,8 @@ impl Context { } let inner = &mut self.inner; - let run = move || inner.send_frame(frame, params); - match &self.pool { - Some(pool) => pool.install(run), - None => run(), - } + inner.send_frame(frame, params) } /// Returns the first-pass data of a two-pass encode for the frame that was diff --git a/src/api/internal.rs b/src/api/internal.rs index 57bda98002..e32f239d82 100644 --- a/src/api/internal.rs +++ b/src/api/internal.rs @@ -258,6 +258,7 @@ pub(crate) struct ContextInner { next_lookahead_output_frameno: u64, /// Optional opaque to be sent back to the user opaque_q: BTreeMap, + is_flushing: bool, } impl ContextInner { @@ -325,6 +326,7 @@ impl ContextInner { next_lookahead_frame: 1, next_lookahead_output_frameno: 0, opaque_q: BTreeMap::new(), + is_flushing: false, } } @@ -333,8 +335,9 @@ impl ContextInner { &mut self, frame: Option>>, params: Option, ) -> Result<(), EncoderStatus> { let input_frameno = self.frame_count; - let is_flushing = frame.is_none(); - if !is_flushing { + + self.is_flushing = frame.is_none(); + if !self.is_flushing { self.frame_count += 1; } self.frame_q.insert(input_frameno, frame); @@ -348,33 +351,6 @@ impl ContextInner { } } - if !self.needs_more_frame_q_lookahead(self.next_lookahead_frame) { - let lookahead_frames = self - .frame_q - .range(self.next_lookahead_frame - 1..) - .filter_map(|(&_input_frameno, frame)| frame.clone()) - .collect::>(); - - if is_flushing { - // This is the last time send_frame is called, process all the - // remaining frames. - for cur_lookahead_frames in - std::iter::successors(Some(&lookahead_frames[..]), |s| s.get(1..)) - { - if cur_lookahead_frames.len() < 2 { - // All frames have been processed - break; - } - - self.compute_keyframe_placement(cur_lookahead_frames); - } - } else { - self.compute_keyframe_placement(&lookahead_frames); - } - } - - self.compute_frame_invariants(); - Ok(()) } @@ -1288,12 +1264,44 @@ impl ContextInner { } } + // lookahead computations + pub(crate) fn compute_fi(&mut self) { + if !self.needs_more_frame_q_lookahead(self.next_lookahead_frame) { + let lookahead_frames = self + .frame_q + .range(self.next_lookahead_frame - 1..) + .filter_map(|(&_input_frameno, frame)| frame.clone()) + .collect::>(); + + if self.is_flushing { + // This is the last time send_frame is called, process all the + // remaining frames. + for cur_lookahead_frames in + std::iter::successors(Some(&lookahead_frames[..]), |s| s.get(1..)) + { + if cur_lookahead_frames.len() < 2 { + // All frames have been processed + break; + } + + self.compute_keyframe_placement(cur_lookahead_frames); + } + } else { + self.compute_keyframe_placement(&lookahead_frames); + } + } + + self.compute_frame_invariants(); + } + #[hawktracer(receive_packet)] pub fn receive_packet(&mut self) -> Result, EncoderStatus> { if self.done_processing() { return Err(EncoderStatus::LimitReached); } + self.compute_fi(); + if self.needs_more_fi_lookahead() { return Err(EncoderStatus::NeedMoreData); } diff --git a/src/api/test.rs b/src/api/test.rs index 5cbeaa6ea1..6faa299d5b 100644 --- a/src/api/test.rs +++ b/src/api/test.rs @@ -274,8 +274,9 @@ fn send_test_frame(ctx: &mut Context, content_value: T) { } fn get_frame_invariants( - ctx: Context, + mut ctx: Context, ) -> impl Iterator> { + ctx.inner.compute_fi(); ctx.inner.frame_data.into_iter().map(|(_, v)| v.fi) }