2 * Copyright 2013 Sveriges Television AB http://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
34 #include <core/consumer/frame_consumer.h>
\r
35 #include <core/consumer/write_frame_consumer.h>
\r
37 #include <boost/foreach.hpp>
\r
38 #include <boost/timer.hpp>
\r
40 #include <tbb/parallel_for_each.h>
\r
41 #include <tbb/concurrent_unordered_map.h>
\r
43 #include <boost/property_tree/ptree.hpp>
\r
47 namespace caspar { namespace core {
\r
49 template<typename T>
\r
50 class tweened_transform
\r
61 , tweener_(get_tweener(L"linear")){}
\r
62 tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")
\r
65 , duration_(duration)
\r
67 , tweener_(get_tweener(tween)){}
\r
69 const T& source() const
\r
74 const T& dest() const
\r
81 return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);
\r
84 T fetch_and_tick(int num)
\r
86 time_ = std::min(time_+num, duration_);
\r
91 struct stage::implementation : public std::enable_shared_from_this<implementation>
\r
92 , boost::noncopyable
\r
94 safe_ptr<diagnostics::graph> graph_;
\r
95 safe_ptr<stage::target_t> target_;
\r
96 video_format_desc format_desc_;
\r
98 boost::timer produce_timer_;
\r
99 boost::timer tick_timer_;
\r
101 std::map<int, std::shared_ptr<layer>> layers_;
\r
102 tbb::concurrent_unordered_map<int, tweened_transform<core::frame_transform>> transforms_;
\r
103 // map of layer -> map of tokens (src ref) -> layer_consumer
\r
104 std::map<int, std::map<void*, std::shared_ptr<write_frame_consumer>>> layer_consumers_;
\r
106 safe_ptr<monitor::subject> monitor_subject_;
\r
108 executor executor_;
\r
111 implementation(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<stage::target_t>& target, const video_format_desc& format_desc)
\r
113 , format_desc_(format_desc)
\r
115 , monitor_subject_(make_safe<monitor::subject>("/stage"))
\r
116 , executor_(L"stage")
\r
118 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f, 0.8));
\r
119 graph_->set_color("produce-time", diagnostics::color(0.0f, 1.0f, 0.0f));
\r
124 std::weak_ptr<implementation> self = shared_from_this();
\r
125 executor_.begin_invoke([=]{tick(self);});
\r
128 void add_layer_consumer(void* token, int layer, const std::shared_ptr<write_frame_consumer>& layer_consumer)
\r
130 executor_.begin_invoke([=]
\r
132 layer_consumers_[layer][token] = layer_consumer;
\r
136 void remove_layer_consumer(void* token, int layer)
\r
138 executor_.begin_invoke([=]
\r
140 auto& layer_map = layer_consumers_[layer];
\r
141 layer_map.erase(token);
\r
142 if (layer_map.empty())
\r
144 layer_consumers_.erase(layer);
\r
149 void tick(const std::weak_ptr<implementation>& self)
\r
153 produce_timer_.restart();
\r
155 std::map<int, safe_ptr<basic_frame>> frames;
\r
157 for(auto it = layers_.begin(); it != layers_.end(); ++it)
\r
158 frames[it->first] = basic_frame::empty();
\r
160 tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](std::map<int, std::shared_ptr<layer>>::value_type& layer)
\r
162 auto transform = transforms_[layer.first].fetch_and_tick(1);
\r
164 int hints = frame_producer::NO_HINT;
\r
165 if(format_desc_.field_mode != field_mode::progressive)
\r
167 hints |= std::abs(transform.fill_scale[1] - 1.0) > 0.0001 ? frame_producer::DEINTERLACE_HINT : frame_producer::NO_HINT;
\r
168 hints |= std::abs(transform.fill_translation[1]) > 0.0001 ? frame_producer::DEINTERLACE_HINT : frame_producer::NO_HINT;
\r
171 if(transform.is_key)
\r
172 hints |= frame_producer::ALPHA_HINT;
\r
174 auto frame = layer.second->receive(hints);
\r
175 auto layer_consumers_it = layer_consumers_.find(layer.first);
\r
176 if (layer_consumers_it != layer_consumers_.end())
\r
178 auto consumer_it = (*layer_consumers_it).second | boost::adaptors::map_values;
\r
179 tbb::parallel_for_each(consumer_it.begin(), consumer_it.end(), [&](decltype(consumer_it[0]) layer_consumer)
\r
181 layer_consumer->send(frame);
\r
185 auto frame1 = make_safe<core::basic_frame>(frame);
\r
186 frame1->get_frame_transform() = transform;
\r
188 if(format_desc_.field_mode != core::field_mode::progressive)
\r
190 auto frame2 = make_safe<core::basic_frame>(frame);
\r
191 frame2->get_frame_transform() = transforms_[layer.first].fetch_and_tick(1);
\r
192 frame1 = core::basic_frame::interlace(frame1, frame2, format_desc_.field_mode);
\r
195 frames[layer.first] = frame1;
\r
198 // Tick the transforms that does not have a corresponding layer.
\r
199 BOOST_FOREACH(auto& elem, transforms_)
\r
200 if (layers_.find(elem.first) == layers_.end())
\r
201 elem.second.fetch_and_tick(format_desc_.field_mode != core::field_mode::progressive ? 2 : 1);
\r
203 graph_->set_value("produce-time", produce_timer_.elapsed()*format_desc_.fps*0.5);
\r
205 std::shared_ptr<void> ticket(nullptr, [self](void*)
\r
207 auto self2 = self.lock();
\r
209 self2->executor_.begin_invoke([=]{tick(self);});
\r
212 target_->send(std::make_pair(frames, ticket));
\r
214 graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);
\r
215 tick_timer_.restart();
\r
220 CASPAR_LOG_CURRENT_EXCEPTION();
\r
224 void set_transform(int index, const frame_transform& transform, unsigned int mix_duration, const std::wstring& tween)
\r
226 executor_.begin_invoke([=]
\r
228 auto src = transforms_[index].fetch();
\r
229 auto dst = transform;
\r
230 transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);
\r
234 void apply_transforms(const std::vector<std::tuple<int, stage::transform_func_t, unsigned int, std::wstring>>& transforms)
\r
236 executor_.begin_invoke([=]
\r
238 BOOST_FOREACH(auto& transform, transforms)
\r
240 auto& tween = transforms_[std::get<0>(transform)];
\r
241 auto src = tween.fetch();
\r
242 auto dst = std::get<1>(transform)(tween.dest());
\r
243 transforms_[std::get<0>(transform)] = tweened_transform<frame_transform>(src, dst, std::get<2>(transform), std::get<3>(transform));
\r
248 void apply_transform(int index, const stage::transform_func_t& transform, unsigned int mix_duration, const std::wstring& tween)
\r
250 executor_.begin_invoke([=]
\r
252 auto src = transforms_[index].fetch();
\r
253 auto dst = transform(src);
\r
254 transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);
\r
258 void clear_transforms(int index)
\r
260 executor_.begin_invoke([=]
\r
262 transforms_.unsafe_erase(index);
\r
266 void clear_transforms()
\r
268 executor_.begin_invoke([=]
\r
270 transforms_.clear();
\r
274 frame_transform get_current_transform(int index)
\r
276 return executor_.invoke([=]
\r
278 return transforms_[index].fetch();
\r
282 layer& get_layer(int index)
\r
284 auto it = layers_.find(index);
\r
285 if(it == std::end(layers_))
\r
287 it = layers_.insert(std::make_pair(index, std::make_shared<layer>(index))).first;
\r
288 it->second->monitor_output().attach_parent(monitor_subject_);
\r
290 return *it->second;
\r
293 void load(int index, const safe_ptr<frame_producer>& producer, bool preview, int auto_play_delta)
\r
295 executor_.begin_invoke([=]
\r
297 get_layer(index).load(producer, preview, auto_play_delta);
\r
301 void pause(int index)
\r
303 executor_.begin_invoke([=]
\r
305 get_layer(index).pause();
\r
309 void play(int index)
\r
311 executor_.begin_invoke([=]
\r
313 get_layer(index).play();
\r
317 void stop(int index)
\r
319 executor_.begin_invoke([=]
\r
321 get_layer(index).stop();
\r
325 void clear(int index)
\r
327 executor_.begin_invoke([=]
\r
329 layers_.erase(index);
\r
335 executor_.begin_invoke([=]
\r
341 boost::unique_future<std::wstring> call(int index, bool foreground, const std::wstring& param)
\r
343 return std::move(*executor_.invoke([=]
\r
345 return std::make_shared<boost::unique_future<std::wstring>>(std::move(get_layer(index).call(foreground, param)));
\r
346 }, high_priority));
\r
349 void swap_layers(stage& other)
\r
351 auto other_impl = other.impl_;
\r
353 if(other_impl.get() == this)
\r
358 auto layers = layers_ | boost::adaptors::map_values;
\r
359 auto other_layers = other_impl->layers_ | boost::adaptors::map_values;
\r
361 BOOST_FOREACH(auto& layer, layers)
\r
362 layer->monitor_output().detach_parent();
\r
364 BOOST_FOREACH(auto& layer, other_layers)
\r
365 layer->monitor_output().attach_parent(monitor_subject_);
\r
367 std::swap(layers_, other_impl->layers_);
\r
369 BOOST_FOREACH(auto& layer, layers)
\r
370 layer->monitor_output().detach_parent();
\r
372 BOOST_FOREACH(auto& layer, other_layers)
\r
373 layer->monitor_output().detach_parent();
\r
376 executor_.begin_invoke([=]
\r
378 other_impl->executor_.invoke(func, task_priority::high_priority);
\r
379 }, task_priority::high_priority);
\r
382 void swap_layer(int index, int other_index)
\r
384 executor_.begin_invoke([=]
\r
386 std::swap(get_layer(index), get_layer(other_index));
\r
387 }, task_priority::high_priority);
\r
390 void swap_layer(int index, int other_index, stage& other)
\r
392 auto other_impl = other.impl_;
\r
394 if(other_impl.get() == this)
\r
395 swap_layer(index, other_index);
\r
400 auto& my_layer = get_layer(index);
\r
401 auto& other_layer = other_impl->get_layer(other_index);
\r
403 my_layer.monitor_output().detach_parent();
\r
404 other_layer.monitor_output().attach_parent(other_impl->monitor_subject_);
\r
406 std::swap(my_layer, other_layer);
\r
408 my_layer.monitor_output().detach_parent();
\r
409 other_layer.monitor_output().attach_parent(other_impl->monitor_subject_);
\r
412 executor_.begin_invoke([=]
\r
414 other_impl->executor_.invoke(func, task_priority::high_priority);
\r
415 }, task_priority::high_priority);
\r
419 boost::unique_future<safe_ptr<frame_producer>> foreground(int index)
\r
421 return executor_.begin_invoke([=]
\r
423 return get_layer(index).foreground();
\r
427 boost::unique_future<safe_ptr<frame_producer>> background(int index)
\r
429 return executor_.begin_invoke([=]
\r
431 return get_layer(index).background();
\r
435 void set_video_format_desc(const video_format_desc& format_desc)
\r
437 executor_.begin_invoke([=]
\r
439 format_desc_ = format_desc;
\r
443 boost::unique_future<boost::property_tree::wptree> info()
\r
445 return std::move(executor_.begin_invoke([this]() -> boost::property_tree::wptree
\r
447 boost::property_tree::wptree info;
\r
448 BOOST_FOREACH(auto& layer, layers_)
\r
449 info.add_child(L"layers.layer", layer.second->info())
\r
450 .add(L"index", layer.first);
\r
452 }, high_priority));
\r
455 boost::unique_future<boost::property_tree::wptree> info(int index)
\r
457 return std::move(executor_.begin_invoke([=]() -> boost::property_tree::wptree
\r
459 return get_layer(index).info();
\r
460 }, high_priority));
\r
463 boost::unique_future<boost::property_tree::wptree> delay_info()
\r
465 return std::move(executor_.begin_invoke([this]() -> boost::property_tree::wptree
\r
467 boost::property_tree::wptree info;
\r
468 BOOST_FOREACH(auto& layer, layers_)
\r
469 info.add_child(L"layer", layer.second->delay_info())
\r
470 .add(L"index", layer.first);
\r
472 }, high_priority));
\r
475 boost::unique_future<boost::property_tree::wptree> delay_info(int index)
\r
477 return std::move(executor_.begin_invoke([=]() -> boost::property_tree::wptree
\r
479 return get_layer(index).delay_info();
\r
480 }, high_priority));
\r
484 stage::stage(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<target_t>& target, const video_format_desc& format_desc)
\r
485 : impl_(new implementation(graph, target, format_desc)){}
\r
486 void stage::apply_transforms(const std::vector<stage::transform_tuple_t>& transforms){impl_->apply_transforms(transforms);}
\r
487 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
488 void stage::clear_transforms(int index){impl_->clear_transforms(index);}
\r
489 void stage::clear_transforms(){impl_->clear_transforms();}
\r
490 frame_transform stage::get_current_transform(int index) { return impl_->get_current_transform(index); }
\r
491 void stage::spawn_token(){impl_->spawn_token();}
\r
492 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
493 void stage::pause(int index){impl_->pause(index);}
\r
494 void stage::play(int index){impl_->play(index);}
\r
495 void stage::stop(int index){impl_->stop(index);}
\r
496 void stage::clear(int index){impl_->clear(index);}
\r
497 void stage::clear(){impl_->clear();}
\r
498 void stage::swap_layers(const safe_ptr<stage>& other){impl_->swap_layers(*other);}
\r
499 void stage::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}
\r
500 void stage::swap_layer(int index, size_t other_index, const safe_ptr<stage>& other){impl_->swap_layer(index, other_index, *other);}
\r
501 void stage::add_layer_consumer(void* token, int layer, const std::shared_ptr<write_frame_consumer>& layer_consumer){impl_->add_layer_consumer(token, layer, layer_consumer);}
\r
502 void stage::remove_layer_consumer(void* token, int layer){impl_->remove_layer_consumer(token, layer);}
\r
503 boost::unique_future<safe_ptr<frame_producer>> stage::foreground(int index) {return impl_->foreground(index);}
\r
504 boost::unique_future<safe_ptr<frame_producer>> stage::background(int index) {return impl_->background(index);}
\r
505 boost::unique_future<std::wstring> stage::call(int index, bool foreground, const std::wstring& param){return impl_->call(index, foreground, param);}
\r
506 void stage::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}
\r
507 boost::unique_future<boost::property_tree::wptree> stage::info() const{return impl_->info();}
\r
508 boost::unique_future<boost::property_tree::wptree> stage::info(int index) const{return impl_->info(index);}
\r
509 boost::unique_future<boost::property_tree::wptree> stage::delay_info() const{return impl_->delay_info();}
\r
510 boost::unique_future<boost::property_tree::wptree> stage::delay_info(int index) const{return impl_->delay_info(index);}
\r
511 monitor::subject& stage::monitor_output(){return *impl_->monitor_subject_;}
\r