Shraddha Basantwani | 4e2a8735 | 2022-12-08 16:05:21 +0530 | [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 | #define LOG_TAG "AHAL_LoudnessEnhancerImpl" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | |
| 21 | #include "EffectLoudnessEnhancer.h" |
| 22 | |
| 23 | using aidl::android::hardware::audio::effect::Descriptor; |
| 24 | using aidl::android::hardware::audio::effect::IEffect; |
| 25 | using aidl::android::hardware::audio::effect::kLoudnessEnhancerImplUUID; |
| 26 | using aidl::android::hardware::audio::effect::LoudnessEnhancerImpl; |
| 27 | using aidl::android::hardware::audio::effect::State; |
| 28 | using aidl::android::media::audio::common::AudioUuid; |
| 29 | |
| 30 | extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid, |
| 31 | std::shared_ptr<IEffect>* instanceSpp) { |
| 32 | if (!in_impl_uuid || *in_impl_uuid != kLoudnessEnhancerImplUUID) { |
| 33 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 34 | return EX_ILLEGAL_ARGUMENT; |
| 35 | } |
| 36 | if (instanceSpp) { |
| 37 | *instanceSpp = ndk::SharedRefBase::make<LoudnessEnhancerImpl>(); |
| 38 | LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created"; |
| 39 | return EX_NONE; |
| 40 | } else { |
| 41 | LOG(ERROR) << __func__ << " invalid input parameter!"; |
| 42 | return EX_ILLEGAL_ARGUMENT; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) { |
| 47 | if (!in_impl_uuid || *in_impl_uuid != kLoudnessEnhancerImplUUID) { |
| 48 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 49 | return EX_ILLEGAL_ARGUMENT; |
| 50 | } |
| 51 | *_aidl_return = LoudnessEnhancerImpl::kDescriptor; |
| 52 | return EX_NONE; |
| 53 | } |
| 54 | |
| 55 | namespace aidl::android::hardware::audio::effect { |
| 56 | |
| 57 | const std::string LoudnessEnhancerImpl::kEffectName = "Loudness Enhancer"; |
| 58 | const Descriptor LoudnessEnhancerImpl::kDescriptor = { |
| 59 | .common = {.id = {.type = kLoudnessEnhancerTypeUUID, |
| 60 | .uuid = kLoudnessEnhancerImplUUID, |
| 61 | .proxy = std::nullopt}, |
| 62 | .flags = {.type = Flags::Type::INSERT, .insert = Flags::Insert::FIRST}, |
| 63 | .name = LoudnessEnhancerImpl::kEffectName, |
| 64 | .implementor = "The Android Open Source Project"}}; |
| 65 | |
| 66 | ndk::ScopedAStatus LoudnessEnhancerImpl::getDescriptor(Descriptor* _aidl_return) { |
| 67 | RETURN_IF(!_aidl_return, EX_ILLEGAL_ARGUMENT, "Parameter:nullptr"); |
| 68 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 69 | *_aidl_return = kDescriptor; |
| 70 | return ndk::ScopedAStatus::ok(); |
| 71 | } |
| 72 | |
| 73 | ndk::ScopedAStatus LoudnessEnhancerImpl::commandImpl(CommandId command) { |
| 74 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 75 | switch (command) { |
| 76 | case CommandId::START: |
| 77 | mContext->enable(); |
| 78 | break; |
| 79 | case CommandId::STOP: |
| 80 | mContext->disable(); |
| 81 | break; |
| 82 | case CommandId::RESET: |
| 83 | mContext->disable(); |
| 84 | mContext->resetBuffer(); |
| 85 | break; |
| 86 | default: |
| 87 | LOG(ERROR) << __func__ << " commandId " << toString(command) << " not supported"; |
| 88 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 89 | "commandIdNotSupported"); |
| 90 | } |
| 91 | return ndk::ScopedAStatus::ok(); |
| 92 | } |
| 93 | |
| 94 | ndk::ScopedAStatus LoudnessEnhancerImpl::setParameterSpecific(const Parameter::Specific& specific) { |
| 95 | RETURN_IF(Parameter::Specific::loudnessEnhancer != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 96 | "EffectNotSupported"); |
| 97 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 98 | |
| 99 | auto& leParam = specific.get<Parameter::Specific::loudnessEnhancer>(); |
| 100 | auto tag = leParam.getTag(); |
| 101 | |
| 102 | switch (tag) { |
| 103 | case LoudnessEnhancer::gainMb: { |
| 104 | RETURN_IF(mContext->setLeGain(leParam.get<LoudnessEnhancer::gainMb>()) != |
| 105 | RetCode::SUCCESS, |
| 106 | EX_ILLEGAL_ARGUMENT, "setGainMbFailed"); |
| 107 | return ndk::ScopedAStatus::ok(); |
| 108 | } |
| 109 | default: { |
| 110 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 111 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 112 | EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported"); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | ndk::ScopedAStatus LoudnessEnhancerImpl::getParameterSpecific(const Parameter::Id& id, |
| 118 | Parameter::Specific* specific) { |
| 119 | RETURN_IF(!specific, EX_NULL_POINTER, "nullPtr"); |
| 120 | auto tag = id.getTag(); |
| 121 | RETURN_IF(Parameter::Id::loudnessEnhancerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
| 122 | auto leId = id.get<Parameter::Id::loudnessEnhancerTag>(); |
| 123 | auto leIdTag = leId.getTag(); |
| 124 | switch (leIdTag) { |
| 125 | case LoudnessEnhancer::Id::commonTag: |
| 126 | return getParameterLoudnessEnhancer(leId.get<LoudnessEnhancer::Id::commonTag>(), |
| 127 | specific); |
| 128 | default: |
| 129 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(leIdTag); |
| 130 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 131 | EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported"); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | ndk::ScopedAStatus LoudnessEnhancerImpl::getParameterLoudnessEnhancer( |
| 136 | const LoudnessEnhancer::Tag& tag, Parameter::Specific* specific) { |
| 137 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 138 | |
| 139 | LoudnessEnhancer leParam; |
| 140 | switch (tag) { |
| 141 | case LoudnessEnhancer::gainMb: { |
| 142 | leParam.set<LoudnessEnhancer::gainMb>(mContext->getLeGain()); |
| 143 | break; |
| 144 | } |
| 145 | default: { |
| 146 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 147 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 148 | EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported"); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | specific->set<Parameter::Specific::loudnessEnhancer>(leParam); |
| 153 | return ndk::ScopedAStatus::ok(); |
| 154 | } |
| 155 | |
| 156 | std::shared_ptr<EffectContext> LoudnessEnhancerImpl::createContext( |
| 157 | const Parameter::Common& common) { |
| 158 | if (mContext) { |
| 159 | LOG(DEBUG) << __func__ << " context already exist"; |
| 160 | return mContext; |
| 161 | } |
| 162 | |
| 163 | mContext = std::make_shared<LoudnessEnhancerContext>(1 /* statusFmqDepth */, common); |
| 164 | return mContext; |
| 165 | } |
| 166 | |
| 167 | RetCode LoudnessEnhancerImpl::releaseContext() { |
| 168 | if (mContext) { |
| 169 | mContext->disable(); |
| 170 | mContext->resetBuffer(); |
| 171 | } |
| 172 | return RetCode::SUCCESS; |
| 173 | } |
| 174 | |
| 175 | // Processing method running in EffectWorker thread. |
| 176 | IEffect::Status LoudnessEnhancerImpl::effectProcessImpl(float* in, float* out, int samples) { |
| 177 | IEffect::Status status = {EX_NULL_POINTER, 0, 0}; |
| 178 | RETURN_VALUE_IF(!mContext, status, "nullContext"); |
| 179 | return mContext->lvmProcess(in, out, samples); |
| 180 | } |
| 181 | |
| 182 | } // namespace aidl::android::hardware::audio::effect |