2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
20 // TODO: Try to recover consumer from bad_alloc...
\r
21 #include "../StdAfx.h"
\r
24 #pragma warning (disable : 4244)
\r
29 #include "../video_format.h"
\r
30 #include "../mixer/gpu/ogl_device.h"
\r
31 #include "../mixer/read_frame.h"
\r
33 #include <common/concurrency/executor.h>
\r
34 #include <common/utility/assert.h>
\r
35 #include <common/utility/timer.h>
\r
36 #include <common/memory/memshfl.h>
\r
37 #include <common/env.h>
\r
39 #include <boost/circular_buffer.hpp>
\r
40 #include <boost/timer.hpp>
\r
41 #include <boost/range/algorithm.hpp>
\r
42 #include <boost/range/adaptors.hpp>
\r
44 namespace caspar { namespace core {
\r
46 struct output::implementation
\r
48 const int channel_index_;
\r
49 const safe_ptr<diagnostics::graph> graph_;
\r
50 boost::timer consume_timer_;
\r
52 video_format_desc format_desc_;
\r
54 std::map<int, safe_ptr<frame_consumer>> consumers_;
\r
56 high_prec_timer sync_timer_;
\r
58 boost::circular_buffer<safe_ptr<read_frame>> frames_;
\r
63 implementation(const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc, int channel_index)
\r
64 : channel_index_(channel_index)
\r
66 , format_desc_(format_desc)
\r
67 , executor_(L"output")
\r
69 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f));
\r
72 void add(int index, safe_ptr<frame_consumer>&& consumer)
\r
74 executor_.invoke([&]
\r
76 consumers_.erase(index);
\r
79 consumer = create_consumer_cadence_guard(std::move(consumer));
\r
80 consumer->initialize(format_desc_, channel_index_, index);
\r
82 executor_.invoke([&]
\r
84 consumers_.insert(std::make_pair(index, consumer));
\r
86 CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";
\r
90 void remove(int index)
\r
92 executor_.invoke([&]
\r
94 auto it = consumers_.find(index);
\r
95 if(it != consumers_.end())
\r
97 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
98 consumers_.erase(it);
\r
103 void set_video_format_desc(const video_format_desc& format_desc)
\r
105 executor_.invoke([&]
\r
107 auto it = consumers_.begin();
\r
108 while(it != consumers_.end())
\r
112 it->second->initialize(format_desc_, channel_index_, it->first);
\r
117 CASPAR_LOG_CURRENT_EXCEPTION();
\r
118 consumers_.erase(it++);
\r
122 format_desc_ = format_desc;
\r
127 std::pair<size_t, size_t> minmax_buffer_depth() const
\r
129 if(consumers_.empty())
\r
130 return std::make_pair(0, 0);
\r
132 auto buffer_depths = consumers_ |
\r
133 boost::adaptors::map_values | // std::function is MSVC workaround
\r
134 boost::adaptors::transformed(std::function<int(const safe_ptr<frame_consumer>&)>([](const safe_ptr<frame_consumer>& x){return x->buffer_depth();}));
\r
137 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));
\r
140 bool has_synchronization_clock() const
\r
142 return boost::range::count_if(consumers_ | boost::adaptors::map_values, [](const safe_ptr<frame_consumer>& x){return x->has_synchronization_clock();}) > 0;
\r
145 void send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& packet)
\r
147 executor_.begin_invoke([=]
\r
151 consume_timer_.restart();
\r
153 auto input_frame = packet.first;
\r
155 if(!has_synchronization_clock())
\r
156 sync_timer_.tick(1.0/format_desc_.fps);
\r
158 if(input_frame->image_size() != format_desc_.size)
\r
160 sync_timer_.tick(1.0/format_desc_.fps);
\r
164 auto minmax = minmax_buffer_depth();
\r
166 frames_.set_capacity(minmax.second - minmax.first + 1);
\r
167 frames_.push_back(input_frame);
\r
169 if(!frames_.full())
\r
172 auto it = consumers_.begin();
\r
173 while(it != consumers_.end())
\r
175 auto consumer = it->second;
\r
176 auto frame = frames_.at(consumer->buffer_depth()-minmax.first);
\r
180 if(consumer->send(frame))
\r
183 consumers_.erase(it++);
\r
187 CASPAR_LOG_CURRENT_EXCEPTION();
\r
190 consumer->initialize(format_desc_, channel_index_, it->first);
\r
191 if(consumer->send(frame))
\r
194 consumers_.erase(it++);
\r
198 CASPAR_LOG_CURRENT_EXCEPTION();
\r
199 CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";
\r
200 consumers_.erase(it++);
\r
205 graph_->update_value("consume-time", consume_timer_.elapsed()*format_desc_.fps*0.5);
\r
209 CASPAR_LOG_CURRENT_EXCEPTION();
\r
214 std::wstring print() const
\r
216 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
\r
220 output::output(const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc, int channel_index) : impl_(new implementation(graph, format_desc, channel_index)){}
\r
221 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}
\r
222 void output::remove(int index){impl_->remove(index);}
\r
223 void output::send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& frame) {impl_->send(frame); }
\r
224 void output::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}
\r