]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / producer / stage.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\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
10 *\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
15 \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
18 *\r
19 */\r
20 \r
21 #include "../StdAfx.h"\r
22 \r
23 #include "stage.h"\r
24 \r
25 #include "layer.h"\r
26 \r
27 #include <core/producer/frame/basic_frame.h>\r
28 #include <core/producer/frame/frame_factory.h>\r
29 \r
30 #include <common/concurrency/executor.h>\r
31 #include <common/utility/move_on_copy.h>\r
32 \r
33 #include <boost/foreach.hpp>\r
34 \r
35 #include <agents.h>\r
36 #include <ppl.h>\r
37 \r
38 #include <map>\r
39 #include <set>\r
40 \r
41 using namespace Concurrency;\r
42 \r
43 namespace caspar { namespace core {\r
44 \r
45 struct stage::implementation : public agent, public boost::noncopyable\r
46 {               \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
52 public:\r
53         implementation(stage::target_t& target, const safe_ptr<semaphore>& semaphore)  \r
54                 : target_(target)\r
55                 , semaphore_(semaphore)\r
56         {\r
57                 start();\r
58         }\r
59 \r
60         ~implementation()\r
61         {\r
62                 send(is_running_, false);\r
63                 semaphore_->release();\r
64                 agent::wait(this);\r
65         }\r
66 \r
67         virtual void run()\r
68         {\r
69                 send(is_running_, true);\r
70                 while(is_running_.value())\r
71                 {\r
72                         try\r
73                         {\r
74                                 std::map<int, safe_ptr<basic_frame>> frames;\r
75                                 {\r
76                                         critical_section::scoped_lock lock(mutex_);\r
77                 \r
78                                         BOOST_FOREACH(auto& layer, layers_)                     \r
79                                                 frames[layer.first] = basic_frame::empty();     \r
80 \r
81                                         Concurrency::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer) \r
82                                         {\r
83                                                 frames[layer.first] = layer.second.receive();   \r
84                                         });\r
85                                 }\r
86 \r
87                                 send(target_, make_safe<message<decltype(frames)>>(frames, token(semaphore_)));\r
88                         }\r
89                         catch(...)\r
90                         {\r
91                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
92                                 Concurrency::wait(20);\r
93                         }       \r
94                 }\r
95 \r
96                 send(is_running_, false);\r
97                 done();\r
98         }\r
99                                 \r
100         void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
101         {\r
102                 {\r
103                         critical_section::scoped_lock lock(mutex_);\r
104                         \r
105                         layers_[index].load(producer, preview, auto_play_delta);\r
106                 }\r
107         }\r
108 \r
109         void pause(int index)\r
110         {               \r
111                 {\r
112                         critical_section::scoped_lock lock(mutex_);\r
113                         \r
114                         layers_[index].pause();\r
115                 }\r
116         }\r
117 \r
118         void play(int index)\r
119         {               \r
120                 {\r
121                         critical_section::scoped_lock lock(mutex_);\r
122                         \r
123                         layers_[index].play();\r
124                 }\r
125         }\r
126 \r
127         void stop(int index)\r
128         {               \r
129                 {\r
130                         critical_section::scoped_lock lock(mutex_);\r
131                         \r
132                         layers_[index].stop();\r
133                 }\r
134         }\r
135 \r
136         void clear(int index)\r
137         {\r
138                 {\r
139                         critical_section::scoped_lock lock(mutex_);\r
140                         \r
141                         layers_.erase(index);\r
142                 }\r
143         }\r
144                 \r
145         void clear()\r
146         {\r
147                 {\r
148                         critical_section::scoped_lock lock(mutex_);\r
149                         \r
150                         layers_.clear();\r
151                 }\r
152         }       \r
153         \r
154         void swap_layer(int index, size_t other_index)\r
155         {\r
156                 {\r
157                         critical_section::scoped_lock lock(mutex_);\r
158                         \r
159                         std::swap(layers_[index], layers_[other_index]);\r
160                 }\r
161         }\r
162 \r
163         void swap_layer(int index, size_t other_index, stage& other)\r
164         {\r
165                 if(other.impl_.get() == this)\r
166                         swap_layer(index, other_index);\r
167                 else\r
168                 {                       \r
169                         {\r
170                                 critical_section::scoped_lock lock1(mutex_);\r
171                                 critical_section::scoped_lock lock2(other.impl_->mutex_);\r
172 \r
173                                 std::swap(layers_[index], other.impl_->layers_[other_index]);\r
174                         }\r
175                 }\r
176         }\r
177 \r
178         void swap(stage& other)\r
179         {\r
180                 if(other.impl_.get() == this)\r
181                         return;\r
182                 \r
183                 {\r
184                         critical_section::scoped_lock lock1(mutex_);\r
185                         critical_section::scoped_lock lock2(other.impl_->mutex_);\r
186                         \r
187                         std::swap(layers_, other.impl_->layers_);\r
188                 }\r
189         }\r
190 \r
191         layer_status get_status(int index)\r
192         {               \r
193                 {\r
194                         critical_section::scoped_lock lock(mutex_);\r
195                         \r
196                         return layers_[index].status();\r
197                 }\r
198         }\r
199         \r
200         safe_ptr<frame_producer> foreground(int index)\r
201         {\r
202                 {\r
203                         critical_section::scoped_lock lock(mutex_);\r
204                         \r
205                         return layers_[index].foreground();\r
206                 }\r
207         }\r
208         \r
209         safe_ptr<frame_producer> background(int index)\r
210         {\r
211                 {\r
212                         critical_section::scoped_lock lock(mutex_);\r
213                         \r
214                         return layers_[index].background();\r
215                 }\r
216         }\r
217 \r
218         std::wstring print() const\r
219         {\r
220                 // C-TODO\r
221                 return L"stage []";// + boost::lexical_cast<std::wstring>(channel_.index()) + L"]";\r
222         }\r
223 \r
224 };\r
225 \r
226 stage::stage(target_t& target, const safe_ptr<semaphore>& semaphore) : impl_(new implementation(target, semaphore)){}\r
227 void stage::swap(stage& other){impl_->swap(other);}\r
228 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
229 void stage::pause(int index){impl_->pause(index);}\r
230 void stage::play(int index){impl_->play(index);}\r
231 void stage::stop(int index){impl_->stop(index);}\r
232 void stage::clear(int index){impl_->clear(index);}\r
233 void stage::clear(){impl_->clear();}\r
234 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
235 void stage::swap_layer(int index, size_t other_index, stage& other){impl_->swap_layer(index, other_index, other);}\r
236 layer_status stage::get_status(int index){return impl_->get_status(index);}\r
237 safe_ptr<frame_producer> stage::foreground(size_t index) {return impl_->foreground(index);}\r
238 safe_ptr<frame_producer> stage::background(size_t index) {return impl_->background(index);}\r
239 }}