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