]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
2.1.0: Some mayor refactoring.
[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/basic_frame.h"\r
29 #include "frame/frame_factory.h"\r
30 \r
31 #include <common/concurrency/executor.h>\r
32 #include <common/diagnostics/graph.h>\r
33 \r
34 #include <core/producer/frame/frame_transform.h>\r
35 \r
36 #include <boost/foreach.hpp>\r
37 #include <boost/timer.hpp>\r
38 \r
39 #include <tbb/parallel_for_each.h>\r
40 \r
41 #include <boost/property_tree/ptree.hpp>\r
42 #include <boost/range/algorithm_ext.hpp>\r
43 \r
44 #include <map>\r
45 #include <vector>\r
46 \r
47 namespace caspar { namespace core {\r
48         \r
49 template<typename T>\r
50 class tweened_transform\r
51 {\r
52         T source_;\r
53         T dest_;\r
54         int duration_;\r
55         int time_;\r
56         tweener_t tweener_;\r
57 public: \r
58         tweened_transform()\r
59                 : duration_(0)\r
60                 , time_(0)\r
61                 , tweener_(get_tweener(L"linear")){}\r
62         tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")\r
63                 : source_(source)\r
64                 , dest_(dest)\r
65                 , duration_(duration)\r
66                 , time_(0)\r
67                 , tweener_(get_tweener(tween)){}\r
68         \r
69         T fetch()\r
70         {\r
71                 return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);\r
72         }\r
73 \r
74         T fetch_and_tick(int num)\r
75         {                                               \r
76                 time_ = std::min(time_+num, duration_);\r
77                 return fetch();\r
78         }\r
79 };\r
80 \r
81 struct stage::impl : public std::enable_shared_from_this<impl>\r
82                                    , boost::noncopyable\r
83 {               \r
84         safe_ptr<stage::target_t>                                                               target_;\r
85         video_format_desc                                                                               format_desc_;\r
86         \r
87         safe_ptr<diagnostics::graph>                                                    graph_;\r
88 \r
89         boost::timer                                                                                    produce_timer_;\r
90         boost::timer                                                                                    tick_timer_;\r
91 \r
92         std::map<int, layer>                                                                    layers_;        \r
93         std::map<int, tweened_transform<core::frame_transform>> transforms_;    \r
94 \r
95         executor                                                                                                executor_;\r
96 public:\r
97         impl(const safe_ptr<stage::target_t>& target, const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc)  \r
98                 : target_(target)\r
99                 , graph_(graph)\r
100                 , format_desc_(format_desc)\r
101                 , executor_(L"stage")\r
102         {\r
103                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
104                 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));\r
105         }\r
106 \r
107         void spawn_token()\r
108         {\r
109                 std::weak_ptr<impl> self = shared_from_this();\r
110                 executor_.begin_invoke([=]{tick(self);});\r
111         }\r
112         \r
113         void tick(const std::weak_ptr<impl>& self)\r
114         {               \r
115                 try\r
116                 {\r
117                         produce_timer_.restart();\r
118 \r
119                         std::map<int, safe_ptr<basic_frame>> frames;\r
120                 \r
121                         BOOST_FOREACH(auto& layer, layers_)                     \r
122                                 frames[layer.first] = basic_frame::empty();     \r
123 \r
124                         tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer) \r
125                         {\r
126                                 auto transform = transforms_[layer.first].fetch_and_tick(1);\r
127 \r
128                                 int flags = frame_producer::flags::none;\r
129                                 if(format_desc_.field_mode != field_mode::progressive)\r
130                                 {\r
131                                         flags |= std::abs(transform.fill_scale[1]  - 1.0) > 0.0001 ? frame_producer::flags::deinterlace : frame_producer::flags::none;\r
132                                         flags |= std::abs(transform.fill_translation[1])  > 0.0001 ? frame_producer::flags::deinterlace : frame_producer::flags::none;\r
133                                 }\r
134 \r
135                                 if(transform.is_key)\r
136                                         flags |= frame_producer::flags::alpha_only;\r
137 \r
138                                 auto frame = layer.second.receive(flags);       \r
139                                 \r
140                                 auto frame1 = make_safe<core::basic_frame>(frame);\r
141                                 frame1->get_frame_transform() = transform;\r
142 \r
143                                 if(format_desc_.field_mode != core::field_mode::progressive)\r
144                                 {                               \r
145                                         auto frame2 = make_safe<core::basic_frame>(frame);\r
146                                         frame2->get_frame_transform() = transforms_[layer.first].fetch_and_tick(1);\r
147                                         frame1 = core::basic_frame::interlace(frame1, frame2, format_desc_.field_mode);\r
148                                 }\r
149 \r
150                                 frames[layer.first] = frame1;\r
151                         });\r
152                         \r
153                         graph_->set_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);\r
154                         \r
155                         std::shared_ptr<void> ticket(nullptr, [self](void*)\r
156                         {\r
157                                 auto self2 = self.lock();\r
158                                 if(self2)                               \r
159                                         self2->executor_.begin_invoke([=]{tick(self);});                                \r
160                         });\r
161                                                 \r
162                         target_->send(std::make_pair(frames, ticket));\r
163 \r
164                         graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
165                         tick_timer_.restart();\r
166                 }\r
167                 catch(...)\r
168                 {\r
169                         layers_.clear();\r
170                         CASPAR_LOG_CURRENT_EXCEPTION();\r
171                 }               \r
172         }\r
173                 \r
174         void set_transform(int index, const frame_transform& transform, unsigned int mix_duration, const std::wstring& tween)\r
175         {\r
176                 executor_.begin_invoke([=]\r
177                 {\r
178                         auto src = transforms_[index].fetch();\r
179                         auto dst = transform;\r
180                         transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
181                 }, high_priority);\r
182         }\r
183                                 \r
184         void apply_transform(int index, const std::function<frame_transform(frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween)\r
185         {\r
186                 executor_.begin_invoke([=]\r
187                 {\r
188                         auto src = transforms_[index].fetch();\r
189                         auto dst = transform(src);\r
190                         transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
191                 }, high_priority);\r
192         }\r
193 \r
194         void clear_transforms(int index)\r
195         {\r
196                 executor_.begin_invoke([=]\r
197                 {\r
198                         transforms_.erase(index);\r
199                 }, high_priority);\r
200         }\r
201 \r
202         void clear_transforms()\r
203         {\r
204                 executor_.begin_invoke([=]\r
205                 {\r
206                         transforms_.clear();\r
207                 }, high_priority);\r
208         }\r
209                 \r
210         void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
211         {\r
212                 executor_.begin_invoke([=]\r
213                 {\r
214                         layers_[index].load(producer, preview, auto_play_delta);\r
215                 }, high_priority);\r
216         }\r
217 \r
218         void pause(int index)\r
219         {               \r
220                 executor_.begin_invoke([=]\r
221                 {\r
222                         layers_[index].pause();\r
223                 }, high_priority);\r
224         }\r
225 \r
226         void play(int index)\r
227         {               \r
228                 executor_.begin_invoke([=]\r
229                 {\r
230                         layers_[index].play();\r
231                 }, high_priority);\r
232         }\r
233 \r
234         void stop(int index)\r
235         {               \r
236                 executor_.begin_invoke([=]\r
237                 {\r
238                         layers_[index].stop();\r
239                 }, high_priority);\r
240         }\r
241 \r
242         void clear(int index)\r
243         {\r
244                 executor_.begin_invoke([=]\r
245                 {\r
246                         layers_.erase(index);\r
247                 }, high_priority);\r
248         }\r
249                 \r
250         void clear()\r
251         {\r
252                 executor_.begin_invoke([=]\r
253                 {\r
254                         layers_.clear();\r
255                 }, high_priority);\r
256         }       \r
257         \r
258         boost::unique_future<std::wstring> call(int index, bool foreground, const std::wstring& param)\r
259         {\r
260                 return std::move(*executor_.invoke([=]\r
261                 {\r
262                         return std::make_shared<boost::unique_future<std::wstring>>(std::move(layers_[index].call(foreground, param)));\r
263                 }, high_priority));\r
264         }\r
265         \r
266         void swap_layers(const safe_ptr<stage>& other)\r
267         {\r
268                 if(other->impl_.get() == this)\r
269                         return;\r
270                 \r
271                 auto func = [=]\r
272                 {\r
273                         std::swap(layers_, other->impl_->layers_);\r
274                 };              \r
275                 executor_.begin_invoke([=]\r
276                 {\r
277                         other->impl_->executor_.invoke(func, high_priority);\r
278                 }, high_priority);\r
279         }\r
280 \r
281         void swap_layer(int index, int other_index)\r
282         {\r
283                 executor_.begin_invoke([=]\r
284                 {\r
285                         std::swap(layers_[index], layers_[other_index]);\r
286                 }, high_priority);\r
287         }\r
288 \r
289         void swap_layer(int index, int other_index, const safe_ptr<stage>& other)\r
290         {\r
291                 if(other->impl_.get() == this)\r
292                         swap_layer(index, other_index);\r
293                 else\r
294                 {\r
295                         auto func = [=]\r
296                         {\r
297                                 std::swap(layers_[index], other->impl_->layers_[other_index]);\r
298                         };              \r
299                         executor_.begin_invoke([=]\r
300                         {\r
301                                 other->impl_->executor_.invoke(func, high_priority);\r
302                         }, high_priority);\r
303                 }\r
304         }\r
305                 \r
306         boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
307         {\r
308                 return executor_.begin_invoke([=]\r
309                 {\r
310                         return layers_[index].foreground();\r
311                 }, high_priority);\r
312         }\r
313         \r
314         boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
315         {\r
316                 return executor_.begin_invoke([=]\r
317                 {\r
318                         return layers_[index].background();\r
319                 }, high_priority);\r
320         }\r
321         \r
322         void set_video_format_desc(const video_format_desc& format_desc)\r
323         {\r
324                 executor_.begin_invoke([=]\r
325                 {\r
326                         format_desc_ = format_desc;\r
327                 }, high_priority);\r
328         }\r
329 \r
330         boost::unique_future<boost::property_tree::wptree> info()\r
331         {\r
332                 return std::move(executor_.begin_invoke([this]() -> boost::property_tree::wptree\r
333                 {\r
334                         boost::property_tree::wptree info;\r
335                         BOOST_FOREACH(auto& layer, layers_)                     \r
336                                 info.add_child(L"layers.layer", layer.second.info())\r
337                                         .add(L"index", layer.first);    \r
338                         return info;\r
339                 }, high_priority));\r
340         }\r
341 \r
342         boost::unique_future<boost::property_tree::wptree> info(int index)\r
343         {\r
344                 return std::move(executor_.begin_invoke([=]() -> boost::property_tree::wptree\r
345                 {\r
346                         return layers_[index].info();\r
347                 }, high_priority));\r
348         }\r
349 };\r
350 \r
351 stage::stage(const safe_ptr<stage::target_t>& target, const safe_ptr<diagnostics::graph>& graph, const struct video_format_desc& format_desc) : impl_(new impl(target, graph, format_desc)){}\r
352 void stage::set_frame_transform(int index, const core::frame_transform& transform, unsigned int mix_duration, const std::wstring& tween){impl_->set_transform(index, transform, mix_duration, tween);}\r
353 void stage::apply_frame_transform(int index, const std::function<core::frame_transform(core::frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween){impl_->apply_transform(index, transform, mix_duration, tween);}\r
354 void stage::clear_transforms(int index){impl_->clear_transforms(index);}\r
355 void stage::clear_transforms(){impl_->clear_transforms();}\r
356 void stage::spawn_token(){impl_->spawn_token();}\r
357 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
358 void stage::pause(int index){impl_->pause(index);}\r
359 void stage::play(int index){impl_->play(index);}\r
360 void stage::stop(int index){impl_->stop(index);}\r
361 void stage::clear(int index){impl_->clear(index);}\r
362 void stage::clear(){impl_->clear();}\r
363 void stage::swap_layers(const safe_ptr<stage>& other){impl_->swap_layers(other);}\r
364 void stage::swap_layer(int index, int other_index){impl_->swap_layer(index, other_index);}\r
365 void stage::swap_layer(int index, int other_index, const safe_ptr<stage>& other){impl_->swap_layer(index, other_index, other);}\r
366 boost::unique_future<safe_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}\r
367 boost::unique_future<safe_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}\r
368 boost::unique_future<std::wstring> stage::call(int index, bool foreground, const std::wstring& param){return impl_->call(index, foreground, param);}\r
369 void stage::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
370 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}\r
371 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}\r
372 }}