]> git.sesse.net Git - casparcg/blob - core/mixer/mixer.cpp
0a423fb20dc799e519ac68b91f49e9c6900632d4
[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/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/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 mixer::impl : boost::noncopyable\r
57 {                               \r
58         spl::shared_ptr<diagnostics::graph> graph_;\r
59         audio_mixer                                                     audio_mixer_;\r
60         spl::shared_ptr<image_mixer>            image_mixer_;\r
61         \r
62         std::unordered_map<int, blend_mode>     blend_modes_;\r
63                         \r
64         executor executor_;\r
65 \r
66 public:\r
67         impl(spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer) \r
68                 : graph_(std::move(graph))\r
69                 , audio_mixer_()\r
70                 , image_mixer_(std::move(image_mixer))\r
71                 , executor_(L"mixer")\r
72         {                       \r
73                 graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8));\r
74         }       \r
75         \r
76         const_frame operator()(std::map<int, draw_frame> frames, const video_format_desc& format_desc)\r
77         {               \r
78                 boost::timer frame_timer;\r
79 \r
80                 auto frame = executor_.invoke([=]() mutable -> const_frame\r
81                 {               \r
82                         try\r
83                         {       \r
84                                 BOOST_FOREACH(auto& frame, frames)\r
85                                 {\r
86                                         auto blend_it = blend_modes_.find(frame.first);\r
87                                         image_mixer_->begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);\r
88                                                                                                         \r
89                                         frame.second.accept(audio_mixer_);                                      \r
90                                         frame.second.accept(*image_mixer_);\r
91 \r
92                                         image_mixer_->end_layer();\r
93                                 }\r
94                                 \r
95                                 auto image = (*image_mixer_)(format_desc);\r
96                                 auto audio = audio_mixer_(format_desc);\r
97 \r
98                                 auto desc = core::pixel_format_desc(core::pixel_format::bgra);\r
99                                 desc.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));\r
100                                 return const_frame(std::move(image), std::move(audio), this, desc);     \r
101                         }\r
102                         catch(...)\r
103                         {\r
104                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
105                                 return const_frame::empty();\r
106                         }       \r
107                 });             \r
108                                 \r
109                 graph_->set_value("mix-time", frame_timer.elapsed()*format_desc.fps*0.5);\r
110 \r
111                 return frame;\r
112         }\r
113                                         \r
114         void set_blend_mode(int index, blend_mode value)\r
115         {\r
116                 executor_.begin_invoke([=]\r
117                 {\r
118                         auto it = blend_modes_.find(index);\r
119                         if(it == blend_modes_.end())\r
120                                 blend_modes_.insert(std::make_pair(index, value));\r
121                         else\r
122                                 it->second = value;\r
123                 }, task_priority::high_priority);\r
124         }\r
125         \r
126         boost::unique_future<boost::property_tree::wptree> info() const\r
127         {\r
128                 boost::promise<boost::property_tree::wptree> info;\r
129                 info.set_value(boost::property_tree::wptree());\r
130                 return info.get_future();\r
131         }\r
132 };\r
133         \r
134 mixer::mixer(spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer) \r
135         : impl_(new impl(std::move(graph), std::move(image_mixer))){}\r
136 void mixer::set_blend_mode(int index, blend_mode value){impl_->set_blend_mode(index, value);}\r
137 boost::unique_future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}\r
138 const_frame mixer::operator()(std::map<int, draw_frame> frames, const struct video_format_desc& format_desc){return (*impl_)(std::move(frames), format_desc);}\r
139 mutable_frame mixer::create_frame(const void* tag, const core::pixel_format_desc& desc) {return impl_->image_mixer_->create_frame(tag, desc);}\r
140 }}