]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
2.0.2: ffmpeg_consumer: Added support for DVCPRO and DNXHD. Added multithreading...
[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(int index, safe_ptr<frame_consumer>&& consumer)\r
73         {               \r
74                 executor_.invoke([&]\r
75                 {\r
76                         consumers_.erase(index);\r
77                 }, high_priority);\r
78 \r
79                 consumer = create_consumer_cadence_guard(std::move(consumer));\r
80                 consumer->initialize(format_desc_, channel_index_, index);\r
81 \r
82                 executor_.invoke([&]\r
83                 {\r
84                         consumers_.insert(std::make_pair(index, consumer));\r
85 \r
86                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
87                 }, high_priority);\r
88         }\r
89 \r
90         void remove(int index)\r
91         {\r
92                 executor_.invoke([&]\r
93                 {\r
94                         auto it = consumers_.find(index);\r
95                         if(it != consumers_.end())\r
96                         {\r
97                                 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
98                                 consumers_.erase(it);\r
99                         }\r
100                 }, high_priority);\r
101         }\r
102         \r
103         void set_video_format_desc(const video_format_desc& format_desc)\r
104         {\r
105                 executor_.invoke([&]\r
106                 {\r
107                         auto it = consumers_.begin();\r
108                         while(it != consumers_.end())\r
109                         {                                               \r
110                                 try\r
111                                 {\r
112                                         it->second->initialize(format_desc_, channel_index_, it->first);\r
113                                         ++it;\r
114                                 }\r
115                                 catch(...)\r
116                                 {\r
117                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
118                                         CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
119                                         consumers_.erase(it++);\r
120                                 }\r
121                         }\r
122                         \r
123                         format_desc_ = format_desc;\r
124                         frames_.clear();\r
125                 });\r
126         }\r
127 \r
128         std::pair<size_t, size_t> minmax_buffer_depth() const\r
129         {               \r
130                 if(consumers_.empty())\r
131                         return std::make_pair(0, 0);\r
132                 \r
133                 auto buffer_depths = consumers_ | \r
134                                                          boost::adaptors::map_values | // std::function is MSVC workaround\r
135                                                          boost::adaptors::transformed(std::function<int(const safe_ptr<frame_consumer>&)>([](const safe_ptr<frame_consumer>& x){return x->buffer_depth();})); \r
136                 \r
137 \r
138                 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));\r
139         }\r
140 \r
141         bool has_synchronization_clock() const\r
142         {\r
143                 return boost::range::count_if(consumers_ | boost::adaptors::map_values, [](const safe_ptr<frame_consumer>& x){return x->has_synchronization_clock();}) > 0;\r
144         }\r
145 \r
146         void send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& packet)\r
147         {\r
148                 executor_.begin_invoke([=]\r
149                 {\r
150                         try\r
151                         {\r
152                                 consume_timer_.restart();\r
153 \r
154                                 auto input_frame = packet.first;\r
155 \r
156                                 if(!has_synchronization_clock())\r
157                                         sync_timer_.tick(1.0/format_desc_.fps);\r
158 \r
159                                 if(input_frame->image_size() != format_desc_.size)\r
160                                 {\r
161                                         sync_timer_.tick(1.0/format_desc_.fps);\r
162                                         return;\r
163                                 }\r
164                                         \r
165                                 auto minmax = minmax_buffer_depth();\r
166 \r
167                                 frames_.set_capacity(minmax.second - minmax.first + 1);\r
168                                 frames_.push_back(input_frame);\r
169 \r
170                                 if(!frames_.full())\r
171                                         return;\r
172 \r
173                                 auto it = consumers_.begin();\r
174                                 while(it != consumers_.end())\r
175                                 {\r
176                                         auto consumer   = it->second;\r
177                                         auto frame              = frames_.at(consumer->buffer_depth()-minmax.first);\r
178                                                 \r
179                                         try\r
180                                         {\r
181                                                 if(consumer->send(frame))\r
182                                                         ++it;\r
183                                                 else\r
184                                                 {\r
185                                                         CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
186                                                         consumers_.erase(it++);\r
187                                                 }\r
188                                         }\r
189                                         catch(...)\r
190                                         {\r
191                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
192                                                 try\r
193                                                 {\r
194                                                         consumer->initialize(format_desc_, channel_index_, it->first);\r
195                                                         if(consumer->send(frame))\r
196                                                                 ++it;\r
197                                                         else\r
198                                                                 consumers_.erase(it++);\r
199                                                 }\r
200                                                 catch(...)\r
201                                                 {\r
202                                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
203                                                         CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";\r
204                                                         consumers_.erase(it++);\r
205                                                 }\r
206                                         }\r
207                                 }\r
208                                                 \r
209                                 graph_->update_value("consume-time", consume_timer_.elapsed()*format_desc_.fps*0.5);\r
210                         }\r
211                         catch(...)\r
212                         {\r
213                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
214                         }\r
215                 });\r
216         }\r
217 \r
218         std::wstring print() const\r
219         {\r
220                 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";\r
221         }\r
222 };\r
223 \r
224 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
225 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}\r
226 void output::remove(int index){impl_->remove(index);}\r
227 void output::send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& frame) {impl_->send(frame); }\r
228 void output::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
229 }}