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// ── Network errno values (AVERROR = -errno on POSIX) ─────────────────────────
23//
24// These are used in ff-decode to map FFmpeg network errors to typed variants.
25// errno numbering differs across platforms:
26//   - macOS/BSD: uses BSD errno values
27//   - Windows UCRT: uses its own POSIX-extension errno table (VS2015+)
28//   - Linux: standard POSIX errno values
29//
30// Windows UCRT errno.h (relevant socket codes, added in VS2015/UCRT):
31//   ECONNREFUSED=107, EHOSTUNREACH=110, ETIMEDOUT=138, ENETUNREACH=118
32
33/// Connection timed out (`ETIMEDOUT`).
34#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd"))]
35pub const ETIMEDOUT: i32 = -60;
36#[cfg(windows)]
37pub const ETIMEDOUT: i32 = -138; // UCRT: ETIMEDOUT = 138
38#[cfg(not(any(
39    windows,
40    target_os = "macos",
41    target_os = "freebsd",
42    target_os = "openbsd"
43)))]
44pub const ETIMEDOUT: i32 = -110;
45
46/// Connection refused (`ECONNREFUSED`).
47#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd"))]
48pub const ECONNREFUSED: i32 = -61;
49#[cfg(windows)]
50pub const ECONNREFUSED: i32 = -107; // UCRT: ECONNREFUSED = 107
51#[cfg(not(any(
52    windows,
53    target_os = "macos",
54    target_os = "freebsd",
55    target_os = "openbsd"
56)))]
57pub const ECONNREFUSED: i32 = -111;
58
59/// No route to host (`EHOSTUNREACH`).
60#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd"))]
61pub const EHOSTUNREACH: i32 = -65;
62#[cfg(windows)]
63pub const EHOSTUNREACH: i32 = -110; // UCRT: EHOSTUNREACH = 110
64#[cfg(not(any(
65    windows,
66    target_os = "macos",
67    target_os = "freebsd",
68    target_os = "openbsd"
69)))]
70pub const EHOSTUNREACH: i32 = -113;
71
72/// Network unreachable (`ENETUNREACH`).
73#[cfg(any(target_os = "macos", target_os = "freebsd", target_os = "openbsd"))]
74pub const ENETUNREACH: i32 = -51;
75#[cfg(windows)]
76pub const ENETUNREACH: i32 = -118; // UCRT: ENETUNREACH = 118
77#[cfg(not(any(
78    windows,
79    target_os = "macos",
80    target_os = "freebsd",
81    target_os = "openbsd"
82)))]
83pub const ENETUNREACH: i32 = -101;
84
85/// I/O error (`EIO`). Same value on all POSIX platforms.
86pub const EIO: i32 = -5;
87
88/// Invalid data found when processing input (`AVERROR_INVALIDDATA`).
89///
90/// This is `FFERRTAG(0xF8,'I','N','V')` — a tag-based code, not an errno,
91/// so it has the same value on all platforms.
92pub const AVERROR_INVALIDDATA: i32 = -1_447_971_320;
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    fn error_codes_should_all_be_negative() {
100        assert!(EAGAIN < 0);
101        assert!(EOF < 0);
102        assert!(ENOMEM < 0);
103        assert!(EINVAL < 0);
104    }
105
106    #[test]
107    fn averror_invaliddata_should_be_negative_and_distinct_from_einval() {
108        assert!(AVERROR_INVALIDDATA < 0);
109        assert_ne!(AVERROR_INVALIDDATA, EINVAL);
110    }
111
112    #[test]
113    fn averror_invaliddata_should_have_expected_value() {
114        // FFERRTAG(0xF8,'I','N','V') = -(0xF8 | 'I'<<8 | 'N'<<16 | 'V'<<24)
115        assert_eq!(AVERROR_INVALIDDATA, -1_447_971_320);
116    }
117}