use ff_format::VideoFrame;
use super::builder::{VideoEncoder, VideoEncoderBuilder};
use crate::EncodeError;
use crate::async_encoder::{AsyncEncoder, SyncEncoder};
impl SyncEncoder<VideoFrame> for VideoEncoder {
fn push_frame(&mut self, frame: &VideoFrame) -> Result<(), EncodeError> {
self.push_video(frame)
}
fn drain_and_finish(self) -> Result<(), EncodeError> {
self.finish()
}
}
pub struct AsyncVideoEncoder {
inner: AsyncEncoder<VideoFrame>,
}
impl std::fmt::Debug for AsyncVideoEncoder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AsyncVideoEncoder").finish_non_exhaustive()
}
}
impl AsyncVideoEncoder {
pub fn from_builder(builder: VideoEncoderBuilder) -> Result<Self, EncodeError> {
let encoder = builder.build()?;
Ok(Self {
inner: AsyncEncoder::new(encoder),
})
}
pub async fn push(&mut self, frame: VideoFrame) -> Result<(), EncodeError> {
self.inner.push(frame).await
}
pub async fn finish(self) -> Result<(), EncodeError> {
self.inner.finish().await
}
}
#[cfg(test)]
mod tests {
use super::*;
fn _assert_send() {
fn is_send<T: Send>() {}
is_send::<AsyncVideoEncoder>();
}
#[test]
fn from_builder_should_fail_on_invalid_config() {
let result = AsyncVideoEncoder::from_builder(VideoEncoder::create("out.mp4"));
assert!(
result.is_err(),
"expected error for unconfigured builder, got Ok"
);
assert!(
matches!(result.unwrap_err(), EncodeError::InvalidConfig { .. }),
"expected InvalidConfig"
);
}
}