blob: 3a7f61812bd1b4b0caea50297489ab0020e45b26 [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
18#include "webrtc/audio/utility/audio_frame_operations.h"
aleloi2c9306e2017-03-29 04:25:16 -070019#include "webrtc/base/array_view.h"
20#include "webrtc/base/checks.h"
aleloi24899e52017-02-21 05:06:29 -080021#include "webrtc/base/logging.h"
22#include "webrtc/modules/audio_mixer/audio_frame_manipulator.h"
23#include "webrtc/modules/audio_mixer/audio_mixer_impl.h"
24
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_;
53 std::copy(input_frame->data_,
54 input_frame->data_ +
55 input_frame->num_channels_ * input_frame->samples_per_channel_,
56 audio_frame_for_mixing->data_);
aleloi2c9306e2017-03-29 04:25:16 -070057 if (use_limiter) {
58 AudioFrameOperations::ApplyHalfGain(audio_frame_for_mixing);
59 RTC_DCHECK(limiter);
60 limiter->ProcessStream(audio_frame_for_mixing);
61 AudioFrameOperations::Add(*audio_frame_for_mixing, audio_frame_for_mixing);
62 }
63}
64
65// Lower-level helper function called from Combine(...) when there
66// are several input frames.
67//
68// TODO(aleloi): change interface to ArrayView<int16_t> output_frame
69// once we have gotten rid of the APM limiter.
70//
71// Only the 'data' field of output_frame should be modified. The
72// rest are used for potentially sending the output to the APM
73// limiter.
74void CombineMultipleFrames(
75 const std::vector<rtc::ArrayView<const int16_t>>& input_frames,
76 bool use_limiter,
77 AudioProcessing* limiter,
78 AudioFrame* audio_frame_for_mixing) {
79 RTC_DCHECK(!input_frames.empty());
80 RTC_DCHECK(audio_frame_for_mixing);
81
82 const size_t frame_length = input_frames.front().size();
83 for (const auto& frame : input_frames) {
84 RTC_DCHECK_EQ(frame_length, frame.size());
85 }
86
87 // Algorithm: int16 frames are added to a sufficiently large
88 // statically allocated int32 buffer. For > 2 participants this is
89 // more efficient than addition in place in the int16 audio
90 // frame. The audio quality loss due to halving the samples is
91 // smaller than 16-bit addition in place.
92 RTC_DCHECK_GE(kMaximalFrameSize, frame_length);
93 std::array<int32_t, kMaximalFrameSize> add_buffer;
94
95 add_buffer.fill(0);
96
97 for (const auto& frame : input_frames) {
98 std::transform(frame.begin(), frame.end(), add_buffer.begin(),
99 add_buffer.begin(), std::plus<int32_t>());
100 }
101
102 if (use_limiter) {
103 // Halve all samples to avoid saturation before limiting.
104 std::transform(add_buffer.begin(), add_buffer.begin() + frame_length,
105 audio_frame_for_mixing->data_, [](int32_t a) {
106 return rtc::saturated_cast<int16_t>(a / 2);
107 });
108
109 // Smoothly limit the audio.
110 RTC_DCHECK(limiter);
111 const int error = limiter->ProcessStream(audio_frame_for_mixing);
112 if (error != limiter->kNoError) {
113 LOG_F(LS_ERROR) << "Error from AudioProcessing: " << error;
114 RTC_NOTREACHED();
115 }
116
117 // And now we can safely restore the level. This procedure results in
118 // some loss of resolution, deemed acceptable.
119 //
120 // It's possible to apply the gain in the AGC (with a target level of 0 dbFS
121 // and compression gain of 6 dB). However, in the transition frame when this
122 // is enabled (moving from one to two audio sources) it has the potential to
123 // create discontinuities in the mixed frame.
124 //
125 // Instead we double the frame (with addition since left-shifting a
126 // negative value is undefined).
127 AudioFrameOperations::Add(*audio_frame_for_mixing, audio_frame_for_mixing);
128 } else {
129 std::transform(add_buffer.begin(), add_buffer.begin() + frame_length,
130 audio_frame_for_mixing->data_,
131 [](int32_t a) { return rtc::saturated_cast<int16_t>(a); });
132 }
aleloi24899e52017-02-21 05:06:29 -0800133}
134
135std::unique_ptr<AudioProcessing> CreateLimiter() {
136 Config config;
137 config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
aleloi0f23fa82017-05-11 00:25:45 -0700138
aleloi24899e52017-02-21 05:06:29 -0800139 std::unique_ptr<AudioProcessing> limiter(AudioProcessing::Create(config));
140 RTC_DCHECK(limiter);
141
aleloi0f23fa82017-05-11 00:25:45 -0700142 webrtc::AudioProcessing::Config apm_config;
143 apm_config.residual_echo_detector.enabled = false;
144 limiter->ApplyConfig(apm_config);
145
aleloi24899e52017-02-21 05:06:29 -0800146 const auto check_no_error = [](int x) {
147 RTC_DCHECK_EQ(x, AudioProcessing::kNoError);
148 };
149 auto* const gain_control = limiter->gain_control();
150 check_no_error(gain_control->set_mode(GainControl::kFixedDigital));
151
152 // We smoothly limit the mixed frame to -7 dbFS. -6 would correspond to the
153 // divide-by-2 but -7 is used instead to give a bit of headroom since the
154 // AGC is not a hard limiter.
155 check_no_error(gain_control->set_target_level_dbfs(7));
156
157 check_no_error(gain_control->set_compression_gain_db(0));
158 check_no_error(gain_control->enable_limiter(true));
159 check_no_error(gain_control->Enable(true));
160 return limiter;
161}
162} // namespace
163
164FrameCombiner::FrameCombiner(bool use_apm_limiter)
165 : use_apm_limiter_(use_apm_limiter),
166 limiter_(use_apm_limiter ? CreateLimiter() : nullptr) {}
167
168FrameCombiner::~FrameCombiner() = default;
169
170void FrameCombiner::Combine(const std::vector<AudioFrame*>& mix_list,
171 size_t number_of_channels,
172 int sample_rate,
aleloi2c9306e2017-03-29 04:25:16 -0700173 size_t number_of_streams,
aleloi24899e52017-02-21 05:06:29 -0800174 AudioFrame* audio_frame_for_mixing) const {
175 RTC_DCHECK(audio_frame_for_mixing);
176 const size_t samples_per_channel = static_cast<size_t>(
177 (sample_rate * webrtc::AudioMixerImpl::kFrameDurationInMs) / 1000);
178
179 for (const auto* frame : mix_list) {
180 RTC_DCHECK_EQ(samples_per_channel, frame->samples_per_channel_);
181 RTC_DCHECK_EQ(sample_rate, frame->sample_rate_hz_);
182 }
183
184 // Frames could be both stereo and mono.
185 for (auto* frame : mix_list) {
186 RemixFrame(number_of_channels, frame);
187 }
188
189 // TODO(aleloi): Issue bugs.webrtc.org/3390.
190 // Audio frame timestamp. The 'timestamp_' field is set to dummy
191 // value '0', because it is only supported in the one channel case and
192 // is then updated in the helper functions.
193 audio_frame_for_mixing->UpdateFrame(
194 -1, 0, nullptr, samples_per_channel, sample_rate, AudioFrame::kUndefined,
195 AudioFrame::kVadUnknown, number_of_channels);
196
aleloi2c9306e2017-03-29 04:25:16 -0700197 const bool use_limiter_this_round = use_apm_limiter_ && number_of_streams > 1;
198
aleloi61a2b1b2017-02-23 01:16:14 -0800199 if (mix_list.empty()) {
aleloi2c9306e2017-03-29 04:25:16 -0700200 CombineZeroFrames(use_limiter_this_round, limiter_.get(),
201 audio_frame_for_mixing);
aleloi24899e52017-02-21 05:06:29 -0800202 } else if (mix_list.size() == 1) {
aleloi2c9306e2017-03-29 04:25:16 -0700203 CombineOneFrame(mix_list.front(), use_limiter_this_round, limiter_.get(),
204 audio_frame_for_mixing);
aleloi24899e52017-02-21 05:06:29 -0800205 } else {
206 std::vector<rtc::ArrayView<const int16_t>> input_frames;
207 for (size_t i = 0; i < mix_list.size(); ++i) {
208 input_frames.push_back(rtc::ArrayView<const int16_t>(
209 mix_list[i]->data_, samples_per_channel * number_of_channels));
210 }
aleloi2c9306e2017-03-29 04:25:16 -0700211 CombineMultipleFrames(input_frames, use_limiter_this_round, limiter_.get(),
212 audio_frame_for_mixing);
aleloi24899e52017-02-21 05:06:29 -0800213 }
214}
215} // namespace webrtc