1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/audio_mixer/audio_mixer_impl.h"
12
13 #include <stdint.h>
14
15 #include <algorithm>
16 #include <iterator>
17 #include <type_traits>
18 #include <utility>
19
20 #include "modules/audio_mixer/audio_frame_manipulator.h"
21 #include "modules/audio_mixer/default_output_rate_calculator.h"
22 #include "rtc_base/checks.h"
23 #include "rtc_base/logging.h"
24 #include "rtc_base/ref_counted_object.h"
25
26 namespace webrtc {
27 namespace {
28
29 struct SourceFrame {
SourceFramewebrtc::__anon902c70160111::SourceFrame30 SourceFrame(AudioMixerImpl::SourceStatus* source_status,
31 AudioFrame* audio_frame,
32 bool muted)
33 : source_status(source_status), audio_frame(audio_frame), muted(muted) {
34 RTC_DCHECK(source_status);
35 RTC_DCHECK(audio_frame);
36 if (!muted) {
37 energy = AudioMixerCalculateEnergy(*audio_frame);
38 }
39 }
40
SourceFramewebrtc::__anon902c70160111::SourceFrame41 SourceFrame(AudioMixerImpl::SourceStatus* source_status,
42 AudioFrame* audio_frame,
43 bool muted,
44 uint32_t energy)
45 : source_status(source_status),
46 audio_frame(audio_frame),
47 muted(muted),
48 energy(energy) {
49 RTC_DCHECK(source_status);
50 RTC_DCHECK(audio_frame);
51 }
52
53 AudioMixerImpl::SourceStatus* source_status = nullptr;
54 AudioFrame* audio_frame = nullptr;
55 bool muted = true;
56 uint32_t energy = 0;
57 };
58
59 // ShouldMixBefore(a, b) is used to select mixer sources.
ShouldMixBefore(const SourceFrame & a,const SourceFrame & b)60 bool ShouldMixBefore(const SourceFrame& a, const SourceFrame& b) {
61 if (a.muted != b.muted) {
62 return b.muted;
63 }
64
65 const auto a_activity = a.audio_frame->vad_activity_;
66 const auto b_activity = b.audio_frame->vad_activity_;
67
68 if (a_activity != b_activity) {
69 return a_activity == AudioFrame::kVadActive;
70 }
71
72 return a.energy > b.energy;
73 }
74
RampAndUpdateGain(const std::vector<SourceFrame> & mixed_sources_and_frames)75 void RampAndUpdateGain(
76 const std::vector<SourceFrame>& mixed_sources_and_frames) {
77 for (const auto& source_frame : mixed_sources_and_frames) {
78 float target_gain = source_frame.source_status->is_mixed ? 1.0f : 0.0f;
79 Ramp(source_frame.source_status->gain, target_gain,
80 source_frame.audio_frame);
81 source_frame.source_status->gain = target_gain;
82 }
83 }
84
FindSourceInList(AudioMixerImpl::Source const * audio_source,AudioMixerImpl::SourceStatusList const * audio_source_list)85 AudioMixerImpl::SourceStatusList::const_iterator FindSourceInList(
86 AudioMixerImpl::Source const* audio_source,
87 AudioMixerImpl::SourceStatusList const* audio_source_list) {
88 return std::find_if(
89 audio_source_list->begin(), audio_source_list->end(),
90 [audio_source](const std::unique_ptr<AudioMixerImpl::SourceStatus>& p) {
91 return p->audio_source == audio_source;
92 });
93 }
94 } // namespace
95
AudioMixerImpl(std::unique_ptr<OutputRateCalculator> output_rate_calculator,bool use_limiter)96 AudioMixerImpl::AudioMixerImpl(
97 std::unique_ptr<OutputRateCalculator> output_rate_calculator,
98 bool use_limiter)
99 : output_rate_calculator_(std::move(output_rate_calculator)),
100 output_frequency_(0),
101 sample_size_(0),
102 audio_source_list_(),
103 frame_combiner_(use_limiter) {}
104
~AudioMixerImpl()105 AudioMixerImpl::~AudioMixerImpl() {}
106
Create()107 rtc::scoped_refptr<AudioMixerImpl> AudioMixerImpl::Create() {
108 return Create(std::unique_ptr<DefaultOutputRateCalculator>(
109 new DefaultOutputRateCalculator()),
110 true);
111 }
112
Create(std::unique_ptr<OutputRateCalculator> output_rate_calculator,bool use_limiter)113 rtc::scoped_refptr<AudioMixerImpl> AudioMixerImpl::Create(
114 std::unique_ptr<OutputRateCalculator> output_rate_calculator,
115 bool use_limiter) {
116 return rtc::scoped_refptr<AudioMixerImpl>(
117 new rtc::RefCountedObject<AudioMixerImpl>(
118 std::move(output_rate_calculator), use_limiter));
119 }
120
Mix(size_t number_of_channels,AudioFrame * audio_frame_for_mixing)121 void AudioMixerImpl::Mix(size_t number_of_channels,
122 AudioFrame* audio_frame_for_mixing) {
123 RTC_DCHECK(number_of_channels >= 1);
124 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
125
126 CalculateOutputFrequency();
127
128 {
129 MutexLock lock(&mutex_);
130 const size_t number_of_streams = audio_source_list_.size();
131 frame_combiner_.Combine(GetAudioFromSources(), number_of_channels,
132 OutputFrequency(), number_of_streams,
133 audio_frame_for_mixing);
134 }
135
136 return;
137 }
138
CalculateOutputFrequency()139 void AudioMixerImpl::CalculateOutputFrequency() {
140 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
141 MutexLock lock(&mutex_);
142
143 std::vector<int> preferred_rates;
144 std::transform(audio_source_list_.begin(), audio_source_list_.end(),
145 std::back_inserter(preferred_rates),
146 [&](std::unique_ptr<SourceStatus>& a) {
147 return a->audio_source->PreferredSampleRate();
148 });
149
150 output_frequency_ =
151 output_rate_calculator_->CalculateOutputRate(preferred_rates);
152 sample_size_ = (output_frequency_ * kFrameDurationInMs) / 1000;
153 }
154
OutputFrequency() const155 int AudioMixerImpl::OutputFrequency() const {
156 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
157 return output_frequency_;
158 }
159
AddSource(Source * audio_source)160 bool AudioMixerImpl::AddSource(Source* audio_source) {
161 RTC_DCHECK(audio_source);
162 MutexLock lock(&mutex_);
163 RTC_DCHECK(FindSourceInList(audio_source, &audio_source_list_) ==
164 audio_source_list_.end())
165 << "Source already added to mixer";
166 audio_source_list_.emplace_back(new SourceStatus(audio_source, false, 0));
167 return true;
168 }
169
RemoveSource(Source * audio_source)170 void AudioMixerImpl::RemoveSource(Source* audio_source) {
171 RTC_DCHECK(audio_source);
172 MutexLock lock(&mutex_);
173 const auto iter = FindSourceInList(audio_source, &audio_source_list_);
174 RTC_DCHECK(iter != audio_source_list_.end()) << "Source not present in mixer";
175 audio_source_list_.erase(iter);
176 }
177
GetAudioFromSources()178 AudioFrameList AudioMixerImpl::GetAudioFromSources() {
179 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
180 AudioFrameList result;
181 std::vector<SourceFrame> audio_source_mixing_data_list;
182 std::vector<SourceFrame> ramp_list;
183
184 // Get audio from the audio sources and put it in the SourceFrame vector.
185 for (auto& source_and_status : audio_source_list_) {
186 const auto audio_frame_info =
187 source_and_status->audio_source->GetAudioFrameWithInfo(
188 OutputFrequency(), &source_and_status->audio_frame);
189
190 if (audio_frame_info == Source::AudioFrameInfo::kError) {
191 RTC_LOG_F(LS_WARNING) << "failed to GetAudioFrameWithInfo() from source";
192 continue;
193 }
194 audio_source_mixing_data_list.emplace_back(
195 source_and_status.get(), &source_and_status->audio_frame,
196 audio_frame_info == Source::AudioFrameInfo::kMuted);
197 }
198
199 // Sort frames by sorting function.
200 std::sort(audio_source_mixing_data_list.begin(),
201 audio_source_mixing_data_list.end(), ShouldMixBefore);
202
203 int max_audio_frame_counter = kMaximumAmountOfMixedAudioSources;
204
205 // Go through list in order and put unmuted frames in result list.
206 for (const auto& p : audio_source_mixing_data_list) {
207 // Filter muted.
208 if (p.muted) {
209 p.source_status->is_mixed = false;
210 continue;
211 }
212
213 // Add frame to result vector for mixing.
214 bool is_mixed = false;
215 if (max_audio_frame_counter > 0) {
216 --max_audio_frame_counter;
217 result.push_back(p.audio_frame);
218 ramp_list.emplace_back(p.source_status, p.audio_frame, false, -1);
219 is_mixed = true;
220 }
221 p.source_status->is_mixed = is_mixed;
222 }
223 RampAndUpdateGain(ramp_list);
224 return result;
225 }
226
GetAudioSourceMixabilityStatusForTest(AudioMixerImpl::Source * audio_source) const227 bool AudioMixerImpl::GetAudioSourceMixabilityStatusForTest(
228 AudioMixerImpl::Source* audio_source) const {
229 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
230 MutexLock lock(&mutex_);
231
232 const auto iter = FindSourceInList(audio_source, &audio_source_list_);
233 if (iter != audio_source_list_.end()) {
234 return (*iter)->is_mixed;
235 }
236
237 RTC_LOG(LS_ERROR) << "Audio source unknown";
238 return false;
239 }
240 } // namespace webrtc
241