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_VirtualizerSw" |
| 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 "VirtualizerSw.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::kVirtualizerSwImplUUID; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 31 | using aidl::android::hardware::audio::effect::State; |
| 32 | using aidl::android::hardware::audio::effect::VirtualizerSw; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 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 != kVirtualizerSwImplUUID) { |
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<VirtualizerSw>(); |
| 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 != kVirtualizerSwImplUUID) { |
| 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 = VirtualizerSw::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 VirtualizerSw::kEffectName = "VirtualizerSw"; |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 63 | const bool VirtualizerSw::kStrengthSupported = true; |
Sham Rathod | e808b07 | 2022-12-27 11:56:30 +0530 | [diff] [blame] | 64 | const Virtualizer::Capability VirtualizerSw::kCapability = { |
| 65 | .maxStrengthPm = 1000, .strengthSupported = kStrengthSupported}; |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 66 | const Descriptor VirtualizerSw::kDescriptor = { |
| 67 | .common = {.id = {.type = kVirtualizerTypeUUID, |
| 68 | .uuid = kVirtualizerSwImplUUID, |
Sham Rathod | 5fd232f | 2022-12-08 13:56:38 +0530 | [diff] [blame] | 69 | .proxy = kVirtualizerProxyUUID}, |
Shunkai Yao | c12e082 | 2022-12-12 07:13:58 +0000 | [diff] [blame] | 70 | .flags = {.type = Flags::Type::INSERT, |
| 71 | .insert = Flags::Insert::FIRST, |
| 72 | .volume = Flags::Volume::CTRL}, |
| 73 | .name = VirtualizerSw::kEffectName, |
| 74 | .implementor = "The Android Open Source Project"}, |
| 75 | .capability = Capability::make<Capability::virtualizer>(VirtualizerSw::kCapability)}; |
| 76 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 77 | ndk::ScopedAStatus VirtualizerSw::getDescriptor(Descriptor* _aidl_return) { |
| 78 | LOG(DEBUG) << __func__ << kDescriptor.toString(); |
| 79 | *_aidl_return = kDescriptor; |
| 80 | return ndk::ScopedAStatus::ok(); |
| 81 | } |
| 82 | |
| 83 | ndk::ScopedAStatus VirtualizerSw::setParameterSpecific(const Parameter::Specific& specific) { |
| 84 | RETURN_IF(Parameter::Specific::virtualizer != specific.getTag(), EX_ILLEGAL_ARGUMENT, |
| 85 | "EffectNotSupported"); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 86 | |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 87 | auto& vrParam = specific.get<Parameter::Specific::virtualizer>(); |
| 88 | auto tag = vrParam.getTag(); |
| 89 | |
| 90 | switch (tag) { |
| 91 | case Virtualizer::strengthPm: { |
| 92 | RETURN_IF(!kStrengthSupported, EX_ILLEGAL_ARGUMENT, "SettingStrengthNotSupported"); |
| 93 | |
| 94 | RETURN_IF(mContext->setVrStrength(vrParam.get<Virtualizer::strengthPm>()) != |
| 95 | RetCode::SUCCESS, |
| 96 | EX_ILLEGAL_ARGUMENT, "strengthPmNotSupported"); |
| 97 | return ndk::ScopedAStatus::ok(); |
| 98 | } |
| 99 | default: { |
| 100 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 101 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 102 | "VirtualizerTagNotSupported"); |
| 103 | } |
| 104 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | ndk::ScopedAStatus VirtualizerSw::getParameterSpecific(const Parameter::Id& id, |
| 108 | Parameter::Specific* specific) { |
| 109 | auto tag = id.getTag(); |
| 110 | RETURN_IF(Parameter::Id::virtualizerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag"); |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 111 | auto vrId = id.get<Parameter::Id::virtualizerTag>(); |
| 112 | auto vrIdTag = vrId.getTag(); |
| 113 | switch (vrIdTag) { |
| 114 | case Virtualizer::Id::commonTag: |
| 115 | return getParameterVirtualizer(vrId.get<Virtualizer::Id::commonTag>(), specific); |
| 116 | default: |
| 117 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 118 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 119 | "VirtualizerTagNotSupported"); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | ndk::ScopedAStatus VirtualizerSw::getParameterVirtualizer(const Virtualizer::Tag& tag, |
| 124 | Parameter::Specific* specific) { |
| 125 | RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext"); |
| 126 | |
| 127 | Virtualizer vrParam; |
| 128 | switch (tag) { |
| 129 | case Virtualizer::strengthPm: { |
| 130 | vrParam.set<Virtualizer::strengthPm>(mContext->getVrStrength()); |
| 131 | break; |
| 132 | } |
| 133 | default: { |
| 134 | LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag); |
| 135 | return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, |
| 136 | "VirtualizerTagNotSupported"); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | specific->set<Parameter::Specific::virtualizer>(vrParam); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 141 | return ndk::ScopedAStatus::ok(); |
| 142 | } |
| 143 | |
| 144 | std::shared_ptr<EffectContext> VirtualizerSw::createContext(const Parameter::Common& common) { |
| 145 | if (mContext) { |
| 146 | LOG(DEBUG) << __func__ << " context already exist"; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 147 | } else { |
| 148 | mContext = std::make_shared<VirtualizerSwContext>(1 /* statusFmqDepth */, common); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 149 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 150 | |
| 151 | return mContext; |
| 152 | } |
| 153 | |
| 154 | std::shared_ptr<EffectContext> VirtualizerSw::getContext() { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 155 | return mContext; |
| 156 | } |
| 157 | |
| 158 | RetCode VirtualizerSw::releaseContext() { |
| 159 | if (mContext) { |
| 160 | mContext.reset(); |
| 161 | } |
| 162 | return RetCode::SUCCESS; |
| 163 | } |
| 164 | |
| 165 | // Processing method running in EffectWorker thread. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 166 | IEffect::Status VirtualizerSw::effectProcessImpl(float* in, float* out, int samples) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 167 | // TODO: get data buffer and process. |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 168 | LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples; |
| 169 | for (int i = 0; i < samples; i++) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 170 | *out++ = *in++; |
| 171 | } |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 172 | return {STATUS_OK, samples, samples}; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Sham Rathod | e808b07 | 2022-12-27 11:56:30 +0530 | [diff] [blame] | 175 | RetCode VirtualizerSwContext::setVrStrength(int strength) { |
| 176 | if (strength < 0 || strength > VirtualizerSw::kCapability.maxStrengthPm) { |
| 177 | LOG(ERROR) << __func__ << " invalid strength: " << strength; |
| 178 | return RetCode::ERROR_ILLEGAL_PARAMETER; |
| 179 | } |
| 180 | // TODO : Add implementation to apply new strength |
| 181 | mStrength = strength; |
| 182 | return RetCode::SUCCESS; |
| 183 | } |
| 184 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 185 | } // namespace aidl::android::hardware::audio::effect |