]> git.sesse.net Git - casparcg/blob - modules/reroute/producer/layer_producer.cpp
[reroute] Fixed serious out of memory situation where too many audio samples are...
[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 spl::shared_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                 channel_->stage().remove_layer_consumer(this, layer_);
172                 CASPAR_LOG(info) << print() << L" Uninitialized";
173         }
174
175         // frame_producer
176
177         core::draw_frame receive_impl() override
178         {
179                 if (!frame_buffer_.empty())
180                 {
181                         last_frame_ = frame_buffer_.front();
182                         frame_buffer_.pop();
183                         return last_frame_;
184                 }
185
186                 auto consumer_frame = consumer_->receive();
187                 if (consumer_frame == core::draw_frame::late())
188                         return last_frame_;
189
190                 auto actual_frames = extract_actual_frames(std::move(consumer_frame), channel_->video_format_desc().field_mode);
191                 double_framerate_ = actual_frames.size() == 2;
192
193                 for (auto& frame : actual_frames)
194                         frame_buffer_.push(std::move(frame));
195
196                 return receive_impl();
197         }
198
199         core::draw_frame last_frame() override
200         {
201                 return last_frame_;
202         }
203
204         std::wstring print() const override
205         {
206                 return L"layer-producer[" + boost::lexical_cast<std::wstring>(layer_) + L"]";
207         }
208
209         std::wstring name() const override
210         {
211                 return L"layer-producer";
212         }
213
214         boost::property_tree::wptree info() const override
215         {
216                 boost::property_tree::wptree info;
217                 info.add(L"type", L"layer-producer");
218                 return info;
219         }
220
221         core::monitor::subject& monitor_output() override
222         {
223                 return monitor_subject_;
224         }
225
226         core::constraints& pixel_constraints() override
227         {
228                 return pixel_constraints_;
229         }
230
231         boost::rational<int> current_framerate() const
232         {
233                 return double_framerate_
234                                 ? channel_->video_format_desc().framerate * 2
235                                 : channel_->video_format_desc().framerate;
236         }
237 };
238
239 spl::shared_ptr<core::frame_producer> create_layer_producer(
240                 const spl::shared_ptr<core::video_channel>& channel,
241                 int layer,
242                 int frames_delay,
243                 const core::video_format_desc& destination_mode)
244 {
245         auto producer = spl::make_shared<layer_producer>(channel, layer, frames_delay);
246
247         return core::create_framerate_producer(
248                         producer,
249                         std::bind(&layer_producer::current_framerate, producer),
250                         destination_mode.framerate,
251                         destination_mode.field_mode,
252                         destination_mode.audio_cadence);
253 }
254
255 }}