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/utility/assert.h>
\r
34 #include <common/utility/timer.h>
\r
36 #include <concrt_extras.h>
\r
38 using namespace Concurrency;
\r
40 namespace caspar { namespace core {
\r
42 struct output::implementation
\r
44 typedef std::pair<safe_ptr<read_frame>, safe_ptr<read_frame>> fill_and_key;
\r
46 const video_format_desc format_desc_;
\r
48 std::map<int, safe_ptr<frame_consumer>> consumers_;
\r
49 typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;
\r
51 high_prec_timer timer_;
\r
53 critical_section mutex_;
\r
54 call<output::source_element_t> output_;
\r
56 boost::circular_buffer<safe_ptr<read_frame>> frames_;
\r
59 implementation(output::source_t& source, const video_format_desc& format_desc)
\r
60 : format_desc_(format_desc)
\r
61 , output_(std::bind(&implementation::execute, this, std::placeholders::_1))
\r
63 source.link_target(&output_);
\r
66 void add(int index, safe_ptr<frame_consumer>&& consumer)
\r
68 // C-TODO: Consumers are destroyed asynchronously, need to wait for destruction to finish before callign initialize.
\r
71 consumer->initialize(format_desc_);
\r
74 critical_section::scoped_lock lock(mutex_);
\r
76 consumers_.insert(std::make_pair(index, consumer));
\r
78 CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";
\r
82 void remove(int index)
\r
85 critical_section::scoped_lock lock(mutex_);
\r
87 auto it = consumers_.find(index);
\r
88 if(it != consumers_.end())
\r
90 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
91 consumers_.erase(it);
\r
96 std::pair<size_t, size_t> minmax_buffer_depth() const
\r
98 if(consumers_.empty())
\r
99 return std::make_pair(0, 0);
\r
100 std::vector<size_t> buffer_depths;
\r
101 std::transform(consumers_.begin(), consumers_.end(), std::back_inserter(buffer_depths), [](const decltype(*consumers_.begin())& pair)
\r
103 return pair.second->buffer_depth();
\r
105 std::sort(buffer_depths.begin(), buffer_depths.end());
\r
106 auto min = buffer_depths.front();
\r
107 auto max = buffer_depths.back();
\r
108 return std::make_pair(min, max);
\r
111 void execute(const output::source_element_t& element)
\r
113 auto frame = element.first;
\r
116 critical_section::scoped_lock lock(mutex_);
\r
118 if(!has_synchronization_clock() || frame->image_size() != format_desc_.size)
\r
120 scoped_oversubcription_token oversubscribe;
\r
121 timer_.tick(1.0/format_desc_.fps);
\r
124 auto minmax = minmax_buffer_depth();
\r
126 frames_.set_capacity(minmax.second - minmax.first + 1);
\r
127 frames_.push_back(frame);
\r
129 if(!frames_.full())
\r
132 std::vector<int> removables;
\r
133 Concurrency::parallel_for_each(consumers_.begin(), consumers_.end(), [&](const decltype(*consumers_.begin())& pair)
\r
137 auto consumer = pair.second;
\r
139 if(!consumer->send(frames_.at(consumer->buffer_depth()-minmax.first)))
\r
140 removables.push_back(pair.first);
\r
144 CASPAR_LOG_CURRENT_EXCEPTION();
\r
145 CASPAR_LOG(error) << "Consumer error. Trying to recover:" << pair.second->print();
\r
148 pair.second->initialize(format_desc_);
\r
149 pair.second->send(frame);
\r
153 removables.push_back(pair.first);
\r
154 CASPAR_LOG_CURRENT_EXCEPTION();
\r
155 CASPAR_LOG(error) << "Failed to recover consumer: " << pair.second->print() << L". Removing it.";
\r
160 BOOST_FOREACH(auto& removable, removables)
\r
161 consumers_.erase(removable);
\r
167 bool has_synchronization_clock()
\r
169 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)
\r
171 return p.second->has_synchronization_clock();
\r
175 std::wstring print() const
\r
181 output::output(output::source_t& source, const video_format_desc& format_desc) : impl_(new implementation(source, format_desc)){}
\r
182 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}
\r
183 void output::remove(int index){impl_->remove(index);}
\r