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 "v3_0/icodec_callback.h"
28 #include "v3_0/icodec_component.h"
29 #include "component_mgr.h"
30 using OHOS::HDI::Codec::V3_0::CompVerInfo;
31 using OHOS::HDI::Codec::V3_0::ICodecCallback;
32 using OHOS::HDI::Codec::V3_0::OmxCodecBuffer;
33 using OHOS::HDI::Codec::V3_0::CodecStateType;
34 namespace OHOS {
35 namespace Codec {
36 namespace Omx {
37 class ComponentNode : NoCopyable {
38 public:
39     ComponentNode(const sptr<ICodecCallback> &callbacks, int64_t appData, std::shared_ptr<ComponentMgr>& mgr);
40     ~ComponentNode();
41     int32_t OpenHandle(const std::string& name);
42     int32_t CloseHandle();
43     int32_t GetComponentVersion(CompVerInfo &verInfo);
44     int32_t SendCommand(HDI::Codec::V3_0::CodecCommandType cmd, uint32_t param, int8_t *cmdData);
45     int32_t GetParameter(OMX_INDEXTYPE paramIndex, int8_t *param);
46     int32_t SetParameter(OMX_INDEXTYPE paramIndex, const int8_t *param);
47     int32_t SetParameterWithBuffer(int32_t index, const std::vector<int8_t>& paramStruct,
48             const OmxCodecBuffer& inBuffer);
49     int32_t GetConfig(OMX_INDEXTYPE index, int8_t *config);
50     int32_t SetConfig(OMX_INDEXTYPE index, const int8_t *config);
51     int32_t GetExtensionIndex(const char *parameterName, uint32_t& index);
52     int32_t GetState(HDI::Codec::V3_0::CodecStateType &state);
53     int32_t ComponentTunnelRequest(uint32_t port, int32_t omxHandleTypeTunneledComp, uint32_t tunneledPort,
54                                    OHOS::HDI::Codec::V3_0::CodecTunnelSetupType &tunnelSetup);
55     int32_t UseBuffer(uint32_t portIndex, OmxCodecBuffer &buffer);
56     int32_t AllocateBuffer(uint32_t portIndex, OmxCodecBuffer &buffer);
57     int32_t FreeBuffer(uint32_t portIndex, const OmxCodecBuffer &buffer);
58     int32_t EmptyThisBuffer(OmxCodecBuffer &buffer);
59     int32_t FillThisBuffer(OmxCodecBuffer &buffer);
60     int32_t SetCallbacks(const sptr<ICodecCallback> &callbacks, int64_t appData);
61     int32_t UseEglImage(struct OmxCodecBuffer &buffer, uint32_t portIndex, const int8_t *eglImage);
62     int32_t ComponentRoleEnum(std::vector<uint8_t> &role, uint32_t index);
63     int32_t ComponentDeInit();
64     OMX_ERRORTYPE static OnEvent(OMX_HANDLETYPE component, void *appData, OMX_EVENTTYPE event, uint32_t data1,
65                                  uint32_t data2, void *eventData);
66     OMX_ERRORTYPE static OnEmptyBufferDone(OMX_HANDLETYPE component, void *appData, OMX_BUFFERHEADERTYPE *buffer);
67     OMX_ERRORTYPE static OnFillBufferDone(OMX_HANDLETYPE component, void *appData, OMX_BUFFERHEADERTYPE *buffer);
68     void ReleaseOMXResource();
69     void GetBuffCount(uint32_t &inputBuffCount, uint32_t &outputBuffCount);
70 
71 public:
72     static OMX_CALLBACKTYPE callbacks_;  // callbacks
73 
74 private:
75     int32_t OnEvent(HDI::Codec::V3_0::CodecEventType event, uint32_t data1, uint32_t data2, void *eventData);
76     int32_t OnEmptyBufferDone(OMX_BUFFERHEADERTYPE *buffer);
77     int32_t OnFillBufferDone(OMX_BUFFERHEADERTYPE *buffer);
78     int32_t UseBufferByType(uint32_t portIndex, OmxCodecBuffer &buffer,
79         sptr<ICodecBuffer> codecBuffer, OMX_BUFFERHEADERTYPE *&bufferHdrType);
80     uint32_t GenerateBufferId();
81     sptr<ICodecBuffer> GetBufferInfoByHeader(OMX_BUFFERHEADERTYPE *buffer);
82     bool GetBufferById(uint32_t bufferId, sptr<ICodecBuffer> &codecBuffer, OMX_BUFFERHEADERTYPE *&bufferHdrType);
83     void ReleaseCodecBuffer(struct OmxCodecBuffer &buffer);
84     void WaitStateChange(CodecStateType objState, CodecStateType &status);
85     int32_t ReleaseAllBuffer();
86 private:
87     OMX_HANDLETYPE comp_;  // Compnent handle
88     sptr<ICodecCallback> omxCallback_;
89     int64_t appData_;
90     std::map<uint32_t, sptr<ICodecBuffer>> codecBufferMap_;       // Key is buffferID
91     std::map<OMX_BUFFERHEADERTYPE *, uint32_t> portIndexMap_;
92     std::map<OMX_BUFFERHEADERTYPE *, uint32_t> bufferHeaderMap_;  // Key is omx buffer header type
93     std::map<OMX_BUFFERHEADERTYPE *, uint32_t> bufferHeaderPortMap_;
94     uint32_t bufferIdCount_;
95     std::shared_ptr<ComponentMgr> mgr_;
96     uint32_t maxStateWaitTime = 10000;
97     uint32_t maxStateWaitCount = 100;
98     std::shared_mutex mapMutex_;
99     std::string compName_;
100 };
101 }  // namespace Omx
102 }  // namespace Codec
103 }  // namespace OHOS
104 #endif /* COMPONENT_NODE_H */