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