2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\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
21 #include "../StdAfx.h"
\r
23 #include "frame_producer_device.h"
\r
26 #include "destroy_producer_proxy.h"
\r
28 #include <core/producer/frame/basic_frame.h>
\r
29 #include <core/producer/frame/frame_factory.h>
\r
31 #include <common/diagnostics/graph.h>
\r
32 #include <common/concurrency/executor.h>
\r
34 #include <boost/timer.hpp>
\r
36 #include <tbb/parallel_for.h>
\r
40 namespace caspar { namespace core {
\r
42 struct frame_producer_device::implementation : boost::noncopyable
\r
44 std::map<int, layer> layers_;
\r
45 const video_format_desc format_desc_;
\r
46 const output_t output_;
\r
48 safe_ptr<diagnostics::graph> diag_;
\r
49 boost::timer frame_timer_;
\r
50 boost::timer tick_timer_;
\r
51 boost::timer output_timer_;
\r
55 implementation(const video_format_desc& format_desc, const output_t& output)
\r
56 : format_desc_(format_desc)
\r
57 , diag_(diagnostics::create_graph(std::string("frame_producer_device")))
\r
58 , executor_(L"frame_producer_device")
\r
61 diag_->add_guide("frame-time", 0.5f);
\r
62 diag_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
63 diag_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));
\r
64 diag_->set_color("output-time", diagnostics::color(0.5f, 1.0f, 0.2f));
\r
66 executor_.set_priority_class(above_normal_priority_class);
\r
67 executor_.begin_invoke([=]{tick();});
\r
74 auto frame = render();
\r
75 output_timer_.restart();
\r
77 diag_->update_value("output-time", static_cast<float>(output_timer_.elapsed()*format_desc_.fps*0.5));
\r
81 CASPAR_LOG_CURRENT_EXCEPTION();
\r
84 executor_.begin_invoke([=]{tick();});
\r
87 std::map<int, safe_ptr<basic_frame>> render()
\r
89 frame_timer_.restart();
\r
91 std::map<int, safe_ptr<basic_frame>> frames;
\r
92 BOOST_FOREACH(auto& layer, layers_)
\r
93 frames[layer.first] = basic_frame::empty();
\r
95 tbb::parallel_for_each(layers_.begin(), layers_.end(), [&](decltype(*layers_.begin())& pair)
\r
97 frames[pair.first] = pair.second.receive();
\r
100 diag_->update_value("frame-time", frame_timer_.elapsed()*format_desc_.fps*0.5);
\r
102 diag_->update_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);
\r
103 tick_timer_.restart();
\r
108 void load(int index, const safe_ptr<frame_producer>& producer, bool preview)
\r
110 executor_.invoke([&]{layers_[index].load(make_safe<destroy_producer_proxy>(producer), preview);});
\r
113 void pause(int index)
\r
115 executor_.invoke([&]{layers_[index].pause();});
\r
118 void play(int index)
\r
120 executor_.invoke([&]{layers_[index].play();});
\r
123 void stop(int index)
\r
125 executor_.invoke([&]{layers_[index].stop();});
\r
128 void clear(int index)
\r
130 executor_.invoke([&]{layers_.erase(index);});
\r
135 executor_.invoke([&]{layers_.clear();});
\r
138 void swap_layer(int index, size_t other_index)
\r
140 executor_.invoke([&]{layers_[index].swap(layers_[other_index]);});
\r
143 void swap_layer(int index, size_t other_index, frame_producer_device& other)
\r
145 if(other.impl_.get() == this)
\r
146 swap_layer(index, other_index);
\r
149 if(format_desc_ != other.impl_->format_desc_)
\r
150 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot swap between channels with different formats."));
\r
152 auto func = [&]{layers_[index].swap(other.impl_->layers_[other_index]);};
\r
154 executor_.invoke([&]{other.impl_->executor_.invoke(func);});
\r
158 void swap(frame_producer_device& other)
\r
160 if(other.impl_.get() == this)
\r
163 if(format_desc_ != other.impl_->format_desc_)
\r
164 BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Cannot swap between channels with different formats."));
\r
168 auto sel_first = [](const std::pair<int, layer>& pair){return pair.first;};
\r
170 std::set<int> indices;
\r
171 auto inserter = std::inserter(indices, indices.begin());
\r
173 std::transform(layers_.begin(), layers_.end(), inserter, sel_first);
\r
174 std::transform(other.impl_->layers_.begin(), other.impl_->layers_.end(), inserter, sel_first);
\r
175 std::for_each(indices.begin(), indices.end(), [&](int index)
\r
177 layers_[index].swap(other.impl_->layers_[index]);
\r
181 executor_.invoke([&]{other.impl_->executor_.invoke(func);});
\r
184 boost::unique_future<safe_ptr<frame_producer>> foreground(int index)
\r
186 return executor_.begin_invoke([=]{return layers_[index].foreground();});
\r
189 boost::unique_future<safe_ptr<frame_producer>> background(int index)
\r
191 return executor_.begin_invoke([=]{return layers_[index].background();});
\r
195 frame_producer_device::frame_producer_device(const video_format_desc& format_desc, const output_t& output) : impl_(new implementation(format_desc, output)){}
\r
196 frame_producer_device::frame_producer_device(frame_producer_device&& other) : impl_(std::move(other.impl_)){}
\r
197 void frame_producer_device::swap(frame_producer_device& other){impl_->swap(other);}
\r
198 void frame_producer_device::load(int index, const safe_ptr<frame_producer>& producer, bool preview){impl_->load(index, producer, preview);}
\r
199 void frame_producer_device::pause(int index){impl_->pause(index);}
\r
200 void frame_producer_device::play(int index){impl_->play(index);}
\r
201 void frame_producer_device::stop(int index){impl_->stop(index);}
\r
202 void frame_producer_device::clear(int index){impl_->clear(index);}
\r
203 void frame_producer_device::clear(){impl_->clear();}
\r
204 void frame_producer_device::swap_layer(int index, size_t other_index){impl_->swap_layer(index, other_index);}
\r
205 void frame_producer_device::swap_layer(int index, size_t other_index, frame_producer_device& other){impl_->swap_layer(index, other_index, other);}
\r
206 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::foreground(size_t index) {return impl_->foreground(index);}
\r
207 boost::unique_future<safe_ptr<frame_producer>> frame_producer_device::background(size_t index) {return impl_->background(index);}
\r