blob: e570e224992e17c6924a88b5915448cd9e228902 [file] [log] [blame]
aleloi77ad3942016-07-04 06:33:02 -07001/*
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
aleloi5d167d62016-08-24 02:20:54 -070011#include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
aleloi77ad3942016-07-04 06:33:02 -070012
13#include <algorithm>
aleloif3882572016-07-29 02:12:41 -070014#include <functional>
aleloi311525e2016-09-07 06:13:12 -070015#include <utility>
aleloi77ad3942016-07-04 06:33:02 -070016
aleloi36542512016-10-07 05:28:32 -070017#include "webrtc/base/logging.h"
aleloi5bcc00e2016-08-15 03:01:31 -070018#include "webrtc/modules/audio_mixer/audio_frame_manipulator.h"
aleloi77ad3942016-07-04 06:33:02 -070019#include "webrtc/modules/utility/include/audio_frame_operations.h"
aleloi77ad3942016-07-04 06:33:02 -070020
21namespace webrtc {
22namespace {
23
aleloi4b8bfb82016-10-12 02:14:59 -070024struct SourceFrame {
25 SourceFrame(AudioMixerImpl::SourceStatus* source_status,
aleloi36542512016-10-07 05:28:32 -070026 AudioFrame* audio_frame,
27 bool muted)
aleloi4b8bfb82016-10-12 02:14:59 -070028 : source_status(source_status), audio_frame(audio_frame), muted(muted) {
29 RTC_DCHECK(source_status);
30 RTC_DCHECK(audio_frame);
31 if (!muted) {
32 energy = AudioMixerCalculateEnergy(*audio_frame);
aleloif3882572016-07-29 02:12:41 -070033 }
34 }
aleloi77ad3942016-07-04 06:33:02 -070035
aleloi4b8bfb82016-10-12 02:14:59 -070036 SourceFrame(AudioMixerImpl::SourceStatus* source_status,
aleloi36542512016-10-07 05:28:32 -070037 AudioFrame* audio_frame,
38 bool muted,
aleloi652ac892016-09-07 07:42:14 -070039 uint32_t energy)
aleloi4b8bfb82016-10-12 02:14:59 -070040 : source_status(source_status),
41 audio_frame(audio_frame),
42 muted(muted),
43 energy(energy) {
44 RTC_DCHECK(source_status);
45 RTC_DCHECK(audio_frame);
aleloif3882572016-07-29 02:12:41 -070046 }
47
aleloi4b8bfb82016-10-12 02:14:59 -070048 AudioMixerImpl::SourceStatus* source_status = nullptr;
49 AudioFrame* audio_frame = nullptr;
50 bool muted = true;
51 uint32_t energy = 0;
aleloif3882572016-07-29 02:12:41 -070052};
tereliusea4c1412016-07-29 01:36:14 -070053
aleloi4b8bfb82016-10-12 02:14:59 -070054// ShouldMixBefore(a, b) is used to select mixer sources.
55bool ShouldMixBefore(const SourceFrame& a, const SourceFrame& b) {
56 if (a.muted != b.muted) {
57 return b.muted;
58 }
aleloi44968092016-08-08 10:18:58 -070059
aleloi4b8bfb82016-10-12 02:14:59 -070060 const auto a_activity = a.audio_frame->vad_activity_;
61 const auto b_activity = b.audio_frame->vad_activity_;
62
63 if (a_activity != b_activity) {
64 return a_activity == AudioFrame::kVadActive;
65 }
66
67 return a.energy > b.energy;
68}
69
70void RampAndUpdateGain(
71 const std::vector<SourceFrame>& mixed_sources_and_frames) {
aleloi652ac892016-09-07 07:42:14 -070072 for (const auto& source_frame : mixed_sources_and_frames) {
aleloi4b8bfb82016-10-12 02:14:59 -070073 float target_gain = source_frame.source_status->is_mixed ? 1.0f : 0.0f;
74 Ramp(source_frame.source_status->gain, target_gain,
75 source_frame.audio_frame);
76 source_frame.source_status->gain = target_gain;
aleloi77ad3942016-07-04 06:33:02 -070077 }
aleloi77ad3942016-07-04 06:33:02 -070078}
79
aleloidc7669a2016-10-04 04:06:20 -070080// Mix the AudioFrames stored in audioFrameList into mixed_audio.
81int32_t MixFromList(AudioFrame* mixed_audio,
82 const AudioFrameList& audio_frame_list,
aleloidc7669a2016-10-04 04:06:20 -070083 bool use_limiter) {
aleloie97974d2016-10-12 03:06:09 -070084 if (audio_frame_list.empty()) {
aleloidc7669a2016-10-04 04:06:20 -070085 return 0;
aleloie97974d2016-10-12 03:06:09 -070086 }
aleloi77ad3942016-07-04 06:33:02 -070087
aleloidc7669a2016-10-04 04:06:20 -070088 if (audio_frame_list.size() == 1) {
89 mixed_audio->timestamp_ = audio_frame_list.front()->timestamp_;
90 mixed_audio->elapsed_time_ms_ = audio_frame_list.front()->elapsed_time_ms_;
91 } else {
92 // TODO(wu): Issue 3390.
93 // Audio frame timestamp is only supported in one channel case.
94 mixed_audio->timestamp_ = 0;
95 mixed_audio->elapsed_time_ms_ = -1;
96 }
aleloi77ad3942016-07-04 06:33:02 -070097
aleloidc7669a2016-10-04 04:06:20 -070098 for (const auto& frame : audio_frame_list) {
99 RTC_DCHECK_EQ(mixed_audio->sample_rate_hz_, frame->sample_rate_hz_);
100 RTC_DCHECK_EQ(
101 frame->samples_per_channel_,
102 static_cast<size_t>((mixed_audio->sample_rate_hz_ *
103 webrtc::AudioMixerImpl::kFrameDurationInMs) /
104 1000));
aleloi77ad3942016-07-04 06:33:02 -0700105
aleloidc7669a2016-10-04 04:06:20 -0700106 // Mix |f.frame| into |mixed_audio|, with saturation protection.
107 // These effect is applied to |f.frame| itself prior to mixing.
108 if (use_limiter) {
109 // Divide by two to avoid saturation in the mixing.
110 // This is only meaningful if the limiter will be used.
111 *frame >>= 1;
112 }
113 RTC_DCHECK_EQ(frame->num_channels_, mixed_audio->num_channels_);
114 *mixed_audio += *frame;
115 }
aleloi77ad3942016-07-04 06:33:02 -0700116 return 0;
117}
118
aleloi4b8bfb82016-10-12 02:14:59 -0700119AudioMixerImpl::SourceStatusList::const_iterator FindSourceInList(
aleloie8914152016-10-11 06:18:31 -0700120 AudioMixerImpl::Source const* audio_source,
aleloi4b8bfb82016-10-12 02:14:59 -0700121 AudioMixerImpl::SourceStatusList const* audio_source_list) {
aleloi36542512016-10-07 05:28:32 -0700122 return std::find_if(audio_source_list->begin(), audio_source_list->end(),
aleloi4b8bfb82016-10-12 02:14:59 -0700123 [audio_source](const AudioMixerImpl::SourceStatus& p) {
124 return p.audio_source == audio_source;
aleloi36542512016-10-07 05:28:32 -0700125 });
126}
127
aleloie97974d2016-10-12 03:06:09 -0700128// TODO(aleloi): remove non-const version when WEBRTC only supports modern STL.
aleloi4b8bfb82016-10-12 02:14:59 -0700129AudioMixerImpl::SourceStatusList::iterator FindSourceInList(
aleloie8914152016-10-11 06:18:31 -0700130 AudioMixerImpl::Source const* audio_source,
aleloi4b8bfb82016-10-12 02:14:59 -0700131 AudioMixerImpl::SourceStatusList* audio_source_list) {
aleloi36542512016-10-07 05:28:32 -0700132 return std::find_if(audio_source_list->begin(), audio_source_list->end(),
aleloi4b8bfb82016-10-12 02:14:59 -0700133 [audio_source](const AudioMixerImpl::SourceStatus& p) {
134 return p.audio_source == audio_source;
aleloi36542512016-10-07 05:28:32 -0700135 });
136}
137
aleloidc7669a2016-10-04 04:06:20 -0700138} // namespace
aleloi77ad3942016-07-04 06:33:02 -0700139
aleloie97974d2016-10-12 03:06:09 -0700140std::unique_ptr<AudioMixer> AudioMixer::Create() {
141 return AudioMixerImpl::Create();
aleloi77ad3942016-07-04 06:33:02 -0700142}
143
aleloie97974d2016-10-12 03:06:09 -0700144AudioMixerImpl::AudioMixerImpl(std::unique_ptr<AudioProcessing> limiter)
145 : audio_source_list_(),
aleloi09f45102016-07-28 03:52:15 -0700146 num_mixed_audio_sources_(0),
aleloi77ad3942016-07-04 06:33:02 -0700147 use_limiter_(true),
aleloi311525e2016-09-07 06:13:12 -0700148 time_stamp_(0),
149 limiter_(std::move(limiter)) {
150 SetOutputFrequency(kDefaultFrequency);
aleloi8b2233f2016-07-28 06:24:14 -0700151 thread_checker_.DetachFromThread();
aleloia0db81f2016-07-28 06:36:22 -0700152}
aleloi77ad3942016-07-04 06:33:02 -0700153
aleloi5d167d62016-08-24 02:20:54 -0700154AudioMixerImpl::~AudioMixerImpl() {}
aleloi70f866c2016-08-16 02:15:49 -0700155
aleloie97974d2016-10-12 03:06:09 -0700156std::unique_ptr<AudioMixerImpl> AudioMixerImpl::Create() {
aleloi77ad3942016-07-04 06:33:02 -0700157 Config config;
158 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
aleloi311525e2016-09-07 06:13:12 -0700159 std::unique_ptr<AudioProcessing> limiter(AudioProcessing::Create(config));
aleloie97974d2016-10-12 03:06:09 -0700160 if (!limiter.get()) {
aleloi311525e2016-09-07 06:13:12 -0700161 return nullptr;
aleloie97974d2016-10-12 03:06:09 -0700162 }
aleloi77ad3942016-07-04 06:33:02 -0700163
aleloi311525e2016-09-07 06:13:12 -0700164 if (limiter->gain_control()->set_mode(GainControl::kFixedDigital) !=
aleloie97974d2016-10-12 03:06:09 -0700165 limiter->kNoError) {
aleloi311525e2016-09-07 06:13:12 -0700166 return nullptr;
aleloie97974d2016-10-12 03:06:09 -0700167 }
aleloi77ad3942016-07-04 06:33:02 -0700168
169 // We smoothly limit the mixed frame to -7 dbFS. -6 would correspond to the
170 // divide-by-2 but -7 is used instead to give a bit of headroom since the
171 // AGC is not a hard limiter.
aleloie97974d2016-10-12 03:06:09 -0700172 if (limiter->gain_control()->set_target_level_dbfs(7) != limiter->kNoError) {
aleloi311525e2016-09-07 06:13:12 -0700173 return nullptr;
aleloie97974d2016-10-12 03:06:09 -0700174 }
aleloi77ad3942016-07-04 06:33:02 -0700175
aleloie97974d2016-10-12 03:06:09 -0700176 if (limiter->gain_control()->set_compression_gain_db(0) !=
177 limiter->kNoError) {
aleloi311525e2016-09-07 06:13:12 -0700178 return nullptr;
aleloie97974d2016-10-12 03:06:09 -0700179 }
aleloi77ad3942016-07-04 06:33:02 -0700180
aleloie97974d2016-10-12 03:06:09 -0700181 if (limiter->gain_control()->enable_limiter(true) != limiter->kNoError) {
aleloi311525e2016-09-07 06:13:12 -0700182 return nullptr;
aleloie97974d2016-10-12 03:06:09 -0700183 }
aleloi77ad3942016-07-04 06:33:02 -0700184
aleloie97974d2016-10-12 03:06:09 -0700185 if (limiter->gain_control()->Enable(true) != limiter->kNoError) {
aleloi311525e2016-09-07 06:13:12 -0700186 return nullptr;
aleloie97974d2016-10-12 03:06:09 -0700187 }
aleloi77ad3942016-07-04 06:33:02 -0700188
aleloi36542512016-10-07 05:28:32 -0700189 return std::unique_ptr<AudioMixerImpl>(
aleloie97974d2016-10-12 03:06:09 -0700190 new AudioMixerImpl(std::move(limiter)));
aleloi77ad3942016-07-04 06:33:02 -0700191}
192
aleloi5d167d62016-08-24 02:20:54 -0700193void AudioMixerImpl::Mix(int sample_rate,
194 size_t number_of_channels,
195 AudioFrame* audio_frame_for_mixing) {
aleloi44968092016-08-08 10:18:58 -0700196 RTC_DCHECK(number_of_channels == 1 || number_of_channels == 2);
aleloi311525e2016-09-07 06:13:12 -0700197 RTC_DCHECK_RUN_ON(&thread_checker_);
aleloi311525e2016-09-07 06:13:12 -0700198
aleloi311525e2016-09-07 06:13:12 -0700199 if (OutputFrequency() != sample_rate) {
aleloie97974d2016-10-12 03:06:09 -0700200 SetOutputFrequency(sample_rate);
aleloi311525e2016-09-07 06:13:12 -0700201 }
202
aleloi652ac892016-09-07 07:42:14 -0700203 AudioFrameList mix_list;
aleloidc7669a2016-10-04 04:06:20 -0700204 size_t num_mixed_audio_sources;
aleloi77ad3942016-07-04 06:33:02 -0700205 {
aleloi311525e2016-09-07 06:13:12 -0700206 rtc::CritScope lock(&crit_);
aleloi652ac892016-09-07 07:42:14 -0700207 mix_list = GetNonAnonymousAudio();
aleloidc7669a2016-10-04 04:06:20 -0700208 num_mixed_audio_sources = num_mixed_audio_sources_;
aleloi77ad3942016-07-04 06:33:02 -0700209 }
210
aleloi652ac892016-09-07 07:42:14 -0700211 for (const auto& frame : mix_list) {
aleloie8914152016-10-11 06:18:31 -0700212 RemixFrame(number_of_channels, frame);
aleloi44968092016-08-08 10:18:58 -0700213 }
aleloi09f45102016-07-28 03:52:15 -0700214
215 audio_frame_for_mixing->UpdateFrame(
aleloia4c21062016-09-08 01:25:46 -0700216 -1, time_stamp_, NULL, 0, OutputFrequency(), AudioFrame::kNormalSpeech,
aleloi44968092016-08-08 10:18:58 -0700217 AudioFrame::kVadPassive, number_of_channels);
aleloi09f45102016-07-28 03:52:15 -0700218
aleloi6382a192016-08-08 10:25:04 -0700219 time_stamp_ += static_cast<uint32_t>(sample_size_);
aleloi09f45102016-07-28 03:52:15 -0700220
aleloi311525e2016-09-07 06:13:12 -0700221 use_limiter_ = num_mixed_audio_sources > 1;
aleloi09f45102016-07-28 03:52:15 -0700222
aleloi652ac892016-09-07 07:42:14 -0700223 // We only use the limiter if we're actually mixing multiple streams.
aleloie97974d2016-10-12 03:06:09 -0700224 MixFromList(audio_frame_for_mixing, mix_list, use_limiter_);
aleloi652ac892016-09-07 07:42:14 -0700225
aleloi311525e2016-09-07 06:13:12 -0700226 if (audio_frame_for_mixing->samples_per_channel_ == 0) {
227 // Nothing was mixed, set the audio samples to silence.
228 audio_frame_for_mixing->samples_per_channel_ = sample_size_;
229 audio_frame_for_mixing->Mute();
230 } else {
231 // Only call the limiter if we have something to mix.
232 LimitMixedAudio(audio_frame_for_mixing);
aleloi77ad3942016-07-04 06:33:02 -0700233 }
aleloi616df1e2016-08-24 01:17:12 -0700234
aleloi77ad3942016-07-04 06:33:02 -0700235 return;
236}
237
aleloie97974d2016-10-12 03:06:09 -0700238void AudioMixerImpl::SetOutputFrequency(int frequency) {
aleloi311525e2016-09-07 06:13:12 -0700239 RTC_DCHECK_RUN_ON(&thread_checker_);
aleloi6382a192016-08-08 10:25:04 -0700240 output_frequency_ = frequency;
aleloidc7669a2016-10-04 04:06:20 -0700241 sample_size_ = (output_frequency_ * kFrameDurationInMs) / 1000;
aleloi77ad3942016-07-04 06:33:02 -0700242}
243
aleloie97974d2016-10-12 03:06:09 -0700244int AudioMixerImpl::OutputFrequency() const {
aleloi311525e2016-09-07 06:13:12 -0700245 RTC_DCHECK_RUN_ON(&thread_checker_);
aleloi6382a192016-08-08 10:25:04 -0700246 return output_frequency_;
aleloi77ad3942016-07-04 06:33:02 -0700247}
248
aleloie8914152016-10-11 06:18:31 -0700249int32_t AudioMixerImpl::SetMixabilityStatus(Source* audio_source,
aleloi5d167d62016-08-24 02:20:54 -0700250 bool mixable) {
aleloi77ad3942016-07-04 06:33:02 -0700251 {
aleloi311525e2016-09-07 06:13:12 -0700252 rtc::CritScope lock(&crit_);
aleloi36542512016-10-07 05:28:32 -0700253 const bool is_mixed = FindSourceInList(audio_source, &audio_source_list_) !=
254 audio_source_list_.end();
aleloi77ad3942016-07-04 06:33:02 -0700255 // API must be called with a new state.
aleloia4c21062016-09-08 01:25:46 -0700256 if (!(mixable ^ is_mixed)) {
aleloi77ad3942016-07-04 06:33:02 -0700257 return -1;
258 }
259 bool success = false;
260 if (mixable) {
aleloi09f45102016-07-28 03:52:15 -0700261 success = AddAudioSourceToList(audio_source, &audio_source_list_);
aleloi77ad3942016-07-04 06:33:02 -0700262 } else {
aleloi09f45102016-07-28 03:52:15 -0700263 success = RemoveAudioSourceFromList(audio_source, &audio_source_list_);
aleloi77ad3942016-07-04 06:33:02 -0700264 }
265 if (!success) {
aleloi09f45102016-07-28 03:52:15 -0700266 RTC_NOTREACHED();
aleloi77ad3942016-07-04 06:33:02 -0700267 return -1;
268 }
269
aleloia4c21062016-09-08 01:25:46 -0700270 size_t num_mixed_non_anonymous = audio_source_list_.size();
271 if (num_mixed_non_anonymous > kMaximumAmountOfMixedAudioSources) {
272 num_mixed_non_anonymous = kMaximumAmountOfMixedAudioSources;
aleloi77ad3942016-07-04 06:33:02 -0700273 }
aleloie97974d2016-10-12 03:06:09 -0700274 num_mixed_audio_sources_ = num_mixed_non_anonymous;
aleloi77ad3942016-07-04 06:33:02 -0700275 }
aleloi77ad3942016-07-04 06:33:02 -0700276 return 0;
277}
278
aleloi77ad3942016-07-04 06:33:02 -0700279
aleloi77ad3942016-07-04 06:33:02 -0700280
aleloi36542512016-10-07 05:28:32 -0700281AudioFrameList AudioMixerImpl::GetNonAnonymousAudio() {
aleloi311525e2016-09-07 06:13:12 -0700282 RTC_DCHECK_RUN_ON(&thread_checker_);
aleloif3882572016-07-29 02:12:41 -0700283 AudioFrameList result;
aleloia4c21062016-09-08 01:25:46 -0700284 std::vector<SourceFrame> audio_source_mixing_data_list;
aleloi652ac892016-09-07 07:42:14 -0700285 std::vector<SourceFrame> ramp_list;
aleloi77ad3942016-07-04 06:33:02 -0700286
aleloif3882572016-07-29 02:12:41 -0700287 // Get audio source audio and put it in the struct vector.
aleloi36542512016-10-07 05:28:32 -0700288 for (auto& source_and_status : audio_source_list_) {
289 auto audio_frame_with_info =
aleloi4b8bfb82016-10-12 02:14:59 -0700290 source_and_status.audio_source->GetAudioFrameWithInfo(
aleloie97974d2016-10-12 03:06:09 -0700291 static_cast<int>(OutputFrequency()));
aleloi77ad3942016-07-04 06:33:02 -0700292
aleloia4c21062016-09-08 01:25:46 -0700293 const auto audio_frame_info = audio_frame_with_info.audio_frame_info;
aleloif3882572016-07-29 02:12:41 -0700294 AudioFrame* audio_source_audio_frame = audio_frame_with_info.audio_frame;
295
aleloie8914152016-10-11 06:18:31 -0700296 if (audio_frame_info == Source::AudioFrameInfo::kError) {
aleloie97974d2016-10-12 03:06:09 -0700297 LOG_F(LS_WARNING) << "failed to GetAudioFrameWithInfo() from source";
tereliusea4c1412016-07-29 01:36:14 -0700298 continue;
299 }
aleloia4c21062016-09-08 01:25:46 -0700300 audio_source_mixing_data_list.emplace_back(
aleloi36542512016-10-07 05:28:32 -0700301 &source_and_status, audio_source_audio_frame,
aleloie8914152016-10-11 06:18:31 -0700302 audio_frame_info == Source::AudioFrameInfo::kMuted);
aleloif3882572016-07-29 02:12:41 -0700303 }
304
305 // Sort frames by sorting function.
aleloia4c21062016-09-08 01:25:46 -0700306 std::sort(audio_source_mixing_data_list.begin(),
aleloi4b8bfb82016-10-12 02:14:59 -0700307 audio_source_mixing_data_list.end(), ShouldMixBefore);
aleloif3882572016-07-29 02:12:41 -0700308
aleloia4c21062016-09-08 01:25:46 -0700309 int max_audio_frame_counter = kMaximumAmountOfMixedAudioSources;
310
311 // Go through list in order and put unmuted frames in result list.
aleloi36542512016-10-07 05:28:32 -0700312 for (const auto& p : audio_source_mixing_data_list) {
aleloif3882572016-07-29 02:12:41 -0700313 // Filter muted.
aleloi4b8bfb82016-10-12 02:14:59 -0700314 if (p.muted) {
315 p.source_status->is_mixed = false;
aleloif3882572016-07-29 02:12:41 -0700316 continue;
tereliusea4c1412016-07-29 01:36:14 -0700317 }
aleloi2942e242016-07-29 01:23:49 -0700318
aleloif3882572016-07-29 02:12:41 -0700319 // Add frame to result vector for mixing.
320 bool is_mixed = false;
aleloia4c21062016-09-08 01:25:46 -0700321 if (max_audio_frame_counter > 0) {
322 --max_audio_frame_counter;
aleloi4b8bfb82016-10-12 02:14:59 -0700323 result.push_back(p.audio_frame);
324 ramp_list.emplace_back(p.source_status, p.audio_frame, false, -1);
aleloif3882572016-07-29 02:12:41 -0700325 is_mixed = true;
tereliusea4c1412016-07-29 01:36:14 -0700326 }
aleloi4b8bfb82016-10-12 02:14:59 -0700327 p.source_status->is_mixed = is_mixed;
tereliusea4c1412016-07-29 01:36:14 -0700328 }
aleloi4b8bfb82016-10-12 02:14:59 -0700329 RampAndUpdateGain(ramp_list);
aleloif3882572016-07-29 02:12:41 -0700330 return result;
aleloi77ad3942016-07-04 06:33:02 -0700331}
332
aleloi5d167d62016-08-24 02:20:54 -0700333bool AudioMixerImpl::AddAudioSourceToList(
aleloie8914152016-10-11 06:18:31 -0700334 Source* audio_source,
aleloi4b8bfb82016-10-12 02:14:59 -0700335 SourceStatusList* audio_source_list) const {
aleloi4b8bfb82016-10-12 02:14:59 -0700336 audio_source_list->emplace_back(audio_source, false, 0);
aleloi77ad3942016-07-04 06:33:02 -0700337 return true;
338}
339
aleloi5d167d62016-08-24 02:20:54 -0700340bool AudioMixerImpl::RemoveAudioSourceFromList(
aleloie8914152016-10-11 06:18:31 -0700341 Source* audio_source,
aleloi4b8bfb82016-10-12 02:14:59 -0700342 SourceStatusList* audio_source_list) const {
aleloi36542512016-10-07 05:28:32 -0700343 const auto iter = FindSourceInList(audio_source, audio_source_list);
aleloia4c21062016-09-08 01:25:46 -0700344 if (iter != audio_source_list->end()) {
345 audio_source_list->erase(iter);
aleloi6382a192016-08-08 10:25:04 -0700346 return true;
347 } else {
348 return false;
aleloi77ad3942016-07-04 06:33:02 -0700349 }
aleloi77ad3942016-07-04 06:33:02 -0700350}
351
aleloia4c21062016-09-08 01:25:46 -0700352bool AudioMixerImpl::LimitMixedAudio(AudioFrame* mixed_audio) const {
aleloi311525e2016-09-07 06:13:12 -0700353 RTC_DCHECK_RUN_ON(&thread_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700354 if (!use_limiter_) {
355 return true;
356 }
357
358 // Smoothly limit the mixed frame.
aleloia4c21062016-09-08 01:25:46 -0700359 const int error = limiter_->ProcessStream(mixed_audio);
aleloi77ad3942016-07-04 06:33:02 -0700360
361 // And now we can safely restore the level. This procedure results in
362 // some loss of resolution, deemed acceptable.
363 //
364 // It's possible to apply the gain in the AGC (with a target level of 0 dbFS
365 // and compression gain of 6 dB). However, in the transition frame when this
aleloi09f45102016-07-28 03:52:15 -0700366 // is enabled (moving from one to two audio sources) it has the potential to
aleloi77ad3942016-07-04 06:33:02 -0700367 // create discontinuities in the mixed frame.
368 //
369 // Instead we double the frame (with addition since left-shifting a
370 // negative value is undefined).
aleloia4c21062016-09-08 01:25:46 -0700371 *mixed_audio += *mixed_audio;
aleloi77ad3942016-07-04 06:33:02 -0700372
aleloi6382a192016-08-08 10:25:04 -0700373 if (error != limiter_->kNoError) {
aleloie97974d2016-10-12 03:06:09 -0700374 LOG_F(LS_ERROR) << "Error from AudioProcessing: " << error;
aleloi09f45102016-07-28 03:52:15 -0700375 RTC_NOTREACHED();
aleloi77ad3942016-07-04 06:33:02 -0700376 return false;
377 }
378 return true;
379}
aleloi616df1e2016-08-24 01:17:12 -0700380
aleloi36542512016-10-07 05:28:32 -0700381bool AudioMixerImpl::GetAudioSourceMixabilityStatusForTest(
aleloie97974d2016-10-12 03:06:09 -0700382 AudioMixerImpl::Source* audio_source) const {
aleloi36542512016-10-07 05:28:32 -0700383 RTC_DCHECK_RUN_ON(&thread_checker_);
384 rtc::CritScope lock(&crit_);
385
386 const auto non_anonymous_iter =
387 FindSourceInList(audio_source, &audio_source_list_);
388 if (non_anonymous_iter != audio_source_list_.end()) {
aleloi4b8bfb82016-10-12 02:14:59 -0700389 return non_anonymous_iter->is_mixed;
aleloi36542512016-10-07 05:28:32 -0700390 }
391
aleloi36542512016-10-07 05:28:32 -0700392 LOG(LS_ERROR) << "Audio source unknown";
393 return false;
394}
aleloi77ad3942016-07-04 06:33:02 -0700395} // namespace webrtc