]> 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 <concrt_extras.h>\r
39 \r
40 using namespace Concurrency;\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 video_format_desc format_desc_;\r
49 \r
50         std::map<int, safe_ptr<frame_consumer>> consumers_;\r
51         typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;\r
52         \r
53         high_prec_timer timer_;\r
54 \r
55         critical_section                                                                mutex_;\r
56         call<safe_ptr<message<safe_ptr<read_frame>>>>   output_;\r
57                 \r
58 public:\r
59         implementation(output::source_t& source, const video_format_desc& format_desc) \r
60                 : format_desc_(format_desc)\r
61                 , output_(std::bind(&implementation::execute, this, std::placeholders::_1))\r
62         {\r
63                 source.link_target(&output_);\r
64         }       \r
65         \r
66         void add(int index, safe_ptr<frame_consumer>&& consumer)\r
67         {               \r
68                 remove(index);\r
69 \r
70                 consumer->initialize(format_desc_);             \r
71                 \r
72                 {\r
73                         critical_section::scoped_lock lock(mutex_);\r
74 \r
75                         consumers_.insert(std::make_pair(index, consumer));\r
76 \r
77                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added."; \r
78                 }\r
79         }\r
80 \r
81         void remove(int index)\r
82         {\r
83                 {\r
84                         critical_section::scoped_lock lock(mutex_);\r
85 \r
86                         auto it = consumers_.find(index);\r
87                         if(it != consumers_.end())\r
88                         {\r
89                                 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
90                                 consumers_.erase(it);\r
91                         }\r
92                 }\r
93         }\r
94                                                 \r
95         void execute(const safe_ptr<message<safe_ptr<read_frame>>>& msg)\r
96         {       \r
97                 auto frame = msg->value();\r
98 \r
99                 {\r
100                         critical_section::scoped_lock lock(mutex_);             \r
101 \r
102                         if(!has_synchronization_clock() || frame->image_size() != format_desc_.size)\r
103                         {               \r
104                                 scoped_oversubcription_token oversubscribe;\r
105                                 timer_.tick(1.0/format_desc_.fps);\r
106                         }\r
107         \r
108                         std::vector<int> removables;            \r
109                         Concurrency::parallel_for_each(consumers_.begin(), consumers_.end(), [&](const decltype(*consumers_.begin())& pair)\r
110                         {               \r
111                                 try\r
112                                 {\r
113                                         if(!pair.second->send(frame))\r
114                                                 removables.push_back(pair.first);\r
115                                 }\r
116                                 catch(...)\r
117                                 {               \r
118                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
119                                         CASPAR_LOG(error) << "Consumer error. Trying to recover:" << pair.second->print();\r
120                                         try\r
121                                         {\r
122                                                 pair.second->initialize(format_desc_);\r
123                                                 pair.second->send(frame);\r
124                                         }\r
125                                         catch(...)\r
126                                         {\r
127                                                 removables.push_back(pair.first);                               \r
128                                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
129                                                 CASPAR_LOG(error) << "Failed to recover consumer: " << pair.second->print() << L". Removing it.";\r
130                                         }\r
131                                 }\r
132                         });\r
133 \r
134                         BOOST_FOREACH(auto& removable, removables)\r
135                                 consumers_.erase(removable);            \r
136                 }\r
137         }\r
138 \r
139 private:\r
140         \r
141         bool has_synchronization_clock()\r
142         {\r
143                 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
144                 {\r
145                         return p.second->has_synchronization_clock();\r
146                 });\r
147         }\r
148         \r
149         std::wstring print() const\r
150         {\r
151                 return L"output";\r
152         }\r
153 };\r
154 \r
155 output::output(output::source_t& source, const video_format_desc& format_desc) : impl_(new implementation(source, format_desc)){}\r
156 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}\r
157 void output::remove(int index){impl_->remove(index);}\r
158 }}