xref: /aosp_15_r20/frameworks/av/media/libeffects/hapticgenerator/aidl/EffectHapticGenerator.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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_HapticGeneratorImpl"
18 
19 #include <android-base/logging.h>
20 #include <audio_effects/effect_hapticgenerator.h>
21 #include <system/audio_effects/effect_uuid.h>
22 
23 #include "EffectHapticGenerator.h"
24 
25 using aidl::android::hardware::audio::effect::Descriptor;
26 using aidl::android::hardware::audio::effect::getEffectImplUuidHapticGenerator;
27 using aidl::android::hardware::audio::effect::getEffectTypeUuidHapticGenerator;
28 using aidl::android::hardware::audio::effect::HapticGeneratorImpl;
29 using aidl::android::hardware::audio::effect::IEffect;
30 using aidl::android::media::audio::common::AudioUuid;
31 
createEffect(const AudioUuid * in_impl_uuid,std::shared_ptr<IEffect> * instanceSpp)32 extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
33                                            std::shared_ptr<IEffect>* instanceSpp) {
34     if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidHapticGenerator()) {
35         LOG(ERROR) << __func__ << "uuid not supported";
36         return EX_ILLEGAL_ARGUMENT;
37     }
38     if (instanceSpp) {
39         *instanceSpp = ndk::SharedRefBase::make<HapticGeneratorImpl>();
40         return EX_NONE;
41     } else {
42         LOG(ERROR) << __func__ << " invalid input parameter!";
43         return EX_ILLEGAL_ARGUMENT;
44     }
45 }
46 
queryEffect(const AudioUuid * in_impl_uuid,Descriptor * _aidl_return)47 extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
48     if (!in_impl_uuid || *in_impl_uuid != getEffectImplUuidHapticGenerator()) {
49         LOG(ERROR) << __func__ << "uuid not supported";
50         return EX_ILLEGAL_ARGUMENT;
51     }
52     *_aidl_return = HapticGeneratorImpl::kDescriptor;
53     return EX_NONE;
54 }
55 
56 namespace aidl::android::hardware::audio::effect {
57 
58 const std::vector<Range::HapticGeneratorRange> kHapticRange = {
59         MAKE_RANGE(HapticGenerator, vibratorInfo,
60                    HapticGenerator::VibratorInformation(
61                            {.resonantFrequencyHz = 1, .qFactor = 1, .maxAmplitude = -1}),
62                    HapticGenerator::VibratorInformation(
63                            {.resonantFrequencyHz = std::numeric_limits<float>::max(),
64                             .qFactor = std::numeric_limits<float>::max(),
65                             .maxAmplitude = 1}))};
66 
67 static const Capability kHapticCap = {.range = kHapticRange};
68 
69 const std::string HapticGeneratorImpl::kEffectName = "Haptic Generator";
70 const Descriptor HapticGeneratorImpl::kDescriptor = {
71         .common = {.id = {.type = getEffectTypeUuidHapticGenerator(),
72                           .uuid = getEffectImplUuidHapticGenerator(),
73                           .proxy = std::nullopt},
74                    .flags = {.type = Flags::Type::INSERT, .insert = Flags::Insert::FIRST},
75                    .name = HapticGeneratorImpl::kEffectName,
76                    .implementor = "The Android Open Source Project"},
77         .capability = kHapticCap};
78 
getDescriptor(Descriptor * _aidl_return)79 ndk::ScopedAStatus HapticGeneratorImpl::getDescriptor(Descriptor* _aidl_return) {
80     RETURN_IF(!_aidl_return, EX_ILLEGAL_ARGUMENT, "Parameter:nullptr");
81     *_aidl_return = kDescriptor;
82     return ndk::ScopedAStatus::ok();
83 }
84 
setParameterSpecific(const Parameter::Specific & specific)85 ndk::ScopedAStatus HapticGeneratorImpl::setParameterSpecific(const Parameter::Specific& specific) {
86     RETURN_IF(Parameter::Specific::hapticGenerator != specific.getTag(), EX_ILLEGAL_ARGUMENT,
87               "EffectNotSupported");
88     RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
89 
90     auto& hgParam = specific.get<Parameter::Specific::hapticGenerator>();
91     RETURN_IF(!inRange(hgParam, kHapticRange), EX_ILLEGAL_ARGUMENT, "outOfRange");
92 
93     auto tag = hgParam.getTag();
94 
95     switch (tag) {
96         case HapticGenerator::hapticScales: {
97             RETURN_IF(mContext->setHgHapticScales(hgParam.get<HapticGenerator::hapticScales>()) !=
98                               RetCode::SUCCESS,
99                       EX_ILLEGAL_ARGUMENT, "setHapticScaleFailed");
100             return ndk::ScopedAStatus::ok();
101         }
102         case HapticGenerator::vibratorInfo: {
103             RETURN_IF(mContext->setHgVibratorInformation(
104                               hgParam.get<HapticGenerator::vibratorInfo>()) != RetCode::SUCCESS,
105                       EX_ILLEGAL_ARGUMENT, "setVibratorInfoFailed");
106             return ndk::ScopedAStatus::ok();
107         }
108         default: {
109             LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
110             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
111                     EX_ILLEGAL_ARGUMENT, "HapticGeneratorTagNotSupported");
112         }
113     }
114 }
115 
getParameterSpecific(const Parameter::Id & id,Parameter::Specific * specific)116 ndk::ScopedAStatus HapticGeneratorImpl::getParameterSpecific(const Parameter::Id& id,
117                                                              Parameter::Specific* specific) {
118     RETURN_IF(!specific, EX_NULL_POINTER, "nullPtr");
119     auto tag = id.getTag();
120     RETURN_IF(Parameter::Id::hapticGeneratorTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
121     auto hgId = id.get<Parameter::Id::hapticGeneratorTag>();
122     auto hgIdTag = hgId.getTag();
123     switch (hgIdTag) {
124         case HapticGenerator::Id::commonTag:
125             return getParameterHapticGenerator(hgId.get<HapticGenerator::Id::commonTag>(),
126                                                specific);
127         default:
128             LOG(ERROR) << __func__ << " unsupported tag: " << toString(hgIdTag);
129             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
130                     EX_ILLEGAL_ARGUMENT, "HapticGeneratorTagNotSupported");
131     }
132 }
133 
getParameterHapticGenerator(const HapticGenerator::Tag & tag,Parameter::Specific * specific)134 ndk::ScopedAStatus HapticGeneratorImpl::getParameterHapticGenerator(const HapticGenerator::Tag& tag,
135                                                                     Parameter::Specific* specific) {
136     RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
137 
138     HapticGenerator hgParam;
139     switch (tag) {
140         case HapticGenerator::hapticScales: {
141             hgParam.set<HapticGenerator::hapticScales>(mContext->getHgHapticScales());
142             break;
143         }
144         case HapticGenerator::vibratorInfo: {
145             hgParam.set<HapticGenerator::vibratorInfo>(mContext->getHgVibratorInformation());
146             break;
147         }
148         default: {
149             LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
150             return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
151                     EX_ILLEGAL_ARGUMENT, "HapticGeneratorTagNotSupported");
152         }
153     }
154 
155     specific->set<Parameter::Specific::hapticGenerator>(hgParam);
156     return ndk::ScopedAStatus::ok();
157 }
158 
createContext(const Parameter::Common & common)159 std::shared_ptr<EffectContext> HapticGeneratorImpl::createContext(const Parameter::Common& common) {
160     if (mContext) {
161         LOG(DEBUG) << __func__ << " context already exist";
162         return mContext;
163     }
164 
165     mContext = std::make_shared<HapticGeneratorContext>(1 /* statusFmqDepth */, common);
166     return mContext;
167 }
168 
releaseContext()169 RetCode HapticGeneratorImpl::releaseContext() {
170     if (mContext) {
171         mContext->reset();
172     }
173     return RetCode::SUCCESS;
174 }
175 
176 // Processing method running in EffectWorker thread.
effectProcessImpl(float * in,float * out,int samples)177 IEffect::Status HapticGeneratorImpl::effectProcessImpl(float* in, float* out, int samples) {
178     IEffect::Status status = {EX_NULL_POINTER, 0, 0};
179     RETURN_VALUE_IF(!mContext, status, "nullContext");
180     return mContext->process(in, out, samples);
181 }
182 
183 }  // namespace aidl::android::hardware::audio::effect
184