]> git.sesse.net Git - casparcg/blob - modules/ffmpeg/ffmpeg.cpp
[ffmpeg] Never log AV_LOG_TRACE. Not even when we have trace set.
[casparcg] / modules / ffmpeg / ffmpeg.cpp
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 #include "StdAfx.h"
23
24 #include "ffmpeg.h"
25
26 #include "consumer/ffmpeg_consumer.h"
27 #include "consumer/streaming_consumer.h"
28 #include "producer/ffmpeg_producer.h"
29 #include "producer/util/util.h"
30
31 #include <common/log.h>
32 #include <common/os/general_protection_fault.h>
33
34 #include <core/consumer/frame_consumer.h>
35 #include <core/frame/draw_frame.h>
36 #include <core/producer/frame_producer.h>
37 #include <core/producer/media_info/media_info.h>
38 #include <core/producer/media_info/media_info_repository.h>
39 #include <core/system_info_provider.h>
40
41 #include <boost/property_tree/ptree.hpp>
42 #include <boost/thread/tss.hpp>
43 #include <boost/bind.hpp>
44
45 #include <tbb/recursive_mutex.h>
46
47 #if defined(_MSC_VER)
48 #pragma warning (disable : 4244)
49 #pragma warning (disable : 4603)
50 #pragma warning (disable : 4996)
51 #endif
52
53 extern "C"
54 {
55         #define __STDC_CONSTANT_MACROS
56         #define __STDC_LIMIT_MACROS
57         #include <libavformat/avformat.h>
58         #include <libswscale/swscale.h>
59         #include <libavutil/avutil.h>
60         #include <libavfilter/avfilter.h>
61         #include <libavdevice/avdevice.h>
62 }
63
64 namespace caspar { namespace ffmpeg {
65 int ffmpeg_lock_callback(void **mutex, enum AVLockOp op)
66 {
67         if(!mutex)
68                 return 0;
69
70         auto my_mutex = reinterpret_cast<tbb::recursive_mutex*>(*mutex);
71
72         switch(op)
73         {
74                 case AV_LOCK_CREATE:
75                 {
76                         *mutex = new tbb::recursive_mutex();
77                         break;
78                 }
79                 case AV_LOCK_OBTAIN:
80                 {
81                         if(my_mutex)
82                                 my_mutex->lock();
83                         break;
84                 }
85                 case AV_LOCK_RELEASE:
86                 {
87                         if(my_mutex)
88                                 my_mutex->unlock();
89                         break;
90                 }
91                 case AV_LOCK_DESTROY:
92                 {
93                         delete my_mutex;
94                         *mutex = nullptr;
95                         break;
96                 }
97         }
98         return 0;
99 }
100
101 static void sanitize(uint8_t *line)
102 {
103         while(*line)
104         {
105                 if(*line < 0x08 || (*line > 0x0D && *line < 0x20))
106                         *line='?';
107                 line++;
108         }
109 }
110
111 void log_callback(void* ptr, int level, const char* fmt, va_list vl)
112 {
113         static int print_prefix=1;
114         static char prev[1024];
115         char line[8192];
116         AVClass* avc= ptr ? *(AVClass**)ptr : NULL;
117         if (level > AV_LOG_DEBUG)
118                 return;
119         line[0]=0;
120
121 #undef fprintf
122         if(print_prefix && avc)
123         {
124                 if (avc->parent_log_context_offset)
125                 {
126                         AVClass** parent= *(AVClass***)(((uint8_t*)ptr) + avc->parent_log_context_offset);
127                         if(parent && *parent)
128                                 std::sprintf(line, "[%s @ %p] ", (*parent)->item_name(parent), parent);
129                 }
130                 std::sprintf(line + strlen(line), "[%s @ %p] ", avc->item_name(ptr), ptr);
131         }
132
133         std::vsprintf(line + strlen(line), fmt, vl);
134
135         print_prefix = strlen(line) && line[strlen(line)-1] == '\n';
136
137         strcpy(prev, line);
138         sanitize((uint8_t*)line);
139
140         auto len = strlen(line);
141         if(len > 0)
142                 line[len-1] = 0;
143
144         try
145         {
146                 if (level == AV_LOG_VERBOSE)
147                         CASPAR_LOG(debug) << L"[ffmpeg] " << line;
148                 else if (level == AV_LOG_INFO)
149                         CASPAR_LOG(info) << L"[ffmpeg] " << line;
150                 else if (level == AV_LOG_WARNING)
151                         CASPAR_LOG(warning) << L"[ffmpeg] " << line;
152                 else if (level == AV_LOG_ERROR)
153                         CASPAR_LOG(error) << L"[ffmpeg] " << line;
154                 else if (level == AV_LOG_FATAL)
155                         CASPAR_LOG(fatal) << L"[ffmpeg] " << line;
156                 else
157                         CASPAR_LOG(trace) << L"[ffmpeg] " << line;
158         }
159         catch (...)
160         {
161         }
162 }
163
164 std::wstring make_version(unsigned int ver)
165 {
166         std::wstringstream str;
167         str << ((ver >> 16) & 0xFF) << L"." << ((ver >> 8) & 0xFF) << L"." << ((ver >> 0) & 0xFF);
168         return str.str();
169 }
170
171 std::wstring avcodec_version()
172 {
173         return make_version(::avcodec_version());
174 }
175
176 std::wstring avformat_version()
177 {
178         return make_version(::avformat_version());
179 }
180
181 std::wstring avutil_version()
182 {
183         return make_version(::avutil_version());
184 }
185
186 std::wstring avfilter_version()
187 {
188         return make_version(::avfilter_version());
189 }
190
191 std::wstring swscale_version()
192 {
193         return make_version(::swscale_version());
194 }
195 bool& get_quiet_logging_for_thread()
196 {
197         static boost::thread_specific_ptr<bool> quiet_logging_for_thread;
198
199         auto local = quiet_logging_for_thread.get();
200
201         if (!local)
202         {
203                 local = new bool(false);
204                 quiet_logging_for_thread.reset(local);
205         }
206
207         return *local;
208 }
209
210 void enable_quiet_logging_for_thread()
211 {
212         get_quiet_logging_for_thread() = true;
213 }
214
215 bool is_logging_quiet_for_thread()
216 {
217         return get_quiet_logging_for_thread();
218 }
219
220 std::shared_ptr<void> temporary_enable_quiet_logging_for_thread(bool enable)
221 {
222         if (!enable || is_logging_quiet_for_thread())
223                 return std::shared_ptr<void>();
224
225         get_quiet_logging_for_thread() = true;
226
227         return std::shared_ptr<void>(nullptr, [](void*)
228         {
229                 get_quiet_logging_for_thread() = false; // Only works correctly if destructed in same thread as original caller.
230         });
231 }
232
233 void log_for_thread(void* ptr, int level, const char* fmt, va_list vl)
234 {
235         ensure_gpf_handler_installed_for_thread("ffmpeg-thread");
236
237         int min_level = is_logging_quiet_for_thread() ? AV_LOG_DEBUG : AV_LOG_FATAL;
238
239         log_callback(ptr, std::max(level, min_level), fmt, vl);
240 }
241
242 void init(core::module_dependencies dependencies)
243 {
244         av_lockmgr_register(ffmpeg_lock_callback);
245         av_log_set_callback(log_for_thread);
246
247         avfilter_register_all();
248         //fix_yadif_filter_format_query();
249         av_register_all();
250         avformat_network_init();
251         avcodec_register_all();
252         avdevice_register_all();
253
254         auto info_repo = dependencies.media_info_repo;
255
256         dependencies.consumer_registry->register_consumer_factory(L"FFmpeg Consumer", create_consumer, describe_consumer);
257         dependencies.consumer_registry->register_consumer_factory(L"Streaming Consumer", create_streaming_consumer, describe_streaming_consumer);
258         dependencies.consumer_registry->register_preconfigured_consumer_factory(L"file", create_preconfigured_consumer);
259         dependencies.consumer_registry->register_preconfigured_consumer_factory(L"stream", create_preconfigured_streaming_consumer);
260         dependencies.producer_registry->register_producer_factory(L"FFmpeg Producer", boost::bind(&create_producer, _1, _2, info_repo), describe_producer);
261         dependencies.producer_registry->register_thumbnail_producer(boost::bind(&create_thumbnail_frame, _1, _2, info_repo));
262
263         info_repo->register_extractor(
264                         [](const std::wstring& file, const std::wstring& extension, core::media_info& info) -> bool
265                         {
266                                 auto quiet_logging = temporary_enable_quiet_logging_for_thread(true);
267                                 if (extension == L".WAV" || extension == L".MP3")
268                                 {
269                                         info.clip_type = L"AUDIO";
270                                         return true;
271                                 }
272
273                                 if (!is_valid_file(file, true))
274                                         return false;
275
276                                 info.clip_type = L"MOVIE";
277
278                                 return try_get_duration(file, info.duration, info.time_base);
279                         });
280         dependencies.system_info_provider_repo->register_system_info_provider([](boost::property_tree::wptree& info)
281         {
282                 info.add(L"system.ffmpeg.avcodec", avcodec_version());
283                 info.add(L"system.ffmpeg.avformat", avformat_version());
284                 info.add(L"system.ffmpeg.avfilter", avfilter_version());
285                 info.add(L"system.ffmpeg.avutil", avutil_version());
286                 info.add(L"system.ffmpeg.swscale", swscale_version());
287         });
288 }
289
290 void uninit()
291 {
292         avfilter_uninit();
293         avformat_network_deinit();
294         av_lockmgr_register(nullptr);
295 }
296 }}