]> git.sesse.net Git - casparcg/blob - modules/reroute/producer/layer_producer.cpp
85af0ebd7964fb6d3d08b4c75d143d4e7acb12dc
[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 #include <core/producer/framerate/framerate_producer.h>
34
35 #include <common/except.h>
36 #include <common/semaphore.h>
37
38 #include <boost/format.hpp>
39
40 #include <tbb/concurrent_queue.h>
41
42 namespace caspar { namespace reroute {
43
44 class layer_consumer : public core::write_frame_consumer
45 {
46         tbb::concurrent_bounded_queue<core::draw_frame> frame_buffer_;
47         semaphore                                                                               frames_available_ { 0 };
48         int                                                                                             frames_delay_;
49
50 public:
51         layer_consumer(int frames_delay)
52                 : frames_delay_(frames_delay)
53         {
54                 frame_buffer_.set_capacity(2 + frames_delay);
55         }
56
57         ~layer_consumer()
58         {
59         }
60
61         // write_frame_consumer
62
63         void send(const core::draw_frame& src_frame) override
64         {
65                 bool pushed = frame_buffer_.try_push(src_frame);
66
67                 if (pushed)
68                         frames_available_.release();
69         }
70
71         std::wstring print() const override
72         {
73                 return L"[layer_consumer]";
74         }
75
76         core::draw_frame receive()
77         {
78                 core::draw_frame frame;
79                 if (!frame_buffer_.try_pop(frame))
80                 {
81                         return core::draw_frame::late();
82                 }
83                 return frame;
84         }
85
86         void block_until_first_frame_available()
87         {
88                 if (!frames_available_.try_acquire(1 + frames_delay_, boost::chrono::seconds(2)))
89                         CASPAR_LOG(warning)
90                                         << print() << L" Timed out while waiting for first frame";
91         }
92 };
93
94 std::vector<core::draw_frame> extract_actual_frames(core::draw_frame original, core::field_mode field_order)
95 {
96         if (field_order == core::field_mode::progressive)
97                 return { std::move(original) };
98
99         struct field_extractor : public core::frame_visitor
100         {
101                 std::vector<core::draw_frame>   fields;
102                 core::field_mode                                field_order_first;
103                 core::field_mode                                visiting_mode = core::field_mode::progressive;
104
105                 field_extractor(core::field_mode field_order_)
106                         : field_order_first(field_order_)
107                 {
108                 }
109
110                 void push(const core::frame_transform& transform) override
111                 {
112                         if (visiting_mode == core::field_mode::progressive)
113                                 visiting_mode = transform.image_transform.field_mode;
114                 }
115
116                 void visit(const core::const_frame& frame) override
117                 {
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)));
122                 }
123
124                 void pop() override
125                 {
126                         visiting_mode = core::field_mode::progressive;
127                 };
128         };
129
130         field_extractor extractor(field_order);
131         original.accept(extractor);
132
133         if (extractor.fields.size() == 2)
134                 return std::move(extractor.fields);
135         else
136                 return { std::move(original) };
137 }
138
139 class layer_producer : public core::frame_producer_base
140 {
141         core::monitor::subject                                          monitor_subject_;
142
143         const int                                                                       layer_;
144         const spl::shared_ptr<layer_consumer>           consumer_;
145
146         core::draw_frame                                                        last_frame_;
147
148         const std::weak_ptr<core::video_channel>        channel_;
149         core::constraints                                                       pixel_constraints_;
150
151         tbb::atomic<bool>                                                       double_framerate_;
152         std::queue<core::draw_frame>                            frame_buffer_;
153
154 public:
155         explicit layer_producer(const spl::shared_ptr<core::video_channel>& channel, int layer, int frames_delay)
156                 : layer_(layer)
157                 , consumer_(spl::make_shared<layer_consumer>(frames_delay))
158                 , channel_(channel)
159                 , last_frame_(core::draw_frame::late())
160         {
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";
167         }
168
169         ~layer_producer()
170         {
171                 auto channel = channel_.lock();
172
173                 if (channel)
174                         channel->stage().remove_layer_consumer(this, layer_);
175
176                 CASPAR_LOG(info) << print() << L" Uninitialized";
177         }
178
179         // frame_producer
180
181         core::draw_frame receive_impl() override
182         {
183                 if (!frame_buffer_.empty())
184                 {
185                         last_frame_ = frame_buffer_.front();
186                         frame_buffer_.pop();
187                         return last_frame_;
188                 }
189
190                 auto channel = channel_.lock();
191
192                 if (!channel)
193                         return last_frame_;
194
195                 auto consumer_frame = consumer_->receive();
196                 if (consumer_frame == core::draw_frame::late())
197                         return last_frame_;
198
199                 auto actual_frames = extract_actual_frames(std::move(consumer_frame), channel->video_format_desc().field_mode);
200                 double_framerate_ = actual_frames.size() == 2;
201
202                 for (auto& frame : actual_frames)
203                         frame_buffer_.push(std::move(frame));
204
205                 return receive_impl();
206         }
207
208         core::draw_frame last_frame() override
209         {
210                 return last_frame_;
211         }
212
213         std::wstring print() const override
214         {
215                 return L"layer-producer[" + boost::lexical_cast<std::wstring>(layer_) + L"]";
216         }
217
218         std::wstring name() const override
219         {
220                 return L"layer-producer";
221         }
222
223         boost::property_tree::wptree info() const override
224         {
225                 boost::property_tree::wptree info;
226                 info.add(L"type", L"layer-producer");
227                 return info;
228         }
229
230         core::monitor::subject& monitor_output() override
231         {
232                 return monitor_subject_;
233         }
234
235         core::constraints& pixel_constraints() override
236         {
237                 return pixel_constraints_;
238         }
239
240         boost::rational<int> current_framerate() const
241         {
242                 auto channel = channel_.lock();
243
244                 if (!channel)
245                         return 1;
246
247                 return double_framerate_
248                                 ? channel->video_format_desc().framerate * 2
249                                 : channel->video_format_desc().framerate;
250         }
251 };
252
253 spl::shared_ptr<core::frame_producer> create_layer_producer(
254                 const spl::shared_ptr<core::video_channel>& channel,
255                 int layer,
256                 int frames_delay,
257                 const core::video_format_desc& destination_mode)
258 {
259         auto producer = spl::make_shared<layer_producer>(channel, layer, frames_delay);
260
261         return core::create_framerate_producer(
262                         producer,
263                         std::bind(&layer_producer::current_framerate, producer),
264                         destination_mode.framerate,
265                         destination_mode.field_mode,
266                         destination_mode.audio_cadence);
267 }
268
269 }}