1 /* 2 * Copyright (c) 2022-2023 Shenzhen Kaihong DID Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef COMPONENT_NODE_H 16 #define COMPONENT_NODE_H 17 #include <OMX_Component.h> 18 #include <OMX_Core.h> 19 #include <OMX_Types.h> 20 #include <map> 21 #include <memory> 22 #include <shared_mutex> 23 #include <nocopyable.h> 24 #include <osal_mem.h> 25 #include <vector> 26 #include "icodec_buffer.h" 27 #include "v4_0/icodec_callback.h" 28 #include "v4_0/icodec_component.h" 29 #include "component_mgr.h" 30 using OHOS::HDI::Codec::V4_0::CompVerInfo; 31 using OHOS::HDI::Codec::V4_0::ICodecCallback; 32 using OHOS::HDI::Codec::V4_0::CodecStateType; 33 namespace OHOS { 34 namespace Codec { 35 namespace Omx { 36 class ComponentNode : NoCopyable { 37 public: 38 ComponentNode(const sptr<ICodecCallback> &callbacks, int64_t appData, std::shared_ptr<ComponentMgr>& mgr); 39 ~ComponentNode(); 40 int32_t OpenHandle(const std::string& name); 41 int32_t CloseHandle(); 42 int32_t GetComponentVersion(CompVerInfo &verInfo); 43 int32_t SendCommand(HDI::Codec::V4_0::CodecCommandType cmd, uint32_t param, int8_t *cmdData); 44 int32_t GetParameter(OMX_INDEXTYPE paramIndex, int8_t *param); 45 int32_t SetParameter(OMX_INDEXTYPE paramIndex, const int8_t *param); 46 int32_t SetParameterWithBuffer(int32_t index, const std::vector<int8_t>& paramStruct, 47 const OmxCodecBuffer& inBuffer); 48 int32_t GetConfig(OMX_INDEXTYPE index, int8_t *config); 49 int32_t SetConfig(OMX_INDEXTYPE index, const int8_t *config); 50 int32_t GetExtensionIndex(const char *parameterName, uint32_t& index); 51 int32_t GetState(HDI::Codec::V4_0::CodecStateType &state); 52 int32_t ComponentTunnelRequest(uint32_t port, int32_t omxHandleTypeTunneledComp, uint32_t tunneledPort, 53 OHOS::HDI::Codec::V4_0::CodecTunnelSetupType &tunnelSetup); 54 int32_t UseBuffer(uint32_t portIndex, OmxCodecBuffer &buffer); 55 int32_t AllocateBuffer(uint32_t portIndex, OmxCodecBuffer &buffer); 56 int32_t FreeBuffer(uint32_t portIndex, const OmxCodecBuffer &buffer); 57 int32_t EmptyThisBuffer(OmxCodecBuffer &buffer); 58 int32_t FillThisBuffer(OmxCodecBuffer &buffer); 59 int32_t SetCallbacks(const sptr<ICodecCallback> &callbacks, int64_t appData); 60 int32_t UseEglImage(struct OmxCodecBuffer &buffer, uint32_t portIndex, const int8_t *eglImage); 61 int32_t ComponentRoleEnum(std::vector<uint8_t> &role, uint32_t index); 62 int32_t ComponentDeInit(); 63 OMX_ERRORTYPE static OnEvent(OMX_HANDLETYPE component, void *appData, OMX_EVENTTYPE event, uint32_t data1, 64 uint32_t data2, void *eventData); 65 OMX_ERRORTYPE static OnEmptyBufferDone(OMX_HANDLETYPE component, void *appData, OMX_BUFFERHEADERTYPE *buffer); 66 OMX_ERRORTYPE static OnFillBufferDone(OMX_HANDLETYPE component, void *appData, OMX_BUFFERHEADERTYPE *buffer); 67 void ReleaseOMXResource(); 68 void GetBuffCount(uint32_t &inputBuffCount, uint32_t &outputBuffCount); 69 70 public: 71 static OMX_CALLBACKTYPE callbacks_; // callbacks 72 73 private: 74 int32_t OnEvent(HDI::Codec::V4_0::CodecEventType event, uint32_t data1, uint32_t data2, void *eventData); 75 int32_t OnEmptyBufferDone(OMX_BUFFERHEADERTYPE *buffer); 76 int32_t OnFillBufferDone(OMX_BUFFERHEADERTYPE *buffer); 77 uint32_t GenerateBufferId(); 78 sptr<ICodecBuffer> GetBufferInfoByHeader(OMX_BUFFERHEADERTYPE *buffer); 79 sptr<ICodecBuffer> GetBufferById(uint32_t bufferId); 80 void WaitStateChange(CodecStateType objState, CodecStateType &status); 81 82 private: 83 struct BufferInfo { 84 uint32_t bufferId; 85 uint32_t portIndex; 86 sptr<ICodecBuffer> icodecBuf; 87 OMX_BUFFERHEADERTYPE *omxHeader; 88 }; 89 90 OMX_HANDLETYPE comp_; // Compnent handle 91 sptr<ICodecCallback> omxCallback_; 92 int64_t appData_; 93 std::shared_mutex poolMutex_; 94 std::vector<BufferInfo> bufferPool_; 95 uint32_t bufferIdCount_; 96 std::shared_ptr<ComponentMgr> mgr_; 97 uint32_t maxStateWaitTime = 10000; 98 uint32_t maxStateWaitCount = 100; 99 std::string compName_; 100 bool isIPCMode_; 101 }; 102 } // namespace Omx 103 } // namespace Codec 104 } // namespace OHOS 105 #endif /* COMPONENT_NODE_H */