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"
31 #include <common/executor.h>
32 #include <common/future.h>
33 #include <common/diagnostics/graph.h>
35 #include <core/frame/frame_transform.h>
37 #include <boost/foreach.hpp>
38 #include <boost/timer.hpp>
39 #include <boost/property_tree/ptree.hpp>
40 #include <boost/range/algorithm_ext.hpp>
42 #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 monitor::basic_subject event_subject_;
54 reactive::basic_subject<std::map<int, class draw_frame>> frames_subject_;
55 std::map<int, layer> layers_;
56 std::map<int, tweened_transform> tweens_;
59 impl(spl::shared_ptr<diagnostics::graph> graph)
60 : graph_(std::move(graph))
61 , event_subject_("stage")
64 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
67 std::map<int, draw_frame> operator()(const struct video_format_desc& format_desc)
69 boost::timer frame_timer;
71 auto frames = executor_.invoke([=]() -> std::map<int, draw_frame>
74 std::map<int, class draw_frame> frames;
78 std::vector<int> indices;
80 BOOST_FOREACH(auto& layer, layers_)
82 frames[layer.first] = draw_frame::empty();
83 indices.push_back(layer.first);
86 // WORKAROUND: Compiler doesn't seem to like lambda.
87 tbb::parallel_for_each(indices.begin(), indices.end(), std::bind(&stage::impl::draw, this, std::placeholders::_1, std::ref(format_desc), std::ref(frames)));
92 CASPAR_LOG_CURRENT_EXCEPTION();
99 frames_subject_ << frames;
101 graph_->set_value("produce-time", frame_timer.elapsed()*format_desc.fps*0.5);
102 event_subject_ << monitor::event("profiler/time") % frame_timer.elapsed() % (1.0/format_desc.fps);
107 void draw(int index, const video_format_desc& format_desc, std::map<int, draw_frame>& frames)
109 auto& layer = layers_[index];
110 auto& tween = tweens_[index];
112 auto frame = layer.receive(format_desc);
114 frame1.transform() *= tween.fetch_and_tick(1);
116 if(format_desc.field_mode != core::field_mode::progressive)
119 frame2.transform() *= tween.fetch_and_tick(1);
120 frame1 = core::draw_frame::interlace(frame1, frame2, format_desc.field_mode);
123 frames[index] = frame1;
126 layer& get_layer(int index)
128 auto it = layers_.find(index);
129 if(it == std::end(layers_))
131 it = layers_.insert(std::make_pair(index, layer(index))).first;
132 it->second.subscribe(event_subject_);
137 boost::unique_future<void> apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)
139 return executor_.begin_invoke([=]
141 BOOST_FOREACH(auto& transform, transforms)
143 auto src = tweens_[std::get<0>(transform)].fetch();
144 auto dst = std::get<1>(transform)(src);
145 tweens_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));
147 }, task_priority::high_priority);
150 boost::unique_future<void> apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)
152 return executor_.begin_invoke([=]
154 auto src = tweens_[index].fetch();
155 auto dst = transform(src);
156 tweens_[index] = tweened_transform(src, dst, mix_duration, tween);
157 }, task_priority::high_priority);
160 boost::unique_future<void> clear_transforms(int index)
162 return executor_.begin_invoke([=]
164 tweens_.erase(index);
165 }, task_priority::high_priority);
168 boost::unique_future<void> clear_transforms()
170 return executor_.begin_invoke([=]
173 }, task_priority::high_priority);
176 boost::unique_future<void> load(int index, const spl::shared_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
178 return executor_.begin_invoke([=]
180 get_layer(index).load(producer, preview, auto_play_delta);
181 }, task_priority::high_priority);
184 boost::unique_future<void> pause(int index)
186 return executor_.begin_invoke([=]
188 layers_[index].pause();
189 }, task_priority::high_priority);
192 boost::unique_future<void> play(int index)
194 return executor_.begin_invoke([=]
196 layers_[index].play();
197 }, task_priority::high_priority);
200 boost::unique_future<void> stop(int index)
202 return executor_.begin_invoke([=]
204 layers_[index].stop();
205 }, task_priority::high_priority);
208 boost::unique_future<void> clear(int index)
210 return executor_.begin_invoke([=]
212 layers_.erase(index);
213 }, task_priority::high_priority);
216 boost::unique_future<void> clear()
218 return executor_.begin_invoke([=]
221 }, task_priority::high_priority);
224 boost::unique_future<void> swap_layers(stage& other)
226 auto other_impl = other.impl_;
228 if(other_impl.get() == this)
229 return async(launch::deferred, []{});
233 auto layers = layers_ | boost::adaptors::map_values;
234 auto other_layers = other_impl->layers_ | boost::adaptors::map_values;
236 BOOST_FOREACH(auto& layer, layers)
237 layer.unsubscribe(event_subject_);
239 BOOST_FOREACH(auto& layer, other_layers)
240 layer.unsubscribe(event_subject_);
242 std::swap(layers_, other_impl->layers_);
244 BOOST_FOREACH(auto& layer, layers)
245 layer.subscribe(event_subject_);
247 BOOST_FOREACH(auto& layer, other_layers)
248 layer.subscribe(event_subject_);
251 return executor_.begin_invoke([=]
253 other_impl->executor_.invoke(func, task_priority::high_priority);
254 }, task_priority::high_priority);
257 boost::unique_future<void> swap_layer(int index, int other_index)
259 return executor_.begin_invoke([=]
261 std::swap(layers_[index], layers_[other_index]);
262 }, task_priority::high_priority);
265 boost::unique_future<void> swap_layer(int index, int other_index, stage& other)
267 auto other_impl = other.impl_;
269 if(other_impl.get() == this)
270 return swap_layer(index, other_index);
275 auto& my_layer = get_layer(index);
276 auto& other_layer = other_impl->get_layer(other_index);
278 my_layer.unsubscribe(event_subject_);
279 other_layer.unsubscribe(other_impl->event_subject_);
281 std::swap(my_layer, other_layer);
283 my_layer.subscribe(event_subject_);
284 other_layer.subscribe(other_impl->event_subject_);
287 return executor_.begin_invoke([=]
289 other_impl->executor_.invoke(func, task_priority::high_priority);
290 }, task_priority::high_priority);
294 boost::unique_future<spl::shared_ptr<frame_producer>> foreground(int index)
296 return executor_.begin_invoke([=]
298 return layers_[index].foreground();
299 }, task_priority::high_priority);
302 boost::unique_future<spl::shared_ptr<frame_producer>> background(int index)
304 return executor_.begin_invoke([=]
306 return layers_[index].background();
307 }, task_priority::high_priority);
310 boost::unique_future<boost::property_tree::wptree> info()
312 return executor_.begin_invoke([this]() -> boost::property_tree::wptree
314 boost::property_tree::wptree info;
315 BOOST_FOREACH(auto& layer, layers_)
316 info.add_child(L"layers.layer", layer.second.info())
317 .add(L"index", layer.first);
319 }, task_priority::high_priority);
322 boost::unique_future<boost::property_tree::wptree> info(int index)
324 return executor_.begin_invoke([=]
326 return layers_[index].info();
327 }, task_priority::high_priority);
330 boost::unique_future<std::wstring> call(int index, const std::wstring& params)
332 return flatten(executor_.begin_invoke([=]
334 return make_shared(layers_[index].foreground()->call(params));
335 }, task_priority::high_priority));
339 stage::stage(spl::shared_ptr<diagnostics::graph> graph) : impl_(new impl(std::move(graph))){}
340 boost::unique_future<std::wstring> stage::call(int index, const std::wstring& params){return impl_->call(index, params);}
341 boost::unique_future<void> stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){return impl_->apply_transforms(transforms);}
342 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);}
343 boost::unique_future<void> stage::clear_transforms(int index){return impl_->clear_transforms(index);}
344 boost::unique_future<void> stage::clear_transforms(){return impl_->clear_transforms();}
345 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);}
346 boost::unique_future<void> stage::pause(int index){return impl_->pause(index);}
347 boost::unique_future<void> stage::play(int index){return impl_->play(index);}
348 boost::unique_future<void> stage::stop(int index){return impl_->stop(index);}
349 boost::unique_future<void> stage::clear(int index){return impl_->clear(index);}
350 boost::unique_future<void> stage::clear(){return impl_->clear();}
351 boost::unique_future<void> stage::swap_layers(stage& other){return impl_->swap_layers(other);}
352 boost::unique_future<void> stage::swap_layer(int index, int other_index){return impl_->swap_layer(index, other_index);}
353 boost::unique_future<void> stage::swap_layer(int index, int other_index, stage& other){return impl_->swap_layer(index, other_index, other);}
354 boost::unique_future<spl::shared_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}
355 boost::unique_future<spl::shared_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}
356 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}
357 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}
358 std::map<int, class draw_frame> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}
359 void stage::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}
360 void stage::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}
361 void stage::subscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.subscribe(o);}
362 void stage::unsubscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.unsubscribe(o);}