/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
*\r
-* This file is part of CasparCG.\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
+* 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
+* 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
#ifdef _MSC_VER\r
\r
#include "output.h"\r
\r
-#include "../video_channel_context.h"\r
-\r
#include "../video_format.h"\r
#include "../mixer/gpu/ogl_device.h"\r
#include "../mixer/read_frame.h"\r
#include <common/utility/assert.h>\r
#include <common/utility/timer.h>\r
#include <common/memory/memshfl.h>\r
+#include <common/env.h>\r
+\r
+#include <boost/circular_buffer.hpp>\r
+#include <boost/timer.hpp>\r
+#include <boost/range/algorithm.hpp>\r
+#include <boost/range/adaptors.hpp>\r
+#include <boost/property_tree/ptree.hpp>\r
\r
namespace caspar { namespace core {\r
\r
struct output::implementation\r
-{ \r
- typedef std::pair<safe_ptr<const read_frame>, safe_ptr<const read_frame>> fill_and_key;\r
- \r
- video_channel_context& channel_;\r
+{ \r
+ const int channel_index_;\r
+ const safe_ptr<diagnostics::graph> graph_;\r
+ boost::timer consume_timer_;\r
\r
- std::map<int, safe_ptr<frame_consumer>> consumers_;\r
- typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;\r
+ video_format_desc format_desc_;\r
+\r
+ std::map<int, safe_ptr<frame_consumer>> consumers_;\r
\r
- high_prec_timer timer_;\r
+ high_prec_timer sync_timer_;\r
+\r
+ boost::circular_buffer<safe_ptr<read_frame>> frames_;\r
+\r
+ executor executor_;\r
\r
public:\r
- implementation(video_channel_context& video_channel) \r
- : channel_(video_channel){} \r
+ implementation(const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc, int channel_index) \r
+ : channel_index_(channel_index)\r
+ , graph_(graph)\r
+ , format_desc_(format_desc)\r
+ , executor_(L"output")\r
+ {\r
+ graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8));\r
+ } \r
\r
- void add(int index, safe_ptr<frame_consumer>&& consumer)\r
+ void add(int index, safe_ptr<frame_consumer> consumer)\r
{ \r
- consumer->initialize(channel_.get_format_desc());\r
- channel_.execution().invoke([&]\r
+ remove(index);\r
+\r
+ consumer = create_consumer_cadence_guard(consumer);\r
+ consumer->initialize(format_desc_, channel_index_);\r
+\r
+ executor_.invoke([&]\r
{\r
- consumers_.erase(index);\r
consumers_.insert(std::make_pair(index, consumer));\r
-\r
CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
- });\r
+ }, high_priority);\r
}\r
\r
- void remove(int index)\r
+ void add(const safe_ptr<frame_consumer>& consumer)\r
{\r
- channel_.execution().invoke([&]\r
+ add(consumer->index(), consumer);\r
+ }\r
+\r
+ void remove(int index)\r
+ { \r
+ // Destroy consumer on calling thread:\r
+ std::shared_ptr<frame_consumer> old_consumer;\r
+\r
+ executor_.invoke([&]\r
{\r
auto it = consumers_.find(index);\r
if(it != consumers_.end())\r
{\r
- CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
+ old_consumer = it->second;\r
consumers_.erase(it);\r
}\r
- });\r
+ }, high_priority);\r
+\r
+ if(old_consumer)\r
+ {\r
+ auto str = old_consumer->print();\r
+ old_consumer.reset();\r
+ CASPAR_LOG(info) << print() << L" " << str << L" Removed.";\r
+ }\r
}\r
- \r
- void execute(const safe_ptr<read_frame>& frame)\r
- { \r
- try\r
- { \r
- if(!has_synchronization_clock())\r
- timer_.tick(1.0/channel_.get_format_desc().fps);\r
- \r
- auto fill = frame;\r
- auto key = get_key_frame(frame);\r
\r
+ void remove(const safe_ptr<frame_consumer>& consumer)\r
+ {\r
+ remove(consumer->index());\r
+ }\r
+ \r
+ void set_video_format_desc(const video_format_desc& format_desc)\r
+ {\r
+ executor_.invoke([&]\r
+ {\r
auto it = consumers_.begin();\r
while(it != consumers_.end())\r
- {\r
+ { \r
try\r
{\r
- auto consumer = it->second;\r
-\r
- if(consumer->get_video_format_desc() != channel_.get_format_desc())\r
- consumer->initialize(channel_.get_format_desc());\r
-\r
- auto frame = consumer->key_only() ? key : fill;\r
-\r
- if(static_cast<size_t>(frame->image_data().size()) == consumer->get_video_format_desc().size)\r
- consumer->send(frame);\r
-\r
+ it->second->initialize(format_desc_, channel_index_);\r
++it;\r
}\r
catch(...)\r
{\r
CASPAR_LOG_CURRENT_EXCEPTION();\r
- CASPAR_LOG(error) << print() << L" " << it->second->print() << L" Removed.";\r
+ CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
consumers_.erase(it++);\r
}\r
}\r
- }\r
- catch(...)\r
- {\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- }\r
+ \r
+ format_desc_ = format_desc;\r
+ frames_.clear();\r
+ });\r
}\r
\r
-private:\r
- \r
- bool has_synchronization_clock()\r
+ std::pair<size_t, size_t> minmax_buffer_depth() const\r
+ { \r
+ if(consumers_.empty())\r
+ return std::make_pair(0, 0);\r
+ \r
+ auto buffer_depths = consumers_ | \r
+ boost::adaptors::map_values | // std::function is MSVC workaround\r
+ boost::adaptors::transformed(std::function<int(const safe_ptr<frame_consumer>&)>([](const safe_ptr<frame_consumer>& x){return x->buffer_depth();})); \r
+ \r
+\r
+ return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));\r
+ }\r
+\r
+ bool has_synchronization_clock() const\r
{\r
- return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
- {\r
- return p.second->has_synchronization_clock();\r
- });\r
+ return boost::range::count_if(consumers_ | boost::adaptors::map_values, [](const safe_ptr<frame_consumer>& x){return x->has_synchronization_clock();}) > 0;\r
}\r
\r
- safe_ptr<const read_frame> get_key_frame(const safe_ptr<const read_frame>& frame)\r
+ void send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& packet)\r
{\r
- bool has_key_only = std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
+ executor_.begin_invoke([=]\r
{\r
- return p.second->key_only();\r
- });\r
+ try\r
+ {\r
+ consume_timer_.restart();\r
\r
- if(has_key_only)\r
- {\r
- // Currently do key_only transform on cpu. Unsure if the extra 400MB/s (1080p50) overhead is worth it to do it on gpu.\r
- auto key_data = channel_.ogl().create_host_buffer(frame->image_data().size(), host_buffer::write_only); \r
- fast_memsfhl(key_data->data(), frame->image_data().begin(), frame->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);\r
- std::vector<int16_t> audio_data(frame->audio_data().begin(), frame->audio_data().end());\r
- return make_safe<read_frame>(std::move(key_data), std::move(audio_data));\r
- }\r
- \r
- return make_safe<read_frame>();\r
+ auto input_frame = packet.first;\r
+\r
+ if(!has_synchronization_clock())\r
+ sync_timer_.tick(1.0/format_desc_.fps);\r
+\r
+ if(input_frame->image_size() != format_desc_.size)\r
+ {\r
+ sync_timer_.tick(1.0/format_desc_.fps);\r
+ return;\r
+ }\r
+ \r
+ auto minmax = minmax_buffer_depth();\r
+\r
+ frames_.set_capacity(minmax.second - minmax.first + 1);\r
+ frames_.push_back(input_frame);\r
+\r
+ if(!frames_.full())\r
+ return;\r
+\r
+ auto it = consumers_.begin();\r
+ while(it != consumers_.end())\r
+ {\r
+ auto consumer = it->second;\r
+ auto frame = frames_.at(consumer->buffer_depth()-minmax.first);\r
+ \r
+ try\r
+ {\r
+ if(consumer->send(frame))\r
+ ++it;\r
+ else\r
+ {\r
+ CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
+ consumers_.erase(it++);\r
+ }\r
+ }\r
+ catch(...)\r
+ {\r
+ CASPAR_LOG_CURRENT_EXCEPTION();\r
+ try\r
+ {\r
+ consumer->initialize(format_desc_, channel_index_);\r
+ if(consumer->send(frame))\r
+ ++it;\r
+ else\r
+ {\r
+ CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
+ consumers_.erase(it++);\r
+ }\r
+ }\r
+ catch(...)\r
+ {\r
+ CASPAR_LOG_CURRENT_EXCEPTION();\r
+ CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";\r
+ consumers_.erase(it++);\r
+ }\r
+ }\r
+ }\r
+ \r
+ graph_->set_value("consume-time", consume_timer_.elapsed()*format_desc_.fps*0.5);\r
+ }\r
+ catch(...)\r
+ {\r
+ CASPAR_LOG_CURRENT_EXCEPTION();\r
+ }\r
+ });\r
}\r
- \r
+\r
std::wstring print() const\r
{\r
- return L"output";\r
+ return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";\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& consumer, consumers_)\r
+ {\r
+ info.add_child(L"consumers.consumer", consumer.second->info())\r
+ .add(L"index", consumer.first); \r
+ }\r
+ return info;\r
+ }, high_priority));\r
+ }\r
+\r
+ bool empty()\r
+ {\r
+ return executor_.invoke([this]\r
+ {\r
+ return consumers_.empty();\r
+ });\r
}\r
};\r
\r
-output::output(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}\r
-void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}\r
+output::output(const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc, int channel_index) : impl_(new implementation(graph, format_desc, channel_index)){}\r
+void output::add(int index, const safe_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}\r
+void output::add(const safe_ptr<frame_consumer>& consumer){impl_->add(consumer);}\r
void output::remove(int index){impl_->remove(index);}\r
-void output::execute(const safe_ptr<read_frame>& frame) {impl_->execute(frame); }\r
+void output::remove(const safe_ptr<frame_consumer>& consumer){impl_->remove(consumer);}\r
+void output::send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& frame) {impl_->send(frame); }\r
+void output::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> output::info() const{return impl_->info();}\r
+bool output::empty() const{return impl_->empty();}\r
}}
\ No newline at end of file