Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [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 | |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 17 | #include <memory.h> |
| 18 | |
| 19 | #define LOG_TAG "EffectHAL" |
Mikhail Naganov | b0abafb | 2017-01-31 17:24:48 -0800 | [diff] [blame] | 20 | #define ATRACE_TAG ATRACE_TAG_AUDIO |
| 21 | |
Yifan Hong | f9d3034 | 2016-11-30 13:45:34 -0800 | [diff] [blame] | 22 | #include <android/log.h> |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 23 | #include <media/EffectsFactoryApi.h> |
Mikhail Naganov | b0abafb | 2017-01-31 17:24:48 -0800 | [diff] [blame] | 24 | #include <utils/Trace.h> |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 25 | |
| 26 | #include "Conversions.h" |
| 27 | #include "Effect.h" |
Mikhail Naganov | 1054829 | 2016-10-31 10:39:47 -0700 | [diff] [blame] | 28 | #include "EffectMap.h" |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace hardware { |
| 32 | namespace audio { |
| 33 | namespace effect { |
| 34 | namespace V2_0 { |
| 35 | namespace implementation { |
| 36 | |
| 37 | using ::android::hardware::audio::common::V2_0::AudioChannelMask; |
| 38 | using ::android::hardware::audio::common::V2_0::AudioFormat; |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 39 | using ::android::hardware::audio::effect::V2_0::MessageQueueFlagBits; |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | class ProcessThread : public Thread { |
| 44 | public: |
| 45 | // ProcessThread's lifespan never exceeds Effect's lifespan. |
| 46 | ProcessThread(std::atomic<bool>* stop, |
| 47 | effect_handle_t effect, |
| 48 | std::atomic<audio_buffer_t*>* inBuffer, |
| 49 | std::atomic<audio_buffer_t*>* outBuffer, |
| 50 | Effect::StatusMQ* statusMQ, |
| 51 | EventFlag* efGroup) |
| 52 | : Thread(false /*canCallJava*/), |
| 53 | mStop(stop), |
| 54 | mEffect(effect), |
| 55 | mHasProcessReverse((*mEffect)->process_reverse != NULL), |
| 56 | mInBuffer(inBuffer), |
| 57 | mOutBuffer(outBuffer), |
| 58 | mStatusMQ(statusMQ), |
| 59 | mEfGroup(efGroup) { |
| 60 | } |
| 61 | virtual ~ProcessThread() {} |
| 62 | |
| 63 | private: |
| 64 | std::atomic<bool>* mStop; |
| 65 | effect_handle_t mEffect; |
| 66 | bool mHasProcessReverse; |
| 67 | std::atomic<audio_buffer_t*>* mInBuffer; |
| 68 | std::atomic<audio_buffer_t*>* mOutBuffer; |
| 69 | Effect::StatusMQ* mStatusMQ; |
| 70 | EventFlag* mEfGroup; |
| 71 | |
| 72 | bool threadLoop() override; |
| 73 | }; |
| 74 | |
| 75 | bool ProcessThread::threadLoop() { |
| 76 | // This implementation doesn't return control back to the Thread until it decides to stop, |
| 77 | // as the Thread uses mutexes, and this can lead to priority inversion. |
| 78 | while(!std::atomic_load_explicit(mStop, std::memory_order_acquire)) { |
| 79 | uint32_t efState = 0; |
| 80 | mEfGroup->wait( |
| 81 | static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_ALL), |
| 82 | &efState, |
| 83 | NS_PER_SEC); |
Mikhail Naganov | b0abafb | 2017-01-31 17:24:48 -0800 | [diff] [blame] | 84 | if (!(efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_ALL)) |
| 85 | || (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT))) { |
| 86 | continue; // Nothing to do or time to quit. |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 87 | } |
| 88 | Result retval = Result::OK; |
| 89 | if (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS_REVERSE) |
| 90 | && !mHasProcessReverse) { |
| 91 | retval = Result::NOT_SUPPORTED; |
| 92 | } |
| 93 | |
| 94 | if (retval == Result::OK) { |
| 95 | // affects both buffer pointers and their contents. |
| 96 | std::atomic_thread_fence(std::memory_order_acquire); |
| 97 | int32_t processResult; |
| 98 | audio_buffer_t* inBuffer = |
| 99 | std::atomic_load_explicit(mInBuffer, std::memory_order_relaxed); |
| 100 | audio_buffer_t* outBuffer = |
| 101 | std::atomic_load_explicit(mOutBuffer, std::memory_order_relaxed); |
| 102 | if (inBuffer != nullptr && outBuffer != nullptr) { |
| 103 | if (efState & static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_PROCESS)) { |
| 104 | processResult = (*mEffect)->process(mEffect, inBuffer, outBuffer); |
| 105 | } else { |
| 106 | processResult = (*mEffect)->process_reverse(mEffect, inBuffer, outBuffer); |
| 107 | } |
| 108 | std::atomic_thread_fence(std::memory_order_release); |
| 109 | } else { |
| 110 | ALOGE("processing buffers were not set before calling 'process'"); |
| 111 | processResult = -ENODEV; |
| 112 | } |
| 113 | switch(processResult) { |
| 114 | case 0: retval = Result::OK; break; |
| 115 | case -ENODATA: retval = Result::INVALID_STATE; break; |
| 116 | case -EINVAL: retval = Result::INVALID_ARGUMENTS; break; |
| 117 | default: retval = Result::NOT_INITIALIZED; |
| 118 | } |
| 119 | } |
| 120 | if (!mStatusMQ->write(&retval)) { |
| 121 | ALOGW("status message queue write failed"); |
| 122 | } |
| 123 | mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::DONE_PROCESSING)); |
| 124 | } |
| 125 | |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | } // namespace |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 130 | |
| 131 | // static |
| 132 | const char *Effect::sContextResultOfCommand = "returned status"; |
| 133 | const char *Effect::sContextCallToCommand = "error"; |
| 134 | const char *Effect::sContextCallFunction = sContextCallToCommand; |
| 135 | |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 136 | Effect::Effect(effect_handle_t handle) |
| 137 | : mIsClosed(false), mHandle(handle), mEfGroup(nullptr), mStopProcessThread(false) { |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | Effect::~Effect() { |
Mikhail Naganov | b0abafb | 2017-01-31 17:24:48 -0800 | [diff] [blame] | 141 | ATRACE_CALL(); |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 142 | close(); |
Mikhail Naganov | b0abafb | 2017-01-31 17:24:48 -0800 | [diff] [blame] | 143 | if (mProcessThread.get()) { |
| 144 | ATRACE_NAME("mProcessThread->join"); |
| 145 | status_t status = mProcessThread->join(); |
| 146 | ALOGE_IF(status, "processing thread exit error: %s", strerror(-status)); |
| 147 | } |
| 148 | if (mEfGroup) { |
| 149 | status_t status = EventFlag::deleteEventFlag(&mEfGroup); |
| 150 | ALOGE_IF(status, "processing MQ event flag deletion error: %s", strerror(-status)); |
| 151 | } |
| 152 | mInBuffer.clear(); |
| 153 | mOutBuffer.clear(); |
| 154 | int status = EffectRelease(mHandle); |
| 155 | ALOGW_IF(status, "Error releasing effect %p: %s", mHandle, strerror(-status)); |
| 156 | EffectMap::getInstance().remove(mHandle); |
| 157 | mHandle = 0; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // static |
| 161 | template<typename T> size_t Effect::alignedSizeIn(size_t s) { |
| 162 | return (s + sizeof(T) - 1) / sizeof(T); |
| 163 | } |
| 164 | |
| 165 | // static |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 166 | template<typename T> std::unique_ptr<uint8_t[]> Effect::hidlVecToHal( |
| 167 | const hidl_vec<T>& vec, uint32_t* halDataSize) { |
| 168 | // Due to bugs in HAL, they may attempt to write into the provided |
| 169 | // input buffer. The original binder buffer is r/o, thus it is needed |
| 170 | // to create a r/w version. |
| 171 | *halDataSize = vec.size() * sizeof(T); |
| 172 | std::unique_ptr<uint8_t[]> halData(new uint8_t[*halDataSize]); |
| 173 | memcpy(&halData[0], &vec[0], *halDataSize); |
| 174 | return halData; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // static |
| 178 | void Effect::effectAuxChannelsConfigFromHal( |
| 179 | const channel_config_t& halConfig, EffectAuxChannelsConfig* config) { |
| 180 | config->mainChannels = AudioChannelMask(halConfig.main_channels); |
| 181 | config->auxChannels = AudioChannelMask(halConfig.aux_channels); |
| 182 | } |
| 183 | |
| 184 | // static |
| 185 | void Effect::effectAuxChannelsConfigToHal( |
| 186 | const EffectAuxChannelsConfig& config, channel_config_t* halConfig) { |
| 187 | halConfig->main_channels = static_cast<audio_channel_mask_t>(config.mainChannels); |
| 188 | halConfig->aux_channels = static_cast<audio_channel_mask_t>(config.auxChannels); |
| 189 | } |
| 190 | |
| 191 | // static |
| 192 | void Effect::effectBufferConfigFromHal( |
| 193 | const buffer_config_t& halConfig, EffectBufferConfig* config) { |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 194 | config->samplingRateHz = halConfig.samplingRate; |
| 195 | config->channels = AudioChannelMask(halConfig.channels); |
| 196 | config->format = AudioFormat(halConfig.format); |
| 197 | config->accessMode = EffectBufferAccess(halConfig.accessMode); |
| 198 | config->mask = EffectConfigParameters(halConfig.mask); |
| 199 | } |
| 200 | |
| 201 | // static |
| 202 | void Effect::effectBufferConfigToHal(const EffectBufferConfig& config, buffer_config_t* halConfig) { |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 203 | // Note: setting the buffers directly is considered obsolete. They need to be set |
| 204 | // using 'setProcessBuffers'. |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 205 | halConfig->buffer.frameCount = 0; |
| 206 | halConfig->buffer.raw = NULL; |
| 207 | halConfig->samplingRate = config.samplingRateHz; |
| 208 | halConfig->channels = static_cast<uint32_t>(config.channels); |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 209 | // TODO(mnaganov): The framework code currently does not use BP, implement later. |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 210 | halConfig->bufferProvider.cookie = NULL; |
| 211 | halConfig->bufferProvider.getBuffer = NULL; |
| 212 | halConfig->bufferProvider.releaseBuffer = NULL; |
| 213 | halConfig->format = static_cast<uint8_t>(config.format); |
| 214 | halConfig->accessMode = static_cast<uint8_t>(config.accessMode); |
| 215 | halConfig->mask = static_cast<uint8_t>(config.mask); |
| 216 | } |
| 217 | |
| 218 | // static |
| 219 | void Effect::effectConfigFromHal(const effect_config_t& halConfig, EffectConfig* config) { |
| 220 | effectBufferConfigFromHal(halConfig.inputCfg, &config->inputCfg); |
| 221 | effectBufferConfigFromHal(halConfig.outputCfg, &config->outputCfg); |
| 222 | } |
| 223 | |
| 224 | // static |
| 225 | void Effect::effectConfigToHal(const EffectConfig& config, effect_config_t* halConfig) { |
| 226 | effectBufferConfigToHal(config.inputCfg, &halConfig->inputCfg); |
| 227 | effectBufferConfigToHal(config.outputCfg, &halConfig->outputCfg); |
| 228 | } |
| 229 | |
| 230 | // static |
| 231 | void Effect::effectOffloadParamToHal( |
| 232 | const EffectOffloadParameter& offload, effect_offload_param_t* halOffload) { |
| 233 | halOffload->isOffload = offload.isOffload; |
| 234 | halOffload->ioHandle = offload.ioHandle; |
| 235 | } |
| 236 | |
| 237 | // static |
| 238 | std::vector<uint8_t> Effect::parameterToHal( |
| 239 | uint32_t paramSize, |
| 240 | const void* paramData, |
| 241 | uint32_t valueSize, |
| 242 | const void** valueData) { |
| 243 | size_t valueOffsetFromData = alignedSizeIn<uint32_t>(paramSize) * sizeof(uint32_t); |
| 244 | size_t halParamBufferSize = sizeof(effect_param_t) + valueOffsetFromData + valueSize; |
| 245 | std::vector<uint8_t> halParamBuffer(halParamBufferSize, 0); |
| 246 | effect_param_t *halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]); |
| 247 | halParam->psize = paramSize; |
| 248 | halParam->vsize = valueSize; |
| 249 | memcpy(halParam->data, paramData, paramSize); |
| 250 | if (valueData) { |
| 251 | if (*valueData) { |
| 252 | // Value data is provided. |
| 253 | memcpy(halParam->data + valueOffsetFromData, *valueData, valueSize); |
| 254 | } else { |
| 255 | // The caller needs the pointer to the value data location. |
| 256 | *valueData = halParam->data + valueOffsetFromData; |
| 257 | } |
| 258 | } |
| 259 | return halParamBuffer; |
| 260 | } |
| 261 | |
| 262 | Result Effect::analyzeCommandStatus(const char* commandName, const char* context, status_t status) { |
| 263 | return analyzeStatus("command", commandName, context, status); |
| 264 | } |
| 265 | |
| 266 | Result Effect::analyzeStatus( |
| 267 | const char* funcName, |
| 268 | const char* subFuncName, |
| 269 | const char* contextDescription, |
| 270 | status_t status) { |
| 271 | if (status != OK) { |
| 272 | ALOGW("Effect %p %s %s %s: %s", |
| 273 | mHandle, funcName, subFuncName, contextDescription, strerror(-status)); |
| 274 | } |
| 275 | switch (status) { |
| 276 | case OK: return Result::OK; |
| 277 | case -EINVAL: return Result::INVALID_ARGUMENTS; |
| 278 | case -ENODATA: return Result::INVALID_STATE; |
| 279 | case -ENODEV: return Result::NOT_INITIALIZED; |
| 280 | case -ENOMEM: return Result::RESULT_TOO_BIG; |
| 281 | case -ENOSYS: return Result::NOT_SUPPORTED; |
| 282 | default: return Result::INVALID_STATE; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void Effect::getConfigImpl(int commandCode, const char* commandName, GetConfigCallback cb) { |
| 287 | uint32_t halResultSize = sizeof(effect_config_t); |
| 288 | effect_config_t halConfig; |
| 289 | status_t status = (*mHandle)->command( |
| 290 | mHandle, commandCode, 0, NULL, &halResultSize, &halConfig); |
| 291 | EffectConfig config; |
| 292 | if (status == OK) { |
| 293 | effectConfigFromHal(halConfig, &config); |
| 294 | } |
| 295 | cb(analyzeCommandStatus(commandName, sContextCallToCommand, status), config); |
| 296 | } |
| 297 | |
| 298 | Result Effect::getCurrentConfigImpl( |
| 299 | uint32_t featureId, uint32_t configSize, GetCurrentConfigSuccessCallback onSuccess) { |
| 300 | uint32_t halCmd = featureId; |
| 301 | uint32_t halResult[alignedSizeIn<uint32_t>(sizeof(uint32_t) + configSize)]; |
| 302 | memset(halResult, 0, sizeof(halResult)); |
| 303 | uint32_t halResultSize = 0; |
| 304 | return sendCommandReturningStatusAndData( |
| 305 | EFFECT_CMD_GET_FEATURE_CONFIG, "GET_FEATURE_CONFIG", |
| 306 | sizeof(uint32_t), &halCmd, |
| 307 | &halResultSize, halResult, |
| 308 | sizeof(uint32_t), |
| 309 | [&]{ onSuccess(&halResult[1]); }); |
| 310 | } |
| 311 | |
| 312 | Result Effect::getParameterImpl( |
| 313 | uint32_t paramSize, |
| 314 | const void* paramData, |
| 315 | uint32_t valueSize, |
| 316 | GetParameterSuccessCallback onSuccess) { |
| 317 | // As it is unknown what method HAL uses for copying the provided parameter data, |
| 318 | // it is safer to make sure that input and output buffers do not overlap. |
| 319 | std::vector<uint8_t> halCmdBuffer = |
| 320 | parameterToHal(paramSize, paramData, valueSize, nullptr); |
| 321 | const void *valueData = nullptr; |
| 322 | std::vector<uint8_t> halParamBuffer = |
| 323 | parameterToHal(paramSize, paramData, valueSize, &valueData); |
| 324 | uint32_t halParamBufferSize = halParamBuffer.size(); |
| 325 | |
| 326 | return sendCommandReturningStatusAndData( |
| 327 | EFFECT_CMD_GET_PARAM, "GET_PARAM", |
| 328 | halCmdBuffer.size(), &halCmdBuffer[0], |
| 329 | &halParamBufferSize, &halParamBuffer[0], |
| 330 | sizeof(effect_param_t), |
| 331 | [&]{ |
| 332 | effect_param_t *halParam = reinterpret_cast<effect_param_t*>(&halParamBuffer[0]); |
| 333 | onSuccess(halParam->vsize, valueData); |
| 334 | }); |
| 335 | } |
| 336 | |
| 337 | Result Effect::getSupportedConfigsImpl( |
| 338 | uint32_t featureId, |
| 339 | uint32_t maxConfigs, |
| 340 | uint32_t configSize, |
| 341 | GetSupportedConfigsSuccessCallback onSuccess) { |
| 342 | uint32_t halCmd[2] = { featureId, maxConfigs }; |
| 343 | uint32_t halResultSize = 2 * sizeof(uint32_t) + maxConfigs * sizeof(configSize); |
| 344 | uint8_t halResult[halResultSize]; |
| 345 | memset(&halResult[0], 0, halResultSize); |
| 346 | return sendCommandReturningStatusAndData( |
| 347 | EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, "GET_FEATURE_SUPPORTED_CONFIGS", |
| 348 | sizeof(halCmd), halCmd, |
| 349 | &halResultSize, &halResult[0], |
| 350 | 2 * sizeof(uint32_t), |
| 351 | [&]{ |
| 352 | uint32_t *halResult32 = reinterpret_cast<uint32_t*>(&halResult[0]); |
| 353 | uint32_t supportedConfigs = *(++halResult32); // skip status field |
| 354 | if (supportedConfigs > maxConfigs) supportedConfigs = maxConfigs; |
| 355 | onSuccess(supportedConfigs, ++halResult32); |
| 356 | }); |
| 357 | } |
| 358 | |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 359 | Return<void> Effect::prepareForProcessing(prepareForProcessing_cb _hidl_cb) { |
| 360 | status_t status; |
| 361 | // Create message queue. |
| 362 | if (mStatusMQ) { |
| 363 | ALOGE("the client attempts to call prepareForProcessing_cb twice"); |
| 364 | _hidl_cb(Result::INVALID_STATE, StatusMQ::Descriptor()); |
| 365 | return Void(); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 366 | } |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 367 | std::unique_ptr<StatusMQ> tempStatusMQ(new StatusMQ(1, true /*EventFlag*/)); |
| 368 | if (!tempStatusMQ->isValid()) { |
| 369 | ALOGE_IF(!tempStatusMQ->isValid(), "status MQ is invalid"); |
| 370 | _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor()); |
| 371 | return Void(); |
| 372 | } |
| 373 | status = EventFlag::createEventFlag(tempStatusMQ->getEventFlagWord(), &mEfGroup); |
| 374 | if (status != OK || !mEfGroup) { |
| 375 | ALOGE("failed creating event flag for status MQ: %s", strerror(-status)); |
| 376 | _hidl_cb(Result::INVALID_ARGUMENTS, StatusMQ::Descriptor()); |
| 377 | return Void(); |
| 378 | } |
| 379 | |
| 380 | // Create and launch the thread. |
| 381 | mProcessThread = new ProcessThread( |
| 382 | &mStopProcessThread, |
| 383 | mHandle, |
| 384 | &mHalInBufferPtr, |
| 385 | &mHalOutBufferPtr, |
| 386 | tempStatusMQ.get(), |
| 387 | mEfGroup); |
| 388 | status = mProcessThread->run("effect", PRIORITY_URGENT_AUDIO); |
| 389 | if (status != OK) { |
| 390 | ALOGW("failed to start effect processing thread: %s", strerror(-status)); |
| 391 | _hidl_cb(Result::INVALID_ARGUMENTS, MQDescriptorSync<Result>()); |
| 392 | return Void(); |
| 393 | } |
| 394 | |
| 395 | mStatusMQ = std::move(tempStatusMQ); |
| 396 | _hidl_cb(Result::OK, *mStatusMQ->getDesc()); |
| 397 | return Void(); |
| 398 | } |
| 399 | |
| 400 | Return<Result> Effect::setProcessBuffers( |
| 401 | const AudioBuffer& inBuffer, const AudioBuffer& outBuffer) { |
| 402 | AudioBufferManager& manager = AudioBufferManager::getInstance(); |
| 403 | sp<AudioBufferWrapper> tempInBuffer, tempOutBuffer; |
| 404 | if (!manager.wrap(inBuffer, &tempInBuffer)) { |
| 405 | ALOGE("Could not map memory of the input buffer"); |
| 406 | return Result::INVALID_ARGUMENTS; |
| 407 | } |
| 408 | if (!manager.wrap(outBuffer, &tempOutBuffer)) { |
| 409 | ALOGE("Could not map memory of the output buffer"); |
| 410 | return Result::INVALID_ARGUMENTS; |
| 411 | } |
| 412 | mInBuffer = tempInBuffer; |
| 413 | mOutBuffer = tempOutBuffer; |
| 414 | // The processing thread only reads these pointers after waking up by an event flag, |
| 415 | // so it's OK to update the pair non-atomically. |
| 416 | mHalInBufferPtr.store(mInBuffer->getHalBuffer(), std::memory_order_release); |
| 417 | mHalOutBufferPtr.store(mOutBuffer->getHalBuffer(), std::memory_order_release); |
| 418 | return Result::OK; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | Result Effect::sendCommand(int commandCode, const char* commandName) { |
| 422 | return sendCommand(commandCode, commandName, 0, NULL); |
| 423 | } |
| 424 | |
| 425 | Result Effect::sendCommand( |
| 426 | int commandCode, const char* commandName, uint32_t size, void* data) { |
| 427 | status_t status = (*mHandle)->command(mHandle, commandCode, size, data, 0, NULL); |
| 428 | return analyzeCommandStatus(commandName, sContextCallToCommand, status); |
| 429 | } |
| 430 | |
| 431 | Result Effect::sendCommandReturningData( |
| 432 | int commandCode, const char* commandName, |
| 433 | uint32_t* replySize, void* replyData) { |
| 434 | return sendCommandReturningData(commandCode, commandName, 0, NULL, replySize, replyData); |
| 435 | } |
| 436 | |
| 437 | Result Effect::sendCommandReturningData( |
| 438 | int commandCode, const char* commandName, |
| 439 | uint32_t size, void* data, |
| 440 | uint32_t* replySize, void* replyData) { |
| 441 | uint32_t expectedReplySize = *replySize; |
| 442 | status_t status = (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData); |
| 443 | if (status == OK && *replySize != expectedReplySize) { |
| 444 | status = -ENODATA; |
| 445 | } |
| 446 | return analyzeCommandStatus(commandName, sContextCallToCommand, status); |
| 447 | } |
| 448 | |
| 449 | Result Effect::sendCommandReturningStatus(int commandCode, const char* commandName) { |
| 450 | return sendCommandReturningStatus(commandCode, commandName, 0, NULL); |
| 451 | } |
| 452 | |
| 453 | Result Effect::sendCommandReturningStatus( |
| 454 | int commandCode, const char* commandName, uint32_t size, void* data) { |
| 455 | uint32_t replyCmdStatus; |
| 456 | uint32_t replySize = sizeof(uint32_t); |
| 457 | return sendCommandReturningStatusAndData( |
| 458 | commandCode, commandName, size, data, &replySize, &replyCmdStatus, replySize, []{}); |
| 459 | } |
| 460 | |
| 461 | Result Effect::sendCommandReturningStatusAndData( |
| 462 | int commandCode, const char* commandName, |
| 463 | uint32_t size, void* data, |
| 464 | uint32_t* replySize, void* replyData, |
| 465 | uint32_t minReplySize, |
| 466 | CommandSuccessCallback onSuccess) { |
| 467 | status_t status = |
| 468 | (*mHandle)->command(mHandle, commandCode, size, data, replySize, replyData); |
| 469 | Result retval; |
| 470 | if (status == OK && minReplySize >= sizeof(uint32_t) && *replySize >= minReplySize) { |
| 471 | uint32_t commandStatus = *reinterpret_cast<uint32_t*>(replyData); |
| 472 | retval = analyzeCommandStatus(commandName, sContextResultOfCommand, commandStatus); |
| 473 | if (commandStatus == OK) { |
| 474 | onSuccess(); |
| 475 | } |
| 476 | } else { |
| 477 | retval = analyzeCommandStatus(commandName, sContextCallToCommand, status); |
| 478 | } |
| 479 | return retval; |
| 480 | } |
| 481 | |
| 482 | Result Effect::setConfigImpl( |
| 483 | int commandCode, const char* commandName, |
| 484 | const EffectConfig& config, |
| 485 | const sp<IEffectBufferProviderCallback>& inputBufferProvider, |
| 486 | const sp<IEffectBufferProviderCallback>& outputBufferProvider) { |
| 487 | effect_config_t halConfig; |
| 488 | effectConfigToHal(config, &halConfig); |
| 489 | if (inputBufferProvider != 0) { |
| 490 | LOG_FATAL("Using input buffer provider is not supported"); |
| 491 | } |
| 492 | if (outputBufferProvider != 0) { |
| 493 | LOG_FATAL("Using output buffer provider is not supported"); |
| 494 | } |
| 495 | return sendCommandReturningStatus( |
| 496 | commandCode, commandName, sizeof(effect_config_t), &halConfig); |
| 497 | } |
| 498 | |
| 499 | |
| 500 | Result Effect::setParameterImpl( |
| 501 | uint32_t paramSize, const void* paramData, uint32_t valueSize, const void* valueData) { |
| 502 | std::vector<uint8_t> halParamBuffer = parameterToHal( |
| 503 | paramSize, paramData, valueSize, &valueData); |
| 504 | return sendCommandReturningStatus( |
| 505 | EFFECT_CMD_SET_PARAM, "SET_PARAM", halParamBuffer.size(), &halParamBuffer[0]); |
| 506 | } |
| 507 | |
| 508 | // Methods from ::android::hardware::audio::effect::V2_0::IEffect follow. |
| 509 | Return<Result> Effect::init() { |
| 510 | return sendCommandReturningStatus(EFFECT_CMD_INIT, "INIT"); |
| 511 | } |
| 512 | |
| 513 | Return<Result> Effect::setConfig( |
| 514 | const EffectConfig& config, |
| 515 | const sp<IEffectBufferProviderCallback>& inputBufferProvider, |
| 516 | const sp<IEffectBufferProviderCallback>& outputBufferProvider) { |
| 517 | return setConfigImpl( |
| 518 | EFFECT_CMD_SET_CONFIG, "SET_CONFIG", config, inputBufferProvider, outputBufferProvider); |
| 519 | } |
| 520 | |
| 521 | Return<Result> Effect::reset() { |
| 522 | return sendCommand(EFFECT_CMD_RESET, "RESET"); |
| 523 | } |
| 524 | |
| 525 | Return<Result> Effect::enable() { |
| 526 | return sendCommandReturningStatus(EFFECT_CMD_ENABLE, "ENABLE"); |
| 527 | } |
| 528 | |
| 529 | Return<Result> Effect::disable() { |
| 530 | return sendCommandReturningStatus(EFFECT_CMD_DISABLE, "DISABLE"); |
| 531 | } |
| 532 | |
| 533 | Return<Result> Effect::setDevice(AudioDevice device) { |
| 534 | uint32_t halDevice = static_cast<uint32_t>(device); |
| 535 | return sendCommand(EFFECT_CMD_SET_DEVICE, "SET_DEVICE", sizeof(uint32_t), &halDevice); |
| 536 | } |
| 537 | |
| 538 | Return<void> Effect::setAndGetVolume( |
| 539 | const hidl_vec<uint32_t>& volumes, setAndGetVolume_cb _hidl_cb) { |
| 540 | uint32_t halDataSize; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 541 | std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 542 | uint32_t halResultSize = halDataSize; |
| 543 | uint32_t halResult[volumes.size()]; |
| 544 | Result retval = sendCommandReturningData( |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 545 | EFFECT_CMD_SET_VOLUME, "SET_VOLUME", |
| 546 | halDataSize, &halData[0], |
| 547 | &halResultSize, halResult); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 548 | hidl_vec<uint32_t> result; |
| 549 | if (retval == Result::OK) { |
| 550 | result.setToExternal(&halResult[0], halResultSize); |
| 551 | } |
| 552 | _hidl_cb(retval, result); |
| 553 | return Void(); |
| 554 | } |
| 555 | |
Mikhail Naganov | f4f2ff3 | 2017-01-19 12:38:39 -0800 | [diff] [blame] | 556 | Return<Result> Effect::volumeChangeNotification(const hidl_vec<uint32_t>& volumes) { |
| 557 | uint32_t halDataSize; |
| 558 | std::unique_ptr<uint8_t[]> halData = hidlVecToHal(volumes, &halDataSize); |
| 559 | return sendCommand( |
| 560 | EFFECT_CMD_SET_VOLUME, "SET_VOLUME", |
| 561 | halDataSize, &halData[0]); |
| 562 | } |
| 563 | |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 564 | Return<Result> Effect::setAudioMode(AudioMode mode) { |
| 565 | uint32_t halMode = static_cast<uint32_t>(mode); |
| 566 | return sendCommand( |
| 567 | EFFECT_CMD_SET_AUDIO_MODE, "SET_AUDIO_MODE", sizeof(uint32_t), &halMode); |
| 568 | } |
| 569 | |
| 570 | Return<Result> Effect::setConfigReverse( |
| 571 | const EffectConfig& config, |
| 572 | const sp<IEffectBufferProviderCallback>& inputBufferProvider, |
| 573 | const sp<IEffectBufferProviderCallback>& outputBufferProvider) { |
| 574 | return setConfigImpl(EFFECT_CMD_SET_CONFIG_REVERSE, "SET_CONFIG_REVERSE", |
| 575 | config, inputBufferProvider, outputBufferProvider); |
| 576 | } |
| 577 | |
| 578 | Return<Result> Effect::setInputDevice(AudioDevice device) { |
| 579 | uint32_t halDevice = static_cast<uint32_t>(device); |
| 580 | return sendCommand( |
| 581 | EFFECT_CMD_SET_INPUT_DEVICE, "SET_INPUT_DEVICE", sizeof(uint32_t), &halDevice); |
| 582 | } |
| 583 | |
| 584 | Return<void> Effect::getConfig(getConfig_cb _hidl_cb) { |
| 585 | getConfigImpl(EFFECT_CMD_GET_CONFIG, "GET_CONFIG", _hidl_cb); |
| 586 | return Void(); |
| 587 | } |
| 588 | |
| 589 | Return<void> Effect::getConfigReverse(getConfigReverse_cb _hidl_cb) { |
| 590 | getConfigImpl(EFFECT_CMD_GET_CONFIG_REVERSE, "GET_CONFIG_REVERSE", _hidl_cb); |
| 591 | return Void(); |
| 592 | } |
| 593 | |
| 594 | Return<void> Effect::getSupportedAuxChannelsConfigs( |
| 595 | uint32_t maxConfigs, getSupportedAuxChannelsConfigs_cb _hidl_cb) { |
| 596 | hidl_vec<EffectAuxChannelsConfig> result; |
| 597 | Result retval = getSupportedConfigsImpl( |
| 598 | EFFECT_FEATURE_AUX_CHANNELS, |
| 599 | maxConfigs, |
| 600 | sizeof(channel_config_t), |
| 601 | [&] (uint32_t supportedConfigs, void* configsData) { |
| 602 | result.resize(supportedConfigs); |
| 603 | channel_config_t *config = reinterpret_cast<channel_config_t*>(configsData); |
| 604 | for (size_t i = 0; i < result.size(); ++i) { |
| 605 | effectAuxChannelsConfigFromHal(*config++, &result[i]); |
| 606 | } |
| 607 | }); |
| 608 | _hidl_cb(retval, result); |
| 609 | return Void(); |
| 610 | } |
| 611 | |
| 612 | Return<void> Effect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) { |
| 613 | uint32_t halCmd = EFFECT_FEATURE_AUX_CHANNELS; |
| 614 | uint32_t halResult[alignedSizeIn<uint32_t>(sizeof(uint32_t) + sizeof(channel_config_t))]; |
| 615 | memset(halResult, 0, sizeof(halResult)); |
| 616 | uint32_t halResultSize = 0; |
| 617 | EffectAuxChannelsConfig result; |
| 618 | Result retval = getCurrentConfigImpl( |
| 619 | EFFECT_FEATURE_AUX_CHANNELS, |
| 620 | sizeof(channel_config_t), |
| 621 | [&] (void* configData) { |
| 622 | effectAuxChannelsConfigFromHal( |
| 623 | *reinterpret_cast<channel_config_t*>(configData), &result); |
| 624 | }); |
| 625 | _hidl_cb(retval, result); |
| 626 | return Void(); |
| 627 | } |
| 628 | |
| 629 | Return<Result> Effect::setAuxChannelsConfig(const EffectAuxChannelsConfig& config) { |
| 630 | uint32_t halCmd[alignedSizeIn<uint32_t>(sizeof(uint32_t) + sizeof(channel_config_t))]; |
| 631 | halCmd[0] = EFFECT_FEATURE_AUX_CHANNELS; |
| 632 | effectAuxChannelsConfigToHal(config, reinterpret_cast<channel_config_t*>(&halCmd[1])); |
| 633 | return sendCommandReturningStatus(EFFECT_CMD_SET_FEATURE_CONFIG, |
| 634 | "SET_FEATURE_CONFIG AUX_CHANNELS", sizeof(halCmd), halCmd); |
| 635 | } |
| 636 | |
| 637 | Return<Result> Effect::setAudioSource(AudioSource source) { |
| 638 | uint32_t halSource = static_cast<uint32_t>(source); |
| 639 | return sendCommand( |
| 640 | EFFECT_CMD_SET_AUDIO_SOURCE, "SET_AUDIO_SOURCE", sizeof(uint32_t), &halSource); |
| 641 | } |
| 642 | |
| 643 | Return<Result> Effect::offload(const EffectOffloadParameter& param) { |
| 644 | effect_offload_param_t halParam; |
| 645 | effectOffloadParamToHal(param, &halParam); |
| 646 | return sendCommandReturningStatus( |
| 647 | EFFECT_CMD_OFFLOAD, "OFFLOAD", sizeof(effect_offload_param_t), &halParam); |
| 648 | } |
| 649 | |
| 650 | Return<void> Effect::getDescriptor(getDescriptor_cb _hidl_cb) { |
| 651 | effect_descriptor_t halDescriptor; |
| 652 | memset(&halDescriptor, 0, sizeof(effect_descriptor_t)); |
| 653 | status_t status = (*mHandle)->get_descriptor(mHandle, &halDescriptor); |
| 654 | EffectDescriptor descriptor; |
| 655 | if (status == OK) { |
| 656 | effectDescriptorFromHal(halDescriptor, &descriptor); |
| 657 | } |
| 658 | _hidl_cb(analyzeStatus("get_descriptor", "", sContextCallFunction, status), descriptor); |
| 659 | return Void(); |
| 660 | } |
| 661 | |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 662 | Return<void> Effect::command( |
| 663 | uint32_t commandId, |
| 664 | const hidl_vec<uint8_t>& data, |
| 665 | uint32_t resultMaxSize, |
| 666 | command_cb _hidl_cb) { |
| 667 | uint32_t halDataSize; |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 668 | std::unique_ptr<uint8_t[]> halData = hidlVecToHal(data, &halDataSize); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 669 | uint32_t halResultSize = resultMaxSize; |
| 670 | std::unique_ptr<uint8_t[]> halResult(new uint8_t[halResultSize]); |
| 671 | memset(&halResult[0], 0, halResultSize); |
Mikhail Naganov | f4f2ff3 | 2017-01-19 12:38:39 -0800 | [diff] [blame] | 672 | |
| 673 | void* dataPtr = halDataSize > 0 ? &halData[0] : NULL; |
| 674 | void* resultPtr = halResultSize > 0 ? &halResult[0] : NULL; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 675 | status_t status = (*mHandle)->command( |
Mikhail Naganov | f4f2ff3 | 2017-01-19 12:38:39 -0800 | [diff] [blame] | 676 | mHandle, commandId, halDataSize, dataPtr, &halResultSize, resultPtr); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 677 | hidl_vec<uint8_t> result; |
Mikhail Naganov | f4f2ff3 | 2017-01-19 12:38:39 -0800 | [diff] [blame] | 678 | if (status == OK && resultPtr != NULL) { |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 679 | result.setToExternal(&halResult[0], halResultSize); |
| 680 | } |
| 681 | _hidl_cb(status, result); |
| 682 | return Void(); |
| 683 | } |
| 684 | |
| 685 | Return<Result> Effect::setParameter( |
| 686 | const hidl_vec<uint8_t>& parameter, const hidl_vec<uint8_t>& value) { |
| 687 | return setParameterImpl(parameter.size(), ¶meter[0], value.size(), &value[0]); |
| 688 | } |
| 689 | |
| 690 | Return<void> Effect::getParameter( |
| 691 | const hidl_vec<uint8_t>& parameter, uint32_t valueMaxSize, getParameter_cb _hidl_cb) { |
| 692 | hidl_vec<uint8_t> value; |
| 693 | Result retval = getParameterImpl( |
| 694 | parameter.size(), |
| 695 | ¶meter[0], |
| 696 | valueMaxSize, |
| 697 | [&] (uint32_t valueSize, const void* valueData) { |
| 698 | value.setToExternal( |
| 699 | reinterpret_cast<uint8_t*>(const_cast<void*>(valueData)), valueSize); |
| 700 | }); |
| 701 | _hidl_cb(retval, value); |
| 702 | return Void(); |
| 703 | } |
| 704 | |
| 705 | Return<void> Effect::getSupportedConfigsForFeature( |
| 706 | uint32_t featureId, |
| 707 | uint32_t maxConfigs, |
| 708 | uint32_t configSize, |
| 709 | getSupportedConfigsForFeature_cb _hidl_cb) { |
| 710 | uint32_t configCount = 0; |
| 711 | hidl_vec<uint8_t> result; |
| 712 | Result retval = getSupportedConfigsImpl( |
| 713 | featureId, |
| 714 | maxConfigs, |
| 715 | configSize, |
| 716 | [&] (uint32_t supportedConfigs, void* configsData) { |
| 717 | configCount = supportedConfigs; |
| 718 | result.resize(configCount * configSize); |
| 719 | memcpy(&result[0], configsData, result.size()); |
| 720 | }); |
| 721 | _hidl_cb(retval, configCount, result); |
| 722 | return Void(); |
| 723 | } |
| 724 | |
| 725 | Return<void> Effect::getCurrentConfigForFeature( |
| 726 | uint32_t featureId, uint32_t configSize, getCurrentConfigForFeature_cb _hidl_cb) { |
| 727 | hidl_vec<uint8_t> result; |
| 728 | Result retval = getCurrentConfigImpl( |
| 729 | featureId, |
| 730 | configSize, |
| 731 | [&] (void* configData) { |
| 732 | result.resize(configSize); |
| 733 | memcpy(&result[0], configData, result.size()); |
| 734 | }); |
| 735 | _hidl_cb(retval, result); |
| 736 | return Void(); |
| 737 | } |
| 738 | |
| 739 | Return<Result> Effect::setCurrentConfigForFeature( |
| 740 | uint32_t featureId, const hidl_vec<uint8_t>& configData) { |
| 741 | uint32_t halCmd[alignedSizeIn<uint32_t>(sizeof(uint32_t) + configData.size())]; |
| 742 | memset(halCmd, 0, sizeof(halCmd)); |
| 743 | halCmd[0] = featureId; |
| 744 | memcpy(&halCmd[1], &configData[0], configData.size()); |
| 745 | return sendCommandReturningStatus( |
| 746 | EFFECT_CMD_SET_FEATURE_CONFIG, "SET_FEATURE_CONFIG", sizeof(halCmd), halCmd); |
| 747 | } |
| 748 | |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 749 | Return<Result> Effect::close() { |
| 750 | if (mIsClosed) return Result::INVALID_STATE; |
| 751 | mIsClosed = true; |
| 752 | if (mProcessThread.get()) { |
| 753 | mStopProcessThread.store(true, std::memory_order_release); |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 754 | } |
| 755 | if (mEfGroup) { |
Mikhail Naganov | b0abafb | 2017-01-31 17:24:48 -0800 | [diff] [blame] | 756 | mEfGroup->wake(static_cast<uint32_t>(MessageQueueFlagBits::REQUEST_QUIT)); |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 757 | } |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 758 | return Result::OK; |
| 759 | } |
| 760 | |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 761 | } // namespace implementation |
| 762 | } // namespace V2_0 |
| 763 | } // namespace effect |
| 764 | } // namespace audio |
| 765 | } // namespace hardware |
| 766 | } // namespace android |