]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
2.1.0: Refactored frame_consumer exception handling and recovery,
[casparcg] / core / consumer / output.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../StdAfx.h"\r
23 \r
24 #ifdef _MSC_VER\r
25 #pragma warning (disable : 4244)\r
26 #endif\r
27 \r
28 #include "output.h"\r
29 \r
30 #include "frame_consumer.h"\r
31 \r
32 #include "../video_format.h"\r
33 #include "../frame/data_frame.h"\r
34 \r
35 #include <common/concurrency/async.h>\r
36 #include <common/concurrency/executor.h>\r
37 #include <common/diagnostics/graph.h>\r
38 #include <common/prec_timer.h>\r
39 #include <common/memory/memshfl.h>\r
40 #include <common/env.h>\r
41 \r
42 #include <common/assert.h>\r
43 #include <boost/circular_buffer.hpp>\r
44 #include <boost/timer.hpp>\r
45 #include <boost/range/algorithm.hpp>\r
46 #include <boost/range/adaptors.hpp>\r
47 #include <boost/property_tree/ptree.hpp>\r
48 \r
49 namespace caspar { namespace core {\r
50         \r
51 struct output::impl\r
52 {               \r
53         const int                                                                                                       channel_index_;\r
54         video_format_desc                                                                                       format_desc_;\r
55         std::map<int, spl::shared_ptr<frame_consumer>>                          consumers_;     \r
56         prec_timer                                                                                                      sync_timer_;\r
57         boost::circular_buffer<spl::shared_ptr<const data_frame>>       frames_;\r
58         executor                                                                                                        executor_;              \r
59 public:\r
60         impl(const video_format_desc& format_desc, int channel_index) \r
61                 : channel_index_(channel_index)\r
62                 , format_desc_(format_desc)\r
63                 , executor_(L"output")\r
64         {\r
65         }       \r
66         \r
67         void add(int index, spl::shared_ptr<frame_consumer> consumer)\r
68         {               \r
69                 remove(index);\r
70 \r
71                 consumer->initialize(format_desc_, channel_index_);\r
72 \r
73                 executor_.begin_invoke([=]\r
74                 {\r
75                         consumers_.insert(std::make_pair(index, consumer));\r
76                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
77                 }, task_priority::high_priority);\r
78         }\r
79 \r
80         void add(const spl::shared_ptr<frame_consumer>& consumer)\r
81         {\r
82                 add(consumer->index(), consumer);\r
83         }\r
84 \r
85         void remove(int index)\r
86         {               \r
87                 executor_.begin_invoke([=]\r
88                 {\r
89                         auto it = consumers_.find(index);\r
90                         if(it != consumers_.end())\r
91                                 consumers_.erase(it);                                   \r
92                 }, task_priority::high_priority);\r
93         }\r
94 \r
95         void remove(const spl::shared_ptr<frame_consumer>& consumer)\r
96         {\r
97                 remove(consumer->index());\r
98         }\r
99         \r
100         void set_video_format_desc(const video_format_desc& format_desc)\r
101         {\r
102                 executor_.invoke([&]\r
103                 {\r
104                         auto it = consumers_.begin();\r
105                         while(it != consumers_.end())\r
106                         {                                               \r
107                                 try\r
108                                 {\r
109                                         it->second->initialize(format_desc, channel_index_);\r
110                                         ++it;\r
111                                 }\r
112                                 catch(...)\r
113                                 {\r
114                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
115                                         CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
116                                         consumers_.erase(it++);\r
117                                 }\r
118                         }\r
119                         \r
120                         format_desc_ = format_desc;\r
121                         frames_.clear();\r
122                 });\r
123         }\r
124 \r
125         std::pair<int, int> minmax_buffer_depth() const\r
126         {               \r
127                 if(consumers_.empty())\r
128                         return std::make_pair(0, 0);\r
129                 \r
130                 auto buffer_depths = consumers_ | \r
131                                                          boost::adaptors::map_values | // std::function is MSVC workaround\r
132                                                          boost::adaptors::transformed(std::function<int(const spl::shared_ptr<frame_consumer>&)>([](const spl::shared_ptr<frame_consumer>& x){return x->buffer_depth();})); \r
133                 \r
134 \r
135                 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));\r
136         }\r
137 \r
138         bool has_synchronization_clock() const\r
139         {\r
140                 return boost::range::count_if(consumers_ | boost::adaptors::map_values, [](const spl::shared_ptr<frame_consumer>& x){return x->has_synchronization_clock();}) > 0;\r
141         }\r
142                 \r
143         void operator()(spl::shared_ptr<const data_frame> input_frame, const video_format_desc& format_desc)\r
144         {\r
145                 executor_.invoke([=]\r
146                 {                       \r
147                         if(!has_synchronization_clock())\r
148                                 sync_timer_.tick(1.0/format_desc_.fps);\r
149                                 \r
150                         if(format_desc_ != format_desc)\r
151                                 set_video_format_desc(format_desc);\r
152 \r
153                         if(input_frame->image_data().size() != format_desc_.size)\r
154                         {\r
155                                 sync_timer_.tick(1.0/format_desc_.fps);\r
156                                 return;\r
157                         }\r
158                                         \r
159                         auto minmax = minmax_buffer_depth();\r
160 \r
161                         frames_.set_capacity(std::max(1, minmax.second - minmax.first) + 1); // std::max(1, x) since we want to guarantee some pipeline depth for asycnhronous mixer read-back.\r
162                         frames_.push_back(input_frame);\r
163 \r
164                         if(!frames_.full())\r
165                                 return;\r
166 \r
167                         for(auto it = consumers_.begin(); it != consumers_.end();)\r
168                         {\r
169                                 auto consumer   = it->second;\r
170                                 auto frame              = frames_.at(consumer->buffer_depth()-minmax.first);\r
171                                                 \r
172                                 if(consumer->send(frame))\r
173                                         ++it;\r
174                                 else\r
175                                 {\r
176                                         CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
177                                         consumers_.erase(it++);\r
178                                 }\r
179                         }\r
180                 });\r
181         }\r
182 \r
183         std::wstring print() const\r
184         {\r
185                 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";\r
186         }\r
187 \r
188         boost::unique_future<boost::property_tree::wptree> info()\r
189         {\r
190                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree\r
191                 {                       \r
192                         boost::property_tree::wptree info;\r
193                         BOOST_FOREACH(auto& consumer, consumers_)\r
194                         {\r
195                                 info.add_child(L"consumers.consumer", consumer.second->info())\r
196                                         .add(L"index", consumer.first); \r
197                         }\r
198                         return info;\r
199                 }, task_priority::high_priority));\r
200         }\r
201 };\r
202 \r
203 output::output(const video_format_desc& format_desc, int channel_index) : impl_(new impl(format_desc, channel_index)){}\r
204 void output::add(int index, const spl::shared_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}\r
205 void output::add(const spl::shared_ptr<frame_consumer>& consumer){impl_->add(consumer);}\r
206 void output::remove(int index){impl_->remove(index);}\r
207 void output::remove(const spl::shared_ptr<frame_consumer>& consumer){impl_->remove(consumer);}\r
208 boost::unique_future<boost::property_tree::wptree> output::info() const{return impl_->info();}\r
209 void output::operator()(spl::shared_ptr<const data_frame> frame, const video_format_desc& format_desc){(*impl_)(std::move(frame), format_desc);}\r
210 }}