blob: 5e18f1b12f7599d526b0f1378016670d67f66d91 [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>
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000025#include <system/audio_effects/effect_uuid.h>
Shunkai Yaobd862b82022-12-20 00:11:41 +000026
27#include "AcousticEchoCancelerSw.h"
28
29using aidl::android::hardware::audio::effect::AcousticEchoCancelerSw;
30using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000031using aidl::android::hardware::audio::effect::getEffectImplUuidAcousticEchoCancelerSw;
32using aidl::android::hardware::audio::effect::getEffectTypeUuidAcousticEchoCanceler;
Shunkai Yaobd862b82022-12-20 00:11:41 +000033using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao87811022023-02-13 17:40:37 +000034using aidl::android::hardware::audio::effect::Range;
Shunkai Yaobd862b82022-12-20 00:11:41 +000035using aidl::android::media::audio::common::AudioUuid;
36
37extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
38 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000039 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidAcousticEchoCancelerSw()) {
Shunkai Yaobd862b82022-12-20 00:11:41 +000040 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
53extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000054 if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidAcousticEchoCancelerSw()) {
Shunkai Yaobd862b82022-12-20 00:11:41 +000055 LOG(ERROR) << __func__ << "uuid not supported";
56 return EX_ILLEGAL_ARGUMENT;
57 }
58 *_aidl_return = AcousticEchoCancelerSw::kDescriptor;
Shunkai Yaobd862b82022-12-20 00:11:41 +000059 return EX_NONE;
60}
61
62namespace aidl::android::hardware::audio::effect {
63
64const std::string AcousticEchoCancelerSw::kEffectName = "AcousticEchoCancelerSw";
Shunkai Yao87811022023-02-13 17:40:37 +000065
66const 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
71const Capability AcousticEchoCancelerSw::kCapability = {.range = AcousticEchoCancelerSw::kRanges};
72
Shunkai Yaobd862b82022-12-20 00:11:41 +000073const Descriptor AcousticEchoCancelerSw::kDescriptor = {
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000074 .common = {.id = {.type = getEffectTypeUuidAcousticEchoCanceler(),
75 .uuid = getEffectImplUuidAcousticEchoCancelerSw(),
Shunkai Yaobd862b82022-12-20 00:11:41 +000076 .proxy = std::nullopt},
Shunkai Yao12179c32023-10-18 01:15:37 +000077 .flags = {.type = Flags::Type::PRE_PROC,
Shunkai Yaobd862b82022-12-20 00:11:41 +000078 .insert = Flags::Insert::FIRST,
David Li1a56fdd2023-11-14 23:31:53 +080079 .volume = Flags::Volume::NONE},
Shunkai Yaobd862b82022-12-20 00:11:41 +000080 .name = AcousticEchoCancelerSw::kEffectName,
81 .implementor = "The Android Open Source Project"},
Shunkai Yao87811022023-02-13 17:40:37 +000082 .capability = AcousticEchoCancelerSw::kCapability};
Shunkai Yaobd862b82022-12-20 00:11:41 +000083
84ndk::ScopedAStatus AcousticEchoCancelerSw::getDescriptor(Descriptor* _aidl_return) {
85 LOG(DEBUG) << __func__ << kDescriptor.toString();
86 *_aidl_return = kDescriptor;
87 return ndk::ScopedAStatus::ok();
88}
89
90ndk::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>();
Shunkai Yao87811022023-02-13 17:40:37 +000097 RETURN_IF(!inRange(param, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
Shunkai Yaobd862b82022-12-20 00:11:41 +000098
Shunkai Yao87811022023-02-13 17:40:37 +000099 auto tag = param.getTag();
Shunkai Yaobd862b82022-12-20 00:11:41 +0000100 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
120ndk::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
137ndk::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
161std::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
171std::shared_ptr<EffectContext> AcousticEchoCancelerSw::getContext() {
172 return mContext;
173}
174
175RetCode AcousticEchoCancelerSw::releaseContext() {
176 if (mContext) {
177 mContext.reset();
178 }
179 return RetCode::SUCCESS;
180}
181
182// Processing method running in EffectWorker thread.
183IEffect::Status AcousticEchoCancelerSw::effectProcessImpl(float* in, float* out, int samples) {
184 // TODO: get data buffer and process.
185 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
186 for (int i = 0; i < samples; i++) {
187 *out++ = *in++;
188 }
189 return {STATUS_OK, samples, samples};
190}
191
192RetCode AcousticEchoCancelerSwContext::setEchoDelay(int echoDelayUs) {
Shunkai Yaobd862b82022-12-20 00:11:41 +0000193 mEchoDelayUs = echoDelayUs;
194 return RetCode::SUCCESS;
195}
196
197} // namespace aidl::android::hardware::audio::effect