]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
2.0.2: Intergrated channel-exp branch:
[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 \r
42 namespace caspar { namespace core {\r
43 \r
44 struct output::implementation\r
45 {       \r
46         typedef std::pair<safe_ptr<read_frame>, safe_ptr<read_frame>> fill_and_key;\r
47         \r
48         safe_ptr<diagnostics::graph>    graph_;\r
49         boost::timer                                    consume_timer_;\r
50 \r
51         video_format_desc                               format_desc_;\r
52 \r
53         std::map<int, safe_ptr<frame_consumer>> consumers_;\r
54         typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;\r
55         \r
56         high_prec_timer timer_;\r
57 \r
58         boost::circular_buffer<safe_ptr<read_frame>> frames_;\r
59 \r
60         tbb::concurrent_bounded_queue<std::shared_ptr<read_frame>> input_;\r
61 \r
62         executor executor_;\r
63                 \r
64 public:\r
65         implementation(const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc) \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                 executor_.invoke([&]\r
76                 {\r
77                         consumers_.erase(index);\r
78                 });\r
79 \r
80                 consumer->initialize(format_desc_);\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                 });\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                 });\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                         format_desc_ = format_desc;\r
108 \r
109                         auto it = consumers_.begin();\r
110                         while(it != consumers_.end())\r
111                         {                                               \r
112                                 try\r
113                                 {\r
114                                         it->second->initialize(format_desc_);\r
115                                         ++it;\r
116                                 }\r
117                                 catch(...)\r
118                                 {\r
119                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
120                                         consumers_.erase(it++);\r
121                                 }\r
122                         }\r
123 \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                 std::vector<size_t> buffer_depths;\r
133                 std::transform(consumers_.begin(), consumers_.end(), std::back_inserter(buffer_depths), [](const decltype(*consumers_.begin())& pair)\r
134                 {\r
135                         return pair.second->buffer_depth();\r
136                 });\r
137                 std::sort(buffer_depths.begin(), buffer_depths.end());\r
138                 auto min = buffer_depths.front();\r
139                 auto max = buffer_depths.back();\r
140                 return std::make_pair(min, max);\r
141         }\r
142 \r
143         void send(const std::pair<safe_ptr<read_frame>, ticket>& packet)\r
144         {\r
145                 executor_.begin_invoke([=]\r
146                 {\r
147                         try\r
148                         {\r
149                                 consume_timer_.restart();\r
150 \r
151                                 auto input_frame = packet.first;\r
152 \r
153                                 if(!has_synchronization_clock())\r
154                                         timer_.tick(1.0/format_desc_.fps);\r
155 \r
156                                 if(input_frame->image_size() != format_desc_.size)\r
157                                 {\r
158                                         timer_.tick(1.0/format_desc_.fps);\r
159                                         return;\r
160                                 }\r
161                                         \r
162                                 const auto minmax = minmax_buffer_depth();\r
163 \r
164                                 frames_.set_capacity(minmax.second - minmax.first + 1);\r
165                                 frames_.push_back(input_frame);\r
166 \r
167                                 if(!frames_.full())\r
168                                         return;\r
169 \r
170                                 auto it = consumers_.begin();\r
171                                 while(it != consumers_.end())\r
172                                 {\r
173                                         auto consumer   = it->second;\r
174                                         auto frame              = frames_.at(consumer->buffer_depth()-minmax.first);\r
175                                                 \r
176                                         try\r
177                                         {\r
178                                                 if(consumer->send(frame))\r
179                                                         ++it;\r
180                                                 else\r
181                                                         consumers_.erase(it++);\r
182                                         }\r
183                                         catch(...)\r
184                                         {\r
185                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
186                                                 try\r
187                                                 {\r
188                                                         consumer->initialize(format_desc_);\r
189                                                         if(consumer->send(frame))\r
190                                                                 ++it;\r
191                                                         else\r
192                                                                 consumers_.erase(it++);\r
193                                                 }\r
194                                                 catch(...)\r
195                                                 {\r
196                                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
197                                                         CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";\r
198                                                         consumers_.erase(it++);\r
199                                                 }\r
200                                         }\r
201                                 }\r
202                                                 \r
203                                 graph_->update_value("consume-time", consume_timer_.elapsed()*format_desc_.fps*0.5);\r
204                         }\r
205                         catch(...)\r
206                         {\r
207                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
208                         }\r
209                 });\r
210         }\r
211 \r
212 private:\r
213         \r
214         bool has_synchronization_clock()\r
215         {\r
216                 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
217                 {\r
218                         return p.second->has_synchronization_clock();\r
219                 });\r
220         }\r
221         \r
222         std::wstring print() const\r
223         {\r
224                 return L"output";\r
225         }\r
226 };\r
227 \r
228 output::output(const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc) : impl_(new implementation(graph, format_desc)){}\r
229 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}\r
230 void output::remove(int index){impl_->remove(index);}\r
231 void output::send(const std::pair<safe_ptr<read_frame>, ticket>& frame) {impl_->send(frame); }\r
232 void output::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
233 }}