]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
2.0.2: output: Destroy consumers on calling thread instead of pipeline thread.
[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 \r
44 namespace caspar { namespace core {\r
45         \r
46 struct output::implementation\r
47 {               \r
48         const int                                                                               channel_index_;\r
49         const safe_ptr<diagnostics::graph>                              graph_;\r
50         boost::timer                                                                    consume_timer_;\r
51 \r
52         video_format_desc                                                               format_desc_;\r
53 \r
54         std::map<int, safe_ptr<frame_consumer>>                 consumers_;\r
55         \r
56         high_prec_timer                                                                 sync_timer_;\r
57 \r
58         boost::circular_buffer<safe_ptr<read_frame>>    frames_;\r
59 \r
60         executor                                                                                executor_;\r
61                 \r
62 public:\r
63         implementation(const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc, int channel_index) \r
64                 : channel_index_(channel_index)\r
65                 , graph_(graph)\r
66                 , format_desc_(format_desc)\r
67                 , executor_(L"output")\r
68         {\r
69                 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f));\r
70         }       \r
71         \r
72         void add(safe_ptr<frame_consumer>&& consumer)\r
73         {               \r
74                 remove(consumer->index());\r
75 \r
76                 consumer = create_consumer_cadence_guard(std::move(consumer));\r
77                 consumer->initialize(format_desc_, channel_index_);\r
78 \r
79                 executor_.invoke([&]\r
80                 {\r
81                         consumers_.insert(std::make_pair(consumer->index(), consumer));\r
82                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
83                 }, high_priority);\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                 }, 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 set_video_format_desc(const video_format_desc& format_desc)\r
110         {\r
111                 executor_.invoke([&]\r
112                 {\r
113                         auto it = consumers_.begin();\r
114                         while(it != consumers_.end())\r
115                         {                                               \r
116                                 try\r
117                                 {\r
118                                         it->second->initialize(format_desc_, channel_index_);\r
119                                         ++it;\r
120                                 }\r
121                                 catch(...)\r
122                                 {\r
123                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
124                                         CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
125                                         consumers_.erase(it++);\r
126                                 }\r
127                         }\r
128                         \r
129                         format_desc_ = format_desc;\r
130                         frames_.clear();\r
131                 });\r
132         }\r
133 \r
134         std::pair<size_t, size_t> minmax_buffer_depth() const\r
135         {               \r
136                 if(consumers_.empty())\r
137                         return std::make_pair(0, 0);\r
138                 \r
139                 auto buffer_depths = consumers_ | \r
140                                                          boost::adaptors::map_values | // std::function is MSVC workaround\r
141                                                          boost::adaptors::transformed(std::function<int(const safe_ptr<frame_consumer>&)>([](const safe_ptr<frame_consumer>& x){return x->buffer_depth();})); \r
142                 \r
143 \r
144                 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));\r
145         }\r
146 \r
147         bool has_synchronization_clock() const\r
148         {\r
149                 return boost::range::count_if(consumers_ | boost::adaptors::map_values, [](const safe_ptr<frame_consumer>& x){return x->has_synchronization_clock();}) > 0;\r
150         }\r
151 \r
152         void send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& packet)\r
153         {\r
154                 executor_.begin_invoke([=]\r
155                 {\r
156                         try\r
157                         {\r
158                                 consume_timer_.restart();\r
159 \r
160                                 auto input_frame = packet.first;\r
161 \r
162                                 if(!has_synchronization_clock())\r
163                                         sync_timer_.tick(1.0/format_desc_.fps);\r
164 \r
165                                 if(input_frame->image_size() != format_desc_.size)\r
166                                 {\r
167                                         sync_timer_.tick(1.0/format_desc_.fps);\r
168                                         return;\r
169                                 }\r
170                                         \r
171                                 auto minmax = minmax_buffer_depth();\r
172 \r
173                                 frames_.set_capacity(minmax.second - minmax.first + 1);\r
174                                 frames_.push_back(input_frame);\r
175 \r
176                                 if(!frames_.full())\r
177                                         return;\r
178 \r
179                                 auto it = consumers_.begin();\r
180                                 while(it != consumers_.end())\r
181                                 {\r
182                                         auto consumer   = it->second;\r
183                                         auto frame              = frames_.at(consumer->buffer_depth()-minmax.first);\r
184                                                 \r
185                                         try\r
186                                         {\r
187                                                 if(consumer->send(frame))\r
188                                                         ++it;\r
189                                                 else\r
190                                                 {\r
191                                                         CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
192                                                         consumers_.erase(it++);\r
193                                                 }\r
194                                         }\r
195                                         catch(...)\r
196                                         {\r
197                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
198                                                 try\r
199                                                 {\r
200                                                         consumer->initialize(format_desc_, channel_index_);\r
201                                                         if(consumer->send(frame))\r
202                                                                 ++it;\r
203                                                         else\r
204                                                                 consumers_.erase(it++);\r
205                                                 }\r
206                                                 catch(...)\r
207                                                 {\r
208                                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
209                                                         CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";\r
210                                                         consumers_.erase(it++);\r
211                                                 }\r
212                                         }\r
213                                 }\r
214                                                 \r
215                                 graph_->update_value("consume-time", consume_timer_.elapsed()*format_desc_.fps*0.5);\r
216                         }\r
217                         catch(...)\r
218                         {\r
219                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
220                         }\r
221                 });\r
222         }\r
223 \r
224         std::wstring print() const\r
225         {\r
226                 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";\r
227         }\r
228 };\r
229 \r
230 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
231 void output::add(safe_ptr<frame_consumer>&& consumer){impl_->add(std::move(consumer));}\r
232 void output::remove(int index){impl_->remove(index);}\r
233 void output::send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& frame) {impl_->send(frame); }\r
234 void output::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
235 }}