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