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