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_channel_context.h"
\r
30 #include "../video_format.h"
\r
31 #include "../mixer/gpu/ogl_device.h"
\r
32 #include "../mixer/read_frame.h"
\r
34 #include <common/concurrency/executor.h>
\r
35 #include <common/diagnostics/graph.h>
\r
36 #include <common/utility/assert.h>
\r
37 #include <common/utility/timer.h>
\r
38 #include <common/memory/memshfl.h>
\r
40 #include <boost/circular_buffer.hpp>
\r
41 #include <boost/timer.hpp>
\r
43 namespace caspar { namespace core {
\r
45 struct frame_consumer_device::implementation
\r
47 typedef std::pair<safe_ptr<const read_frame>, safe_ptr<const read_frame>> fill_and_key;
\r
49 video_channel_context& channel_;
\r
51 boost::circular_buffer<fill_and_key> buffer_;
\r
53 std::map<int, safe_ptr<frame_consumer>> consumers_;
\r
54 typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;
\r
56 high_prec_timer timer_;
\r
58 safe_ptr<diagnostics::graph> diag_;
\r
60 boost::timer frame_timer_;
\r
61 boost::timer tick_timer_;
\r
64 implementation(video_channel_context& video_channel)
\r
65 : channel_(video_channel)
\r
66 , diag_(diagnostics::create_graph(std::string("frame_consumer_device")))
\r
68 diag_->add_guide("frame-time", 0.5f);
\r
69 diag_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));
\r
70 diag_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));
\r
73 std::pair<size_t, size_t> buffer_depth()
\r
75 auto depth_comp = [](const layer_t& lhs, const layer_t& rhs){ return lhs.second->buffer_depth() < rhs.second->buffer_depth(); };
\r
76 auto min = std::min_element(consumers_.begin(), consumers_.end(), depth_comp)->second->buffer_depth();
\r
77 auto max = std::max_element(consumers_.begin(), consumers_.end(), depth_comp)->second->buffer_depth();
\r
78 CASPAR_ASSERT(max >= min);
\r
79 return std::make_pair(min, max);
\r
82 void add(int index, safe_ptr<frame_consumer>&& consumer)
\r
84 consumer->initialize(channel_.format_desc);
\r
85 channel_.execution.invoke([&]
\r
87 this->remove(index);
\r
88 consumers_.insert(std::make_pair(index, consumer));
\r
90 auto depth = buffer_depth();
\r
91 auto diff = depth.second-depth.first+1;
\r
93 if(diff != buffer_.capacity())
\r
95 buffer_.set_capacity(diff);
\r
96 CASPAR_LOG(info) << print() << L" Depth-diff: " << diff-1;
\r
99 CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";
\r
103 void remove(int index)
\r
105 channel_.execution.invoke([&]
\r
107 auto it = consumers_.find(index);
\r
108 if(it != consumers_.end())
\r
110 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
111 consumers_.erase(it);
\r
116 void operator()(const safe_ptr<read_frame>& frame)
\r
118 if(!has_synchronization_clock())
\r
119 timer_.tick(1.0/channel_.format_desc.fps);
\r
121 frame_timer_.restart();
\r
123 buffer_.push_back(std::make_pair(frame, get_key_frame(frame)));
\r
124 if(!buffer_.full())
\r
127 for_each_consumer([&](safe_ptr<frame_consumer>& consumer)
\r
129 if(consumer->get_video_format_desc() != channel_.format_desc)
\r
130 consumer->initialize(channel_.format_desc);
\r
132 auto pair = buffer_[consumer->buffer_depth()-buffer_depth().first];
\r
133 auto frame = consumer->key_only() ? pair.second : pair.first;
\r
135 if(static_cast<size_t>(frame->image_data().size()) == consumer->get_video_format_desc().size)
\r
136 consumer->send(frame);
\r
139 diag_->update_value("frame-time", frame_timer_.elapsed()*channel_.format_desc.fps*0.5);
\r
141 diag_->update_value("tick-time", tick_timer_.elapsed()*channel_.format_desc.fps*0.5);
\r
142 tick_timer_.restart();
\r
147 bool has_synchronization_clock()
\r
149 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)
\r
151 return p.second->has_synchronization_clock();
\r
155 safe_ptr<const read_frame> get_key_frame(const safe_ptr<const read_frame>& frame)
\r
157 bool has_key_only = std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)
\r
159 return p.second->key_only();
\r
164 // Currently do key_only transform on cpu. Unsure if the extra 400MB/s (1080p50) overhead is worth it to do it on gpu.
\r
165 auto key_data = channel_.ogl.create_host_buffer(frame->image_data().size(), host_buffer::write_only);
\r
166 fast_memsfhl(key_data->data(), frame->image_data().begin(), frame->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
\r
167 std::vector<int16_t> audio_data(frame->audio_data().begin(), frame->audio_data().end());
\r
168 return make_safe<read_frame>(std::move(key_data), std::move(audio_data));
\r
171 return read_frame::empty();
\r
174 void for_each_consumer(const std::function<void(safe_ptr<frame_consumer>& consumer)>& func)
\r
176 auto it = consumers_.begin();
\r
177 while(it != consumers_.end())
\r
186 CASPAR_LOG_CURRENT_EXCEPTION();
\r
187 consumers_.erase(it++);
\r
188 CASPAR_LOG(error) << print() << L" " << it->second->print() << L" Removed.";
\r
193 std::wstring print() const
\r
195 return L"frame_consumer_device";
\r
199 frame_consumer_device::frame_consumer_device(video_channel_context& video_channel)
\r
200 : impl_(new implementation(video_channel)){}
\r
201 void frame_consumer_device::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}
\r
202 void frame_consumer_device::remove(int index){impl_->remove(index);}
\r
203 void frame_consumer_device::operator()(const safe_ptr<read_frame>& frame) { (*impl_)(frame); }
\r