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