2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
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.
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.
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/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "../StdAfx.h"
26 #include "../frame/frame.h"
28 #include "audio/audio_mixer.h"
29 #include "image/image_mixer.h"
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>
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/frame/audio_channel_layout.h>
43 #include <core/video_format.h>
45 #include <boost/property_tree/ptree.hpp>
46 #include <boost/lexical_cast.hpp>
48 #include <tbb/concurrent_queue.h>
49 #include <tbb/spin_mutex.h>
50 #include <tbb/atomic.h>
52 #include <unordered_map>
55 namespace caspar { namespace core {
57 struct mixer::impl : boost::noncopyable
60 spl::shared_ptr<diagnostics::graph> graph_;
61 tbb::atomic<int64_t> current_mix_time_;
62 spl::shared_ptr<monitor::subject> monitor_subject_ = spl::make_shared<monitor::subject>("/mixer");
63 audio_mixer audio_mixer_ { graph_ };
64 spl::shared_ptr<image_mixer> image_mixer_;
66 bool straighten_alpha_ = false;
68 executor executor_ { L"mixer " + boost::lexical_cast<std::wstring>(channel_index_) };
71 impl(int channel_index, spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer)
72 : channel_index_(channel_index)
73 , graph_(std::move(graph))
74 , image_mixer_(std::move(image_mixer))
76 graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8f));
77 current_mix_time_ = 0;
78 audio_mixer_.monitor_output().attach_parent(monitor_subject_);
81 const_frame operator()(std::map<int, draw_frame> frames, const video_format_desc& format_desc, const core::audio_channel_layout& channel_layout)
83 caspar::timer frame_timer;
85 auto frame = executor_.invoke([=]() mutable -> const_frame
89 CASPAR_SCOPED_CONTEXT_MSG(L" '" + executor_.name() + L"' ");
91 detail::set_current_aspect_ratio(
92 static_cast<double>(format_desc.square_width)
93 / static_cast<double>(format_desc.square_height));
95 for (auto& frame : frames)
97 frame.second.accept(audio_mixer_);
98 frame.second.transform().image_transform.layer_depth = 1;
99 frame.second.accept(*image_mixer_);
102 auto image = (*image_mixer_)(format_desc, straighten_alpha_);
103 auto audio = audio_mixer_(format_desc, channel_layout);
105 auto desc = core::pixel_format_desc(core::pixel_format::bgra);
106 desc.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));
107 return const_frame(std::move(image), std::move(audio), this, desc, channel_layout);
111 CASPAR_LOG_CURRENT_EXCEPTION();
112 return const_frame::empty();
116 auto mix_time = frame_timer.elapsed();
117 graph_->set_value("mix-time", mix_time * format_desc.fps * 0.5);
118 current_mix_time_ = static_cast<int64_t>(mix_time * 1000.0);
123 void set_master_volume(float volume)
125 executor_.begin_invoke([=]
127 audio_mixer_.set_master_volume(volume);
128 }, task_priority::high_priority);
131 float get_master_volume()
133 return executor_.invoke([=]
135 return audio_mixer_.get_master_volume();
136 }, task_priority::high_priority);
139 void set_straight_alpha_output(bool value)
141 executor_.begin_invoke([=]
143 straighten_alpha_ = value;
144 }, task_priority::high_priority);
147 bool get_straight_alpha_output()
149 return executor_.invoke([=]
151 return straighten_alpha_;
152 }, task_priority::high_priority);
155 std::future<boost::property_tree::wptree> info() const
157 boost::property_tree::wptree info;
158 info.add(L"mix-time", current_mix_time_);
160 return make_ready_future(std::move(info));
163 std::future<boost::property_tree::wptree> delay_info() const
165 boost::property_tree::wptree info;
166 info.put_value(current_mix_time_);
168 return make_ready_future(std::move(info));
172 mixer::mixer(int channel_index, spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer)
173 : impl_(new impl(channel_index, std::move(graph), std::move(image_mixer))){}
174 void mixer::set_master_volume(float volume) { impl_->set_master_volume(volume); }
175 float mixer::get_master_volume() { return impl_->get_master_volume(); }
176 void mixer::set_straight_alpha_output(bool value) { impl_->set_straight_alpha_output(value); }
177 bool mixer::get_straight_alpha_output() { return impl_->get_straight_alpha_output(); }
178 std::future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}
179 std::future<boost::property_tree::wptree> mixer::delay_info() const{ return impl_->delay_info(); }
180 const_frame mixer::operator()(std::map<int, draw_frame> frames, const video_format_desc& format_desc, const core::audio_channel_layout& channel_layout){ return (*impl_)(std::move(frames), format_desc, channel_layout); }
181 mutable_frame mixer::create_frame(const void* tag, const core::pixel_format_desc& desc, const core::audio_channel_layout& channel_layout) {return impl_->image_mixer_->create_frame(tag, desc, channel_layout);}
182 monitor::subject& mixer::monitor_output() { return *impl_->monitor_subject_; }