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>
34 #include <common/except.h>
35 #include <common/future.h>
37 #include <boost/format.hpp>
39 #include <tbb/concurrent_queue.h>
41 namespace caspar { namespace reroute {
43 class layer_consumer : public core::write_frame_consumer
45 tbb::concurrent_bounded_queue<core::draw_frame> frame_buffer_;
46 std::promise<void> first_frame_promise_;
47 std::future<void> first_frame_available_;
48 bool first_frame_reported_;
52 : first_frame_available_(first_frame_promise_.get_future())
53 , first_frame_reported_(false)
55 frame_buffer_.set_capacity(2);
62 // write_frame_consumer
64 void send(const core::draw_frame& src_frame) override
66 bool pushed = frame_buffer_.try_push(src_frame);
68 if (pushed && !first_frame_reported_)
70 first_frame_promise_.set_value();
71 first_frame_reported_ = true;
75 std::wstring print() const override
77 return L"[layer_consumer]";
80 core::draw_frame receive()
82 core::draw_frame frame;
83 if (!frame_buffer_.try_pop(frame))
85 return core::draw_frame::late();
90 void block_until_first_frame_available()
92 if (first_frame_available_.wait_for(std::chrono::seconds(2)) == std::future_status::timeout)
94 << print() << L" Timed out while waiting for first frame";
98 class layer_producer : public core::frame_producer_base
100 core::monitor::subject monitor_subject_;
103 const spl::shared_ptr<layer_consumer> consumer_;
105 core::draw_frame last_frame_;
106 uint64_t frame_number_;
108 const spl::shared_ptr<core::video_channel> channel_;
109 core::constraints pixel_constraints_;
112 explicit layer_producer(const spl::shared_ptr<core::video_channel>& channel, int layer)
115 , last_frame_(core::draw_frame::empty())
118 pixel_constraints_.width.set(channel->video_format_desc().width);
119 pixel_constraints_.height.set(channel->video_format_desc().height);
120 channel_->stage().add_layer_consumer(this, layer_, consumer_);
121 consumer_->block_until_first_frame_available();
122 CASPAR_LOG(info) << print() << L" Initialized";
127 channel_->stage().remove_layer_consumer(this, layer_);
128 CASPAR_LOG(info) << print() << L" Uninitialized";
133 core::draw_frame receive_impl() override
135 auto consumer_frame = consumer_->receive();
136 if (consumer_frame == core::draw_frame::late())
140 return last_frame_ = consumer_frame;
143 std::wstring print() const override
145 return L"layer-producer[" + boost::lexical_cast<std::wstring>(layer_) + L"]";
148 std::wstring name() const override
150 return L"layer-producer";
153 boost::property_tree::wptree info() const override
155 boost::property_tree::wptree info;
156 info.add(L"type", L"layer-producer");
160 core::monitor::subject& monitor_output() override
162 return monitor_subject_;
165 core::constraints& pixel_constraints() override
167 return pixel_constraints_;
171 spl::shared_ptr<core::frame_producer> create_layer_producer(const spl::shared_ptr<core::video_channel>& channel, int layer)
173 return spl::make_shared<layer_producer>(channel, layer);