]> git.sesse.net Git - casparcg/blobdiff - core/consumer/output.cpp
Created a consumer that provides sync to a channel based on the pace of another chann...
[casparcg] / core / consumer / output.cpp
index 59f279bcdb44cbe3ab86e78658953314e5eda1c7..d3c0580eb01bc624b18b396db5a8d9ec42a39206 100644 (file)
-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\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
-*\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
-#pragma warning (disable : 4244)\r
-#endif\r
-\r
-#include "output.h"\r
-\r
-#include "../video_format.h"\r
-#include "../mixer/gpu/ogl_device.h"\r
-#include "../mixer/read_frame.h"\r
-\r
-#include <common/concurrency/executor.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
-       const int                                                                               channel_index_;\r
-       const safe_ptr<diagnostics::graph>                              graph_;\r
-       boost::timer                                                                    consume_timer_;\r
-\r
-       video_format_desc                                                               format_desc_;\r
-\r
-       std::map<int, safe_ptr<frame_consumer>>                 consumers_;\r
-       \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(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_("output")\r
-       {\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
-       {               \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_.insert(std::make_pair(index, consumer));\r
-                       CASPAR_LOG(info) << print() << " " << consumer->print() << " Added.";\r
-               }, high_priority);\r
-       }\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
-               // 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
-                               old_consumer = it->second;\r
-                               consumers_.erase(it);\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() << " " << str << " 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
-                       auto it = consumers_.begin();\r
-                       while(it != consumers_.end())\r
-                       {                                               \r
-                               try\r
-                               {\r
-                                       it->second->initialize(format_desc_, channel_index_);\r
-                                       ++it;\r
-                               }\r
-                               catch(...)\r
-                               {\r
-                                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                                       CASPAR_LOG(info) << print() << " " << it->second->print() << " Removed.";\r
-                                       consumers_.erase(it++);\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
-               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_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() << " " << it->second->print() << " 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
-                                                               consumers_.erase(it++);\r
-                                               }\r
-                                               catch(...)\r
-                                               {\r
-                                                       CASPAR_LOG_CURRENT_EXCEPTION();\r
-                                                       CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << ". Removing it.";\r
-                                                       consumers_.erase(it++);\r
-                                               }\r
-                                       }\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
-       std::string print() const\r
-       {\r
-               return "output[" + boost::lexical_cast<std::string>(channel_index_) + "]";\r
-       }\r
-\r
-       boost::unique_future<boost::property_tree::ptree> info()\r
-       {\r
-               return std::move(executor_.begin_invoke([&]() -> boost::property_tree::ptree\r
-               {                       \r
-                       boost::property_tree::ptree info;\r
-                       BOOST_FOREACH(auto& consumer, consumers_)\r
-                       {\r
-                               info.add_child("consumers.consumer", consumer.second->info())\r
-                                       .add("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, 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::ptree> output::info() const{return impl_->info();}\r
-}}
\ No newline at end of file
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../StdAfx.h"
+
+#ifdef _MSC_VER
+#pragma warning (disable : 4244)
+#endif
+
+#include "output.h"
+
+#include "frame_consumer.h"
+#include "port.h"
+
+#include "../video_format.h"
+#include "../frame/frame.h"
+#include "../frame/audio_channel_layout.h"
+
+#include <common/assert.h>
+#include <common/future.h>
+#include <common/executor.h>
+#include <common/diagnostics/graph.h>
+#include <common/prec_timer.h>
+#include <common/memshfl.h>
+#include <common/env.h>
+#include <common/linq.h>
+#include <common/timer.h>
+
+#include <boost/circular_buffer.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/property_tree/ptree.hpp>
+
+#include <functional>
+
+namespace caspar { namespace core {
+
+struct output::impl
+{
+       spl::shared_ptr<diagnostics::graph>     graph_;
+       spl::shared_ptr<monitor::subject>       monitor_subject_                        = spl::make_shared<monitor::subject>("/output");
+       const int                                                       channel_index_;
+       video_format_desc                                       format_desc_;
+       audio_channel_layout                            channel_layout_;
+       std::map<int, port>                                     ports_;
+       prec_timer                                                      sync_timer_;
+       boost::circular_buffer<const_frame>     frames_;
+       std::map<int, int64_t>                          send_to_consumers_delays_;
+       executor                                                        executor_                                       { L"output " + boost::lexical_cast<std::wstring>(channel_index_) };
+public:
+       impl(spl::shared_ptr<diagnostics::graph> graph, const video_format_desc& format_desc, const audio_channel_layout& channel_layout, int channel_index)
+               : graph_(std::move(graph))
+               , channel_index_(channel_index)
+               , format_desc_(format_desc)
+               , channel_layout_(channel_layout)
+       {
+               graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8f));
+       }
+
+       void add(int index, spl::shared_ptr<frame_consumer> consumer)
+       {
+               remove(index);
+
+               consumer->initialize(format_desc_, channel_layout_, channel_index_);
+
+               executor_.begin_invoke([this, index, consumer]
+               {
+                       port p(index, channel_index_, std::move(consumer));
+                       p.monitor_output().attach_parent(monitor_subject_);
+                       ports_.insert(std::make_pair(index, std::move(p)));
+               }, task_priority::high_priority);
+       }
+
+       void add(const spl::shared_ptr<frame_consumer>& consumer)
+       {
+               add(consumer->index(), consumer);
+       }
+
+       void remove(int index)
+       {
+               executor_.begin_invoke([=]
+               {
+                       auto it = ports_.find(index);
+                       if (it != ports_.end())
+                       {
+                               ports_.erase(it);
+                               send_to_consumers_delays_.erase(index);
+                       }
+               }, task_priority::high_priority);
+       }
+
+       void remove(const spl::shared_ptr<frame_consumer>& consumer)
+       {
+               remove(consumer->index());
+       }
+
+       void change_channel_format(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout)
+       {
+               executor_.invoke([&]
+               {
+                       if(format_desc_ == format_desc && channel_layout_ == channel_layout)
+                               return;
+
+                       auto it = ports_.begin();
+                       while(it != ports_.end())
+                       {
+                               try
+                               {
+                                       it->second.change_channel_format(format_desc, channel_layout);
+                                       ++it;
+                               }
+                               catch(...)
+                               {
+                                       CASPAR_LOG_CURRENT_EXCEPTION();
+                                       send_to_consumers_delays_.erase(it->first);
+                                       ports_.erase(it++);
+                               }
+                       }
+
+                       format_desc_ = format_desc;
+                       channel_layout_ = channel_layout;
+                       frames_.clear();
+               });
+       }
+
+       std::pair<int, int> minmax_buffer_depth() const
+       {
+               if(ports_.empty())
+                       return std::make_pair(0, 0);
+
+               return cpplinq::from(ports_)
+                       .select(values())
+                       .select(std::mem_fn(&port::buffer_depth))
+                       .where([](int v) { return v >= 0; })
+                       .aggregate(minmax::initial_value<int>(), minmax());
+       }
+
+       bool has_synchronization_clock() const
+       {
+               return cpplinq::from(ports_)
+                       .select(values())
+                       .where(std::mem_fn(&port::has_synchronization_clock))
+                       .any();
+       }
+
+       std::future<void> operator()(const_frame input_frame, const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout)
+       {
+               spl::shared_ptr<caspar::timer> frame_timer;
+
+               change_channel_format(format_desc, channel_layout);
+
+               auto pending_send_results = executor_.invoke([=]() -> std::shared_ptr<std::map<int, std::future<bool>>>
+               {
+                       if (input_frame.size() != format_desc_.size)
+                       {
+                               CASPAR_LOG(warning) << print() << L" Invalid input frame dimension.";
+                               return nullptr;
+                       }
+
+                       auto minmax = minmax_buffer_depth();
+
+                       frames_.set_capacity(std::max(2, minmax.second - minmax.first) + 1); // std::max(2, x) since we want to guarantee some pipeline depth for asycnhronous mixer read-back.
+                       frames_.push_back(input_frame);
+
+                       if (!frames_.full())
+                               return nullptr;
+
+                       spl::shared_ptr<std::map<int, std::future<bool>>> send_results;
+
+                       // Start invocations
+                       for (auto it = ports_.begin(); it != ports_.end();)
+                       {
+                               auto& port = it->second;
+                               auto depth = port.buffer_depth();
+                               auto& frame = depth < 0 ? frames_.back() : frames_.at(depth - minmax.first);
+
+                               send_to_consumers_delays_[it->first] = frame.get_age_millis();
+
+                               try
+                               {
+                                       send_results->insert(std::make_pair(it->first, port.send(frame)));
+                                       ++it;
+                               }
+                               catch (...)
+                               {
+                                       CASPAR_LOG_CURRENT_EXCEPTION();
+                                       try
+                                       {
+                                               send_results->insert(std::make_pair(it->first, port.send(frame)));
+                                               ++it;
+                                       }
+                                       catch (...)
+                                       {
+                                               CASPAR_LOG_CURRENT_EXCEPTION();
+                                               CASPAR_LOG(error) << "Failed to recover consumer: " << port.print() << L". Removing it.";
+                                               send_to_consumers_delays_.erase(it->first);
+                                               it = ports_.erase(it);
+                                       }
+                               }
+                       }
+
+                       return send_results;
+               });
+
+               if (!pending_send_results)
+                       return make_ready_future();
+
+               return executor_.begin_invoke([=]()
+               {
+                       // Retrieve results
+                       for (auto it = pending_send_results->begin(); it != pending_send_results->end(); ++it)
+                       {
+                               try
+                               {
+                                       if (!it->second.get())
+                                       {
+                                               send_to_consumers_delays_.erase(it->first);
+                                               ports_.erase(it->first);
+                                       }
+                               }
+                               catch (...)
+                               {
+                                       CASPAR_LOG_CURRENT_EXCEPTION();
+                                       send_to_consumers_delays_.erase(it->first);
+                                       ports_.erase(it->first);
+                               }
+                       }
+
+                       if (!has_synchronization_clock())
+                               sync_timer_.tick(1.0 / format_desc_.fps);
+
+                       auto consume_time = frame_timer->elapsed();
+                       graph_->set_value("consume-time", consume_time * format_desc.fps * 0.5);
+                       *monitor_subject_
+                               << monitor::message("/consume_time") % consume_time
+                               << monitor::message("/profiler/time") % consume_time % (1.0 / format_desc.fps);
+               });
+       }
+
+       std::wstring print() const
+       {
+               return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
+       }
+
+       std::future<boost::property_tree::wptree> info()
+       {
+               return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
+               {
+                       boost::property_tree::wptree info;
+                       for (auto& port : ports_)
+                       {
+                               info.add_child(L"consumers.consumer", port.second.info())
+                                       .add(L"index", port.first);
+                       }
+                       return info;
+               }, task_priority::high_priority));
+       }
+
+       std::future<boost::property_tree::wptree> delay_info()
+       {
+               return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
+               {
+                       boost::property_tree::wptree info;
+
+                       for (auto& port : ports_)
+                       {
+                               auto total_age =
+                                       port.second.presentation_frame_age_millis();
+                               auto sendoff_age = send_to_consumers_delays_[port.first];
+                               auto presentation_time = total_age - sendoff_age;
+
+                               boost::property_tree::wptree child;
+                               child.add(L"name", port.second.print());
+                               child.add(L"age-at-arrival", sendoff_age);
+                               child.add(L"presentation-time", presentation_time);
+                               child.add(L"age-at-presentation", total_age);
+
+                               info.add_child(L"consumer", child);
+                       }
+                       return info;
+               }, task_priority::high_priority));
+       }
+
+       std::vector<spl::shared_ptr<const frame_consumer>> get_consumers()
+       {
+               return executor_.invoke([=]
+               {
+                       std::vector<spl::shared_ptr<const frame_consumer>> consumers;
+
+                       for (auto& port : ports_)
+                               consumers.push_back(port.second.consumer());
+
+                       return consumers;
+               });
+       }
+};
+
+output::output(spl::shared_ptr<diagnostics::graph> graph, const video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int channel_index) : impl_(new impl(std::move(graph), format_desc, channel_layout, channel_index)){}
+void output::add(int index, const spl::shared_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}
+void output::add(const spl::shared_ptr<frame_consumer>& consumer){impl_->add(consumer);}
+void output::remove(int index){impl_->remove(index);}
+void output::remove(const spl::shared_ptr<frame_consumer>& consumer){impl_->remove(consumer);}
+std::future<boost::property_tree::wptree> output::info() const{return impl_->info();}
+std::future<boost::property_tree::wptree> output::delay_info() const{ return impl_->delay_info(); }
+std::vector<spl::shared_ptr<const frame_consumer>> output::get_consumers() const { return impl_->get_consumers(); }
+std::future<void> output::operator()(const_frame frame, const video_format_desc& format_desc, const core::audio_channel_layout& channel_layout){ return (*impl_)(std::move(frame), format_desc, channel_layout); }
+monitor::subject& output::monitor_output() {return *impl_->monitor_subject_;}
+}}