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
20 #include "../StdAfx.h"
\r
23 #pragma warning (disable : 4244)
\r
26 #include "frame_consumer_device.h"
\r
28 #include "../video_format.h"
\r
29 #include "../mixer/gpu/ogl_device.h"
\r
30 #include "../mixer/read_frame.h"
\r
32 #include <common/concurrency/executor.h>
\r
33 #include <common/diagnostics/graph.h>
\r
34 #include <common/utility/assert.h>
\r
35 #include <common/memory/memshfl.h>
\r
37 #include <boost/range/algorithm_ext/erase.hpp>
\r
38 #include <boost/range/algorithm.hpp>
\r
39 #include <boost/circular_buffer.hpp>
\r
40 #include <boost/timer.hpp>
\r
41 #include <boost/range/algorithm.hpp>
\r
43 namespace caspar { namespace core {
\r
45 struct frame_consumer_device::implementation
\r
47 boost::circular_buffer<std::pair<safe_ptr<const read_frame>,safe_ptr<const read_frame>>> buffer_;
\r
49 std::map<int, std::shared_ptr<frame_consumer>> consumers_; // Valid iterators after erase
\r
51 safe_ptr<diagnostics::graph> diag_;
\r
53 video_format_desc format_desc_;
\r
55 boost::timer frame_timer_;
\r
56 boost::timer tick_timer_;
\r
58 executor executor_;
\r
60 implementation( const video_format_desc& format_desc)
\r
61 : format_desc_(format_desc)
\r
62 , diag_(diagnostics::create_graph(std::string("frame_consumer_device")))
\r
63 , executor_(L"frame_consumer_device", true)
\r
65 diag_->set_color("input-buffer", diagnostics::color(1.0f, 1.0f, 0.0f));
\r
66 diag_->add_guide("frame-time", 0.5f);
\r
67 diag_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
68 diag_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));
\r
70 executor_.set_capacity(1);
\r
71 executor_.begin_invoke([]
\r
73 SetThreadPriority(GetCurrentThread(), ABOVE_NORMAL_PRIORITY_CLASS);
\r
77 void add(int index, safe_ptr<frame_consumer>&& consumer)
\r
79 consumer->initialize(format_desc_);
\r
80 executor_.invoke([&]
\r
82 if(buffer_.capacity() < consumer->buffer_depth())
\r
83 buffer_.set_capacity(consumer->buffer_depth());
\r
84 consumers_[index] = std::move(consumer);
\r
88 void remove(int index)
\r
90 executor_.invoke([&]
\r
92 auto it = consumers_.find(index);
\r
93 if(it != consumers_.end())
\r
95 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
96 consumers_.erase(it);
\r
101 void send(const safe_ptr<const read_frame>& frame)
\r
103 executor_.begin_invoke([=]
\r
105 diag_->set_value("input-buffer", static_cast<float>(executor_.size())/static_cast<float>(executor_.capacity()));
\r
106 frame_timer_.restart();
\r
108 auto key_frame = read_frame::empty();
\r
110 if(boost::range::find_if(consumers_, [](const decltype(*consumers_.begin())& p){return p.second->key_only();}) != consumers_.end())
\r
112 // Currently do key_only transform on cpu. Unsure if the extra 400MB/s (1080p50) overhead is worth it to do it on gpu.
\r
113 auto key_data = ogl_device::create_host_buffer(frame->image_data().size(), host_buffer::write_only);
\r
114 fast_memsfhl(key_data->data(), frame->image_data().begin(), frame->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
\r
115 std::vector<short> audio_data(frame->audio_data().begin(), frame->audio_data().end());
\r
116 key_frame = make_safe<const read_frame>(std::move(key_data), std::move(audio_data));
\r
119 buffer_.push_back(std::make_pair(std::move(frame), std::move(key_frame)));
\r
121 if(!buffer_.full())
\r
125 auto it = consumers_.begin();
\r
126 while(it != consumers_.end())
\r
130 auto p = buffer_[it->second->buffer_depth()-1];
\r
131 it->second->send(it->second->key_only() ? p.second : p.first);
\r
136 CASPAR_LOG_CURRENT_EXCEPTION();
\r
137 consumers_.erase(it++);
\r
138 CASPAR_LOG(error) << print() << L" " << it->second->print() << L" Removed.";
\r
141 diag_->update_value("frame-time", static_cast<float>(frame_timer_.elapsed()*format_desc_.fps*0.5));
\r
143 diag_->update_value("tick-time", static_cast<float>(tick_timer_.elapsed()*format_desc_.fps*0.5));
\r
144 tick_timer_.restart();
\r
146 diag_->set_value("input-buffer", static_cast<float>(executor_.size())/static_cast<float>(executor_.capacity()));
\r
149 std::wstring print() const
\r
151 return L"frame_consumer_device";
\r
154 void set_video_format_desc(const video_format_desc& format_desc)
\r
156 executor_.invoke([&]
\r
158 format_desc_ = format_desc;
\r
161 auto it = consumers_.begin();
\r
162 while(it != consumers_.end())
\r
166 it->second->initialize(format_desc_);
\r
171 CASPAR_LOG_CURRENT_EXCEPTION();
\r
172 consumers_.erase(it++);
\r
173 CASPAR_LOG(error) << print() << L" " << it->second->print() << L" Removed.";
\r
180 frame_consumer_device::frame_consumer_device(const video_format_desc& format_desc) : impl_(new implementation(format_desc)){}
\r
181 void frame_consumer_device::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}
\r
182 void frame_consumer_device::remove(int index){impl_->remove(index);}
\r
183 void frame_consumer_device::send(const safe_ptr<const read_frame>& future_frame) { impl_->send(future_frame); }
\r
184 void frame_consumer_device::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}
\r