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 "../video_format.h"
\r
31 #include "../mixer/gpu/ogl_device.h"
\r
32 #include "../mixer/read_frame.h"
\r
34 #include <common/concurrency/executor.h>
\r
35 #include <common/utility/assert.h>
\r
36 #include <common/utility/timer.h>
\r
37 #include <common/memory/memshfl.h>
\r
38 #include <common/env.h>
\r
40 #include <boost/circular_buffer.hpp>
\r
41 #include <boost/timer.hpp>
\r
42 #include <boost/range/algorithm.hpp>
\r
43 #include <boost/range/adaptors.hpp>
\r
44 #include <boost/property_tree/ptree.hpp>
\r
46 namespace caspar { namespace core {
\r
48 struct output::implementation
\r
50 const int channel_index_;
\r
51 const safe_ptr<diagnostics::graph> graph_;
\r
52 boost::timer consume_timer_;
\r
54 video_format_desc format_desc_;
\r
56 std::map<int, safe_ptr<frame_consumer>> consumers_;
\r
58 high_prec_timer sync_timer_;
\r
60 boost::circular_buffer<safe_ptr<read_frame>> frames_;
\r
65 implementation(const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc, int channel_index)
\r
66 : 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));
\r
74 void add(int index, safe_ptr<frame_consumer> consumer)
\r
78 consumer = create_consumer_cadence_guard(consumer);
\r
79 consumer->initialize(format_desc_, channel_index_);
\r
81 executor_.invoke([&]
\r
83 consumers_.insert(std::make_pair(index, consumer));
\r
84 CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";
\r
88 void add(const safe_ptr<frame_consumer>& consumer)
\r
90 add(consumer->index(), consumer);
\r
93 void remove(int index)
\r
95 // Destroy consumer on calling thread:
\r
96 std::shared_ptr<frame_consumer> old_consumer;
\r
98 executor_.invoke([&]
\r
100 auto it = consumers_.find(index);
\r
101 if(it != consumers_.end())
\r
103 old_consumer = it->second;
\r
104 consumers_.erase(it);
\r
110 auto str = old_consumer->print();
\r
111 old_consumer.reset();
\r
112 CASPAR_LOG(info) << print() << L" " << str << L" Removed.";
\r
116 void remove(const safe_ptr<frame_consumer>& consumer)
\r
118 remove(consumer->index());
\r
121 void set_video_format_desc(const video_format_desc& format_desc)
\r
123 executor_.invoke([&]
\r
125 auto it = consumers_.begin();
\r
126 while(it != consumers_.end())
\r
130 it->second->initialize(format_desc_, channel_index_);
\r
135 CASPAR_LOG_CURRENT_EXCEPTION();
\r
136 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
137 consumers_.erase(it++);
\r
141 format_desc_ = format_desc;
\r
146 std::pair<int, int> minmax_buffer_depth() const
\r
148 if(consumers_.empty())
\r
149 return std::make_pair(0, 0);
\r
151 auto buffer_depths = consumers_ |
\r
152 boost::adaptors::map_values | // std::function is MSVC workaround
\r
153 boost::adaptors::transformed(std::function<int(const safe_ptr<frame_consumer>&)>([](const safe_ptr<frame_consumer>& x){return x->buffer_depth();}));
\r
156 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));
\r
159 bool has_synchronization_clock() const
\r
161 return boost::range::count_if(consumers_ | boost::adaptors::map_values, [](const safe_ptr<frame_consumer>& x){return x->has_synchronization_clock();}) > 0;
\r
164 void send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& packet)
\r
166 executor_.begin_invoke([=]
\r
170 consume_timer_.restart();
\r
172 auto input_frame = packet.first;
\r
174 if(!has_synchronization_clock())
\r
175 sync_timer_.tick(1.0/format_desc_.fps);
\r
177 if(input_frame->image_data().size() != format_desc_.size)
\r
179 sync_timer_.tick(1.0/format_desc_.fps);
\r
183 auto minmax = minmax_buffer_depth();
\r
185 frames_.set_capacity(minmax.second - minmax.first + 1);
\r
186 frames_.push_back(input_frame);
\r
188 if(!frames_.full())
\r
191 auto it = consumers_.begin();
\r
192 while(it != consumers_.end())
\r
194 auto consumer = it->second;
\r
195 auto frame = frames_.at(consumer->buffer_depth()-minmax.first);
\r
199 if(consumer->send(frame))
\r
203 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
204 consumers_.erase(it++);
\r
209 CASPAR_LOG_CURRENT_EXCEPTION();
\r
212 consumer->initialize(format_desc_, channel_index_);
\r
213 if(consumer->send(frame))
\r
217 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
218 consumers_.erase(it++);
\r
223 CASPAR_LOG_CURRENT_EXCEPTION();
\r
224 CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";
\r
225 consumers_.erase(it++);
\r
230 graph_->set_value("consume-time", consume_timer_.elapsed()*format_desc_.fps*0.5);
\r
234 CASPAR_LOG_CURRENT_EXCEPTION();
\r
239 std::wstring print() const
\r
241 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
\r
244 boost::unique_future<boost::property_tree::wptree> info()
\r
246 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
\r
248 boost::property_tree::wptree info;
\r
249 BOOST_FOREACH(auto& consumer, consumers_)
\r
251 info.add_child(L"consumers.consumer", consumer.second->info())
\r
252 .add(L"index", consumer.first);
\r
255 }, high_priority));
\r
259 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
260 void output::add(int index, const safe_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}
\r
261 void output::add(const safe_ptr<frame_consumer>& consumer){impl_->add(consumer);}
\r
262 void output::remove(int index){impl_->remove(index);}
\r
263 void output::remove(const safe_ptr<frame_consumer>& consumer){impl_->remove(consumer);}
\r
264 void output::send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& frame) {impl_->send(frame); }
\r
265 void output::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}
\r
266 boost::unique_future<boost::property_tree::wptree> output::info() const{return impl_->info();}
\r