blob: 99f2cafad7303a8feeb9ba212e3c76c9440644d7 [file] [log] [blame]
Shunkai Yaobd862b82022-12-20 00:11:41 +00001/*
2 * Copyright (C) 2022 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#include <algorithm>
18#include <cstddef>
19#include <memory>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000020#define LOG_TAG "AHAL_NoiseSuppressionSw"
Shunkai Yaobd862b82022-12-20 00:11:41 +000021
Mikhail Naganov872d4a62023-03-09 18:19:01 -080022#define LOG_TAG "AHAL_NoiseSuppressionSw"
Shunkai Yaobd862b82022-12-20 00:11:41 +000023#include <android-base/logging.h>
24#include <fmq/AidlMessageQueue.h>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000025#include <system/audio_effects/effect_uuid.h>
Shunkai Yaobd862b82022-12-20 00:11:41 +000026
27#include "NoiseSuppressionSw.h"
28
29using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000030using aidl::android::hardware::audio::effect::getEffectImplUuidNoiseSuppressionSw;
31using aidl::android::hardware::audio::effect::getEffectTypeUuidNoiseSuppression;
Shunkai Yaobd862b82022-12-20 00:11:41 +000032using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yaobd862b82022-12-20 00:11:41 +000033using aidl::android::hardware::audio::effect::NoiseSuppressionSw;
34using aidl::android::media::audio::common::AudioUuid;
35
36extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
37 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000038 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidNoiseSuppressionSw()) {
Shunkai Yaobd862b82022-12-20 00:11:41 +000039 LOG(ERROR) << __func__ << "uuid not supported";
40 return EX_ILLEGAL_ARGUMENT;
41 }
42 if (instanceSpp) {
43 *instanceSpp = ndk::SharedRefBase::make<NoiseSuppressionSw>();
44 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
45 return EX_NONE;
46 } else {
47 LOG(ERROR) << __func__ << " invalid input parameter!";
48 return EX_ILLEGAL_ARGUMENT;
49 }
50}
51
52extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000053 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidNoiseSuppressionSw()) {
Shunkai Yaobd862b82022-12-20 00:11:41 +000054 LOG(ERROR) << __func__ << "uuid not supported";
55 return EX_ILLEGAL_ARGUMENT;
56 }
57 *_aidl_return = NoiseSuppressionSw::kDescriptor;
58 return EX_NONE;
59}
60
61namespace aidl::android::hardware::audio::effect {
62
63const std::string NoiseSuppressionSw::kEffectName = "NoiseSuppressionSw";
Shunkai Yaobd862b82022-12-20 00:11:41 +000064const Descriptor NoiseSuppressionSw::kDescriptor = {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000065 .common = {.id = {.type = getEffectTypeUuidNoiseSuppression(),
66 .uuid = getEffectImplUuidNoiseSuppressionSw(),
Shunkai Yaobd862b82022-12-20 00:11:41 +000067 .proxy = std::nullopt},
Shunkai Yao12179c32023-10-18 01:15:37 +000068 .flags = {.type = Flags::Type::PRE_PROC,
Shunkai Yaobd862b82022-12-20 00:11:41 +000069 .insert = Flags::Insert::FIRST,
70 .volume = Flags::Volume::CTRL},
71 .name = NoiseSuppressionSw::kEffectName,
Shunkai Yao87811022023-02-13 17:40:37 +000072 .implementor = "The Android Open Source Project"}};
Shunkai Yaobd862b82022-12-20 00:11:41 +000073
74ndk::ScopedAStatus NoiseSuppressionSw::getDescriptor(Descriptor* _aidl_return) {
75 LOG(DEBUG) << __func__ << kDescriptor.toString();
76 *_aidl_return = kDescriptor;
77 return ndk::ScopedAStatus::ok();
78}
79
80ndk::ScopedAStatus NoiseSuppressionSw::setParameterSpecific(const Parameter::Specific& specific) {
81 RETURN_IF(Parameter::Specific::noiseSuppression != specific.getTag(), EX_ILLEGAL_ARGUMENT,
82 "EffectNotSupported");
83 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
84
85 auto& param = specific.get<Parameter::Specific::noiseSuppression>();
86 auto tag = param.getTag();
87
88 switch (tag) {
89 case NoiseSuppression::level: {
90 RETURN_IF(mContext->setLevel(param.get<NoiseSuppression::level>()) != RetCode::SUCCESS,
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000091 EX_ILLEGAL_ARGUMENT, "levelNotSupported");
Shunkai Yaobd862b82022-12-20 00:11:41 +000092 return ndk::ScopedAStatus::ok();
93 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000094 case NoiseSuppression::type: {
95 RETURN_IF(mContext->setType(param.get<NoiseSuppression::type>()) != RetCode::SUCCESS,
96 EX_ILLEGAL_ARGUMENT, "typeNotSupported");
97 return ndk::ScopedAStatus::ok();
98 }
99 case NoiseSuppression::vendor: {
Shunkai Yaobd862b82022-12-20 00:11:41 +0000100 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
101 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
102 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
103 }
104 }
105}
106
107ndk::ScopedAStatus NoiseSuppressionSw::getParameterSpecific(const Parameter::Id& id,
108 Parameter::Specific* specific) {
109 auto tag = id.getTag();
110 RETURN_IF(Parameter::Id::noiseSuppressionTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
111 auto specificId = id.get<Parameter::Id::noiseSuppressionTag>();
112 auto specificIdTag = specificId.getTag();
113 switch (specificIdTag) {
114 case NoiseSuppression::Id::commonTag:
115 return getParameterNoiseSuppression(specificId.get<NoiseSuppression::Id::commonTag>(),
116 specific);
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000117 case NoiseSuppression::Id::vendorExtensionTag: {
Shunkai Yaobd862b82022-12-20 00:11:41 +0000118 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
119 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
120 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000121 }
Shunkai Yaobd862b82022-12-20 00:11:41 +0000122 }
123}
124
125ndk::ScopedAStatus NoiseSuppressionSw::getParameterNoiseSuppression(
126 const NoiseSuppression::Tag& tag, Parameter::Specific* specific) {
127 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
128 NoiseSuppression param;
129 switch (tag) {
130 case NoiseSuppression::level: {
131 param.set<NoiseSuppression::level>(mContext->getLevel());
132 break;
133 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000134 case NoiseSuppression::type: {
135 param.set<NoiseSuppression::type>(mContext->getType());
136 break;
137 }
138 case NoiseSuppression::vendor: {
Shunkai Yaobd862b82022-12-20 00:11:41 +0000139 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
140 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
141 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
142 }
143 }
144
145 specific->set<Parameter::Specific::noiseSuppression>(param);
146 return ndk::ScopedAStatus::ok();
147}
148
149std::shared_ptr<EffectContext> NoiseSuppressionSw::createContext(const Parameter::Common& common) {
150 if (mContext) {
151 LOG(DEBUG) << __func__ << " context already exist";
152 } else {
153 mContext = std::make_shared<NoiseSuppressionSwContext>(1 /* statusFmqDepth */, common);
154 }
155 return mContext;
156}
157
158std::shared_ptr<EffectContext> NoiseSuppressionSw::getContext() {
159 return mContext;
160}
161
162RetCode NoiseSuppressionSw::releaseContext() {
163 if (mContext) {
164 mContext.reset();
165 }
166 return RetCode::SUCCESS;
167}
168
169// Processing method running in EffectWorker thread.
170IEffect::Status NoiseSuppressionSw::effectProcessImpl(float* in, float* out, int samples) {
171 // TODO: get data buffer and process.
172 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
173 for (int i = 0; i < samples; i++) {
174 *out++ = *in++;
175 }
176 return {STATUS_OK, samples, samples};
177}
178
179RetCode NoiseSuppressionSwContext::setLevel(NoiseSuppression::Level level) {
180 mLevel = level;
181 return RetCode::SUCCESS;
182}
183
184NoiseSuppression::Level NoiseSuppressionSwContext::getLevel() {
185 return mLevel;
186}
187
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000188RetCode NoiseSuppressionSwContext::setType(NoiseSuppression::Type type) {
189 mType = type;
190 return RetCode::SUCCESS;
191}
192
Shunkai Yaobd862b82022-12-20 00:11:41 +0000193} // namespace aidl::android::hardware::audio::effect