blob: 49e41a47e0fe6eace9b221bed1420173a15796c5 [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;
Shunkai Yaoda4a6402023-03-03 19:38:17 +000042using ::aidl::android::hardware::audio::effect::VendorExtension;
Shunkai Yao242521c2023-01-29 18:08:09 +000043using ::android::status_t;
44using utils::EffectParamReader;
45using utils::EffectParamWriter;
46
Shunkai Yao529525c2023-02-21 18:03:44 +000047/**
48 * Macro to get a parameter from effect_param_t wrapper and set it to AIDL effect.
49 *
50 * Return if there is any error, otherwise continue execution.
51 *
52 * @param param EffectParamReader, a reader wrapper of effect_param_t.
53 * @param aidlType Type of the AIDL parameter field, used to construct AIDL Parameter union.
54 * @param valueType Type of the value get from effect_param_t.
55 * @param tag The AIDL parameter union field tag.
56 */
57#define SET_AIDL_PARAMETER(param, aidlType, valueType, tag) \
58 { \
59 Parameter aidlParam; \
60 valueType value; \
61 if (status_t status = param.readFromValue(&value); status != OK) { \
62 ALOGE("%s %s read from parameter failed, ret %d", __func__, #tag, status); \
63 return status; \
64 } \
65 aidlParam = MAKE_SPECIFIC_PARAMETER( \
66 EnvironmentalReverb, environmentalReverb, tag, \
67 VALUE_OR_RETURN_STATUS(aidl::android::convertIntegral<aidlType>(value))); \
68 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->setParameter(aidlParam))); \
Shunkai Yao242521c2023-01-29 18:08:09 +000069 }
70
Shunkai Yao529525c2023-02-21 18:03:44 +000071/**
72 * Macro to get a parameter from AIDL effect and write the value to effect_param_t with wrapper.
73 *
74 * Return if there is any error, otherwise continue execution.
75 *
76 * @param param EffectParamWriter, a writer wrapper of effect_param_t.
77 * @param aidlType Type of the AIDL parameter field, used to construct AIDL Parameter union.
78 * @param valueType Type of the value get from effect_param_t.
79 * @param tag The AIDL parameter union field tag.
80 */
81#define GET_AIDL_PARAMETER(param, aidltype, valueType, tag) \
Shunkai Yao242521c2023-01-29 18:08:09 +000082 { \
Shunkai Yao529525c2023-02-21 18:03:44 +000083 aidltype value; \
Shunkai Yao242521c2023-01-29 18:08:09 +000084 Parameter aidlParam; \
85 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(EnvironmentalReverb, environmentalReverbTag, \
86 EnvironmentalReverb::tag); \
87 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); \
Shunkai Yao529525c2023-02-21 18:03:44 +000088 value = VALUE_OR_RETURN_STATUS( \
89 GET_PARAMETER_SPECIFIC_FIELD(aidlParam, EnvironmentalReverb, environmentalReverb, \
90 EnvironmentalReverb::tag, std::decay_t<aidltype>)); \
91 if (status_t status = param.writeToValue((valueType*)&value); status != OK) { \
92 param.setStatus(status); \
93 ALOGE("%s %s write to parameter failed %d, ret %d", __func__, #tag, value, status); \
94 return status; \
95 } \
Shunkai Yao242521c2023-01-29 18:08:09 +000096 }
97
98status_t AidlConversionEnvReverb::setParameter(EffectParamReader& param) {
99 uint32_t type = 0;
Shunkai Yao529525c2023-02-21 18:03:44 +0000100 if (status_t status = param.readFromParameter(&type); status != OK) {
101 ALOGE("%s failed to read type from %s, ret %d", __func__, param.toString().c_str(), status);
Shunkai Yao242521c2023-01-29 18:08:09 +0000102 return BAD_VALUE;
103 }
Shunkai Yao529525c2023-02-21 18:03:44 +0000104
Shunkai Yao242521c2023-01-29 18:08:09 +0000105 switch (type) {
106 case REVERB_PARAM_ROOM_LEVEL: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000107 SET_AIDL_PARAMETER(param, int32_t, int16_t, roomLevelMb);
Shunkai Yao242521c2023-01-29 18:08:09 +0000108 break;
109 }
110 case REVERB_PARAM_ROOM_HF_LEVEL: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000111 SET_AIDL_PARAMETER(param, int32_t, int16_t, roomHfLevelMb);
Shunkai Yao242521c2023-01-29 18:08:09 +0000112 break;
113 }
114 case REVERB_PARAM_DECAY_TIME: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000115 SET_AIDL_PARAMETER(param, int32_t, uint32_t, decayTimeMs);
Shunkai Yao242521c2023-01-29 18:08:09 +0000116 break;
117 }
118 case REVERB_PARAM_DECAY_HF_RATIO: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000119 SET_AIDL_PARAMETER(param, int32_t, int16_t, decayHfRatioPm);
Shunkai Yao242521c2023-01-29 18:08:09 +0000120 break;
121 }
122 case REVERB_PARAM_REFLECTIONS_LEVEL: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000123 SET_AIDL_PARAMETER(param, int32_t, int16_t, reflectionsLevelMb);
Shunkai Yao242521c2023-01-29 18:08:09 +0000124 break;
125 }
126 case REVERB_PARAM_REFLECTIONS_DELAY: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000127 SET_AIDL_PARAMETER(param, int32_t, uint32_t, reflectionsDelayMs);
128 break;
129 }
130 case REVERB_PARAM_REVERB_LEVEL: {
131 SET_AIDL_PARAMETER(param, int32_t, int16_t, levelMb);
132 break;
133 }
134 case REVERB_PARAM_REVERB_DELAY: {
135 SET_AIDL_PARAMETER(param, int32_t, uint32_t, delayMs);
136 break;
137 }
138 case REVERB_PARAM_DIFFUSION: {
139 SET_AIDL_PARAMETER(param, int32_t, int16_t, diffusionPm);
140 break;
141 }
142 case REVERB_PARAM_DENSITY: {
143 SET_AIDL_PARAMETER(param, int32_t, int16_t, densityPm);
144 break;
145 }
146 case REVERB_PARAM_BYPASS: {
147 SET_AIDL_PARAMETER(param, bool, int32_t, bypass);
Shunkai Yao242521c2023-01-29 18:08:09 +0000148 break;
149 }
150 case REVERB_PARAM_PROPERTIES: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000151 if (sizeof(t_reverb_settings) > param.getValueSize()) {
152 ALOGE("%s vsize %zu less than t_reverb_settings size %zu", __func__,
153 param.getValueSize(), sizeof(t_reverb_settings));
154 return BAD_VALUE;
155 }
156 // this sequency needs to be aligned with t_reverb_settings
157 SET_AIDL_PARAMETER(param, int32_t, int16_t, roomLevelMb);
158 SET_AIDL_PARAMETER(param, int32_t, int16_t, roomHfLevelMb);
159 SET_AIDL_PARAMETER(param, int32_t, uint32_t, decayTimeMs);
160 SET_AIDL_PARAMETER(param, int32_t, int16_t, decayHfRatioPm);
161 SET_AIDL_PARAMETER(param, int32_t, int16_t, reflectionsLevelMb);
162 SET_AIDL_PARAMETER(param, int32_t, uint32_t, reflectionsDelayMs);
163 SET_AIDL_PARAMETER(param, int32_t, int16_t, levelMb);
164 SET_AIDL_PARAMETER(param, int32_t, uint32_t, delayMs);
165 SET_AIDL_PARAMETER(param, int32_t, int16_t, diffusionPm);
166 SET_AIDL_PARAMETER(param, int32_t, int16_t, densityPm);
Shunkai Yao242521c2023-01-29 18:08:09 +0000167 break;
168 }
169 default: {
Shunkai Yaoda4a6402023-03-03 19:38:17 +0000170 // for vendor extension, copy data area to the DefaultExtension, parameter ignored
171 VendorExtension ext = VALUE_OR_RETURN_STATUS(
172 aidl::android::legacy2aidl_EffectParameterReader_Data_VendorExtension(param));
173 Parameter aidlParam = MAKE_SPECIFIC_PARAMETER(EnvironmentalReverb,
174 environmentalReverb, vendor, ext);
175 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->setParameter(aidlParam)));
176 break;
Shunkai Yao242521c2023-01-29 18:08:09 +0000177 }
178 }
Shunkai Yao529525c2023-02-21 18:03:44 +0000179 return OK;
Shunkai Yao242521c2023-01-29 18:08:09 +0000180}
181
182status_t AidlConversionEnvReverb::getParameter(EffectParamWriter& param) {
183 uint32_t type = 0;
Shunkai Yao529525c2023-02-21 18:03:44 +0000184 if (status_t status = param.readFromParameter(&type); status != OK) {
185 ALOGE("%s failed to read type from %s", __func__, param.toString().c_str());
186 param.setStatus(status);
187 return status;
Shunkai Yao242521c2023-01-29 18:08:09 +0000188 }
Shunkai Yao529525c2023-02-21 18:03:44 +0000189
Shunkai Yao242521c2023-01-29 18:08:09 +0000190 switch (type) {
191 case REVERB_PARAM_ROOM_LEVEL: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000192 GET_AIDL_PARAMETER(param, int32_t, int16_t, roomLevelMb);
193 break;
Shunkai Yao242521c2023-01-29 18:08:09 +0000194 }
195 case REVERB_PARAM_ROOM_HF_LEVEL: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000196 GET_AIDL_PARAMETER(param, int32_t, int16_t, roomHfLevelMb);
197 break;
Shunkai Yao242521c2023-01-29 18:08:09 +0000198 }
199 case REVERB_PARAM_DECAY_TIME: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000200 GET_AIDL_PARAMETER(param, int32_t, uint32_t, decayTimeMs);
201 break;
Shunkai Yao242521c2023-01-29 18:08:09 +0000202 }
203 case REVERB_PARAM_DECAY_HF_RATIO: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000204 GET_AIDL_PARAMETER(param, int32_t, int16_t, decayHfRatioPm);
205 break;
Shunkai Yao242521c2023-01-29 18:08:09 +0000206 }
207 case REVERB_PARAM_REFLECTIONS_LEVEL: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000208 GET_AIDL_PARAMETER(param, int32_t, int16_t, reflectionsLevelMb);
Shunkai Yao242521c2023-01-29 18:08:09 +0000209 break;
210 }
211 case REVERB_PARAM_REFLECTIONS_DELAY: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000212 GET_AIDL_PARAMETER(param, int32_t, uint32_t, reflectionsDelayMs);
213 break;
214 }
215 case REVERB_PARAM_REVERB_LEVEL: {
216 GET_AIDL_PARAMETER(param, int32_t, int16_t, levelMb);
217 break;
218 }
219 case REVERB_PARAM_REVERB_DELAY: {
220 GET_AIDL_PARAMETER(param, int32_t, uint32_t, delayMs);
221 break;
222 }
223 case REVERB_PARAM_DIFFUSION: {
224 GET_AIDL_PARAMETER(param, int32_t, int16_t, diffusionPm);
225 break;
226 }
227 case REVERB_PARAM_DENSITY: {
228 GET_AIDL_PARAMETER(param, int32_t, int16_t, densityPm);
229 break;
230 }
231 case REVERB_PARAM_BYPASS: {
232 GET_AIDL_PARAMETER(param, bool, int32_t, bypass);
Shunkai Yao242521c2023-01-29 18:08:09 +0000233 break;
234 }
235 case REVERB_PARAM_PROPERTIES: {
Shunkai Yao529525c2023-02-21 18:03:44 +0000236 // this sequency needs to be aligned with t_reverb_settings
237 GET_AIDL_PARAMETER(param, int32_t, int16_t, roomLevelMb);
238 GET_AIDL_PARAMETER(param, int32_t, int16_t, roomHfLevelMb);
239 GET_AIDL_PARAMETER(param, int32_t, uint32_t, decayTimeMs);
240 GET_AIDL_PARAMETER(param, int32_t, int16_t, decayHfRatioPm);
241 GET_AIDL_PARAMETER(param, int32_t, int16_t, reflectionsLevelMb);
242 GET_AIDL_PARAMETER(param, int32_t, uint32_t, reflectionsDelayMs);
243 GET_AIDL_PARAMETER(param, int32_t, int16_t, levelMb);
244 GET_AIDL_PARAMETER(param, int32_t, uint32_t, delayMs);
245 GET_AIDL_PARAMETER(param, int32_t, int16_t, diffusionPm);
246 GET_AIDL_PARAMETER(param, int32_t, int16_t, densityPm);
Shunkai Yao242521c2023-01-29 18:08:09 +0000247 break;
248 }
249 default: {
Shunkai Yaoda4a6402023-03-03 19:38:17 +0000250 VENDOR_EXTENSION_GET_AND_RETURN(EnvironmentalReverb, environmentalReverb, param);
Shunkai Yao242521c2023-01-29 18:08:09 +0000251 }
252 }
Shunkai Yao529525c2023-02-21 18:03:44 +0000253 return OK;
Shunkai Yao242521c2023-01-29 18:08:09 +0000254}
255
256} // namespace effect
257} // namespace android