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