]> 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) 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 #include "port.h"\r
32 \r
33 #include "../video_format.h"\r
34 #include "../frame/frame.h"\r
35 \r
36 #include <common/assert.h>\r
37 #include <common/future.h>\r
38 #include <common/executor.h>\r
39 #include <common/diagnostics/graph.h>\r
40 #include <common/prec_timer.h>\r
41 #include <common/memshfl.h>\r
42 #include <common/env.h>\r
43 \r
44 #include <boost/circular_buffer.hpp>\r
45 #include <boost/lexical_cast.hpp>\r
46 #include <boost/property_tree/ptree.hpp>\r
47 #include <boost/range/algorithm.hpp>\r
48 #include <boost/range/adaptors.hpp>\r
49 #include <boost/timer.hpp>\r
50 \r
51 namespace caspar { namespace core {\r
52 \r
53 struct output::impl\r
54 {               \r
55         spl::shared_ptr<diagnostics::graph>     graph_;\r
56         monitor::basic_subject                          event_subject_;\r
57         const int                                                       channel_index_;\r
58         video_format_desc                                       format_desc_;\r
59         std::map<int, port>                                     ports_; \r
60         prec_timer                                                      sync_timer_;\r
61         boost::circular_buffer<const_frame>     frames_;\r
62         executor                                                        executor_;              \r
63 public:\r
64         impl(spl::shared_ptr<diagnostics::graph> graph, const video_format_desc& format_desc, int channel_index) \r
65                 : graph_(std::move(graph))\r
66                 , event_subject_("output")\r
67                 , channel_index_(channel_index)\r
68                 , format_desc_(format_desc)\r
69                 , executor_(L"output")\r
70         {\r
71                 graph_->set_color("consume-time", diagnostics::color(1.0f, 0.4f, 0.0f, 0.8));\r
72         }       \r
73         \r
74         void add(int index, spl::shared_ptr<frame_consumer> consumer)\r
75         {               \r
76                 remove(index);\r
77 \r
78                 consumer->initialize(format_desc_, channel_index_);\r
79                 \r
80                 executor_.begin_invoke([this, index, consumer]\r
81                 {                       \r
82                         port p(index, channel_index_, std::move(consumer));\r
83                         p.subscribe(event_subject_);\r
84                         ports_.insert(std::make_pair(index, std::move(p)));\r
85                 }, task_priority::high_priority);\r
86         }\r
87 \r
88         void add(const spl::shared_ptr<frame_consumer>& consumer)\r
89         {\r
90                 add(consumer->index(), consumer);\r
91         }\r
92 \r
93         void remove(int index)\r
94         {               \r
95                 executor_.begin_invoke([=]\r
96                 {\r
97                         auto it = ports_.find(index);\r
98                         if(it != ports_.end())\r
99                                 ports_.erase(it);                                       \r
100                 }, task_priority::high_priority);\r
101         }\r
102 \r
103         void remove(const spl::shared_ptr<frame_consumer>& consumer)\r
104         {\r
105                 remove(consumer->index());\r
106         }\r
107         \r
108         void video_format_desc(const core::video_format_desc& format_desc)\r
109         {\r
110                 executor_.invoke([&]\r
111                 {\r
112                         if(format_desc_ == format_desc)\r
113                                 return;\r
114 \r
115                         auto it = ports_.begin();\r
116                         while(it != ports_.end())\r
117                         {                                               \r
118                                 try\r
119                                 {\r
120                                         it->second.video_format_desc(format_desc);\r
121                                         ++it;\r
122                                 }\r
123                                 catch(...)\r
124                                 {\r
125                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
126                                         ports_.erase(it++);\r
127                                 }\r
128                         }\r
129                         \r
130                         format_desc_ = format_desc;\r
131                         frames_.clear();\r
132                 });\r
133         }\r
134 \r
135         std::pair<int, int> minmax_buffer_depth() const\r
136         {               \r
137                 if(ports_.empty())\r
138                         return std::make_pair(0, 0);\r
139                 \r
140                 auto buffer_depths = ports_ | \r
141                                                          boost::adaptors::map_values | // std::function is MSVC workaround\r
142                                                          boost::adaptors::transformed(std::function<int(const port&)>([](const port& p){return p.buffer_depth();})); \r
143                 \r
144 \r
145                 return std::make_pair(*boost::range::min_element(buffer_depths), *boost::range::max_element(buffer_depths));\r
146         }\r
147 \r
148         bool has_synchronization_clock() const\r
149         {\r
150                 return boost::range::count_if(ports_ | boost::adaptors::map_values,\r
151                                                                           [](const port& p){return p.has_synchronization_clock();}) > 0;\r
152         }\r
153                 \r
154         void operator()(const_frame input_frame, const core::video_format_desc& format_desc)\r
155         {\r
156                 video_format_desc(format_desc);\r
157 \r
158                 executor_.invoke([=]\r
159                 {                       \r
160                         boost::timer frame_timer;\r
161 \r
162                         if(!has_synchronization_clock())\r
163                                 sync_timer_.tick(1.0/format_desc_.fps);\r
164                                 \r
165                         if(input_frame.size() != format_desc_.size)\r
166                         {\r
167                                 CASPAR_LOG(debug) << print() << L" Invalid input frame dimension.";\r
168                                 return;\r
169                         }\r
170 \r
171                         auto minmax = minmax_buffer_depth();\r
172 \r
173                         frames_.set_capacity(std::max(2, minmax.second - minmax.first) + 1); // std::max(2, x) since we want to guarantee some pipeline depth for asycnhronous mixer read-back.\r
174                         frames_.push_back(input_frame);\r
175 \r
176                         if(!frames_.full())\r
177                                 return;\r
178 \r
179                         for(auto it = ports_.begin(); it != ports_.end();)\r
180                         {\r
181                                 auto& port      = it->second;\r
182                                 auto& frame     = frames_.at(port.buffer_depth()-minmax.first);\r
183                                         \r
184                                 try\r
185                                 {\r
186                                         if(port.send(frame))\r
187                                                 ++it;\r
188                                         else\r
189                                                 ports_.erase(it++);                                     \r
190                                 }\r
191                                 catch(...)\r
192                                 {\r
193                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
194                                         ports_.erase(it++);\r
195                                 }\r
196                         }\r
197 \r
198                         graph_->set_value("consume-time", frame_timer.elapsed()*format_desc.fps*0.5);\r
199                 });\r
200         }\r
201 \r
202         std::wstring print() const\r
203         {\r
204                 return L"output[" + boost::lexical_cast<std::wstring>(channel_index_) + L"]";\r
205         }\r
206 \r
207         boost::unique_future<boost::property_tree::wptree> info()\r
208         {\r
209                 return std::move(executor_.begin_invoke([&]() -> boost::property_tree::wptree\r
210                 {                       \r
211                         boost::property_tree::wptree info;\r
212                         BOOST_FOREACH(auto& port, ports_)\r
213                         {\r
214                                 info.add_child(L"consumers.consumer", port.second.info())\r
215                                         .add(L"index", port.first); \r
216                         }\r
217                         return info;\r
218                 }, task_priority::high_priority));\r
219         }\r
220 };\r
221 \r
222 output::output(spl::shared_ptr<diagnostics::graph> graph, const video_format_desc& format_desc, int channel_index) : impl_(new impl(std::move(graph), format_desc, channel_index)){}\r
223 void output::add(int index, const spl::shared_ptr<frame_consumer>& consumer){impl_->add(index, consumer);}\r
224 void output::add(const spl::shared_ptr<frame_consumer>& consumer){impl_->add(consumer);}\r
225 void output::remove(int index){impl_->remove(index);}\r
226 void output::remove(const spl::shared_ptr<frame_consumer>& consumer){impl_->remove(consumer);}\r
227 boost::unique_future<boost::property_tree::wptree> output::info() const{return impl_->info();}\r
228 void output::operator()(const_frame frame, const video_format_desc& format_desc){(*impl_)(std::move(frame), format_desc);}\r
229 void output::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}\r
230 void output::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}\r
231 }}