Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 1 | /* |
| 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 Rocard | 4a7484bd | 2018-02-23 19:11:06 -0800 | [diff] [blame^] | 20 | #include <common/all-versions/VersionUtils.h> |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 21 | #include <hwbinder/IPCThreadState.h> |
| 22 | #include <media/EffectsFactoryApi.h> |
| 23 | #include <utils/Log.h> |
| 24 | |
| 25 | #include "ConversionHelperHidl.h" |
| 26 | #include "EffectBufferHalHidl.h" |
| 27 | #include "EffectHalHidl.h" |
| 28 | #include "HidlUtils.h" |
| 29 | |
Kevin Rocard | 2390b5d | 2018-02-28 14:36:53 -0800 | [diff] [blame] | 30 | using ::android::hardware::audio::effect::V4_0::AudioBuffer; |
| 31 | using ::android::hardware::audio::effect::V4_0::EffectBufferAccess; |
| 32 | using ::android::hardware::audio::effect::V4_0::EffectConfigParameters; |
| 33 | using ::android::hardware::audio::effect::V4_0::MessageQueueFlagBits; |
| 34 | using ::android::hardware::audio::effect::V4_0::Result; |
| 35 | using ::android::hardware::audio::common::V4_0::HidlUtils; |
| 36 | using ::android::hardware::audio::common::V4_0::AudioChannelMask; |
| 37 | using ::android::hardware::audio::common::V4_0::AudioFormat; |
Kevin Rocard | 4a7484bd | 2018-02-23 19:11:06 -0800 | [diff] [blame^] | 38 | using ::android::hardware::audio::common::utils::mkEnumConverter; |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 39 | using ::android::hardware::hidl_vec; |
| 40 | using ::android::hardware::MQDescriptorSync; |
| 41 | using ::android::hardware::Return; |
| 42 | |
| 43 | namespace android { |
Kevin Rocard | 2390b5d | 2018-02-28 14:36:53 -0800 | [diff] [blame] | 44 | namespace V4_0 { |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 45 | |
| 46 | EffectHalHidl::EffectHalHidl(const sp<IEffect>& effect, uint64_t effectId) |
| 47 | : mEffect(effect), mEffectId(effectId), mBuffersChanged(true), mEfGroup(nullptr) { |
| 48 | } |
| 49 | |
| 50 | EffectHalHidl::~EffectHalHidl() { |
| 51 | if (mEffect != 0) { |
| 52 | close(); |
| 53 | mEffect.clear(); |
| 54 | hardware::IPCThreadState::self()->flushCommands(); |
| 55 | } |
| 56 | if (mEfGroup) { |
| 57 | EventFlag::deleteEventFlag(&mEfGroup); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // static |
| 62 | void EffectHalHidl::effectDescriptorToHal( |
| 63 | const EffectDescriptor& descriptor, effect_descriptor_t* halDescriptor) { |
| 64 | HidlUtils::uuidToHal(descriptor.type, &halDescriptor->type); |
| 65 | HidlUtils::uuidToHal(descriptor.uuid, &halDescriptor->uuid); |
| 66 | halDescriptor->flags = static_cast<uint32_t>(descriptor.flags); |
| 67 | halDescriptor->cpuLoad = descriptor.cpuLoad; |
| 68 | halDescriptor->memoryUsage = descriptor.memoryUsage; |
| 69 | memcpy(halDescriptor->name, descriptor.name.data(), descriptor.name.size()); |
| 70 | memcpy(halDescriptor->implementor, |
| 71 | descriptor.implementor.data(), descriptor.implementor.size()); |
| 72 | } |
| 73 | |
| 74 | // TODO(mnaganov): These buffer conversion functions should be shared with Effect wrapper |
| 75 | // via HidlUtils. Move them there when hardware/interfaces will get un-frozen again. |
| 76 | |
| 77 | // static |
| 78 | void EffectHalHidl::effectBufferConfigFromHal( |
| 79 | const buffer_config_t& halConfig, EffectBufferConfig* config) { |
| 80 | config->samplingRateHz = halConfig.samplingRate; |
Kevin Rocard | 4a7484bd | 2018-02-23 19:11:06 -0800 | [diff] [blame^] | 81 | config->channels = mkEnumConverter<AudioChannelMask>(halConfig.channels); |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 82 | config->format = AudioFormat(halConfig.format); |
| 83 | config->accessMode = EffectBufferAccess(halConfig.accessMode); |
Kevin Rocard | 4a7484bd | 2018-02-23 19:11:06 -0800 | [diff] [blame^] | 84 | config->mask = mkEnumConverter<EffectConfigParameters>(halConfig.mask); |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // static |
| 88 | void EffectHalHidl::effectBufferConfigToHal( |
| 89 | const EffectBufferConfig& config, buffer_config_t* halConfig) { |
| 90 | halConfig->buffer.frameCount = 0; |
| 91 | halConfig->buffer.raw = NULL; |
| 92 | halConfig->samplingRate = config.samplingRateHz; |
| 93 | halConfig->channels = static_cast<uint32_t>(config.channels); |
| 94 | halConfig->bufferProvider.cookie = NULL; |
| 95 | halConfig->bufferProvider.getBuffer = NULL; |
| 96 | halConfig->bufferProvider.releaseBuffer = NULL; |
| 97 | halConfig->format = static_cast<uint8_t>(config.format); |
| 98 | halConfig->accessMode = static_cast<uint8_t>(config.accessMode); |
| 99 | halConfig->mask = static_cast<uint8_t>(config.mask); |
| 100 | } |
| 101 | |
| 102 | // static |
| 103 | void EffectHalHidl::effectConfigFromHal(const effect_config_t& halConfig, EffectConfig* config) { |
| 104 | effectBufferConfigFromHal(halConfig.inputCfg, &config->inputCfg); |
| 105 | effectBufferConfigFromHal(halConfig.outputCfg, &config->outputCfg); |
| 106 | } |
| 107 | |
| 108 | // static |
| 109 | void EffectHalHidl::effectConfigToHal(const EffectConfig& config, effect_config_t* halConfig) { |
| 110 | effectBufferConfigToHal(config.inputCfg, &halConfig->inputCfg); |
| 111 | effectBufferConfigToHal(config.outputCfg, &halConfig->outputCfg); |
| 112 | } |
| 113 | |
| 114 | // static |
| 115 | status_t EffectHalHidl::analyzeResult(const Result& result) { |
| 116 | switch (result) { |
| 117 | case Result::OK: return OK; |
| 118 | case Result::INVALID_ARGUMENTS: return BAD_VALUE; |
| 119 | case Result::INVALID_STATE: return NOT_ENOUGH_DATA; |
| 120 | case Result::NOT_INITIALIZED: return NO_INIT; |
| 121 | case Result::NOT_SUPPORTED: return INVALID_OPERATION; |
| 122 | case Result::RESULT_TOO_BIG: return NO_MEMORY; |
| 123 | default: return NO_INIT; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | status_t EffectHalHidl::setInBuffer(const sp<EffectBufferHalInterface>& buffer) { |
| 128 | if (!mBuffersChanged) { |
| 129 | if (buffer.get() == nullptr || mInBuffer.get() == nullptr) { |
| 130 | mBuffersChanged = buffer.get() != mInBuffer.get(); |
| 131 | } else { |
| 132 | mBuffersChanged = buffer->audioBuffer() != mInBuffer->audioBuffer(); |
| 133 | } |
| 134 | } |
| 135 | mInBuffer = buffer; |
| 136 | return OK; |
| 137 | } |
| 138 | |
| 139 | status_t EffectHalHidl::setOutBuffer(const sp<EffectBufferHalInterface>& buffer) { |
| 140 | if (!mBuffersChanged) { |
| 141 | if (buffer.get() == nullptr || mOutBuffer.get() == nullptr) { |
| 142 | mBuffersChanged = buffer.get() != mOutBuffer.get(); |
| 143 | } else { |
| 144 | mBuffersChanged = buffer->audioBuffer() != mOutBuffer->audioBuffer(); |
| 145 | } |
| 146 | } |
| 147 | mOutBuffer = buffer; |
| 148 | return OK; |
| 149 | } |
| 150 | |
| 151 | status_t EffectHalHidl::process() { |
| 152 | return processImpl(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS)); |
| 153 | } |
| 154 | |
| 155 | status_t EffectHalHidl::processReverse() { |
| 156 | return processImpl(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_REVERSE)); |
| 157 | } |
| 158 | |
| 159 | status_t EffectHalHidl::prepareForProcessing() { |
| 160 | std::unique_ptr<StatusMQ> tempStatusMQ; |
| 161 | Result retval; |
| 162 | Return<void> ret = mEffect->prepareForProcessing( |
| 163 | [&](Result r, const MQDescriptorSync<Result>& statusMQ) { |
| 164 | retval = r; |
| 165 | if (retval == Result::OK) { |
| 166 | tempStatusMQ.reset(new StatusMQ(statusMQ)); |
| 167 | if (tempStatusMQ->isValid() && tempStatusMQ->getEventFlagWord()) { |
| 168 | EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup); |
| 169 | } |
| 170 | } |
| 171 | }); |
| 172 | if (!ret.isOk() || retval != Result::OK) { |
| 173 | return ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION; |
| 174 | } |
| 175 | if (!tempStatusMQ || !tempStatusMQ->isValid() || !mEfGroup) { |
| 176 | ALOGE_IF(!tempStatusMQ, "Failed to obtain status message queue for effects"); |
| 177 | ALOGE_IF(tempStatusMQ && !tempStatusMQ->isValid(), |
| 178 | "Status message queue for effects is invalid"); |
| 179 | ALOGE_IF(!mEfGroup, "Event flag creation for effects failed"); |
| 180 | return NO_INIT; |
| 181 | } |
| 182 | mStatusMQ = std::move(tempStatusMQ); |
| 183 | return OK; |
| 184 | } |
| 185 | |
| 186 | bool EffectHalHidl::needToResetBuffers() { |
| 187 | if (mBuffersChanged) return true; |
| 188 | bool inBufferFrameCountUpdated = mInBuffer->checkFrameCountChange(); |
| 189 | bool outBufferFrameCountUpdated = mOutBuffer->checkFrameCountChange(); |
| 190 | return inBufferFrameCountUpdated || outBufferFrameCountUpdated; |
| 191 | } |
| 192 | |
| 193 | status_t EffectHalHidl::processImpl(uint32_t mqFlag) { |
| 194 | if (mEffect == 0 || mInBuffer == 0 || mOutBuffer == 0) return NO_INIT; |
| 195 | status_t status; |
| 196 | if (!mStatusMQ && (status = prepareForProcessing()) != OK) { |
| 197 | return status; |
| 198 | } |
| 199 | if (needToResetBuffers() && (status = setProcessBuffers()) != OK) { |
| 200 | return status; |
| 201 | } |
| 202 | // The data is already in the buffers, just need to flush it and wake up the server side. |
| 203 | std::atomic_thread_fence(std::memory_order_release); |
| 204 | mEfGroup->wake(mqFlag); |
| 205 | uint32_t efState = 0; |
| 206 | retry: |
| 207 | status_t ret = mEfGroup->wait( |
| 208 | static_cast<uint32_t>(MessageQueueFlagBits::DONE_PROCESSING), &efState); |
| 209 | if (efState & static_cast<uint32_t>(MessageQueueFlagBits::DONE_PROCESSING)) { |
| 210 | Result retval = Result::NOT_INITIALIZED; |
| 211 | mStatusMQ->read(&retval); |
| 212 | if (retval == Result::OK || retval == Result::INVALID_STATE) { |
| 213 | // Sync back the changed contents of the buffer. |
| 214 | std::atomic_thread_fence(std::memory_order_acquire); |
| 215 | } |
| 216 | return analyzeResult(retval); |
| 217 | } |
| 218 | if (ret == -EAGAIN || ret == -EINTR) { |
| 219 | // Spurious wakeup. This normally retries no more than once. |
| 220 | goto retry; |
| 221 | } |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | status_t EffectHalHidl::setProcessBuffers() { |
| 226 | Return<Result> ret = mEffect->setProcessBuffers( |
| 227 | static_cast<EffectBufferHalHidl*>(mInBuffer.get())->hidlBuffer(), |
| 228 | static_cast<EffectBufferHalHidl*>(mOutBuffer.get())->hidlBuffer()); |
| 229 | if (ret.isOk() && ret == Result::OK) { |
| 230 | mBuffersChanged = false; |
| 231 | return OK; |
| 232 | } |
| 233 | return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION; |
| 234 | } |
| 235 | |
| 236 | status_t EffectHalHidl::command(uint32_t cmdCode, uint32_t cmdSize, void *pCmdData, |
| 237 | uint32_t *replySize, void *pReplyData) { |
| 238 | if (mEffect == 0) return NO_INIT; |
| 239 | |
| 240 | // Special cases. |
| 241 | if (cmdCode == EFFECT_CMD_SET_CONFIG || cmdCode == EFFECT_CMD_SET_CONFIG_REVERSE) { |
| 242 | return setConfigImpl(cmdCode, cmdSize, pCmdData, replySize, pReplyData); |
| 243 | } else if (cmdCode == EFFECT_CMD_GET_CONFIG || cmdCode == EFFECT_CMD_GET_CONFIG_REVERSE) { |
| 244 | return getConfigImpl(cmdCode, replySize, pReplyData); |
| 245 | } |
| 246 | |
| 247 | // Common case. |
| 248 | hidl_vec<uint8_t> hidlData; |
| 249 | if (pCmdData != nullptr && cmdSize > 0) { |
| 250 | hidlData.setToExternal(reinterpret_cast<uint8_t*>(pCmdData), cmdSize); |
| 251 | } |
| 252 | status_t status; |
| 253 | uint32_t replySizeStub = 0; |
| 254 | if (replySize == nullptr || pReplyData == nullptr) replySize = &replySizeStub; |
| 255 | Return<void> ret = mEffect->command(cmdCode, hidlData, *replySize, |
| 256 | [&](int32_t s, const hidl_vec<uint8_t>& result) { |
| 257 | status = s; |
| 258 | if (status == 0) { |
| 259 | if (*replySize > result.size()) *replySize = result.size(); |
| 260 | if (pReplyData != nullptr && *replySize > 0) { |
| 261 | memcpy(pReplyData, &result[0], *replySize); |
| 262 | } |
| 263 | } |
| 264 | }); |
| 265 | return ret.isOk() ? status : FAILED_TRANSACTION; |
| 266 | } |
| 267 | |
| 268 | status_t EffectHalHidl::getDescriptor(effect_descriptor_t *pDescriptor) { |
| 269 | if (mEffect == 0) return NO_INIT; |
| 270 | Result retval = Result::NOT_INITIALIZED; |
| 271 | Return<void> ret = mEffect->getDescriptor( |
| 272 | [&](Result r, const EffectDescriptor& result) { |
| 273 | retval = r; |
| 274 | if (retval == Result::OK) { |
| 275 | effectDescriptorToHal(result, pDescriptor); |
| 276 | } |
| 277 | }); |
| 278 | return ret.isOk() ? analyzeResult(retval) : FAILED_TRANSACTION; |
| 279 | } |
| 280 | |
| 281 | status_t EffectHalHidl::close() { |
| 282 | if (mEffect == 0) return NO_INIT; |
| 283 | Return<Result> ret = mEffect->close(); |
| 284 | return ret.isOk() ? analyzeResult(ret) : FAILED_TRANSACTION; |
| 285 | } |
| 286 | |
| 287 | status_t EffectHalHidl::getConfigImpl( |
| 288 | uint32_t cmdCode, uint32_t *replySize, void *pReplyData) { |
| 289 | if (replySize == NULL || *replySize != sizeof(effect_config_t) || pReplyData == NULL) { |
| 290 | return BAD_VALUE; |
| 291 | } |
| 292 | status_t result = FAILED_TRANSACTION; |
| 293 | Return<void> ret; |
| 294 | if (cmdCode == EFFECT_CMD_GET_CONFIG) { |
| 295 | ret = mEffect->getConfig([&] (Result r, const EffectConfig &hidlConfig) { |
| 296 | result = analyzeResult(r); |
| 297 | if (r == Result::OK) { |
| 298 | effectConfigToHal(hidlConfig, static_cast<effect_config_t*>(pReplyData)); |
| 299 | } |
| 300 | }); |
| 301 | } else { |
| 302 | ret = mEffect->getConfigReverse([&] (Result r, const EffectConfig &hidlConfig) { |
| 303 | result = analyzeResult(r); |
| 304 | if (r == Result::OK) { |
| 305 | effectConfigToHal(hidlConfig, static_cast<effect_config_t*>(pReplyData)); |
| 306 | } |
| 307 | }); |
| 308 | } |
| 309 | if (!ret.isOk()) { |
| 310 | result = FAILED_TRANSACTION; |
| 311 | } |
| 312 | return result; |
| 313 | } |
| 314 | |
| 315 | status_t EffectHalHidl::setConfigImpl( |
| 316 | uint32_t cmdCode, uint32_t cmdSize, void *pCmdData, uint32_t *replySize, void *pReplyData) { |
| 317 | if (pCmdData == NULL || cmdSize != sizeof(effect_config_t) || |
| 318 | replySize == NULL || *replySize != sizeof(int32_t) || pReplyData == NULL) { |
| 319 | return BAD_VALUE; |
| 320 | } |
| 321 | const effect_config_t *halConfig = static_cast<effect_config_t*>(pCmdData); |
| 322 | if (halConfig->inputCfg.bufferProvider.getBuffer != NULL || |
| 323 | halConfig->inputCfg.bufferProvider.releaseBuffer != NULL || |
| 324 | halConfig->outputCfg.bufferProvider.getBuffer != NULL || |
| 325 | halConfig->outputCfg.bufferProvider.releaseBuffer != NULL) { |
| 326 | ALOGE("Buffer provider callbacks are not supported"); |
| 327 | } |
| 328 | EffectConfig hidlConfig; |
| 329 | effectConfigFromHal(*halConfig, &hidlConfig); |
| 330 | Return<Result> ret = cmdCode == EFFECT_CMD_SET_CONFIG ? |
| 331 | mEffect->setConfig(hidlConfig, nullptr, nullptr) : |
| 332 | mEffect->setConfigReverse(hidlConfig, nullptr, nullptr); |
| 333 | status_t result = FAILED_TRANSACTION; |
| 334 | if (ret.isOk()) { |
| 335 | result = analyzeResult(ret); |
| 336 | *static_cast<int32_t*>(pReplyData) = result; |
| 337 | } |
| 338 | return result; |
| 339 | } |
| 340 | |
Kevin Rocard | 2390b5d | 2018-02-28 14:36:53 -0800 | [diff] [blame] | 341 | } // namespace V4_0 |
Kevin Rocard | d4de288 | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 342 | } // namespace android |