]> git.sesse.net Git - casparcg/blob - core/mixer/mixer.cpp
2.1.0: Started integrating monitoring from monitor-exp.
[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 <common/env.h>\r
32 #include <common/concurrency/executor.h>\r
33 #include <common/diagnostics/graph.h>\r
34 #include <common/except.h>\r
35 #include <common/gl/gl_check.h>\r
36 \r
37 #include <core/frame/draw_frame.h>\r
38 #include <core/frame/frame_factory.h>\r
39 #include <core/frame/frame_transform.h>\r
40 #include <core/frame/pixel_format.h>\r
41 #include <core/video_format.h>\r
42 \r
43 #include <boost/foreach.hpp>\r
44 #include <boost/timer.hpp>\r
45 #include <boost/property_tree/ptree.hpp>\r
46 #include <boost/range/algorithm_ext.hpp>\r
47 \r
48 #include <tbb/concurrent_queue.h>\r
49 #include <tbb/spin_mutex.h>\r
50 \r
51 #include <unordered_map>\r
52 #include <vector>\r
53 \r
54 namespace caspar { namespace core {\r
55 \r
56 struct mixed_frame : public data_frame\r
57 {\r
58         mutable boost::shared_future<boost::iterator_range<const uint8_t*>>     image_data_;\r
59         const audio_buffer                                                                                                      audio_data_;\r
60         const video_format_desc                                                                                         video_desc_;\r
61         pixel_format_desc                                                                                                       pixel_desc_;\r
62         const void*                                                                                                                     tag_;\r
63 \r
64 public:\r
65         mixed_frame(const void* tag, boost::shared_future<boost::iterator_range<const uint8_t*>>&& image_data, audio_buffer&& audio_data, const video_format_desc& format_desc) \r
66                 : tag_(tag)\r
67                 , image_data_(std::move(image_data))\r
68                 , audio_data_(std::move(audio_data))\r
69                 , video_desc_(format_desc)\r
70                 , pixel_desc_(core::pixel_format::bgra)\r
71         {\r
72                 pixel_desc_.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));\r
73         }       \r
74         \r
75         const boost::iterator_range<const uint8_t*> image_data(int index = 0) const override\r
76         {\r
77                 return image_data_.get();\r
78         }\r
79                 \r
80         const boost::iterator_range<uint8_t*> image_data(int) override\r
81         {\r
82                 BOOST_THROW_EXCEPTION(invalid_operation());\r
83         }\r
84         \r
85         virtual const struct pixel_format_desc& get_pixel_format_desc() const override\r
86         {\r
87                 return pixel_desc_;\r
88         }\r
89 \r
90         virtual const audio_buffer& audio_data() const override\r
91         {\r
92                 return audio_data_;\r
93         }\r
94 \r
95         virtual audio_buffer& audio_data() override\r
96         {\r
97                 BOOST_THROW_EXCEPTION(invalid_operation());\r
98         }\r
99 \r
100         virtual double get_frame_rate() const override\r
101         {\r
102                 return video_desc_.fps;\r
103         }\r
104 \r
105         virtual int width() const override\r
106         {\r
107                 return video_desc_.width;\r
108         }\r
109 \r
110         virtual int height() const override\r
111         {\r
112                 return video_desc_.height;\r
113         }\r
114 \r
115         virtual const void* tag() const override\r
116         {\r
117                 return tag_;\r
118         }\r
119 };\r
120                 \r
121 struct mixer::impl : boost::noncopyable\r
122 {                               \r
123         spl::shared_ptr<diagnostics::graph> graph_;\r
124         audio_mixer                                                     audio_mixer_;\r
125         spl::shared_ptr<image_mixer>            image_mixer_;\r
126         \r
127         std::unordered_map<int, blend_mode>     blend_modes_;\r
128                         \r
129         executor executor_;\r
130 \r
131 public:\r
132         impl(spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer) \r
133                 : graph_(std::move(graph))\r
134                 , audio_mixer_()\r
135                 , image_mixer_(std::move(image_mixer))\r
136                 , executor_(L"mixer")\r
137         {                       \r
138                 graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8));\r
139         }       \r
140         \r
141         spl::shared_ptr<const data_frame> operator()(std::map<int, spl::shared_ptr<draw_frame>> frames, const video_format_desc& format_desc)\r
142         {               \r
143                 return executor_.invoke([=]() mutable -> spl::shared_ptr<const struct data_frame>\r
144                 {               \r
145                         try\r
146                         {       \r
147                                 boost::timer frame_timer;\r
148 \r
149                                 BOOST_FOREACH(auto& frame, frames)\r
150                                 {\r
151                                         auto blend_it = blend_modes_.find(frame.first);\r
152                                         image_mixer_->begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);\r
153                                                                                                         \r
154                                         frame.second->accept(audio_mixer_);                                     \r
155                                         frame.second->accept(*image_mixer_);\r
156 \r
157                                         image_mixer_->end_layer();\r
158                                 }\r
159 \r
160                                 auto image = (*image_mixer_)(format_desc);\r
161                                 auto audio = audio_mixer_(format_desc);\r
162                                 \r
163                                 graph_->set_value("mix-time", frame_timer.elapsed()*format_desc.fps*0.5);\r
164 \r
165                                 return spl::make_shared<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                 }, task_priority::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(spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer) \r
196         : impl_(new impl(std::move(graph), std::move(image_mixer))){}\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 spl::shared_ptr<const data_frame> mixer::operator()(std::map<int, spl::shared_ptr<draw_frame>> frames, const struct video_format_desc& format_desc){return (*impl_)(std::move(frames), format_desc);}\r
200 spl::shared_ptr<write_frame> mixer::create_frame(const void* tag, const core::pixel_format_desc& desc) {return impl_->image_mixer_->create_frame(tag, desc);}\r
201 }}