/*\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
-// TODO: Try to recover consumer from bad_alloc...\r
+\r
#include "../StdAfx.h"\r
\r
#ifdef _MSC_VER\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<read_frame>, safe_ptr<read_frame>> fill_and_key;\r
\r
- const int channel_index_;\r
- safe_ptr<diagnostics::graph> graph_;\r
- boost::timer consume_timer_;\r
+struct output::implementation\r
+{ \r
+ const int channel_index_;\r
+ const safe_ptr<diagnostics::graph> graph_;\r
+ boost::timer consume_timer_;\r
\r
- video_format_desc format_desc_;\r
+ video_format_desc format_desc_;\r
\r
- std::map<int, safe_ptr<frame_consumer>> consumers_;\r
- typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;\r
+ std::map<int, safe_ptr<frame_consumer>> consumers_;\r
\r
- high_prec_timer timer_;\r
-\r
- boost::circular_buffer<safe_ptr<read_frame>> frames_;\r
+ high_prec_timer sync_timer_;\r
\r
- tbb::concurrent_bounded_queue<std::shared_ptr<read_frame>> input_;\r
+ boost::circular_buffer<safe_ptr<read_frame>> frames_;\r
\r
- executor executor_;\r
+ executor executor_;\r
\r
public:\r
implementation(const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc, int channel_index) \r
graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f));\r
} \r
\r
- void add(int index, safe_ptr<frame_consumer>&& consumer)\r
+ void add(int index, safe_ptr<frame_consumer> consumer)\r
{ \r
- executor_.invoke([&]\r
- {\r
- consumers_.erase(index);\r
- });\r
+ remove(index);\r
\r
- consumer->initialize(format_desc_, channel_index_, index);\r
+ consumer = create_consumer_cadence_guard(consumer);\r
+ consumer->initialize(format_desc_, channel_index_);\r
\r
executor_.invoke([&]\r
{\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
+ 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 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
- format_desc_ = format_desc;\r
-\r
auto it = consumers_.begin();\r
while(it != consumers_.end())\r
{ \r
try\r
{\r
- it->second->initialize(format_desc_, channel_index_, it->first);\r
+ it->second->initialize(format_desc_, channel_index_);\r
++it;\r
}\r
catch(...)\r
{\r
CASPAR_LOG_CURRENT_EXCEPTION();\r
+ CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
consumers_.erase(it++);\r
}\r
}\r
-\r
+ \r
+ format_desc_ = format_desc;\r
frames_.clear();\r
});\r
}\r
- \r
- std::pair<size_t, size_t> minmax_buffer_depth() const\r
+\r
+ std::pair<int, int> minmax_buffer_depth() const\r
{ \r
if(consumers_.empty())\r
return std::make_pair(0, 0);\r
- std::vector<size_t> buffer_depths;\r
- std::transform(consumers_.begin(), consumers_.end(), std::back_inserter(buffer_depths), [](const decltype(*consumers_.begin())& pair)\r
- {\r
- return pair.second->buffer_depth();\r
- });\r
- std::sort(buffer_depths.begin(), buffer_depths.end());\r
- auto min = buffer_depths.front();\r
- auto max = buffer_depths.back();\r
- return std::make_pair(min, max);\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
- void send(const std::pair<safe_ptr<read_frame>, ticket>& packet)\r
+ bool has_synchronization_clock() const\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
+ void send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& packet)\r
{\r
executor_.begin_invoke([=]\r
{\r
auto input_frame = packet.first;\r
\r
if(!has_synchronization_clock())\r
- timer_.tick(1.0/format_desc_.fps);\r
+ sync_timer_.tick(1.0/format_desc_.fps);\r
\r
- if(input_frame->image_size() != format_desc_.size)\r
+ if(input_frame->image_data().size() != format_desc_.size)\r
{\r
- timer_.tick(1.0/format_desc_.fps);\r
+ sync_timer_.tick(1.0/format_desc_.fps);\r
return;\r
}\r
\r
- const auto minmax = minmax_buffer_depth();\r
+ auto minmax = minmax_buffer_depth();\r
\r
frames_.set_capacity(minmax.second - minmax.first + 1);\r
frames_.push_back(input_frame);\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_, it->first);\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
});\r
}\r
\r
-private:\r
- \r
- bool has_synchronization_clock()\r
+ std::wstring print() 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 L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";\r
}\r
- \r
- std::wstring print() const\r
+\r
+ boost::unique_future<boost::property_tree::wptree> info()\r
{\r
- return L"output";\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
\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, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}\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::send(const std::pair<safe_ptr<read_frame>, ticket>& frame) {impl_->send(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
}}
\ No newline at end of file