2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../StdAfx.h"
28 #include "../frame/draw_frame.h"
29 #include "../frame/frame_factory.h"
30 #include "../interaction/interaction_aggregator.h"
31 #include "../consumer/write_frame_consumer.h"
33 #include <common/executor.h>
34 #include <common/future.h>
35 #include <common/diagnostics/graph.h>
36 #include <common/timer.h>
38 #include <core/frame/frame_transform.h>
40 #include <boost/property_tree/ptree.hpp>
42 #include <tbb/parallel_for_each.h>
49 namespace caspar { namespace core {
51 struct stage::impl : public std::enable_shared_from_this<impl>
53 spl::shared_ptr<diagnostics::graph> graph_;
54 spl::shared_ptr<monitor::subject> monitor_subject_;
55 //reactive::basic_subject<std::map<int, draw_frame>> frames_subject_;
56 std::map<int, layer> layers_;
57 std::map<int, tweened_transform> tweens_;
58 interaction_aggregator aggregator_;
59 // map of layer -> map of tokens (src ref) -> layer_consumer
60 std::map<int, std::map<void*, spl::shared_ptr<write_frame_consumer>>> layer_consumers_;
63 impl(spl::shared_ptr<diagnostics::graph> graph)
64 : graph_(std::move(graph))
65 , monitor_subject_(spl::make_shared<monitor::subject>("/stage"))
66 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
69 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
72 std::map<int, draw_frame> operator()(const video_format_desc& format_desc)
74 caspar::timer frame_timer;
76 auto frames = executor_.invoke([=]() -> std::map<int, draw_frame>
79 std::map<int, draw_frame> frames;
83 std::vector<int> indices;
85 for (auto& layer : layers_)
87 // Prevent race conditions in parallel for each later
88 frames[layer.first] = draw_frame::empty();
90 layer_consumers_[layer.first];
92 indices.push_back(layer.first);
95 aggregator_.translate_and_send();
97 tbb::parallel_for_each(indices.begin(), indices.end(), [&](int index)
99 draw(index, format_desc, frames);
105 CASPAR_LOG_CURRENT_EXCEPTION();
112 //frames_subject_ << frames;
114 graph_->set_value("produce-time", frame_timer.elapsed()*format_desc.fps*0.5);
115 *monitor_subject_ << monitor::message("/profiler/time") % frame_timer.elapsed() % (1.0/format_desc.fps);
120 void draw(int index, const video_format_desc& format_desc, std::map<int, draw_frame>& frames)
122 auto& layer = layers_[index];
123 auto& tween = tweens_[index];
124 auto& consumers = layer_consumers_[index];
126 auto frame = layer.receive(format_desc);
128 if (!consumers.empty())
130 auto consumer_it = consumers | boost::adaptors::map_values;
131 tbb::parallel_for_each(consumer_it.begin(), consumer_it.end(), [&](decltype(*consumer_it.begin()) layer_consumer)
133 layer_consumer->send(frame);
139 frame1.transform() *= tween.fetch_and_tick(1);
141 if(format_desc.field_mode != core::field_mode::progressive)
144 frame2.transform() *= tween.fetch_and_tick(1);
145 frame1 = core::draw_frame::interlace(frame1, frame2, format_desc.field_mode);
148 frames[index] = frame1;
151 layer& get_layer(int index)
153 auto it = layers_.find(index);
154 if(it == std::end(layers_))
156 it = layers_.insert(std::make_pair(index, layer(index))).first;
157 it->second.monitor_output().attach_parent(monitor_subject_);
162 std::future<void> apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)
164 return executor_.begin_invoke([=]
166 for (auto& transform : transforms)
168 auto src = tweens_[std::get<0>(transform)].fetch();
169 auto dst = std::get<1>(transform)(src);
170 tweens_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));
172 }, task_priority::high_priority);
175 std::future<void> apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)
177 return executor_.begin_invoke([=]
179 auto src = tweens_[index].fetch();
180 auto dst = transform(src);
181 tweens_[index] = tweened_transform(src, dst, mix_duration, tween);
182 }, task_priority::high_priority);
185 std::future<void> clear_transforms(int index)
187 return executor_.begin_invoke([=]
189 tweens_.erase(index);
190 }, task_priority::high_priority);
193 std::future<void> clear_transforms()
195 return executor_.begin_invoke([=]
198 }, task_priority::high_priority);
201 std::future<frame_transform> get_current_transform(int index)
203 return executor_.begin_invoke([=]
205 return tweens_[index].fetch();
206 }, task_priority::high_priority);
209 std::future<void> load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
211 return executor_.begin_invoke([=]
213 get_layer(index).load(producer, preview, auto_play_delta);
214 }, task_priority::high_priority);
217 std::future<void> pause(int index)
219 return executor_.begin_invoke([=]
221 get_layer(index).pause();
222 }, task_priority::high_priority);
225 std::future<void> resume(int index)
227 return executor_.begin_invoke([=]
229 get_layer(index).resume();
230 }, task_priority::high_priority);
233 std::future<void> play(int index)
235 return executor_.begin_invoke([=]
237 get_layer(index).play();
238 }, task_priority::high_priority);
241 std::future<void> stop(int index)
243 return executor_.begin_invoke([=]
245 get_layer(index).stop();
246 }, task_priority::high_priority);
249 std::future<void> clear(int index)
251 return executor_.begin_invoke([=]
253 layers_.erase(index);
254 }, task_priority::high_priority);
257 std::future<void> clear()
259 return executor_.begin_invoke([=]
262 }, task_priority::high_priority);
265 std::future<void> swap_layers(stage& other, bool swap_transforms)
267 auto other_impl = other.impl_;
269 if (other_impl.get() == this)
271 return make_ready_future();
276 auto layers = layers_ | boost::adaptors::map_values;
277 auto other_layers = other_impl->layers_ | boost::adaptors::map_values;
279 for (auto& layer : layers)
280 layer.monitor_output().detach_parent();
282 for (auto& layer : other_layers)
283 layer.monitor_output().detach_parent();
285 std::swap(layers_, other_impl->layers_);
287 for (auto& layer : layers)
288 layer.monitor_output().attach_parent(monitor_subject_);
290 for (auto& layer : other_layers)
291 layer.monitor_output().attach_parent(monitor_subject_);
294 std::swap(tweens_, other_impl->tweens_);
297 return executor_.begin_invoke([=]
299 other_impl->executor_.invoke(func, task_priority::high_priority);
300 }, task_priority::high_priority);
303 std::future<void> swap_layer(int index, int other_index, bool swap_transforms)
305 return executor_.begin_invoke([=]
307 std::swap(get_layer(index), get_layer(other_index));
310 std::swap(tweens_[index], tweens_[other_index]);
311 }, task_priority::high_priority);
314 std::future<void> swap_layer(int index, int other_index, stage& other, bool swap_transforms)
316 auto other_impl = other.impl_;
318 if(other_impl.get() == this)
319 return swap_layer(index, other_index, swap_transforms);
324 auto& my_layer = get_layer(index);
325 auto& other_layer = other_impl->get_layer(other_index);
327 my_layer.monitor_output().detach_parent();
328 other_layer.monitor_output().detach_parent();
330 std::swap(my_layer, other_layer);
332 my_layer.monitor_output().attach_parent(monitor_subject_);
333 other_layer.monitor_output().attach_parent(other_impl->monitor_subject_);
337 auto& my_tween = tweens_[index];
338 auto& other_tween = other_impl->tweens_[other_index];
339 std::swap(my_tween, other_tween);
343 return executor_.begin_invoke([=]
345 other_impl->executor_.invoke(func, task_priority::high_priority);
346 }, task_priority::high_priority);
350 void add_layer_consumer(void* token, int layer, const spl::shared_ptr<write_frame_consumer>& layer_consumer)
352 executor_.begin_invoke([=]
354 layer_consumers_[layer].insert(std::make_pair(token, layer_consumer));
355 }, task_priority::high_priority);
358 void remove_layer_consumer(void* token, int layer)
360 executor_.begin_invoke([=]
362 auto& layer_map = layer_consumers_[layer];
363 layer_map.erase(token);
364 if (layer_map.empty())
366 layer_consumers_.erase(layer);
368 }, task_priority::high_priority);
371 std::future<std::shared_ptr<frame_producer>> foreground(int index)
373 return executor_.begin_invoke([=]() -> std::shared_ptr<frame_producer>
375 return get_layer(index).foreground();
376 }, task_priority::high_priority);
379 std::future<std::shared_ptr<frame_producer>> background(int index)
381 return executor_.begin_invoke([=]() -> std::shared_ptr<frame_producer>
383 return get_layer(index).background();
384 }, task_priority::high_priority);
387 std::future<boost::property_tree::wptree> info()
389 return executor_.begin_invoke([this]() -> boost::property_tree::wptree
391 boost::property_tree::wptree info;
392 for (auto& layer : layers_)
393 info.add_child(L"layers.layer", layer.second.info())
394 .add(L"index", layer.first);
396 }, task_priority::high_priority);
399 std::future<boost::property_tree::wptree> info(int index)
401 return executor_.begin_invoke([=]
403 return get_layer(index).info();
404 }, task_priority::high_priority);
407 std::future<std::wstring> call(int index, const std::vector<std::wstring>& params)
409 return flatten(executor_.begin_invoke([=]
411 return get_layer(index).foreground()->call(params).share();
412 }, task_priority::high_priority));
415 void on_interaction(const interaction_event::ptr& event)
417 executor_.begin_invoke([=]
419 aggregator_.offer(event);
420 }, task_priority::high_priority);
423 boost::optional<interaction_target> collission_detect(double x, double y)
425 for (auto& layer : layers_ | boost::adaptors::reversed)
427 auto transform = tweens_[layer.first].fetch();
428 auto translated = translate(x, y, transform);
430 if (translated.first >= 0.0
431 && translated.first <= 1.0
432 && translated.second >= 0.0
433 && translated.second <= 1.0
434 && layer.second.collides(translated.first, translated.second))
436 return std::make_pair(transform, static_cast<interaction_sink*>(&layer.second));
440 return boost::optional<interaction_target>();
444 stage::stage(spl::shared_ptr<diagnostics::graph> graph) : impl_(new impl(std::move(graph))){}
445 std::future<std::wstring> stage::call(int index, const std::vector<std::wstring>& params){return impl_->call(index, params);}
446 std::future<void> stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){ return impl_->apply_transforms(transforms); }
447 std::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); }
448 std::future<void> stage::clear_transforms(int index){ return impl_->clear_transforms(index); }
449 std::future<void> stage::clear_transforms(){ return impl_->clear_transforms(); }
450 std::future<frame_transform> stage::get_current_transform(int index){ return impl_->get_current_transform(index); }
451 std::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); }
452 std::future<void> stage::pause(int index){ return impl_->pause(index); }
453 std::future<void> stage::resume(int index){ return impl_->resume(index); }
454 std::future<void> stage::play(int index){ return impl_->play(index); }
455 std::future<void> stage::stop(int index){ return impl_->stop(index); }
456 std::future<void> stage::clear(int index){ return impl_->clear(index); }
457 std::future<void> stage::clear(){ return impl_->clear(); }
458 std::future<void> stage::swap_layers(stage& other, bool swap_transforms){ return impl_->swap_layers(other, swap_transforms); }
459 std::future<void> stage::swap_layer(int index, int other_index, bool swap_transforms){ return impl_->swap_layer(index, other_index, swap_transforms); }
460 std::future<void> stage::swap_layer(int index, int other_index, stage& other, bool swap_transforms){ return impl_->swap_layer(index, other_index, other, swap_transforms); }
461 void stage::add_layer_consumer(void* token, int layer, const spl::shared_ptr<write_frame_consumer>& layer_consumer){ impl_->add_layer_consumer(token, layer, layer_consumer); }
462 void stage::remove_layer_consumer(void* token, int layer){ impl_->remove_layer_consumer(token, layer); }std::future<std::shared_ptr<frame_producer>> stage::foreground(int index) { return impl_->foreground(index); }
463 std::future<std::shared_ptr<frame_producer>> stage::background(int index) { return impl_->background(index); }
464 std::future<boost::property_tree::wptree> stage::info() const{ return impl_->info(); }
465 std::future<boost::property_tree::wptree> stage::info(int index) const{ return impl_->info(index); }
466 std::map<int, draw_frame> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}
467 monitor::subject& stage::monitor_output(){return *impl_->monitor_subject_;}
468 //void stage::subscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.subscribe(o);}
469 //void stage::unsubscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.unsubscribe(o);}
470 void stage::on_interaction(const interaction_event::ptr& event) { impl_->on_interaction(event); }