]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
Refactored to use non-static data member initializers where it makes sense. Mixes...
[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
44 #include <boost/circular_buffer.hpp>
45 #include <boost/lexical_cast.hpp>
46 #include <boost/property_tree/ptree.hpp>
47 #include <boost/range/algorithm.hpp>
48 #include <boost/range/adaptors.hpp>
49 #include <boost/timer.hpp>
50
51 namespace caspar { namespace core {
52
53 struct output::impl
54 {               
55         spl::shared_ptr<diagnostics::graph>     graph_;
56         spl::shared_ptr<monitor::subject>       monitor_subject_        = spl::make_shared<monitor::subject>("/output");
57         const int                                                       channel_index_;
58         video_format_desc                                       format_desc_;
59         std::map<int, port>                                     ports_; 
60         prec_timer                                                      sync_timer_;
61         boost::circular_buffer<const_frame>     frames_;
62         executor                                                        executor_                       = { L"output" };
63 public:
64         impl(spl::shared_ptr<diagnostics::graph> graph, const video_format_desc& format_desc, int channel_index) 
65                 : graph_(std::move(graph))
66                 , channel_index_(channel_index)
67                 , format_desc_(format_desc)
68         {
69                 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8));
70         }       
71         
72         void add(int index, spl::shared_ptr<frame_consumer> consumer)
73         {               
74                 remove(index);
75
76                 consumer->initialize(format_desc_, channel_index_);
77                 
78                 executor_.begin_invoke([this, index, consumer]
79                 {                       
80                         port p(index, channel_index_, std::move(consumer));
81                         p.monitor_output().attach_parent(monitor_subject_);
82                         ports_.insert(std::make_pair(index, std::move(p)));
83                 }, task_priority::high_priority);
84         }
85
86         void add(const spl::shared_ptr<frame_consumer>& consumer)
87         {
88                 add(consumer->index(), consumer);
89         }
90
91         void remove(int index)
92         {               
93                 executor_.begin_invoke([=]
94                 {
95                         auto it = ports_.find(index);
96                         if(it != ports_.end())
97                                 ports_.erase(it);                                       
98                 }, task_priority::high_priority);
99         }
100
101         void remove(const spl::shared_ptr<frame_consumer>& consumer)
102         {
103                 remove(consumer->index());
104         }
105         
106         void video_format_desc(const core::video_format_desc& format_desc)
107         {
108                 executor_.invoke([&]
109                 {
110                         if(format_desc_ == format_desc)
111                                 return;
112
113                         auto it = ports_.begin();
114                         while(it != ports_.end())
115                         {                                               
116                                 try
117                                 {
118                                         it->second.video_format_desc(format_desc);
119                                         ++it;
120                                 }
121                                 catch(...)
122                                 {
123                                         CASPAR_LOG_CURRENT_EXCEPTION();
124                                         ports_.erase(it++);
125                                 }
126                         }
127                         
128                         format_desc_ = format_desc;
129                         frames_.clear();
130                 });
131         }
132
133         std::pair<int, int> minmax_buffer_depth() const
134         {               
135                 if(ports_.empty())
136                         return std::make_pair(0, 0);
137                 
138                 auto buffer_depths = ports_ | 
139                                                          boost::adaptors::map_values |
140                                                          boost::adaptors::transformed([](const port& p){return p.buffer_depth();}); 
141                 
142
143                 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));
144         }
145
146         bool has_synchronization_clock() const
147         {
148                 return boost::range::count_if(ports_ | boost::adaptors::map_values,
149                                                                           [](const port& p){return p.has_synchronization_clock();}) > 0;
150         }
151                 
152         void operator()(const_frame input_frame, const core::video_format_desc& format_desc)
153         {
154                 boost::timer frame_timer;
155
156                 video_format_desc(format_desc);         
157
158                 executor_.invoke([=]
159                 {                       
160
161                         if(!has_synchronization_clock())
162                                 sync_timer_.tick(1.0/format_desc_.fps);
163                                 
164                         if(input_frame.size() != format_desc_.size)
165                         {
166                                 CASPAR_LOG(debug) << print() << L" Invalid input frame dimension.";
167                                 return;
168                         }
169
170                         auto minmax = minmax_buffer_depth();
171
172                         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.
173                         frames_.push_back(input_frame);
174
175                         if(!frames_.full())
176                                 return;
177
178                         std::map<int, std::future<bool>> send_results;
179
180                         // Start invocations
181                         for (auto it = ports_.begin(); it != ports_.end();)
182                         {
183                                 auto& port      = it->second;
184                                 auto& frame     = frames_.at(port.buffer_depth()-minmax.first);
185                                         
186                                 try
187                                 {
188                                         send_results.insert(std::make_pair(it->first, port.send(frame)));
189                                         ++it;
190                                 }
191                                 catch (...)
192                                 {
193                                         CASPAR_LOG_CURRENT_EXCEPTION();
194                                         try
195                                         {
196                                                 send_results.insert(std::make_pair(it->first, port.send(frame)));
197                                                 ++it;
198                                         }
199                                         catch(...)
200                                         {
201                                                 CASPAR_LOG_CURRENT_EXCEPTION();
202                                                 CASPAR_LOG(error) << "Failed to recover consumer: " << port.print() << L". Removing it.";
203                                                 it = ports_.erase(it);
204                                         }
205                                 }
206                         }
207
208                         // Retrieve results
209                         for (auto it = send_results.begin(); it != send_results.end(); ++it)
210                         {
211                                 try
212                                 {
213                                         if (!it->second.get())
214                                                 ports_.erase(it->first);
215                                 }
216                                 catch (...)
217                                 {
218                                         CASPAR_LOG_CURRENT_EXCEPTION();
219                                         ports_.erase(it->first);
220                                 }
221                         }
222                 });
223
224                 graph_->set_value("consume-time", frame_timer.elapsed()*format_desc.fps*0.5);
225         }
226
227         std::wstring print() const
228         {
229                 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
230         }
231
232         std::future<boost::property_tree::wptree> info()
233         {
234                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
235                 {                       
236                         boost::property_tree::wptree info;
237                         for (auto& port : ports_)
238                         {
239                                 info.add_child(L"consumers.consumer", port.second.info())
240                                         .add(L"index", port.first); 
241                         }
242                         return info;
243                 }, task_priority::high_priority));
244         }
245 };
246
247 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)){}
248 void output::add(int index, const spl::shared_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}
249 void output::add(const spl::shared_ptr<frame_consumer>& consumer){impl_->add(consumer);}
250 void output::remove(int index){impl_->remove(index);}
251 void output::remove(const spl::shared_ptr<frame_consumer>& consumer){impl_->remove(consumer);}
252 std::future<boost::property_tree::wptree> output::info() const{return impl_->info();}
253 void output::operator()(const_frame frame, const video_format_desc& format_desc){(*impl_)(std::move(frame), format_desc);}
254 monitor::subject& output::monitor_output() {return *impl_->monitor_subject_;}
255 }}