blob: d61ef97f24737e2b7c863c2f58604f29c0c52654 [file] [log] [blame]
Shunkai Yao45905172022-08-24 18:14:02 +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
Shunkai Yao6afc8552022-10-26 22:47:20 +000017#include <cstddef>
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000018#define LOG_TAG "AHAL_EqualizerSw"
19#include <Utils.h>
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000020#include <algorithm>
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000021#include <unordered_set>
Shunkai Yao45905172022-08-24 18:14:02 +000022
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000023#include <android-base/logging.h>
24#include <fmq/AidlMessageQueue.h>
25
Shunkai Yao6c370642022-11-08 02:42:09 +000026#include "EqualizerSw.h"
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000027
Shunkai Yao6afc8552022-10-26 22:47:20 +000028using aidl::android::hardware::audio::effect::EqualizerSw;
Shunkai Yao6afc8552022-10-26 22:47:20 +000029using aidl::android::hardware::audio::effect::IEffect;
Shunkai Yao812d5b42022-11-16 18:08:50 +000030using aidl::android::hardware::audio::effect::kEqualizerSwImplUUID;
Shunkai Yao6afc8552022-10-26 22:47:20 +000031using aidl::android::hardware::audio::effect::State;
32using aidl::android::media::audio::common::AudioUuid;
Shunkai Yao45905172022-08-24 18:14:02 +000033
Shunkai Yao6afc8552022-10-26 22:47:20 +000034extern "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 != kEqualizerSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000037 LOG(ERROR) << __func__ << "uuid not supported";
38 return EX_ILLEGAL_ARGUMENT;
39 }
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000040 if (instanceSpp) {
41 *instanceSpp = ndk::SharedRefBase::make<EqualizerSw>();
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 State state;
52 ndk::ScopedAStatus status = instanceSp->getState(&state);
53 if (!status.isOk() || State::INIT != state) {
54 LOG(ERROR) << __func__ << " instance " << instanceSp.get()
55 << " in state: " << toString(state) << ", status: " << status.getDescription();
56 return EX_ILLEGAL_STATE;
57 }
58 LOG(DEBUG) << __func__ << " instance " << instanceSp.get() << " destroyed";
59 return EX_NONE;
60}
61
Shunkai Yao6afc8552022-10-26 22:47:20 +000062namespace aidl::android::hardware::audio::effect {
Shunkai Yao45905172022-08-24 18:14:02 +000063
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000064ndk::ScopedAStatus EqualizerSw::getDescriptor(Descriptor* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000065 LOG(DEBUG) << __func__ << kDesc.toString();
66 *_aidl_return = kDesc;
Shunkai Yao45905172022-08-24 18:14:02 +000067 return ndk::ScopedAStatus::ok();
68}
69
Shunkai Yao6afc8552022-10-26 22:47:20 +000070ndk::ScopedAStatus EqualizerSw::setParameterSpecific(const Parameter::Specific& specific) {
71 RETURN_IF(Parameter::Specific::equalizer != specific.getTag(), EX_ILLEGAL_ARGUMENT,
72 "EffectNotSupported");
73 std::lock_guard lg(mMutex);
74 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
Shunkai Yaoea24c1a2022-09-28 17:39:23 +000075
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000076 auto& eqParam = specific.get<Parameter::Specific::equalizer>();
77 auto tag = eqParam.getTag();
78 switch (tag) {
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000079 case Equalizer::preset: {
Shunkai Yao6afc8552022-10-26 22:47:20 +000080 RETURN_IF(mContext->setEqPreset(eqParam.get<Equalizer::preset>()) != RetCode::SUCCESS,
81 EX_ILLEGAL_ARGUMENT, "setBandLevelsFailed");
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000082 return ndk::ScopedAStatus::ok();
83 }
Shunkai Yao6afc8552022-10-26 22:47:20 +000084 case Equalizer::bandLevels: {
85 RETURN_IF(mContext->setEqBandLevels(eqParam.get<Equalizer::bandLevels>()) !=
86 RetCode::SUCCESS,
87 EX_ILLEGAL_ARGUMENT, "setBandLevelsFailed");
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000088 return ndk::ScopedAStatus::ok();
89 }
Shunkai Yao6afc8552022-10-26 22:47:20 +000090 default: {
91 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
92 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
93 "EqTagNotSupported");
94 }
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000095 }
96
97 LOG(ERROR) << __func__ << " unsupported eq param tag: " << toString(tag);
98 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
99 "ParamNotSupported");
100}
101
Shunkai Yao6afc8552022-10-26 22:47:20 +0000102ndk::ScopedAStatus EqualizerSw::getParameterSpecific(const Parameter::Id& id,
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000103 Parameter::Specific* specific) {
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000104 auto tag = id.getTag();
Shunkai Yao6afc8552022-10-26 22:47:20 +0000105 RETURN_IF(Parameter::Id::equalizerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
106 auto eqId = id.get<Parameter::Id::equalizerTag>();
107 auto eqIdTag = eqId.getTag();
108 switch (eqIdTag) {
109 case Equalizer::Id::commonTag:
110 return getParameterEqualizer(eqId.get<Equalizer::Id::commonTag>(), specific);
111 default:
112 LOG(ERROR) << __func__ << " tag " << toString(eqIdTag) << " not supported";
113 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
114 "EqualizerTagNotSupported");
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000115 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000116}
117
118ndk::ScopedAStatus EqualizerSw::getParameterEqualizer(const Equalizer::Tag& tag,
119 Parameter::Specific* specific) {
120 std::lock_guard lg(mMutex);
121 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
122
123 Equalizer eqParam;
124 switch (tag) {
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000125 case Equalizer::bandLevels: {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000126 eqParam.set<Equalizer::bandLevels>(mContext->getEqBandLevels());
127 break;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000128 }
129 case Equalizer::preset: {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000130 eqParam.set<Equalizer::preset>(mContext->getEqPreset());
131 break;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000132 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000133 default: {
134 LOG(ERROR) << __func__ << " not handled tag: " << toString(tag);
135 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
136 "unsupportedTag");
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000137 }
138 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000139
140 specific->set<Parameter::Specific::equalizer>(eqParam);
141 return ndk::ScopedAStatus::ok();
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000142}
143
Shunkai Yao6afc8552022-10-26 22:47:20 +0000144std::shared_ptr<EffectContext> EqualizerSw::createContext(const Parameter::Common& common) {
145 if (mContext) {
146 LOG(DEBUG) << __func__ << " context already exist";
147 return mContext;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000148 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000149 mContext = std::make_shared<EqualizerSwContext>(1 /* statusFmqDepth */, common);
150 return mContext;
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000151}
152
Shunkai Yao6afc8552022-10-26 22:47:20 +0000153RetCode EqualizerSw::releaseContext() {
154 if (mContext) {
155 mContext.reset();
156 }
157 return RetCode::SUCCESS;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000158}
159
160// Processing method running in EffectWorker thread.
Shunkai Yao6afc8552022-10-26 22:47:20 +0000161IEffect::Status EqualizerSw::effectProcessImpl(float* in, float* out, int process) {
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000162 // TODO: get data buffer and process.
Shunkai Yao6afc8552022-10-26 22:47:20 +0000163 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " process " << process;
164 for (int i = 0; i < process; i++) {
165 *out++ = *in++;
166 }
167 return {STATUS_OK, process, process};
Shunkai Yaoea24c1a2022-09-28 17:39:23 +0000168}
169
Shunkai Yao45905172022-08-24 18:14:02 +0000170} // namespace aidl::android::hardware::audio::effect