blob: 784b47c457695ba8afb87dfb538e80ba70f888e9 [file] [log] [blame]
solenberg76377c52017-02-21 00:54:31 -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/media/engine/apm_helpers.h"
12
solenberg76377c52017-02-21 00:54:31 -080013#include "webrtc/modules/audio_device/include/audio_device.h"
14#include "webrtc/modules/audio_processing/include/audio_processing.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020015#include "webrtc/rtc_base/logging.h"
solenberg76377c52017-02-21 00:54:31 -080016#include "webrtc/voice_engine/transmit_mixer.h"
17
18namespace webrtc {
19namespace apm_helpers {
20
21AgcConfig GetAgcConfig(AudioProcessing* apm) {
22 RTC_DCHECK(apm);
23 AgcConfig result;
24 result.targetLeveldBOv = apm->gain_control()->target_level_dbfs();
25 result.digitalCompressionGaindB = apm->gain_control()->compression_gain_db();
26 result.limiterEnable = apm->gain_control()->is_limiter_enabled();
27 return result;
28}
29
30void SetAgcConfig(AudioProcessing* apm,
31 const AgcConfig& config) {
32 RTC_DCHECK(apm);
33 GainControl* gc = apm->gain_control();
34 if (gc->set_target_level_dbfs(config.targetLeveldBOv) != 0) {
35 LOG(LS_ERROR) << "Failed to set target level: " << config.targetLeveldBOv;
36 }
37 if (gc->set_compression_gain_db(config.digitalCompressionGaindB) != 0) {
38 LOG(LS_ERROR) << "Failed to set compression gain: "
39 << config.digitalCompressionGaindB;
40 }
41 if (gc->enable_limiter(config.limiterEnable) != 0) {
42 LOG(LS_ERROR) << "Failed to set limiter on/off: " << config.limiterEnable;
43 }
44}
45
46void SetAgcStatus(AudioProcessing* apm,
47 AudioDeviceModule* adm,
solenberg22818a52017-03-16 01:20:23 -070048 bool enable) {
solenberg76377c52017-02-21 00:54:31 -080049 RTC_DCHECK(apm);
50 RTC_DCHECK(adm);
51#if defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID)
solenberg76377c52017-02-21 00:54:31 -080052 GainControl::Mode agc_mode = GainControl::kFixedDigital;
53#else
solenberg76377c52017-02-21 00:54:31 -080054 GainControl::Mode agc_mode = GainControl::kAdaptiveAnalog;
55#endif
56 GainControl* gc = apm->gain_control();
57 if (gc->set_mode(agc_mode) != 0) {
58 LOG(LS_ERROR) << "Failed to set AGC mode: " << agc_mode;
59 return;
60 }
61 if (gc->Enable(enable) != 0) {
62 LOG(LS_ERROR) << "Failed to enable/disable AGC: " << enable;
63 return;
64 }
65 // Set AGC state in the ADM when adaptive AGC mode has been selected.
66 if (adm->SetAGC(enable && agc_mode == GainControl::kAdaptiveAnalog) != 0) {
67 LOG(LS_ERROR) << "Failed to set AGC mode in ADM: " << enable;
68 return;
69 }
solenberg22818a52017-03-16 01:20:23 -070070 LOG(LS_INFO) << "AGC set to " << enable << " with mode " << agc_mode;
solenberg76377c52017-02-21 00:54:31 -080071}
72
73void SetEcStatus(AudioProcessing* apm,
74 bool enable,
75 EcModes mode) {
76 RTC_DCHECK(apm);
77 RTC_DCHECK(mode == kEcConference || mode == kEcAecm) << "mode: " << mode;
78 EchoCancellation* ec = apm->echo_cancellation();
79 EchoControlMobile* ecm = apm->echo_control_mobile();
80 if (mode == kEcConference) {
81 // Disable the AECM before enabling the AEC.
82 if (enable && ecm->is_enabled() && ecm->Enable(false) != 0) {
83 LOG(LS_ERROR) << "Failed to disable AECM.";
84 return;
85 }
86 if (ec->Enable(enable) != 0) {
87 LOG(LS_ERROR) << "Failed to enable/disable AEC: " << enable;
88 return;
89 }
90 if (ec->set_suppression_level(EchoCancellation::kHighSuppression)
91 != 0) {
92 LOG(LS_ERROR) << "Failed to set high AEC aggressiveness.";
93 return;
94 }
95 } else {
96 // Disable the AEC before enabling the AECM.
97 if (enable && ec->is_enabled() && ec->Enable(false) != 0) {
98 LOG(LS_ERROR) << "Failed to disable AEC.";
99 return;
100 }
101 if (ecm->Enable(enable) != 0) {
102 LOG(LS_ERROR) << "Failed to enable/disable AECM: " << enable;
103 return;
104 }
105 }
106 LOG(LS_INFO) << "Echo control set to " << enable << " with mode " << mode;
107}
108
109void SetEcMetricsStatus(AudioProcessing* apm, bool enable) {
110 RTC_DCHECK(apm);
111 if ((apm->echo_cancellation()->enable_metrics(enable) != 0) ||
112 (apm->echo_cancellation()->enable_delay_logging(enable) != 0)) {
113 LOG(LS_ERROR) << "Failed to enable/disable EC metrics: " << enable;
114 return;
115 }
116 LOG(LS_INFO) << "EC metrics set to " << enable;
117}
118
119void SetAecmMode(AudioProcessing* apm, bool enable) {
120 RTC_DCHECK(apm);
121 EchoControlMobile* ecm = apm->echo_control_mobile();
122 RTC_DCHECK_EQ(EchoControlMobile::kSpeakerphone, ecm->routing_mode());
123 if (ecm->enable_comfort_noise(enable) != 0) {
124 LOG(LS_ERROR) << "Failed to enable/disable CNG: " << enable;
125 return;
126 }
127 LOG(LS_INFO) << "CNG set to " << enable;
128}
129
130void SetNsStatus(AudioProcessing* apm, bool enable) {
131 RTC_DCHECK(apm);
132 NoiseSuppression* ns = apm->noise_suppression();
133 if (ns->set_level(NoiseSuppression::kHigh) != 0) {
134 LOG(LS_ERROR) << "Failed to set high NS level.";
135 return;
136 }
137 if (ns->Enable(enable) != 0) {
138 LOG(LS_ERROR) << "Failed to enable/disable NS: " << enable;
139 return;
140 }
141 LOG(LS_INFO) << "NS set to " << enable;
142}
143
144void SetTypingDetectionStatus(AudioProcessing* apm, bool enable) {
145 RTC_DCHECK(apm);
146#if WEBRTC_VOICE_ENGINE_TYPING_DETECTION
147 // Typing detection takes place in TransmitMixer::PrepareDemux() and
148 // TransmitMixer::TypingDetection(). The typing detection algorithm takes as
149 // input two booleans:
150 // 1. A signal whether a key was pressed during the audio frame.
151 // 2. Whether VAD is active or not.
152 // TransmitMixer will not even call the detector if APM has set kVadUnknown in
153 // the audio frame after near end processing, so enabling/disabling VAD is
154 // sufficient for turning typing detection on/off.
155 // TODO(solenberg): Rather than relying on a side effect, consider forcing the
156 // feature on/off in TransmitMixer.
157 VoiceDetection* vd = apm->voice_detection();
158 if (vd->Enable(enable)) {
159 LOG(LS_ERROR) << "Failed to enable/disable VAD: " << enable;
160 return;
161 }
162 if (vd->set_likelihood(VoiceDetection::kVeryLowLikelihood)) {
163 LOG(LS_ERROR) << "Failed to set low VAD likelihood.";
164 return;
165 }
166 LOG(LS_INFO) << "VAD set to " << enable << " for typing detection.";
167#endif
168}
169} // namespace apm_helpers
170} // namespace webrtc