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