blob: bf75e4a3ccb8f6195bd8a9ee7df21eb4deffc100 [file] [log] [blame]
Shunkai Yao242521c2023-01-29 18:08:09 +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 "AidlConversionNoiseSuppression"
21//#define LOG_NDEBUG 0
22
23#include <error/expected_utils.h>
24#include <media/AidlConversionNdk.h>
25#include <media/AidlConversionEffect.h>
Shunkai Yao242521c2023-01-29 18:08:09 +000026#include <system/audio_effects/effect_ns.h>
27
28#include <utils/Log.h>
29
30#include "AidlConversionNoiseSuppression.h"
31
32namespace android {
33namespace effect {
34
Shunkai Yaof6a5b172023-02-14 06:26:06 +000035using ::aidl::android::getParameterSpecificField;
Shunkai Yaoda4a6402023-03-03 19:38:17 +000036using ::aidl::android::aidl_utils::statusTFromBinderStatus;
Shunkai Yaof6a5b172023-02-14 06:26:06 +000037using ::aidl::android::hardware::audio::effect::NoiseSuppression;
Shunkai Yaoda4a6402023-03-03 19:38:17 +000038using ::aidl::android::hardware::audio::effect::Parameter;
39using ::aidl::android::hardware::audio::effect::VendorExtension;
Shunkai Yao242521c2023-01-29 18:08:09 +000040using ::android::status_t;
41using utils::EffectParamReader;
42using utils::EffectParamWriter;
43
44status_t AidlConversionNoiseSuppression::setParameter(EffectParamReader& param) {
Shunkai Yaof6a5b172023-02-14 06:26:06 +000045 uint32_t type = 0, value = 0;
Shunkai Yao242521c2023-01-29 18:08:09 +000046 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;
Shunkai Yaof6a5b172023-02-14 06:26:06 +000052 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: {
Shunkai Yaoda4a6402023-03-03 19:38:17 +000064 // for vendor extension, copy data area to the DefaultExtension, parameter ignored
65 VendorExtension ext = VALUE_OR_RETURN_STATUS(
Shunkai Yaodbd06942023-06-29 18:07:09 +000066 aidl::android::legacy2aidl_EffectParameterReader_VendorExtension(param));
Shunkai Yaoda4a6402023-03-03 19:38:17 +000067 aidlParam = MAKE_SPECIFIC_PARAMETER(NoiseSuppression, noiseSuppression, vendor, ext);
68 break;
Shunkai Yaof6a5b172023-02-14 06:26:06 +000069 }
70 }
Shunkai Yao242521c2023-01-29 18:08:09 +000071 return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
72}
73
74status_t AidlConversionNoiseSuppression::getParameter(EffectParamWriter& param) {
Shunkai Yaof6a5b172023-02-14 06:26:06 +000075 uint32_t paramType = 0, value = 0;
Shunkai Yao242521c2023-01-29 18:08:09 +000076 if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
Shunkai Yaof6a5b172023-02-14 06:26:06 +000077 OK != param.readFromParameter(&paramType)) {
Shunkai Yao242521c2023-01-29 18:08:09 +000078 ALOGE("%s invalid param %s", __func__, param.toString().c_str());
79 param.setStatus(BAD_VALUE);
80 return BAD_VALUE;
81 }
Shunkai Yaof6a5b172023-02-14 06:26:06 +000082 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: {
Shunkai Yaoda4a6402023-03-03 19:38:17 +0000105 VENDOR_EXTENSION_GET_AND_RETURN(NoiseSuppression, noiseSuppression, param);
Shunkai Yaof6a5b172023-02-14 06:26:06 +0000106 }
107 }
Shunkai Yao242521c2023-01-29 18:08:09 +0000108 return param.writeToValue(&value);
109}
110
111} // namespace effect
112} // namespace android