]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
2.0.2: Improved INFO.
[casparcg] / core / producer / stage.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 \r
21 #include "../StdAfx.h"\r
22 \r
23 #include "stage.h"\r
24 \r
25 #include "layer.h"\r
26 \r
27 #include "frame/basic_frame.h"\r
28 #include "frame/frame_factory.h"\r
29 \r
30 #include <common/concurrency/executor.h>\r
31 \r
32 #include <boost/foreach.hpp>\r
33 #include <boost/timer.hpp>\r
34 \r
35 #include <tbb/parallel_for_each.h>\r
36 \r
37 #include <boost/property_tree/ptree.hpp>\r
38 \r
39 #include <map>\r
40 \r
41 namespace caspar { namespace core {\r
42 \r
43 struct stage::implementation : public std::enable_shared_from_this<implementation>\r
44                                                          , boost::noncopyable\r
45 {               \r
46         safe_ptr<diagnostics::graph>    graph_;\r
47         safe_ptr<stage::target_t>               target_;\r
48         video_format_desc                               format_desc_;\r
49 \r
50         boost::timer                                    produce_timer_;\r
51         boost::timer                                    tick_timer_;\r
52 \r
53         std::map<int, layer>                    layers_;        \r
54 \r
55         executor                                                executor_;\r
56 public:\r
57         implementation(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<stage::target_t>& target, const video_format_desc& format_desc)  \r
58                 : graph_(graph)\r
59                 , format_desc_(format_desc)\r
60                 , target_(target)\r
61                 , executor_(L"stage")\r
62         {\r
63                 graph_->add_guide("tick-time", 0.5f);   \r
64                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
65                 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));\r
66         }\r
67 \r
68         void spawn_token()\r
69         {\r
70                 std::weak_ptr<implementation> self = shared_from_this();\r
71                 executor_.begin_invoke([=]{tick(self);});\r
72         }\r
73                                                         \r
74         void tick(const std::weak_ptr<implementation>& self)\r
75         {               \r
76                 try\r
77                 {\r
78                         produce_timer_.restart();\r
79 \r
80                         std::map<int, safe_ptr<basic_frame>> frames;\r
81                 \r
82                         BOOST_FOREACH(auto& layer, layers_)                     \r
83                                 frames[layer.first] = basic_frame::empty();     \r
84 \r
85                         tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer) \r
86                         {\r
87                                 frames[layer.first] = layer.second.receive();   \r
88                         });\r
89                         \r
90                         graph_->update_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);\r
91                         \r
92                         std::shared_ptr<void> ticket(nullptr, [self](void*)\r
93                         {\r
94                                 auto self2 = self.lock();\r
95                                 if(self2)                               \r
96                                         self2->executor_.begin_invoke([=]{tick(self);});                                \r
97                         });\r
98 \r
99                         target_->send(std::make_pair(frames, ticket));\r
100 \r
101                         graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
102                         tick_timer_.restart();\r
103                 }\r
104                 catch(...)\r
105                 {\r
106                         layers_.clear();\r
107                         CASPAR_LOG_CURRENT_EXCEPTION();\r
108                 }               \r
109         }\r
110 \r
111         void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
112         {\r
113                 executor_.begin_invoke([=]\r
114                 {\r
115                         layers_[index].load(producer, preview, auto_play_delta);\r
116                 }, high_priority);\r
117         }\r
118 \r
119         void pause(int index)\r
120         {               \r
121                 executor_.begin_invoke([=]\r
122                 {\r
123                         layers_[index].pause();\r
124                 }, high_priority);\r
125         }\r
126 \r
127         void play(int index)\r
128         {               \r
129                 executor_.begin_invoke([=]\r
130                 {\r
131                         layers_[index].play();\r
132                 }, high_priority);\r
133         }\r
134 \r
135         void stop(int index)\r
136         {               \r
137                 executor_.begin_invoke([=]\r
138                 {\r
139                         layers_[index].stop();\r
140                 }, high_priority);\r
141         }\r
142 \r
143         void clear(int index)\r
144         {\r
145                 executor_.begin_invoke([=]\r
146                 {\r
147                         layers_.erase(index);\r
148                 }, high_priority);\r
149         }\r
150                 \r
151         void clear()\r
152         {\r
153                 executor_.begin_invoke([=]\r
154                 {\r
155                         layers_.clear();\r
156                 }, high_priority);\r
157         }       \r
158         \r
159         boost::unique_future<std::wstring> call(int index, bool foreground, const std::wstring& param)\r
160         {\r
161                 return std::move(*executor_.invoke([=]\r
162                 {\r
163                         return std::make_shared<boost::unique_future<std::wstring>>(std::move(layers_[index].call(foreground, param)));\r
164                 }, high_priority));\r
165         }\r
166         \r
167         void swap_layers(const safe_ptr<stage>& other)\r
168         {\r
169                 if(other->impl_.get() == this)\r
170                         return;\r
171                 \r
172                 auto func = [=]\r
173                 {\r
174                         std::swap(layers_, other->impl_->layers_);\r
175                 };              \r
176                 executor_.begin_invoke([=]\r
177                 {\r
178                         other->impl_->executor_.invoke(func, high_priority);\r
179                 }, high_priority);\r
180         }\r
181 \r
182         void swap_layer(int index, size_t other_index)\r
183         {\r
184                 executor_.begin_invoke([=]\r
185                 {\r
186                         std::swap(layers_[index], layers_[other_index]);\r
187                 }, high_priority);\r
188         }\r
189 \r
190         void swap_layer(int index, size_t other_index, const safe_ptr<stage>& other)\r
191         {\r
192                 if(other->impl_.get() == this)\r
193                         swap_layer(index, other_index);\r
194                 else\r
195                 {\r
196                         auto func = [=]\r
197                         {\r
198                                 std::swap(layers_[index], other->impl_->layers_[other_index]);\r
199                         };              \r
200                         executor_.begin_invoke([=]\r
201                         {\r
202                                 other->impl_->executor_.invoke(func, high_priority);\r
203                         }, high_priority);\r
204                 }\r
205         }\r
206 \r
207         boost::unique_future<layer_status> get_status(int index)\r
208         {               \r
209                 return executor_.begin_invoke([=]\r
210                 {\r
211                         return layers_[index].status();\r
212                 }, high_priority );\r
213         }\r
214         \r
215         boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
216         {\r
217                 return executor_.begin_invoke([=]\r
218                 {\r
219                         return layers_[index].foreground();\r
220                 }, high_priority);\r
221         }\r
222         \r
223         boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
224         {\r
225                 return executor_.begin_invoke([=]\r
226                 {\r
227                         return layers_[index].background();\r
228                 }, high_priority);\r
229         }\r
230         \r
231         void set_video_format_desc(const video_format_desc& format_desc)\r
232         {\r
233                 executor_.begin_invoke([=]\r
234                 {\r
235                         format_desc_ = format_desc;\r
236                 }, high_priority);\r
237         }\r
238 \r
239         boost::unique_future<boost::property_tree::wptree> info()\r
240         {\r
241                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree\r
242                 {\r
243                         boost::property_tree::wptree info;\r
244                         auto& layers_node = info.add(L"layers", L"");\r
245                         BOOST_FOREACH(auto& layer, layers_)\r
246                         {\r
247                                 auto layer_info = layer.second.info();\r
248                                 layer_info.add(L"layer.index", layer.first);\r
249                                 BOOST_FOREACH(auto& update, layer_info)   \r
250                                         layers_node.add_child(update.first, update.second);\r
251                         }\r
252                         return info;\r
253                 }, high_priority));\r
254         }\r
255 };\r
256 \r
257 stage::stage(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<target_t>& target, const video_format_desc& format_desc) : impl_(new implementation(graph, target, format_desc)){}\r
258 void stage::spawn_token(){impl_->spawn_token();}\r
259 void stage::load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta){impl_->load(index, producer, preview, auto_play_delta);}\r
260 void stage::pause(int index){impl_->pause(index);}\r
261 void stage::play(int index){impl_->play(index);}\r
262 void stage::stop(int index){impl_->stop(index);}\r
263 void stage::clear(int index){impl_->clear(index);}\r
264 void stage::clear(){impl_->clear();}\r
265 void stage::swap_layers(const safe_ptr<stage>& other){impl_->swap_layers(other);}\r
266 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
267 void stage::swap_layer(int index, size_t other_index, const safe_ptr<stage>& other){impl_->swap_layer(index, other_index, other);}\r
268 boost::unique_future<layer_status> stage::get_status(int index){return impl_->get_status(index);}\r
269 boost::unique_future<safe_ptr<frame_producer>> stage::foreground(size_t index) {return impl_->foreground(index);}\r
270 boost::unique_future<safe_ptr<frame_producer>> stage::background(size_t index) {return impl_->background(index);}\r
271 boost::unique_future<std::wstring> stage::call(int index, bool foreground, const std::wstring& param){return impl_->call(index, foreground, param);}\r
272 void stage::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
273 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}\r
274 }}