• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cstdint>
18 #include <cstring>
19 #include <optional>
20 #define LOG_TAG "AidlConversionAgc1"
21 //#define LOG_NDEBUG 0
22 
23 #include <error/expected_utils.h>
24 #include <media/AidlConversionNdk.h>
25 #include <media/AidlConversionEffect.h>
26 #include <system/audio_effects/effect_agc.h>
27 
28 #include <utils/Log.h>
29 
30 #include "AidlConversionAgc1.h"
31 
32 namespace android {
33 namespace effect {
34 
35 using ::aidl::android::getParameterSpecificField;
36 using ::aidl::android::aidl_utils::statusTFromBinderStatus;
37 using ::aidl::android::hardware::audio::effect::AutomaticGainControlV1;
38 using ::aidl::android::hardware::audio::effect::Parameter;
39 using ::aidl::android::hardware::audio::effect::VendorExtension;
40 using ::android::status_t;
41 using utils::EffectParamReader;
42 using utils::EffectParamWriter;
43 
setParameterLevel(EffectParamReader & param)44 status_t AidlConversionAgc1::setParameterLevel(EffectParamReader& param) {
45     int16_t level;
46     RETURN_STATUS_IF_ERROR(param.readFromValue(&level));
47     Parameter aidlParam = MAKE_SPECIFIC_PARAMETER(AutomaticGainControlV1, automaticGainControlV1,
48                                                   targetPeakLevelDbFs, level);
49     return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
50 }
51 
setParameterGain(EffectParamReader & param)52 status_t AidlConversionAgc1::setParameterGain(EffectParamReader& param) {
53     int16_t gain;
54     RETURN_STATUS_IF_ERROR(param.readFromValue(&gain));
55     Parameter aidlParam = MAKE_SPECIFIC_PARAMETER(AutomaticGainControlV1, automaticGainControlV1,
56                                                   maxCompressionGainDb, gain);
57     return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
58 }
59 
setParameterLimiterEnable(EffectParamReader & param)60 status_t AidlConversionAgc1::setParameterLimiterEnable(EffectParamReader& param) {
61     bool enable;
62     RETURN_STATUS_IF_ERROR(param.readFromValue(&enable));
63     Parameter aidlParam = MAKE_SPECIFIC_PARAMETER(AutomaticGainControlV1, automaticGainControlV1,
64                                                   enableLimiter, enable);
65     return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
66 }
67 
setParameter(EffectParamReader & param)68 status_t AidlConversionAgc1::setParameter(EffectParamReader& param) {
69     uint32_t type = 0;
70     if (OK != param.readFromParameter(&type)) {
71         ALOGE("%s invalid param %s", __func__, param.toString().c_str());
72         return BAD_VALUE;
73     }
74     switch (type) {
75         case AGC_PARAM_TARGET_LEVEL: {
76             return setParameterLevel(param);
77         }
78         case AGC_PARAM_COMP_GAIN: {
79             return setParameterGain(param);
80         }
81         case AGC_PARAM_LIMITER_ENA: {
82             return setParameterLimiterEnable(param);
83         }
84         case AGC_PARAM_PROPERTIES: {
85             RETURN_STATUS_IF_ERROR(setParameterLevel(param));
86             RETURN_STATUS_IF_ERROR(setParameterGain(param));
87             RETURN_STATUS_IF_ERROR(setParameterLimiterEnable(param));
88             return OK;
89         }
90         default: {
91             // for vendor extension, copy data area to the DefaultExtension, parameter ignored
92             VendorExtension ext = VALUE_OR_RETURN_STATUS(
93                     aidl::android::legacy2aidl_EffectParameterReader_VendorExtension(param));
94             Parameter aidlParam = MAKE_SPECIFIC_PARAMETER(AutomaticGainControlV1,
95                                                           automaticGainControlV1, vendor, ext);
96             return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
97         }
98     }
99 }
100 
getParameterLevel(EffectParamWriter & param)101 status_t AidlConversionAgc1::getParameterLevel(EffectParamWriter& param) {
102     Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(AutomaticGainControlV1, automaticGainControlV1Tag,
103                                                   AutomaticGainControlV1::targetPeakLevelDbFs);
104     Parameter aidlParam;
105     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
106     int32_t level = VALUE_OR_RETURN_STATUS(
107             GET_PARAMETER_SPECIFIC_FIELD(aidlParam, AutomaticGainControlV1, automaticGainControlV1,
108                                          AutomaticGainControlV1::targetPeakLevelDbFs, int32_t));
109     return param.writeToValue(&level);
110 }
111 
getParameterGain(EffectParamWriter & param)112 status_t AidlConversionAgc1::getParameterGain(EffectParamWriter& param) {
113     Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(AutomaticGainControlV1, automaticGainControlV1Tag,
114                                                   AutomaticGainControlV1::maxCompressionGainDb);
115     Parameter aidlParam;
116     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
117     int32_t gain = VALUE_OR_RETURN_STATUS(
118             GET_PARAMETER_SPECIFIC_FIELD(aidlParam, AutomaticGainControlV1, automaticGainControlV1,
119                                          AutomaticGainControlV1::maxCompressionGainDb, int32_t));
120     return param.writeToValue(&gain);
121 }
122 
getParameterLimiterEnable(EffectParamWriter & param)123 status_t AidlConversionAgc1::getParameterLimiterEnable(EffectParamWriter& param) {
124     Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(AutomaticGainControlV1, automaticGainControlV1Tag,
125                                                   AutomaticGainControlV1::enableLimiter);
126     Parameter aidlParam;
127     RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
128     bool enable = VALUE_OR_RETURN_STATUS(
129             GET_PARAMETER_SPECIFIC_FIELD(aidlParam, AutomaticGainControlV1, automaticGainControlV1,
130                                          AutomaticGainControlV1::enableLimiter, bool));
131     return param.writeToValue(&enable);
132 }
133 
getParameter(EffectParamWriter & param)134 status_t AidlConversionAgc1::getParameter(EffectParamWriter& param) {
135     uint32_t type = 0;
136     if (OK != param.readFromParameter(&type)) {
137         ALOGE("%s invalid param %s", __func__, param.toString().c_str());
138         return BAD_VALUE;
139     }
140     switch (type) {
141         case AGC_PARAM_TARGET_LEVEL: {
142             return getParameterLevel(param);
143         }
144         case AGC_PARAM_COMP_GAIN: {
145             return getParameterGain(param);
146         }
147         case AGC_PARAM_LIMITER_ENA: {
148             return getParameterLimiterEnable(param);
149         }
150         case AGC_PARAM_PROPERTIES: {
151             RETURN_STATUS_IF_ERROR(getParameterLevel(param));
152             RETURN_STATUS_IF_ERROR(getParameterGain(param));
153             RETURN_STATUS_IF_ERROR(getParameterLimiterEnable(param));
154             return OK;
155         }
156         default: {
157             VENDOR_EXTENSION_GET_AND_RETURN(AutomaticGainControlV1, automaticGainControlV1, param);
158         }
159     }
160 }
161 
162 } // namespace effect
163 } // namespace android
164