media: ffmpeg: Add AvError::result to convert simple return codes.

We're going to use this pattern more in future CLs, so factor it out to
a helper.

BUG=None
TEST=cargo test -p ffmpeg &&
     cargo test --features "video-decoder,ffmpeg" -p devices video
     + v4l2r test from crosvm book

Change-Id: Ic20555f8a4ebf0403cc2f933c92bb9bb1d3f51f9
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3822432
Reviewed-by: Alexandre Courbot <acourbot@chromium.org>
Commit-Queue: Tatsuyuki Ishi <ishitatsuyuki@google.com>
Tested-by: Tatsuyuki Ishi <ishitatsuyuki@google.com>
This commit is contained in:
Tatsuyuki Ishi 2022-08-10 10:32:46 +09:00 committed by crosvm LUCI
parent 40ffb3708b
commit 894b15044d

View file

@ -24,6 +24,16 @@ use super::*;
#[derive(Debug, ThisError)]
pub struct AvError(pub libc::c_int);
impl AvError {
pub fn result(ret: c_int) -> Result<(), Self> {
if ret >= 0 {
Ok(())
} else {
Err(AvError(ret))
}
}
}
impl Display for AvError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut buffer = [0u8; 255];
@ -370,10 +380,7 @@ impl AvCodecContext {
/// The flush process is complete when `try_receive_frame` returns `FlushCompleted`,
pub fn flush(&mut self) -> Result<(), AvError> {
// Safe because the context is valid through the life of this object.
match unsafe { ffi::avcodec_send_packet(self.0, std::ptr::null()) } {
ret if ret >= 0 => Ok(()),
err => Err(AvError(err)),
}
AvError::result(unsafe { ffi::avcodec_send_packet(self.0, std::ptr::null()) })
}
}