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
21 #include "../StdAfx.h"
\r
25 #include "../video_channel_context.h"
\r
29 #include <core/producer/frame/basic_frame.h>
\r
30 #include <core/producer/frame/frame_factory.h>
\r
32 #include <common/concurrency/executor.h>
\r
34 #include <boost/range/algorithm.hpp>
\r
36 #include <tbb/parallel_for.h>
\r
40 namespace caspar { namespace core {
\r
42 void destroy_producer(safe_ptr<frame_producer>& producer)
\r
44 if(!producer.unique())
\r
45 CASPAR_LOG(warning) << producer->print() << L" Not destroyed on safe asynchronous destruction thread.";
\r
47 producer = frame_producer::empty();
\r
50 class destroy_producer_proxy : public frame_producer
\r
52 safe_ptr<frame_producer> producer_;
\r
53 executor& destroy_context_;
\r
55 destroy_producer_proxy(executor& destroy_context, const safe_ptr<frame_producer>& producer)
\r
56 : producer_(producer)
\r
57 , destroy_context_(destroy_context){}
\r
59 ~destroy_producer_proxy()
\r
61 if(destroy_context_.size() > 4)
\r
62 CASPAR_LOG(error) << L" Potential destroyer deadlock.";
\r
64 destroy_context_.begin_invoke(std::bind(&destroy_producer, std::move(producer_)));
\r
67 virtual safe_ptr<basic_frame> receive() {return core::receive(producer_);}
\r
68 virtual std::wstring print() const {return producer_->print();}
\r
69 virtual void param(const std::wstring& str) {producer_->param(str);}
\r
70 virtual safe_ptr<frame_producer> get_following_producer() const {return producer_->get_following_producer();}
\r
71 virtual void set_leading_producer(const safe_ptr<frame_producer>& producer) {producer_->set_leading_producer(producer);}
\r
74 struct stage::implementation : boost::noncopyable
\r
76 std::map<int, layer> layers_;
\r
77 typedef std::map<int, layer>::value_type layer_t;
\r
79 video_channel_context& channel_;
\r
81 implementation(video_channel_context& video_channel)
\r
82 : channel_(video_channel)
\r
86 std::map<int, safe_ptr<basic_frame>> execute()
\r
88 std::map<int, safe_ptr<basic_frame>> frames;
\r
92 // Allocate placeholders.
\r
93 BOOST_FOREACH(auto layer, layers_)
\r
94 frames[layer.first] = basic_frame::empty();
\r
97 tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](layer_t& layer)
\r
99 frames[layer.first] = layer.second.receive();
\r
104 CASPAR_LOG_CURRENT_EXCEPTION();
\r
110 void load(int index, const safe_ptr<frame_producer>& producer, bool preview)
\r
112 channel_.execution().invoke([&]
\r
114 layers_[index].load(make_safe<destroy_producer_proxy>(channel_.destruction(), producer), preview);
\r
115 CASPAR_LOG(info) << print() << " layer [ " << index << "] Loaded: " << producer << ".";
\r
119 void pause(int index)
\r
121 channel_.execution().invoke([&]
\r
123 layers_[index].pause();
\r
124 CASPAR_LOG(info) << print() << " layer [ " << index << "] Paused.";
\r
128 void play(int index)
\r
130 channel_.execution().invoke([&]
\r
132 layers_[index].play();
\r
133 CASPAR_LOG(info) << print() << " layer [ " << index << "] Playing.";
\r
137 void stop(int index)
\r
139 channel_.execution().invoke([&]
\r
141 layers_[index].stop();
\r
142 CASPAR_LOG(info) << print() << " layer [ " << index << "] Stopped.";
\r
146 void clear(int index)
\r
148 channel_.execution().invoke([&]
\r
150 layers_.erase(index);
\r
151 CASPAR_LOG(info) << print() << " layer [ " << index << "] Cleared.";
\r
157 channel_.execution().invoke([&]
\r
160 CASPAR_LOG(info) << print() << " Cleared.";
\r
164 void swap_layer(int index, size_t other_index)
\r
166 channel_.execution().invoke([&]
\r
168 layers_[index].swap(layers_[other_index]);
\r
172 void swap_layer(int index, size_t other_index, stage& other)
\r
174 if(other.impl_.get() == this)
\r
175 swap_layer(index, other_index);
\r
178 if(channel_.get_format_desc() != other.impl_->channel_.get_format_desc())
\r
179 BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between channels with different formats."));
\r
181 auto func = [&]{layers_[index].swap(other.impl_->layers_[other_index]);};
\r
183 channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func);});
\r
187 void swap(stage& other)
\r
189 if(other.impl_.get() == this)
\r
192 if(channel_.get_format_desc() != other.impl_->channel_.get_format_desc())
\r
193 BOOST_THROW_EXCEPTION(not_supported() << msg_info("Cannot swap between channels with different formats."));
\r
197 auto sel_first = [](const std::pair<int, layer>& pair){return pair.first;};
\r
199 std::set<int> indices;
\r
200 auto inserter = std::inserter(indices, indices.begin());
\r
202 std::transform(layers_.begin(), layers_.end(), inserter, sel_first);
\r
203 std::transform(other.impl_->layers_.begin(), other.impl_->layers_.end(), inserter, sel_first);
\r
205 BOOST_FOREACH(auto index, indices)
\r
206 layers_[index].swap(other.impl_->layers_[index]);
\r
209 channel_.execution().invoke([&]{other.impl_->channel_.execution().invoke(func);});
\r
212 boost::unique_future<safe_ptr<frame_producer>> foreground(int index)
\r
214 return channel_.execution().begin_invoke([=]{return layers_[index].foreground();});
\r
217 boost::unique_future<safe_ptr<frame_producer>> background(int index)
\r
219 return channel_.execution().begin_invoke([=]{return layers_[index].background();});
\r
222 std::wstring print() const
\r
224 return L"stage [" + boost::lexical_cast<std::wstring>(channel_.index()) + L"]";
\r
229 stage::stage(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}
\r
230 void stage::swap(stage& other){impl_->swap(other);}
\r
231 void stage::load(int index, const safe_ptr<frame_producer>& producer, bool preview){impl_->load(index, producer, preview);}
\r
232 void stage::pause(int index){impl_->pause(index);}
\r
233 void stage::play(int index){impl_->play(index);}
\r
234 void stage::stop(int index){impl_->stop(index);}
\r
235 void stage::clear(int index){impl_->clear(index);}
\r
236 void stage::clear(){impl_->clear();}
\r
237 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}
\r
238 void stage::swap_layer(int index, size_t other_index, stage& other){impl_->swap_layer(index, other_index, other);}
\r
239 boost::unique_future<safe_ptr<frame_producer>> stage::foreground(size_t index) {return impl_->foreground(index);}
\r
240 boost::unique_future<safe_ptr<frame_producer>> stage::background(size_t index) {return impl_->background(index);}
\r
241 std::map<int, safe_ptr<basic_frame>> stage::execute(){return impl_->execute();}
\r