2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../stdafx.h"
24 #include "channel_producer.h"
26 #include <core/monitor/monitor.h>
27 #include <core/consumer/frame_consumer.h>
28 #include <core/consumer/output.h>
29 #include <core/producer/frame_producer.h>
30 #include <core/video_channel.h>
32 #include <core/frame/frame.h>
33 #include <core/frame/pixel_format.h>
34 #include <core/frame/audio_channel_layout.h>
35 #include <core/frame/draw_frame.h>
36 #include <core/frame/frame_factory.h>
37 #include <core/video_format.h>
39 #include <boost/thread/once.hpp>
40 #include <boost/lexical_cast.hpp>
41 #include <boost/property_tree/ptree.hpp>
42 #include <boost/range/algorithm/copy.hpp>
44 #include <common/except.h>
45 #include <common/memory.h>
46 #include <common/memcpy.h>
47 #include <common/semaphore.h>
48 #include <common/future.h>
50 #include <tbb/concurrent_queue.h>
56 namespace caspar { namespace reroute {
58 class channel_consumer : public core::frame_consumer
60 core::monitor::subject monitor_subject_;
61 tbb::concurrent_bounded_queue<core::const_frame> frame_buffer_;
62 core::video_format_desc format_desc_;
63 core::audio_channel_layout channel_layout_ = core::audio_channel_layout::invalid();
66 tbb::atomic<bool> is_running_;
67 tbb::atomic<int64_t> current_age_;
68 semaphore frames_available_ { 0 };
72 channel_consumer(int frames_delay)
73 : consumer_index_(next_consumer_index())
74 , frames_delay_(frames_delay)
78 frame_buffer_.set_capacity(3 + frames_delay);
81 static int next_consumer_index()
83 static tbb::atomic<int> consumer_index_counter;
84 static boost::once_flag consumer_index_counter_initialized;
86 boost::call_once(consumer_index_counter_initialized, [&]()
88 consumer_index_counter = 0;
91 return ++consumer_index_counter;
100 std::future<bool> send(core::const_frame frame) override
102 bool pushed = frame_buffer_.try_push(frame);
105 frames_available_.release();
107 return make_ready_future(is_running_.load());
111 const core::video_format_desc& format_desc,
112 const core::audio_channel_layout& channel_layout,
113 int channel_index) override
115 format_desc_ = format_desc;
116 channel_layout_ = channel_layout;
117 channel_index_ = channel_index;
120 std::wstring name() const override
122 return L"channel-consumer";
125 int64_t presentation_frame_age_millis() const override
130 std::wstring print() const override
132 return L"[channel-consumer|" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
135 boost::property_tree::wptree info() const override
137 boost::property_tree::wptree info;
138 info.add(L"type", L"channel-consumer");
139 info.add(L"channel-index", channel_index_);
143 bool has_synchronization_clock() const override
148 int buffer_depth() const override
153 int index() const override
155 return 78500 + consumer_index_;
158 core::monitor::subject& monitor_output() override
160 return monitor_subject_;
165 const core::video_format_desc& get_video_format_desc()
170 const core::audio_channel_layout& get_audio_channel_layout()
172 return channel_layout_;
175 void block_until_first_frame_available()
177 if (!frames_available_.try_acquire(1 + frames_delay_, boost::chrono::seconds(2)))
179 << print() << L" Timed out while waiting for first frame";
182 core::const_frame receive()
184 core::const_frame frame = core::const_frame::empty();
189 if (frame_buffer_.try_pop(frame))
190 current_age_ = frame.get_age_millis();
201 class channel_producer : public core::frame_producer_base
203 core::monitor::subject monitor_subject_;
205 const spl::shared_ptr<core::frame_factory> frame_factory_;
206 const core::video_format_desc output_format_desc_;
207 const spl::shared_ptr<channel_consumer> consumer_;
208 core::constraints pixel_constraints_;
210 std::queue<core::draw_frame> frame_buffer_;
211 uint64_t frame_number_;
214 explicit channel_producer(const core::frame_producer_dependencies& dependecies, const spl::shared_ptr<core::video_channel>& channel, int frames_delay)
215 : frame_factory_(dependecies.frame_factory)
216 , output_format_desc_(dependecies.format_desc)
217 , consumer_(spl::make_shared<channel_consumer>(frames_delay))
220 pixel_constraints_.width.set(output_format_desc_.width);
221 pixel_constraints_.height.set(output_format_desc_.height);
222 channel->output().add(consumer_);
223 consumer_->block_until_first_frame_available();
224 CASPAR_LOG(info) << print() << L" Initialized";
230 CASPAR_LOG(info) << print() << L" Uninitialized";
235 core::draw_frame receive_impl() override
237 auto format_desc = consumer_->get_video_format_desc();
239 if(frame_buffer_.size() > 0)
241 auto frame = frame_buffer_.front();
246 auto read_frame = consumer_->receive();
247 if(read_frame == core::const_frame::empty() || read_frame.image_data().empty())
248 return core::draw_frame::late();
252 bool double_speed = std::abs(output_format_desc_.fps / 2.0 - format_desc.fps) < 0.01;
253 bool half_speed = std::abs(format_desc.fps / 2.0 - output_format_desc_.fps) < 0.01;
255 if(half_speed && frame_number_ % 2 == 0) // Skip frame
256 return receive_impl();
258 core::pixel_format_desc desc;
259 desc.format = core::pixel_format::bgra;
260 desc.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));
261 auto frame = frame_factory_->create_frame(this, desc, consumer_->get_audio_channel_layout());
263 bool copy_audio = !double_speed && !half_speed;
267 frame.audio_data().reserve(read_frame.audio_data().size());
268 boost::copy(read_frame.audio_data(), std::back_inserter(frame.audio_data()));
271 fast_memcpy(frame.image_data().begin(), read_frame.image_data().begin(), read_frame.image_data().size());
273 frame_buffer_.push(core::draw_frame(std::move(frame)));
276 frame_buffer_.push(frame_buffer_.back());
278 return receive_impl();
281 std::wstring name() const override
283 return L"channel-producer";
286 std::wstring print() const override
288 return L"channel-producer[]";
291 core::constraints& pixel_constraints() override
293 return pixel_constraints_;
296 boost::property_tree::wptree info() const override
298 boost::property_tree::wptree info;
299 info.add(L"type", L"channel-producer");
303 core::monitor::subject& monitor_output() override
305 return monitor_subject_;
309 spl::shared_ptr<core::frame_producer> create_channel_producer(
310 const core::frame_producer_dependencies& dependencies,
311 const spl::shared_ptr<core::video_channel>& channel,
314 return spl::make_shared<channel_producer>(dependencies, channel, frames_delay);