Shunkai Yao | 242521c | 2023-01-29 18:08:09 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 33 | namespace android { |
| 34 | namespace effect { |
| 35 | |
| 36 | using ::aidl::android::getParameterSpecificField; |
| 37 | using ::aidl::android::aidl_utils::statusTFromBinderStatus; |
| 38 | using ::aidl::android::hardware::audio::effect::Equalizer; |
| 39 | using ::aidl::android::hardware::audio::effect::Parameter; |
| 40 | using ::android::status_t; |
| 41 | using utils::EffectParamReader; |
| 42 | using utils::EffectParamWriter; |
| 43 | |
| 44 | status_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: { |
| 72 | if (value >= 0) { |
| 73 | aidlParam = MAKE_SPECIFIC_PARAMETER(Equalizer, equalizer, preset, (int)value); |
| 74 | } else { |
| 75 | std::vector<Equalizer::BandLevel> bandLevels; |
| 76 | uint16_t numBands; |
| 77 | if (OK != param.readFromValue(&numBands)) { |
| 78 | ALOGE("%s invalid bandLevel param %s", __func__, param.toString().c_str()); |
| 79 | return BAD_VALUE; |
| 80 | } |
| 81 | for (int i = 0; i < numBands; i++) { |
| 82 | uint16_t level; |
| 83 | if (OK != param.readFromValue(&level)) { |
| 84 | ALOGE("%s invalid property param %s", __func__, param.toString().c_str()); |
| 85 | return BAD_VALUE; |
| 86 | } |
| 87 | bandLevels.push_back({.index = i, .levelMb = level}); |
| 88 | } |
| 89 | aidlParam = MAKE_SPECIFIC_PARAMETER(Equalizer, equalizer, bandLevels, bandLevels); |
| 90 | } |
| 91 | break; |
| 92 | } |
| 93 | default: { |
| 94 | // TODO: implement vendor extension parameters |
| 95 | ALOGW("%s unknown param %s", __func__, param.toString().c_str()); |
| 96 | return BAD_VALUE; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return statusTFromBinderStatus(mEffect->setParameter(aidlParam)); |
| 101 | } |
| 102 | |
| 103 | aidl::ConversionResult<Parameter> AidlConversionEq::getAidlParameter(Equalizer::Tag tag) { |
| 104 | Parameter aidlParam; |
| 105 | Parameter::Id id = MAKE_SPECIFIC_PARAMETER_ID(Equalizer, equalizerTag, tag); |
| 106 | RETURN_IF_ERROR(statusTFromBinderStatus(mEffect->getParameter(id, &aidlParam))); |
| 107 | return aidlParam; |
| 108 | } |
| 109 | |
| 110 | status_t AidlConversionEq::getParameter(EffectParamWriter& param) { |
| 111 | uint32_t type = 0, value = 0; |
| 112 | if (!param.validateParamValueSize(sizeof(uint32_t), sizeof(uint32_t)) || |
| 113 | OK != param.readFromParameter(&type)) { |
| 114 | param.setStatus(BAD_VALUE); |
| 115 | ALOGE("%s invalid param %s", __func__, param.toString().c_str()); |
| 116 | return BAD_VALUE; |
| 117 | } |
| 118 | Parameter aidlParam; |
| 119 | switch (type) { |
| 120 | case EQ_PARAM_NUM_BANDS: { |
| 121 | aidlParam = VALUE_OR_RETURN_STATUS(getAidlParameter(Equalizer::bandLevels)); |
| 122 | auto bandLevels = VALUE_OR_RETURN_STATUS(GET_PARAMETER_SPECIFIC_FIELD( |
| 123 | aidlParam, Equalizer, equalizer, Equalizer::bandLevels, |
| 124 | std::vector<Equalizer::BandLevel>)); |
| 125 | uint32_t num = bandLevels.size(); |
| 126 | return param.writeToValue(&num); |
| 127 | } |
| 128 | default: |
| 129 | ALOGW("%s unknown param %s", __func__, param.toString().c_str()); |
| 130 | return BAD_VALUE; |
| 131 | } |
| 132 | return param.writeToValue(&value); |
| 133 | } |
| 134 | |
| 135 | } // namespace effect |
| 136 | } // namespace android |