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