]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
* Upgraded to Visual Studio 2013
[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 #include "../interaction/interaction_aggregator.h"
31
32 #include <common/executor.h>
33 #include <common/future.h>
34 #include <common/diagnostics/graph.h>
35
36 #include <core/frame/frame_transform.h>
37
38 #include <boost/foreach.hpp>
39 #include <boost/timer.hpp>
40 #include <boost/property_tree/ptree.hpp>
41 #include <boost/range/algorithm_ext.hpp>
42
43 #include <tbb/parallel_for_each.h>
44
45 #include <functional>
46 #include <map>
47 #include <vector>
48 #include <future>
49
50 namespace caspar { namespace core {
51         
52 struct stage::impl : public std::enable_shared_from_this<impl>
53 {                               
54         spl::shared_ptr<diagnostics::graph>                                                     graph_;
55         spl::shared_ptr<monitor::subject>                                                       monitor_subject_;
56         //reactive::basic_subject<std::map<int, class draw_frame>>      frames_subject_;
57         std::map<int, layer>                                                                            layers_;        
58         std::map<int, tweened_transform>                                                        tweens_;
59         interaction_aggregator                                                                          aggregator_;
60         executor                                                                                                        executor_;
61 public:
62         impl(spl::shared_ptr<diagnostics::graph> graph) 
63                 : graph_(std::move(graph))
64                 , monitor_subject_(spl::make_shared<monitor::subject>("/stage"))
65                 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
66                 , executor_(L"stage")
67         {
68                 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
69         }
70                 
71         std::map<int, draw_frame> operator()(const struct video_format_desc& format_desc)
72         {               
73                 boost::timer frame_timer;
74
75                 auto frames = executor_.invoke([=]() -> std::map<int, draw_frame>
76                 {
77
78                         std::map<int, class draw_frame> frames;
79                         
80                         try
81                         {                       
82                                 std::vector<int> indices;
83
84                                 BOOST_FOREACH(auto& layer, layers_)     
85                                 {
86                                         frames[layer.first] = draw_frame::empty();      
87                                         indices.push_back(layer.first);
88                                 }
89
90                                 aggregator_.translate_and_send();
91
92                                 // WORKAROUND: Compiler doesn't seem to like lambda.
93                                 tbb::parallel_for_each(indices.begin(), indices.end(), [&](int index)
94                                 {
95                                         draw(index, format_desc, frames);
96                                 });
97                         }
98                         catch(...)
99                         {
100                                 layers_.clear();
101                                 CASPAR_LOG_CURRENT_EXCEPTION();
102                         }       
103                         
104
105                         return frames;
106                 });
107                 
108                 //frames_subject_ << frames;
109                 
110                 graph_->set_value("produce-time", frame_timer.elapsed()*format_desc.fps*0.5);
111                 *monitor_subject_ << monitor::message("/profiler/time") % frame_timer.elapsed() % (1.0/format_desc.fps);
112
113                 return frames;
114         }
115
116         void draw(int index, const video_format_desc& format_desc, std::map<int, draw_frame>& frames)
117         {
118                 auto& layer             = layers_[index];
119                 auto& tween             = tweens_[index];
120                                 
121                 auto frame  = layer.receive(format_desc);                                       
122                 auto frame1 = frame;
123                 frame1.transform() *= tween.fetch_and_tick(1);
124
125                 if(format_desc.field_mode != core::field_mode::progressive)
126                 {                               
127                         auto frame2 = frame;
128                         frame2.transform() *= tween.fetch_and_tick(1);
129                         frame1 = core::draw_frame::interlace(frame1, frame2, format_desc.field_mode);
130                 }
131
132                 frames[index] = frame1;
133         }
134
135         layer& get_layer(int index)
136         {
137                 auto it = layers_.find(index);
138                 if(it == std::end(layers_))
139                 {
140                         it = layers_.insert(std::make_pair(index, layer(index))).first;
141                         it->second.monitor_output().attach_parent(monitor_subject_);
142                 }
143                 return it->second;
144         }
145                 
146         std::future<void> apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)
147         {
148                 return executor_.begin_invoke([=]
149                 {
150                         BOOST_FOREACH(auto& transform, transforms)
151                         {
152                                 auto src = tweens_[std::get<0>(transform)].fetch();
153                                 auto dst = std::get<1>(transform)(src);
154                                 tweens_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));
155                         }
156                 }, task_priority::high_priority);
157         }
158                                                 
159         std::future<void> apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)
160         {
161                 return executor_.begin_invoke([=]
162                 {
163                         auto src = tweens_[index].fetch();
164                         auto dst = transform(src);
165                         tweens_[index] = tweened_transform(src, dst, mix_duration, tween);
166                 }, task_priority::high_priority);
167         }
168
169         std::future<void> clear_transforms(int index)
170         {
171                 return executor_.begin_invoke([=]
172                 {
173                         tweens_.erase(index);
174                 }, task_priority::high_priority);
175         }
176
177         std::future<void> clear_transforms()
178         {
179                 return executor_.begin_invoke([=]
180                 {
181                         tweens_.clear();
182                 }, task_priority::high_priority);
183         }
184                 
185         std::future<void> load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
186         {
187                 return executor_.begin_invoke([=]
188                 {
189                         get_layer(index).load(producer, preview, auto_play_delta);                      
190                 }, task_priority::high_priority);
191         }
192
193         std::future<void> pause(int index)
194         {               
195                 return executor_.begin_invoke([=]
196                 {
197                         get_layer(index).pause();
198                 }, task_priority::high_priority);
199         }
200
201         std::future<void> play(int index)
202         {               
203                 return executor_.begin_invoke([=]
204                 {
205                         get_layer(index).play();
206                 }, task_priority::high_priority);
207         }
208
209         std::future<void> stop(int index)
210         {               
211                 return executor_.begin_invoke([=]
212                 {
213                         get_layer(index).stop();
214                 }, task_priority::high_priority);
215         }
216
217         std::future<void> clear(int index)
218         {
219                 return executor_.begin_invoke([=]
220                 {
221                         layers_.erase(index);
222                 }, task_priority::high_priority);
223         }
224                 
225         std::future<void> clear()
226         {
227                 return executor_.begin_invoke([=]
228                 {
229                         layers_.clear();
230                 }, task_priority::high_priority);
231         }       
232                 
233         std::future<void> swap_layers(stage& other)
234         {
235                 auto other_impl = other.impl_;
236
237                 if (other_impl.get() == this)
238                 {
239                         return make_ready_future();
240                 }
241                 
242                 auto func = [=]
243                 {
244                         auto layers                     = layers_ | boost::adaptors::map_values;
245                         auto other_layers       = other_impl->layers_ | boost::adaptors::map_values;
246
247                         BOOST_FOREACH(auto& layer, layers)
248                                 layer.monitor_output().detach_parent();
249                         
250                         BOOST_FOREACH(auto& layer, other_layers)
251                                 layer.monitor_output().detach_parent();
252                         
253                         std::swap(layers_, other_impl->layers_);
254                                                 
255                         BOOST_FOREACH(auto& layer, layers)
256                                 layer.monitor_output().attach_parent(monitor_subject_);
257                         
258                         BOOST_FOREACH(auto& layer, other_layers)
259                                 layer.monitor_output().attach_parent(monitor_subject_);
260                 };              
261
262                 return executor_.begin_invoke([=]
263                 {
264                         other_impl->executor_.invoke(func, task_priority::high_priority);
265                 }, task_priority::high_priority);
266         }
267
268         std::future<void> swap_layer(int index, int other_index)
269         {
270                 return executor_.begin_invoke([=]
271                 {
272                         std::swap(get_layer(index), get_layer(other_index));
273                 }, task_priority::high_priority);
274         }
275
276         std::future<void> swap_layer(int index, int other_index, stage& other)
277         {
278                 auto other_impl = other.impl_;
279
280                 if(other_impl.get() == this)
281                         return swap_layer(index, other_index);
282                 else
283                 {
284                         auto func = [=]
285                         {
286                                 auto& my_layer          = get_layer(index);
287                                 auto& other_layer       = other_impl->get_layer(other_index);
288
289                                 my_layer.monitor_output().detach_parent();
290                                 other_layer.monitor_output().detach_parent();
291
292                                 std::swap(my_layer, other_layer);
293
294                                 my_layer.monitor_output().attach_parent(monitor_subject_);
295                                 other_layer.monitor_output().attach_parent(other_impl->monitor_subject_);
296                         };              
297
298                         return executor_.begin_invoke([=]
299                         {
300                                 other_impl->executor_.invoke(func, task_priority::high_priority);
301                         }, task_priority::high_priority);
302                 }
303         }
304                 
305         std::future<std::shared_ptr<frame_producer>> foreground(int index)
306         {
307                 return executor_.begin_invoke([=]() -> std::shared_ptr<frame_producer>
308                 {
309                         return get_layer(index).foreground();
310                 }, task_priority::high_priority);
311         }
312         
313         std::future<std::shared_ptr<frame_producer>> background(int index)
314         {
315                 return executor_.begin_invoke([=]() -> std::shared_ptr<frame_producer>
316                 {
317                         return get_layer(index).background();
318                 }, task_priority::high_priority);
319         }
320
321         std::future<boost::property_tree::wptree> info()
322         {
323                 return executor_.begin_invoke([this]() -> boost::property_tree::wptree
324                 {
325                         boost::property_tree::wptree info;
326                         BOOST_FOREACH(auto& layer, layers_)                     
327                                 info.add_child(L"layers.layer", layer.second.info())
328                                         .add(L"index", layer.first);    
329                         return info;
330                 }, task_priority::high_priority);
331         }
332
333         std::future<boost::property_tree::wptree> info(int index)
334         {
335                 return executor_.begin_invoke([=]
336                 {
337                         return get_layer(index).info();
338                 }, task_priority::high_priority);
339         }               
340         
341         std::future<std::wstring> call(int index, const std::vector<std::wstring>& params)
342         {
343                 return flatten(executor_.begin_invoke([=]
344                 {
345                         return get_layer(index).foreground()->call(params).share();
346                 }, task_priority::high_priority));
347         }
348
349         void on_interaction(const interaction_event::ptr& event)
350         {
351                 executor_.begin_invoke([=]
352                 {
353                         aggregator_.offer(event);
354                 }, task_priority::high_priority);
355         }
356
357         boost::optional<interaction_target> collission_detect(double x, double y)
358         {
359                 BOOST_FOREACH(auto& layer, layers_ | boost::adaptors::reversed)
360                 {
361                         auto transform = tweens_[layer.first].fetch();
362                         auto translated = translate(x, y, transform);
363
364                         if (translated.first >= 0.0
365                                 && translated.first <= 1.0
366                                 && translated.second >= 0.0
367                                 && translated.second <= 1.0
368                                 && layer.second.collides(translated.first, translated.second))
369                         {
370                                 return std::make_pair(transform, &layer.second);
371                         }
372                 }
373
374                 return boost::optional<interaction_target>();
375         }
376 };
377
378 stage::stage(spl::shared_ptr<diagnostics::graph> graph) : impl_(new impl(std::move(graph))){}
379 std::future<std::wstring> stage::call(int index, const std::vector<std::wstring>& params){return impl_->call(index, params);}
380 std::future<void> stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){ return impl_->apply_transforms(transforms); }
381 std::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); }
382 std::future<void> stage::clear_transforms(int index){ return impl_->clear_transforms(index); }
383 std::future<void> stage::clear_transforms(){ return impl_->clear_transforms(); }
384 std::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); }
385 std::future<void> stage::pause(int index){ return impl_->pause(index); }
386 std::future<void> stage::play(int index){ return impl_->play(index); }
387 std::future<void> stage::stop(int index){ return impl_->stop(index); }
388 std::future<void> stage::clear(int index){ return impl_->clear(index); }
389 std::future<void> stage::clear(){ return impl_->clear(); }
390 std::future<void> stage::swap_layers(stage& other){ return impl_->swap_layers(other); }
391 std::future<void> stage::swap_layer(int index, int other_index){ return impl_->swap_layer(index, other_index); }
392 std::future<void> stage::swap_layer(int index, int other_index, stage& other){ return impl_->swap_layer(index, other_index, other); }
393 std::future<std::shared_ptr<frame_producer>> stage::foreground(int index) { return impl_->foreground(index); }
394 std::future<std::shared_ptr<frame_producer>> stage::background(int index) { return impl_->background(index); }
395 std::future<boost::property_tree::wptree> stage::info() const{ return impl_->info(); }
396 std::future<boost::property_tree::wptree> stage::info(int index) const{ return impl_->info(index); }
397 std::map<int, class draw_frame> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}
398 monitor::subject& stage::monitor_output(){return *impl_->monitor_subject_;}
399 //void stage::subscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.subscribe(o);}
400 //void stage::unsubscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.unsubscribe(o);}
401 void stage::on_interaction(const interaction_event::ptr& event) { impl_->on_interaction(event); }
402 }}