blob: 0bc23f9af869c1649799fbe72cf45501548f0056 [file] [log] [blame]
Shunkai Yaodba8ba32023-01-27 17:02:21 +00001/*
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 <media/audiohal/AudioEffectUuid.h>
27#include <system/audio_effects/effect_aec.h>
28
29#include <utils/Log.h>
30
31#include "AidlConversionAec.h"
32
33namespace android {
34namespace effect {
35
Shunkai Yaoda4a6402023-03-03 19:38:17 +000036using ::aidl::android::getParameterSpecificField;
Shunkai Yaodba8ba32023-01-27 17:02:21 +000037using ::aidl::android::aidl_utils::statusTFromBinderStatus;
38using ::aidl::android::hardware::audio::effect::AcousticEchoCanceler;
39using ::aidl::android::hardware::audio::effect::Parameter;
Shunkai Yaoda4a6402023-03-03 19:38:17 +000040using ::aidl::android::hardware::audio::effect::VendorExtension;
Shunkai Yaodba8ba32023-01-27 17:02:21 +000041using ::android::status_t;
42using utils::EffectParamReader;
43using utils::EffectParamWriter;
44
45status_t AidlConversionAec::setParameter(EffectParamReader& param) {
46 uint32_t type, value = 0;
47 if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
48 OK != param.readFromParameter(&type) ||
49 OK != param.readFromValue(&value)) {
50 ALOGE("%s invalid param %s", __func__, param.toString().c_str());
51 return BAD_VALUE;
52 }
53
54 Parameter aidlParam;
55 switch (type) {
56 case AEC_PARAM_ECHO_DELAY:
57 FALLTHROUGH_INTENDED;
58 case AEC_PARAM_PROPERTIES: {
59 aidlParam = VALUE_OR_RETURN_STATUS(
60 aidl::android::legacy2aidl_uint32_echoDelay_Parameter_aec(value));
61 break;
62 }
63 case AEC_PARAM_MOBILE_MODE: {
64 aidlParam = VALUE_OR_RETURN_STATUS(
65 aidl::android::legacy2aidl_uint32_mobileMode_Parameter_aec(value));
66 break;
67 }
68 default: {
Shunkai Yaoda4a6402023-03-03 19:38:17 +000069 // for vendor extension, copy data area to the DefaultExtension, parameter ignored
70 VendorExtension ext = VALUE_OR_RETURN_STATUS(
71 aidl::android::legacy2aidl_EffectParameterReader_Data_VendorExtension(param));
72 aidlParam = MAKE_SPECIFIC_PARAMETER(AcousticEchoCanceler, acousticEchoCanceler, vendor,
73 ext);
74 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->setParameter(aidlParam)));
75 break;
Shunkai Yaodba8ba32023-01-27 17:02:21 +000076 }
77 }
78
79 return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
80}
81
82status_t AidlConversionAec::getParameter(EffectParamWriter& param) {
Shunkai Yaoda4a6402023-03-03 19:38:17 +000083 uint32_t type = 0;
Shunkai Yaodba8ba32023-01-27 17:02:21 +000084 if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
85 OK != param.readFromParameter(&type)) {
86 param.setStatus(BAD_VALUE);
87 ALOGE("%s invalid param %s", __func__, param.toString().c_str());
88 return BAD_VALUE;
89 }
90 Parameter aidlParam;
91 switch (type) {
92 case AEC_PARAM_ECHO_DELAY:
93 FALLTHROUGH_INTENDED;
94 case AEC_PARAM_PROPERTIES: {
Shunkai Yaoda4a6402023-03-03 19:38:17 +000095 int32_t delay = 0;
Shunkai Yaodba8ba32023-01-27 17:02:21 +000096 Parameter::Id id =
97 MAKE_SPECIFIC_PARAMETER_ID(AcousticEchoCanceler, acousticEchoCancelerTag,
98 AcousticEchoCanceler::echoDelayUs);
99 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
Shunkai Yaoda4a6402023-03-03 19:38:17 +0000100 delay = VALUE_OR_RETURN_STATUS(
Shunkai Yaodba8ba32023-01-27 17:02:21 +0000101 aidl::android::aidl2legacy_Parameter_aec_uint32_echoDelay(aidlParam));
Shunkai Yaoda4a6402023-03-03 19:38:17 +0000102 return param.writeToValue(&delay);
Shunkai Yaodba8ba32023-01-27 17:02:21 +0000103 }
104 case AEC_PARAM_MOBILE_MODE: {
Shunkai Yaoda4a6402023-03-03 19:38:17 +0000105 int32_t mode = 0;
Shunkai Yaodba8ba32023-01-27 17:02:21 +0000106 Parameter::Id id =
107 MAKE_SPECIFIC_PARAMETER_ID(AcousticEchoCanceler, acousticEchoCancelerTag,
108 AcousticEchoCanceler::mobileMode);
109 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
Shunkai Yaoda4a6402023-03-03 19:38:17 +0000110 mode = VALUE_OR_RETURN_STATUS(
Shunkai Yaodba8ba32023-01-27 17:02:21 +0000111 aidl::android::aidl2legacy_Parameter_aec_uint32_mobileMode(aidlParam));
Shunkai Yaoda4a6402023-03-03 19:38:17 +0000112 return param.writeToValue(&mode);
Shunkai Yaodba8ba32023-01-27 17:02:21 +0000113 }
Shunkai Yaoda4a6402023-03-03 19:38:17 +0000114 default: {
115 // use vendor extension implementation, the first 32bits (param type) won't pass to HAL
116 VENDOR_EXTENSION_GET_AND_RETURN(AcousticEchoCanceler, acousticEchoCanceler, param);
117 }
Shunkai Yaodba8ba32023-01-27 17:02:21 +0000118 }
Shunkai Yaodba8ba32023-01-27 17:02:21 +0000119}
120
121} // namespace effect
122} // namespace android