blob: 9ba1bbead53423078db3b9d38ec90849691f392b [file] [log] [blame]
Shraddha Basantwanice054522023-01-20 23:36:54 +05301/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <android-base/logging.h>
20#include <android-base/thread_annotations.h>
21#include <audio_processing.h>
22#include <unordered_map>
23
24#include "PreProcessingTypes.h"
25#include "effect-impl/EffectContext.h"
26
27namespace aidl::android::hardware::audio::effect {
28
29enum PreProcEffectState {
30 PRE_PROC_STATE_UNINITIALIZED,
31 PRE_PROC_STATE_INITIALIZED,
32 PRE_PROC_STATE_ACTIVE,
33};
34
35class PreProcessingContext final : public EffectContext {
36 public:
37 PreProcessingContext(int statusDepth, const Parameter::Common& common,
38 const PreProcessingEffectType& type)
39 : EffectContext(statusDepth, common), mType(type) {
40 LOG(DEBUG) << __func__ << type;
41 mState = PRE_PROC_STATE_UNINITIALIZED;
42 }
43 ~PreProcessingContext() override { LOG(DEBUG) << __func__; }
44
45 RetCode init(const Parameter::Common& common);
46 RetCode deInit();
47
48 PreProcessingEffectType getPreProcessingType() const { return mType; }
49
50 RetCode enable();
51 RetCode disable();
52
53 RetCode setCommon(const Parameter::Common& common) override;
54 void updateConfigs(const Parameter::Common& common);
55
56 RetCode setAcousticEchoCancelerEchoDelay(int echoDelayUs);
57 int getAcousticEchoCancelerEchoDelay() const;
58 RetCode setAcousticEchoCancelerMobileMode(bool mobileMode);
59 bool getAcousticEchoCancelerMobileMode() const;
60
61 RetCode setAutomaticGainControlV1TargetPeakLevel(int targetPeakLevel);
62 int getAutomaticGainControlV1TargetPeakLevel() const;
63 RetCode setAutomaticGainControlV1MaxCompressionGain(int maxCompressionGain);
64 int getAutomaticGainControlV1MaxCompressionGain() const;
65 RetCode setAutomaticGainControlV1EnableLimiter(bool enableLimiter);
66 bool getAutomaticGainControlV1EnableLimiter() const;
67
68 RetCode setAutomaticGainControlV2DigitalGain(int gain);
69 int getAutomaticGainControlV2DigitalGain() const;
70 RetCode setAutomaticGainControlV2LevelEstimator(
71 AutomaticGainControlV2::LevelEstimator levelEstimator);
72 AutomaticGainControlV2::LevelEstimator getAutomaticGainControlV2LevelEstimator() const;
73 RetCode setAutomaticGainControlV2SaturationMargin(int saturationMargin);
74 int getAutomaticGainControlV2SaturationMargin() const;
75
76 RetCode setNoiseSuppressionLevel(NoiseSuppression::Level level);
77 NoiseSuppression::Level getNoiseSuppressionLevel() const;
78
79 IEffect::Status lvmProcess(float* in, float* out, int samples);
80
81 private:
82 static constexpr inline int kAgcDefaultTargetLevel = 3;
83 static constexpr inline int kAgcDefaultCompGain = 9;
84 static constexpr inline bool kAgcDefaultLimiter = true;
85 static constexpr inline webrtc::AudioProcessing::Config::NoiseSuppression::Level
86 kNsDefaultLevel = webrtc::AudioProcessing::Config::NoiseSuppression::kModerate;
87
88 std::mutex mMutex;
89 const PreProcessingEffectType mType;
90 PreProcEffectState mState; // current state
91
92 // handle on webRTC audio processing module (APM)
93 rtc::scoped_refptr<webrtc::AudioProcessing> mAudioProcessingModule GUARDED_BY(mMutex);
94
95 int mEnabledMsk GUARDED_BY(mMutex); // bit field containing IDs of enabled pre processors
96 int mProcessedMsk GUARDED_BY(mMutex); // bit field containing IDs of pre processors already
97 // processed in current round
98 int mRevEnabledMsk GUARDED_BY(mMutex); // bit field containing IDs of enabled pre processors
99 // with reverse channel
100 int mRevProcessedMsk GUARDED_BY(mMutex); // bit field containing IDs of pre processors with
101 // reverse channel already processed in current round
102
103 webrtc::StreamConfig mInputConfig; // input stream configuration
104 webrtc::StreamConfig mOutputConfig; // output stream configuration
105
106 // Acoustic Echo Canceler
107 int mEchoDelayUs = 0;
108 bool mMobileMode = false;
109
110 // Automatic Gain Control V1
111 int mTargetPeakLevel = 0;
112 int mMaxCompressionGain = 0;
113 bool mEnableLimiter = false;
114
115 // Automatic Gain Control V2
116 int mDigitalGain = 0;
117 AutomaticGainControlV2::LevelEstimator mLevelEstimator =
118 AutomaticGainControlV2::LevelEstimator::RMS;
119 int mSaturationMargin = 2;
120
121 // NoiseSuppression
122 NoiseSuppression::Level mLevel = NoiseSuppression::Level::LOW;
123};
124
125} // namespace aidl::android::hardware::audio::effect