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