blob: 4015e6134a10fdbe1c5b06cea2ade35b578478ea [file] [log] [blame]
Shunkai Yao6afc8552022-10-26 22:47:20 +00001/*
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
28using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao812d5b42022-11-16 18:08:50 +000029using aidl::android::hardware::audio::effect::kLoudnessEnhancerSwImplUUID;
Shunkai Yao6afc8552022-10-26 22:47:20 +000030using aidl::android::hardware::audio::effect::LoudnessEnhancerSw;
Shunkai Yao6afc8552022-10-26 22:47:20 +000031using aidl::android::hardware::audio::effect::State;
32using aidl::android::media::audio::common::AudioUuid;
33
34extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
35 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000036 if (!in_impl_uuid || *in_impl_uuid != kLoudnessEnhancerSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000037 LOG(ERROR) << __func__ << "uuid not supported";
38 return EX_ILLEGAL_ARGUMENT;
39 }
40 if (instanceSpp) {
41 *instanceSpp = ndk::SharedRefBase::make<LoudnessEnhancerSw>();
42 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
43 return EX_NONE;
44 } else {
45 LOG(ERROR) << __func__ << " invalid input parameter!";
46 return EX_ILLEGAL_ARGUMENT;
47 }
48}
49
50extern "C" binder_exception_t destroyEffect(const std::shared_ptr<IEffect>& instanceSp) {
51 if (!instanceSp) {
52 return EX_NONE;
53 }
54 State state;
55 ndk::ScopedAStatus status = instanceSp->getState(&state);
56 if (!status.isOk() || State::INIT != state) {
57 LOG(ERROR) << __func__ << " instance " << instanceSp.get()
58 << " in state: " << toString(state) << ", status: " << status.getDescription();
59 return EX_ILLEGAL_STATE;
60 }
61 LOG(DEBUG) << __func__ << " instance " << instanceSp.get() << " destroyed";
62 return EX_NONE;
63}
64
65namespace aidl::android::hardware::audio::effect {
66
67ndk::ScopedAStatus LoudnessEnhancerSw::getDescriptor(Descriptor* _aidl_return) {
68 LOG(DEBUG) << __func__ << kDescriptor.toString();
69 *_aidl_return = kDescriptor;
70 return ndk::ScopedAStatus::ok();
71}
72
73ndk::ScopedAStatus LoudnessEnhancerSw::setParameterSpecific(const Parameter::Specific& specific) {
74 RETURN_IF(Parameter::Specific::loudnessEnhancer != specific.getTag(), EX_ILLEGAL_ARGUMENT,
75 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000076 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
77
Shraddha Basantwani68041ca2022-11-04 15:13:32 +053078 auto& leParam = specific.get<Parameter::Specific::loudnessEnhancer>();
79 auto tag = leParam.getTag();
80
81 switch (tag) {
82 case LoudnessEnhancer::gainMb: {
83 RETURN_IF(mContext->setLeGainMb(leParam.get<LoudnessEnhancer::gainMb>()) !=
84 RetCode::SUCCESS,
85 EX_ILLEGAL_ARGUMENT, "setGainMbFailed");
86 return ndk::ScopedAStatus::ok();
87 }
88 default: {
89 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
90 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
91 EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported");
92 }
93 }
Shunkai Yao6afc8552022-10-26 22:47:20 +000094}
95
96ndk::ScopedAStatus LoudnessEnhancerSw::getParameterSpecific(const Parameter::Id& id,
97 Parameter::Specific* specific) {
98 auto tag = id.getTag();
99 RETURN_IF(Parameter::Id::loudnessEnhancerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Shraddha Basantwani68041ca2022-11-04 15:13:32 +0530100 auto leId = id.get<Parameter::Id::loudnessEnhancerTag>();
101 auto leIdTag = leId.getTag();
102 switch (leIdTag) {
103 case LoudnessEnhancer::Id::commonTag:
104 return getParameterLoudnessEnhancer(leId.get<LoudnessEnhancer::Id::commonTag>(),
105 specific);
106 default:
107 LOG(ERROR) << __func__ << " unsupported tag: " << toString(leIdTag);
108 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
109 EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported");
110 }
111}
112
113ndk::ScopedAStatus LoudnessEnhancerSw::getParameterLoudnessEnhancer(
114 const LoudnessEnhancer::Tag& tag, Parameter::Specific* specific) {
Shraddha Basantwani68041ca2022-11-04 15:13:32 +0530115 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
116
117 LoudnessEnhancer leParam;
118 switch (tag) {
119 case LoudnessEnhancer::gainMb: {
120 leParam.set<LoudnessEnhancer::gainMb>(mContext->getLeGainMb());
121 break;
122 }
123 default: {
124 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
125 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
126 EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported");
127 }
128 }
129
130 specific->set<Parameter::Specific::loudnessEnhancer>(leParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000131 return ndk::ScopedAStatus::ok();
132}
133
134std::shared_ptr<EffectContext> LoudnessEnhancerSw::createContext(const Parameter::Common& common) {
135 if (mContext) {
136 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530137 } else {
138 mContext = std::make_shared<LoudnessEnhancerSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000139 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530140
141 return mContext;
142}
143
144std::shared_ptr<EffectContext> LoudnessEnhancerSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000145 return mContext;
146}
147
148RetCode LoudnessEnhancerSw::releaseContext() {
149 if (mContext) {
150 mContext.reset();
151 }
152 return RetCode::SUCCESS;
153}
154
155// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530156IEffect::Status LoudnessEnhancerSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000157 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530158 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
159 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000160 *out++ = *in++;
161 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530162 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000163}
164
165} // namespace aidl::android::hardware::audio::effect