1 /* 2 * Copyright (C) 2022 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 #include <memory> 19 #include <vector> 20 21 #include <Utils.h> 22 #include <android-base/logging.h> 23 #include <fmq/AidlMessageQueue.h> 24 #include <fmq/EventFlag.h> 25 26 #include <aidl/android/hardware/audio/effect/BnEffect.h> 27 #include "EffectTypes.h" 28 29 namespace aidl::android::hardware::audio::effect { 30 31 class EffectContext { 32 public: 33 typedef ::android::AidlMessageQueue< 34 IEffect::Status, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> 35 StatusMQ; 36 typedef ::android::AidlMessageQueue< 37 float, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> 38 DataMQ; 39 40 EffectContext(size_t statusDepth, const Parameter::Common& common); ~EffectContext()41 virtual ~EffectContext() { 42 if (mEfGroup) { 43 ::android::hardware::EventFlag::deleteEventFlag(&mEfGroup); 44 } 45 } 46 setVersion(int version)47 void setVersion(int version) { mVersion = version; } 48 std::shared_ptr<StatusMQ> getStatusFmq() const; 49 std::shared_ptr<DataMQ> getInputDataFmq() const; 50 std::shared_ptr<DataMQ> getOutputDataFmq() const; 51 52 float* getWorkBuffer(); 53 size_t getWorkBufferSize() const; 54 55 // reset buffer status by abandon input data in FMQ 56 void resetBuffer(); 57 void dupeFmq(IEffect::OpenEffectReturn* effectRet); 58 size_t getInputFrameSize() const; 59 size_t getOutputFrameSize() const; 60 int getSessionId() const; 61 int getIoHandle() const; 62 63 virtual void dupeFmqWithReopen(IEffect::OpenEffectReturn* effectRet); 64 65 virtual RetCode setOutputDevice( 66 const std::vector<aidl::android::media::audio::common::AudioDeviceDescription>& device); 67 68 virtual std::vector<aidl::android::media::audio::common::AudioDeviceDescription> 69 getOutputDevice(); 70 71 virtual RetCode setAudioMode(const aidl::android::media::audio::common::AudioMode& mode); 72 virtual aidl::android::media::audio::common::AudioMode getAudioMode(); 73 74 virtual RetCode setAudioSource(const aidl::android::media::audio::common::AudioSource& source); 75 virtual aidl::android::media::audio::common::AudioSource getAudioSource(); 76 77 virtual RetCode setVolumeStereo(const Parameter::VolumeStereo& volumeStereo); 78 virtual Parameter::VolumeStereo getVolumeStereo(); 79 80 virtual RetCode setCommon(const Parameter::Common& common); 81 virtual Parameter::Common getCommon(); 82 83 virtual ::android::hardware::EventFlag* getStatusEventFlag(); 84 85 virtual RetCode enable(); 86 virtual RetCode disable(); 87 virtual RetCode reset(); 88 89 virtual RetCode startDraining(); 90 virtual RetCode finishDraining(); 91 virtual bool isDraining(); 92 93 protected: 94 bool mIsDraining = false; 95 int mVersion = 0; 96 size_t mInputFrameSize = 0; 97 size_t mOutputFrameSize = 0; 98 size_t mInputChannelCount = 0; 99 size_t mOutputChannelCount = 0; 100 Parameter::Common mCommon = {}; 101 std::vector<aidl::android::media::audio::common::AudioDeviceDescription> mOutputDevice = {}; 102 aidl::android::media::audio::common::AudioMode mMode = 103 aidl::android::media::audio::common::AudioMode::SYS_RESERVED_INVALID; 104 aidl::android::media::audio::common::AudioSource mSource = 105 aidl::android::media::audio::common::AudioSource::SYS_RESERVED_INVALID; 106 Parameter::VolumeStereo mVolumeStereo = {}; 107 RetCode updateIOFrameSize(const Parameter::Common& common); 108 RetCode notifyDataMqUpdate(); 109 110 private: 111 // fmq and buffers 112 std::shared_ptr<StatusMQ> mStatusMQ = nullptr; 113 std::shared_ptr<DataMQ> mInputMQ = nullptr; 114 std::shared_ptr<DataMQ> mOutputMQ = nullptr; 115 // std::shared_ptr<IEffect::OpenEffectReturn> mRet; 116 // work buffer set by effect instances, the access and update are in same thread 117 std::vector<float> mWorkBuffer = {}; 118 119 ::android::hardware::EventFlag* mEfGroup = nullptr; 120 }; 121 } // namespace aidl::android::hardware::audio::effect 122