blob: 9d2b978fa44f3367a332626acd49f58eba288f9d [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");
76 std::lock_guard lg(mMutex);
77 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
78
Shraddha Basantwani68041ca2022-11-04 15:13:32 +053079 auto& leParam = specific.get<Parameter::Specific::loudnessEnhancer>();
80 auto tag = leParam.getTag();
81
82 switch (tag) {
83 case LoudnessEnhancer::gainMb: {
84 RETURN_IF(mContext->setLeGainMb(leParam.get<LoudnessEnhancer::gainMb>()) !=
85 RetCode::SUCCESS,
86 EX_ILLEGAL_ARGUMENT, "setGainMbFailed");
87 return ndk::ScopedAStatus::ok();
88 }
89 default: {
90 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
91 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
92 EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported");
93 }
94 }
Shunkai Yao6afc8552022-10-26 22:47:20 +000095}
96
97ndk::ScopedAStatus LoudnessEnhancerSw::getParameterSpecific(const Parameter::Id& id,
98 Parameter::Specific* specific) {
99 auto tag = id.getTag();
100 RETURN_IF(Parameter::Id::loudnessEnhancerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Shraddha Basantwani68041ca2022-11-04 15:13:32 +0530101 auto leId = id.get<Parameter::Id::loudnessEnhancerTag>();
102 auto leIdTag = leId.getTag();
103 switch (leIdTag) {
104 case LoudnessEnhancer::Id::commonTag:
105 return getParameterLoudnessEnhancer(leId.get<LoudnessEnhancer::Id::commonTag>(),
106 specific);
107 default:
108 LOG(ERROR) << __func__ << " unsupported tag: " << toString(leIdTag);
109 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
110 EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported");
111 }
112}
113
114ndk::ScopedAStatus LoudnessEnhancerSw::getParameterLoudnessEnhancer(
115 const LoudnessEnhancer::Tag& tag, Parameter::Specific* specific) {
116 std::lock_guard lg(mMutex);
117 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
118
119 LoudnessEnhancer leParam;
120 switch (tag) {
121 case LoudnessEnhancer::gainMb: {
122 leParam.set<LoudnessEnhancer::gainMb>(mContext->getLeGainMb());
123 break;
124 }
125 default: {
126 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
127 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(
128 EX_ILLEGAL_ARGUMENT, "LoudnessEnhancerTagNotSupported");
129 }
130 }
131
132 specific->set<Parameter::Specific::loudnessEnhancer>(leParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000133 return ndk::ScopedAStatus::ok();
134}
135
136std::shared_ptr<EffectContext> LoudnessEnhancerSw::createContext(const Parameter::Common& common) {
137 if (mContext) {
138 LOG(DEBUG) << __func__ << " context already exist";
139 return mContext;
140 }
141 mContext = std::make_shared<LoudnessEnhancerSwContext>(1 /* statusFmqDepth */, common);
142 return mContext;
143}
144
145RetCode LoudnessEnhancerSw::releaseContext() {
146 if (mContext) {
147 mContext.reset();
148 }
149 return RetCode::SUCCESS;
150}
151
152// Processing method running in EffectWorker thread.
153IEffect::Status LoudnessEnhancerSw::effectProcessImpl(float* in, float* out, int process) {
154 // TODO: get data buffer and process.
155 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " process " << process;
156 for (int i = 0; i < process; i++) {
157 *out++ = *in++;
158 }
159 return {STATUS_OK, process, process};
160}
161
162} // namespace aidl::android::hardware::audio::effect