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