blob: 51fe4ea8367d42fd5e16b3c72d3f22c39631da35 [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";
63const NoiseSuppression::Capability NoiseSuppressionSw::kCapability;
64const Descriptor NoiseSuppressionSw::kDescriptor = {
65 .common = {.id = {.type = kNoiseSuppressionTypeUUID,
66 .uuid = kNoiseSuppressionSwImplUUID,
67 .proxy = std::nullopt},
68 .flags = {.type = Flags::Type::INSERT,
69 .insert = Flags::Insert::FIRST,
70 .volume = Flags::Volume::CTRL},
71 .name = NoiseSuppressionSw::kEffectName,
72 .implementor = "The Android Open Source Project"},
73 .capability =
74 Capability::make<Capability::noiseSuppression>(NoiseSuppressionSw::kCapability)};
75
76ndk::ScopedAStatus NoiseSuppressionSw::getDescriptor(Descriptor* _aidl_return) {
77 LOG(DEBUG) << __func__ << kDescriptor.toString();
78 *_aidl_return = kDescriptor;
79 return ndk::ScopedAStatus::ok();
80}
81
82ndk::ScopedAStatus NoiseSuppressionSw::setParameterSpecific(const Parameter::Specific& specific) {
83 RETURN_IF(Parameter::Specific::noiseSuppression != specific.getTag(), EX_ILLEGAL_ARGUMENT,
84 "EffectNotSupported");
85 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
86
87 auto& param = specific.get<Parameter::Specific::noiseSuppression>();
88 auto tag = param.getTag();
89
90 switch (tag) {
91 case NoiseSuppression::level: {
92 RETURN_IF(mContext->setLevel(param.get<NoiseSuppression::level>()) != RetCode::SUCCESS,
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000093 EX_ILLEGAL_ARGUMENT, "levelNotSupported");
Shunkai Yaobd862b82022-12-20 00:11:41 +000094 return ndk::ScopedAStatus::ok();
95 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000096 case NoiseSuppression::type: {
97 RETURN_IF(mContext->setType(param.get<NoiseSuppression::type>()) != RetCode::SUCCESS,
98 EX_ILLEGAL_ARGUMENT, "typeNotSupported");
99 return ndk::ScopedAStatus::ok();
100 }
101 case NoiseSuppression::vendor: {
Shunkai Yaobd862b82022-12-20 00:11:41 +0000102 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
103 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
104 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
105 }
106 }
107}
108
109ndk::ScopedAStatus NoiseSuppressionSw::getParameterSpecific(const Parameter::Id& id,
110 Parameter::Specific* specific) {
111 auto tag = id.getTag();
112 RETURN_IF(Parameter::Id::noiseSuppressionTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
113 auto specificId = id.get<Parameter::Id::noiseSuppressionTag>();
114 auto specificIdTag = specificId.getTag();
115 switch (specificIdTag) {
116 case NoiseSuppression::Id::commonTag:
117 return getParameterNoiseSuppression(specificId.get<NoiseSuppression::Id::commonTag>(),
118 specific);
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000119 case NoiseSuppression::Id::vendorExtensionTag: {
Shunkai Yaobd862b82022-12-20 00:11:41 +0000120 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
121 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
122 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000123 }
Shunkai Yaobd862b82022-12-20 00:11:41 +0000124 }
125}
126
127ndk::ScopedAStatus NoiseSuppressionSw::getParameterNoiseSuppression(
128 const NoiseSuppression::Tag& tag, Parameter::Specific* specific) {
129 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
130 NoiseSuppression param;
131 switch (tag) {
132 case NoiseSuppression::level: {
133 param.set<NoiseSuppression::level>(mContext->getLevel());
134 break;
135 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000136 case NoiseSuppression::type: {
137 param.set<NoiseSuppression::type>(mContext->getType());
138 break;
139 }
140 case NoiseSuppression::vendor: {
Shunkai Yaobd862b82022-12-20 00:11:41 +0000141 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
142 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
143 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
144 }
145 }
146
147 specific->set<Parameter::Specific::noiseSuppression>(param);
148 return ndk::ScopedAStatus::ok();
149}
150
151std::shared_ptr<EffectContext> NoiseSuppressionSw::createContext(const Parameter::Common& common) {
152 if (mContext) {
153 LOG(DEBUG) << __func__ << " context already exist";
154 } else {
155 mContext = std::make_shared<NoiseSuppressionSwContext>(1 /* statusFmqDepth */, common);
156 }
157 return mContext;
158}
159
160std::shared_ptr<EffectContext> NoiseSuppressionSw::getContext() {
161 return mContext;
162}
163
164RetCode NoiseSuppressionSw::releaseContext() {
165 if (mContext) {
166 mContext.reset();
167 }
168 return RetCode::SUCCESS;
169}
170
171// Processing method running in EffectWorker thread.
172IEffect::Status NoiseSuppressionSw::effectProcessImpl(float* in, float* out, int samples) {
173 // TODO: get data buffer and process.
174 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
175 for (int i = 0; i < samples; i++) {
176 *out++ = *in++;
177 }
178 return {STATUS_OK, samples, samples};
179}
180
181RetCode NoiseSuppressionSwContext::setLevel(NoiseSuppression::Level level) {
182 mLevel = level;
183 return RetCode::SUCCESS;
184}
185
186NoiseSuppression::Level NoiseSuppressionSwContext::getLevel() {
187 return mLevel;
188}
189
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000190RetCode NoiseSuppressionSwContext::setType(NoiseSuppression::Type type) {
191 mType = type;
192 return RetCode::SUCCESS;
193}
194
Shunkai Yaobd862b82022-12-20 00:11:41 +0000195} // namespace aidl::android::hardware::audio::effect