2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../../stdafx.h"
\r
24 #include "channel_producer.h"
\r
26 #include "../../monitor/monitor.h"
\r
27 #include "../../consumer/frame_consumer.h"
\r
28 #include "../../consumer/output.h"
\r
29 #include "../../video_channel.h"
\r
31 #include "../frame/basic_frame.h"
\r
32 #include "../frame/frame_factory.h"
\r
33 #include "../../mixer/write_frame.h"
\r
34 #include "../../mixer/read_frame.h"
\r
36 #include <common/exception/exceptions.h>
\r
37 #include <common/memory/memcpy.h>
\r
38 #include <common/concurrency/future_util.h>
\r
40 #include <tbb/concurrent_queue.h>
\r
42 namespace caspar { namespace core {
\r
44 class channel_consumer : public frame_consumer
\r
46 tbb::concurrent_bounded_queue<std::shared_ptr<read_frame>> frame_buffer_;
\r
47 core::video_format_desc format_desc_;
\r
49 tbb::atomic<bool> is_running_;
\r
55 frame_buffer_.set_capacity(3);
\r
65 virtual boost::unique_future<bool> send(const safe_ptr<read_frame>& frame) override
\r
67 frame_buffer_.try_push(frame);
\r
68 return caspar::wrap_as_future(is_running_.load());
\r
71 virtual void initialize(const core::video_format_desc& format_desc, int channel_index) override
\r
73 format_desc_ = format_desc;
\r
74 channel_index_ = channel_index;
\r
77 virtual std::wstring print() const override
\r
79 return L"[channel-consumer|" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
\r
82 virtual boost::property_tree::wptree info() const override
\r
84 boost::property_tree::wptree info;
\r
85 info.add(L"type", L"channel-consumer");
\r
86 info.add(L"channel-index", channel_index_);
\r
90 virtual bool has_synchronization_clock() const override
\r
95 virtual size_t buffer_depth() const override
\r
100 virtual int index() const override
\r
102 return 78500 + channel_index_;
\r
105 // channel_consumer
\r
109 is_running_ = false;
\r
110 frame_buffer_.try_push(make_safe<read_frame>());
\r
113 const core::video_format_desc& get_video_format_desc()
\r
115 return format_desc_;
\r
118 std::shared_ptr<read_frame> receive()
\r
121 return make_safe<read_frame>();
\r
122 std::shared_ptr<read_frame> frame;
\r
123 frame_buffer_.try_pop(frame);
\r
128 class channel_producer : public frame_producer
\r
130 monitor::subject monitor_subject_;
\r
132 const safe_ptr<frame_factory> frame_factory_;
\r
133 const safe_ptr<channel_consumer> consumer_;
\r
135 std::queue<safe_ptr<basic_frame>> frame_buffer_;
\r
136 safe_ptr<basic_frame> last_frame_;
\r
137 uint64_t frame_number_;
\r
140 explicit channel_producer(const safe_ptr<frame_factory>& frame_factory, const safe_ptr<video_channel>& channel)
\r
141 : frame_factory_(frame_factory)
\r
142 , consumer_(make_safe<channel_consumer>())
\r
143 , last_frame_(basic_frame::empty())
\r
146 channel->output()->add(consumer_);
\r
147 CASPAR_LOG(info) << print() << L" Initialized";
\r
150 ~channel_producer()
\r
153 CASPAR_LOG(info) << print() << L" Uninitialized";
\r
158 virtual safe_ptr<basic_frame> receive(int) override
\r
160 auto format_desc = consumer_->get_video_format_desc();
\r
162 if(frame_buffer_.size() > 1)
\r
164 auto frame = frame_buffer_.front();
\r
165 frame_buffer_.pop();
\r
166 return last_frame_ = frame;
\r
169 auto read_frame = consumer_->receive();
\r
170 if(!read_frame || read_frame->image_data().empty())
\r
171 return basic_frame::late();
\r
175 core::pixel_format_desc desc;
\r
176 bool double_speed = std::abs(frame_factory_->get_video_format_desc().fps / 2.0 - format_desc.fps) < 0.01;
\r
177 bool half_speed = std::abs(format_desc.fps / 2.0 - frame_factory_->get_video_format_desc().fps) < 0.01;
\r
179 if(half_speed && frame_number_ % 2 == 0) // Skip frame
\r
182 desc.pix_fmt = core::pixel_format::bgra;
\r
183 desc.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));
\r
184 auto frame = frame_factory_->create_frame(this, desc);
\r
186 fast_memcpy(frame->image_data().begin(), read_frame->image_data().begin(), read_frame->image_data().size());
\r
189 frame_buffer_.push(frame);
\r
192 frame_buffer_.push(frame);
\r
197 virtual safe_ptr<basic_frame> last_frame() const override
\r
199 return last_frame_;
\r
202 virtual std::wstring print() const override
\r
204 return L"channel[]";
\r
207 virtual boost::property_tree::wptree info() const override
\r
209 boost::property_tree::wptree info;
\r
210 info.add(L"type", L"channel-producer");
\r
214 monitor::source& monitor_output()
\r
216 return monitor_subject_;
\r
220 safe_ptr<frame_producer> create_channel_producer(const safe_ptr<core::frame_factory>& frame_factory, const safe_ptr<video_channel>& channel)
\r
222 return create_producer_print_proxy(
\r
223 make_safe<channel_producer>(frame_factory, channel));
\r