-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG (www.casparcg.com).\r
-*\r
-* CasparCG is free software: you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation, either version 3 of the License, or\r
-* (at your option) any later version.\r
-*\r
-* CasparCG is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-* GNU General Public License for more details.\r
-*\r
-* You should have received a copy of the GNU General Public License\r
-* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-* Author: Robert Nagy, ronag89@gmail.com\r
-*/\r
-\r
-#include "../StdAfx.h"\r
-\r
-#include "stage.h"\r
-\r
-#include "layer.h"\r
-\r
-#include "frame/basic_frame.h"\r
-#include "frame/frame_factory.h"\r
-\r
-#include <common/concurrency/executor.h>\r
-\r
-#include <core/producer/frame/frame_transform.h>\r
-\r
-#include <boost/foreach.hpp>\r
-#include <boost/timer.hpp>\r
-\r
-#include <tbb/parallel_for_each.h>\r
-\r
-#include <boost/property_tree/ptree.hpp>\r
-\r
-#include <map>\r
-\r
-namespace caspar { namespace core {\r
- \r
-template<typename T>\r
-class tweened_transform\r
-{\r
- T source_;\r
- T dest_;\r
- int duration_;\r
- int time_;\r
- tweener_t tweener_;\r
-public: \r
- tweened_transform()\r
- : duration_(0)\r
- , time_(0)\r
- , tweener_(get_tweener(L"linear")){}\r
- tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")\r
- : source_(source)\r
- , dest_(dest)\r
- , duration_(duration)\r
- , time_(0)\r
- , tweener_(get_tweener(tween)){}\r
- \r
- T fetch()\r
- {\r
- return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);\r
- }\r
-\r
- T fetch_and_tick(int num)\r
- { \r
- time_ = std::min(time_+num, duration_);\r
- return fetch();\r
- }\r
-};\r
-\r
-struct stage::implementation : public std::enable_shared_from_this<implementation>\r
- , boost::noncopyable\r
-{ \r
- safe_ptr<diagnostics::graph> graph_;\r
- safe_ptr<stage::target_t> target_;\r
- video_format_desc format_desc_;\r
-\r
- boost::timer produce_timer_;\r
- boost::timer tick_timer_;\r
-\r
- std::map<int, layer> layers_; \r
- std::map<int, tweened_transform<core::frame_transform>> transforms_; \r
-\r
- executor executor_;\r
-public:\r
- implementation(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<stage::target_t>& target, const video_format_desc& format_desc) \r
- : graph_(graph)\r
- , format_desc_(format_desc)\r
- , target_(target)\r
- , executor_(L"stage")\r
- {\r
- graph_->add_guide("tick-time", 0.5f); \r
- graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f)); \r
- graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));\r
- }\r
-\r
- void spawn_token()\r
- {\r
- std::weak_ptr<implementation> self = shared_from_this();\r
- executor_.begin_invoke([=]{tick(self);});\r
- }\r
- \r
- void tick(const std::weak_ptr<implementation>& self)\r
- { \r
- try\r
- {\r
- produce_timer_.restart();\r
-\r
- std::map<int, safe_ptr<basic_frame>> frames;\r
- \r
- BOOST_FOREACH(auto& layer, layers_) \r
- frames[layer.first] = basic_frame::empty(); \r
-\r
- tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer) \r
- {\r
- auto transform = transforms_[layer.first].fetch_and_tick(1);\r
-\r
- int hints = frame_producer::NO_HINT;\r
- if(format_desc_.field_mode != field_mode::progressive)\r
- {\r
- hints |= std::abs(transform.fill_scale[1] - 1.0) > 0.0001 ? frame_producer::DEINTERLACE_HINT : frame_producer::NO_HINT;\r
- hints |= std::abs(transform.fill_translation[1]) > 0.0001 ? frame_producer::DEINTERLACE_HINT : frame_producer::NO_HINT;\r
- }\r
-\r
- if(transform.is_key)\r
- hints |= frame_producer::ALPHA_HINT;\r
-\r
- auto frame = layer.second.receive(hints); \r
- \r
- auto frame1 = make_safe<core::basic_frame>(frame);\r
- frame1->get_frame_transform() = transform;\r
-\r
- if(format_desc_.field_mode != core::field_mode::progressive)\r
- { \r
- auto frame2 = make_safe<core::basic_frame>(frame);\r
- frame2->get_frame_transform() = transforms_[layer.first].fetch_and_tick(1);\r
- frame1 = core::basic_frame::interlace(frame1, frame2, format_desc_.field_mode);\r
- }\r
-\r
- frames[layer.first] = frame1;\r
- });\r
- \r
- graph_->update_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);\r
- \r
- std::shared_ptr<void> ticket(nullptr, [self](void*)\r
- {\r
- auto self2 = self.lock();\r
- if(self2) \r
- self2->executor_.begin_invoke([=]{tick(self);}); \r
- });\r
-\r
- target_->send(std::make_pair(frames, ticket));\r
-\r
- graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
- tick_timer_.restart();\r
- }\r
- catch(...)\r
- {\r
- layers_.clear();\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- } \r
- }\r
- void set_transform(int index, const frame_transform& transform, unsigned int mix_duration, const std::wstring& tween)\r
- {\r
- executor_.begin_invoke([=]\r
- {\r
- auto src = transforms_[index].fetch();\r
- auto dst = transform;\r
- transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
- }, high_priority);\r
- }\r
- \r
- void apply_transform(int index, const std::function<frame_transform(frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween)\r
- {\r
- executor_.begin_invoke([=]\r
- {\r
- auto src = transforms_[index].fetch();\r
- auto dst = transform(src);\r
- transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
- }, high_priority);\r
- }\r
-\r
- void clear_transforms(int index)\r
- {\r
- executor_.begin_invoke([=]\r
- {\r
- transforms_.erase(index);\r
- }, high_priority);\r
- }\r
-\r
- void clear_transforms()\r
- {\r
- executor_.begin_invoke([=]\r
- {\r
- transforms_.clear();\r
- }, high_priority);\r
- }\r
- \r
- void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
- {\r
- executor_.begin_invoke([=]\r
- {\r
- layers_[index].load(producer, preview, auto_play_delta);\r
- }, high_priority);\r
- }\r
-\r
- void pause(int index)\r
- { \r
- executor_.begin_invoke([=]\r
- {\r
- layers_[index].pause();\r
- }, high_priority);\r
- }\r
-\r
- void play(int index)\r
- { \r
- executor_.begin_invoke([=]\r
- {\r
- layers_[index].play();\r
- }, high_priority);\r
- }\r
-\r
- void stop(int index)\r
- { \r
- executor_.begin_invoke([=]\r
- {\r
- layers_[index].stop();\r
- }, high_priority);\r
- }\r
-\r
- void clear(int index)\r
- {\r
- executor_.begin_invoke([=]\r
- {\r
- layers_.erase(index);\r
- }, high_priority);\r
- }\r
- \r
- void clear()\r
- {\r
- executor_.begin_invoke([=]\r
- {\r
- layers_.clear();\r
- }, high_priority);\r
- } \r
- \r
- boost::unique_future<std::wstring> call(int index, bool foreground, const std::wstring& param)\r
- {\r
- return std::move(*executor_.invoke([=]\r
- {\r
- return std::make_shared<boost::unique_future<std::wstring>>(std::move(layers_[index].call(foreground, param)));\r
- }, high_priority));\r
- }\r
- \r
- void swap_layers(const safe_ptr<stage>& other)\r
- {\r
- if(other->impl_.get() == this)\r
- return;\r
- \r
- auto func = [=]\r
- {\r
- std::swap(layers_, other->impl_->layers_);\r
- }; \r
- executor_.begin_invoke([=]\r
- {\r
- other->impl_->executor_.invoke(func, high_priority);\r
- }, high_priority);\r
- }\r
-\r
- void swap_layer(int index, size_t other_index)\r
- {\r
- executor_.begin_invoke([=]\r
- {\r
- std::swap(layers_[index], layers_[other_index]);\r
- }, high_priority);\r
- }\r
-\r
- void swap_layer(int index, size_t other_index, const safe_ptr<stage>& other)\r
- {\r
- if(other->impl_.get() == this)\r
- swap_layer(index, other_index);\r
- else\r
- {\r
- auto func = [=]\r
- {\r
- std::swap(layers_[index], other->impl_->layers_[other_index]);\r
- }; \r
- executor_.begin_invoke([=]\r
- {\r
- other->impl_->executor_.invoke(func, high_priority);\r
- }, high_priority);\r
- }\r
- }\r
- \r
- boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
- {\r
- return executor_.begin_invoke([=]\r
- {\r
- return layers_[index].foreground();\r
- }, high_priority);\r
- }\r
- \r
- boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
- {\r
- return executor_.begin_invoke([=]\r
- {\r
- return layers_[index].background();\r
- }, high_priority);\r
- }\r
- \r
- void set_video_format_desc(const video_format_desc& format_desc)\r
- {\r
- executor_.begin_invoke([=]\r
- {\r
- format_desc_ = format_desc;\r
- }, high_priority);\r
- }\r
-\r
- boost::unique_future<boost::property_tree::wptree> info()\r
- {\r
- return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree\r
- {\r
- boost::property_tree::wptree info;\r
- BOOST_FOREACH(auto& layer, layers_) \r
- info.add_child(L"layers.layer", layer.second.info())\r
- .add(L"index", layer.first); \r
- return info;\r
- }, high_priority));\r
- }\r
-\r
- boost::unique_future<boost::property_tree::wptree> info(int index)\r
- {\r
- return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree\r
- {\r
- return layers_[index].info();\r
- }, high_priority));\r
- }\r
-};\r
-\r
-stage::stage(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<target_t>& target, const video_format_desc& format_desc) : impl_(new implementation(graph, target, format_desc)){}\r
-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
-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
-void stage::clear_transforms(int index){impl_->clear_transforms(index);}\r
-void stage::clear_transforms(){impl_->clear_transforms();}\r
-void stage::spawn_token(){impl_->spawn_token();}\r
-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
-void stage::pause(int index){impl_->pause(index);}\r
-void stage::play(int index){impl_->play(index);}\r
-void stage::stop(int index){impl_->stop(index);}\r
-void stage::clear(int index){impl_->clear(index);}\r
-void stage::clear(){impl_->clear();}\r
-void stage::swap_layers(const safe_ptr<stage>& other){impl_->swap_layers(other);}\r
-void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
-void stage::swap_layer(int index, size_t other_index, const safe_ptr<stage>& other){impl_->swap_layer(index, other_index, other);}\r
-boost::unique_future<safe_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}\r
-boost::unique_future<safe_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}\r
-boost::unique_future<std::wstring> stage::call(int index, bool foreground, const std::wstring& param){return impl_->call(index, foreground, param);}\r
-void stage::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
-boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}\r
-boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}\r
-}}
\ No newline at end of file
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../StdAfx.h"
+
+#include "stage.h"
+
+#include "layer.h"
+
+#include "../frame/draw_frame.h"
+#include "../frame/frame_factory.h"
+#include "../interaction/interaction_aggregator.h"
+#include "../consumer/write_frame_consumer.h"
+
+#include <common/executor.h>
+#include <common/future.h>
+#include <common/diagnostics/graph.h>
+#include <common/timer.h>
+
+#include <core/frame/frame_transform.h>
+
+#include <boost/property_tree/ptree.hpp>
+
+#include <tbb/parallel_for_each.h>
+
+#include <functional>
+#include <map>
+#include <vector>
+#include <future>
+
+namespace caspar { namespace core {
+
+struct stage::impl : public std::enable_shared_from_this<impl>
+{
+ int channel_index_;
+ spl::shared_ptr<diagnostics::graph> graph_;
+ spl::shared_ptr<monitor::subject> monitor_subject_ = spl::make_shared<monitor::subject>("/stage");
+ std::map<int, layer> layers_;
+ std::map<int, tweened_transform> tweens_;
+ interaction_aggregator aggregator_;
+ // map of layer -> map of tokens (src ref) -> layer_consumer
+ std::map<int, std::map<void*, spl::shared_ptr<write_frame_consumer>>> layer_consumers_;
+ executor executor_ { L"stage " + boost::lexical_cast<std::wstring>(channel_index_) };
+public:
+ impl(int channel_index, spl::shared_ptr<diagnostics::graph> graph)
+ : channel_index_(channel_index)
+ , graph_(std::move(graph))
+ , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
+ {
+ graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
+ }
+
+ std::map<int, draw_frame> operator()(const video_format_desc& format_desc)
+ {
+ caspar::timer frame_timer;
+
+ auto frames = executor_.invoke([=]() -> std::map<int, draw_frame>
+ {
+
+ std::map<int, draw_frame> frames;
+
+ try
+ {
+ std::vector<int> indices;
+
+ for (auto& layer : layers_)
+ {
+ // Prevent race conditions in parallel for each later
+ frames[layer.first] = draw_frame::empty();
+ tweens_[layer.first];
+ layer_consumers_[layer.first];
+
+ indices.push_back(layer.first);
+ }
+
+ aggregator_.translate_and_send();
+
+ tbb::parallel_for_each(indices.begin(), indices.end(), [&](int index)
+ {
+ draw(index, format_desc, frames);
+ });
+ }
+ catch(...)
+ {
+ layers_.clear();
+ CASPAR_LOG_CURRENT_EXCEPTION();
+ }
+
+
+ return frames;
+ });
+
+ //frames_subject_ << frames;
+
+ graph_->set_value("produce-time", frame_timer.elapsed()*format_desc.fps*0.5);
+ *monitor_subject_ << monitor::message("/profiler/time") % frame_timer.elapsed() % (1.0/format_desc.fps);
+
+ return frames;
+ }
+
+ void draw(int index, const video_format_desc& format_desc, std::map<int, draw_frame>& frames)
+ {
+ auto& layer = layers_[index];
+ auto& tween = tweens_[index];
+ auto& consumers = layer_consumers_[index];
+
+ auto frame = layer.receive(format_desc);
+
+ if (!consumers.empty())
+ {
+ auto consumer_it = consumers | boost::adaptors::map_values;
+ tbb::parallel_for_each(consumer_it.begin(), consumer_it.end(), [&](decltype(*consumer_it.begin()) layer_consumer)
+ {
+ layer_consumer->send(frame);
+ });
+ }
+
+ auto frame1 = frame;
+
+ frame1.transform() *= tween.fetch_and_tick(1);
+
+ if(format_desc.field_mode != core::field_mode::progressive)
+ {
+ auto frame2 = frame;
+ frame2.transform() *= tween.fetch_and_tick(1);
+ frame2.transform().audio_transform.volume = 0.0;
+ frame1 = core::draw_frame::interlace(frame1, frame2, format_desc.field_mode);
+ }
+
+ frames[index] = frame1;
+ }
+
+ layer& get_layer(int index)
+ {
+ auto it = layers_.find(index);
+ if(it == std::end(layers_))
+ {
+ it = layers_.insert(std::make_pair(index, layer(index))).first;
+ it->second.monitor_output().attach_parent(monitor_subject_);
+ }
+ return it->second;
+ }
+
+ std::future<void> apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)
+ {
+ return executor_.begin_invoke([=]
+ {
+ for (auto& transform : transforms)
+ {
+ auto& tween = tweens_[std::get<0>(transform)];
+ auto src = tween.fetch();
+ auto dst = std::get<1>(transform)(tween.dest());
+ tweens_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));
+ }
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)
+ {
+ return executor_.begin_invoke([=]
+ {
+ auto src = tweens_[index].fetch();
+ auto dst = transform(src);
+ tweens_[index] = tweened_transform(src, dst, mix_duration, tween);
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> clear_transforms(int index)
+ {
+ return executor_.begin_invoke([=]
+ {
+ tweens_.erase(index);
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> clear_transforms()
+ {
+ return executor_.begin_invoke([=]
+ {
+ tweens_.clear();
+ }, task_priority::high_priority);
+ }
+
+ std::future<frame_transform> get_current_transform(int index)
+ {
+ return executor_.begin_invoke([=]
+ {
+ return tweens_[index].fetch();
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
+ {
+ return executor_.begin_invoke([=]
+ {
+ get_layer(index).load(producer, preview, auto_play_delta);
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> pause(int index)
+ {
+ return executor_.begin_invoke([=]
+ {
+ get_layer(index).pause();
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> resume(int index)
+ {
+ return executor_.begin_invoke([=]
+ {
+ get_layer(index).resume();
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> play(int index)
+ {
+ return executor_.begin_invoke([=]
+ {
+ get_layer(index).play();
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> stop(int index)
+ {
+ return executor_.begin_invoke([=]
+ {
+ get_layer(index).stop();
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> clear(int index)
+ {
+ return executor_.begin_invoke([=]
+ {
+ layers_.erase(index);
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> clear()
+ {
+ return executor_.begin_invoke([=]
+ {
+ layers_.clear();
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> swap_layers(stage& other, bool swap_transforms)
+ {
+ auto other_impl = other.impl_;
+
+ if (other_impl.get() == this)
+ {
+ return make_ready_future();
+ }
+
+ auto func = [=]
+ {
+ auto layers = layers_ | boost::adaptors::map_values;
+ auto other_layers = other_impl->layers_ | boost::adaptors::map_values;
+
+ for (auto& layer : layers)
+ layer.monitor_output().detach_parent();
+
+ for (auto& layer : other_layers)
+ layer.monitor_output().detach_parent();
+
+ std::swap(layers_, other_impl->layers_);
+
+ for (auto& layer : layers)
+ layer.monitor_output().attach_parent(monitor_subject_);
+
+ for (auto& layer : other_layers)
+ layer.monitor_output().attach_parent(monitor_subject_);
+
+ if (swap_transforms)
+ std::swap(tweens_, other_impl->tweens_);
+ };
+
+ return executor_.begin_invoke([=]
+ {
+ other_impl->executor_.invoke(func, task_priority::high_priority);
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> swap_layer(int index, int other_index, bool swap_transforms)
+ {
+ return executor_.begin_invoke([=]
+ {
+ std::swap(get_layer(index), get_layer(other_index));
+
+ if (swap_transforms)
+ std::swap(tweens_[index], tweens_[other_index]);
+ }, task_priority::high_priority);
+ }
+
+ std::future<void> swap_layer(int index, int other_index, stage& other, bool swap_transforms)
+ {
+ auto other_impl = other.impl_;
+
+ if(other_impl.get() == this)
+ return swap_layer(index, other_index, swap_transforms);
+ else
+ {
+ auto func = [=]
+ {
+ auto& my_layer = get_layer(index);
+ auto& other_layer = other_impl->get_layer(other_index);
+
+ my_layer.monitor_output().detach_parent();
+ other_layer.monitor_output().detach_parent();
+
+ std::swap(my_layer, other_layer);
+
+ my_layer.monitor_output().attach_parent(monitor_subject_);
+ other_layer.monitor_output().attach_parent(other_impl->monitor_subject_);
+
+ if (swap_transforms)
+ {
+ auto& my_tween = tweens_[index];
+ auto& other_tween = other_impl->tweens_[other_index];
+ std::swap(my_tween, other_tween);
+ }
+ };
+
+ return executor_.begin_invoke([=]
+ {
+ other_impl->executor_.invoke(func, task_priority::high_priority);
+ }, task_priority::high_priority);
+ }
+ }
+
+ void add_layer_consumer(void* token, int layer, const spl::shared_ptr<write_frame_consumer>& layer_consumer)
+ {
+ executor_.begin_invoke([=]
+ {
+ layer_consumers_[layer].insert(std::make_pair(token, layer_consumer));
+ }, task_priority::high_priority);
+ }
+
+ void remove_layer_consumer(void* token, int layer)
+ {
+ executor_.begin_invoke([=]
+ {
+ auto& layer_map = layer_consumers_[layer];
+ layer_map.erase(token);
+ if (layer_map.empty())
+ {
+ layer_consumers_.erase(layer);
+ }
+ }, task_priority::high_priority);
+ }
+
+ std::future<std::shared_ptr<frame_producer>> foreground(int index)
+ {
+ return executor_.begin_invoke([=]() -> std::shared_ptr<frame_producer>
+ {
+ return get_layer(index).foreground();
+ }, task_priority::high_priority);
+ }
+
+ std::future<std::shared_ptr<frame_producer>> background(int index)
+ {
+ return executor_.begin_invoke([=]() -> std::shared_ptr<frame_producer>
+ {
+ return get_layer(index).background();
+ }, task_priority::high_priority);
+ }
+
+ std::future<boost::property_tree::wptree> info()
+ {
+ return executor_.begin_invoke([this]() -> boost::property_tree::wptree
+ {
+ boost::property_tree::wptree info;
+ for (auto& layer : layers_)
+ info.add_child(L"layers.layer", layer.second.info())
+ .add(L"index", layer.first);
+ return info;
+ }, task_priority::high_priority);
+ }
+
+ std::future<boost::property_tree::wptree> info(int index)
+ {
+ return executor_.begin_invoke([=]
+ {
+ return get_layer(index).info();
+ }, task_priority::high_priority);
+ }
+
+ std::future<boost::property_tree::wptree> delay_info()
+ {
+ return std::move(executor_.begin_invoke([this]() -> boost::property_tree::wptree
+ {
+ boost::property_tree::wptree info;
+
+ for (auto& layer : layers_)
+ info.add_child(L"layer", layer.second.delay_info()).add(L"index", layer.first);
+
+ return info;
+ }, task_priority::high_priority));
+ }
+
+ std::future<boost::property_tree::wptree> delay_info(int index)
+ {
+ return std::move(executor_.begin_invoke([=]() -> boost::property_tree::wptree
+ {
+ return get_layer(index).delay_info();
+ }, task_priority::high_priority));
+ }
+
+ std::future<std::wstring> call(int index, const std::vector<std::wstring>& params)
+ {
+ return flatten(executor_.begin_invoke([=]
+ {
+ return get_layer(index).foreground()->call(params).share();
+ }, task_priority::high_priority));
+ }
+
+ void on_interaction(const interaction_event::ptr& event)
+ {
+ executor_.begin_invoke([=]
+ {
+ aggregator_.offer(event);
+ }, task_priority::high_priority);
+ }
+
+ boost::optional<interaction_target> collission_detect(double x, double y)
+ {
+ for (auto& layer : layers_ | boost::adaptors::reversed)
+ {
+ auto transform = tweens_[layer.first].fetch();
+ auto translated = translate(x, y, transform);
+
+ if (translated.first >= 0.0
+ && translated.first <= 1.0
+ && translated.second >= 0.0
+ && translated.second <= 1.0
+ && layer.second.collides(translated.first, translated.second))
+ {
+ return std::make_pair(transform, static_cast<interaction_sink*>(&layer.second));
+ }
+ }
+
+ return boost::optional<interaction_target>();
+ }
+};
+
+stage::stage(int channel_index, spl::shared_ptr<diagnostics::graph> graph) : impl_(new impl(channel_index, std::move(graph))){}
+std::future<std::wstring> stage::call(int index, const std::vector<std::wstring>& params){return impl_->call(index, params);}
+std::future<void> stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){ return impl_->apply_transforms(transforms); }
+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); }
+std::future<void> stage::clear_transforms(int index){ return impl_->clear_transforms(index); }
+std::future<void> stage::clear_transforms(){ return impl_->clear_transforms(); }
+std::future<frame_transform> stage::get_current_transform(int index){ return impl_->get_current_transform(index); }
+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); }
+std::future<void> stage::pause(int index){ return impl_->pause(index); }
+std::future<void> stage::resume(int index){ return impl_->resume(index); }
+std::future<void> stage::play(int index){ return impl_->play(index); }
+std::future<void> stage::stop(int index){ return impl_->stop(index); }
+std::future<void> stage::clear(int index){ return impl_->clear(index); }
+std::future<void> stage::clear(){ return impl_->clear(); }
+std::future<void> stage::swap_layers(stage& other, bool swap_transforms){ return impl_->swap_layers(other, swap_transforms); }
+std::future<void> stage::swap_layer(int index, int other_index, bool swap_transforms){ return impl_->swap_layer(index, other_index, swap_transforms); }
+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); }
+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); }
+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); }
+std::future<std::shared_ptr<frame_producer>> stage::background(int index) { return impl_->background(index); }
+std::future<boost::property_tree::wptree> stage::info() const{ return impl_->info(); }
+std::future<boost::property_tree::wptree> stage::info(int index) const{ return impl_->info(index); }
+std::future<boost::property_tree::wptree> stage::delay_info() const{ return impl_->delay_info(); }
+std::future<boost::property_tree::wptree> stage::delay_info(int index) const{ return impl_->delay_info(index); }
+std::map<int, draw_frame> stage::operator()(const video_format_desc& format_desc){ return (*impl_)(format_desc); }
+monitor::subject& stage::monitor_output(){return *impl_->monitor_subject_;}
+void stage::on_interaction(const interaction_event::ptr& event) { impl_->on_interaction(event); }
+}}