]> git.sesse.net Git - casparcg/blob - modules/reroute/producer/channel_producer.cpp
* Reenabled unit-test project after move to CMake.
[casparcg] / modules / reroute / producer / channel_producer.cpp
1 /*
2 * Copyright 2013 Sveriges Television AB http://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: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../stdafx.h"
23
24 #include "channel_producer.h"
25
26 #include <core/monitor/monitor.h>
27 #include <core/consumer/frame_consumer.h>
28 #include <core/consumer/output.h>
29 #include <core/producer/frame_producer.h>
30 #include <core/video_channel.h>
31
32 #include <core/frame/frame.h>
33 #include <core/frame/pixel_format.h>
34 #include <core/frame/audio_channel_layout.h>
35 #include <core/frame/draw_frame.h>
36 #include <core/frame/frame_factory.h>
37 #include <core/video_format.h>
38
39 #include <boost/thread/once.hpp>
40 #include <boost/lexical_cast.hpp>
41 #include <boost/property_tree/ptree.hpp>
42 #include <boost/range/algorithm/copy.hpp>
43
44 #include <common/except.h>
45 #include <common/memory.h>
46 #include <common/future.h>
47
48 #include <tbb/concurrent_queue.h>
49
50 #include <asmlib.h>
51
52 #include <queue>
53
54 namespace caspar { namespace reroute {
55
56 class channel_consumer : public core::frame_consumer
57 {       
58         core::monitor::subject                                                          monitor_subject_;
59         tbb::concurrent_bounded_queue<core::const_frame>        frame_buffer_;
60         core::video_format_desc                                                         format_desc_;
61         core::audio_channel_layout                                                      channel_layout_                 = core::audio_channel_layout::invalid();
62         int                                                                                                     channel_index_;
63         int                                                                                                     consumer_index_;
64         tbb::atomic<bool>                                                                       is_running_;
65         tbb::atomic<int64_t>                                                            current_age_;
66         std::promise<void>                                                                      first_frame_promise_;
67         std::future<void>                                                                       first_frame_available_;
68         bool                                                                                            first_frame_reported_;
69
70 public:
71         channel_consumer()
72                 : consumer_index_(next_consumer_index())
73                 , first_frame_available_(first_frame_promise_.get_future())
74                 , first_frame_reported_(false)
75         {
76                 is_running_ = true;
77                 current_age_ = 0;
78                 frame_buffer_.set_capacity(3);
79         }
80
81         static int next_consumer_index()
82         {
83                 static tbb::atomic<int> consumer_index_counter;
84                 static boost::once_flag consumer_index_counter_initialized;
85
86                 boost::call_once(consumer_index_counter_initialized, [&]()
87                 {
88                         consumer_index_counter = 0;
89                 });
90
91                 return ++consumer_index_counter;
92         }
93
94         ~channel_consumer()
95         {
96         }
97
98         // frame_consumer
99
100         std::future<bool> send(core::const_frame frame) override
101         {
102                 bool pushed = frame_buffer_.try_push(frame);
103
104                 if (pushed && !first_frame_reported_)
105                 {
106                         first_frame_promise_.set_value();
107                         first_frame_reported_ = true;
108                 }
109
110                 return make_ready_future(is_running_.load());
111         }
112
113         void initialize(
114                         const core::video_format_desc& format_desc,
115                         const core::audio_channel_layout& channel_layout,
116                         int channel_index) override
117         {
118                 format_desc_    = format_desc;
119                 channel_layout_ = channel_layout;
120                 channel_index_  = channel_index;
121         }
122
123         std::wstring name() const override
124         {
125                 return L"channel-consumer";
126         }
127
128         int64_t presentation_frame_age_millis() const override
129         {
130                 return current_age_;
131         }
132
133         std::wstring print() const override
134         {
135                 return L"[channel-consumer|" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
136         }
137
138         boost::property_tree::wptree info() const override
139         {
140                 boost::property_tree::wptree info;
141                 info.add(L"type", L"channel-consumer");
142                 info.add(L"channel-index", channel_index_);
143                 return info;
144         }
145         
146         bool has_synchronization_clock() const override
147         {
148                 return false;
149         }
150
151         int buffer_depth() const override
152         {
153                 return -1;
154         }
155
156         int index() const override
157         {
158                 return 78500 + consumer_index_;
159         }
160
161         core::monitor::subject& monitor_output() override
162         {
163                 return monitor_subject_;
164         }
165
166         // channel_consumer
167         
168         const core::video_format_desc& get_video_format_desc()
169         {
170                 return format_desc_;
171         }
172
173         const core::audio_channel_layout& get_audio_channel_layout()
174         {
175                 return channel_layout_;
176         }
177
178         void block_until_first_frame_available()
179         {
180                 if (first_frame_available_.wait_for(std::chrono::seconds(2)) == std::future_status::timeout)
181                         CASPAR_LOG(warning)
182                                         << print() << L" Timed out while waiting for first frame";
183         }
184
185         core::const_frame receive()
186         {
187                 core::const_frame frame = core::const_frame::empty();
188
189                 if (!is_running_)
190                         return frame;
191                 
192                 if (frame_buffer_.try_pop(frame))
193                         current_age_ = frame.get_age_millis();
194
195                 return frame;
196         }
197
198         void stop()
199         {
200                 is_running_ = false;
201         }
202 };
203         
204 class channel_producer : public core::frame_producer_base
205 {
206         core::monitor::subject                                          monitor_subject_;
207
208         const spl::shared_ptr<core::frame_factory>      frame_factory_;
209         const core::video_format_desc                           output_format_desc_;
210         const spl::shared_ptr<channel_consumer>         consumer_;
211         core::constraints                                                       pixel_constraints_;
212
213         std::queue<core::draw_frame>                            frame_buffer_;
214         uint64_t                                                                        frame_number_;
215
216 public:
217         explicit channel_producer(const core::frame_producer_dependencies& dependecies, const spl::shared_ptr<core::video_channel>& channel) 
218                 : frame_factory_(dependecies.frame_factory)
219                 , output_format_desc_(dependecies.format_desc)
220                 , frame_number_(0)
221         {
222                 pixel_constraints_.width.set(output_format_desc_.width);
223                 pixel_constraints_.height.set(output_format_desc_.height);
224                 channel->output().add(consumer_);
225                 consumer_->block_until_first_frame_available();
226                 CASPAR_LOG(info) << print() << L" Initialized";
227         }
228
229         ~channel_producer()
230         {
231                 consumer_->stop();
232                 CASPAR_LOG(info) << print() << L" Uninitialized";
233         }
234
235         // frame_producer
236                         
237         core::draw_frame receive_impl() override
238         {
239                 auto format_desc = consumer_->get_video_format_desc();
240
241                 if(frame_buffer_.size() > 0)
242                 {
243                         auto frame = frame_buffer_.front();
244                         frame_buffer_.pop();
245                         return frame;
246                 }
247                 
248                 auto read_frame = consumer_->receive();
249                 if(read_frame == core::const_frame::empty() || read_frame.image_data().empty())
250                         return core::draw_frame::late();
251
252                 frame_number_++;
253                 
254                 bool double_speed       = std::abs(output_format_desc_.fps / 2.0 - format_desc.fps) < 0.01;
255                 bool half_speed         = std::abs(format_desc.fps / 2.0 - output_format_desc_.fps) < 0.01;
256
257                 if(half_speed && frame_number_ % 2 == 0) // Skip frame
258                         return receive_impl();
259
260                 core::pixel_format_desc desc;
261                 desc.format = core::pixel_format::bgra;
262                 desc.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));
263                 auto frame = frame_factory_->create_frame(this, desc, consumer_->get_audio_channel_layout());
264
265                 bool copy_audio = !double_speed && !half_speed;
266
267                 if (copy_audio)
268                 {
269                         frame.audio_data().reserve(read_frame.audio_data().size());
270                         boost::copy(read_frame.audio_data(), std::back_inserter(frame.audio_data()));
271                 }
272
273                 A_memcpy(frame.image_data().begin(), read_frame.image_data().begin(), read_frame.image_data().size());
274
275                 frame_buffer_.push(core::draw_frame(std::move(frame)));
276                 
277                 if(double_speed)
278                         frame_buffer_.push(frame_buffer_.back());
279
280                 return receive_impl();
281         }       
282
283         std::wstring name() const override
284         {
285                 return L"channel-producer";
286         }
287
288         std::wstring print() const override
289         {
290                 return L"channel-producer[]";
291         }
292
293         core::constraints& pixel_constraints() override
294         {
295                 return pixel_constraints_;
296         }
297
298         boost::property_tree::wptree info() const override
299         {
300                 boost::property_tree::wptree info;
301                 info.add(L"type", L"channel-producer");
302                 return info;
303         }
304
305         core::monitor::subject& monitor_output() override
306         {
307                 return monitor_subject_;
308         }
309 };
310
311 spl::shared_ptr<core::frame_producer> create_channel_producer(
312                 const core::frame_producer_dependencies& dependencies,
313                 const spl::shared_ptr<core::video_channel>& channel)
314 {
315         return spl::make_shared<channel_producer>(dependencies, channel);
316 }
317
318 }}