]> git.sesse.net Git - casparcg/blob - core/consumer/frame_consumer_device.cpp
2.0.0.2:
[casparcg] / core / consumer / frame_consumer_device.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 #include "../StdAfx.h"\r
21 \r
22 #ifdef _MSC_VER\r
23 #pragma warning (disable : 4244)\r
24 #endif\r
25 \r
26 #include "frame_consumer_device.h"\r
27 \r
28 #include "../video_channel_context.h"\r
29 \r
30 #include "../video_format.h"\r
31 #include "../mixer/gpu/ogl_device.h"\r
32 #include "../mixer/read_frame.h"\r
33 \r
34 #include <common/concurrency/executor.h>\r
35 #include <common/diagnostics/graph.h>\r
36 #include <common/utility/assert.h>\r
37 #include <common/utility/timer.h>\r
38 #include <common/memory/memshfl.h>\r
39 \r
40 #include <boost/circular_buffer.hpp>\r
41 #include <boost/timer.hpp>\r
42 \r
43 namespace caspar { namespace core {\r
44         \r
45 struct frame_consumer_device::implementation\r
46 {       \r
47         typedef std::pair<safe_ptr<const read_frame>, safe_ptr<const read_frame>> fill_and_key;\r
48         \r
49         video_channel_context& channel_;\r
50 \r
51         boost::circular_buffer<fill_and_key> buffer_;\r
52 \r
53         std::map<int, safe_ptr<frame_consumer>> consumers_;\r
54         typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;\r
55         \r
56         high_prec_timer timer_;\r
57         \r
58         safe_ptr<diagnostics::graph> diag_;\r
59 \r
60         boost::timer frame_timer_;\r
61         boost::timer tick_timer_;\r
62                 \r
63 public:\r
64         implementation(video_channel_context& video_channel) \r
65                 : channel_(video_channel)\r
66                 , diag_(diagnostics::create_graph(std::string("frame_consumer_device")))\r
67         {               \r
68                 diag_->add_guide("frame-time", 0.5f);   \r
69                 diag_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));\r
70                 diag_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));\r
71         }\r
72 \r
73         std::pair<size_t, size_t> buffer_depth()\r
74         {               \r
75                 auto depth_comp = [](const layer_t& lhs, const layer_t& rhs){ return lhs.second->buffer_depth() < rhs.second->buffer_depth(); };\r
76                 auto min = std::min_element(consumers_.begin(), consumers_.end(), depth_comp)->second->buffer_depth();\r
77                 auto max = std::max_element(consumers_.begin(), consumers_.end(), depth_comp)->second->buffer_depth();\r
78                 CASPAR_ASSERT(max >= min);\r
79                 return std::make_pair(min, max);\r
80         }\r
81 \r
82         void add(int index, safe_ptr<frame_consumer>&& consumer)\r
83         {               \r
84                 consumer->initialize(channel_.format_desc);\r
85                 channel_.execution.invoke([&]\r
86                 {\r
87                         this->remove(index);\r
88                         consumers_.insert(std::make_pair(index, consumer));\r
89 \r
90                         auto depth = buffer_depth();\r
91                         auto diff = depth.second-depth.first+1;\r
92                         \r
93                         if(diff != buffer_.capacity())\r
94                         {\r
95                                 buffer_.set_capacity(diff);\r
96                                 CASPAR_LOG(info) << print() << L" Depth-diff: " << diff-1;\r
97                         }\r
98 \r
99                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
100                 });\r
101         }\r
102 \r
103         void remove(int index)\r
104         {\r
105                 channel_.execution.invoke([&]\r
106                 {\r
107                         auto it = consumers_.find(index);\r
108                         if(it != consumers_.end())\r
109                         {\r
110                                 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
111                                 consumers_.erase(it);\r
112                         }\r
113                 });\r
114         }\r
115                                                 \r
116         void operator()(const safe_ptr<read_frame>& frame)\r
117         {               \r
118                 if(!has_synchronization_clock())\r
119                         timer_.tick(1.0/channel_.format_desc.fps);\r
120 \r
121                 frame_timer_.restart();\r
122                                                 \r
123                 buffer_.push_back(std::make_pair(frame, get_key_frame(frame)));\r
124                 if(!buffer_.full())\r
125                         return;\r
126         \r
127                 for_each_consumer([&](safe_ptr<frame_consumer>& consumer)\r
128                 {\r
129                         if(consumer->get_video_format_desc() != channel_.format_desc)\r
130                                 consumer->initialize(channel_.format_desc);\r
131 \r
132                         auto pair = buffer_[consumer->buffer_depth()-buffer_depth().first];\r
133                         auto frame = consumer->key_only() ? pair.second : pair.first;\r
134 \r
135                         if(static_cast<size_t>(frame->image_data().size()) == consumer->get_video_format_desc().size)\r
136                                 consumer->send(frame);\r
137                 });\r
138 \r
139                 diag_->update_value("frame-time", frame_timer_.elapsed()*channel_.format_desc.fps*0.5);\r
140                         \r
141                 diag_->update_value("tick-time", tick_timer_.elapsed()*channel_.format_desc.fps*0.5);\r
142                 tick_timer_.restart();\r
143         }\r
144 \r
145 private:\r
146 \r
147         bool has_synchronization_clock()\r
148         {\r
149                 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
150                 {\r
151                         return p.second->has_synchronization_clock();\r
152                 });\r
153         }\r
154 \r
155         safe_ptr<const read_frame> get_key_frame(const safe_ptr<const read_frame>& frame)\r
156         {\r
157                 bool has_key_only = std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
158                 {\r
159                         return p.second->key_only();\r
160                 });\r
161 \r
162                 if(has_key_only)\r
163                 {\r
164                         // Currently do key_only transform on cpu. Unsure if the extra 400MB/s (1080p50) overhead is worth it to do it on gpu.\r
165                         auto key_data = channel_.ogl.create_host_buffer(frame->image_data().size(), host_buffer::write_only);                           \r
166                         fast_memsfhl(key_data->data(), frame->image_data().begin(), frame->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);\r
167                         std::vector<int16_t> audio_data(frame->audio_data().begin(), frame->audio_data().end());\r
168                         return make_safe<read_frame>(std::move(key_data), std::move(audio_data));\r
169                 }\r
170                 \r
171                 return read_frame::empty();\r
172         }\r
173                 \r
174         void for_each_consumer(const std::function<void(safe_ptr<frame_consumer>& consumer)>& func)\r
175         {\r
176                 auto it = consumers_.begin();\r
177                 while(it != consumers_.end())\r
178                 {\r
179                         try\r
180                         {\r
181                                 func(it->second);\r
182                                 ++it;\r
183                         }\r
184                         catch(...)\r
185                         {\r
186                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
187                                 consumers_.erase(it++);\r
188                                 CASPAR_LOG(error) << print() << L" " << it->second->print() << L" Removed.";\r
189                         }\r
190                 }\r
191         }\r
192 \r
193         std::wstring print() const\r
194         {\r
195                 return L"frame_consumer_device";\r
196         }\r
197 };\r
198 \r
199 frame_consumer_device::frame_consumer_device(video_channel_context& video_channel) \r
200         : impl_(new implementation(video_channel)){}\r
201 void frame_consumer_device::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}\r
202 void frame_consumer_device::remove(int index){impl_->remove(index);}\r
203 void frame_consumer_device::operator()(const safe_ptr<read_frame>& frame) { (*impl_)(frame); }\r
204 }}