blob: fdfe225e90aaa9c87252ce124aaeac948b8ffe54 [file] [log] [blame]
Kevin Rocardd4de2882018-02-28 14:33:38 -08001/*
2 * Copyright (C) 2016 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 "EffectHalHidl"
18//#define LOG_NDEBUG 0
19
Kevin Rocard4a7484bd2018-02-23 19:11:06 -080020#include <common/all-versions/VersionUtils.h>
Mikhail Naganov4d547672019-02-22 14:19:19 -080021#include <cutils/native_handle.h>
Kevin Rocardd4de2882018-02-28 14:33:38 -080022#include <hwbinder/IPCThreadState.h>
23#include <media/EffectsFactoryApi.h>
Andy Hung2924a292022-04-04 22:14:57 -070024#include <mediautils/TimeCheck.h>
Kevin Rocardd4de2882018-02-28 14:33:38 -080025#include <utils/Log.h>
26
Mikhail Naganov247b5f92021-01-15 19:16:12 +000027#include <util/EffectUtils.h>
28
Kevin Rocardd4de2882018-02-28 14:33:38 -080029#include "EffectBufferHalHidl.h"
30#include "EffectHalHidl.h"
Kevin Rocardd4de2882018-02-28 14:33:38 -080031
Kevin Rocard7a9f05a2018-11-28 16:52:25 -080032using ::android::hardware::audio::common::utils::EnumBitfield;
Mikhail Naganov247b5f92021-01-15 19:16:12 +000033using ::android::hardware::audio::effect::CPP_VERSION::implementation::EffectUtils;
Kevin Rocardd4de2882018-02-28 14:33:38 -080034using ::android::hardware::hidl_vec;
35using ::android::hardware::MQDescriptorSync;
36using ::android::hardware::Return;
37
38namespace android {
Mikhail Naganov595caa32018-12-13 11:08:28 -080039namespace effect {
Kevin Rocardd4de2882018-02-28 14:33:38 -080040
Mikhail Naganov9ccaa162018-12-12 10:27:29 -080041using namespace ::android::hardware::audio::common::CPP_VERSION;
Mikhail Naganov595caa32018-12-13 11:08:28 -080042using namespace ::android::hardware::audio::effect::CPP_VERSION;
Mikhail Naganov9ccaa162018-12-12 10:27:29 -080043
Andy Hung2924a292022-04-04 22:14:57 -070044#define TIME_CHECK() auto timeCheck = \
45 mediautils::makeTimeCheckStatsForClassMethod(getClassName(), __func__)
46
Kevin Rocardd4de2882018-02-28 14:33:38 -080047EffectHalHidl::EffectHalHidl(const sp<IEffect>& effect, uint64_t effectId)
Andy Hung2924a292022-04-04 22:14:57 -070048 : EffectConversionHelperHidl("EffectHalHidl"),
Mikhail Naganov288a3432022-03-25 00:29:56 +000049 mEffect(effect), mEffectId(effectId), mBuffersChanged(true), mEfGroup(nullptr) {
Mikhail Naganov247b5f92021-01-15 19:16:12 +000050 effect_descriptor_t halDescriptor{};
51 if (EffectHalHidl::getDescriptor(&halDescriptor) == NO_ERROR) {
52 mIsInput = (halDescriptor.flags & EFFECT_FLAG_TYPE_PRE_PROC) == EFFECT_FLAG_TYPE_PRE_PROC;
53 }
Kevin Rocardd4de2882018-02-28 14:33:38 -080054}
55
56EffectHalHidl::~EffectHalHidl() {
57 if (mEffect != 0) {
58 close();
59 mEffect.clear();
60 hardware::IPCThreadState::self()->flushCommands();
61 }
62 if (mEfGroup) {
63 EventFlag::deleteEventFlag(&mEfGroup);
64 }
65}
66
Kevin Rocardd4de2882018-02-28 14:33:38 -080067status_t EffectHalHidl::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
Andy Hung2924a292022-04-04 22:14:57 -070068 TIME_CHECK();
69
Kevin Rocardd4de2882018-02-28 14:33:38 -080070 if (!mBuffersChanged) {
71 if (buffer.get() == nullptr || mInBuffer.get() == nullptr) {
72 mBuffersChanged = buffer.get() != mInBuffer.get();
73 } else {
74 mBuffersChanged = buffer->audioBuffer() != mInBuffer->audioBuffer();
75 }
76 }
77 mInBuffer = buffer;
78 return OK;
79}
80
81status_t EffectHalHidl::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
Andy Hung2924a292022-04-04 22:14:57 -070082 TIME_CHECK();
83
Kevin Rocardd4de2882018-02-28 14:33:38 -080084 if (!mBuffersChanged) {
85 if (buffer.get() == nullptr || mOutBuffer.get() == nullptr) {
86 mBuffersChanged = buffer.get() != mOutBuffer.get();
87 } else {
88 mBuffersChanged = buffer->audioBuffer() != mOutBuffer->audioBuffer();
89 }
90 }
91 mOutBuffer = buffer;
92 return OK;
93}
94
95status_t EffectHalHidl::process() {
Andy Hung2924a292022-04-04 22:14:57 -070096 TIME_CHECK();
97
Kevin Rocardd4de2882018-02-28 14:33:38 -080098 return processImpl(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS));
99}
100
101status_t EffectHalHidl::processReverse() {
Andy Hung2924a292022-04-04 22:14:57 -0700102 TIME_CHECK();
103
Kevin Rocardd4de2882018-02-28 14:33:38 -0800104 return processImpl(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_REVERSE));
105}
106
107status_t EffectHalHidl::prepareForProcessing() {
108 std::unique_ptr<StatusMQ> tempStatusMQ;
109 Result retval;
110 Return<void> ret = mEffect->prepareForProcessing(
111 [&](Result r, const MQDescriptorSync<Result>& statusMQ) {
112 retval = r;
113 if (retval == Result::OK) {
114 tempStatusMQ.reset(new StatusMQ(statusMQ));
115 if (tempStatusMQ->isValid() && tempStatusMQ->getEventFlagWord()) {
116 EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup);
117 }
118 }
119 });
120 if (!ret.isOk() || retval != Result::OK) {
121 return ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION;
122 }
123 if (!tempStatusMQ || !tempStatusMQ->isValid() || !mEfGroup) {
124 ALOGE_IF(!tempStatusMQ, "Failed to obtain status message queue for effects");
125 ALOGE_IF(tempStatusMQ && !tempStatusMQ->isValid(),
126 "Status message queue for effects is invalid");
127 ALOGE_IF(!mEfGroup, "Event flag creation for effects failed");
128 return NO_INIT;
129 }
130 mStatusMQ = std::move(tempStatusMQ);
131 return OK;
132}
133
134bool EffectHalHidl::needToResetBuffers() {
135 if (mBuffersChanged) return true;
136 bool inBufferFrameCountUpdated = mInBuffer->checkFrameCountChange();
137 bool outBufferFrameCountUpdated = mOutBuffer->checkFrameCountChange();
138 return inBufferFrameCountUpdated || outBufferFrameCountUpdated;
139}
140
141status_t EffectHalHidl::processImpl(uint32_t mqFlag) {
142 if (mEffect == 0 || mInBuffer == 0 || mOutBuffer == 0) return NO_INIT;
143 status_t status;
144 if (!mStatusMQ && (status = prepareForProcessing()) != OK) {
145 return status;
146 }
147 if (needToResetBuffers() && (status = setProcessBuffers()) != OK) {
148 return status;
149 }
150 // The data is already in the buffers, just need to flush it and wake up the server side.
151 std::atomic_thread_fence(std::memory_order_release);
152 mEfGroup->wake(mqFlag);
153 uint32_t efState = 0;
154retry:
155 status_t ret = mEfGroup->wait(
156 static_cast<uint32_t>(MessageQueueFlagBits::DONE_PROCESSING), &efState);
157 if (efState & static_cast<uint32_t>(MessageQueueFlagBits::DONE_PROCESSING)) {
158 Result retval = Result::NOT_INITIALIZED;
159 mStatusMQ->read(&retval);
160 if (retval == Result::OK || retval == Result::INVALID_STATE) {
161 // Sync back the changed contents of the buffer.
162 std::atomic_thread_fence(std::memory_order_acquire);
163 }
164 return analyzeResult(retval);
165 }
166 if (ret == -EAGAIN || ret == -EINTR) {
167 // Spurious wakeup. This normally retries no more than once.
168 goto retry;
169 }
170 return ret;
171}
172
173status_t EffectHalHidl::setProcessBuffers() {
174 Return<Result> ret = mEffect->setProcessBuffers(
175 static_cast<EffectBufferHalHidl*>(mInBuffer.get())->hidlBuffer(),
176 static_cast<EffectBufferHalHidl*>(mOutBuffer.get())->hidlBuffer());
177 if (ret.isOk() && ret == Result::OK) {
178 mBuffersChanged = false;
179 return OK;
180 }
181 return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION;
182}
183
184status_t EffectHalHidl::command(uint32_t cmdCode, uint32_t cmdSize, void *pCmdData,
185 uint32_t *replySize, void *pReplyData) {
Andy Hung2924a292022-04-04 22:14:57 -0700186 TIME_CHECK();
187
Kevin Rocardd4de2882018-02-28 14:33:38 -0800188 if (mEffect == 0) return NO_INIT;
189
190 // Special cases.
191 if (cmdCode == EFFECT_CMD_SET_CONFIG || cmdCode == EFFECT_CMD_SET_CONFIG_REVERSE) {
192 return setConfigImpl(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
193 } else if (cmdCode == EFFECT_CMD_GET_CONFIG || cmdCode == EFFECT_CMD_GET_CONFIG_REVERSE) {
194 return getConfigImpl(cmdCode, replySize, pReplyData);
195 }
196
197 // Common case.
198 hidl_vec<uint8_t> hidlData;
199 if (pCmdData != nullptr && cmdSize > 0) {
200 hidlData.setToExternal(reinterpret_cast<uint8_t*>(pCmdData), cmdSize);
201 }
202 status_t status;
203 uint32_t replySizeStub = 0;
204 if (replySize == nullptr || pReplyData == nullptr) replySize = &replySizeStub;
205 Return<void> ret = mEffect->command(cmdCode, hidlData, *replySize,
206 [&](int32_t s, const hidl_vec<uint8_t>& result) {
207 status = s;
208 if (status == 0) {
209 if (*replySize > result.size()) *replySize = result.size();
210 if (pReplyData != nullptr && *replySize > 0) {
211 memcpy(pReplyData, &result[0], *replySize);
212 }
213 }
214 });
215 return ret.isOk() ? status : FAILED_TRANSACTION;
216}
217
218status_t EffectHalHidl::getDescriptor(effect_descriptor_t *pDescriptor) {
Andy Hung2924a292022-04-04 22:14:57 -0700219 TIME_CHECK();
220
Kevin Rocardd4de2882018-02-28 14:33:38 -0800221 if (mEffect == 0) return NO_INIT;
222 Result retval = Result::NOT_INITIALIZED;
223 Return<void> ret = mEffect->getDescriptor(
224 [&](Result r, const EffectDescriptor& result) {
225 retval = r;
226 if (retval == Result::OK) {
Mikhail Naganov247b5f92021-01-15 19:16:12 +0000227 EffectUtils::effectDescriptorToHal(result, pDescriptor);
Kevin Rocardd4de2882018-02-28 14:33:38 -0800228 }
229 });
230 return ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION;
231}
232
233status_t EffectHalHidl::close() {
Andy Hung2924a292022-04-04 22:14:57 -0700234 TIME_CHECK();
235
Kevin Rocardd4de2882018-02-28 14:33:38 -0800236 if (mEffect == 0) return NO_INIT;
237 Return<Result> ret = mEffect->close();
238 return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION;
239}
240
Mikhail Naganov4d547672019-02-22 14:19:19 -0800241status_t EffectHalHidl::dump(int fd) {
Andy Hung2924a292022-04-04 22:14:57 -0700242 TIME_CHECK();
243
Mikhail Naganov4d547672019-02-22 14:19:19 -0800244 if (mEffect == 0) return NO_INIT;
245 native_handle_t* hidlHandle = native_handle_create(1, 0);
246 hidlHandle->data[0] = fd;
247 Return<void> ret = mEffect->debug(hidlHandle, {} /* options */);
248 native_handle_delete(hidlHandle);
Andy Hunge72ff022021-08-16 10:16:15 -0700249
250 // TODO(b/111997867, b/177271958) Workaround - remove when fixed.
251 // A Binder transmitted fd may not close immediately due to a race condition b/111997867
252 // when the remote binder thread removes the last refcount to the fd blocks in the
253 // kernel for binder activity. We send a Binder ping() command to unblock the thread
254 // and complete the fd close / release.
255 //
256 // See DeviceHalHidl::dump(), EffectHalHidl::dump(), StreamHalHidl::dump(),
257 // EffectsFactoryHalHidl::dumpEffects().
258
259 (void)mEffect->ping(); // synchronous Binder call
260
Mikhail Naganov4d547672019-02-22 14:19:19 -0800261 return ret.isOk() ? OK : FAILED_TRANSACTION;
262}
263
Kevin Rocardd4de2882018-02-28 14:33:38 -0800264status_t EffectHalHidl::getConfigImpl(
265 uint32_t cmdCode, uint32_t *replySize, void *pReplyData) {
266 if (replySize == NULL || *replySize != sizeof(effect_config_t) || pReplyData == NULL) {
267 return BAD_VALUE;
268 }
269 status_t result = FAILED_TRANSACTION;
270 Return<void> ret;
271 if (cmdCode == EFFECT_CMD_GET_CONFIG) {
272 ret = mEffect->getConfig([&] (Result r, const EffectConfig &hidlConfig) {
273 result = analyzeResult(r);
274 if (r == Result::OK) {
Mikhail Naganov247b5f92021-01-15 19:16:12 +0000275 EffectUtils::effectConfigToHal(
276 hidlConfig, static_cast<effect_config_t*>(pReplyData));
Kevin Rocardd4de2882018-02-28 14:33:38 -0800277 }
278 });
279 } else {
280 ret = mEffect->getConfigReverse([&] (Result r, const EffectConfig &hidlConfig) {
281 result = analyzeResult(r);
282 if (r == Result::OK) {
Mikhail Naganov247b5f92021-01-15 19:16:12 +0000283 EffectUtils::effectConfigToHal(
284 hidlConfig, static_cast<effect_config_t*>(pReplyData));
Kevin Rocardd4de2882018-02-28 14:33:38 -0800285 }
286 });
287 }
288 if (!ret.isOk()) {
289 result = FAILED_TRANSACTION;
290 }
291 return result;
292}
293
294status_t EffectHalHidl::setConfigImpl(
295 uint32_t cmdCode, uint32_t cmdSize, void *pCmdData, uint32_t *replySize, void *pReplyData) {
296 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t) ||
297 replySize == NULL || *replySize != sizeof(int32_t) || pReplyData == NULL) {
298 return BAD_VALUE;
299 }
300 const effect_config_t *halConfig = static_cast<effect_config_t*>(pCmdData);
301 if (halConfig->inputCfg.bufferProvider.getBuffer != NULL ||
302 halConfig->inputCfg.bufferProvider.releaseBuffer != NULL ||
303 halConfig->outputCfg.bufferProvider.getBuffer != NULL ||
304 halConfig->outputCfg.bufferProvider.releaseBuffer != NULL) {
305 ALOGE("Buffer provider callbacks are not supported");
306 }
307 EffectConfig hidlConfig;
Mikhail Naganov247b5f92021-01-15 19:16:12 +0000308 EffectUtils::effectConfigFromHal(*halConfig, mIsInput, &hidlConfig);
Kevin Rocardd4de2882018-02-28 14:33:38 -0800309 Return<Result> ret = cmdCode == EFFECT_CMD_SET_CONFIG ?
310 mEffect->setConfig(hidlConfig, nullptr, nullptr) :
311 mEffect->setConfigReverse(hidlConfig, nullptr, nullptr);
312 status_t result = FAILED_TRANSACTION;
313 if (ret.isOk()) {
314 result = analyzeResult(ret);
315 *static_cast<int32_t*>(pReplyData) = result;
316 }
317 return result;
318}
319
Mikhail Naganov595caa32018-12-13 11:08:28 -0800320} // namespace effect
Kevin Rocardd4de2882018-02-28 14:33:38 -0800321} // namespace android