blob: 3d75e480f2697041f8f87932ffe7b2f7ec5cf793 [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>
26#include <media/audiohal/AudioEffectUuid.h>
27#include <system/audio_effects/effect_ns.h>
28
29#include <utils/Log.h>
30
31#include "AidlConversionNoiseSuppression.h"
32
33namespace android {
34namespace effect {
35
Shunkai Yaof6a5b172023-02-14 06:26:06 +000036using ::aidl::android::getParameterSpecificField;
Shunkai Yaoda4a6402023-03-03 19:38:17 +000037using ::aidl::android::aidl_utils::statusTFromBinderStatus;
Shunkai Yaof6a5b172023-02-14 06:26:06 +000038using ::aidl::android::hardware::audio::effect::NoiseSuppression;
Shunkai Yaoda4a6402023-03-03 19:38:17 +000039using ::aidl::android::hardware::audio::effect::Parameter;
40using ::aidl::android::hardware::audio::effect::VendorExtension;
Shunkai Yao242521c2023-01-29 18:08:09 +000041using ::android::status_t;
42using utils::EffectParamReader;
43using utils::EffectParamWriter;
44
45status_t AidlConversionNoiseSuppression::setParameter(EffectParamReader& param) {
Shunkai Yaof6a5b172023-02-14 06:26:06 +000046 uint32_t type = 0, value = 0;
Shunkai Yao242521c2023-01-29 18:08:09 +000047 if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint16_t)) ||
48 OK != param.readFromParameter(&type) || OK != param.readFromValue(&value)) {
49 ALOGE("%s invalid param %s", __func__, param.toString().c_str());
50 return BAD_VALUE;
51 }
52 Parameter aidlParam;
Shunkai Yaof6a5b172023-02-14 06:26:06 +000053 switch (type) {
54 case NS_PARAM_LEVEL: {
55 aidlParam = MAKE_SPECIFIC_PARAMETER(NoiseSuppression, noiseSuppression, level,
56 static_cast<NoiseSuppression::Level>(value));
57 break;
58 }
59 case NS_PARAM_TYPE: {
60 aidlParam = MAKE_SPECIFIC_PARAMETER(NoiseSuppression, noiseSuppression, type,
61 static_cast<NoiseSuppression::Type>(value));
62 break;
63 }
64 default: {
Shunkai Yaoda4a6402023-03-03 19:38:17 +000065 // for vendor extension, copy data area to the DefaultExtension, parameter ignored
66 VendorExtension ext = VALUE_OR_RETURN_STATUS(
67 aidl::android::legacy2aidl_EffectParameterReader_Data_VendorExtension(param));
68 aidlParam = MAKE_SPECIFIC_PARAMETER(NoiseSuppression, noiseSuppression, vendor, ext);
69 break;
Shunkai Yaof6a5b172023-02-14 06:26:06 +000070 }
71 }
Shunkai Yao242521c2023-01-29 18:08:09 +000072 return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
73}
74
75status_t AidlConversionNoiseSuppression::getParameter(EffectParamWriter& param) {
Shunkai Yaof6a5b172023-02-14 06:26:06 +000076 uint32_t paramType = 0, value = 0;
Shunkai Yao242521c2023-01-29 18:08:09 +000077 if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
Shunkai Yaof6a5b172023-02-14 06:26:06 +000078 OK != param.readFromParameter(&paramType)) {
Shunkai Yao242521c2023-01-29 18:08:09 +000079 ALOGE("%s invalid param %s", __func__, param.toString().c_str());
80 param.setStatus(BAD_VALUE);
81 return BAD_VALUE;
82 }
Shunkai Yaof6a5b172023-02-14 06:26:06 +000083 Parameter aidlParam;
84 switch (paramType) {
85 case NS_PARAM_LEVEL: {
86 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(NoiseSuppression, noiseSuppressionTag,
87 NoiseSuppression::level);
88 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
89 NoiseSuppression::Level level = VALUE_OR_RETURN_STATUS(
90 GET_PARAMETER_SPECIFIC_FIELD(aidlParam, NoiseSuppression, noiseSuppression,
91 NoiseSuppression::level, NoiseSuppression::Level));
92 value = static_cast<uint32_t>(level);
93 break;
94 }
95 case NS_PARAM_TYPE: {
96 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(NoiseSuppression, noiseSuppressionTag,
97 NoiseSuppression::type);
98 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
99 NoiseSuppression::Type nsType = VALUE_OR_RETURN_STATUS(
100 GET_PARAMETER_SPECIFIC_FIELD(aidlParam, NoiseSuppression, noiseSuppression,
101 NoiseSuppression::type, NoiseSuppression::Type));
102 value = static_cast<uint32_t>(nsType);
103 break;
104 }
105 default: {
Shunkai Yaoda4a6402023-03-03 19:38:17 +0000106 VENDOR_EXTENSION_GET_AND_RETURN(NoiseSuppression, noiseSuppression, param);
Shunkai Yaof6a5b172023-02-14 06:26:06 +0000107 }
108 }
Shunkai Yao242521c2023-01-29 18:08:09 +0000109 return param.writeToValue(&value);
110}
111
112} // namespace effect
113} // namespace android