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/concurrency/async.h>
\r
36 #include <common/concurrency/executor.h>
\r
37 #include <common/diagnostics/graph.h>
\r
38 #include <common/prec_timer.h>
\r
39 #include <common/memory/memshfl.h>
\r
40 #include <common/env.h>
\r
42 #include <common/assert.h>
\r
43 #include <boost/circular_buffer.hpp>
\r
44 #include <boost/timer.hpp>
\r
45 #include <boost/range/algorithm.hpp>
\r
46 #include <boost/range/adaptors.hpp>
\r
47 #include <boost/property_tree/ptree.hpp>
\r
49 namespace caspar { namespace core {
\r
53 spl::shared_ptr<diagnostics::graph> graph_;
\r
54 const int channel_index_;
\r
55 video_format_desc format_desc_;
\r
56 std::map<int, spl::shared_ptr<frame_consumer>> consumers_;
\r
57 prec_timer sync_timer_;
\r
58 boost::circular_buffer<const_frame> frames_;
\r
59 executor executor_;
\r
61 impl(spl::shared_ptr<diagnostics::graph> graph, const video_format_desc& format_desc, int channel_index)
\r
62 : graph_(std::move(graph))
\r
63 , channel_index_(channel_index)
\r
64 , format_desc_(format_desc)
\r
65 , executor_(L"output")
\r
67 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8));
\r
70 void add(int index, spl::shared_ptr<frame_consumer> consumer)
\r
74 consumer->initialize(format_desc_, channel_index_);
\r
76 executor_.begin_invoke([=]
\r
78 consumers_.insert(std::make_pair(index, consumer));
\r
79 CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";
\r
80 }, task_priority::high_priority);
\r
83 void add(const spl::shared_ptr<frame_consumer>& consumer)
\r
85 add(consumer->index(), consumer);
\r
88 void remove(int index)
\r
90 executor_.begin_invoke([=]
\r
92 auto it = consumers_.find(index);
\r
93 if(it != consumers_.end())
\r
94 consumers_.erase(it);
\r
95 }, task_priority::high_priority);
\r
98 void remove(const spl::shared_ptr<frame_consumer>& consumer)
\r
100 remove(consumer->index());
\r
103 void video_format_desc(const core::video_format_desc& format_desc)
\r
105 executor_.invoke([&]
\r
107 if(format_desc_ == format_desc)
\r
110 auto it = consumers_.begin();
\r
111 while(it != consumers_.end())
\r
115 it->second->initialize(format_desc, channel_index_);
\r
120 CASPAR_LOG_CURRENT_EXCEPTION();
\r
121 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
122 consumers_.erase(it++);
\r
126 format_desc_ = format_desc;
\r
131 std::pair<int, int> minmax_buffer_depth() const
\r
133 if(consumers_.empty())
\r
134 return std::make_pair(0, 0);
\r
136 auto buffer_depths = consumers_ |
\r
137 boost::adaptors::map_values | // std::function is MSVC workaround
\r
138 boost::adaptors::transformed(std::function<int(const spl::shared_ptr<frame_consumer>&)>([](const spl::shared_ptr<frame_consumer>& x){return x->buffer_depth();}));
\r
141 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));
\r
144 bool has_synchronization_clock() const
\r
146 return boost::range::count_if(consumers_ | boost::adaptors::map_values, [](const spl::shared_ptr<frame_consumer>& x){return x->has_synchronization_clock();}) > 0;
\r
149 void operator()(const_frame input_frame, const core::video_format_desc& format_desc)
\r
151 video_format_desc(format_desc);
\r
153 executor_.invoke([=]
\r
155 boost::timer frame_timer;
\r
157 if(!has_synchronization_clock())
\r
158 sync_timer_.tick(1.0/format_desc_.fps);
\r
160 if(input_frame.size() != format_desc_.size)
\r
162 sync_timer_.tick(1.0/format_desc_.fps);
\r
166 auto minmax = minmax_buffer_depth();
\r
168 frames_.set_capacity(std::max(2, minmax.second - minmax.first) + 1); // std::max(1, x) since we want to guarantee some pipeline depth for asycnhronous mixer read-back.
\r
169 frames_.push_back(input_frame);
\r
171 if(!frames_.full())
\r
174 for(auto it = consumers_.begin(); it != consumers_.end();)
\r
176 auto consumer = it->second;
\r
177 auto frame = frames_.at(consumer->buffer_depth()-minmax.first);
\r
179 if(consumer->send(frame))
\r
183 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
184 consumers_.erase(it++);
\r
188 graph_->set_value("consume-time", frame_timer.elapsed()*format_desc.fps*0.5);
\r
192 std::wstring print() const
\r
194 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
\r
197 boost::unique_future<boost::property_tree::wptree> info()
\r
199 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
\r
201 boost::property_tree::wptree info;
\r
202 BOOST_FOREACH(auto& consumer, consumers_)
\r
204 info.add_child(L"consumers.consumer", consumer.second->info())
\r
205 .add(L"index", consumer.first);
\r
208 }, task_priority::high_priority));
\r
212 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
213 void output::add(int index, const spl::shared_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}
\r
214 void output::add(const spl::shared_ptr<frame_consumer>& consumer){impl_->add(consumer);}
\r
215 void output::remove(int index){impl_->remove(index);}
\r
216 void output::remove(const spl::shared_ptr<frame_consumer>& consumer){impl_->remove(consumer);}
\r
217 boost::unique_future<boost::property_tree::wptree> output::info() const{return impl_->info();}
\r
218 void output::operator()(const_frame frame, const video_format_desc& format_desc){(*impl_)(std::move(frame), format_desc);}
\r