]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/ffmpeg_error.h
[ffmpeg] Classify most ffmpeg errors as user errors, so we don't get a stacktrace...
[casparcg] / modules / ffmpeg / ffmpeg_error.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #pragma once
23
24 #include <common/except.h>
25
26 #include <string>
27
28 namespace caspar { namespace ffmpeg {
29
30 struct ffmpeg_error : virtual caspar_exception{};
31 struct ffmpeg_user_error : virtual user_error {};
32 struct averror_bsf_not_found : virtual ffmpeg_error{};
33 struct averror_decoder_not_found : virtual ffmpeg_user_error {};
34 struct averror_demuxer_not_found : virtual ffmpeg_user_error {};
35 struct averror_encoder_not_found : virtual ffmpeg_user_error {};
36 struct averror_eof : virtual ffmpeg_error{};
37 struct averror_exit : virtual ffmpeg_error{};
38 struct averror_filter_not_found : virtual ffmpeg_user_error {};
39 struct averror_muxer_not_found : virtual ffmpeg_user_error {};
40 struct averror_option_not_found : virtual ffmpeg_user_error {};
41 struct averror_patchwelcome : virtual ffmpeg_error{};
42 struct averror_protocol_not_found : virtual ffmpeg_user_error {};
43 struct averror_stream_not_found : virtual ffmpeg_error{};
44 struct averror_invalid_argument : virtual ffmpeg_user_error {};
45
46 std::string av_error_str(int errn);
47
48 void throw_on_ffmpeg_error(int ret, const char* source, const char* func, const char* local_func, const char* file, int line);
49 void throw_on_ffmpeg_error(int ret, const std::wstring& source, const char* func, const char* local_func, const char* file, int line);
50
51
52 //#define THROW_ON_ERROR(ret, source, func) throw_on_ffmpeg_error(ret, source, __FUNC__, __FILE__, __LINE__)
53
54 #define THROW_ON_ERROR_STR_(call) #call
55 #define THROW_ON_ERROR_STR(call) THROW_ON_ERROR_STR_(call)
56
57 #define THROW_ON_ERROR(ret, func, source) \
58                 throw_on_ffmpeg_error(ret, source, func, __FUNCTION__, __FILE__, __LINE__);
59
60 #define THROW_ON_ERROR2(call, source)                                                                           \
61         [&]() -> int                                                                                                                    \
62         {                                                                                                                                               \
63                 int ret = call;                                                                                                         \
64                 throw_on_ffmpeg_error(ret, source, THROW_ON_ERROR_STR(call), __FUNCTION__, __FILE__, __LINE__); \
65                 return ret;                                                                                                                     \
66         }()
67
68 #define LOG_ON_ERROR2(call, source)                                                                                     \
69         [&]() -> int                                                                                                                    \
70         {                                       \
71                 int ret = -1;\
72                 try{                                                                                                                            \
73                  ret = call;                                                                                                                    \
74                 throw_on_ffmpeg_error(ret, source, THROW_ON_ERROR_STR(call), __FUNCTION__, __FILE__, __LINE__); \
75                 return ret;                                                                                                                     \
76                 }catch(...){CASPAR_LOG_CURRENT_EXCEPTION();}                                            \
77                 return ret;                                                                                                                     \
78         }()
79
80 #define FF_RET(ret, func) \
81                 caspar::ffmpeg::throw_on_ffmpeg_error(ret, L"", func, __FUNCTION__, __FILE__, __LINE__);
82
83 #define FF(call)                                                                                \
84         [&]() -> int                                                                                                            \
85         {                                                                                                                                               \
86                 auto ret = call;                                                                                                                \
87                 caspar::ffmpeg::throw_on_ffmpeg_error(static_cast<int>(ret), L"", THROW_ON_ERROR_STR(call), __FUNCTION__, __FILE__, __LINE__);  \
88                 return ret;                                                                                                                     \
89         }()
90
91 }}