xref: /aosp_15_r20/frameworks/av/media/libaudiohal/impl/EffectHalAidl.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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     // for TIME_CHECK
getClassName()66     const std::string getClassName() const { return "EffectHalAidl"; }
67 
68   private:
69     friend class sp<EffectHalAidl>;
70 
71     const std::shared_ptr<::aidl::android::hardware::audio::effect::IFactory> mFactory;
72     const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect> mEffect;
73     const int32_t mSessionId;
74     const int32_t mIoId;
75     const bool mIsProxyEffect;
76     const int32_t mHalVersion;
77     // Audio effect HAL v2+ changes flag to kEventFlagDataMqNotEmpty to avoid conflict from using
78     // kEventFlagNotEmpty
79     const uint32_t mEventFlagDataMqNotEmpty;
80     bool mIsHapticGenerator = false;
81     std::string mEffectName;
82 
83     std::unique_ptr<EffectConversionHelperAidl> mConversion;
84 
85     sp<EffectBufferHalInterface> mInBuffer, mOutBuffer;
86 
87     status_t createAidlConversion(
88             std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect> effect,
89             int32_t sessionId, int32_t ioId,
90             const ::aidl::android::hardware::audio::effect::Descriptor& desc);
91     // Can not be constructed directly by clients.
92     EffectHalAidl(
93             const std::shared_ptr<::aidl::android::hardware::audio::effect::IFactory>& factory,
94             const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& effect,
95             int32_t sessionId, int32_t ioId,
96             const ::aidl::android::hardware::audio::effect::Descriptor& desc,
97             bool isProxyEffect);
98     bool setEffectReverse(bool reverse);
99     bool needUpdateReturnParam(uint32_t cmdCode);
100 
101     status_t maybeReopen(const std::shared_ptr<android::hardware::EventFlag>& efGroup) const;
102     void writeHapticGeneratorData(size_t totalSamples, float* const outputRawBuffer,
103                                   float* const fmqOutputBuffer) const;
104     size_t writeToHalInputFmqAndSignal(
105             const std::shared_ptr<android::hardware::EventFlag>& efGroup) const;
106     status_t waitHalStatusFmq(size_t samplesWritten) const;
107     status_t readFromHalOutputFmq(size_t samplesWritten) const;
108 
109     // The destructor automatically releases the effect.
110     virtual ~EffectHalAidl();
111 };
112 
113 } // namespace effect
114 } // namespace android
115