blob: 69184cf3cded02aa6ca4a7ebd0c752eb992cbd0a [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
36using ::aidl::android::aidl_utils::statusTFromBinderStatus;
Shunkai Yaof6a5b172023-02-14 06:26:06 +000037using ::aidl::android::getParameterSpecificField;
Shunkai Yao242521c2023-01-29 18:08:09 +000038using ::aidl::android::hardware::audio::effect::Parameter;
Shunkai Yaof6a5b172023-02-14 06:26:06 +000039using ::aidl::android::hardware::audio::effect::NoiseSuppression;
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: {
64 // TODO: implement vendor extension parameters
65 ALOGW("%s unknown param %s", __func__, param.toString().c_str());
66 return BAD_VALUE;
67 }
68 }
Shunkai Yao242521c2023-01-29 18:08:09 +000069 return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
70}
71
72status_t AidlConversionNoiseSuppression::getParameter(EffectParamWriter& param) {
Shunkai Yaof6a5b172023-02-14 06:26:06 +000073 uint32_t paramType = 0, value = 0;
Shunkai Yao242521c2023-01-29 18:08:09 +000074 if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
Shunkai Yaof6a5b172023-02-14 06:26:06 +000075 OK != param.readFromParameter(&paramType)) {
Shunkai Yao242521c2023-01-29 18:08:09 +000076 ALOGE("%s invalid param %s", __func__, param.toString().c_str());
77 param.setStatus(BAD_VALUE);
78 return BAD_VALUE;
79 }
Shunkai Yaof6a5b172023-02-14 06:26:06 +000080 Parameter aidlParam;
81 switch (paramType) {
82 case NS_PARAM_LEVEL: {
83 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(NoiseSuppression, noiseSuppressionTag,
84 NoiseSuppression::level);
85 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
86 NoiseSuppression::Level level = VALUE_OR_RETURN_STATUS(
87 GET_PARAMETER_SPECIFIC_FIELD(aidlParam, NoiseSuppression, noiseSuppression,
88 NoiseSuppression::level, NoiseSuppression::Level));
89 value = static_cast<uint32_t>(level);
90 break;
91 }
92 case NS_PARAM_TYPE: {
93 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(NoiseSuppression, noiseSuppressionTag,
94 NoiseSuppression::type);
95 RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
96 NoiseSuppression::Type nsType = VALUE_OR_RETURN_STATUS(
97 GET_PARAMETER_SPECIFIC_FIELD(aidlParam, NoiseSuppression, noiseSuppression,
98 NoiseSuppression::type, NoiseSuppression::Type));
99 value = static_cast<uint32_t>(nsType);
100 break;
101 }
102 default: {
103 // TODO: implement vendor extension parameters
104 ALOGW("%s unknown param %s", __func__, param.toString().c_str());
105 return BAD_VALUE;
106 }
107 }
Shunkai Yao242521c2023-01-29 18:08:09 +0000108 return param.writeToValue(&value);
109}
110
111} // namespace effect
112} // namespace android