]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
9e2b05e61dd979195a40b236c6bb99df375dd8ff
[casparcg] / core / consumer / output.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../StdAfx.h"\r
23 \r
24 #ifdef _MSC_VER\r
25 #pragma warning (disable : 4244)\r
26 #endif\r
27 \r
28 #include "output.h"\r
29 \r
30 #include "../video_format.h"\r
31 #include "../mixer/gpu/ogl_device.h"\r
32 #include "../mixer/read_frame.h"\r
33 \r
34 #include <common/concurrency/executor.h>\r
35 #include <common/utility/assert.h>\r
36 #include <common/utility/timer.h>\r
37 #include <common/memory/memshfl.h>\r
38 #include <common/env.h>\r
39 \r
40 #include <boost/circular_buffer.hpp>\r
41 #include <boost/timer.hpp>\r
42 #include <boost/range/algorithm.hpp>\r
43 #include <boost/range/adaptors.hpp>\r
44 #include <boost/property_tree/ptree.hpp>\r
45 \r
46 namespace caspar { namespace core {\r
47         \r
48 struct output::implementation\r
49 {               \r
50         const int                                                                               channel_index_;\r
51         const safe_ptr<diagnostics::graph>                              graph_;\r
52         monitor::subject                                                                monitor_subject_;\r
53         boost::timer                                                                    consume_timer_;\r
54 \r
55         video_format_desc                                                               format_desc_;\r
56         channel_layout                                                                  audio_channel_layout_;\r
57 \r
58         std::map<int, safe_ptr<frame_consumer>>                 consumers_;\r
59         \r
60         high_prec_timer                                                                 sync_timer_;\r
61 \r
62         boost::circular_buffer<safe_ptr<read_frame>>    frames_;\r
63         std::map<int, int64_t>                                                  send_to_consumers_delays_;\r
64 \r
65         executor                                                                                executor_;\r
66                 \r
67 public:\r
68         implementation(\r
69                         const safe_ptr<diagnostics::graph>& graph,\r
70                         const video_format_desc& format_desc,\r
71                         const channel_layout& audio_channel_layout,\r
72                         int channel_index) \r
73                 : channel_index_(channel_index)\r
74                 , graph_(graph)\r
75                 , monitor_subject_("/output")\r
76                 , format_desc_(format_desc)\r
77                 , audio_channel_layout_(audio_channel_layout)\r
78                 , executor_(L"output")\r
79         {\r
80                 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8));\r
81         }\r
82 \r
83         void add(int index, safe_ptr<frame_consumer> consumer)\r
84         {               \r
85                 remove(index);\r
86 \r
87                 consumer = create_consumer_cadence_guard(consumer);\r
88                 consumer->initialize(format_desc_, audio_channel_layout_, channel_index_);\r
89 \r
90                 executor_.invoke([&]\r
91                 {\r
92                         consumers_.insert(std::make_pair(index, consumer));\r
93                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
94                 }, high_priority);\r
95         }\r
96 \r
97         void add(const safe_ptr<frame_consumer>& consumer)\r
98         {\r
99                 add(consumer->index(), consumer);\r
100         }\r
101 \r
102         void remove(int index)\r
103         {               \r
104                 // Destroy  consumer on calling thread:\r
105                 std::shared_ptr<frame_consumer> old_consumer;\r
106 \r
107                 executor_.invoke([&]\r
108                 {\r
109                         auto it = consumers_.find(index);\r
110                         if(it != consumers_.end())\r
111                         {\r
112                                 old_consumer = it->second;\r
113                                 send_to_consumers_delays_.erase(it->first);\r
114                                 consumers_.erase(it);\r
115                         }\r
116                 }, high_priority);\r
117 \r
118                 if(old_consumer)\r
119                 {\r
120                         auto str = old_consumer->print();\r
121                         old_consumer.reset();\r
122                         CASPAR_LOG(info) << print() << L" " << str << L" Removed.";\r
123                 }\r
124         }\r
125 \r
126         void remove(const safe_ptr<frame_consumer>& consumer)\r
127         {\r
128                 remove(consumer->index());\r
129         }\r
130         \r
131         void set_video_format_desc(const video_format_desc& format_desc)\r
132         {\r
133                 executor_.invoke([&]\r
134                 {\r
135                         auto it = consumers_.begin();\r
136                         while(it != consumers_.end())\r
137                         {                                               \r
138                                 try\r
139                                 {\r
140                                         it->second->initialize(format_desc, audio_channel_layout_, channel_index_);\r
141                                         ++it;\r
142                                 }\r
143                                 catch(...)\r
144                                 {\r
145                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
146                                         CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
147                                         send_to_consumers_delays_.erase(it->first);\r
148                                         consumers_.erase(it++);\r
149                                 }\r
150                         }\r
151                         \r
152                         format_desc_ = format_desc;\r
153                         frames_.clear();\r
154                 });\r
155         }\r
156         \r
157         std::map<int, size_t> buffer_depths_snapshot() const\r
158         {\r
159                 std::map<int, size_t> result;\r
160 \r
161                 BOOST_FOREACH(auto& consumer, consumers_)\r
162                         result.insert(std::make_pair(\r
163                                         consumer.first,\r
164                                         consumer.second->buffer_depth()));\r
165 \r
166                 return std::move(result);\r
167         }\r
168 \r
169         std::pair<size_t, size_t> minmax_buffer_depth(\r
170                         const std::map<int, size_t>& buffer_depths) const\r
171         {               \r
172                 if(consumers_.empty())\r
173                         return std::make_pair(0, 0);\r
174                 \r
175                 auto depths = buffer_depths | boost::adaptors::map_values; \r
176                 \r
177                 return std::make_pair(\r
178                                 *boost::range::min_element(depths),\r
179                                 *boost::range::max_element(depths));\r
180         }\r
181 \r
182         bool has_synchronization_clock() const\r
183         {\r
184                 return boost::range::count_if(consumers_ | boost::adaptors::map_values, [](const safe_ptr<frame_consumer>& x){return x->has_synchronization_clock();}) > 0;\r
185         }\r
186 \r
187         void send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& packet)\r
188         {\r
189                 executor_.begin_invoke([=]\r
190                 {\r
191                         try\r
192                         {\r
193                                 consume_timer_.restart();\r
194 \r
195                                 auto input_frame = packet.first;\r
196 \r
197                                 if(!has_synchronization_clock())\r
198                                         sync_timer_.tick(1.0/format_desc_.fps);\r
199 \r
200                                 if(input_frame->image_size() != format_desc_.size)\r
201                                 {\r
202                                         sync_timer_.tick(1.0/format_desc_.fps);\r
203                                         return;\r
204                                 }\r
205                                 \r
206                                 auto buffer_depths = buffer_depths_snapshot();\r
207                                 auto minmax = minmax_buffer_depth(buffer_depths);\r
208 \r
209                                 frames_.set_capacity(minmax.second - minmax.first + 1);\r
210                                 frames_.push_back(input_frame);\r
211 \r
212                                 if(!frames_.full())\r
213                                         return;\r
214 \r
215                                 std::map<int, boost::unique_future<bool>> send_results;\r
216 \r
217                                 // Start invocations\r
218                                 for (auto it = consumers_.begin(); it != consumers_.end();)\r
219                                 {\r
220                                         auto consumer   = it->second;\r
221                                         auto frame              = frames_.at(buffer_depths[it->first]-minmax.first);\r
222 \r
223                                         send_to_consumers_delays_[it->first] = frame->get_age_millis();\r
224                                                 \r
225                                         try\r
226                                         {\r
227                                                 send_results.insert(std::make_pair(it->first, consumer->send(frame)));\r
228                                                 ++it;\r
229                                         }\r
230                                         catch(...)\r
231                                         {\r
232                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
233                                                 try\r
234                                                 {\r
235                                                         send_results.insert(std::make_pair(it->first, consumer->send(frame)));\r
236                                                         ++it;\r
237                                                 }\r
238                                                 catch(...)\r
239                                                 {\r
240                                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
241                                                         CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";\r
242                                                         send_to_consumers_delays_.erase(it->first);\r
243                                                         it = consumers_.erase(it);\r
244                                                 }\r
245                                         }\r
246                                 }\r
247 \r
248                                 // Retrieve results\r
249                                 for (auto result_it = send_results.begin(); result_it != send_results.end(); ++result_it)\r
250                                 {\r
251                                         auto consumer           = consumers_.at(result_it->first);\r
252                                         auto frame                      = frames_.at(buffer_depths[result_it->first]-minmax.first);\r
253                                         auto& result_future     = result_it->second;\r
254                                                 \r
255                                         try\r
256                                         {\r
257                                                 if (!result_future.timed_wait(boost::posix_time::seconds(2)))\r
258                                                 {\r
259                                                         BOOST_THROW_EXCEPTION(timed_out() << msg_info(narrow(print()) + " " + narrow(consumer->print()) + " Timed out during send"));\r
260                                                 }\r
261 \r
262                                                 if (!result_future.get())\r
263                                                 {\r
264                                                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Removed.";\r
265                                                         send_to_consumers_delays_.erase(result_it->first);\r
266                                                         consumers_.erase(result_it->first);\r
267                                                 }\r
268                                         }\r
269                                         catch (...)\r
270                                         {\r
271                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
272                                                 try\r
273                                                 {\r
274                                                         consumer->initialize(format_desc_, audio_channel_layout_, channel_index_);\r
275                                                         auto retry_future = consumer->send(frame);\r
276 \r
277                                                         if (!retry_future.timed_wait(boost::posix_time::seconds(2)))\r
278                                                         {\r
279                                                                 BOOST_THROW_EXCEPTION(timed_out() << msg_info(narrow(print()) + " " + narrow(consumer->print()) + " Timed out during retry"));\r
280                                                         }\r
281 \r
282                                                         if (!retry_future.get())\r
283                                                         {\r
284                                                                 CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Removed.";\r
285                                                                 send_to_consumers_delays_.erase(result_it->first);\r
286                                                                 consumers_.erase(result_it->first);\r
287                                                         }\r
288                                                 }\r
289                                                 catch (...)\r
290                                                 {\r
291                                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
292                                                         CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";\r
293                                                         send_to_consumers_delays_.erase(result_it->first);\r
294                                                         consumers_.erase(result_it->first);\r
295                                                 }\r
296                                         }\r
297                                 }\r
298                                                 \r
299                                 graph_->set_value("consume-time", consume_timer_.elapsed()*format_desc_.fps*0.5);\r
300                                 monitor_subject_ << monitor::message("/consume_time") % (consume_timer_.elapsed());\r
301                         }\r
302                         catch(...)\r
303                         {\r
304                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
305                         }\r
306                 });\r
307         }\r
308 \r
309         std::wstring print() const\r
310         {\r
311                 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";\r
312         }\r
313 \r
314         boost::unique_future<boost::property_tree::wptree> info()\r
315         {\r
316                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree\r
317                 {                       \r
318                         boost::property_tree::wptree info;\r
319                         BOOST_FOREACH(auto& consumer, consumers_)\r
320                         {\r
321                                 info.add_child(L"consumers.consumer", consumer.second->info())\r
322                                         .add(L"index", consumer.first); \r
323                         }\r
324                         return info;\r
325                 }, high_priority));\r
326         }\r
327 \r
328         boost::unique_future<boost::property_tree::wptree> delay_info()\r
329         {\r
330                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree\r
331                 {                       \r
332                         boost::property_tree::wptree info;\r
333                         BOOST_FOREACH(auto& consumer, consumers_)\r
334                         {\r
335                                 auto total_age =\r
336                                                 consumer.second->presentation_frame_age_millis();\r
337                                 auto sendoff_age = send_to_consumers_delays_[consumer.first];\r
338                                 auto presentation_time = total_age - sendoff_age;\r
339 \r
340                                 boost::property_tree::wptree child;\r
341                                 child.add(L"name", consumer.second->print());\r
342                                 child.add(L"age-at-arrival", sendoff_age);\r
343                                 child.add(L"presentation-time", presentation_time);\r
344                                 child.add(L"age-at-presentation", total_age);\r
345 \r
346                                 info.add_child(L"consumer", child);\r
347                         }\r
348                         return info;\r
349                 }, high_priority));\r
350         }\r
351 \r
352         bool empty()\r
353         {\r
354                 return executor_.invoke([this]\r
355                 {\r
356                         return consumers_.empty();\r
357                 });\r
358         }\r
359 \r
360         monitor::subject& monitor_output()\r
361         { \r
362                 return monitor_subject_;\r
363         }\r
364 };\r
365 \r
366 output::output(const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc, const channel_layout& audio_channel_layout, int channel_index) : impl_(new implementation(graph, format_desc, audio_channel_layout, channel_index)){}\r
367 void output::add(int index, const safe_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}\r
368 void output::add(const safe_ptr<frame_consumer>& consumer){impl_->add(consumer);}\r
369 void output::remove(int index){impl_->remove(index);}\r
370 void output::remove(const safe_ptr<frame_consumer>& consumer){impl_->remove(consumer);}\r
371 void output::send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& frame) {impl_->send(frame); }\r
372 void output::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
373 boost::unique_future<boost::property_tree::wptree> output::info() const{return impl_->info();}\r
374 boost::unique_future<boost::property_tree::wptree> output::delay_info() const{return impl_->delay_info();}\r
375 bool output::empty() const{return impl_->empty();}\r
376 monitor::subject& output::monitor_output() { return impl_->monitor_output(); }\r
377 }}