blob: a10d27173dbad7408bdce75316de4e59dd53629e [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 "AidlConversionEQ"
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_equalizer.h>
28
29#include <utils/Log.h>
30
31#include "AidlConversionEq.h"
32
33namespace android {
34namespace effect {
35
36using ::aidl::android::getParameterSpecificField;
37using ::aidl::android::aidl_utils::statusTFromBinderStatus;
38using ::aidl::android::hardware::audio::effect::Equalizer;
39using ::aidl::android::hardware::audio::effect::Parameter;
40using ::android::status_t;
41using utils::EffectParamReader;
42using utils::EffectParamWriter;
43
44status_t AidlConversionEq::setParameter(EffectParamReader& param) {
45 uint32_t type;
46 uint16_t 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 EQ_PARAM_CUR_PRESET: {
57 aidlParam = MAKE_SPECIFIC_PARAMETER(Equalizer, equalizer, preset, (int)value);
58 break;
59 }
60 case EQ_PARAM_BAND_LEVEL: {
61 int32_t band;
62 uint16_t level;
63 if (OK != param.readFromParameter(&band) || OK != param.readFromParameter(&level)) {
64 ALOGE("%s invalid bandLevel param %s", __func__, param.toString().c_str());
65 return BAD_VALUE;
66 }
67 std::vector<Equalizer::BandLevel> bandLevels = {{.index = band, .levelMb = level}};
68 aidlParam = MAKE_SPECIFIC_PARAMETER(Equalizer, equalizer, bandLevels, bandLevels);
69 break;
70 }
71 case EQ_PARAM_PROPERTIES: {
Shunkai Yao3ecc3eb2023-02-08 19:50:16 +000072 // TODO: handle properties setting
Shunkai Yao242521c2023-01-29 18:08:09 +000073 break;
74 }
75 default: {
76 // TODO: implement vendor extension parameters
77 ALOGW("%s unknown param %s", __func__, param.toString().c_str());
78 return BAD_VALUE;
79 }
80 }
81
82 return statusTFromBinderStatus(mEffect->setParameter(aidlParam));
83}
84
85aidl::ConversionResult<Parameter> AidlConversionEq::getAidlParameter(Equalizer::Tag tag) {
86 Parameter aidlParam;
87 Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(Equalizer, equalizerTag, tag);
88 RETURN_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam)));
89 return aidlParam;
90}
91
92status_t AidlConversionEq::getParameter(EffectParamWriter& param) {
93 uint32_t type = 0, value = 0;
94 if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) ||
95 OK != param.readFromParameter(&type)) {
96 param.setStatus(BAD_VALUE);
97 ALOGE("%s invalid param %s", __func__, param.toString().c_str());
98 return BAD_VALUE;
99 }
100 Parameter aidlParam;
101 switch (type) {
102 case EQ_PARAM_NUM_BANDS: {
103 aidlParam = VALUE_OR_RETURN_STATUS(getAidlParameter(Equalizer::bandLevels));
104 auto bandLevels = VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD(
105 aidlParam, Equalizer, equalizer, Equalizer::bandLevels,
106 std::vector<Equalizer::BandLevel>));
107 uint32_t num = bandLevels.size();
108 return param.writeToValue(&num);
109 }
110 default:
111 ALOGW("%s unknown param %s", __func__, param.toString().c_str());
112 return BAD_VALUE;
113 }
114 return param.writeToValue(&value);
115}
116
117} // namespace effect
118} // namespace android