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 "cadence/cadence_guard_consumer.h"
\r
34 #include "../video_format.h"
\r
35 #include "../frame/data_frame.h"
\r
37 #include <common/concurrency/executor.h>
\r
38 #include <common/diagnostics/graph.h>
\r
39 #include <common/prec_timer.h>
\r
40 #include <common/memory/memshfl.h>
\r
41 #include <common/env.h>
\r
43 #include <common/assert.h>
\r
44 #include <boost/circular_buffer.hpp>
\r
45 #include <boost/timer.hpp>
\r
46 #include <boost/range/algorithm.hpp>
\r
47 #include <boost/range/adaptors.hpp>
\r
48 #include <boost/property_tree/ptree.hpp>
\r
50 namespace caspar { namespace core {
\r
54 const int channel_index_;
\r
55 video_format_desc format_desc_;
\r
56 std::map<int, safe_ptr<frame_consumer>> consumers_;
\r
57 prec_timer sync_timer_;
\r
58 boost::circular_buffer<safe_ptr<const data_frame>> frames_;
\r
60 executor executor_;
\r
62 impl(const video_format_desc& format_desc, int channel_index)
\r
63 : channel_index_(channel_index)
\r
64 , format_desc_(format_desc)
\r
65 , executor_(L"output")
\r
69 void add(int index, safe_ptr<frame_consumer> consumer)
\r
73 consumer = create_consumer_cadence_guard(consumer);
\r
74 consumer->initialize(format_desc_, channel_index_);
\r
76 executor_.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 safe_ptr<frame_consumer>& consumer)
\r
85 add(consumer->index(), consumer);
\r
88 void remove(int index)
\r
90 auto consumer = executor_.invoke([&]() -> std::shared_ptr<frame_consumer>
\r
92 auto consumer = frame_consumer::empty();
\r
93 auto it = consumers_.find(index);
\r
94 if(it != consumers_.end())
\r
96 consumer = it->second;
\r
97 consumers_.erase(it);
\r
100 }, task_priority::high_priority);
\r
102 // Destroy consumer on calling thread:
\r
105 auto str = consumer->print();
\r
107 CASPAR_LOG(info) << print() << L" " << str << L" Removed.";
\r
111 void remove(const safe_ptr<frame_consumer>& consumer)
\r
113 remove(consumer->index());
\r
116 void set_video_format_desc(const video_format_desc& format_desc)
\r
118 executor_.invoke([&]
\r
120 auto it = consumers_.begin();
\r
121 while(it != consumers_.end())
\r
125 it->second->initialize(format_desc, channel_index_);
\r
130 CASPAR_LOG_CURRENT_EXCEPTION();
\r
131 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
132 consumers_.erase(it++);
\r
136 format_desc_ = format_desc;
\r
141 std::pair<int, int> minmax_buffer_depth() const
\r
143 if(consumers_.empty())
\r
144 return std::make_pair(0, 0);
\r
146 auto buffer_depths = consumers_ |
\r
147 boost::adaptors::map_values | // std::function is MSVC workaround
\r
148 boost::adaptors::transformed(std::function<int(const safe_ptr<frame_consumer>&)>([](const safe_ptr<frame_consumer>& x){return x->buffer_depth();}));
\r
151 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));
\r
154 bool has_synchronization_clock() const
\r
156 return boost::range::count_if(consumers_ | boost::adaptors::map_values, [](const safe_ptr<frame_consumer>& x){return x->has_synchronization_clock();}) > 0;
\r
159 void operator()(safe_ptr<const data_frame> input_frame, const video_format_desc& format_desc)
\r
161 executor_.invoke([=]
\r
165 if(!has_synchronization_clock())
\r
166 sync_timer_.tick(1.0/format_desc_.fps);
\r
168 if(format_desc_ != format_desc)
\r
169 set_video_format_desc(format_desc);
\r
171 if(input_frame->image_data().size() != format_desc_.size)
\r
173 sync_timer_.tick(1.0/format_desc_.fps);
\r
177 auto minmax = minmax_buffer_depth();
\r
179 frames_.set_capacity(std::max(1, minmax.second - minmax.first) + 1); // std::max(1, x) since we want to guarantee some pipeline depth for asycnhronous mixer read-back.
\r
180 frames_.push_back(input_frame);
\r
182 if(!frames_.full())
\r
185 auto it = consumers_.begin();
\r
186 while(it != consumers_.end())
\r
188 auto consumer = it->second;
\r
189 auto frame = frames_.at(consumer->buffer_depth()-minmax.first);
\r
193 if(consumer->send(frame))
\r
197 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
198 consumers_.erase(it++);
\r
203 CASPAR_LOG_CURRENT_EXCEPTION();
\r
206 consumer->initialize(format_desc_, channel_index_);
\r
207 if(consumer->send(frame))
\r
211 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
212 consumers_.erase(it++);
\r
217 CASPAR_LOG_CURRENT_EXCEPTION();
\r
218 CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";
\r
219 consumers_.erase(it++);
\r
226 CASPAR_LOG_CURRENT_EXCEPTION();
\r
231 std::wstring print() const
\r
233 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
\r
236 boost::unique_future<boost::property_tree::wptree> info()
\r
238 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
\r
240 boost::property_tree::wptree info;
\r
241 BOOST_FOREACH(auto& consumer, consumers_)
\r
243 info.add_child(L"consumers.consumer", consumer.second->info())
\r
244 .add(L"index", consumer.first);
\r
247 }, task_priority::high_priority));
\r
251 output::output(const video_format_desc& format_desc, int channel_index) : impl_(new impl(format_desc, channel_index)){}
\r
252 void output::add(int index, const safe_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}
\r
253 void output::add(const safe_ptr<frame_consumer>& consumer){impl_->add(consumer);}
\r
254 void output::remove(int index){impl_->remove(index);}
\r
255 void output::remove(const safe_ptr<frame_consumer>& consumer){impl_->remove(consumer);}
\r
256 boost::unique_future<boost::property_tree::wptree> output::info() const{return impl_->info();}
\r
257 void output::operator()(safe_ptr<const data_frame> frame, const video_format_desc& format_desc){(*impl_)(std::move(frame), format_desc);}
\r