blob: f46a9537bffbadc6bb9f6565a6fa28179d6d0f78 [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>
24#include <utils/Log.h>
25
Mikhail Naganov247b5f92021-01-15 19:16:12 +000026#include <util/EffectUtils.h>
27
Kevin Rocardd4de2882018-02-28 14:33:38 -080028#include "EffectBufferHalHidl.h"
29#include "EffectHalHidl.h"
Kevin Rocardd4de2882018-02-28 14:33:38 -080030
Kevin Rocard7a9f05a2018-11-28 16:52:25 -080031using ::android::hardware::audio::common::utils::EnumBitfield;
Mikhail Naganov247b5f92021-01-15 19:16:12 +000032using ::android::hardware::audio::effect::CPP_VERSION::implementation::EffectUtils;
Kevin Rocardd4de2882018-02-28 14:33:38 -080033using ::android::hardware::hidl_vec;
34using ::android::hardware::MQDescriptorSync;
35using ::android::hardware::Return;
36
37namespace android {
Mikhail Naganov595caa32018-12-13 11:08:28 -080038namespace effect {
Kevin Rocardd4de2882018-02-28 14:33:38 -080039
Mikhail Naganov9ccaa162018-12-12 10:27:29 -080040using namespace ::android::hardware::audio::common::CPP_VERSION;
Mikhail Naganov595caa32018-12-13 11:08:28 -080041using namespace ::android::hardware::audio::effect::CPP_VERSION;
Mikhail Naganov9ccaa162018-12-12 10:27:29 -080042
Kevin Rocardd4de2882018-02-28 14:33:38 -080043EffectHalHidl::EffectHalHidl(const sp<IEffect>& effect, uint64_t effectId)
Mikhail Naganov288a3432022-03-25 00:29:56 +000044 : EffectConversionHelperHidl("Effect"),
45 mEffect(effect), mEffectId(effectId), mBuffersChanged(true), mEfGroup(nullptr) {
Mikhail Naganov247b5f92021-01-15 19:16:12 +000046 effect_descriptor_t halDescriptor{};
47 if (EffectHalHidl::getDescriptor(&halDescriptor) == NO_ERROR) {
48 mIsInput = (halDescriptor.flags & EFFECT_FLAG_TYPE_PRE_PROC) == EFFECT_FLAG_TYPE_PRE_PROC;
49 }
Kevin Rocardd4de2882018-02-28 14:33:38 -080050}
51
52EffectHalHidl::~EffectHalHidl() {
53 if (mEffect != 0) {
54 close();
55 mEffect.clear();
56 hardware::IPCThreadState::self()->flushCommands();
57 }
58 if (mEfGroup) {
59 EventFlag::deleteEventFlag(&mEfGroup);
60 }
61}
62
Kevin Rocardd4de2882018-02-28 14:33:38 -080063status_t EffectHalHidl::setInBuffer(const sp<EffectBufferHalInterface>& buffer) {
64 if (!mBuffersChanged) {
65 if (buffer.get() == nullptr || mInBuffer.get() == nullptr) {
66 mBuffersChanged = buffer.get() != mInBuffer.get();
67 } else {
68 mBuffersChanged = buffer->audioBuffer() != mInBuffer->audioBuffer();
69 }
70 }
71 mInBuffer = buffer;
72 return OK;
73}
74
75status_t EffectHalHidl::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) {
76 if (!mBuffersChanged) {
77 if (buffer.get() == nullptr || mOutBuffer.get() == nullptr) {
78 mBuffersChanged = buffer.get() != mOutBuffer.get();
79 } else {
80 mBuffersChanged = buffer->audioBuffer() != mOutBuffer->audioBuffer();
81 }
82 }
83 mOutBuffer = buffer;
84 return OK;
85}
86
87status_t EffectHalHidl::process() {
88 return processImpl(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS));
89}
90
91status_t EffectHalHidl::processReverse() {
92 return processImpl(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_REVERSE));
93}
94
95status_t EffectHalHidl::prepareForProcessing() {
96 std::unique_ptr<StatusMQ> tempStatusMQ;
97 Result retval;
98 Return<void> ret = mEffect->prepareForProcessing(
99 [&](Result r, const MQDescriptorSync<Result>& statusMQ) {
100 retval = r;
101 if (retval == Result::OK) {
102 tempStatusMQ.reset(new StatusMQ(statusMQ));
103 if (tempStatusMQ->isValid() && tempStatusMQ->getEventFlagWord()) {
104 EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup);
105 }
106 }
107 });
108 if (!ret.isOk() || retval != Result::OK) {
109 return ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION;
110 }
111 if (!tempStatusMQ || !tempStatusMQ->isValid() || !mEfGroup) {
112 ALOGE_IF(!tempStatusMQ, "Failed to obtain status message queue for effects");
113 ALOGE_IF(tempStatusMQ && !tempStatusMQ->isValid(),
114 "Status message queue for effects is invalid");
115 ALOGE_IF(!mEfGroup, "Event flag creation for effects failed");
116 return NO_INIT;
117 }
118 mStatusMQ = std::move(tempStatusMQ);
119 return OK;
120}
121
122bool EffectHalHidl::needToResetBuffers() {
123 if (mBuffersChanged) return true;
124 bool inBufferFrameCountUpdated = mInBuffer->checkFrameCountChange();
125 bool outBufferFrameCountUpdated = mOutBuffer->checkFrameCountChange();
126 return inBufferFrameCountUpdated || outBufferFrameCountUpdated;
127}
128
129status_t EffectHalHidl::processImpl(uint32_t mqFlag) {
130 if (mEffect == 0 || mInBuffer == 0 || mOutBuffer == 0) return NO_INIT;
131 status_t status;
132 if (!mStatusMQ && (status = prepareForProcessing()) != OK) {
133 return status;
134 }
135 if (needToResetBuffers() && (status = setProcessBuffers()) != OK) {
136 return status;
137 }
138 // The data is already in the buffers, just need to flush it and wake up the server side.
139 std::atomic_thread_fence(std::memory_order_release);
140 mEfGroup->wake(mqFlag);
141 uint32_t efState = 0;
142retry:
143 status_t ret = mEfGroup->wait(
144 static_cast<uint32_t>(MessageQueueFlagBits::DONE_PROCESSING), &efState);
145 if (efState & static_cast<uint32_t>(MessageQueueFlagBits::DONE_PROCESSING)) {
146 Result retval = Result::NOT_INITIALIZED;
147 mStatusMQ->read(&retval);
148 if (retval == Result::OK || retval == Result::INVALID_STATE) {
149 // Sync back the changed contents of the buffer.
150 std::atomic_thread_fence(std::memory_order_acquire);
151 }
152 return analyzeResult(retval);
153 }
154 if (ret == -EAGAIN || ret == -EINTR) {
155 // Spurious wakeup. This normally retries no more than once.
156 goto retry;
157 }
158 return ret;
159}
160
161status_t EffectHalHidl::setProcessBuffers() {
162 Return<Result> ret = mEffect->setProcessBuffers(
163 static_cast<EffectBufferHalHidl*>(mInBuffer.get())->hidlBuffer(),
164 static_cast<EffectBufferHalHidl*>(mOutBuffer.get())->hidlBuffer());
165 if (ret.isOk() && ret == Result::OK) {
166 mBuffersChanged = false;
167 return OK;
168 }
169 return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION;
170}
171
172status_t EffectHalHidl::command(uint32_t cmdCode, uint32_t cmdSize, void *pCmdData,
173 uint32_t *replySize, void *pReplyData) {
174 if (mEffect == 0) return NO_INIT;
175
176 // Special cases.
177 if (cmdCode == EFFECT_CMD_SET_CONFIG || cmdCode == EFFECT_CMD_SET_CONFIG_REVERSE) {
178 return setConfigImpl(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
179 } else if (cmdCode == EFFECT_CMD_GET_CONFIG || cmdCode == EFFECT_CMD_GET_CONFIG_REVERSE) {
180 return getConfigImpl(cmdCode, replySize, pReplyData);
181 }
182
183 // Common case.
184 hidl_vec<uint8_t> hidlData;
185 if (pCmdData != nullptr && cmdSize > 0) {
186 hidlData.setToExternal(reinterpret_cast<uint8_t*>(pCmdData), cmdSize);
187 }
188 status_t status;
189 uint32_t replySizeStub = 0;
190 if (replySize == nullptr || pReplyData == nullptr) replySize = &replySizeStub;
191 Return<void> ret = mEffect->command(cmdCode, hidlData, *replySize,
192 [&](int32_t s, const hidl_vec<uint8_t>& result) {
193 status = s;
194 if (status == 0) {
195 if (*replySize > result.size()) *replySize = result.size();
196 if (pReplyData != nullptr && *replySize > 0) {
197 memcpy(pReplyData, &result[0], *replySize);
198 }
199 }
200 });
201 return ret.isOk() ? status : FAILED_TRANSACTION;
202}
203
204status_t EffectHalHidl::getDescriptor(effect_descriptor_t *pDescriptor) {
205 if (mEffect == 0) return NO_INIT;
206 Result retval = Result::NOT_INITIALIZED;
207 Return<void> ret = mEffect->getDescriptor(
208 [&](Result r, const EffectDescriptor& result) {
209 retval = r;
210 if (retval == Result::OK) {
Mikhail Naganov247b5f92021-01-15 19:16:12 +0000211 EffectUtils::effectDescriptorToHal(result, pDescriptor);
Kevin Rocardd4de2882018-02-28 14:33:38 -0800212 }
213 });
214 return ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION;
215}
216
217status_t EffectHalHidl::close() {
218 if (mEffect == 0) return NO_INIT;
219 Return<Result> ret = mEffect->close();
220 return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION;
221}
222
Mikhail Naganov4d547672019-02-22 14:19:19 -0800223status_t EffectHalHidl::dump(int fd) {
224 if (mEffect == 0) return NO_INIT;
225 native_handle_t* hidlHandle = native_handle_create(1, 0);
226 hidlHandle->data[0] = fd;
227 Return<void> ret = mEffect->debug(hidlHandle, {} /* options */);
228 native_handle_delete(hidlHandle);
Andy Hunge72ff022021-08-16 10:16:15 -0700229
230 // TODO(b/111997867, b/177271958) Workaround - remove when fixed.
231 // A Binder transmitted fd may not close immediately due to a race condition b/111997867
232 // when the remote binder thread removes the last refcount to the fd blocks in the
233 // kernel for binder activity. We send a Binder ping() command to unblock the thread
234 // and complete the fd close / release.
235 //
236 // See DeviceHalHidl::dump(), EffectHalHidl::dump(), StreamHalHidl::dump(),
237 // EffectsFactoryHalHidl::dumpEffects().
238
239 (void)mEffect->ping(); // synchronous Binder call
240
Mikhail Naganov4d547672019-02-22 14:19:19 -0800241 return ret.isOk() ? OK : FAILED_TRANSACTION;
242}
243
Kevin Rocardd4de2882018-02-28 14:33:38 -0800244status_t EffectHalHidl::getConfigImpl(
245 uint32_t cmdCode, uint32_t *replySize, void *pReplyData) {
246 if (replySize == NULL || *replySize != sizeof(effect_config_t) || pReplyData == NULL) {
247 return BAD_VALUE;
248 }
249 status_t result = FAILED_TRANSACTION;
250 Return<void> ret;
251 if (cmdCode == EFFECT_CMD_GET_CONFIG) {
252 ret = mEffect->getConfig([&] (Result r, const EffectConfig &hidlConfig) {
253 result = analyzeResult(r);
254 if (r == Result::OK) {
Mikhail Naganov247b5f92021-01-15 19:16:12 +0000255 EffectUtils::effectConfigToHal(
256 hidlConfig, static_cast<effect_config_t*>(pReplyData));
Kevin Rocardd4de2882018-02-28 14:33:38 -0800257 }
258 });
259 } else {
260 ret = mEffect->getConfigReverse([&] (Result r, const EffectConfig &hidlConfig) {
261 result = analyzeResult(r);
262 if (r == Result::OK) {
Mikhail Naganov247b5f92021-01-15 19:16:12 +0000263 EffectUtils::effectConfigToHal(
264 hidlConfig, static_cast<effect_config_t*>(pReplyData));
Kevin Rocardd4de2882018-02-28 14:33:38 -0800265 }
266 });
267 }
268 if (!ret.isOk()) {
269 result = FAILED_TRANSACTION;
270 }
271 return result;
272}
273
274status_t EffectHalHidl::setConfigImpl(
275 uint32_t cmdCode, uint32_t cmdSize, void *pCmdData, uint32_t *replySize, void *pReplyData) {
276 if (pCmdData == NULL || cmdSize != sizeof(effect_config_t) ||
277 replySize == NULL || *replySize != sizeof(int32_t) || pReplyData == NULL) {
278 return BAD_VALUE;
279 }
280 const effect_config_t *halConfig = static_cast<effect_config_t*>(pCmdData);
281 if (halConfig->inputCfg.bufferProvider.getBuffer != NULL ||
282 halConfig->inputCfg.bufferProvider.releaseBuffer != NULL ||
283 halConfig->outputCfg.bufferProvider.getBuffer != NULL ||
284 halConfig->outputCfg.bufferProvider.releaseBuffer != NULL) {
285 ALOGE("Buffer provider callbacks are not supported");
286 }
287 EffectConfig hidlConfig;
Mikhail Naganov247b5f92021-01-15 19:16:12 +0000288 EffectUtils::effectConfigFromHal(*halConfig, mIsInput, &hidlConfig);
Kevin Rocardd4de2882018-02-28 14:33:38 -0800289 Return<Result> ret = cmdCode == EFFECT_CMD_SET_CONFIG ?
290 mEffect->setConfig(hidlConfig, nullptr, nullptr) :
291 mEffect->setConfigReverse(hidlConfig, nullptr, nullptr);
292 status_t result = FAILED_TRANSACTION;
293 if (ret.isOk()) {
294 result = analyzeResult(ret);
295 *static_cast<int32_t*>(pReplyData) = result;
296 }
297 return result;
298}
299
Mikhail Naganov595caa32018-12-13 11:08:28 -0800300} // namespace effect
Kevin Rocardd4de2882018-02-28 14:33:38 -0800301} // namespace android