2 * Copyright (c) 2011 Sveriges Television AB <info@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: Cambell Prince, cambell.prince@gmail.com
22 #include "../stdafx.h"
24 #include "layer_producer.h"
26 #include <core/consumer/write_frame_consumer.h>
27 #include <core/consumer/output.h>
28 #include <core/video_channel.h>
29 #include <core/frame/draw_frame.h>
30 #include <core/frame/frame_factory.h>
31 #include <core/producer/frame_producer.h>
32 #include <core/producer/stage.h>
33 #include <core/producer/framerate/framerate_producer.h>
35 #include <common/except.h>
36 #include <common/semaphore.h>
38 #include <boost/format.hpp>
40 #include <tbb/concurrent_queue.h>
42 namespace caspar { namespace reroute {
44 class layer_consumer : public core::write_frame_consumer
46 tbb::concurrent_bounded_queue<core::draw_frame> frame_buffer_;
47 semaphore frames_available_ { 0 };
51 layer_consumer(int frames_delay)
52 : frames_delay_(frames_delay)
54 frame_buffer_.set_capacity(2 + frames_delay);
61 // write_frame_consumer
63 void send(const core::draw_frame& src_frame) override
65 bool pushed = frame_buffer_.try_push(src_frame);
68 frames_available_.release();
71 std::wstring print() const override
73 return L"[layer_consumer]";
76 core::draw_frame receive()
78 core::draw_frame frame;
79 if (!frame_buffer_.try_pop(frame))
81 return core::draw_frame::late();
86 void block_until_first_frame_available()
88 if (!frames_available_.try_acquire(1 + frames_delay_, boost::chrono::seconds(2)))
90 << print() << L" Timed out while waiting for first frame";
94 std::vector<core::draw_frame> extract_actual_frames(core::draw_frame original, core::field_mode field_order)
96 if (field_order == core::field_mode::progressive)
97 return { std::move(original) };
99 struct field_extractor : public core::frame_visitor
101 std::vector<core::draw_frame> fields;
102 core::field_mode field_order_first;
103 core::field_mode visiting_mode = core::field_mode::progressive;
105 field_extractor(core::field_mode field_order_)
106 : field_order_first(field_order_)
110 void push(const core::frame_transform& transform) override
112 if (visiting_mode == core::field_mode::progressive)
113 visiting_mode = transform.image_transform.field_mode;
116 void visit(const core::const_frame& frame) override
118 if (visiting_mode == field_order_first && fields.size() == 0)
119 fields.push_back(core::draw_frame(core::const_frame(frame)));
120 else if (visiting_mode != core::field_mode::progressive && visiting_mode != field_order_first && fields.size() == 1)
121 fields.push_back(core::draw_frame(core::const_frame(frame)));
126 visiting_mode = core::field_mode::progressive;
130 field_extractor extractor(field_order);
131 original.accept(extractor);
133 if (extractor.fields.size() == 2)
134 return std::move(extractor.fields);
136 return { std::move(original) };
139 class layer_producer : public core::frame_producer_base
141 core::monitor::subject monitor_subject_;
144 const spl::shared_ptr<layer_consumer> consumer_;
146 core::draw_frame last_frame_;
148 const std::weak_ptr<core::video_channel> channel_;
149 core::constraints pixel_constraints_;
151 tbb::atomic<bool> double_framerate_;
152 std::queue<core::draw_frame> frame_buffer_;
155 explicit layer_producer(const spl::shared_ptr<core::video_channel>& channel, int layer, int frames_delay)
157 , consumer_(spl::make_shared<layer_consumer>(frames_delay))
159 , last_frame_(core::draw_frame::late())
161 pixel_constraints_.width.set(channel->video_format_desc().width);
162 pixel_constraints_.height.set(channel->video_format_desc().height);
163 channel->stage().add_layer_consumer(this, layer_, consumer_);
164 consumer_->block_until_first_frame_available();
165 double_framerate_ = false;
166 CASPAR_LOG(info) << print() << L" Initialized";
171 auto channel = channel_.lock();
174 channel->stage().remove_layer_consumer(this, layer_);
176 CASPAR_LOG(info) << print() << L" Uninitialized";
181 core::draw_frame receive_impl() override
183 if (!frame_buffer_.empty())
185 last_frame_ = frame_buffer_.front();
190 auto channel = channel_.lock();
195 auto consumer_frame = consumer_->receive();
196 if (consumer_frame == core::draw_frame::late())
199 auto actual_frames = extract_actual_frames(std::move(consumer_frame), channel->video_format_desc().field_mode);
200 double_framerate_ = actual_frames.size() == 2;
202 for (auto& frame : actual_frames)
203 frame_buffer_.push(std::move(frame));
205 return receive_impl();
208 core::draw_frame last_frame() override
213 std::wstring print() const override
215 return L"layer-producer[" + boost::lexical_cast<std::wstring>(layer_) + L"]";
218 std::wstring name() const override
220 return L"layer-producer";
223 boost::property_tree::wptree info() const override
225 boost::property_tree::wptree info;
226 info.add(L"type", L"layer-producer");
230 core::monitor::subject& monitor_output() override
232 return monitor_subject_;
235 core::constraints& pixel_constraints() override
237 return pixel_constraints_;
240 boost::rational<int> current_framerate() const
242 auto channel = channel_.lock();
247 return double_framerate_
248 ? channel->video_format_desc().framerate * 2
249 : channel->video_format_desc().framerate;
253 spl::shared_ptr<core::frame_producer> create_layer_producer(
254 const spl::shared_ptr<core::video_channel>& channel,
257 const core::video_format_desc& destination_mode)
259 auto producer = spl::make_shared<layer_producer>(channel, layer, frames_delay);
261 return core::create_framerate_producer(
263 std::bind(&layer_producer::current_framerate, producer),
264 destination_mode.framerate,
265 destination_mode.field_mode,
266 destination_mode.audio_cadence);