]> git.sesse.net Git - casparcg/blobdiff - core/consumer/output.cpp
2.1.0: Seal of appropriate classes.
[casparcg] / core / consumer / output.cpp
index 95bcbc887aea1c13eee1c8aa48a52bf3932d62b2..504898e927abe5ec075622bce8a788dfd31860f7 100644 (file)
@@ -1,23 +1,24 @@
 /*\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
 #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 <concrt_extras.h>\r
-\r
-using namespace Concurrency;\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 video_format_desc format_desc_;\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
-       critical_section                                                                mutex_;\r
-       call<safe_ptr<message<safe_ptr<read_frame>>>>   output_;\r
+       executor                                                                                executor_;\r
                \r
 public:\r
-       implementation(output::source_t& source, const video_format_desc& format_desc) \r
-               : format_desc_(format_desc)\r
-               , output_(std::bind(&implementation::execute, this, std::placeholders::_1))\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
-               source.link_target(&output_);\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
                remove(index);\r
 \r
-               consumer->initialize(format_desc_);             \r
-               \r
-               {\r
-                       critical_section::scoped_lock lock(mutex_);\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
+                       CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
+               }, high_priority);\r
+       }\r
 \r
-                       CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added."; \r
-               }\r
+       void add(const safe_ptr<frame_consumer>& consumer)\r
+       {\r
+               add(consumer->index(), consumer);\r
        }\r
 \r
        void remove(int index)\r
-       {\r
-               {\r
-                       critical_section::scoped_lock lock(mutex_);\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
-       }\r
-                                               \r
-       void execute(const safe_ptr<message<safe_ptr<read_frame>>>& msg)\r
-       {       \r
-               auto frame = msg->value();\r
+               }, high_priority);\r
 \r
+               if(old_consumer)\r
                {\r
-                       critical_section::scoped_lock lock(mutex_);             \r
+                       auto str = old_consumer->print();\r
+                       old_consumer.reset();\r
+                       CASPAR_LOG(info) << print() << L" " << str << L" Removed.";\r
+               }\r
+       }\r
 \r
-                       if(!has_synchronization_clock() || frame->image_size() != format_desc_.size)\r
-                       {               \r
-                               scoped_oversubcription_token oversubscribe;\r
-                               timer_.tick(1.0/format_desc_.fps);\r
-                       }\r
+       void remove(const safe_ptr<frame_consumer>& consumer)\r
+       {\r
+               remove(consumer->index());\r
+       }\r
        \r
-                       std::vector<int> removables;            \r
-                       Concurrency::parallel_for_each(consumers_.begin(), consumers_.end(), [&](const decltype(*consumers_.begin())& pair)\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
                                try\r
                                {\r
-                                       if(!pair.second->send(frame))\r
-                                               removables.push_back(pair.first);\r
+                                       it->second->initialize(format_desc_, channel_index_);\r
+                                       ++it;\r
                                }\r
                                catch(...)\r
-                               {               \r
+                               {\r
                                        CASPAR_LOG_CURRENT_EXCEPTION();\r
-                                       CASPAR_LOG(error) << "Consumer error. Trying to recover:" << pair.second->print();\r
+                                       CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
+                                       consumers_.erase(it++);\r
+                               }\r
+                       }\r
+                       \r
+                       format_desc_ = format_desc;\r
+                       frames_.clear();\r
+               });\r
+       }\r
+\r
+       std::pair<int, int> 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 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
+                       try\r
+                       {\r
+                               consume_timer_.restart();\r
+\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_data().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
-                                               pair.second->initialize(format_desc_);\r
-                                               pair.second->send(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
-                                               removables.push_back(pair.first);                               \r
                                                CASPAR_LOG_CURRENT_EXCEPTION();\r
-                                               CASPAR_LOG(error) << "Failed to recover consumer: " << pair.second->print() << L". Removing it.";\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
-\r
-                       BOOST_FOREACH(auto& removable, removables)\r
-                               consumers_.erase(removable);            \r
-               }\r
+                                               \r
+                               graph_->update_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
-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(output::source_t& source, const video_format_desc& format_desc) : impl_(new implementation(source, format_desc)){}\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::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