]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
2.1.0: -reroute: WIP.
[casparcg] / core / producer / stage.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 "stage.h"\r
25 \r
26 #include "layer.h"\r
27 \r
28 #include "../frame/draw_frame.h"\r
29 #include "../frame/frame_factory.h"\r
30 \r
31 #include <common/executor.h>\r
32 #include <common/future.h>\r
33 #include <common/diagnostics/graph.h>\r
34 \r
35 #include <core/frame/frame_transform.h>\r
36 \r
37 #include <boost/foreach.hpp>\r
38 #include <boost/timer.hpp>\r
39 #include <boost/property_tree/ptree.hpp>\r
40 #include <boost/range/algorithm_ext.hpp>\r
41 \r
42 #include <tbb/parallel_for_each.h>\r
43 \r
44 #include <functional>\r
45 #include <map>\r
46 #include <vector>\r
47 \r
48 namespace caspar { namespace core {\r
49         \r
50 struct stage::impl : public std::enable_shared_from_this<impl>\r
51 {                               \r
52         spl::shared_ptr<diagnostics::graph>                                                     graph_;\r
53         monitor::basic_subject                                                                          event_subject_;\r
54         reactive::basic_subject<std::map<int, class draw_frame>>        frames_subject_;\r
55         std::map<int, layer>                                                                            layers_;        \r
56         std::map<int, tweened_transform>                                                        tweens_;        \r
57         executor                                                                                                        executor_;\r
58 public:\r
59         impl(spl::shared_ptr<diagnostics::graph> graph) \r
60                 : graph_(std::move(graph))\r
61                 , event_subject_("stage")\r
62                 , executor_(L"stage")\r
63         {\r
64                 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));\r
65         }\r
66                 \r
67         std::map<int, draw_frame> operator()(const struct video_format_desc& format_desc)\r
68         {               \r
69                 auto frames = executor_.invoke([=]() -> std::map<int, draw_frame>\r
70                 {\r
71                         boost::timer frame_timer;\r
72 \r
73                         std::map<int, class draw_frame> frames;\r
74                         \r
75                         try\r
76                         {                       \r
77                                 std::vector<int> indices;\r
78 \r
79                                 BOOST_FOREACH(auto& layer, layers_)     \r
80                                 {\r
81                                         frames[layer.first] = draw_frame::empty();      \r
82                                         indices.push_back(layer.first);\r
83                                 }                               \r
84 \r
85                                 // WORKAROUND: Compiler doesn't seem to like lambda.\r
86                                 tbb::parallel_for_each(indices.begin(), indices.end(), std::bind(&stage::impl::draw, this, std::placeholders::_1, std::ref(format_desc), std::ref(frames)));\r
87                         }\r
88                         catch(...)\r
89                         {\r
90                                 layers_.clear();\r
91                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
92                         }       \r
93                         \r
94                         graph_->set_value("produce-time", frame_timer.elapsed()*format_desc.fps*0.5);\r
95                         event_subject_ << monitor::event("profiler/time") % frame_timer.elapsed() % (1.0/format_desc.fps);\r
96 \r
97                         return frames;\r
98                 });\r
99                 \r
100                 frames_subject_ << frames;\r
101                 return frames;\r
102         }\r
103 \r
104         void draw(int index, const video_format_desc& format_desc, std::map<int, draw_frame>& frames)\r
105         {\r
106                 auto& layer             = layers_[index];\r
107                 auto& tween             = tweens_[index];\r
108                                 \r
109                 auto frame  = layer.receive(format_desc);                                       \r
110                 auto frame1 = frame;\r
111                 frame1.transform() *= tween.fetch_and_tick(1);\r
112 \r
113                 if(format_desc.field_mode != core::field_mode::progressive)\r
114                 {                               \r
115                         auto frame2 = frame;\r
116                         frame2.transform() *= tween.fetch_and_tick(1);\r
117                         frame1 = core::draw_frame::interlace(frame1, frame2, format_desc.field_mode);\r
118                 }\r
119 \r
120                 frames[index] = frame1;\r
121         }\r
122 \r
123         layer& get_layer(int index)\r
124         {\r
125                 auto it = layers_.find(index);\r
126                 if(it == std::end(layers_))\r
127                 {\r
128                         it = layers_.insert(std::make_pair(index, layer(index))).first;\r
129                         it->second.subscribe(event_subject_);\r
130                 }\r
131                 return it->second;\r
132         }\r
133                 \r
134         boost::unique_future<void> apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)\r
135         {\r
136                 return executor_.begin_invoke([=]\r
137                 {\r
138                         BOOST_FOREACH(auto& transform, transforms)\r
139                         {\r
140                                 auto src = tweens_[std::get<0>(transform)].fetch();\r
141                                 auto dst = std::get<1>(transform)(src);\r
142                                 tweens_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));\r
143                         }\r
144                 }, task_priority::high_priority);\r
145         }\r
146                                                 \r
147         boost::unique_future<void> apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)\r
148         {\r
149                 return executor_.begin_invoke([=]\r
150                 {\r
151                         auto src = tweens_[index].fetch();\r
152                         auto dst = transform(src);\r
153                         tweens_[index] = tweened_transform(src, dst, mix_duration, tween);\r
154                 }, task_priority::high_priority);\r
155         }\r
156 \r
157         boost::unique_future<void> clear_transforms(int index)\r
158         {\r
159                 return executor_.begin_invoke([=]\r
160                 {\r
161                         tweens_.erase(index);\r
162                 }, task_priority::high_priority);\r
163         }\r
164 \r
165         boost::unique_future<void> clear_transforms()\r
166         {\r
167                 return executor_.begin_invoke([=]\r
168                 {\r
169                         tweens_.clear();\r
170                 }, task_priority::high_priority);\r
171         }\r
172                 \r
173         boost::unique_future<void> load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta)\r
174         {\r
175                 return executor_.begin_invoke([=]\r
176                 {\r
177                         get_layer(index).load(producer, preview, auto_play_delta);                      \r
178                 }, task_priority::high_priority);\r
179         }\r
180 \r
181         boost::unique_future<void> pause(int index)\r
182         {               \r
183                 return executor_.begin_invoke([=]\r
184                 {\r
185                         layers_[index].pause();\r
186                 }, task_priority::high_priority);\r
187         }\r
188 \r
189         boost::unique_future<void> play(int index)\r
190         {               \r
191                 return executor_.begin_invoke([=]\r
192                 {\r
193                         layers_[index].play();\r
194                 }, task_priority::high_priority);\r
195         }\r
196 \r
197         boost::unique_future<void> stop(int index)\r
198         {               \r
199                 return executor_.begin_invoke([=]\r
200                 {\r
201                         layers_[index].stop();\r
202                 }, task_priority::high_priority);\r
203         }\r
204 \r
205         boost::unique_future<void> clear(int index)\r
206         {\r
207                 return executor_.begin_invoke([=]\r
208                 {\r
209                         layers_.erase(index);\r
210                 }, task_priority::high_priority);\r
211         }\r
212                 \r
213         boost::unique_future<void> clear()\r
214         {\r
215                 return executor_.begin_invoke([=]\r
216                 {\r
217                         layers_.clear();\r
218                 }, task_priority::high_priority);\r
219         }       \r
220                 \r
221         boost::unique_future<void> swap_layers(stage& other)\r
222         {\r
223                 auto other_impl = other.impl_;\r
224 \r
225                 if(other_impl.get() == this)\r
226                         return async(launch::deferred, []{});\r
227                 \r
228                 auto func = [=]\r
229                 {\r
230                         auto layers                     = layers_ | boost::adaptors::map_values;\r
231                         auto other_layers       = other_impl->layers_ | boost::adaptors::map_values;\r
232 \r
233                         BOOST_FOREACH(auto& layer, layers)\r
234                                 layer.unsubscribe(event_subject_);\r
235                         \r
236                         BOOST_FOREACH(auto& layer, other_layers)\r
237                                 layer.unsubscribe(event_subject_);\r
238                         \r
239                         std::swap(layers_, other_impl->layers_);\r
240                                                 \r
241                         BOOST_FOREACH(auto& layer, layers)\r
242                                 layer.subscribe(event_subject_);\r
243                         \r
244                         BOOST_FOREACH(auto& layer, other_layers)\r
245                                 layer.subscribe(event_subject_);\r
246                 };              \r
247 \r
248                 return executor_.begin_invoke([=]\r
249                 {\r
250                         other_impl->executor_.invoke(func, task_priority::high_priority);\r
251                 }, task_priority::high_priority);\r
252         }\r
253 \r
254         boost::unique_future<void> swap_layer(int index, int other_index)\r
255         {\r
256                 return executor_.begin_invoke([=]\r
257                 {\r
258                         std::swap(layers_[index], layers_[other_index]);\r
259                 }, task_priority::high_priority);\r
260         }\r
261 \r
262         boost::unique_future<void> swap_layer(int index, int other_index, stage& other)\r
263         {\r
264                 auto other_impl = other.impl_;\r
265 \r
266                 if(other_impl.get() == this)\r
267                         return swap_layer(index, other_index);\r
268                 else\r
269                 {\r
270                         auto func = [=]\r
271                         {\r
272                                 auto& my_layer          = get_layer(index);\r
273                                 auto& other_layer       = other_impl->get_layer(other_index);\r
274 \r
275                                 my_layer.unsubscribe(event_subject_);\r
276                                 other_layer.unsubscribe(other_impl->event_subject_);\r
277 \r
278                                 std::swap(my_layer, other_layer);\r
279 \r
280                                 my_layer.subscribe(event_subject_);\r
281                                 other_layer.subscribe(other_impl->event_subject_);\r
282                         };              \r
283 \r
284                         return executor_.begin_invoke([=]\r
285                         {\r
286                                 other_impl->executor_.invoke(func, task_priority::high_priority);\r
287                         }, task_priority::high_priority);\r
288                 }\r
289         }\r
290                 \r
291         boost::unique_future<spl::shared_ptr<frame_producer>> foreground(int index)\r
292         {\r
293                 return executor_.begin_invoke([=]\r
294                 {\r
295                         return layers_[index].foreground();\r
296                 }, task_priority::high_priority);\r
297         }\r
298         \r
299         boost::unique_future<spl::shared_ptr<frame_producer>> background(int index)\r
300         {\r
301                 return executor_.begin_invoke([=]\r
302                 {\r
303                         return layers_[index].background();\r
304                 }, task_priority::high_priority);\r
305         }\r
306 \r
307         boost::unique_future<boost::property_tree::wptree> info()\r
308         {\r
309                 return executor_.begin_invoke([this]() -> boost::property_tree::wptree\r
310                 {\r
311                         boost::property_tree::wptree info;\r
312                         BOOST_FOREACH(auto& layer, layers_)                     \r
313                                 info.add_child(L"layers.layer", layer.second.info())\r
314                                         .add(L"index", layer.first);    \r
315                         return info;\r
316                 }, task_priority::high_priority);\r
317         }\r
318 \r
319         boost::unique_future<boost::property_tree::wptree> info(int index)\r
320         {\r
321                 return executor_.begin_invoke([=]\r
322                 {\r
323                         return layers_[index].info();\r
324                 }, task_priority::high_priority);\r
325         }               \r
326         \r
327         boost::unique_future<std::wstring> call(int index, const std::wstring& params)\r
328         {\r
329                 return flatten(executor_.begin_invoke([=]\r
330                 {\r
331                         return make_shared(layers_[index].foreground()->call(params));\r
332                 }, task_priority::high_priority));\r
333         }\r
334 };\r
335 \r
336 stage::stage(spl::shared_ptr<diagnostics::graph> graph) : impl_(new impl(std::move(graph))){}\r
337 boost::unique_future<std::wstring> stage::call(int index, const std::wstring& params){return impl_->call(index, params);}\r
338 boost::unique_future<void> stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){return impl_->apply_transforms(transforms);}\r
339 boost::unique_future<void> stage::apply_transform(int index, const std::function<core::frame_transform(core::frame_transform)>& transform, unsigned int mix_duration, const tweener& tween){return impl_->apply_transform(index, transform, mix_duration, tween);}\r
340 boost::unique_future<void> stage::clear_transforms(int index){return impl_->clear_transforms(index);}\r
341 boost::unique_future<void> stage::clear_transforms(){return impl_->clear_transforms();}\r
342 boost::unique_future<void> stage::load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta){return impl_->load(index, producer, preview, auto_play_delta);}\r
343 boost::unique_future<void> stage::pause(int index){return impl_->pause(index);}\r
344 boost::unique_future<void> stage::play(int index){return impl_->play(index);}\r
345 boost::unique_future<void> stage::stop(int index){return impl_->stop(index);}\r
346 boost::unique_future<void> stage::clear(int index){return impl_->clear(index);}\r
347 boost::unique_future<void> stage::clear(){return impl_->clear();}\r
348 boost::unique_future<void> stage::swap_layers(stage& other){return impl_->swap_layers(other);}\r
349 boost::unique_future<void> stage::swap_layer(int index, int other_index){return impl_->swap_layer(index, other_index);}\r
350 boost::unique_future<void> stage::swap_layer(int index, int other_index, stage& other){return impl_->swap_layer(index, other_index, other);}\r
351 boost::unique_future<spl::shared_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}\r
352 boost::unique_future<spl::shared_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}\r
353 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}\r
354 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}\r
355 std::map<int, class draw_frame> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}\r
356 void stage::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}\r
357 void stage::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}\r
358 void stage::subscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.subscribe(o);}\r
359 void stage::unsubscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.unsubscribe(o);}\r
360 }}