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