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/concurrency/executor.h>
\r
32 #include <common/diagnostics/graph.h>
\r
34 #include <core/frame/frame_transform.h>
\r
36 #include <boost/foreach.hpp>
\r
37 #include <boost/timer.hpp>
\r
38 #include <boost/property_tree/ptree.hpp>
\r
39 #include <boost/range/algorithm_ext.hpp>
\r
41 #include <tbb/parallel_for_each.h>
\r
43 #include <functional>
\r
47 namespace caspar { namespace core {
\r
49 struct stage::impl : public std::enable_shared_from_this<impl>
\r
51 spl::shared_ptr<diagnostics::graph> graph_;
\r
52 spl::shared_ptr<monitor::subject> event_subject_;
\r
53 std::map<int, layer> layers_;
\r
54 std::map<int, tweened_transform> tweens_;
\r
57 impl(spl::shared_ptr<diagnostics::graph> graph)
\r
58 : graph_(std::move(graph))
\r
59 , executor_(L"stage")
\r
61 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
\r
64 std::map<int, spl::shared_ptr<draw_frame>> operator()(const struct video_format_desc& format_desc)
\r
66 return executor_.invoke([=]() -> std::map<int, spl::shared_ptr<draw_frame>>
\r
68 boost::timer frame_timer;
\r
70 std::map<int, spl::shared_ptr<class draw_frame>> frames;
\r
74 std::vector<int> indices;
\r
76 BOOST_FOREACH(auto& layer, layers_)
\r
78 frames[layer.first] = draw_frame::empty();
\r
79 indices.push_back(layer.first);
\r
82 // WORKAROUND: Compiler doesn't seem to like lambda.
\r
83 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
88 CASPAR_LOG_CURRENT_EXCEPTION();
\r
91 graph_->set_value("produce-time", frame_timer.elapsed()*format_desc.fps*0.5);
\r
92 *event_subject_ << monitor::event("profiler/time") % frame_timer.elapsed() % (1.0/format_desc.fps);
\r
98 void draw(int index, const video_format_desc& format_desc, std::map<int, spl::shared_ptr<draw_frame>>& frames)
\r
100 auto& layer = layers_[index];
\r
101 auto& tween = tweens_[index];
\r
102 auto transform = tween.fetch_and_tick(1);
\r
104 frame_producer::flags flags = frame_producer::flags::none;
\r
105 if(format_desc.field_mode != field_mode::progressive)
\r
107 flags |= std::abs(transform.fill_scale[1] - 1.0) > 0.0001 ? frame_producer::flags::deinterlace : frame_producer::flags::none;
\r
108 flags |= std::abs(transform.fill_translation[1]) > 0.0001 ? frame_producer::flags::deinterlace : frame_producer::flags::none;
\r
111 if(transform.is_key)
\r
112 flags |= frame_producer::flags::alpha_only;
\r
114 auto frame = layer.receive(flags, format_desc);
\r
116 auto frame = spl::make_shared<core::draw_frame>(frame);
\r
117 frame->get_frame_transform() = transform;
\r
119 if(format_desc.field_mode != core::field_mode::progressive)
\r
121 auto frame2 = spl::make_shared<core::draw_frame>(frame);
\r
122 frame2->get_frame_transform() = tween.fetch_and_tick(1);
\r
123 frame = core::draw_frame::interlace(frame, frame2, format_desc.field_mode);
\r
126 frames[index] = frame;
\r
129 layer& get_layer(int index)
\r
131 auto it = layers_.find(index);
\r
132 if(it == std::end(layers_))
\r
134 it = layers_.insert(std::make_pair(index, layer(index))).first;
\r
135 it->second.subscribe(event_subject_);
\r
140 void apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, tweener>>& transforms)
\r
142 executor_.begin_invoke([=]
\r
144 BOOST_FOREACH(auto& transform, transforms)
\r
146 auto src = tweens_[std::get<0>(transform)].fetch();
\r
147 auto dst = std::get<1>(transform)(src);
\r
148 tweens_[std::get<0>(transform)] = tweened_transform(src, dst, std::get<2>(transform), std::get<3>(transform));
\r
150 }, task_priority::high_priority);
\r
153 void apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const tweener& tween)
\r
155 executor_.begin_invoke([=]
\r
157 auto src = tweens_[index].fetch();
\r
158 auto dst = transform(src);
\r
159 tweens_[index] = tweened_transform(src, dst, mix_duration, tween);
\r
160 }, task_priority::high_priority);
\r
163 void clear_transforms(int index)
\r
165 executor_.begin_invoke([=]
\r
167 tweens_.erase(index);
\r
168 }, task_priority::high_priority);
\r
171 void clear_transforms()
\r
173 executor_.begin_invoke([=]
\r
176 }, task_priority::high_priority);
\r
179 void load(int index, const spl::shared_ptr<frame_producer>& producer, const boost::optional<int32_t>& auto_play_delta)
\r
181 executor_.begin_invoke([=]
\r
183 get_layer(index).load(producer, auto_play_delta);
\r
184 }, task_priority::high_priority);
\r
187 void pause(int index)
\r
189 executor_.begin_invoke([=]
\r
191 layers_[index].pause();
\r
192 }, task_priority::high_priority);
\r
195 void play(int index)
\r
197 executor_.begin_invoke([=]
\r
199 layers_[index].play();
\r
200 }, task_priority::high_priority);
\r
203 void stop(int index)
\r
205 executor_.begin_invoke([=]
\r
207 layers_[index].stop();
\r
208 }, task_priority::high_priority);
\r
211 void clear(int index)
\r
213 executor_.begin_invoke([=]
\r
215 layers_.erase(index);
\r
216 }, task_priority::high_priority);
\r
221 executor_.begin_invoke([=]
\r
224 }, task_priority::high_priority);
\r
227 void swap_layers(stage& other)
\r
229 auto other_impl = other.impl_;
\r
231 if(other_impl.get() == this)
\r
236 auto layers = layers_ | boost::adaptors::map_values;
\r
237 auto other_layers = other_impl->layers_ | boost::adaptors::map_values;
\r
239 BOOST_FOREACH(auto& layer, layers)
\r
240 layer.unsubscribe(event_subject_);
\r
242 BOOST_FOREACH(auto& layer, other_layers)
\r
243 layer.unsubscribe(event_subject_);
\r
245 std::swap(layers_, other_impl->layers_);
\r
247 BOOST_FOREACH(auto& layer, layers)
\r
248 layer.subscribe(event_subject_);
\r
250 BOOST_FOREACH(auto& layer, other_layers)
\r
251 layer.subscribe(event_subject_);
\r
253 executor_.begin_invoke([=]
\r
255 other_impl->executor_.invoke(func, task_priority::high_priority);
\r
256 }, task_priority::high_priority);
\r
259 void swap_layer(int index, int other_index)
\r
261 executor_.begin_invoke([=]
\r
263 std::swap(layers_[index], layers_[other_index]);
\r
264 }, task_priority::high_priority);
\r
267 void swap_layer(int index, int other_index, stage& other)
\r
269 auto other_impl = other.impl_;
\r
271 if(other_impl.get() == this)
\r
272 swap_layer(index, other_index);
\r
277 auto& my_layer = get_layer(index);
\r
278 auto& other_layer = other_impl->get_layer(other_index);
\r
280 my_layer.unsubscribe(event_subject_);
\r
281 other_layer.unsubscribe(other_impl->event_subject_);
\r
283 std::swap(my_layer, other_layer);
\r
285 my_layer.subscribe(event_subject_);
\r
286 other_layer.subscribe(other_impl->event_subject_);
\r
288 executor_.begin_invoke([=]
\r
290 other_impl->executor_.invoke(func, task_priority::high_priority);
\r
291 }, task_priority::high_priority);
\r
295 boost::unique_future<spl::shared_ptr<frame_producer>> foreground(int index)
\r
297 return executor_.begin_invoke([=]
\r
299 return layers_[index].foreground();
\r
300 }, task_priority::high_priority);
\r
303 boost::unique_future<spl::shared_ptr<frame_producer>> background(int index)
\r
305 return executor_.begin_invoke([=]
\r
307 return layers_[index].background();
\r
308 }, task_priority::high_priority);
\r
311 boost::unique_future<boost::property_tree::wptree> info()
\r
313 return executor_.begin_invoke([this]() -> boost::property_tree::wptree
\r
315 boost::property_tree::wptree info;
\r
316 BOOST_FOREACH(auto& layer, layers_)
\r
317 info.add_child(L"layers.layer", layer.second.info())
\r
318 .add(L"index", layer.first);
\r
320 }, task_priority::high_priority);
\r
323 boost::unique_future<boost::property_tree::wptree> info(int index)
\r
325 return executor_.begin_invoke([=]
\r
327 return layers_[index].info();
\r
328 }, task_priority::high_priority);
\r
332 stage::stage(spl::shared_ptr<diagnostics::graph> graph) : impl_(new impl(std::move(graph))){}
\r
333 void stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){impl_->apply_transforms(transforms);}
\r
334 void stage::apply_transform(int index, const std::function<core::frame_transform(core::frame_transform)>& transform, unsigned int mix_duration, const tweener& tween){impl_->apply_transform(index, transform, mix_duration, tween);}
\r
335 void stage::clear_transforms(int index){impl_->clear_transforms(index);}
\r
336 void stage::clear_transforms(){impl_->clear_transforms();}
\r
337 void stage::load(int index, const spl::shared_ptr<frame_producer>& producer, const boost::optional<int32_t>& auto_play_delta){impl_->load(index, producer, auto_play_delta);}
\r
338 void stage::pause(int index){impl_->pause(index);}
\r
339 void stage::play(int index){impl_->play(index);}
\r
340 void stage::stop(int index){impl_->stop(index);}
\r
341 void stage::clear(int index){impl_->clear(index);}
\r
342 void stage::clear(){impl_->clear();}
\r
343 void stage::swap_layers(stage& other){impl_->swap_layers(other);}
\r
344 void stage::swap_layer(int index, int other_index){impl_->swap_layer(index, other_index);}
\r
345 void stage::swap_layer(int index, int other_index, stage& other){impl_->swap_layer(index, other_index, other);}
\r
346 boost::unique_future<spl::shared_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}
\r
347 boost::unique_future<spl::shared_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}
\r
348 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}
\r
349 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}
\r
350 std::map<int, spl::shared_ptr<class draw_frame>> stage::operator()(const video_format_desc& format_desc){return (*impl_)(format_desc);}
\r
351 void stage::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_->subscribe(o);}
\r
352 void stage::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_->unsubscribe(o);}
\r