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 <string>
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_ci#include "icodec_buffer.h"
28094332d3Sopenharmony_ci#include "codec_callback_if.h"
29094332d3Sopenharmony_ci#include "codec_component_type.h"
30094332d3Sopenharmony_ci
31094332d3Sopenharmony_cinamespace OHOS {
32094332d3Sopenharmony_cinamespace Codec {
33094332d3Sopenharmony_cinamespace Omx {
34094332d3Sopenharmony_ciclass ComponentNode : NoCopyable {
35094332d3Sopenharmony_cipublic:
36094332d3Sopenharmony_ci    ComponentNode(struct CodecCallbackType *callback, int64_t appData, const std::string &compName);
37094332d3Sopenharmony_ci
38094332d3Sopenharmony_ci    ~ComponentNode() override;
39094332d3Sopenharmony_ci
40094332d3Sopenharmony_ci    int32_t GetComponentVersion(struct CompVerInfo &verInfo);
41094332d3Sopenharmony_ci
42094332d3Sopenharmony_ci    int32_t SendCommand(OMX_COMMANDTYPE cmd, uint32_t param, int8_t *cmdData, uint32_t cmdDataLen);
43094332d3Sopenharmony_ci
44094332d3Sopenharmony_ci    int32_t GetParameter(OMX_INDEXTYPE paramIndex, int8_t *param, uint32_t paramLen);
45094332d3Sopenharmony_ci
46094332d3Sopenharmony_ci    int32_t SetParameter(OMX_INDEXTYPE paramIndex, int8_t *param, uint32_t paramLen);
47094332d3Sopenharmony_ci
48094332d3Sopenharmony_ci    int32_t GetConfig(OMX_INDEXTYPE index, int8_t *config, uint32_t configLen);
49094332d3Sopenharmony_ci
50094332d3Sopenharmony_ci    int32_t SetConfig(OMX_INDEXTYPE index, int8_t *config, uint32_t configLen);
51094332d3Sopenharmony_ci
52094332d3Sopenharmony_ci    int32_t GetExtensionIndex(const char *parameterName, OMX_INDEXTYPE *indexType);
53094332d3Sopenharmony_ci
54094332d3Sopenharmony_ci    int32_t GetState(OMX_STATETYPE *state);
55094332d3Sopenharmony_ci
56094332d3Sopenharmony_ci    int32_t ComponentTunnelRequest(uint32_t port, int32_t omxHandleTypeTunneledComp, uint32_t tunneledPort,
57094332d3Sopenharmony_ci                                   struct OMX_TUNNELSETUPTYPE *tunnelSetup);
58094332d3Sopenharmony_ci
59094332d3Sopenharmony_ci    int32_t UseBuffer(uint32_t portIndex, struct OmxCodecBuffer &buffer);
60094332d3Sopenharmony_ci
61094332d3Sopenharmony_ci    int32_t AllocateBuffer(uint32_t portIndex, struct OmxCodecBuffer &buffer);
62094332d3Sopenharmony_ci
63094332d3Sopenharmony_ci    int32_t FreeBuffer(uint32_t portIndex, struct OmxCodecBuffer &buffer);
64094332d3Sopenharmony_ci
65094332d3Sopenharmony_ci    int32_t EmptyThisBuffer(struct OmxCodecBuffer &buffer);
66094332d3Sopenharmony_ci
67094332d3Sopenharmony_ci    int32_t FillThisBuffer(struct OmxCodecBuffer &buffer);
68094332d3Sopenharmony_ci
69094332d3Sopenharmony_ci    int32_t SetCallbacks(struct CodecCallbackType *omxCallback, int64_t appData);
70094332d3Sopenharmony_ci
71094332d3Sopenharmony_ci    int32_t UseEglImage(struct OmxCodecBuffer &buffer, uint32_t portIndex, int8_t *eglImage, uint32_t eglImageLen);
72094332d3Sopenharmony_ci
73094332d3Sopenharmony_ci    int32_t ComponentRoleEnum(uint8_t *role, uint32_t roleLen, uint32_t index);
74094332d3Sopenharmony_ci
75094332d3Sopenharmony_ci    int32_t DeInit();
76094332d3Sopenharmony_ci
77094332d3Sopenharmony_ci    static OMX_ERRORTYPE OnEvent(OMX_HANDLETYPE component, void *appData, OMX_EVENTTYPE event, uint32_t data1,
78094332d3Sopenharmony_ci                                 uint32_t data2, void *eventData);
79094332d3Sopenharmony_ci
80094332d3Sopenharmony_ci    static OMX_ERRORTYPE OnEmptyBufferDone(OMX_HANDLETYPE component, void *appData, OMX_BUFFERHEADERTYPE *buffer);
81094332d3Sopenharmony_ci
82094332d3Sopenharmony_ci    static OMX_ERRORTYPE OnFillBufferDone(OMX_HANDLETYPE component, void *appData, OMX_BUFFERHEADERTYPE *buffer);
83094332d3Sopenharmony_ci
84094332d3Sopenharmony_ci    void SetHandle(OMX_HANDLETYPE comp)
85094332d3Sopenharmony_ci    {
86094332d3Sopenharmony_ci        this->comp_ = comp;
87094332d3Sopenharmony_ci    }
88094332d3Sopenharmony_ci
89094332d3Sopenharmony_ci    OMX_HANDLETYPE GetHandle()
90094332d3Sopenharmony_ci    {
91094332d3Sopenharmony_ci        return comp_;
92094332d3Sopenharmony_ci    }
93094332d3Sopenharmony_ci
94094332d3Sopenharmony_ci    uint32_t GetBufferCount()
95094332d3Sopenharmony_ci    {
96094332d3Sopenharmony_ci        return codecBufferMap_.size();
97094332d3Sopenharmony_ci    }
98094332d3Sopenharmony_ci
99094332d3Sopenharmony_ci    std::string GetCompName()
100094332d3Sopenharmony_ci    {
101094332d3Sopenharmony_ci        return name_;
102094332d3Sopenharmony_ci    }
103094332d3Sopenharmony_ci
104094332d3Sopenharmony_ci    void WaitStateChange(uint32_t objState, OMX_STATETYPE *status);
105094332d3Sopenharmony_ci
106094332d3Sopenharmony_ci    void ReleaseOMXResource();
107094332d3Sopenharmony_ci
108094332d3Sopenharmony_ci    int32_t ReleaseAllBuffer();
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_cipublic:
111094332d3Sopenharmony_ci    static OMX_CALLBACKTYPE callbacks_;  // callbacks
112094332d3Sopenharmony_ci
113094332d3Sopenharmony_ciprivate:
114094332d3Sopenharmony_ci    int32_t OnEvent(OMX_EVENTTYPE event, uint32_t data1, uint32_t data2, void *eventData);
115094332d3Sopenharmony_ci
116094332d3Sopenharmony_ci    int32_t OnEmptyBufferDone(OMX_BUFFERHEADERTYPE *buffer);
117094332d3Sopenharmony_ci
118094332d3Sopenharmony_ci    int32_t OnFillBufferDone(OMX_BUFFERHEADERTYPE *buffer);
119094332d3Sopenharmony_ci
120094332d3Sopenharmony_ci    uint32_t GenerateBufferId();
121094332d3Sopenharmony_ci    sptr<ICodecBuffer> GetBufferInfoByHeader(OMX_BUFFERHEADERTYPE *buffer);
122094332d3Sopenharmony_ci    bool GetBufferById(uint32_t bufferId, sptr<ICodecBuffer> &codecBuffer, OMX_BUFFERHEADERTYPE *&bufferHdrType);
123094332d3Sopenharmony_ci
124094332d3Sopenharmony_ciprivate:
125094332d3Sopenharmony_ci    OMX_HANDLETYPE comp_;                                         // Component handle
126094332d3Sopenharmony_ci    struct CodecCallbackType *omxCallback_;                       // Callbacks in HDI
127094332d3Sopenharmony_ci    int64_t appData_;                                             // Use data, default is 0
128094332d3Sopenharmony_ci    std::map<uint32_t, sptr<ICodecBuffer>> codecBufferMap_;       // Key is buffferID
129094332d3Sopenharmony_ci    std::map<OMX_BUFFERHEADERTYPE *, uint32_t> bufferHeaderMap_;  // Key is omx buffer header type
130094332d3Sopenharmony_ci    uint32_t bufferIdCount_;
131094332d3Sopenharmony_ci    std::string name_;
132094332d3Sopenharmony_ci    std::map<OMX_BUFFERHEADERTYPE *, uint32_t> bufferHeaderPortMap_;
133094332d3Sopenharmony_ci    uint32_t maxStateWaitTime = 10000;
134094332d3Sopenharmony_ci    uint32_t maxStateWaitCount = 100;
135094332d3Sopenharmony_ci    std::shared_mutex mapMutex_;
136094332d3Sopenharmony_ci    std::shared_mutex callbackMutex_;
137094332d3Sopenharmony_ci};
138094332d3Sopenharmony_ci}  // namespace Omx
139094332d3Sopenharmony_ci}  // namespace Codec
140094332d3Sopenharmony_ci}  // namespace OHOS
141094332d3Sopenharmony_ci#endif /* COMPONENT_NODE_H */