+/*\r
+* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+*\r
+* This file is part of CasparCG.\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
+*/\r
+\r
#include "../StdAfx.h"\r
\r
#include "frame_producer_device.h"\r
\r
-#include "../mixer/frame/draw_frame.h"\r
-#include "../mixer/frame_factory.h"\r
-\r
#include "layer.h"\r
\r
+#include <core/producer/frame/basic_frame.h>\r
+#include <core/producer/frame/frame_factory.h>\r
+\r
+#include <common/diagnostics/graph.h>\r
#include <common/concurrency/executor.h>\r
\r
-#include <boost/range/algorithm_ext/erase.hpp>\r
+#include <boost/timer.hpp>\r
\r
#include <tbb/parallel_for.h>\r
\r
#include <map>\r
-#include <memory>\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
+\r
+ destroy_context_.begin_invoke(std::bind(&destroy_producer, std::move(producer_)));\r
+ }\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
\r
struct frame_producer_device::implementation : boost::noncopyable\r
{ \r
- std::map<int, layer> layers_; \r
-\r
- const video_format_desc format_desc_; \r
- const output_func output_;\r
- const safe_ptr<frame_factory> factory_;\r
+ std::map<int, layer> layers_; \r
+ const video_format_desc format_desc_; \r
+ const output_t output_;\r
\r
- mutable executor executor_;\r
-\r
+ safe_ptr<diagnostics::graph> diag_;\r
+ boost::timer frame_timer_;\r
+ boost::timer tick_timer_;\r
+ boost::timer output_timer_;\r
+ \r
+ executor& context_;\r
+ executor& destroy_context_;\r
public:\r
- implementation(const video_format_desc& format_desc, const safe_ptr<frame_factory>& factory, const output_func& output) \r
+ implementation(executor& context, executor& destroy_context, const video_format_desc& format_desc, const output_t& output) \r
: format_desc_(format_desc)\r
- , factory_(factory)\r
+ , diag_(diagnostics::create_graph(std::string("frame_producer_device")))\r
+ , context_(context)\r
+ , destroy_context_(destroy_context)\r
, output_(output)\r
{\r
- executor_.start();\r
- executor_.begin_invoke([=]{tick();});\r
- }\r
+ diag_->add_guide("frame-time", 0.5f); \r
+ diag_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));\r
+ diag_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));\r
+ diag_->set_color("output-time", diagnostics::color(0.5f, 1.0f, 0.2f));\r
\r
- ~implementation()\r
- {\r
- CASPAR_LOG(info) << "Shutting down producer-device.";\r
+ context_.begin_invoke([=]{tick();}); \r
}\r
- \r
+ \r
void tick()\r
- { \r
- output_(draw());\r
- executor_.begin_invoke([=]{tick();});\r
- }\r
- \r
- safe_ptr<draw_frame> draw()\r
- { \r
- std::vector<safe_ptr<draw_frame>> frames(layers_.size(), draw_frame::empty());\r
- tbb::parallel_for(tbb::blocked_range<size_t>(0, frames.size()), \r
- [&](const tbb::blocked_range<size_t>& r)\r
+ { \r
+ try\r
{\r
- auto it = layers_.begin();\r
- std::advance(it, r.begin());\r
- for(size_t i = r.begin(); i != r.end(); ++i, ++it)\r
- frames[i] = it->second.receive();\r
- }); \r
- boost::range::remove_erase(frames, draw_frame::eof());\r
- boost::range::remove_erase(frames, draw_frame::empty());\r
- return draw_frame(frames);\r
- }\r
-\r
- // Layers and Producers\r
- void set_video_gain(int index, double value)\r
- {\r
- begin_invoke_layer(index, std::bind(&layer::set_video_gain, std::placeholders::_1, value));\r
- }\r
+ auto frame = render();\r
+ output_timer_.restart();\r
+ output_(frame);\r
+ diag_->update_value("output-time", static_cast<float>(output_timer_.elapsed()*format_desc_.fps*0.5));\r
+ }\r
+ catch(...)\r
+ {\r
+ CASPAR_LOG_CURRENT_EXCEPTION();\r
+ }\r
\r
- void set_video_opacity(int index, double value)\r
- {\r
- begin_invoke_layer(index, std::bind(&layer::set_video_opacity, std::placeholders::_1, value));\r
+ context_.begin_invoke([=]{tick();});\r
}\r
+ \r
+ std::map<int, safe_ptr<basic_frame>> render()\r
+ { \r
+ frame_timer_.restart();\r
\r
- void set_audio_gain(int index, double value)\r
- {\r
- begin_invoke_layer(index, std::bind(&layer::set_audio_gain, std::placeholders::_1, value));\r
- }\r
+ std::map<int, safe_ptr<basic_frame>> frames;\r
+ BOOST_FOREACH(auto& layer, layers_)\r
+ frames[layer.first] = basic_frame::empty();\r
\r
- void load(int index, const safe_ptr<frame_producer>& producer, bool play_on_load)\r
- {\r
- producer->initialize(factory_);\r
- executor_.begin_invoke([=]\r
+ tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](decltype(*layers_.begin())& pair)\r
{\r
- auto it = layers_.insert(std::make_pair(index, layer(index))).first;\r
- it->second.load(producer, play_on_load);\r
+ frames[pair.first] = pair.second.receive();\r
});\r
+ \r
+ diag_->update_value("frame-time", frame_timer_.elapsed()*format_desc_.fps*0.5);\r
+\r
+ diag_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
+ tick_timer_.restart();\r
+\r
+ return frames;\r
}\r
- \r
- void preview(int index, const safe_ptr<frame_producer>& producer)\r
+\r
+ void load(int index, const safe_ptr<frame_producer>& producer, bool preview)\r
{\r
- producer->initialize(factory_);\r
- executor_.begin_invoke([=]\r
- {\r
- auto it = layers_.insert(std::make_pair(index, layer(index))).first;\r
- it->second.preview(producer);\r
- });\r
+ context_.invoke([&]{layers_[index].load(make_safe<destroy_producer_proxy>(destroy_context_, producer), preview);});\r
}\r
\r
void pause(int index)\r
{ \r
- begin_invoke_layer(index, std::mem_fn(&layer::pause));\r
+ context_.invoke([&]{layers_[index].pause();});\r
}\r
\r
void play(int index)\r
{ \r
- begin_invoke_layer(index, std::mem_fn(&layer::play));\r
+ context_.invoke([&]{layers_[index].play();});\r
}\r
\r
void stop(int index)\r
{ \r
- begin_invoke_layer(index, std::mem_fn(&layer::stop));\r
+ context_.invoke([&]{layers_[index].stop();});\r
}\r
\r
void clear(int index)\r
{\r
- executor_.begin_invoke([=]\r
- { \r
- auto it = layers_.find(index);\r
- if(it != layers_.end())\r
- {\r
- it->second.clear(); \r
- layers_.erase(it);\r
- }\r
- });\r
+ context_.invoke([&]{layers_.erase(index);});\r
}\r
\r
void clear()\r
{\r
- executor_.begin_invoke([=]\r
- { \r
- layers_.clear();\r
- });\r
- } \r
+ context_.invoke([&]{layers_.clear();});\r
+ } \r
+ \r
+ void swap_layer(int index, size_t other_index)\r
+ {\r
+ context_.invoke([&]{layers_[index].swap(layers_[other_index]);});\r
+ }\r
\r
- template<typename F>\r
- void begin_invoke_layer(int index, F&& func)\r
+ void swap_layer(int index, size_t other_index, frame_producer_device& other)\r
{\r
- executor_.begin_invoke([=]\r
+ if(other.impl_.get() == this)\r
+ swap_layer(index, other_index);\r
+ else\r
{\r
- auto it = layers_.find(index);\r
- if(it != layers_.end())\r
- func(it->second); \r
- });\r
+ if(format_desc_ != other.impl_->format_desc_)\r
+ BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot swap between channels with different formats."));\r
+\r
+ auto func = [&]{layers_[index].swap(other.impl_->layers_[other_index]);};\r
+ \r
+ context_.invoke([&]{other.impl_->context_.invoke(func);});\r
+ }\r
}\r
\r
- boost::unique_future<safe_ptr<frame_producer>> foreground(int index) const\r
+ void swap(frame_producer_device& other)\r
{\r
- return executor_.begin_invoke([=]() -> safe_ptr<frame_producer>\r
- { \r
- auto it = layers_.find(index);\r
- return it != layers_.end() ? it->second.foreground() : frame_producer::empty();\r
- });\r
+ if(other.impl_.get() == this)\r
+ return;\r
+\r
+ if(format_desc_ != other.impl_->format_desc_)\r
+ BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot swap between channels with different formats."));\r
+\r
+ auto func = [&]\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
+ std::for_each(indices.begin(), indices.end(), [&](int index)\r
+ {\r
+ layers_[index].swap(other.impl_->layers_[index]);\r
+ }); \r
+ };\r
+ \r
+ context_.invoke([&]{other.impl_->context_.invoke(func);});\r
}\r
\r
- boost::unique_future<safe_ptr<frame_producer>> background(int index) const\r
+ boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
{\r
- return executor_.begin_invoke([=]() -> safe_ptr<frame_producer>\r
- {\r
- auto it = layers_.find(index);\r
- return it != layers_.end() ? it->second.background() : frame_producer::empty();\r
- });\r
- };\r
+ return context_.begin_invoke([=]{return layers_[index].foreground();});\r
+ }\r
+ \r
+ boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
+ {\r
+ return context_.begin_invoke([=]{return layers_[index].background();});\r
+ }\r
};\r
\r
+frame_producer_device::frame_producer_device(executor& context, executor& destroy_context, const video_format_desc& format_desc, const output_t& output)\r
+ : impl_(new implementation(context, destroy_context, format_desc, output)){}\r
frame_producer_device::frame_producer_device(frame_producer_device&& other) : impl_(std::move(other.impl_)){}\r
-frame_producer_device::frame_producer_device(const video_format_desc& format_desc, const safe_ptr<frame_factory>& factory, const output_func& output) \r
- : impl_(new implementation(format_desc, factory, output)){}\r
-void frame_producer_device::set_video_gain(int index, double value){impl_->set_video_gain(index, value);}\r
-void frame_producer_device::set_video_opacity(int index, double value){impl_->set_video_opacity(index, value);}\r
-void frame_producer_device::set_audio_gain(int index, double value){impl_->set_audio_gain(index, value);}\r
-void frame_producer_device::load(int index, const safe_ptr<frame_producer>& producer, bool play_on_load){impl_->load(index, producer, play_on_load);}\r
-void frame_producer_device::preview(int index, const safe_ptr<frame_producer>& producer){impl_->preview(index, producer);}\r
+void frame_producer_device::swap(frame_producer_device& other){impl_->swap(other);}\r
+void frame_producer_device::load(int index, const safe_ptr<frame_producer>& producer, bool preview){impl_->load(index, producer, preview);}\r
void frame_producer_device::pause(int index){impl_->pause(index);}\r
void frame_producer_device::play(int index){impl_->play(index);}\r
void frame_producer_device::stop(int index){impl_->stop(index);}\r
void frame_producer_device::clear(int index){impl_->clear(index);}\r
void frame_producer_device::clear(){impl_->clear();}\r
-boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::foreground(int index) const{ return impl_->foreground(index);}\r
-boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::background(int index) const{return impl_->background(index);}\r
-const video_format_desc& frame_producer_device::get_video_format_desc() const{ return impl_->format_desc_;}\r
-\r
+void frame_producer_device::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
+void frame_producer_device::swap_layer(int index, size_t other_index, frame_producer_device& other){impl_->swap_layer(index, other_index, other);}\r
+boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::foreground(size_t index) {return impl_->foreground(index);}\r
+boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::background(size_t index) {return impl_->background(index);}\r
}}
\ No newline at end of file