blob: 561f9a32ad93a11eaeb8d95fc589c12f2ebde9c9 [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#include <unordered_set>
21
Mikhail Naganov872d4a62023-03-09 18:19:01 -080022#define LOG_TAG "AHAL_AcousticEchoCancelerSw"
Shunkai Yaobd862b82022-12-20 00:11:41 +000023#include <android-base/logging.h>
24#include <fmq/AidlMessageQueue.h>
25
26#include "AcousticEchoCancelerSw.h"
27
28using aidl::android::hardware::audio::effect::AcousticEchoCancelerSw;
29using aidl::android::hardware::audio::effect::Descriptor;
30using aidl::android::hardware::audio::effect::IEffect;
31using aidl::android::hardware::audio::effect::kAcousticEchoCancelerSwImplUUID;
Shunkai Yao87811022023-02-13 17:40:37 +000032using aidl::android::hardware::audio::effect::Range;
Shunkai Yaobd862b82022-12-20 00:11:41 +000033using 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 != kAcousticEchoCancelerSwImplUUID) {
38 LOG(ERROR) << __func__ << "uuid not supported";
39 return EX_ILLEGAL_ARGUMENT;
40 }
41 if (instanceSpp) {
42 *instanceSpp = ndk::SharedRefBase::make<AcousticEchoCancelerSw>();
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 != kAcousticEchoCancelerSwImplUUID) {
53 LOG(ERROR) << __func__ << "uuid not supported";
54 return EX_ILLEGAL_ARGUMENT;
55 }
56 *_aidl_return = AcousticEchoCancelerSw::kDescriptor;
Shunkai Yaobd862b82022-12-20 00:11:41 +000057 return EX_NONE;
58}
59
60namespace aidl::android::hardware::audio::effect {
61
62const std::string AcousticEchoCancelerSw::kEffectName = "AcousticEchoCancelerSw";
Shunkai Yao87811022023-02-13 17:40:37 +000063
64const std::vector<Range::AcousticEchoCancelerRange> AcousticEchoCancelerSw::kRanges = {
65 MAKE_RANGE(AcousticEchoCanceler, echoDelayUs, 0, 500),
66 /* mobile mode not supported, and not settable */
67 MAKE_RANGE(AcousticEchoCanceler, mobileMode, false, false)};
68
69const Capability AcousticEchoCancelerSw::kCapability = {.range = AcousticEchoCancelerSw::kRanges};
70
Shunkai Yaobd862b82022-12-20 00:11:41 +000071const Descriptor AcousticEchoCancelerSw::kDescriptor = {
72 .common = {.id = {.type = kAcousticEchoCancelerTypeUUID,
73 .uuid = kAcousticEchoCancelerSwImplUUID,
74 .proxy = std::nullopt},
75 .flags = {.type = Flags::Type::INSERT,
76 .insert = Flags::Insert::FIRST,
77 .volume = Flags::Volume::CTRL},
78 .name = AcousticEchoCancelerSw::kEffectName,
79 .implementor = "The Android Open Source Project"},
Shunkai Yao87811022023-02-13 17:40:37 +000080 .capability = AcousticEchoCancelerSw::kCapability};
Shunkai Yaobd862b82022-12-20 00:11:41 +000081
82ndk::ScopedAStatus AcousticEchoCancelerSw::getDescriptor(Descriptor* _aidl_return) {
83 LOG(DEBUG) << __func__ << kDescriptor.toString();
84 *_aidl_return = kDescriptor;
85 return ndk::ScopedAStatus::ok();
86}
87
88ndk::ScopedAStatus AcousticEchoCancelerSw::setParameterSpecific(
89 const Parameter::Specific& specific) {
90 RETURN_IF(Parameter::Specific::acousticEchoCanceler != specific.getTag(), EX_ILLEGAL_ARGUMENT,
91 "EffectNotSupported");
92 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
93
94 auto& param = specific.get<Parameter::Specific::acousticEchoCanceler>();
Shunkai Yao87811022023-02-13 17:40:37 +000095 RETURN_IF(!inRange(param, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
Shunkai Yaobd862b82022-12-20 00:11:41 +000096
Shunkai Yao87811022023-02-13 17:40:37 +000097 auto tag = param.getTag();
Shunkai Yaobd862b82022-12-20 00:11:41 +000098 switch (tag) {
99 case AcousticEchoCanceler::echoDelayUs: {
100 RETURN_IF(mContext->setEchoDelay(param.get<AcousticEchoCanceler::echoDelayUs>()) !=
101 RetCode::SUCCESS,
102 EX_ILLEGAL_ARGUMENT, "echoDelayNotSupported");
103 return ndk::ScopedAStatus::ok();
104 }
105 case AcousticEchoCanceler::mobileMode: {
106 RETURN_IF(true == param.get<AcousticEchoCanceler::mobileMode>(), EX_ILLEGAL_ARGUMENT,
107 "SettingmobileModeSupported");
108 return ndk::ScopedAStatus::ok();
109 }
110 default: {
111 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
112 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
113 EX_ILLEGAL_ARGUMENT, "AcousticEchoCancelerTagNotSupported");
114 }
115 }
116}
117
118ndk::ScopedAStatus AcousticEchoCancelerSw::getParameterSpecific(const Parameter::Id& id,
119 Parameter::Specific* specific) {
120 auto tag = id.getTag();
121 RETURN_IF(Parameter::Id::acousticEchoCancelerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
122 auto specificId = id.get<Parameter::Id::acousticEchoCancelerTag>();
123 auto specificIdTag = specificId.getTag();
124 switch (specificIdTag) {
125 case AcousticEchoCanceler::Id::commonTag:
126 return getParameterAcousticEchoCanceler(
127 specificId.get<AcousticEchoCanceler::Id::commonTag>(), specific);
128 default:
129 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
130 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
131 EX_ILLEGAL_ARGUMENT, "AcousticEchoCancelerTagNotSupported");
132 }
133}
134
135ndk::ScopedAStatus AcousticEchoCancelerSw::getParameterAcousticEchoCanceler(
136 const AcousticEchoCanceler::Tag& tag, Parameter::Specific* specific) {
137 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
138 AcousticEchoCanceler param;
139 switch (tag) {
140 case AcousticEchoCanceler::echoDelayUs: {
141 param.set<AcousticEchoCanceler::echoDelayUs>(mContext->getEchoDelay());
142 break;
143 }
144 case AcousticEchoCanceler::mobileMode: {
145 param.set<AcousticEchoCanceler::mobileMode>(false);
146 break;
147 }
148 default: {
149 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
150 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
151 EX_ILLEGAL_ARGUMENT, "AcousticEchoCancelerTagNotSupported");
152 }
153 }
154
155 specific->set<Parameter::Specific::acousticEchoCanceler>(param);
156 return ndk::ScopedAStatus::ok();
157}
158
159std::shared_ptr<EffectContext> AcousticEchoCancelerSw::createContext(
160 const Parameter::Common& common) {
161 if (mContext) {
162 LOG(DEBUG) << __func__ << " context already exist";
163 } else {
164 mContext = std::make_shared<AcousticEchoCancelerSwContext>(1 /* statusFmqDepth */, common);
165 }
166 return mContext;
167}
168
169std::shared_ptr<EffectContext> AcousticEchoCancelerSw::getContext() {
170 return mContext;
171}
172
173RetCode AcousticEchoCancelerSw::releaseContext() {
174 if (mContext) {
175 mContext.reset();
176 }
177 return RetCode::SUCCESS;
178}
179
180// Processing method running in EffectWorker thread.
181IEffect::Status AcousticEchoCancelerSw::effectProcessImpl(float* in, float* out, int samples) {
182 // TODO: get data buffer and process.
183 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
184 for (int i = 0; i < samples; i++) {
185 *out++ = *in++;
186 }
187 return {STATUS_OK, samples, samples};
188}
189
190RetCode AcousticEchoCancelerSwContext::setEchoDelay(int echoDelayUs) {
Shunkai Yaobd862b82022-12-20 00:11:41 +0000191 mEchoDelayUs = echoDelayUs;
192 return RetCode::SUCCESS;
193}
194
195} // namespace aidl::android::hardware::audio::effect