]> git.sesse.net Git - casparcg/blob - core/mixer/mixer.cpp
750c9bbb06759325303609a6fa8ab9fd900961e6
[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 "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/exception/exceptions.h>\r
35 #include <common/gl/gl_check.h>\r
36 #include <common/utility/tweener.h>\r
37 \r
38 #include <core/mixer/read_frame.h>\r
39 #include <core/mixer/write_frame.h>\r
40 #include <core/producer/frame/basic_frame.h>\r
41 #include <core/producer/frame/frame_factory.h>\r
42 #include <core/producer/frame/frame_transform.h>\r
43 #include <core/producer/frame/pixel_format.h>\r
44 \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 \r
51 #include <tbb/concurrent_queue.h>\r
52 #include <tbb/spin_mutex.h>\r
53 \r
54 #include <unordered_map>\r
55 \r
56 namespace caspar { namespace core {\r
57                 \r
58 struct mixer::implementation : boost::noncopyable\r
59 {               \r
60         safe_ptr<diagnostics::graph>    graph_;\r
61         boost::timer                                    mix_timer_;\r
62 \r
63         safe_ptr<mixer::target_t>               target_;\r
64         mutable tbb::spin_mutex                 format_desc_mutex_;\r
65         video_format_desc                               format_desc_;\r
66         safe_ptr<ogl_device>                    ogl_;\r
67         \r
68         audio_mixer     audio_mixer_;\r
69         image_mixer image_mixer_;\r
70         \r
71         std::unordered_map<int, blend_mode::type> blend_modes_;\r
72                         \r
73         executor executor_;\r
74 \r
75 public:\r
76         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) \r
77                 : graph_(graph)\r
78                 , target_(target)\r
79                 , format_desc_(format_desc)\r
80                 , ogl_(ogl)\r
81                 , image_mixer_(ogl)\r
82                 , audio_mixer_(graph_)\r
83                 , executor_(L"mixer")\r
84         {                       \r
85                 graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f));\r
86         }\r
87         \r
88         void send(const std::pair<std::map<int, safe_ptr<core::basic_frame>>, std::shared_ptr<void>>& packet)\r
89         {                       \r
90                 executor_.begin_invoke([=]\r
91                 {               \r
92                         try\r
93                         {\r
94                                 mix_timer_.restart();\r
95 \r
96                                 auto frames = packet.first;\r
97                                 \r
98                                 BOOST_FOREACH(auto& frame, frames)\r
99                                 {\r
100                                         auto blend_it = blend_modes_.find(frame.first);\r
101                                         image_mixer_.begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);\r
102                                                                                                         \r
103                                         frame.second->accept(audio_mixer_);                                     \r
104                                         frame.second->accept(image_mixer_);\r
105 \r
106                                         image_mixer_.end_layer();\r
107                                 }\r
108 \r
109                                 auto image = image_mixer_(format_desc_);\r
110                                 auto audio = audio_mixer_(format_desc_);\r
111                                 image.wait();\r
112 \r
113                                 graph_->update_value("mix-time", mix_timer_.elapsed()*format_desc_.fps*0.5);\r
114 \r
115                                 target_->send(std::make_pair(make_safe<read_frame>(ogl_, format_desc_.size, std::move(image.get()), std::move(audio)), packet.second));                                 \r
116                         }\r
117                         catch(...)\r
118                         {\r
119                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
120                         }       \r
121                 });             \r
122         }\r
123                                         \r
124         safe_ptr<core::write_frame> create_frame(const void* tag, const core::pixel_format_desc& desc)\r
125         {               \r
126                 return make_safe<write_frame>(ogl_, tag, desc);\r
127         }\r
128                                 \r
129         void set_blend_mode(int index, blend_mode::type value)\r
130         {\r
131                 executor_.begin_invoke([=]\r
132                 {\r
133                         blend_modes_[index] = value;\r
134                 }, high_priority);\r
135         }\r
136         \r
137         void set_video_format_desc(const video_format_desc& format_desc)\r
138         {\r
139                 executor_.begin_invoke([=]\r
140                 {\r
141                         tbb::spin_mutex::scoped_lock lock(format_desc_mutex_);\r
142                         format_desc_ = format_desc;\r
143                 });\r
144         }\r
145 \r
146         core::video_format_desc get_video_format_desc() const // nothrow\r
147         {\r
148                 tbb::spin_mutex::scoped_lock lock(format_desc_mutex_);\r
149                 return format_desc_;\r
150         }\r
151 \r
152         boost::unique_future<boost::property_tree::wptree> info() const\r
153         {\r
154                 boost::promise<boost::property_tree::wptree> info;\r
155                 info.set_value(boost::property_tree::wptree());\r
156                 return info.get_future();\r
157         }\r
158 };\r
159         \r
160 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) \r
161         : impl_(new implementation(graph, target, format_desc, ogl)){}\r
162 void mixer::send(const std::pair<std::map<int, safe_ptr<core::basic_frame>>, std::shared_ptr<void>>& frames){ impl_->send(frames);}\r
163 core::video_format_desc mixer::get_video_format_desc() const { return impl_->get_video_format_desc(); }\r
164 safe_ptr<core::write_frame> mixer::create_frame(const void* tag, const core::pixel_format_desc& desc){ return impl_->create_frame(tag, desc); }         \r
165 void mixer::set_blend_mode(int index, blend_mode::type value){impl_->set_blend_mode(index, value);}\r
166 void mixer::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}\r
167 boost::unique_future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}\r
168 }}