]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
2.0.0.2: - Renamed core components, since they no longer are active objects then...
[casparcg] / core / consumer / output.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 "output.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/utility/assert.h>\r
36 #include <common/utility/timer.h>\r
37 #include <common/memory/memshfl.h>\r
38 \r
39 #include <boost/circular_buffer.hpp>\r
40 \r
41 namespace caspar { namespace core {\r
42         \r
43 struct output::implementation\r
44 {       \r
45         typedef std::pair<safe_ptr<const read_frame>, safe_ptr<const read_frame>> fill_and_key;\r
46         \r
47         video_channel_context& channel_;\r
48 \r
49         std::map<int, safe_ptr<frame_consumer>> consumers_;\r
50         typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;\r
51         \r
52         high_prec_timer timer_;\r
53                 \r
54 public:\r
55         implementation(video_channel_context& video_channel) \r
56                 : channel_(video_channel)\r
57         {               \r
58         }\r
59 \r
60         std::pair<size_t, size_t> buffer_depth()\r
61         {               \r
62                 auto depth_comp = [](const layer_t& lhs, const layer_t& rhs){ return lhs.second->buffer_depth() < rhs.second->buffer_depth(); };\r
63                 auto min = std::min_element(consumers_.begin(), consumers_.end(), depth_comp)->second->buffer_depth();\r
64                 auto max = std::max_element(consumers_.begin(), consumers_.end(), depth_comp)->second->buffer_depth();\r
65                 CASPAR_ASSERT(max >= min);\r
66                 return std::make_pair(min, max);\r
67         }\r
68 \r
69         void add(int index, safe_ptr<frame_consumer>&& consumer)\r
70         {               \r
71                 consumer->initialize(channel_.get_format_desc());\r
72                 channel_.execution().invoke([&]\r
73                 {\r
74                         this->remove(index);\r
75                         consumers_.insert(std::make_pair(index, consumer));\r
76 \r
77                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
78                 });\r
79         }\r
80 \r
81         void remove(int index)\r
82         {\r
83                 channel_.execution().invoke([&]\r
84                 {\r
85                         auto it = consumers_.find(index);\r
86                         if(it != consumers_.end())\r
87                         {\r
88                                 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
89                                 consumers_.erase(it);\r
90                         }\r
91                 });\r
92         }\r
93                                                 \r
94         void execute(const safe_ptr<read_frame>& frame)\r
95         {               \r
96                 try\r
97                 {               \r
98                         if(!has_synchronization_clock())\r
99                                 timer_.tick(1.0/channel_.get_format_desc().fps);\r
100                                                 \r
101                         auto fill = frame;\r
102                         auto key = get_key_frame(frame);\r
103 \r
104                         auto it = consumers_.begin();\r
105                         while(it != consumers_.end())\r
106                         {\r
107                                 try\r
108                                 {\r
109                                         auto consumer = it->second;\r
110 \r
111                                         if(consumer->get_video_format_desc() != channel_.get_format_desc())\r
112                                                 consumer->initialize(channel_.get_format_desc());\r
113 \r
114                                         auto frame = consumer->key_only() ? key : fill;\r
115 \r
116                                         if(static_cast<size_t>(frame->image_data().size()) == consumer->get_video_format_desc().size)\r
117                                                 consumer->send(frame);\r
118 \r
119                                         ++it;\r
120                                 }\r
121                                 catch(...)\r
122                                 {\r
123                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
124                                         consumers_.erase(it++);\r
125                                         CASPAR_LOG(error) << print() << L" " << it->second->print() << L" Removed.";\r
126                                 }\r
127                         }\r
128                 }\r
129                 catch(...)\r
130                 {\r
131                         CASPAR_LOG_CURRENT_EXCEPTION();\r
132                 }\r
133         }\r
134 \r
135 private:\r
136         \r
137         bool has_synchronization_clock()\r
138         {\r
139                 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
140                 {\r
141                         return p.second->has_synchronization_clock();\r
142                 });\r
143         }\r
144 \r
145         safe_ptr<const read_frame> get_key_frame(const safe_ptr<const read_frame>& frame)\r
146         {\r
147                 bool has_key_only = std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
148                 {\r
149                         return p.second->key_only();\r
150                 });\r
151 \r
152                 if(has_key_only)\r
153                 {\r
154                         // Currently do key_only transform on cpu. Unsure if the extra 400MB/s (1080p50) overhead is worth it to do it on gpu.\r
155                         auto key_data = channel_.ogl().create_host_buffer(frame->image_data().size(), host_buffer::write_only);                         \r
156                         fast_memsfhl(key_data->data(), frame->image_data().begin(), frame->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);\r
157                         std::vector<int16_t> audio_data(frame->audio_data().begin(), frame->audio_data().end());\r
158                         return make_safe<read_frame>(std::move(key_data), std::move(audio_data));\r
159                 }\r
160                 \r
161                 return make_safe<read_frame>();\r
162         }\r
163         \r
164 \r
165         std::wstring print() const\r
166         {\r
167                 return L"output";\r
168         }\r
169 };\r
170 \r
171 output::output(video_channel_context& video_channel) \r
172         : impl_(new implementation(video_channel)){}\r
173 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}\r
174 void output::remove(int index){impl_->remove(index);}\r
175 void output::execute(const safe_ptr<read_frame>& frame) {impl_->execute(frame); }\r
176 }}