]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
Merged asynchronous invocation of consumers 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
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         monitor::basic_subject                          event_subject_;
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_;              
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                 , event_subject_("output")
67                 , channel_index_(channel_index)
68                 , format_desc_(format_desc)
69                 , executor_(L"output")
70         {
71                 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8));
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.subscribe(event_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                                 ports_.erase(it);                                       
100                 }, task_priority::high_priority);
101         }
102
103         void remove(const spl::shared_ptr<frame_consumer>& consumer)
104         {
105                 remove(consumer->index());
106         }
107         
108         void video_format_desc(const core::video_format_desc& format_desc)
109         {
110                 executor_.invoke([&]
111                 {
112                         if(format_desc_ == format_desc)
113                                 return;
114
115                         auto it = ports_.begin();
116                         while(it != ports_.end())
117                         {                                               
118                                 try
119                                 {
120                                         it->second.video_format_desc(format_desc);
121                                         ++it;
122                                 }
123                                 catch(...)
124                                 {
125                                         CASPAR_LOG_CURRENT_EXCEPTION();
126                                         ports_.erase(it++);
127                                 }
128                         }
129                         
130                         format_desc_ = format_desc;
131                         frames_.clear();
132                 });
133         }
134
135         std::pair<int, int> minmax_buffer_depth() const
136         {               
137                 if(ports_.empty())
138                         return std::make_pair(0, 0);
139                 
140                 auto buffer_depths = ports_ | 
141                                                          boost::adaptors::map_values | // std::function is MSVC workaround
142                                                          boost::adaptors::transformed(std::function<int(const port&)>([](const port& p){return p.buffer_depth();})); 
143                 
144
145                 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));
146         }
147
148         bool has_synchronization_clock() const
149         {
150                 return boost::range::count_if(ports_ | boost::adaptors::map_values,
151                                                                           [](const port& p){return p.has_synchronization_clock();}) > 0;
152         }
153                 
154         void operator()(const_frame input_frame, const core::video_format_desc& format_desc)
155         {
156                 boost::timer frame_timer;
157
158                 video_format_desc(format_desc);         
159
160                 executor_.invoke([=]
161                 {                       
162
163                         if(!has_synchronization_clock())
164                                 sync_timer_.tick(1.0/format_desc_.fps);
165                                 
166                         if(input_frame.size() != format_desc_.size)
167                         {
168                                 CASPAR_LOG(debug) << print() << L" Invalid input frame dimension.";
169                                 return;
170                         }
171
172                         auto minmax = minmax_buffer_depth();
173
174                         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.
175                         frames_.push_back(input_frame);
176
177                         if(!frames_.full())
178                                 return;
179
180                         std::map<int, boost::unique_future<bool>> send_results;
181
182                         // Start invocations
183                         for (auto it = ports_.begin(); it != ports_.end(); ++it)
184                         {
185                                 auto& port      = it->second;
186                                 auto& frame     = frames_.at(port.buffer_depth()-minmax.first);
187                                         
188                                 try
189                                 {
190                                         send_results.insert(std::make_pair(it->first, port.send(frame)));
191                                 }
192                                 catch (...)
193                                 {
194                                         CASPAR_LOG_CURRENT_EXCEPTION();
195                                         ports_.erase(it);
196                                 }
197                         }
198
199                         // Retrieve results
200                         for (auto it = send_results.begin(); it != send_results.end(); ++it)
201                         {
202                                 try
203                                 {
204                                         if (!it->second.get())
205                                                 ports_.erase(it->first);
206                                 }
207                                 catch (...)
208                                 {
209                                         CASPAR_LOG_CURRENT_EXCEPTION();
210                                         ports_.erase(it->first);
211                                 }
212                         }
213                 });
214
215                 graph_->set_value("consume-time", frame_timer.elapsed()*format_desc.fps*0.5);
216         }
217
218         std::wstring print() const
219         {
220                 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";
221         }
222
223         boost::unique_future<boost::property_tree::wptree> info()
224         {
225                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree
226                 {                       
227                         boost::property_tree::wptree info;
228                         BOOST_FOREACH(auto& port, ports_)
229                         {
230                                 info.add_child(L"consumers.consumer", port.second.info())
231                                         .add(L"index", port.first); 
232                         }
233                         return info;
234                 }, task_priority::high_priority));
235         }
236 };
237
238 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)){}
239 void output::add(int index, const spl::shared_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}
240 void output::add(const spl::shared_ptr<frame_consumer>& consumer){impl_->add(consumer);}
241 void output::remove(int index){impl_->remove(index);}
242 void output::remove(const spl::shared_ptr<frame_consumer>& consumer){impl_->remove(consumer);}
243 boost::unique_future<boost::property_tree::wptree> output::info() const{return impl_->info();}
244 void output::operator()(const_frame frame, const video_format_desc& format_desc){(*impl_)(std::move(frame), format_desc);}
245 void output::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}
246 void output::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}
247 }}