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 "AidlConversionNoiseSuppression"
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_ns.h>
27 
28 #include <utils/Log.h>
29 
30 #include "AidlConversionNoiseSuppression.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::NoiseSuppression;
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 AidlConversionNoiseSuppression::setParameter(EffectParamReader& param) {
45     uint32_t type = 0, value = 0;
46     if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint16_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 NS_PARAM_LEVEL: {
54             aidlParam = MAKE_SPECIFIC_PARAMETER(NoiseSuppression, noiseSuppression, level,
55                                                 static_cast<NoiseSuppression::Level>(value));
56             break;
57         }
58         case NS_PARAM_TYPE: {
59             aidlParam = MAKE_SPECIFIC_PARAMETER(NoiseSuppression, noiseSuppression, type,
60                                                 static_cast<NoiseSuppression::Type>(value));
61             break;
62         }
63         default: {
64             // for vendor extension, copy data area to the DefaultExtension, parameter ignored
65             VendorExtension ext = VALUE_OR_RETURN_STATUS(
66                     aidl::android::legacy2aidl_EffectParameterReader_VendorExtension(param));
67             aidlParam = MAKE_SPECIFIC_PARAMETER(NoiseSuppression, noiseSuppression, vendor, ext);
68             break;
69         }
70     }
71     return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
72 }
73 
getParameter(EffectParamWriter & param)74 status_t AidlConversionNoiseSuppression::getParameter(EffectParamWriter& param) {
75     uint32_t paramType = 0, value = 0;
76     if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
77         OK != param.readFromParameter(&paramType)) {
78         ALOGE("%s invalid param %s", __func__, param.toString().c_str());
79         param.setStatus(BAD_VALUE);
80         return BAD_VALUE;
81     }
82     Parameter aidlParam;
83     switch (paramType) {
84         case NS_PARAM_LEVEL: {
85             Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(NoiseSuppression, noiseSuppressionTag,
86                                                         NoiseSuppression::level);
87             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
88             NoiseSuppression::Level level = VALUE_OR_RETURN_STATUS(
89                     GET_PARAMETER_SPECIFIC_FIELD(aidlParam, NoiseSuppression, noiseSuppression,
90                                                  NoiseSuppression::level, NoiseSuppression::Level));
91             value = static_cast<uint32_t>(level);
92             break;
93         }
94         case NS_PARAM_TYPE: {
95             Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(NoiseSuppression, noiseSuppressionTag,
96                                                         NoiseSuppression::type);
97             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
98             NoiseSuppression::Type nsType = VALUE_OR_RETURN_STATUS(
99                     GET_PARAMETER_SPECIFIC_FIELD(aidlParam, NoiseSuppression, noiseSuppression,
100                                                  NoiseSuppression::type, NoiseSuppression::Type));
101             value = static_cast<uint32_t>(nsType);
102             break;
103         }
104         default: {
105             VENDOR_EXTENSION_GET_AND_RETURN(NoiseSuppression, noiseSuppression, param);
106         }
107     }
108     return param.writeToValue(&value);
109 }
110 
111 } // namespace effect
112 } // namespace android
113