ffmpeg/
error.rs

1// Copyright 2022 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use libc::c_int;
6
7// Equivalent of FFmpeg's `FFERRTAG` macro to generate error codes.
8#[allow(non_snake_case)]
9const fn FFERRTAG(tag: &[u8; 4]) -> c_int {
10    -(tag[0] as c_int | (tag[1] as c_int) << 8 | (tag[2] as c_int) << 16 | (tag[3] as c_int) << 24)
11}
12
13pub const AVERROR_EOF: c_int = FFERRTAG(b"EOF ");
14pub const AVERROR_INVALIDDATA: c_int = FFERRTAG(b"INDA");
15pub const AVERROR_EAGAIN: c_int = -11;
16
17#[cfg(test)]
18mod test {
19    use libc::c_char;
20
21    use super::*;
22    use crate::ffi;
23
24    fn test_averror(averror: c_int, expected_message: &str) {
25        let mut buffer = [0u8; 255];
26        // TODO(b:315859322): add safety doc string
27        #[allow(clippy::undocumented_unsafe_blocks)]
28        let ret =
29            unsafe { ffi::av_strerror(averror, buffer.as_mut_ptr() as *mut c_char, buffer.len()) };
30        assert_eq!(ret, 0);
31
32        let end_of_string = buffer.iter().position(|i| *i == 0).unwrap_or(buffer.len());
33        let error_string = std::str::from_utf8(&buffer[0..end_of_string]).unwrap();
34        assert_eq!(error_string, expected_message);
35    }
36
37    #[test]
38    // Check that we got our error bindings correctly.
39    fn test_error_bindings() {
40        test_averror(AVERROR_EOF, "End of file");
41        test_averror(
42            AVERROR_INVALIDDATA,
43            "Invalid data found when processing input",
44        );
45        test_averror(AVERROR_EAGAIN, "Resource temporarily unavailable");
46    }
47}