]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/ffmpeg_error.h
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / modules / ffmpeg / ffmpeg_error.h
1 #pragma once\r
2 \r
3 #include <common/exception/exceptions.h>\r
4 #include <common/utility/string.h>\r
5 \r
6 #include <string>\r
7 \r
8 #pragma warning(push, 1)\r
9 \r
10 extern "C" \r
11 {\r
12 #include <libavutil/error.h>\r
13 }\r
14 \r
15 #pragma warning(pop)\r
16 \r
17 namespace caspar { namespace ffmpeg {\r
18 \r
19 struct ffmpeg_error : virtual caspar_exception{};\r
20 struct averror_bsf_not_found : virtual ffmpeg_error{};\r
21 struct averror_decoder_not_found : virtual ffmpeg_error{};\r
22 struct averror_demuxer_not_found : virtual ffmpeg_error{};\r
23 struct averror_encoder_not_found : virtual ffmpeg_error{};\r
24 struct averror_eof : virtual ffmpeg_error{};\r
25 struct averror_exit : virtual ffmpeg_error{};\r
26 struct averror_filter_not_found : virtual ffmpeg_error{};\r
27 struct averror_muxer_not_found : virtual ffmpeg_error{};\r
28 struct averror_option_not_found : virtual ffmpeg_error{};\r
29 struct averror_patchwelcome : virtual ffmpeg_error{};\r
30 struct averror_protocol_not_found : virtual ffmpeg_error{};\r
31 struct averror_stream_not_found : virtual ffmpeg_error{};\r
32 \r
33 static std::string av_error_str(int errn)\r
34 {\r
35         char buf[256];\r
36         memset(buf, 0, 256);\r
37         if(av_strerror(errn, buf, 256) < 0)\r
38                 return "";\r
39         return std::string(buf);\r
40 }\r
41 \r
42 static void throw_on_ffmpeg_error(int ret, const char* source, const char* func, const char* local_func, const char* file, int line)\r
43 {\r
44         if(ret >= 0)\r
45                 return;\r
46 \r
47         switch(ret)\r
48         {\r
49         case AVERROR_BSF_NOT_FOUND:\r
50                 ::boost::exception_detail::throw_exception_(averror_bsf_not_found()<<                                                                           \r
51                         msg_info(av_error_str(ret)) <<                                                  \r
52                         source_info(narrow(source)) <<                                          \r
53                         boost::errinfo_api_function(func) <<                                    \r
54                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);  \r
55         case AVERROR_DECODER_NOT_FOUND:\r
56                 ::boost::exception_detail::throw_exception_(averror_decoder_not_found()<<                                                                               \r
57                         msg_info(av_error_str(ret)) <<                                                  \r
58                         source_info(narrow(source)) <<                                          \r
59                         boost::errinfo_api_function(func) <<                                    \r
60                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);\r
61         case AVERROR_DEMUXER_NOT_FOUND:\r
62                 ::boost::exception_detail::throw_exception_(averror_demuxer_not_found()<<                                                                               \r
63                         msg_info(av_error_str(ret)) <<                                                  \r
64                         source_info(narrow(source)) <<                                          \r
65                         boost::errinfo_api_function(func) <<                                    \r
66                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);\r
67         case AVERROR_ENCODER_NOT_FOUND:\r
68                 ::boost::exception_detail::throw_exception_(averror_encoder_not_found()<<                                                                               \r
69                         msg_info(av_error_str(ret)) <<                                                  \r
70                         source_info(narrow(source)) <<                                          \r
71                         boost::errinfo_api_function(func) <<                                    \r
72                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);  \r
73         case AVERROR_EOF:       \r
74                 ::boost::exception_detail::throw_exception_(averror_eof()<<                                                                             \r
75                         msg_info(av_error_str(ret)) <<                                                  \r
76                         source_info(narrow(source)) <<                                          \r
77                         boost::errinfo_api_function(func) <<                                    \r
78                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);\r
79         case AVERROR_EXIT:                              \r
80                 ::boost::exception_detail::throw_exception_(averror_exit()<<                                                                            \r
81                         msg_info(av_error_str(ret)) <<                                                  \r
82                         source_info(narrow(source)) <<                                          \r
83                         boost::errinfo_api_function(func) <<                                    \r
84                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);\r
85         case AVERROR_FILTER_NOT_FOUND:                          \r
86                 ::boost::exception_detail::throw_exception_(averror_filter_not_found()<<                                                                                \r
87                         msg_info(av_error_str(ret)) <<                                                  \r
88                         source_info(narrow(source)) <<                                          \r
89                         boost::errinfo_api_function(func) <<                                    \r
90                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);\r
91         case AVERROR_MUXER_NOT_FOUND:   \r
92                 ::boost::exception_detail::throw_exception_(averror_muxer_not_found()<<                                                                         \r
93                         msg_info(av_error_str(ret)) <<                                                  \r
94                         source_info(narrow(source)) <<                                          \r
95                         boost::errinfo_api_function(func) <<                                    \r
96                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);\r
97         case AVERROR_OPTION_NOT_FOUND:  \r
98                 ::boost::exception_detail::throw_exception_(averror_option_not_found()<<                                                                                \r
99                         msg_info(av_error_str(ret)) <<                                                  \r
100                         source_info(narrow(source)) <<                                          \r
101                         boost::errinfo_api_function(func) <<                                    \r
102                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);\r
103         case AVERROR_PATCHWELCOME:      \r
104                 ::boost::exception_detail::throw_exception_(averror_patchwelcome()<<                                                                            \r
105                         msg_info(av_error_str(ret)) <<                                                  \r
106                         source_info(narrow(source)) <<                                          \r
107                         boost::errinfo_api_function(func) <<                                    \r
108                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);\r
109         case AVERROR_PROTOCOL_NOT_FOUND:        \r
110                 ::boost::exception_detail::throw_exception_(averror_protocol_not_found()<<                                                                              \r
111                         msg_info(av_error_str(ret)) <<                                                  \r
112                         source_info(narrow(source)) <<                                          \r
113                         boost::errinfo_api_function(func) <<                                    \r
114                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);\r
115         case AVERROR_STREAM_NOT_FOUND:\r
116                 ::boost::exception_detail::throw_exception_(averror_stream_not_found()<<                                                                                \r
117                         msg_info(av_error_str(ret)) <<                                                  \r
118                         source_info(narrow(source)) <<                                          \r
119                         boost::errinfo_api_function(func) <<                                    \r
120                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);\r
121         default:\r
122                 ::boost::exception_detail::throw_exception_(ffmpeg_error()<<                                                                            \r
123                         msg_info(av_error_str(ret)) <<                                                  \r
124                         source_info(narrow(source)) <<                                          \r
125                         boost::errinfo_api_function(func) <<                                    \r
126                         boost::errinfo_errno(AVUNERROR(ret)), local_func, file, line);\r
127         }\r
128 }\r
129 \r
130 static void throw_on_ffmpeg_error(int ret, const std::wstring& source, const char* func, const char* local_func, const char* file, int line)\r
131 {\r
132         throw_on_ffmpeg_error(ret, narrow(source).c_str(), func, local_func, file, line);\r
133 }\r
134 \r
135 \r
136 //#define THROW_ON_ERROR(ret, source, func) throw_on_ffmpeg_error(ret, source, __FUNC__, __FILE__, __LINE__)\r
137 \r
138 #define THROW_ON_ERROR_STR_(call) #call\r
139 #define THROW_ON_ERROR_STR(call) THROW_ON_ERROR_STR_(call)\r
140 \r
141 #define THROW_ON_ERROR(ret, func, source) \\r
142                 throw_on_ffmpeg_error(ret, source, func, __FUNCTION__, __FILE__, __LINE__);             \r
143 \r
144 #define THROW_ON_ERROR2(call, source)                                                                           \\r
145         [&]() -> int                                                                                                                    \\r
146         {                                                                                                                                               \\r
147                 int ret = call;                                                                                                         \\r
148                 throw_on_ffmpeg_error(ret, source, THROW_ON_ERROR_STR(call), __FUNCTION__, __FILE__, __LINE__); \\r
149                 return ret;                                                                                                                     \\r
150         }()\r
151 \r
152 }}