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
33 #include "../video_format.h"
\r
34 #include "../frame/frame.h"
\r
36 #include <common/assert.h>
\r
37 #include <common/future.h>
\r
38 #include <common/executor.h>
\r
39 #include <common/diagnostics/graph.h>
\r
40 #include <common/prec_timer.h>
\r
41 #include <common/memshfl.h>
\r
42 #include <common/env.h>
\r
44 #include <boost/circular_buffer.hpp>
\r
45 #include <boost/lexical_cast.hpp>
\r
46 #include <boost/property_tree/ptree.hpp>
\r
47 #include <boost/range/algorithm.hpp>
\r
48 #include <boost/range/adaptors.hpp>
\r
49 #include <boost/timer.hpp>
\r
51 namespace caspar { namespace core {
\r
55 spl::shared_ptr<diagnostics::graph> graph_;
\r
56 monitor::basic_subject event_subject_;
\r
57 const int channel_index_;
\r
58 video_format_desc format_desc_;
\r
59 std::map<int, port> ports_;
\r
60 prec_timer sync_timer_;
\r
61 boost::circular_buffer<const_frame> frames_;
\r
62 executor executor_;
\r
64 impl(spl::shared_ptr<diagnostics::graph> graph, const video_format_desc& format_desc, int channel_index)
\r
65 : graph_(std::move(graph))
\r
66 , event_subject_("output")
\r
67 , channel_index_(channel_index)
\r
68 , format_desc_(format_desc)
\r
69 , executor_(L"output")
\r
71 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8));
\r
74 void add(int index, spl::shared_ptr<frame_consumer> consumer)
\r
78 consumer->initialize(format_desc_, channel_index_);
\r
80 executor_.begin_invoke([this, index, consumer]
\r
82 port p(index, channel_index_, std::move(consumer));
\r
83 p.subscribe(event_subject_);
\r
84 ports_.insert(std::make_pair(index, std::move(p)));
\r
85 }, task_priority::high_priority);
\r
88 void add(const spl::shared_ptr<frame_consumer>& consumer)
\r
90 add(consumer->index(), consumer);
\r
93 void remove(int index)
\r
95 executor_.begin_invoke([=]
\r
97 auto it = ports_.find(index);
\r
98 if(it != ports_.end())
\r
100 }, task_priority::high_priority);
\r
103 void remove(const spl::shared_ptr<frame_consumer>& consumer)
\r
105 remove(consumer->index());
\r
108 void video_format_desc(const core::video_format_desc& format_desc)
\r
110 executor_.invoke([&]
\r
112 if(format_desc_ == format_desc)
\r
115 auto it = ports_.begin();
\r
116 while(it != ports_.end())
\r
120 it->second.video_format_desc(format_desc);
\r
125 CASPAR_LOG_CURRENT_EXCEPTION();
\r
126 ports_.erase(it++);
\r
130 format_desc_ = format_desc;
\r
135 std::pair<int, int> minmax_buffer_depth() const
\r
138 return std::make_pair(0, 0);
\r
140 auto buffer_depths = ports_ |
\r
141 boost::adaptors::map_values | // std::function is MSVC workaround
\r
142 boost::adaptors::transformed(std::function<int(const port&)>([](const port& p){return p.buffer_depth();}));
\r
145 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));
\r
148 bool has_synchronization_clock() const
\r
150 return boost::range::count_if(ports_ | boost::adaptors::map_values,
\r
151 [](const port& p){return p.has_synchronization_clock();}) > 0;
\r
154 void operator()(const_frame input_frame, const core::video_format_desc& format_desc)
\r
156 video_format_desc(format_desc);
\r
158 executor_.invoke([=]
\r
160 boost::timer frame_timer;
\r
162 if(!has_synchronization_clock())
\r
163 sync_timer_.tick(1.0/format_desc_.fps);
\r
165 if(input_frame.size() != format_desc_.size)
\r
168 auto minmax = minmax_buffer_depth();
\r
170 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
171 frames_.push_back(input_frame);
\r
173 if(!frames_.full())
\r
176 for(auto it = ports_.begin(); it != ports_.end();)
\r
178 auto& port = it->second;
\r
179 auto& frame = frames_.at(port.buffer_depth()-minmax.first);
\r
183 if(port.send(frame))
\r
186 ports_.erase(it++);
\r
190 CASPAR_LOG_CURRENT_EXCEPTION();
\r
191 ports_.erase(it++);
\r
195 graph_->set_value("consume-time", frame_timer.elapsed()*format_desc.fps*0.5);
\r
199 std::wstring print() const
\r
201 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
\r
204 boost::unique_future<boost::property_tree::wptree> info()
\r
206 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
\r
208 boost::property_tree::wptree info;
\r
209 BOOST_FOREACH(auto& port, ports_)
\r
211 info.add_child(L"consumers.consumer", port.second.info())
\r
212 .add(L"index", port.first);
\r
215 }, task_priority::high_priority));
\r
219 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
220 void output::add(int index, const spl::shared_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}
\r
221 void output::add(const spl::shared_ptr<frame_consumer>& consumer){impl_->add(consumer);}
\r
222 void output::remove(int index){impl_->remove(index);}
\r
223 void output::remove(const spl::shared_ptr<frame_consumer>& consumer){impl_->remove(consumer);}
\r
224 boost::unique_future<boost::property_tree::wptree> output::info() const{return impl_->info();}
\r
225 void output::operator()(const_frame frame, const video_format_desc& format_desc){(*impl_)(std::move(frame), format_desc);}
\r
226 void output::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}
\r
227 void output::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}
\r