Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #define LOG_TAG "EffectHalAidl" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <media/AidlConversionCppNdk.h> |
| 21 | #include <media/AidlConversionNdk.h> |
| 22 | #include <media/audiohal/AudioHalUtils.h> |
| 23 | #include <media/EffectsFactoryApi.h> |
| 24 | #include <mediautils/TimeCheck.h> |
| 25 | #include <utils/Log.h> |
| 26 | |
| 27 | #include "EffectHalAidl.h" |
| 28 | |
| 29 | #include <system/audio.h> |
| 30 | |
| 31 | #include <aidl/android/hardware/audio/effect/IEffect.h> |
| 32 | |
| 33 | using ::aidl::android::hardware::audio::effect::CommandId; |
| 34 | using ::aidl::android::hardware::audio::effect::Descriptor; |
| 35 | using ::aidl::android::hardware::audio::effect::IEffect; |
| 36 | using ::aidl::android::hardware::audio::effect::State; |
| 37 | using ::aidl::android::hardware::audio::effect::Parameter; |
| 38 | |
| 39 | namespace android { |
| 40 | namespace effect { |
| 41 | |
| 42 | EffectHalAidl::EffectHalAidl(const std::shared_ptr<IEffect>& effect, uint64_t effectId, |
| 43 | int32_t sessionId, int32_t ioId) |
| 44 | : mEffectId(effectId), mSessionId(sessionId), mIoId(ioId), mEffect(effect) {} |
| 45 | |
| 46 | EffectHalAidl::~EffectHalAidl() {} |
| 47 | |
| 48 | status_t EffectHalAidl::setInBuffer(const sp<EffectBufferHalInterface>& buffer) { |
| 49 | if (buffer == nullptr) { |
| 50 | return BAD_VALUE; |
| 51 | } |
| 52 | ALOGW("%s not implemented yet", __func__); |
| 53 | return OK; |
| 54 | } |
| 55 | |
| 56 | status_t EffectHalAidl::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) { |
| 57 | if (buffer == nullptr) { |
| 58 | return BAD_VALUE; |
| 59 | } |
| 60 | ALOGW("%s not implemented yet", __func__); |
| 61 | return OK; |
| 62 | } |
| 63 | |
| 64 | status_t EffectHalAidl::process() { |
| 65 | ALOGW("%s not implemented yet", __func__); |
| 66 | // write to input FMQ here? |
| 67 | return OK; |
| 68 | } |
| 69 | |
| 70 | // TODO: no one using, maybe deprecate this interface |
| 71 | status_t EffectHalAidl::processReverse() { |
| 72 | ALOGW("%s not implemented yet", __func__); |
| 73 | return OK; |
| 74 | } |
| 75 | |
| 76 | status_t EffectHalAidl::handleSetConfig(uint32_t cmdCode, uint32_t cmdSize, void* pCmdData, |
| 77 | uint32_t* replySize, void* pReplyData) { |
| 78 | if (pCmdData == NULL || cmdSize != sizeof(effect_config_t) || replySize == NULL || |
| 79 | *replySize != sizeof(int32_t) || pReplyData == NULL) { |
| 80 | ALOGE("%s parameter error code %u", __func__, cmdCode); |
| 81 | return BAD_VALUE; |
| 82 | } |
| 83 | |
| 84 | *static_cast<int32_t*>(pReplyData) = FAILED_TRANSACTION; |
| 85 | memcpy(&mConfig, pCmdData, cmdSize); |
| 86 | |
| 87 | State state; |
| 88 | RETURN_IF_BINDER_FAIL(mEffect->getState(&state)); |
| 89 | // effect not open yet, save settings locally |
| 90 | if (state != State::INIT) { |
| 91 | effect_config_t* legacyConfig = static_cast<effect_config_t*>(pCmdData); |
| 92 | // already open, apply latest settings |
| 93 | Parameter aidlParam; |
| 94 | Parameter::Common aidlCommon; |
| 95 | aidlCommon.input.base = |
| 96 | VALUE_OR_RETURN_STATUS(::aidl::android::legacy2aidl_AudioConfigBase_buffer_config_t( |
| 97 | legacyConfig->inputCfg, true /* isInput */)); |
| 98 | aidlCommon.output.base = |
| 99 | VALUE_OR_RETURN_STATUS(::aidl::android::legacy2aidl_AudioConfigBase_buffer_config_t( |
| 100 | legacyConfig->outputCfg, false /* isInput */)); |
| 101 | aidlCommon.session = mSessionId; |
| 102 | aidlCommon.ioHandle = mIoId; |
| 103 | Parameter::Id id; |
| 104 | id.set<Parameter::Id::commonTag>(Parameter::common); |
| 105 | aidlParam.set<Parameter::common>(aidlCommon); |
| 106 | RETURN_IF_BINDER_FAIL(mEffect->setParameter(aidlParam)); |
| 107 | } |
| 108 | *(int*)pReplyData = 0; |
| 109 | *static_cast<int32_t*>(pReplyData) = OK; |
| 110 | return OK; |
| 111 | } |
| 112 | |
| 113 | status_t EffectHalAidl::handleGetConfig(uint32_t cmdCode, uint32_t cmdSize, void* pCmdData, |
| 114 | uint32_t* replySize, void* pReplyData) { |
| 115 | if (pCmdData == NULL || cmdSize == 0 || replySize == NULL || |
| 116 | *replySize != sizeof(effect_config_t) || pReplyData == NULL) { |
| 117 | ALOGE("%s parameter error with cmdCode %d", __func__, cmdCode); |
| 118 | return BAD_VALUE; |
| 119 | } |
| 120 | |
| 121 | *(effect_config_t*)pReplyData = mConfig; |
| 122 | return OK; |
| 123 | } |
| 124 | |
| 125 | status_t EffectHalAidl::handleSetParameter(uint32_t cmdCode, uint32_t cmdSize, void* pCmdData, |
| 126 | uint32_t* replySize, void* pReplyData) { |
| 127 | ALOGW("%s not implemented yet", __func__); |
| 128 | if (pCmdData == NULL || cmdSize == 0 || replySize == NULL || |
| 129 | *replySize != sizeof(effect_config_t) || pReplyData == NULL) { |
| 130 | ALOGE("%s parameter error with cmdCode %d", __func__, cmdCode); |
| 131 | return BAD_VALUE; |
| 132 | } |
| 133 | return OK; |
| 134 | } |
| 135 | |
| 136 | status_t EffectHalAidl::handleGetParameter(uint32_t cmdCode, uint32_t cmdSize, void* pCmdData, |
| 137 | uint32_t* replySize, void* pReplyData) { |
| 138 | ALOGW("%s not implemented yet", __func__); |
| 139 | if (pCmdData == NULL || cmdSize == 0 || replySize == NULL || |
| 140 | *replySize != sizeof(effect_config_t) || pReplyData == NULL) { |
| 141 | ALOGE("%s parameter error with cmdCode %d", __func__, cmdCode); |
| 142 | return BAD_VALUE; |
| 143 | } |
| 144 | return OK; |
| 145 | } |
| 146 | |
| 147 | status_t EffectHalAidl::command(uint32_t cmdCode, uint32_t cmdSize, void* pCmdData, |
| 148 | uint32_t* replySize, void* pReplyData) { |
| 149 | ALOGW("%s code %d not implemented yet", __func__, cmdCode); |
| 150 | ::ndk::ScopedAStatus status; |
| 151 | switch (cmdCode) { |
| 152 | case EFFECT_CMD_INIT: { |
| 153 | // open with default effect_config_t (convert to Parameter.Common) |
| 154 | IEffect::OpenEffectReturn ret; |
| 155 | Parameter::Common common; |
| 156 | RETURN_IF_BINDER_FAIL(mEffect->open(common, std::nullopt, &ret)); |
| 157 | return OK; |
| 158 | } |
| 159 | case EFFECT_CMD_SET_CONFIG: |
| 160 | return handleSetConfig(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 161 | case EFFECT_CMD_GET_CONFIG: |
| 162 | return handleGetConfig(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 163 | case EFFECT_CMD_RESET: |
| 164 | return mEffect->command(CommandId::RESET).getStatus(); |
| 165 | case EFFECT_CMD_ENABLE: |
| 166 | return mEffect->command(CommandId::START).getStatus(); |
| 167 | case EFFECT_CMD_DISABLE: |
| 168 | return mEffect->command(CommandId::STOP).getStatus(); |
| 169 | case EFFECT_CMD_SET_PARAM: |
| 170 | return handleSetParameter(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 171 | case EFFECT_CMD_SET_PARAM_DEFERRED: |
| 172 | case EFFECT_CMD_SET_PARAM_COMMIT: |
| 173 | // TODO |
| 174 | return OK; |
| 175 | case EFFECT_CMD_GET_PARAM: |
| 176 | return handleGetParameter(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 177 | case EFFECT_CMD_SET_DEVICE: |
| 178 | return OK; |
| 179 | case EFFECT_CMD_SET_VOLUME: |
| 180 | return OK; |
| 181 | case EFFECT_CMD_SET_AUDIO_MODE: |
| 182 | return OK; |
| 183 | case EFFECT_CMD_SET_CONFIG_REVERSE: |
| 184 | return OK; |
| 185 | case EFFECT_CMD_SET_INPUT_DEVICE: |
| 186 | return OK; |
| 187 | case EFFECT_CMD_GET_CONFIG_REVERSE: |
| 188 | return OK; |
| 189 | case EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS: |
| 190 | return OK; |
| 191 | case EFFECT_CMD_GET_FEATURE_CONFIG: |
| 192 | return OK; |
| 193 | case EFFECT_CMD_SET_FEATURE_CONFIG: |
| 194 | return OK; |
| 195 | case EFFECT_CMD_SET_AUDIO_SOURCE: |
| 196 | return OK; |
| 197 | case EFFECT_CMD_OFFLOAD: |
| 198 | return OK; |
| 199 | case EFFECT_CMD_DUMP: |
| 200 | return OK; |
| 201 | case EFFECT_CMD_FIRST_PROPRIETARY: |
| 202 | return OK; |
| 203 | default: |
| 204 | return INVALID_OPERATION; |
| 205 | } |
| 206 | return INVALID_OPERATION; |
| 207 | } |
| 208 | |
| 209 | status_t EffectHalAidl::getDescriptor(effect_descriptor_t* pDescriptor) { |
| 210 | ALOGW("%s %p", __func__, pDescriptor); |
| 211 | if (pDescriptor == nullptr) { |
| 212 | return BAD_VALUE; |
| 213 | } |
| 214 | Descriptor aidlDesc; |
| 215 | RETURN_IF_BINDER_FAIL(mEffect->getDescriptor(&aidlDesc)); |
| 216 | |
| 217 | *pDescriptor = VALUE_OR_RETURN_STATUS( |
| 218 | ::aidl::android::aidl2legacy_Descriptor_effect_descriptor(aidlDesc)); |
| 219 | return OK; |
| 220 | } |
| 221 | |
| 222 | status_t EffectHalAidl::close() { |
| 223 | auto ret = mEffect->close(); |
| 224 | ALOGI("%s %s", __func__, ret.getMessage()); |
| 225 | return ret.getStatus(); |
| 226 | } |
| 227 | |
| 228 | status_t EffectHalAidl::dump(int fd) { |
| 229 | ALOGW("%s not implemented yet, fd %d", __func__, fd); |
| 230 | return OK; |
| 231 | } |
| 232 | |
| 233 | } // namespace effect |
| 234 | } // namespace android |