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/semaphore.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 semaphore frames_available_ { 0 };
50 layer_consumer(int frames_delay)
51 : frames_delay_(frames_delay)
53 frame_buffer_.set_capacity(2 + frames_delay);
60 // write_frame_consumer
62 void send(const core::draw_frame& src_frame) override
64 bool pushed = frame_buffer_.try_push(src_frame);
67 frames_available_.release();
70 std::wstring print() const override
72 return L"[layer_consumer]";
75 core::draw_frame receive()
77 core::draw_frame frame;
78 if (!frame_buffer_.try_pop(frame))
80 return core::draw_frame::late();
85 void block_until_first_frame_available()
87 if (!frames_available_.try_acquire(1 + frames_delay_, boost::chrono::seconds(2)))
89 << print() << L" Timed out while waiting for first frame";
93 class layer_producer : public core::frame_producer_base
95 core::monitor::subject monitor_subject_;
98 const spl::shared_ptr<layer_consumer> consumer_;
100 core::draw_frame last_frame_;
101 uint64_t frame_number_;
103 const spl::shared_ptr<core::video_channel> channel_;
104 core::constraints pixel_constraints_;
107 explicit layer_producer(const spl::shared_ptr<core::video_channel>& channel, int layer, int frames_delay)
109 , consumer_(spl::make_shared<layer_consumer>(frames_delay))
111 , last_frame_(core::draw_frame::late())
114 pixel_constraints_.width.set(channel->video_format_desc().width);
115 pixel_constraints_.height.set(channel->video_format_desc().height);
116 channel_->stage().add_layer_consumer(this, layer_, consumer_);
117 consumer_->block_until_first_frame_available();
118 CASPAR_LOG(info) << print() << L" Initialized";
123 channel_->stage().remove_layer_consumer(this, layer_);
124 CASPAR_LOG(info) << print() << L" Uninitialized";
129 core::draw_frame receive_impl() override
131 auto consumer_frame = consumer_->receive();
132 if (consumer_frame == core::draw_frame::late())
136 return last_frame_ = consumer_frame;
139 core::draw_frame last_frame() override
144 std::wstring print() const override
146 return L"layer-producer[" + boost::lexical_cast<std::wstring>(layer_) + L"]";
149 std::wstring name() const override
151 return L"layer-producer";
154 boost::property_tree::wptree info() const override
156 boost::property_tree::wptree info;
157 info.add(L"type", L"layer-producer");
161 core::monitor::subject& monitor_output() override
163 return monitor_subject_;
166 core::constraints& pixel_constraints() override
168 return pixel_constraints_;
172 spl::shared_ptr<core::frame_producer> create_layer_producer(const spl::shared_ptr<core::video_channel>& channel, int layer, int frames_delay)
174 return spl::make_shared<layer_producer>(channel, layer, frames_delay);