]> git.sesse.net Git - casparcg/blob - core/mixer/mixer.cpp
Merged INFO THREADS from 2.0
[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/future.h>
36 #include <common/timer.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/property_tree/ptree.hpp>
45 #include <boost/lexical_cast.hpp>
46
47 #include <tbb/concurrent_queue.h>
48 #include <tbb/spin_mutex.h>
49
50 #include <unordered_map>
51 #include <vector>
52
53 namespace caspar { namespace core {
54
55 struct mixer::impl : boost::noncopyable
56 {
57         int                                                                     channel_index_;
58         spl::shared_ptr<diagnostics::graph> graph_;
59         audio_mixer                                                     audio_mixer_;
60         spl::shared_ptr<image_mixer>            image_mixer_;
61                         
62         executor                                                        executor_               { L"mixer " + boost::lexical_cast<std::wstring>(channel_index_) };
63
64 public:
65         impl(int channel_index, spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer) 
66                 : channel_index_(channel_index)
67                 , graph_(std::move(graph))
68                 , image_mixer_(std::move(image_mixer))
69         {                       
70                 graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8f));
71         }
72         
73         const_frame operator()(std::map<int, draw_frame> frames, const video_format_desc& format_desc)
74         {               
75                 caspar::timer frame_timer;
76
77                 auto frame = executor_.invoke([=]() mutable -> const_frame
78                 {               
79                         try
80                         {
81                                 detail::set_current_aspect_ratio(
82                                                 static_cast<double>(format_desc.square_width)
83                                                 / static_cast<double>(format_desc.square_height));
84
85                                 for (auto& frame : frames)
86                                 {
87                                         frame.second.accept(audio_mixer_);
88                                         frame.second.transform().image_transform.layer_depth = 1;
89                                         frame.second.accept(*image_mixer_);
90                                 }
91                                 
92                                 auto image = (*image_mixer_)(format_desc);
93                                 auto audio = audio_mixer_(format_desc);
94
95                                 auto desc = core::pixel_format_desc(core::pixel_format::bgra);
96                                 desc.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));
97                                 return const_frame(std::move(image), std::move(audio), this, desc);     
98                         }
99                         catch(...)
100                         {
101                                 CASPAR_LOG_CURRENT_EXCEPTION();
102                                 return const_frame::empty();
103                         }       
104                 });             
105                                 
106                 graph_->set_value("mix-time", frame_timer.elapsed()*format_desc.fps*0.5);
107
108                 return frame;
109         }
110
111         void set_master_volume(float volume)
112         {
113                 executor_.begin_invoke([=]
114                 {
115                         audio_mixer_.set_master_volume(volume);
116                 }, task_priority::high_priority);
117         }
118
119         float get_master_volume()
120         {
121                 return executor_.invoke([=]
122                 {
123                         return audio_mixer_.get_master_volume();
124                 }, task_priority::high_priority);
125         }
126
127         std::future<boost::property_tree::wptree> info() const
128         {
129                 return make_ready_future(boost::property_tree::wptree());
130         }
131 };
132         
133 mixer::mixer(int channel_index, spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer) 
134         : impl_(new impl(channel_index, std::move(graph), std::move(image_mixer))){}
135 void mixer::set_master_volume(float volume) { impl_->set_master_volume(volume); }
136 float mixer::get_master_volume() { return impl_->get_master_volume(); }
137 std::future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}
138 const_frame mixer::operator()(std::map<int, draw_frame> frames, const video_format_desc& format_desc){return (*impl_)(std::move(frames), format_desc);}
139 mutable_frame mixer::create_frame(const void* tag, const core::pixel_format_desc& desc) {return impl_->image_mixer_->create_frame(tag, desc);}
140 }}