2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../StdAfx.h"
\r
28 #include "../frame/draw_frame.h"
\r
29 #include "../frame/frame_factory.h"
\r
31 #include <common/executor.h>
\r
32 #include <common/future.h>
\r
33 #include <common/diagnostics/graph.h>
\r
35 #include <core/frame/frame_transform.h>
\r
37 #include <boost/foreach.hpp>
\r
38 #include <boost/timer.hpp>
\r
39 #include <boost/property_tree/ptree.hpp>
\r
40 #include <boost/range/algorithm_ext.hpp>
\r
42 #include <tbb/parallel_for_each.h>
\r
44 #include <functional>
\r
48 namespace caspar { namespace core {
\r
50 struct stage::impl : public std::enable_shared_from_this<impl>
\r
52 spl::shared_ptr<diagnostics::graph> graph_;
\r
53 monitor::basic_subject event_subject_;
\r
54 reactive::basic_subject<std::map<int, class draw_frame>> frames_subject_;
\r
55 std::map<int, layer> layers_;
\r
56 std::map<int, tweened_transform> tweens_;
\r
59 impl(spl::shared_ptr<diagnostics::graph> graph)
\r
60 : graph_(std::move(graph))
\r
61 , event_subject_("stage")
\r
62 , executor_(L"stage")
\r
64 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
\r
67 std::map<int, draw_frame> operator()(const struct video_format_desc& format_desc)
\r
69 boost::timer frame_timer;
\r
71 auto frames = executor_.invoke([=]() -> std::map<int, draw_frame>
\r
74 std::map<int, class draw_frame> frames;
\r
78 std::vector<int> indices;
\r
80 BOOST_FOREACH(auto& layer, layers_)
\r
82 frames[layer.first] = draw_frame::empty();
\r
83 indices.push_back(layer.first);
\r
86 // WORKAROUND: Compiler doesn't seem to like lambda.
\r
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)));
\r
92 CASPAR_LOG_CURRENT_EXCEPTION();
\r
99 frames_subject_ << frames;
\r
101 graph_->set_value("produce-time", frame_timer.elapsed()*format_desc.fps*0.5);
\r
102 event_subject_ << monitor::event("profiler/time") % frame_timer.elapsed() % (1.0/format_desc.fps);
\r
107 void draw(int index, const video_format_desc& format_desc, std::map<int, draw_frame>& frames)
\r
109 auto& layer = layers_[index];
\r
110 auto& tween = tweens_[index];
\r
112 auto frame = layer.receive(format_desc);
\r
113 auto frame1 = frame;
\r
114 frame1.transform() *= tween.fetch_and_tick(1);
\r
116 if(format_desc.field_mode != core::field_mode::progressive)
\r
118 auto frame2 = frame;
\r
119 frame2.transform() *= tween.fetch_and_tick(1);
\r
120 frame1 = core::draw_frame::interlace(frame1, frame2, format_desc.field_mode);
\r
123 frames[index] = frame1;
\r
126 layer& get_layer(int index)
\r
128 auto it = layers_.find(index);
\r
129 if(it == std::end(layers_))
\r
131 it = layers_.insert(std::make_pair(index, layer(index))).first;
\r
132 it->second.subscribe(event_subject_);
\r
137 boost::unique_future<void> apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)
\r
139 return executor_.begin_invoke([=]
\r
141 BOOST_FOREACH(auto& transform, transforms)
\r
143 auto src = tweens_[std::get<0>(transform)].fetch();
\r
144 auto dst = std::get<1>(transform)(src);
\r
145 tweens_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));
\r
147 }, task_priority::high_priority);
\r
150 boost::unique_future<void> apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)
\r
152 return executor_.begin_invoke([=]
\r
154 auto src = tweens_[index].fetch();
\r
155 auto dst = transform(src);
\r
156 tweens_[index] = tweened_transform(src, dst, mix_duration, tween);
\r
157 }, task_priority::high_priority);
\r
160 boost::unique_future<void> clear_transforms(int index)
\r
162 return executor_.begin_invoke([=]
\r
164 tweens_.erase(index);
\r
165 }, task_priority::high_priority);
\r
168 boost::unique_future<void> clear_transforms()
\r
170 return executor_.begin_invoke([=]
\r
173 }, task_priority::high_priority);
\r
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)
\r
178 return executor_.begin_invoke([=]
\r
180 get_layer(index).load(producer, preview, auto_play_delta);
\r
181 }, task_priority::high_priority);
\r
184 boost::unique_future<void> pause(int index)
\r
186 return executor_.begin_invoke([=]
\r
188 layers_[index].pause();
\r
189 }, task_priority::high_priority);
\r
192 boost::unique_future<void> play(int index)
\r
194 return executor_.begin_invoke([=]
\r
196 layers_[index].play();
\r
197 }, task_priority::high_priority);
\r
200 boost::unique_future<void> stop(int index)
\r
202 return executor_.begin_invoke([=]
\r
204 layers_[index].stop();
\r
205 }, task_priority::high_priority);
\r
208 boost::unique_future<void> clear(int index)
\r
210 return executor_.begin_invoke([=]
\r
212 layers_.erase(index);
\r
213 }, task_priority::high_priority);
\r
216 boost::unique_future<void> clear()
\r
218 return executor_.begin_invoke([=]
\r
221 }, task_priority::high_priority);
\r
224 boost::unique_future<void> swap_layers(stage& other)
\r
226 auto other_impl = other.impl_;
\r
228 if(other_impl.get() == this)
\r
229 return async(launch::deferred, []{});
\r
233 auto layers = layers_ | boost::adaptors::map_values;
\r
234 auto other_layers = other_impl->layers_ | boost::adaptors::map_values;
\r
236 BOOST_FOREACH(auto& layer, layers)
\r
237 layer.unsubscribe(event_subject_);
\r
239 BOOST_FOREACH(auto& layer, other_layers)
\r
240 layer.unsubscribe(event_subject_);
\r
242 std::swap(layers_, other_impl->layers_);
\r
244 BOOST_FOREACH(auto& layer, layers)
\r
245 layer.subscribe(event_subject_);
\r
247 BOOST_FOREACH(auto& layer, other_layers)
\r
248 layer.subscribe(event_subject_);
\r
251 return executor_.begin_invoke([=]
\r
253 other_impl->executor_.invoke(func, task_priority::high_priority);
\r
254 }, task_priority::high_priority);
\r
257 boost::unique_future<void> swap_layer(int index, int other_index)
\r
259 return executor_.begin_invoke([=]
\r
261 std::swap(layers_[index], layers_[other_index]);
\r
262 }, task_priority::high_priority);
\r
265 boost::unique_future<void> swap_layer(int index, int other_index, stage& other)
\r
267 auto other_impl = other.impl_;
\r
269 if(other_impl.get() == this)
\r
270 return swap_layer(index, other_index);
\r
275 auto& my_layer = get_layer(index);
\r
276 auto& other_layer = other_impl->get_layer(other_index);
\r
278 my_layer.unsubscribe(event_subject_);
\r
279 other_layer.unsubscribe(other_impl->event_subject_);
\r
281 std::swap(my_layer, other_layer);
\r
283 my_layer.subscribe(event_subject_);
\r
284 other_layer.subscribe(other_impl->event_subject_);
\r
287 return executor_.begin_invoke([=]
\r
289 other_impl->executor_.invoke(func, task_priority::high_priority);
\r
290 }, task_priority::high_priority);
\r
294 boost::unique_future<spl::shared_ptr<frame_producer>> foreground(int index)
\r
296 return executor_.begin_invoke([=]
\r
298 return layers_[index].foreground();
\r
299 }, task_priority::high_priority);
\r
302 boost::unique_future<spl::shared_ptr<frame_producer>> background(int index)
\r
304 return executor_.begin_invoke([=]
\r
306 return layers_[index].background();
\r
307 }, task_priority::high_priority);
\r
310 boost::unique_future<boost::property_tree::wptree> info()
\r
312 return executor_.begin_invoke([this]() -> boost::property_tree::wptree
\r
314 boost::property_tree::wptree info;
\r
315 BOOST_FOREACH(auto& layer, layers_)
\r
316 info.add_child(L"layers.layer", layer.second.info())
\r
317 .add(L"index", layer.first);
\r
319 }, task_priority::high_priority);
\r
322 boost::unique_future<boost::property_tree::wptree> info(int index)
\r
324 return executor_.begin_invoke([=]
\r
326 return layers_[index].info();
\r
327 }, task_priority::high_priority);
\r
330 boost::unique_future<std::wstring> call(int index, const std::wstring& params)
\r
332 return flatten(executor_.begin_invoke([=]
\r
334 return make_shared(layers_[index].foreground()->call(params));
\r
335 }, task_priority::high_priority));
\r
339 stage::stage(spl::shared_ptr<diagnostics::graph> graph) : impl_(new impl(std::move(graph))){}
\r
340 boost::unique_future<std::wstring> stage::call(int index, const std::wstring& params){return impl_->call(index, params);}
\r
341 boost::unique_future<void> stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){return impl_->apply_transforms(transforms);}
\r
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);}
\r
343 boost::unique_future<void> stage::clear_transforms(int index){return impl_->clear_transforms(index);}
\r
344 boost::unique_future<void> stage::clear_transforms(){return impl_->clear_transforms();}
\r
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);}
\r
346 boost::unique_future<void> stage::pause(int index){return impl_->pause(index);}
\r
347 boost::unique_future<void> stage::play(int index){return impl_->play(index);}
\r
348 boost::unique_future<void> stage::stop(int index){return impl_->stop(index);}
\r
349 boost::unique_future<void> stage::clear(int index){return impl_->clear(index);}
\r
350 boost::unique_future<void> stage::clear(){return impl_->clear();}
\r
351 boost::unique_future<void> stage::swap_layers(stage& other){return impl_->swap_layers(other);}
\r
352 boost::unique_future<void> stage::swap_layer(int index, int other_index){return impl_->swap_layer(index, other_index);}
\r
353 boost::unique_future<void> stage::swap_layer(int index, int other_index, stage& other){return impl_->swap_layer(index, other_index, other);}
\r
354 boost::unique_future<spl::shared_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}
\r
355 boost::unique_future<spl::shared_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}
\r
356 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}
\r
357 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}
\r
358 std::map<int, class draw_frame> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}
\r
359 void stage::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}
\r
360 void stage::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}
\r
361 void stage::subscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.subscribe(o);}
\r
362 void stage::unsubscribe(const frame_observable::observer_ptr& o) {impl_->frames_subject_.unsubscribe(o);}
\r