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