]> git.sesse.net Git - casparcg/blob - core/mixer/mixer.cpp
Merge branch 'master' of https://github.com/CasparCG/Server
[casparcg] / core / mixer / mixer.cpp
1 /*\r
2 * Copyright 2013 Sveriges Television AB http://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 #include "mixer.h"\r
25 \r
26 #include "read_frame.h"\r
27 #include "write_frame.h"\r
28 \r
29 #include "audio/audio_mixer.h"\r
30 #include "image/image_mixer.h"\r
31 \r
32 #include <common/env.h>\r
33 #include <common/concurrency/executor.h>\r
34 #include <common/concurrency/future_util.h>\r
35 #include <common/exception/exceptions.h>\r
36 #include <common/gl/gl_check.h>\r
37 #include <common/utility/tweener.h>\r
38 #include <common/memory/safe_ptr.h>\r
39 \r
40 #include <core/mixer/audio/audio_util.h>\r
41 #include <core/mixer/read_frame.h>\r
42 #include <core/mixer/write_frame.h>\r
43 #include <core/producer/frame/basic_frame.h>\r
44 #include <core/producer/frame/frame_factory.h>\r
45 #include <core/producer/frame/frame_transform.h>\r
46 #include <core/producer/frame/pixel_format.h>\r
47 \r
48 #include <core/monitor/monitor.h>\r
49 \r
50 #include <core/video_format.h>\r
51 \r
52 #include <boost/foreach.hpp>\r
53 #include <boost/timer.hpp>\r
54 #include <boost/property_tree/ptree.hpp>\r
55 \r
56 #include <tbb/concurrent_queue.h>\r
57 #include <tbb/spin_mutex.h>\r
58 #include <tbb/atomic.h>\r
59 \r
60 #include <unordered_map>\r
61 \r
62 namespace caspar { namespace core {\r
63                 \r
64 struct mixer::implementation : boost::noncopyable\r
65 {               \r
66         safe_ptr<diagnostics::graph>    graph_;\r
67         boost::timer                                    mix_timer_;\r
68         tbb::atomic<int64_t>                    current_mix_time_;\r
69 \r
70         safe_ptr<mixer::target_t>               target_;\r
71         mutable tbb::spin_mutex                 format_desc_mutex_;\r
72         video_format_desc                               format_desc_;\r
73         safe_ptr<ogl_device>                    ogl_;\r
74         channel_layout                                  audio_channel_layout_;\r
75         bool                                                    straighten_alpha_;\r
76         \r
77         audio_mixer     audio_mixer_;\r
78         image_mixer image_mixer_;\r
79         \r
80         std::unordered_map<int, blend_mode> blend_modes_;\r
81                         \r
82         executor executor_;\r
83         safe_ptr<monitor::subject>               monitor_subject_;\r
84 \r
85 public:\r
86         implementation(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<mixer::target_t>& target, const video_format_desc& format_desc, const safe_ptr<ogl_device>& ogl, const channel_layout& audio_channel_layout) \r
87                 : graph_(graph)\r
88                 , target_(target)\r
89                 , format_desc_(format_desc)\r
90                 , ogl_(ogl)\r
91                 , audio_channel_layout_(audio_channel_layout)\r
92                 , straighten_alpha_(false)\r
93                 , audio_mixer_(graph_)\r
94                 , image_mixer_(ogl)\r
95                 , executor_(L"mixer")\r
96                 , monitor_subject_(make_safe<monitor::subject>("/mixer"))\r
97         {                       \r
98                 graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8));\r
99                 current_mix_time_ = 0;\r
100 \r
101                 audio_mixer_.monitor_output().attach_parent(monitor_subject_);\r
102         }\r
103         \r
104         void send(const std::pair<std::map<int, safe_ptr<core::basic_frame>>, std::shared_ptr<void>>& packet)\r
105         {                       \r
106                 executor_.begin_invoke([=]\r
107                 {               \r
108                         try\r
109                         {\r
110                                 mix_timer_.restart();\r
111 \r
112                                 auto frames = packet.first;\r
113                                 \r
114                                 BOOST_FOREACH(auto& frame, frames)\r
115                                 {\r
116                                         auto blend_it = blend_modes_.find(frame.first);\r
117                                         image_mixer_.begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);\r
118                                                                                                         \r
119                                         frame.second->accept(audio_mixer_);                                     \r
120                                         frame.second->accept(image_mixer_);\r
121 \r
122                                         image_mixer_.end_layer();\r
123                                 }\r
124 \r
125                                 auto image = image_mixer_(format_desc_, straighten_alpha_);\r
126                                 auto audio = audio_mixer_(format_desc_, audio_channel_layout_);\r
127                                 image.wait();\r
128 \r
129                                 auto mix_time = mix_timer_.elapsed();\r
130                                 graph_->set_value("mix-time", mix_time*format_desc_.fps*0.5);\r
131                                 current_mix_time_ = static_cast<int64_t>(mix_time * 1000.0);\r
132 \r
133                                 target_->send(std::make_pair(make_safe<read_frame>(ogl_, format_desc_.size, std::move(image.get()), std::move(audio), audio_channel_layout_), packet.second));\r
134                         }\r
135                         catch(...)\r
136                         {\r
137                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
138                         }       \r
139                 });             \r
140         }\r
141                                         \r
142         safe_ptr<core::write_frame> create_frame(\r
143                         const void* tag,\r
144                         const core::pixel_format_desc& desc,\r
145                         const channel_layout& audio_channel_layout)\r
146         {               \r
147                 return make_safe<write_frame>(ogl_, tag, desc, audio_channel_layout);\r
148         }\r
149 \r
150         blend_mode::type get_blend_mode(int index)\r
151         {\r
152                 return executor_.invoke([=]\r
153                 {\r
154                         return blend_modes_[index].mode;\r
155                 });\r
156         }\r
157                                 \r
158         void set_blend_mode(int index, blend_mode::type value)\r
159         {\r
160                 executor_.begin_invoke([=]\r
161                 {\r
162                         blend_modes_[index].mode = value;\r
163                 }, high_priority);\r
164         }\r
165 \r
166         void clear_blend_mode(int index)\r
167         {\r
168                 executor_.begin_invoke([=]\r
169                 {\r
170                         blend_modes_.erase(index);\r
171                 }, high_priority);\r
172         }\r
173 \r
174         void clear_blend_modes()\r
175         {\r
176                 executor_.begin_invoke([=]\r
177                 {\r
178                         blend_modes_.clear();\r
179                 }, high_priority);\r
180         }\r
181 \r
182         chroma get_chroma(int index)\r
183         {\r
184                 return executor_.invoke([=]\r
185                 {\r
186                         return blend_modes_[index].chroma;\r
187                 });\r
188         }\r
189 \r
190     void set_chroma(int index, const chroma & value)\r
191     {\r
192         executor_.begin_invoke([=]\r
193         {\r
194             blend_modes_[index].chroma = value;\r
195         }, high_priority);\r
196     }\r
197 \r
198         void set_straight_alpha_output(bool value)\r
199         {\r
200         executor_.begin_invoke([=]\r
201         {\r
202                         straighten_alpha_ = value;\r
203         }, high_priority);\r
204         }\r
205 \r
206         bool get_straight_alpha_output()\r
207         {\r
208                 return executor_.invoke([=]\r
209                 {\r
210                         return straighten_alpha_;\r
211                 });\r
212         }\r
213 \r
214         float get_master_volume()\r
215         {\r
216                 return executor_.invoke([=]\r
217                 {\r
218                         return audio_mixer_.get_master_volume();\r
219                 });\r
220         }\r
221 \r
222         void set_master_volume(float volume)\r
223         {\r
224                 executor_.begin_invoke([=]\r
225                 {\r
226                         audio_mixer_.set_master_volume(volume);\r
227                 }, high_priority);\r
228         }\r
229         \r
230         void set_video_format_desc(const video_format_desc& format_desc)\r
231         {\r
232                 executor_.begin_invoke([=]\r
233                 {\r
234                         tbb::spin_mutex::scoped_lock lock(format_desc_mutex_);\r
235                         format_desc_ = format_desc;\r
236                 });\r
237         }\r
238 \r
239         core::video_format_desc get_video_format_desc() const // nothrow\r
240         {\r
241                 tbb::spin_mutex::scoped_lock lock(format_desc_mutex_);\r
242                 return format_desc_;\r
243         }\r
244 \r
245         boost::unique_future<boost::property_tree::wptree> info() const\r
246         {\r
247                 boost::property_tree::wptree info;\r
248                 info.add(L"mix-time", current_mix_time_);\r
249 \r
250                 return wrap_as_future(std::move(info));\r
251         }\r
252 \r
253         boost::unique_future<boost::property_tree::wptree> delay_info() const\r
254         {\r
255                 boost::property_tree::wptree info;\r
256                 info.put_value(current_mix_time_);\r
257 \r
258                 return wrap_as_future(std::move(info));\r
259         }\r
260 };\r
261         \r
262 mixer::mixer(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<target_t>& target, const video_format_desc& format_desc, const safe_ptr<ogl_device>& ogl, const channel_layout& audio_channel_layout) \r
263         : impl_(new implementation(graph, target, format_desc, ogl, audio_channel_layout)){}\r
264 void mixer::send(const std::pair<std::map<int, safe_ptr<core::basic_frame>>, std::shared_ptr<void>>& frames){ impl_->send(frames);}\r
265 core::video_format_desc mixer::get_video_format_desc() const { return impl_->get_video_format_desc(); }\r
266 safe_ptr<core::write_frame> mixer::create_frame(const void* tag, const core::pixel_format_desc& desc, const channel_layout& audio_channel_layout){ return impl_->create_frame(tag, desc, audio_channel_layout); }               \r
267 blend_mode::type mixer::get_blend_mode(int index) { return impl_->get_blend_mode(index); }\r
268 void mixer::set_blend_mode(int index, blend_mode::type value){impl_->set_blend_mode(index, value);}\r
269 chroma mixer::get_chroma(int index) { return impl_->get_chroma(index); }\r
270 void mixer::set_chroma(int index, const chroma & value){impl_->set_chroma(index, value);}\r
271 void mixer::clear_blend_mode(int index) { impl_->clear_blend_mode(index); }\r
272 void mixer::clear_blend_modes() { impl_->clear_blend_modes(); }\r
273 void mixer::set_straight_alpha_output(bool value) { impl_->set_straight_alpha_output(value); }\r
274 bool mixer::get_straight_alpha_output() { return impl_->get_straight_alpha_output(); }\r
275 float mixer::get_master_volume() { return impl_->get_master_volume(); }\r
276 void mixer::set_master_volume(float volume) { impl_->set_master_volume(volume); }\r
277 void mixer::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
278 boost::unique_future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}\r
279 boost::unique_future<boost::property_tree::wptree> mixer::delay_info() const{return impl_->delay_info();}\r
280 monitor::subject& mixer::monitor_output(){return *impl_->monitor_subject_;}\r
281 }}