]> 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 ffmpeg_stream_not_found : virtual ffmpeg_error{};\r
21 \r
22 static std::string av_error_str(int errn)\r
23 {\r
24         char buf[256];\r
25         memset(buf, 0, 256);\r
26         if(av_strerror(errn, buf, 256) < 0)\r
27                 return "";\r
28         return std::string(buf);\r
29 }\r
30 \r
31 #define THROW_ON_ERROR(ret, source, func)                                                       \\r
32         if(ret < 0)                                                                                                             \\r
33         {                                                                                                                               \\r
34                 BOOST_THROW_EXCEPTION(                                                                          \\r
35                         ffmpeg_error() <<                                                                               \\r
36                         msg_info(av_error_str(ret)) <<                                                  \\r
37                         source_info(narrow(source)) <<                                                  \\r
38                         boost::errinfo_api_function(func) <<                                    \\r
39                         boost::errinfo_errno(AVUNERROR(ret)));                                  \\r
40         }\r
41 \r
42 #define THROW_ON_ERROR_STR_(call) #call\r
43 #define THROW_ON_ERROR_STR(call) THROW_ON_ERROR_STR_(call)\r
44 \r
45 #define THROW_ON_ERROR3(call, source, exception)                                                                                \\r
46         [&]() -> int                                                                                                                    \\r
47         {                                                                                                                                               \\r
48                 int ret = call;                                                                                                         \\r
49                 if(ret < 0)                                                                                                                     \\r
50                 {                                                                                                                                       \\r
51                         BOOST_THROW_EXCEPTION(                                                                                  \\r
52                                 exception() <<                                                                                  \\r
53                                 msg_info(av_error_str(ret)) <<                                                          \\r
54                                 source_info(narrow(source)) <<                                                          \\r
55                                 boost::errinfo_api_function(THROW_ON_ERROR_STR(call)) <<        \\r
56                                 boost::errinfo_errno(AVUNERROR(ret)));                                          \\r
57                 }                                                                                                                                       \\r
58                 return ret;                                                                                                                     \\r
59         }()\r
60 #define THROW_ON_ERROR2(call, source) THROW_ON_ERROR3(call, source, ffmpeg_error)\r
61 \r
62 }}