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/basic_frame.h"
\r
29 #include "frame/frame_factory.h"
\r
31 #include <common/concurrency/executor.h>
\r
33 #include <core/producer/frame/frame_transform.h>
\r
35 #include <boost/foreach.hpp>
\r
36 #include <boost/timer.hpp>
\r
38 #include <tbb/parallel_for_each.h>
\r
39 #include <tbb/concurrent_unordered_map.h>
\r
41 #include <boost/property_tree/ptree.hpp>
\r
45 namespace caspar { namespace core {
\r
47 template<typename T>
\r
48 class tweened_transform
\r
59 , tweener_(get_tweener(L"linear")){}
\r
60 tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")
\r
63 , duration_(duration)
\r
65 , tweener_(get_tweener(tween)){}
\r
67 const T& source() const
\r
72 const T& dest() const
\r
79 return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);
\r
82 T fetch_and_tick(int num)
\r
84 time_ = std::min(time_+num, duration_);
\r
89 struct stage::implementation : public std::enable_shared_from_this<implementation>
\r
90 , boost::noncopyable
\r
92 safe_ptr<diagnostics::graph> graph_;
\r
93 safe_ptr<stage::target_t> target_;
\r
94 video_format_desc format_desc_;
\r
96 boost::timer produce_timer_;
\r
97 boost::timer tick_timer_;
\r
99 std::map<int, layer> layers_;
\r
100 tbb::concurrent_unordered_map<int, tweened_transform<core::frame_transform>> transforms_;
\r
102 executor executor_;
\r
104 implementation(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<stage::target_t>& target, const video_format_desc& format_desc)
\r
106 , format_desc_(format_desc)
\r
108 , executor_(L"stage")
\r
110 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f, 0.8));
\r
111 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
\r
116 std::weak_ptr<implementation> self = shared_from_this();
\r
117 executor_.begin_invoke([=]{tick(self);});
\r
120 void tick(const std::weak_ptr<implementation>& self)
\r
124 produce_timer_.restart();
\r
126 std::map<int, safe_ptr<basic_frame>> frames;
\r
128 BOOST_FOREACH(auto& layer, layers_)
\r
129 frames[layer.first] = basic_frame::empty();
\r
131 tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, layer>::value_type& layer)
\r
133 auto transform = transforms_[layer.first].fetch_and_tick(1);
\r
135 int hints = frame_producer::NO_HINT;
\r
136 if(format_desc_.field_mode != field_mode::progressive)
\r
138 hints |= std::abs(transform.fill_scale[1] - 1.0) > 0.0001 ? frame_producer::DEINTERLACE_HINT : frame_producer::NO_HINT;
\r
139 hints |= std::abs(transform.fill_translation[1]) > 0.0001 ? frame_producer::DEINTERLACE_HINT : frame_producer::NO_HINT;
\r
142 if(transform.is_key)
\r
143 hints |= frame_producer::ALPHA_HINT;
\r
145 auto frame = layer.second.receive(hints);
\r
147 auto frame1 = make_safe<core::basic_frame>(frame);
\r
148 frame1->get_frame_transform() = transform;
\r
150 if(format_desc_.field_mode != core::field_mode::progressive)
\r
152 auto frame2 = make_safe<core::basic_frame>(frame);
\r
153 frame2->get_frame_transform() = transforms_[layer.first].fetch_and_tick(1);
\r
154 frame1 = core::basic_frame::interlace(frame1, frame2, format_desc_.field_mode);
\r
157 frames[layer.first] = frame1;
\r
160 graph_->set_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);
\r
162 std::shared_ptr<void> ticket(nullptr, [self](void*)
\r
164 auto self2 = self.lock();
\r
166 self2->executor_.begin_invoke([=]{tick(self);});
\r
169 target_->send(std::make_pair(frames, ticket));
\r
171 graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);
\r
172 tick_timer_.restart();
\r
177 CASPAR_LOG_CURRENT_EXCEPTION();
\r
181 void set_transform(int index, const frame_transform& transform, unsigned int mix_duration, const std::wstring& tween)
\r
183 executor_.begin_invoke([=]
\r
185 auto src = transforms_[index].fetch();
\r
186 auto dst = transform;
\r
187 transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);
\r
191 void apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, std::wstring>>& transforms)
\r
193 executor_.begin_invoke([=]
\r
195 BOOST_FOREACH(auto& transform, transforms)
\r
197 auto& tween = transforms_[std::get<0>(transform)];
\r
198 auto src = tween.fetch();
\r
199 auto dst = std::get<1>(transform)(tween.dest());
\r
200 transforms_[std::get<0>(transform)] = tweened_transform<frame_transform>(src, dst, std::get<2>(transform), std::get<3>(transform));
\r
205 void apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const std::wstring& tween)
\r
207 executor_.begin_invoke([=]
\r
209 auto src = transforms_[index].fetch();
\r
210 auto dst = transform(src);
\r
211 transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);
\r
215 void clear_transforms(int index)
\r
217 executor_.begin_invoke([=]
\r
219 transforms_[index] = tweened_transform<core::frame_transform>();
\r
223 void clear_transforms()
\r
225 executor_.begin_invoke([=]
\r
227 transforms_.clear();
\r
231 void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)
\r
233 executor_.begin_invoke([=]
\r
235 layers_[index].load(producer, preview, auto_play_delta);
\r
239 void pause(int index)
\r
241 executor_.begin_invoke([=]
\r
243 layers_[index].pause();
\r
247 void play(int index)
\r
249 executor_.begin_invoke([=]
\r
251 layers_[index].play();
\r
255 void stop(int index)
\r
257 executor_.begin_invoke([=]
\r
259 layers_[index].stop();
\r
263 void clear(int index)
\r
265 executor_.begin_invoke([=]
\r
267 layers_.erase(index);
\r
273 executor_.begin_invoke([=]
\r
279 boost::unique_future<std::wstring> call(int index, bool foreground, const std::wstring& param)
\r
281 return std::move(*executor_.invoke([=]
\r
283 return std::make_shared<boost::unique_future<std::wstring>>(std::move(layers_[index].call(foreground, param)));
\r
284 }, high_priority));
\r
287 void swap_layers(const safe_ptr<stage>& other)
\r
289 if(other->impl_.get() == this)
\r
294 std::swap(layers_, other->impl_->layers_);
\r
296 executor_.begin_invoke([=]
\r
298 other->impl_->executor_.invoke(func, high_priority);
\r
302 void swap_layer(int index, size_t other_index)
\r
304 executor_.begin_invoke([=]
\r
306 std::swap(layers_[index], layers_[other_index]);
\r
310 void swap_layer(int index, size_t other_index, const safe_ptr<stage>& other)
\r
312 if(other->impl_.get() == this)
\r
313 swap_layer(index, other_index);
\r
318 std::swap(layers_[index], other->impl_->layers_[other_index]);
\r
320 executor_.begin_invoke([=]
\r
322 other->impl_->executor_.invoke(func, high_priority);
\r
327 boost::unique_future<safe_ptr<frame_producer>> foreground(int index)
\r
329 return executor_.begin_invoke([=]
\r
331 return layers_[index].foreground();
\r
335 boost::unique_future<safe_ptr<frame_producer>> background(int index)
\r
337 return executor_.begin_invoke([=]
\r
339 return layers_[index].background();
\r
343 void set_video_format_desc(const video_format_desc& format_desc)
\r
345 executor_.begin_invoke([=]
\r
347 format_desc_ = format_desc;
\r
351 boost::unique_future<boost::property_tree::wptree> info()
\r
353 return std::move(executor_.begin_invoke([this]() -> boost::property_tree::wptree
\r
355 boost::property_tree::wptree info;
\r
356 BOOST_FOREACH(auto& layer, layers_)
\r
357 info.add_child(L"layers.layer", layer.second.info())
\r
358 .add(L"index", layer.first);
\r
360 }, high_priority));
\r
363 boost::unique_future<boost::property_tree::wptree> info(int index)
\r
365 return std::move(executor_.begin_invoke([=]() -> boost::property_tree::wptree
\r
367 return layers_[index].info();
\r
368 }, high_priority));
\r
372 stage::stage(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<target_t>& target, const video_format_desc& format_desc) : impl_(new implementation(graph, target, format_desc)){}
\r
373 void stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){impl_->apply_transforms(transforms);}
\r
374 void stage::apply_transform(int index, const std::function<core::frame_transform(core::frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween){impl_->apply_transform(index, transform, mix_duration, tween);}
\r
375 void stage::clear_transforms(int index){impl_->clear_transforms(index);}
\r
376 void stage::clear_transforms(){impl_->clear_transforms();}
\r
377 void stage::spawn_token(){impl_->spawn_token();}
\r
378 void stage::load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta){impl_->load(index, producer, preview, auto_play_delta);}
\r
379 void stage::pause(int index){impl_->pause(index);}
\r
380 void stage::play(int index){impl_->play(index);}
\r
381 void stage::stop(int index){impl_->stop(index);}
\r
382 void stage::clear(int index){impl_->clear(index);}
\r
383 void stage::clear(){impl_->clear();}
\r
384 void stage::swap_layers(const safe_ptr<stage>& other){impl_->swap_layers(other);}
\r
385 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}
\r
386 void stage::swap_layer(int index, size_t other_index, const safe_ptr<stage>& other){impl_->swap_layer(index, other_index, other);}
\r
387 boost::unique_future<safe_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}
\r
388 boost::unique_future<safe_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}
\r
389 boost::unique_future<std::wstring> stage::call(int index, bool foreground, const std::wstring& param){return impl_->call(index, foreground, param);}
\r
390 void stage::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}
\r
391 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}
\r
392 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}
\r