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