]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer.cpp
[frame_producer] Created workaround for bug in FFmpeg where every new thread used...
[casparcg] / core / producer / frame_producer.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 "frame_producer.h"
25 #include "cg_proxy.h"
26
27 #include "../frame/draw_frame.h"
28 #include "../frame/frame_transform.h"
29
30 #include "color/color_producer.h"
31 #include "separated/separated_producer.h"
32 #include "variable.h"
33
34 #include <common/assert.h>
35 #include <common/except.h>
36 #include <common/executor.h>
37 #include <common/future.h>
38 #include <common/memory.h>
39
40 #include <boost/thread.hpp>
41
42 namespace caspar { namespace core {
43 struct frame_producer_registry::impl
44 {
45         std::vector<producer_factory_t>         producer_factories;
46         std::vector<thumbnail_producer_t>       thumbnail_producers;
47         spl::shared_ptr<help_repository>        help_repo;
48
49         impl(spl::shared_ptr<help_repository> help_repo)
50                 : help_repo(std::move(help_repo))
51         {
52         }
53 };
54
55 frame_producer_registry::frame_producer_registry(spl::shared_ptr<help_repository> help_repo)
56     : impl_(new impl(std::move(help_repo)))
57 {
58 }
59
60 void frame_producer_registry::register_producer_factory(std::wstring name, const producer_factory_t& factory, const help_item_describer& describer)
61 {
62         impl_->producer_factories.push_back(factory);
63         impl_->help_repo->register_item({ L"producer" }, std::move(name), describer);
64 }
65
66 void frame_producer_registry::register_thumbnail_producer(const thumbnail_producer_t& thumbnail_producer)
67 {
68         impl_->thumbnail_producers.push_back(thumbnail_producer);
69 }
70
71 frame_producer_dependencies::frame_producer_dependencies(
72                 const spl::shared_ptr<core::frame_factory>& frame_factory,
73                 const std::vector<spl::shared_ptr<video_channel>>& channels,
74                 const video_format_desc& format_desc,
75                 const spl::shared_ptr<const frame_producer_registry> producer_registry,
76                 const spl::shared_ptr<const cg_producer_registry> cg_registry)
77         : frame_factory(frame_factory)
78         , channels(channels)
79         , format_desc(format_desc)
80         , producer_registry(producer_registry)
81         , cg_registry(cg_registry)
82 {
83 }
84
85 constraints::constraints(double width, double height)
86         : width(width), height(height)
87 {
88 }
89
90 constraints::constraints()
91 {
92 }
93
94 struct frame_producer_base::impl
95 {
96         tbb::atomic<uint32_t>   frame_number_;
97         tbb::atomic<bool>               paused_;
98         frame_producer_base&    self_;
99         draw_frame                              last_frame_;
100
101         impl(frame_producer_base& self)
102                 : self_(self)
103                 , last_frame_(draw_frame::empty())
104         {
105                 frame_number_ = 0;
106                 paused_ = false;
107         }
108
109         draw_frame receive()
110         {
111                 if(paused_)
112                         return self_.last_frame();
113
114                 auto frame = self_.receive_impl();
115                 if(frame == draw_frame::late())
116                         return self_.last_frame();
117
118                 ++frame_number_;
119
120                 return last_frame_ = draw_frame::push(frame);
121         }
122
123         void paused(bool value)
124         {
125                 paused_ = value;
126         }
127
128         draw_frame last_frame()
129         {
130                 return draw_frame::still(last_frame_);
131         }
132 };
133
134 frame_producer_base::frame_producer_base() : impl_(new impl(*this))
135 {
136 }
137
138 draw_frame frame_producer_base::receive()
139 {
140         return impl_->receive();
141 }
142
143 void frame_producer_base::paused(bool value)
144 {
145         impl_->paused(value);
146 }
147
148 draw_frame frame_producer_base::last_frame()
149 {
150         return impl_->last_frame();
151 }
152
153 std::future<std::wstring> frame_producer_base::call(const std::vector<std::wstring>&)
154 {
155         CASPAR_THROW_EXCEPTION(not_supported());
156 }
157
158 uint32_t frame_producer_base::nb_frames() const
159 {
160         return std::numeric_limits<uint32_t>::max();
161 }
162
163 uint32_t frame_producer_base::frame_number() const
164 {
165         return impl_->frame_number_;
166 }
167
168 variable& frame_producer_base::get_variable(const std::wstring& name)
169 {
170         CASPAR_THROW_EXCEPTION(user_error()
171                         << msg_info(L"No variable called " + name + L" found in " + print()));
172 }
173
174 const std::vector<std::wstring>& frame_producer_base::get_variables() const
175 {
176         static std::vector<std::wstring> empty;
177
178         return empty;
179 }
180
181 const spl::shared_ptr<frame_producer>& frame_producer::empty()
182 {
183         class empty_frame_producer : public frame_producer
184         {
185         public:
186                 empty_frame_producer(){}
187                 draw_frame receive() override{return draw_frame::empty();}
188                 void paused(bool value) override{}
189                 uint32_t nb_frames() const override {return 0;}
190                 std::wstring print() const override { return L"empty";}
191                 monitor::subject& monitor_output() override {static monitor::subject monitor_subject(""); return monitor_subject;}
192                 std::wstring name() const override {return L"empty";}
193                 uint32_t frame_number() const override {return 0;}
194                 std::future<std::wstring> call(const std::vector<std::wstring>& params) override{CASPAR_THROW_EXCEPTION(not_implemented());}
195                 variable& get_variable(const std::wstring& name) override { CASPAR_THROW_EXCEPTION(not_implemented()); }
196                 const std::vector<std::wstring>& get_variables() const override { static std::vector<std::wstring> empty; return empty; }
197                 draw_frame last_frame() {return draw_frame::empty();}
198                 constraints& pixel_constraints() override { static constraints c; return c; }
199
200                 boost::property_tree::wptree info() const override
201                 {
202                         boost::property_tree::wptree info;
203                         info.add(L"type", L"empty-producer");
204                         return info;
205                 }
206         };
207
208         static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();
209         return producer;
210 }
211
212 std::shared_ptr<executor>& producer_destroyer()
213 {
214         static auto destroyer = []
215         {
216                 auto result = std::make_shared<executor>(L"Producer destroyer");
217                 result->set_capacity(std::numeric_limits<unsigned int>::max());
218                 return result;
219         }();;
220
221         return destroyer;
222 }
223
224 tbb::atomic<bool>& destroy_producers_in_separate_thread()
225 {
226         static tbb::atomic<bool> state;
227
228         return state;
229 }
230
231 void destroy_producers_synchronously()
232 {
233         destroy_producers_in_separate_thread() = false;
234         // Join destroyer, executing rest of producers in queue synchronously.
235         producer_destroyer().reset();
236 }
237
238 class destroy_producer_proxy : public frame_producer
239 {
240         std::shared_ptr<frame_producer> producer_;
241 public:
242         destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer)
243                 : producer_(std::move(producer))
244         {
245                 destroy_producers_in_separate_thread() = true;
246         }
247
248         virtual ~destroy_producer_proxy()
249         {
250                 if(producer_ == core::frame_producer::empty() || !destroy_producers_in_separate_thread())
251                         return;
252
253                 auto destroyer = producer_destroyer();
254
255                 if (!destroyer)
256                         return;
257
258                 CASPAR_VERIFY(destroyer->size() < 8);
259
260                 auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));
261
262                 destroyer->begin_invoke([=]
263                 {
264                         std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);
265                         auto str = (*producer)->print();
266                         try
267                         {
268                                 if (!producer->unique())
269                                         CASPAR_LOG(debug) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
270                                 else
271                                         CASPAR_LOG(debug) << str << L" Destroying on asynchronous destruction thread.";
272                         }
273                         catch(...){}
274
275                         try
276                         {
277                                 pointer_guard.reset();
278                                 CASPAR_LOG(info) << str << L" Destroyed.";
279                         }
280                         catch(...)
281                         {
282                                 CASPAR_LOG_CURRENT_EXCEPTION();
283                         }
284                 });
285         }
286
287         draw_frame                                                                                      receive() override                                                                                                                                                                                                              {return producer_->receive();}
288         std::wstring                                                                            print() const override                                                                                                                  {return producer_->print();}
289         void                                                                                            paused(bool value) override                                                                                                             {producer_->paused(value);}
290         std::wstring                                                                            name() const override                                                                                                                   {return producer_->name();}
291         uint32_t                                                                                        frame_number() const override                                                                                                   {return producer_->frame_number();}
292         boost::property_tree::wptree                                            info() const override                                                                                                                   {return producer_->info();}
293         std::future<std::wstring>                                                       call(const std::vector<std::wstring>& params) override                                                  {return producer_->call(params);}
294         variable&                                                                                       get_variable(const std::wstring& name) override                                                                 {return producer_->get_variable(name);}
295         const std::vector<std::wstring>&                                        get_variables() const override                                                                                                  {return producer_->get_variables();}
296         void                                                                                            leading_producer(const spl::shared_ptr<frame_producer>& producer) override              {return producer_->leading_producer(producer);}
297         uint32_t                                                                                        nb_frames() const override                                                                                                              {return producer_->nb_frames();}
298         draw_frame                                                                                      last_frame()                                                                                                                                    {return producer_->last_frame();}
299         monitor::subject&                                                                       monitor_output() override                                                                                                               {return producer_->monitor_output();}
300         bool                                                                                            collides(double x, double y) const override                                                                             {return producer_->collides(x, y);}
301         void                                                                                            on_interaction(const interaction_event::ptr& event)     override                                        {return producer_->on_interaction(event);}
302         constraints&                                                                            pixel_constraints() override                                                                                                    {return producer_->pixel_constraints();}
303 };
304
305 spl::shared_ptr<core::frame_producer> create_destroy_proxy(spl::shared_ptr<core::frame_producer> producer)
306 {
307         return spl::make_shared<destroy_producer_proxy>(std::move(producer));
308 }
309
310 spl::shared_ptr<core::frame_producer> do_create_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params, const std::vector<producer_factory_t>& factories, bool throw_on_fail = false)
311 {
312         if(params.empty())
313                 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("params cannot be empty"));
314
315         auto producer = frame_producer::empty();
316         std::any_of(factories.begin(), factories.end(), [&](const producer_factory_t& factory) -> bool
317                 {
318                         try
319                         {
320                                 producer = factory(dependencies, params);
321                         }
322                         catch (user_error&)
323                         {
324                                 throw;
325                         }
326                         catch(...)
327                         {
328                                 if(throw_on_fail)
329                                         throw;
330                                 else
331                                         CASPAR_LOG_CURRENT_EXCEPTION();
332                         }
333                         return producer != frame_producer::empty();
334                 });
335
336         if(producer == frame_producer::empty())
337                 producer = create_color_producer(dependencies.frame_factory, params);
338
339         if(producer == frame_producer::empty())
340                 return producer;
341
342         return producer;
343 }
344
345 draw_frame do_create_thumbnail_frame(
346                 const frame_producer_dependencies& dependencies,
347                 const std::wstring& media_file,
348                 const std::vector<thumbnail_producer_t>& thumbnail_producers)
349 {
350         for (auto& thumbnail_producer : thumbnail_producers)
351         {
352                 auto frame = thumbnail_producer(dependencies, media_file);
353
354                 if (frame != draw_frame::empty())
355                         return frame;
356         }
357
358         return draw_frame::empty();
359 }
360
361 draw_frame frame_producer_registry::create_thumbnail(const frame_producer_dependencies& dependencies, const std::wstring& media_file) const
362 {
363         auto& thumbnail_producers = impl_->thumbnail_producers;
364         std::vector<std::wstring> params;
365         params.push_back(media_file);
366
367         auto fill_frame = do_create_thumbnail_frame(dependencies, media_file, thumbnail_producers);
368         auto key_frame = do_create_thumbnail_frame(dependencies, media_file + L"_A", thumbnail_producers);
369
370         if (key_frame == draw_frame::empty())
371                 key_frame = do_create_thumbnail_frame(dependencies, media_file + L"_ALPHA", thumbnail_producers);
372
373         if (fill_frame != draw_frame::empty() && key_frame != draw_frame::empty())
374                 return draw_frame::mask(fill_frame, key_frame);
375
376         return fill_frame;
377 }
378
379 spl::shared_ptr<core::frame_producer> frame_producer_registry::create_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params) const
380 {
381         auto& producer_factories = impl_->producer_factories;
382         auto producer = do_create_producer(dependencies, params, producer_factories);
383         auto key_producer = frame_producer::empty();
384
385         if (!params.empty() && !boost::contains(params.at(0), L"://"))
386         {
387                 try // to find a key file.
388                 {
389                         auto params_copy = params;
390                         if (params_copy.size() > 0)
391                         {
392                                 params_copy[0] += L"_A";
393                                 key_producer = do_create_producer(dependencies, params_copy, producer_factories);
394                                 if (key_producer == frame_producer::empty())
395                                 {
396                                         params_copy[0] += L"LPHA";
397                                         key_producer = do_create_producer(dependencies, params_copy, producer_factories);
398                                 }
399                         }
400                 }
401                 catch (...) {}
402         }
403
404         if(producer != frame_producer::empty() && key_producer != frame_producer::empty())
405                 return create_separated_producer(producer, key_producer);
406
407         if(producer == frame_producer::empty())
408         {
409                 std::wstring str;
410                 for (auto& param : params)
411                         str += param + L" ";
412                 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));
413         }
414
415         return producer;
416 }
417
418
419 spl::shared_ptr<core::frame_producer> frame_producer_registry::create_producer(const frame_producer_dependencies& dependencies, const std::wstring& params) const
420 {
421         std::wstringstream iss(params);
422         std::vector<std::wstring> tokens;
423         typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;
424         std::copy(iterator(iss),  iterator(), std::back_inserter(tokens));
425         return create_producer(dependencies, tokens);
426 }
427 }}