]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
2.0.2: Merged ntsc-audio-exp branch for proper/native NTSC audio support.
[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         const int                                               channel_index_;\r
49         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         typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;\r
56         \r
57         high_prec_timer timer_;\r
58 \r
59         boost::circular_buffer<safe_ptr<read_frame>> frames_;\r
60 \r
61         tbb::concurrent_bounded_queue<std::shared_ptr<read_frame>> input_;\r
62 \r
63         executor executor_;\r
64                 \r
65 public:\r
66         implementation(const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc, int channel_index) \r
67                 : channel_index_(channel_index)\r
68                 , graph_(graph)\r
69                 , format_desc_(format_desc)\r
70                 , executor_(L"output")\r
71         {\r
72                 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f));\r
73         }       \r
74         \r
75         void add(int index, safe_ptr<frame_consumer>&& consumer)\r
76         {               \r
77                 executor_.invoke([&]\r
78                 {\r
79                         consumers_.erase(index);\r
80                 });\r
81 \r
82                 consumer = create_consumer_cadence_guard(std::move(consumer));\r
83                 consumer->initialize(format_desc_, channel_index_, index);\r
84 \r
85                 executor_.invoke([&]\r
86                 {\r
87                         consumers_.insert(std::make_pair(index, consumer));\r
88 \r
89                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
90                 });\r
91         }\r
92 \r
93         void remove(int index)\r
94         {\r
95                 executor_.invoke([&]\r
96                 {\r
97                         auto it = consumers_.find(index);\r
98                         if(it != consumers_.end())\r
99                         {\r
100                                 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
101                                 consumers_.erase(it);\r
102                         }\r
103                 });\r
104         }\r
105         \r
106         void set_video_format_desc(const video_format_desc& format_desc)\r
107         {\r
108                 executor_.invoke([&]\r
109                 {\r
110                         format_desc_ = format_desc;\r
111 \r
112                         auto it = consumers_.begin();\r
113                         while(it != consumers_.end())\r
114                         {                                               \r
115                                 try\r
116                                 {\r
117                                         it->second->initialize(format_desc_, channel_index_, it->first);\r
118                                         ++it;\r
119                                 }\r
120                                 catch(...)\r
121                                 {\r
122                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
123                                         consumers_.erase(it++);\r
124                                 }\r
125                         }\r
126 \r
127                         frames_.clear();\r
128                 });\r
129         }\r
130                         \r
131         std::pair<size_t, size_t> minmax_buffer_depth() const\r
132         {               \r
133                 if(consumers_.empty())\r
134                         return std::make_pair(0, 0);\r
135                 std::vector<size_t> buffer_depths;\r
136                 std::transform(consumers_.begin(), consumers_.end(), std::back_inserter(buffer_depths), [](const decltype(*consumers_.begin())& pair)\r
137                 {\r
138                         return pair.second->buffer_depth();\r
139                 });\r
140                 std::sort(buffer_depths.begin(), buffer_depths.end());\r
141                 auto min = buffer_depths.front();\r
142                 auto max = buffer_depths.back();\r
143                 return std::make_pair(min, max);\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                                         timer_.tick(1.0/format_desc_.fps);\r
158 \r
159                                 if(input_frame->image_size() != format_desc_.size)\r
160                                 {\r
161                                         timer_.tick(1.0/format_desc_.fps);\r
162                                         return;\r
163                                 }\r
164                                         \r
165                                 const 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                                                         consumers_.erase(it++);\r
185                                         }\r
186                                         catch(...)\r
187                                         {\r
188                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
189                                                 try\r
190                                                 {\r
191                                                         consumer->initialize(format_desc_, channel_index_, it->first);\r
192                                                         if(consumer->send(frame))\r
193                                                                 ++it;\r
194                                                         else\r
195                                                                 consumers_.erase(it++);\r
196                                                 }\r
197                                                 catch(...)\r
198                                                 {\r
199                                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
200                                                         CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";\r
201                                                         consumers_.erase(it++);\r
202                                                 }\r
203                                         }\r
204                                 }\r
205                                                 \r
206                                 graph_->update_value("consume-time", consume_timer_.elapsed()*format_desc_.fps*0.5);\r
207                         }\r
208                         catch(...)\r
209                         {\r
210                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
211                         }\r
212                 });\r
213         }\r
214 \r
215 private:\r
216         \r
217         bool has_synchronization_clock()\r
218         {\r
219                 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
220                 {\r
221                         return p.second->has_synchronization_clock();\r
222                 });\r
223         }\r
224         \r
225         std::wstring print() const\r
226         {\r
227                 return L"output";\r
228         }\r
229 };\r
230 \r
231 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
232 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}\r
233 void output::remove(int index){impl_->remove(index);}\r
234 void output::send(const std::pair<safe_ptr<read_frame>, std::shared_ptr<void>>& frame) {impl_->send(frame); }\r
235 void output::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
236 }}