]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
Created a consumer that provides sync to a channel based on the pace of another chann...
[casparcg] / core / consumer / output.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: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../StdAfx.h"
23
24 #ifdef _MSC_VER
25 #pragma warning (disable : 4244)
26 #endif
27
28 #include "output.h"
29
30 #include "frame_consumer.h"
31 #include "port.h"
32
33 #include "../video_format.h"
34 #include "../frame/frame.h"
35 #include "../frame/audio_channel_layout.h"
36
37 #include <common/assert.h>
38 #include <common/future.h>
39 #include <common/executor.h>
40 #include <common/diagnostics/graph.h>
41 #include <common/prec_timer.h>
42 #include <common/memshfl.h>
43 #include <common/env.h>
44 #include <common/linq.h>
45 #include <common/timer.h>
46
47 #include <boost/circular_buffer.hpp>
48 #include <boost/lexical_cast.hpp>
49 #include <boost/property_tree/ptree.hpp>
50
51 #include <functional>
52
53 namespace caspar { namespace core {
54
55 struct output::impl
56 {
57         spl::shared_ptr<diagnostics::graph>     graph_;
58         spl::shared_ptr<monitor::subject>       monitor_subject_                        = spl::make_shared<monitor::subject>("/output");
59         const int                                                       channel_index_;
60         video_format_desc                                       format_desc_;
61         audio_channel_layout                            channel_layout_;
62         std::map<int, port>                                     ports_;
63         prec_timer                                                      sync_timer_;
64         boost::circular_buffer<const_frame>     frames_;
65         std::map<int, int64_t>                          send_to_consumers_delays_;
66         executor                                                        executor_                                       { L"output " + boost::lexical_cast<std::wstring>(channel_index_) };
67 public:
68         impl(spl::shared_ptr<diagnostics::graph> graph, const video_format_desc& format_desc, const audio_channel_layout& channel_layout, int channel_index)
69                 : graph_(std::move(graph))
70                 , channel_index_(channel_index)
71                 , format_desc_(format_desc)
72                 , channel_layout_(channel_layout)
73         {
74                 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8f));
75         }
76
77         void add(int index, spl::shared_ptr<frame_consumer> consumer)
78         {
79                 remove(index);
80
81                 consumer->initialize(format_desc_, channel_layout_, channel_index_);
82
83                 executor_.begin_invoke([this, index, consumer]
84                 {
85                         port p(index, channel_index_, std::move(consumer));
86                         p.monitor_output().attach_parent(monitor_subject_);
87                         ports_.insert(std::make_pair(index, std::move(p)));
88                 }, task_priority::high_priority);
89         }
90
91         void add(const spl::shared_ptr<frame_consumer>& consumer)
92         {
93                 add(consumer->index(), consumer);
94         }
95
96         void remove(int index)
97         {
98                 executor_.begin_invoke([=]
99                 {
100                         auto it = ports_.find(index);
101                         if (it != ports_.end())
102                         {
103                                 ports_.erase(it);
104                                 send_to_consumers_delays_.erase(index);
105                         }
106                 }, task_priority::high_priority);
107         }
108
109         void remove(const spl::shared_ptr<frame_consumer>& consumer)
110         {
111                 remove(consumer->index());
112         }
113
114         void change_channel_format(const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout)
115         {
116                 executor_.invoke([&]
117                 {
118                         if(format_desc_ == format_desc && channel_layout_ == channel_layout)
119                                 return;
120
121                         auto it = ports_.begin();
122                         while(it != ports_.end())
123                         {
124                                 try
125                                 {
126                                         it->second.change_channel_format(format_desc, channel_layout);
127                                         ++it;
128                                 }
129                                 catch(...)
130                                 {
131                                         CASPAR_LOG_CURRENT_EXCEPTION();
132                                         send_to_consumers_delays_.erase(it->first);
133                                         ports_.erase(it++);
134                                 }
135                         }
136
137                         format_desc_ = format_desc;
138                         channel_layout_ = channel_layout;
139                         frames_.clear();
140                 });
141         }
142
143         std::pair<int, int> minmax_buffer_depth() const
144         {
145                 if(ports_.empty())
146                         return std::make_pair(0, 0);
147
148                 return cpplinq::from(ports_)
149                         .select(values())
150                         .select(std::mem_fn(&port::buffer_depth))
151                         .where([](int v) { return v >= 0; })
152                         .aggregate(minmax::initial_value<int>(), minmax());
153         }
154
155         bool has_synchronization_clock() const
156         {
157                 return cpplinq::from(ports_)
158                         .select(values())
159                         .where(std::mem_fn(&port::has_synchronization_clock))
160                         .any();
161         }
162
163         std::future<void> operator()(const_frame input_frame, const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout)
164         {
165                 spl::shared_ptr<caspar::timer> frame_timer;
166
167                 change_channel_format(format_desc, channel_layout);
168
169                 auto pending_send_results = executor_.invoke([=]() -> std::shared_ptr<std::map<int, std::future<bool>>>
170                 {
171                         if (input_frame.size() != format_desc_.size)
172                         {
173                                 CASPAR_LOG(warning) << print() << L" Invalid input frame dimension.";
174                                 return nullptr;
175                         }
176
177                         auto minmax = minmax_buffer_depth();
178
179                         frames_.set_capacity(std::max(2, minmax.second - minmax.first) + 1); // std::max(2, x) since we want to guarantee some pipeline depth for asycnhronous mixer read-back.
180                         frames_.push_back(input_frame);
181
182                         if (!frames_.full())
183                                 return nullptr;
184
185                         spl::shared_ptr<std::map<int, std::future<bool>>> send_results;
186
187                         // Start invocations
188                         for (auto it = ports_.begin(); it != ports_.end();)
189                         {
190                                 auto& port = it->second;
191                                 auto depth = port.buffer_depth();
192                                 auto& frame = depth < 0 ? frames_.back() : frames_.at(depth - minmax.first);
193
194                                 send_to_consumers_delays_[it->first] = frame.get_age_millis();
195
196                                 try
197                                 {
198                                         send_results->insert(std::make_pair(it->first, port.send(frame)));
199                                         ++it;
200                                 }
201                                 catch (...)
202                                 {
203                                         CASPAR_LOG_CURRENT_EXCEPTION();
204                                         try
205                                         {
206                                                 send_results->insert(std::make_pair(it->first, port.send(frame)));
207                                                 ++it;
208                                         }
209                                         catch (...)
210                                         {
211                                                 CASPAR_LOG_CURRENT_EXCEPTION();
212                                                 CASPAR_LOG(error) << "Failed to recover consumer: " << port.print() << L". Removing it.";
213                                                 send_to_consumers_delays_.erase(it->first);
214                                                 it = ports_.erase(it);
215                                         }
216                                 }
217                         }
218
219                         return send_results;
220                 });
221
222                 if (!pending_send_results)
223                         return make_ready_future();
224
225                 return executor_.begin_invoke([=]()
226                 {
227                         // Retrieve results
228                         for (auto it = pending_send_results->begin(); it != pending_send_results->end(); ++it)
229                         {
230                                 try
231                                 {
232                                         if (!it->second.get())
233                                         {
234                                                 send_to_consumers_delays_.erase(it->first);
235                                                 ports_.erase(it->first);
236                                         }
237                                 }
238                                 catch (...)
239                                 {
240                                         CASPAR_LOG_CURRENT_EXCEPTION();
241                                         send_to_consumers_delays_.erase(it->first);
242                                         ports_.erase(it->first);
243                                 }
244                         }
245
246                         if (!has_synchronization_clock())
247                                 sync_timer_.tick(1.0 / format_desc_.fps);
248
249                         auto consume_time = frame_timer->elapsed();
250                         graph_->set_value("consume-time", consume_time * format_desc.fps * 0.5);
251                         *monitor_subject_
252                                 << monitor::message("/consume_time") % consume_time
253                                 << monitor::message("/profiler/time") % consume_time % (1.0 / format_desc.fps);
254                 });
255         }
256
257         std::wstring print() const
258         {
259                 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
260         }
261
262         std::future<boost::property_tree::wptree> info()
263         {
264                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
265                 {
266                         boost::property_tree::wptree info;
267                         for (auto& port : ports_)
268                         {
269                                 info.add_child(L"consumers.consumer", port.second.info())
270                                         .add(L"index", port.first);
271                         }
272                         return info;
273                 }, task_priority::high_priority));
274         }
275
276         std::future<boost::property_tree::wptree> delay_info()
277         {
278                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
279                 {
280                         boost::property_tree::wptree info;
281
282                         for (auto& port : ports_)
283                         {
284                                 auto total_age =
285                                         port.second.presentation_frame_age_millis();
286                                 auto sendoff_age = send_to_consumers_delays_[port.first];
287                                 auto presentation_time = total_age - sendoff_age;
288
289                                 boost::property_tree::wptree child;
290                                 child.add(L"name", port.second.print());
291                                 child.add(L"age-at-arrival", sendoff_age);
292                                 child.add(L"presentation-time", presentation_time);
293                                 child.add(L"age-at-presentation", total_age);
294
295                                 info.add_child(L"consumer", child);
296                         }
297                         return info;
298                 }, task_priority::high_priority));
299         }
300
301         std::vector<spl::shared_ptr<const frame_consumer>> get_consumers()
302         {
303                 return executor_.invoke([=]
304                 {
305                         std::vector<spl::shared_ptr<const frame_consumer>> consumers;
306
307                         for (auto& port : ports_)
308                                 consumers.push_back(port.second.consumer());
309
310                         return consumers;
311                 });
312         }
313 };
314
315 output::output(spl::shared_ptr<diagnostics::graph> graph, const video_format_desc& format_desc, const core::audio_channel_layout& channel_layout, int channel_index) : impl_(new impl(std::move(graph), format_desc, channel_layout, channel_index)){}
316 void output::add(int index, const spl::shared_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}
317 void output::add(const spl::shared_ptr<frame_consumer>& consumer){impl_->add(consumer);}
318 void output::remove(int index){impl_->remove(index);}
319 void output::remove(const spl::shared_ptr<frame_consumer>& consumer){impl_->remove(consumer);}
320 std::future<boost::property_tree::wptree> output::info() const{return impl_->info();}
321 std::future<boost::property_tree::wptree> output::delay_info() const{ return impl_->delay_info(); }
322 std::vector<spl::shared_ptr<const frame_consumer>> output::get_consumers() const { return impl_->get_consumers(); }
323 std::future<void> output::operator()(const_frame frame, const video_format_desc& format_desc, const core::audio_channel_layout& channel_layout){ return (*impl_)(std::move(frame), format_desc, channel_layout); }
324 monitor::subject& output::monitor_output() {return *impl_->monitor_subject_;}
325 }}