\r
#include "stage.h"\r
\r
-#include "../video_channel_context.h"\r
-\r
#include "layer.h"\r
\r
-#include <core/producer/frame/basic_frame.h>\r
-#include <core/producer/frame/frame_factory.h>\r
+#include "frame/basic_frame.h"\r
+#include "frame/frame_factory.h"\r
\r
#include <common/concurrency/executor.h>\r
\r
-#include <boost/range/algorithm.hpp>\r
+#include <boost/foreach.hpp>\r
+#include <boost/timer.hpp>\r
\r
-#include <tbb/parallel_for.h>\r
+#include <tbb/parallel_for_each.h>\r
\r
#include <map>\r
\r
namespace caspar { namespace core {\r
- \r
-void destroy_producer(safe_ptr<frame_producer>& producer)\r
-{\r
- if(!producer.unique())\r
- CASPAR_LOG(warning) << producer->print() << L" Not destroyed on safe asynchronous destruction thread.";\r
- \r
- producer = frame_producer::empty();\r
-}\r
\r
-class destroy_producer_proxy : public frame_producer\r
-{\r
- safe_ptr<frame_producer> producer_;\r
- executor& destroy_context_;\r
-public:\r
- destroy_producer_proxy(executor& destroy_context, const safe_ptr<frame_producer>& producer) \r
- : producer_(producer)\r
- , destroy_context_(destroy_context){}\r
-\r
- ~destroy_producer_proxy()\r
- { \r
- if(destroy_context_.size() > 4)\r
- CASPAR_LOG(error) << L" Potential destroyer deadlock.";\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
- destroy_context_.begin_invoke(std::bind(&destroy_producer, std::move(producer_)));\r
- }\r
+ boost::timer produce_timer_;\r
+ boost::timer tick_timer_;\r
\r
- virtual safe_ptr<basic_frame> receive() {return core::receive(producer_);}\r
- virtual std::wstring print() const {return producer_->print();}\r
- virtual void param(const std::wstring& str) {producer_->param(str);}\r
- virtual safe_ptr<frame_producer> get_following_producer() const {return producer_->get_following_producer();}\r
- virtual void set_leading_producer(const safe_ptr<frame_producer>& producer) {producer_->set_leading_producer(producer);}\r
-};\r
+ std::map<int, layer> layers_; \r
\r
-struct stage::implementation : boost::noncopyable\r
-{ \r
- std::map<int, layer> layers_; \r
- typedef std::map<int, layer>::value_type layer_t;\r
- \r
- video_channel_context& channel_;\r
+ executor executor_;\r
public:\r
- implementation(video_channel_context& video_channel) \r
- : channel_(video_channel)\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
- std::map<int, safe_ptr<basic_frame>> execute()\r
- { \r
- std::map<int, safe_ptr<basic_frame>> frames;\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
- // Allocate placeholders.\r
- BOOST_FOREACH(auto layer, layers_)\r
- frames[layer.first] = basic_frame::empty();\r
+ produce_timer_.restart();\r
\r
- // Render layers\r
- tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](layer_t& layer)\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
+ frames[layer.first] = layer.second.receive(); \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
- frames[layer.first] = layer.second.receive();\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
- return frames;\r
+ } \r
}\r
\r
- void load(int index, const safe_ptr<frame_producer>& producer, bool preview)\r
+ void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
{\r
- channel_.execution().invoke([&]\r
+ executor_.begin_invoke([=]\r
{\r
- layers_[index].load(make_safe<destroy_producer_proxy>(channel_.destruction(), producer), preview);\r
- });\r
+ layers_[index].load(producer, preview, auto_play_delta);\r
+ }, high_priority);\r
}\r
\r
void pause(int index)\r
{ \r
- channel_.execution().invoke([&]\r
+ executor_.begin_invoke([=]\r
{\r
layers_[index].pause();\r
- });\r
+ }, high_priority);\r
}\r
\r
void play(int index)\r
{ \r
- channel_.execution().invoke([&]\r
+ executor_.begin_invoke([=]\r
{\r
layers_[index].play();\r
- });\r
+ }, high_priority);\r
}\r
\r
void stop(int index)\r
{ \r
- channel_.execution().invoke([&]\r
+ executor_.begin_invoke([=]\r
{\r
layers_[index].stop();\r
- });\r
+ }, high_priority);\r
}\r
\r
void clear(int index)\r
{\r
- channel_.execution().invoke([&]\r
+ executor_.begin_invoke([=]\r
{\r
layers_.erase(index);\r
- });\r
+ }, high_priority);\r
}\r
\r
void clear()\r
{\r
- channel_.execution().invoke([&]\r
+ executor_.begin_invoke([=]\r
{\r
layers_.clear();\r
- });\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
- channel_.execution().invoke([&]\r
+ executor_.begin_invoke([=]\r
{\r
- layers_[index].swap(layers_[other_index]);\r
- });\r
+ std::swap(layers_[index], layers_[other_index]);\r
+ }, high_priority);\r
}\r
\r
- void swap_layer(int index, size_t other_index, stage& other)\r
+ void swap_layer(int index, size_t other_index, const safe_ptr<stage>& other)\r
{\r
- if(other.impl_.get() == this)\r
+ if(other->impl_.get() == this)\r
swap_layer(index, other_index);\r
else\r
{\r
- if(channel_.get_format_desc() != other.impl_->channel_.get_format_desc())\r
- BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between channels with different formats."));\r
-\r
- auto func = [&]{layers_[index].swap(other.impl_->layers_[other_index]);};\r
- \r
- channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func);});\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
- void swap(stage& other)\r
- {\r
- if(other.impl_.get() == this)\r
- return;\r
-\r
- if(channel_.get_format_desc() != other.impl_->channel_.get_format_desc())\r
- BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between channels with different formats."));\r
-\r
- auto func = [&]\r
+ boost::unique_future<layer_status> get_status(int index)\r
+ { \r
+ return executor_.begin_invoke([=]\r
{\r
- auto sel_first = [](const std::pair<int, layer>& pair){return pair.first;};\r
-\r
- std::set<int> indices;\r
- auto inserter = std::inserter(indices, indices.begin());\r
-\r
- std::transform(layers_.begin(), layers_.end(), inserter, sel_first);\r
- std::transform(other.impl_->layers_.begin(), other.impl_->layers_.end(), inserter, sel_first);\r
-\r
- BOOST_FOREACH(auto index, indices)\r
- layers_[index].swap(other.impl_->layers_[index]);\r
- };\r
- \r
- channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func);});\r
+ return layers_[index].status();\r
+ }, high_priority );\r
}\r
\r
boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
{\r
- return channel_.execution().begin_invoke([=]{return layers_[index].foreground();});\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 channel_.execution().begin_invoke([=]{return layers_[index].background();});\r
+ return executor_.begin_invoke([=]\r
+ {\r
+ return layers_[index].background();\r
+ }, high_priority);\r
}\r
-\r
- std::wstring print() const\r
+ \r
+ void set_video_format_desc(const video_format_desc& format_desc)\r
{\r
- return L"stage [" + boost::lexical_cast<std::wstring>(channel_.index()) + L"]";\r
+ executor_.begin_invoke([=]\r
+ {\r
+ format_desc_ = format_desc;\r
+ }, high_priority );\r
}\r
-\r
};\r
\r
-stage::stage(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}\r
-void stage::swap(stage& other){impl_->swap(other);}\r
-void stage::load(int index, const safe_ptr<frame_producer>& producer, bool preview){impl_->load(index, producer, preview);}\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::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, stage& other){impl_->swap_layer(index, other_index, other);}\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<layer_status> stage::get_status(int index){return impl_->get_status(index);}\r
boost::unique_future<safe_ptr<frame_producer>> stage::foreground(size_t index) {return impl_->foreground(index);}\r
boost::unique_future<safe_ptr<frame_producer>> stage::background(size_t index) {return impl_->background(index);}\r
-std::map<int, safe_ptr<basic_frame>> stage::execute(){return impl_->execute();}\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
}}
\ No newline at end of file