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 | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 18 | #include <memory> |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Mikhail Naganov | 872d4a6 | 2023-03-09 18:19:01 -0800 | [diff] [blame] | 21 | #include <Utils.h> |
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) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 40 | auto& input = common.input; |
| 41 | auto& output = common.output; |
| 42 | |
| 43 | LOG_ALWAYS_FATAL_IF( |
| 44 | input.base.format.pcm != aidl::android::media::audio::common::PcmType::FLOAT_32_BIT, |
| 45 | "inputFormatNotFloat"); |
| 46 | LOG_ALWAYS_FATAL_IF(output.base.format.pcm != |
| 47 | aidl::android::media::audio::common::PcmType::FLOAT_32_BIT, |
| 48 | "outputFormatNotFloat"); |
Shraddha Basantwani | a3d4847 | 2024-01-09 10:34:04 +0000 | [diff] [blame] | 49 | |
| 50 | size_t inputChannelCount = |
| 51 | ::aidl::android::hardware::audio::common::getChannelCount(input.base.channelMask); |
| 52 | LOG_ALWAYS_FATAL_IF(inputChannelCount == 0, "inputChannelCountNotValid"); |
| 53 | size_t outputChannelCount = |
| 54 | ::aidl::android::hardware::audio::common::getChannelCount(output.base.channelMask); |
| 55 | LOG_ALWAYS_FATAL_IF(outputChannelCount == 0, "outputChannelCountNotValid"); |
| 56 | |
Mikhail Naganov | 872d4a6 | 2023-03-09 18:19:01 -0800 | [diff] [blame] | 57 | mInputFrameSize = ::aidl::android::hardware::audio::common::getFrameSizeInBytes( |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 58 | input.base.format, input.base.channelMask); |
Mikhail Naganov | 872d4a6 | 2023-03-09 18:19:01 -0800 | [diff] [blame] | 59 | mOutputFrameSize = ::aidl::android::hardware::audio::common::getFrameSizeInBytes( |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 60 | output.base.format, output.base.channelMask); |
| 61 | // in/outBuffer size in float (FMQ data format defined for DataMQ) |
| 62 | size_t inBufferSizeInFloat = input.frameCount * mInputFrameSize / sizeof(float); |
| 63 | size_t outBufferSizeInFloat = output.frameCount * mOutputFrameSize / sizeof(float); |
| 64 | |
Shunkai Yao | 41888a2 | 2023-04-10 17:13:30 +0000 | [diff] [blame] | 65 | // only status FMQ use the EventFlag |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 66 | mStatusMQ = std::make_shared<StatusMQ>(statusDepth, true /*configureEventFlagWord*/); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 67 | mInputMQ = std::make_shared<DataMQ>(inBufferSizeInFloat); |
| 68 | mOutputMQ = std::make_shared<DataMQ>(outBufferSizeInFloat); |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 69 | |
| 70 | if (!mStatusMQ->isValid() || !mInputMQ->isValid() || !mOutputMQ->isValid()) { |
| 71 | LOG(ERROR) << __func__ << " created invalid FMQ"; |
| 72 | } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 73 | mWorkBuffer.reserve(std::max(inBufferSizeInFloat, outBufferSizeInFloat)); |
Shunkai Yao | 1d2d51a | 2023-01-19 16:55:02 +0530 | [diff] [blame] | 74 | mCommon = common; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 75 | } |
| 76 | virtual ~EffectContext() {} |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 77 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 78 | std::shared_ptr<StatusMQ> getStatusFmq() { return mStatusMQ; } |
| 79 | std::shared_ptr<DataMQ> getInputDataFmq() { return mInputMQ; } |
| 80 | std::shared_ptr<DataMQ> getOutputDataFmq() { return mOutputMQ; } |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 81 | |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 82 | float* getWorkBuffer() { return static_cast<float*>(mWorkBuffer.data()); } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 83 | |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 84 | // reset buffer status by abandon input data in FMQ |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 85 | void resetBuffer() { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 86 | auto buffer = static_cast<float*>(mWorkBuffer.data()); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 87 | std::vector<IEffect::Status> status(mStatusMQ->availableToRead()); |
| 88 | mInputMQ->read(buffer, mInputMQ->availableToRead()); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void dupeFmq(IEffect::OpenEffectReturn* effectRet) { |
| 92 | if (effectRet) { |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 93 | effectRet->statusMQ = mStatusMQ->dupeDesc(); |
| 94 | effectRet->inputDataMQ = mInputMQ->dupeDesc(); |
| 95 | effectRet->outputDataMQ = mOutputMQ->dupeDesc(); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | size_t getInputFrameSize() { return mInputFrameSize; } |
| 99 | size_t getOutputFrameSize() { return mOutputFrameSize; } |
Shunkai Yao | 1d2d51a | 2023-01-19 16:55:02 +0530 | [diff] [blame] | 100 | int getSessionId() { return mCommon.session; } |
| 101 | int getIoHandle() { return mCommon.ioHandle; } |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 102 | |
| 103 | virtual RetCode setOutputDevice( |
Shunkai Yao | 5df4e6c | 2023-01-10 03:20:00 +0000 | [diff] [blame] | 104 | const std::vector<aidl::android::media::audio::common::AudioDeviceDescription>& |
| 105 | device) { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 106 | mOutputDevice = device; |
| 107 | return RetCode::SUCCESS; |
| 108 | } |
Shunkai Yao | 1d2d51a | 2023-01-19 16:55:02 +0530 | [diff] [blame] | 109 | |
Shunkai Yao | 5df4e6c | 2023-01-10 03:20:00 +0000 | [diff] [blame] | 110 | virtual std::vector<aidl::android::media::audio::common::AudioDeviceDescription> |
| 111 | getOutputDevice() { |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 112 | return mOutputDevice; |
| 113 | } |
| 114 | |
| 115 | virtual RetCode setAudioMode(const aidl::android::media::audio::common::AudioMode& mode) { |
| 116 | mMode = mode; |
| 117 | return RetCode::SUCCESS; |
| 118 | } |
| 119 | virtual aidl::android::media::audio::common::AudioMode getAudioMode() { return mMode; } |
| 120 | |
| 121 | virtual RetCode setAudioSource(const aidl::android::media::audio::common::AudioSource& source) { |
| 122 | mSource = source; |
| 123 | return RetCode::SUCCESS; |
| 124 | } |
| 125 | virtual aidl::android::media::audio::common::AudioSource getAudioSource() { return mSource; } |
| 126 | |
| 127 | virtual RetCode setVolumeStereo(const Parameter::VolumeStereo& volumeStereo) { |
| 128 | mVolumeStereo = volumeStereo; |
| 129 | return RetCode::SUCCESS; |
| 130 | } |
| 131 | virtual Parameter::VolumeStereo getVolumeStereo() { return mVolumeStereo; } |
| 132 | |
| 133 | virtual RetCode setCommon(const Parameter::Common& common) { |
| 134 | mCommon = common; |
Shunkai Yao | 074cf23 | 2023-07-14 21:08:31 +0000 | [diff] [blame] | 135 | LOG(VERBOSE) << __func__ << mCommon.toString(); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 136 | return RetCode::SUCCESS; |
| 137 | } |
| 138 | virtual Parameter::Common getCommon() { |
Shunkai Yao | 074cf23 | 2023-07-14 21:08:31 +0000 | [diff] [blame] | 139 | LOG(VERBOSE) << __func__ << mCommon.toString(); |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 140 | return mCommon; |
| 141 | } |
| 142 | |
| 143 | protected: |
| 144 | // common parameters |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 145 | size_t mInputFrameSize; |
| 146 | size_t mOutputFrameSize; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 147 | Parameter::Common mCommon; |
Shunkai Yao | 5df4e6c | 2023-01-10 03:20:00 +0000 | [diff] [blame] | 148 | std::vector<aidl::android::media::audio::common::AudioDeviceDescription> mOutputDevice; |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 149 | aidl::android::media::audio::common::AudioMode mMode; |
| 150 | aidl::android::media::audio::common::AudioSource mSource; |
| 151 | Parameter::VolumeStereo mVolumeStereo; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 152 | |
| 153 | private: |
Shunkai Yao | 6afc855 | 2022-10-26 22:47:20 +0000 | [diff] [blame] | 154 | // fmq and buffers |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 155 | std::shared_ptr<StatusMQ> mStatusMQ; |
| 156 | std::shared_ptr<DataMQ> mInputMQ; |
| 157 | std::shared_ptr<DataMQ> mOutputMQ; |
| 158 | // TODO handle effect process input and output |
| 159 | // 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] | 160 | std::vector<float> mWorkBuffer; |
Shunkai Yao | a4ab38c | 2022-10-14 01:07:47 +0000 | [diff] [blame] | 161 | }; |
| 162 | } // namespace aidl::android::hardware::audio::effect |