blob: 2da3ff6ded0d7b077dd23935e6c1dc2db98a54d6 [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
Shunkai Yao87811022023-02-13 17:40:37 +000065const std::vector<PresetReverb::Presets> PresetReverbSw::kSupportedPresets{
Sham Rathod73aa2c32022-12-20 17:03:40 +053066 ndk::enum_range<PresetReverb::Presets>().begin(),
67 ndk::enum_range<PresetReverb::Presets>().end()};
68
Shunkai Yao87811022023-02-13 17:40:37 +000069const std::vector<Range::PresetReverbRange> PresetReverbSw::kRanges = {
70 MAKE_RANGE(PresetReverb, supportedPresets, PresetReverbSw::kSupportedPresets,
71 PresetReverbSw::kSupportedPresets)};
72
73const Capability PresetReverbSw::kCapability = {
74 .range = Range::make<Range::presetReverb>(PresetReverbSw::kRanges)};
75
Shunkai Yaoc12e0822022-12-12 07:13:58 +000076const Descriptor PresetReverbSw::kDescriptor = {
77 .common = {.id = {.type = kPresetReverbTypeUUID,
78 .uuid = kPresetReverbSwImplUUID,
79 .proxy = std::nullopt},
80 .flags = {.type = Flags::Type::INSERT,
81 .insert = Flags::Insert::FIRST,
82 .volume = Flags::Volume::CTRL},
83 .name = PresetReverbSw::kEffectName,
84 .implementor = "The Android Open Source Project"},
Shunkai Yao87811022023-02-13 17:40:37 +000085 .capability = PresetReverbSw::kCapability};
Shunkai Yaoc12e0822022-12-12 07:13:58 +000086
Shunkai Yao812d5b42022-11-16 18:08:50 +000087ndk::ScopedAStatus PresetReverbSw::getDescriptor(Descriptor* _aidl_return) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000088 LOG(DEBUG) << __func__ << kDescriptor.toString();
89 *_aidl_return = kDescriptor;
90 return ndk::ScopedAStatus::ok();
91}
92
Shunkai Yao812d5b42022-11-16 18:08:50 +000093ndk::ScopedAStatus PresetReverbSw::setParameterSpecific(const Parameter::Specific& specific) {
Shunkai Yaoc12e0822022-12-12 07:13:58 +000094 RETURN_IF(Parameter::Specific::presetReverb != specific.getTag(), EX_ILLEGAL_ARGUMENT,
Shunkai Yao6afc8552022-10-26 22:47:20 +000095 "EffectNotSupported");
Shunkai Yao6afc8552022-10-26 22:47:20 +000096
Sham Rathod73aa2c32022-12-20 17:03:40 +053097 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
98
99 auto& prParam = specific.get<Parameter::Specific::presetReverb>();
Shunkai Yao87811022023-02-13 17:40:37 +0000100 RETURN_IF(!inRange(prParam, kRanges), EX_ILLEGAL_ARGUMENT, "outOfRange");
Sham Rathod73aa2c32022-12-20 17:03:40 +0530101 auto tag = prParam.getTag();
102
103 switch (tag) {
104 case PresetReverb::preset: {
105 RETURN_IF(
106 mContext->setPRPreset(prParam.get<PresetReverb::preset>()) != RetCode::SUCCESS,
107 EX_ILLEGAL_ARGUMENT, "setPresetFailed");
108 return ndk::ScopedAStatus::ok();
109 }
110 default: {
111 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
112 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
113 "PresetReverbTagNotSupported");
114 }
115 }
Shunkai Yao6afc8552022-10-26 22:47:20 +0000116}
117
Shunkai Yao812d5b42022-11-16 18:08:50 +0000118ndk::ScopedAStatus PresetReverbSw::getParameterSpecific(const Parameter::Id& id,
119 Parameter::Specific* specific) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000120 auto tag = id.getTag();
Shunkai Yaoc12e0822022-12-12 07:13:58 +0000121 RETURN_IF(Parameter::Id::presetReverbTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
Sham Rathod73aa2c32022-12-20 17:03:40 +0530122 auto prId = id.get<Parameter::Id::presetReverbTag>();
123 auto prIdTag = prId.getTag();
124 switch (prIdTag) {
125 case PresetReverb::Id::commonTag:
126 return getParameterPresetReverb(prId.get<PresetReverb::Id::commonTag>(), specific);
127 default:
128 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
129 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
130 "PresetReverbTagNotSupported");
131 }
132}
133
134ndk::ScopedAStatus PresetReverbSw::getParameterPresetReverb(const PresetReverb::Tag& tag,
135 Parameter::Specific* specific) {
136 RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
137 PresetReverb prParam;
138 switch (tag) {
139 case PresetReverb::preset: {
140 prParam.set<PresetReverb::preset>(mContext->getPRPreset());
141 break;
142 }
143 default: {
144 LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
145 return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
146 "PresetReverbTagNotSupported");
147 }
148 }
149
150 specific->set<Parameter::Specific::presetReverb>(prParam);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000151 return ndk::ScopedAStatus::ok();
152}
153
Shunkai Yao812d5b42022-11-16 18:08:50 +0000154std::shared_ptr<EffectContext> PresetReverbSw::createContext(const Parameter::Common& common) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000155 if (mContext) {
156 LOG(DEBUG) << __func__ << " context already exist";
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530157 } else {
158 mContext = std::make_shared<PresetReverbSwContext>(1 /* statusFmqDepth */, common);
Shunkai Yao6afc8552022-10-26 22:47:20 +0000159 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530160
161 return mContext;
162}
163
164std::shared_ptr<EffectContext> PresetReverbSw::getContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000165 return mContext;
166}
167
Shunkai Yao812d5b42022-11-16 18:08:50 +0000168RetCode PresetReverbSw::releaseContext() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000169 if (mContext) {
170 mContext.reset();
171 }
172 return RetCode::SUCCESS;
173}
174
175// Processing method running in EffectWorker thread.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530176IEffect::Status PresetReverbSw::effectProcessImpl(float* in, float* out, int samples) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000177 // TODO: get data buffer and process.
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530178 LOG(DEBUG) << __func__ << " in " << in << " out " << out << " samples " << samples;
179 for (int i = 0; i < samples; i++) {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000180 *out++ = *in++;
181 }
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530182 return {STATUS_OK, samples, samples};
Shunkai Yao6afc8552022-10-26 22:47:20 +0000183}
184
185} // namespace aidl::android::hardware::audio::effect