]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
2.0. stage: Changed error handling.
[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_channel_context.h"\r
30 \r
31 #include "../video_format.h"\r
32 #include "../mixer/gpu/ogl_device.h"\r
33 #include "../mixer/read_frame.h"\r
34 \r
35 #include <common/concurrency/executor.h>\r
36 #include <common/utility/assert.h>\r
37 #include <common/utility/timer.h>\r
38 #include <common/memory/memshfl.h>\r
39 \r
40 #include <tbb/mutex.h>\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         video_channel_context&          channel_;\r
49         const std::function<void()> restart_channel_;\r
50 \r
51         std::map<int, safe_ptr<frame_consumer>> consumers_;\r
52         typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;\r
53         \r
54         high_prec_timer timer_;\r
55                 \r
56 public:\r
57         implementation(video_channel_context& video_channel, const std::function<void()>& restart_channel) \r
58                 : channel_(video_channel)\r
59                 , restart_channel_(restart_channel)\r
60         {\r
61         }       \r
62         \r
63         void add(int index, safe_ptr<frame_consumer>&& consumer)\r
64         {               \r
65                 channel_.execution().invoke([&]\r
66                 {\r
67                         consumers_.erase(index);\r
68                 });\r
69 \r
70                 consumer->initialize(channel_.get_format_desc());\r
71 \r
72                 channel_.execution().invoke([&]\r
73                 {\r
74                         consumers_.insert(std::make_pair(index, consumer));\r
75 \r
76                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
77                 });\r
78         }\r
79 \r
80         void remove(int index)\r
81         {\r
82                 channel_.execution().invoke([&]\r
83                 {\r
84                         auto it = consumers_.find(index);\r
85                         if(it != consumers_.end())\r
86                         {\r
87                                 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
88                                 consumers_.erase(it);\r
89                         }\r
90                 });\r
91         }\r
92                                                 \r
93         void execute(const safe_ptr<read_frame>& frame)\r
94         {                       \r
95                 if(!has_synchronization_clock())\r
96                         timer_.tick(1.0/channel_.get_format_desc().fps);\r
97 \r
98                 if(frame->image_size() != channel_.get_format_desc().size)\r
99                 {\r
100                         timer_.tick(1.0/channel_.get_format_desc().fps);\r
101                         return;\r
102                 }\r
103                 \r
104                 auto it = consumers_.begin();\r
105                 while(it != consumers_.end())\r
106                 {\r
107                         auto consumer = it->second;\r
108 \r
109                         if(consumer->get_video_format_desc() != channel_.get_format_desc())\r
110                                 consumer->initialize(channel_.get_format_desc());\r
111 \r
112                         try\r
113                         {\r
114                                 if(consumer->send(frame))\r
115                                         ++it;\r
116                                 else\r
117                                         consumers_.erase(it++);\r
118                         }\r
119                         catch(...)\r
120                         {\r
121                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
122                                 CASPAR_LOG(warning) << "Trying to restart consumer: " << consumer->print() << L".";\r
123                                 try\r
124                                 {\r
125                                         consumer->initialize(channel_.get_format_desc());\r
126                                         consumer->send(frame);\r
127                                 }\r
128                                 catch(...)\r
129                                 {       \r
130                                         CASPAR_LOG_CURRENT_EXCEPTION(); \r
131                                         CASPAR_LOG(warning) << "Consumer restart failed, trying to restart channel: " << consumer->print() << L".";     \r
132 \r
133                                         try\r
134                                         {\r
135                                                 restart_channel_();\r
136                                                 consumer->initialize(channel_.get_format_desc());\r
137                                                 consumer->send(frame);\r
138                                         }\r
139                                         catch(...)\r
140                                         {\r
141                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
142                                                 CASPAR_LOG(error) << "Failed to recover consumer: " << consumer->print() << L". Removing it.";\r
143                                                 consumers_.erase(it++);\r
144                                         }\r
145                                 }\r
146                         }\r
147                 }\r
148         }\r
149 \r
150 private:\r
151         \r
152         bool has_synchronization_clock()\r
153         {\r
154                 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
155                 {\r
156                         return p.second->has_synchronization_clock();\r
157                 });\r
158         }\r
159         \r
160         std::wstring print() const\r
161         {\r
162                 return L"output";\r
163         }\r
164 };\r
165 \r
166 output::output(video_channel_context& video_channel, const std::function<void()>& restart_channel) : impl_(new implementation(video_channel, restart_channel)){}\r
167 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}\r
168 void output::remove(int index){impl_->remove(index);}\r
169 void output::execute(const safe_ptr<read_frame>& frame) {impl_->execute(frame); }\r
170 }}