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