blob: ad9ddafa78a34858bbe1d0a336690769d09ec3b6 [file] [log] [blame]
aleloi24899e52017-02-21 05:06:29 -08001/*
2 * Copyright (c) 2017 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 "webrtc/modules/audio_mixer/frame_combiner.h"
12
13#include <algorithm>
14#include <array>
15#include <functional>
16#include <memory>
17
kwiberg529662a2017-09-04 05:43:17 -070018#include "webrtc/api/array_view.h"
aleloi24899e52017-02-21 05:06:29 -080019#include "webrtc/audio/utility/audio_frame_operations.h"
aleloi24899e52017-02-21 05:06:29 -080020#include "webrtc/modules/audio_mixer/audio_frame_manipulator.h"
21#include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020022#include "webrtc/rtc_base/checks.h"
23#include "webrtc/rtc_base/logging.h"
aleloi24899e52017-02-21 05:06:29 -080024
25namespace webrtc {
26namespace {
27
28// Stereo, 48 kHz, 10 ms.
29constexpr int kMaximalFrameSize = 2 * 48 * 10;
30
aleloi2c9306e2017-03-29 04:25:16 -070031void CombineZeroFrames(bool use_limiter,
32 AudioProcessing* limiter,
33 AudioFrame* audio_frame_for_mixing) {
aleloi24899e52017-02-21 05:06:29 -080034 audio_frame_for_mixing->elapsed_time_ms_ = -1;
35 AudioFrameOperations::Mute(audio_frame_for_mixing);
aleloi2c9306e2017-03-29 04:25:16 -070036 // The limiter should still process a zero frame to avoid jumps in
37 // its gain curve.
38 if (use_limiter) {
39 RTC_DCHECK(limiter);
40 // The limiter smoothly increases frames with half gain to full
41 // volume. Here there's no need to apply half gain, since the frame
42 // is zero anyway.
43 limiter->ProcessStream(audio_frame_for_mixing);
44 }
aleloi24899e52017-02-21 05:06:29 -080045}
46
47void CombineOneFrame(const AudioFrame* input_frame,
aleloi2c9306e2017-03-29 04:25:16 -070048 bool use_limiter,
49 AudioProcessing* limiter,
aleloi24899e52017-02-21 05:06:29 -080050 AudioFrame* audio_frame_for_mixing) {
51 audio_frame_for_mixing->timestamp_ = input_frame->timestamp_;
52 audio_frame_for_mixing->elapsed_time_ms_ = input_frame->elapsed_time_ms_;
yujo36b1a5f2017-06-12 12:45:32 -070053 // TODO(yujo): can we optimize muted frames?
54 std::copy(input_frame->data(),
55 input_frame->data() +
aleloi24899e52017-02-21 05:06:29 -080056 input_frame->num_channels_ * input_frame->samples_per_channel_,
yujo36b1a5f2017-06-12 12:45:32 -070057 audio_frame_for_mixing->mutable_data());
aleloi2c9306e2017-03-29 04:25:16 -070058 if (use_limiter) {
59 AudioFrameOperations::ApplyHalfGain(audio_frame_for_mixing);
60 RTC_DCHECK(limiter);
61 limiter->ProcessStream(audio_frame_for_mixing);
62 AudioFrameOperations::Add(*audio_frame_for_mixing, audio_frame_for_mixing);
63 }
64}
65
66// Lower-level helper function called from Combine(...) when there
67// are several input frames.
68//
69// TODO(aleloi): change interface to ArrayView<int16_t> output_frame
70// once we have gotten rid of the APM limiter.
71//
72// Only the 'data' field of output_frame should be modified. The
73// rest are used for potentially sending the output to the APM
74// limiter.
75void CombineMultipleFrames(
76 const std::vector<rtc::ArrayView<const int16_t>>& input_frames,
77 bool use_limiter,
78 AudioProcessing* limiter,
79 AudioFrame* audio_frame_for_mixing) {
80 RTC_DCHECK(!input_frames.empty());
81 RTC_DCHECK(audio_frame_for_mixing);
82
83 const size_t frame_length = input_frames.front().size();
84 for (const auto& frame : input_frames) {
85 RTC_DCHECK_EQ(frame_length, frame.size());
86 }
87
88 // Algorithm: int16 frames are added to a sufficiently large
89 // statically allocated int32 buffer. For > 2 participants this is
90 // more efficient than addition in place in the int16 audio
91 // frame. The audio quality loss due to halving the samples is
92 // smaller than 16-bit addition in place.
93 RTC_DCHECK_GE(kMaximalFrameSize, frame_length);
94 std::array<int32_t, kMaximalFrameSize> add_buffer;
95
96 add_buffer.fill(0);
97
98 for (const auto& frame : input_frames) {
yujo36b1a5f2017-06-12 12:45:32 -070099 // TODO(yujo): skip this for muted frames.
aleloi2c9306e2017-03-29 04:25:16 -0700100 std::transform(frame.begin(), frame.end(), add_buffer.begin(),
101 add_buffer.begin(), std::plus<int32_t>());
102 }
103
104 if (use_limiter) {
105 // Halve all samples to avoid saturation before limiting.
106 std::transform(add_buffer.begin(), add_buffer.begin() + frame_length,
yujo36b1a5f2017-06-12 12:45:32 -0700107 audio_frame_for_mixing->mutable_data(), [](int32_t a) {
aleloi2c9306e2017-03-29 04:25:16 -0700108 return rtc::saturated_cast<int16_t>(a / 2);
109 });
110
111 // Smoothly limit the audio.
112 RTC_DCHECK(limiter);
113 const int error = limiter->ProcessStream(audio_frame_for_mixing);
114 if (error != limiter->kNoError) {
115 LOG_F(LS_ERROR) << "Error from AudioProcessing: " << error;
116 RTC_NOTREACHED();
117 }
118
119 // And now we can safely restore the level. This procedure results in
120 // some loss of resolution, deemed acceptable.
121 //
122 // It's possible to apply the gain in the AGC (with a target level of 0 dbFS
123 // and compression gain of 6 dB). However, in the transition frame when this
124 // is enabled (moving from one to two audio sources) it has the potential to
125 // create discontinuities in the mixed frame.
126 //
127 // Instead we double the frame (with addition since left-shifting a
128 // negative value is undefined).
129 AudioFrameOperations::Add(*audio_frame_for_mixing, audio_frame_for_mixing);
130 } else {
131 std::transform(add_buffer.begin(), add_buffer.begin() + frame_length,
yujo36b1a5f2017-06-12 12:45:32 -0700132 audio_frame_for_mixing->mutable_data(),
aleloi2c9306e2017-03-29 04:25:16 -0700133 [](int32_t a) { return rtc::saturated_cast<int16_t>(a); });
134 }
aleloi24899e52017-02-21 05:06:29 -0800135}
136
137std::unique_ptr<AudioProcessing> CreateLimiter() {
138 Config config;
139 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
aleloi0f23fa82017-05-11 00:25:45 -0700140
aleloi24899e52017-02-21 05:06:29 -0800141 std::unique_ptr<AudioProcessing> limiter(AudioProcessing::Create(config));
142 RTC_DCHECK(limiter);
143
aleloi0f23fa82017-05-11 00:25:45 -0700144 webrtc::AudioProcessing::Config apm_config;
145 apm_config.residual_echo_detector.enabled = false;
146 limiter->ApplyConfig(apm_config);
147
aleloi24899e52017-02-21 05:06:29 -0800148 const auto check_no_error = [](int x) {
149 RTC_DCHECK_EQ(x, AudioProcessing::kNoError);
150 };
151 auto* const gain_control = limiter->gain_control();
152 check_no_error(gain_control->set_mode(GainControl::kFixedDigital));
153
154 // We smoothly limit the mixed frame to -7 dbFS. -6 would correspond to the
155 // divide-by-2 but -7 is used instead to give a bit of headroom since the
156 // AGC is not a hard limiter.
157 check_no_error(gain_control->set_target_level_dbfs(7));
158
159 check_no_error(gain_control->set_compression_gain_db(0));
160 check_no_error(gain_control->enable_limiter(true));
161 check_no_error(gain_control->Enable(true));
162 return limiter;
163}
164} // namespace
165
166FrameCombiner::FrameCombiner(bool use_apm_limiter)
167 : use_apm_limiter_(use_apm_limiter),
168 limiter_(use_apm_limiter ? CreateLimiter() : nullptr) {}
169
170FrameCombiner::~FrameCombiner() = default;
171
172void FrameCombiner::Combine(const std::vector<AudioFrame*>& mix_list,
173 size_t number_of_channels,
174 int sample_rate,
aleloi2c9306e2017-03-29 04:25:16 -0700175 size_t number_of_streams,
aleloi24899e52017-02-21 05:06:29 -0800176 AudioFrame* audio_frame_for_mixing) const {
177 RTC_DCHECK(audio_frame_for_mixing);
178 const size_t samples_per_channel = static_cast<size_t>(
179 (sample_rate * webrtc::AudioMixerImpl::kFrameDurationInMs) / 1000);
180
181 for (const auto* frame : mix_list) {
182 RTC_DCHECK_EQ(samples_per_channel, frame->samples_per_channel_);
183 RTC_DCHECK_EQ(sample_rate, frame->sample_rate_hz_);
184 }
185
186 // Frames could be both stereo and mono.
187 for (auto* frame : mix_list) {
188 RemixFrame(number_of_channels, frame);
189 }
190
191 // TODO(aleloi): Issue bugs.webrtc.org/3390.
192 // Audio frame timestamp. The 'timestamp_' field is set to dummy
193 // value '0', because it is only supported in the one channel case and
194 // is then updated in the helper functions.
195 audio_frame_for_mixing->UpdateFrame(
196 -1, 0, nullptr, samples_per_channel, sample_rate, AudioFrame::kUndefined,
197 AudioFrame::kVadUnknown, number_of_channels);
198
aleloi2c9306e2017-03-29 04:25:16 -0700199 const bool use_limiter_this_round = use_apm_limiter_ && number_of_streams > 1;
200
aleloi61a2b1b2017-02-23 01:16:14 -0800201 if (mix_list.empty()) {
aleloi2c9306e2017-03-29 04:25:16 -0700202 CombineZeroFrames(use_limiter_this_round, limiter_.get(),
203 audio_frame_for_mixing);
aleloi24899e52017-02-21 05:06:29 -0800204 } else if (mix_list.size() == 1) {
aleloi2c9306e2017-03-29 04:25:16 -0700205 CombineOneFrame(mix_list.front(), use_limiter_this_round, limiter_.get(),
206 audio_frame_for_mixing);
aleloi24899e52017-02-21 05:06:29 -0800207 } else {
208 std::vector<rtc::ArrayView<const int16_t>> input_frames;
209 for (size_t i = 0; i < mix_list.size(); ++i) {
210 input_frames.push_back(rtc::ArrayView<const int16_t>(
yujo36b1a5f2017-06-12 12:45:32 -0700211 mix_list[i]->data(), samples_per_channel * number_of_channels));
aleloi24899e52017-02-21 05:06:29 -0800212 }
aleloi2c9306e2017-03-29 04:25:16 -0700213 CombineMultipleFrames(input_frames, use_limiter_this_round, limiter_.get(),
214 audio_frame_for_mixing);
aleloi24899e52017-02-21 05:06:29 -0800215 }
216}
yujo36b1a5f2017-06-12 12:45:32 -0700217
aleloi24899e52017-02-21 05:06:29 -0800218} // namespace webrtc