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