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