1 #include "../StdAfx.h"
\r
3 #include "frame_producer_device.h"
\r
5 #include "../mixer/frame/draw_frame.h"
\r
6 #include "../mixer/frame_factory.h"
\r
10 #include <common/concurrency/executor.h>
\r
11 #include <common/utility/printer.h>
\r
13 #include <boost/range/algorithm_ext/erase.hpp>
\r
14 #include <boost/lexical_cast.hpp>
\r
16 #include <tbb/parallel_for.h>
\r
17 #include <tbb/mutex.h>
\r
21 #include <unordered_map>
\r
23 namespace caspar { namespace core {
\r
25 struct frame_producer_device::implementation : boost::noncopyable
\r
27 const printer parent_printer_;
\r
29 std::unordered_map<int, layer> layers_;
\r
31 output_func output_;
\r
33 const safe_ptr<frame_factory> factory_;
\r
35 mutable executor executor_;
\r
37 implementation(const printer& parent_printer, const safe_ptr<frame_factory>& factory, const output_func& output)
\r
38 : parent_printer_(parent_printer)
\r
43 executor_.begin_invoke([=]{tick();});
\r
48 CASPAR_LOG(info) << "Shutting down producer-device.";
\r
54 executor_.begin_invoke([=]{tick();});
\r
57 std::vector<safe_ptr<draw_frame>> draw()
\r
59 std::vector<safe_ptr<draw_frame>> frames(layers_.size(), draw_frame::empty());
\r
60 tbb::parallel_for(tbb::blocked_range<size_t>(0, frames.size(), 1), [&](const tbb::blocked_range<size_t>& r)
\r
62 auto it = layers_.begin();
\r
63 std::advance(it, r.begin());
\r
64 for(size_t i = r.begin(); i != r.end(); ++i, ++it)
\r
66 frames[i] = it->second.receive();
\r
67 frames[i]->set_layer_index(i);
\r
70 boost::range::remove_erase(frames, draw_frame::empty());
\r
74 void load(int index, const safe_ptr<frame_producer>& producer, bool play_on_load, bool preview)
\r
76 producer->set_parent_printer(std::bind(&layer::print, &layers_[index]));
\r
77 producer->initialize(factory_);
\r
78 executor_.invoke([&]{layers_[index].load(producer, play_on_load, preview);});
\r
81 void pause(int index)
\r
83 executor_.invoke([&]{layers_[index].pause();});
\r
86 void play(int index)
\r
88 executor_.invoke([&]{layers_[index].play();});
\r
91 void stop(int index)
\r
93 executor_.invoke([&]{layers_[index].stop();});
\r
96 void clear(int index)
\r
98 executor_.invoke([&]{layers_[index].clear();});
\r
103 executor_.invoke([&]
\r
105 BOOST_FOREACH(auto& pair, layers_)
\r
106 pair.second.clear();
\r
110 void swap_layer(int index, size_t other_index)
\r
112 executor_.invoke([&]
\r
114 layers_[index].swap(layers_[other_index]);
\r
118 void swap_layer(int index, size_t other_index, frame_producer_device& other)
\r
120 if(other.impl_.get() == this)
\r
121 swap_layer(index, other_index);
\r
126 layers_[index].swap(other.impl_->layers_.at(other_index));
\r
128 CASPAR_LOG(info) << print() << L" Swapped layer " << index << L" with " << other.impl_->print() << L" layer " << other_index << L".";
\r
131 executor_.invoke([&]{other.impl_->executor_.invoke(func);});
\r
135 void swap(frame_producer_device& other)
\r
137 if(other.impl_.get() == this)
\r
142 std::set<int> my_indices;
\r
143 BOOST_FOREACH(auto& pair, layers_)
\r
144 my_indices.insert(pair.first);
\r
146 std::set<int> other_indicies;
\r
147 BOOST_FOREACH(auto& pair, other.impl_->layers_)
\r
148 other_indicies.insert(pair.first);
\r
150 std::vector<int> indices;
\r
151 std::set_union(my_indices.begin(), my_indices.end(), other_indicies.begin(), other_indicies.end(), std::back_inserter(indices));
\r
153 BOOST_FOREACH(auto index, indices)
\r
154 layers_[index].swap(other.impl_->layers_[index]);
\r
156 CASPAR_LOG(info) << print() << L" Swapped layers with " << other.impl_->print() << L".";
\r
159 executor_.invoke([&]{other.impl_->executor_.invoke(func);});
\r
162 boost::unique_future<safe_ptr<frame_producer>> foreground(int index) const
\r
164 return executor_.begin_invoke([=]() mutable -> safe_ptr<frame_producer>
\r
166 auto it = layers_.find(index);
\r
167 return it != layers_.end() ? it->second.foreground() : frame_producer::empty();
\r
171 std::wstring print() const
\r
173 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"producer";
\r
177 frame_producer_device::frame_producer_device(const printer& parent_printer, const safe_ptr<frame_factory>& factory, const output_func& output) : impl_(new implementation(parent_printer, factory, output)){}
\r
178 frame_producer_device::frame_producer_device(frame_producer_device&& other) : impl_(std::move(other.impl_)){}
\r
179 void frame_producer_device::swap(frame_producer_device& other){impl_->swap(other);}
\r
180 void frame_producer_device::load(int index, const safe_ptr<frame_producer>& producer, bool play_on_load, bool preview){impl_->load(index, producer, play_on_load, preview);}
\r
181 void frame_producer_device::pause(int index){impl_->pause(index);}
\r
182 void frame_producer_device::play(int index){impl_->play(index);}
\r
183 void frame_producer_device::stop(int index){impl_->stop(index);}
\r
184 void frame_producer_device::clear(int index){impl_->clear(index);}
\r
185 void frame_producer_device::clear(){impl_->clear();}
\r
186 void frame_producer_device::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}
\r
187 void frame_producer_device::swap_layer(int index, size_t other_index, frame_producer_device& other){impl_->swap_layer(index, other_index, other);}
\r
188 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::foreground(size_t index) const{ return impl_->foreground(index);}
\r