]> git.sesse.net Git - casparcg/blob - core/mixer/mixer.cpp
Refactored to use non-static data member initializers where it makes sense. Mixes...
[casparcg] / core / mixer / mixer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "../StdAfx.h"
23
24 #include "mixer.h"
25
26 #include "../frame/frame.h"
27
28 #include "audio/audio_mixer.h"
29 #include "image/image_mixer.h"
30
31 #include <common/env.h>
32 #include <common/executor.h>
33 #include <common/diagnostics/graph.h>
34 #include <common/except.h>
35 #include <common/gl/gl_check.h>
36 #include <common/future.h>
37
38 #include <core/frame/draw_frame.h>
39 #include <core/frame/frame_factory.h>
40 #include <core/frame/frame_transform.h>
41 #include <core/frame/pixel_format.h>
42 #include <core/video_format.h>
43
44 #include <boost/timer.hpp>
45 #include <boost/property_tree/ptree.hpp>
46 #include <boost/range/algorithm_ext.hpp>
47
48 #include <tbb/concurrent_queue.h>
49 #include <tbb/spin_mutex.h>
50
51 #include <unordered_map>
52 #include <vector>
53
54 namespace caspar { namespace core {
55
56 struct mixer::impl : boost::noncopyable
57 {                               
58         spl::shared_ptr<diagnostics::graph> graph_;
59         audio_mixer                                                     audio_mixer_;
60         spl::shared_ptr<image_mixer>            image_mixer_;
61         
62         std::unordered_map<int, blend_mode>     blend_modes_;
63                         
64         executor executor_                                                                      = L"mixer";
65
66 public:
67         impl(spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer) 
68                 : graph_(std::move(graph))
69                 , image_mixer_(std::move(image_mixer))
70         {                       
71                 graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8));
72         }       
73         
74         const_frame operator()(std::map<int, draw_frame> frames, const video_format_desc& format_desc)
75         {               
76                 boost::timer frame_timer;
77
78                 auto frame = executor_.invoke([=]() mutable -> const_frame
79                 {               
80                         try
81                         {       
82                                 for (auto& frame : frames)
83                                 {
84                                         auto blend_it = blend_modes_.find(frame.first);
85                                         image_mixer_->begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);
86                                                                                                         
87                                         frame.second.accept(audio_mixer_);                                      
88                                         frame.second.accept(*image_mixer_);
89
90                                         image_mixer_->end_layer();
91                                 }
92                                 
93                                 auto image = (*image_mixer_)(format_desc);
94                                 auto audio = audio_mixer_(format_desc);
95
96                                 auto desc = core::pixel_format_desc(core::pixel_format::bgra);
97                                 desc.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));
98                                 return const_frame(std::move(image), std::move(audio), this, desc);     
99                         }
100                         catch(...)
101                         {
102                                 CASPAR_LOG_CURRENT_EXCEPTION();
103                                 return const_frame::empty();
104                         }       
105                 });             
106                                 
107                 graph_->set_value("mix-time", frame_timer.elapsed()*format_desc.fps*0.5);
108
109                 return frame;
110         }
111                                         
112         void set_blend_mode(int index, blend_mode value)
113         {
114                 executor_.begin_invoke([=]
115                 {
116                         auto it = blend_modes_.find(index);
117                         if(it == blend_modes_.end())
118                                 blend_modes_.insert(std::make_pair(index, value));
119                         else
120                                 it->second = value;
121                 }, task_priority::high_priority);
122         }
123
124         void clear_blend_mode(int index)
125         {
126                 executor_.begin_invoke([=]
127                 {
128                         blend_modes_.erase(index);
129                 }, task_priority::high_priority);
130         }
131
132         void clear_blend_modes()
133         {
134                 executor_.begin_invoke([=]
135                 {
136                         blend_modes_.clear();
137                 }, task_priority::high_priority);
138         }
139
140         void set_master_volume(float volume)
141         {
142                 executor_.begin_invoke([=]
143                 {
144                         audio_mixer_.set_master_volume(volume);
145                 }, task_priority::high_priority);
146         }
147
148         std::future<boost::property_tree::wptree> info() const
149         {
150                 return make_ready_future(boost::property_tree::wptree());
151         }
152 };
153         
154 mixer::mixer(spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer) 
155         : impl_(new impl(std::move(graph), std::move(image_mixer))){}
156 void mixer::set_blend_mode(int index, blend_mode value){impl_->set_blend_mode(index, value);}
157 void mixer::clear_blend_mode(int index) { impl_->clear_blend_mode(index); }
158 void mixer::clear_blend_modes() { impl_->clear_blend_modes(); }
159 void mixer::set_master_volume(float volume) { impl_->set_master_volume(volume); }
160 std::future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}
161 const_frame mixer::operator()(std::map<int, draw_frame> frames, const struct video_format_desc& format_desc){return (*impl_)(std::move(frames), format_desc);}
162 mutable_frame mixer::create_frame(const void* tag, const core::pixel_format_desc& desc) {return impl_->image_mixer_->create_frame(tag, desc);}
163 }}