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>
35 #include <common/timer.h>
37 #include <core/frame/frame_transform.h>
39 #include <boost/property_tree/ptree.hpp>
41 #include <tbb/parallel_for_each.h>
48 namespace caspar { namespace core {
50 struct stage::impl : public std::enable_shared_from_this<impl>
52 spl::shared_ptr<diagnostics::graph> graph_;
53 spl::shared_ptr<monitor::subject> monitor_subject_;
54 //reactive::basic_subject<std::map<int, draw_frame>> frames_subject_;
55 std::map<int, layer> layers_;
56 std::map<int, tweened_transform> tweens_;
57 interaction_aggregator aggregator_;
60 impl(spl::shared_ptr<diagnostics::graph> graph)
61 : graph_(std::move(graph))
62 , monitor_subject_(spl::make_shared<monitor::subject>("/stage"))
63 , aggregator_([=] (double x, double y) { return collission_detect(x, y); })
66 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
69 std::map<int, draw_frame> operator()(const video_format_desc& format_desc)
71 caspar::timer frame_timer;
73 auto frames = executor_.invoke([=]() -> std::map<int, draw_frame>
76 std::map<int, draw_frame> frames;
80 std::vector<int> indices;
82 for (auto& layer : layers_)
84 frames[layer.first] = draw_frame::empty();
85 indices.push_back(layer.first);
88 aggregator_.translate_and_send();
90 tbb::parallel_for_each(indices.begin(), indices.end(), [&](int index)
92 draw(index, format_desc, frames);
98 CASPAR_LOG_CURRENT_EXCEPTION();
105 //frames_subject_ << frames;
107 graph_->set_value("produce-time", frame_timer.elapsed()*format_desc.fps*0.5);
108 *monitor_subject_ << monitor::message("/profiler/time") % frame_timer.elapsed() % (1.0/format_desc.fps);
113 void draw(int index, const video_format_desc& format_desc, std::map<int, draw_frame>& frames)
115 auto& layer = layers_[index];
116 auto& tween = tweens_[index];
118 auto frame = layer.receive(format_desc);
120 frame1.transform() *= tween.fetch_and_tick(1);
122 if(format_desc.field_mode != core::field_mode::progressive)
125 frame2.transform() *= tween.fetch_and_tick(1);
126 frame1 = core::draw_frame::interlace(frame1, frame2, format_desc.field_mode);
129 frames[index] = frame1;
132 layer& get_layer(int index)
134 auto it = layers_.find(index);
135 if(it == std::end(layers_))
137 it = layers_.insert(std::make_pair(index, layer(index))).first;
138 it->second.monitor_output().attach_parent(monitor_subject_);
143 std::future<void> apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)
145 return executor_.begin_invoke([=]
147 for (auto& transform : transforms)
149 auto src = tweens_[std::get<0>(transform)].fetch();
150 auto dst = std::get<1>(transform)(src);
151 tweens_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));
153 }, task_priority::high_priority);
156 std::future<void> apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)
158 return executor_.begin_invoke([=]
160 auto src = tweens_[index].fetch();
161 auto dst = transform(src);
162 tweens_[index] = tweened_transform(src, dst, mix_duration, tween);
163 }, task_priority::high_priority);
166 std::future<void> clear_transforms(int index)
168 return executor_.begin_invoke([=]
170 tweens_.erase(index);
171 }, task_priority::high_priority);
174 std::future<void> clear_transforms()
176 return executor_.begin_invoke([=]
179 }, task_priority::high_priority);
182 std::future<frame_transform> get_current_transform(int index)
184 return executor_.begin_invoke([=]
186 return tweens_[index].fetch();
187 }, task_priority::high_priority);
190 std::future<void> load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
192 return executor_.begin_invoke([=]
194 get_layer(index).load(producer, preview, auto_play_delta);
195 }, task_priority::high_priority);
198 std::future<void> pause(int index)
200 return executor_.begin_invoke([=]
202 get_layer(index).pause();
203 }, task_priority::high_priority);
206 std::future<void> play(int index)
208 return executor_.begin_invoke([=]
210 get_layer(index).play();
211 }, task_priority::high_priority);
214 std::future<void> stop(int index)
216 return executor_.begin_invoke([=]
218 get_layer(index).stop();
219 }, task_priority::high_priority);
222 std::future<void> clear(int index)
224 return executor_.begin_invoke([=]
226 layers_.erase(index);
227 }, task_priority::high_priority);
230 std::future<void> clear()
232 return executor_.begin_invoke([=]
235 }, task_priority::high_priority);
238 std::future<void> swap_layers(stage& other)
240 auto other_impl = other.impl_;
242 if (other_impl.get() == this)
244 return make_ready_future();
249 auto layers = layers_ | boost::adaptors::map_values;
250 auto other_layers = other_impl->layers_ | boost::adaptors::map_values;
252 for (auto& layer : layers)
253 layer.monitor_output().detach_parent();
255 for (auto& layer : other_layers)
256 layer.monitor_output().detach_parent();
258 std::swap(layers_, other_impl->layers_);
260 for (auto& layer : layers)
261 layer.monitor_output().attach_parent(monitor_subject_);
263 for (auto& layer : other_layers)
264 layer.monitor_output().attach_parent(monitor_subject_);
267 return executor_.begin_invoke([=]
269 other_impl->executor_.invoke(func, task_priority::high_priority);
270 }, task_priority::high_priority);
273 std::future<void> swap_layer(int index, int other_index)
275 return executor_.begin_invoke([=]
277 std::swap(get_layer(index), get_layer(other_index));
278 }, task_priority::high_priority);
281 std::future<void> swap_layer(int index, int other_index, stage& other)
283 auto other_impl = other.impl_;
285 if(other_impl.get() == this)
286 return swap_layer(index, other_index);
291 auto& my_layer = get_layer(index);
292 auto& other_layer = other_impl->get_layer(other_index);
294 my_layer.monitor_output().detach_parent();
295 other_layer.monitor_output().detach_parent();
297 std::swap(my_layer, other_layer);
299 my_layer.monitor_output().attach_parent(monitor_subject_);
300 other_layer.monitor_output().attach_parent(other_impl->monitor_subject_);
303 return executor_.begin_invoke([=]
305 other_impl->executor_.invoke(func, task_priority::high_priority);
306 }, task_priority::high_priority);
310 std::future<std::shared_ptr<frame_producer>> foreground(int index)
312 return executor_.begin_invoke([=]() -> std::shared_ptr<frame_producer>
314 return get_layer(index).foreground();
315 }, task_priority::high_priority);
318 std::future<std::shared_ptr<frame_producer>> background(int index)
320 return executor_.begin_invoke([=]() -> std::shared_ptr<frame_producer>
322 return get_layer(index).background();
323 }, task_priority::high_priority);
326 std::future<boost::property_tree::wptree> info()
328 return executor_.begin_invoke([this]() -> boost::property_tree::wptree
330 boost::property_tree::wptree info;
331 for (auto& layer : layers_)
332 info.add_child(L"layers.layer", layer.second.info())
333 .add(L"index", layer.first);
335 }, task_priority::high_priority);
338 std::future<boost::property_tree::wptree> info(int index)
340 return executor_.begin_invoke([=]
342 return get_layer(index).info();
343 }, task_priority::high_priority);
346 std::future<std::wstring> call(int index, const std::vector<std::wstring>& params)
348 return flatten(executor_.begin_invoke([=]
350 return get_layer(index).foreground()->call(params).share();
351 }, task_priority::high_priority));
354 void on_interaction(const interaction_event::ptr& event)
356 executor_.begin_invoke([=]
358 aggregator_.offer(event);
359 }, task_priority::high_priority);
362 boost::optional<interaction_target> collission_detect(double x, double y)
364 for (auto& layer : layers_ | boost::adaptors::reversed)
366 auto transform = tweens_[layer.first].fetch();
367 auto translated = translate(x, y, transform);
369 if (translated.first >= 0.0
370 && translated.first <= 1.0
371 && translated.second >= 0.0
372 && translated.second <= 1.0
373 && layer.second.collides(translated.first, translated.second))
375 return std::make_pair(transform, static_cast<interaction_sink*>(&layer.second));
379 return boost::optional<interaction_target>();
383 stage::stage(spl::shared_ptr<diagnostics::graph> graph) : impl_(new impl(std::move(graph))){}
384 std::future<std::wstring> stage::call(int index, const std::vector<std::wstring>& params){return impl_->call(index, params);}
385 std::future<void> stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){ return impl_->apply_transforms(transforms); }
386 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); }
387 std::future<void> stage::clear_transforms(int index){ return impl_->clear_transforms(index); }
388 std::future<void> stage::clear_transforms(){ return impl_->clear_transforms(); }
389 std::future<frame_transform> stage::get_current_transform(int index){ return impl_->get_current_transform(index); }
390 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); }
391 std::future<void> stage::pause(int index){ return impl_->pause(index); }
392 std::future<void> stage::play(int index){ return impl_->play(index); }
393 std::future<void> stage::stop(int index){ return impl_->stop(index); }
394 std::future<void> stage::clear(int index){ return impl_->clear(index); }
395 std::future<void> stage::clear(){ return impl_->clear(); }
396 std::future<void> stage::swap_layers(stage& other){ return impl_->swap_layers(other); }
397 std::future<void> stage::swap_layer(int index, int other_index){ return impl_->swap_layer(index, other_index); }
398 std::future<void> stage::swap_layer(int index, int other_index, stage& other){ return impl_->swap_layer(index, other_index, other); }
399 std::future<std::shared_ptr<frame_producer>> stage::foreground(int index) { return impl_->foreground(index); }
400 std::future<std::shared_ptr<frame_producer>> stage::background(int index) { return impl_->background(index); }
401 std::future<boost::property_tree::wptree> stage::info() const{ return impl_->info(); }
402 std::future<boost::property_tree::wptree> stage::info(int index) const{ return impl_->info(index); }
403 std::map<int, draw_frame> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}
404 monitor::subject& stage::monitor_output(){return *impl_->monitor_subject_;}
405 //void stage::subscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.subscribe(o);}
406 //void stage::unsubscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.unsubscribe(o);}
407 void stage::on_interaction(const interaction_event::ptr& event) { impl_->on_interaction(event); }