blob: 8e4779dd01de86f9d2bccabef324198057833f1f [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 Yaoc12e0822022-12-12 07:13:58 +000030using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000031using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao812d5b42022-11-16 18:08:50 +000032using aidl::android::hardware::audio::effect::kBassBoostSwImplUUID;
Shunkai Yao6afc8552022-10-26 22:47:20 +000033using aidl::android::hardware::audio::effect::State;
34using aidl::android::media::audio::common::AudioUuid;
35
36extern "C" binder_exception_t createEffect(const AudioUuid* in_impl_uuid,
37 std::shared_ptr<IEffect>* instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000038 if (!in_impl_uuid || *in_impl_uuid != kBassBoostSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000039 LOG(ERROR) << __func__ << "uuid not supported";
40 return EX_ILLEGAL_ARGUMENT;
41 }
42 if (instanceSpp) {
43 *instanceSpp = ndk::SharedRefBase::make<BassBoostSw>();
44 LOG(DEBUG) << __func__ << " instance " << instanceSpp->get() << " created";
45 return EX_NONE;
46 } else {
47 LOG(ERROR) << __func__ << " invalid input parameter!";
48 return EX_ILLEGAL_ARGUMENT;
49 }
50}
51
Shunkai Yaoc12e0822022-12-12 07:13:58 +000052extern "C" binder_exception_t queryEffect(const AudioUuid* in_impl_uuid, Descriptor* _aidl_return) {
53 if (!in_impl_uuid || *in_impl_uuid != kBassBoostSwImplUUID) {
54 LOG(ERROR) << __func__ << "uuid not supported";
55 return EX_ILLEGAL_ARGUMENT;
Shunkai Yao6afc8552022-10-26 22:47:20 +000056 }
Shunkai Yaoc12e0822022-12-12 07:13:58 +000057 *_aidl_return = BassBoostSw::kDescriptor;
Shunkai Yao6afc8552022-10-26 22:47:20 +000058 return EX_NONE;
59}
60
61namespace aidl::android::hardware::audio::effect {
62
Shunkai Yaoc12e0822022-12-12 07:13:58 +000063const std::string BassBoostSw::kEffectName = "BassBoostSw";
64const bool BassBoostSw::kStrengthSupported = true;
65const BassBoost::Capability BassBoostSw::kCapability = {.strengthSupported = kStrengthSupported};
66const Descriptor BassBoostSw::kDescriptor = {
67 .common = {.id = {.type = kBassBoostTypeUUID,
68 .uuid = kBassBoostSwImplUUID,
Shraddha Basantwani3a2fb032022-11-22 11:04:35 +053069 .proxy = kBassBoostProxyUUID},
Shunkai Yaoc12e0822022-12-12 07:13:58 +000070 .flags = {.type = Flags::Type::INSERT,
71 .insert = Flags::Insert::FIRST,
72 .volume = Flags::Volume::CTRL},
73 .name = BassBoostSw::kEffectName,
74 .implementor = "The Android Open Source Project"},
75 .capability = Capability::make<Capability::bassBoost>(BassBoostSw::kCapability)};
76
Shunkai Yao6afc8552022-10-26 22:47:20 +000077ndk::ScopedAStatus BassBoostSw::getDescriptor(Descriptor* _aidl_return) {
78 LOG(DEBUG) << __func__ << kDescriptor.toString();
79 *_aidl_return = kDescriptor;
80 return ndk::ScopedAStatus::ok();
81}
82
83ndk::ScopedAStatus BassBoostSw::setParameterSpecific(const Parameter::Specific& specific) {
84 RETURN_IF(Parameter::Specific::bassBoost != specific.getTag(), EX_ILLEGAL_ARGUMENT,
85 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000086 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
87
Shraddha Basantwanif627d802022-11-08 14:45:07 +053088 auto& bbParam = specific.get<Parameter::Specific::bassBoost>();
89 auto tag = bbParam.getTag();
90
91 switch (tag) {
92 case BassBoost::strengthPm: {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000093 RETURN_IF(!kStrengthSupported, EX_ILLEGAL_ARGUMENT, "SettingStrengthNotSupported");
Shraddha Basantwanif627d802022-11-08 14:45:07 +053094
95 RETURN_IF(mContext->setBbStrengthPm(bbParam.get<BassBoost::strengthPm>()) !=
96 RetCode::SUCCESS,
97 EX_ILLEGAL_ARGUMENT, "strengthPmNotSupported");
98 return ndk::ScopedAStatus::ok();
99 }
100 default: {
101 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
102 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
103 "BassBoostTagNotSupported");
104 }
105 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000106}
107
108ndk::ScopedAStatus BassBoostSw::getParameterSpecific(const Parameter::Id& id,
109 Parameter::Specific* specific) {
110 auto tag = id.getTag();
111 RETURN_IF(Parameter::Id::bassBoostTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530112 auto bbId = id.get<Parameter::Id::bassBoostTag>();
113 auto bbIdTag = bbId.getTag();
114 switch (bbIdTag) {
115 case BassBoost::Id::commonTag:
116 return getParameterBassBoost(bbId.get<BassBoost::Id::commonTag>(), specific);
117 default:
118 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
119 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
120 "BassBoostTagNotSupported");
121 }
122}
123
124ndk::ScopedAStatus BassBoostSw::getParameterBassBoost(const BassBoost::Tag& tag,
125 Parameter::Specific* specific) {
126 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
127 BassBoost bbParam;
128 switch (tag) {
129 case BassBoost::strengthPm: {
130 bbParam.set<BassBoost::strengthPm>(mContext->getBbStrengthPm());
131 break;
132 }
133 default: {
134 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
135 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
136 "BassBoostTagNotSupported");
137 }
138 }
139
140 specific->set<Parameter::Specific::bassBoost>(bbParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000141 return ndk::ScopedAStatus::ok();
142}
143
144std::shared_ptr<EffectContext> BassBoostSw::createContext(const Parameter::Common& common) {
145 if (mContext) {
146 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530147 } else {
148 mContext = std::make_shared<BassBoostSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000149 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530150 return mContext;
151}
152
153std::shared_ptr<EffectContext> BassBoostSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000154 return mContext;
155}
156
157RetCode BassBoostSw::releaseContext() {
158 if (mContext) {
159 mContext.reset();
160 }
161 return RetCode::SUCCESS;
162}
163
164// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530165IEffect::Status BassBoostSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000166 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530167 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
168 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000169 *out++ = *in++;
170 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530171 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000172}
173
174} // namespace aidl::android::hardware::audio::effect