]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / core / producer / stage.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../StdAfx.h"
23
24 #include "stage.h"
25
26 #include "layer.h"
27
28 #include "../frame/draw_frame.h"
29 #include "../frame/frame_factory.h"
30
31 #include <common/executor.h>
32 #include <common/future.h>
33 #include <common/diagnostics/graph.h>
34
35 #include <core/frame/frame_transform.h>
36
37 #include <boost/foreach.hpp>
38 #include <boost/timer.hpp>
39 #include <boost/property_tree/ptree.hpp>
40 #include <boost/range/algorithm_ext.hpp>
41
42 #include <tbb/parallel_for_each.h>
43
44 #include <functional>
45 #include <map>
46 #include <vector>
47
48 namespace caspar { namespace core {
49         
50 struct stage::impl : public std::enable_shared_from_this<impl>
51 {                               
52         spl::shared_ptr<diagnostics::graph>                                                     graph_;
53         monitor::basic_subject                                                                          event_subject_;
54         reactive::basic_subject<std::map<int, class draw_frame>>        frames_subject_;
55         std::map<int, layer>                                                                            layers_;        
56         std::map<int, tweened_transform>                                                        tweens_;        
57         executor                                                                                                        executor_;
58 public:
59         impl(spl::shared_ptr<diagnostics::graph> graph) 
60                 : graph_(std::move(graph))
61                 , event_subject_("stage")
62                 , executor_(L"stage")
63         {
64                 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
65         }
66                 
67         std::map<int, draw_frame> operator()(const struct video_format_desc& format_desc)
68         {               
69                 boost::timer frame_timer;
70
71                 auto frames = executor_.invoke([=]() -> std::map<int, draw_frame>
72                 {
73
74                         std::map<int, class draw_frame> frames;
75                         
76                         try
77                         {                       
78                                 std::vector<int> indices;
79
80                                 BOOST_FOREACH(auto& layer, layers_)     
81                                 {
82                                         frames[layer.first] = draw_frame::empty();      
83                                         indices.push_back(layer.first);
84                                 }                               
85
86                                 // WORKAROUND: Compiler doesn't seem to like lambda.
87                                 tbb::parallel_for_each(indices.begin(), indices.end(), std::bind(&stage::impl::draw, this, std::placeholders::_1, std::ref(format_desc), std::ref(frames)));
88                         }
89                         catch(...)
90                         {
91                                 layers_.clear();
92                                 CASPAR_LOG_CURRENT_EXCEPTION();
93                         }       
94                         
95
96                         return frames;
97                 });
98                 
99                 frames_subject_ << frames;
100                 
101                 graph_->set_value("produce-time", frame_timer.elapsed()*format_desc.fps*0.5);
102                 event_subject_ << monitor::event("profiler/time") % frame_timer.elapsed() % (1.0/format_desc.fps);
103
104                 return frames;
105         }
106
107         void draw(int index, const video_format_desc& format_desc, std::map<int, draw_frame>& frames)
108         {
109                 auto& layer             = layers_[index];
110                 auto& tween             = tweens_[index];
111                                 
112                 auto frame  = layer.receive(format_desc);                                       
113                 auto frame1 = frame;
114                 frame1.transform() *= tween.fetch_and_tick(1);
115
116                 if(format_desc.field_mode != core::field_mode::progressive)
117                 {                               
118                         auto frame2 = frame;
119                         frame2.transform() *= tween.fetch_and_tick(1);
120                         frame1 = core::draw_frame::interlace(frame1, frame2, format_desc.field_mode);
121                 }
122
123                 frames[index] = frame1;
124         }
125
126         layer& get_layer(int index)
127         {
128                 auto it = layers_.find(index);
129                 if(it == std::end(layers_))
130                 {
131                         it = layers_.insert(std::make_pair(index, layer(index))).first;
132                         it->second.subscribe(event_subject_);
133                 }
134                 return it->second;
135         }
136                 
137         boost::unique_future<void> apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)
138         {
139                 return executor_.begin_invoke([=]
140                 {
141                         BOOST_FOREACH(auto& transform, transforms)
142                         {
143                                 auto src = tweens_[std::get<0>(transform)].fetch();
144                                 auto dst = std::get<1>(transform)(src);
145                                 tweens_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));
146                         }
147                 }, task_priority::high_priority);
148         }
149                                                 
150         boost::unique_future<void> apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)
151         {
152                 return executor_.begin_invoke([=]
153                 {
154                         auto src = tweens_[index].fetch();
155                         auto dst = transform(src);
156                         tweens_[index] = tweened_transform(src, dst, mix_duration, tween);
157                 }, task_priority::high_priority);
158         }
159
160         boost::unique_future<void> clear_transforms(int index)
161         {
162                 return executor_.begin_invoke([=]
163                 {
164                         tweens_.erase(index);
165                 }, task_priority::high_priority);
166         }
167
168         boost::unique_future<void> clear_transforms()
169         {
170                 return executor_.begin_invoke([=]
171                 {
172                         tweens_.clear();
173                 }, task_priority::high_priority);
174         }
175                 
176         boost::unique_future<void> load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
177         {
178                 return executor_.begin_invoke([=]
179                 {
180                         get_layer(index).load(producer, preview, auto_play_delta);                      
181                 }, task_priority::high_priority);
182         }
183
184         boost::unique_future<void> pause(int index)
185         {               
186                 return executor_.begin_invoke([=]
187                 {
188                         layers_[index].pause();
189                 }, task_priority::high_priority);
190         }
191
192         boost::unique_future<void> play(int index)
193         {               
194                 return executor_.begin_invoke([=]
195                 {
196                         layers_[index].play();
197                 }, task_priority::high_priority);
198         }
199
200         boost::unique_future<void> stop(int index)
201         {               
202                 return executor_.begin_invoke([=]
203                 {
204                         layers_[index].stop();
205                 }, task_priority::high_priority);
206         }
207
208         boost::unique_future<void> clear(int index)
209         {
210                 return executor_.begin_invoke([=]
211                 {
212                         layers_.erase(index);
213                 }, task_priority::high_priority);
214         }
215                 
216         boost::unique_future<void> clear()
217         {
218                 return executor_.begin_invoke([=]
219                 {
220                         layers_.clear();
221                 }, task_priority::high_priority);
222         }       
223                 
224         boost::unique_future<void> swap_layers(stage& other)
225         {
226                 auto other_impl = other.impl_;
227
228                 if(other_impl.get() == this)
229                         return async(launch::deferred, []{});
230                 
231                 auto func = [=]
232                 {
233                         auto layers                     = layers_ | boost::adaptors::map_values;
234                         auto other_layers       = other_impl->layers_ | boost::adaptors::map_values;
235
236                         BOOST_FOREACH(auto& layer, layers)
237                                 layer.unsubscribe(event_subject_);
238                         
239                         BOOST_FOREACH(auto& layer, other_layers)
240                                 layer.unsubscribe(event_subject_);
241                         
242                         std::swap(layers_, other_impl->layers_);
243                                                 
244                         BOOST_FOREACH(auto& layer, layers)
245                                 layer.subscribe(event_subject_);
246                         
247                         BOOST_FOREACH(auto& layer, other_layers)
248                                 layer.subscribe(event_subject_);
249                 };              
250
251                 return executor_.begin_invoke([=]
252                 {
253                         other_impl->executor_.invoke(func, task_priority::high_priority);
254                 }, task_priority::high_priority);
255         }
256
257         boost::unique_future<void> swap_layer(int index, int other_index)
258         {
259                 return executor_.begin_invoke([=]
260                 {
261                         std::swap(layers_[index], layers_[other_index]);
262                 }, task_priority::high_priority);
263         }
264
265         boost::unique_future<void> swap_layer(int index, int other_index, stage& other)
266         {
267                 auto other_impl = other.impl_;
268
269                 if(other_impl.get() == this)
270                         return swap_layer(index, other_index);
271                 else
272                 {
273                         auto func = [=]
274                         {
275                                 auto& my_layer          = get_layer(index);
276                                 auto& other_layer       = other_impl->get_layer(other_index);
277
278                                 my_layer.unsubscribe(event_subject_);
279                                 other_layer.unsubscribe(other_impl->event_subject_);
280
281                                 std::swap(my_layer, other_layer);
282
283                                 my_layer.subscribe(event_subject_);
284                                 other_layer.subscribe(other_impl->event_subject_);
285                         };              
286
287                         return executor_.begin_invoke([=]
288                         {
289                                 other_impl->executor_.invoke(func, task_priority::high_priority);
290                         }, task_priority::high_priority);
291                 }
292         }
293                 
294         boost::unique_future<spl::shared_ptr<frame_producer>> foreground(int index)
295         {
296                 return executor_.begin_invoke([=]
297                 {
298                         return layers_[index].foreground();
299                 }, task_priority::high_priority);
300         }
301         
302         boost::unique_future<spl::shared_ptr<frame_producer>> background(int index)
303         {
304                 return executor_.begin_invoke([=]
305                 {
306                         return layers_[index].background();
307                 }, task_priority::high_priority);
308         }
309
310         boost::unique_future<boost::property_tree::wptree> info()
311         {
312                 return executor_.begin_invoke([this]() -> boost::property_tree::wptree
313                 {
314                         boost::property_tree::wptree info;
315                         BOOST_FOREACH(auto& layer, layers_)                     
316                                 info.add_child(L"layers.layer", layer.second.info())
317                                         .add(L"index", layer.first);    
318                         return info;
319                 }, task_priority::high_priority);
320         }
321
322         boost::unique_future<boost::property_tree::wptree> info(int index)
323         {
324                 return executor_.begin_invoke([=]
325                 {
326                         return layers_[index].info();
327                 }, task_priority::high_priority);
328         }               
329         
330         boost::unique_future<std::wstring> call(int index, const std::wstring& params)
331         {
332                 return flatten(executor_.begin_invoke([=]
333                 {
334                         return make_shared(layers_[index].foreground()->call(params));
335                 }, task_priority::high_priority));
336         }
337 };
338
339 stage::stage(spl::shared_ptr<diagnostics::graph> graph) : impl_(new impl(std::move(graph))){}
340 boost::unique_future<std::wstring> stage::call(int index, const std::wstring& params){return impl_->call(index, params);}
341 boost::unique_future<void> stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){return impl_->apply_transforms(transforms);}
342 boost::unique_future<void> stage::apply_transform(int index, const std::function<core::frame_transform(core::frame_transform)>& transform, unsigned int mix_duration, const tweener& tween){return impl_->apply_transform(index, transform, mix_duration, tween);}
343 boost::unique_future<void> stage::clear_transforms(int index){return impl_->clear_transforms(index);}
344 boost::unique_future<void> stage::clear_transforms(){return impl_->clear_transforms();}
345 boost::unique_future<void> stage::load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta){return impl_->load(index, producer, preview, auto_play_delta);}
346 boost::unique_future<void> stage::pause(int index){return impl_->pause(index);}
347 boost::unique_future<void> stage::play(int index){return impl_->play(index);}
348 boost::unique_future<void> stage::stop(int index){return impl_->stop(index);}
349 boost::unique_future<void> stage::clear(int index){return impl_->clear(index);}
350 boost::unique_future<void> stage::clear(){return impl_->clear();}
351 boost::unique_future<void> stage::swap_layers(stage& other){return impl_->swap_layers(other);}
352 boost::unique_future<void> stage::swap_layer(int index, int other_index){return impl_->swap_layer(index, other_index);}
353 boost::unique_future<void> stage::swap_layer(int index, int other_index, stage& other){return impl_->swap_layer(index, other_index, other);}
354 boost::unique_future<spl::shared_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}
355 boost::unique_future<spl::shared_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}
356 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}
357 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}
358 std::map<int, class draw_frame> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}
359 void stage::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}
360 void stage::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}
361 void stage::subscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.subscribe(o);}
362 void stage::unsubscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.unsubscribe(o);}
363 }}