Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 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 |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame^] | 18 | #include <Utils.h> |
| 19 | #include <android-base/logging.h> |
| 20 | #include <utils/Log.h> |
| 21 | #include <cstddef> |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 22 | #include <cstdint> |
| 23 | #include <memory> |
| 24 | #include <utility> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <aidl/android/hardware/audio/effect/BnEffect.h> |
| 28 | #include <fmq/AidlMessageQueue.h> |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame^] | 29 | #include "EffectTypes.h" |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 30 | |
| 31 | namespace aidl::android::hardware::audio::effect { |
| 32 | |
| 33 | class EffectContext { |
| 34 | public: |
| 35 | typedef ::android::AidlMessageQueue< |
| 36 | IEffect::Status, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> |
| 37 | StatusMQ; |
| 38 | typedef ::android::AidlMessageQueue< |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame^] | 39 | float, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite> |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 40 | DataMQ; |
| 41 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame^] | 42 | EffectContext(size_t statusDepth, const Parameter::Common& common) { |
| 43 | mSessionId = common.session; |
| 44 | auto& input = common.input; |
| 45 | auto& output = common.output; |
| 46 | |
| 47 | LOG_ALWAYS_FATAL_IF( |
| 48 | input.base.format.pcm != aidl::android::media::audio::common::PcmType::FLOAT_32_BIT, |
| 49 | "inputFormatNotFloat"); |
| 50 | LOG_ALWAYS_FATAL_IF(output.base.format.pcm != |
| 51 | aidl::android::media::audio::common::PcmType::FLOAT_32_BIT, |
| 52 | "outputFormatNotFloat"); |
| 53 | mInputFrameSize = ::android::hardware::audio::common::getFrameSizeInBytes( |
| 54 | input.base.format, input.base.channelMask); |
| 55 | mOutputFrameSize = ::android::hardware::audio::common::getFrameSizeInBytes( |
| 56 | output.base.format, output.base.channelMask); |
| 57 | // in/outBuffer size in float (FMQ data format defined for DataMQ) |
| 58 | size_t inBufferSizeInFloat = input.frameCount * mInputFrameSize / sizeof(float); |
| 59 | size_t outBufferSizeInFloat = output.frameCount * mOutputFrameSize / sizeof(float); |
| 60 | |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 61 | mStatusMQ = std::make_shared<StatusMQ>(statusDepth, true /*configureEventFlagWord*/); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame^] | 62 | mInputMQ = std::make_shared<DataMQ>(inBufferSizeInFloat); |
| 63 | mOutputMQ = std::make_shared<DataMQ>(outBufferSizeInFloat); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 64 | |
| 65 | if (!mStatusMQ->isValid() || !mInputMQ->isValid() || !mOutputMQ->isValid()) { |
| 66 | LOG(ERROR) << __func__ << " created invalid FMQ"; |
| 67 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame^] | 68 | mWorkBuffer.reserve(std::max(inBufferSizeInFloat, outBufferSizeInFloat)); |
| 69 | } |
| 70 | virtual ~EffectContext() {} |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 71 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame^] | 72 | std::shared_ptr<StatusMQ> getStatusFmq() { return mStatusMQ; } |
| 73 | std::shared_ptr<DataMQ> getInputDataFmq() { return mInputMQ; } |
| 74 | std::shared_ptr<DataMQ> getOutputDataFmq() { return mOutputMQ; } |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 75 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame^] | 76 | float* getWorkBuffer() { return static_cast<float*>(mWorkBuffer.data()); } |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 77 | // TODO: update with actual available size |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame^] | 78 | size_t availableToRead() { return mWorkBuffer.capacity(); } |
| 79 | size_t availableToWrite() { return mWorkBuffer.capacity(); } |
| 80 | |
| 81 | // reset buffer status by abandon all data and status in FMQ |
| 82 | void resetBuffer() { |
| 83 | auto buffer = getWorkBuffer(); |
| 84 | std::vector<IEffect::Status> status(mStatusMQ->availableToRead()); |
| 85 | mInputMQ->read(buffer, mInputMQ->availableToRead()); |
| 86 | mOutputMQ->read(buffer, mOutputMQ->availableToRead()); |
| 87 | mStatusMQ->read(status.data(), mStatusMQ->availableToRead()); |
| 88 | } |
| 89 | |
| 90 | void dupeFmq(IEffect::OpenEffectReturn* effectRet) { |
| 91 | if (effectRet) { |
| 92 | effectRet->statusMQ = getStatusFmq()->dupeDesc(); |
| 93 | effectRet->inputDataMQ = getInputDataFmq()->dupeDesc(); |
| 94 | effectRet->outputDataMQ = getOutputDataFmq()->dupeDesc(); |
| 95 | } |
| 96 | } |
| 97 | size_t getInputFrameSize() { return mInputFrameSize; } |
| 98 | size_t getOutputFrameSize() { return mOutputFrameSize; } |
| 99 | int getSessionId() { return mSessionId; } |
| 100 | |
| 101 | virtual RetCode setOutputDevice( |
| 102 | const aidl::android::media::audio::common::AudioDeviceDescription& device) { |
| 103 | mOutputDevice = device; |
| 104 | return RetCode::SUCCESS; |
| 105 | } |
| 106 | virtual aidl::android::media::audio::common::AudioDeviceDescription getOutputDevice() { |
| 107 | return mOutputDevice; |
| 108 | } |
| 109 | |
| 110 | virtual RetCode setAudioMode(const aidl::android::media::audio::common::AudioMode& mode) { |
| 111 | mMode = mode; |
| 112 | return RetCode::SUCCESS; |
| 113 | } |
| 114 | virtual aidl::android::media::audio::common::AudioMode getAudioMode() { return mMode; } |
| 115 | |
| 116 | virtual RetCode setAudioSource(const aidl::android::media::audio::common::AudioSource& source) { |
| 117 | mSource = source; |
| 118 | return RetCode::SUCCESS; |
| 119 | } |
| 120 | virtual aidl::android::media::audio::common::AudioSource getAudioSource() { return mSource; } |
| 121 | |
| 122 | virtual RetCode setVolumeStereo(const Parameter::VolumeStereo& volumeStereo) { |
| 123 | mVolumeStereo = volumeStereo; |
| 124 | return RetCode::SUCCESS; |
| 125 | } |
| 126 | virtual Parameter::VolumeStereo getVolumeStereo() { return mVolumeStereo; } |
| 127 | |
| 128 | virtual RetCode setCommon(const Parameter::Common& common) { |
| 129 | mCommon = common; |
| 130 | LOG(ERROR) << __func__ << mCommon.toString(); |
| 131 | return RetCode::SUCCESS; |
| 132 | } |
| 133 | virtual Parameter::Common getCommon() { |
| 134 | LOG(ERROR) << __func__ << mCommon.toString(); |
| 135 | return mCommon; |
| 136 | } |
| 137 | |
| 138 | protected: |
| 139 | // common parameters |
| 140 | int mSessionId = INVALID_AUDIO_SESSION_ID; |
| 141 | size_t mInputFrameSize, mOutputFrameSize; |
| 142 | Parameter::Common mCommon; |
| 143 | aidl::android::media::audio::common::AudioDeviceDescription mOutputDevice; |
| 144 | aidl::android::media::audio::common::AudioMode mMode; |
| 145 | aidl::android::media::audio::common::AudioSource mSource; |
| 146 | Parameter::VolumeStereo mVolumeStereo; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 147 | |
| 148 | private: |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame^] | 149 | // fmq and buffers |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 150 | std::shared_ptr<StatusMQ> mStatusMQ; |
| 151 | std::shared_ptr<DataMQ> mInputMQ; |
| 152 | std::shared_ptr<DataMQ> mOutputMQ; |
| 153 | // TODO handle effect process input and output |
| 154 | // work buffer set by effect instances, the access and update are in same thread |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame^] | 155 | std::vector<float> mWorkBuffer; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 156 | }; |
| 157 | } // namespace aidl::android::hardware::audio::effect |