]> git.sesse.net Git - casparcg/blob - core/mixer/mixer.cpp
cc062e207aba9e82ad70e1b49ffe38ddb5e16a8d
[casparcg] / core / mixer / mixer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@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 "../frame/data_frame.h"\r
27 \r
28 #include "audio/audio_mixer.h"\r
29 #include "image/image_mixer.h"\r
30 \r
31 #include "gpu/image/image_mixer.h"\r
32 #include "gpu/accelerator.h"\r
33 #include "gpu/write_frame.h"\r
34 \r
35 #include <common/env.h>\r
36 #include <common/concurrency/executor.h>\r
37 #include <common/diagnostics/graph.h>\r
38 #include <common/except.h>\r
39 #include <common/gl/gl_check.h>\r
40 \r
41 #include <core/frame/draw_frame.h>\r
42 #include <core/frame/frame_factory.h>\r
43 #include <core/frame/frame_transform.h>\r
44 #include <core/frame/pixel_format.h>\r
45 #include <core/video_format.h>\r
46 \r
47 #include <boost/foreach.hpp>\r
48 #include <boost/timer.hpp>\r
49 #include <boost/property_tree/ptree.hpp>\r
50 #include <boost/range/algorithm_ext.hpp>\r
51 \r
52 #include <tbb/concurrent_queue.h>\r
53 #include <tbb/spin_mutex.h>\r
54 \r
55 #include <unordered_map>\r
56 #include <vector>\r
57 \r
58 namespace caspar { namespace core {\r
59 \r
60 struct mixed_frame : public data_frame\r
61 {\r
62         mutable boost::unique_future<boost::iterator_range<const uint8_t*>>     image_data_;\r
63         const audio_buffer                                                                                                      audio_data_;\r
64         const video_format_desc                                                                                         video_desc_;\r
65         pixel_format_desc                                                                                                       pixel_desc_;\r
66         const void*                                                                                                                     tag_;\r
67 \r
68 public:\r
69         mixed_frame(const void* tag, boost::unique_future<boost::iterator_range<const uint8_t*>>&& image_data, audio_buffer&& audio_data, const video_format_desc& format_desc) \r
70                 : tag_(tag)\r
71                 , image_data_(std::move(image_data))\r
72                 , audio_data_(std::move(audio_data))\r
73                 , video_desc_(format_desc)\r
74                 , pixel_desc_(core::pixel_format::bgra)\r
75         {\r
76                 pixel_desc_.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));\r
77         }       \r
78         \r
79         const boost::iterator_range<const uint8_t*> image_data(int index = 0) const override\r
80         {\r
81                 return image_data_.get();\r
82         }\r
83                 \r
84         const boost::iterator_range<uint8_t*> image_data(int) override\r
85         {\r
86                 BOOST_THROW_EXCEPTION(invalid_operation());\r
87         }\r
88         \r
89         virtual const struct pixel_format_desc& get_pixel_format_desc() const override\r
90         {\r
91                 return pixel_desc_;\r
92         }\r
93 \r
94         virtual const audio_buffer& audio_data() const override\r
95         {\r
96                 return audio_data_;\r
97         }\r
98 \r
99         virtual audio_buffer& audio_data() override\r
100         {\r
101                 BOOST_THROW_EXCEPTION(invalid_operation());\r
102         }\r
103 \r
104         virtual double get_frame_rate() const override\r
105         {\r
106                 return video_desc_.fps;\r
107         }\r
108 \r
109         virtual int width() const override\r
110         {\r
111                 return video_desc_.width;\r
112         }\r
113 \r
114         virtual int height() const override\r
115         {\r
116                 return video_desc_.height;\r
117         }\r
118 \r
119         virtual const void* tag() const override\r
120         {\r
121                 return tag_;\r
122         }\r
123 };\r
124                 \r
125 struct mixer::impl : boost::noncopyable\r
126 {                       \r
127         safe_ptr<gpu::accelerator>                      ogl_;\r
128         \r
129         audio_mixer                                                     audio_mixer_;\r
130         safe_ptr<image_mixer>                           image_mixer_;\r
131         \r
132         std::unordered_map<int, blend_mode>     blend_modes_;\r
133                         \r
134         executor executor_;\r
135 \r
136 public:\r
137         impl(const safe_ptr<gpu::accelerator>& ogl) \r
138                 : ogl_(ogl)\r
139                 , audio_mixer_()\r
140                 , image_mixer_(make_safe<gpu::image_mixer>(ogl_))\r
141                 , executor_(L"mixer")\r
142         {                       \r
143         }       \r
144         \r
145         safe_ptr<const data_frame> operator()(std::map<int, safe_ptr<draw_frame>> frames, const video_format_desc& format_desc)\r
146         {               \r
147                 return executor_.invoke([=]() mutable -> safe_ptr<const struct data_frame>\r
148                 {               \r
149                         try\r
150                         {                               \r
151                                 BOOST_FOREACH(auto& frame, frames)\r
152                                 {\r
153                                         auto blend_it = blend_modes_.find(frame.first);\r
154                                         image_mixer_->begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);\r
155                                                                                                         \r
156                                         frame.second->accept(audio_mixer_);                                     \r
157                                         frame.second->accept(*image_mixer_);\r
158 \r
159                                         image_mixer_->end_layer();\r
160                                 }\r
161 \r
162                                 auto image = (*image_mixer_)(format_desc);\r
163                                 auto audio = audio_mixer_(format_desc);\r
164 \r
165                                 return make_safe<mixed_frame>(this, std::move(image), std::move(audio), format_desc);   \r
166                         }\r
167                         catch(...)\r
168                         {\r
169                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
170                                 return data_frame::empty();\r
171                         }       \r
172                 });             \r
173         }\r
174                                         \r
175         void set_blend_mode(int index, blend_mode value)\r
176         {\r
177                 executor_.begin_invoke([=]\r
178                 {\r
179                         auto it = blend_modes_.find(index);\r
180                         if(it == blend_modes_.end())\r
181                                 blend_modes_.insert(std::make_pair(index, value));\r
182                         else\r
183                                 it->second = value;\r
184                 }, high_priority);\r
185         }\r
186         \r
187         boost::unique_future<boost::property_tree::wptree> info() const\r
188         {\r
189                 boost::promise<boost::property_tree::wptree> info;\r
190                 info.set_value(boost::property_tree::wptree());\r
191                 return info.get_future();\r
192         }\r
193 };\r
194         \r
195 mixer::mixer(const safe_ptr<gpu::accelerator>& ogl) \r
196         : impl_(new impl(ogl)){}\r
197 void mixer::set_blend_mode(int index, blend_mode value){impl_->set_blend_mode(index, value);}\r
198 boost::unique_future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}\r
199 safe_ptr<const data_frame> mixer::operator()(std::map<int, safe_ptr<draw_frame>> frames, const struct video_format_desc& format_desc){return (*impl_)(std::move(frames), format_desc);}\r
200 }}