• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
26 
27 #include "icodec_buffer.h"
28 #include "codec_callback_if.h"
29 #include "codec_component_type.h"
30 
31 namespace OHOS {
32 namespace Codec {
33 namespace Omx {
34 class ComponentNode : NoCopyable {
35 public:
36     ComponentNode(struct CodecCallbackType *callback, int64_t appData, const std::string &compName);
37 
38     ~ComponentNode() override;
39 
40     int32_t GetComponentVersion(struct CompVerInfo &verInfo);
41 
42     int32_t SendCommand(OMX_COMMANDTYPE cmd, uint32_t param, int8_t *cmdData, uint32_t cmdDataLen);
43 
44     int32_t GetParameter(OMX_INDEXTYPE paramIndex, int8_t *param, uint32_t paramLen);
45 
46     int32_t SetParameter(OMX_INDEXTYPE paramIndex, int8_t *param, uint32_t paramLen);
47 
48     int32_t GetConfig(OMX_INDEXTYPE index, int8_t *config, uint32_t configLen);
49 
50     int32_t SetConfig(OMX_INDEXTYPE index, int8_t *config, uint32_t configLen);
51 
52     int32_t GetExtensionIndex(const char *parameterName, OMX_INDEXTYPE *indexType);
53 
54     int32_t GetState(OMX_STATETYPE *state);
55 
56     int32_t ComponentTunnelRequest(uint32_t port, int32_t omxHandleTypeTunneledComp, uint32_t tunneledPort,
57                                    struct OMX_TUNNELSETUPTYPE *tunnelSetup);
58 
59     int32_t UseBuffer(uint32_t portIndex, struct OmxCodecBuffer &buffer);
60 
61     int32_t AllocateBuffer(uint32_t portIndex, struct OmxCodecBuffer &buffer);
62 
63     int32_t FreeBuffer(uint32_t portIndex, struct OmxCodecBuffer &buffer);
64 
65     int32_t EmptyThisBuffer(struct OmxCodecBuffer &buffer);
66 
67     int32_t FillThisBuffer(struct OmxCodecBuffer &buffer);
68 
69     int32_t SetCallbacks(struct CodecCallbackType *omxCallback, int64_t appData);
70 
71     int32_t UseEglImage(struct OmxCodecBuffer &buffer, uint32_t portIndex, int8_t *eglImage, uint32_t eglImageLen);
72 
73     int32_t ComponentRoleEnum(uint8_t *role, uint32_t roleLen, uint32_t index);
74 
75     int32_t DeInit();
76 
77     static OMX_ERRORTYPE OnEvent(OMX_HANDLETYPE component, void *appData, OMX_EVENTTYPE event, uint32_t data1,
78                                  uint32_t data2, void *eventData);
79 
80     static OMX_ERRORTYPE OnEmptyBufferDone(OMX_HANDLETYPE component, void *appData, OMX_BUFFERHEADERTYPE *buffer);
81 
82     static OMX_ERRORTYPE OnFillBufferDone(OMX_HANDLETYPE component, void *appData, OMX_BUFFERHEADERTYPE *buffer);
83 
SetHandle(OMX_HANDLETYPE comp)84     void SetHandle(OMX_HANDLETYPE comp)
85     {
86         this->comp_ = comp;
87     }
88 
GetHandle()89     OMX_HANDLETYPE GetHandle()
90     {
91         return comp_;
92     }
93 
GetBufferCount()94     uint32_t GetBufferCount()
95     {
96         return codecBufferMap_.size();
97     }
98 
GetCompName()99     std::string GetCompName()
100     {
101         return name_;
102     }
103 
104     void WaitStateChange(uint32_t objState, OMX_STATETYPE *status);
105 
106     void ReleaseOMXResource();
107 
108     int32_t ReleaseAllBuffer();
109 
110 public:
111     static OMX_CALLBACKTYPE callbacks_;  // callbacks
112 
113 private:
114     int32_t OnEvent(OMX_EVENTTYPE event, uint32_t data1, uint32_t data2, void *eventData);
115 
116     int32_t OnEmptyBufferDone(OMX_BUFFERHEADERTYPE *buffer);
117 
118     int32_t OnFillBufferDone(OMX_BUFFERHEADERTYPE *buffer);
119 
120     uint32_t GenerateBufferId();
121     sptr<ICodecBuffer> GetBufferInfoByHeader(OMX_BUFFERHEADERTYPE *buffer);
122     bool GetBufferById(uint32_t bufferId, sptr<ICodecBuffer> &codecBuffer, OMX_BUFFERHEADERTYPE *&bufferHdrType);
123 
124 private:
125     OMX_HANDLETYPE comp_;                                         // Component handle
126     struct CodecCallbackType *omxCallback_;                       // Callbacks in HDI
127     int64_t appData_;                                             // Use data, default is 0
128     std::map<uint32_t, sptr<ICodecBuffer>> codecBufferMap_;       // Key is buffferID
129     std::map<OMX_BUFFERHEADERTYPE *, uint32_t> bufferHeaderMap_;  // Key is omx buffer header type
130     uint32_t bufferIdCount_;
131     std::string name_;
132     std::map<OMX_BUFFERHEADERTYPE *, uint32_t> bufferHeaderPortMap_;
133     uint32_t maxStateWaitTime = 10000;
134     uint32_t maxStateWaitCount = 100;
135     std::shared_mutex mapMutex_;
136     std::shared_mutex callbackMutex_;
137 };
138 }  // namespace Omx
139 }  // namespace Codec
140 }  // namespace OHOS
141 #endif /* COMPONENT_NODE_H */