1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2022-2023 Shenzhen Kaihong DID Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci * 		http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci#ifndef COMPONENT_NODE_H
16094332d3Sopenharmony_ci#define COMPONENT_NODE_H
17094332d3Sopenharmony_ci#include <OMX_Component.h>
18094332d3Sopenharmony_ci#include <OMX_Core.h>
19094332d3Sopenharmony_ci#include <OMX_Types.h>
20094332d3Sopenharmony_ci#include <map>
21094332d3Sopenharmony_ci#include <memory>
22094332d3Sopenharmony_ci#include <shared_mutex>
23094332d3Sopenharmony_ci#include <nocopyable.h>
24094332d3Sopenharmony_ci#include <osal_mem.h>
25094332d3Sopenharmony_ci#include <vector>
26094332d3Sopenharmony_ci#include "icodec_buffer.h"
27094332d3Sopenharmony_ci#include "v3_0/icodec_callback.h"
28094332d3Sopenharmony_ci#include "v3_0/icodec_component.h"
29094332d3Sopenharmony_ci#include "component_mgr.h"
30094332d3Sopenharmony_ciusing OHOS::HDI::Codec::V3_0::CompVerInfo;
31094332d3Sopenharmony_ciusing OHOS::HDI::Codec::V3_0::ICodecCallback;
32094332d3Sopenharmony_ciusing OHOS::HDI::Codec::V3_0::OmxCodecBuffer;
33094332d3Sopenharmony_ciusing OHOS::HDI::Codec::V3_0::CodecStateType;
34094332d3Sopenharmony_cinamespace OHOS {
35094332d3Sopenharmony_cinamespace Codec {
36094332d3Sopenharmony_cinamespace Omx {
37094332d3Sopenharmony_ciclass ComponentNode : NoCopyable {
38094332d3Sopenharmony_cipublic:
39094332d3Sopenharmony_ci    ComponentNode(const sptr<ICodecCallback> &callbacks, int64_t appData, std::shared_ptr<ComponentMgr>& mgr);
40094332d3Sopenharmony_ci    ~ComponentNode();
41094332d3Sopenharmony_ci    int32_t OpenHandle(const std::string& name);
42094332d3Sopenharmony_ci    int32_t CloseHandle();
43094332d3Sopenharmony_ci    int32_t GetComponentVersion(CompVerInfo &verInfo);
44094332d3Sopenharmony_ci    int32_t SendCommand(HDI::Codec::V3_0::CodecCommandType cmd, uint32_t param, int8_t *cmdData);
45094332d3Sopenharmony_ci    int32_t GetParameter(OMX_INDEXTYPE paramIndex, int8_t *param);
46094332d3Sopenharmony_ci    int32_t SetParameter(OMX_INDEXTYPE paramIndex, const int8_t *param);
47094332d3Sopenharmony_ci    int32_t SetParameterWithBuffer(int32_t index, const std::vector<int8_t>& paramStruct,
48094332d3Sopenharmony_ci            const OmxCodecBuffer& inBuffer);
49094332d3Sopenharmony_ci    int32_t GetConfig(OMX_INDEXTYPE index, int8_t *config);
50094332d3Sopenharmony_ci    int32_t SetConfig(OMX_INDEXTYPE index, const int8_t *config);
51094332d3Sopenharmony_ci    int32_t GetExtensionIndex(const char *parameterName, uint32_t& index);
52094332d3Sopenharmony_ci    int32_t GetState(HDI::Codec::V3_0::CodecStateType &state);
53094332d3Sopenharmony_ci    int32_t ComponentTunnelRequest(uint32_t port, int32_t omxHandleTypeTunneledComp, uint32_t tunneledPort,
54094332d3Sopenharmony_ci                                   OHOS::HDI::Codec::V3_0::CodecTunnelSetupType &tunnelSetup);
55094332d3Sopenharmony_ci    int32_t UseBuffer(uint32_t portIndex, OmxCodecBuffer &buffer);
56094332d3Sopenharmony_ci    int32_t AllocateBuffer(uint32_t portIndex, OmxCodecBuffer &buffer);
57094332d3Sopenharmony_ci    int32_t FreeBuffer(uint32_t portIndex, const OmxCodecBuffer &buffer);
58094332d3Sopenharmony_ci    int32_t EmptyThisBuffer(OmxCodecBuffer &buffer);
59094332d3Sopenharmony_ci    int32_t FillThisBuffer(OmxCodecBuffer &buffer);
60094332d3Sopenharmony_ci    int32_t SetCallbacks(const sptr<ICodecCallback> &callbacks, int64_t appData);
61094332d3Sopenharmony_ci    int32_t UseEglImage(struct OmxCodecBuffer &buffer, uint32_t portIndex, const int8_t *eglImage);
62094332d3Sopenharmony_ci    int32_t ComponentRoleEnum(std::vector<uint8_t> &role, uint32_t index);
63094332d3Sopenharmony_ci    int32_t ComponentDeInit();
64094332d3Sopenharmony_ci    OMX_ERRORTYPE static OnEvent(OMX_HANDLETYPE component, void *appData, OMX_EVENTTYPE event, uint32_t data1,
65094332d3Sopenharmony_ci                                 uint32_t data2, void *eventData);
66094332d3Sopenharmony_ci    OMX_ERRORTYPE static OnEmptyBufferDone(OMX_HANDLETYPE component, void *appData, OMX_BUFFERHEADERTYPE *buffer);
67094332d3Sopenharmony_ci    OMX_ERRORTYPE static OnFillBufferDone(OMX_HANDLETYPE component, void *appData, OMX_BUFFERHEADERTYPE *buffer);
68094332d3Sopenharmony_ci    void ReleaseOMXResource();
69094332d3Sopenharmony_ci    void GetBuffCount(uint32_t &inputBuffCount, uint32_t &outputBuffCount);
70094332d3Sopenharmony_ci
71094332d3Sopenharmony_cipublic:
72094332d3Sopenharmony_ci    static OMX_CALLBACKTYPE callbacks_;  // callbacks
73094332d3Sopenharmony_ci
74094332d3Sopenharmony_ciprivate:
75094332d3Sopenharmony_ci    int32_t OnEvent(HDI::Codec::V3_0::CodecEventType event, uint32_t data1, uint32_t data2, void *eventData);
76094332d3Sopenharmony_ci    int32_t OnEmptyBufferDone(OMX_BUFFERHEADERTYPE *buffer);
77094332d3Sopenharmony_ci    int32_t OnFillBufferDone(OMX_BUFFERHEADERTYPE *buffer);
78094332d3Sopenharmony_ci    int32_t UseBufferByType(uint32_t portIndex, OmxCodecBuffer &buffer,
79094332d3Sopenharmony_ci        sptr<ICodecBuffer> codecBuffer, OMX_BUFFERHEADERTYPE *&bufferHdrType);
80094332d3Sopenharmony_ci    uint32_t GenerateBufferId();
81094332d3Sopenharmony_ci    sptr<ICodecBuffer> GetBufferInfoByHeader(OMX_BUFFERHEADERTYPE *buffer);
82094332d3Sopenharmony_ci    bool GetBufferById(uint32_t bufferId, sptr<ICodecBuffer> &codecBuffer, OMX_BUFFERHEADERTYPE *&bufferHdrType);
83094332d3Sopenharmony_ci    void ReleaseCodecBuffer(struct OmxCodecBuffer &buffer);
84094332d3Sopenharmony_ci    void WaitStateChange(CodecStateType objState, CodecStateType &status);
85094332d3Sopenharmony_ci    int32_t ReleaseAllBuffer();
86094332d3Sopenharmony_ciprivate:
87094332d3Sopenharmony_ci    OMX_HANDLETYPE comp_;  // Compnent handle
88094332d3Sopenharmony_ci    sptr<ICodecCallback> omxCallback_;
89094332d3Sopenharmony_ci    int64_t appData_;
90094332d3Sopenharmony_ci    std::map<uint32_t, sptr<ICodecBuffer>> codecBufferMap_;       // Key is buffferID
91094332d3Sopenharmony_ci    std::map<OMX_BUFFERHEADERTYPE *, uint32_t> portIndexMap_;
92094332d3Sopenharmony_ci    std::map<OMX_BUFFERHEADERTYPE *, uint32_t> bufferHeaderMap_;  // Key is omx buffer header type
93094332d3Sopenharmony_ci    std::map<OMX_BUFFERHEADERTYPE *, uint32_t> bufferHeaderPortMap_;
94094332d3Sopenharmony_ci    std::vector<std::pair<void *, uint32_t>> audioBuffer_;
95094332d3Sopenharmony_ci    uint32_t bufferIdCount_;
96094332d3Sopenharmony_ci    std::shared_ptr<ComponentMgr> mgr_;
97094332d3Sopenharmony_ci    uint32_t maxStateWaitTime = 10000;
98094332d3Sopenharmony_ci    uint32_t maxStateWaitCount = 100;
99094332d3Sopenharmony_ci    std::shared_mutex mapMutex_;
100094332d3Sopenharmony_ci    std::string compName_;
101094332d3Sopenharmony_ci};
102094332d3Sopenharmony_ci}  // namespace Omx
103094332d3Sopenharmony_ci}  // namespace Codec
104094332d3Sopenharmony_ci}  // namespace OHOS
105094332d3Sopenharmony_ci#endif /* COMPONENT_NODE_H */