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