]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer.cpp
2.1.0: Fixed PAUSE bugs.
[casparcg] / core / producer / frame_producer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../StdAfx.h"\r
23 \r
24 #include "frame_producer.h"\r
25 \r
26 #include "../frame/draw_frame.h"\r
27 #include "../frame/frame_transform.h"\r
28 \r
29 #include "color/color_producer.h"\r
30 #include "separated/separated_producer.h"\r
31 \r
32 #include <common/assert.h>\r
33 #include <common/except.h>\r
34 #include <common/executor.h>\r
35 #include <common/future.h>\r
36 #include <common/memory.h>\r
37 \r
38 #include <boost/thread.hpp>\r
39 \r
40 namespace caspar { namespace core {\r
41         \r
42 std::vector<const producer_factory_t> g_factories;\r
43 \r
44 void register_producer_factory(const producer_factory_t& factory)\r
45 {\r
46         g_factories.push_back(factory);\r
47 }\r
48 \r
49 struct frame_producer_base::impl\r
50 {\r
51         tbb::atomic<uint32_t>   frame_number_;\r
52         tbb::atomic<bool>               paused_;\r
53         frame_producer_base&    self_;\r
54         draw_frame                              last_frame_;\r
55 \r
56         impl(frame_producer_base& self)\r
57                 : self_(self)\r
58                 , last_frame_(draw_frame::empty())\r
59         {\r
60                 frame_number_ = 0;\r
61                 paused_ = false;\r
62         }\r
63         \r
64         draw_frame receive()\r
65         {\r
66                 if(paused_)\r
67                         return self_.last_frame();\r
68 \r
69                 auto frame = self_.receive_impl();\r
70                 if(frame == draw_frame::late())\r
71                         return self_.last_frame();\r
72 \r
73                 ++frame_number_;\r
74 \r
75                 return last_frame_ = draw_frame::push(frame);\r
76         }\r
77 \r
78         void paused(bool value)\r
79         {\r
80                 paused_ = value;\r
81         }\r
82 \r
83         draw_frame last_frame()\r
84         {\r
85                 return draw_frame::still(last_frame_);\r
86         }\r
87 };\r
88 \r
89 frame_producer_base::frame_producer_base() : impl_(new impl(*this))\r
90 {\r
91 }\r
92 \r
93 draw_frame frame_producer_base::receive()\r
94 {\r
95         return impl_->receive();\r
96 }\r
97 \r
98 void frame_producer_base::paused(bool value)\r
99 {\r
100         impl_->paused(value);\r
101 }\r
102 \r
103 draw_frame frame_producer_base::last_frame()\r
104 {\r
105         return impl_->last_frame();\r
106 }\r
107 \r
108 boost::unique_future<std::wstring> frame_producer_base::call(const std::wstring&) \r
109 {\r
110         CASPAR_THROW_EXCEPTION(not_supported());\r
111 }\r
112 \r
113 uint32_t frame_producer_base::nb_frames() const\r
114 {\r
115         return std::numeric_limits<uint32_t>::max();\r
116 }\r
117 \r
118 uint32_t frame_producer_base::frame_number() const\r
119 {\r
120         return impl_->frame_number_;\r
121 }\r
122 \r
123 const spl::shared_ptr<frame_producer>& frame_producer::empty() \r
124 {\r
125         class empty_frame_producer : public frame_producer\r
126         {\r
127         public:\r
128                 empty_frame_producer(){}\r
129                 draw_frame receive() override{return draw_frame::empty();}\r
130                 void paused(bool value) override{}\r
131                 uint32_t nb_frames() const override {return 0;}\r
132                 std::wstring print() const override { return L"empty";}\r
133                 void subscribe(const monitor::observable::observer_ptr& o) override{}\r
134                 void unsubscribe(const monitor::observable::observer_ptr& o) override{} \r
135                 std::wstring name() const override {return L"empty";}\r
136                 uint32_t frame_number() const override {return 0;}\r
137                 boost::unique_future<std::wstring> call(const std::wstring& params) override{CASPAR_THROW_EXCEPTION(not_supported());}\r
138                 draw_frame last_frame() {return draw_frame::empty();}\r
139         \r
140                 boost::property_tree::wptree info() const override\r
141                 {\r
142                         boost::property_tree::wptree info;\r
143                         info.add(L"type", L"empty-producer");\r
144                         return info;\r
145                 }\r
146         };\r
147 \r
148         static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();\r
149         return producer;\r
150 }       \r
151 \r
152 class destroy_producer_proxy : public frame_producer\r
153 {       \r
154         std::shared_ptr<frame_producer> producer_;\r
155 public:\r
156         destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer) \r
157                 : producer_(std::move(producer))\r
158         {\r
159         }\r
160 \r
161         virtual ~destroy_producer_proxy()\r
162         {               \r
163                 static tbb::atomic<int> counter = tbb::atomic<int>();\r
164                 \r
165                 if(producer_ == core::frame_producer::empty())\r
166                         return;\r
167 \r
168                 ++counter;\r
169                 CASPAR_VERIFY(counter < 8);\r
170                 \r
171                 auto producer = new spl::shared_ptr<frame_producer>(std::move(producer_));\r
172                 boost::thread([=]\r
173                 {\r
174                         std::unique_ptr<spl::shared_ptr<frame_producer>> pointer_guard(producer);\r
175                         auto str = (*producer)->print();\r
176                         try\r
177                         {\r
178                                 if(!producer->unique())\r
179                                         CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();\r
180                                 else\r
181                                         CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";\r
182                         }\r
183                         catch(...){}\r
184                         \r
185                         try\r
186                         {\r
187                                 pointer_guard.reset();\r
188                                 CASPAR_LOG(info) << str << L" Destroyed.";\r
189                         }\r
190                         catch(...)\r
191                         {\r
192                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
193                         }\r
194 \r
195                         --counter;\r
196                 }).detach(); \r
197         }\r
198         \r
199         draw_frame      receive() override                                                                                                                                                                                                              {return producer_->receive();}\r
200         std::wstring                                                                            print() const override                                                                                                                  {return producer_->print();}\r
201         void                                                                                            paused(bool value) override                                                                                                             {producer_->paused(value);}\r
202         std::wstring                                                                            name() const override                                                                                                                   {return producer_->name();}\r
203         uint32_t                                                                                        frame_number() const override                                                                                                   {return producer_->frame_number();}\r
204         boost::property_tree::wptree                                            info() const override                                                                                                                   {return producer_->info();}\r
205         boost::unique_future<std::wstring>                                      call(const std::wstring& str) override                                                                                  {return producer_->call(str);}\r
206         void                                                                                            leading_producer(const spl::shared_ptr<frame_producer>& producer) override              {return producer_->leading_producer(producer);}\r
207         uint32_t                                                                                        nb_frames() const override                                                                                                              {return producer_->nb_frames();}\r
208         class draw_frame                                                                        last_frame()                                                                                                                            {return producer_->last_frame();}\r
209         void                                                                                            subscribe(const monitor::observable::observer_ptr& o)                                                   {return producer_->subscribe(o);}\r
210         void                                                                                            unsubscribe(const monitor::observable::observer_ptr& o)                                                 {return producer_->unsubscribe(o);}\r
211 };\r
212 \r
213 spl::shared_ptr<core::frame_producer> create_destroy_proxy(spl::shared_ptr<core::frame_producer> producer)\r
214 {\r
215         return spl::make_shared<destroy_producer_proxy>(std::move(producer));\r
216 }\r
217 \r
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)\r
219 {\r
220         if(params.empty())\r
221                 CASPAR_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));\r
222         \r
223         auto producer = frame_producer::empty();\r
224         std::any_of(g_factories.begin(), g_factories.end(), [&](const producer_factory_t& factory) -> bool\r
225                 {\r
226                         try\r
227                         {\r
228                                 producer = factory(my_frame_factory, format_desc, params);\r
229                         }\r
230                         catch(...)\r
231                         {\r
232                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
233                         }\r
234                         return producer != frame_producer::empty();\r
235                 });\r
236 \r
237         if(producer == frame_producer::empty())\r
238                 producer = create_color_producer(my_frame_factory, params);\r
239 \r
240         if(producer == frame_producer::empty())\r
241                 return producer;\r
242                 \r
243         return producer;\r
244 }\r
245 \r
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)\r
247 {       \r
248         auto producer = do_create_producer(my_frame_factory, format_desc, params);\r
249         auto key_producer = frame_producer::empty();\r
250         \r
251         try // to find a key file.\r
252         {\r
253                 auto params_copy = params;\r
254                 if(params_copy.size() > 0)\r
255                 {\r
256                         params_copy[0] += L"_A";\r
257                         key_producer = do_create_producer(my_frame_factory, format_desc, params_copy);                  \r
258                         if(key_producer == frame_producer::empty())\r
259                         {\r
260                                 params_copy[0] += L"LPHA";\r
261                                 key_producer = do_create_producer(my_frame_factory, format_desc, params_copy);  \r
262                         }\r
263                 }\r
264         }\r
265         catch(...){}\r
266 \r
267         if(producer != frame_producer::empty() && key_producer != frame_producer::empty())\r
268                 return create_separated_producer(producer, key_producer);\r
269         \r
270         if(producer == frame_producer::empty())\r
271         {\r
272                 std::wstring str;\r
273                 BOOST_FOREACH(auto& param, params)\r
274                         str += param + L" ";\r
275                 CASPAR_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));\r
276         }\r
277 \r
278         return producer;\r
279 }\r
280 \r
281 \r
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)\r
283 {\r
284         std::wstringstream iss(params);\r
285         std::vector<std::wstring> tokens;\r
286         typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;\r
287         std::copy(iterator(iss),  iterator(), std::back_inserter(tokens));\r
288         return create_producer(factory, format_desc, tokens);\r
289 }\r
290 \r
291 }}