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