blob: c646fcb56c22797de18c2cf68d81dd04bfcb36db [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) {
aleloi6c278492016-10-20 14:24:39 -0700122 return std::find_if(
123 audio_source_list->begin(), audio_source_list->end(),
124 [audio_source](const std::unique_ptr<AudioMixerImpl::SourceStatus>& p) {
125 return p->audio_source == audio_source;
126 });
aleloi36542512016-10-07 05:28:32 -0700127}
128
aleloie97974d2016-10-12 03:06:09 -0700129// TODO(aleloi): remove non-const version when WEBRTC only supports modern STL.
aleloi4b8bfb82016-10-12 02:14:59 -0700130AudioMixerImpl::SourceStatusList::iterator FindSourceInList(
aleloie8914152016-10-11 06:18:31 -0700131 AudioMixerImpl::Source const* audio_source,
aleloi4b8bfb82016-10-12 02:14:59 -0700132 AudioMixerImpl::SourceStatusList* audio_source_list) {
aleloi6c278492016-10-20 14:24:39 -0700133 return std::find_if(
134 audio_source_list->begin(), audio_source_list->end(),
135 [audio_source](const std::unique_ptr<AudioMixerImpl::SourceStatus>& p) {
136 return p->audio_source == audio_source;
137 });
aleloi36542512016-10-07 05:28:32 -0700138}
139
aleloidc7669a2016-10-04 04:06:20 -0700140} // namespace
aleloi77ad3942016-07-04 06:33:02 -0700141
aleloie97974d2016-10-12 03:06:09 -0700142AudioMixerImpl::AudioMixerImpl(std::unique_ptr<AudioProcessing> limiter)
143 : audio_source_list_(),
aleloi77ad3942016-07-04 06:33:02 -0700144 use_limiter_(true),
aleloi311525e2016-09-07 06:13:12 -0700145 time_stamp_(0),
146 limiter_(std::move(limiter)) {
147 SetOutputFrequency(kDefaultFrequency);
aleloia0db81f2016-07-28 06:36:22 -0700148}
aleloi77ad3942016-07-04 06:33:02 -0700149
aleloi5d167d62016-08-24 02:20:54 -0700150AudioMixerImpl::~AudioMixerImpl() {}
aleloi70f866c2016-08-16 02:15:49 -0700151
aleloi116ec6d2016-10-12 06:07:07 -0700152rtc::scoped_refptr<AudioMixerImpl> AudioMixerImpl::Create() {
aleloi77ad3942016-07-04 06:33:02 -0700153 Config config;
154 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
aleloi311525e2016-09-07 06:13:12 -0700155 std::unique_ptr<AudioProcessing> limiter(AudioProcessing::Create(config));
aleloie97974d2016-10-12 03:06:09 -0700156 if (!limiter.get()) {
aleloi311525e2016-09-07 06:13:12 -0700157 return nullptr;
aleloie97974d2016-10-12 03:06:09 -0700158 }
aleloi77ad3942016-07-04 06:33:02 -0700159
aleloi311525e2016-09-07 06:13:12 -0700160 if (limiter->gain_control()->set_mode(GainControl::kFixedDigital) !=
aleloie97974d2016-10-12 03:06:09 -0700161 limiter->kNoError) {
aleloi311525e2016-09-07 06:13:12 -0700162 return nullptr;
aleloie97974d2016-10-12 03:06:09 -0700163 }
aleloi77ad3942016-07-04 06:33:02 -0700164
165 // We smoothly limit the mixed frame to -7 dbFS. -6 would correspond to the
166 // divide-by-2 but -7 is used instead to give a bit of headroom since the
167 // AGC is not a hard limiter.
aleloie97974d2016-10-12 03:06:09 -0700168 if (limiter->gain_control()->set_target_level_dbfs(7) != limiter->kNoError) {
aleloi311525e2016-09-07 06:13:12 -0700169 return nullptr;
aleloie97974d2016-10-12 03:06:09 -0700170 }
aleloi77ad3942016-07-04 06:33:02 -0700171
aleloie97974d2016-10-12 03:06:09 -0700172 if (limiter->gain_control()->set_compression_gain_db(0) !=
173 limiter->kNoError) {
aleloi311525e2016-09-07 06:13:12 -0700174 return nullptr;
aleloie97974d2016-10-12 03:06:09 -0700175 }
aleloi77ad3942016-07-04 06:33:02 -0700176
aleloie97974d2016-10-12 03:06:09 -0700177 if (limiter->gain_control()->enable_limiter(true) != 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(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
aleloi116ec6d2016-10-12 06:07:07 -0700185 return rtc::scoped_refptr<AudioMixerImpl>(
186 new rtc::RefCountedObject<AudioMixerImpl>(std::move(limiter)));
aleloi77ad3942016-07-04 06:33:02 -0700187}
188
aleloi5d167d62016-08-24 02:20:54 -0700189void AudioMixerImpl::Mix(int sample_rate,
190 size_t number_of_channels,
191 AudioFrame* audio_frame_for_mixing) {
aleloi44968092016-08-08 10:18:58 -0700192 RTC_DCHECK(number_of_channels == 1 || number_of_channels == 2);
aleloi920d30b2016-10-20 14:23:24 -0700193 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
aleloi311525e2016-09-07 06:13:12 -0700194
aleloi311525e2016-09-07 06:13:12 -0700195 if (OutputFrequency() != sample_rate) {
aleloie97974d2016-10-12 03:06:09 -0700196 SetOutputFrequency(sample_rate);
aleloi311525e2016-09-07 06:13:12 -0700197 }
198
aleloi652ac892016-09-07 07:42:14 -0700199 AudioFrameList mix_list;
aleloi77ad3942016-07-04 06:33:02 -0700200 {
aleloi311525e2016-09-07 06:13:12 -0700201 rtc::CritScope lock(&crit_);
aleloi116ec6d2016-10-12 06:07:07 -0700202 mix_list = GetAudioFromSources();
aleloi77ad3942016-07-04 06:33:02 -0700203 }
204
aleloi652ac892016-09-07 07:42:14 -0700205 for (const auto& frame : mix_list) {
aleloie8914152016-10-11 06:18:31 -0700206 RemixFrame(number_of_channels, frame);
aleloi44968092016-08-08 10:18:58 -0700207 }
aleloi09f45102016-07-28 03:52:15 -0700208
209 audio_frame_for_mixing->UpdateFrame(
aleloia4c21062016-09-08 01:25:46 -0700210 -1, time_stamp_, NULL, 0, OutputFrequency(), AudioFrame::kNormalSpeech,
aleloi44968092016-08-08 10:18:58 -0700211 AudioFrame::kVadPassive, number_of_channels);
aleloi09f45102016-07-28 03:52:15 -0700212
aleloi6382a192016-08-08 10:25:04 -0700213 time_stamp_ += static_cast<uint32_t>(sample_size_);
aleloi09f45102016-07-28 03:52:15 -0700214
aleloi116ec6d2016-10-12 06:07:07 -0700215 use_limiter_ = mix_list.size() > 1;
aleloi09f45102016-07-28 03:52:15 -0700216
aleloi652ac892016-09-07 07:42:14 -0700217 // We only use the limiter if we're actually mixing multiple streams.
aleloie97974d2016-10-12 03:06:09 -0700218 MixFromList(audio_frame_for_mixing, mix_list, use_limiter_);
aleloi652ac892016-09-07 07:42:14 -0700219
aleloi311525e2016-09-07 06:13:12 -0700220 if (audio_frame_for_mixing->samples_per_channel_ == 0) {
221 // Nothing was mixed, set the audio samples to silence.
222 audio_frame_for_mixing->samples_per_channel_ = sample_size_;
223 audio_frame_for_mixing->Mute();
224 } else {
225 // Only call the limiter if we have something to mix.
226 LimitMixedAudio(audio_frame_for_mixing);
aleloi77ad3942016-07-04 06:33:02 -0700227 }
aleloi616df1e2016-08-24 01:17:12 -0700228
aleloi77ad3942016-07-04 06:33:02 -0700229 return;
230}
231
aleloie97974d2016-10-12 03:06:09 -0700232void AudioMixerImpl::SetOutputFrequency(int frequency) {
aleloi920d30b2016-10-20 14:23:24 -0700233 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
aleloi6382a192016-08-08 10:25:04 -0700234 output_frequency_ = frequency;
aleloidc7669a2016-10-04 04:06:20 -0700235 sample_size_ = (output_frequency_ * kFrameDurationInMs) / 1000;
aleloi77ad3942016-07-04 06:33:02 -0700236}
237
aleloie97974d2016-10-12 03:06:09 -0700238int AudioMixerImpl::OutputFrequency() const {
aleloi920d30b2016-10-20 14:23:24 -0700239 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
aleloi6382a192016-08-08 10:25:04 -0700240 return output_frequency_;
aleloi77ad3942016-07-04 06:33:02 -0700241}
242
aleloi116ec6d2016-10-12 06:07:07 -0700243bool AudioMixerImpl::AddSource(Source* audio_source) {
244 RTC_DCHECK(audio_source);
245 rtc::CritScope lock(&crit_);
246 RTC_DCHECK(FindSourceInList(audio_source, &audio_source_list_) ==
247 audio_source_list_.end())
248 << "Source already added to mixer";
aleloi6c278492016-10-20 14:24:39 -0700249 audio_source_list_.emplace_back(new SourceStatus(audio_source, false, 0));
aleloi116ec6d2016-10-12 06:07:07 -0700250 return true;
aleloi77ad3942016-07-04 06:33:02 -0700251}
252
aleloi116ec6d2016-10-12 06:07:07 -0700253bool AudioMixerImpl::RemoveSource(Source* audio_source) {
254 RTC_DCHECK(audio_source);
255 rtc::CritScope lock(&crit_);
256 const auto iter = FindSourceInList(audio_source, &audio_source_list_);
257 RTC_DCHECK(iter != audio_source_list_.end()) << "Source not present in mixer";
258 audio_source_list_.erase(iter);
259 return true;
260}
aleloi77ad3942016-07-04 06:33:02 -0700261
aleloi116ec6d2016-10-12 06:07:07 -0700262AudioFrameList AudioMixerImpl::GetAudioFromSources() {
aleloi920d30b2016-10-20 14:23:24 -0700263 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
aleloif3882572016-07-29 02:12:41 -0700264 AudioFrameList result;
aleloia4c21062016-09-08 01:25:46 -0700265 std::vector<SourceFrame> audio_source_mixing_data_list;
aleloi652ac892016-09-07 07:42:14 -0700266 std::vector<SourceFrame> ramp_list;
aleloi77ad3942016-07-04 06:33:02 -0700267
aleloi6c278492016-10-20 14:24:39 -0700268 // Get audio from the audio sources and put it in the SourceFrame vector.
aleloi36542512016-10-07 05:28:32 -0700269 for (auto& source_and_status : audio_source_list_) {
aleloi6c278492016-10-20 14:24:39 -0700270 const auto audio_frame_info =
271 source_and_status->audio_source->GetAudioFrameWithInfo(
272 OutputFrequency(), &source_and_status->audio_frame);
aleloif3882572016-07-29 02:12:41 -0700273
aleloie8914152016-10-11 06:18:31 -0700274 if (audio_frame_info == Source::AudioFrameInfo::kError) {
aleloie97974d2016-10-12 03:06:09 -0700275 LOG_F(LS_WARNING) << "failed to GetAudioFrameWithInfo() from source";
tereliusea4c1412016-07-29 01:36:14 -0700276 continue;
277 }
aleloia4c21062016-09-08 01:25:46 -0700278 audio_source_mixing_data_list.emplace_back(
aleloi6c278492016-10-20 14:24:39 -0700279 source_and_status.get(), &source_and_status->audio_frame,
aleloie8914152016-10-11 06:18:31 -0700280 audio_frame_info == Source::AudioFrameInfo::kMuted);
aleloif3882572016-07-29 02:12:41 -0700281 }
282
283 // Sort frames by sorting function.
aleloia4c21062016-09-08 01:25:46 -0700284 std::sort(audio_source_mixing_data_list.begin(),
aleloi4b8bfb82016-10-12 02:14:59 -0700285 audio_source_mixing_data_list.end(), ShouldMixBefore);
aleloif3882572016-07-29 02:12:41 -0700286
aleloia4c21062016-09-08 01:25:46 -0700287 int max_audio_frame_counter = kMaximumAmountOfMixedAudioSources;
288
289 // Go through list in order and put unmuted frames in result list.
aleloi36542512016-10-07 05:28:32 -0700290 for (const auto& p : audio_source_mixing_data_list) {
aleloif3882572016-07-29 02:12:41 -0700291 // Filter muted.
aleloi4b8bfb82016-10-12 02:14:59 -0700292 if (p.muted) {
293 p.source_status->is_mixed = false;
aleloif3882572016-07-29 02:12:41 -0700294 continue;
tereliusea4c1412016-07-29 01:36:14 -0700295 }
aleloi2942e242016-07-29 01:23:49 -0700296
aleloif3882572016-07-29 02:12:41 -0700297 // Add frame to result vector for mixing.
298 bool is_mixed = false;
aleloia4c21062016-09-08 01:25:46 -0700299 if (max_audio_frame_counter > 0) {
300 --max_audio_frame_counter;
aleloi4b8bfb82016-10-12 02:14:59 -0700301 result.push_back(p.audio_frame);
302 ramp_list.emplace_back(p.source_status, p.audio_frame, false, -1);
aleloif3882572016-07-29 02:12:41 -0700303 is_mixed = true;
tereliusea4c1412016-07-29 01:36:14 -0700304 }
aleloi4b8bfb82016-10-12 02:14:59 -0700305 p.source_status->is_mixed = is_mixed;
tereliusea4c1412016-07-29 01:36:14 -0700306 }
aleloi4b8bfb82016-10-12 02:14:59 -0700307 RampAndUpdateGain(ramp_list);
aleloif3882572016-07-29 02:12:41 -0700308 return result;
aleloi77ad3942016-07-04 06:33:02 -0700309}
310
aleloi77ad3942016-07-04 06:33:02 -0700311
aleloia4c21062016-09-08 01:25:46 -0700312bool AudioMixerImpl::LimitMixedAudio(AudioFrame* mixed_audio) const {
aleloi920d30b2016-10-20 14:23:24 -0700313 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
aleloi77ad3942016-07-04 06:33:02 -0700314 if (!use_limiter_) {
315 return true;
316 }
317
318 // Smoothly limit the mixed frame.
aleloia4c21062016-09-08 01:25:46 -0700319 const int error = limiter_->ProcessStream(mixed_audio);
aleloi77ad3942016-07-04 06:33:02 -0700320
321 // And now we can safely restore the level. This procedure results in
322 // some loss of resolution, deemed acceptable.
323 //
324 // It's possible to apply the gain in the AGC (with a target level of 0 dbFS
325 // and compression gain of 6 dB). However, in the transition frame when this
aleloi09f45102016-07-28 03:52:15 -0700326 // is enabled (moving from one to two audio sources) it has the potential to
aleloi77ad3942016-07-04 06:33:02 -0700327 // create discontinuities in the mixed frame.
328 //
329 // Instead we double the frame (with addition since left-shifting a
330 // negative value is undefined).
aleloia4c21062016-09-08 01:25:46 -0700331 *mixed_audio += *mixed_audio;
aleloi77ad3942016-07-04 06:33:02 -0700332
aleloi6382a192016-08-08 10:25:04 -0700333 if (error != limiter_->kNoError) {
aleloie97974d2016-10-12 03:06:09 -0700334 LOG_F(LS_ERROR) << "Error from AudioProcessing: " << error;
aleloi09f45102016-07-28 03:52:15 -0700335 RTC_NOTREACHED();
aleloi77ad3942016-07-04 06:33:02 -0700336 return false;
337 }
338 return true;
339}
aleloi616df1e2016-08-24 01:17:12 -0700340
aleloi36542512016-10-07 05:28:32 -0700341bool AudioMixerImpl::GetAudioSourceMixabilityStatusForTest(
aleloie97974d2016-10-12 03:06:09 -0700342 AudioMixerImpl::Source* audio_source) const {
aleloi920d30b2016-10-20 14:23:24 -0700343 RTC_DCHECK_RUNS_SERIALIZED(&race_checker_);
aleloi36542512016-10-07 05:28:32 -0700344 rtc::CritScope lock(&crit_);
345
aleloi6c278492016-10-20 14:24:39 -0700346 const auto iter = FindSourceInList(audio_source, &audio_source_list_);
347 if (iter != audio_source_list_.end()) {
348 return (*iter)->is_mixed;
aleloi36542512016-10-07 05:28:32 -0700349 }
350
aleloi36542512016-10-07 05:28:32 -0700351 LOG(LS_ERROR) << "Audio source unknown";
352 return false;
353}
aleloi77ad3942016-07-04 06:33:02 -0700354} // namespace webrtc