2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../StdAfx.h"
28 #include "../frame/draw_frame.h"
29 #include "../frame/frame_factory.h"
30 #include "../interaction/interaction_aggregator.h"
32 #include <common/executor.h>
33 #include <common/future.h>
34 #include <common/diagnostics/graph.h>
36 #include <core/frame/frame_transform.h>
38 #include <boost/foreach.hpp>
39 #include <boost/timer.hpp>
40 #include <boost/property_tree/ptree.hpp>
41 #include <boost/range/algorithm_ext.hpp>
43 #include <tbb/parallel_for_each.h>
50 namespace caspar { namespace core {
52 struct stage::impl : public std::enable_shared_from_this<impl>
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_;
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); })
68 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
71 std::map<int, draw_frame> operator()(const struct video_format_desc& format_desc)
73 boost::timer frame_timer;
75 auto frames = executor_.invoke([=]() -> std::map<int, draw_frame>
78 std::map<int, class draw_frame> frames;
82 std::vector<int> indices;
84 BOOST_FOREACH(auto& layer, layers_)
86 frames[layer.first] = draw_frame::empty();
87 indices.push_back(layer.first);
90 aggregator_.translate_and_send();
92 // WORKAROUND: Compiler doesn't seem to like lambda.
93 tbb::parallel_for_each(indices.begin(), indices.end(), [&](int index)
95 draw(index, format_desc, frames);
101 CASPAR_LOG_CURRENT_EXCEPTION();
108 //frames_subject_ << frames;
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);
116 void draw(int index, const video_format_desc& format_desc, std::map<int, draw_frame>& frames)
118 auto& layer = layers_[index];
119 auto& tween = tweens_[index];
121 auto frame = layer.receive(format_desc);
123 frame1.transform() *= tween.fetch_and_tick(1);
125 if(format_desc.field_mode != core::field_mode::progressive)
128 frame2.transform() *= tween.fetch_and_tick(1);
129 frame1 = core::draw_frame::interlace(frame1, frame2, format_desc.field_mode);
132 frames[index] = frame1;
135 layer& get_layer(int index)
137 auto it = layers_.find(index);
138 if(it == std::end(layers_))
140 it = layers_.insert(std::make_pair(index, layer(index))).first;
141 it->second.monitor_output().attach_parent(monitor_subject_);
146 std::future<void> apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)
148 return executor_.begin_invoke([=]
150 BOOST_FOREACH(auto& transform, transforms)
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));
156 }, task_priority::high_priority);
159 std::future<void> apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)
161 return executor_.begin_invoke([=]
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);
169 std::future<void> clear_transforms(int index)
171 return executor_.begin_invoke([=]
173 tweens_.erase(index);
174 }, task_priority::high_priority);
177 std::future<void> clear_transforms()
179 return executor_.begin_invoke([=]
182 }, task_priority::high_priority);
185 std::future<void> load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
187 return executor_.begin_invoke([=]
189 get_layer(index).load(producer, preview, auto_play_delta);
190 }, task_priority::high_priority);
193 std::future<void> pause(int index)
195 return executor_.begin_invoke([=]
197 get_layer(index).pause();
198 }, task_priority::high_priority);
201 std::future<void> play(int index)
203 return executor_.begin_invoke([=]
205 get_layer(index).play();
206 }, task_priority::high_priority);
209 std::future<void> stop(int index)
211 return executor_.begin_invoke([=]
213 get_layer(index).stop();
214 }, task_priority::high_priority);
217 std::future<void> clear(int index)
219 return executor_.begin_invoke([=]
221 layers_.erase(index);
222 }, task_priority::high_priority);
225 std::future<void> clear()
227 return executor_.begin_invoke([=]
230 }, task_priority::high_priority);
233 std::future<void> swap_layers(stage& other)
235 auto other_impl = other.impl_;
237 if (other_impl.get() == this)
239 return make_ready_future();
244 auto layers = layers_ | boost::adaptors::map_values;
245 auto other_layers = other_impl->layers_ | boost::adaptors::map_values;
247 BOOST_FOREACH(auto& layer, layers)
248 layer.monitor_output().detach_parent();
250 BOOST_FOREACH(auto& layer, other_layers)
251 layer.monitor_output().detach_parent();
253 std::swap(layers_, other_impl->layers_);
255 BOOST_FOREACH(auto& layer, layers)
256 layer.monitor_output().attach_parent(monitor_subject_);
258 BOOST_FOREACH(auto& layer, other_layers)
259 layer.monitor_output().attach_parent(monitor_subject_);
262 return executor_.begin_invoke([=]
264 other_impl->executor_.invoke(func, task_priority::high_priority);
265 }, task_priority::high_priority);
268 std::future<void> swap_layer(int index, int other_index)
270 return executor_.begin_invoke([=]
272 std::swap(get_layer(index), get_layer(other_index));
273 }, task_priority::high_priority);
276 std::future<void> swap_layer(int index, int other_index, stage& other)
278 auto other_impl = other.impl_;
280 if(other_impl.get() == this)
281 return swap_layer(index, other_index);
286 auto& my_layer = get_layer(index);
287 auto& other_layer = other_impl->get_layer(other_index);
289 my_layer.monitor_output().detach_parent();
290 other_layer.monitor_output().detach_parent();
292 std::swap(my_layer, other_layer);
294 my_layer.monitor_output().attach_parent(monitor_subject_);
295 other_layer.monitor_output().attach_parent(other_impl->monitor_subject_);
298 return executor_.begin_invoke([=]
300 other_impl->executor_.invoke(func, task_priority::high_priority);
301 }, task_priority::high_priority);
305 std::future<std::shared_ptr<frame_producer>> foreground(int index)
307 return executor_.begin_invoke([=]() -> std::shared_ptr<frame_producer>
309 return get_layer(index).foreground();
310 }, task_priority::high_priority);
313 std::future<std::shared_ptr<frame_producer>> background(int index)
315 return executor_.begin_invoke([=]() -> std::shared_ptr<frame_producer>
317 return get_layer(index).background();
318 }, task_priority::high_priority);
321 std::future<boost::property_tree::wptree> info()
323 return executor_.begin_invoke([this]() -> boost::property_tree::wptree
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);
330 }, task_priority::high_priority);
333 std::future<boost::property_tree::wptree> info(int index)
335 return executor_.begin_invoke([=]
337 return get_layer(index).info();
338 }, task_priority::high_priority);
341 std::future<std::wstring> call(int index, const std::vector<std::wstring>& params)
343 return flatten(executor_.begin_invoke([=]
345 return get_layer(index).foreground()->call(params).share();
346 }, task_priority::high_priority));
349 void on_interaction(const interaction_event::ptr& event)
351 executor_.begin_invoke([=]
353 aggregator_.offer(event);
354 }, task_priority::high_priority);
357 boost::optional<interaction_target> collission_detect(double x, double y)
359 BOOST_FOREACH(auto& layer, layers_ | boost::adaptors::reversed)
361 auto transform = tweens_[layer.first].fetch();
362 auto translated = translate(x, y, transform);
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))
370 return std::make_pair(transform, &layer.second);
374 return boost::optional<interaction_target>();
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); }