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