Skip to main content

ff_sys/
error_codes.rs

1//! Common FFmpeg error codes for convenience.
2
3/// Resource temporarily unavailable (try again).
4///
5/// `AVERROR(EAGAIN)` is platform-specific:
6/// - Linux/Windows (MinGW): `EAGAIN = 11`, so `AVERROR(EAGAIN) = -11`
7/// - macOS/BSD: `EAGAIN = 35`, so `AVERROR(EAGAIN) = -35`
8#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd"))]
9pub const EAGAIN: i32 = -35;
10#[cfg(not(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd")))]
11pub const EAGAIN: i32 = -11;
12
13/// End of file/stream
14pub const EOF: i32 = -541_478_725; // AVERROR_EOF
15
16/// Out of memory
17pub const ENOMEM: i32 = -12;
18
19/// Invalid data
20pub const EINVAL: i32 = -22;
21
22/// Bitstream filter not found (`AVERROR_BSF_NOT_FOUND`).
23///
24/// `FFERRTAG(0xF8, 'B', 'S', 'F')` = `-(0xF8 | 'B' << 8 | 'S' << 16 | 'F' << 24)`.
25/// Unlike the `errno`-derived codes above this is a fixed FFmpeg tag, so it is the
26/// same on every platform.
27pub const BSF_NOT_FOUND: i32 = -1_179_861_752;
28
29// Network errno values (AVERROR = -errno on POSIX)
30//
31// These are used in ff-decode to map FFmpeg network errors to typed variants.
32// errno numbering differs across platforms:
33//   - macOS/BSD: uses BSD errno values
34//   - Windows UCRT: uses its own POSIX-extension errno table (VS2015+)
35//   - Linux: standard POSIX errno values
36//
37// Windows UCRT errno.h (relevant socket codes, added in VS2015/UCRT):
38//   ECONNREFUSED=107, EHOSTUNREACH=110, ETIMEDOUT=138, ENETUNREACH=118
39
40/// Connection timed out (`ETIMEDOUT`).
41#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd"))]
42pub const ETIMEDOUT: i32 = -60;
43#[cfg(windows)]
44pub const ETIMEDOUT: i32 = -138; // UCRT: ETIMEDOUT = 138
45#[cfg(not(any(
46    windows,
47    target_os = "macos",
48    target_os = "freebsd",
49    target_os = "openbsd"
50)))]
51pub const ETIMEDOUT: i32 = -110;
52
53/// Connection refused (`ECONNREFUSED`).
54#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd"))]
55pub const ECONNREFUSED: i32 = -61;
56#[cfg(windows)]
57pub const ECONNREFUSED: i32 = -107; // UCRT: ECONNREFUSED = 107
58#[cfg(not(any(
59    windows,
60    target_os = "macos",
61    target_os = "freebsd",
62    target_os = "openbsd"
63)))]
64pub const ECONNREFUSED: i32 = -111;
65
66/// No route to host (`EHOSTUNREACH`).
67#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd"))]
68pub const EHOSTUNREACH: i32 = -65;
69#[cfg(windows)]
70pub const EHOSTUNREACH: i32 = -110; // UCRT: EHOSTUNREACH = 110
71#[cfg(not(any(
72    windows,
73    target_os = "macos",
74    target_os = "freebsd",
75    target_os = "openbsd"
76)))]
77pub const EHOSTUNREACH: i32 = -113;
78
79/// Network unreachable (`ENETUNREACH`).
80#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd"))]
81pub const ENETUNREACH: i32 = -51;
82#[cfg(windows)]
83pub const ENETUNREACH: i32 = -118; // UCRT: ENETUNREACH = 118
84#[cfg(not(any(
85    windows,
86    target_os = "macos",
87    target_os = "freebsd",
88    target_os = "openbsd"
89)))]
90pub const ENETUNREACH: i32 = -101;
91
92/// I/O error (`EIO`). Same value on all POSIX platforms.
93pub const EIO: i32 = -5;
94
95/// Invalid data found when processing input (`AVERROR_INVALIDDATA`).
96///
97/// This is `FFERRTAG(0xF8,'I','N','V')` — a tag-based code, not an errno,
98/// so it has the same value on all platforms.
99pub const AVERROR_INVALIDDATA: i32 = -1_447_971_320;
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn error_codes_should_all_be_negative() {
107        assert!(EAGAIN < 0);
108        assert!(EOF < 0);
109        assert!(ENOMEM < 0);
110        assert!(EINVAL < 0);
111    }
112
113    #[test]
114    fn averror_invaliddata_should_be_negative_and_distinct_from_einval() {
115        assert!(AVERROR_INVALIDDATA < 0);
116        assert_ne!(AVERROR_INVALIDDATA, EINVAL);
117    }
118
119    #[test]
120    fn averror_invaliddata_should_have_expected_value() {
121        // FFERRTAG(0xF8,'I','N','V') = -(0xF8 | 'I'<<8 | 'N'<<16 | 'V'<<24)
122        assert_eq!(AVERROR_INVALIDDATA, -1_447_971_320);
123    }
124}