]> git.sesse.net Git - casparcg/blob - modules/reroute/producer/layer_producer.cpp
81bf198f12edfb1738c3981b06b7b0b0fb99acf5
[casparcg] / modules / reroute / producer / layer_producer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Cambell Prince, cambell.prince@gmail.com
20 */
21
22 #include "../stdafx.h"
23
24 #include "layer_producer.h"
25
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
34 #include <common/except.h>
35 #include <common/future.h>
36
37 #include <boost/format.hpp>
38
39 #include <tbb/concurrent_queue.h>
40
41 namespace caspar { namespace reroute {
42
43 class layer_consumer : public core::write_frame_consumer
44 {       
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_;
49
50 public:
51         layer_consumer()
52                 : first_frame_available_(first_frame_promise_.get_future())
53                 , first_frame_reported_(false)
54         {
55                 frame_buffer_.set_capacity(2);
56         }
57
58         ~layer_consumer()
59         {
60         }
61
62         // write_frame_consumer
63
64         void send(const core::draw_frame& src_frame) override
65         {
66                 bool pushed = frame_buffer_.try_push(src_frame);
67
68                 if (pushed && !first_frame_reported_)
69                 {
70                         first_frame_promise_.set_value();
71                         first_frame_reported_ = true;
72                 }
73         }
74
75         std::wstring print() const override
76         {
77                 return L"[layer_consumer]";
78         }
79
80         core::draw_frame receive()
81         {
82                 core::draw_frame frame;
83                 if (!frame_buffer_.try_pop(frame))
84                 {
85                         return core::draw_frame::late();
86                 }
87                 return frame;
88         }
89
90         void block_until_first_frame_available()
91         {
92                 if (first_frame_available_.wait_for(std::chrono::seconds(2)) == std::future_status::timeout)
93                         CASPAR_LOG(warning)
94                                         << print() << L" Timed out while waiting for first frame";
95         }
96 };
97
98 class layer_producer : public core::frame_producer_base
99 {
100         core::monitor::subject                                          monitor_subject_;
101
102         const int                                                                       layer_;
103         const spl::shared_ptr<layer_consumer>           consumer_;
104
105         core::draw_frame                                                        last_frame_;
106         uint64_t                                                                        frame_number_;
107
108         const spl::shared_ptr<core::video_channel>      channel_;
109         core::constraints                                                       pixel_constraints_;
110
111 public:
112         explicit layer_producer(const spl::shared_ptr<core::video_channel>& channel, int layer) 
113                 : layer_(layer)
114                 , channel_(channel)
115                 , last_frame_(core::draw_frame::empty())
116                 , frame_number_(0)
117         {
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";
123         }
124
125         ~layer_producer()
126         {
127                 channel_->stage().remove_layer_consumer(this, layer_);
128                 CASPAR_LOG(info) << print() << L" Uninitialized";
129         }
130
131         // frame_producer
132                         
133         core::draw_frame receive_impl() override
134         {
135                 auto consumer_frame = consumer_->receive();
136                 if (consumer_frame == core::draw_frame::late())
137                         return last_frame_;
138
139                 frame_number_++;
140                 return last_frame_ = consumer_frame;
141         }       
142
143         std::wstring print() const override
144         {
145                 return L"layer-producer[" + boost::lexical_cast<std::wstring>(layer_) + L"]";
146         }
147
148         std::wstring name() const override
149         {
150                 return L"layer-producer";
151         }
152
153         boost::property_tree::wptree info() const override
154         {
155                 boost::property_tree::wptree info;
156                 info.add(L"type", L"layer-producer");
157                 return info;
158         }
159
160         core::monitor::subject& monitor_output() override
161         {
162                 return monitor_subject_;
163         }
164
165         core::constraints& pixel_constraints() override
166         {
167                 return pixel_constraints_;
168         }
169 };
170
171 spl::shared_ptr<core::frame_producer> create_layer_producer(const spl::shared_ptr<core::video_channel>& channel, int layer)
172 {
173         return spl::make_shared<layer_producer>(channel, layer);
174 }
175
176 }}