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
38 #include <concrt_extras.h>
\r
40 using namespace Concurrency;
\r
42 namespace caspar { namespace core {
\r
44 struct destruction_context
\r
46 std::shared_ptr<frame_consumer> consumer;
\r
47 Concurrency::event event;
\r
49 destruction_context(std::shared_ptr<frame_consumer>&& consumer)
\r
50 : consumer(consumer)
\r
55 void __cdecl destroy_consumer(LPVOID lpParam)
\r
57 auto destruction = std::unique_ptr<destruction_context>(static_cast<destruction_context*>(lpParam));
\r
61 if(destruction->consumer.unique())
\r
63 Concurrency::scoped_oversubcription_token oversubscribe;
\r
64 destruction->consumer.reset();
\r
67 CASPAR_LOG(warning) << destruction->consumer->print() << " Not destroyed asynchronously.";
\r
71 CASPAR_LOG_CURRENT_EXCEPTION();
\r
74 destruction->event.set();
\r
77 void __cdecl destroy_and_wait_consumer(LPVOID lpParam)
\r
81 auto destruction = static_cast<destruction_context*>(lpParam);
\r
82 Concurrency::CurrentScheduler::ScheduleTask(destroy_consumer, lpParam);
\r
83 if(destruction->event.wait(1000) == Concurrency::COOPERATIVE_WAIT_TIMEOUT)
\r
84 CASPAR_LOG(warning) << " Potential destruction deadlock detected. Might leak resources.";
\r
88 CASPAR_LOG_CURRENT_EXCEPTION();
\r
92 struct output::implementation
\r
94 typedef std::pair<safe_ptr<read_frame>, safe_ptr<read_frame>> fill_and_key;
\r
96 const video_format_desc format_desc_;
\r
98 std::map<int, safe_ptr<frame_consumer>> consumers_;
\r
99 typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;
\r
101 high_prec_timer timer_;
\r
103 critical_section mutex_;
\r
104 call<safe_ptr<message<safe_ptr<read_frame>>>> output_;
\r
107 implementation(output::source_t& source, const video_format_desc& format_desc)
\r
108 : format_desc_(format_desc)
\r
109 , output_(std::bind(&implementation::execute, this, std::placeholders::_1))
\r
111 source.link_target(&output_);
\r
114 void add(int index, safe_ptr<frame_consumer>&& consumer)
\r
117 critical_section::scoped_lock lock(mutex_);
\r
118 consumers_.erase(index);
\r
121 consumer->initialize(format_desc_);
\r
124 critical_section::scoped_lock lock(mutex_);
\r
125 consumers_.insert(std::make_pair(index, consumer));
\r
127 CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";
\r
131 void remove(int index)
\r
133 critical_section::scoped_lock lock(mutex_);
\r
134 auto it = consumers_.find(index);
\r
135 if(it != consumers_.end())
\r
137 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
138 consumers_.erase(it);
\r
142 void execute(const safe_ptr<message<safe_ptr<read_frame>>>& msg)
\r
144 auto frame = msg->value();
\r
146 critical_section::scoped_lock lock(mutex_);
\r
148 if(!has_synchronization_clock() || frame->image_size() != format_desc_.size)
\r
150 scoped_oversubcription_token oversubscribe;
\r
151 timer_.tick(1.0/format_desc_.fps);
\r
154 std::vector<int> removables;
\r
155 Concurrency::parallel_for_each(consumers_.begin(), consumers_.end(), [&](const decltype(*consumers_.begin())& pair)
\r
159 if(!pair.second->send(frame))
\r
160 removables.push_back(pair.first);
\r
164 CASPAR_LOG_CURRENT_EXCEPTION();
\r
165 CASPAR_LOG(error) << "Consumer error. Trying to recover:" << pair.second->print();
\r
168 pair.second->initialize(format_desc_);
\r
169 pair.second->send(frame);
\r
173 removables.push_back(pair.first);
\r
174 CASPAR_LOG_CURRENT_EXCEPTION();
\r
175 CASPAR_LOG(error) << "Failed to recover consumer: " << pair.second->print() << L". Removing it.";
\r
180 BOOST_FOREACH(auto& removable, removables)
\r
182 std::shared_ptr<frame_consumer> consumer = consumers_.find(removable)->second;
\r
183 consumers_.erase(removable);
\r
184 Concurrency::CurrentScheduler::ScheduleTask(destroy_consumer, new destruction_context(std::move(consumer)));
\r
190 bool has_synchronization_clock()
\r
192 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)
\r
194 return p.second->has_synchronization_clock();
\r
198 std::wstring print() const
\r
204 output::output(output::source_t& source, const video_format_desc& format_desc) : impl_(new implementation(source, format_desc)){}
\r
205 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}
\r
206 void output::remove(int index){impl_->remove(index);}
\r