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