]> git.sesse.net Git - casparcg/blob - core/producer/stage.cpp
Merged INFO DELAY from 2.0
[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 #include "../consumer/write_frame_consumer.h"
32
33 #include <common/executor.h>
34 #include <common/future.h>
35 #include <common/diagnostics/graph.h>
36 #include <common/timer.h>
37
38 #include <core/frame/frame_transform.h>
39
40 #include <boost/property_tree/ptree.hpp>
41
42 #include <tbb/parallel_for_each.h>
43
44 #include <functional>
45 #include <map>
46 #include <vector>
47 #include <future>
48
49 namespace caspar { namespace core {
50         
51 struct stage::impl : public std::enable_shared_from_this<impl>
52 {
53         int                                                                                                                                             channel_index_;
54         spl::shared_ptr<diagnostics::graph>                                                                             graph_;
55         spl::shared_ptr<monitor::subject>                                                                               monitor_subject_        = spl::make_shared<monitor::subject>("/stage");
56         std::map<int, layer>                                                                                                    layers_;
57         std::map<int, tweened_transform>                                                                                tweens_;
58         interaction_aggregator                                                                                                  aggregator_;
59         // map of layer -> map of tokens (src ref) -> layer_consumer
60         std::map<int, std::map<void*, spl::shared_ptr<write_frame_consumer>>>   layer_consumers_;
61         executor                                                                                                                                executor_                       { L"stage " + boost::lexical_cast<std::wstring>(channel_index_) };
62 public:
63         impl(int channel_index, spl::shared_ptr<diagnostics::graph> graph)
64                 : channel_index_(channel_index)
65                 , graph_(std::move(graph))
66                 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
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 video_format_desc& format_desc)
72         {               
73                 caspar::timer frame_timer;
74
75                 auto frames = executor_.invoke([=]() -> std::map<int, draw_frame>
76                 {
77
78                         std::map<int, draw_frame> frames;
79                         
80                         try
81                         {                       
82                                 std::vector<int> indices;
83
84                                 for (auto& layer : layers_)     
85                                 {
86                                         // Prevent race conditions in parallel for each later
87                                         frames[layer.first] = draw_frame::empty();
88                                         tweens_[layer.first];
89                                         layer_consumers_[layer.first];
90
91                                         indices.push_back(layer.first);
92                                 }
93
94                                 aggregator_.translate_and_send();
95
96                                 tbb::parallel_for_each(indices.begin(), indices.end(), [&](int index)
97                                 {
98                                         draw(index, format_desc, frames);
99                                 });
100                         }
101                         catch(...)
102                         {
103                                 layers_.clear();
104                                 CASPAR_LOG_CURRENT_EXCEPTION();
105                         }       
106                         
107
108                         return frames;
109                 });
110                 
111                 //frames_subject_ << frames;
112                 
113                 graph_->set_value("produce-time", frame_timer.elapsed()*format_desc.fps*0.5);
114                 *monitor_subject_ << monitor::message("/profiler/time") % frame_timer.elapsed() % (1.0/format_desc.fps);
115
116                 return frames;
117         }
118
119         void draw(int index, const video_format_desc& format_desc, std::map<int, draw_frame>& frames)
120         {
121                 auto& layer             = layers_[index];
122                 auto& tween             = tweens_[index];
123                 auto& consumers = layer_consumers_[index];
124
125                 auto frame  = layer.receive(format_desc);                                       
126                 
127                 if (!consumers.empty())
128                 {
129                         auto consumer_it = consumers | boost::adaptors::map_values;
130                         tbb::parallel_for_each(consumer_it.begin(), consumer_it.end(), [&](decltype(*consumer_it.begin()) layer_consumer)
131                         {
132                                 layer_consumer->send(frame);
133                         });
134                 }
135
136                 auto frame1 = frame;
137                 
138                 frame1.transform() *= tween.fetch_and_tick(1);
139
140                 if(format_desc.field_mode != core::field_mode::progressive)
141                 {                               
142                         auto frame2 = frame;
143                         frame2.transform() *= tween.fetch_and_tick(1);
144                         frame1 = core::draw_frame::interlace(frame1, frame2, format_desc.field_mode);
145                 }
146
147                 frames[index] = frame1;
148         }
149
150         layer& get_layer(int index)
151         {
152                 auto it = layers_.find(index);
153                 if(it == std::end(layers_))
154                 {
155                         it = layers_.insert(std::make_pair(index, layer(index))).first;
156                         it->second.monitor_output().attach_parent(monitor_subject_);
157                 }
158                 return it->second;
159         }
160                 
161         std::future<void> apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)
162         {
163                 return executor_.begin_invoke([=]
164                 {
165                         for (auto& transform : transforms)
166                         {
167                                 auto src = tweens_[std::get<0>(transform)].fetch();
168                                 auto dst = std::get<1>(transform)(src);
169                                 tweens_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));
170                         }
171                 }, task_priority::high_priority);
172         }
173                                                 
174         std::future<void> apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)
175         {
176                 return executor_.begin_invoke([=]
177                 {
178                         auto src = tweens_[index].fetch();
179                         auto dst = transform(src);
180                         tweens_[index] = tweened_transform(src, dst, mix_duration, tween);
181                 }, task_priority::high_priority);
182         }
183
184         std::future<void> clear_transforms(int index)
185         {
186                 return executor_.begin_invoke([=]
187                 {
188                         tweens_.erase(index);
189                 }, task_priority::high_priority);
190         }
191
192         std::future<void> clear_transforms()
193         {
194                 return executor_.begin_invoke([=]
195                 {
196                         tweens_.clear();
197                 }, task_priority::high_priority);
198         }
199
200         std::future<frame_transform> get_current_transform(int index)
201         {
202                 return executor_.begin_invoke([=]
203                 {
204                         return tweens_[index].fetch();
205                 }, task_priority::high_priority);
206         }
207
208         std::future<void> load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
209         {
210                 return executor_.begin_invoke([=]
211                 {
212                         get_layer(index).load(producer, preview, auto_play_delta);                      
213                 }, task_priority::high_priority);
214         }
215
216         std::future<void> pause(int index)
217         {               
218                 return executor_.begin_invoke([=]
219                 {
220                         get_layer(index).pause();
221                 }, task_priority::high_priority);
222         }
223
224         std::future<void> resume(int index)
225         {
226                 return executor_.begin_invoke([=]
227                 {
228                         get_layer(index).resume();
229                 }, task_priority::high_priority);
230         }
231
232         std::future<void> play(int index)
233         {               
234                 return executor_.begin_invoke([=]
235                 {
236                         get_layer(index).play();
237                 }, task_priority::high_priority);
238         }
239
240         std::future<void> stop(int index)
241         {               
242                 return executor_.begin_invoke([=]
243                 {
244                         get_layer(index).stop();
245                 }, task_priority::high_priority);
246         }
247
248         std::future<void> clear(int index)
249         {
250                 return executor_.begin_invoke([=]
251                 {
252                         layers_.erase(index);
253                 }, task_priority::high_priority);
254         }
255                 
256         std::future<void> clear()
257         {
258                 return executor_.begin_invoke([=]
259                 {
260                         layers_.clear();
261                 }, task_priority::high_priority);
262         }       
263                 
264         std::future<void> swap_layers(stage& other, bool swap_transforms)
265         {
266                 auto other_impl = other.impl_;
267
268                 if (other_impl.get() == this)
269                 {
270                         return make_ready_future();
271                 }
272                 
273                 auto func = [=]
274                 {
275                         auto layers                     = layers_ | boost::adaptors::map_values;
276                         auto other_layers       = other_impl->layers_ | boost::adaptors::map_values;
277
278                         for (auto& layer : layers)
279                                 layer.monitor_output().detach_parent();
280                         
281                         for (auto& layer : other_layers)
282                                 layer.monitor_output().detach_parent();
283                         
284                         std::swap(layers_, other_impl->layers_);
285                                                 
286                         for (auto& layer : layers)
287                                 layer.monitor_output().attach_parent(monitor_subject_);
288                         
289                         for (auto& layer : other_layers)
290                                 layer.monitor_output().attach_parent(monitor_subject_);
291
292                         if (swap_transforms)
293                                 std::swap(tweens_, other_impl->tweens_);
294                 };
295
296                 return executor_.begin_invoke([=]
297                 {
298                         other_impl->executor_.invoke(func, task_priority::high_priority);
299                 }, task_priority::high_priority);
300         }
301
302         std::future<void> swap_layer(int index, int other_index, bool swap_transforms)
303         {
304                 return executor_.begin_invoke([=]
305                 {
306                         std::swap(get_layer(index), get_layer(other_index));
307
308                         if (swap_transforms)
309                                 std::swap(tweens_[index], tweens_[other_index]);
310                 }, task_priority::high_priority);
311         }
312
313         std::future<void> swap_layer(int index, int other_index, stage& other, bool swap_transforms)
314         {
315                 auto other_impl = other.impl_;
316
317                 if(other_impl.get() == this)
318                         return swap_layer(index, other_index, swap_transforms);
319                 else
320                 {
321                         auto func = [=]
322                         {
323                                 auto& my_layer          = get_layer(index);
324                                 auto& other_layer       = other_impl->get_layer(other_index);
325
326                                 my_layer.monitor_output().detach_parent();
327                                 other_layer.monitor_output().detach_parent();
328
329                                 std::swap(my_layer, other_layer);
330
331                                 my_layer.monitor_output().attach_parent(monitor_subject_);
332                                 other_layer.monitor_output().attach_parent(other_impl->monitor_subject_);
333
334                                 if (swap_transforms)
335                                 {
336                                         auto& my_tween          = tweens_[index];
337                                         auto& other_tween       = other_impl->tweens_[other_index];
338                                         std::swap(my_tween, other_tween);
339                                 }
340                         };              
341
342                         return executor_.begin_invoke([=]
343                         {
344                                 other_impl->executor_.invoke(func, task_priority::high_priority);
345                         }, task_priority::high_priority);
346                 }
347         }
348
349         void add_layer_consumer(void* token, int layer, const spl::shared_ptr<write_frame_consumer>& layer_consumer)
350         {
351                 executor_.begin_invoke([=]
352                 {
353                         layer_consumers_[layer].insert(std::make_pair(token, layer_consumer));
354                 }, task_priority::high_priority);
355         }
356
357         void remove_layer_consumer(void* token, int layer)
358         {
359                 executor_.begin_invoke([=]
360                 {
361                         auto& layer_map = layer_consumers_[layer];
362                         layer_map.erase(token);
363                         if (layer_map.empty())
364                         {
365                                 layer_consumers_.erase(layer);
366                         }
367                 }, task_priority::high_priority);
368         }
369
370         std::future<std::shared_ptr<frame_producer>> foreground(int index)
371         {
372                 return executor_.begin_invoke([=]() -> std::shared_ptr<frame_producer>
373                 {
374                         return get_layer(index).foreground();
375                 }, task_priority::high_priority);
376         }
377         
378         std::future<std::shared_ptr<frame_producer>> background(int index)
379         {
380                 return executor_.begin_invoke([=]() -> std::shared_ptr<frame_producer>
381                 {
382                         return get_layer(index).background();
383                 }, task_priority::high_priority);
384         }
385
386         std::future<boost::property_tree::wptree> info()
387         {
388                 return executor_.begin_invoke([this]() -> boost::property_tree::wptree
389                 {
390                         boost::property_tree::wptree info;
391                         for (auto& layer : layers_)                     
392                                 info.add_child(L"layers.layer", layer.second.info())
393                                         .add(L"index", layer.first);    
394                         return info;
395                 }, task_priority::high_priority);
396         }
397
398         std::future<boost::property_tree::wptree> info(int index)
399         {
400                 return executor_.begin_invoke([=]
401                 {
402                         return get_layer(index).info();
403                 }, task_priority::high_priority);
404         }
405
406         std::future<boost::property_tree::wptree> delay_info()
407         {
408                 return std::move(executor_.begin_invoke([this]() -> boost::property_tree::wptree
409                 {
410                         boost::property_tree::wptree info;
411
412                         for (auto& layer : layers_)
413                                 info.add_child(L"layer", layer.second.delay_info()).add(L"index", layer.first);
414
415                         return info;
416                 }, task_priority::high_priority));
417         }
418
419         std::future<boost::property_tree::wptree> delay_info(int index)
420         {
421                 return std::move(executor_.begin_invoke([=]() -> boost::property_tree::wptree
422                 {
423                         return get_layer(index).delay_info();
424                 }, task_priority::high_priority));
425         }
426         
427         std::future<std::wstring> call(int index, const std::vector<std::wstring>& params)
428         {
429                 return flatten(executor_.begin_invoke([=]
430                 {
431                         return get_layer(index).foreground()->call(params).share();
432                 }, task_priority::high_priority));
433         }
434
435         void on_interaction(const interaction_event::ptr& event)
436         {
437                 executor_.begin_invoke([=]
438                 {
439                         aggregator_.offer(event);
440                 }, task_priority::high_priority);
441         }
442
443         boost::optional<interaction_target> collission_detect(double x, double y)
444         {
445                 for (auto& layer : layers_ | boost::adaptors::reversed)
446                 {
447                         auto transform = tweens_[layer.first].fetch();
448                         auto translated = translate(x, y, transform);
449
450                         if (translated.first >= 0.0
451                                 && translated.first <= 1.0
452                                 && translated.second >= 0.0
453                                 && translated.second <= 1.0
454                                 && layer.second.collides(translated.first, translated.second))
455                         {
456                                 return std::make_pair(transform, static_cast<interaction_sink*>(&layer.second));
457                         }
458                 }
459
460                 return boost::optional<interaction_target>();
461         }
462 };
463
464 stage::stage(int channel_index, spl::shared_ptr<diagnostics::graph> graph) : impl_(new impl(channel_index, std::move(graph))){}
465 std::future<std::wstring> stage::call(int index, const std::vector<std::wstring>& params){return impl_->call(index, params);}
466 std::future<void> stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){ return impl_->apply_transforms(transforms); }
467 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); }
468 std::future<void> stage::clear_transforms(int index){ return impl_->clear_transforms(index); }
469 std::future<void> stage::clear_transforms(){ return impl_->clear_transforms(); }
470 std::future<frame_transform> stage::get_current_transform(int index){ return impl_->get_current_transform(index); }
471 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); }
472 std::future<void> stage::pause(int index){ return impl_->pause(index); }
473 std::future<void> stage::resume(int index){ return impl_->resume(index); }
474 std::future<void> stage::play(int index){ return impl_->play(index); }
475 std::future<void> stage::stop(int index){ return impl_->stop(index); }
476 std::future<void> stage::clear(int index){ return impl_->clear(index); }
477 std::future<void> stage::clear(){ return impl_->clear(); }
478 std::future<void> stage::swap_layers(stage& other, bool swap_transforms){ return impl_->swap_layers(other, swap_transforms); }
479 std::future<void> stage::swap_layer(int index, int other_index, bool swap_transforms){ return impl_->swap_layer(index, other_index, swap_transforms); }
480 std::future<void> stage::swap_layer(int index, int other_index, stage& other, bool swap_transforms){ return impl_->swap_layer(index, other_index, other, swap_transforms); }
481 void stage::add_layer_consumer(void* token, int layer, const spl::shared_ptr<write_frame_consumer>& layer_consumer){ impl_->add_layer_consumer(token, layer, layer_consumer); }
482 void stage::remove_layer_consumer(void* token, int layer){ impl_->remove_layer_consumer(token, layer); }std::future<std::shared_ptr<frame_producer>> stage::foreground(int index) { return impl_->foreground(index); }
483 std::future<std::shared_ptr<frame_producer>> stage::background(int index) { return impl_->background(index); }
484 std::future<boost::property_tree::wptree> stage::info() const{ return impl_->info(); }
485 std::future<boost::property_tree::wptree> stage::info(int index) const{ return impl_->info(index); }
486 std::future<boost::property_tree::wptree> stage::delay_info() const{ return impl_->delay_info(); }
487 std::future<boost::property_tree::wptree> stage::delay_info(int index) const{ return impl_->delay_info(index); }
488 std::map<int, draw_frame> stage::operator()(const video_format_desc& format_desc){ return (*impl_)(format_desc); }
489 monitor::subject& stage::monitor_output(){return *impl_->monitor_subject_;}
490 void stage::on_interaction(const interaction_event::ptr& event) { impl_->on_interaction(event); }
491 }}