1 /*
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 <cstddef>
18 #include <cstdint>
19 #include <cstring>
20 #include <optional>
21 #define LOG_TAG "AidlConversionVisualizer"
22 //#define LOG_NDEBUG 0
23
24 #include <error/expected_utils.h>
25 #include <media/AidlConversionNdk.h>
26 #include <media/AidlConversionEffect.h>
27 #include <system/audio_effects/effect_visualizer.h>
28
29 #include <utils/Log.h>
30
31 #include "AidlConversionVisualizer.h"
32
33 namespace android {
34 namespace effect {
35
36 using ::aidl::android::getParameterSpecificField;
37 using ::aidl::android::aidl_utils::statusTFromBinderStatus;
38 using ::aidl::android::hardware::audio::effect::Parameter;
39 using ::aidl::android::hardware::audio::effect::VendorExtension;
40 using ::aidl::android::hardware::audio::effect::Visualizer;
41 using ::android::status_t;
42 using utils::EffectParamReader;
43 using utils::EffectParamWriter;
44
setParameter(EffectParamReader & param)45 status_t AidlConversionVisualizer::setParameter(EffectParamReader& param) {
46 uint32_t type = 0, value = 0;
47 if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
48 OK != param.readFromParameter(&type) || OK != param.readFromValue(&value)) {
49 ALOGE("%s invalid param %s", __func__, param.toString().c_str());
50 return BAD_VALUE;
51 }
52 Parameter aidlParam;
53 switch (type) {
54 case VISUALIZER_PARAM_CAPTURE_SIZE: {
55 mCaptureSize = value;
56 aidlParam = MAKE_SPECIFIC_PARAMETER(Visualizer, visualizer, captureSamples, value);
57 break;
58 }
59 case VISUALIZER_PARAM_SCALING_MODE: {
60 Visualizer::ScalingMode mode = VALUE_OR_RETURN_STATUS(
61 aidl::android::legacy2aidl_Parameter_Visualizer_uint32_ScalingMode(value));
62 aidlParam = MAKE_SPECIFIC_PARAMETER(Visualizer, visualizer, scalingMode, mode);
63 break;
64 }
65 case VISUALIZER_PARAM_LATENCY: {
66 aidlParam = MAKE_SPECIFIC_PARAMETER(Visualizer, visualizer, latencyMs, value);
67 break;
68 }
69 case VISUALIZER_PARAM_MEASUREMENT_MODE: {
70 Visualizer::MeasurementMode mode = VALUE_OR_RETURN_STATUS(
71 aidl::android::legacy2aidl_Parameter_Visualizer_uint32_MeasurementMode(value));
72 aidlParam = MAKE_SPECIFIC_PARAMETER(Visualizer, visualizer, measurementMode, mode);
73 break;
74 }
75 default: {
76 // for vendor extension, copy data area to the DefaultExtension, parameter ignored
77 VendorExtension ext = VALUE_OR_RETURN_STATUS(
78 aidl::android::legacy2aidl_EffectParameterReader_VendorExtension(param));
79 aidlParam = MAKE_SPECIFIC_PARAMETER(Visualizer, visualizer, vendor, ext);
80 break;
81 }
82 }
83 return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
84 }
85
getParameter(EffectParamWriter & param)86 status_t AidlConversionVisualizer::getParameter(EffectParamWriter& param) {
87 uint32_t type = 0, value = 0;
88 if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(int32_t)) ||
89 OK != param.readFromParameter(&type)) {
90 ALOGE("%s invalid param %s", __func__, param.toString().c_str());
91 param.setStatus(BAD_VALUE);
92 return BAD_VALUE;
93 }
94 Parameter aidlParam;
95 switch (type) {
96 case VISUALIZER_PARAM_CAPTURE_SIZE: {
97 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(Visualizer, visualizerTag,
98 Visualizer::captureSamples);
99 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
100 value = VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD(
101 aidlParam, Visualizer, visualizer, Visualizer::captureSamples, int32_t));
102 mCaptureSize = value;
103 return param.writeToValue(&value);
104 }
105 case VISUALIZER_PARAM_SCALING_MODE: {
106 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(Visualizer, visualizerTag,
107 Visualizer::scalingMode);
108 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
109 Visualizer::ScalingMode mode = VALUE_OR_RETURN_STATUS(
110 GET_PARAMETER_SPECIFIC_FIELD(aidlParam, Visualizer, visualizer,
111 Visualizer::scalingMode, Visualizer::ScalingMode));
112 value = VALUE_OR_RETURN_STATUS(
113 aidl::android::aidl2legacy_Parameter_Visualizer_ScalingMode_uint32(mode));
114 return param.writeToValue(&value);
115 }
116 case VISUALIZER_PARAM_LATENCY: {
117 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(Visualizer, visualizerTag,
118 Visualizer::latencyMs);
119 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
120 value = (int32_t)VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD(
121 aidlParam, Visualizer, visualizer, Visualizer::latencyMs, int32_t));
122 return param.writeToValue(&value);
123 }
124 case VISUALIZER_PARAM_MEASUREMENT_MODE: {
125 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(Visualizer, visualizerTag,
126 Visualizer::measurementMode);
127 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
128 Visualizer::MeasurementMode mode = VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD(
129 aidlParam, Visualizer, visualizer, Visualizer::measurementMode,
130 Visualizer::MeasurementMode));
131 value = VALUE_OR_RETURN_STATUS(
132 aidl::android::aidl2legacy_Parameter_Visualizer_MeasurementMode_uint32(mode));
133 return param.writeToValue(&value);
134 }
135 default: {
136 VENDOR_EXTENSION_GET_AND_RETURN(Visualizer, visualizer, param);
137 }
138 }
139 }
140
visualizerCapture(uint32_t * replySize,void * pReplyData)141 status_t AidlConversionVisualizer::visualizerCapture(uint32_t* replySize, void* pReplyData) {
142 if (!replySize || !pReplyData || *replySize != mCaptureSize) {
143 ALOGE("%s illegal param replySize %p pReplyData %p", __func__, replySize, pReplyData);
144 return BAD_VALUE;
145 }
146
147 Parameter aidlParam;
148 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(Visualizer, visualizerTag,
149 Visualizer::captureSampleBuffer);
150 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
151 const auto& samples = VALUE_OR_RETURN_STATUS(
152 GET_PARAMETER_SPECIFIC_FIELD(aidlParam, Visualizer, visualizer,
153 Visualizer::captureSampleBuffer, std::vector<uint8_t>));
154 size_t len = std::min((size_t)*replySize, samples.size());
155 std::memcpy(pReplyData, samples.data(), *replySize = len);
156 return OK;
157 }
158
visualizerMeasure(uint32_t * replySize,void * pReplyData)159 status_t AidlConversionVisualizer::visualizerMeasure(uint32_t* replySize, void* pReplyData) {
160 if (!replySize || !pReplyData || *replySize != 2 * sizeof(int32_t)) {
161 ALOGE("%s illegal param replySize %p pReplyData %p", __func__, replySize, pReplyData);
162 return BAD_VALUE;
163 }
164
165 Parameter aidlParam;
166 Parameter::Id id =
167 MAKE_SPECIFIC_PARAMETER_ID(Visualizer, visualizerTag, Visualizer::measurement);
168 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
169 const auto& measure = VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD(
170 aidlParam, Visualizer, visualizer, Visualizer::measurement, Visualizer::Measurement));
171 int32_t* reply = (int32_t *) pReplyData;
172 *reply++ = measure.peak;
173 *reply = measure.rms;
174 return OK;
175 }
176
177 } // namespace effect
178 } // namespace android
179