Shunkai Yao | bd862b8 | 2022-12-20 00:11:41 +0000 | [diff] [blame^] | 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 | #define LOG_TAG "AHAL_AcousticEchoCancelerSw" |
| 21 | #include <Utils.h> |
| 22 | #include <unordered_set> |
| 23 | |
| 24 | #include <android-base/logging.h> |
| 25 | #include <fmq/AidlMessageQueue.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::IEffect; |
| 32 | using aidl::android::hardware::audio::effect::kAcousticEchoCancelerSwImplUUID; |
| 33 | using aidl::android::media::audio::common::AudioUuid; |
| 34 | |
| 35 | extern "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 | |
| 51 | extern "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; |
| 57 | LOG(ERROR) << __func__ << "xxx " << _aidl_return->toString(); |
| 58 | return EX_NONE; |
| 59 | } |
| 60 | |
| 61 | namespace aidl::android::hardware::audio::effect { |
| 62 | |
| 63 | const std::string AcousticEchoCancelerSw::kEffectName = "AcousticEchoCancelerSw"; |
| 64 | const AcousticEchoCanceler::Capability AcousticEchoCancelerSw::kCapability = { |
| 65 | .maxEchoDelayUs = 500, .supportMobileMode = false}; |
| 66 | const Descriptor AcousticEchoCancelerSw::kDescriptor = { |
| 67 | .common = {.id = {.type = kAcousticEchoCancelerTypeUUID, |
| 68 | .uuid = kAcousticEchoCancelerSwImplUUID, |
| 69 | .proxy = std::nullopt}, |
| 70 | .flags = {.type = Flags::Type::INSERT, |
| 71 | .insert = Flags::Insert::FIRST, |
| 72 | .volume = Flags::Volume::CTRL}, |
| 73 | .name = AcousticEchoCancelerSw::kEffectName, |
| 74 | .implementor = "The Android Open Source Project"}, |
| 75 | .capability = Capability::make<Capability::acousticEchoCanceler>( |
| 76 | AcousticEchoCancelerSw::kCapability)}; |
| 77 | |
| 78 | ndk::ScopedAStatus AcousticEchoCancelerSw::getDescriptor(Descriptor* _aidl_return) { |
| 79 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 80 | *_aidl_return = kDescriptor; |
| 81 | return ndk::ScopedAStatus::ok(); |
| 82 | } |
| 83 | |
| 84 | ndk::ScopedAStatus AcousticEchoCancelerSw::setParameterSpecific( |
| 85 | const Parameter::Specific& specific) { |
| 86 | RETURN_IF(Parameter::Specific::acousticEchoCanceler != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 87 | "EffectNotSupported"); |
| 88 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 89 | |
| 90 | auto& param = specific.get<Parameter::Specific::acousticEchoCanceler>(); |
| 91 | auto tag = param.getTag(); |
| 92 | |
| 93 | switch (tag) { |
| 94 | case AcousticEchoCanceler::echoDelayUs: { |
| 95 | RETURN_IF(mContext->setEchoDelay(param.get<AcousticEchoCanceler::echoDelayUs>()) != |
| 96 | RetCode::SUCCESS, |
| 97 | EX_ILLEGAL_ARGUMENT, "echoDelayNotSupported"); |
| 98 | return ndk::ScopedAStatus::ok(); |
| 99 | } |
| 100 | case AcousticEchoCanceler::mobileMode: { |
| 101 | RETURN_IF(true == param.get<AcousticEchoCanceler::mobileMode>(), EX_ILLEGAL_ARGUMENT, |
| 102 | "SettingmobileModeSupported"); |
| 103 | return ndk::ScopedAStatus::ok(); |
| 104 | } |
| 105 | default: { |
| 106 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 107 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 108 | EX_ILLEGAL_ARGUMENT, "AcousticEchoCancelerTagNotSupported"); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | ndk::ScopedAStatus AcousticEchoCancelerSw::getParameterSpecific(const Parameter::Id& id, |
| 114 | Parameter::Specific* specific) { |
| 115 | auto tag = id.getTag(); |
| 116 | RETURN_IF(Parameter::Id::acousticEchoCancelerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
| 117 | auto specificId = id.get<Parameter::Id::acousticEchoCancelerTag>(); |
| 118 | auto specificIdTag = specificId.getTag(); |
| 119 | switch (specificIdTag) { |
| 120 | case AcousticEchoCanceler::Id::commonTag: |
| 121 | return getParameterAcousticEchoCanceler( |
| 122 | specificId.get<AcousticEchoCanceler::Id::commonTag>(), specific); |
| 123 | default: |
| 124 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 125 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 126 | EX_ILLEGAL_ARGUMENT, "AcousticEchoCancelerTagNotSupported"); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | ndk::ScopedAStatus AcousticEchoCancelerSw::getParameterAcousticEchoCanceler( |
| 131 | const AcousticEchoCanceler::Tag& tag, Parameter::Specific* specific) { |
| 132 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 133 | AcousticEchoCanceler param; |
| 134 | switch (tag) { |
| 135 | case AcousticEchoCanceler::echoDelayUs: { |
| 136 | param.set<AcousticEchoCanceler::echoDelayUs>(mContext->getEchoDelay()); |
| 137 | break; |
| 138 | } |
| 139 | case AcousticEchoCanceler::mobileMode: { |
| 140 | param.set<AcousticEchoCanceler::mobileMode>(false); |
| 141 | break; |
| 142 | } |
| 143 | default: { |
| 144 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 145 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 146 | EX_ILLEGAL_ARGUMENT, "AcousticEchoCancelerTagNotSupported"); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | specific->set<Parameter::Specific::acousticEchoCanceler>(param); |
| 151 | return ndk::ScopedAStatus::ok(); |
| 152 | } |
| 153 | |
| 154 | std::shared_ptr<EffectContext> AcousticEchoCancelerSw::createContext( |
| 155 | const Parameter::Common& common) { |
| 156 | if (mContext) { |
| 157 | LOG(DEBUG) << __func__ << " context already exist"; |
| 158 | } else { |
| 159 | mContext = std::make_shared<AcousticEchoCancelerSwContext>(1 /* statusFmqDepth */, common); |
| 160 | } |
| 161 | return mContext; |
| 162 | } |
| 163 | |
| 164 | std::shared_ptr<EffectContext> AcousticEchoCancelerSw::getContext() { |
| 165 | return mContext; |
| 166 | } |
| 167 | |
| 168 | RetCode AcousticEchoCancelerSw::releaseContext() { |
| 169 | if (mContext) { |
| 170 | mContext.reset(); |
| 171 | } |
| 172 | return RetCode::SUCCESS; |
| 173 | } |
| 174 | |
| 175 | // Processing method running in EffectWorker thread. |
| 176 | IEffect::Status AcousticEchoCancelerSw::effectProcessImpl(float* in, float* out, int samples) { |
| 177 | // TODO: get data buffer and process. |
| 178 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 179 | for (int i = 0; i < samples; i++) { |
| 180 | *out++ = *in++; |
| 181 | } |
| 182 | return {STATUS_OK, samples, samples}; |
| 183 | } |
| 184 | |
| 185 | RetCode AcousticEchoCancelerSwContext::setEchoDelay(int echoDelayUs) { |
| 186 | if (echoDelayUs < 0 || echoDelayUs > AcousticEchoCancelerSw::kCapability.maxEchoDelayUs) { |
| 187 | LOG(DEBUG) << __func__ << " illegal delay " << echoDelayUs; |
| 188 | return RetCode::ERROR_ILLEGAL_PARAMETER; |
| 189 | } |
| 190 | mEchoDelayUs = echoDelayUs; |
| 191 | return RetCode::SUCCESS; |
| 192 | } |
| 193 | |
| 194 | } // namespace aidl::android::hardware::audio::effect |