• 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 "AidlConversionAgc2"
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_agc2.h>
27 
28 #include <utils/Log.h>
29 
30 #include "AidlConversionAgc2.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::AutomaticGainControlV2;
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 
setParameter(EffectParamReader & param)44 status_t AidlConversionAgc2::setParameter(EffectParamReader& param) {
45     uint32_t type = 0, value = 0;
46     if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
47         OK != param.readFromParameter(&type) || OK != param.readFromValue(&value)) {
48         ALOGE("%s invalid param %s", __func__, param.toString().c_str());
49         return BAD_VALUE;
50     }
51     Parameter aidlParam;
52     switch (type) {
53         case AGC2_PARAM_FIXED_DIGITAL_GAIN: {
54             aidlParam = VALUE_OR_RETURN_STATUS(
55                     aidl::android::legacy2aidl_uint32_fixedDigitalGain_Parameter_agc(value));
56             break;
57         }
58         case AGC2_PARAM_ADAPT_DIGI_LEVEL_ESTIMATOR: {
59             aidlParam = VALUE_OR_RETURN_STATUS(
60                     aidl::android::legacy2aidl_uint32_levelEstimator_Parameter_agc(value));
61             break;
62         }
63         case AGC2_PARAM_ADAPT_DIGI_EXTRA_SATURATION_MARGIN: {
64             aidlParam = VALUE_OR_RETURN_STATUS(
65                     aidl::android::legacy2aidl_uint32_saturationMargin_Parameter_agc(value));
66             break;
67         }
68         default: {
69             // for vendor extension, copy data area to the DefaultExtension, parameter ignored
70             VendorExtension ext = VALUE_OR_RETURN_STATUS(
71                     aidl::android::legacy2aidl_EffectParameterReader_VendorExtension(param));
72             aidlParam = MAKE_SPECIFIC_PARAMETER(AutomaticGainControlV2, automaticGainControlV2,
73                                                 vendor, ext);
74             break;
75         }
76     }
77 
78     return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
79 }
80 
getParameter(EffectParamWriter & param)81 status_t AidlConversionAgc2::getParameter(EffectParamWriter& param) {
82     uint32_t type = 0, value = 0;
83     if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
84         OK != param.readFromParameter(&type)) {
85         ALOGE("%s invalid param %s", __func__, param.toString().c_str());
86         return BAD_VALUE;
87     }
88     Parameter aidlParam;
89     switch (type) {
90         case AGC2_PARAM_FIXED_DIGITAL_GAIN: {
91             Parameter::Id id =
92                     MAKE_SPECIFIC_PARAMETER_ID(AutomaticGainControlV2, automaticGainControlV2Tag,
93                                                AutomaticGainControlV2::fixedDigitalGainMb);
94             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
95             value = VALUE_OR_RETURN_STATUS(
96                     aidl::android::aidl2legacy_Parameter_agc_uint32_fixedDigitalGain(aidlParam));
97             break;
98         }
99         case AGC2_PARAM_ADAPT_DIGI_LEVEL_ESTIMATOR: {
100             Parameter::Id id =
101                     MAKE_SPECIFIC_PARAMETER_ID(AutomaticGainControlV2, automaticGainControlV2Tag,
102                                                AutomaticGainControlV2::levelEstimator);
103             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
104             value = VALUE_OR_RETURN_STATUS(
105                     aidl::android::aidl2legacy_Parameter_agc_uint32_levelEstimator(aidlParam));
106             break;
107         }
108         case AGC2_PARAM_ADAPT_DIGI_EXTRA_SATURATION_MARGIN: {
109             Parameter::Id id =
110                     MAKE_SPECIFIC_PARAMETER_ID(AutomaticGainControlV2, automaticGainControlV2Tag,
111                                                AutomaticGainControlV2::saturationMarginMb);
112             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
113             value = VALUE_OR_RETURN_STATUS(
114                     aidl::android::aidl2legacy_Parameter_agc_uint32_saturationMargin(aidlParam));
115             break;
116         }
117         default: {
118             VENDOR_EXTENSION_GET_AND_RETURN(AutomaticGainControlV2, automaticGainControlV2, param);
119         }
120     }
121 
122     return param.writeToValue(&value);
123 }
124 
125 } // namespace effect
126 } // namespace android
127