blob: 2ab0ade4dd7d3cbb1f96910e3eb2a3677d54ebe6 [file] [log] [blame]
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +00001/*
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 Yao6afc8552022-10-26 22:47:20 +000018#include <Utils.h>
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000019#include <memory>
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000020#include <vector>
21
Shraddha Basantwanif627d802022-11-08 14:45:07 +053022#include <android-base/logging.h>
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000023#include <fmq/AidlMessageQueue.h>
Shraddha Basantwanif627d802022-11-08 14:45:07 +053024
25#include <aidl/android/hardware/audio/effect/BnEffect.h>
Shunkai Yao6afc8552022-10-26 22:47:20 +000026#include "EffectTypes.h"
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000027
28namespace aidl::android::hardware::audio::effect {
29
30class EffectContext {
31 public:
32 typedef ::android::AidlMessageQueue<
33 IEffect::Status, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>
34 StatusMQ;
35 typedef ::android::AidlMessageQueue<
Shunkai Yao6afc8552022-10-26 22:47:20 +000036 float, ::aidl::android::hardware::common::fmq::SynchronizedReadWrite>
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000037 DataMQ;
38
Shunkai Yao6afc8552022-10-26 22:47:20 +000039 EffectContext(size_t statusDepth, const Parameter::Common& common) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000040 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");
49 mInputFrameSize = ::android::hardware::audio::common::getFrameSizeInBytes(
50 input.base.format, input.base.channelMask);
51 mOutputFrameSize = ::android::hardware::audio::common::getFrameSizeInBytes(
52 output.base.format, output.base.channelMask);
53 // in/outBuffer size in float (FMQ data format defined for DataMQ)
54 size_t inBufferSizeInFloat = input.frameCount * mInputFrameSize / sizeof(float);
55 size_t outBufferSizeInFloat = output.frameCount * mOutputFrameSize / sizeof(float);
56
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000057 mStatusMQ = std::make_shared<StatusMQ>(statusDepth, true /*configureEventFlagWord*/);
Shunkai Yao6afc8552022-10-26 22:47:20 +000058 mInputMQ = std::make_shared<DataMQ>(inBufferSizeInFloat);
59 mOutputMQ = std::make_shared<DataMQ>(outBufferSizeInFloat);
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000060
61 if (!mStatusMQ->isValid() || !mInputMQ->isValid() || !mOutputMQ->isValid()) {
62 LOG(ERROR) << __func__ << " created invalid FMQ";
63 }
Shunkai Yao6afc8552022-10-26 22:47:20 +000064 mWorkBuffer.reserve(std::max(inBufferSizeInFloat, outBufferSizeInFloat));
Shunkai Yao1d2d51a2023-01-19 16:55:02 +053065 mCommon = common;
Shunkai Yao6afc8552022-10-26 22:47:20 +000066 }
67 virtual ~EffectContext() {}
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000068
Shunkai Yao6afc8552022-10-26 22:47:20 +000069 std::shared_ptr<StatusMQ> getStatusFmq() { return mStatusMQ; }
70 std::shared_ptr<DataMQ> getInputDataFmq() { return mInputMQ; }
71 std::shared_ptr<DataMQ> getOutputDataFmq() { return mOutputMQ; }
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +000072
Shunkai Yao6afc8552022-10-26 22:47:20 +000073 float* getWorkBuffer() { return static_cast<float*>(mWorkBuffer.data()); }
Shunkai Yao6afc8552022-10-26 22:47:20 +000074
Shunkai Yaocb0fc412022-12-15 20:34:32 +000075 // reset buffer status by abandon input data in FMQ
Shunkai Yao6afc8552022-10-26 22:47:20 +000076 void resetBuffer() {
Shraddha Basantwanif627d802022-11-08 14:45:07 +053077 auto buffer = static_cast<float*>(mWorkBuffer.data());
Shunkai Yao6afc8552022-10-26 22:47:20 +000078 std::vector<IEffect::Status> status(mStatusMQ->availableToRead());
79 mInputMQ->read(buffer, mInputMQ->availableToRead());
Shunkai Yao6afc8552022-10-26 22:47:20 +000080 }
81
82 void dupeFmq(IEffect::OpenEffectReturn* effectRet) {
83 if (effectRet) {
Shraddha Basantwanif627d802022-11-08 14:45:07 +053084 effectRet->statusMQ = mStatusMQ->dupeDesc();
85 effectRet->inputDataMQ = mInputMQ->dupeDesc();
86 effectRet->outputDataMQ = mOutputMQ->dupeDesc();
Shunkai Yao6afc8552022-10-26 22:47:20 +000087 }
88 }
89 size_t getInputFrameSize() { return mInputFrameSize; }
90 size_t getOutputFrameSize() { return mOutputFrameSize; }
Shunkai Yao1d2d51a2023-01-19 16:55:02 +053091 int getSessionId() { return mCommon.session; }
92 int getIoHandle() { return mCommon.ioHandle; }
Shunkai Yao6afc8552022-10-26 22:47:20 +000093
94 virtual RetCode setOutputDevice(
Shunkai Yao5df4e6c2023-01-10 03:20:00 +000095 const std::vector<aidl::android::media::audio::common::AudioDeviceDescription>&
96 device) {
Shunkai Yao6afc8552022-10-26 22:47:20 +000097 mOutputDevice = device;
98 return RetCode::SUCCESS;
99 }
Shunkai Yao1d2d51a2023-01-19 16:55:02 +0530100
Shunkai Yao5df4e6c2023-01-10 03:20:00 +0000101 virtual std::vector<aidl::android::media::audio::common::AudioDeviceDescription>
102 getOutputDevice() {
Shunkai Yao6afc8552022-10-26 22:47:20 +0000103 return mOutputDevice;
104 }
105
106 virtual RetCode setAudioMode(const aidl::android::media::audio::common::AudioMode& mode) {
107 mMode = mode;
108 return RetCode::SUCCESS;
109 }
110 virtual aidl::android::media::audio::common::AudioMode getAudioMode() { return mMode; }
111
112 virtual RetCode setAudioSource(const aidl::android::media::audio::common::AudioSource& source) {
113 mSource = source;
114 return RetCode::SUCCESS;
115 }
116 virtual aidl::android::media::audio::common::AudioSource getAudioSource() { return mSource; }
117
118 virtual RetCode setVolumeStereo(const Parameter::VolumeStereo& volumeStereo) {
119 mVolumeStereo = volumeStereo;
120 return RetCode::SUCCESS;
121 }
122 virtual Parameter::VolumeStereo getVolumeStereo() { return mVolumeStereo; }
123
124 virtual RetCode setCommon(const Parameter::Common& common) {
125 mCommon = common;
Ram Mohane4064ce2022-12-20 18:05:14 +0530126 LOG(INFO) << __func__ << mCommon.toString();
Shunkai Yao6afc8552022-10-26 22:47:20 +0000127 return RetCode::SUCCESS;
128 }
129 virtual Parameter::Common getCommon() {
Ram Mohane4064ce2022-12-20 18:05:14 +0530130 LOG(INFO) << __func__ << mCommon.toString();
Shunkai Yao6afc8552022-10-26 22:47:20 +0000131 return mCommon;
132 }
133
134 protected:
135 // common parameters
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530136 size_t mInputFrameSize;
137 size_t mOutputFrameSize;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000138 Parameter::Common mCommon;
Shunkai Yao5df4e6c2023-01-10 03:20:00 +0000139 std::vector<aidl::android::media::audio::common::AudioDeviceDescription> mOutputDevice;
Shunkai Yao6afc8552022-10-26 22:47:20 +0000140 aidl::android::media::audio::common::AudioMode mMode;
141 aidl::android::media::audio::common::AudioSource mSource;
142 Parameter::VolumeStereo mVolumeStereo;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000143
144 private:
Shunkai Yao6afc8552022-10-26 22:47:20 +0000145 // fmq and buffers
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000146 std::shared_ptr<StatusMQ> mStatusMQ;
147 std::shared_ptr<DataMQ> mInputMQ;
148 std::shared_ptr<DataMQ> mOutputMQ;
149 // TODO handle effect process input and output
150 // work buffer set by effect instances, the access and update are in same thread
Shunkai Yao6afc8552022-10-26 22:47:20 +0000151 std::vector<float> mWorkBuffer;
Shunkai Yaoa4ab38c2022-10-14 01:07:47 +0000152};
153} // namespace aidl::android::hardware::audio::effect