Shunkai Yao | 284bb0d | 2023-01-10 00:42:36 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2023 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 | |
| 19 | #include <cstddef> |
| 20 | #include <map> |
| 21 | #include <memory> |
| 22 | #include <utils/Errors.h> |
| 23 | |
| 24 | #include <aidl/android/hardware/audio/effect/IEffect.h> |
| 25 | |
| 26 | #include <media/AidlConversionNdk.h> |
| 27 | #include <system/audio_effect.h> |
| 28 | #include <system/audio_effects/effect_aec.h> |
| 29 | #include <system/audio_effects/effect_downmix.h> |
| 30 | #include <system/audio_effects/effect_dynamicsprocessing.h> |
| 31 | #include <system/audio_effects/effect_hapticgenerator.h> |
| 32 | #include <system/audio_effects/effect_ns.h> |
| 33 | #include <system/audio_effects/effect_visualizer.h> |
| 34 | |
| 35 | namespace android { |
| 36 | namespace effect { |
| 37 | |
| 38 | static const size_t kEffectParamSize = sizeof(effect_param_t); |
| 39 | static const size_t kEffectConfigSize = sizeof(effect_config_t); |
| 40 | |
| 41 | class EffectConversionHelperAidl { |
| 42 | protected: |
| 43 | EffectConversionHelperAidl( |
| 44 | std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect> effect, |
| 45 | int32_t sessionId, int32_t ioId, ::aidl::android::media::audio::common::AudioUuid uuid) |
| 46 | : mSessionId(sessionId), |
| 47 | mIoId(ioId), |
| 48 | mTypeUuid(std::move(uuid)), |
| 49 | mEffect(std::move(effect)) {} |
| 50 | |
| 51 | status_t handleCommand(uint32_t cmdCode, uint32_t cmdSize, void* pCmdData, uint32_t* replySize, |
| 52 | void* pReplyData); |
| 53 | |
| 54 | private: |
| 55 | const int32_t mSessionId; |
| 56 | const int32_t mIoId; |
| 57 | ::aidl::android::media::audio::common::AudioUuid mTypeUuid; |
| 58 | const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect> mEffect; |
| 59 | |
| 60 | // command handler map |
| 61 | typedef status_t (EffectConversionHelperAidl::*CommandHandler)(uint32_t /* cmdSize */, |
| 62 | const void* /* pCmdData */, |
| 63 | uint32_t* /* replySize */, |
| 64 | void* /* pReplyData */); |
| 65 | static const std::map<uint32_t /* effect_command_e */, CommandHandler> mCommandHandlerMap; |
| 66 | |
| 67 | // parameter set/get handler map |
| 68 | typedef status_t (EffectConversionHelperAidl::*SetParameter)(const effect_param_t& param); |
| 69 | typedef status_t (EffectConversionHelperAidl::*GetParameter)(effect_param_t& param); |
| 70 | static const std::map<::aidl::android::media::audio::common::AudioUuid /* TypeUUID */, |
| 71 | std::pair<SetParameter, GetParameter>> |
| 72 | mParameterHandlerMap; |
| 73 | |
| 74 | // align to 32 bit boundary |
| 75 | static constexpr size_t padding(size_t size) { |
| 76 | return ((size - 1) / sizeof(int) + 1) * sizeof(int); |
| 77 | } |
| 78 | static constexpr bool validatePVsize(const effect_param_t& param, size_t p, size_t v) { |
| 79 | return padding(param.psize) == p && param.vsize == v; |
| 80 | } |
| 81 | static constexpr bool validateCommandSize(const effect_param_t& param, size_t size) { |
| 82 | return padding(param.psize) + param.vsize + kEffectParamSize <= size; |
| 83 | } |
| 84 | |
| 85 | status_t handleInit(uint32_t cmdSize, const void* pCmdData, uint32_t* replySize, |
| 86 | void* pReplyData); |
| 87 | status_t handleSetParameter(uint32_t cmdSize, const void* pCmdData, uint32_t* replySize, |
| 88 | void* pReplyData); |
| 89 | status_t handleGetParameter(uint32_t cmdSize, const void* pCmdData, uint32_t* replySize, |
| 90 | void* pReplyData); |
| 91 | status_t handleSetConfig(uint32_t cmdSize, const void* pCmdData, uint32_t* replySize, |
| 92 | void* pReplyData); |
| 93 | status_t handleGetConfig(uint32_t cmdSize, const void* pCmdData, uint32_t* replySize, |
| 94 | void* pReplyData); |
| 95 | status_t handleEnable(uint32_t cmdSize, const void* pCmdData, uint32_t* replySize, |
| 96 | void* pReplyData); |
| 97 | status_t handleDisable(uint32_t cmdSize, const void* pCmdData, uint32_t* replySize, |
| 98 | void* pReplyData); |
| 99 | status_t handleReset(uint32_t cmdSize, const void* pCmdData, uint32_t* replySize, |
| 100 | void* pReplyData); |
| 101 | status_t handleSetDevice(uint32_t cmdSize, const void* pCmdData, uint32_t* replySize, |
| 102 | void* pReplyData); |
| 103 | status_t handleSetVolume(uint32_t cmdSize, const void* pCmdData, uint32_t* replySize, |
| 104 | void* pReplyData); |
| 105 | status_t handleSetOffload(uint32_t cmdSize, const void* pCmdData, uint32_t* replySize, |
| 106 | void* pReplyData); |
| 107 | status_t handleFirstPriority(uint32_t cmdSize, const void* pCmdData, uint32_t* replySize, |
| 108 | void* pReplyData); |
| 109 | |
| 110 | // AEC parameter handler |
| 111 | status_t setAecParameter(const effect_param_t& param); |
| 112 | status_t getAecParameter(effect_param_t& param); |
| 113 | }; |
| 114 | |
| 115 | } // namespace effect |
| 116 | } // namespace android |