]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer.cpp
fa458be4450e2408fbfcdf149f1712383ce936fd
[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
43 struct frame_producer_registry::impl
44 {
45         std::vector<producer_factory_t>         producer_factories;
46         std::vector<producer_factory_t>         thumbnail_factories;
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_factory(const producer_factory_t& factory)
67 {
68         impl_->thumbnail_factories.push_back(factory);
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         : frame_factory(frame_factory)
77         , channels(channels)
78         , format_desc(format_desc)
79         , producer_registry(producer_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         draw_frame create_thumbnail_frame()
131         {
132                 return draw_frame::empty();
133         }
134 };
135
136 frame_producer_base::frame_producer_base() : impl_(new impl(*this))
137 {
138 }
139
140 draw_frame frame_producer_base::receive()
141 {
142         return impl_->receive();
143 }
144
145 void frame_producer_base::paused(bool value)
146 {
147         impl_->paused(value);
148 }
149
150 draw_frame frame_producer_base::last_frame()
151 {
152         return impl_->last_frame();
153 }
154 draw_frame frame_producer_base::create_thumbnail_frame()
155 {
156         return impl_->create_thumbnail_frame();
157 }
158
159 std::future<std::wstring> frame_producer_base::call(const std::vector<std::wstring>&) 
160 {
161         CASPAR_THROW_EXCEPTION(not_supported());
162 }
163
164 uint32_t frame_producer_base::nb_frames() const
165 {
166         return std::numeric_limits<uint32_t>::max();
167 }
168
169 uint32_t frame_producer_base::frame_number() const
170 {
171         return impl_->frame_number_;
172 }
173
174 variable& frame_producer_base::get_variable(const std::wstring& name)
175 {
176         CASPAR_THROW_EXCEPTION(user_error()
177                         << msg_info(L"No variable called " + name + L" found in " + print()));
178 }
179
180 const std::vector<std::wstring>& frame_producer_base::get_variables() const
181 {
182         static std::vector<std::wstring> empty;
183
184         return empty;
185 }
186
187 const spl::shared_ptr<frame_producer>& frame_producer::empty() 
188 {
189         class empty_frame_producer : public frame_producer
190         {
191         public:
192                 empty_frame_producer(){}
193                 draw_frame receive() override{return draw_frame::empty();}
194                 void paused(bool value) override{}
195                 uint32_t nb_frames() const override {return 0;}
196                 std::wstring print() const override { return L"empty";}
197                 monitor::subject& monitor_output() override {static monitor::subject monitor_subject(""); return monitor_subject;}                                                                              
198                 std::wstring name() const override {return L"empty";}
199                 uint32_t frame_number() const override {return 0;}
200                 std::future<std::wstring> call(const std::vector<std::wstring>& params) override{CASPAR_THROW_EXCEPTION(not_implemented());}
201                 variable& get_variable(const std::wstring& name) override { CASPAR_THROW_EXCEPTION(not_implemented()); }
202                 const std::vector<std::wstring>& get_variables() const override { static std::vector<std::wstring> empty; return empty; }
203                 draw_frame last_frame() {return draw_frame::empty();}
204                 draw_frame create_thumbnail_frame() {return draw_frame::empty();}
205                 constraints& pixel_constraints() override { static constraints c; return c; }
206         
207                 boost::property_tree::wptree info() const override
208                 {
209                         boost::property_tree::wptree info;
210                         info.add(L"type", L"empty-producer");
211                         return info;
212                 }
213         };
214
215         static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();
216         return producer;
217 }
218
219 tbb::atomic<bool>& destroy_producers_in_separate_thread()
220 {
221         static tbb::atomic<bool> state;
222
223         return state;
224 }
225
226 void destroy_producers_synchronously()
227 {
228         destroy_producers_in_separate_thread() = false;
229 }
230
231 class destroy_producer_proxy : public frame_producer
232 {       
233         std::shared_ptr<frame_producer> producer_;
234 public:
235         destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer) 
236                 : producer_(std::move(producer))
237         {
238                 destroy_producers_in_separate_thread() = true;
239         }
240
241         virtual ~destroy_producer_proxy()
242         {
243                 static tbb::atomic<int> counter;
244                 static std::once_flag counter_init_once;
245                 std::call_once(counter_init_once, []{ counter = 0; });
246                 
247                 if(producer_ == core::frame_producer::empty() || !destroy_producers_in_separate_thread())
248                         return;
249
250                 ++counter;
251                 CASPAR_VERIFY(counter < 8);
252                 
253                 auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));
254                 boost::thread([=]
255                 {
256                         std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);
257                         auto str = (*producer)->print();
258                         try
259                         {
260                                 ensure_gpf_handler_installed_for_thread(u8(L"Destroyer: " + str).c_str());
261
262                                 if (!producer->unique())
263                                         CASPAR_LOG(debug) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();
264                                 else
265                                         CASPAR_LOG(debug) << str << L" Destroying on asynchronous destruction thread.";
266                         }
267                         catch(...){}
268                         
269                         try
270                         {
271                                 pointer_guard.reset();
272                                 CASPAR_LOG(info) << str << L" Destroyed.";
273                         }
274                         catch(...)
275                         {
276                                 CASPAR_LOG_CURRENT_EXCEPTION();
277                         }
278
279                         --counter;
280                 }).detach(); 
281         }
282         
283         draw_frame                                                                                      receive() override                                                                                                                                                                                                              {return producer_->receive();}
284         std::wstring                                                                            print() const override                                                                                                                  {return producer_->print();}
285         void                                                                                            paused(bool value) override                                                                                                             {producer_->paused(value);}
286         std::wstring                                                                            name() const override                                                                                                                   {return producer_->name();}
287         uint32_t                                                                                        frame_number() const override                                                                                                   {return producer_->frame_number();}
288         boost::property_tree::wptree                                            info() const override                                                                                                                   {return producer_->info();}
289         std::future<std::wstring>                                                       call(const std::vector<std::wstring>& params) override                                                  {return producer_->call(params);}
290         variable&                                                                                       get_variable(const std::wstring& name) override                                                                 {return producer_->get_variable(name);}
291         const std::vector<std::wstring>&                                        get_variables() const override                                                                                                  {return producer_->get_variables();}
292         void                                                                                            leading_producer(const spl::shared_ptr<frame_producer>& producer) override              {return producer_->leading_producer(producer);}
293         uint32_t                                                                                        nb_frames() const override                                                                                                              {return producer_->nb_frames();}
294         draw_frame                                                                                      last_frame()                                                                                                                                    {return producer_->last_frame();}
295         draw_frame                                                                                      create_thumbnail_frame()                                                                                                                {return producer_->create_thumbnail_frame();}
296         monitor::subject&                                                                       monitor_output() override                                                                                                               {return producer_->monitor_output();}                                                                           
297         bool                                                                                            collides(double x, double y) const override                                                                             {return producer_->collides(x, y);}
298         void                                                                                            on_interaction(const interaction_event::ptr& event)     override                                        {return producer_->on_interaction(event);}
299         constraints&                                                                            pixel_constraints() override                                                                                                    {return producer_->pixel_constraints();}
300 };
301
302 spl::shared_ptr<core::frame_producer> create_destroy_proxy(spl::shared_ptr<core::frame_producer> producer)
303 {
304         return spl::make_shared<destroy_producer_proxy>(std::move(producer));
305 }
306
307 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)
308 {
309         if(params.empty())
310                 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("params cannot be empty"));
311         
312         auto producer = frame_producer::empty();
313         std::any_of(factories.begin(), factories.end(), [&](const producer_factory_t& factory) -> bool
314                 {
315                         try
316                         {
317                                 producer = factory(dependencies, params);
318                         }
319                         catch (user_error&)
320                         {
321                                 throw;
322                         }
323                         catch(...)
324                         {
325                                 if(throw_on_fail)
326                                         throw;
327                                 else
328                                         CASPAR_LOG_CURRENT_EXCEPTION();
329                         }
330                         return producer != frame_producer::empty();
331                 });
332
333         if(producer == frame_producer::empty())
334                 producer = create_color_producer(dependencies.frame_factory, params);
335
336         if(producer == frame_producer::empty())
337                 return producer;
338                 
339         return producer;
340 }
341
342 spl::shared_ptr<core::frame_producer> frame_producer_registry::create_thumbnail_producer(const frame_producer_dependencies& dependencies, const std::wstring& media_file) const
343 {
344         auto& thumbnail_factories = impl_->thumbnail_factories;
345         std::vector<std::wstring> params;
346         params.push_back(media_file);
347
348         auto producer = do_create_producer(dependencies, params, thumbnail_factories, true);
349         auto key_producer = frame_producer::empty();
350   
351         try // to find a key file.
352         {
353                 auto params_copy = params;
354                 if (params_copy.size() > 0)
355                 {
356                         params_copy[0] += L"_A";
357                         key_producer = do_create_producer(dependencies, params_copy, thumbnail_factories, true);
358                         if (key_producer == frame_producer::empty())
359                         {
360                                 params_copy[0] += L"LPHA";
361                                 key_producer = do_create_producer(dependencies, params_copy, thumbnail_factories, true);
362                         }
363                 }
364         }
365         catch(...){}
366
367         if (producer != frame_producer::empty() && key_producer != frame_producer::empty())
368                 return create_separated_producer(producer, key_producer);
369   
370         return producer;
371 }
372
373 spl::shared_ptr<core::frame_producer> frame_producer_registry::create_producer(const frame_producer_dependencies& dependencies, const std::vector<std::wstring>& params) const
374 {       
375         auto& producer_factories = impl_->producer_factories;
376         auto producer = do_create_producer(dependencies, params, producer_factories);
377         auto key_producer = frame_producer::empty();
378         
379         try // to find a key file.
380         {
381                 auto params_copy = params;
382                 if(params_copy.size() > 0)
383                 {
384                         params_copy[0] += L"_A";
385                         key_producer = do_create_producer(dependencies, params_copy, producer_factories);
386                         if(key_producer == frame_producer::empty())
387                         {
388                                 params_copy[0] += L"LPHA";
389                                 key_producer = do_create_producer(dependencies, params_copy, producer_factories);
390                         }
391                 }
392         }
393         catch(...){}
394
395         if(producer != frame_producer::empty() && key_producer != frame_producer::empty())
396                 return create_separated_producer(producer, key_producer);
397         
398         if(producer == frame_producer::empty())
399         {
400                 std::wstring str;
401                 for (auto& param : params)
402                         str += param + L" ";
403                 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));
404         }
405
406         return producer;
407 }
408
409
410 spl::shared_ptr<core::frame_producer> frame_producer_registry::create_producer(const frame_producer_dependencies& dependencies, const std::wstring& params) const
411 {
412         std::wstringstream iss(params);
413         std::vector<std::wstring> tokens;
414         typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;
415         std::copy(iterator(iss),  iterator(), std::back_inserter(tokens));
416         return create_producer(dependencies, tokens);
417 }
418
419 }}