]> git.sesse.net Git - casparcg/blob - modules/reroute/producer/layer_producer.cpp
[layer_producer] Return the last known framerate when channel has been destroyed.
[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         mutable boost::rational<int>                            last_frame_rate_;
148
149         const std::weak_ptr<core::video_channel>        channel_;
150         core::constraints                                                       pixel_constraints_;
151
152         tbb::atomic<bool>                                                       double_framerate_;
153         std::queue<core::draw_frame>                            frame_buffer_;
154
155 public:
156         explicit layer_producer(const spl::shared_ptr<core::video_channel>& channel, int layer, int frames_delay)
157                 : layer_(layer)
158                 , consumer_(spl::make_shared<layer_consumer>(frames_delay))
159                 , channel_(channel)
160                 , last_frame_(core::draw_frame::late())
161         {
162                 pixel_constraints_.width.set(channel->video_format_desc().width);
163                 pixel_constraints_.height.set(channel->video_format_desc().height);
164                 channel->stage().add_layer_consumer(this, layer_, consumer_);
165                 consumer_->block_until_first_frame_available();
166                 double_framerate_ = false;
167                 CASPAR_LOG(info) << print() << L" Initialized";
168         }
169
170         ~layer_producer()
171         {
172                 auto channel = channel_.lock();
173
174                 if (channel)
175                         channel->stage().remove_layer_consumer(this, layer_);
176
177                 CASPAR_LOG(info) << print() << L" Uninitialized";
178         }
179
180         // frame_producer
181
182         core::draw_frame receive_impl() override
183         {
184                 if (!frame_buffer_.empty())
185                 {
186                         last_frame_ = frame_buffer_.front();
187                         frame_buffer_.pop();
188                         return last_frame_;
189                 }
190
191                 auto channel = channel_.lock();
192
193                 if (!channel)
194                         return last_frame_;
195
196                 auto consumer_frame = consumer_->receive();
197                 if (consumer_frame == core::draw_frame::late())
198                         return last_frame_;
199
200                 auto actual_frames = extract_actual_frames(std::move(consumer_frame), channel->video_format_desc().field_mode);
201                 double_framerate_ = actual_frames.size() == 2;
202
203                 for (auto& frame : actual_frames)
204                         frame_buffer_.push(std::move(frame));
205
206                 return receive_impl();
207         }
208
209         core::draw_frame last_frame() override
210         {
211                 return last_frame_;
212         }
213
214         std::wstring print() const override
215         {
216                 return L"layer-producer[" + boost::lexical_cast<std::wstring>(layer_) + L"]";
217         }
218
219         std::wstring name() const override
220         {
221                 return L"layer-producer";
222         }
223
224         boost::property_tree::wptree info() const override
225         {
226                 boost::property_tree::wptree info;
227                 info.add(L"type", L"layer-producer");
228                 return info;
229         }
230
231         core::monitor::subject& monitor_output() override
232         {
233                 return monitor_subject_;
234         }
235
236         core::constraints& pixel_constraints() override
237         {
238                 return pixel_constraints_;
239         }
240
241         boost::rational<int> current_framerate() const
242         {
243                 auto channel = channel_.lock();
244
245                 if (!channel)
246                         return last_frame_rate_;
247
248                 last_frame_rate_ = double_framerate_
249                                 ? channel->video_format_desc().framerate * 2
250                                 : channel->video_format_desc().framerate;
251
252                 return last_frame_rate_;
253         }
254 };
255
256 spl::shared_ptr<core::frame_producer> create_layer_producer(
257                 const spl::shared_ptr<core::video_channel>& channel,
258                 int layer,
259                 int frames_delay,
260                 const core::video_format_desc& destination_mode)
261 {
262         auto producer = spl::make_shared<layer_producer>(channel, layer, frames_delay);
263
264         return core::create_framerate_producer(
265                         producer,
266                         std::bind(&layer_producer::current_framerate, producer),
267                         destination_mode.framerate,
268                         destination_mode.field_mode,
269                         destination_mode.audio_cadence);
270 }
271
272 }}