1 /*
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 #include <unordered_set>
21
22 #define LOG_TAG "AHAL_AcousticEchoCancelerSw"
23 #include <android-base/logging.h>
24 #include <fmq/AidlMessageQueue.h>
25 #include <system/audio_effects/effect_uuid.h>
26
27 #include "AcousticEchoCancelerSw.h"
28
29 using aidl::android::hardware::audio::effect::AcousticEchoCancelerSw;
30 using aidl::android::hardware::audio::effect::Descriptor;
31 using aidl::android::hardware::audio::effect::getEffectImplUuidAcousticEchoCancelerSw;
32 using aidl::android::hardware::audio::effect::getEffectTypeUuidAcousticEchoCanceler;
33 using aidl::android::hardware::audio::effect::IEffect;
34 using aidl::android::hardware::audio::effect::Range;
35 using aidl::android::media::audio::common::AudioUuid;
36
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)37 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
38 std::shared_ptr<IEffect>* instanceSpp) {
39 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidAcousticEchoCancelerSw()) {
40 LOG(ERROR) << __func__ << "uuid not supported";
41 return EX_ILLEGAL_ARGUMENT;
42 }
43 if (instanceSpp) {
44 *instanceSpp = ndk::SharedRefBase::make<AcousticEchoCancelerSw>();
45 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
46 return EX_NONE;
47 } else {
48 LOG(ERROR) << __func__ << " invalid input parameter!";
49 return EX_ILLEGAL_ARGUMENT;
50 }
51 }
52
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)53 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
54 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidAcousticEchoCancelerSw()) {
55 LOG(ERROR) << __func__ << "uuid not supported";
56 return EX_ILLEGAL_ARGUMENT;
57 }
58 *_aidl_return = AcousticEchoCancelerSw::kDescriptor;
59 return EX_NONE;
60 }
61
62 namespace aidl::android::hardware::audio::effect {
63
64 const std::string AcousticEchoCancelerSw::kEffectName = "AcousticEchoCancelerSw";
65
66 const std::vector<Range::AcousticEchoCancelerRange> AcousticEchoCancelerSw::kRanges = {
67 MAKE_RANGE(AcousticEchoCanceler, echoDelayUs, 0, 500),
68 /* mobile mode not supported, and not settable */
69 MAKE_RANGE(AcousticEchoCanceler, mobileMode, false, false)};
70
71 const Capability AcousticEchoCancelerSw::kCapability = {.range = AcousticEchoCancelerSw::kRanges};
72
73 const Descriptor AcousticEchoCancelerSw::kDescriptor = {
74 .common = {.id = {.type = getEffectTypeUuidAcousticEchoCanceler(),
75 .uuid = getEffectImplUuidAcousticEchoCancelerSw(),
76 .proxy = std::nullopt},
77 .flags = {.type = Flags::Type::PRE_PROC,
78 .insert = Flags::Insert::FIRST,
79 .volume = Flags::Volume::NONE},
80 .name = AcousticEchoCancelerSw::kEffectName,
81 .implementor = "The Android Open Source Project"},
82 .capability = AcousticEchoCancelerSw::kCapability};
83
getDescriptor(Descriptor * _aidl_return)84 ndk::ScopedAStatus AcousticEchoCancelerSw::getDescriptor(Descriptor* _aidl_return) {
85 LOG(DEBUG) << __func__ << kDescriptor.toString();
86 *_aidl_return = kDescriptor;
87 return ndk::ScopedAStatus::ok();
88 }
89
setParameterSpecific(const Parameter::Specific & specific)90 ndk::ScopedAStatus AcousticEchoCancelerSw::setParameterSpecific(
91 const Parameter::Specific& specific) {
92 RETURN_IF(Parameter::Specific::acousticEchoCanceler != specific.getTag(), EX_ILLEGAL_ARGUMENT,
93 "EffectNotSupported");
94 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
95
96 auto& param = specific.get<Parameter::Specific::acousticEchoCanceler>();
97 RETURN_IF(!inRange(param, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
98
99 auto tag = param.getTag();
100 switch (tag) {
101 case AcousticEchoCanceler::echoDelayUs: {
102 RETURN_IF(mContext->setEchoDelay(param.get<AcousticEchoCanceler::echoDelayUs>()) !=
103 RetCode::SUCCESS,
104 EX_ILLEGAL_ARGUMENT, "echoDelayNotSupported");
105 return ndk::ScopedAStatus::ok();
106 }
107 case AcousticEchoCanceler::mobileMode: {
108 RETURN_IF(true == param.get<AcousticEchoCanceler::mobileMode>(), EX_ILLEGAL_ARGUMENT,
109 "SettingmobileModeSupported");
110 return ndk::ScopedAStatus::ok();
111 }
112 default: {
113 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
114 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
115 EX_ILLEGAL_ARGUMENT, "AcousticEchoCancelerTagNotSupported");
116 }
117 }
118 }
119
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)120 ndk::ScopedAStatus AcousticEchoCancelerSw::getParameterSpecific(const Parameter::Id& id,
121 Parameter::Specific* specific) {
122 auto tag = id.getTag();
123 RETURN_IF(Parameter::Id::acousticEchoCancelerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
124 auto specificId = id.get<Parameter::Id::acousticEchoCancelerTag>();
125 auto specificIdTag = specificId.getTag();
126 switch (specificIdTag) {
127 case AcousticEchoCanceler::Id::commonTag:
128 return getParameterAcousticEchoCanceler(
129 specificId.get<AcousticEchoCanceler::Id::commonTag>(), specific);
130 default:
131 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
132 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
133 EX_ILLEGAL_ARGUMENT, "AcousticEchoCancelerTagNotSupported");
134 }
135 }
136
getParameterAcousticEchoCanceler(const AcousticEchoCanceler::Tag & tag,Parameter::Specific * specific)137 ndk::ScopedAStatus AcousticEchoCancelerSw::getParameterAcousticEchoCanceler(
138 const AcousticEchoCanceler::Tag& tag, Parameter::Specific* specific) {
139 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
140 AcousticEchoCanceler param;
141 switch (tag) {
142 case AcousticEchoCanceler::echoDelayUs: {
143 param.set<AcousticEchoCanceler::echoDelayUs>(mContext->getEchoDelay());
144 break;
145 }
146 case AcousticEchoCanceler::mobileMode: {
147 param.set<AcousticEchoCanceler::mobileMode>(false);
148 break;
149 }
150 default: {
151 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
152 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
153 EX_ILLEGAL_ARGUMENT, "AcousticEchoCancelerTagNotSupported");
154 }
155 }
156
157 specific->set<Parameter::Specific::acousticEchoCanceler>(param);
158 return ndk::ScopedAStatus::ok();
159 }
160
createContext(const Parameter::Common & common)161 std::shared_ptr<EffectContext> AcousticEchoCancelerSw::createContext(
162 const Parameter::Common& common) {
163 if (mContext) {
164 LOG(DEBUG) << __func__ << " context already exist";
165 } else {
166 mContext = std::make_shared<AcousticEchoCancelerSwContext>(1 /* statusFmqDepth */, common);
167 }
168 return mContext;
169 }
170
releaseContext()171 RetCode AcousticEchoCancelerSw::releaseContext() {
172 if (mContext) {
173 mContext.reset();
174 }
175 return RetCode::SUCCESS;
176 }
177
178 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)179 IEffect::Status AcousticEchoCancelerSw::effectProcessImpl(float* in, float* out, int samples) {
180 // TODO: get data buffer and process.
181 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
182 for (int i = 0; i < samples; i++) {
183 *out++ = *in++;
184 }
185 return {STATUS_OK, samples, samples};
186 }
187
setEchoDelay(int echoDelayUs)188 RetCode AcousticEchoCancelerSwContext::setEchoDelay(int echoDelayUs) {
189 mEchoDelayUs = echoDelayUs;
190 return RetCode::SUCCESS;
191 }
192
193 } // namespace aidl::android::hardware::audio::effect
194