]> git.sesse.net Git - casparcg/blob - core/producer/frame_producer.cpp
2.1.0: - frame_producer: Refactored. -cg_producer: Refactored. Remade into simply...
[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/concurrency/executor.h>\r
35 #include <common/concurrency/async.h>\r
36 #include <common/spl/memory.h>\r
37 \r
38 namespace caspar { namespace core {\r
39         \r
40 std::vector<const producer_factory_t> g_factories;\r
41 \r
42 void register_producer_factory(const producer_factory_t& factory)\r
43 {\r
44         g_factories.push_back(factory);\r
45 }\r
46 \r
47 boost::unique_future<std::wstring> frame_producer::call(const std::wstring&) \r
48 {\r
49         BOOST_THROW_EXCEPTION(not_supported());\r
50 }\r
51 \r
52 struct empty_frame_producer : public frame_producer\r
53 {\r
54         virtual spl::shared_ptr<draw_frame> receive(int){return draw_frame::empty();}\r
55         virtual spl::shared_ptr<draw_frame> last_frame() const{return draw_frame::empty();}\r
56         virtual void set_frame_factory(const spl::shared_ptr<frame_factory>&){}\r
57         virtual uint32_t nb_frames() const {return 0;}\r
58         virtual std::wstring print() const { return L"empty";}\r
59         \r
60         virtual boost::property_tree::wptree info() const override\r
61         {\r
62                 boost::property_tree::wptree info;\r
63                 info.add(L"type", L"empty-producer");\r
64                 return info;\r
65         }\r
66 };\r
67 \r
68 const spl::shared_ptr<frame_producer>& frame_producer::empty() // nothrow\r
69 {\r
70         static spl::shared_ptr<frame_producer> producer = spl::make_shared<empty_frame_producer>();\r
71         return producer;\r
72 }       \r
73 \r
74 class destroy_producer_proxy : public frame_producer\r
75 {       \r
76         std::unique_ptr<std::shared_ptr<frame_producer>> producer_;\r
77 public:\r
78         destroy_producer_proxy(spl::shared_ptr<frame_producer>&& producer) \r
79                 : producer_(new std::shared_ptr<frame_producer>(std::move(producer)))\r
80         {\r
81         }\r
82 \r
83         ~destroy_producer_proxy()\r
84         {               \r
85                 static tbb::atomic<int> counter = tbb::atomic<int>();\r
86                 \r
87                 ++counter;\r
88                 CASPAR_VERIFY(counter < 32);\r
89                 \r
90                 auto producer = producer_.release();\r
91                 async([=]\r
92                 {\r
93                         std::unique_ptr<std::shared_ptr<frame_producer>> pointer_guard(producer);\r
94 \r
95                         auto str = (*producer)->print();\r
96                         try\r
97                         {\r
98                                 if(!producer->unique())\r
99                                         CASPAR_LOG(trace) << str << L" Not destroyed on asynchronous destruction thread: " << producer->use_count();\r
100                                 else\r
101                                         CASPAR_LOG(trace) << str << L" Destroying on asynchronous destruction thread.";\r
102                         }\r
103                         catch(...){}\r
104 \r
105                         pointer_guard.reset();\r
106 \r
107                         --counter;\r
108                 }); \r
109         }\r
110 \r
111         virtual spl::shared_ptr<draw_frame>                                                     receive(int hints) override                                                                                                             {return (*producer_)->receive(hints);}\r
112         virtual spl::shared_ptr<draw_frame>                                                     last_frame() const override                                                                                                             {return (*producer_)->last_frame();}\r
113         virtual std::wstring                                                                            print() const override                                                                                                                  {return (*producer_)->print();}\r
114         virtual boost::property_tree::wptree                                            info() const override                                                                                                                   {return (*producer_)->info();}\r
115         virtual boost::unique_future<std::wstring>                                      call(const std::wstring& str) override                                                                                  {return (*producer_)->call(str);}\r
116         virtual spl::shared_ptr<frame_producer>                                         get_following_producer() const override                                                                                 {return (*producer_)->get_following_producer();}\r
117         virtual void                                                                                            set_leading_producer(const spl::shared_ptr<frame_producer>& producer) override  {return (*producer_)->set_leading_producer(producer);}\r
118         virtual uint32_t                                                                                        nb_frames() const override                                                                                                              {return (*producer_)->nb_frames();}\r
119 };\r
120 \r
121 class print_producer_proxy : public frame_producer\r
122 {       \r
123         std::shared_ptr<frame_producer> producer_;\r
124 public:\r
125         print_producer_proxy(spl::shared_ptr<frame_producer>&& producer) \r
126                 : producer_(std::move(producer))\r
127         {\r
128                 CASPAR_LOG(info) << producer_->print() << L" Initialized.";\r
129         }\r
130 \r
131         ~print_producer_proxy()\r
132         {               \r
133                 auto str = producer_->print();\r
134                 CASPAR_LOG(trace) << str << L" Uninitializing.";\r
135                 producer_.reset();\r
136                 CASPAR_LOG(info) << str << L" Uninitialized.";\r
137         }\r
138         \r
139 \r
140         virtual spl::shared_ptr<draw_frame>                                                     receive(int hints) override                                                                                                             {return producer_->receive(hints);}\r
141         virtual spl::shared_ptr<draw_frame>                                                     last_frame() const override                                                                                                             {return producer_->last_frame();}\r
142         virtual std::wstring                                                                            print() const override                                                                                                                  {return producer_->print();}\r
143         virtual boost::property_tree::wptree                                            info() const override                                                                                                                   {return producer_->info();}\r
144         virtual boost::unique_future<std::wstring>                                      call(const std::wstring& str) override                                                                                  {return producer_->call(str);}\r
145         virtual spl::shared_ptr<frame_producer>                                         get_following_producer() const override                                                                                 {return producer_->get_following_producer();}\r
146         virtual void                                                                                            set_leading_producer(const spl::shared_ptr<frame_producer>& producer) override  {return producer_->set_leading_producer(producer);}\r
147         virtual uint32_t                                                                                        nb_frames() const override                                                                                                              {return producer_->nb_frames();}\r
148 };\r
149 \r
150 class follow_producer_proxy : public frame_producer\r
151 {       \r
152         spl::shared_ptr<frame_producer> producer_;\r
153 public:\r
154         follow_producer_proxy(spl::shared_ptr<frame_producer>&& producer) \r
155                 : producer_(std::move(producer))\r
156         {\r
157         }\r
158 \r
159         virtual spl::shared_ptr<draw_frame>     receive(int hints) override                                                                                                             \r
160         {\r
161                 auto frame = producer_->receive(hints);\r
162                 if(frame == draw_frame::eof())\r
163                 {\r
164                         CASPAR_LOG(info) << producer_->print() << " End Of File.";\r
165                         auto following = producer_->get_following_producer();\r
166                         if(following != frame_producer::empty())\r
167                         {\r
168                                 following->set_leading_producer(producer_);\r
169                                 producer_ = std::move(following);\r
170                         }\r
171 \r
172                         return receive(hints);\r
173                 }\r
174                 return frame;\r
175         }\r
176 \r
177         virtual spl::shared_ptr<draw_frame>                                                     last_frame() const override                                                                                                             {return producer_->last_frame();}\r
178         virtual std::wstring                                                                            print() const override                                                                                                                  {return producer_->print();}\r
179         virtual boost::property_tree::wptree                                            info() const override                                                                                                                   {return producer_->info();}\r
180         virtual boost::unique_future<std::wstring>                                      call(const std::wstring& str) override                                                                                  {return producer_->call(str);}\r
181         virtual spl::shared_ptr<frame_producer>                                         get_following_producer() const override                                                                                 {return producer_->get_following_producer();}\r
182         virtual void                                                                                            set_leading_producer(const spl::shared_ptr<frame_producer>& producer) override  {return producer_->set_leading_producer(producer);}\r
183         virtual uint32_t                                                                                        nb_frames() const override                                                                                                              {return producer_->nb_frames();}\r
184 };\r
185 \r
186 spl::shared_ptr<core::frame_producer> wrap_producer(spl::shared_ptr<core::frame_producer> producer)\r
187 {\r
188         return spl::make_shared<follow_producer_proxy>(\r
189                         spl::make_shared<destroy_producer_proxy>(\r
190                          spl::make_shared<print_producer_proxy>(\r
191                           std::move(producer))));\r
192 }\r
193 \r
194 spl::shared_ptr<core::frame_producer> do_create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)\r
195 {\r
196         if(params.empty())\r
197                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));\r
198         \r
199         auto producer = frame_producer::empty();\r
200         std::any_of(g_factories.begin(), g_factories.end(), [&](const producer_factory_t& factory) -> bool\r
201                 {\r
202                         try\r
203                         {\r
204                                 producer = factory(my_frame_factory, params);\r
205                         }\r
206                         catch(...)\r
207                         {\r
208                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
209                         }\r
210                         return producer != frame_producer::empty();\r
211                 });\r
212 \r
213         if(producer == frame_producer::empty())\r
214                 producer = create_color_producer(my_frame_factory, params);\r
215 \r
216         if(producer == frame_producer::empty())\r
217                 return producer;\r
218                 \r
219         return producer;\r
220 }\r
221 \r
222 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& my_frame_factory, const std::vector<std::wstring>& params)\r
223 {       \r
224         auto producer = do_create_producer(my_frame_factory, params);\r
225         auto key_producer = frame_producer::empty();\r
226         \r
227         try // to find a key file.\r
228         {\r
229                 auto params_copy = params;\r
230                 if(params_copy.size() > 0)\r
231                 {\r
232                         params_copy[0] += L"_A";\r
233                         key_producer = do_create_producer(my_frame_factory, params_copy);                       \r
234                         if(key_producer == frame_producer::empty())\r
235                         {\r
236                                 params_copy[0] += L"LPHA";\r
237                                 key_producer = do_create_producer(my_frame_factory, params_copy);       \r
238                         }\r
239                 }\r
240         }\r
241         catch(...){}\r
242 \r
243         if(producer != frame_producer::empty() && key_producer != frame_producer::empty())\r
244                 return create_separated_producer(producer, key_producer);\r
245         \r
246         if(producer == frame_producer::empty())\r
247         {\r
248                 std::wstring str;\r
249                 BOOST_FOREACH(auto& param, params)\r
250                         str += param + L" ";\r
251                 BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax.") << arg_value_info(u8(str)));\r
252         }\r
253 \r
254         return producer;\r
255 }\r
256 \r
257 \r
258 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<frame_factory>& factory, const std::wstring& params)\r
259 {\r
260         std::wstringstream iss(params);\r
261         std::vector<std::wstring> tokens;\r
262         typedef std::istream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t> > iterator;\r
263         std::copy(iterator(iss),  iterator(), std::back_inserter(tokens));\r
264         return create_producer(factory, tokens);\r
265 }\r
266 \r
267 }}