blob: a36cfe00a8aa524fb553ad25ebad53dd63a01938 [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,
93 EX_ILLEGAL_ARGUMENT, "levelSupported");
94 return ndk::ScopedAStatus::ok();
95 }
96 default: {
97 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);
114 default:
115 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
116 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
117 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
118 }
119}
120
121ndk::ScopedAStatus NoiseSuppressionSw::getParameterNoiseSuppression(
122 const NoiseSuppression::Tag& tag, Parameter::Specific* specific) {
123 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
124 NoiseSuppression param;
125 switch (tag) {
126 case NoiseSuppression::level: {
127 param.set<NoiseSuppression::level>(mContext->getLevel());
128 break;
129 }
130 default: {
131 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
132 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
133 EX_ILLEGAL_ARGUMENT, "NoiseSuppressionTagNotSupported");
134 }
135 }
136
137 specific->set<Parameter::Specific::noiseSuppression>(param);
138 return ndk::ScopedAStatus::ok();
139}
140
141std::shared_ptr<EffectContext> NoiseSuppressionSw::createContext(const Parameter::Common& common) {
142 if (mContext) {
143 LOG(DEBUG) << __func__ << " context already exist";
144 } else {
145 mContext = std::make_shared<NoiseSuppressionSwContext>(1 /* statusFmqDepth */, common);
146 }
147 return mContext;
148}
149
150std::shared_ptr<EffectContext> NoiseSuppressionSw::getContext() {
151 return mContext;
152}
153
154RetCode NoiseSuppressionSw::releaseContext() {
155 if (mContext) {
156 mContext.reset();
157 }
158 return RetCode::SUCCESS;
159}
160
161// Processing method running in EffectWorker thread.
162IEffect::Status NoiseSuppressionSw::effectProcessImpl(float* in, float* out, int samples) {
163 // TODO: get data buffer and process.
164 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
165 for (int i = 0; i < samples; i++) {
166 *out++ = *in++;
167 }
168 return {STATUS_OK, samples, samples};
169}
170
171RetCode NoiseSuppressionSwContext::setLevel(NoiseSuppression::Level level) {
172 mLevel = level;
173 return RetCode::SUCCESS;
174}
175
176NoiseSuppression::Level NoiseSuppressionSwContext::getLevel() {
177 return mLevel;
178}
179
180} // namespace aidl::android::hardware::audio::effect