]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
* Added some more */profiler/time OSC messages for external monitoring of frame time...
[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         void operator()(const_frame input_frame, const core::video_format_desc& format_desc, const core::audio_channel_layout& channel_layout)
164         {
165                 caspar::timer frame_timer;
166
167                 change_channel_format(format_desc, channel_layout);
168
169                 executor_.invoke([=]
170                 {                       
171                         if(!has_synchronization_clock())
172                                 sync_timer_.tick(1.0/format_desc_.fps);
173                                 
174                         if(input_frame.size() != format_desc_.size)
175                         {
176                                 CASPAR_LOG(debug) << print() << L" Invalid input frame dimension.";
177                                 return;
178                         }
179
180                         auto minmax = minmax_buffer_depth();
181
182                         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.
183                         frames_.push_back(input_frame);
184
185                         if(!frames_.full())
186                                 return;
187
188                         std::map<int, std::future<bool>> send_results;
189
190                         // Start invocations
191                         for (auto it = ports_.begin(); it != ports_.end();)
192                         {
193                                 auto& port      = it->second;
194                                 auto depth = port.buffer_depth();
195                                 auto& frame = depth < 0 ? frames_.back() : frames_.at(depth - minmax.first);
196
197                                 send_to_consumers_delays_[it->first] = frame.get_age_millis();
198                                         
199                                 try
200                                 {
201                                         send_results.insert(std::make_pair(it->first, port.send(frame)));
202                                         ++it;
203                                 }
204                                 catch (...)
205                                 {
206                                         CASPAR_LOG_CURRENT_EXCEPTION();
207                                         try
208                                         {
209                                                 send_results.insert(std::make_pair(it->first, port.send(frame)));
210                                                 ++it;
211                                         }
212                                         catch(...)
213                                         {
214                                                 CASPAR_LOG_CURRENT_EXCEPTION();
215                                                 CASPAR_LOG(error) << "Failed to recover consumer: " << port.print() << L". Removing it.";
216                                                 send_to_consumers_delays_.erase(it->first);
217                                                 it = ports_.erase(it);
218                                         }
219                                 }
220                         }
221
222                         // Retrieve results
223                         for (auto it = send_results.begin(); it != send_results.end(); ++it)
224                         {
225                                 try
226                                 {
227                                         if (!it->second.get())
228                                         {
229                                                 send_to_consumers_delays_.erase(it->first);
230                                                 ports_.erase(it->first);
231                                         }
232                                 }
233                                 catch (...)
234                                 {
235                                         CASPAR_LOG_CURRENT_EXCEPTION();
236                                         send_to_consumers_delays_.erase(it->first);
237                                         ports_.erase(it->first);
238                                 }
239                         }
240                 });
241
242                 auto consume_time = frame_timer.elapsed();
243                 graph_->set_value("consume-time", consume_time * format_desc.fps * 0.5);
244                 *monitor_subject_
245                                 << monitor::message("/consume_time")    % consume_time
246                                 << monitor::message("/profiler/time")   % consume_time          % (1.0 / format_desc.fps);
247         }
248
249         std::wstring print() const
250         {
251                 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
252         }
253
254         std::future<boost::property_tree::wptree> info()
255         {
256                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
257                 {                       
258                         boost::property_tree::wptree info;
259                         for (auto& port : ports_)
260                         {
261                                 info.add_child(L"consumers.consumer", port.second.info())
262                                         .add(L"index", port.first); 
263                         }
264                         return info;
265                 }, task_priority::high_priority));
266         }
267
268         std::future<boost::property_tree::wptree> delay_info()
269         {
270                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
271                 {
272                         boost::property_tree::wptree info;
273
274                         for (auto& port : ports_)
275                         {
276                                 auto total_age =
277                                         port.second.presentation_frame_age_millis();
278                                 auto sendoff_age = send_to_consumers_delays_[port.first];
279                                 auto presentation_time = total_age - sendoff_age;
280
281                                 boost::property_tree::wptree child;
282                                 child.add(L"name", port.second.print());
283                                 child.add(L"age-at-arrival", sendoff_age);
284                                 child.add(L"presentation-time", presentation_time);
285                                 child.add(L"age-at-presentation", total_age);
286
287                                 info.add_child(L"consumer", child);
288                         }
289                         return info;
290                 }, task_priority::high_priority));
291         }
292 };
293
294 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)){}
295 void output::add(int index, const spl::shared_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}
296 void output::add(const spl::shared_ptr<frame_consumer>& consumer){impl_->add(consumer);}
297 void output::remove(int index){impl_->remove(index);}
298 void output::remove(const spl::shared_ptr<frame_consumer>& consumer){impl_->remove(consumer);}
299 std::future<boost::property_tree::wptree> output::info() const{return impl_->info();}
300 std::future<boost::property_tree::wptree> output::delay_info() const{ return impl_->delay_info(); }
301 void output::operator()(const_frame frame, const video_format_desc& format_desc, const core::audio_channel_layout& channel_layout){ (*impl_)(std::move(frame), format_desc, channel_layout); }
302 monitor::subject& output::monitor_output() {return *impl_->monitor_subject_;}
303 }}