-
Notifications
You must be signed in to change notification settings - Fork 80
/
averror.h
234 lines (195 loc) · 4.79 KB
/
averror.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#pragma once
#include <system_error>
#include <exception>
namespace av
{
enum class Errors
{
NoError = 0,
Generic,
Unallocated,
InvalidArgument,
OutOfRange,
CantAllocateFrame,
CodecStreamInvalid,
CodecInvalidDirection,
CodecAlreadyOpened,
CodecIsNotOpened,
CodecInvalid,
CodecNotOpened,
CodecInvalidDecodeProc,
CodecInvalidEncodeProc,
CodecDecodingOffsetToLarge,
CodecInvalidForEncode,
CodecInvalidForDecoce,
CodecInvalidMediaType,
FrameInvalid,
DictOutOfRage,
DictNoKey,
FormatCantAddStream,
FormatAlreadyOpened,
FormatNullOutputFormat,
FormatWrongCountOfStreamOptions,
FormatNoStreams,
FormatInvalidStreamIndex,
FormatNotOpened,
FormatInvalidDirection,
FormatHeaderNotWriten,
FormatCodecUnsupported,
ResamplerInvalidParameters,
ResamplerNotInited,
ResamplerInputChanges,
ResamplerOutputChanges,
RescalerInvalidParameters,
RescalerInternalSwsError,
FilterNotInFilterGraph,
FilterGraphDescriptionEmpty,
IncorrectBufferSrcFilter,
IncorrectBufferSrcMediaType,
IncorrectBufferSinkFilter,
IncorrectBufferSinkMediaType,
MixBufferSinkAccess,
};
class OptionalErrorCode
{
OptionalErrorCode() {}
public:
OptionalErrorCode(std::error_code &ec);
static OptionalErrorCode null();
operator bool() const;
std::error_code& operator*();
private:
std::error_code *m_ec = nullptr;
};
/**
* @brief The AvException class
* Default exception that thows from function that does not accept error code storage.
*
* Simple wrap error code value and category
*/
class Exception : public std::system_error
{
public:
using std::system_error::system_error;
};
/**
* @brief The AvcppCategory class
* Describes internal AvCpp errors
*/
class AvcppCategory : public std::error_category
{
public:
virtual const char *name() const noexcept override;
virtual std::string message(int ev) const override;
};
/**
* @brief The FfmpegCategory class
* Describers FFmpeg internal errors
*/
class FfmpegCategory : public std::error_category
{
virtual const char *name() const noexcept override;
virtual std::string message(int ev) const override;
};
inline
const AvcppCategory& avcpp_category()
{
static AvcppCategory res;
return res;
}
inline FfmpegCategory& ffmpeg_category()
{
static FfmpegCategory res;
return res;
}
inline
std::error_condition make_error_condition(av::Errors errc) noexcept
{
return std::error_condition(static_cast<int>(errc), av::avcpp_category());
}
inline
std::error_code make_error_code(av::Errors errc) noexcept
{
return std::error_code(static_cast<int>(errc), av::avcpp_category());
}
inline
std::error_code make_avcpp_error(Errors code)
{
return std::error_code(static_cast<int>(code), avcpp_category());
}
inline
std::error_condition make_avcpp_condition(Errors code)
{
return std::error_condition(static_cast<int>(code), avcpp_category());
}
inline
std::error_code make_ffmpeg_error(int code)
{
return std::error_code(code, ffmpeg_category());
}
inline
std::error_condition make_ffmpeg_condition(int code)
{
return std::error_condition(code, ffmpeg_category());
}
template<typename Exception = av::Exception>
void throw_error_code(const std::error_code& ec)
{
throw Exception(ec);
}
template<typename Exception = av::Exception>
void throw_error_code(Errors errc)
{
throw Exception(make_error_code(errc));
}
/**
* @brief Helper to construct null OptionalErrorCode object
* @return null OptionalErrorCode object
*/
inline OptionalErrorCode throws()
{
return OptionalErrorCode::null();
}
/**
* @brief Throws exception if ec is av::throws() or fill error code
*/
template<typename Category, typename Exception = av::Exception>
void throws_if(OptionalErrorCode ec, int errcode, const Category &cat)
{
if (ec)
*ec = std::error_code(errcode, cat);
else
throw Exception(std::error_code(errcode, cat));
}
template<typename T, typename Exception = av::Exception>
void throws_if(OptionalErrorCode ec, T errcode)
{
if (ec)
*ec = make_error_code(errcode);
else
throw Exception(make_error_code(errcode));
}
/**
* @brief clear_if - clear error code if it is not av::throws()
* @param ec error code to clear
*/
inline
void clear_if(OptionalErrorCode ec)
{
if (ec)
(*ec).clear();
}
inline
bool is_error(OptionalErrorCode ec)
{
if (ec)
return static_cast<bool>(*ec);
return false;
}
} // ::av
namespace std {
template<> struct is_error_condition_enum<av::Errors> : public true_type {};
// Commented out for future invertigations. Note, for correct comparation, users error enum must
// declared only as is_error_condition_enum<> or is_error_code_enum<>
//template<> struct is_error_code_enum<av::AvError> : public true_type {};
}