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