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