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