scuffle_ffmpeg/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use ffmpeg_sys_next::*;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FfmpegError {
	Alloc,
	Code(FfmpegErrorCode),
	NoDecoder,
	NoEncoder,
	NoStream,
	NoFilter,
	NoFrame,
	Arguments(&'static str),
}

pub(crate) const AVERROR_EAGAIN: i32 = AVERROR(EAGAIN);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FfmpegErrorCode {
	EndOfFile,
	InvalidData,
	MuxerNotFound,
	OptionNotFound,
	PatchWelcome,
	ProtocolNotFound,
	StreamNotFound,
	BitstreamFilterNotFound,
	Bug,
	BufferTooSmall,
	DecoderNotFound,
	DemuxerNotFound,
	EncoderNotFound,
	Exit,
	External,
	FilterNotFound,
	HttpBadRequest,
	HttpForbidden,
	HttpNotFound,
	HttpOther4xx,
	HttpServerError,
	HttpUnauthorized,
	Bug2,
	Unknown,
	UnknownError(i32),
}

impl std::fmt::Display for FfmpegErrorCode {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::EndOfFile => write!(f, "end of file"),
			Self::InvalidData => write!(f, "invalid data"),
			Self::MuxerNotFound => write!(f, "muxer not found"),
			Self::OptionNotFound => write!(f, "option not found"),
			Self::PatchWelcome => write!(f, "patch welcome"),
			Self::ProtocolNotFound => write!(f, "protocol not found"),
			Self::StreamNotFound => write!(f, "stream not found"),
			Self::BitstreamFilterNotFound => write!(f, "bitstream filter not found"),
			Self::Bug => write!(f, "bug"),
			Self::BufferTooSmall => write!(f, "buffer too small"),
			Self::DecoderNotFound => write!(f, "decoder not found"),
			Self::DemuxerNotFound => write!(f, "demuxer not found"),
			Self::EncoderNotFound => write!(f, "encoder not found"),
			Self::Exit => write!(f, "exit"),
			Self::External => write!(f, "external"),
			Self::FilterNotFound => write!(f, "filter not found"),
			Self::HttpBadRequest => write!(f, "http bad request"),
			Self::HttpForbidden => write!(f, "http forbidden"),
			Self::HttpNotFound => write!(f, "http not found"),
			Self::HttpOther4xx => write!(f, "http other 4xx"),
			Self::HttpServerError => write!(f, "http server error"),
			Self::HttpUnauthorized => write!(f, "http unauthorized"),
			Self::Bug2 => write!(f, "bug2"),
			Self::Unknown => write!(f, "unknown"),
			Self::UnknownError(ec) => write!(f, "unknown error code: {ec}"),
		}
	}
}

impl From<i32> for FfmpegErrorCode {
	fn from(value: i32) -> Self {
		match value {
			AVERROR_EOF => FfmpegErrorCode::EndOfFile,
			AVERROR_INVALIDDATA => FfmpegErrorCode::InvalidData,
			AVERROR_MUXER_NOT_FOUND => FfmpegErrorCode::MuxerNotFound,
			AVERROR_OPTION_NOT_FOUND => FfmpegErrorCode::OptionNotFound,
			AVERROR_PATCHWELCOME => FfmpegErrorCode::PatchWelcome,
			AVERROR_PROTOCOL_NOT_FOUND => FfmpegErrorCode::ProtocolNotFound,
			AVERROR_STREAM_NOT_FOUND => FfmpegErrorCode::StreamNotFound,
			AVERROR_BSF_NOT_FOUND => FfmpegErrorCode::BitstreamFilterNotFound,
			AVERROR_BUG => FfmpegErrorCode::Bug,
			AVERROR_BUFFER_TOO_SMALL => FfmpegErrorCode::BufferTooSmall,
			AVERROR_DECODER_NOT_FOUND => FfmpegErrorCode::DecoderNotFound,
			AVERROR_DEMUXER_NOT_FOUND => FfmpegErrorCode::DemuxerNotFound,
			AVERROR_ENCODER_NOT_FOUND => FfmpegErrorCode::EncoderNotFound,
			AVERROR_EXIT => FfmpegErrorCode::Exit,
			AVERROR_EXTERNAL => FfmpegErrorCode::External,
			AVERROR_FILTER_NOT_FOUND => FfmpegErrorCode::FilterNotFound,
			AVERROR_HTTP_BAD_REQUEST => FfmpegErrorCode::HttpBadRequest,
			AVERROR_HTTP_FORBIDDEN => FfmpegErrorCode::HttpForbidden,
			AVERROR_HTTP_NOT_FOUND => FfmpegErrorCode::HttpNotFound,
			AVERROR_HTTP_OTHER_4XX => FfmpegErrorCode::HttpOther4xx,
			AVERROR_HTTP_SERVER_ERROR => FfmpegErrorCode::HttpServerError,
			AVERROR_HTTP_UNAUTHORIZED => FfmpegErrorCode::HttpUnauthorized,
			AVERROR_BUG2 => FfmpegErrorCode::Bug2,
			AVERROR_UNKNOWN => FfmpegErrorCode::Unknown,
			_ => FfmpegErrorCode::UnknownError(value),
		}
	}
}

impl std::error::Error for FfmpegError {}

impl std::fmt::Display for FfmpegError {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			FfmpegError::Alloc => write!(f, "failed to allocate memory"),
			FfmpegError::Code(code) => write!(f, "ffmpeg error: {code}"),
			FfmpegError::NoDecoder => write!(f, "no decoder found"),
			FfmpegError::NoEncoder => write!(f, "no encoder found"),
			FfmpegError::NoStream => write!(f, "no stream found"),
			FfmpegError::NoFilter => write!(f, "no filter found"),
			FfmpegError::NoFrame => write!(f, "no frame found"),
			FfmpegError::Arguments(msg) => write!(f, "invalid arguments: {}", msg),
		}
	}
}