1 /* 2 * Copyright (C) 2023 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 #pragma once 18 19 #include <memory> 20 21 #include <aidl/android/hardware/audio/effect/IEffect.h> 22 #include <aidl/android/hardware/audio/effect/IFactory.h> 23 #include <fmq/AidlMessageQueue.h> 24 #include <media/audiohal/EffectHalInterface.h> 25 #include <system/audio_effect.h> 26 #include <system/audio_effects/aidl_effects_utils.h> 27 28 #include "EffectConversionHelperAidl.h" 29 30 namespace android { 31 namespace effect { 32 33 class EffectHalAidl : public EffectHalInterface { 34 public: 35 36 // Set the input buffer. 37 status_t setInBuffer(const sp<EffectBufferHalInterface>& buffer) override; 38 39 // Set the output buffer. 40 status_t setOutBuffer(const sp<EffectBufferHalInterface>& buffer) override; 41 42 // Effect process function. 43 status_t process() override; 44 45 // Process reverse stream function. This function is used to pass 46 // a reference stream to the effect engine. 47 status_t processReverse() override; 48 49 // Send a command and receive a response to/from effect engine. 50 status_t command(uint32_t cmdCode, uint32_t cmdSize, void* pCmdData, uint32_t* replySize, 51 void* pReplyData) override; 52 53 // Returns the effect descriptor. 54 status_t getDescriptor(effect_descriptor_t *pDescriptor) override; 55 56 // Free resources on the remote side. 57 status_t close() override; 58 59 status_t dump(int fd) override; 60 getIEffect()61 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect> getIEffect() const { 62 return mEffect; 63 } 64 65 // Set devices in AIDL type 66 status_t setDevices(const AudioDeviceTypeAddrVector& deviceTypes); 67 68 // for TIME_CHECK getClassName()69 const std::string getClassName() const { return "EffectHalAidl"; } 70 71 private: 72 friend class sp<EffectHalAidl>; 73 74 const std::shared_ptr<::aidl::android::hardware::audio::effect::IFactory> mFactory; 75 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect> mEffect; 76 const int32_t mSessionId; 77 const int32_t mIoId; 78 const bool mIsProxyEffect; 79 const int32_t mHalVersion; 80 // Audio effect HAL v2+ changes flag to kEventFlagDataMqNotEmpty to avoid conflict from using 81 // kEventFlagNotEmpty 82 const uint32_t mEventFlagDataMqNotEmpty; 83 bool mIsHapticGenerator = false; 84 std::string mEffectName; 85 86 std::unique_ptr<EffectConversionHelperAidl> mConversion; 87 88 sp<EffectBufferHalInterface> mInBuffer, mOutBuffer; 89 90 status_t createAidlConversion( 91 std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect> effect, 92 int32_t sessionId, int32_t ioId, 93 const ::aidl::android::hardware::audio::effect::Descriptor& desc); 94 // Can not be constructed directly by clients. 95 EffectHalAidl( 96 const std::shared_ptr<::aidl::android::hardware::audio::effect::IFactory>& factory, 97 const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& effect, 98 int32_t sessionId, int32_t ioId, 99 const ::aidl::android::hardware::audio::effect::Descriptor& desc, 100 bool isProxyEffect); 101 bool setEffectReverse(bool reverse); 102 bool needUpdateReturnParam(uint32_t cmdCode); 103 104 status_t maybeReopen(const std::shared_ptr<android::hardware::EventFlag>& efGroup) const; 105 void writeHapticGeneratorData(size_t totalSamples, float* const outputRawBuffer, 106 float* const fmqOutputBuffer) const; 107 size_t writeToHalInputFmqAndSignal( 108 const std::shared_ptr<android::hardware::EventFlag>& efGroup) const; 109 status_t waitHalStatusFmq(size_t samplesWritten) const; 110 status_t readFromHalOutputFmq(size_t samplesWritten) const; 111 112 // The destructor automatically releases the effect. 113 virtual ~EffectHalAidl(); 114 }; 115 116 } // namespace effect 117 } // namespace android 118