• 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 "AidlConversionAec"
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_aec.h>
27 
28 #include <utils/Log.h>
29 
30 #include "AidlConversionAec.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::AcousticEchoCanceler;
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 AidlConversionAec::setParameter(EffectParamReader& param) {
45     uint32_t type, value = 0;
46     if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
47         OK != param.readFromParameter(&type) ||
48         OK != param.readFromValue(&value)) {
49         ALOGE("%s invalid param %s", __func__, param.toString().c_str());
50         return BAD_VALUE;
51     }
52 
53     Parameter aidlParam;
54     switch (type) {
55         case AEC_PARAM_ECHO_DELAY:
56             FALLTHROUGH_INTENDED;
57         case AEC_PARAM_PROPERTIES: {
58             aidlParam = VALUE_OR_RETURN_STATUS(
59                     aidl::android::legacy2aidl_uint32_echoDelay_Parameter_aec(value));
60             break;
61         }
62         case AEC_PARAM_MOBILE_MODE: {
63             aidlParam = VALUE_OR_RETURN_STATUS(
64                     aidl::android::legacy2aidl_uint32_mobileMode_Parameter_aec(value));
65             break;
66         }
67         default: {
68             // for vendor extension, copy data area to the DefaultExtension, parameter ignored
69             VendorExtension ext = VALUE_OR_RETURN_STATUS(
70                     aidl::android::legacy2aidl_EffectParameterReader_VendorExtension(param));
71             aidlParam = MAKE_SPECIFIC_PARAMETER(AcousticEchoCanceler, acousticEchoCanceler, vendor,
72                                                 ext);
73             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->setParameter(aidlParam)));
74             break;
75         }
76     }
77 
78     return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
79 }
80 
getParameter(EffectParamWriter & param)81 status_t AidlConversionAec::getParameter(EffectParamWriter& param) {
82     uint32_t type = 0;
83     if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
84         OK != param.readFromParameter(&type)) {
85         param.setStatus(BAD_VALUE);
86         ALOGE("%s invalid param %s", __func__, param.toString().c_str());
87         return BAD_VALUE;
88     }
89     Parameter aidlParam;
90     switch (type) {
91         case AEC_PARAM_ECHO_DELAY:
92             FALLTHROUGH_INTENDED;
93         case AEC_PARAM_PROPERTIES: {
94             int32_t delay = 0;
95             Parameter::Id id =
96                     MAKE_SPECIFIC_PARAMETER_ID(AcousticEchoCanceler, acousticEchoCancelerTag,
97                                                AcousticEchoCanceler::echoDelayUs);
98             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
99             delay = VALUE_OR_RETURN_STATUS(
100                     aidl::android::aidl2legacy_Parameter_aec_uint32_echoDelay(aidlParam));
101             return param.writeToValue(&delay);
102         }
103         case AEC_PARAM_MOBILE_MODE: {
104             int32_t mode = 0;
105             Parameter::Id id =
106                     MAKE_SPECIFIC_PARAMETER_ID(AcousticEchoCanceler, acousticEchoCancelerTag,
107                                                AcousticEchoCanceler::mobileMode);
108             RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
109             mode = VALUE_OR_RETURN_STATUS(
110                     aidl::android::aidl2legacy_Parameter_aec_uint32_mobileMode(aidlParam));
111             return param.writeToValue(&mode);
112         }
113         default: {
114             // use vendor extension implementation, the first 32bits (param type) won't pass to HAL
115             VENDOR_EXTENSION_GET_AND_RETURN(AcousticEchoCanceler, acousticEchoCanceler, param);
116         }
117     }
118 }
119 
120 } // namespace effect
121 } // namespace android
122