]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
2.0.2: Intergrated channel-exp branch:
[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 #include <common/concurrency/governor.h>\r
32 #include <common/env.h>\r
33 \r
34 #include <boost/foreach.hpp>\r
35 #include <boost/timer.hpp>\r
36 \r
37 #include <tbb/parallel_for_each.h>\r
38 \r
39 #include <map>\r
40 \r
41 namespace caspar { namespace core {\r
42 \r
43 struct stage::implementation : boost::noncopyable\r
44 {               \r
45         safe_ptr<diagnostics::graph>    graph_;\r
46         safe_ptr<stage::target_t>               target_;\r
47         video_format_desc                               format_desc_;\r
48 \r
49         boost::timer                                    produce_timer_;\r
50         boost::timer                                    tick_timer_;\r
51 \r
52         std::map<int, layer>                    layers_;        \r
53 \r
54         governor                                                governor_;\r
55         executor                                                executor_;\r
56 public:\r
57         implementation(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<stage::target_t>& target, const video_format_desc& format_desc)  \r
58                 : graph_(graph)\r
59                 , format_desc_(format_desc)\r
60                 , target_(target)\r
61                 , executor_(L"stage")\r
62                 , governor_(std::max(1, env::properties().get("configuration.pipeline-tokens", 2)))\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                 executor_.begin_invoke([this]{tick();});\r
69         }\r
70 \r
71         ~implementation()\r
72         {\r
73                 governor_.cancel();\r
74         }\r
75                                                 \r
76         void tick()\r
77         {               \r
78                 try\r
79                 {\r
80                         auto ticket = governor_.acquire();\r
81 \r
82                         produce_timer_.restart();\r
83 \r
84                         std::map<int, safe_ptr<basic_frame>> frames;\r
85                 \r
86                         BOOST_FOREACH(auto& layer, layers_)                     \r
87                                 frames[layer.first] = basic_frame::empty();     \r
88 \r
89                         tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer) \r
90                         {\r
91                                 frames[layer.first] = layer.second.receive();   \r
92                         });\r
93                         \r
94                         graph_->update_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);\r
95                         \r
96                         target_->send(std::make_pair(frames, ticket));\r
97 \r
98                         graph_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
99                         tick_timer_.restart();\r
100                 }\r
101                 catch(...)\r
102                 {\r
103                         layers_.clear();\r
104                         CASPAR_LOG_CURRENT_EXCEPTION();\r
105                 }               \r
106                 executor_.begin_invoke([this]{tick();});\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_.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_.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_.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_.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_.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_.invoke([&]\r
152                 {\r
153                         layers_.clear();\r
154                 }, high_priority);\r
155         }       \r
156         \r
157         void swap_layer(int index, size_t other_index)\r
158         {\r
159                 executor_.invoke([&]\r
160                 {\r
161                         std::swap(layers_[index], layers_[other_index]);\r
162                 }, high_priority);\r
163         }\r
164 \r
165         void swap_layer(int index, size_t other_index, stage& other)\r
166         {\r
167                 if(other.impl_.get() == this)\r
168                         swap_layer(index, other_index);\r
169                 else\r
170                 {\r
171                         auto func = [&]\r
172                         {\r
173                                 std::swap(layers_[index], other.impl_->layers_[other_index]);\r
174                         };              \r
175                         executor_.invoke([&]{other.impl_->executor_.invoke(func, high_priority);}, high_priority);\r
176                 }\r
177         }\r
178 \r
179         void swap(stage& other)\r
180         {\r
181                 if(other.impl_.get() == this)\r
182                         return;\r
183                 \r
184                 auto func = [&]\r
185                 {\r
186                         std::swap(layers_, other.impl_->layers_);\r
187                 };              \r
188                 executor_.invoke([&]{other.impl_->executor_.invoke(func, high_priority);}, high_priority);\r
189         }\r
190 \r
191         layer_status get_status(int index)\r
192         {               \r
193                 return executor_.invoke([&]\r
194                 {\r
195                         return layers_[index].status();\r
196                 }, high_priority );\r
197         }\r
198         \r
199         safe_ptr<frame_producer> foreground(int index)\r
200         {\r
201                 return executor_.invoke([=]{return layers_[index].foreground();}, high_priority);\r
202         }\r
203         \r
204         safe_ptr<frame_producer> background(int index)\r
205         {\r
206                 return executor_.invoke([=]{return layers_[index].background();}, high_priority);\r
207         }\r
208         \r
209         void set_video_format_desc(const video_format_desc& format_desc)\r
210         {\r
211                 executor_.begin_invoke([=]\r
212                 {\r
213                         format_desc_ = format_desc;\r
214                 }, high_priority );\r
215         }\r
216 };\r
217 \r
218 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
219 void stage::swap(stage& other){impl_->swap(other);}\r
220 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
221 void stage::pause(int index){impl_->pause(index);}\r
222 void stage::play(int index){impl_->play(index);}\r
223 void stage::stop(int index){impl_->stop(index);}\r
224 void stage::clear(int index){impl_->clear(index);}\r
225 void stage::clear(){impl_->clear();}\r
226 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}\r
227 void stage::swap_layer(int index, size_t other_index, stage& other){impl_->swap_layer(index, other_index, other);}\r
228 layer_status stage::get_status(int index){return impl_->get_status(index);}\r
229 safe_ptr<frame_producer> stage::foreground(size_t index) {return impl_->foreground(index);}\r
230 safe_ptr<frame_producer> stage::background(size_t index) {return impl_->background(index);}\r
231 void stage::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
232 }}