]> git.sesse.net Git - casparcg/blob - core/consumer/output.cpp
2.0.0.2: Refactoring. Working on re-enabling filter functionality.
[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 <tbb/mutex.h>\r
40 \r
41 namespace caspar { namespace core {\r
42 \r
43 class key_read_frame_muxer : public core::read_frame\r
44 {\r
45         ogl_device&                                              ogl_;\r
46         safe_ptr<read_frame>                     fill_;\r
47         std::shared_ptr<host_buffer>     key_;\r
48         tbb::mutex                                           mutex_;\r
49 public:\r
50         key_read_frame_muxer(ogl_device& ogl, const safe_ptr<read_frame>& fill)\r
51                 : ogl_(ogl)\r
52                 , fill_(fill)\r
53         {\r
54         }\r
55 \r
56         virtual const boost::iterator_range<const uint8_t*> image_data()\r
57         {\r
58                 tbb::mutex::scoped_lock lock(mutex_);\r
59                 if(!key_)\r
60                 {\r
61                         key_ = ogl_.create_host_buffer(fill_->image_data().size(), host_buffer::write_only);                            \r
62                         fast_memsfhl(key_->data(), fill_->image_data().begin(), fill_->image_data().size(), 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);\r
63                 }\r
64 \r
65                 auto ptr = static_cast<const uint8_t*>(key_->data());\r
66                 return boost::iterator_range<const uint8_t*>(ptr, ptr + key_->size());\r
67         }\r
68 \r
69         virtual const boost::iterator_range<const int16_t*> audio_data()\r
70         {\r
71                 return fill_->audio_data();\r
72         }       \r
73 };\r
74         \r
75 struct output::implementation\r
76 {       \r
77         typedef std::pair<safe_ptr<read_frame>, safe_ptr<read_frame>> fill_and_key;\r
78         \r
79         video_channel_context& channel_;\r
80 \r
81         std::map<int, safe_ptr<frame_consumer>> consumers_;\r
82         typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;\r
83         \r
84         high_prec_timer timer_;\r
85                 \r
86 public:\r
87         implementation(video_channel_context& video_channel) \r
88                 : channel_(video_channel){}     \r
89         \r
90         void add(int index, safe_ptr<frame_consumer>&& consumer)\r
91         {               \r
92                 consumer->initialize(channel_.get_format_desc());\r
93                 channel_.execution().invoke([&]\r
94                 {\r
95                         consumers_.erase(index);\r
96                         consumers_.insert(std::make_pair(index, consumer));\r
97 \r
98                         CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";\r
99                 });\r
100         }\r
101 \r
102         void remove(int index)\r
103         {\r
104                 channel_.execution().invoke([&]\r
105                 {\r
106                         auto it = consumers_.find(index);\r
107                         if(it != consumers_.end())\r
108                         {\r
109                                 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";\r
110                                 consumers_.erase(it);\r
111                         }\r
112                 });\r
113         }\r
114                                                 \r
115         void execute(const safe_ptr<read_frame>& frame)\r
116         {               \r
117                 try\r
118                 {               \r
119                         if(!has_synchronization_clock())\r
120                                 timer_.tick(1.0/channel_.get_format_desc().fps);\r
121                                                 \r
122                         auto fill = frame;\r
123                         auto key = make_safe<key_read_frame_muxer>(channel_.ogl(), frame);\r
124 \r
125                         auto it = consumers_.begin();\r
126                         while(it != consumers_.end())\r
127                         {\r
128                                 try\r
129                                 {\r
130                                         auto consumer = it->second;\r
131 \r
132                                         if(consumer->get_video_format_desc() != channel_.get_format_desc())\r
133                                                 consumer->initialize(channel_.get_format_desc());\r
134 \r
135                                         auto frame = consumer->key_only() ? key : fill;\r
136 \r
137                                         if(static_cast<size_t>(frame->image_data().size()) == consumer->get_video_format_desc().size)\r
138                                                 consumer->send(frame);\r
139 \r
140                                         ++it;\r
141                                 }\r
142                                 catch(...)\r
143                                 {\r
144                                         CASPAR_LOG_CURRENT_EXCEPTION();\r
145                                         CASPAR_LOG(error) << print() << L" " << it->second->print() << L" Removed.";\r
146                                         consumers_.erase(it++);\r
147                                 }\r
148                         }\r
149                 }\r
150                 catch(...)\r
151                 {\r
152                         CASPAR_LOG_CURRENT_EXCEPTION();\r
153                 }\r
154         }\r
155 \r
156 private:\r
157         \r
158         bool has_synchronization_clock()\r
159         {\r
160                 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)\r
161                 {\r
162                         return p.second->has_synchronization_clock();\r
163                 });\r
164         }\r
165         \r
166         std::wstring print() const\r
167         {\r
168                 return L"output";\r
169         }\r
170 };\r
171 \r
172 output::output(video_channel_context& video_channel) : 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 }}