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