blob: ba39b1621733c5ab7bd60ba08cec36d716b9603c [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 Yaobd862b82022-12-20 00:11:41 +000020
Mikhail Naganov872d4a62023-03-09 18:19:01 -080021#define LOG_TAG "AHAL_NoiseSuppressionSw"
Shunkai Yaobd862b82022-12-20 00:11:41 +000022#include <android-base/logging.h>
23#include <fmq/AidlMessageQueue.h>
24
25#include "NoiseSuppressionSw.h"
26
27using aidl::android::hardware::audio::effect::Descriptor;
28using aidl::android::hardware::audio::effect::IEffect;
29using aidl::android::hardware::audio::effect::kNoiseSuppressionSwImplUUID;
30using aidl::android::hardware::audio::effect::NoiseSuppressionSw;
31using aidl::android::media::audio::common::AudioUuid;
32
33extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
34 std::shared_ptr<IEffect>* instanceSpp) {
35 if (!in_impl_uuid || *in_impl_uuid != kNoiseSuppressionSwImplUUID) {
36 LOG(ERROR) << __func__ << "uuid not supported";
37 return EX_ILLEGAL_ARGUMENT;
38 }
39 if (instanceSpp) {
40 *instanceSpp = ndk::SharedRefBase::make<NoiseSuppressionSw>();
41 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
42 return EX_NONE;
43 } else {
44 LOG(ERROR) << __func__ << " invalid input parameter!";
45 return EX_ILLEGAL_ARGUMENT;
46 }
47}
48
49extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
50 if (!in_impl_uuid || *in_impl_uuid != kNoiseSuppressionSwImplUUID) {
51 LOG(ERROR) << __func__ << "uuid not supported";
52 return EX_ILLEGAL_ARGUMENT;
53 }
54 *_aidl_return = NoiseSuppressionSw::kDescriptor;
55 return EX_NONE;
56}
57
58namespace aidl::android::hardware::audio::effect {
59
60const std::string NoiseSuppressionSw::kEffectName = "NoiseSuppressionSw";
Shunkai Yaobd862b82022-12-20 00:11:41 +000061const Descriptor NoiseSuppressionSw::kDescriptor = {
62 .common = {.id = {.type = kNoiseSuppressionTypeUUID,
63 .uuid = kNoiseSuppressionSwImplUUID,
64 .proxy = std::nullopt},
65 .flags = {.type = Flags::Type::INSERT,
66 .insert = Flags::Insert::FIRST,
67 .volume = Flags::Volume::CTRL},
68 .name = NoiseSuppressionSw::kEffectName,
Shunkai Yao87811022023-02-13 17:40:37 +000069 .implementor = "The Android Open Source Project"}};
Shunkai Yaobd862b82022-12-20 00:11:41 +000070
71ndk::ScopedAStatus NoiseSuppressionSw::getDescriptor(Descriptor* _aidl_return) {
72 LOG(DEBUG) << __func__ << kDescriptor.toString();
73 *_aidl_return = kDescriptor;
74 return ndk::ScopedAStatus::ok();
75}
76
77ndk::ScopedAStatus NoiseSuppressionSw::setParameterSpecific(const Parameter::Specific& specific) {
78 RETURN_IF(Parameter::Specific::noiseSuppression != specific.getTag(), EX_ILLEGAL_ARGUMENT,
79 "EffectNotSupported");
80 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
81
82 auto& param = specific.get<Parameter::Specific::noiseSuppression>();
83 auto tag = param.getTag();
84
85 switch (tag) {
86 case NoiseSuppression::level: {
87 RETURN_IF(mContext->setLevel(param.get<NoiseSuppression::level>()) != RetCode::SUCCESS,
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000088 EX_ILLEGAL_ARGUMENT, "levelNotSupported");
Shunkai Yaobd862b82022-12-20 00:11:41 +000089 return ndk::ScopedAStatus::ok();
90 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000091 case NoiseSuppression::type: {
92 RETURN_IF(mContext->setType(param.get<NoiseSuppression::type>()) != RetCode::SUCCESS,
93 EX_ILLEGAL_ARGUMENT, "typeNotSupported");
94 return ndk::ScopedAStatus::ok();
95 }
96 case NoiseSuppression::vendor: {
Shunkai Yaobd862b82022-12-20 00:11:41 +000097 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
98 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
99 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
100 }
101 }
102}
103
104ndk::ScopedAStatus NoiseSuppressionSw::getParameterSpecific(const Parameter::Id& id,
105 Parameter::Specific* specific) {
106 auto tag = id.getTag();
107 RETURN_IF(Parameter::Id::noiseSuppressionTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
108 auto specificId = id.get<Parameter::Id::noiseSuppressionTag>();
109 auto specificIdTag = specificId.getTag();
110 switch (specificIdTag) {
111 case NoiseSuppression::Id::commonTag:
112 return getParameterNoiseSuppression(specificId.get<NoiseSuppression::Id::commonTag>(),
113 specific);
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000114 case NoiseSuppression::Id::vendorExtensionTag: {
Shunkai Yaobd862b82022-12-20 00:11:41 +0000115 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
116 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
117 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000118 }
Shunkai Yaobd862b82022-12-20 00:11:41 +0000119 }
120}
121
122ndk::ScopedAStatus NoiseSuppressionSw::getParameterNoiseSuppression(
123 const NoiseSuppression::Tag& tag, Parameter::Specific* specific) {
124 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
125 NoiseSuppression param;
126 switch (tag) {
127 case NoiseSuppression::level: {
128 param.set<NoiseSuppression::level>(mContext->getLevel());
129 break;
130 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000131 case NoiseSuppression::type: {
132 param.set<NoiseSuppression::type>(mContext->getType());
133 break;
134 }
135 case NoiseSuppression::vendor: {
Shunkai Yaobd862b82022-12-20 00:11:41 +0000136 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
137 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
138 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
139 }
140 }
141
142 specific->set<Parameter::Specific::noiseSuppression>(param);
143 return ndk::ScopedAStatus::ok();
144}
145
146std::shared_ptr<EffectContext> NoiseSuppressionSw::createContext(const Parameter::Common& common) {
147 if (mContext) {
148 LOG(DEBUG) << __func__ << " context already exist";
149 } else {
150 mContext = std::make_shared<NoiseSuppressionSwContext>(1 /* statusFmqDepth */, common);
151 }
152 return mContext;
153}
154
155std::shared_ptr<EffectContext> NoiseSuppressionSw::getContext() {
156 return mContext;
157}
158
159RetCode NoiseSuppressionSw::releaseContext() {
160 if (mContext) {
161 mContext.reset();
162 }
163 return RetCode::SUCCESS;
164}
165
166// Processing method running in EffectWorker thread.
167IEffect::Status NoiseSuppressionSw::effectProcessImpl(float* in, float* out, int samples) {
168 // TODO: get data buffer and process.
169 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
170 for (int i = 0; i < samples; i++) {
171 *out++ = *in++;
172 }
173 return {STATUS_OK, samples, samples};
174}
175
176RetCode NoiseSuppressionSwContext::setLevel(NoiseSuppression::Level level) {
177 mLevel = level;
178 return RetCode::SUCCESS;
179}
180
181NoiseSuppression::Level NoiseSuppressionSwContext::getLevel() {
182 return mLevel;
183}
184
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000185RetCode NoiseSuppressionSwContext::setType(NoiseSuppression::Type type) {
186 mType = type;
187 return RetCode::SUCCESS;
188}
189
Shunkai Yaobd862b82022-12-20 00:11:41 +0000190} // namespace aidl::android::hardware::audio::effect