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 protected: 86 int mVersion = 0; 87 size_t mInputFrameSize = 0; 88 size_t mOutputFrameSize = 0; 89 size_t mInputChannelCount = 0; 90 size_t mOutputChannelCount = 0; 91 Parameter::Common mCommon = {}; 92 std::vector<aidl::android::media::audio::common::AudioDeviceDescription> mOutputDevice = {}; 93 aidl::android::media::audio::common::AudioMode mMode = 94 aidl::android::media::audio::common::AudioMode::SYS_RESERVED_INVALID; 95 aidl::android::media::audio::common::AudioSource mSource = 96 aidl::android::media::audio::common::AudioSource::SYS_RESERVED_INVALID; 97 Parameter::VolumeStereo mVolumeStereo = {}; 98 RetCode updateIOFrameSize(const Parameter::Common& common); 99 RetCode notifyDataMqUpdate(); 100 101 private: 102 // fmq and buffers 103 std::shared_ptr<StatusMQ> mStatusMQ = nullptr; 104 std::shared_ptr<DataMQ> mInputMQ = nullptr; 105 std::shared_ptr<DataMQ> mOutputMQ = nullptr; 106 // std::shared_ptr<IEffect::OpenEffectReturn> mRet; 107 // work buffer set by effect instances, the access and update are in same thread 108 std::vector<float> mWorkBuffer = {}; 109 110 ::android::hardware::EventFlag* mEfGroup = nullptr; 111 }; 112 } // namespace aidl::android::hardware::audio::effect 113