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
23 namespace caspar { namespace core {
\r
25 struct frame_producer_device::implementation : boost::noncopyable
\r
27 const printer parent_printer_;
\r
29 std::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 layer& get_layer(int index)
\r
59 auto it = layers_.find(index);
\r
60 if(it == layers_.end())
\r
61 it = layers_.insert(std::make_pair(index, layer(index, std::bind(&implementation::print, this)))).first;
\r
65 std::vector<safe_ptr<draw_frame>> draw()
\r
67 std::vector<safe_ptr<draw_frame>> frames(layers_.size(), draw_frame::empty());
\r
68 tbb::parallel_for(tbb::blocked_range<size_t>(0, frames.size(), 1), [&](const tbb::blocked_range<size_t>& r)
\r
70 auto it = layers_.begin();
\r
71 std::advance(it, r.begin());
\r
72 for(size_t i = r.begin(); i != r.end(); ++i, ++it)
\r
74 frames[i] = it->second.receive();
\r
75 frames[i]->set_layer_index(it->first);
\r
78 boost::range::remove_erase(frames, draw_frame::empty());
\r
82 void load(int index, const safe_ptr<frame_producer>& producer, bool play_on_load, bool preview)
\r
84 producer->set_parent_printer(std::bind(&layer::print, &get_layer(index)));
\r
85 producer->initialize(factory_);
\r
86 executor_.invoke([&]{get_layer(index).load(producer, play_on_load, preview);});
\r
89 void pause(int index)
\r
91 executor_.invoke([&]{get_layer(index).pause();});
\r
94 void play(int index)
\r
96 executor_.invoke([&]{get_layer(index).play();});
\r
99 void stop(int index)
\r
101 executor_.invoke([&]{get_layer(index).stop();});
\r
104 void clear(int index)
\r
106 executor_.invoke([&]{layers_.erase(index);});
\r
111 executor_.invoke([&]{layers_.clear();});
\r
114 void swap_layer(int index, size_t other_index)
\r
116 executor_.invoke([&]
\r
118 get_layer(index).swap(layers_[other_index]);
\r
122 void swap_layer(int index, size_t other_index, frame_producer_device& other)
\r
124 if(other.impl_.get() == this)
\r
125 swap_layer(index, other_index);
\r
128 if(factory_->get_video_format_desc() != other.impl_->factory_->get_video_format_desc())
\r
129 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot swap between channels with different formats."));
\r
133 get_layer(index).swap(other.impl_->layers_.at(other_index));
\r
135 CASPAR_LOG(info) << print() << L" Swapped layer " << index << L" with " << other.impl_->print() << L" layer " << other_index << L".";
\r
138 executor_.invoke([&]{other.impl_->executor_.invoke(func);});
\r
142 void swap(frame_producer_device& other)
\r
144 if(other.impl_.get() == this)
\r
147 if(factory_->get_video_format_desc() != other.impl_->factory_->get_video_format_desc())
\r
148 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot swap between channels with different formats."));
\r
152 std::set<int> my_indices;
\r
153 BOOST_FOREACH(auto& pair, layers_)
\r
154 my_indices.insert(pair.first);
\r
156 std::set<int> other_indicies;
\r
157 BOOST_FOREACH(auto& pair, other.impl_->layers_)
\r
158 other_indicies.insert(pair.first);
\r
160 std::vector<int> indices;
\r
161 std::set_union(my_indices.begin(), my_indices.end(), other_indicies.begin(), other_indicies.end(), std::back_inserter(indices));
\r
163 BOOST_FOREACH(auto index, indices)
\r
164 get_layer(index).swap(other.impl_->get_layer(index));
\r
166 CASPAR_LOG(info) << print() << L" Swapped layers with " << other.impl_->print() << L".";
\r
169 executor_.invoke([&]{other.impl_->executor_.invoke(func);});
\r
172 boost::unique_future<safe_ptr<frame_producer>> foreground(int index) const
\r
174 return executor_.begin_invoke([=]() mutable -> safe_ptr<frame_producer>
\r
176 auto it = layers_.find(index);
\r
177 return it != layers_.end() ? it->second.foreground() : frame_producer::empty();
\r
181 std::wstring print() const
\r
183 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"producer";
\r
187 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
188 frame_producer_device::frame_producer_device(frame_producer_device&& other) : impl_(std::move(other.impl_)){}
\r
189 void frame_producer_device::swap(frame_producer_device& other){impl_->swap(other);}
\r
190 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
191 void frame_producer_device::pause(int index){impl_->pause(index);}
\r
192 void frame_producer_device::play(int index){impl_->play(index);}
\r
193 void frame_producer_device::stop(int index){impl_->stop(index);}
\r
194 void frame_producer_device::clear(int index){impl_->clear(index);}
\r
195 void frame_producer_device::clear(){impl_->clear();}
\r
196 void frame_producer_device::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}
\r
197 void frame_producer_device::swap_layer(int index, size_t other_index, frame_producer_device& other){impl_->swap_layer(index, other_index, other);}
\r
198 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::foreground(size_t index) const{ return impl_->foreground(index);}
\r