blob: d0385967b7d897880a469d826191967c1ccb3ed5 [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>
Shunkai Yao812d5b42022-11-16 18:08:50 +000018#define LOG_TAG "AHAL_PresetReverbSw"
Shunkai Yao6afc8552022-10-26 22:47:20 +000019#include <Utils.h>
20#include <algorithm>
21#include <unordered_set>
22
23#include <android-base/logging.h>
Sham Rathod73aa2c32022-12-20 17:03:40 +053024#include <android/binder_enums.h>
Shunkai Yao6afc8552022-10-26 22:47:20 +000025#include <fmq/AidlMessageQueue.h>
26
Shunkai Yao812d5b42022-11-16 18:08:50 +000027#include "PresetReverbSw.h"
Shunkai Yao6afc8552022-10-26 22:47:20 +000028
Shunkai Yaoc12e0822022-12-12 07:13:58 +000029using aidl::android::hardware::audio::effect::Descriptor;
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::kPresetReverbSwImplUUID;
32using aidl::android::hardware::audio::effect::PresetReverbSw;
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 != kPresetReverbSwImplUUID) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000039 LOG(ERROR) << __func__ << "uuid not supported";
40 return EX_ILLEGAL_ARGUMENT;
41 }
42 if (instanceSpp) {
Shunkai Yao812d5b42022-11-16 18:08:50 +000043 *instanceSpp = ndk::SharedRefBase::make<PresetReverbSw>();
Shunkai Yao6afc8552022-10-26 22:47:20 +000044 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 != kPresetReverbSwImplUUID) {
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 = PresetReverbSw::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 PresetReverbSw::kEffectName = "PresetReverbSw";
Sham Rathod73aa2c32022-12-20 17:03:40 +053064
65const std::vector<PresetReverb::Presets> kSupportedPresets{
66 ndk::enum_range<PresetReverb::Presets>().begin(),
67 ndk::enum_range<PresetReverb::Presets>().end()};
68
69const PresetReverb::Capability PresetReverbSw::kCapability = {.supportedPresets =
70 kSupportedPresets};
Shunkai Yaoc12e0822022-12-12 07:13:58 +000071const Descriptor PresetReverbSw::kDescriptor = {
72 .common = {.id = {.type = kPresetReverbTypeUUID,
73 .uuid = kPresetReverbSwImplUUID,
74 .proxy = std::nullopt},
75 .flags = {.type = Flags::Type::INSERT,
76 .insert = Flags::Insert::FIRST,
77 .volume = Flags::Volume::CTRL},
78 .name = PresetReverbSw::kEffectName,
79 .implementor = "The Android Open Source Project"},
80 .capability = Capability::make<Capability::presetReverb>(PresetReverbSw::kCapability)};
81
Shunkai Yao812d5b42022-11-16 18:08:50 +000082ndk::ScopedAStatus PresetReverbSw::getDescriptor(Descriptor* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000083 LOG(DEBUG) << __func__ << kDescriptor.toString();
84 *_aidl_return = kDescriptor;
85 return ndk::ScopedAStatus::ok();
86}
87
Shunkai Yao812d5b42022-11-16 18:08:50 +000088ndk::ScopedAStatus PresetReverbSw::setParameterSpecific(const Parameter::Specific& specific) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000089 RETURN_IF(Parameter::Specific::presetReverb != specific.getTag(), EX_ILLEGAL_ARGUMENT,
Shunkai Yao6afc8552022-10-26 22:47:20 +000090 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000091
Sham Rathod73aa2c32022-12-20 17:03:40 +053092 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
93
94 auto& prParam = specific.get<Parameter::Specific::presetReverb>();
95 auto tag = prParam.getTag();
96
97 switch (tag) {
98 case PresetReverb::preset: {
99 RETURN_IF(
100 mContext->setPRPreset(prParam.get<PresetReverb::preset>()) != RetCode::SUCCESS,
101 EX_ILLEGAL_ARGUMENT, "setPresetFailed");
102 return ndk::ScopedAStatus::ok();
103 }
104 default: {
105 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
106 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
107 "PresetReverbTagNotSupported");
108 }
109 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000110}
111
Shunkai Yao812d5b42022-11-16 18:08:50 +0000112ndk::ScopedAStatus PresetReverbSw::getParameterSpecific(const Parameter::Id& id,
113 Parameter::Specific* specific) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000114 auto tag = id.getTag();
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000115 RETURN_IF(Parameter::Id::presetReverbTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Sham Rathod73aa2c32022-12-20 17:03:40 +0530116 auto prId = id.get<Parameter::Id::presetReverbTag>();
117 auto prIdTag = prId.getTag();
118 switch (prIdTag) {
119 case PresetReverb::Id::commonTag:
120 return getParameterPresetReverb(prId.get<PresetReverb::Id::commonTag>(), specific);
121 default:
122 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
123 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
124 "PresetReverbTagNotSupported");
125 }
126}
127
128ndk::ScopedAStatus PresetReverbSw::getParameterPresetReverb(const PresetReverb::Tag& tag,
129 Parameter::Specific* specific) {
130 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
131 PresetReverb prParam;
132 switch (tag) {
133 case PresetReverb::preset: {
134 prParam.set<PresetReverb::preset>(mContext->getPRPreset());
135 break;
136 }
137 default: {
138 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
139 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
140 "PresetReverbTagNotSupported");
141 }
142 }
143
144 specific->set<Parameter::Specific::presetReverb>(prParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000145 return ndk::ScopedAStatus::ok();
146}
147
Shunkai Yao812d5b42022-11-16 18:08:50 +0000148std::shared_ptr<EffectContext> PresetReverbSw::createContext(const Parameter::Common& common) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000149 if (mContext) {
150 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530151 } else {
152 mContext = std::make_shared<PresetReverbSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000153 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530154
155 return mContext;
156}
157
158std::shared_ptr<EffectContext> PresetReverbSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000159 return mContext;
160}
161
Shunkai Yao812d5b42022-11-16 18:08:50 +0000162RetCode PresetReverbSw::releaseContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000163 if (mContext) {
164 mContext.reset();
165 }
166 return RetCode::SUCCESS;
167}
168
169// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530170IEffect::Status PresetReverbSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000171 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530172 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
173 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000174 *out++ = *in++;
175 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530176 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000177}
178
179} // namespace aidl::android::hardware::audio::effect