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