]> 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) 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/utility/assert.h>\r
34 #include <common/utility/timer.h>\r
35 \r
36 #include <concurrent_vector.h>\r
37 #include <concrt_extras.h>\r
38 \r
39 using namespace Concurrency;\r
40 \r
41 namespace caspar { namespace core {\r
42         \r
43 struct output::implementation\r
44 {       \r
45         typedef std::pair<safe_ptr<read_frame>, safe_ptr<read_frame>> fill_and_key;\r
46         \r
47         const video_format_desc format_desc_;\r
48 \r
49         std::map<int, safe_ptr<frame_consumer>> consumers_;\r
50         typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;\r
51         \r
52         high_prec_timer timer_;\r
53 \r
54         critical_section                                mutex_;\r
55         call<output::source_element_t>  output_;\r
56 \r
57         boost::circular_buffer<safe_ptr<read_frame>> frames_;\r
58                 \r
59 public:\r
60         implementation(output::source_t& source, const video_format_desc& format_desc) \r
61                 : format_desc_(format_desc)\r
62                 , output_(std::bind(&implementation::execute, this, std::placeholders::_1))\r
63         {\r
64                 source.link_target(&output_);\r
65         }       \r
66         \r
67         void add(int index, safe_ptr<frame_consumer>&& consumer)\r
68         {               \r
69                 // C-TODO: Consumers are destroyed asynchronously, need to wait for destruction to finish before callign initialize.\r
70                 remove(index);\r
71 \r
72                 consumer->initialize(format_desc_);             \r
73                 \r
74                 {\r
75                         critical_section::scoped_lock lock(mutex_);\r
76 \r
77                         consumers_.insert(std::make_pair(index, consumer));\r
78 \r
79                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added."; \r
80                 }\r
81         }\r
82 \r
83         void remove(int index)\r
84         {\r
85                 {\r
86                         critical_section::scoped_lock lock(mutex_);\r
87 \r
88                         auto it = consumers_.find(index);\r
89                         if(it != consumers_.end())\r
90                         {\r
91                                 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
92                                 consumers_.erase(it);\r
93                         }\r
94                 }\r
95         }\r
96 \r
97         std::pair<size_t, size_t> minmax_buffer_depth() const\r
98         {               \r
99                 if(consumers_.empty())\r
100                         return std::make_pair(0, 0);\r
101                 std::vector<size_t> buffer_depths;\r
102                 std::transform(consumers_.begin(), consumers_.end(), std::back_inserter(buffer_depths), [](const decltype(*consumers_.begin())& pair)\r
103                 {\r
104                         return pair.second->buffer_depth();\r
105                 });\r
106                 std::sort(buffer_depths.begin(), buffer_depths.end());\r
107                 auto min = buffer_depths.front();\r
108                 auto max = buffer_depths.back();\r
109                 return std::make_pair(min, max);\r
110         }\r
111                                                 \r
112         void execute(const output::source_element_t& element)\r
113         {       \r
114                 const auto frame = element.first;\r
115 \r
116                 if(frame->image_data().size() != format_desc_.size)\r
117                         return;\r
118 \r
119                 {\r
120                         critical_section::scoped_lock lock(mutex_);             \r
121                         \r
122                         if(!has_synchronization_clock() || frame->image_size() != format_desc_.size)\r
123                         {               \r
124                                 scoped_oversubcription_token oversubscribe;\r
125                                 timer_.tick(1.0/format_desc_.fps);\r
126                         }\r
127                                 \r
128                         const auto minmax = minmax_buffer_depth();\r
129 \r
130                         frames_.set_capacity(minmax.second - minmax.first + 1);\r
131                         frames_.push_back(frame);\r
132 \r
133                         if(!frames_.full())\r
134                                 return;\r
135 \r
136                         Concurrency::concurrent_vector<int> removables;         \r
137                         Concurrency::parallel_for_each(consumers_.begin(), consumers_.end(), [&](const decltype(*consumers_.begin())& pair)\r
138                         {               \r
139                                 try\r
140                                 {\r
141                                         auto consumer = pair.second;\r
142 \r
143                                         if(!consumer->send(frames_.at(consumer->buffer_depth()-minmax.first)))\r
144                                                 removables.push_back(pair.first);\r
145                                 }\r
146                                 catch(...)\r
147                                 {               \r
148                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
149                                         CASPAR_LOG(error) << "Consumer error. Trying to recover:" << pair.second->print();\r
150                                         try\r
151                                         {\r
152                                                 pair.second->initialize(format_desc_);\r
153                                                 pair.second->send(frame);\r
154                                         }\r
155                                         catch(...)\r
156                                         {\r
157                                                 removables.push_back(pair.first);                               \r
158                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
159                                                 CASPAR_LOG(error) << "Failed to recover consumer: " << pair.second->print() << L". Removing it.";\r
160                                         }\r
161                                 }\r
162                         });\r
163 \r
164                         BOOST_FOREACH(auto& removable, removables)\r
165                                 consumers_.erase(removable);            \r
166                 }\r
167         }\r
168 \r
169 private:\r
170         \r
171         bool has_synchronization_clock()\r
172         {\r
173                 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
174                 {\r
175                         return p.second->has_synchronization_clock();\r
176                 });\r
177         }\r
178         \r
179         std::wstring print() const\r
180         {\r
181                 return L"output";\r
182         }\r
183 };\r
184 \r
185 output::output(output::source_t& source, const video_format_desc& format_desc) : impl_(new implementation(source, format_desc)){}\r
186 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}\r
187 void output::remove(int index){impl_->remove(index);}\r
188 }}