2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../StdAfx.h"
25 #pragma warning (disable : 4244)
30 #include "frame_consumer.h"
33 #include "../video_format.h"
34 #include "../frame/frame.h"
35 #include "../frame/audio_channel_layout.h"
37 #include <common/assert.h>
38 #include <common/future.h>
39 #include <common/executor.h>
40 #include <common/diagnostics/graph.h>
41 #include <common/prec_timer.h>
42 #include <common/memshfl.h>
43 #include <common/env.h>
44 #include <common/linq.h>
45 #include <common/timer.h>
47 #include <boost/circular_buffer.hpp>
48 #include <boost/lexical_cast.hpp>
49 #include <boost/property_tree/ptree.hpp>
53 namespace caspar { namespace core {
57 spl::shared_ptr<diagnostics::graph> graph_;
58 spl::shared_ptr<monitor::subject> monitor_subject_ = spl::make_shared<monitor::subject>("/output");
59 const int channel_index_;
60 video_format_desc format_desc_;
61 audio_channel_layout channel_layout_;
62 std::map<int, port> ports_;
63 prec_timer sync_timer_;
64 boost::circular_buffer<const_frame> frames_;
65 std::map<int, int64_t> send_to_consumers_delays_;
66 executor executor_ { L"output " + boost::lexical_cast<std::wstring>(channel_index_) };
68 impl(spl::shared_ptr<diagnostics::graph> graph, const video_format_desc& format_desc, const audio_channel_layout& channel_layout, int channel_index)
69 : graph_(std::move(graph))
70 , channel_index_(channel_index)
71 , format_desc_(format_desc)
72 , channel_layout_(channel_layout)
74 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8f));
77 void add(int index, spl::shared_ptr<frame_consumer> consumer)
81 consumer->initialize(format_desc_, channel_layout_, channel_index_);
83 executor_.begin_invoke([this, index, consumer]
85 port p(index, channel_index_, std::move(consumer));
86 p.monitor_output().attach_parent(monitor_subject_);
87 ports_.insert(std::make_pair(index, std::move(p)));
88 }, task_priority::high_priority);
91 void add(const spl::shared_ptr<frame_consumer>& consumer)
93 add(consumer->index(), consumer);
96 void remove(int index)
98 executor_.begin_invoke([=]
100 auto it = ports_.find(index);
101 if (it != ports_.end())
104 send_to_consumers_delays_.erase(index);
106 }, task_priority::high_priority);
109 void remove(const spl::shared_ptr<frame_consumer>& consumer)
111 remove(consumer->index());
114 void change_channel_format(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout)
118 if(format_desc_ == format_desc && channel_layout_ == channel_layout)
121 auto it = ports_.begin();
122 while(it != ports_.end())
126 it->second.change_channel_format(format_desc, channel_layout);
131 CASPAR_LOG_CURRENT_EXCEPTION();
132 send_to_consumers_delays_.erase(it->first);
137 format_desc_ = format_desc;
138 channel_layout_ = channel_layout;
143 std::pair<int, int> minmax_buffer_depth() const
146 return std::make_pair(0, 0);
148 return cpplinq::from(ports_)
150 .select(std::mem_fn(&port::buffer_depth))
151 .where([](int v) { return v >= 0; })
152 .aggregate(minmax::initial_value<int>(), minmax());
155 bool has_synchronization_clock() const
157 return cpplinq::from(ports_)
159 .where(std::mem_fn(&port::has_synchronization_clock))
163 std::future<void> operator()(const_frame input_frame, const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout)
165 spl::shared_ptr<caspar::timer> frame_timer;
167 change_channel_format(format_desc, channel_layout);
169 auto pending_send_results = executor_.invoke([=]() -> std::shared_ptr<std::map<int, std::future<bool>>>
171 if (input_frame.size() != format_desc_.size)
173 CASPAR_LOG(warning) << print() << L" Invalid input frame dimension.";
177 auto minmax = minmax_buffer_depth();
179 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.
180 frames_.push_back(input_frame);
185 spl::shared_ptr<std::map<int, std::future<bool>>> send_results;
188 for (auto it = ports_.begin(); it != ports_.end();)
190 auto& port = it->second;
191 auto depth = port.buffer_depth();
192 auto& frame = depth < 0 ? frames_.back() : frames_.at(depth - minmax.first);
194 send_to_consumers_delays_[it->first] = frame.get_age_millis();
198 send_results->insert(std::make_pair(it->first, port.send(frame)));
203 CASPAR_LOG_CURRENT_EXCEPTION();
206 send_results->insert(std::make_pair(it->first, port.send(frame)));
211 CASPAR_LOG_CURRENT_EXCEPTION();
212 CASPAR_LOG(error) << "Failed to recover consumer: " << port.print() << L". Removing it.";
213 send_to_consumers_delays_.erase(it->first);
214 it = ports_.erase(it);
222 if (!pending_send_results)
223 return make_ready_future();
225 return executor_.begin_invoke([=]()
228 for (auto it = pending_send_results->begin(); it != pending_send_results->end(); ++it)
232 if (!it->second.get())
234 send_to_consumers_delays_.erase(it->first);
235 ports_.erase(it->first);
240 CASPAR_LOG_CURRENT_EXCEPTION();
241 send_to_consumers_delays_.erase(it->first);
242 ports_.erase(it->first);
246 if (!has_synchronization_clock())
247 sync_timer_.tick(1.0 / format_desc_.fps);
249 auto consume_time = frame_timer->elapsed();
250 graph_->set_value("consume-time", consume_time * format_desc.fps * 0.5);
252 << monitor::message("/consume_time") % consume_time
253 << monitor::message("/profiler/time") % consume_time % (1.0 / format_desc.fps);
257 std::wstring print() const
259 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
262 std::future<boost::property_tree::wptree> info()
264 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
266 boost::property_tree::wptree info;
267 for (auto& port : ports_)
269 info.add_child(L"consumers.consumer", port.second.info())
270 .add(L"index", port.first);
273 }, task_priority::high_priority));
276 std::future<boost::property_tree::wptree> delay_info()
278 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
280 boost::property_tree::wptree info;
282 for (auto& port : ports_)
285 port.second.presentation_frame_age_millis();
286 auto sendoff_age = send_to_consumers_delays_[port.first];
287 auto presentation_time = total_age - sendoff_age;
289 boost::property_tree::wptree child;
290 child.add(L"name", port.second.print());
291 child.add(L"age-at-arrival", sendoff_age);
292 child.add(L"presentation-time", presentation_time);
293 child.add(L"age-at-presentation", total_age);
295 info.add_child(L"consumer", child);
298 }, task_priority::high_priority));
301 std::vector<spl::shared_ptr<const frame_consumer>> get_consumers()
303 return executor_.invoke([=]
305 std::vector<spl::shared_ptr<const frame_consumer>> consumers;
307 for (auto& port : ports_)
308 consumers.push_back(port.second.consumer());
315 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)){}
316 void output::add(int index, const spl::shared_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}
317 void output::add(const spl::shared_ptr<frame_consumer>& consumer){impl_->add(consumer);}
318 void output::remove(int index){impl_->remove(index);}
319 void output::remove(const spl::shared_ptr<frame_consumer>& consumer){impl_->remove(consumer);}
320 std::future<boost::property_tree::wptree> output::info() const{return impl_->info();}
321 std::future<boost::property_tree::wptree> output::delay_info() const{ return impl_->delay_info(); }
322 std::vector<spl::shared_ptr<const frame_consumer>> output::get_consumers() const { return impl_->get_consumers(); }
323 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); }
324 monitor::subject& output::monitor_output() {return *impl_->monitor_subject_;}