avcpp 3.2.1
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
averror.h
Go to the documentation of this file.
1#pragma once
2
9
10#include <system_error>
11#include <exception>
12
13namespace av
14{
15
22enum class Errors
23{
24 NoError = 0,
25 Generic,
26 Unallocated,
27 InvalidArgument,
28 OutOfRange,
29 CantAllocateFrame,
30 CodecStreamInvalid,
31 CodecInvalidDirection,
32 CodecAlreadyOpened,
33 CodecIsNotOpened,
34 CodecInvalid,
35 CodecNotOpened,
36 CodecInvalidDecodeProc,
37 CodecInvalidEncodeProc,
38 CodecDecodingOffsetToLarge,
39 CodecInvalidForEncode,
40 CodecInvalidForDecoce,
41 CodecInvalidMediaType,
42 FrameInvalid,
43 DictOutOfRage,
44 DictNoKey,
45
46 BufferReadonly,
47
48 FormatCantAddStream,
49 FormatAlreadyOpened,
50 FormatNullOutputFormat,
51 FormatWrongCountOfStreamOptions,
52 FormatNoStreams,
53 FormatInvalidStreamIndex,
54 FormatNotOpened,
55 FormatInvalidDirection,
56 FormatHeaderNotWriten,
57 FormatCodecUnsupported,
58
59 ResamplerInvalidParameters,
60 ResamplerNotInited,
61 ResamplerInputChanges,
62 ResamplerOutputChanges,
63
64 RescalerInvalidParameters,
65 RescalerInternalSwsError,
66
67 FilterNotInFilterGraph,
68 FilterGraphDescriptionEmpty,
69
70 IncorrectBufferSrcFilter,
71 IncorrectBufferSrcMediaType,
72
73 IncorrectBufferSinkFilter,
74 IncorrectBufferSinkMediaType,
75 MixBufferSinkAccess,
76
77 CodecUnsupportedOperation,
78
79 HardwareDeviceUnavailable,
80 HardwareDeviceInvalid,
81 HardwareConfigNotFound,
82 HardwareFramesInvalid,
83 HardwareTransferUnsupported,
84 HardwareCodecNotFound,
85};
86
95class OptionalErrorCode
96{
97 OptionalErrorCode() {}
98
99public:
105 OptionalErrorCode(std::error_code &ec);
106
112 static OptionalErrorCode null();
113
119 operator bool() const;
120
126 std::error_code& operator*();
127
128private:
129 std::error_code *m_ec = nullptr;
130};
131
138class Exception : public std::system_error
139{
140public:
141 using std::system_error::system_error;
142};
143
148class AvcppCategory : public std::error_category
149{
150public:
151
157 const char *name() const noexcept override;
158
166 std::string message(int ev) const override;
167};
168
173class FfmpegCategory : public std::error_category
174{
175 virtual const char *name() const noexcept override;
176 virtual std::string message(int ev) const override;
177};
178
184inline
186{
187 static AvcppCategory res;
188 return res;
189}
190
197{
198 static FfmpegCategory res;
199 return res;
200}
201
202inline
210std::error_condition make_error_condition(av::Errors errc) noexcept
211{
212 return std::error_condition(static_cast<int>(errc), av::avcpp_category());
213}
214
215inline
223std::error_code make_error_code(av::Errors errc) noexcept
224{
225 return std::error_code(static_cast<int>(errc), av::avcpp_category());
226}
227
228inline
236std::error_code make_avcpp_error(Errors code)
237{
238 return std::error_code(static_cast<int>(code), avcpp_category());
239}
240
241inline
249std::error_condition make_avcpp_condition(Errors code)
250{
251 return std::error_condition(static_cast<int>(code), avcpp_category());
252}
253
254inline
262std::error_code make_ffmpeg_error(int code)
263{
264 return std::error_code(code, ffmpeg_category());
265}
266
267inline
275std::error_condition make_ffmpeg_condition(int code)
276{
277 return std::error_condition(code, ffmpeg_category());
278}
279
280template<typename Exception = av::Exception>
288void throw_error_code(const std::error_code& ec)
289{
290 throw Exception(ec);
291}
292
293template<typename Exception = av::Exception>
302{
303 throw Exception(make_error_code(errc));
304}
305
310inline OptionalErrorCode throws()
311{
313}
314
322template<typename Category, typename Exception = av::Exception>
323void throws_if(OptionalErrorCode ec, int errcode, const Category &cat)
324{
325 if (ec)
326 *ec = std::error_code(errcode, cat);
327 else
328 throw Exception(std::error_code(errcode, cat));
329}
330
331template<typename T, typename Exception = av::Exception>
340void throws_if(OptionalErrorCode ec, T errcode)
341{
342 if (ec)
343 *ec = make_error_code(errcode);
344 else
345 throw Exception(make_error_code(errcode));
346}
347
354static inline void throws_if(OptionalErrorCode ec, const std::error_code& errcode)
355{
356 if (ec)
357 *ec = errcode;
358 else
359 throw Exception(errcode);
360}
361
362
367inline
369{
370 if (ec)
371 (*ec).clear();
372}
373
374inline
383{
384 if (ec)
385 return static_cast<bool>(*ec);
386
387 return false;
388}
389
390} // ::av
391
392namespace std {
393template<> struct is_error_condition_enum<av::Errors> : public true_type {};
394// Commented out for future invertigations. Note, for correct comparation, users error enum must
395// declared only as is_error_condition_enum<> or is_error_code_enum<>
396//template<> struct is_error_code_enum<av::AvError> : public true_type {};
397}
398
The AvcppCategory class Describes internal AvCpp errors.
Definition averror.h:149
std::string message(int ev) const override
Text for an error value.
const char * name() const noexcept override
Name of the category.
The AvException class Default exception that thows from function that does not accept error code stor...
Definition averror.h:139
The FfmpegCategory class Describers FFmpeg internal errors.
Definition averror.h:174
Carries the caller's choice between exceptions and error codes.
Definition averror.h:96
std::error_code & operator*()
The caller's variable.
OptionalErrorCode(std::error_code &ec)
Report errors through the given variable.
static OptionalErrorCode null()
The value that means "the caller wants no error code".
Root namespace of the library.
Definition audioresampler.h:19
void clear_if(OptionalErrorCode ec)
clear_if - clear error code if it is not av::throws()
Definition averror.h:368
void throw_error_code(const std::error_code &ec)
Throw an exception carrying the given error code.
Definition averror.h:288
bool is_error(OptionalErrorCode ec)
Whether an error was stored in the caller's variable.
Definition averror.h:382
std::error_code make_avcpp_error(Errors code)
Build an error code from an avcpp error.
Definition averror.h:236
std::error_code make_error_code(av::Errors errc) noexcept
Build an error code from an avcpp error.
Definition averror.h:223
std::error_condition make_ffmpeg_condition(int code)
Build an error condition from an FFmpeg return value.
Definition averror.h:275
std::error_code make_ffmpeg_error(int code)
Build an error code from an FFmpeg return value.
Definition averror.h:262
Errors
Errors the library raises itself, as opposed to FFmpeg's return codes.
Definition averror.h:23
FfmpegCategory & ffmpeg_category()
Error category that wraps FFmpeg return codes.
Definition averror.h:196
const AvcppCategory & avcpp_category()
Error category of the library's own errors.
Definition averror.h:185
std::error_condition make_error_condition(av::Errors errc) noexcept
Build an error condition from an avcpp error.
Definition averror.h:210
void throws_if(OptionalErrorCode ec, int errcode, const Category &cat)
Throw or store an error, depending on what the caller passed.
Definition averror.h:323
std::error_condition make_avcpp_condition(Errors code)
Build an error condition from an avcpp error.
Definition averror.h:249
STL namespace.