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
27 #include <core/producer/frame/basic_frame.h>
\r
28 #include <core/producer/frame/frame_factory.h>
\r
30 #include <common/concurrency/executor.h>
\r
31 #include <common/utility/move_on_copy.h>
\r
33 #include <boost/foreach.hpp>
\r
41 using namespace Concurrency;
\r
43 namespace caspar { namespace core {
\r
45 struct stage::implementation : public agent, public boost::noncopyable
\r
47 overwrite_buffer<bool> is_running_;
\r
48 Concurrency::critical_section mutex_;
\r
49 stage::target_t& target_;
\r
50 std::map<int, layer> layers_;
\r
51 safe_ptr<semaphore> semaphore_;
\r
53 implementation(stage::target_t& target)
\r
55 , semaphore_(make_safe<semaphore>(3))
\r
62 send(is_running_, false);
\r
63 semaphore_->release();
\r
69 send(is_running_, true);
\r
70 while(is_running_.value())
\r
74 std::map<int, safe_ptr<basic_frame>> frames;
\r
76 critical_section::scoped_lock lock(mutex_);
\r
78 BOOST_FOREACH(auto& layer, layers_)
\r
79 frames[layer.first] = basic_frame::empty();
\r
81 Concurrency::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer)
\r
83 frames[layer.first] = layer.second.receive();
\r
87 send(target_, make_safe<message<decltype(frames)>>(frames, token(semaphore_)));
\r
91 CASPAR_LOG_CURRENT_EXCEPTION();
\r
95 send(is_running_, false);
\r
99 void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)
\r
101 critical_section::scoped_lock lock(mutex_);
\r
102 layers_[index].load(create_destroy_producer_proxy(producer), preview, auto_play_delta);
\r
105 void pause(int index)
\r
107 critical_section::scoped_lock lock(mutex_);
\r
108 layers_[index].pause();
\r
111 void play(int index)
\r
113 critical_section::scoped_lock lock(mutex_);
\r
114 layers_[index].play();
\r
117 void stop(int index)
\r
119 critical_section::scoped_lock lock(mutex_);
\r
120 layers_[index].stop();
\r
123 void clear(int index)
\r
125 critical_section::scoped_lock lock(mutex_);
\r
126 layers_.erase(index);
\r
131 critical_section::scoped_lock lock(mutex_);
\r
135 void swap_layer(int index, size_t other_index)
\r
137 critical_section::scoped_lock lock(mutex_);
\r
138 std::swap(layers_[index], layers_[other_index]);
\r
141 void swap_layer(int index, size_t other_index, stage& other)
\r
143 if(other.impl_.get() == this)
\r
144 swap_layer(index, other_index);
\r
147 critical_section::scoped_lock lock1(mutex_);
\r
148 critical_section::scoped_lock lock2(other.impl_->mutex_);
\r
149 std::swap(layers_[index], other.impl_->layers_[other_index]);
\r
153 void swap(stage& other)
\r
155 if(other.impl_.get() == this)
\r
158 critical_section::scoped_lock lock1(mutex_);
\r
159 critical_section::scoped_lock lock2(other.impl_->mutex_);
\r
160 std::swap(layers_, other.impl_->layers_);
\r
163 layer_status get_status(int index)
\r
165 critical_section::scoped_lock lock(mutex_);
\r
166 return layers_[index].status();
\r
169 safe_ptr<frame_producer> foreground(int index)
\r
171 critical_section::scoped_lock lock(mutex_);
\r
172 return layers_[index].foreground();
\r
175 safe_ptr<frame_producer> background(int index)
\r
177 critical_section::scoped_lock lock(mutex_);
\r
178 return layers_[index].background();
\r
181 std::wstring print() const
\r
184 return L"stage []";// + boost::lexical_cast<std::wstring>(channel_.index()) + L"]";
\r
189 stage::stage(target_t& target) : impl_(new implementation(target)){}
\r
190 void stage::swap(stage& other){impl_->swap(other);}
\r
191 void stage::load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta){impl_->load(index, producer, preview, auto_play_delta);}
\r
192 void stage::pause(int index){impl_->pause(index);}
\r
193 void stage::play(int index){impl_->play(index);}
\r
194 void stage::stop(int index){impl_->stop(index);}
\r
195 void stage::clear(int index){impl_->clear(index);}
\r
196 void stage::clear(){impl_->clear();}
\r
197 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}
\r
198 void stage::swap_layer(int index, size_t other_index, stage& other){impl_->swap_layer(index, other_index, other);}
\r
199 layer_status stage::get_status(int index){return impl_->get_status(index);}
\r
200 safe_ptr<frame_producer> stage::foreground(size_t index) {return impl_->foreground(index);}
\r
201 safe_ptr<frame_producer> stage::background(size_t index) {return impl_->background(index);}
\r