|
avcpp 3.1.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
|
Every operation that can fail follows one convention: it takes an OptionalErrorCode as its last parameter. The convention removes the need for two overloads of every function, and it lets the caller choose between exceptions and error codes per call site — not per library.
av::throws() is the default argument, and it is an empty holder: no variable was passed, so the library throws av::Exception instead of storing a code. Passing a std::error_code& gives you a non-owning view of it: the variable must outlive the call, which it does in every normal pattern.
The two styles are exclusive inside one call. Mixing them in one program is fine — pick the style per boundary: exceptions inside an application, error codes at an API edge that has to stay noexcept.
if (ec) is the test. An std::error_code that was never set compares equal to std::error_code{} and its value() is 0.
Most functions call clear_if(ec) on entry, so a code from a previous call does not leak into the next result. This is why a loop can reuse one std::error_code for the whole pipeline, and why example/api2-samples/api2-send-receive.cpp checks ec after each step without clearing it by hand.
Two categories exist:
| Category | Origin | Example codes |
|---|---|---|
| av::avcpp_category() | checks made by the library itself | Errors::CodecAlreadyOpened, Errors::FormatCodecUnsupported |
| av::ffmpeg_category() | return values of FFmpeg calls | AVERROR(EAGAIN), AVERROR(ENOSYS) |
av::Errors is an enum with an std::error_code adapter, so comparison is direct:
Codes you will meet in practice:
| Code | Meaning | Typical reaction |
|---|---|---|
| CodecAlreadyOpened | a setting was changed after open() | move the setting before open() |
| CodecIsNotOpened | an operation needs an open codec | call open() first |
| FormatCodecUnsupported | the container cannot store the codec | skip the stream or re-encode |
| FormatNoStreams | the output context has no streams | add streams before writeHeader() |
| FormatHeaderNotWriten | packets were written before the header | call writeHeader() first |
| FrameInvalid | an operation on an empty frame | check isValid() before using it |
| HardwareTransferUnsupported | a transfer is impossible | see Hardware decoding and encoding |
| ResamplerInputChanges | the input layout changed after initialisation | create a new resampler |
av::Exception derives from std::system_error, so code() gives the same std::error_code the non-throwing style would have produced. There is no separate exception type per failure, and no string matching is ever needed.
The convention is small enough to follow in application code:
throws_if() throws or stores the code depending on what the caller passed, and clear_if() clears it. Together they are the whole protocol, and using them keeps your code indistinguishable from the library's own.