]> 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 bf274f16392a8bd42e3dc6f5ec8b655d1f8c4d3d..d3c0580eb01bc624b18b396db5a8d9ec42a39206 100644 (file)
@@ -53,7 +53,7 @@
 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_;
@@ -65,23 +65,23 @@ struct output::impl
        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) 
+       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)));
@@ -94,7 +94,7 @@ public:
        }
 
        void remove(int index)
-       {               
+       {
                executor_.begin_invoke([=]
                {
                        auto it = ports_.find(index);
@@ -110,7 +110,7 @@ public:
        {
                remove(consumer->index());
        }
-       
+
        void change_channel_format(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout)
        {
                executor_.invoke([&]
@@ -120,7 +120,7 @@ public:
 
                        auto it = ports_.begin();
                        while(it != ports_.end())
-                       {                                               
+                       {
                                try
                                {
                                        it->second.change_channel_format(format_desc, channel_layout);
@@ -133,7 +133,7 @@ public:
                                        ports_.erase(it++);
                                }
                        }
-                       
+
                        format_desc_ = format_desc;
                        channel_layout_ = channel_layout;
                        frames_.clear();
@@ -141,7 +141,7 @@ public:
        }
 
        std::pair<int, int> minmax_buffer_depth() const
-       {               
+       {
                if(ports_.empty())
                        return std::make_pair(0, 0);
 
@@ -159,22 +159,19 @@ public:
                        .where(std::mem_fn(&port::has_synchronization_clock))
                        .any();
        }
-               
-       void operator()(const_frame input_frame, const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout)
+
+       std::future<void> operator()(const_frame input_frame, const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout)
        {
-               caspar::timer frame_timer;
+               spl::shared_ptr<caspar::timer> frame_timer;
 
                change_channel_format(format_desc, channel_layout);
 
-               executor_.invoke([=]
-               {                       
-                       if(!has_synchronization_clock())
-                               sync_timer_.tick(1.0/format_desc_.fps);
-                               
-                       if(input_frame.size() != format_desc_.size)
+               auto pending_send_results = executor_.invoke([=]() -> std::shared_ptr<std::map<int, std::future<bool>>>
+               {
+                       if (input_frame.size() != format_desc_.size)
                        {
-                               CASPAR_LOG(debug) << print() << L" Invalid input frame dimension.";
-                               return;
+                               CASPAR_LOG(warning) << print() << L" Invalid input frame dimension.";
+                               return nullptr;
                        }
 
                        auto minmax = minmax_buffer_depth();
@@ -182,23 +179,23 @@ public:
                        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;
+                       if (!frames_.full())
+                               return nullptr;
 
-                       std::map<int, std::future<bool>> send_results;
+                       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& 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)));
+                                       send_results->insert(std::make_pair(it->first, port.send(frame)));
                                        ++it;
                                }
                                catch (...)
@@ -206,10 +203,10 @@ public:
                                        CASPAR_LOG_CURRENT_EXCEPTION();
                                        try
                                        {
-                                               send_results.insert(std::make_pair(it->first, port.send(frame)));
+                                               send_results->insert(std::make_pair(it->first, port.send(frame)));
                                                ++it;
                                        }
-                                       catch(...)
+                                       catch (...)
                                        {
                                                CASPAR_LOG_CURRENT_EXCEPTION();
                                                CASPAR_LOG(error) << "Failed to recover consumer: " << port.print() << L". Removing it.";
@@ -219,8 +216,16 @@ public:
                                }
                        }
 
+                       return send_results;
+               });
+
+               if (!pending_send_results)
+                       return make_ready_future();
+
+               return executor_.begin_invoke([=]()
+               {
                        // Retrieve results
-                       for (auto it = send_results.begin(); it != send_results.end(); ++it)
+                       for (auto it = pending_send_results->begin(); it != pending_send_results->end(); ++it)
                        {
                                try
                                {
@@ -237,13 +242,16 @@ public:
                                        ports_.erase(it->first);
                                }
                        }
-               });
 
-               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);
+                       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
@@ -254,12 +262,12 @@ public:
        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); 
+                                       .add(L"index", port.first);
                        }
                        return info;
                }, task_priority::high_priority));
@@ -289,6 +297,19 @@ public:
                        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)){}
@@ -298,6 +319,7 @@ 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(); }
-void output::operator()(const_frame frame, const video_format_desc& format_desc, const core::audio_channel_layout& channel_layout){ (*impl_)(std::move(frame), format_desc, channel_layout); }
+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_;}
 }}