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>
49 namespace caspar { namespace core {
51 struct stage::impl : public std::enable_shared_from_this<impl>
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_;
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); })
67 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
70 std::map<int, draw_frame> operator()(const struct video_format_desc& format_desc)
72 boost::timer frame_timer;
74 auto frames = executor_.invoke([=]() -> std::map<int, draw_frame>
77 std::map<int, class draw_frame> frames;
81 std::vector<int> indices;
83 BOOST_FOREACH(auto& layer, layers_)
85 frames[layer.first] = draw_frame::empty();
86 indices.push_back(layer.first);
89 aggregator_.translate_and_send();
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)));
97 CASPAR_LOG_CURRENT_EXCEPTION();
104 frames_subject_ << frames;
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);
112 void draw(int index, const video_format_desc& format_desc, std::map<int, draw_frame>& frames)
114 auto& layer = layers_[index];
115 auto& tween = tweens_[index];
117 auto frame = layer.receive(format_desc);
119 frame1.transform() *= tween.fetch_and_tick(1);
121 if(format_desc.field_mode != core::field_mode::progressive)
124 frame2.transform() *= tween.fetch_and_tick(1);
125 frame1 = core::draw_frame::interlace(frame1, frame2, format_desc.field_mode);
128 frames[index] = frame1;
131 layer& get_layer(int index)
133 auto it = layers_.find(index);
134 if(it == std::end(layers_))
136 it = layers_.insert(std::make_pair(index, layer(index))).first;
137 it->second.subscribe(event_subject_);
142 boost::unique_future<void> apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)
144 return executor_.begin_invoke([=]
146 BOOST_FOREACH(auto& transform, transforms)
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));
152 }, task_priority::high_priority);
155 boost::unique_future<void> apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)
157 return executor_.begin_invoke([=]
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);
165 boost::unique_future<void> clear_transforms(int index)
167 return executor_.begin_invoke([=]
169 tweens_.erase(index);
170 }, task_priority::high_priority);
173 boost::unique_future<void> clear_transforms()
175 return executor_.begin_invoke([=]
178 }, task_priority::high_priority);
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)
183 return executor_.begin_invoke([=]
185 get_layer(index).load(producer, preview, auto_play_delta);
186 }, task_priority::high_priority);
189 boost::unique_future<void> pause(int index)
191 return executor_.begin_invoke([=]
193 layers_[index].pause();
194 }, task_priority::high_priority);
197 boost::unique_future<void> play(int index)
199 return executor_.begin_invoke([=]
201 layers_[index].play();
202 }, task_priority::high_priority);
205 boost::unique_future<void> stop(int index)
207 return executor_.begin_invoke([=]
209 layers_[index].stop();
210 }, task_priority::high_priority);
213 boost::unique_future<void> clear(int index)
215 return executor_.begin_invoke([=]
217 layers_.erase(index);
218 }, task_priority::high_priority);
221 boost::unique_future<void> clear()
223 return executor_.begin_invoke([=]
226 }, task_priority::high_priority);
229 boost::unique_future<void> swap_layers(stage& other)
231 auto other_impl = other.impl_;
233 if(other_impl.get() == this)
234 return async(launch::deferred, []{});
238 auto layers = layers_ | boost::adaptors::map_values;
239 auto other_layers = other_impl->layers_ | boost::adaptors::map_values;
241 BOOST_FOREACH(auto& layer, layers)
242 layer.unsubscribe(event_subject_);
244 BOOST_FOREACH(auto& layer, other_layers)
245 layer.unsubscribe(event_subject_);
247 std::swap(layers_, other_impl->layers_);
249 BOOST_FOREACH(auto& layer, layers)
250 layer.subscribe(event_subject_);
252 BOOST_FOREACH(auto& layer, other_layers)
253 layer.subscribe(event_subject_);
256 return executor_.begin_invoke([=]
258 other_impl->executor_.invoke(func, task_priority::high_priority);
259 }, task_priority::high_priority);
262 boost::unique_future<void> swap_layer(int index, int other_index)
264 return executor_.begin_invoke([=]
266 std::swap(layers_[index], layers_[other_index]);
267 }, task_priority::high_priority);
270 boost::unique_future<void> swap_layer(int index, int other_index, stage& other)
272 auto other_impl = other.impl_;
274 if(other_impl.get() == this)
275 return swap_layer(index, other_index);
280 auto& my_layer = get_layer(index);
281 auto& other_layer = other_impl->get_layer(other_index);
283 my_layer.unsubscribe(event_subject_);
284 other_layer.unsubscribe(other_impl->event_subject_);
286 std::swap(my_layer, other_layer);
288 my_layer.subscribe(event_subject_);
289 other_layer.subscribe(other_impl->event_subject_);
292 return executor_.begin_invoke([=]
294 other_impl->executor_.invoke(func, task_priority::high_priority);
295 }, task_priority::high_priority);
299 boost::unique_future<spl::shared_ptr<frame_producer>> foreground(int index)
301 return executor_.begin_invoke([=]
303 return layers_[index].foreground();
304 }, task_priority::high_priority);
307 boost::unique_future<spl::shared_ptr<frame_producer>> background(int index)
309 return executor_.begin_invoke([=]
311 return layers_[index].background();
312 }, task_priority::high_priority);
315 boost::unique_future<boost::property_tree::wptree> info()
317 return executor_.begin_invoke([this]() -> boost::property_tree::wptree
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);
324 }, task_priority::high_priority);
327 boost::unique_future<boost::property_tree::wptree> info(int index)
329 return executor_.begin_invoke([=]
331 return layers_[index].info();
332 }, task_priority::high_priority);
335 boost::unique_future<std::wstring> call(int index, const std::wstring& params)
337 return flatten(executor_.begin_invoke([=]
339 return make_shared(layers_[index].foreground()->call(params));
340 }, task_priority::high_priority));
343 void on_interaction(const interaction_event::ptr& event)
345 executor_.begin_invoke([=]
347 aggregator_.offer(event);
348 }, task_priority::high_priority);
351 boost::optional<interaction_target> collission_detect(double x, double y)
353 BOOST_FOREACH(auto& layer, layers_ | boost::adaptors::reversed)
355 auto transform = tweens_[layer.first].fetch();
356 auto translated = translate(x, y, transform);
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))
364 return std::make_pair(transform, &layer.second);
368 return boost::optional<interaction_target>();
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::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); }