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