]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
2.0.2: Refactoring. AMCP commands now executed asynchronously when possible, which...
[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 "frame/basic_frame.h"\r
28 #include "frame/frame_factory.h"\r
29 \r
30 #include <common/concurrency/executor.h>\r
31 \r
32 #include <boost/foreach.hpp>\r
33 #include <boost/timer.hpp>\r
34 \r
35 #include <tbb/parallel_for_each.h>\r
36 \r
37 #include <map>\r
38 \r
39 namespace caspar { namespace core {\r
40 \r
41 struct stage::implementation : public std::enable_shared_from_this<implementation>\r
42                                                          , boost::noncopyable\r
43 {               \r
44         safe_ptr<diagnostics::graph>    graph_;\r
45         safe_ptr<stage::target_t>               target_;\r
46         video_format_desc                               format_desc_;\r
47 \r
48         boost::timer                                    produce_timer_;\r
49         boost::timer                                    tick_timer_;\r
50 \r
51         std::map<int, layer>                    layers_;        \r
52 \r
53         executor                                                executor_;\r
54 public:\r
55         implementation(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<stage::target_t>& target, const video_format_desc& format_desc)  \r
56                 : graph_(graph)\r
57                 , format_desc_(format_desc)\r
58                 , target_(target)\r
59                 , executor_(L"stage")\r
60         {\r
61                 graph_->add_guide("tick-time", 0.5f);   \r
62                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
63                 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));\r
64         }\r
65 \r
66         void spawn_token()\r
67         {\r
68                 std::weak_ptr<implementation> self = shared_from_this();\r
69                 executor_.begin_invoke([=]{tick(self);});\r
70         }\r
71                                                         \r
72         void tick(const std::weak_ptr<implementation>& self)\r
73         {               \r
74                 try\r
75                 {\r
76                         produce_timer_.restart();\r
77 \r
78                         std::map<int, safe_ptr<basic_frame>> frames;\r
79                 \r
80                         BOOST_FOREACH(auto& layer, layers_)                     \r
81                                 frames[layer.first] = basic_frame::empty();     \r
82 \r
83                         tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer) \r
84                         {\r
85                                 frames[layer.first] = layer.second.receive();   \r
86                         });\r
87                         \r
88                         graph_->update_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);\r
89                         \r
90                         std::shared_ptr<void> ticket(nullptr, [self](void*)\r
91                         {\r
92                                 auto self2 = self.lock();\r
93                                 if(self2)                               \r
94                                         self2->executor_.begin_invoke([=]{tick(self);});                                \r
95                         });\r
96 \r
97                         target_->send(std::make_pair(frames, ticket));\r
98 \r
99                         graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
100                         tick_timer_.restart();\r
101                 }\r
102                 catch(...)\r
103                 {\r
104                         layers_.clear();\r
105                         CASPAR_LOG_CURRENT_EXCEPTION();\r
106                 }               \r
107         }\r
108 \r
109         void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)\r
110         {\r
111                 executor_.begin_invoke([=]\r
112                 {\r
113                         layers_[index].load(producer, preview, auto_play_delta);\r
114                 }, high_priority);\r
115         }\r
116 \r
117         void pause(int index)\r
118         {               \r
119                 executor_.begin_invoke([=]\r
120                 {\r
121                         layers_[index].pause();\r
122                 }, high_priority);\r
123         }\r
124 \r
125         void play(int index)\r
126         {               \r
127                 executor_.begin_invoke([=]\r
128                 {\r
129                         layers_[index].play();\r
130                 }, high_priority);\r
131         }\r
132 \r
133         void stop(int index)\r
134         {               \r
135                 executor_.begin_invoke([=]\r
136                 {\r
137                         layers_[index].stop();\r
138                 }, high_priority);\r
139         }\r
140 \r
141         void clear(int index)\r
142         {\r
143                 executor_.begin_invoke([=]\r
144                 {\r
145                         layers_.erase(index);\r
146                 }, high_priority);\r
147         }\r
148                 \r
149         void clear()\r
150         {\r
151                 executor_.begin_invoke([=]\r
152                 {\r
153                         layers_.clear();\r
154                 }, high_priority);\r
155         }       \r
156         \r
157         boost::unique_future<std::wstring> call(int index, bool foreground, const std::wstring& param)\r
158         {\r
159                 return std::move(*executor_.invoke([=]\r
160                 {\r
161                         return std::make_shared<boost::unique_future<std::wstring>>(std::move(layers_[index].call(foreground, param)));\r
162                 }, high_priority));\r
163         }\r
164         \r
165         void swap_layers(const safe_ptr<stage>& other)\r
166         {\r
167                 if(other->impl_.get() == this)\r
168                         return;\r
169                 \r
170                 auto func = [=]\r
171                 {\r
172                         std::swap(layers_, other->impl_->layers_);\r
173                 };              \r
174                 executor_.begin_invoke([=]\r
175                 {\r
176                         other->impl_->executor_.invoke(func, high_priority);\r
177                 }, high_priority);\r
178         }\r
179 \r
180         void swap_layer(int index, size_t other_index)\r
181         {\r
182                 executor_.begin_invoke([=]\r
183                 {\r
184                         std::swap(layers_[index], layers_[other_index]);\r
185                 }, high_priority);\r
186         }\r
187 \r
188         void swap_layer(int index, size_t other_index, const safe_ptr<stage>& other)\r
189         {\r
190                 if(other->impl_.get() == this)\r
191                         swap_layer(index, other_index);\r
192                 else\r
193                 {\r
194                         auto func = [=]\r
195                         {\r
196                                 std::swap(layers_[index], other->impl_->layers_[other_index]);\r
197                         };              \r
198                         executor_.begin_invoke([=]\r
199                         {\r
200                                 other->impl_->executor_.invoke(func, high_priority);\r
201                         }, high_priority);\r
202                 }\r
203         }\r
204 \r
205         boost::unique_future<layer_status> get_status(int index)\r
206         {               \r
207                 return executor_.begin_invoke([=]\r
208                 {\r
209                         return layers_[index].status();\r
210                 }, high_priority );\r
211         }\r
212         \r
213         boost::unique_future<safe_ptr<frame_producer>> foreground(int index)\r
214         {\r
215                 return executor_.begin_invoke([=]\r
216                 {\r
217                         return layers_[index].foreground();\r
218                 }, high_priority);\r
219         }\r
220         \r
221         boost::unique_future<safe_ptr<frame_producer>> background(int index)\r
222         {\r
223                 return executor_.begin_invoke([=]\r
224                 {\r
225                         return layers_[index].background();\r
226                 }, high_priority);\r
227         }\r
228         \r
229         void set_video_format_desc(const video_format_desc& format_desc)\r
230         {\r
231                 executor_.begin_invoke([=]\r
232                 {\r
233                         format_desc_ = format_desc;\r
234                 }, high_priority );\r
235         }\r
236 };\r
237 \r
238 stage::stage(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<target_t>& target, const video_format_desc& format_desc) : impl_(new implementation(graph, target, format_desc)){}\r
239 void stage::spawn_token(){impl_->spawn_token();}\r
240 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
241 void stage::pause(int index){impl_->pause(index);}\r
242 void stage::play(int index){impl_->play(index);}\r
243 void stage::stop(int index){impl_->stop(index);}\r
244 void stage::clear(int index){impl_->clear(index);}\r
245 void stage::clear(){impl_->clear();}\r
246 void stage::swap_layers(const safe_ptr<stage>& other){impl_->swap_layers(other);}\r
247 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
248 void stage::swap_layer(int index, size_t other_index, const safe_ptr<stage>& other){impl_->swap_layer(index, other_index, other);}\r
249 boost::unique_future<layer_status> stage::get_status(int index){return impl_->get_status(index);}\r
250 boost::unique_future<safe_ptr<frame_producer>> stage::foreground(size_t index) {return impl_->foreground(index);}\r
251 boost::unique_future<safe_ptr<frame_producer>> stage::background(size_t index) {return impl_->background(index);}\r
252 boost::unique_future<std::wstring> stage::call(int index, bool foreground, const std::wstring& param){return impl_->call(index, foreground, param);}\r
253 void stage::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
254 }}