Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 1 | /* |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame^] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 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 | |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame^] | 17 | #ifndef ANDROID_HARDWARE_AUDIO_EFFECT_EFFECT_H |
| 18 | #define ANDROID_HARDWARE_AUDIO_EFFECT_EFFECT_H |
| 19 | |
| 20 | #include PATH(android/hardware/audio/effect/FILE_VERSION/IEffect.h) |
| 21 | |
| 22 | #include "AudioBufferManager.h" |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 23 | |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 24 | #include <atomic> |
Mikhail Naganov | 6e81e9b | 2016-11-16 16:30:17 -0800 | [diff] [blame] | 25 | #include <memory> |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 28 | #include <fmq/EventFlag.h> |
| 29 | #include <fmq/MessageQueue.h> |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 30 | #include <hidl/MQDescriptor.h> |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 31 | #include <hidl/Status.h> |
| 32 | #include <utils/Thread.h> |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 33 | |
| 34 | #include <hardware/audio_effect.h> |
| 35 | |
Kevin Rocard | 30a7fcc | 2018-03-01 15:08:07 -0800 | [diff] [blame] | 36 | #include "VersionUtils.h" |
| 37 | |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 38 | namespace android { |
| 39 | namespace hardware { |
| 40 | namespace audio { |
| 41 | namespace effect { |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame^] | 42 | namespace CPP_VERSION { |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 43 | namespace implementation { |
| 44 | |
Kevin Rocard | 55b1061 | 2018-11-12 12:33:16 -0800 | [diff] [blame] | 45 | using ::android::sp; |
| 46 | using ::android::hardware::hidl_string; |
| 47 | using ::android::hardware::hidl_vec; |
| 48 | using ::android::hardware::Return; |
| 49 | using ::android::hardware::Void; |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame^] | 50 | using ::android::hardware::audio::common::CPP_VERSION::AudioDevice; |
| 51 | using ::android::hardware::audio::common::CPP_VERSION::AudioMode; |
| 52 | using ::android::hardware::audio::common::CPP_VERSION::AudioSource; |
| 53 | using ::android::hardware::audio::common::CPP_VERSION::Uuid; |
| 54 | using ::android::hardware::audio::common::CPP_VERSION::implementation::AudioDeviceBitfield; |
| 55 | using ::android::hardware::audio::effect::CPP_VERSION::AudioBuffer; |
| 56 | using ::android::hardware::audio::effect::CPP_VERSION::EffectAuxChannelsConfig; |
| 57 | using ::android::hardware::audio::effect::CPP_VERSION::EffectConfig; |
| 58 | using ::android::hardware::audio::effect::CPP_VERSION::EffectDescriptor; |
| 59 | using ::android::hardware::audio::effect::CPP_VERSION::EffectFeature; |
| 60 | using ::android::hardware::audio::effect::CPP_VERSION::EffectOffloadParameter; |
| 61 | using ::android::hardware::audio::effect::CPP_VERSION::IEffect; |
| 62 | using ::android::hardware::audio::effect::CPP_VERSION::IEffectBufferProviderCallback; |
| 63 | using ::android::hardware::audio::effect::CPP_VERSION::Result; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 64 | |
| 65 | struct Effect : public IEffect { |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 66 | typedef MessageQueue<Result, kSynchronizedReadWrite> StatusMQ; |
Mikhail Naganov | 9f28904 | 2017-02-23 08:39:36 -0800 | [diff] [blame] | 67 | using GetParameterSuccessCallback = |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 68 | std::function<void(uint32_t valueSize, const void* valueData)>; |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 69 | |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 70 | explicit Effect(effect_handle_t handle); |
| 71 | |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame^] | 72 | // Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow. |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 73 | Return<Result> init() override; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 74 | Return<Result> setConfig( |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 75 | const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider, |
| 76 | const sp<IEffectBufferProviderCallback>& outputBufferProvider) override; |
| 77 | Return<Result> reset() override; |
| 78 | Return<Result> enable() override; |
| 79 | Return<Result> disable() override; |
Kevin Rocard | 30a7fcc | 2018-03-01 15:08:07 -0800 | [diff] [blame] | 80 | Return<Result> setDevice(AudioDeviceBitfield device) override; |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 81 | Return<void> setAndGetVolume(const hidl_vec<uint32_t>& volumes, |
| 82 | setAndGetVolume_cb _hidl_cb) override; |
| 83 | Return<Result> volumeChangeNotification(const hidl_vec<uint32_t>& volumes) override; |
| 84 | Return<Result> setAudioMode(AudioMode mode) override; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 85 | Return<Result> setConfigReverse( |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 86 | const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider, |
| 87 | const sp<IEffectBufferProviderCallback>& outputBufferProvider) override; |
Kevin Rocard | 30a7fcc | 2018-03-01 15:08:07 -0800 | [diff] [blame] | 88 | Return<Result> setInputDevice(AudioDeviceBitfield device) override; |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 89 | Return<void> getConfig(getConfig_cb _hidl_cb) override; |
| 90 | Return<void> getConfigReverse(getConfigReverse_cb _hidl_cb) override; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 91 | Return<void> getSupportedAuxChannelsConfigs( |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 92 | uint32_t maxConfigs, getSupportedAuxChannelsConfigs_cb _hidl_cb) override; |
| 93 | Return<void> getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) override; |
| 94 | Return<Result> setAuxChannelsConfig(const EffectAuxChannelsConfig& config) override; |
| 95 | Return<Result> setAudioSource(AudioSource source) override; |
| 96 | Return<Result> offload(const EffectOffloadParameter& param) override; |
| 97 | Return<void> getDescriptor(getDescriptor_cb _hidl_cb) override; |
| 98 | Return<void> prepareForProcessing(prepareForProcessing_cb _hidl_cb) override; |
| 99 | Return<Result> setProcessBuffers(const AudioBuffer& inBuffer, |
| 100 | const AudioBuffer& outBuffer) override; |
| 101 | Return<void> command(uint32_t commandId, const hidl_vec<uint8_t>& data, uint32_t resultMaxSize, |
| 102 | command_cb _hidl_cb) override; |
| 103 | Return<Result> setParameter(const hidl_vec<uint8_t>& parameter, |
| 104 | const hidl_vec<uint8_t>& value) override; |
| 105 | Return<void> getParameter(const hidl_vec<uint8_t>& parameter, uint32_t valueMaxSize, |
| 106 | getParameter_cb _hidl_cb) override; |
| 107 | Return<void> getSupportedConfigsForFeature(uint32_t featureId, uint32_t maxConfigs, |
| 108 | uint32_t configSize, |
| 109 | getSupportedConfigsForFeature_cb _hidl_cb) override; |
| 110 | Return<void> getCurrentConfigForFeature(uint32_t featureId, uint32_t configSize, |
| 111 | getCurrentConfigForFeature_cb _hidl_cb) override; |
| 112 | Return<Result> setCurrentConfigForFeature(uint32_t featureId, |
| 113 | const hidl_vec<uint8_t>& configData) override; |
| 114 | Return<Result> close() override; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 115 | |
| 116 | // Utility methods for extending interfaces. |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 117 | template <typename T> |
| 118 | Return<void> getIntegerParam(uint32_t paramId, |
| 119 | std::function<void(Result retval, T paramValue)> cb) { |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 120 | T value; |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 121 | Result retval = getParameterImpl(sizeof(uint32_t), ¶mId, sizeof(T), |
| 122 | [&](uint32_t valueSize, const void* valueData) { |
| 123 | if (valueSize > sizeof(T)) valueSize = sizeof(T); |
| 124 | memcpy(&value, valueData, valueSize); |
| 125 | }); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 126 | cb(retval, value); |
| 127 | return Void(); |
| 128 | } |
| 129 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 130 | template <typename T> |
| 131 | Result getParam(uint32_t paramId, T& paramValue) { |
| 132 | return getParameterImpl(sizeof(uint32_t), ¶mId, sizeof(T), |
| 133 | [&](uint32_t valueSize, const void* valueData) { |
| 134 | if (valueSize > sizeof(T)) valueSize = sizeof(T); |
| 135 | memcpy(¶mValue, valueData, valueSize); |
| 136 | }); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 139 | template <typename T> |
| 140 | Result getParam(uint32_t paramId, uint32_t paramArg, T& paramValue) { |
| 141 | uint32_t params[2] = {paramId, paramArg}; |
| 142 | return getParameterImpl(sizeof(params), params, sizeof(T), |
| 143 | [&](uint32_t valueSize, const void* valueData) { |
| 144 | if (valueSize > sizeof(T)) valueSize = sizeof(T); |
| 145 | memcpy(¶mValue, valueData, valueSize); |
| 146 | }); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 149 | template <typename T> |
| 150 | Result setParam(uint32_t paramId, const T& paramValue) { |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 151 | return setParameterImpl(sizeof(uint32_t), ¶mId, sizeof(T), ¶mValue); |
| 152 | } |
| 153 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 154 | template <typename T> |
| 155 | Result setParam(uint32_t paramId, uint32_t paramArg, const T& paramValue) { |
| 156 | uint32_t params[2] = {paramId, paramArg}; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 157 | return setParameterImpl(sizeof(params), params, sizeof(T), ¶mValue); |
| 158 | } |
| 159 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 160 | Result getParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize, |
| 161 | GetParameterSuccessCallback onSuccess) { |
Mikhail Naganov | 9f28904 | 2017-02-23 08:39:36 -0800 | [diff] [blame] | 162 | return getParameterImpl(paramSize, paramData, valueSize, valueSize, onSuccess); |
| 163 | } |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 164 | Result getParameterImpl(uint32_t paramSize, const void* paramData, uint32_t requestValueSize, |
| 165 | uint32_t replyValueSize, GetParameterSuccessCallback onSuccess); |
| 166 | Result setParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize, |
| 167 | const void* valueData); |
Mikhail Naganov | 9f28904 | 2017-02-23 08:39:36 -0800 | [diff] [blame] | 168 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 169 | private: |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 170 | friend struct VirtualizerEffect; // for getParameterImpl |
| 171 | friend struct VisualizerEffect; // to allow executing commands |
| 172 | |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 173 | using CommandSuccessCallback = std::function<void()>; |
| 174 | using GetConfigCallback = std::function<void(Result retval, const EffectConfig& config)>; |
| 175 | using GetCurrentConfigSuccessCallback = std::function<void(void* configData)>; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 176 | using GetSupportedConfigsSuccessCallback = |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 177 | std::function<void(uint32_t supportedConfigs, void* configsData)>; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 178 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 179 | static const char* sContextResultOfCommand; |
| 180 | static const char* sContextCallToCommand; |
| 181 | static const char* sContextCallFunction; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 182 | |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 183 | bool mIsClosed; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 184 | effect_handle_t mHandle; |
Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 185 | sp<AudioBufferWrapper> mInBuffer; |
| 186 | sp<AudioBufferWrapper> mOutBuffer; |
| 187 | std::atomic<audio_buffer_t*> mHalInBufferPtr; |
| 188 | std::atomic<audio_buffer_t*> mHalOutBufferPtr; |
| 189 | std::unique_ptr<StatusMQ> mStatusMQ; |
| 190 | EventFlag* mEfGroup; |
| 191 | std::atomic<bool> mStopProcessThread; |
| 192 | sp<Thread> mProcessThread; |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 193 | |
| 194 | virtual ~Effect(); |
| 195 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 196 | template <typename T> |
| 197 | static size_t alignedSizeIn(size_t s); |
| 198 | template <typename T> |
| 199 | std::unique_ptr<uint8_t[]> hidlVecToHal(const hidl_vec<T>& vec, uint32_t* halDataSize); |
| 200 | static void effectAuxChannelsConfigFromHal(const channel_config_t& halConfig, |
| 201 | EffectAuxChannelsConfig* config); |
| 202 | static void effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config, |
| 203 | channel_config_t* halConfig); |
| 204 | static void effectBufferConfigFromHal(const buffer_config_t& halConfig, |
| 205 | EffectBufferConfig* config); |
| 206 | static void effectBufferConfigToHal(const EffectBufferConfig& config, |
| 207 | buffer_config_t* halConfig); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 208 | static void effectConfigFromHal(const effect_config_t& halConfig, EffectConfig* config); |
| 209 | static void effectConfigToHal(const EffectConfig& config, effect_config_t* halConfig); |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 210 | static void effectOffloadParamToHal(const EffectOffloadParameter& offload, |
| 211 | effect_offload_param_t* halOffload); |
| 212 | static std::vector<uint8_t> parameterToHal(uint32_t paramSize, const void* paramData, |
| 213 | uint32_t valueSize, const void** valueData); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 214 | |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 215 | Result analyzeCommandStatus(const char* commandName, const char* context, status_t status); |
| 216 | Result analyzeStatus(const char* funcName, const char* subFuncName, |
| 217 | const char* contextDescription, status_t status); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 218 | void getConfigImpl(int commandCode, const char* commandName, GetConfigCallback cb); |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 219 | Result getCurrentConfigImpl(uint32_t featureId, uint32_t configSize, |
| 220 | GetCurrentConfigSuccessCallback onSuccess); |
| 221 | Result getSupportedConfigsImpl(uint32_t featureId, uint32_t maxConfigs, uint32_t configSize, |
| 222 | GetSupportedConfigsSuccessCallback onSuccess); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 223 | Result sendCommand(int commandCode, const char* commandName); |
| 224 | Result sendCommand(int commandCode, const char* commandName, uint32_t size, void* data); |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 225 | Result sendCommandReturningData(int commandCode, const char* commandName, uint32_t* replySize, |
| 226 | void* replyData); |
| 227 | Result sendCommandReturningData(int commandCode, const char* commandName, uint32_t size, |
| 228 | void* data, uint32_t* replySize, void* replyData); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 229 | Result sendCommandReturningStatus(int commandCode, const char* commandName); |
Kevin Rocard | 22505e6 | 2017-12-14 18:50:12 -0800 | [diff] [blame] | 230 | Result sendCommandReturningStatus(int commandCode, const char* commandName, uint32_t size, |
| 231 | void* data); |
| 232 | Result sendCommandReturningStatusAndData(int commandCode, const char* commandName, |
| 233 | uint32_t size, void* data, uint32_t* replySize, |
| 234 | void* replyData, uint32_t minReplySize, |
| 235 | CommandSuccessCallback onSuccess); |
| 236 | Result setConfigImpl(int commandCode, const char* commandName, const EffectConfig& config, |
| 237 | const sp<IEffectBufferProviderCallback>& inputBufferProvider, |
| 238 | const sp<IEffectBufferProviderCallback>& outputBufferProvider); |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | } // namespace implementation |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame^] | 242 | } // namespace CPP_VERSION |
Mikhail Naganov | 7cbf2f1 | 2016-10-27 20:05:35 -0700 | [diff] [blame] | 243 | } // namespace effect |
| 244 | } // namespace audio |
| 245 | } // namespace hardware |
| 246 | } // namespace android |
Kevin Rocard | 96d2cd9 | 2018-11-14 16:22:07 -0800 | [diff] [blame^] | 247 | |
| 248 | #endif // ANDROID_HARDWARE_AUDIO_EFFECT_EFFECT_H |