]> 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/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 \r
38 #include <tbb/mutex.h>\r
39 \r
40 #include <concrt_extras.h>\r
41 \r
42 using namespace Concurrency;\r
43 \r
44 namespace caspar { namespace core {\r
45         \r
46 struct destruction_context\r
47 {\r
48         std::shared_ptr<frame_consumer> consumer;\r
49         Concurrency::event                              event;\r
50 \r
51         destruction_context(std::shared_ptr<frame_consumer>&& consumer) \r
52                 : consumer(consumer)\r
53         {\r
54         }\r
55 };\r
56 \r
57 void __cdecl destroy_consumer(LPVOID lpParam)\r
58 {\r
59         auto destruction = std::unique_ptr<destruction_context>(static_cast<destruction_context*>(lpParam));\r
60         \r
61         try\r
62         {               \r
63                 if(destruction->consumer.unique())\r
64                 {\r
65                         Concurrency::scoped_oversubcription_token oversubscribe;\r
66                         destruction->consumer.reset();\r
67                 }\r
68                 else\r
69                         CASPAR_LOG(warning) << destruction->consumer->print() << " Not destroyed asynchronously.";              \r
70         }\r
71         catch(...)\r
72         {\r
73                 CASPAR_LOG_CURRENT_EXCEPTION();\r
74         }\r
75         \r
76         destruction->event.set();\r
77 }\r
78 \r
79 void __cdecl destroy_and_wait_consumer(LPVOID lpParam)\r
80 {\r
81         try\r
82         {\r
83                 auto destruction = static_cast<destruction_context*>(lpParam);\r
84                 Concurrency::CurrentScheduler::ScheduleTask(destroy_consumer, lpParam);\r
85                 if(destruction->event.wait(1000) == Concurrency::COOPERATIVE_WAIT_TIMEOUT)\r
86                         CASPAR_LOG(warning) << " Potential destruction deadlock detected. Might leak resources.";\r
87         }\r
88         catch(...)\r
89         {\r
90                 CASPAR_LOG_CURRENT_EXCEPTION();\r
91         }\r
92 }\r
93 \r
94 struct output::implementation\r
95 {       \r
96         typedef std::pair<safe_ptr<read_frame>, safe_ptr<read_frame>> fill_and_key;\r
97         \r
98         const video_format_desc format_desc_;\r
99 \r
100         std::map<int, safe_ptr<frame_consumer>> consumers_;\r
101         typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;\r
102         \r
103         high_prec_timer timer_;\r
104 \r
105         critical_section                                                                mutex_;\r
106         std::shared_ptr<Scheduler>                                              scheduler_;\r
107         call<safe_ptr<message<safe_ptr<read_frame>>>>   output_;\r
108                 \r
109 public:\r
110         implementation(output::source_t& source, const video_format_desc& format_desc) \r
111                 : format_desc_(format_desc)\r
112                 //, scheduler_(Scheduler::Create(SchedulerPolicy(1, ContextPriority, THREAD_PRIORITY_ABOVE_NORMAL)), [](Scheduler* p){p->Release();})\r
113                 , output_(std::bind(&implementation::execute, this, std::placeholders::_1))\r
114         {\r
115                 source.link_target(&output_);\r
116         }       \r
117         \r
118         void add(int index, safe_ptr<frame_consumer>&& consumer)\r
119         {               \r
120                 {\r
121                         critical_section::scoped_lock lock(mutex_);\r
122                         consumers_.erase(index);\r
123                 }\r
124 \r
125                 consumer->initialize(format_desc_);\r
126                 \r
127                 {\r
128                         critical_section::scoped_lock lock(mutex_);\r
129                         consumers_.insert(std::make_pair(index, consumer));\r
130 \r
131                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
132                 }\r
133         }\r
134 \r
135         void remove(int index)\r
136         {\r
137                 critical_section::scoped_lock lock(mutex_);\r
138                 auto it = consumers_.find(index);\r
139                 if(it != consumers_.end())\r
140                 {\r
141                         CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
142                         consumers_.erase(it);\r
143                 }\r
144         }\r
145                                                 \r
146         void execute(const safe_ptr<message<safe_ptr<read_frame>>>& msg)\r
147         {       \r
148                 auto frame = msg->value();\r
149 \r
150                 critical_section::scoped_lock lock(mutex_);             \r
151 \r
152                 if(!has_synchronization_clock() || frame->image_size() != format_desc_.size)\r
153                 {               \r
154                         scoped_oversubcription_token oversubscribe;\r
155                         timer_.tick(1.0/format_desc_.fps);\r
156                 }\r
157                 \r
158                 std::vector<int> removables;            \r
159                 Concurrency::parallel_for_each(consumers_.begin(), consumers_.end(), [&](const decltype(*consumers_.begin())& pair)\r
160                 {               \r
161                         try\r
162                         {\r
163                                 if(!pair.second->send(frame))\r
164                                         removables.push_back(pair.first);\r
165                         }\r
166                         catch(...)\r
167                         {               \r
168                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
169                                 CASPAR_LOG(error) << "Consumer error. Trying to recover:" << pair.second->print();\r
170                                 try\r
171                                 {\r
172                                         pair.second->initialize(format_desc_);\r
173                                         pair.second->send(frame);\r
174                                 }\r
175                                 catch(...)\r
176                                 {\r
177                                         removables.push_back(pair.first);                               \r
178                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
179                                         CASPAR_LOG(error) << "Failed to recover consumer: " << pair.second->print() << L". Removing it.";\r
180                                 }\r
181                         }\r
182                 });\r
183 \r
184                 BOOST_FOREACH(auto& removable, removables)\r
185                 {\r
186                         std::shared_ptr<frame_consumer> consumer = consumers_.find(removable)->second;\r
187                         consumers_.erase(removable);                    \r
188                         Concurrency::CurrentScheduler::ScheduleTask(destroy_consumer, new destruction_context(std::move(consumer)));\r
189                 }\r
190         }\r
191 \r
192 private:\r
193         \r
194         bool has_synchronization_clock()\r
195         {\r
196                 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
197                 {\r
198                         return p.second->has_synchronization_clock();\r
199                 });\r
200         }\r
201         \r
202         std::wstring print() const\r
203         {\r
204                 return L"output";\r
205         }\r
206 };\r
207 \r
208 output::output(output::source_t& source, const video_format_desc& format_desc) : impl_(new implementation(source, format_desc)){}\r
209 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}\r
210 void output::remove(int index){impl_->remove(index);}\r
211 }}