rsmpeg/
error.rs

1//! Errors of the rsmpeg.
2use std::{
3    cmp::{Eq, PartialEq},
4    num::TryFromIntError,
5    os::raw::c_int,
6};
7
8use crate::{avutil::err2str, ffi, shared::AVERROR_EAGAIN};
9
10/// All the error variants of rsmpeg.
11#[non_exhaustive]
12#[derive(thiserror::Error, Debug, Eq, PartialEq)]
13pub enum RsmpegError {
14    #[error("AVERROR({code}): `{msg}`", code = .0, msg = err2str(*.0).unwrap_or_else(|| "Unknown error code.".to_string()))]
15    AVError(c_int),
16
17    // --------- Unstablized error type below ------
18
19    // FFmpeg errors
20    #[error("Cannot open input file. ({0})")]
21    OpenInputError(c_int),
22    #[error("Cannot find stream information. ({0})")]
23    FindStreamInfoError(c_int),
24
25    // Decoder errors
26    #[error("Send packet to a codec context failed. ({0})")]
27    SendPacketError(c_int),
28    #[error("Decoder isn't accepting input, try to receive several frames and send again.")]
29    DecoderFullError,
30    #[error("Receive frame from a codec context failed. ({0})")]
31    ReceiveFrameError(c_int),
32    #[error("Decoder have no frame currently, Try send new input.")]
33    DecoderDrainError,
34    #[error("Decoder is already flushed.")]
35    DecoderFlushedError,
36
37    // Encoder errors
38    #[error("Send frame to a codec context failed. ({0})")]
39    SendFrameError(c_int),
40    #[error("Encoder isn't accepting input, try to receive several packets and send again.")]
41    SendFrameAgainError,
42    #[error("Receive packet from a codec context failed. ({0})")]
43    ReceivePacketError(c_int),
44    #[error("Encoder have no packet currently, Try send new input.")]
45    EncoderDrainError,
46    #[error("Encoder is already flushed.")]
47    EncoderFlushedError,
48
49    // Bitstream errors
50    #[error("Bitstream filter isn't accepting input, receive packets and send again.")]
51    BitstreamFullError,
52    #[error("More packets need to be sent to the bitstream filter.")]
53    BitstreamDrainError,
54    #[error("Bitstream filter is already flushed")]
55    BitstreamFlushedError,
56    #[error("Send packet to a bitstream filter context failed. ({0})")]
57    BitstreamSendPacketError(c_int),
58    #[error("Receive packet from a bitstream filter context failed. ({0})")]
59    BitstreamReceivePacketError(c_int),
60
61    #[error("Pulling filtered frame from filters failed ({0})")]
62    BufferSinkGetFrameError(c_int),
63    #[error("No frames are available at this point")]
64    BufferSinkDrainError,
65    #[error("There will be no more output frames on this sink")]
66    BufferSinkEofError,
67
68    #[error("AVFrame buffer double allocating.")]
69    AVFrameDoubleAllocatingError,
70    #[error("AVFrame buffer allocating with incorrect parameters. ({0})")]
71    AVFrameInvalidAllocatingError(c_int),
72
73    #[error("{0}")]
74    TryFromIntError(TryFromIntError),
75
76    // Non exhaustive
77    #[error("Unknown error.")]
78    Unknown,
79}
80
81impl RsmpegError {
82    #[must_use]
83    pub fn raw_error(&self) -> Option<c_int> {
84        match self {
85            Self::AVError(err)
86            | Self::OpenInputError(err)
87            | Self::FindStreamInfoError(err)
88            | Self::SendPacketError(err)
89            | Self::ReceiveFrameError(err)
90            | Self::SendFrameError(err)
91            | Self::ReceivePacketError(err)
92            | Self::BitstreamSendPacketError(err)
93            | Self::BitstreamReceivePacketError(err)
94            | Self::BufferSinkGetFrameError(err)
95            | Self::AVFrameInvalidAllocatingError(err) => Some(*err),
96
97            Self::DecoderFullError
98            | Self::BufferSinkDrainError
99            | Self::DecoderDrainError
100            | Self::SendFrameAgainError
101            | Self::BitstreamFullError
102            | Self::BitstreamDrainError
103            | Self::EncoderDrainError => Some(AVERROR_EAGAIN),
104
105            Self::BufferSinkEofError
106            | Self::DecoderFlushedError
107            | Self::EncoderFlushedError
108            | Self::BitstreamFlushedError => Some(ffi::AVERROR_EOF),
109
110            Self::AVFrameDoubleAllocatingError | Self::TryFromIntError(_) | Self::Unknown => None,
111        }
112    }
113}
114
115/// Overall result of Rsmpeg functions
116pub type Result<T, E = RsmpegError> = std::result::Result<T, E>;
117
118/// A wrapper around c_int(return type of many ffmpeg inner libraries functions)
119pub type Ret = std::result::Result<c_int, c_int>;
120
121impl From<c_int> for RsmpegError {
122    fn from(err: c_int) -> Self {
123        Self::AVError(err)
124    }
125}
126
127impl From<TryFromIntError> for RsmpegError {
128    fn from(err: TryFromIntError) -> Self {
129        Self::TryFromIntError(err)
130    }
131}