1 /* 2 * Copyright (C) 2018 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 #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" 23 24 #include <atomic> 25 #include <memory> 26 #include <tuple> 27 #include <vector> 28 29 #include <fmq/EventFlag.h> 30 #include <fmq/MessageQueue.h> 31 #include <hidl/MQDescriptor.h> 32 #include <hidl/Status.h> 33 #include <mediautils/MethodStatistics.h> 34 #include <utils/Thread.h> 35 36 #include <hardware/audio_effect.h> 37 38 #include "VersionUtils.h" 39 40 namespace android { 41 namespace hardware { 42 namespace audio { 43 namespace effect { 44 namespace CPP_VERSION { 45 namespace implementation { 46 47 using ::android::sp; 48 using ::android::hardware::hidl_string; 49 using ::android::hardware::hidl_vec; 50 using ::android::hardware::Return; 51 using ::android::hardware::Void; 52 #if MAJOR_VERSION <= 6 53 using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation:: 54 AudioDeviceBitfield; 55 #endif 56 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION; 57 using namespace ::android::hardware::audio::effect::CPP_VERSION; 58 59 struct Effect : public IEffect { 60 typedef MessageQueue<Result, kSynchronizedReadWrite> StatusMQ; 61 using GetParameterSuccessCallback = 62 std::function<void(uint32_t valueSize, const void* valueData)>; 63 64 Effect(bool isInput, effect_handle_t handle); 65 66 // Methods from ::android::hardware::audio::effect::CPP_VERSION::IEffect follow. 67 Return<Result> init() override; 68 Return<Result> setConfig( 69 const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider, 70 const sp<IEffectBufferProviderCallback>& outputBufferProvider) override; 71 Return<Result> reset() override; 72 Return<Result> enable() override; 73 Return<Result> disable() override; 74 #if MAJOR_VERSION <= 6 75 Return<Result> setAudioSource(AudioSource source) override; 76 Return<Result> setDevice(AudioDeviceBitfield device) override; 77 Return<Result> setInputDevice(AudioDeviceBitfield device) override; 78 #else 79 Return<Result> setAudioSource(const AudioSource& source) override; 80 Return<Result> setDevice(const DeviceAddress& device) override; 81 Return<Result> setInputDevice(const DeviceAddress& device) override; 82 #endif 83 Return<void> setAndGetVolume(const hidl_vec<uint32_t>& volumes, 84 setAndGetVolume_cb _hidl_cb) override; 85 Return<Result> volumeChangeNotification(const hidl_vec<uint32_t>& volumes) override; 86 Return<Result> setAudioMode(AudioMode mode) override; 87 Return<Result> setConfigReverse( 88 const EffectConfig& config, const sp<IEffectBufferProviderCallback>& inputBufferProvider, 89 const sp<IEffectBufferProviderCallback>& outputBufferProvider) override; 90 Return<void> getConfig(getConfig_cb _hidl_cb) override; 91 Return<void> getConfigReverse(getConfigReverse_cb _hidl_cb) override; 92 Return<void> getSupportedAuxChannelsConfigs( 93 uint32_t maxConfigs, getSupportedAuxChannelsConfigs_cb _hidl_cb) override; 94 Return<void> getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) override; 95 Return<Result> setAuxChannelsConfig(const EffectAuxChannelsConfig& config) 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; 115 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override; 116 117 // Utility methods for extending interfaces. 118 static const char* sContextConversion; 119 120 Result analyzeStatus(const char* funcName, const char* subFuncName, 121 const char* contextDescription, status_t status); 122 template <typename T> getIntegerParamEffect123 Return<void> getIntegerParam(uint32_t paramId, 124 std::function<void(Result retval, T paramValue)> cb) { 125 T value; 126 Result retval = getParameterImpl(sizeof(uint32_t), ¶mId, sizeof(T), 127 [&](uint32_t valueSize, const void* valueData) { 128 if (valueSize > sizeof(T)) valueSize = sizeof(T); 129 memcpy(&value, valueData, valueSize); 130 }); 131 cb(retval, value); 132 return Void(); 133 } 134 135 template <typename T> getParamEffect136 Result getParam(uint32_t paramId, T& paramValue) { 137 return getParameterImpl(sizeof(uint32_t), ¶mId, sizeof(T), 138 [&](uint32_t valueSize, const void* valueData) { 139 if (valueSize > sizeof(T)) valueSize = sizeof(T); 140 memcpy(¶mValue, valueData, valueSize); 141 }); 142 } 143 144 template <typename T> getParamEffect145 Result getParam(uint32_t paramId, uint32_t paramArg, T& paramValue) { 146 uint32_t params[2] = {paramId, paramArg}; 147 return getParameterImpl(sizeof(params), params, sizeof(T), 148 [&](uint32_t valueSize, const void* valueData) { 149 if (valueSize > sizeof(T)) valueSize = sizeof(T); 150 memcpy(¶mValue, valueData, valueSize); 151 }); 152 } 153 154 template <typename T> setParamEffect155 Result setParam(uint32_t paramId, const T& paramValue) { 156 return setParameterImpl(sizeof(uint32_t), ¶mId, sizeof(T), ¶mValue); 157 } 158 159 template <typename T> setParamEffect160 Result setParam(uint32_t paramId, uint32_t paramArg, const T& paramValue) { 161 uint32_t params[2] = {paramId, paramArg}; 162 return setParameterImpl(sizeof(params), params, sizeof(T), ¶mValue); 163 } 164 getParameterImplEffect165 Result getParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize, 166 GetParameterSuccessCallback onSuccess) { 167 return getParameterImpl(paramSize, paramData, valueSize, valueSize, onSuccess); 168 } 169 Result getParameterImpl(uint32_t paramSize, const void* paramData, uint32_t requestValueSize, 170 uint32_t replyValueSize, GetParameterSuccessCallback onSuccess); 171 Result setParameterImpl(uint32_t paramSize, const void* paramData, uint32_t valueSize, 172 const void* valueData); 173 174 // process execution statistics 175 const std::shared_ptr<mediautils::MethodStatistics<std::string>> mStatistics = 176 std::make_shared<mediautils::MethodStatistics<std::string>>(); 177 178 private: 179 friend struct VirtualizerEffect; // for getParameterImpl 180 friend struct VisualizerEffect; // to allow executing commands 181 182 using CommandSuccessCallback = std::function<void()>; 183 using GetConfigCallback = std::function<void(Result retval, const EffectConfig& config)>; 184 using GetCurrentConfigSuccessCallback = std::function<void(void* configData)>; 185 using GetSupportedConfigsSuccessCallback = 186 std::function<void(uint32_t supportedConfigs, void* configsData)>; 187 188 // Sets the limit on the maximum size of vendor-provided data structures. 189 static constexpr size_t kMaxDataSize = 1 << 20; 190 static constexpr effect_handle_t kInvalidEffectHandle = nullptr; 191 192 static const char* sContextResultOfCommand; 193 static const char* sContextCallToCommand; 194 static const char* sContextCallFunction; 195 196 const bool mIsInput; 197 effect_handle_t mHandle; 198 sp<AudioBufferWrapper> mInBuffer; 199 sp<AudioBufferWrapper> mOutBuffer; 200 std::atomic<audio_buffer_t*> mHalInBufferPtr; 201 std::atomic<audio_buffer_t*> mHalOutBufferPtr; 202 std::unique_ptr<StatusMQ> mStatusMQ; 203 EventFlag* mEfGroup; 204 std::atomic<bool> mStopProcessThread; 205 sp<Thread> mProcessThread; 206 207 virtual ~Effect(); 208 209 template <typename T> 210 static size_t alignedSizeIn(size_t s); 211 template <typename T> 212 std::unique_ptr<uint8_t[]> hidlVecToHal(const hidl_vec<T>& vec, uint32_t* halDataSize); 213 std::tuple<Result, effect_handle_t> closeImpl(); 214 void effectAuxChannelsConfigFromHal(const channel_config_t& halConfig, 215 EffectAuxChannelsConfig* config); 216 static void effectAuxChannelsConfigToHal(const EffectAuxChannelsConfig& config, 217 channel_config_t* halConfig); 218 static void effectOffloadParamToHal(const EffectOffloadParameter& offload, 219 effect_offload_param_t* halOffload); 220 static bool parameterToHal(uint32_t paramSize, const void* paramData, uint32_t valueSize, 221 const void** valueData, std::vector<uint8_t>* halParamBuffer); 222 223 Result analyzeCommandStatus(const char* commandName, const char* context, status_t status); 224 Return<void> getConfigImpl(int commandCode, const char* commandName, 225 GetConfigCallback _hidl_cb); 226 Result getCurrentConfigImpl(uint32_t featureId, uint32_t configSize, 227 GetCurrentConfigSuccessCallback onSuccess); 228 Result getSupportedConfigsImpl(uint32_t featureId, uint32_t maxConfigs, uint32_t configSize, 229 GetSupportedConfigsSuccessCallback onSuccess); 230 Result sendCommand(int commandCode, const char* commandName); 231 Result sendCommand(int commandCode, const char* commandName, uint32_t size, void* data); 232 Result sendCommandReturningData(int commandCode, const char* commandName, uint32_t* replySize, 233 void* replyData); 234 Result sendCommandReturningData(int commandCode, const char* commandName, uint32_t size, 235 void* data, uint32_t* replySize, void* replyData); 236 Result sendCommandReturningStatus(int commandCode, const char* commandName); 237 Result sendCommandReturningStatus(int commandCode, const char* commandName, uint32_t size, 238 void* data); 239 Result sendCommandReturningStatusAndData(int commandCode, const char* commandName, 240 uint32_t size, void* data, uint32_t* replySize, 241 void* replyData, uint32_t minReplySize, 242 CommandSuccessCallback onSuccess); 243 Result setConfigImpl(int commandCode, const char* commandName, const EffectConfig& config, 244 const sp<IEffectBufferProviderCallback>& inputBufferProvider, 245 const sp<IEffectBufferProviderCallback>& outputBufferProvider); 246 }; 247 248 } // namespace implementation 249 } // namespace CPP_VERSION 250 } // namespace effect 251 } // namespace audio 252 } // namespace hardware 253 } // namespace android 254 255 #endif // ANDROID_HARDWARE_AUDIO_EFFECT_EFFECT_H 256