blob: 7971dee5bc18814c28f479c58c8641e1740da0fa [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
Shraddha Basantwanif627d802022-11-08 14:45:07 +053017#include <algorithm>
Shunkai Yao6afc8552022-10-26 22:47:20 +000018#include <cstddef>
Shraddha Basantwanif627d802022-11-08 14:45:07 +053019#include <memory>
Shunkai Yao6afc8552022-10-26 22:47:20 +000020#define LOG_TAG "AHAL_BassBoostSw"
21#include <Utils.h>
Shunkai Yao6afc8552022-10-26 22:47:20 +000022#include <unordered_set>
23
24#include <android-base/logging.h>
25#include <fmq/AidlMessageQueue.h>
26
27#include "BassBoostSw.h"
28
29using aidl::android::hardware::audio::effect::BassBoostSw;
Shunkai Yao6afc8552022-10-26 22:47:20 +000030using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao812d5b42022-11-16 18:08:50 +000031using aidl::android::hardware::audio::effect::kBassBoostSwImplUUID;
Shunkai Yao6afc8552022-10-26 22:47:20 +000032using aidl::android::hardware::audio::effect::State;
33using aidl::android::media::audio::common::AudioUuid;
34
35extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
36 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000037 if (!in_impl_uuid || *in_impl_uuid != kBassBoostSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000038 LOG(ERROR) << __func__ << "uuid not supported";
39 return EX_ILLEGAL_ARGUMENT;
40 }
41 if (instanceSpp) {
42 *instanceSpp = ndk::SharedRefBase::make<BassBoostSw>();
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
51extern "C" binder_exception_t destroyEffect(const std::shared_ptr<IEffect>& instanceSp) {
52 if (!instanceSp) {
53 return EX_NONE;
54 }
55 State state;
56 ndk::ScopedAStatus status = instanceSp->getState(&state);
57 if (!status.isOk() || State::INIT != state) {
58 LOG(ERROR) << __func__ << " instance " << instanceSp.get()
59 << " in state: " << toString(state) << ", status: " << status.getDescription();
60 return EX_ILLEGAL_STATE;
61 }
62 LOG(DEBUG) << __func__ << " instance " << instanceSp.get() << " destroyed";
63 return EX_NONE;
64}
65
66namespace aidl::android::hardware::audio::effect {
67
68ndk::ScopedAStatus BassBoostSw::getDescriptor(Descriptor* _aidl_return) {
69 LOG(DEBUG) << __func__ << kDescriptor.toString();
70 *_aidl_return = kDescriptor;
71 return ndk::ScopedAStatus::ok();
72}
73
74ndk::ScopedAStatus BassBoostSw::setParameterSpecific(const Parameter::Specific& specific) {
75 RETURN_IF(Parameter::Specific::bassBoost != specific.getTag(), EX_ILLEGAL_ARGUMENT,
76 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000077 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
78
Shraddha Basantwanif627d802022-11-08 14:45:07 +053079 auto& bbParam = specific.get<Parameter::Specific::bassBoost>();
80 auto tag = bbParam.getTag();
81
82 switch (tag) {
83 case BassBoost::strengthPm: {
84 RETURN_IF(!mStrengthSupported, EX_ILLEGAL_ARGUMENT, "SettingStrengthNotSupported");
85
86 RETURN_IF(mContext->setBbStrengthPm(bbParam.get<BassBoost::strengthPm>()) !=
87 RetCode::SUCCESS,
88 EX_ILLEGAL_ARGUMENT, "strengthPmNotSupported");
89 return ndk::ScopedAStatus::ok();
90 }
91 default: {
92 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
93 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
94 "BassBoostTagNotSupported");
95 }
96 }
Shunkai Yao6afc8552022-10-26 22:47:20 +000097}
98
99ndk::ScopedAStatus BassBoostSw::getParameterSpecific(const Parameter::Id& id,
100 Parameter::Specific* specific) {
101 auto tag = id.getTag();
102 RETURN_IF(Parameter::Id::bassBoostTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530103 auto bbId = id.get<Parameter::Id::bassBoostTag>();
104 auto bbIdTag = bbId.getTag();
105 switch (bbIdTag) {
106 case BassBoost::Id::commonTag:
107 return getParameterBassBoost(bbId.get<BassBoost::Id::commonTag>(), specific);
108 default:
109 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
110 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
111 "BassBoostTagNotSupported");
112 }
113}
114
115ndk::ScopedAStatus BassBoostSw::getParameterBassBoost(const BassBoost::Tag& tag,
116 Parameter::Specific* specific) {
117 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
118 BassBoost bbParam;
119 switch (tag) {
120 case BassBoost::strengthPm: {
121 bbParam.set<BassBoost::strengthPm>(mContext->getBbStrengthPm());
122 break;
123 }
124 default: {
125 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
126 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
127 "BassBoostTagNotSupported");
128 }
129 }
130
131 specific->set<Parameter::Specific::bassBoost>(bbParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000132 return ndk::ScopedAStatus::ok();
133}
134
135std::shared_ptr<EffectContext> BassBoostSw::createContext(const Parameter::Common& common) {
136 if (mContext) {
137 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530138 } else {
139 mContext = std::make_shared<BassBoostSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000140 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530141 return mContext;
142}
143
144std::shared_ptr<EffectContext> BassBoostSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000145 return mContext;
146}
147
148RetCode BassBoostSw::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 BassBoostSw::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