Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +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 <cstddef> |
| 18 | #define LOG_TAG "AHAL_LoudnessEnhancerSw" |
| 19 | #include <Utils.h> |
| 20 | #include <algorithm> |
| 21 | #include <unordered_set> |
| 22 | |
| 23 | #include <android-base/logging.h> |
| 24 | #include <fmq/AidlMessageQueue.h> |
| 25 | |
| 26 | #include "LoudnessEnhancerSw.h" |
| 27 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 28 | using aidl::android::hardware::audio::effect::Descriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 29 | using aidl::android::hardware::audio::effect::IEffect; |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 30 | using aidl::android::hardware::audio::effect::kLoudnessEnhancerSwImplUUID; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 31 | using aidl::android::hardware::audio::effect::LoudnessEnhancerSw; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 32 | using aidl::android::hardware::audio::effect::State; |
| 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) { |
Shunkai Yao | 812d5b4 | 2022-11-16 18:08:50 +0000 | [diff] [blame] | 37 | if (!in_impl_uuid || *in_impl_uuid != kLoudnessEnhancerSwImplUUID) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 38 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 39 | return EX_ILLEGAL_ARGUMENT; |
| 40 | } |
| 41 | if (instanceSpp) { |
| 42 | *instanceSpp = ndk::SharedRefBase::make<LoudnessEnhancerSw>(); |
| 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 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 51 | extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) { |
| 52 | if (!in_impl_uuid || *in_impl_uuid != kLoudnessEnhancerSwImplUUID) { |
| 53 | LOG(ERROR) << __func__ << "uuid not supported"; |
| 54 | return EX_ILLEGAL_ARGUMENT; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 55 | } |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 56 | *_aidl_return = LoudnessEnhancerSw::kDescriptor; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 57 | return EX_NONE; |
| 58 | } |
| 59 | |
| 60 | namespace aidl::android::hardware::audio::effect { |
| 61 | |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 62 | const std::string LoudnessEnhancerSw::kEffectName = "LoudnessEnhancerSw"; |
| 63 | const Descriptor LoudnessEnhancerSw::kDescriptor = { |
| 64 | .common = {.id = {.type = kLoudnessEnhancerTypeUUID, |
| 65 | .uuid = kLoudnessEnhancerSwImplUUID, |
| 66 | .proxy = std::nullopt}, |
| 67 | .flags = {.type = Flags::Type::INSERT, |
| 68 | .insert = Flags::Insert::FIRST, |
| 69 | .volume = Flags::Volume::CTRL}, |
| 70 | .name = LoudnessEnhancerSw::kEffectName, |
| 71 | .implementor = "The Android Open Source Project"}}; |
| 72 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 73 | ndk::ScopedAStatus LoudnessEnhancerSw::getDescriptor(Descriptor* _aidl_return) { |
| 74 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 75 | *_aidl_return = kDescriptor; |
| 76 | return ndk::ScopedAStatus::ok(); |
| 77 | } |
| 78 | |
| 79 | ndk::ScopedAStatus LoudnessEnhancerSw::setParameterSpecific(const Parameter::Specific& specific) { |
| 80 | RETURN_IF(Parameter::Specific::loudnessEnhancer != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 81 | "EffectNotSupported"); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 82 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 83 | |
Shraddha Basantwani | 68041ca | 2022-11-04 15:13:32 +0530 | [diff] [blame] | 84 | auto& leParam = specific.get<Parameter::Specific::loudnessEnhancer>(); |
| 85 | auto tag = leParam.getTag(); |
| 86 | |
| 87 | switch (tag) { |
| 88 | case LoudnessEnhancer::gainMb: { |
| 89 | RETURN_IF(mContext->setLeGainMb(leParam.get<LoudnessEnhancer::gainMb>()) != |
| 90 | RetCode::SUCCESS, |
| 91 | EX_ILLEGAL_ARGUMENT, "setGainMbFailed"); |
| 92 | return ndk::ScopedAStatus::ok(); |
| 93 | } |
| 94 | default: { |
| 95 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 96 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 97 | EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported"); |
| 98 | } |
| 99 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | ndk::ScopedAStatus LoudnessEnhancerSw::getParameterSpecific(const Parameter::Id& id, |
| 103 | Parameter::Specific* specific) { |
| 104 | auto tag = id.getTag(); |
| 105 | RETURN_IF(Parameter::Id::loudnessEnhancerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
Shraddha Basantwani | 68041ca | 2022-11-04 15:13:32 +0530 | [diff] [blame] | 106 | auto leId = id.get<Parameter::Id::loudnessEnhancerTag>(); |
| 107 | auto leIdTag = leId.getTag(); |
| 108 | switch (leIdTag) { |
| 109 | case LoudnessEnhancer::Id::commonTag: |
| 110 | return getParameterLoudnessEnhancer(leId.get<LoudnessEnhancer::Id::commonTag>(), |
| 111 | specific); |
| 112 | default: |
| 113 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(leIdTag); |
| 114 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 115 | EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported"); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | ndk::ScopedAStatus LoudnessEnhancerSw::getParameterLoudnessEnhancer( |
| 120 | const LoudnessEnhancer::Tag& tag, Parameter::Specific* specific) { |
Shraddha Basantwani | 68041ca | 2022-11-04 15:13:32 +0530 | [diff] [blame] | 121 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 122 | |
| 123 | LoudnessEnhancer leParam; |
| 124 | switch (tag) { |
| 125 | case LoudnessEnhancer::gainMb: { |
| 126 | leParam.set<LoudnessEnhancer::gainMb>(mContext->getLeGainMb()); |
| 127 | break; |
| 128 | } |
| 129 | default: { |
| 130 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 131 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage( |
| 132 | EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported"); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | specific->set<Parameter::Specific::loudnessEnhancer>(leParam); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 137 | return ndk::ScopedAStatus::ok(); |
| 138 | } |
| 139 | |
| 140 | std::shared_ptr<EffectContext> LoudnessEnhancerSw::createContext(const Parameter::Common& common) { |
| 141 | if (mContext) { |
| 142 | LOG(DEBUG) << __func__ << " context already exist"; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 143 | } else { |
| 144 | mContext = std::make_shared<LoudnessEnhancerSwContext>(1 /* statusFmqDepth */, common); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 145 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 146 | |
| 147 | return mContext; |
| 148 | } |
| 149 | |
| 150 | std::shared_ptr<EffectContext> LoudnessEnhancerSw::getContext() { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 151 | return mContext; |
| 152 | } |
| 153 | |
| 154 | RetCode LoudnessEnhancerSw::releaseContext() { |
| 155 | if (mContext) { |
| 156 | mContext.reset(); |
| 157 | } |
| 158 | return RetCode::SUCCESS; |
| 159 | } |
| 160 | |
| 161 | // Processing method running in EffectWorker thread. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 162 | IEffect::Status LoudnessEnhancerSw::effectProcessImpl(float* in, float* out, int samples) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 163 | // TODO: get data buffer and process. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 164 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 165 | for (int i = 0; i < samples; i++) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 166 | *out++ = *in++; |
| 167 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 168 | return {STATUS_OK, samples, samples}; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | } // namespace aidl::android::hardware::audio::effect |