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 "../mixer/gpu/accelerator.h"
\r
34 #include "../mixer/read_frame.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 const int channel_index_;
\r
55 video_format_desc format_desc_;
\r
57 std::map<int, safe_ptr<frame_consumer>> consumers_;
\r
59 prec_timer sync_timer_;
\r
61 boost::circular_buffer<safe_ptr<const frame>> frames_;
\r
66 impl(const video_format_desc& format_desc, int channel_index)
\r
67 : channel_index_(channel_index)
\r
68 , format_desc_(format_desc)
\r
69 , executor_(L"output")
\r
73 void add(int index, safe_ptr<frame_consumer> consumer)
\r
77 consumer = create_consumer_cadence_guard(consumer);
\r
78 consumer->initialize(format_desc_, channel_index_);
\r
80 executor_.invoke([&]
\r
82 consumers_.insert(std::make_pair(index, consumer));
\r
83 CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";
\r
87 void add(const safe_ptr<frame_consumer>& consumer)
\r
89 add(consumer->index(), consumer);
\r
92 void remove(int index)
\r
94 // Destroy consumer on calling thread:
\r
95 std::shared_ptr<frame_consumer> old_consumer;
\r
97 executor_.invoke([&]
\r
99 auto it = consumers_.find(index);
\r
100 if(it != consumers_.end())
\r
102 old_consumer = it->second;
\r
103 consumers_.erase(it);
\r
109 auto str = old_consumer->print();
\r
110 old_consumer.reset();
\r
111 CASPAR_LOG(info) << print() << L" " << str << L" Removed.";
\r
115 void remove(const safe_ptr<frame_consumer>& consumer)
\r
117 remove(consumer->index());
\r
120 void set_video_format_desc(const video_format_desc& format_desc)
\r
122 executor_.invoke([&]
\r
124 auto it = consumers_.begin();
\r
125 while(it != consumers_.end())
\r
129 it->second->initialize(format_desc, channel_index_);
\r
134 CASPAR_LOG_CURRENT_EXCEPTION();
\r
135 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
136 consumers_.erase(it++);
\r
140 format_desc_ = format_desc;
\r
145 std::pair<int, int> minmax_buffer_depth() const
\r
147 if(consumers_.empty())
\r
148 return std::make_pair(0, 0);
\r
150 auto buffer_depths = consumers_ |
\r
151 boost::adaptors::map_values | // std::function is MSVC workaround
\r
152 boost::adaptors::transformed(std::function<int(const safe_ptr<frame_consumer>&)>([](const safe_ptr<frame_consumer>& x){return x->buffer_depth();}));
\r
155 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));
\r
158 bool has_synchronization_clock() const
\r
160 return boost::range::count_if(consumers_ | boost::adaptors::map_values, [](const safe_ptr<frame_consumer>& x){return x->has_synchronization_clock();}) > 0;
\r
163 void operator()(safe_ptr<const frame> input_frame, const video_format_desc& format_desc)
\r
165 executor_.invoke([=]
\r
169 if(!has_synchronization_clock())
\r
170 sync_timer_.tick(1.0/format_desc_.fps);
\r
172 if(format_desc_ != format_desc)
\r
173 set_video_format_desc(format_desc);
\r
175 if(input_frame->image_data().size() != format_desc_.size)
\r
177 sync_timer_.tick(1.0/format_desc_.fps);
\r
181 auto minmax = minmax_buffer_depth();
\r
183 frames_.set_capacity(minmax.second - minmax.first + 1);
\r
184 frames_.push_back(input_frame);
\r
186 if(!frames_.full())
\r
189 auto it = consumers_.begin();
\r
190 while(it != consumers_.end())
\r
192 auto consumer = it->second;
\r
193 auto frame = frames_.at(consumer->buffer_depth()-minmax.first);
\r
197 if(consumer->send(frame))
\r
201 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
202 consumers_.erase(it++);
\r
207 CASPAR_LOG_CURRENT_EXCEPTION();
\r
210 consumer->initialize(format_desc_, channel_index_);
\r
211 if(consumer->send(frame))
\r
215 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
216 consumers_.erase(it++);
\r
221 CASPAR_LOG_CURRENT_EXCEPTION();
\r
222 CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";
\r
223 consumers_.erase(it++);
\r
230 CASPAR_LOG_CURRENT_EXCEPTION();
\r
235 std::wstring print() const
\r
237 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
\r
240 boost::unique_future<boost::property_tree::wptree> info()
\r
242 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
\r
244 boost::property_tree::wptree info;
\r
245 BOOST_FOREACH(auto& consumer, consumers_)
\r
247 info.add_child(L"consumers.consumer", consumer.second->info())
\r
248 .add(L"index", consumer.first);
\r
251 }, high_priority));
\r
255 output::output(const video_format_desc& format_desc, int channel_index) : impl_(new impl(format_desc, channel_index)){}
\r
256 void output::add(int index, const safe_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}
\r
257 void output::add(const safe_ptr<frame_consumer>& consumer){impl_->add(consumer);}
\r
258 void output::remove(int index){impl_->remove(index);}
\r
259 void output::remove(const safe_ptr<frame_consumer>& consumer){impl_->remove(consumer);}
\r
260 boost::unique_future<boost::property_tree::wptree> output::info() const{return impl_->info();}
\r
261 void output::operator()(safe_ptr<const frame> frame, const video_format_desc& format_desc){(*impl_)(std::move(frame), format_desc);}
\r