2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../StdAfx.h"
\r
25 #pragma warning (disable : 4244)
\r
30 #include "frame_consumer.h"
\r
32 #include "../video_format.h"
\r
33 #include "../frame/frame.h"
\r
35 #include <common/assert.h>
\r
36 #include <common/future.h>
\r
37 #include <common/executor.h>
\r
38 #include <common/diagnostics/graph.h>
\r
39 #include <common/prec_timer.h>
\r
40 #include <common/memshfl.h>
\r
41 #include <common/env.h>
\r
43 #include <boost/circular_buffer.hpp>
\r
44 #include <boost/lexical_cast.hpp>
\r
45 #include <boost/property_tree/ptree.hpp>
\r
46 #include <boost/range/algorithm.hpp>
\r
47 #include <boost/range/adaptors.hpp>
\r
48 #include <boost/timer.hpp>
\r
50 namespace caspar { namespace core {
\r
52 class port : public monitor::observable
\r
55 port& operator=(const port&);
\r
57 monitor::basic_subject event_subject_;
\r
58 std::shared_ptr<frame_consumer> consumer_;
\r
62 port(int index, int channel_index, spl::shared_ptr<frame_consumer> consumer)
\r
63 : event_subject_(monitor::path("port") % index)
\r
64 , consumer_(std::move(consumer))
\r
66 , channel_index_(channel_index)
\r
68 consumer_->subscribe(event_subject_);
\r
72 : event_subject_(std::move(other.event_subject_))
\r
73 , consumer_(std::move(other.consumer_))
\r
74 , index_(other.index_)
\r
75 , channel_index_(other.channel_index_)
\r
79 port& operator=(port&& other)
\r
81 event_subject_ = std::move(other.event_subject_);
\r
82 consumer_ = std::move(other.consumer_);
\r
83 index_ = std::move(other.index_);
\r
84 channel_index_ = std::move(other.channel_index_);
\r
87 void video_format_desc(const struct video_format_desc& format_desc)
\r
89 consumer_->initialize(format_desc, channel_index_);
\r
92 bool send(class const_frame frame)
\r
94 event_subject_ << monitor::event("type") % consumer_->name();
\r
95 return consumer_->send(frame);
\r
103 int buffer_depth() const
\r
105 return consumer_->buffer_depth();
\r
108 bool has_synchronization_clock() const
\r
110 return consumer_->has_synchronization_clock();
\r
113 boost::property_tree::wptree info() const
\r
115 return consumer_->info();
\r
118 void subscribe(const monitor::observable::observer_ptr& o) override
\r
120 event_subject_.subscribe(o);
\r
123 void unsubscribe(const monitor::observable::observer_ptr& o) override
\r
125 event_subject_.unsubscribe(o);
\r
129 struct output::impl
\r
131 spl::shared_ptr<diagnostics::graph> graph_;
\r
132 monitor::basic_subject event_subject_;
\r
133 const int channel_index_;
\r
134 video_format_desc format_desc_;
\r
135 std::map<int, port> ports_;
\r
136 prec_timer sync_timer_;
\r
137 boost::circular_buffer<const_frame> frames_;
\r
138 executor executor_;
\r
140 impl(spl::shared_ptr<diagnostics::graph> graph, const video_format_desc& format_desc, int channel_index)
\r
141 : graph_(std::move(graph))
\r
142 , event_subject_("output")
\r
143 , channel_index_(channel_index)
\r
144 , format_desc_(format_desc)
\r
145 , executor_(L"output")
\r
147 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8));
\r
150 void add(int index, spl::shared_ptr<frame_consumer> consumer)
\r
154 consumer->initialize(format_desc_, channel_index_);
\r
156 executor_.begin_invoke([this, index, consumer]
\r
158 port p(index, channel_index_, std::move(consumer));
\r
159 p.subscribe(event_subject_);
\r
160 ports_.insert(std::make_pair(index, std::move(p)));
\r
161 }, task_priority::high_priority);
\r
164 void add(const spl::shared_ptr<frame_consumer>& consumer)
\r
166 add(consumer->index(), consumer);
\r
169 void remove(int index)
\r
171 executor_.begin_invoke([=]
\r
173 auto it = ports_.find(index);
\r
174 if(it != ports_.end())
\r
176 }, task_priority::high_priority);
\r
179 void remove(const spl::shared_ptr<frame_consumer>& consumer)
\r
181 remove(consumer->index());
\r
184 void video_format_desc(const core::video_format_desc& format_desc)
\r
186 executor_.invoke([&]
\r
188 if(format_desc_ == format_desc)
\r
191 auto it = ports_.begin();
\r
192 while(it != ports_.end())
\r
196 it->second.video_format_desc(format_desc);
\r
201 CASPAR_LOG_CURRENT_EXCEPTION();
\r
202 ports_.erase(it++);
\r
206 format_desc_ = format_desc;
\r
211 std::pair<int, int> minmax_buffer_depth() const
\r
214 return std::make_pair(0, 0);
\r
216 auto buffer_depths = ports_ |
\r
217 boost::adaptors::map_values | // std::function is MSVC workaround
\r
218 boost::adaptors::transformed(std::function<int(const port&)>([](const port& p){return p.buffer_depth();}));
\r
221 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));
\r
224 bool has_synchronization_clock() const
\r
226 return boost::range::count_if(ports_ | boost::adaptors::map_values,
\r
227 [](const port& p){return p.has_synchronization_clock();}) > 0;
\r
230 void operator()(const_frame input_frame, const core::video_format_desc& format_desc)
\r
232 video_format_desc(format_desc);
\r
234 executor_.invoke([=]
\r
236 boost::timer frame_timer;
\r
238 if(!has_synchronization_clock())
\r
239 sync_timer_.tick(1.0/format_desc_.fps);
\r
241 if(input_frame.size() != format_desc_.size)
\r
243 sync_timer_.tick(1.0/format_desc_.fps);
\r
247 auto minmax = minmax_buffer_depth();
\r
249 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.
\r
250 frames_.push_back(input_frame);
\r
252 if(!frames_.full())
\r
255 for(auto it = ports_.begin(); it != ports_.end();)
\r
257 auto& port = it->second;
\r
258 auto& frame = frames_.at(port.buffer_depth()-minmax.first);
\r
262 if(port.send(frame))
\r
265 ports_.erase(it++);
\r
269 CASPAR_LOG_CURRENT_EXCEPTION();
\r
270 ports_.erase(it++);
\r
274 graph_->set_value("consume-time", frame_timer.elapsed()*format_desc.fps*0.5);
\r
278 std::wstring print() const
\r
280 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
\r
283 boost::unique_future<boost::property_tree::wptree> info()
\r
285 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
\r
287 boost::property_tree::wptree info;
\r
288 BOOST_FOREACH(auto& port, ports_ | boost::adaptors::map_values)
\r
290 info.add_child(L"consumers.consumer", port.info())
\r
291 .add(L"index", port.index());
\r
294 }, task_priority::high_priority));
\r
298 output::output(spl::shared_ptr<diagnostics::graph> graph, const video_format_desc& format_desc, int channel_index) : impl_(new impl(std::move(graph), format_desc, channel_index)){}
\r
299 void output::add(int index, const spl::shared_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}
\r
300 void output::add(const spl::shared_ptr<frame_consumer>& consumer){impl_->add(consumer);}
\r
301 void output::remove(int index){impl_->remove(index);}
\r
302 void output::remove(const spl::shared_ptr<frame_consumer>& consumer){impl_->remove(consumer);}
\r
303 boost::unique_future<boost::property_tree::wptree> output::info() const{return impl_->info();}
\r
304 void output::operator()(const_frame frame, const video_format_desc& format_desc){(*impl_)(std::move(frame), format_desc);}
\r
305 void output::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}
\r
306 void output::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}
\r