blob: 0544e3fae17ec23426d9790fbea0cdc5c387b919 [file] [log] [blame]
Shunkai Yao242521c2023-01-29 18:08:09 +00001/*
2 * Copyright (C) 2023 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 <cstdint>
18#include <cstring>
19#include <optional>
20#define LOG_TAG "AidlConversionEnvReverb"
21//#define LOG_NDEBUG 0
22
23#include <error/expected_utils.h>
24#include <media/AidlConversionCppNdk.h>
25#include <media/AidlConversionNdk.h>
26#include <media/AidlConversionEffect.h>
27#include <media/audiohal/AudioEffectUuid.h>
28#include <system/audio_effects/effect_environmentalreverb.h>
29
30#include <utils/Log.h>
31
32#include "AidlConversionEnvReverb.h"
33
34namespace android {
35namespace effect {
36
37using ::aidl::android::convertIntegral;
38using ::aidl::android::getParameterSpecificField;
39using ::aidl::android::aidl_utils::statusTFromBinderStatus;
40using ::aidl::android::hardware::audio::effect::EnvironmentalReverb;
41using ::aidl::android::hardware::audio::effect::Parameter;
42using ::android::status_t;
43using utils::EffectParamReader;
44using utils::EffectParamWriter;
45
Shunkai Yao529525c2023-02-21 18:03:44 +000046/**
47 * Macro to get a parameter from effect_param_t wrapper and set it to AIDL effect.
48 *
49 * Return if there is any error, otherwise continue execution.
50 *
51 * @param param EffectParamReader, a reader wrapper of effect_param_t.
52 * @param aidlType Type of the AIDL parameter field, used to construct AIDL Parameter union.
53 * @param valueType Type of the value get from effect_param_t.
54 * @param tag The AIDL parameter union field tag.
55 */
56#define SET_AIDL_PARAMETER(param, aidlType, valueType, tag) \
57 { \
58 Parameter aidlParam; \
59 valueType value; \
60 if (status_t status = param.readFromValue(&value); status != OK) { \
61 ALOGE("%s %s read from parameter failed, ret %d", __func__, #tag, status); \
62 return status; \
63 } \
64 aidlParam = MAKE_SPECIFIC_PARAMETER( \
65 EnvironmentalReverb, environmentalReverb, tag, \
66 VALUE_OR_RETURN_STATUS(aidl::android::convertIntegral<aidlType>(value))); \
67 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->setParameter(aidlParam))); \
Shunkai Yao242521c2023-01-29 18:08:09 +000068 }
69
Shunkai Yao529525c2023-02-21 18:03:44 +000070/**
71 * Macro to get a parameter from AIDL effect and write the value to effect_param_t with wrapper.
72 *
73 * Return if there is any error, otherwise continue execution.
74 *
75 * @param param EffectParamWriter, a writer wrapper of effect_param_t.
76 * @param aidlType Type of the AIDL parameter field, used to construct AIDL Parameter union.
77 * @param valueType Type of the value get from effect_param_t.
78 * @param tag The AIDL parameter union field tag.
79 */
80#define GET_AIDL_PARAMETER(param, aidltype, valueType, tag) \
Shunkai Yao242521c2023-01-29 18:08:09 +000081 { \
Shunkai Yao529525c2023-02-21 18:03:44 +000082 aidltype value; \
Shunkai Yao242521c2023-01-29 18:08:09 +000083 Parameter aidlParam; \
84 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(EnvironmentalReverb, environmentalReverbTag, \
85 EnvironmentalReverb::tag); \
86 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); \
Shunkai Yao529525c2023-02-21 18:03:44 +000087 value = VALUE_OR_RETURN_STATUS( \
88 GET_PARAMETER_SPECIFIC_FIELD(aidlParam, EnvironmentalReverb, environmentalReverb, \
89 EnvironmentalReverb::tag, std::decay_t<aidltype>)); \
90 if (status_t status = param.writeToValue((valueType*)&value); status != OK) { \
91 param.setStatus(status); \
92 ALOGE("%s %s write to parameter failed %d, ret %d", __func__, #tag, value, status); \
93 return status; \
94 } \
Shunkai Yao242521c2023-01-29 18:08:09 +000095 }
96
97status_t AidlConversionEnvReverb::setParameter(EffectParamReader& param) {
98 uint32_t type = 0;
Shunkai Yao529525c2023-02-21 18:03:44 +000099 if (status_t status = param.readFromParameter(&type); status != OK) {
100 ALOGE("%s failed to read type from %s, ret %d", __func__, param.toString().c_str(), status);
Shunkai Yao242521c2023-01-29 18:08:09 +0000101 return BAD_VALUE;
102 }
Shunkai Yao529525c2023-02-21 18:03:44 +0000103
Shunkai Yao242521c2023-01-29 18:08:09 +0000104 switch (type) {
105 case REVERB_PARAM_ROOM_LEVEL: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000106 SET_AIDL_PARAMETER(param, int32_t, int16_t, roomLevelMb);
Shunkai Yao242521c2023-01-29 18:08:09 +0000107 break;
108 }
109 case REVERB_PARAM_ROOM_HF_LEVEL: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000110 SET_AIDL_PARAMETER(param, int32_t, int16_t, roomHfLevelMb);
Shunkai Yao242521c2023-01-29 18:08:09 +0000111 break;
112 }
113 case REVERB_PARAM_DECAY_TIME: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000114 SET_AIDL_PARAMETER(param, int32_t, uint32_t, decayTimeMs);
Shunkai Yao242521c2023-01-29 18:08:09 +0000115 break;
116 }
117 case REVERB_PARAM_DECAY_HF_RATIO: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000118 SET_AIDL_PARAMETER(param, int32_t, int16_t, decayHfRatioPm);
Shunkai Yao242521c2023-01-29 18:08:09 +0000119 break;
120 }
121 case REVERB_PARAM_REFLECTIONS_LEVEL: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000122 SET_AIDL_PARAMETER(param, int32_t, int16_t, reflectionsLevelMb);
Shunkai Yao242521c2023-01-29 18:08:09 +0000123 break;
124 }
125 case REVERB_PARAM_REFLECTIONS_DELAY: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000126 SET_AIDL_PARAMETER(param, int32_t, uint32_t, reflectionsDelayMs);
127 break;
128 }
129 case REVERB_PARAM_REVERB_LEVEL: {
130 SET_AIDL_PARAMETER(param, int32_t, int16_t, levelMb);
131 break;
132 }
133 case REVERB_PARAM_REVERB_DELAY: {
134 SET_AIDL_PARAMETER(param, int32_t, uint32_t, delayMs);
135 break;
136 }
137 case REVERB_PARAM_DIFFUSION: {
138 SET_AIDL_PARAMETER(param, int32_t, int16_t, diffusionPm);
139 break;
140 }
141 case REVERB_PARAM_DENSITY: {
142 SET_AIDL_PARAMETER(param, int32_t, int16_t, densityPm);
143 break;
144 }
145 case REVERB_PARAM_BYPASS: {
146 SET_AIDL_PARAMETER(param, bool, int32_t, bypass);
Shunkai Yao242521c2023-01-29 18:08:09 +0000147 break;
148 }
149 case REVERB_PARAM_PROPERTIES: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000150 if (sizeof(t_reverb_settings) > param.getValueSize()) {
151 ALOGE("%s vsize %zu less than t_reverb_settings size %zu", __func__,
152 param.getValueSize(), sizeof(t_reverb_settings));
153 return BAD_VALUE;
154 }
155 // this sequency needs to be aligned with t_reverb_settings
156 SET_AIDL_PARAMETER(param, int32_t, int16_t, roomLevelMb);
157 SET_AIDL_PARAMETER(param, int32_t, int16_t, roomHfLevelMb);
158 SET_AIDL_PARAMETER(param, int32_t, uint32_t, decayTimeMs);
159 SET_AIDL_PARAMETER(param, int32_t, int16_t, decayHfRatioPm);
160 SET_AIDL_PARAMETER(param, int32_t, int16_t, reflectionsLevelMb);
161 SET_AIDL_PARAMETER(param, int32_t, uint32_t, reflectionsDelayMs);
162 SET_AIDL_PARAMETER(param, int32_t, int16_t, levelMb);
163 SET_AIDL_PARAMETER(param, int32_t, uint32_t, delayMs);
164 SET_AIDL_PARAMETER(param, int32_t, int16_t, diffusionPm);
165 SET_AIDL_PARAMETER(param, int32_t, int16_t, densityPm);
Shunkai Yao242521c2023-01-29 18:08:09 +0000166 break;
167 }
168 default: {
169 // TODO: handle with vendor extension
170 }
171 }
Shunkai Yao529525c2023-02-21 18:03:44 +0000172 return OK;
Shunkai Yao242521c2023-01-29 18:08:09 +0000173}
174
175status_t AidlConversionEnvReverb::getParameter(EffectParamWriter& param) {
176 uint32_t type = 0;
Shunkai Yao529525c2023-02-21 18:03:44 +0000177 if (status_t status = param.readFromParameter(&type); status != OK) {
178 ALOGE("%s failed to read type from %s", __func__, param.toString().c_str());
179 param.setStatus(status);
180 return status;
Shunkai Yao242521c2023-01-29 18:08:09 +0000181 }
Shunkai Yao529525c2023-02-21 18:03:44 +0000182
Shunkai Yao242521c2023-01-29 18:08:09 +0000183 switch (type) {
184 case REVERB_PARAM_ROOM_LEVEL: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000185 GET_AIDL_PARAMETER(param, int32_t, int16_t, roomLevelMb);
186 break;
Shunkai Yao242521c2023-01-29 18:08:09 +0000187 }
188 case REVERB_PARAM_ROOM_HF_LEVEL: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000189 GET_AIDL_PARAMETER(param, int32_t, int16_t, roomHfLevelMb);
190 break;
Shunkai Yao242521c2023-01-29 18:08:09 +0000191 }
192 case REVERB_PARAM_DECAY_TIME: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000193 GET_AIDL_PARAMETER(param, int32_t, uint32_t, decayTimeMs);
194 break;
Shunkai Yao242521c2023-01-29 18:08:09 +0000195 }
196 case REVERB_PARAM_DECAY_HF_RATIO: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000197 GET_AIDL_PARAMETER(param, int32_t, int16_t, decayHfRatioPm);
198 break;
Shunkai Yao242521c2023-01-29 18:08:09 +0000199 }
200 case REVERB_PARAM_REFLECTIONS_LEVEL: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000201 GET_AIDL_PARAMETER(param, int32_t, int16_t, reflectionsLevelMb);
Shunkai Yao242521c2023-01-29 18:08:09 +0000202 break;
203 }
204 case REVERB_PARAM_REFLECTIONS_DELAY: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000205 GET_AIDL_PARAMETER(param, int32_t, uint32_t, reflectionsDelayMs);
206 break;
207 }
208 case REVERB_PARAM_REVERB_LEVEL: {
209 GET_AIDL_PARAMETER(param, int32_t, int16_t, levelMb);
210 break;
211 }
212 case REVERB_PARAM_REVERB_DELAY: {
213 GET_AIDL_PARAMETER(param, int32_t, uint32_t, delayMs);
214 break;
215 }
216 case REVERB_PARAM_DIFFUSION: {
217 GET_AIDL_PARAMETER(param, int32_t, int16_t, diffusionPm);
218 break;
219 }
220 case REVERB_PARAM_DENSITY: {
221 GET_AIDL_PARAMETER(param, int32_t, int16_t, densityPm);
222 break;
223 }
224 case REVERB_PARAM_BYPASS: {
225 GET_AIDL_PARAMETER(param, bool, int32_t, bypass);
Shunkai Yao242521c2023-01-29 18:08:09 +0000226 break;
227 }
228 case REVERB_PARAM_PROPERTIES: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000229 // this sequency needs to be aligned with t_reverb_settings
230 GET_AIDL_PARAMETER(param, int32_t, int16_t, roomLevelMb);
231 GET_AIDL_PARAMETER(param, int32_t, int16_t, roomHfLevelMb);
232 GET_AIDL_PARAMETER(param, int32_t, uint32_t, decayTimeMs);
233 GET_AIDL_PARAMETER(param, int32_t, int16_t, decayHfRatioPm);
234 GET_AIDL_PARAMETER(param, int32_t, int16_t, reflectionsLevelMb);
235 GET_AIDL_PARAMETER(param, int32_t, uint32_t, reflectionsDelayMs);
236 GET_AIDL_PARAMETER(param, int32_t, int16_t, levelMb);
237 GET_AIDL_PARAMETER(param, int32_t, uint32_t, delayMs);
238 GET_AIDL_PARAMETER(param, int32_t, int16_t, diffusionPm);
239 GET_AIDL_PARAMETER(param, int32_t, int16_t, densityPm);
Shunkai Yao242521c2023-01-29 18:08:09 +0000240 break;
241 }
242 default: {
243 // TODO: handle with vendor extension
Shunkai Yao529525c2023-02-21 18:03:44 +0000244 return BAD_VALUE;
Shunkai Yao242521c2023-01-29 18:08:09 +0000245 }
246 }
Shunkai Yao529525c2023-02-21 18:03:44 +0000247 return OK;
Shunkai Yao242521c2023-01-29 18:08:09 +0000248}
249
250} // namespace effect
251} // namespace android