]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
2.1.0: executor: Refactored.
[casparcg] / core / consumer / output.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@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 "frame_consumer.h"\r
31 \r
32 #include "../video_format.h"\r
33 #include "../frame/data_frame.h"\r
34 \r
35 #include <common/concurrency/executor.h>\r
36 #include <common/diagnostics/graph.h>\r
37 #include <common/prec_timer.h>\r
38 #include <common/memory/memshfl.h>\r
39 #include <common/env.h>\r
40 \r
41 #include <common/assert.h>\r
42 #include <boost/circular_buffer.hpp>\r
43 #include <boost/timer.hpp>\r
44 #include <boost/range/algorithm.hpp>\r
45 #include <boost/range/adaptors.hpp>\r
46 #include <boost/property_tree/ptree.hpp>\r
47 \r
48 namespace caspar { namespace core {\r
49         \r
50 struct output::impl\r
51 {               \r
52         const int                                                                                       channel_index_;\r
53         video_format_desc                                                                       format_desc_;\r
54         std::map<int, safe_ptr<frame_consumer>>                         consumers_;     \r
55         prec_timer                                                                                      sync_timer_;\r
56         boost::circular_buffer<safe_ptr<const data_frame>>      frames_;\r
57 \r
58         executor                                                                                        executor_;              \r
59 public:\r
60         impl(const video_format_desc& format_desc, int channel_index) \r
61                 : channel_index_(channel_index)\r
62                 , format_desc_(format_desc)\r
63                 , executor_(L"output")\r
64         {\r
65         }       \r
66         \r
67         void add(int index, safe_ptr<frame_consumer> consumer)\r
68         {               \r
69                 remove(index);\r
70 \r
71                 consumer = create_consumer_cadence_guard(consumer);\r
72                 consumer->initialize(format_desc_, channel_index_);\r
73 \r
74                 executor_.invoke([&]\r
75                 {\r
76                         consumers_.insert(std::make_pair(index, consumer));\r
77                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
78                 }, task_priority::high_priority);\r
79         }\r
80 \r
81         void add(const safe_ptr<frame_consumer>& consumer)\r
82         {\r
83                 add(consumer->index(), consumer);\r
84         }\r
85 \r
86         void remove(int index)\r
87         {               \r
88                 // Destroy  consumer on calling thread:\r
89                 std::shared_ptr<frame_consumer> old_consumer;\r
90 \r
91                 executor_.invoke([&]\r
92                 {\r
93                         auto it = consumers_.find(index);\r
94                         if(it != consumers_.end())\r
95                         {\r
96                                 old_consumer = it->second;\r
97                                 consumers_.erase(it);\r
98                         }\r
99                 }, task_priority::high_priority);\r
100 \r
101                 if(old_consumer)\r
102                 {\r
103                         auto str = old_consumer->print();\r
104                         old_consumer.reset();\r
105                         CASPAR_LOG(info) << print() << L" " << str << L" Removed.";\r
106                 }\r
107         }\r
108 \r
109         void remove(const safe_ptr<frame_consumer>& consumer)\r
110         {\r
111                 remove(consumer->index());\r
112         }\r
113         \r
114         void set_video_format_desc(const video_format_desc& format_desc)\r
115         {\r
116                 executor_.invoke([&]\r
117                 {\r
118                         auto it = consumers_.begin();\r
119                         while(it != consumers_.end())\r
120                         {                                               \r
121                                 try\r
122                                 {\r
123                                         it->second->initialize(format_desc, channel_index_);\r
124                                         ++it;\r
125                                 }\r
126                                 catch(...)\r
127                                 {\r
128                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
129                                         CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
130                                         consumers_.erase(it++);\r
131                                 }\r
132                         }\r
133                         \r
134                         format_desc_ = format_desc;\r
135                         frames_.clear();\r
136                 });\r
137         }\r
138 \r
139         std::pair<int, int> minmax_buffer_depth() const\r
140         {               \r
141                 if(consumers_.empty())\r
142                         return std::make_pair(0, 0);\r
143                 \r
144                 auto buffer_depths = consumers_ | \r
145                                                          boost::adaptors::map_values | // std::function is MSVC workaround\r
146                                                          boost::adaptors::transformed(std::function<int(const safe_ptr<frame_consumer>&)>([](const safe_ptr<frame_consumer>& x){return x->buffer_depth();})); \r
147                 \r
148 \r
149                 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));\r
150         }\r
151 \r
152         bool has_synchronization_clock() const\r
153         {\r
154                 return boost::range::count_if(consumers_ | boost::adaptors::map_values, [](const safe_ptr<frame_consumer>& x){return x->has_synchronization_clock();}) > 0;\r
155         }\r
156                 \r
157         void operator()(safe_ptr<const data_frame> input_frame, const video_format_desc& format_desc)\r
158         {\r
159                 executor_.invoke([=]\r
160                 {\r
161                         try\r
162                         {                               \r
163                                 if(!has_synchronization_clock())\r
164                                         sync_timer_.tick(1.0/format_desc_.fps);\r
165                                 \r
166                                 if(format_desc_ != format_desc)\r
167                                         set_video_format_desc(format_desc);\r
168 \r
169                                 if(input_frame->image_data().size() != format_desc_.size)\r
170                                 {\r
171                                         sync_timer_.tick(1.0/format_desc_.fps);\r
172                                         return;\r
173                                 }\r
174                                         \r
175                                 auto minmax = minmax_buffer_depth();\r
176 \r
177                                 frames_.set_capacity(minmax.second - minmax.first + 1);\r
178                                 frames_.push_back(input_frame);\r
179 \r
180                                 if(!frames_.full())\r
181                                         return;\r
182 \r
183                                 auto it = consumers_.begin();\r
184                                 while(it != consumers_.end())\r
185                                 {\r
186                                         auto consumer   = it->second;\r
187                                         auto frame              = frames_.at(consumer->buffer_depth()-minmax.first);\r
188                                                 \r
189                                         try\r
190                                         {\r
191                                                 if(consumer->send(frame))\r
192                                                         ++it;\r
193                                                 else\r
194                                                 {\r
195                                                         CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
196                                                         consumers_.erase(it++);\r
197                                                 }\r
198                                         }\r
199                                         catch(...)\r
200                                         {\r
201                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
202                                                 try\r
203                                                 {\r
204                                                         consumer->initialize(format_desc_, channel_index_);\r
205                                                         if(consumer->send(frame))\r
206                                                                 ++it;\r
207                                                         else\r
208                                                         {\r
209                                                                 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
210                                                                 consumers_.erase(it++);\r
211                                                         }\r
212                                                 }\r
213                                                 catch(...)\r
214                                                 {\r
215                                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
216                                                         CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";\r
217                                                         consumers_.erase(it++);\r
218                                                 }\r
219                                         }\r
220                                 }\r
221                         }\r
222                         catch(...)\r
223                         {\r
224                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
225                         }\r
226                 });\r
227         }\r
228 \r
229         std::wstring print() const\r
230         {\r
231                 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";\r
232         }\r
233 \r
234         boost::unique_future<boost::property_tree::wptree> info()\r
235         {\r
236                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree\r
237                 {                       \r
238                         boost::property_tree::wptree info;\r
239                         BOOST_FOREACH(auto& consumer, consumers_)\r
240                         {\r
241                                 info.add_child(L"consumers.consumer", consumer.second->info())\r
242                                         .add(L"index", consumer.first); \r
243                         }\r
244                         return info;\r
245                 }, task_priority::high_priority));\r
246         }\r
247 };\r
248 \r
249 output::output(const video_format_desc& format_desc, int channel_index) : impl_(new impl(format_desc, channel_index)){}\r
250 void output::add(int index, const safe_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}\r
251 void output::add(const safe_ptr<frame_consumer>& consumer){impl_->add(consumer);}\r
252 void output::remove(int index){impl_->remove(index);}\r
253 void output::remove(const safe_ptr<frame_consumer>& consumer){impl_->remove(consumer);}\r
254 boost::unique_future<boost::property_tree::wptree> output::info() const{return impl_->info();}\r
255 void output::operator()(safe_ptr<const data_frame> frame, const video_format_desc& format_desc){(*impl_)(std::move(frame), format_desc);}\r
256 }}