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 "AidlConversionLoudnessEnhancer"
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_loudnessenhancer.h>
27 
28 #include <utils/Log.h>
29 
30 #include "AidlConversionLoudnessEnhancer.h"
31 
32 namespace android {
33 namespace effect {
34 
35 using ::aidl::android::aidl_utils::statusTFromBinderStatus;
36 using ::aidl::android::getParameterSpecificField;
37 using ::aidl::android::hardware::audio::effect::LoudnessEnhancer;
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 AidlConversionLoudnessEnhancer::setParameter(EffectParamReader& param) {
45     uint32_t type = 0;
46     int32_t gain = 0;
47     if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint16_t)) ||
48         OK != param.readFromParameter(&type) || OK != param.readFromValue(&gain)) {
49         ALOGE("%s invalid param %s", __func__, param.toString().c_str());
50         return BAD_VALUE;
51     }
52     Parameter aidlParam;
53     switch (type) {
54         case LOUDNESS_ENHANCER_PARAM_TARGET_GAIN_MB: {
55             aidlParam = MAKE_SPECIFIC_PARAMETER(LoudnessEnhancer, loudnessEnhancer, gainMb, gain);
56             break;
57         }
58         default: {
59             // for vendor extension, copy data area to the DefaultExtension, parameter ignored
60             VendorExtension ext = VALUE_OR_RETURN_STATUS(
61                     aidl::android::legacy2aidl_EffectParameterReader_VendorExtension(param));
62             aidlParam = MAKE_SPECIFIC_PARAMETER(LoudnessEnhancer, loudnessEnhancer, vendor, ext);
63             break;
64         }
65     }
66     return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
67 }
68 
getParameter(EffectParamWriter & param)69 status_t AidlConversionLoudnessEnhancer::getParameter(EffectParamWriter& param) {
70     uint32_t type = 0;
71     if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
72         OK != param.readFromParameter(&type)) {
73         ALOGE("%s invalid param %s", __func__, param.toString().c_str());
74         param.setStatus(BAD_VALUE);
75         return BAD_VALUE;
76     }
77     switch (type) {
78         case LOUDNESS_ENHANCER_PARAM_TARGET_GAIN_MB: {
79             Parameter aidlParam;
80             Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(LoudnessEnhancer, loudnessEnhancerTag,
81                                                         LoudnessEnhancer::gainMb);
82             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
83             int32_t gain = VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD(
84                     aidlParam, LoudnessEnhancer, loudnessEnhancer, LoudnessEnhancer::gainMb,
85                     std::decay_t<decltype(gain)>));
86             return param.writeToValue(&gain);
87         }
88         default: {
89             VENDOR_EXTENSION_GET_AND_RETURN(LoudnessEnhancer, loudnessEnhancer, param);
90         }
91     }
92 }
93 
94 } // namespace effect
95 } // namespace android
96